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 | 7 | * |
1da177e4 LT |
8 | * This program is free software; you can redistribute it and/or |
9 | * modify it under the terms of the GNU General Public License | |
10 | * as published by the Free Software Foundation; either version | |
11 | * 2 of the License, or (at your option) any later version. | |
12 | */ | |
13 | ||
14 | /* Changes: | |
15 | * | |
16 | * YOSHIFUJI Hideaki @USAGI | |
17 | * reworked default router selection. | |
18 | * - respect outgoing interface | |
19 | * - select from (probably) reachable routers (i.e. | |
20 | * routers in REACHABLE, STALE, DELAY or PROBE states). | |
21 | * - always select the same router if it is (probably) | |
22 | * reachable. otherwise, round-robin the list. | |
c0bece9f YH |
23 | * Ville Nuorvala |
24 | * Fixed routing subtrees. | |
1da177e4 LT |
25 | */ |
26 | ||
f3213831 JP |
27 | #define pr_fmt(fmt) "IPv6: " fmt |
28 | ||
4fc268d2 | 29 | #include <linux/capability.h> |
1da177e4 | 30 | #include <linux/errno.h> |
bc3b2d7f | 31 | #include <linux/export.h> |
1da177e4 LT |
32 | #include <linux/types.h> |
33 | #include <linux/times.h> | |
34 | #include <linux/socket.h> | |
35 | #include <linux/sockios.h> | |
36 | #include <linux/net.h> | |
37 | #include <linux/route.h> | |
38 | #include <linux/netdevice.h> | |
39 | #include <linux/in6.h> | |
7bc570c8 | 40 | #include <linux/mroute6.h> |
1da177e4 | 41 | #include <linux/init.h> |
1da177e4 | 42 | #include <linux/if_arp.h> |
1da177e4 LT |
43 | #include <linux/proc_fs.h> |
44 | #include <linux/seq_file.h> | |
5b7c931d | 45 | #include <linux/nsproxy.h> |
5a0e3ad6 | 46 | #include <linux/slab.h> |
457c4cbc | 47 | #include <net/net_namespace.h> |
1da177e4 LT |
48 | #include <net/snmp.h> |
49 | #include <net/ipv6.h> | |
50 | #include <net/ip6_fib.h> | |
51 | #include <net/ip6_route.h> | |
52 | #include <net/ndisc.h> | |
53 | #include <net/addrconf.h> | |
54 | #include <net/tcp.h> | |
55 | #include <linux/rtnetlink.h> | |
56 | #include <net/dst.h> | |
57 | #include <net/xfrm.h> | |
8d71740c | 58 | #include <net/netevent.h> |
21713ebc | 59 | #include <net/netlink.h> |
51ebd318 | 60 | #include <net/nexthop.h> |
1da177e4 LT |
61 | |
62 | #include <asm/uaccess.h> | |
63 | ||
64 | #ifdef CONFIG_SYSCTL | |
65 | #include <linux/sysctl.h> | |
66 | #endif | |
67 | ||
b4f1489e HFS |
68 | enum rt6_nud_state { |
69 | RT6_NUD_FAIL_HARD = -2, | |
70 | RT6_NUD_FAIL_SOFT = -1, | |
71 | RT6_NUD_SUCCEED = 1 | |
72 | }; | |
73 | ||
1716a961 | 74 | static struct rt6_info *ip6_rt_copy(struct rt6_info *ort, |
21efcfa0 | 75 | const struct in6_addr *dest); |
1da177e4 | 76 | static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie); |
0dbaee3b | 77 | static unsigned int ip6_default_advmss(const struct dst_entry *dst); |
ebb762f2 | 78 | static unsigned int ip6_mtu(const struct dst_entry *dst); |
1da177e4 LT |
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); | |
5af58eb0 K |
87 | static int ip6_pkt_prohibit(struct sk_buff *skb); |
88 | static int ip6_pkt_prohibit_out(struct sk_buff *skb); | |
1da177e4 | 89 | static void ip6_link_failure(struct sk_buff *skb); |
6700c270 DM |
90 | static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, |
91 | struct sk_buff *skb, u32 mtu); | |
92 | static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, | |
93 | struct sk_buff *skb); | |
1da177e4 | 94 | |
70ceb4f5 | 95 | #ifdef CONFIG_IPV6_ROUTE_INFO |
efa2cea0 | 96 | static struct rt6_info *rt6_add_route_info(struct net *net, |
b71d1d42 ED |
97 | const struct in6_addr *prefix, int prefixlen, |
98 | const struct in6_addr *gwaddr, int ifindex, | |
95c96174 | 99 | unsigned int pref); |
efa2cea0 | 100 | static struct rt6_info *rt6_get_route_info(struct net *net, |
b71d1d42 ED |
101 | const struct in6_addr *prefix, int prefixlen, |
102 | const struct in6_addr *gwaddr, int ifindex); | |
70ceb4f5 YH |
103 | #endif |
104 | ||
06582540 DM |
105 | static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old) |
106 | { | |
107 | struct rt6_info *rt = (struct rt6_info *) dst; | |
108 | struct inet_peer *peer; | |
109 | u32 *p = NULL; | |
110 | ||
8e2ec639 YZ |
111 | if (!(rt->dst.flags & DST_HOST)) |
112 | return NULL; | |
113 | ||
fbfe95a4 | 114 | peer = rt6_get_peer_create(rt); |
06582540 DM |
115 | if (peer) { |
116 | u32 *old_p = __DST_METRICS_PTR(old); | |
117 | unsigned long prev, new; | |
118 | ||
119 | p = peer->metrics; | |
120 | if (inet_metrics_new(peer)) | |
121 | memcpy(p, old_p, sizeof(u32) * RTAX_MAX); | |
122 | ||
123 | new = (unsigned long) p; | |
124 | prev = cmpxchg(&dst->_metrics, old, new); | |
125 | ||
126 | if (prev != old) { | |
127 | p = __DST_METRICS_PTR(prev); | |
128 | if (prev & DST_METRICS_READ_ONLY) | |
129 | p = NULL; | |
130 | } | |
131 | } | |
132 | return p; | |
133 | } | |
134 | ||
f894cbf8 DM |
135 | static inline const void *choose_neigh_daddr(struct rt6_info *rt, |
136 | struct sk_buff *skb, | |
137 | const void *daddr) | |
39232973 DM |
138 | { |
139 | struct in6_addr *p = &rt->rt6i_gateway; | |
140 | ||
a7563f34 | 141 | if (!ipv6_addr_any(p)) |
39232973 | 142 | return (const void *) p; |
f894cbf8 DM |
143 | else if (skb) |
144 | return &ipv6_hdr(skb)->daddr; | |
39232973 DM |
145 | return daddr; |
146 | } | |
147 | ||
f894cbf8 DM |
148 | static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst, |
149 | struct sk_buff *skb, | |
150 | const void *daddr) | |
d3aaeb38 | 151 | { |
39232973 DM |
152 | struct rt6_info *rt = (struct rt6_info *) dst; |
153 | struct neighbour *n; | |
154 | ||
f894cbf8 | 155 | daddr = choose_neigh_daddr(rt, skb, daddr); |
8e022ee6 | 156 | n = __ipv6_neigh_lookup(dst->dev, daddr); |
f83c7790 DM |
157 | if (n) |
158 | return n; | |
159 | return neigh_create(&nd_tbl, daddr, dst->dev); | |
160 | } | |
161 | ||
9a7ec3a9 | 162 | static struct dst_ops ip6_dst_ops_template = { |
1da177e4 | 163 | .family = AF_INET6, |
09640e63 | 164 | .protocol = cpu_to_be16(ETH_P_IPV6), |
1da177e4 LT |
165 | .gc = ip6_dst_gc, |
166 | .gc_thresh = 1024, | |
167 | .check = ip6_dst_check, | |
0dbaee3b | 168 | .default_advmss = ip6_default_advmss, |
ebb762f2 | 169 | .mtu = ip6_mtu, |
06582540 | 170 | .cow_metrics = ipv6_cow_metrics, |
1da177e4 LT |
171 | .destroy = ip6_dst_destroy, |
172 | .ifdown = ip6_dst_ifdown, | |
173 | .negative_advice = ip6_negative_advice, | |
174 | .link_failure = ip6_link_failure, | |
175 | .update_pmtu = ip6_rt_update_pmtu, | |
6e157b6a | 176 | .redirect = rt6_do_redirect, |
1ac06e03 | 177 | .local_out = __ip6_local_out, |
d3aaeb38 | 178 | .neigh_lookup = ip6_neigh_lookup, |
1da177e4 LT |
179 | }; |
180 | ||
ebb762f2 | 181 | static unsigned int ip6_blackhole_mtu(const struct dst_entry *dst) |
ec831ea7 | 182 | { |
618f9bc7 SK |
183 | unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); |
184 | ||
185 | return mtu ? : dst->dev->mtu; | |
ec831ea7 RD |
186 | } |
187 | ||
6700c270 DM |
188 | static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, struct sock *sk, |
189 | struct sk_buff *skb, u32 mtu) | |
14e50e57 DM |
190 | { |
191 | } | |
192 | ||
6700c270 DM |
193 | static void ip6_rt_blackhole_redirect(struct dst_entry *dst, struct sock *sk, |
194 | struct sk_buff *skb) | |
b587ee3b DM |
195 | { |
196 | } | |
197 | ||
0972ddb2 HB |
198 | static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst, |
199 | unsigned long old) | |
200 | { | |
201 | return NULL; | |
202 | } | |
203 | ||
14e50e57 DM |
204 | static struct dst_ops ip6_dst_blackhole_ops = { |
205 | .family = AF_INET6, | |
09640e63 | 206 | .protocol = cpu_to_be16(ETH_P_IPV6), |
14e50e57 DM |
207 | .destroy = ip6_dst_destroy, |
208 | .check = ip6_dst_check, | |
ebb762f2 | 209 | .mtu = ip6_blackhole_mtu, |
214f45c9 | 210 | .default_advmss = ip6_default_advmss, |
14e50e57 | 211 | .update_pmtu = ip6_rt_blackhole_update_pmtu, |
b587ee3b | 212 | .redirect = ip6_rt_blackhole_redirect, |
0972ddb2 | 213 | .cow_metrics = ip6_rt_blackhole_cow_metrics, |
d3aaeb38 | 214 | .neigh_lookup = ip6_neigh_lookup, |
14e50e57 DM |
215 | }; |
216 | ||
62fa8a84 | 217 | static const u32 ip6_template_metrics[RTAX_MAX] = { |
14edd87d | 218 | [RTAX_HOPLIMIT - 1] = 0, |
62fa8a84 DM |
219 | }; |
220 | ||
fb0af4c7 | 221 | static const struct rt6_info ip6_null_entry_template = { |
d8d1f30b CG |
222 | .dst = { |
223 | .__refcnt = ATOMIC_INIT(1), | |
224 | .__use = 1, | |
2c20cbd7 | 225 | .obsolete = DST_OBSOLETE_FORCE_CHK, |
d8d1f30b | 226 | .error = -ENETUNREACH, |
d8d1f30b CG |
227 | .input = ip6_pkt_discard, |
228 | .output = ip6_pkt_discard_out, | |
1da177e4 LT |
229 | }, |
230 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), | |
4f724279 | 231 | .rt6i_protocol = RTPROT_KERNEL, |
1da177e4 LT |
232 | .rt6i_metric = ~(u32) 0, |
233 | .rt6i_ref = ATOMIC_INIT(1), | |
234 | }; | |
235 | ||
101367c2 TG |
236 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
237 | ||
fb0af4c7 | 238 | static const struct rt6_info ip6_prohibit_entry_template = { |
d8d1f30b CG |
239 | .dst = { |
240 | .__refcnt = ATOMIC_INIT(1), | |
241 | .__use = 1, | |
2c20cbd7 | 242 | .obsolete = DST_OBSOLETE_FORCE_CHK, |
d8d1f30b | 243 | .error = -EACCES, |
d8d1f30b CG |
244 | .input = ip6_pkt_prohibit, |
245 | .output = ip6_pkt_prohibit_out, | |
101367c2 TG |
246 | }, |
247 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), | |
4f724279 | 248 | .rt6i_protocol = RTPROT_KERNEL, |
101367c2 TG |
249 | .rt6i_metric = ~(u32) 0, |
250 | .rt6i_ref = ATOMIC_INIT(1), | |
251 | }; | |
252 | ||
fb0af4c7 | 253 | static const struct rt6_info ip6_blk_hole_entry_template = { |
d8d1f30b CG |
254 | .dst = { |
255 | .__refcnt = ATOMIC_INIT(1), | |
256 | .__use = 1, | |
2c20cbd7 | 257 | .obsolete = DST_OBSOLETE_FORCE_CHK, |
d8d1f30b | 258 | .error = -EINVAL, |
d8d1f30b CG |
259 | .input = dst_discard, |
260 | .output = dst_discard, | |
101367c2 TG |
261 | }, |
262 | .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP), | |
4f724279 | 263 | .rt6i_protocol = RTPROT_KERNEL, |
101367c2 TG |
264 | .rt6i_metric = ~(u32) 0, |
265 | .rt6i_ref = ATOMIC_INIT(1), | |
266 | }; | |
267 | ||
268 | #endif | |
269 | ||
1da177e4 | 270 | /* allocate dst with ip6_dst_ops */ |
97bab73f | 271 | static inline struct rt6_info *ip6_dst_alloc(struct net *net, |
957c665f | 272 | struct net_device *dev, |
8b96d22d DM |
273 | int flags, |
274 | struct fib6_table *table) | |
1da177e4 | 275 | { |
97bab73f | 276 | struct rt6_info *rt = dst_alloc(&net->ipv6.ip6_dst_ops, dev, |
6f3118b5 | 277 | 0, DST_OBSOLETE_FORCE_CHK, flags); |
cf911662 | 278 | |
97bab73f | 279 | if (rt) { |
8104891b SK |
280 | struct dst_entry *dst = &rt->dst; |
281 | ||
282 | memset(dst + 1, 0, sizeof(*rt) - sizeof(*dst)); | |
8b96d22d | 283 | rt6_init_peer(rt, table ? &table->tb6_peers : net->ipv6.peers); |
6f3118b5 | 284 | rt->rt6i_genid = rt_genid(net); |
51ebd318 ND |
285 | INIT_LIST_HEAD(&rt->rt6i_siblings); |
286 | rt->rt6i_nsiblings = 0; | |
97bab73f | 287 | } |
cf911662 | 288 | return rt; |
1da177e4 LT |
289 | } |
290 | ||
291 | static void ip6_dst_destroy(struct dst_entry *dst) | |
292 | { | |
293 | struct rt6_info *rt = (struct rt6_info *)dst; | |
294 | struct inet6_dev *idev = rt->rt6i_idev; | |
ecd98837 | 295 | struct dst_entry *from = dst->from; |
1da177e4 | 296 | |
8e2ec639 YZ |
297 | if (!(rt->dst.flags & DST_HOST)) |
298 | dst_destroy_metrics_generic(dst); | |
299 | ||
38308473 | 300 | if (idev) { |
1da177e4 LT |
301 | rt->rt6i_idev = NULL; |
302 | in6_dev_put(idev); | |
1ab1457c | 303 | } |
1716a961 | 304 | |
ecd98837 YH |
305 | dst->from = NULL; |
306 | dst_release(from); | |
1716a961 | 307 | |
97bab73f DM |
308 | if (rt6_has_peer(rt)) { |
309 | struct inet_peer *peer = rt6_peer_ptr(rt); | |
b3419363 DM |
310 | inet_putpeer(peer); |
311 | } | |
312 | } | |
313 | ||
314 | void rt6_bind_peer(struct rt6_info *rt, int create) | |
315 | { | |
97bab73f | 316 | struct inet_peer_base *base; |
b3419363 DM |
317 | struct inet_peer *peer; |
318 | ||
97bab73f DM |
319 | base = inetpeer_base_ptr(rt->_rt6i_peer); |
320 | if (!base) | |
321 | return; | |
322 | ||
323 | peer = inet_getpeer_v6(base, &rt->rt6i_dst.addr, create); | |
7b34ca2a DM |
324 | if (peer) { |
325 | if (!rt6_set_peer(rt, peer)) | |
326 | inet_putpeer(peer); | |
7b34ca2a | 327 | } |
1da177e4 LT |
328 | } |
329 | ||
330 | static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, | |
331 | int how) | |
332 | { | |
333 | struct rt6_info *rt = (struct rt6_info *)dst; | |
334 | struct inet6_dev *idev = rt->rt6i_idev; | |
5a3e55d6 | 335 | struct net_device *loopback_dev = |
c346dca1 | 336 | dev_net(dev)->loopback_dev; |
1da177e4 | 337 | |
97cac082 DM |
338 | if (dev != loopback_dev) { |
339 | if (idev && idev->dev == dev) { | |
340 | struct inet6_dev *loopback_idev = | |
341 | in6_dev_get(loopback_dev); | |
342 | if (loopback_idev) { | |
343 | rt->rt6i_idev = loopback_idev; | |
344 | in6_dev_put(idev); | |
345 | } | |
346 | } | |
1da177e4 LT |
347 | } |
348 | } | |
349 | ||
a50feda5 | 350 | static bool rt6_check_expired(const struct rt6_info *rt) |
1da177e4 | 351 | { |
1716a961 G |
352 | if (rt->rt6i_flags & RTF_EXPIRES) { |
353 | if (time_after(jiffies, rt->dst.expires)) | |
a50feda5 | 354 | return true; |
1716a961 | 355 | } else if (rt->dst.from) { |
3fd91fb3 | 356 | return rt6_check_expired((struct rt6_info *) rt->dst.from); |
1716a961 | 357 | } |
a50feda5 | 358 | return false; |
1da177e4 LT |
359 | } |
360 | ||
a50feda5 | 361 | static bool rt6_need_strict(const struct in6_addr *daddr) |
c71099ac | 362 | { |
a02cec21 ED |
363 | return ipv6_addr_type(daddr) & |
364 | (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); | |
c71099ac TG |
365 | } |
366 | ||
51ebd318 ND |
367 | /* Multipath route selection: |
368 | * Hash based function using packet header and flowlabel. | |
369 | * Adapted from fib_info_hashfn() | |
370 | */ | |
371 | static int rt6_info_hash_nhsfn(unsigned int candidate_count, | |
372 | const struct flowi6 *fl6) | |
373 | { | |
374 | unsigned int val = fl6->flowi6_proto; | |
375 | ||
c08977bb YH |
376 | val ^= ipv6_addr_hash(&fl6->daddr); |
377 | val ^= ipv6_addr_hash(&fl6->saddr); | |
51ebd318 ND |
378 | |
379 | /* Work only if this not encapsulated */ | |
380 | switch (fl6->flowi6_proto) { | |
381 | case IPPROTO_UDP: | |
382 | case IPPROTO_TCP: | |
383 | case IPPROTO_SCTP: | |
b3ce5ae1 ND |
384 | val ^= (__force u16)fl6->fl6_sport; |
385 | val ^= (__force u16)fl6->fl6_dport; | |
51ebd318 ND |
386 | break; |
387 | ||
388 | case IPPROTO_ICMPV6: | |
b3ce5ae1 ND |
389 | val ^= (__force u16)fl6->fl6_icmp_type; |
390 | val ^= (__force u16)fl6->fl6_icmp_code; | |
51ebd318 ND |
391 | break; |
392 | } | |
393 | /* RFC6438 recommands to use flowlabel */ | |
b3ce5ae1 | 394 | val ^= (__force u32)fl6->flowlabel; |
51ebd318 ND |
395 | |
396 | /* Perhaps, we need to tune, this function? */ | |
397 | val = val ^ (val >> 7) ^ (val >> 12); | |
398 | return val % candidate_count; | |
399 | } | |
400 | ||
401 | static struct rt6_info *rt6_multipath_select(struct rt6_info *match, | |
402 | struct flowi6 *fl6) | |
403 | { | |
404 | struct rt6_info *sibling, *next_sibling; | |
405 | int route_choosen; | |
406 | ||
407 | route_choosen = rt6_info_hash_nhsfn(match->rt6i_nsiblings + 1, fl6); | |
408 | /* Don't change the route, if route_choosen == 0 | |
409 | * (siblings does not include ourself) | |
410 | */ | |
411 | if (route_choosen) | |
412 | list_for_each_entry_safe(sibling, next_sibling, | |
413 | &match->rt6i_siblings, rt6i_siblings) { | |
414 | route_choosen--; | |
415 | if (route_choosen == 0) { | |
416 | match = sibling; | |
417 | break; | |
418 | } | |
419 | } | |
420 | return match; | |
421 | } | |
422 | ||
1da177e4 | 423 | /* |
c71099ac | 424 | * Route lookup. Any table->tb6_lock is implied. |
1da177e4 LT |
425 | */ |
426 | ||
8ed67789 DL |
427 | static inline struct rt6_info *rt6_device_match(struct net *net, |
428 | struct rt6_info *rt, | |
b71d1d42 | 429 | const struct in6_addr *saddr, |
1da177e4 | 430 | int oif, |
d420895e | 431 | int flags) |
1da177e4 LT |
432 | { |
433 | struct rt6_info *local = NULL; | |
434 | struct rt6_info *sprt; | |
435 | ||
dd3abc4e YH |
436 | if (!oif && ipv6_addr_any(saddr)) |
437 | goto out; | |
438 | ||
d8d1f30b | 439 | for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) { |
d1918542 | 440 | struct net_device *dev = sprt->dst.dev; |
dd3abc4e YH |
441 | |
442 | if (oif) { | |
1da177e4 LT |
443 | if (dev->ifindex == oif) |
444 | return sprt; | |
445 | if (dev->flags & IFF_LOOPBACK) { | |
38308473 | 446 | if (!sprt->rt6i_idev || |
1da177e4 | 447 | sprt->rt6i_idev->dev->ifindex != oif) { |
d420895e | 448 | if (flags & RT6_LOOKUP_F_IFACE && oif) |
1da177e4 | 449 | continue; |
1ab1457c | 450 | if (local && (!oif || |
1da177e4 LT |
451 | local->rt6i_idev->dev->ifindex == oif)) |
452 | continue; | |
453 | } | |
454 | local = sprt; | |
455 | } | |
dd3abc4e YH |
456 | } else { |
457 | if (ipv6_chk_addr(net, saddr, dev, | |
458 | flags & RT6_LOOKUP_F_IFACE)) | |
459 | return sprt; | |
1da177e4 | 460 | } |
dd3abc4e | 461 | } |
1da177e4 | 462 | |
dd3abc4e | 463 | if (oif) { |
1da177e4 LT |
464 | if (local) |
465 | return local; | |
466 | ||
d420895e | 467 | if (flags & RT6_LOOKUP_F_IFACE) |
8ed67789 | 468 | return net->ipv6.ip6_null_entry; |
1da177e4 | 469 | } |
dd3abc4e | 470 | out: |
1da177e4 LT |
471 | return rt; |
472 | } | |
473 | ||
27097255 | 474 | #ifdef CONFIG_IPV6_ROUTER_PREF |
8df627da HFS |
475 | struct __rt6_probe_work { |
476 | struct work_struct work; | |
477 | struct in6_addr target; | |
478 | struct net_device *dev; | |
479 | }; | |
480 | ||
481 | static void rt6_probe_deferred(struct work_struct *w) | |
482 | { | |
483 | struct in6_addr mcaddr; | |
484 | struct __rt6_probe_work *work = | |
485 | container_of(w, struct __rt6_probe_work, work); | |
486 | ||
487 | addrconf_addr_solict_mult(&work->target, &mcaddr); | |
488 | ndisc_send_ns(work->dev, NULL, &work->target, &mcaddr, NULL); | |
489 | dev_put(work->dev); | |
490 | kfree(w); | |
491 | } | |
492 | ||
27097255 YH |
493 | static void rt6_probe(struct rt6_info *rt) |
494 | { | |
f2c31e32 | 495 | struct neighbour *neigh; |
27097255 YH |
496 | /* |
497 | * Okay, this does not seem to be appropriate | |
498 | * for now, however, we need to check if it | |
499 | * is really so; aka Router Reachability Probing. | |
500 | * | |
501 | * Router Reachability Probe MUST be rate-limited | |
502 | * to no more than one per minute. | |
503 | */ | |
2152caea | 504 | if (!rt || !(rt->rt6i_flags & RTF_GATEWAY)) |
7ff74a59 | 505 | return; |
2152caea YH |
506 | rcu_read_lock_bh(); |
507 | neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); | |
508 | if (neigh) { | |
509 | write_lock(&neigh->lock); | |
510 | if (neigh->nud_state & NUD_VALID) | |
511 | goto out; | |
7ff74a59 | 512 | } |
2152caea YH |
513 | |
514 | if (!neigh || | |
52e16356 | 515 | time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) { |
8df627da | 516 | struct __rt6_probe_work *work; |
27097255 | 517 | |
8df627da HFS |
518 | work = kmalloc(sizeof(*work), GFP_ATOMIC); |
519 | ||
520 | if (neigh && work) | |
b820bb6b | 521 | neigh->updated = jiffies; |
8df627da HFS |
522 | |
523 | if (neigh) | |
2152caea YH |
524 | write_unlock(&neigh->lock); |
525 | ||
8df627da HFS |
526 | if (work) { |
527 | INIT_WORK(&work->work, rt6_probe_deferred); | |
528 | work->target = rt->rt6i_gateway; | |
529 | dev_hold(rt->dst.dev); | |
530 | work->dev = rt->dst.dev; | |
531 | schedule_work(&work->work); | |
532 | } | |
f2c31e32 | 533 | } else { |
2152caea YH |
534 | out: |
535 | write_unlock(&neigh->lock); | |
f2c31e32 | 536 | } |
2152caea | 537 | rcu_read_unlock_bh(); |
27097255 YH |
538 | } |
539 | #else | |
540 | static inline void rt6_probe(struct rt6_info *rt) | |
541 | { | |
27097255 YH |
542 | } |
543 | #endif | |
544 | ||
1da177e4 | 545 | /* |
554cfb7e | 546 | * Default Router Selection (RFC 2461 6.3.6) |
1da177e4 | 547 | */ |
b6f99a21 | 548 | static inline int rt6_check_dev(struct rt6_info *rt, int oif) |
554cfb7e | 549 | { |
d1918542 | 550 | struct net_device *dev = rt->dst.dev; |
161980f4 | 551 | if (!oif || dev->ifindex == oif) |
554cfb7e | 552 | return 2; |
161980f4 DM |
553 | if ((dev->flags & IFF_LOOPBACK) && |
554 | rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) | |
555 | return 1; | |
556 | return 0; | |
554cfb7e | 557 | } |
1da177e4 | 558 | |
b4f1489e | 559 | static inline enum rt6_nud_state rt6_check_neigh(struct rt6_info *rt) |
1da177e4 | 560 | { |
f2c31e32 | 561 | struct neighbour *neigh; |
b4f1489e | 562 | enum rt6_nud_state ret = RT6_NUD_FAIL_HARD; |
f2c31e32 | 563 | |
4d0c5911 YH |
564 | if (rt->rt6i_flags & RTF_NONEXTHOP || |
565 | !(rt->rt6i_flags & RTF_GATEWAY)) | |
b4f1489e | 566 | return RT6_NUD_SUCCEED; |
145a3621 YH |
567 | |
568 | rcu_read_lock_bh(); | |
569 | neigh = __ipv6_neigh_lookup_noref(rt->dst.dev, &rt->rt6i_gateway); | |
570 | if (neigh) { | |
571 | read_lock(&neigh->lock); | |
554cfb7e | 572 | if (neigh->nud_state & NUD_VALID) |
b4f1489e | 573 | ret = RT6_NUD_SUCCEED; |
398bcbeb | 574 | #ifdef CONFIG_IPV6_ROUTER_PREF |
a5a81f0b | 575 | else if (!(neigh->nud_state & NUD_FAILED)) |
b4f1489e | 576 | ret = RT6_NUD_SUCCEED; |
398bcbeb | 577 | #endif |
145a3621 | 578 | read_unlock(&neigh->lock); |
b4f1489e HFS |
579 | } else { |
580 | ret = IS_ENABLED(CONFIG_IPV6_ROUTER_PREF) ? | |
581 | RT6_NUD_SUCCEED : RT6_NUD_FAIL_SOFT; | |
a5a81f0b | 582 | } |
145a3621 YH |
583 | rcu_read_unlock_bh(); |
584 | ||
a5a81f0b | 585 | return ret; |
1da177e4 LT |
586 | } |
587 | ||
554cfb7e YH |
588 | static int rt6_score_route(struct rt6_info *rt, int oif, |
589 | int strict) | |
1da177e4 | 590 | { |
a5a81f0b | 591 | int m; |
1ab1457c | 592 | |
4d0c5911 | 593 | m = rt6_check_dev(rt, oif); |
77d16f45 | 594 | if (!m && (strict & RT6_LOOKUP_F_IFACE)) |
b4f1489e | 595 | return RT6_NUD_FAIL_HARD; |
ebacaaa0 YH |
596 | #ifdef CONFIG_IPV6_ROUTER_PREF |
597 | m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; | |
598 | #endif | |
b4f1489e HFS |
599 | if (strict & RT6_LOOKUP_F_REACHABLE) { |
600 | int n = rt6_check_neigh(rt); | |
601 | if (n < 0) | |
602 | return n; | |
603 | } | |
554cfb7e YH |
604 | return m; |
605 | } | |
606 | ||
f11e6659 | 607 | static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, |
b4f1489e HFS |
608 | int *mpri, struct rt6_info *match, |
609 | bool *do_rr) | |
554cfb7e | 610 | { |
f11e6659 | 611 | int m; |
b4f1489e | 612 | bool match_do_rr = false; |
f11e6659 DM |
613 | |
614 | if (rt6_check_expired(rt)) | |
615 | goto out; | |
616 | ||
617 | m = rt6_score_route(rt, oif, strict); | |
b4f1489e HFS |
618 | if (m == RT6_NUD_FAIL_SOFT && !IS_ENABLED(CONFIG_IPV6_ROUTER_PREF)) { |
619 | match_do_rr = true; | |
620 | m = 0; /* lowest valid score */ | |
621 | } else if (m < 0) { | |
f11e6659 | 622 | goto out; |
b4f1489e HFS |
623 | } |
624 | ||
625 | if (strict & RT6_LOOKUP_F_REACHABLE) | |
626 | rt6_probe(rt); | |
f11e6659 DM |
627 | |
628 | if (m > *mpri) { | |
b4f1489e | 629 | *do_rr = match_do_rr; |
f11e6659 DM |
630 | *mpri = m; |
631 | match = rt; | |
f11e6659 | 632 | } |
f11e6659 DM |
633 | out: |
634 | return match; | |
635 | } | |
636 | ||
637 | static struct rt6_info *find_rr_leaf(struct fib6_node *fn, | |
638 | struct rt6_info *rr_head, | |
b4f1489e HFS |
639 | u32 metric, int oif, int strict, |
640 | bool *do_rr) | |
f11e6659 DM |
641 | { |
642 | struct rt6_info *rt, *match; | |
554cfb7e | 643 | int mpri = -1; |
1da177e4 | 644 | |
f11e6659 DM |
645 | match = NULL; |
646 | for (rt = rr_head; rt && rt->rt6i_metric == metric; | |
d8d1f30b | 647 | rt = rt->dst.rt6_next) |
b4f1489e | 648 | match = find_match(rt, oif, strict, &mpri, match, do_rr); |
f11e6659 | 649 | for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric; |
d8d1f30b | 650 | rt = rt->dst.rt6_next) |
b4f1489e | 651 | match = find_match(rt, oif, strict, &mpri, match, do_rr); |
1da177e4 | 652 | |
f11e6659 DM |
653 | return match; |
654 | } | |
1da177e4 | 655 | |
f11e6659 DM |
656 | static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict) |
657 | { | |
658 | struct rt6_info *match, *rt0; | |
8ed67789 | 659 | struct net *net; |
b4f1489e | 660 | bool do_rr = false; |
1da177e4 | 661 | |
f11e6659 DM |
662 | rt0 = fn->rr_ptr; |
663 | if (!rt0) | |
664 | fn->rr_ptr = rt0 = fn->leaf; | |
1da177e4 | 665 | |
b4f1489e HFS |
666 | match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict, |
667 | &do_rr); | |
1da177e4 | 668 | |
b4f1489e | 669 | if (do_rr) { |
d8d1f30b | 670 | struct rt6_info *next = rt0->dst.rt6_next; |
f11e6659 | 671 | |
554cfb7e | 672 | /* no entries matched; do round-robin */ |
f11e6659 DM |
673 | if (!next || next->rt6i_metric != rt0->rt6i_metric) |
674 | next = fn->leaf; | |
675 | ||
676 | if (next != rt0) | |
677 | fn->rr_ptr = next; | |
1da177e4 | 678 | } |
1da177e4 | 679 | |
d1918542 | 680 | net = dev_net(rt0->dst.dev); |
a02cec21 | 681 | return match ? match : net->ipv6.ip6_null_entry; |
1da177e4 LT |
682 | } |
683 | ||
70ceb4f5 YH |
684 | #ifdef CONFIG_IPV6_ROUTE_INFO |
685 | int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, | |
b71d1d42 | 686 | const struct in6_addr *gwaddr) |
70ceb4f5 | 687 | { |
c346dca1 | 688 | struct net *net = dev_net(dev); |
70ceb4f5 YH |
689 | struct route_info *rinfo = (struct route_info *) opt; |
690 | struct in6_addr prefix_buf, *prefix; | |
691 | unsigned int pref; | |
4bed72e4 | 692 | unsigned long lifetime; |
70ceb4f5 YH |
693 | struct rt6_info *rt; |
694 | ||
695 | if (len < sizeof(struct route_info)) { | |
696 | return -EINVAL; | |
697 | } | |
698 | ||
699 | /* Sanity check for prefix_len and length */ | |
700 | if (rinfo->length > 3) { | |
701 | return -EINVAL; | |
702 | } else if (rinfo->prefix_len > 128) { | |
703 | return -EINVAL; | |
704 | } else if (rinfo->prefix_len > 64) { | |
705 | if (rinfo->length < 2) { | |
706 | return -EINVAL; | |
707 | } | |
708 | } else if (rinfo->prefix_len > 0) { | |
709 | if (rinfo->length < 1) { | |
710 | return -EINVAL; | |
711 | } | |
712 | } | |
713 | ||
714 | pref = rinfo->route_pref; | |
715 | if (pref == ICMPV6_ROUTER_PREF_INVALID) | |
3933fc95 | 716 | return -EINVAL; |
70ceb4f5 | 717 | |
4bed72e4 | 718 | lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); |
70ceb4f5 YH |
719 | |
720 | if (rinfo->length == 3) | |
721 | prefix = (struct in6_addr *)rinfo->prefix; | |
722 | else { | |
723 | /* this function is safe */ | |
724 | ipv6_addr_prefix(&prefix_buf, | |
725 | (struct in6_addr *)rinfo->prefix, | |
726 | rinfo->prefix_len); | |
727 | prefix = &prefix_buf; | |
728 | } | |
729 | ||
ea136c0d DJ |
730 | if (rinfo->prefix_len == 0) |
731 | rt = rt6_get_dflt_router(gwaddr, dev); | |
732 | else | |
733 | rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, | |
734 | gwaddr, dev->ifindex); | |
70ceb4f5 YH |
735 | |
736 | if (rt && !lifetime) { | |
e0a1ad73 | 737 | ip6_del_rt(rt); |
70ceb4f5 YH |
738 | rt = NULL; |
739 | } | |
740 | ||
741 | if (!rt && lifetime) | |
efa2cea0 | 742 | rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex, |
70ceb4f5 YH |
743 | pref); |
744 | else if (rt) | |
745 | rt->rt6i_flags = RTF_ROUTEINFO | | |
746 | (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); | |
747 | ||
748 | if (rt) { | |
1716a961 G |
749 | if (!addrconf_finite_timeout(lifetime)) |
750 | rt6_clean_expires(rt); | |
751 | else | |
752 | rt6_set_expires(rt, jiffies + HZ * lifetime); | |
753 | ||
94e187c0 | 754 | ip6_rt_put(rt); |
70ceb4f5 YH |
755 | } |
756 | return 0; | |
757 | } | |
758 | #endif | |
759 | ||
8ed67789 | 760 | #define BACKTRACK(__net, saddr) \ |
982f56f3 | 761 | do { \ |
8ed67789 | 762 | if (rt == __net->ipv6.ip6_null_entry) { \ |
982f56f3 | 763 | struct fib6_node *pn; \ |
e0eda7bb | 764 | while (1) { \ |
982f56f3 YH |
765 | if (fn->fn_flags & RTN_TL_ROOT) \ |
766 | goto out; \ | |
767 | pn = fn->parent; \ | |
768 | if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \ | |
8bce65b9 | 769 | fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \ |
982f56f3 YH |
770 | else \ |
771 | fn = pn; \ | |
772 | if (fn->fn_flags & RTN_RTINFO) \ | |
773 | goto restart; \ | |
c71099ac | 774 | } \ |
c71099ac | 775 | } \ |
38308473 | 776 | } while (0) |
c71099ac | 777 | |
8ed67789 DL |
778 | static struct rt6_info *ip6_pol_route_lookup(struct net *net, |
779 | struct fib6_table *table, | |
4c9483b2 | 780 | struct flowi6 *fl6, int flags) |
1da177e4 LT |
781 | { |
782 | struct fib6_node *fn; | |
783 | struct rt6_info *rt; | |
784 | ||
c71099ac | 785 | read_lock_bh(&table->tb6_lock); |
4c9483b2 | 786 | fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
c71099ac TG |
787 | restart: |
788 | rt = fn->leaf; | |
4c9483b2 | 789 | rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags); |
51ebd318 ND |
790 | if (rt->rt6i_nsiblings && fl6->flowi6_oif == 0) |
791 | rt = rt6_multipath_select(rt, fl6); | |
4c9483b2 | 792 | BACKTRACK(net, &fl6->saddr); |
c71099ac | 793 | out: |
d8d1f30b | 794 | dst_use(&rt->dst, jiffies); |
c71099ac | 795 | read_unlock_bh(&table->tb6_lock); |
c71099ac TG |
796 | return rt; |
797 | ||
798 | } | |
799 | ||
ea6e574e FW |
800 | struct dst_entry * ip6_route_lookup(struct net *net, struct flowi6 *fl6, |
801 | int flags) | |
802 | { | |
803 | return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_lookup); | |
804 | } | |
805 | EXPORT_SYMBOL_GPL(ip6_route_lookup); | |
806 | ||
9acd9f3a YH |
807 | struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, |
808 | const struct in6_addr *saddr, int oif, int strict) | |
c71099ac | 809 | { |
4c9483b2 DM |
810 | struct flowi6 fl6 = { |
811 | .flowi6_oif = oif, | |
812 | .daddr = *daddr, | |
c71099ac TG |
813 | }; |
814 | struct dst_entry *dst; | |
77d16f45 | 815 | int flags = strict ? RT6_LOOKUP_F_IFACE : 0; |
c71099ac | 816 | |
adaa70bb | 817 | if (saddr) { |
4c9483b2 | 818 | memcpy(&fl6.saddr, saddr, sizeof(*saddr)); |
adaa70bb TG |
819 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
820 | } | |
821 | ||
4c9483b2 | 822 | dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup); |
c71099ac TG |
823 | if (dst->error == 0) |
824 | return (struct rt6_info *) dst; | |
825 | ||
826 | dst_release(dst); | |
827 | ||
1da177e4 LT |
828 | return NULL; |
829 | } | |
830 | ||
7159039a YH |
831 | EXPORT_SYMBOL(rt6_lookup); |
832 | ||
c71099ac | 833 | /* ip6_ins_rt is called with FREE table->tb6_lock. |
1da177e4 LT |
834 | It takes new route entry, the addition fails by any reason the |
835 | route is freed. In any case, if caller does not hold it, it may | |
836 | be destroyed. | |
837 | */ | |
838 | ||
86872cb5 | 839 | static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info) |
1da177e4 LT |
840 | { |
841 | int err; | |
c71099ac | 842 | struct fib6_table *table; |
1da177e4 | 843 | |
c71099ac TG |
844 | table = rt->rt6i_table; |
845 | write_lock_bh(&table->tb6_lock); | |
86872cb5 | 846 | err = fib6_add(&table->tb6_root, rt, info); |
c71099ac | 847 | write_unlock_bh(&table->tb6_lock); |
1da177e4 LT |
848 | |
849 | return err; | |
850 | } | |
851 | ||
40e22e8f TG |
852 | int ip6_ins_rt(struct rt6_info *rt) |
853 | { | |
4d1169c1 | 854 | struct nl_info info = { |
d1918542 | 855 | .nl_net = dev_net(rt->dst.dev), |
4d1169c1 | 856 | }; |
528c4ceb | 857 | return __ip6_ins_rt(rt, &info); |
40e22e8f TG |
858 | } |
859 | ||
1716a961 | 860 | static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, |
21efcfa0 | 861 | const struct in6_addr *daddr, |
b71d1d42 | 862 | const struct in6_addr *saddr) |
1da177e4 | 863 | { |
1da177e4 LT |
864 | struct rt6_info *rt; |
865 | ||
866 | /* | |
867 | * Clone the route. | |
868 | */ | |
869 | ||
21efcfa0 | 870 | rt = ip6_rt_copy(ort, daddr); |
1da177e4 LT |
871 | |
872 | if (rt) { | |
38308473 | 873 | if (!(rt->rt6i_flags & RTF_GATEWAY)) { |
bb3c3686 | 874 | if (ort->rt6i_dst.plen != 128 && |
21efcfa0 | 875 | ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) |
58c4fb86 | 876 | rt->rt6i_flags |= RTF_ANYCAST; |
58c4fb86 | 877 | } |
1da177e4 | 878 | |
1da177e4 | 879 | rt->rt6i_flags |= RTF_CACHE; |
1da177e4 LT |
880 | |
881 | #ifdef CONFIG_IPV6_SUBTREES | |
882 | if (rt->rt6i_src.plen && saddr) { | |
4e3fd7a0 | 883 | rt->rt6i_src.addr = *saddr; |
1da177e4 LT |
884 | rt->rt6i_src.plen = 128; |
885 | } | |
886 | #endif | |
95a9a5ba | 887 | } |
1da177e4 | 888 | |
95a9a5ba YH |
889 | return rt; |
890 | } | |
1da177e4 | 891 | |
21efcfa0 ED |
892 | static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, |
893 | const struct in6_addr *daddr) | |
299d9939 | 894 | { |
21efcfa0 ED |
895 | struct rt6_info *rt = ip6_rt_copy(ort, daddr); |
896 | ||
887c95cc | 897 | if (rt) |
299d9939 | 898 | rt->rt6i_flags |= RTF_CACHE; |
299d9939 YH |
899 | return rt; |
900 | } | |
901 | ||
8ed67789 | 902 | static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif, |
4c9483b2 | 903 | struct flowi6 *fl6, int flags) |
1da177e4 LT |
904 | { |
905 | struct fib6_node *fn; | |
519fbd87 | 906 | struct rt6_info *rt, *nrt; |
c71099ac | 907 | int strict = 0; |
1da177e4 | 908 | int attempts = 3; |
519fbd87 | 909 | int err; |
53b7997f | 910 | int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE; |
1da177e4 | 911 | |
77d16f45 | 912 | strict |= flags & RT6_LOOKUP_F_IFACE; |
1da177e4 LT |
913 | |
914 | relookup: | |
c71099ac | 915 | read_lock_bh(&table->tb6_lock); |
1da177e4 | 916 | |
8238dd06 | 917 | restart_2: |
4c9483b2 | 918 | fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); |
1da177e4 LT |
919 | |
920 | restart: | |
4acad72d | 921 | rt = rt6_select(fn, oif, strict | reachable); |
51ebd318 ND |
922 | if (rt->rt6i_nsiblings && oif == 0) |
923 | rt = rt6_multipath_select(rt, fl6); | |
4c9483b2 | 924 | BACKTRACK(net, &fl6->saddr); |
8ed67789 | 925 | if (rt == net->ipv6.ip6_null_entry || |
8238dd06 | 926 | rt->rt6i_flags & RTF_CACHE) |
1ddef044 | 927 | goto out; |
1da177e4 | 928 | |
d8d1f30b | 929 | dst_hold(&rt->dst); |
c71099ac | 930 | read_unlock_bh(&table->tb6_lock); |
fb9de91e | 931 | |
c440f160 | 932 | if (!(rt->rt6i_flags & (RTF_NONEXTHOP | RTF_GATEWAY))) |
4c9483b2 | 933 | nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr); |
7343ff31 | 934 | else if (!(rt->dst.flags & DST_HOST)) |
4c9483b2 | 935 | nrt = rt6_alloc_clone(rt, &fl6->daddr); |
7343ff31 DM |
936 | else |
937 | goto out2; | |
e40cf353 | 938 | |
94e187c0 | 939 | ip6_rt_put(rt); |
8ed67789 | 940 | rt = nrt ? : net->ipv6.ip6_null_entry; |
1da177e4 | 941 | |
d8d1f30b | 942 | dst_hold(&rt->dst); |
519fbd87 | 943 | if (nrt) { |
40e22e8f | 944 | err = ip6_ins_rt(nrt); |
519fbd87 | 945 | if (!err) |
1da177e4 | 946 | goto out2; |
1da177e4 | 947 | } |
1da177e4 | 948 | |
519fbd87 YH |
949 | if (--attempts <= 0) |
950 | goto out2; | |
951 | ||
952 | /* | |
c71099ac | 953 | * Race condition! In the gap, when table->tb6_lock was |
519fbd87 YH |
954 | * released someone could insert this route. Relookup. |
955 | */ | |
94e187c0 | 956 | ip6_rt_put(rt); |
519fbd87 YH |
957 | goto relookup; |
958 | ||
959 | out: | |
8238dd06 YH |
960 | if (reachable) { |
961 | reachable = 0; | |
962 | goto restart_2; | |
963 | } | |
d8d1f30b | 964 | dst_hold(&rt->dst); |
c71099ac | 965 | read_unlock_bh(&table->tb6_lock); |
1da177e4 | 966 | out2: |
d8d1f30b CG |
967 | rt->dst.lastuse = jiffies; |
968 | rt->dst.__use++; | |
c71099ac TG |
969 | |
970 | return rt; | |
1da177e4 LT |
971 | } |
972 | ||
8ed67789 | 973 | static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, |
4c9483b2 | 974 | struct flowi6 *fl6, int flags) |
4acad72d | 975 | { |
4c9483b2 | 976 | return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); |
4acad72d PE |
977 | } |
978 | ||
72331bc0 SL |
979 | static struct dst_entry *ip6_route_input_lookup(struct net *net, |
980 | struct net_device *dev, | |
981 | struct flowi6 *fl6, int flags) | |
982 | { | |
983 | if (rt6_need_strict(&fl6->daddr) && dev->type != ARPHRD_PIMREG) | |
984 | flags |= RT6_LOOKUP_F_IFACE; | |
985 | ||
986 | return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_input); | |
987 | } | |
988 | ||
c71099ac TG |
989 | void ip6_route_input(struct sk_buff *skb) |
990 | { | |
b71d1d42 | 991 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
c346dca1 | 992 | struct net *net = dev_net(skb->dev); |
adaa70bb | 993 | int flags = RT6_LOOKUP_F_HAS_SADDR; |
4c9483b2 DM |
994 | struct flowi6 fl6 = { |
995 | .flowi6_iif = skb->dev->ifindex, | |
996 | .daddr = iph->daddr, | |
997 | .saddr = iph->saddr, | |
6502ca52 | 998 | .flowlabel = ip6_flowinfo(iph), |
4c9483b2 DM |
999 | .flowi6_mark = skb->mark, |
1000 | .flowi6_proto = iph->nexthdr, | |
c71099ac | 1001 | }; |
adaa70bb | 1002 | |
72331bc0 | 1003 | skb_dst_set(skb, ip6_route_input_lookup(net, skb->dev, &fl6, flags)); |
c71099ac TG |
1004 | } |
1005 | ||
8ed67789 | 1006 | static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, |
4c9483b2 | 1007 | struct flowi6 *fl6, int flags) |
1da177e4 | 1008 | { |
4c9483b2 | 1009 | return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); |
c71099ac TG |
1010 | } |
1011 | ||
9c7a4f9c | 1012 | struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk, |
4c9483b2 | 1013 | struct flowi6 *fl6) |
c71099ac TG |
1014 | { |
1015 | int flags = 0; | |
1016 | ||
1fb9489b | 1017 | fl6->flowi6_iif = LOOPBACK_IFINDEX; |
4dc27d1c | 1018 | |
4c9483b2 | 1019 | if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr)) |
77d16f45 | 1020 | flags |= RT6_LOOKUP_F_IFACE; |
c71099ac | 1021 | |
4c9483b2 | 1022 | if (!ipv6_addr_any(&fl6->saddr)) |
adaa70bb | 1023 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
0c9a2ac1 YH |
1024 | else if (sk) |
1025 | flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); | |
adaa70bb | 1026 | |
4c9483b2 | 1027 | return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); |
1da177e4 LT |
1028 | } |
1029 | ||
7159039a | 1030 | EXPORT_SYMBOL(ip6_route_output); |
1da177e4 | 1031 | |
2774c131 | 1032 | struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) |
14e50e57 | 1033 | { |
5c1e6aa3 | 1034 | struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; |
14e50e57 DM |
1035 | struct dst_entry *new = NULL; |
1036 | ||
f5b0a874 | 1037 | rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, DST_OBSOLETE_NONE, 0); |
14e50e57 | 1038 | if (rt) { |
d8d1f30b | 1039 | new = &rt->dst; |
14e50e57 | 1040 | |
8104891b SK |
1041 | memset(new + 1, 0, sizeof(*rt) - sizeof(*new)); |
1042 | rt6_init_peer(rt, net->ipv6.peers); | |
1043 | ||
14e50e57 | 1044 | new->__use = 1; |
352e512c HX |
1045 | new->input = dst_discard; |
1046 | new->output = dst_discard; | |
14e50e57 | 1047 | |
21efcfa0 ED |
1048 | if (dst_metrics_read_only(&ort->dst)) |
1049 | new->_metrics = ort->dst._metrics; | |
1050 | else | |
1051 | dst_copy_metrics(new, &ort->dst); | |
14e50e57 DM |
1052 | rt->rt6i_idev = ort->rt6i_idev; |
1053 | if (rt->rt6i_idev) | |
1054 | in6_dev_hold(rt->rt6i_idev); | |
14e50e57 | 1055 | |
4e3fd7a0 | 1056 | rt->rt6i_gateway = ort->rt6i_gateway; |
1716a961 | 1057 | rt->rt6i_flags = ort->rt6i_flags; |
14e50e57 DM |
1058 | rt->rt6i_metric = 0; |
1059 | ||
1060 | memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); | |
1061 | #ifdef CONFIG_IPV6_SUBTREES | |
1062 | memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); | |
1063 | #endif | |
1064 | ||
1065 | dst_free(new); | |
1066 | } | |
1067 | ||
69ead7af DM |
1068 | dst_release(dst_orig); |
1069 | return new ? new : ERR_PTR(-ENOMEM); | |
14e50e57 | 1070 | } |
14e50e57 | 1071 | |
1da177e4 LT |
1072 | /* |
1073 | * Destination cache support functions | |
1074 | */ | |
1075 | ||
1076 | static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) | |
1077 | { | |
1078 | struct rt6_info *rt; | |
1079 | ||
1080 | rt = (struct rt6_info *) dst; | |
1081 | ||
6f3118b5 ND |
1082 | /* All IPV6 dsts are created with ->obsolete set to the value |
1083 | * DST_OBSOLETE_FORCE_CHK which forces validation calls down | |
1084 | * into this function always. | |
1085 | */ | |
1086 | if (rt->rt6i_genid != rt_genid(dev_net(rt->dst.dev))) | |
1087 | return NULL; | |
1088 | ||
8ffff756 HFS |
1089 | if (!rt->rt6i_node || (rt->rt6i_node->fn_sernum != cookie)) |
1090 | return NULL; | |
a4477c4d | 1091 | |
8ffff756 HFS |
1092 | if (rt6_check_expired(rt)) |
1093 | return NULL; | |
1094 | ||
1095 | return dst; | |
1da177e4 LT |
1096 | } |
1097 | ||
1098 | static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) | |
1099 | { | |
1100 | struct rt6_info *rt = (struct rt6_info *) dst; | |
1101 | ||
1102 | if (rt) { | |
54c1a859 YH |
1103 | if (rt->rt6i_flags & RTF_CACHE) { |
1104 | if (rt6_check_expired(rt)) { | |
1105 | ip6_del_rt(rt); | |
1106 | dst = NULL; | |
1107 | } | |
1108 | } else { | |
1da177e4 | 1109 | dst_release(dst); |
54c1a859 YH |
1110 | dst = NULL; |
1111 | } | |
1da177e4 | 1112 | } |
54c1a859 | 1113 | return dst; |
1da177e4 LT |
1114 | } |
1115 | ||
1116 | static void ip6_link_failure(struct sk_buff *skb) | |
1117 | { | |
1118 | struct rt6_info *rt; | |
1119 | ||
3ffe533c | 1120 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); |
1da177e4 | 1121 | |
adf30907 | 1122 | rt = (struct rt6_info *) skb_dst(skb); |
1da177e4 | 1123 | if (rt) { |
a025e28a HFS |
1124 | if (rt->rt6i_flags & RTF_CACHE) { |
1125 | dst_hold(&rt->dst); | |
1126 | if (ip6_del_rt(rt)) | |
1127 | dst_free(&rt->dst); | |
1128 | } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) { | |
1da177e4 | 1129 | rt->rt6i_node->fn_sernum = -1; |
a025e28a | 1130 | } |
1da177e4 LT |
1131 | } |
1132 | } | |
1133 | ||
6700c270 DM |
1134 | static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, |
1135 | struct sk_buff *skb, u32 mtu) | |
1da177e4 LT |
1136 | { |
1137 | struct rt6_info *rt6 = (struct rt6_info*)dst; | |
1138 | ||
81aded24 | 1139 | dst_confirm(dst); |
1da177e4 | 1140 | if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) { |
81aded24 DM |
1141 | struct net *net = dev_net(dst->dev); |
1142 | ||
1da177e4 LT |
1143 | rt6->rt6i_flags |= RTF_MODIFIED; |
1144 | if (mtu < IPV6_MIN_MTU) { | |
defb3519 | 1145 | u32 features = dst_metric(dst, RTAX_FEATURES); |
1da177e4 | 1146 | mtu = IPV6_MIN_MTU; |
defb3519 DM |
1147 | features |= RTAX_FEATURE_ALLFRAG; |
1148 | dst_metric_set(dst, RTAX_FEATURES, features); | |
1da177e4 | 1149 | } |
defb3519 | 1150 | dst_metric_set(dst, RTAX_MTU, mtu); |
81aded24 | 1151 | rt6_update_expires(rt6, net->ipv6.sysctl.ip6_rt_mtu_expires); |
1da177e4 LT |
1152 | } |
1153 | } | |
1154 | ||
42ae66c8 DM |
1155 | void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu, |
1156 | int oif, u32 mark) | |
81aded24 DM |
1157 | { |
1158 | const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; | |
1159 | struct dst_entry *dst; | |
1160 | struct flowi6 fl6; | |
1161 | ||
1162 | memset(&fl6, 0, sizeof(fl6)); | |
1163 | fl6.flowi6_oif = oif; | |
1164 | fl6.flowi6_mark = mark; | |
3e12939a | 1165 | fl6.flowi6_flags = 0; |
81aded24 DM |
1166 | fl6.daddr = iph->daddr; |
1167 | fl6.saddr = iph->saddr; | |
6502ca52 | 1168 | fl6.flowlabel = ip6_flowinfo(iph); |
81aded24 DM |
1169 | |
1170 | dst = ip6_route_output(net, NULL, &fl6); | |
1171 | if (!dst->error) | |
6700c270 | 1172 | ip6_rt_update_pmtu(dst, NULL, skb, ntohl(mtu)); |
81aded24 DM |
1173 | dst_release(dst); |
1174 | } | |
1175 | EXPORT_SYMBOL_GPL(ip6_update_pmtu); | |
1176 | ||
1177 | void ip6_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, __be32 mtu) | |
1178 | { | |
1179 | ip6_update_pmtu(skb, sock_net(sk), mtu, | |
1180 | sk->sk_bound_dev_if, sk->sk_mark); | |
1181 | } | |
1182 | EXPORT_SYMBOL_GPL(ip6_sk_update_pmtu); | |
1183 | ||
3a5ad2ee DM |
1184 | void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark) |
1185 | { | |
1186 | const struct ipv6hdr *iph = (struct ipv6hdr *) skb->data; | |
1187 | struct dst_entry *dst; | |
1188 | struct flowi6 fl6; | |
1189 | ||
1190 | memset(&fl6, 0, sizeof(fl6)); | |
1191 | fl6.flowi6_oif = oif; | |
1192 | fl6.flowi6_mark = mark; | |
1193 | fl6.flowi6_flags = 0; | |
1194 | fl6.daddr = iph->daddr; | |
1195 | fl6.saddr = iph->saddr; | |
6502ca52 | 1196 | fl6.flowlabel = ip6_flowinfo(iph); |
3a5ad2ee DM |
1197 | |
1198 | dst = ip6_route_output(net, NULL, &fl6); | |
1199 | if (!dst->error) | |
6700c270 | 1200 | rt6_do_redirect(dst, NULL, skb); |
3a5ad2ee DM |
1201 | dst_release(dst); |
1202 | } | |
1203 | EXPORT_SYMBOL_GPL(ip6_redirect); | |
1204 | ||
1205 | void ip6_sk_redirect(struct sk_buff *skb, struct sock *sk) | |
1206 | { | |
1207 | ip6_redirect(skb, sock_net(sk), sk->sk_bound_dev_if, sk->sk_mark); | |
1208 | } | |
1209 | EXPORT_SYMBOL_GPL(ip6_sk_redirect); | |
1210 | ||
0dbaee3b | 1211 | static unsigned int ip6_default_advmss(const struct dst_entry *dst) |
1da177e4 | 1212 | { |
0dbaee3b DM |
1213 | struct net_device *dev = dst->dev; |
1214 | unsigned int mtu = dst_mtu(dst); | |
1215 | struct net *net = dev_net(dev); | |
1216 | ||
1da177e4 LT |
1217 | mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); |
1218 | ||
5578689a DL |
1219 | if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) |
1220 | mtu = net->ipv6.sysctl.ip6_rt_min_advmss; | |
1da177e4 LT |
1221 | |
1222 | /* | |
1ab1457c YH |
1223 | * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and |
1224 | * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. | |
1225 | * IPV6_MAXPLEN is also valid and means: "any MSS, | |
1da177e4 LT |
1226 | * rely only on pmtu discovery" |
1227 | */ | |
1228 | if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) | |
1229 | mtu = IPV6_MAXPLEN; | |
1230 | return mtu; | |
1231 | } | |
1232 | ||
ebb762f2 | 1233 | static unsigned int ip6_mtu(const struct dst_entry *dst) |
d33e4553 | 1234 | { |
d33e4553 | 1235 | struct inet6_dev *idev; |
618f9bc7 SK |
1236 | unsigned int mtu = dst_metric_raw(dst, RTAX_MTU); |
1237 | ||
1238 | if (mtu) | |
1239 | return mtu; | |
1240 | ||
1241 | mtu = IPV6_MIN_MTU; | |
d33e4553 DM |
1242 | |
1243 | rcu_read_lock(); | |
1244 | idev = __in6_dev_get(dst->dev); | |
1245 | if (idev) | |
1246 | mtu = idev->cnf.mtu6; | |
1247 | rcu_read_unlock(); | |
1248 | ||
1249 | return mtu; | |
1250 | } | |
1251 | ||
3b00944c YH |
1252 | static struct dst_entry *icmp6_dst_gc_list; |
1253 | static DEFINE_SPINLOCK(icmp6_dst_lock); | |
5d0bbeeb | 1254 | |
3b00944c | 1255 | struct dst_entry *icmp6_dst_alloc(struct net_device *dev, |
87a11578 | 1256 | struct flowi6 *fl6) |
1da177e4 | 1257 | { |
87a11578 | 1258 | struct dst_entry *dst; |
1da177e4 LT |
1259 | struct rt6_info *rt; |
1260 | struct inet6_dev *idev = in6_dev_get(dev); | |
c346dca1 | 1261 | struct net *net = dev_net(dev); |
1da177e4 | 1262 | |
38308473 | 1263 | if (unlikely(!idev)) |
122bdf67 | 1264 | return ERR_PTR(-ENODEV); |
1da177e4 | 1265 | |
8b96d22d | 1266 | rt = ip6_dst_alloc(net, dev, 0, NULL); |
38308473 | 1267 | if (unlikely(!rt)) { |
1da177e4 | 1268 | in6_dev_put(idev); |
87a11578 | 1269 | dst = ERR_PTR(-ENOMEM); |
1da177e4 LT |
1270 | goto out; |
1271 | } | |
1272 | ||
8e2ec639 YZ |
1273 | rt->dst.flags |= DST_HOST; |
1274 | rt->dst.output = ip6_output; | |
d8d1f30b | 1275 | atomic_set(&rt->dst.__refcnt, 1); |
044d6efb | 1276 | rt->rt6i_gateway = fl6->daddr; |
87a11578 | 1277 | rt->rt6i_dst.addr = fl6->daddr; |
8e2ec639 YZ |
1278 | rt->rt6i_dst.plen = 128; |
1279 | rt->rt6i_idev = idev; | |
14edd87d | 1280 | dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 0); |
1da177e4 | 1281 | |
3b00944c | 1282 | spin_lock_bh(&icmp6_dst_lock); |
d8d1f30b CG |
1283 | rt->dst.next = icmp6_dst_gc_list; |
1284 | icmp6_dst_gc_list = &rt->dst; | |
3b00944c | 1285 | spin_unlock_bh(&icmp6_dst_lock); |
1da177e4 | 1286 | |
5578689a | 1287 | fib6_force_start_gc(net); |
1da177e4 | 1288 | |
87a11578 DM |
1289 | dst = xfrm_lookup(net, &rt->dst, flowi6_to_flowi(fl6), NULL, 0); |
1290 | ||
1da177e4 | 1291 | out: |
87a11578 | 1292 | return dst; |
1da177e4 LT |
1293 | } |
1294 | ||
3d0f24a7 | 1295 | int icmp6_dst_gc(void) |
1da177e4 | 1296 | { |
e9476e95 | 1297 | struct dst_entry *dst, **pprev; |
3d0f24a7 | 1298 | int more = 0; |
1da177e4 | 1299 | |
3b00944c YH |
1300 | spin_lock_bh(&icmp6_dst_lock); |
1301 | pprev = &icmp6_dst_gc_list; | |
5d0bbeeb | 1302 | |
1da177e4 LT |
1303 | while ((dst = *pprev) != NULL) { |
1304 | if (!atomic_read(&dst->__refcnt)) { | |
1305 | *pprev = dst->next; | |
1306 | dst_free(dst); | |
1da177e4 LT |
1307 | } else { |
1308 | pprev = &dst->next; | |
3d0f24a7 | 1309 | ++more; |
1da177e4 LT |
1310 | } |
1311 | } | |
1312 | ||
3b00944c | 1313 | spin_unlock_bh(&icmp6_dst_lock); |
5d0bbeeb | 1314 | |
3d0f24a7 | 1315 | return more; |
1da177e4 LT |
1316 | } |
1317 | ||
1e493d19 DM |
1318 | static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg), |
1319 | void *arg) | |
1320 | { | |
1321 | struct dst_entry *dst, **pprev; | |
1322 | ||
1323 | spin_lock_bh(&icmp6_dst_lock); | |
1324 | pprev = &icmp6_dst_gc_list; | |
1325 | while ((dst = *pprev) != NULL) { | |
1326 | struct rt6_info *rt = (struct rt6_info *) dst; | |
1327 | if (func(rt, arg)) { | |
1328 | *pprev = dst->next; | |
1329 | dst_free(dst); | |
1330 | } else { | |
1331 | pprev = &dst->next; | |
1332 | } | |
1333 | } | |
1334 | spin_unlock_bh(&icmp6_dst_lock); | |
1335 | } | |
1336 | ||
569d3645 | 1337 | static int ip6_dst_gc(struct dst_ops *ops) |
1da177e4 | 1338 | { |
1da177e4 | 1339 | unsigned long now = jiffies; |
86393e52 | 1340 | struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); |
7019b78e DL |
1341 | int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; |
1342 | int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; | |
1343 | int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; | |
1344 | int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; | |
1345 | unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; | |
fc66f95c | 1346 | int entries; |
7019b78e | 1347 | |
fc66f95c | 1348 | entries = dst_entries_get_fast(ops); |
7019b78e | 1349 | if (time_after(rt_last_gc + rt_min_interval, now) && |
fc66f95c | 1350 | entries <= rt_max_size) |
1da177e4 LT |
1351 | goto out; |
1352 | ||
6891a346 BT |
1353 | net->ipv6.ip6_rt_gc_expire++; |
1354 | fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net); | |
1355 | net->ipv6.ip6_rt_last_gc = now; | |
fc66f95c ED |
1356 | entries = dst_entries_get_slow(ops); |
1357 | if (entries < ops->gc_thresh) | |
7019b78e | 1358 | net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; |
1da177e4 | 1359 | out: |
7019b78e | 1360 | net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; |
fc66f95c | 1361 | return entries > rt_max_size; |
1da177e4 LT |
1362 | } |
1363 | ||
6b75d090 | 1364 | int ip6_dst_hoplimit(struct dst_entry *dst) |
1da177e4 | 1365 | { |
5170ae82 | 1366 | int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT); |
a02e4b7d | 1367 | if (hoplimit == 0) { |
6b75d090 | 1368 | struct net_device *dev = dst->dev; |
c68f24cc ED |
1369 | struct inet6_dev *idev; |
1370 | ||
1371 | rcu_read_lock(); | |
1372 | idev = __in6_dev_get(dev); | |
1373 | if (idev) | |
6b75d090 | 1374 | hoplimit = idev->cnf.hop_limit; |
c68f24cc | 1375 | else |
53b7997f | 1376 | hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit; |
c68f24cc | 1377 | rcu_read_unlock(); |
1da177e4 LT |
1378 | } |
1379 | return hoplimit; | |
1380 | } | |
abbf46ae | 1381 | EXPORT_SYMBOL(ip6_dst_hoplimit); |
1da177e4 LT |
1382 | |
1383 | /* | |
1384 | * | |
1385 | */ | |
1386 | ||
86872cb5 | 1387 | int ip6_route_add(struct fib6_config *cfg) |
1da177e4 LT |
1388 | { |
1389 | int err; | |
5578689a | 1390 | struct net *net = cfg->fc_nlinfo.nl_net; |
1da177e4 LT |
1391 | struct rt6_info *rt = NULL; |
1392 | struct net_device *dev = NULL; | |
1393 | struct inet6_dev *idev = NULL; | |
c71099ac | 1394 | struct fib6_table *table; |
1da177e4 LT |
1395 | int addr_type; |
1396 | ||
86872cb5 | 1397 | if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128) |
1da177e4 LT |
1398 | return -EINVAL; |
1399 | #ifndef CONFIG_IPV6_SUBTREES | |
86872cb5 | 1400 | if (cfg->fc_src_len) |
1da177e4 LT |
1401 | return -EINVAL; |
1402 | #endif | |
86872cb5 | 1403 | if (cfg->fc_ifindex) { |
1da177e4 | 1404 | err = -ENODEV; |
5578689a | 1405 | dev = dev_get_by_index(net, cfg->fc_ifindex); |
1da177e4 LT |
1406 | if (!dev) |
1407 | goto out; | |
1408 | idev = in6_dev_get(dev); | |
1409 | if (!idev) | |
1410 | goto out; | |
1411 | } | |
1412 | ||
86872cb5 TG |
1413 | if (cfg->fc_metric == 0) |
1414 | cfg->fc_metric = IP6_RT_PRIO_USER; | |
1da177e4 | 1415 | |
d71314b4 | 1416 | err = -ENOBUFS; |
38308473 DM |
1417 | if (cfg->fc_nlinfo.nlh && |
1418 | !(cfg->fc_nlinfo.nlh->nlmsg_flags & NLM_F_CREATE)) { | |
d71314b4 | 1419 | table = fib6_get_table(net, cfg->fc_table); |
38308473 | 1420 | if (!table) { |
f3213831 | 1421 | pr_warn("NLM_F_CREATE should be specified when creating new route\n"); |
d71314b4 MV |
1422 | table = fib6_new_table(net, cfg->fc_table); |
1423 | } | |
1424 | } else { | |
1425 | table = fib6_new_table(net, cfg->fc_table); | |
1426 | } | |
38308473 DM |
1427 | |
1428 | if (!table) | |
c71099ac | 1429 | goto out; |
c71099ac | 1430 | |
8b96d22d | 1431 | rt = ip6_dst_alloc(net, NULL, DST_NOCOUNT, table); |
1da177e4 | 1432 | |
38308473 | 1433 | if (!rt) { |
1da177e4 LT |
1434 | err = -ENOMEM; |
1435 | goto out; | |
1436 | } | |
1437 | ||
1716a961 G |
1438 | if (cfg->fc_flags & RTF_EXPIRES) |
1439 | rt6_set_expires(rt, jiffies + | |
1440 | clock_t_to_jiffies(cfg->fc_expires)); | |
1441 | else | |
1442 | rt6_clean_expires(rt); | |
1da177e4 | 1443 | |
86872cb5 TG |
1444 | if (cfg->fc_protocol == RTPROT_UNSPEC) |
1445 | cfg->fc_protocol = RTPROT_BOOT; | |
1446 | rt->rt6i_protocol = cfg->fc_protocol; | |
1447 | ||
1448 | addr_type = ipv6_addr_type(&cfg->fc_dst); | |
1da177e4 LT |
1449 | |
1450 | if (addr_type & IPV6_ADDR_MULTICAST) | |
d8d1f30b | 1451 | rt->dst.input = ip6_mc_input; |
ab79ad14 MŻ |
1452 | else if (cfg->fc_flags & RTF_LOCAL) |
1453 | rt->dst.input = ip6_input; | |
1da177e4 | 1454 | else |
d8d1f30b | 1455 | rt->dst.input = ip6_forward; |
1da177e4 | 1456 | |
d8d1f30b | 1457 | rt->dst.output = ip6_output; |
1da177e4 | 1458 | |
86872cb5 TG |
1459 | ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); |
1460 | rt->rt6i_dst.plen = cfg->fc_dst_len; | |
1da177e4 | 1461 | if (rt->rt6i_dst.plen == 128) |
11d53b49 | 1462 | rt->dst.flags |= DST_HOST; |
1da177e4 | 1463 | |
8e2ec639 YZ |
1464 | if (!(rt->dst.flags & DST_HOST) && cfg->fc_mx) { |
1465 | u32 *metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); | |
1466 | if (!metrics) { | |
1467 | err = -ENOMEM; | |
1468 | goto out; | |
1469 | } | |
1470 | dst_init_metrics(&rt->dst, metrics, 0); | |
1471 | } | |
1da177e4 | 1472 | #ifdef CONFIG_IPV6_SUBTREES |
86872cb5 TG |
1473 | ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); |
1474 | rt->rt6i_src.plen = cfg->fc_src_len; | |
1da177e4 LT |
1475 | #endif |
1476 | ||
86872cb5 | 1477 | rt->rt6i_metric = cfg->fc_metric; |
1da177e4 LT |
1478 | |
1479 | /* We cannot add true routes via loopback here, | |
1480 | they would result in kernel looping; promote them to reject routes | |
1481 | */ | |
86872cb5 | 1482 | if ((cfg->fc_flags & RTF_REJECT) || |
38308473 DM |
1483 | (dev && (dev->flags & IFF_LOOPBACK) && |
1484 | !(addr_type & IPV6_ADDR_LOOPBACK) && | |
1485 | !(cfg->fc_flags & RTF_LOCAL))) { | |
1da177e4 | 1486 | /* hold loopback dev/idev if we haven't done so. */ |
5578689a | 1487 | if (dev != net->loopback_dev) { |
1da177e4 LT |
1488 | if (dev) { |
1489 | dev_put(dev); | |
1490 | in6_dev_put(idev); | |
1491 | } | |
5578689a | 1492 | dev = net->loopback_dev; |
1da177e4 LT |
1493 | dev_hold(dev); |
1494 | idev = in6_dev_get(dev); | |
1495 | if (!idev) { | |
1496 | err = -ENODEV; | |
1497 | goto out; | |
1498 | } | |
1499 | } | |
1da177e4 | 1500 | rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; |
ef2c7d7b ND |
1501 | switch (cfg->fc_type) { |
1502 | case RTN_BLACKHOLE: | |
1503 | rt->dst.error = -EINVAL; | |
5af58eb0 K |
1504 | rt->dst.output = dst_discard; |
1505 | rt->dst.input = dst_discard; | |
ef2c7d7b ND |
1506 | break; |
1507 | case RTN_PROHIBIT: | |
1508 | rt->dst.error = -EACCES; | |
5af58eb0 K |
1509 | rt->dst.output = ip6_pkt_prohibit_out; |
1510 | rt->dst.input = ip6_pkt_prohibit; | |
ef2c7d7b | 1511 | break; |
b4949ab2 | 1512 | case RTN_THROW: |
ef2c7d7b | 1513 | default: |
5af58eb0 K |
1514 | rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN |
1515 | : -ENETUNREACH; | |
1516 | rt->dst.output = ip6_pkt_discard_out; | |
1517 | rt->dst.input = ip6_pkt_discard; | |
ef2c7d7b ND |
1518 | break; |
1519 | } | |
1da177e4 LT |
1520 | goto install_route; |
1521 | } | |
1522 | ||
86872cb5 | 1523 | if (cfg->fc_flags & RTF_GATEWAY) { |
b71d1d42 | 1524 | const struct in6_addr *gw_addr; |
1da177e4 LT |
1525 | int gwa_type; |
1526 | ||
86872cb5 | 1527 | gw_addr = &cfg->fc_gateway; |
4e3fd7a0 | 1528 | rt->rt6i_gateway = *gw_addr; |
1da177e4 LT |
1529 | gwa_type = ipv6_addr_type(gw_addr); |
1530 | ||
1531 | if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { | |
1532 | struct rt6_info *grt; | |
1533 | ||
1534 | /* IPv6 strictly inhibits using not link-local | |
1535 | addresses as nexthop address. | |
1536 | Otherwise, router will not able to send redirects. | |
1537 | It is very good, but in some (rare!) circumstances | |
1538 | (SIT, PtP, NBMA NOARP links) it is handy to allow | |
1539 | some exceptions. --ANK | |
1540 | */ | |
1541 | err = -EINVAL; | |
38308473 | 1542 | if (!(gwa_type & IPV6_ADDR_UNICAST)) |
1da177e4 LT |
1543 | goto out; |
1544 | ||
5578689a | 1545 | grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1); |
1da177e4 LT |
1546 | |
1547 | err = -EHOSTUNREACH; | |
38308473 | 1548 | if (!grt) |
1da177e4 LT |
1549 | goto out; |
1550 | if (dev) { | |
d1918542 | 1551 | if (dev != grt->dst.dev) { |
94e187c0 | 1552 | ip6_rt_put(grt); |
1da177e4 LT |
1553 | goto out; |
1554 | } | |
1555 | } else { | |
d1918542 | 1556 | dev = grt->dst.dev; |
1da177e4 LT |
1557 | idev = grt->rt6i_idev; |
1558 | dev_hold(dev); | |
1559 | in6_dev_hold(grt->rt6i_idev); | |
1560 | } | |
38308473 | 1561 | if (!(grt->rt6i_flags & RTF_GATEWAY)) |
1da177e4 | 1562 | err = 0; |
94e187c0 | 1563 | ip6_rt_put(grt); |
1da177e4 LT |
1564 | |
1565 | if (err) | |
1566 | goto out; | |
1567 | } | |
1568 | err = -EINVAL; | |
38308473 | 1569 | if (!dev || (dev->flags & IFF_LOOPBACK)) |
1da177e4 LT |
1570 | goto out; |
1571 | } | |
1572 | ||
1573 | err = -ENODEV; | |
38308473 | 1574 | if (!dev) |
1da177e4 LT |
1575 | goto out; |
1576 | ||
c3968a85 DW |
1577 | if (!ipv6_addr_any(&cfg->fc_prefsrc)) { |
1578 | if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { | |
1579 | err = -EINVAL; | |
1580 | goto out; | |
1581 | } | |
4e3fd7a0 | 1582 | rt->rt6i_prefsrc.addr = cfg->fc_prefsrc; |
c3968a85 DW |
1583 | rt->rt6i_prefsrc.plen = 128; |
1584 | } else | |
1585 | rt->rt6i_prefsrc.plen = 0; | |
1586 | ||
86872cb5 | 1587 | rt->rt6i_flags = cfg->fc_flags; |
1da177e4 LT |
1588 | |
1589 | install_route: | |
86872cb5 TG |
1590 | if (cfg->fc_mx) { |
1591 | struct nlattr *nla; | |
1592 | int remaining; | |
1593 | ||
1594 | nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { | |
8f4c1f9b | 1595 | int type = nla_type(nla); |
86872cb5 TG |
1596 | |
1597 | if (type) { | |
1598 | if (type > RTAX_MAX) { | |
1da177e4 LT |
1599 | err = -EINVAL; |
1600 | goto out; | |
1601 | } | |
86872cb5 | 1602 | |
defb3519 | 1603 | dst_metric_set(&rt->dst, type, nla_get_u32(nla)); |
1da177e4 | 1604 | } |
1da177e4 LT |
1605 | } |
1606 | } | |
1607 | ||
d8d1f30b | 1608 | rt->dst.dev = dev; |
1da177e4 | 1609 | rt->rt6i_idev = idev; |
c71099ac | 1610 | rt->rt6i_table = table; |
63152fc0 | 1611 | |
c346dca1 | 1612 | cfg->fc_nlinfo.nl_net = dev_net(dev); |
63152fc0 | 1613 | |
86872cb5 | 1614 | return __ip6_ins_rt(rt, &cfg->fc_nlinfo); |
1da177e4 LT |
1615 | |
1616 | out: | |
1617 | if (dev) | |
1618 | dev_put(dev); | |
1619 | if (idev) | |
1620 | in6_dev_put(idev); | |
1621 | if (rt) | |
d8d1f30b | 1622 | dst_free(&rt->dst); |
1da177e4 LT |
1623 | return err; |
1624 | } | |
1625 | ||
86872cb5 | 1626 | static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) |
1da177e4 LT |
1627 | { |
1628 | int err; | |
c71099ac | 1629 | struct fib6_table *table; |
d1918542 | 1630 | struct net *net = dev_net(rt->dst.dev); |
1da177e4 | 1631 | |
6825a26c G |
1632 | if (rt == net->ipv6.ip6_null_entry) { |
1633 | err = -ENOENT; | |
1634 | goto out; | |
1635 | } | |
6c813a72 | 1636 | |
c71099ac TG |
1637 | table = rt->rt6i_table; |
1638 | write_lock_bh(&table->tb6_lock); | |
86872cb5 | 1639 | err = fib6_del(rt, info); |
c71099ac | 1640 | write_unlock_bh(&table->tb6_lock); |
1da177e4 | 1641 | |
6825a26c | 1642 | out: |
94e187c0 | 1643 | ip6_rt_put(rt); |
1da177e4 LT |
1644 | return err; |
1645 | } | |
1646 | ||
e0a1ad73 TG |
1647 | int ip6_del_rt(struct rt6_info *rt) |
1648 | { | |
4d1169c1 | 1649 | struct nl_info info = { |
d1918542 | 1650 | .nl_net = dev_net(rt->dst.dev), |
4d1169c1 | 1651 | }; |
528c4ceb | 1652 | return __ip6_del_rt(rt, &info); |
e0a1ad73 TG |
1653 | } |
1654 | ||
86872cb5 | 1655 | static int ip6_route_del(struct fib6_config *cfg) |
1da177e4 | 1656 | { |
c71099ac | 1657 | struct fib6_table *table; |
1da177e4 LT |
1658 | struct fib6_node *fn; |
1659 | struct rt6_info *rt; | |
1660 | int err = -ESRCH; | |
1661 | ||
5578689a | 1662 | table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); |
38308473 | 1663 | if (!table) |
c71099ac TG |
1664 | return err; |
1665 | ||
1666 | read_lock_bh(&table->tb6_lock); | |
1da177e4 | 1667 | |
c71099ac | 1668 | fn = fib6_locate(&table->tb6_root, |
86872cb5 TG |
1669 | &cfg->fc_dst, cfg->fc_dst_len, |
1670 | &cfg->fc_src, cfg->fc_src_len); | |
1ab1457c | 1671 | |
1da177e4 | 1672 | if (fn) { |
d8d1f30b | 1673 | for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { |
86872cb5 | 1674 | if (cfg->fc_ifindex && |
d1918542 DM |
1675 | (!rt->dst.dev || |
1676 | rt->dst.dev->ifindex != cfg->fc_ifindex)) | |
1da177e4 | 1677 | continue; |
86872cb5 TG |
1678 | if (cfg->fc_flags & RTF_GATEWAY && |
1679 | !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) | |
1da177e4 | 1680 | continue; |
86872cb5 | 1681 | if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) |
1da177e4 | 1682 | continue; |
d8d1f30b | 1683 | dst_hold(&rt->dst); |
c71099ac | 1684 | read_unlock_bh(&table->tb6_lock); |
1da177e4 | 1685 | |
86872cb5 | 1686 | return __ip6_del_rt(rt, &cfg->fc_nlinfo); |
1da177e4 LT |
1687 | } |
1688 | } | |
c71099ac | 1689 | read_unlock_bh(&table->tb6_lock); |
1da177e4 LT |
1690 | |
1691 | return err; | |
1692 | } | |
1693 | ||
6700c270 | 1694 | static void rt6_do_redirect(struct dst_entry *dst, struct sock *sk, struct sk_buff *skb) |
a6279458 | 1695 | { |
e8599ff4 | 1696 | struct net *net = dev_net(skb->dev); |
a6279458 | 1697 | struct netevent_redirect netevent; |
e8599ff4 | 1698 | struct rt6_info *rt, *nrt = NULL; |
e8599ff4 DM |
1699 | struct ndisc_options ndopts; |
1700 | struct inet6_dev *in6_dev; | |
1701 | struct neighbour *neigh; | |
71bcdba0 | 1702 | struct rd_msg *msg; |
6e157b6a DM |
1703 | int optlen, on_link; |
1704 | u8 *lladdr; | |
e8599ff4 DM |
1705 | |
1706 | optlen = skb->tail - skb->transport_header; | |
71bcdba0 | 1707 | optlen -= sizeof(*msg); |
e8599ff4 DM |
1708 | |
1709 | if (optlen < 0) { | |
6e157b6a | 1710 | net_dbg_ratelimited("rt6_do_redirect: packet too short\n"); |
e8599ff4 DM |
1711 | return; |
1712 | } | |
1713 | ||
71bcdba0 | 1714 | msg = (struct rd_msg *)icmp6_hdr(skb); |
e8599ff4 | 1715 | |
71bcdba0 | 1716 | if (ipv6_addr_is_multicast(&msg->dest)) { |
6e157b6a | 1717 | net_dbg_ratelimited("rt6_do_redirect: destination address is multicast\n"); |
e8599ff4 DM |
1718 | return; |
1719 | } | |
1720 | ||
6e157b6a | 1721 | on_link = 0; |
71bcdba0 | 1722 | if (ipv6_addr_equal(&msg->dest, &msg->target)) { |
e8599ff4 | 1723 | on_link = 1; |
71bcdba0 | 1724 | } else if (ipv6_addr_type(&msg->target) != |
e8599ff4 | 1725 | (IPV6_ADDR_UNICAST|IPV6_ADDR_LINKLOCAL)) { |
6e157b6a | 1726 | net_dbg_ratelimited("rt6_do_redirect: target address is not link-local unicast\n"); |
e8599ff4 DM |
1727 | return; |
1728 | } | |
1729 | ||
1730 | in6_dev = __in6_dev_get(skb->dev); | |
1731 | if (!in6_dev) | |
1732 | return; | |
1733 | if (in6_dev->cnf.forwarding || !in6_dev->cnf.accept_redirects) | |
1734 | return; | |
1735 | ||
1736 | /* RFC2461 8.1: | |
1737 | * The IP source address of the Redirect MUST be the same as the current | |
1738 | * first-hop router for the specified ICMP Destination Address. | |
1739 | */ | |
1740 | ||
71bcdba0 | 1741 | if (!ndisc_parse_options(msg->opt, optlen, &ndopts)) { |
e8599ff4 DM |
1742 | net_dbg_ratelimited("rt6_redirect: invalid ND options\n"); |
1743 | return; | |
1744 | } | |
6e157b6a DM |
1745 | |
1746 | lladdr = NULL; | |
e8599ff4 DM |
1747 | if (ndopts.nd_opts_tgt_lladdr) { |
1748 | lladdr = ndisc_opt_addr_data(ndopts.nd_opts_tgt_lladdr, | |
1749 | skb->dev); | |
1750 | if (!lladdr) { | |
1751 | net_dbg_ratelimited("rt6_redirect: invalid link-layer address length\n"); | |
1752 | return; | |
1753 | } | |
1754 | } | |
1755 | ||
6e157b6a DM |
1756 | rt = (struct rt6_info *) dst; |
1757 | if (rt == net->ipv6.ip6_null_entry) { | |
1758 | net_dbg_ratelimited("rt6_redirect: source isn't a valid nexthop for redirect target\n"); | |
e8599ff4 | 1759 | return; |
6e157b6a | 1760 | } |
e8599ff4 | 1761 | |
6e157b6a DM |
1762 | /* Redirect received -> path was valid. |
1763 | * Look, redirects are sent only in response to data packets, | |
1764 | * so that this nexthop apparently is reachable. --ANK | |
1765 | */ | |
1766 | dst_confirm(&rt->dst); | |
a6279458 | 1767 | |
71bcdba0 | 1768 | neigh = __neigh_lookup(&nd_tbl, &msg->target, skb->dev, 1); |
6e157b6a DM |
1769 | if (!neigh) |
1770 | return; | |
a6279458 | 1771 | |
1da177e4 LT |
1772 | /* |
1773 | * We have finally decided to accept it. | |
1774 | */ | |
1775 | ||
1ab1457c | 1776 | neigh_update(neigh, lladdr, NUD_STALE, |
1da177e4 LT |
1777 | NEIGH_UPDATE_F_WEAK_OVERRIDE| |
1778 | NEIGH_UPDATE_F_OVERRIDE| | |
1779 | (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| | |
1780 | NEIGH_UPDATE_F_ISROUTER)) | |
1781 | ); | |
1782 | ||
71bcdba0 | 1783 | nrt = ip6_rt_copy(rt, &msg->dest); |
38308473 | 1784 | if (!nrt) |
1da177e4 LT |
1785 | goto out; |
1786 | ||
1787 | nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; | |
1788 | if (on_link) | |
1789 | nrt->rt6i_flags &= ~RTF_GATEWAY; | |
1790 | ||
4e3fd7a0 | 1791 | nrt->rt6i_gateway = *(struct in6_addr *)neigh->primary_key; |
1da177e4 | 1792 | |
40e22e8f | 1793 | if (ip6_ins_rt(nrt)) |
1da177e4 LT |
1794 | goto out; |
1795 | ||
d8d1f30b CG |
1796 | netevent.old = &rt->dst; |
1797 | netevent.new = &nrt->dst; | |
71bcdba0 | 1798 | netevent.daddr = &msg->dest; |
60592833 | 1799 | netevent.neigh = neigh; |
8d71740c TT |
1800 | call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); |
1801 | ||
38308473 | 1802 | if (rt->rt6i_flags & RTF_CACHE) { |
6e157b6a | 1803 | rt = (struct rt6_info *) dst_clone(&rt->dst); |
e0a1ad73 | 1804 | ip6_del_rt(rt); |
1da177e4 LT |
1805 | } |
1806 | ||
1807 | out: | |
e8599ff4 | 1808 | neigh_release(neigh); |
6e157b6a DM |
1809 | } |
1810 | ||
1da177e4 LT |
1811 | /* |
1812 | * Misc support functions | |
1813 | */ | |
1814 | ||
1716a961 | 1815 | static struct rt6_info *ip6_rt_copy(struct rt6_info *ort, |
21efcfa0 | 1816 | const struct in6_addr *dest) |
1da177e4 | 1817 | { |
d1918542 | 1818 | struct net *net = dev_net(ort->dst.dev); |
8b96d22d DM |
1819 | struct rt6_info *rt = ip6_dst_alloc(net, ort->dst.dev, 0, |
1820 | ort->rt6i_table); | |
1da177e4 LT |
1821 | |
1822 | if (rt) { | |
d8d1f30b CG |
1823 | rt->dst.input = ort->dst.input; |
1824 | rt->dst.output = ort->dst.output; | |
8e2ec639 | 1825 | rt->dst.flags |= DST_HOST; |
d8d1f30b | 1826 | |
4e3fd7a0 | 1827 | rt->rt6i_dst.addr = *dest; |
8e2ec639 | 1828 | rt->rt6i_dst.plen = 128; |
defb3519 | 1829 | dst_copy_metrics(&rt->dst, &ort->dst); |
d8d1f30b | 1830 | rt->dst.error = ort->dst.error; |
1da177e4 LT |
1831 | rt->rt6i_idev = ort->rt6i_idev; |
1832 | if (rt->rt6i_idev) | |
1833 | in6_dev_hold(rt->rt6i_idev); | |
d8d1f30b | 1834 | rt->dst.lastuse = jiffies; |
1da177e4 | 1835 | |
044d6efb JA |
1836 | if (ort->rt6i_flags & RTF_GATEWAY) |
1837 | rt->rt6i_gateway = ort->rt6i_gateway; | |
1838 | else | |
1839 | rt->rt6i_gateway = *dest; | |
1716a961 G |
1840 | rt->rt6i_flags = ort->rt6i_flags; |
1841 | if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) == | |
1842 | (RTF_DEFAULT | RTF_ADDRCONF)) | |
1843 | rt6_set_from(rt, ort); | |
1da177e4 LT |
1844 | rt->rt6i_metric = 0; |
1845 | ||
1da177e4 LT |
1846 | #ifdef CONFIG_IPV6_SUBTREES |
1847 | memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); | |
1848 | #endif | |
0f6c6392 | 1849 | memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key)); |
c71099ac | 1850 | rt->rt6i_table = ort->rt6i_table; |
1da177e4 LT |
1851 | } |
1852 | return rt; | |
1853 | } | |
1854 | ||
70ceb4f5 | 1855 | #ifdef CONFIG_IPV6_ROUTE_INFO |
efa2cea0 | 1856 | static struct rt6_info *rt6_get_route_info(struct net *net, |
b71d1d42 ED |
1857 | const struct in6_addr *prefix, int prefixlen, |
1858 | const struct in6_addr *gwaddr, int ifindex) | |
70ceb4f5 YH |
1859 | { |
1860 | struct fib6_node *fn; | |
1861 | struct rt6_info *rt = NULL; | |
c71099ac TG |
1862 | struct fib6_table *table; |
1863 | ||
efa2cea0 | 1864 | table = fib6_get_table(net, RT6_TABLE_INFO); |
38308473 | 1865 | if (!table) |
c71099ac | 1866 | return NULL; |
70ceb4f5 | 1867 | |
5744dd9b | 1868 | read_lock_bh(&table->tb6_lock); |
c71099ac | 1869 | fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0); |
70ceb4f5 YH |
1870 | if (!fn) |
1871 | goto out; | |
1872 | ||
d8d1f30b | 1873 | for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { |
d1918542 | 1874 | if (rt->dst.dev->ifindex != ifindex) |
70ceb4f5 YH |
1875 | continue; |
1876 | if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) | |
1877 | continue; | |
1878 | if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) | |
1879 | continue; | |
d8d1f30b | 1880 | dst_hold(&rt->dst); |
70ceb4f5 YH |
1881 | break; |
1882 | } | |
1883 | out: | |
5744dd9b | 1884 | read_unlock_bh(&table->tb6_lock); |
70ceb4f5 YH |
1885 | return rt; |
1886 | } | |
1887 | ||
efa2cea0 | 1888 | static struct rt6_info *rt6_add_route_info(struct net *net, |
b71d1d42 ED |
1889 | const struct in6_addr *prefix, int prefixlen, |
1890 | const struct in6_addr *gwaddr, int ifindex, | |
95c96174 | 1891 | unsigned int pref) |
70ceb4f5 | 1892 | { |
86872cb5 TG |
1893 | struct fib6_config cfg = { |
1894 | .fc_table = RT6_TABLE_INFO, | |
238fc7ea | 1895 | .fc_metric = IP6_RT_PRIO_USER, |
86872cb5 TG |
1896 | .fc_ifindex = ifindex, |
1897 | .fc_dst_len = prefixlen, | |
1898 | .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | | |
1899 | RTF_UP | RTF_PREF(pref), | |
15e47304 | 1900 | .fc_nlinfo.portid = 0, |
efa2cea0 DL |
1901 | .fc_nlinfo.nlh = NULL, |
1902 | .fc_nlinfo.nl_net = net, | |
86872cb5 TG |
1903 | }; |
1904 | ||
4e3fd7a0 AD |
1905 | cfg.fc_dst = *prefix; |
1906 | cfg.fc_gateway = *gwaddr; | |
70ceb4f5 | 1907 | |
e317da96 YH |
1908 | /* We should treat it as a default route if prefix length is 0. */ |
1909 | if (!prefixlen) | |
86872cb5 | 1910 | cfg.fc_flags |= RTF_DEFAULT; |
70ceb4f5 | 1911 | |
86872cb5 | 1912 | ip6_route_add(&cfg); |
70ceb4f5 | 1913 | |
efa2cea0 | 1914 | return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex); |
70ceb4f5 YH |
1915 | } |
1916 | #endif | |
1917 | ||
b71d1d42 | 1918 | struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) |
1ab1457c | 1919 | { |
1da177e4 | 1920 | struct rt6_info *rt; |
c71099ac | 1921 | struct fib6_table *table; |
1da177e4 | 1922 | |
c346dca1 | 1923 | table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT); |
38308473 | 1924 | if (!table) |
c71099ac | 1925 | return NULL; |
1da177e4 | 1926 | |
5744dd9b | 1927 | read_lock_bh(&table->tb6_lock); |
d8d1f30b | 1928 | for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) { |
d1918542 | 1929 | if (dev == rt->dst.dev && |
045927ff | 1930 | ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && |
1da177e4 LT |
1931 | ipv6_addr_equal(&rt->rt6i_gateway, addr)) |
1932 | break; | |
1933 | } | |
1934 | if (rt) | |
d8d1f30b | 1935 | dst_hold(&rt->dst); |
5744dd9b | 1936 | read_unlock_bh(&table->tb6_lock); |
1da177e4 LT |
1937 | return rt; |
1938 | } | |
1939 | ||
b71d1d42 | 1940 | struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, |
ebacaaa0 YH |
1941 | struct net_device *dev, |
1942 | unsigned int pref) | |
1da177e4 | 1943 | { |
86872cb5 TG |
1944 | struct fib6_config cfg = { |
1945 | .fc_table = RT6_TABLE_DFLT, | |
238fc7ea | 1946 | .fc_metric = IP6_RT_PRIO_USER, |
86872cb5 TG |
1947 | .fc_ifindex = dev->ifindex, |
1948 | .fc_flags = RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | | |
1949 | RTF_UP | RTF_EXPIRES | RTF_PREF(pref), | |
15e47304 | 1950 | .fc_nlinfo.portid = 0, |
5578689a | 1951 | .fc_nlinfo.nlh = NULL, |
c346dca1 | 1952 | .fc_nlinfo.nl_net = dev_net(dev), |
86872cb5 | 1953 | }; |
1da177e4 | 1954 | |
4e3fd7a0 | 1955 | cfg.fc_gateway = *gwaddr; |
1da177e4 | 1956 | |
86872cb5 | 1957 | ip6_route_add(&cfg); |
1da177e4 | 1958 | |
1da177e4 LT |
1959 | return rt6_get_dflt_router(gwaddr, dev); |
1960 | } | |
1961 | ||
7b4da532 | 1962 | void rt6_purge_dflt_routers(struct net *net) |
1da177e4 LT |
1963 | { |
1964 | struct rt6_info *rt; | |
c71099ac TG |
1965 | struct fib6_table *table; |
1966 | ||
1967 | /* NOTE: Keep consistent with rt6_get_dflt_router */ | |
7b4da532 | 1968 | table = fib6_get_table(net, RT6_TABLE_DFLT); |
38308473 | 1969 | if (!table) |
c71099ac | 1970 | return; |
1da177e4 LT |
1971 | |
1972 | restart: | |
c71099ac | 1973 | read_lock_bh(&table->tb6_lock); |
d8d1f30b | 1974 | for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { |
3e8b0ac3 LC |
1975 | if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF) && |
1976 | (!rt->rt6i_idev || rt->rt6i_idev->cnf.accept_ra != 2)) { | |
d8d1f30b | 1977 | dst_hold(&rt->dst); |
c71099ac | 1978 | read_unlock_bh(&table->tb6_lock); |
e0a1ad73 | 1979 | ip6_del_rt(rt); |
1da177e4 LT |
1980 | goto restart; |
1981 | } | |
1982 | } | |
c71099ac | 1983 | read_unlock_bh(&table->tb6_lock); |
1da177e4 LT |
1984 | } |
1985 | ||
5578689a DL |
1986 | static void rtmsg_to_fib6_config(struct net *net, |
1987 | struct in6_rtmsg *rtmsg, | |
86872cb5 TG |
1988 | struct fib6_config *cfg) |
1989 | { | |
1990 | memset(cfg, 0, sizeof(*cfg)); | |
1991 | ||
1992 | cfg->fc_table = RT6_TABLE_MAIN; | |
1993 | cfg->fc_ifindex = rtmsg->rtmsg_ifindex; | |
1994 | cfg->fc_metric = rtmsg->rtmsg_metric; | |
1995 | cfg->fc_expires = rtmsg->rtmsg_info; | |
1996 | cfg->fc_dst_len = rtmsg->rtmsg_dst_len; | |
1997 | cfg->fc_src_len = rtmsg->rtmsg_src_len; | |
1998 | cfg->fc_flags = rtmsg->rtmsg_flags; | |
1999 | ||
5578689a | 2000 | cfg->fc_nlinfo.nl_net = net; |
f1243c2d | 2001 | |
4e3fd7a0 AD |
2002 | cfg->fc_dst = rtmsg->rtmsg_dst; |
2003 | cfg->fc_src = rtmsg->rtmsg_src; | |
2004 | cfg->fc_gateway = rtmsg->rtmsg_gateway; | |
86872cb5 TG |
2005 | } |
2006 | ||
5578689a | 2007 | int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) |
1da177e4 | 2008 | { |
86872cb5 | 2009 | struct fib6_config cfg; |
1da177e4 LT |
2010 | struct in6_rtmsg rtmsg; |
2011 | int err; | |
2012 | ||
2013 | switch(cmd) { | |
2014 | case SIOCADDRT: /* Add a route */ | |
2015 | case SIOCDELRT: /* Delete a route */ | |
af31f412 | 2016 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
1da177e4 LT |
2017 | return -EPERM; |
2018 | err = copy_from_user(&rtmsg, arg, | |
2019 | sizeof(struct in6_rtmsg)); | |
2020 | if (err) | |
2021 | return -EFAULT; | |
86872cb5 | 2022 | |
5578689a | 2023 | rtmsg_to_fib6_config(net, &rtmsg, &cfg); |
86872cb5 | 2024 | |
1da177e4 LT |
2025 | rtnl_lock(); |
2026 | switch (cmd) { | |
2027 | case SIOCADDRT: | |
86872cb5 | 2028 | err = ip6_route_add(&cfg); |
1da177e4 LT |
2029 | break; |
2030 | case SIOCDELRT: | |
86872cb5 | 2031 | err = ip6_route_del(&cfg); |
1da177e4 LT |
2032 | break; |
2033 | default: | |
2034 | err = -EINVAL; | |
2035 | } | |
2036 | rtnl_unlock(); | |
2037 | ||
2038 | return err; | |
3ff50b79 | 2039 | } |
1da177e4 LT |
2040 | |
2041 | return -EINVAL; | |
2042 | } | |
2043 | ||
2044 | /* | |
2045 | * Drop the packet on the floor | |
2046 | */ | |
2047 | ||
d5fdd6ba | 2048 | static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) |
1da177e4 | 2049 | { |
612f09e8 | 2050 | int type; |
adf30907 | 2051 | struct dst_entry *dst = skb_dst(skb); |
612f09e8 YH |
2052 | switch (ipstats_mib_noroutes) { |
2053 | case IPSTATS_MIB_INNOROUTES: | |
0660e03f | 2054 | type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); |
45bb0060 | 2055 | if (type == IPV6_ADDR_ANY) { |
3bd653c8 DL |
2056 | IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), |
2057 | IPSTATS_MIB_INADDRERRORS); | |
612f09e8 YH |
2058 | break; |
2059 | } | |
2060 | /* FALLTHROUGH */ | |
2061 | case IPSTATS_MIB_OUTNOROUTES: | |
3bd653c8 DL |
2062 | IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), |
2063 | ipstats_mib_noroutes); | |
612f09e8 YH |
2064 | break; |
2065 | } | |
3ffe533c | 2066 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); |
1da177e4 LT |
2067 | kfree_skb(skb); |
2068 | return 0; | |
2069 | } | |
2070 | ||
9ce8ade0 TG |
2071 | static int ip6_pkt_discard(struct sk_buff *skb) |
2072 | { | |
612f09e8 | 2073 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); |
9ce8ade0 TG |
2074 | } |
2075 | ||
20380731 | 2076 | static int ip6_pkt_discard_out(struct sk_buff *skb) |
1da177e4 | 2077 | { |
adf30907 | 2078 | skb->dev = skb_dst(skb)->dev; |
612f09e8 | 2079 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); |
1da177e4 LT |
2080 | } |
2081 | ||
9ce8ade0 TG |
2082 | static int ip6_pkt_prohibit(struct sk_buff *skb) |
2083 | { | |
612f09e8 | 2084 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); |
9ce8ade0 TG |
2085 | } |
2086 | ||
2087 | static int ip6_pkt_prohibit_out(struct sk_buff *skb) | |
2088 | { | |
adf30907 | 2089 | skb->dev = skb_dst(skb)->dev; |
612f09e8 | 2090 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); |
9ce8ade0 TG |
2091 | } |
2092 | ||
1da177e4 LT |
2093 | /* |
2094 | * Allocate a dst for local (unicast / anycast) address. | |
2095 | */ | |
2096 | ||
2097 | struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, | |
2098 | const struct in6_addr *addr, | |
8f031519 | 2099 | bool anycast) |
1da177e4 | 2100 | { |
c346dca1 | 2101 | struct net *net = dev_net(idev->dev); |
e28c2d64 HFS |
2102 | struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, |
2103 | DST_NOCOUNT, NULL); | |
2104 | if (!rt) | |
1da177e4 LT |
2105 | return ERR_PTR(-ENOMEM); |
2106 | ||
1da177e4 LT |
2107 | in6_dev_hold(idev); |
2108 | ||
11d53b49 | 2109 | rt->dst.flags |= DST_HOST; |
d8d1f30b CG |
2110 | rt->dst.input = ip6_input; |
2111 | rt->dst.output = ip6_output; | |
1da177e4 | 2112 | rt->rt6i_idev = idev; |
1da177e4 LT |
2113 | |
2114 | rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; | |
58c4fb86 YH |
2115 | if (anycast) |
2116 | rt->rt6i_flags |= RTF_ANYCAST; | |
2117 | else | |
1da177e4 | 2118 | rt->rt6i_flags |= RTF_LOCAL; |
1da177e4 | 2119 | |
044d6efb | 2120 | rt->rt6i_gateway = *addr; |
4e3fd7a0 | 2121 | rt->rt6i_dst.addr = *addr; |
1da177e4 | 2122 | rt->rt6i_dst.plen = 128; |
5578689a | 2123 | rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL); |
1da177e4 | 2124 | |
d8d1f30b | 2125 | atomic_set(&rt->dst.__refcnt, 1); |
1da177e4 LT |
2126 | |
2127 | return rt; | |
2128 | } | |
2129 | ||
c3968a85 DW |
2130 | int ip6_route_get_saddr(struct net *net, |
2131 | struct rt6_info *rt, | |
b71d1d42 | 2132 | const struct in6_addr *daddr, |
c3968a85 DW |
2133 | unsigned int prefs, |
2134 | struct in6_addr *saddr) | |
2135 | { | |
2136 | struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt); | |
2137 | int err = 0; | |
2138 | if (rt->rt6i_prefsrc.plen) | |
4e3fd7a0 | 2139 | *saddr = rt->rt6i_prefsrc.addr; |
c3968a85 DW |
2140 | else |
2141 | err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL, | |
2142 | daddr, prefs, saddr); | |
2143 | return err; | |
2144 | } | |
2145 | ||
2146 | /* remove deleted ip from prefsrc entries */ | |
2147 | struct arg_dev_net_ip { | |
2148 | struct net_device *dev; | |
2149 | struct net *net; | |
2150 | struct in6_addr *addr; | |
2151 | }; | |
2152 | ||
2153 | static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) | |
2154 | { | |
2155 | struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; | |
2156 | struct net *net = ((struct arg_dev_net_ip *)arg)->net; | |
2157 | struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; | |
2158 | ||
d1918542 | 2159 | if (((void *)rt->dst.dev == dev || !dev) && |
c3968a85 DW |
2160 | rt != net->ipv6.ip6_null_entry && |
2161 | ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { | |
2162 | /* remove prefsrc entry */ | |
2163 | rt->rt6i_prefsrc.plen = 0; | |
2164 | } | |
2165 | return 0; | |
2166 | } | |
2167 | ||
2168 | void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) | |
2169 | { | |
2170 | struct net *net = dev_net(ifp->idev->dev); | |
2171 | struct arg_dev_net_ip adni = { | |
2172 | .dev = ifp->idev->dev, | |
2173 | .net = net, | |
2174 | .addr = &ifp->addr, | |
2175 | }; | |
2176 | fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni); | |
2177 | } | |
2178 | ||
8ed67789 DL |
2179 | struct arg_dev_net { |
2180 | struct net_device *dev; | |
2181 | struct net *net; | |
2182 | }; | |
2183 | ||
1da177e4 LT |
2184 | static int fib6_ifdown(struct rt6_info *rt, void *arg) |
2185 | { | |
bc3ef660 | 2186 | const struct arg_dev_net *adn = arg; |
2187 | const struct net_device *dev = adn->dev; | |
8ed67789 | 2188 | |
d1918542 | 2189 | if ((rt->dst.dev == dev || !dev) && |
c159d30c | 2190 | rt != adn->net->ipv6.ip6_null_entry) |
1da177e4 | 2191 | return -1; |
c159d30c | 2192 | |
1da177e4 LT |
2193 | return 0; |
2194 | } | |
2195 | ||
f3db4851 | 2196 | void rt6_ifdown(struct net *net, struct net_device *dev) |
1da177e4 | 2197 | { |
8ed67789 DL |
2198 | struct arg_dev_net adn = { |
2199 | .dev = dev, | |
2200 | .net = net, | |
2201 | }; | |
2202 | ||
2203 | fib6_clean_all(net, fib6_ifdown, 0, &adn); | |
1e493d19 | 2204 | icmp6_clean_all(fib6_ifdown, &adn); |
1da177e4 LT |
2205 | } |
2206 | ||
95c96174 | 2207 | struct rt6_mtu_change_arg { |
1da177e4 | 2208 | struct net_device *dev; |
95c96174 | 2209 | unsigned int mtu; |
1da177e4 LT |
2210 | }; |
2211 | ||
2212 | static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) | |
2213 | { | |
2214 | struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; | |
2215 | struct inet6_dev *idev; | |
2216 | ||
2217 | /* In IPv6 pmtu discovery is not optional, | |
2218 | so that RTAX_MTU lock cannot disable it. | |
2219 | We still use this lock to block changes | |
2220 | caused by addrconf/ndisc. | |
2221 | */ | |
2222 | ||
2223 | idev = __in6_dev_get(arg->dev); | |
38308473 | 2224 | if (!idev) |
1da177e4 LT |
2225 | return 0; |
2226 | ||
2227 | /* For administrative MTU increase, there is no way to discover | |
2228 | IPv6 PMTU increase, so PMTU increase should be updated here. | |
2229 | Since RFC 1981 doesn't include administrative MTU increase | |
2230 | update PMTU increase is a MUST. (i.e. jumbo frame) | |
2231 | */ | |
2232 | /* | |
2233 | If new MTU is less than route PMTU, this new MTU will be the | |
2234 | lowest MTU in the path, update the route PMTU to reflect PMTU | |
2235 | decreases; if new MTU is greater than route PMTU, and the | |
2236 | old MTU is the lowest MTU in the path, update the route PMTU | |
2237 | to reflect the increase. In this case if the other nodes' MTU | |
2238 | also have the lowest MTU, TOO BIG MESSAGE will be lead to | |
2239 | PMTU discouvery. | |
2240 | */ | |
d1918542 | 2241 | if (rt->dst.dev == arg->dev && |
d8d1f30b CG |
2242 | !dst_metric_locked(&rt->dst, RTAX_MTU) && |
2243 | (dst_mtu(&rt->dst) >= arg->mtu || | |
2244 | (dst_mtu(&rt->dst) < arg->mtu && | |
2245 | dst_mtu(&rt->dst) == idev->cnf.mtu6))) { | |
defb3519 | 2246 | dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); |
566cfd8f | 2247 | } |
1da177e4 LT |
2248 | return 0; |
2249 | } | |
2250 | ||
95c96174 | 2251 | void rt6_mtu_change(struct net_device *dev, unsigned int mtu) |
1da177e4 | 2252 | { |
c71099ac TG |
2253 | struct rt6_mtu_change_arg arg = { |
2254 | .dev = dev, | |
2255 | .mtu = mtu, | |
2256 | }; | |
1da177e4 | 2257 | |
c346dca1 | 2258 | fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg); |
1da177e4 LT |
2259 | } |
2260 | ||
ef7c79ed | 2261 | static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { |
5176f91e | 2262 | [RTA_GATEWAY] = { .len = sizeof(struct in6_addr) }, |
86872cb5 | 2263 | [RTA_OIF] = { .type = NLA_U32 }, |
ab364a6f | 2264 | [RTA_IIF] = { .type = NLA_U32 }, |
86872cb5 TG |
2265 | [RTA_PRIORITY] = { .type = NLA_U32 }, |
2266 | [RTA_METRICS] = { .type = NLA_NESTED }, | |
51ebd318 | 2267 | [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, |
86872cb5 TG |
2268 | }; |
2269 | ||
2270 | static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, | |
2271 | struct fib6_config *cfg) | |
1da177e4 | 2272 | { |
86872cb5 TG |
2273 | struct rtmsg *rtm; |
2274 | struct nlattr *tb[RTA_MAX+1]; | |
2275 | int err; | |
1da177e4 | 2276 | |
86872cb5 TG |
2277 | err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); |
2278 | if (err < 0) | |
2279 | goto errout; | |
1da177e4 | 2280 | |
86872cb5 TG |
2281 | err = -EINVAL; |
2282 | rtm = nlmsg_data(nlh); | |
2283 | memset(cfg, 0, sizeof(*cfg)); | |
2284 | ||
2285 | cfg->fc_table = rtm->rtm_table; | |
2286 | cfg->fc_dst_len = rtm->rtm_dst_len; | |
2287 | cfg->fc_src_len = rtm->rtm_src_len; | |
2288 | cfg->fc_flags = RTF_UP; | |
2289 | cfg->fc_protocol = rtm->rtm_protocol; | |
ef2c7d7b | 2290 | cfg->fc_type = rtm->rtm_type; |
86872cb5 | 2291 | |
ef2c7d7b ND |
2292 | if (rtm->rtm_type == RTN_UNREACHABLE || |
2293 | rtm->rtm_type == RTN_BLACKHOLE || | |
b4949ab2 ND |
2294 | rtm->rtm_type == RTN_PROHIBIT || |
2295 | rtm->rtm_type == RTN_THROW) | |
86872cb5 TG |
2296 | cfg->fc_flags |= RTF_REJECT; |
2297 | ||
ab79ad14 MŻ |
2298 | if (rtm->rtm_type == RTN_LOCAL) |
2299 | cfg->fc_flags |= RTF_LOCAL; | |
2300 | ||
15e47304 | 2301 | cfg->fc_nlinfo.portid = NETLINK_CB(skb).portid; |
86872cb5 | 2302 | cfg->fc_nlinfo.nlh = nlh; |
3b1e0a65 | 2303 | cfg->fc_nlinfo.nl_net = sock_net(skb->sk); |
86872cb5 TG |
2304 | |
2305 | if (tb[RTA_GATEWAY]) { | |
2306 | nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16); | |
2307 | cfg->fc_flags |= RTF_GATEWAY; | |
1da177e4 | 2308 | } |
86872cb5 TG |
2309 | |
2310 | if (tb[RTA_DST]) { | |
2311 | int plen = (rtm->rtm_dst_len + 7) >> 3; | |
2312 | ||
2313 | if (nla_len(tb[RTA_DST]) < plen) | |
2314 | goto errout; | |
2315 | ||
2316 | nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); | |
1da177e4 | 2317 | } |
86872cb5 TG |
2318 | |
2319 | if (tb[RTA_SRC]) { | |
2320 | int plen = (rtm->rtm_src_len + 7) >> 3; | |
2321 | ||
2322 | if (nla_len(tb[RTA_SRC]) < plen) | |
2323 | goto errout; | |
2324 | ||
2325 | nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); | |
1da177e4 | 2326 | } |
86872cb5 | 2327 | |
c3968a85 DW |
2328 | if (tb[RTA_PREFSRC]) |
2329 | nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16); | |
2330 | ||
86872cb5 TG |
2331 | if (tb[RTA_OIF]) |
2332 | cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); | |
2333 | ||
2334 | if (tb[RTA_PRIORITY]) | |
2335 | cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); | |
2336 | ||
2337 | if (tb[RTA_METRICS]) { | |
2338 | cfg->fc_mx = nla_data(tb[RTA_METRICS]); | |
2339 | cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); | |
1da177e4 | 2340 | } |
86872cb5 TG |
2341 | |
2342 | if (tb[RTA_TABLE]) | |
2343 | cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); | |
2344 | ||
51ebd318 ND |
2345 | if (tb[RTA_MULTIPATH]) { |
2346 | cfg->fc_mp = nla_data(tb[RTA_MULTIPATH]); | |
2347 | cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); | |
2348 | } | |
2349 | ||
86872cb5 TG |
2350 | err = 0; |
2351 | errout: | |
2352 | return err; | |
1da177e4 LT |
2353 | } |
2354 | ||
51ebd318 ND |
2355 | static int ip6_route_multipath(struct fib6_config *cfg, int add) |
2356 | { | |
2357 | struct fib6_config r_cfg; | |
2358 | struct rtnexthop *rtnh; | |
2359 | int remaining; | |
2360 | int attrlen; | |
2361 | int err = 0, last_err = 0; | |
2362 | ||
2363 | beginning: | |
2364 | rtnh = (struct rtnexthop *)cfg->fc_mp; | |
2365 | remaining = cfg->fc_mp_len; | |
2366 | ||
2367 | /* Parse a Multipath Entry */ | |
2368 | while (rtnh_ok(rtnh, remaining)) { | |
2369 | memcpy(&r_cfg, cfg, sizeof(*cfg)); | |
2370 | if (rtnh->rtnh_ifindex) | |
2371 | r_cfg.fc_ifindex = rtnh->rtnh_ifindex; | |
2372 | ||
2373 | attrlen = rtnh_attrlen(rtnh); | |
2374 | if (attrlen > 0) { | |
2375 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); | |
2376 | ||
2377 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); | |
2378 | if (nla) { | |
2379 | nla_memcpy(&r_cfg.fc_gateway, nla, 16); | |
2380 | r_cfg.fc_flags |= RTF_GATEWAY; | |
2381 | } | |
2382 | } | |
2383 | err = add ? ip6_route_add(&r_cfg) : ip6_route_del(&r_cfg); | |
2384 | if (err) { | |
2385 | last_err = err; | |
2386 | /* If we are trying to remove a route, do not stop the | |
2387 | * loop when ip6_route_del() fails (because next hop is | |
2388 | * already gone), we should try to remove all next hops. | |
2389 | */ | |
2390 | if (add) { | |
2391 | /* If add fails, we should try to delete all | |
2392 | * next hops that have been already added. | |
2393 | */ | |
2394 | add = 0; | |
2395 | goto beginning; | |
2396 | } | |
2397 | } | |
1a72418b ND |
2398 | /* Because each route is added like a single route we remove |
2399 | * this flag after the first nexthop (if there is a collision, | |
2400 | * we have already fail to add the first nexthop: | |
2401 | * fib6_add_rt2node() has reject it). | |
2402 | */ | |
2403 | cfg->fc_nlinfo.nlh->nlmsg_flags &= ~NLM_F_EXCL; | |
51ebd318 ND |
2404 | rtnh = rtnh_next(rtnh, &remaining); |
2405 | } | |
2406 | ||
2407 | return last_err; | |
2408 | } | |
2409 | ||
661d2967 | 2410 | static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh) |
1da177e4 | 2411 | { |
86872cb5 TG |
2412 | struct fib6_config cfg; |
2413 | int err; | |
1da177e4 | 2414 | |
86872cb5 TG |
2415 | err = rtm_to_fib6_config(skb, nlh, &cfg); |
2416 | if (err < 0) | |
2417 | return err; | |
2418 | ||
51ebd318 ND |
2419 | if (cfg.fc_mp) |
2420 | return ip6_route_multipath(&cfg, 0); | |
2421 | else | |
2422 | return ip6_route_del(&cfg); | |
1da177e4 LT |
2423 | } |
2424 | ||
661d2967 | 2425 | static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh) |
1da177e4 | 2426 | { |
86872cb5 TG |
2427 | struct fib6_config cfg; |
2428 | int err; | |
1da177e4 | 2429 | |
86872cb5 TG |
2430 | err = rtm_to_fib6_config(skb, nlh, &cfg); |
2431 | if (err < 0) | |
2432 | return err; | |
2433 | ||
51ebd318 ND |
2434 | if (cfg.fc_mp) |
2435 | return ip6_route_multipath(&cfg, 1); | |
2436 | else | |
2437 | return ip6_route_add(&cfg); | |
1da177e4 LT |
2438 | } |
2439 | ||
339bf98f TG |
2440 | static inline size_t rt6_nlmsg_size(void) |
2441 | { | |
2442 | return NLMSG_ALIGN(sizeof(struct rtmsg)) | |
2443 | + nla_total_size(16) /* RTA_SRC */ | |
2444 | + nla_total_size(16) /* RTA_DST */ | |
2445 | + nla_total_size(16) /* RTA_GATEWAY */ | |
2446 | + nla_total_size(16) /* RTA_PREFSRC */ | |
2447 | + nla_total_size(4) /* RTA_TABLE */ | |
2448 | + nla_total_size(4) /* RTA_IIF */ | |
2449 | + nla_total_size(4) /* RTA_OIF */ | |
2450 | + nla_total_size(4) /* RTA_PRIORITY */ | |
6a2b9ce0 | 2451 | + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ |
339bf98f TG |
2452 | + nla_total_size(sizeof(struct rta_cacheinfo)); |
2453 | } | |
2454 | ||
191cd582 BH |
2455 | static int rt6_fill_node(struct net *net, |
2456 | struct sk_buff *skb, struct rt6_info *rt, | |
0d51aa80 | 2457 | struct in6_addr *dst, struct in6_addr *src, |
15e47304 | 2458 | int iif, int type, u32 portid, u32 seq, |
7bc570c8 | 2459 | int prefix, int nowait, unsigned int flags) |
1da177e4 LT |
2460 | { |
2461 | struct rtmsg *rtm; | |
2d7202bf | 2462 | struct nlmsghdr *nlh; |
e3703b3d | 2463 | long expires; |
9e762a4a | 2464 | u32 table; |
1da177e4 LT |
2465 | |
2466 | if (prefix) { /* user wants prefix routes only */ | |
2467 | if (!(rt->rt6i_flags & RTF_PREFIX_RT)) { | |
2468 | /* success since this is not a prefix route */ | |
2469 | return 1; | |
2470 | } | |
2471 | } | |
2472 | ||
15e47304 | 2473 | nlh = nlmsg_put(skb, portid, seq, type, sizeof(*rtm), flags); |
38308473 | 2474 | if (!nlh) |
26932566 | 2475 | return -EMSGSIZE; |
2d7202bf TG |
2476 | |
2477 | rtm = nlmsg_data(nlh); | |
1da177e4 LT |
2478 | rtm->rtm_family = AF_INET6; |
2479 | rtm->rtm_dst_len = rt->rt6i_dst.plen; | |
2480 | rtm->rtm_src_len = rt->rt6i_src.plen; | |
2481 | rtm->rtm_tos = 0; | |
c71099ac | 2482 | if (rt->rt6i_table) |
9e762a4a | 2483 | table = rt->rt6i_table->tb6_id; |
c71099ac | 2484 | else |
9e762a4a PM |
2485 | table = RT6_TABLE_UNSPEC; |
2486 | rtm->rtm_table = table; | |
c78679e8 DM |
2487 | if (nla_put_u32(skb, RTA_TABLE, table)) |
2488 | goto nla_put_failure; | |
ef2c7d7b ND |
2489 | if (rt->rt6i_flags & RTF_REJECT) { |
2490 | switch (rt->dst.error) { | |
2491 | case -EINVAL: | |
2492 | rtm->rtm_type = RTN_BLACKHOLE; | |
2493 | break; | |
2494 | case -EACCES: | |
2495 | rtm->rtm_type = RTN_PROHIBIT; | |
2496 | break; | |
b4949ab2 ND |
2497 | case -EAGAIN: |
2498 | rtm->rtm_type = RTN_THROW; | |
2499 | break; | |
ef2c7d7b ND |
2500 | default: |
2501 | rtm->rtm_type = RTN_UNREACHABLE; | |
2502 | break; | |
2503 | } | |
2504 | } | |
38308473 | 2505 | else if (rt->rt6i_flags & RTF_LOCAL) |
ab79ad14 | 2506 | rtm->rtm_type = RTN_LOCAL; |
d1918542 | 2507 | else if (rt->dst.dev && (rt->dst.dev->flags & IFF_LOOPBACK)) |
1da177e4 LT |
2508 | rtm->rtm_type = RTN_LOCAL; |
2509 | else | |
2510 | rtm->rtm_type = RTN_UNICAST; | |
2511 | rtm->rtm_flags = 0; | |
2512 | rtm->rtm_scope = RT_SCOPE_UNIVERSE; | |
2513 | rtm->rtm_protocol = rt->rt6i_protocol; | |
38308473 | 2514 | if (rt->rt6i_flags & RTF_DYNAMIC) |
1da177e4 | 2515 | rtm->rtm_protocol = RTPROT_REDIRECT; |
f0396f60 DO |
2516 | else if (rt->rt6i_flags & RTF_ADDRCONF) { |
2517 | if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ROUTEINFO)) | |
2518 | rtm->rtm_protocol = RTPROT_RA; | |
2519 | else | |
2520 | rtm->rtm_protocol = RTPROT_KERNEL; | |
2521 | } | |
1da177e4 | 2522 | |
38308473 | 2523 | if (rt->rt6i_flags & RTF_CACHE) |
1da177e4 LT |
2524 | rtm->rtm_flags |= RTM_F_CLONED; |
2525 | ||
2526 | if (dst) { | |
c78679e8 DM |
2527 | if (nla_put(skb, RTA_DST, 16, dst)) |
2528 | goto nla_put_failure; | |
1ab1457c | 2529 | rtm->rtm_dst_len = 128; |
1da177e4 | 2530 | } else if (rtm->rtm_dst_len) |
c78679e8 DM |
2531 | if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr)) |
2532 | goto nla_put_failure; | |
1da177e4 LT |
2533 | #ifdef CONFIG_IPV6_SUBTREES |
2534 | if (src) { | |
c78679e8 DM |
2535 | if (nla_put(skb, RTA_SRC, 16, src)) |
2536 | goto nla_put_failure; | |
1ab1457c | 2537 | rtm->rtm_src_len = 128; |
c78679e8 DM |
2538 | } else if (rtm->rtm_src_len && |
2539 | nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr)) | |
2540 | goto nla_put_failure; | |
1da177e4 | 2541 | #endif |
7bc570c8 YH |
2542 | if (iif) { |
2543 | #ifdef CONFIG_IPV6_MROUTE | |
2544 | if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { | |
8229efda | 2545 | int err = ip6mr_get_route(net, skb, rtm, nowait); |
7bc570c8 YH |
2546 | if (err <= 0) { |
2547 | if (!nowait) { | |
2548 | if (err == 0) | |
2549 | return 0; | |
2550 | goto nla_put_failure; | |
2551 | } else { | |
2552 | if (err == -EMSGSIZE) | |
2553 | goto nla_put_failure; | |
2554 | } | |
2555 | } | |
2556 | } else | |
2557 | #endif | |
c78679e8 DM |
2558 | if (nla_put_u32(skb, RTA_IIF, iif)) |
2559 | goto nla_put_failure; | |
7bc570c8 | 2560 | } else if (dst) { |
1da177e4 | 2561 | struct in6_addr saddr_buf; |
c78679e8 DM |
2562 | if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 && |
2563 | nla_put(skb, RTA_PREFSRC, 16, &saddr_buf)) | |
2564 | goto nla_put_failure; | |
1da177e4 | 2565 | } |
2d7202bf | 2566 | |
c3968a85 DW |
2567 | if (rt->rt6i_prefsrc.plen) { |
2568 | struct in6_addr saddr_buf; | |
4e3fd7a0 | 2569 | saddr_buf = rt->rt6i_prefsrc.addr; |
c78679e8 DM |
2570 | if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf)) |
2571 | goto nla_put_failure; | |
c3968a85 DW |
2572 | } |
2573 | ||
defb3519 | 2574 | if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0) |
2d7202bf TG |
2575 | goto nla_put_failure; |
2576 | ||
dd0cbf29 YH |
2577 | if (rt->rt6i_flags & RTF_GATEWAY) { |
2578 | if (nla_put(skb, RTA_GATEWAY, 16, &rt->rt6i_gateway) < 0) | |
94f826b8 | 2579 | goto nla_put_failure; |
94f826b8 | 2580 | } |
2d7202bf | 2581 | |
c78679e8 DM |
2582 | if (rt->dst.dev && |
2583 | nla_put_u32(skb, RTA_OIF, rt->dst.dev->ifindex)) | |
2584 | goto nla_put_failure; | |
2585 | if (nla_put_u32(skb, RTA_PRIORITY, rt->rt6i_metric)) | |
2586 | goto nla_put_failure; | |
8253947e LW |
2587 | |
2588 | expires = (rt->rt6i_flags & RTF_EXPIRES) ? rt->dst.expires - jiffies : 0; | |
69cdf8f9 | 2589 | |
87a50699 | 2590 | if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0) |
e3703b3d | 2591 | goto nla_put_failure; |
2d7202bf TG |
2592 | |
2593 | return nlmsg_end(skb, nlh); | |
2594 | ||
2595 | nla_put_failure: | |
26932566 PM |
2596 | nlmsg_cancel(skb, nlh); |
2597 | return -EMSGSIZE; | |
1da177e4 LT |
2598 | } |
2599 | ||
1b43af54 | 2600 | int rt6_dump_route(struct rt6_info *rt, void *p_arg) |
1da177e4 LT |
2601 | { |
2602 | struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; | |
2603 | int prefix; | |
2604 | ||
2d7202bf TG |
2605 | if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { |
2606 | struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); | |
1da177e4 LT |
2607 | prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0; |
2608 | } else | |
2609 | prefix = 0; | |
2610 | ||
191cd582 BH |
2611 | return rt6_fill_node(arg->net, |
2612 | arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, | |
15e47304 | 2613 | NETLINK_CB(arg->cb->skb).portid, arg->cb->nlh->nlmsg_seq, |
7bc570c8 | 2614 | prefix, 0, NLM_F_MULTI); |
1da177e4 LT |
2615 | } |
2616 | ||
661d2967 | 2617 | static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh) |
1da177e4 | 2618 | { |
3b1e0a65 | 2619 | struct net *net = sock_net(in_skb->sk); |
ab364a6f TG |
2620 | struct nlattr *tb[RTA_MAX+1]; |
2621 | struct rt6_info *rt; | |
1da177e4 | 2622 | struct sk_buff *skb; |
ab364a6f | 2623 | struct rtmsg *rtm; |
4c9483b2 | 2624 | struct flowi6 fl6; |
72331bc0 | 2625 | int err, iif = 0, oif = 0; |
1da177e4 | 2626 | |
ab364a6f TG |
2627 | err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); |
2628 | if (err < 0) | |
2629 | goto errout; | |
1da177e4 | 2630 | |
ab364a6f | 2631 | err = -EINVAL; |
4c9483b2 | 2632 | memset(&fl6, 0, sizeof(fl6)); |
1da177e4 | 2633 | |
ab364a6f TG |
2634 | if (tb[RTA_SRC]) { |
2635 | if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) | |
2636 | goto errout; | |
2637 | ||
4e3fd7a0 | 2638 | fl6.saddr = *(struct in6_addr *)nla_data(tb[RTA_SRC]); |
ab364a6f TG |
2639 | } |
2640 | ||
2641 | if (tb[RTA_DST]) { | |
2642 | if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) | |
2643 | goto errout; | |
2644 | ||
4e3fd7a0 | 2645 | fl6.daddr = *(struct in6_addr *)nla_data(tb[RTA_DST]); |
ab364a6f TG |
2646 | } |
2647 | ||
2648 | if (tb[RTA_IIF]) | |
2649 | iif = nla_get_u32(tb[RTA_IIF]); | |
2650 | ||
2651 | if (tb[RTA_OIF]) | |
72331bc0 | 2652 | oif = nla_get_u32(tb[RTA_OIF]); |
1da177e4 LT |
2653 | |
2654 | if (iif) { | |
2655 | struct net_device *dev; | |
72331bc0 SL |
2656 | int flags = 0; |
2657 | ||
5578689a | 2658 | dev = __dev_get_by_index(net, iif); |
1da177e4 LT |
2659 | if (!dev) { |
2660 | err = -ENODEV; | |
ab364a6f | 2661 | goto errout; |
1da177e4 | 2662 | } |
72331bc0 SL |
2663 | |
2664 | fl6.flowi6_iif = iif; | |
2665 | ||
2666 | if (!ipv6_addr_any(&fl6.saddr)) | |
2667 | flags |= RT6_LOOKUP_F_HAS_SADDR; | |
2668 | ||
2669 | rt = (struct rt6_info *)ip6_route_input_lookup(net, dev, &fl6, | |
2670 | flags); | |
2671 | } else { | |
2672 | fl6.flowi6_oif = oif; | |
2673 | ||
2674 | rt = (struct rt6_info *)ip6_route_output(net, NULL, &fl6); | |
1da177e4 LT |
2675 | } |
2676 | ||
ab364a6f | 2677 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
38308473 | 2678 | if (!skb) { |
94e187c0 | 2679 | ip6_rt_put(rt); |
ab364a6f TG |
2680 | err = -ENOBUFS; |
2681 | goto errout; | |
2682 | } | |
1da177e4 | 2683 | |
ab364a6f TG |
2684 | /* Reserve room for dummy headers, this skb can pass |
2685 | through good chunk of routing engine. | |
2686 | */ | |
459a98ed | 2687 | skb_reset_mac_header(skb); |
ab364a6f | 2688 | skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr)); |
1da177e4 | 2689 | |
d8d1f30b | 2690 | skb_dst_set(skb, &rt->dst); |
1da177e4 | 2691 | |
4c9483b2 | 2692 | err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, |
15e47304 | 2693 | RTM_NEWROUTE, NETLINK_CB(in_skb).portid, |
7bc570c8 | 2694 | nlh->nlmsg_seq, 0, 0, 0); |
1da177e4 | 2695 | if (err < 0) { |
ab364a6f TG |
2696 | kfree_skb(skb); |
2697 | goto errout; | |
1da177e4 LT |
2698 | } |
2699 | ||
15e47304 | 2700 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); |
ab364a6f | 2701 | errout: |
1da177e4 | 2702 | return err; |
1da177e4 LT |
2703 | } |
2704 | ||
86872cb5 | 2705 | void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info) |
1da177e4 LT |
2706 | { |
2707 | struct sk_buff *skb; | |
5578689a | 2708 | struct net *net = info->nl_net; |
528c4ceb DL |
2709 | u32 seq; |
2710 | int err; | |
2711 | ||
2712 | err = -ENOBUFS; | |
38308473 | 2713 | seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
86872cb5 | 2714 | |
339bf98f | 2715 | skb = nlmsg_new(rt6_nlmsg_size(), gfp_any()); |
38308473 | 2716 | if (!skb) |
21713ebc TG |
2717 | goto errout; |
2718 | ||
191cd582 | 2719 | err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, |
15e47304 | 2720 | event, info->portid, seq, 0, 0, 0); |
26932566 PM |
2721 | if (err < 0) { |
2722 | /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ | |
2723 | WARN_ON(err == -EMSGSIZE); | |
2724 | kfree_skb(skb); | |
2725 | goto errout; | |
2726 | } | |
15e47304 | 2727 | rtnl_notify(skb, net, info->portid, RTNLGRP_IPV6_ROUTE, |
1ce85fe4 PNA |
2728 | info->nlh, gfp_any()); |
2729 | return; | |
21713ebc TG |
2730 | errout: |
2731 | if (err < 0) | |
5578689a | 2732 | rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); |
1da177e4 LT |
2733 | } |
2734 | ||
8ed67789 DL |
2735 | static int ip6_route_dev_notify(struct notifier_block *this, |
2736 | unsigned long event, void *data) | |
2737 | { | |
2738 | struct net_device *dev = (struct net_device *)data; | |
c346dca1 | 2739 | struct net *net = dev_net(dev); |
8ed67789 DL |
2740 | |
2741 | if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) { | |
d8d1f30b | 2742 | net->ipv6.ip6_null_entry->dst.dev = dev; |
8ed67789 DL |
2743 | net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); |
2744 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | |
d8d1f30b | 2745 | net->ipv6.ip6_prohibit_entry->dst.dev = dev; |
8ed67789 | 2746 | net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); |
d8d1f30b | 2747 | net->ipv6.ip6_blk_hole_entry->dst.dev = dev; |
8ed67789 DL |
2748 | net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); |
2749 | #endif | |
2750 | } | |
2751 | ||
2752 | return NOTIFY_OK; | |
2753 | } | |
2754 | ||
1da177e4 LT |
2755 | /* |
2756 | * /proc | |
2757 | */ | |
2758 | ||
2759 | #ifdef CONFIG_PROC_FS | |
2760 | ||
1da177e4 LT |
2761 | struct rt6_proc_arg |
2762 | { | |
2763 | char *buffer; | |
2764 | int offset; | |
2765 | int length; | |
2766 | int skip; | |
2767 | int len; | |
2768 | }; | |
2769 | ||
2770 | static int rt6_info_route(struct rt6_info *rt, void *p_arg) | |
2771 | { | |
33120b30 | 2772 | struct seq_file *m = p_arg; |
1da177e4 | 2773 | |
4b7a4274 | 2774 | seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen); |
1da177e4 LT |
2775 | |
2776 | #ifdef CONFIG_IPV6_SUBTREES | |
4b7a4274 | 2777 | seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen); |
1da177e4 | 2778 | #else |
33120b30 | 2779 | seq_puts(m, "00000000000000000000000000000000 00 "); |
1da177e4 | 2780 | #endif |
dd0cbf29 YH |
2781 | if (rt->rt6i_flags & RTF_GATEWAY) { |
2782 | seq_printf(m, "%pi6", &rt->rt6i_gateway); | |
1da177e4 | 2783 | } else { |
33120b30 | 2784 | seq_puts(m, "00000000000000000000000000000000"); |
1da177e4 | 2785 | } |
33120b30 | 2786 | seq_printf(m, " %08x %08x %08x %08x %8s\n", |
d8d1f30b CG |
2787 | rt->rt6i_metric, atomic_read(&rt->dst.__refcnt), |
2788 | rt->dst.__use, rt->rt6i_flags, | |
d1918542 | 2789 | rt->dst.dev ? rt->dst.dev->name : ""); |
1da177e4 LT |
2790 | return 0; |
2791 | } | |
2792 | ||
33120b30 | 2793 | static int ipv6_route_show(struct seq_file *m, void *v) |
1da177e4 | 2794 | { |
f3db4851 | 2795 | struct net *net = (struct net *)m->private; |
32b293a5 | 2796 | fib6_clean_all_ro(net, rt6_info_route, 0, m); |
33120b30 AD |
2797 | return 0; |
2798 | } | |
1da177e4 | 2799 | |
33120b30 AD |
2800 | static int ipv6_route_open(struct inode *inode, struct file *file) |
2801 | { | |
de05c557 | 2802 | return single_open_net(inode, file, ipv6_route_show); |
f3db4851 DL |
2803 | } |
2804 | ||
33120b30 AD |
2805 | static const struct file_operations ipv6_route_proc_fops = { |
2806 | .owner = THIS_MODULE, | |
2807 | .open = ipv6_route_open, | |
2808 | .read = seq_read, | |
2809 | .llseek = seq_lseek, | |
b6fcbdb4 | 2810 | .release = single_release_net, |
33120b30 AD |
2811 | }; |
2812 | ||
1da177e4 LT |
2813 | static int rt6_stats_seq_show(struct seq_file *seq, void *v) |
2814 | { | |
69ddb805 | 2815 | struct net *net = (struct net *)seq->private; |
1da177e4 | 2816 | seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", |
69ddb805 DL |
2817 | net->ipv6.rt6_stats->fib_nodes, |
2818 | net->ipv6.rt6_stats->fib_route_nodes, | |
2819 | net->ipv6.rt6_stats->fib_rt_alloc, | |
2820 | net->ipv6.rt6_stats->fib_rt_entries, | |
2821 | net->ipv6.rt6_stats->fib_rt_cache, | |
fc66f95c | 2822 | dst_entries_get_slow(&net->ipv6.ip6_dst_ops), |
69ddb805 | 2823 | net->ipv6.rt6_stats->fib_discarded_routes); |
1da177e4 LT |
2824 | |
2825 | return 0; | |
2826 | } | |
2827 | ||
2828 | static int rt6_stats_seq_open(struct inode *inode, struct file *file) | |
2829 | { | |
de05c557 | 2830 | return single_open_net(inode, file, rt6_stats_seq_show); |
69ddb805 DL |
2831 | } |
2832 | ||
9a32144e | 2833 | static const struct file_operations rt6_stats_seq_fops = { |
1da177e4 LT |
2834 | .owner = THIS_MODULE, |
2835 | .open = rt6_stats_seq_open, | |
2836 | .read = seq_read, | |
2837 | .llseek = seq_lseek, | |
b6fcbdb4 | 2838 | .release = single_release_net, |
1da177e4 LT |
2839 | }; |
2840 | #endif /* CONFIG_PROC_FS */ | |
2841 | ||
2842 | #ifdef CONFIG_SYSCTL | |
2843 | ||
1da177e4 | 2844 | static |
8d65af78 | 2845 | int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, |
1da177e4 LT |
2846 | void __user *buffer, size_t *lenp, loff_t *ppos) |
2847 | { | |
c486da34 LAG |
2848 | struct net *net; |
2849 | int delay; | |
2850 | if (!write) | |
1da177e4 | 2851 | return -EINVAL; |
c486da34 LAG |
2852 | |
2853 | net = (struct net *)ctl->extra1; | |
2854 | delay = net->ipv6.sysctl.flush_delay; | |
2855 | proc_dointvec(ctl, write, buffer, lenp, ppos); | |
2856 | fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net); | |
2857 | return 0; | |
1da177e4 LT |
2858 | } |
2859 | ||
760f2d01 | 2860 | ctl_table ipv6_route_table_template[] = { |
1ab1457c | 2861 | { |
1da177e4 | 2862 | .procname = "flush", |
4990509f | 2863 | .data = &init_net.ipv6.sysctl.flush_delay, |
1da177e4 | 2864 | .maxlen = sizeof(int), |
89c8b3a1 | 2865 | .mode = 0200, |
6d9f239a | 2866 | .proc_handler = ipv6_sysctl_rtcache_flush |
1da177e4 LT |
2867 | }, |
2868 | { | |
1da177e4 | 2869 | .procname = "gc_thresh", |
9a7ec3a9 | 2870 | .data = &ip6_dst_ops_template.gc_thresh, |
1da177e4 LT |
2871 | .maxlen = sizeof(int), |
2872 | .mode = 0644, | |
6d9f239a | 2873 | .proc_handler = proc_dointvec, |
1da177e4 LT |
2874 | }, |
2875 | { | |
1da177e4 | 2876 | .procname = "max_size", |
4990509f | 2877 | .data = &init_net.ipv6.sysctl.ip6_rt_max_size, |
1da177e4 LT |
2878 | .maxlen = sizeof(int), |
2879 | .mode = 0644, | |
6d9f239a | 2880 | .proc_handler = proc_dointvec, |
1da177e4 LT |
2881 | }, |
2882 | { | |
1da177e4 | 2883 | .procname = "gc_min_interval", |
4990509f | 2884 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, |
1da177e4 LT |
2885 | .maxlen = sizeof(int), |
2886 | .mode = 0644, | |
6d9f239a | 2887 | .proc_handler = proc_dointvec_jiffies, |
1da177e4 LT |
2888 | }, |
2889 | { | |
1da177e4 | 2890 | .procname = "gc_timeout", |
4990509f | 2891 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_timeout, |
1da177e4 LT |
2892 | .maxlen = sizeof(int), |
2893 | .mode = 0644, | |
6d9f239a | 2894 | .proc_handler = proc_dointvec_jiffies, |
1da177e4 LT |
2895 | }, |
2896 | { | |
1da177e4 | 2897 | .procname = "gc_interval", |
4990509f | 2898 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_interval, |
1da177e4 LT |
2899 | .maxlen = sizeof(int), |
2900 | .mode = 0644, | |
6d9f239a | 2901 | .proc_handler = proc_dointvec_jiffies, |
1da177e4 LT |
2902 | }, |
2903 | { | |
1da177e4 | 2904 | .procname = "gc_elasticity", |
4990509f | 2905 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_elasticity, |
1da177e4 LT |
2906 | .maxlen = sizeof(int), |
2907 | .mode = 0644, | |
f3d3f616 | 2908 | .proc_handler = proc_dointvec, |
1da177e4 LT |
2909 | }, |
2910 | { | |
1da177e4 | 2911 | .procname = "mtu_expires", |
4990509f | 2912 | .data = &init_net.ipv6.sysctl.ip6_rt_mtu_expires, |
1da177e4 LT |
2913 | .maxlen = sizeof(int), |
2914 | .mode = 0644, | |
6d9f239a | 2915 | .proc_handler = proc_dointvec_jiffies, |
1da177e4 LT |
2916 | }, |
2917 | { | |
1da177e4 | 2918 | .procname = "min_adv_mss", |
4990509f | 2919 | .data = &init_net.ipv6.sysctl.ip6_rt_min_advmss, |
1da177e4 LT |
2920 | .maxlen = sizeof(int), |
2921 | .mode = 0644, | |
f3d3f616 | 2922 | .proc_handler = proc_dointvec, |
1da177e4 LT |
2923 | }, |
2924 | { | |
1da177e4 | 2925 | .procname = "gc_min_interval_ms", |
4990509f | 2926 | .data = &init_net.ipv6.sysctl.ip6_rt_gc_min_interval, |
1da177e4 LT |
2927 | .maxlen = sizeof(int), |
2928 | .mode = 0644, | |
6d9f239a | 2929 | .proc_handler = proc_dointvec_ms_jiffies, |
1da177e4 | 2930 | }, |
f8572d8f | 2931 | { } |
1da177e4 LT |
2932 | }; |
2933 | ||
2c8c1e72 | 2934 | struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) |
760f2d01 DL |
2935 | { |
2936 | struct ctl_table *table; | |
2937 | ||
2938 | table = kmemdup(ipv6_route_table_template, | |
2939 | sizeof(ipv6_route_table_template), | |
2940 | GFP_KERNEL); | |
5ee09105 YH |
2941 | |
2942 | if (table) { | |
2943 | table[0].data = &net->ipv6.sysctl.flush_delay; | |
c486da34 | 2944 | table[0].extra1 = net; |
86393e52 | 2945 | table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; |
5ee09105 YH |
2946 | table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; |
2947 | table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; | |
2948 | table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; | |
2949 | table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; | |
2950 | table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; | |
2951 | table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; | |
2952 | table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; | |
9c69fabe | 2953 | table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; |
464dc801 EB |
2954 | |
2955 | /* Don't export sysctls to unprivileged users */ | |
2956 | if (net->user_ns != &init_user_ns) | |
2957 | table[0].procname = NULL; | |
5ee09105 YH |
2958 | } |
2959 | ||
760f2d01 DL |
2960 | return table; |
2961 | } | |
1da177e4 LT |
2962 | #endif |
2963 | ||
2c8c1e72 | 2964 | static int __net_init ip6_route_net_init(struct net *net) |
cdb18761 | 2965 | { |
633d424b | 2966 | int ret = -ENOMEM; |
8ed67789 | 2967 | |
86393e52 AD |
2968 | memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, |
2969 | sizeof(net->ipv6.ip6_dst_ops)); | |
f2fc6a54 | 2970 | |
fc66f95c ED |
2971 | if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) |
2972 | goto out_ip6_dst_ops; | |
2973 | ||
8ed67789 DL |
2974 | net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, |
2975 | sizeof(*net->ipv6.ip6_null_entry), | |
2976 | GFP_KERNEL); | |
2977 | if (!net->ipv6.ip6_null_entry) | |
fc66f95c | 2978 | goto out_ip6_dst_entries; |
d8d1f30b | 2979 | net->ipv6.ip6_null_entry->dst.path = |
8ed67789 | 2980 | (struct dst_entry *)net->ipv6.ip6_null_entry; |
d8d1f30b | 2981 | net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; |
62fa8a84 DM |
2982 | dst_init_metrics(&net->ipv6.ip6_null_entry->dst, |
2983 | ip6_template_metrics, true); | |
8ed67789 DL |
2984 | |
2985 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | |
2986 | net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, | |
2987 | sizeof(*net->ipv6.ip6_prohibit_entry), | |
2988 | GFP_KERNEL); | |
68fffc67 PZ |
2989 | if (!net->ipv6.ip6_prohibit_entry) |
2990 | goto out_ip6_null_entry; | |
d8d1f30b | 2991 | net->ipv6.ip6_prohibit_entry->dst.path = |
8ed67789 | 2992 | (struct dst_entry *)net->ipv6.ip6_prohibit_entry; |
d8d1f30b | 2993 | net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; |
62fa8a84 DM |
2994 | dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, |
2995 | ip6_template_metrics, true); | |
8ed67789 DL |
2996 | |
2997 | net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, | |
2998 | sizeof(*net->ipv6.ip6_blk_hole_entry), | |
2999 | GFP_KERNEL); | |
68fffc67 PZ |
3000 | if (!net->ipv6.ip6_blk_hole_entry) |
3001 | goto out_ip6_prohibit_entry; | |
d8d1f30b | 3002 | net->ipv6.ip6_blk_hole_entry->dst.path = |
8ed67789 | 3003 | (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; |
d8d1f30b | 3004 | net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; |
62fa8a84 DM |
3005 | dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, |
3006 | ip6_template_metrics, true); | |
8ed67789 DL |
3007 | #endif |
3008 | ||
b339a47c PZ |
3009 | net->ipv6.sysctl.flush_delay = 0; |
3010 | net->ipv6.sysctl.ip6_rt_max_size = 4096; | |
3011 | net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; | |
3012 | net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; | |
3013 | net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; | |
3014 | net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; | |
3015 | net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; | |
3016 | net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; | |
3017 | ||
6891a346 BT |
3018 | net->ipv6.ip6_rt_gc_expire = 30*HZ; |
3019 | ||
8ed67789 DL |
3020 | ret = 0; |
3021 | out: | |
3022 | return ret; | |
f2fc6a54 | 3023 | |
68fffc67 PZ |
3024 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES |
3025 | out_ip6_prohibit_entry: | |
3026 | kfree(net->ipv6.ip6_prohibit_entry); | |
3027 | out_ip6_null_entry: | |
3028 | kfree(net->ipv6.ip6_null_entry); | |
3029 | #endif | |
fc66f95c ED |
3030 | out_ip6_dst_entries: |
3031 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); | |
f2fc6a54 | 3032 | out_ip6_dst_ops: |
f2fc6a54 | 3033 | goto out; |
cdb18761 DL |
3034 | } |
3035 | ||
2c8c1e72 | 3036 | static void __net_exit ip6_route_net_exit(struct net *net) |
cdb18761 | 3037 | { |
8ed67789 DL |
3038 | kfree(net->ipv6.ip6_null_entry); |
3039 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | |
3040 | kfree(net->ipv6.ip6_prohibit_entry); | |
3041 | kfree(net->ipv6.ip6_blk_hole_entry); | |
3042 | #endif | |
41bb78b4 | 3043 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); |
cdb18761 DL |
3044 | } |
3045 | ||
d189634e TG |
3046 | static int __net_init ip6_route_net_init_late(struct net *net) |
3047 | { | |
3048 | #ifdef CONFIG_PROC_FS | |
d4beaa66 G |
3049 | proc_create("ipv6_route", 0, net->proc_net, &ipv6_route_proc_fops); |
3050 | proc_create("rt6_stats", S_IRUGO, net->proc_net, &rt6_stats_seq_fops); | |
d189634e TG |
3051 | #endif |
3052 | return 0; | |
3053 | } | |
3054 | ||
3055 | static void __net_exit ip6_route_net_exit_late(struct net *net) | |
3056 | { | |
3057 | #ifdef CONFIG_PROC_FS | |
ece31ffd G |
3058 | remove_proc_entry("ipv6_route", net->proc_net); |
3059 | remove_proc_entry("rt6_stats", net->proc_net); | |
d189634e TG |
3060 | #endif |
3061 | } | |
3062 | ||
cdb18761 DL |
3063 | static struct pernet_operations ip6_route_net_ops = { |
3064 | .init = ip6_route_net_init, | |
3065 | .exit = ip6_route_net_exit, | |
3066 | }; | |
3067 | ||
c3426b47 DM |
3068 | static int __net_init ipv6_inetpeer_init(struct net *net) |
3069 | { | |
3070 | struct inet_peer_base *bp = kmalloc(sizeof(*bp), GFP_KERNEL); | |
3071 | ||
3072 | if (!bp) | |
3073 | return -ENOMEM; | |
3074 | inet_peer_base_init(bp); | |
3075 | net->ipv6.peers = bp; | |
3076 | return 0; | |
3077 | } | |
3078 | ||
3079 | static void __net_exit ipv6_inetpeer_exit(struct net *net) | |
3080 | { | |
3081 | struct inet_peer_base *bp = net->ipv6.peers; | |
3082 | ||
3083 | net->ipv6.peers = NULL; | |
56a6b248 | 3084 | inetpeer_invalidate_tree(bp); |
c3426b47 DM |
3085 | kfree(bp); |
3086 | } | |
3087 | ||
2b823f72 | 3088 | static struct pernet_operations ipv6_inetpeer_ops = { |
c3426b47 DM |
3089 | .init = ipv6_inetpeer_init, |
3090 | .exit = ipv6_inetpeer_exit, | |
3091 | }; | |
3092 | ||
d189634e TG |
3093 | static struct pernet_operations ip6_route_net_late_ops = { |
3094 | .init = ip6_route_net_init_late, | |
3095 | .exit = ip6_route_net_exit_late, | |
3096 | }; | |
3097 | ||
8ed67789 DL |
3098 | static struct notifier_block ip6_route_dev_notifier = { |
3099 | .notifier_call = ip6_route_dev_notify, | |
3100 | .priority = 0, | |
3101 | }; | |
3102 | ||
433d49c3 | 3103 | int __init ip6_route_init(void) |
1da177e4 | 3104 | { |
433d49c3 DL |
3105 | int ret; |
3106 | ||
9a7ec3a9 DL |
3107 | ret = -ENOMEM; |
3108 | ip6_dst_ops_template.kmem_cachep = | |
e5d679f3 | 3109 | kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, |
f845ab6b | 3110 | SLAB_HWCACHE_ALIGN, NULL); |
9a7ec3a9 | 3111 | if (!ip6_dst_ops_template.kmem_cachep) |
c19a28e1 | 3112 | goto out; |
14e50e57 | 3113 | |
fc66f95c | 3114 | ret = dst_entries_init(&ip6_dst_blackhole_ops); |
8ed67789 | 3115 | if (ret) |
bdb3289f | 3116 | goto out_kmem_cache; |
bdb3289f | 3117 | |
c3426b47 DM |
3118 | ret = register_pernet_subsys(&ipv6_inetpeer_ops); |
3119 | if (ret) | |
e8803b6c | 3120 | goto out_dst_entries; |
2a0c451a | 3121 | |
7e52b33b DM |
3122 | ret = register_pernet_subsys(&ip6_route_net_ops); |
3123 | if (ret) | |
3124 | goto out_register_inetpeer; | |
c3426b47 | 3125 | |
5dc121e9 AE |
3126 | ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; |
3127 | ||
8ed67789 DL |
3128 | /* Registering of the loopback is done before this portion of code, |
3129 | * the loopback reference in rt6_info will not be taken, do it | |
3130 | * manually for init_net */ | |
d8d1f30b | 3131 | init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; |
8ed67789 DL |
3132 | init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
3133 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | |
d8d1f30b | 3134 | init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; |
8ed67789 | 3135 | init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
d8d1f30b | 3136 | init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; |
8ed67789 DL |
3137 | init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); |
3138 | #endif | |
e8803b6c | 3139 | ret = fib6_init(); |
433d49c3 | 3140 | if (ret) |
8ed67789 | 3141 | goto out_register_subsys; |
433d49c3 | 3142 | |
433d49c3 DL |
3143 | ret = xfrm6_init(); |
3144 | if (ret) | |
e8803b6c | 3145 | goto out_fib6_init; |
c35b7e72 | 3146 | |
433d49c3 DL |
3147 | ret = fib6_rules_init(); |
3148 | if (ret) | |
3149 | goto xfrm6_init; | |
7e5449c2 | 3150 | |
d189634e TG |
3151 | ret = register_pernet_subsys(&ip6_route_net_late_ops); |
3152 | if (ret) | |
3153 | goto fib6_rules_init; | |
3154 | ||
433d49c3 | 3155 | ret = -ENOBUFS; |
c7ac8679 GR |
3156 | if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL, NULL) || |
3157 | __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL, NULL) || | |
3158 | __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL, NULL)) | |
d189634e | 3159 | goto out_register_late_subsys; |
c127ea2c | 3160 | |
8ed67789 | 3161 | ret = register_netdevice_notifier(&ip6_route_dev_notifier); |
cdb18761 | 3162 | if (ret) |
d189634e | 3163 | goto out_register_late_subsys; |
8ed67789 | 3164 | |
433d49c3 DL |
3165 | out: |
3166 | return ret; | |
3167 | ||
d189634e TG |
3168 | out_register_late_subsys: |
3169 | unregister_pernet_subsys(&ip6_route_net_late_ops); | |
433d49c3 | 3170 | fib6_rules_init: |
433d49c3 DL |
3171 | fib6_rules_cleanup(); |
3172 | xfrm6_init: | |
433d49c3 | 3173 | xfrm6_fini(); |
2a0c451a TG |
3174 | out_fib6_init: |
3175 | fib6_gc_cleanup(); | |
8ed67789 DL |
3176 | out_register_subsys: |
3177 | unregister_pernet_subsys(&ip6_route_net_ops); | |
7e52b33b DM |
3178 | out_register_inetpeer: |
3179 | unregister_pernet_subsys(&ipv6_inetpeer_ops); | |
fc66f95c ED |
3180 | out_dst_entries: |
3181 | dst_entries_destroy(&ip6_dst_blackhole_ops); | |
433d49c3 | 3182 | out_kmem_cache: |
f2fc6a54 | 3183 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); |
433d49c3 | 3184 | goto out; |
1da177e4 LT |
3185 | } |
3186 | ||
3187 | void ip6_route_cleanup(void) | |
3188 | { | |
8ed67789 | 3189 | unregister_netdevice_notifier(&ip6_route_dev_notifier); |
d189634e | 3190 | unregister_pernet_subsys(&ip6_route_net_late_ops); |
101367c2 | 3191 | fib6_rules_cleanup(); |
1da177e4 | 3192 | xfrm6_fini(); |
1da177e4 | 3193 | fib6_gc_cleanup(); |
c3426b47 | 3194 | unregister_pernet_subsys(&ipv6_inetpeer_ops); |
8ed67789 | 3195 | unregister_pernet_subsys(&ip6_route_net_ops); |
41bb78b4 | 3196 | dst_entries_destroy(&ip6_dst_blackhole_ops); |
f2fc6a54 | 3197 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); |
1da177e4 | 3198 | } |