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