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