inetpeer: Add redirect and PMTU discovery cached info.
[deliverable/linux.git] / net / ipv6 / route.c
1 /*
2 * Linux INET6 implementation
3 * FIB front-end.
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14 /* Changes:
15 *
16 * YOSHIFUJI Hideaki @USAGI
17 * reworked default router selection.
18 * - respect outgoing interface
19 * - select from (probably) reachable routers (i.e.
20 * routers in REACHABLE, STALE, DELAY or PROBE states).
21 * - always select the same router if it is (probably)
22 * reachable. otherwise, round-robin the list.
23 * Ville Nuorvala
24 * Fixed routing subtrees.
25 */
26
27 #include <linux/capability.h>
28 #include <linux/errno.h>
29 #include <linux/types.h>
30 #include <linux/times.h>
31 #include <linux/socket.h>
32 #include <linux/sockios.h>
33 #include <linux/net.h>
34 #include <linux/route.h>
35 #include <linux/netdevice.h>
36 #include <linux/in6.h>
37 #include <linux/mroute6.h>
38 #include <linux/init.h>
39 #include <linux/if_arp.h>
40 #include <linux/proc_fs.h>
41 #include <linux/seq_file.h>
42 #include <linux/nsproxy.h>
43 #include <linux/slab.h>
44 #include <net/net_namespace.h>
45 #include <net/snmp.h>
46 #include <net/ipv6.h>
47 #include <net/ip6_fib.h>
48 #include <net/ip6_route.h>
49 #include <net/ndisc.h>
50 #include <net/addrconf.h>
51 #include <net/tcp.h>
52 #include <linux/rtnetlink.h>
53 #include <net/dst.h>
54 #include <net/xfrm.h>
55 #include <net/netevent.h>
56 #include <net/netlink.h>
57
58 #include <asm/uaccess.h>
59
60 #ifdef CONFIG_SYSCTL
61 #include <linux/sysctl.h>
62 #endif
63
64 /* Set to 3 to get tracing. */
65 #define RT6_DEBUG 2
66
67 #if RT6_DEBUG >= 3
68 #define RDBG(x) printk x
69 #define RT6_TRACE(x...) printk(KERN_DEBUG x)
70 #else
71 #define RDBG(x)
72 #define RT6_TRACE(x...) do { ; } while (0)
73 #endif
74
75 static struct rt6_info * ip6_rt_copy(struct rt6_info *ort);
76 static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
77 static unsigned int ip6_default_advmss(const struct dst_entry *dst);
78 static unsigned int ip6_default_mtu(const struct dst_entry *dst);
79 static struct dst_entry *ip6_negative_advice(struct dst_entry *);
80 static void ip6_dst_destroy(struct dst_entry *);
81 static void ip6_dst_ifdown(struct dst_entry *,
82 struct net_device *dev, int how);
83 static int ip6_dst_gc(struct dst_ops *ops);
84
85 static int ip6_pkt_discard(struct sk_buff *skb);
86 static int ip6_pkt_discard_out(struct sk_buff *skb);
87 static void ip6_link_failure(struct sk_buff *skb);
88 static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu);
89
90 #ifdef CONFIG_IPV6_ROUTE_INFO
91 static struct rt6_info *rt6_add_route_info(struct net *net,
92 struct in6_addr *prefix, int prefixlen,
93 struct in6_addr *gwaddr, int ifindex,
94 unsigned pref);
95 static struct rt6_info *rt6_get_route_info(struct net *net,
96 struct in6_addr *prefix, int prefixlen,
97 struct in6_addr *gwaddr, int ifindex);
98 #endif
99
100 static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old)
101 {
102 struct rt6_info *rt = (struct rt6_info *) dst;
103 struct inet_peer *peer;
104 u32 *p = NULL;
105
106 if (!rt->rt6i_peer)
107 rt6_bind_peer(rt, 1);
108
109 peer = rt->rt6i_peer;
110 if (peer) {
111 u32 *old_p = __DST_METRICS_PTR(old);
112 unsigned long prev, new;
113
114 p = peer->metrics;
115 if (inet_metrics_new(peer))
116 memcpy(p, old_p, sizeof(u32) * RTAX_MAX);
117
118 new = (unsigned long) p;
119 prev = cmpxchg(&dst->_metrics, old, new);
120
121 if (prev != old) {
122 p = __DST_METRICS_PTR(prev);
123 if (prev & DST_METRICS_READ_ONLY)
124 p = NULL;
125 }
126 }
127 return p;
128 }
129
130 static struct dst_ops ip6_dst_ops_template = {
131 .family = AF_INET6,
132 .protocol = cpu_to_be16(ETH_P_IPV6),
133 .gc = ip6_dst_gc,
134 .gc_thresh = 1024,
135 .check = ip6_dst_check,
136 .default_advmss = ip6_default_advmss,
137 .default_mtu = ip6_default_mtu,
138 .cow_metrics = ipv6_cow_metrics,
139 .destroy = ip6_dst_destroy,
140 .ifdown = ip6_dst_ifdown,
141 .negative_advice = ip6_negative_advice,
142 .link_failure = ip6_link_failure,
143 .update_pmtu = ip6_rt_update_pmtu,
144 .local_out = __ip6_local_out,
145 };
146
147 static unsigned int ip6_blackhole_default_mtu(const struct dst_entry *dst)
148 {
149 return 0;
150 }
151
152 static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu)
153 {
154 }
155
156 static struct dst_ops ip6_dst_blackhole_ops = {
157 .family = AF_INET6,
158 .protocol = cpu_to_be16(ETH_P_IPV6),
159 .destroy = ip6_dst_destroy,
160 .check = ip6_dst_check,
161 .default_mtu = ip6_blackhole_default_mtu,
162 .update_pmtu = ip6_rt_blackhole_update_pmtu,
163 };
164
165 static const u32 ip6_template_metrics[RTAX_MAX] = {
166 [RTAX_HOPLIMIT - 1] = 255,
167 };
168
169 static struct rt6_info ip6_null_entry_template = {
170 .dst = {
171 .__refcnt = ATOMIC_INIT(1),
172 .__use = 1,
173 .obsolete = -1,
174 .error = -ENETUNREACH,
175 .input = ip6_pkt_discard,
176 .output = ip6_pkt_discard_out,
177 },
178 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
179 .rt6i_protocol = RTPROT_KERNEL,
180 .rt6i_metric = ~(u32) 0,
181 .rt6i_ref = ATOMIC_INIT(1),
182 };
183
184 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
185
186 static int ip6_pkt_prohibit(struct sk_buff *skb);
187 static int ip6_pkt_prohibit_out(struct sk_buff *skb);
188
189 static struct rt6_info ip6_prohibit_entry_template = {
190 .dst = {
191 .__refcnt = ATOMIC_INIT(1),
192 .__use = 1,
193 .obsolete = -1,
194 .error = -EACCES,
195 .input = ip6_pkt_prohibit,
196 .output = ip6_pkt_prohibit_out,
197 },
198 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
199 .rt6i_protocol = RTPROT_KERNEL,
200 .rt6i_metric = ~(u32) 0,
201 .rt6i_ref = ATOMIC_INIT(1),
202 };
203
204 static struct rt6_info ip6_blk_hole_entry_template = {
205 .dst = {
206 .__refcnt = ATOMIC_INIT(1),
207 .__use = 1,
208 .obsolete = -1,
209 .error = -EINVAL,
210 .input = dst_discard,
211 .output = dst_discard,
212 },
213 .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
214 .rt6i_protocol = RTPROT_KERNEL,
215 .rt6i_metric = ~(u32) 0,
216 .rt6i_ref = ATOMIC_INIT(1),
217 };
218
219 #endif
220
221 /* allocate dst with ip6_dst_ops */
222 static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops)
223 {
224 return (struct rt6_info *)dst_alloc(ops);
225 }
226
227 static void ip6_dst_destroy(struct dst_entry *dst)
228 {
229 struct rt6_info *rt = (struct rt6_info *)dst;
230 struct inet6_dev *idev = rt->rt6i_idev;
231 struct inet_peer *peer = rt->rt6i_peer;
232
233 if (idev != NULL) {
234 rt->rt6i_idev = NULL;
235 in6_dev_put(idev);
236 }
237 if (peer) {
238 rt->rt6i_peer = NULL;
239 inet_putpeer(peer);
240 }
241 }
242
243 void rt6_bind_peer(struct rt6_info *rt, int create)
244 {
245 struct inet_peer *peer;
246
247 peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create);
248 if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL)
249 inet_putpeer(peer);
250 }
251
252 static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
253 int how)
254 {
255 struct rt6_info *rt = (struct rt6_info *)dst;
256 struct inet6_dev *idev = rt->rt6i_idev;
257 struct net_device *loopback_dev =
258 dev_net(dev)->loopback_dev;
259
260 if (dev != loopback_dev && idev != NULL && idev->dev == dev) {
261 struct inet6_dev *loopback_idev =
262 in6_dev_get(loopback_dev);
263 if (loopback_idev != NULL) {
264 rt->rt6i_idev = loopback_idev;
265 in6_dev_put(idev);
266 }
267 }
268 }
269
270 static __inline__ int rt6_check_expired(const struct rt6_info *rt)
271 {
272 return (rt->rt6i_flags & RTF_EXPIRES) &&
273 time_after(jiffies, rt->rt6i_expires);
274 }
275
276 static inline int rt6_need_strict(struct in6_addr *daddr)
277 {
278 return ipv6_addr_type(daddr) &
279 (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK);
280 }
281
282 /*
283 * Route lookup. Any table->tb6_lock is implied.
284 */
285
286 static inline struct rt6_info *rt6_device_match(struct net *net,
287 struct rt6_info *rt,
288 struct in6_addr *saddr,
289 int oif,
290 int flags)
291 {
292 struct rt6_info *local = NULL;
293 struct rt6_info *sprt;
294
295 if (!oif && ipv6_addr_any(saddr))
296 goto out;
297
298 for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) {
299 struct net_device *dev = sprt->rt6i_dev;
300
301 if (oif) {
302 if (dev->ifindex == oif)
303 return sprt;
304 if (dev->flags & IFF_LOOPBACK) {
305 if (sprt->rt6i_idev == NULL ||
306 sprt->rt6i_idev->dev->ifindex != oif) {
307 if (flags & RT6_LOOKUP_F_IFACE && oif)
308 continue;
309 if (local && (!oif ||
310 local->rt6i_idev->dev->ifindex == oif))
311 continue;
312 }
313 local = sprt;
314 }
315 } else {
316 if (ipv6_chk_addr(net, saddr, dev,
317 flags & RT6_LOOKUP_F_IFACE))
318 return sprt;
319 }
320 }
321
322 if (oif) {
323 if (local)
324 return local;
325
326 if (flags & RT6_LOOKUP_F_IFACE)
327 return net->ipv6.ip6_null_entry;
328 }
329 out:
330 return rt;
331 }
332
333 #ifdef CONFIG_IPV6_ROUTER_PREF
334 static void rt6_probe(struct rt6_info *rt)
335 {
336 struct neighbour *neigh = rt ? rt->rt6i_nexthop : NULL;
337 /*
338 * Okay, this does not seem to be appropriate
339 * for now, however, we need to check if it
340 * is really so; aka Router Reachability Probing.
341 *
342 * Router Reachability Probe MUST be rate-limited
343 * to no more than one per minute.
344 */
345 if (!neigh || (neigh->nud_state & NUD_VALID))
346 return;
347 read_lock_bh(&neigh->lock);
348 if (!(neigh->nud_state & NUD_VALID) &&
349 time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) {
350 struct in6_addr mcaddr;
351 struct in6_addr *target;
352
353 neigh->updated = jiffies;
354 read_unlock_bh(&neigh->lock);
355
356 target = (struct in6_addr *)&neigh->primary_key;
357 addrconf_addr_solict_mult(target, &mcaddr);
358 ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL);
359 } else
360 read_unlock_bh(&neigh->lock);
361 }
362 #else
363 static inline void rt6_probe(struct rt6_info *rt)
364 {
365 }
366 #endif
367
368 /*
369 * Default Router Selection (RFC 2461 6.3.6)
370 */
371 static inline int rt6_check_dev(struct rt6_info *rt, int oif)
372 {
373 struct net_device *dev = rt->rt6i_dev;
374 if (!oif || dev->ifindex == oif)
375 return 2;
376 if ((dev->flags & IFF_LOOPBACK) &&
377 rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif)
378 return 1;
379 return 0;
380 }
381
382 static inline int rt6_check_neigh(struct rt6_info *rt)
383 {
384 struct neighbour *neigh = rt->rt6i_nexthop;
385 int m;
386 if (rt->rt6i_flags & RTF_NONEXTHOP ||
387 !(rt->rt6i_flags & RTF_GATEWAY))
388 m = 1;
389 else if (neigh) {
390 read_lock_bh(&neigh->lock);
391 if (neigh->nud_state & NUD_VALID)
392 m = 2;
393 #ifdef CONFIG_IPV6_ROUTER_PREF
394 else if (neigh->nud_state & NUD_FAILED)
395 m = 0;
396 #endif
397 else
398 m = 1;
399 read_unlock_bh(&neigh->lock);
400 } else
401 m = 0;
402 return m;
403 }
404
405 static int rt6_score_route(struct rt6_info *rt, int oif,
406 int strict)
407 {
408 int m, n;
409
410 m = rt6_check_dev(rt, oif);
411 if (!m && (strict & RT6_LOOKUP_F_IFACE))
412 return -1;
413 #ifdef CONFIG_IPV6_ROUTER_PREF
414 m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2;
415 #endif
416 n = rt6_check_neigh(rt);
417 if (!n && (strict & RT6_LOOKUP_F_REACHABLE))
418 return -1;
419 return m;
420 }
421
422 static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
423 int *mpri, struct rt6_info *match)
424 {
425 int m;
426
427 if (rt6_check_expired(rt))
428 goto out;
429
430 m = rt6_score_route(rt, oif, strict);
431 if (m < 0)
432 goto out;
433
434 if (m > *mpri) {
435 if (strict & RT6_LOOKUP_F_REACHABLE)
436 rt6_probe(match);
437 *mpri = m;
438 match = rt;
439 } else if (strict & RT6_LOOKUP_F_REACHABLE) {
440 rt6_probe(rt);
441 }
442
443 out:
444 return match;
445 }
446
447 static struct rt6_info *find_rr_leaf(struct fib6_node *fn,
448 struct rt6_info *rr_head,
449 u32 metric, int oif, int strict)
450 {
451 struct rt6_info *rt, *match;
452 int mpri = -1;
453
454 match = NULL;
455 for (rt = rr_head; rt && rt->rt6i_metric == metric;
456 rt = rt->dst.rt6_next)
457 match = find_match(rt, oif, strict, &mpri, match);
458 for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric;
459 rt = rt->dst.rt6_next)
460 match = find_match(rt, oif, strict, &mpri, match);
461
462 return match;
463 }
464
465 static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict)
466 {
467 struct rt6_info *match, *rt0;
468 struct net *net;
469
470 RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n",
471 __func__, fn->leaf, oif);
472
473 rt0 = fn->rr_ptr;
474 if (!rt0)
475 fn->rr_ptr = rt0 = fn->leaf;
476
477 match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict);
478
479 if (!match &&
480 (strict & RT6_LOOKUP_F_REACHABLE)) {
481 struct rt6_info *next = rt0->dst.rt6_next;
482
483 /* no entries matched; do round-robin */
484 if (!next || next->rt6i_metric != rt0->rt6i_metric)
485 next = fn->leaf;
486
487 if (next != rt0)
488 fn->rr_ptr = next;
489 }
490
491 RT6_TRACE("%s() => %p\n",
492 __func__, match);
493
494 net = dev_net(rt0->rt6i_dev);
495 return match ? match : net->ipv6.ip6_null_entry;
496 }
497
498 #ifdef CONFIG_IPV6_ROUTE_INFO
499 int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
500 struct in6_addr *gwaddr)
501 {
502 struct net *net = dev_net(dev);
503 struct route_info *rinfo = (struct route_info *) opt;
504 struct in6_addr prefix_buf, *prefix;
505 unsigned int pref;
506 unsigned long lifetime;
507 struct rt6_info *rt;
508
509 if (len < sizeof(struct route_info)) {
510 return -EINVAL;
511 }
512
513 /* Sanity check for prefix_len and length */
514 if (rinfo->length > 3) {
515 return -EINVAL;
516 } else if (rinfo->prefix_len > 128) {
517 return -EINVAL;
518 } else if (rinfo->prefix_len > 64) {
519 if (rinfo->length < 2) {
520 return -EINVAL;
521 }
522 } else if (rinfo->prefix_len > 0) {
523 if (rinfo->length < 1) {
524 return -EINVAL;
525 }
526 }
527
528 pref = rinfo->route_pref;
529 if (pref == ICMPV6_ROUTER_PREF_INVALID)
530 return -EINVAL;
531
532 lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ);
533
534 if (rinfo->length == 3)
535 prefix = (struct in6_addr *)rinfo->prefix;
536 else {
537 /* this function is safe */
538 ipv6_addr_prefix(&prefix_buf,
539 (struct in6_addr *)rinfo->prefix,
540 rinfo->prefix_len);
541 prefix = &prefix_buf;
542 }
543
544 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr,
545 dev->ifindex);
546
547 if (rt && !lifetime) {
548 ip6_del_rt(rt);
549 rt = NULL;
550 }
551
552 if (!rt && lifetime)
553 rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex,
554 pref);
555 else if (rt)
556 rt->rt6i_flags = RTF_ROUTEINFO |
557 (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref);
558
559 if (rt) {
560 if (!addrconf_finite_timeout(lifetime)) {
561 rt->rt6i_flags &= ~RTF_EXPIRES;
562 } else {
563 rt->rt6i_expires = jiffies + HZ * lifetime;
564 rt->rt6i_flags |= RTF_EXPIRES;
565 }
566 dst_release(&rt->dst);
567 }
568 return 0;
569 }
570 #endif
571
572 #define BACKTRACK(__net, saddr) \
573 do { \
574 if (rt == __net->ipv6.ip6_null_entry) { \
575 struct fib6_node *pn; \
576 while (1) { \
577 if (fn->fn_flags & RTN_TL_ROOT) \
578 goto out; \
579 pn = fn->parent; \
580 if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \
581 fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \
582 else \
583 fn = pn; \
584 if (fn->fn_flags & RTN_RTINFO) \
585 goto restart; \
586 } \
587 } \
588 } while(0)
589
590 static struct rt6_info *ip6_pol_route_lookup(struct net *net,
591 struct fib6_table *table,
592 struct flowi *fl, int flags)
593 {
594 struct fib6_node *fn;
595 struct rt6_info *rt;
596
597 read_lock_bh(&table->tb6_lock);
598 fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
599 restart:
600 rt = fn->leaf;
601 rt = rt6_device_match(net, rt, &fl->fl6_src, fl->oif, flags);
602 BACKTRACK(net, &fl->fl6_src);
603 out:
604 dst_use(&rt->dst, jiffies);
605 read_unlock_bh(&table->tb6_lock);
606 return rt;
607
608 }
609
610 struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr,
611 const struct in6_addr *saddr, int oif, int strict)
612 {
613 struct flowi fl = {
614 .oif = oif,
615 .fl6_dst = *daddr,
616 };
617 struct dst_entry *dst;
618 int flags = strict ? RT6_LOOKUP_F_IFACE : 0;
619
620 if (saddr) {
621 memcpy(&fl.fl6_src, saddr, sizeof(*saddr));
622 flags |= RT6_LOOKUP_F_HAS_SADDR;
623 }
624
625 dst = fib6_rule_lookup(net, &fl, flags, ip6_pol_route_lookup);
626 if (dst->error == 0)
627 return (struct rt6_info *) dst;
628
629 dst_release(dst);
630
631 return NULL;
632 }
633
634 EXPORT_SYMBOL(rt6_lookup);
635
636 /* ip6_ins_rt is called with FREE table->tb6_lock.
637 It takes new route entry, the addition fails by any reason the
638 route is freed. In any case, if caller does not hold it, it may
639 be destroyed.
640 */
641
642 static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info)
643 {
644 int err;
645 struct fib6_table *table;
646
647 table = rt->rt6i_table;
648 write_lock_bh(&table->tb6_lock);
649 err = fib6_add(&table->tb6_root, rt, info);
650 write_unlock_bh(&table->tb6_lock);
651
652 return err;
653 }
654
655 int ip6_ins_rt(struct rt6_info *rt)
656 {
657 struct nl_info info = {
658 .nl_net = dev_net(rt->rt6i_dev),
659 };
660 return __ip6_ins_rt(rt, &info);
661 }
662
663 static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, struct in6_addr *daddr,
664 struct in6_addr *saddr)
665 {
666 struct rt6_info *rt;
667
668 /*
669 * Clone the route.
670 */
671
672 rt = ip6_rt_copy(ort);
673
674 if (rt) {
675 struct neighbour *neigh;
676 int attempts = !in_softirq();
677
678 if (!(rt->rt6i_flags&RTF_GATEWAY)) {
679 if (rt->rt6i_dst.plen != 128 &&
680 ipv6_addr_equal(&rt->rt6i_dst.addr, daddr))
681 rt->rt6i_flags |= RTF_ANYCAST;
682 ipv6_addr_copy(&rt->rt6i_gateway, daddr);
683 }
684
685 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
686 rt->rt6i_dst.plen = 128;
687 rt->rt6i_flags |= RTF_CACHE;
688 rt->dst.flags |= DST_HOST;
689
690 #ifdef CONFIG_IPV6_SUBTREES
691 if (rt->rt6i_src.plen && saddr) {
692 ipv6_addr_copy(&rt->rt6i_src.addr, saddr);
693 rt->rt6i_src.plen = 128;
694 }
695 #endif
696
697 retry:
698 neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
699 if (IS_ERR(neigh)) {
700 struct net *net = dev_net(rt->rt6i_dev);
701 int saved_rt_min_interval =
702 net->ipv6.sysctl.ip6_rt_gc_min_interval;
703 int saved_rt_elasticity =
704 net->ipv6.sysctl.ip6_rt_gc_elasticity;
705
706 if (attempts-- > 0) {
707 net->ipv6.sysctl.ip6_rt_gc_elasticity = 1;
708 net->ipv6.sysctl.ip6_rt_gc_min_interval = 0;
709
710 ip6_dst_gc(&net->ipv6.ip6_dst_ops);
711
712 net->ipv6.sysctl.ip6_rt_gc_elasticity =
713 saved_rt_elasticity;
714 net->ipv6.sysctl.ip6_rt_gc_min_interval =
715 saved_rt_min_interval;
716 goto retry;
717 }
718
719 if (net_ratelimit())
720 printk(KERN_WARNING
721 "ipv6: Neighbour table overflow.\n");
722 dst_free(&rt->dst);
723 return NULL;
724 }
725 rt->rt6i_nexthop = neigh;
726
727 }
728
729 return rt;
730 }
731
732 static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, struct in6_addr *daddr)
733 {
734 struct rt6_info *rt = ip6_rt_copy(ort);
735 if (rt) {
736 ipv6_addr_copy(&rt->rt6i_dst.addr, daddr);
737 rt->rt6i_dst.plen = 128;
738 rt->rt6i_flags |= RTF_CACHE;
739 rt->dst.flags |= DST_HOST;
740 rt->rt6i_nexthop = neigh_clone(ort->rt6i_nexthop);
741 }
742 return rt;
743 }
744
745 static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif,
746 struct flowi *fl, int flags)
747 {
748 struct fib6_node *fn;
749 struct rt6_info *rt, *nrt;
750 int strict = 0;
751 int attempts = 3;
752 int err;
753 int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE;
754
755 strict |= flags & RT6_LOOKUP_F_IFACE;
756
757 relookup:
758 read_lock_bh(&table->tb6_lock);
759
760 restart_2:
761 fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
762
763 restart:
764 rt = rt6_select(fn, oif, strict | reachable);
765
766 BACKTRACK(net, &fl->fl6_src);
767 if (rt == net->ipv6.ip6_null_entry ||
768 rt->rt6i_flags & RTF_CACHE)
769 goto out;
770
771 dst_hold(&rt->dst);
772 read_unlock_bh(&table->tb6_lock);
773
774 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
775 nrt = rt6_alloc_cow(rt, &fl->fl6_dst, &fl->fl6_src);
776 else
777 nrt = rt6_alloc_clone(rt, &fl->fl6_dst);
778
779 dst_release(&rt->dst);
780 rt = nrt ? : net->ipv6.ip6_null_entry;
781
782 dst_hold(&rt->dst);
783 if (nrt) {
784 err = ip6_ins_rt(nrt);
785 if (!err)
786 goto out2;
787 }
788
789 if (--attempts <= 0)
790 goto out2;
791
792 /*
793 * Race condition! In the gap, when table->tb6_lock was
794 * released someone could insert this route. Relookup.
795 */
796 dst_release(&rt->dst);
797 goto relookup;
798
799 out:
800 if (reachable) {
801 reachable = 0;
802 goto restart_2;
803 }
804 dst_hold(&rt->dst);
805 read_unlock_bh(&table->tb6_lock);
806 out2:
807 rt->dst.lastuse = jiffies;
808 rt->dst.__use++;
809
810 return rt;
811 }
812
813 static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table,
814 struct flowi *fl, int flags)
815 {
816 return ip6_pol_route(net, table, fl->iif, fl, flags);
817 }
818
819 void ip6_route_input(struct sk_buff *skb)
820 {
821 struct ipv6hdr *iph = ipv6_hdr(skb);
822 struct net *net = dev_net(skb->dev);
823 int flags = RT6_LOOKUP_F_HAS_SADDR;
824 struct flowi fl = {
825 .iif = skb->dev->ifindex,
826 .fl6_dst = iph->daddr,
827 .fl6_src = iph->saddr,
828 .fl6_flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK,
829 .mark = skb->mark,
830 .proto = iph->nexthdr,
831 };
832
833 if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG)
834 flags |= RT6_LOOKUP_F_IFACE;
835
836 skb_dst_set(skb, fib6_rule_lookup(net, &fl, flags, ip6_pol_route_input));
837 }
838
839 static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table,
840 struct flowi *fl, int flags)
841 {
842 return ip6_pol_route(net, table, fl->oif, fl, flags);
843 }
844
845 struct dst_entry * ip6_route_output(struct net *net, struct sock *sk,
846 struct flowi *fl)
847 {
848 int flags = 0;
849
850 if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl->fl6_dst))
851 flags |= RT6_LOOKUP_F_IFACE;
852
853 if (!ipv6_addr_any(&fl->fl6_src))
854 flags |= RT6_LOOKUP_F_HAS_SADDR;
855 else if (sk)
856 flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs);
857
858 return fib6_rule_lookup(net, fl, flags, ip6_pol_route_output);
859 }
860
861 EXPORT_SYMBOL(ip6_route_output);
862
863 int ip6_dst_blackhole(struct sock *sk, struct dst_entry **dstp, struct flowi *fl)
864 {
865 struct rt6_info *ort = (struct rt6_info *) *dstp;
866 struct rt6_info *rt = (struct rt6_info *)
867 dst_alloc(&ip6_dst_blackhole_ops);
868 struct dst_entry *new = NULL;
869
870 if (rt) {
871 new = &rt->dst;
872
873 atomic_set(&new->__refcnt, 1);
874 new->__use = 1;
875 new->input = dst_discard;
876 new->output = dst_discard;
877
878 dst_copy_metrics(new, &ort->dst);
879 new->dev = ort->dst.dev;
880 if (new->dev)
881 dev_hold(new->dev);
882 rt->rt6i_idev = ort->rt6i_idev;
883 if (rt->rt6i_idev)
884 in6_dev_hold(rt->rt6i_idev);
885 rt->rt6i_expires = 0;
886
887 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
888 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
889 rt->rt6i_metric = 0;
890
891 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
892 #ifdef CONFIG_IPV6_SUBTREES
893 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
894 #endif
895
896 dst_free(new);
897 }
898
899 dst_release(*dstp);
900 *dstp = new;
901 return new ? 0 : -ENOMEM;
902 }
903 EXPORT_SYMBOL_GPL(ip6_dst_blackhole);
904
905 /*
906 * Destination cache support functions
907 */
908
909 static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
910 {
911 struct rt6_info *rt;
912
913 rt = (struct rt6_info *) dst;
914
915 if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie))
916 return dst;
917
918 return NULL;
919 }
920
921 static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
922 {
923 struct rt6_info *rt = (struct rt6_info *) dst;
924
925 if (rt) {
926 if (rt->rt6i_flags & RTF_CACHE) {
927 if (rt6_check_expired(rt)) {
928 ip6_del_rt(rt);
929 dst = NULL;
930 }
931 } else {
932 dst_release(dst);
933 dst = NULL;
934 }
935 }
936 return dst;
937 }
938
939 static void ip6_link_failure(struct sk_buff *skb)
940 {
941 struct rt6_info *rt;
942
943 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
944
945 rt = (struct rt6_info *) skb_dst(skb);
946 if (rt) {
947 if (rt->rt6i_flags&RTF_CACHE) {
948 dst_set_expires(&rt->dst, 0);
949 rt->rt6i_flags |= RTF_EXPIRES;
950 } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT))
951 rt->rt6i_node->fn_sernum = -1;
952 }
953 }
954
955 static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu)
956 {
957 struct rt6_info *rt6 = (struct rt6_info*)dst;
958
959 if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) {
960 rt6->rt6i_flags |= RTF_MODIFIED;
961 if (mtu < IPV6_MIN_MTU) {
962 u32 features = dst_metric(dst, RTAX_FEATURES);
963 mtu = IPV6_MIN_MTU;
964 features |= RTAX_FEATURE_ALLFRAG;
965 dst_metric_set(dst, RTAX_FEATURES, features);
966 }
967 dst_metric_set(dst, RTAX_MTU, mtu);
968 }
969 }
970
971 static unsigned int ip6_default_advmss(const struct dst_entry *dst)
972 {
973 struct net_device *dev = dst->dev;
974 unsigned int mtu = dst_mtu(dst);
975 struct net *net = dev_net(dev);
976
977 mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr);
978
979 if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss)
980 mtu = net->ipv6.sysctl.ip6_rt_min_advmss;
981
982 /*
983 * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and
984 * corresponding MSS is IPV6_MAXPLEN - tcp_header_size.
985 * IPV6_MAXPLEN is also valid and means: "any MSS,
986 * rely only on pmtu discovery"
987 */
988 if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr))
989 mtu = IPV6_MAXPLEN;
990 return mtu;
991 }
992
993 static unsigned int ip6_default_mtu(const struct dst_entry *dst)
994 {
995 unsigned int mtu = IPV6_MIN_MTU;
996 struct inet6_dev *idev;
997
998 rcu_read_lock();
999 idev = __in6_dev_get(dst->dev);
1000 if (idev)
1001 mtu = idev->cnf.mtu6;
1002 rcu_read_unlock();
1003
1004 return mtu;
1005 }
1006
1007 static struct dst_entry *icmp6_dst_gc_list;
1008 static DEFINE_SPINLOCK(icmp6_dst_lock);
1009
1010 struct dst_entry *icmp6_dst_alloc(struct net_device *dev,
1011 struct neighbour *neigh,
1012 const struct in6_addr *addr)
1013 {
1014 struct rt6_info *rt;
1015 struct inet6_dev *idev = in6_dev_get(dev);
1016 struct net *net = dev_net(dev);
1017
1018 if (unlikely(idev == NULL))
1019 return NULL;
1020
1021 rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
1022 if (unlikely(rt == NULL)) {
1023 in6_dev_put(idev);
1024 goto out;
1025 }
1026
1027 dev_hold(dev);
1028 if (neigh)
1029 neigh_hold(neigh);
1030 else {
1031 neigh = ndisc_get_neigh(dev, addr);
1032 if (IS_ERR(neigh))
1033 neigh = NULL;
1034 }
1035
1036 rt->rt6i_dev = dev;
1037 rt->rt6i_idev = idev;
1038 rt->rt6i_nexthop = neigh;
1039 atomic_set(&rt->dst.__refcnt, 1);
1040 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255);
1041 rt->dst.output = ip6_output;
1042
1043 #if 0 /* there's no chance to use these for ndisc */
1044 rt->dst.flags = ipv6_addr_type(addr) & IPV6_ADDR_UNICAST
1045 ? DST_HOST
1046 : 0;
1047 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
1048 rt->rt6i_dst.plen = 128;
1049 #endif
1050
1051 spin_lock_bh(&icmp6_dst_lock);
1052 rt->dst.next = icmp6_dst_gc_list;
1053 icmp6_dst_gc_list = &rt->dst;
1054 spin_unlock_bh(&icmp6_dst_lock);
1055
1056 fib6_force_start_gc(net);
1057
1058 out:
1059 return &rt->dst;
1060 }
1061
1062 int icmp6_dst_gc(void)
1063 {
1064 struct dst_entry *dst, *next, **pprev;
1065 int more = 0;
1066
1067 next = NULL;
1068
1069 spin_lock_bh(&icmp6_dst_lock);
1070 pprev = &icmp6_dst_gc_list;
1071
1072 while ((dst = *pprev) != NULL) {
1073 if (!atomic_read(&dst->__refcnt)) {
1074 *pprev = dst->next;
1075 dst_free(dst);
1076 } else {
1077 pprev = &dst->next;
1078 ++more;
1079 }
1080 }
1081
1082 spin_unlock_bh(&icmp6_dst_lock);
1083
1084 return more;
1085 }
1086
1087 static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg),
1088 void *arg)
1089 {
1090 struct dst_entry *dst, **pprev;
1091
1092 spin_lock_bh(&icmp6_dst_lock);
1093 pprev = &icmp6_dst_gc_list;
1094 while ((dst = *pprev) != NULL) {
1095 struct rt6_info *rt = (struct rt6_info *) dst;
1096 if (func(rt, arg)) {
1097 *pprev = dst->next;
1098 dst_free(dst);
1099 } else {
1100 pprev = &dst->next;
1101 }
1102 }
1103 spin_unlock_bh(&icmp6_dst_lock);
1104 }
1105
1106 static int ip6_dst_gc(struct dst_ops *ops)
1107 {
1108 unsigned long now = jiffies;
1109 struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops);
1110 int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval;
1111 int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size;
1112 int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity;
1113 int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout;
1114 unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc;
1115 int entries;
1116
1117 entries = dst_entries_get_fast(ops);
1118 if (time_after(rt_last_gc + rt_min_interval, now) &&
1119 entries <= rt_max_size)
1120 goto out;
1121
1122 net->ipv6.ip6_rt_gc_expire++;
1123 fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net);
1124 net->ipv6.ip6_rt_last_gc = now;
1125 entries = dst_entries_get_slow(ops);
1126 if (entries < ops->gc_thresh)
1127 net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1;
1128 out:
1129 net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity;
1130 return entries > rt_max_size;
1131 }
1132
1133 /* Clean host part of a prefix. Not necessary in radix tree,
1134 but results in cleaner routing tables.
1135
1136 Remove it only when all the things will work!
1137 */
1138
1139 int ip6_dst_hoplimit(struct dst_entry *dst)
1140 {
1141 int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT);
1142 if (hoplimit == 0) {
1143 struct net_device *dev = dst->dev;
1144 struct inet6_dev *idev;
1145
1146 rcu_read_lock();
1147 idev = __in6_dev_get(dev);
1148 if (idev)
1149 hoplimit = idev->cnf.hop_limit;
1150 else
1151 hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit;
1152 rcu_read_unlock();
1153 }
1154 return hoplimit;
1155 }
1156 EXPORT_SYMBOL(ip6_dst_hoplimit);
1157
1158 /*
1159 *
1160 */
1161
1162 int ip6_route_add(struct fib6_config *cfg)
1163 {
1164 int err;
1165 struct net *net = cfg->fc_nlinfo.nl_net;
1166 struct rt6_info *rt = NULL;
1167 struct net_device *dev = NULL;
1168 struct inet6_dev *idev = NULL;
1169 struct fib6_table *table;
1170 int addr_type;
1171
1172 if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128)
1173 return -EINVAL;
1174 #ifndef CONFIG_IPV6_SUBTREES
1175 if (cfg->fc_src_len)
1176 return -EINVAL;
1177 #endif
1178 if (cfg->fc_ifindex) {
1179 err = -ENODEV;
1180 dev = dev_get_by_index(net, cfg->fc_ifindex);
1181 if (!dev)
1182 goto out;
1183 idev = in6_dev_get(dev);
1184 if (!idev)
1185 goto out;
1186 }
1187
1188 if (cfg->fc_metric == 0)
1189 cfg->fc_metric = IP6_RT_PRIO_USER;
1190
1191 table = fib6_new_table(net, cfg->fc_table);
1192 if (table == NULL) {
1193 err = -ENOBUFS;
1194 goto out;
1195 }
1196
1197 rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
1198
1199 if (rt == NULL) {
1200 err = -ENOMEM;
1201 goto out;
1202 }
1203
1204 rt->dst.obsolete = -1;
1205 rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ?
1206 jiffies + clock_t_to_jiffies(cfg->fc_expires) :
1207 0;
1208
1209 if (cfg->fc_protocol == RTPROT_UNSPEC)
1210 cfg->fc_protocol = RTPROT_BOOT;
1211 rt->rt6i_protocol = cfg->fc_protocol;
1212
1213 addr_type = ipv6_addr_type(&cfg->fc_dst);
1214
1215 if (addr_type & IPV6_ADDR_MULTICAST)
1216 rt->dst.input = ip6_mc_input;
1217 else if (cfg->fc_flags & RTF_LOCAL)
1218 rt->dst.input = ip6_input;
1219 else
1220 rt->dst.input = ip6_forward;
1221
1222 rt->dst.output = ip6_output;
1223
1224 ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
1225 rt->rt6i_dst.plen = cfg->fc_dst_len;
1226 if (rt->rt6i_dst.plen == 128)
1227 rt->dst.flags = DST_HOST;
1228
1229 #ifdef CONFIG_IPV6_SUBTREES
1230 ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len);
1231 rt->rt6i_src.plen = cfg->fc_src_len;
1232 #endif
1233
1234 rt->rt6i_metric = cfg->fc_metric;
1235
1236 /* We cannot add true routes via loopback here,
1237 they would result in kernel looping; promote them to reject routes
1238 */
1239 if ((cfg->fc_flags & RTF_REJECT) ||
1240 (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK)
1241 && !(cfg->fc_flags&RTF_LOCAL))) {
1242 /* hold loopback dev/idev if we haven't done so. */
1243 if (dev != net->loopback_dev) {
1244 if (dev) {
1245 dev_put(dev);
1246 in6_dev_put(idev);
1247 }
1248 dev = net->loopback_dev;
1249 dev_hold(dev);
1250 idev = in6_dev_get(dev);
1251 if (!idev) {
1252 err = -ENODEV;
1253 goto out;
1254 }
1255 }
1256 rt->dst.output = ip6_pkt_discard_out;
1257 rt->dst.input = ip6_pkt_discard;
1258 rt->dst.error = -ENETUNREACH;
1259 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1260 goto install_route;
1261 }
1262
1263 if (cfg->fc_flags & RTF_GATEWAY) {
1264 struct in6_addr *gw_addr;
1265 int gwa_type;
1266
1267 gw_addr = &cfg->fc_gateway;
1268 ipv6_addr_copy(&rt->rt6i_gateway, gw_addr);
1269 gwa_type = ipv6_addr_type(gw_addr);
1270
1271 if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) {
1272 struct rt6_info *grt;
1273
1274 /* IPv6 strictly inhibits using not link-local
1275 addresses as nexthop address.
1276 Otherwise, router will not able to send redirects.
1277 It is very good, but in some (rare!) circumstances
1278 (SIT, PtP, NBMA NOARP links) it is handy to allow
1279 some exceptions. --ANK
1280 */
1281 err = -EINVAL;
1282 if (!(gwa_type&IPV6_ADDR_UNICAST))
1283 goto out;
1284
1285 grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1);
1286
1287 err = -EHOSTUNREACH;
1288 if (grt == NULL)
1289 goto out;
1290 if (dev) {
1291 if (dev != grt->rt6i_dev) {
1292 dst_release(&grt->dst);
1293 goto out;
1294 }
1295 } else {
1296 dev = grt->rt6i_dev;
1297 idev = grt->rt6i_idev;
1298 dev_hold(dev);
1299 in6_dev_hold(grt->rt6i_idev);
1300 }
1301 if (!(grt->rt6i_flags&RTF_GATEWAY))
1302 err = 0;
1303 dst_release(&grt->dst);
1304
1305 if (err)
1306 goto out;
1307 }
1308 err = -EINVAL;
1309 if (dev == NULL || (dev->flags&IFF_LOOPBACK))
1310 goto out;
1311 }
1312
1313 err = -ENODEV;
1314 if (dev == NULL)
1315 goto out;
1316
1317 if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) {
1318 rt->rt6i_nexthop = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev);
1319 if (IS_ERR(rt->rt6i_nexthop)) {
1320 err = PTR_ERR(rt->rt6i_nexthop);
1321 rt->rt6i_nexthop = NULL;
1322 goto out;
1323 }
1324 }
1325
1326 rt->rt6i_flags = cfg->fc_flags;
1327
1328 install_route:
1329 if (cfg->fc_mx) {
1330 struct nlattr *nla;
1331 int remaining;
1332
1333 nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) {
1334 int type = nla_type(nla);
1335
1336 if (type) {
1337 if (type > RTAX_MAX) {
1338 err = -EINVAL;
1339 goto out;
1340 }
1341
1342 dst_metric_set(&rt->dst, type, nla_get_u32(nla));
1343 }
1344 }
1345 }
1346
1347 rt->dst.dev = dev;
1348 rt->rt6i_idev = idev;
1349 rt->rt6i_table = table;
1350
1351 cfg->fc_nlinfo.nl_net = dev_net(dev);
1352
1353 return __ip6_ins_rt(rt, &cfg->fc_nlinfo);
1354
1355 out:
1356 if (dev)
1357 dev_put(dev);
1358 if (idev)
1359 in6_dev_put(idev);
1360 if (rt)
1361 dst_free(&rt->dst);
1362 return err;
1363 }
1364
1365 static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info)
1366 {
1367 int err;
1368 struct fib6_table *table;
1369 struct net *net = dev_net(rt->rt6i_dev);
1370
1371 if (rt == net->ipv6.ip6_null_entry)
1372 return -ENOENT;
1373
1374 table = rt->rt6i_table;
1375 write_lock_bh(&table->tb6_lock);
1376
1377 err = fib6_del(rt, info);
1378 dst_release(&rt->dst);
1379
1380 write_unlock_bh(&table->tb6_lock);
1381
1382 return err;
1383 }
1384
1385 int ip6_del_rt(struct rt6_info *rt)
1386 {
1387 struct nl_info info = {
1388 .nl_net = dev_net(rt->rt6i_dev),
1389 };
1390 return __ip6_del_rt(rt, &info);
1391 }
1392
1393 static int ip6_route_del(struct fib6_config *cfg)
1394 {
1395 struct fib6_table *table;
1396 struct fib6_node *fn;
1397 struct rt6_info *rt;
1398 int err = -ESRCH;
1399
1400 table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table);
1401 if (table == NULL)
1402 return err;
1403
1404 read_lock_bh(&table->tb6_lock);
1405
1406 fn = fib6_locate(&table->tb6_root,
1407 &cfg->fc_dst, cfg->fc_dst_len,
1408 &cfg->fc_src, cfg->fc_src_len);
1409
1410 if (fn) {
1411 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1412 if (cfg->fc_ifindex &&
1413 (rt->rt6i_dev == NULL ||
1414 rt->rt6i_dev->ifindex != cfg->fc_ifindex))
1415 continue;
1416 if (cfg->fc_flags & RTF_GATEWAY &&
1417 !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway))
1418 continue;
1419 if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric)
1420 continue;
1421 dst_hold(&rt->dst);
1422 read_unlock_bh(&table->tb6_lock);
1423
1424 return __ip6_del_rt(rt, &cfg->fc_nlinfo);
1425 }
1426 }
1427 read_unlock_bh(&table->tb6_lock);
1428
1429 return err;
1430 }
1431
1432 /*
1433 * Handle redirects
1434 */
1435 struct ip6rd_flowi {
1436 struct flowi fl;
1437 struct in6_addr gateway;
1438 };
1439
1440 static struct rt6_info *__ip6_route_redirect(struct net *net,
1441 struct fib6_table *table,
1442 struct flowi *fl,
1443 int flags)
1444 {
1445 struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl;
1446 struct rt6_info *rt;
1447 struct fib6_node *fn;
1448
1449 /*
1450 * Get the "current" route for this destination and
1451 * check if the redirect has come from approriate router.
1452 *
1453 * RFC 2461 specifies that redirects should only be
1454 * accepted if they come from the nexthop to the target.
1455 * Due to the way the routes are chosen, this notion
1456 * is a bit fuzzy and one might need to check all possible
1457 * routes.
1458 */
1459
1460 read_lock_bh(&table->tb6_lock);
1461 fn = fib6_lookup(&table->tb6_root, &fl->fl6_dst, &fl->fl6_src);
1462 restart:
1463 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1464 /*
1465 * Current route is on-link; redirect is always invalid.
1466 *
1467 * Seems, previous statement is not true. It could
1468 * be node, which looks for us as on-link (f.e. proxy ndisc)
1469 * But then router serving it might decide, that we should
1470 * know truth 8)8) --ANK (980726).
1471 */
1472 if (rt6_check_expired(rt))
1473 continue;
1474 if (!(rt->rt6i_flags & RTF_GATEWAY))
1475 continue;
1476 if (fl->oif != rt->rt6i_dev->ifindex)
1477 continue;
1478 if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway))
1479 continue;
1480 break;
1481 }
1482
1483 if (!rt)
1484 rt = net->ipv6.ip6_null_entry;
1485 BACKTRACK(net, &fl->fl6_src);
1486 out:
1487 dst_hold(&rt->dst);
1488
1489 read_unlock_bh(&table->tb6_lock);
1490
1491 return rt;
1492 };
1493
1494 static struct rt6_info *ip6_route_redirect(struct in6_addr *dest,
1495 struct in6_addr *src,
1496 struct in6_addr *gateway,
1497 struct net_device *dev)
1498 {
1499 int flags = RT6_LOOKUP_F_HAS_SADDR;
1500 struct net *net = dev_net(dev);
1501 struct ip6rd_flowi rdfl = {
1502 .fl = {
1503 .oif = dev->ifindex,
1504 .fl6_dst = *dest,
1505 .fl6_src = *src,
1506 },
1507 };
1508
1509 ipv6_addr_copy(&rdfl.gateway, gateway);
1510
1511 if (rt6_need_strict(dest))
1512 flags |= RT6_LOOKUP_F_IFACE;
1513
1514 return (struct rt6_info *)fib6_rule_lookup(net, (struct flowi *)&rdfl,
1515 flags, __ip6_route_redirect);
1516 }
1517
1518 void rt6_redirect(struct in6_addr *dest, struct in6_addr *src,
1519 struct in6_addr *saddr,
1520 struct neighbour *neigh, u8 *lladdr, int on_link)
1521 {
1522 struct rt6_info *rt, *nrt = NULL;
1523 struct netevent_redirect netevent;
1524 struct net *net = dev_net(neigh->dev);
1525
1526 rt = ip6_route_redirect(dest, src, saddr, neigh->dev);
1527
1528 if (rt == net->ipv6.ip6_null_entry) {
1529 if (net_ratelimit())
1530 printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop "
1531 "for redirect target\n");
1532 goto out;
1533 }
1534
1535 /*
1536 * We have finally decided to accept it.
1537 */
1538
1539 neigh_update(neigh, lladdr, NUD_STALE,
1540 NEIGH_UPDATE_F_WEAK_OVERRIDE|
1541 NEIGH_UPDATE_F_OVERRIDE|
1542 (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER|
1543 NEIGH_UPDATE_F_ISROUTER))
1544 );
1545
1546 /*
1547 * Redirect received -> path was valid.
1548 * Look, redirects are sent only in response to data packets,
1549 * so that this nexthop apparently is reachable. --ANK
1550 */
1551 dst_confirm(&rt->dst);
1552
1553 /* Duplicate redirect: silently ignore. */
1554 if (neigh == rt->dst.neighbour)
1555 goto out;
1556
1557 nrt = ip6_rt_copy(rt);
1558 if (nrt == NULL)
1559 goto out;
1560
1561 nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE;
1562 if (on_link)
1563 nrt->rt6i_flags &= ~RTF_GATEWAY;
1564
1565 ipv6_addr_copy(&nrt->rt6i_dst.addr, dest);
1566 nrt->rt6i_dst.plen = 128;
1567 nrt->dst.flags |= DST_HOST;
1568
1569 ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key);
1570 nrt->rt6i_nexthop = neigh_clone(neigh);
1571
1572 if (ip6_ins_rt(nrt))
1573 goto out;
1574
1575 netevent.old = &rt->dst;
1576 netevent.new = &nrt->dst;
1577 call_netevent_notifiers(NETEVENT_REDIRECT, &netevent);
1578
1579 if (rt->rt6i_flags&RTF_CACHE) {
1580 ip6_del_rt(rt);
1581 return;
1582 }
1583
1584 out:
1585 dst_release(&rt->dst);
1586 }
1587
1588 /*
1589 * Handle ICMP "packet too big" messages
1590 * i.e. Path MTU discovery
1591 */
1592
1593 static void rt6_do_pmtu_disc(struct in6_addr *daddr, struct in6_addr *saddr,
1594 struct net *net, u32 pmtu, int ifindex)
1595 {
1596 struct rt6_info *rt, *nrt;
1597 int allfrag = 0;
1598 again:
1599 rt = rt6_lookup(net, daddr, saddr, ifindex, 0);
1600 if (rt == NULL)
1601 return;
1602
1603 if (rt6_check_expired(rt)) {
1604 ip6_del_rt(rt);
1605 goto again;
1606 }
1607
1608 if (pmtu >= dst_mtu(&rt->dst))
1609 goto out;
1610
1611 if (pmtu < IPV6_MIN_MTU) {
1612 /*
1613 * According to RFC2460, PMTU is set to the IPv6 Minimum Link
1614 * MTU (1280) and a fragment header should always be included
1615 * after a node receiving Too Big message reporting PMTU is
1616 * less than the IPv6 Minimum Link MTU.
1617 */
1618 pmtu = IPV6_MIN_MTU;
1619 allfrag = 1;
1620 }
1621
1622 /* New mtu received -> path was valid.
1623 They are sent only in response to data packets,
1624 so that this nexthop apparently is reachable. --ANK
1625 */
1626 dst_confirm(&rt->dst);
1627
1628 /* Host route. If it is static, it would be better
1629 not to override it, but add new one, so that
1630 when cache entry will expire old pmtu
1631 would return automatically.
1632 */
1633 if (rt->rt6i_flags & RTF_CACHE) {
1634 dst_metric_set(&rt->dst, RTAX_MTU, pmtu);
1635 if (allfrag) {
1636 u32 features = dst_metric(&rt->dst, RTAX_FEATURES);
1637 features |= RTAX_FEATURE_ALLFRAG;
1638 dst_metric_set(&rt->dst, RTAX_FEATURES, features);
1639 }
1640 dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
1641 rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES;
1642 goto out;
1643 }
1644
1645 /* Network route.
1646 Two cases are possible:
1647 1. It is connected route. Action: COW
1648 2. It is gatewayed route or NONEXTHOP route. Action: clone it.
1649 */
1650 if (!rt->rt6i_nexthop && !(rt->rt6i_flags & RTF_NONEXTHOP))
1651 nrt = rt6_alloc_cow(rt, daddr, saddr);
1652 else
1653 nrt = rt6_alloc_clone(rt, daddr);
1654
1655 if (nrt) {
1656 dst_metric_set(&nrt->dst, RTAX_MTU, pmtu);
1657 if (allfrag) {
1658 u32 features = dst_metric(&nrt->dst, RTAX_FEATURES);
1659 features |= RTAX_FEATURE_ALLFRAG;
1660 dst_metric_set(&nrt->dst, RTAX_FEATURES, features);
1661 }
1662
1663 /* According to RFC 1981, detecting PMTU increase shouldn't be
1664 * happened within 5 mins, the recommended timer is 10 mins.
1665 * Here this route expiration time is set to ip6_rt_mtu_expires
1666 * which is 10 mins. After 10 mins the decreased pmtu is expired
1667 * and detecting PMTU increase will be automatically happened.
1668 */
1669 dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires);
1670 nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES;
1671
1672 ip6_ins_rt(nrt);
1673 }
1674 out:
1675 dst_release(&rt->dst);
1676 }
1677
1678 void rt6_pmtu_discovery(struct in6_addr *daddr, struct in6_addr *saddr,
1679 struct net_device *dev, u32 pmtu)
1680 {
1681 struct net *net = dev_net(dev);
1682
1683 /*
1684 * RFC 1981 states that a node "MUST reduce the size of the packets it
1685 * is sending along the path" that caused the Packet Too Big message.
1686 * Since it's not possible in the general case to determine which
1687 * interface was used to send the original packet, we update the MTU
1688 * on the interface that will be used to send future packets. We also
1689 * update the MTU on the interface that received the Packet Too Big in
1690 * case the original packet was forced out that interface with
1691 * SO_BINDTODEVICE or similar. This is the next best thing to the
1692 * correct behaviour, which would be to update the MTU on all
1693 * interfaces.
1694 */
1695 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0);
1696 rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex);
1697 }
1698
1699 /*
1700 * Misc support functions
1701 */
1702
1703 static struct rt6_info * ip6_rt_copy(struct rt6_info *ort)
1704 {
1705 struct net *net = dev_net(ort->rt6i_dev);
1706 struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
1707
1708 if (rt) {
1709 rt->dst.input = ort->dst.input;
1710 rt->dst.output = ort->dst.output;
1711
1712 dst_copy_metrics(&rt->dst, &ort->dst);
1713 rt->dst.error = ort->dst.error;
1714 rt->dst.dev = ort->dst.dev;
1715 if (rt->dst.dev)
1716 dev_hold(rt->dst.dev);
1717 rt->rt6i_idev = ort->rt6i_idev;
1718 if (rt->rt6i_idev)
1719 in6_dev_hold(rt->rt6i_idev);
1720 rt->dst.lastuse = jiffies;
1721 rt->rt6i_expires = 0;
1722
1723 ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway);
1724 rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES;
1725 rt->rt6i_metric = 0;
1726
1727 memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key));
1728 #ifdef CONFIG_IPV6_SUBTREES
1729 memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key));
1730 #endif
1731 rt->rt6i_table = ort->rt6i_table;
1732 }
1733 return rt;
1734 }
1735
1736 #ifdef CONFIG_IPV6_ROUTE_INFO
1737 static struct rt6_info *rt6_get_route_info(struct net *net,
1738 struct in6_addr *prefix, int prefixlen,
1739 struct in6_addr *gwaddr, int ifindex)
1740 {
1741 struct fib6_node *fn;
1742 struct rt6_info *rt = NULL;
1743 struct fib6_table *table;
1744
1745 table = fib6_get_table(net, RT6_TABLE_INFO);
1746 if (table == NULL)
1747 return NULL;
1748
1749 write_lock_bh(&table->tb6_lock);
1750 fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0);
1751 if (!fn)
1752 goto out;
1753
1754 for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) {
1755 if (rt->rt6i_dev->ifindex != ifindex)
1756 continue;
1757 if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY))
1758 continue;
1759 if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr))
1760 continue;
1761 dst_hold(&rt->dst);
1762 break;
1763 }
1764 out:
1765 write_unlock_bh(&table->tb6_lock);
1766 return rt;
1767 }
1768
1769 static struct rt6_info *rt6_add_route_info(struct net *net,
1770 struct in6_addr *prefix, int prefixlen,
1771 struct in6_addr *gwaddr, int ifindex,
1772 unsigned pref)
1773 {
1774 struct fib6_config cfg = {
1775 .fc_table = RT6_TABLE_INFO,
1776 .fc_metric = IP6_RT_PRIO_USER,
1777 .fc_ifindex = ifindex,
1778 .fc_dst_len = prefixlen,
1779 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO |
1780 RTF_UP | RTF_PREF(pref),
1781 .fc_nlinfo.pid = 0,
1782 .fc_nlinfo.nlh = NULL,
1783 .fc_nlinfo.nl_net = net,
1784 };
1785
1786 ipv6_addr_copy(&cfg.fc_dst, prefix);
1787 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
1788
1789 /* We should treat it as a default route if prefix length is 0. */
1790 if (!prefixlen)
1791 cfg.fc_flags |= RTF_DEFAULT;
1792
1793 ip6_route_add(&cfg);
1794
1795 return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex);
1796 }
1797 #endif
1798
1799 struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *dev)
1800 {
1801 struct rt6_info *rt;
1802 struct fib6_table *table;
1803
1804 table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT);
1805 if (table == NULL)
1806 return NULL;
1807
1808 write_lock_bh(&table->tb6_lock);
1809 for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) {
1810 if (dev == rt->rt6i_dev &&
1811 ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) &&
1812 ipv6_addr_equal(&rt->rt6i_gateway, addr))
1813 break;
1814 }
1815 if (rt)
1816 dst_hold(&rt->dst);
1817 write_unlock_bh(&table->tb6_lock);
1818 return rt;
1819 }
1820
1821 struct rt6_info *rt6_add_dflt_router(struct in6_addr *gwaddr,
1822 struct net_device *dev,
1823 unsigned int pref)
1824 {
1825 struct fib6_config cfg = {
1826 .fc_table = RT6_TABLE_DFLT,
1827 .fc_metric = IP6_RT_PRIO_USER,
1828 .fc_ifindex = dev->ifindex,
1829 .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT |
1830 RTF_UP | RTF_EXPIRES | RTF_PREF(pref),
1831 .fc_nlinfo.pid = 0,
1832 .fc_nlinfo.nlh = NULL,
1833 .fc_nlinfo.nl_net = dev_net(dev),
1834 };
1835
1836 ipv6_addr_copy(&cfg.fc_gateway, gwaddr);
1837
1838 ip6_route_add(&cfg);
1839
1840 return rt6_get_dflt_router(gwaddr, dev);
1841 }
1842
1843 void rt6_purge_dflt_routers(struct net *net)
1844 {
1845 struct rt6_info *rt;
1846 struct fib6_table *table;
1847
1848 /* NOTE: Keep consistent with rt6_get_dflt_router */
1849 table = fib6_get_table(net, RT6_TABLE_DFLT);
1850 if (table == NULL)
1851 return;
1852
1853 restart:
1854 read_lock_bh(&table->tb6_lock);
1855 for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) {
1856 if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) {
1857 dst_hold(&rt->dst);
1858 read_unlock_bh(&table->tb6_lock);
1859 ip6_del_rt(rt);
1860 goto restart;
1861 }
1862 }
1863 read_unlock_bh(&table->tb6_lock);
1864 }
1865
1866 static void rtmsg_to_fib6_config(struct net *net,
1867 struct in6_rtmsg *rtmsg,
1868 struct fib6_config *cfg)
1869 {
1870 memset(cfg, 0, sizeof(*cfg));
1871
1872 cfg->fc_table = RT6_TABLE_MAIN;
1873 cfg->fc_ifindex = rtmsg->rtmsg_ifindex;
1874 cfg->fc_metric = rtmsg->rtmsg_metric;
1875 cfg->fc_expires = rtmsg->rtmsg_info;
1876 cfg->fc_dst_len = rtmsg->rtmsg_dst_len;
1877 cfg->fc_src_len = rtmsg->rtmsg_src_len;
1878 cfg->fc_flags = rtmsg->rtmsg_flags;
1879
1880 cfg->fc_nlinfo.nl_net = net;
1881
1882 ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst);
1883 ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src);
1884 ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway);
1885 }
1886
1887 int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg)
1888 {
1889 struct fib6_config cfg;
1890 struct in6_rtmsg rtmsg;
1891 int err;
1892
1893 switch(cmd) {
1894 case SIOCADDRT: /* Add a route */
1895 case SIOCDELRT: /* Delete a route */
1896 if (!capable(CAP_NET_ADMIN))
1897 return -EPERM;
1898 err = copy_from_user(&rtmsg, arg,
1899 sizeof(struct in6_rtmsg));
1900 if (err)
1901 return -EFAULT;
1902
1903 rtmsg_to_fib6_config(net, &rtmsg, &cfg);
1904
1905 rtnl_lock();
1906 switch (cmd) {
1907 case SIOCADDRT:
1908 err = ip6_route_add(&cfg);
1909 break;
1910 case SIOCDELRT:
1911 err = ip6_route_del(&cfg);
1912 break;
1913 default:
1914 err = -EINVAL;
1915 }
1916 rtnl_unlock();
1917
1918 return err;
1919 }
1920
1921 return -EINVAL;
1922 }
1923
1924 /*
1925 * Drop the packet on the floor
1926 */
1927
1928 static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes)
1929 {
1930 int type;
1931 struct dst_entry *dst = skb_dst(skb);
1932 switch (ipstats_mib_noroutes) {
1933 case IPSTATS_MIB_INNOROUTES:
1934 type = ipv6_addr_type(&ipv6_hdr(skb)->daddr);
1935 if (type == IPV6_ADDR_ANY) {
1936 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1937 IPSTATS_MIB_INADDRERRORS);
1938 break;
1939 }
1940 /* FALLTHROUGH */
1941 case IPSTATS_MIB_OUTNOROUTES:
1942 IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst),
1943 ipstats_mib_noroutes);
1944 break;
1945 }
1946 icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0);
1947 kfree_skb(skb);
1948 return 0;
1949 }
1950
1951 static int ip6_pkt_discard(struct sk_buff *skb)
1952 {
1953 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES);
1954 }
1955
1956 static int ip6_pkt_discard_out(struct sk_buff *skb)
1957 {
1958 skb->dev = skb_dst(skb)->dev;
1959 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
1960 }
1961
1962 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
1963
1964 static int ip6_pkt_prohibit(struct sk_buff *skb)
1965 {
1966 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
1967 }
1968
1969 static int ip6_pkt_prohibit_out(struct sk_buff *skb)
1970 {
1971 skb->dev = skb_dst(skb)->dev;
1972 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
1973 }
1974
1975 #endif
1976
1977 /*
1978 * Allocate a dst for local (unicast / anycast) address.
1979 */
1980
1981 struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
1982 const struct in6_addr *addr,
1983 int anycast)
1984 {
1985 struct net *net = dev_net(idev->dev);
1986 struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops);
1987 struct neighbour *neigh;
1988
1989 if (rt == NULL) {
1990 if (net_ratelimit())
1991 pr_warning("IPv6: Maximum number of routes reached,"
1992 " consider increasing route/max_size.\n");
1993 return ERR_PTR(-ENOMEM);
1994 }
1995
1996 dev_hold(net->loopback_dev);
1997 in6_dev_hold(idev);
1998
1999 rt->dst.flags = DST_HOST;
2000 rt->dst.input = ip6_input;
2001 rt->dst.output = ip6_output;
2002 rt->rt6i_dev = net->loopback_dev;
2003 rt->rt6i_idev = idev;
2004 dst_metric_set(&rt->dst, RTAX_HOPLIMIT, -1);
2005 rt->dst.obsolete = -1;
2006
2007 rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP;
2008 if (anycast)
2009 rt->rt6i_flags |= RTF_ANYCAST;
2010 else
2011 rt->rt6i_flags |= RTF_LOCAL;
2012 neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway);
2013 if (IS_ERR(neigh)) {
2014 dst_free(&rt->dst);
2015
2016 /* We are casting this because that is the return
2017 * value type. But an errno encoded pointer is the
2018 * same regardless of the underlying pointer type,
2019 * and that's what we are returning. So this is OK.
2020 */
2021 return (struct rt6_info *) neigh;
2022 }
2023 rt->rt6i_nexthop = neigh;
2024
2025 ipv6_addr_copy(&rt->rt6i_dst.addr, addr);
2026 rt->rt6i_dst.plen = 128;
2027 rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL);
2028
2029 atomic_set(&rt->dst.__refcnt, 1);
2030
2031 return rt;
2032 }
2033
2034 struct arg_dev_net {
2035 struct net_device *dev;
2036 struct net *net;
2037 };
2038
2039 static int fib6_ifdown(struct rt6_info *rt, void *arg)
2040 {
2041 const struct arg_dev_net *adn = arg;
2042 const struct net_device *dev = adn->dev;
2043
2044 if ((rt->rt6i_dev == dev || dev == NULL) &&
2045 rt != adn->net->ipv6.ip6_null_entry) {
2046 RT6_TRACE("deleted by ifdown %p\n", rt);
2047 return -1;
2048 }
2049 return 0;
2050 }
2051
2052 void rt6_ifdown(struct net *net, struct net_device *dev)
2053 {
2054 struct arg_dev_net adn = {
2055 .dev = dev,
2056 .net = net,
2057 };
2058
2059 fib6_clean_all(net, fib6_ifdown, 0, &adn);
2060 icmp6_clean_all(fib6_ifdown, &adn);
2061 }
2062
2063 struct rt6_mtu_change_arg
2064 {
2065 struct net_device *dev;
2066 unsigned mtu;
2067 };
2068
2069 static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg)
2070 {
2071 struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg;
2072 struct inet6_dev *idev;
2073
2074 /* In IPv6 pmtu discovery is not optional,
2075 so that RTAX_MTU lock cannot disable it.
2076 We still use this lock to block changes
2077 caused by addrconf/ndisc.
2078 */
2079
2080 idev = __in6_dev_get(arg->dev);
2081 if (idev == NULL)
2082 return 0;
2083
2084 /* For administrative MTU increase, there is no way to discover
2085 IPv6 PMTU increase, so PMTU increase should be updated here.
2086 Since RFC 1981 doesn't include administrative MTU increase
2087 update PMTU increase is a MUST. (i.e. jumbo frame)
2088 */
2089 /*
2090 If new MTU is less than route PMTU, this new MTU will be the
2091 lowest MTU in the path, update the route PMTU to reflect PMTU
2092 decreases; if new MTU is greater than route PMTU, and the
2093 old MTU is the lowest MTU in the path, update the route PMTU
2094 to reflect the increase. In this case if the other nodes' MTU
2095 also have the lowest MTU, TOO BIG MESSAGE will be lead to
2096 PMTU discouvery.
2097 */
2098 if (rt->rt6i_dev == arg->dev &&
2099 !dst_metric_locked(&rt->dst, RTAX_MTU) &&
2100 (dst_mtu(&rt->dst) >= arg->mtu ||
2101 (dst_mtu(&rt->dst) < arg->mtu &&
2102 dst_mtu(&rt->dst) == idev->cnf.mtu6))) {
2103 dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu);
2104 }
2105 return 0;
2106 }
2107
2108 void rt6_mtu_change(struct net_device *dev, unsigned mtu)
2109 {
2110 struct rt6_mtu_change_arg arg = {
2111 .dev = dev,
2112 .mtu = mtu,
2113 };
2114
2115 fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg);
2116 }
2117
2118 static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
2119 [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) },
2120 [RTA_OIF] = { .type = NLA_U32 },
2121 [RTA_IIF] = { .type = NLA_U32 },
2122 [RTA_PRIORITY] = { .type = NLA_U32 },
2123 [RTA_METRICS] = { .type = NLA_NESTED },
2124 };
2125
2126 static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2127 struct fib6_config *cfg)
2128 {
2129 struct rtmsg *rtm;
2130 struct nlattr *tb[RTA_MAX+1];
2131 int err;
2132
2133 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2134 if (err < 0)
2135 goto errout;
2136
2137 err = -EINVAL;
2138 rtm = nlmsg_data(nlh);
2139 memset(cfg, 0, sizeof(*cfg));
2140
2141 cfg->fc_table = rtm->rtm_table;
2142 cfg->fc_dst_len = rtm->rtm_dst_len;
2143 cfg->fc_src_len = rtm->rtm_src_len;
2144 cfg->fc_flags = RTF_UP;
2145 cfg->fc_protocol = rtm->rtm_protocol;
2146
2147 if (rtm->rtm_type == RTN_UNREACHABLE)
2148 cfg->fc_flags |= RTF_REJECT;
2149
2150 if (rtm->rtm_type == RTN_LOCAL)
2151 cfg->fc_flags |= RTF_LOCAL;
2152
2153 cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
2154 cfg->fc_nlinfo.nlh = nlh;
2155 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
2156
2157 if (tb[RTA_GATEWAY]) {
2158 nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16);
2159 cfg->fc_flags |= RTF_GATEWAY;
2160 }
2161
2162 if (tb[RTA_DST]) {
2163 int plen = (rtm->rtm_dst_len + 7) >> 3;
2164
2165 if (nla_len(tb[RTA_DST]) < plen)
2166 goto errout;
2167
2168 nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen);
2169 }
2170
2171 if (tb[RTA_SRC]) {
2172 int plen = (rtm->rtm_src_len + 7) >> 3;
2173
2174 if (nla_len(tb[RTA_SRC]) < plen)
2175 goto errout;
2176
2177 nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen);
2178 }
2179
2180 if (tb[RTA_OIF])
2181 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
2182
2183 if (tb[RTA_PRIORITY])
2184 cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]);
2185
2186 if (tb[RTA_METRICS]) {
2187 cfg->fc_mx = nla_data(tb[RTA_METRICS]);
2188 cfg->fc_mx_len = nla_len(tb[RTA_METRICS]);
2189 }
2190
2191 if (tb[RTA_TABLE])
2192 cfg->fc_table = nla_get_u32(tb[RTA_TABLE]);
2193
2194 err = 0;
2195 errout:
2196 return err;
2197 }
2198
2199 static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
2200 {
2201 struct fib6_config cfg;
2202 int err;
2203
2204 err = rtm_to_fib6_config(skb, nlh, &cfg);
2205 if (err < 0)
2206 return err;
2207
2208 return ip6_route_del(&cfg);
2209 }
2210
2211 static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
2212 {
2213 struct fib6_config cfg;
2214 int err;
2215
2216 err = rtm_to_fib6_config(skb, nlh, &cfg);
2217 if (err < 0)
2218 return err;
2219
2220 return ip6_route_add(&cfg);
2221 }
2222
2223 static inline size_t rt6_nlmsg_size(void)
2224 {
2225 return NLMSG_ALIGN(sizeof(struct rtmsg))
2226 + nla_total_size(16) /* RTA_SRC */
2227 + nla_total_size(16) /* RTA_DST */
2228 + nla_total_size(16) /* RTA_GATEWAY */
2229 + nla_total_size(16) /* RTA_PREFSRC */
2230 + nla_total_size(4) /* RTA_TABLE */
2231 + nla_total_size(4) /* RTA_IIF */
2232 + nla_total_size(4) /* RTA_OIF */
2233 + nla_total_size(4) /* RTA_PRIORITY */
2234 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2235 + nla_total_size(sizeof(struct rta_cacheinfo));
2236 }
2237
2238 static int rt6_fill_node(struct net *net,
2239 struct sk_buff *skb, struct rt6_info *rt,
2240 struct in6_addr *dst, struct in6_addr *src,
2241 int iif, int type, u32 pid, u32 seq,
2242 int prefix, int nowait, unsigned int flags)
2243 {
2244 struct rtmsg *rtm;
2245 struct nlmsghdr *nlh;
2246 long expires;
2247 u32 table;
2248
2249 if (prefix) { /* user wants prefix routes only */
2250 if (!(rt->rt6i_flags & RTF_PREFIX_RT)) {
2251 /* success since this is not a prefix route */
2252 return 1;
2253 }
2254 }
2255
2256 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags);
2257 if (nlh == NULL)
2258 return -EMSGSIZE;
2259
2260 rtm = nlmsg_data(nlh);
2261 rtm->rtm_family = AF_INET6;
2262 rtm->rtm_dst_len = rt->rt6i_dst.plen;
2263 rtm->rtm_src_len = rt->rt6i_src.plen;
2264 rtm->rtm_tos = 0;
2265 if (rt->rt6i_table)
2266 table = rt->rt6i_table->tb6_id;
2267 else
2268 table = RT6_TABLE_UNSPEC;
2269 rtm->rtm_table = table;
2270 NLA_PUT_U32(skb, RTA_TABLE, table);
2271 if (rt->rt6i_flags&RTF_REJECT)
2272 rtm->rtm_type = RTN_UNREACHABLE;
2273 else if (rt->rt6i_flags&RTF_LOCAL)
2274 rtm->rtm_type = RTN_LOCAL;
2275 else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK))
2276 rtm->rtm_type = RTN_LOCAL;
2277 else
2278 rtm->rtm_type = RTN_UNICAST;
2279 rtm->rtm_flags = 0;
2280 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2281 rtm->rtm_protocol = rt->rt6i_protocol;
2282 if (rt->rt6i_flags&RTF_DYNAMIC)
2283 rtm->rtm_protocol = RTPROT_REDIRECT;
2284 else if (rt->rt6i_flags & RTF_ADDRCONF)
2285 rtm->rtm_protocol = RTPROT_KERNEL;
2286 else if (rt->rt6i_flags&RTF_DEFAULT)
2287 rtm->rtm_protocol = RTPROT_RA;
2288
2289 if (rt->rt6i_flags&RTF_CACHE)
2290 rtm->rtm_flags |= RTM_F_CLONED;
2291
2292 if (dst) {
2293 NLA_PUT(skb, RTA_DST, 16, dst);
2294 rtm->rtm_dst_len = 128;
2295 } else if (rtm->rtm_dst_len)
2296 NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr);
2297 #ifdef CONFIG_IPV6_SUBTREES
2298 if (src) {
2299 NLA_PUT(skb, RTA_SRC, 16, src);
2300 rtm->rtm_src_len = 128;
2301 } else if (rtm->rtm_src_len)
2302 NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr);
2303 #endif
2304 if (iif) {
2305 #ifdef CONFIG_IPV6_MROUTE
2306 if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) {
2307 int err = ip6mr_get_route(net, skb, rtm, nowait);
2308 if (err <= 0) {
2309 if (!nowait) {
2310 if (err == 0)
2311 return 0;
2312 goto nla_put_failure;
2313 } else {
2314 if (err == -EMSGSIZE)
2315 goto nla_put_failure;
2316 }
2317 }
2318 } else
2319 #endif
2320 NLA_PUT_U32(skb, RTA_IIF, iif);
2321 } else if (dst) {
2322 struct inet6_dev *idev = ip6_dst_idev(&rt->dst);
2323 struct in6_addr saddr_buf;
2324 if (ipv6_dev_get_saddr(net, idev ? idev->dev : NULL,
2325 dst, 0, &saddr_buf) == 0)
2326 NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf);
2327 }
2328
2329 if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0)
2330 goto nla_put_failure;
2331
2332 if (rt->dst.neighbour)
2333 NLA_PUT(skb, RTA_GATEWAY, 16, &rt->dst.neighbour->primary_key);
2334
2335 if (rt->dst.dev)
2336 NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex);
2337
2338 NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric);
2339
2340 if (!(rt->rt6i_flags & RTF_EXPIRES))
2341 expires = 0;
2342 else if (rt->rt6i_expires - jiffies < INT_MAX)
2343 expires = rt->rt6i_expires - jiffies;
2344 else
2345 expires = INT_MAX;
2346
2347 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0,
2348 expires, rt->dst.error) < 0)
2349 goto nla_put_failure;
2350
2351 return nlmsg_end(skb, nlh);
2352
2353 nla_put_failure:
2354 nlmsg_cancel(skb, nlh);
2355 return -EMSGSIZE;
2356 }
2357
2358 int rt6_dump_route(struct rt6_info *rt, void *p_arg)
2359 {
2360 struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg;
2361 int prefix;
2362
2363 if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) {
2364 struct rtmsg *rtm = nlmsg_data(arg->cb->nlh);
2365 prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0;
2366 } else
2367 prefix = 0;
2368
2369 return rt6_fill_node(arg->net,
2370 arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE,
2371 NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq,
2372 prefix, 0, NLM_F_MULTI);
2373 }
2374
2375 static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg)
2376 {
2377 struct net *net = sock_net(in_skb->sk);
2378 struct nlattr *tb[RTA_MAX+1];
2379 struct rt6_info *rt;
2380 struct sk_buff *skb;
2381 struct rtmsg *rtm;
2382 struct flowi fl;
2383 int err, iif = 0;
2384
2385 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
2386 if (err < 0)
2387 goto errout;
2388
2389 err = -EINVAL;
2390 memset(&fl, 0, sizeof(fl));
2391
2392 if (tb[RTA_SRC]) {
2393 if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr))
2394 goto errout;
2395
2396 ipv6_addr_copy(&fl.fl6_src, nla_data(tb[RTA_SRC]));
2397 }
2398
2399 if (tb[RTA_DST]) {
2400 if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr))
2401 goto errout;
2402
2403 ipv6_addr_copy(&fl.fl6_dst, nla_data(tb[RTA_DST]));
2404 }
2405
2406 if (tb[RTA_IIF])
2407 iif = nla_get_u32(tb[RTA_IIF]);
2408
2409 if (tb[RTA_OIF])
2410 fl.oif = nla_get_u32(tb[RTA_OIF]);
2411
2412 if (iif) {
2413 struct net_device *dev;
2414 dev = __dev_get_by_index(net, iif);
2415 if (!dev) {
2416 err = -ENODEV;
2417 goto errout;
2418 }
2419 }
2420
2421 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2422 if (skb == NULL) {
2423 err = -ENOBUFS;
2424 goto errout;
2425 }
2426
2427 /* Reserve room for dummy headers, this skb can pass
2428 through good chunk of routing engine.
2429 */
2430 skb_reset_mac_header(skb);
2431 skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr));
2432
2433 rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl);
2434 skb_dst_set(skb, &rt->dst);
2435
2436 err = rt6_fill_node(net, skb, rt, &fl.fl6_dst, &fl.fl6_src, iif,
2437 RTM_NEWROUTE, NETLINK_CB(in_skb).pid,
2438 nlh->nlmsg_seq, 0, 0, 0);
2439 if (err < 0) {
2440 kfree_skb(skb);
2441 goto errout;
2442 }
2443
2444 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
2445 errout:
2446 return err;
2447 }
2448
2449 void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info)
2450 {
2451 struct sk_buff *skb;
2452 struct net *net = info->nl_net;
2453 u32 seq;
2454 int err;
2455
2456 err = -ENOBUFS;
2457 seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0;
2458
2459 skb = nlmsg_new(rt6_nlmsg_size(), gfp_any());
2460 if (skb == NULL)
2461 goto errout;
2462
2463 err = rt6_fill_node(net, skb, rt, NULL, NULL, 0,
2464 event, info->pid, seq, 0, 0, 0);
2465 if (err < 0) {
2466 /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */
2467 WARN_ON(err == -EMSGSIZE);
2468 kfree_skb(skb);
2469 goto errout;
2470 }
2471 rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE,
2472 info->nlh, gfp_any());
2473 return;
2474 errout:
2475 if (err < 0)
2476 rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err);
2477 }
2478
2479 static int ip6_route_dev_notify(struct notifier_block *this,
2480 unsigned long event, void *data)
2481 {
2482 struct net_device *dev = (struct net_device *)data;
2483 struct net *net = dev_net(dev);
2484
2485 if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) {
2486 net->ipv6.ip6_null_entry->dst.dev = dev;
2487 net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev);
2488 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2489 net->ipv6.ip6_prohibit_entry->dst.dev = dev;
2490 net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
2491 net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
2492 net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
2493 #endif
2494 }
2495
2496 return NOTIFY_OK;
2497 }
2498
2499 /*
2500 * /proc
2501 */
2502
2503 #ifdef CONFIG_PROC_FS
2504
2505 struct rt6_proc_arg
2506 {
2507 char *buffer;
2508 int offset;
2509 int length;
2510 int skip;
2511 int len;
2512 };
2513
2514 static int rt6_info_route(struct rt6_info *rt, void *p_arg)
2515 {
2516 struct seq_file *m = p_arg;
2517
2518 seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
2519
2520 #ifdef CONFIG_IPV6_SUBTREES
2521 seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
2522 #else
2523 seq_puts(m, "00000000000000000000000000000000 00 ");
2524 #endif
2525
2526 if (rt->rt6i_nexthop) {
2527 seq_printf(m, "%pi6", rt->rt6i_nexthop->primary_key);
2528 } else {
2529 seq_puts(m, "00000000000000000000000000000000");
2530 }
2531 seq_printf(m, " %08x %08x %08x %08x %8s\n",
2532 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2533 rt->dst.__use, rt->rt6i_flags,
2534 rt->rt6i_dev ? rt->rt6i_dev->name : "");
2535 return 0;
2536 }
2537
2538 static int ipv6_route_show(struct seq_file *m, void *v)
2539 {
2540 struct net *net = (struct net *)m->private;
2541 fib6_clean_all(net, rt6_info_route, 0, m);
2542 return 0;
2543 }
2544
2545 static int ipv6_route_open(struct inode *inode, struct file *file)
2546 {
2547 return single_open_net(inode, file, ipv6_route_show);
2548 }
2549
2550 static const struct file_operations ipv6_route_proc_fops = {
2551 .owner = THIS_MODULE,
2552 .open = ipv6_route_open,
2553 .read = seq_read,
2554 .llseek = seq_lseek,
2555 .release = single_release_net,
2556 };
2557
2558 static int rt6_stats_seq_show(struct seq_file *seq, void *v)
2559 {
2560 struct net *net = (struct net *)seq->private;
2561 seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n",
2562 net->ipv6.rt6_stats->fib_nodes,
2563 net->ipv6.rt6_stats->fib_route_nodes,
2564 net->ipv6.rt6_stats->fib_rt_alloc,
2565 net->ipv6.rt6_stats->fib_rt_entries,
2566 net->ipv6.rt6_stats->fib_rt_cache,
2567 dst_entries_get_slow(&net->ipv6.ip6_dst_ops),
2568 net->ipv6.rt6_stats->fib_discarded_routes);
2569
2570 return 0;
2571 }
2572
2573 static int rt6_stats_seq_open(struct inode *inode, struct file *file)
2574 {
2575 return single_open_net(inode, file, rt6_stats_seq_show);
2576 }
2577
2578 static const struct file_operations rt6_stats_seq_fops = {
2579 .owner = THIS_MODULE,
2580 .open = rt6_stats_seq_open,
2581 .read = seq_read,
2582 .llseek = seq_lseek,
2583 .release = single_release_net,
2584 };
2585 #endif /* CONFIG_PROC_FS */
2586
2587 #ifdef CONFIG_SYSCTL
2588
2589 static
2590 int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write,
2591 void __user *buffer, size_t *lenp, loff_t *ppos)
2592 {
2593 struct net *net = current->nsproxy->net_ns;
2594 int delay = net->ipv6.sysctl.flush_delay;
2595 if (write) {
2596 proc_dointvec(ctl, write, buffer, lenp, ppos);
2597 fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net);
2598 return 0;
2599 } else
2600 return -EINVAL;
2601 }
2602
2603 ctl_table ipv6_route_table_template[] = {
2604 {
2605 .procname = "flush",
2606 .data = &init_net.ipv6.sysctl.flush_delay,
2607 .maxlen = sizeof(int),
2608 .mode = 0200,
2609 .proc_handler = ipv6_sysctl_rtcache_flush
2610 },
2611 {
2612 .procname = "gc_thresh",
2613 .data = &ip6_dst_ops_template.gc_thresh,
2614 .maxlen = sizeof(int),
2615 .mode = 0644,
2616 .proc_handler = proc_dointvec,
2617 },
2618 {
2619 .procname = "max_size",
2620 .data = &init_net.ipv6.sysctl.ip6_rt_max_size,
2621 .maxlen = sizeof(int),
2622 .mode = 0644,
2623 .proc_handler = proc_dointvec,
2624 },
2625 {
2626 .procname = "gc_min_interval",
2627 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
2628 .maxlen = sizeof(int),
2629 .mode = 0644,
2630 .proc_handler = proc_dointvec_jiffies,
2631 },
2632 {
2633 .procname = "gc_timeout",
2634 .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout,
2635 .maxlen = sizeof(int),
2636 .mode = 0644,
2637 .proc_handler = proc_dointvec_jiffies,
2638 },
2639 {
2640 .procname = "gc_interval",
2641 .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval,
2642 .maxlen = sizeof(int),
2643 .mode = 0644,
2644 .proc_handler = proc_dointvec_jiffies,
2645 },
2646 {
2647 .procname = "gc_elasticity",
2648 .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity,
2649 .maxlen = sizeof(int),
2650 .mode = 0644,
2651 .proc_handler = proc_dointvec,
2652 },
2653 {
2654 .procname = "mtu_expires",
2655 .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires,
2656 .maxlen = sizeof(int),
2657 .mode = 0644,
2658 .proc_handler = proc_dointvec_jiffies,
2659 },
2660 {
2661 .procname = "min_adv_mss",
2662 .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss,
2663 .maxlen = sizeof(int),
2664 .mode = 0644,
2665 .proc_handler = proc_dointvec,
2666 },
2667 {
2668 .procname = "gc_min_interval_ms",
2669 .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval,
2670 .maxlen = sizeof(int),
2671 .mode = 0644,
2672 .proc_handler = proc_dointvec_ms_jiffies,
2673 },
2674 { }
2675 };
2676
2677 struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net)
2678 {
2679 struct ctl_table *table;
2680
2681 table = kmemdup(ipv6_route_table_template,
2682 sizeof(ipv6_route_table_template),
2683 GFP_KERNEL);
2684
2685 if (table) {
2686 table[0].data = &net->ipv6.sysctl.flush_delay;
2687 table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh;
2688 table[2].data = &net->ipv6.sysctl.ip6_rt_max_size;
2689 table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2690 table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout;
2691 table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval;
2692 table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity;
2693 table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires;
2694 table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss;
2695 table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval;
2696 }
2697
2698 return table;
2699 }
2700 #endif
2701
2702 static int __net_init ip6_route_net_init(struct net *net)
2703 {
2704 int ret = -ENOMEM;
2705
2706 memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template,
2707 sizeof(net->ipv6.ip6_dst_ops));
2708
2709 if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0)
2710 goto out_ip6_dst_ops;
2711
2712 net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template,
2713 sizeof(*net->ipv6.ip6_null_entry),
2714 GFP_KERNEL);
2715 if (!net->ipv6.ip6_null_entry)
2716 goto out_ip6_dst_entries;
2717 net->ipv6.ip6_null_entry->dst.path =
2718 (struct dst_entry *)net->ipv6.ip6_null_entry;
2719 net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops;
2720 dst_init_metrics(&net->ipv6.ip6_null_entry->dst,
2721 ip6_template_metrics, true);
2722
2723 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2724 net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
2725 sizeof(*net->ipv6.ip6_prohibit_entry),
2726 GFP_KERNEL);
2727 if (!net->ipv6.ip6_prohibit_entry)
2728 goto out_ip6_null_entry;
2729 net->ipv6.ip6_prohibit_entry->dst.path =
2730 (struct dst_entry *)net->ipv6.ip6_prohibit_entry;
2731 net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops;
2732 dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst,
2733 ip6_template_metrics, true);
2734
2735 net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
2736 sizeof(*net->ipv6.ip6_blk_hole_entry),
2737 GFP_KERNEL);
2738 if (!net->ipv6.ip6_blk_hole_entry)
2739 goto out_ip6_prohibit_entry;
2740 net->ipv6.ip6_blk_hole_entry->dst.path =
2741 (struct dst_entry *)net->ipv6.ip6_blk_hole_entry;
2742 net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
2743 dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
2744 ip6_template_metrics, true);
2745 #endif
2746
2747 net->ipv6.sysctl.flush_delay = 0;
2748 net->ipv6.sysctl.ip6_rt_max_size = 4096;
2749 net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2;
2750 net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ;
2751 net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ;
2752 net->ipv6.sysctl.ip6_rt_gc_elasticity = 9;
2753 net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ;
2754 net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40;
2755
2756 #ifdef CONFIG_PROC_FS
2757 proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops);
2758 proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops);
2759 #endif
2760 net->ipv6.ip6_rt_gc_expire = 30*HZ;
2761
2762 ret = 0;
2763 out:
2764 return ret;
2765
2766 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2767 out_ip6_prohibit_entry:
2768 kfree(net->ipv6.ip6_prohibit_entry);
2769 out_ip6_null_entry:
2770 kfree(net->ipv6.ip6_null_entry);
2771 #endif
2772 out_ip6_dst_entries:
2773 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2774 out_ip6_dst_ops:
2775 goto out;
2776 }
2777
2778 static void __net_exit ip6_route_net_exit(struct net *net)
2779 {
2780 #ifdef CONFIG_PROC_FS
2781 proc_net_remove(net, "ipv6_route");
2782 proc_net_remove(net, "rt6_stats");
2783 #endif
2784 kfree(net->ipv6.ip6_null_entry);
2785 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2786 kfree(net->ipv6.ip6_prohibit_entry);
2787 kfree(net->ipv6.ip6_blk_hole_entry);
2788 #endif
2789 dst_entries_destroy(&net->ipv6.ip6_dst_ops);
2790 }
2791
2792 static struct pernet_operations ip6_route_net_ops = {
2793 .init = ip6_route_net_init,
2794 .exit = ip6_route_net_exit,
2795 };
2796
2797 static struct notifier_block ip6_route_dev_notifier = {
2798 .notifier_call = ip6_route_dev_notify,
2799 .priority = 0,
2800 };
2801
2802 int __init ip6_route_init(void)
2803 {
2804 int ret;
2805
2806 ret = -ENOMEM;
2807 ip6_dst_ops_template.kmem_cachep =
2808 kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
2809 SLAB_HWCACHE_ALIGN, NULL);
2810 if (!ip6_dst_ops_template.kmem_cachep)
2811 goto out;
2812
2813 ret = dst_entries_init(&ip6_dst_blackhole_ops);
2814 if (ret)
2815 goto out_kmem_cache;
2816
2817 ret = register_pernet_subsys(&ip6_route_net_ops);
2818 if (ret)
2819 goto out_dst_entries;
2820
2821 ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep;
2822
2823 /* Registering of the loopback is done before this portion of code,
2824 * the loopback reference in rt6_info will not be taken, do it
2825 * manually for init_net */
2826 init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev;
2827 init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2828 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2829 init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev;
2830 init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2831 init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
2832 init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
2833 #endif
2834 ret = fib6_init();
2835 if (ret)
2836 goto out_register_subsys;
2837
2838 ret = xfrm6_init();
2839 if (ret)
2840 goto out_fib6_init;
2841
2842 ret = fib6_rules_init();
2843 if (ret)
2844 goto xfrm6_init;
2845
2846 ret = -ENOBUFS;
2847 if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL) ||
2848 __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL) ||
2849 __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL))
2850 goto fib6_rules_init;
2851
2852 ret = register_netdevice_notifier(&ip6_route_dev_notifier);
2853 if (ret)
2854 goto fib6_rules_init;
2855
2856 out:
2857 return ret;
2858
2859 fib6_rules_init:
2860 fib6_rules_cleanup();
2861 xfrm6_init:
2862 xfrm6_fini();
2863 out_fib6_init:
2864 fib6_gc_cleanup();
2865 out_register_subsys:
2866 unregister_pernet_subsys(&ip6_route_net_ops);
2867 out_dst_entries:
2868 dst_entries_destroy(&ip6_dst_blackhole_ops);
2869 out_kmem_cache:
2870 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
2871 goto out;
2872 }
2873
2874 void ip6_route_cleanup(void)
2875 {
2876 unregister_netdevice_notifier(&ip6_route_dev_notifier);
2877 fib6_rules_cleanup();
2878 xfrm6_fini();
2879 fib6_gc_cleanup();
2880 unregister_pernet_subsys(&ip6_route_net_ops);
2881 dst_entries_destroy(&ip6_dst_blackhole_ops);
2882 kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep);
2883 }
This page took 0.117126 seconds and 6 git commands to generate.