]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * INET An implementation of the TCP/IP protocol suite for the LINUX | |
3 | * operating system. INET is implemented using the BSD Socket | |
4 | * interface as the means of communication with the user level. | |
5 | * | |
6 | * IPv4 Forwarding Information Base: semantics. | |
7 | * | |
1da177e4 LT |
8 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
9 | * | |
10 | * This program is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU General Public License | |
12 | * as published by the Free Software Foundation; either version | |
13 | * 2 of the License, or (at your option) any later version. | |
14 | */ | |
15 | ||
1da177e4 | 16 | #include <asm/uaccess.h> |
1da177e4 LT |
17 | #include <linux/bitops.h> |
18 | #include <linux/types.h> | |
19 | #include <linux/kernel.h> | |
20 | #include <linux/jiffies.h> | |
21 | #include <linux/mm.h> | |
22 | #include <linux/string.h> | |
23 | #include <linux/socket.h> | |
24 | #include <linux/sockios.h> | |
25 | #include <linux/errno.h> | |
26 | #include <linux/in.h> | |
27 | #include <linux/inet.h> | |
14c85021 | 28 | #include <linux/inetdevice.h> |
1da177e4 LT |
29 | #include <linux/netdevice.h> |
30 | #include <linux/if_arp.h> | |
31 | #include <linux/proc_fs.h> | |
32 | #include <linux/skbuff.h> | |
1da177e4 | 33 | #include <linux/init.h> |
5a0e3ad6 | 34 | #include <linux/slab.h> |
1da177e4 | 35 | |
14c85021 | 36 | #include <net/arp.h> |
1da177e4 LT |
37 | #include <net/ip.h> |
38 | #include <net/protocol.h> | |
39 | #include <net/route.h> | |
40 | #include <net/tcp.h> | |
41 | #include <net/sock.h> | |
42 | #include <net/ip_fib.h> | |
f21c7bc5 | 43 | #include <net/netlink.h> |
4e902c57 | 44 | #include <net/nexthop.h> |
1da177e4 LT |
45 | |
46 | #include "fib_lookup.h" | |
47 | ||
832b4c5e | 48 | static DEFINE_SPINLOCK(fib_info_lock); |
1da177e4 LT |
49 | static struct hlist_head *fib_info_hash; |
50 | static struct hlist_head *fib_info_laddrhash; | |
123b9731 | 51 | static unsigned int fib_info_hash_size; |
1da177e4 LT |
52 | static unsigned int fib_info_cnt; |
53 | ||
54 | #define DEVINDEX_HASHBITS 8 | |
55 | #define DEVINDEX_HASHSIZE (1U << DEVINDEX_HASHBITS) | |
56 | static struct hlist_head fib_info_devhash[DEVINDEX_HASHSIZE]; | |
57 | ||
58 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
59 | ||
60 | static DEFINE_SPINLOCK(fib_multipath_lock); | |
61 | ||
6a31d2a9 ED |
62 | #define for_nexthops(fi) { \ |
63 | int nhsel; const struct fib_nh *nh; \ | |
64 | for (nhsel = 0, nh = (fi)->fib_nh; \ | |
65 | nhsel < (fi)->fib_nhs; \ | |
66 | nh++, nhsel++) | |
67 | ||
68 | #define change_nexthops(fi) { \ | |
69 | int nhsel; struct fib_nh *nexthop_nh; \ | |
70 | for (nhsel = 0, nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \ | |
71 | nhsel < (fi)->fib_nhs; \ | |
72 | nexthop_nh++, nhsel++) | |
1da177e4 LT |
73 | |
74 | #else /* CONFIG_IP_ROUTE_MULTIPATH */ | |
75 | ||
76 | /* Hope, that gcc will optimize it to get rid of dummy loop */ | |
77 | ||
6a31d2a9 ED |
78 | #define for_nexthops(fi) { \ |
79 | int nhsel; const struct fib_nh *nh = (fi)->fib_nh; \ | |
80 | for (nhsel = 0; nhsel < 1; nhsel++) | |
1da177e4 | 81 | |
6a31d2a9 ED |
82 | #define change_nexthops(fi) { \ |
83 | int nhsel; \ | |
84 | struct fib_nh *nexthop_nh = (struct fib_nh *)((fi)->fib_nh); \ | |
85 | for (nhsel = 0; nhsel < 1; nhsel++) | |
1da177e4 LT |
86 | |
87 | #endif /* CONFIG_IP_ROUTE_MULTIPATH */ | |
88 | ||
89 | #define endfor_nexthops(fi) } | |
90 | ||
91 | ||
3be0686b | 92 | const struct fib_prop fib_props[RTN_MAX + 1] = { |
6a31d2a9 | 93 | [RTN_UNSPEC] = { |
1da177e4 LT |
94 | .error = 0, |
95 | .scope = RT_SCOPE_NOWHERE, | |
6a31d2a9 ED |
96 | }, |
97 | [RTN_UNICAST] = { | |
1da177e4 LT |
98 | .error = 0, |
99 | .scope = RT_SCOPE_UNIVERSE, | |
6a31d2a9 ED |
100 | }, |
101 | [RTN_LOCAL] = { | |
1da177e4 LT |
102 | .error = 0, |
103 | .scope = RT_SCOPE_HOST, | |
6a31d2a9 ED |
104 | }, |
105 | [RTN_BROADCAST] = { | |
1da177e4 LT |
106 | .error = 0, |
107 | .scope = RT_SCOPE_LINK, | |
6a31d2a9 ED |
108 | }, |
109 | [RTN_ANYCAST] = { | |
1da177e4 LT |
110 | .error = 0, |
111 | .scope = RT_SCOPE_LINK, | |
6a31d2a9 ED |
112 | }, |
113 | [RTN_MULTICAST] = { | |
1da177e4 LT |
114 | .error = 0, |
115 | .scope = RT_SCOPE_UNIVERSE, | |
6a31d2a9 ED |
116 | }, |
117 | [RTN_BLACKHOLE] = { | |
1da177e4 LT |
118 | .error = -EINVAL, |
119 | .scope = RT_SCOPE_UNIVERSE, | |
6a31d2a9 ED |
120 | }, |
121 | [RTN_UNREACHABLE] = { | |
1da177e4 LT |
122 | .error = -EHOSTUNREACH, |
123 | .scope = RT_SCOPE_UNIVERSE, | |
6a31d2a9 ED |
124 | }, |
125 | [RTN_PROHIBIT] = { | |
1da177e4 LT |
126 | .error = -EACCES, |
127 | .scope = RT_SCOPE_UNIVERSE, | |
6a31d2a9 ED |
128 | }, |
129 | [RTN_THROW] = { | |
1da177e4 LT |
130 | .error = -EAGAIN, |
131 | .scope = RT_SCOPE_UNIVERSE, | |
6a31d2a9 ED |
132 | }, |
133 | [RTN_NAT] = { | |
1da177e4 LT |
134 | .error = -EINVAL, |
135 | .scope = RT_SCOPE_NOWHERE, | |
6a31d2a9 ED |
136 | }, |
137 | [RTN_XRESOLVE] = { | |
1da177e4 LT |
138 | .error = -EINVAL, |
139 | .scope = RT_SCOPE_NOWHERE, | |
6a31d2a9 | 140 | }, |
1da177e4 LT |
141 | }; |
142 | ||
4895c771 DM |
143 | static void free_nh_exceptions(struct fib_nh *nh) |
144 | { | |
145 | struct fnhe_hash_bucket *hash = nh->nh_exceptions; | |
146 | int i; | |
147 | ||
148 | for (i = 0; i < FNHE_HASH_SIZE; i++) { | |
149 | struct fib_nh_exception *fnhe; | |
150 | ||
5abf7f7e | 151 | fnhe = rcu_dereference_protected(hash[i].chain, 1); |
4895c771 DM |
152 | while (fnhe) { |
153 | struct fib_nh_exception *next; | |
154 | ||
5abf7f7e | 155 | next = rcu_dereference_protected(fnhe->fnhe_next, 1); |
4895c771 DM |
156 | kfree(fnhe); |
157 | ||
158 | fnhe = next; | |
159 | } | |
160 | } | |
161 | kfree(hash); | |
162 | } | |
163 | ||
1da177e4 | 164 | /* Release a nexthop info record */ |
19c1ea14 YZ |
165 | static void free_fib_info_rcu(struct rcu_head *head) |
166 | { | |
167 | struct fib_info *fi = container_of(head, struct fib_info, rcu); | |
168 | ||
e49cc0da YZ |
169 | change_nexthops(fi) { |
170 | if (nexthop_nh->nh_dev) | |
171 | dev_put(nexthop_nh->nh_dev); | |
4895c771 DM |
172 | if (nexthop_nh->nh_exceptions) |
173 | free_nh_exceptions(nexthop_nh); | |
f2bb4bed DM |
174 | if (nexthop_nh->nh_rth_output) |
175 | dst_release(&nexthop_nh->nh_rth_output->dst); | |
e49cc0da YZ |
176 | } endfor_nexthops(fi); |
177 | ||
178 | release_net(fi->fib_net); | |
19c1ea14 YZ |
179 | if (fi->fib_metrics != (u32 *) dst_default_metrics) |
180 | kfree(fi->fib_metrics); | |
181 | kfree(fi); | |
182 | } | |
1da177e4 LT |
183 | |
184 | void free_fib_info(struct fib_info *fi) | |
185 | { | |
186 | if (fi->fib_dead == 0) { | |
058bd4d2 | 187 | pr_warn("Freeing alive fib_info %p\n", fi); |
1da177e4 LT |
188 | return; |
189 | } | |
1da177e4 | 190 | fib_info_cnt--; |
7a9bc9b8 DM |
191 | #ifdef CONFIG_IP_ROUTE_CLASSID |
192 | change_nexthops(fi) { | |
193 | if (nexthop_nh->nh_tclassid) | |
f4530fa5 | 194 | fi->fib_net->ipv4.fib_num_tclassid_users--; |
7a9bc9b8 DM |
195 | } endfor_nexthops(fi); |
196 | #endif | |
19c1ea14 | 197 | call_rcu(&fi->rcu, free_fib_info_rcu); |
1da177e4 LT |
198 | } |
199 | ||
200 | void fib_release_info(struct fib_info *fi) | |
201 | { | |
832b4c5e | 202 | spin_lock_bh(&fib_info_lock); |
1da177e4 LT |
203 | if (fi && --fi->fib_treeref == 0) { |
204 | hlist_del(&fi->fib_hash); | |
205 | if (fi->fib_prefsrc) | |
206 | hlist_del(&fi->fib_lhash); | |
207 | change_nexthops(fi) { | |
71fceff0 | 208 | if (!nexthop_nh->nh_dev) |
1da177e4 | 209 | continue; |
71fceff0 | 210 | hlist_del(&nexthop_nh->nh_hash); |
1da177e4 LT |
211 | } endfor_nexthops(fi) |
212 | fi->fib_dead = 1; | |
213 | fib_info_put(fi); | |
214 | } | |
832b4c5e | 215 | spin_unlock_bh(&fib_info_lock); |
1da177e4 LT |
216 | } |
217 | ||
6a31d2a9 | 218 | static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi) |
1da177e4 LT |
219 | { |
220 | const struct fib_nh *onh = ofi->fib_nh; | |
221 | ||
222 | for_nexthops(fi) { | |
223 | if (nh->nh_oif != onh->nh_oif || | |
224 | nh->nh_gw != onh->nh_gw || | |
225 | nh->nh_scope != onh->nh_scope || | |
226 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
227 | nh->nh_weight != onh->nh_weight || | |
228 | #endif | |
c7066f70 | 229 | #ifdef CONFIG_IP_ROUTE_CLASSID |
1da177e4 LT |
230 | nh->nh_tclassid != onh->nh_tclassid || |
231 | #endif | |
6a31d2a9 | 232 | ((nh->nh_flags ^ onh->nh_flags) & ~RTNH_F_DEAD)) |
1da177e4 LT |
233 | return -1; |
234 | onh++; | |
235 | } endfor_nexthops(fi); | |
236 | return 0; | |
237 | } | |
238 | ||
88ebc72f DM |
239 | static inline unsigned int fib_devindex_hashfn(unsigned int val) |
240 | { | |
241 | unsigned int mask = DEVINDEX_HASHSIZE - 1; | |
242 | ||
243 | return (val ^ | |
244 | (val >> DEVINDEX_HASHBITS) ^ | |
245 | (val >> (DEVINDEX_HASHBITS * 2))) & mask; | |
246 | } | |
247 | ||
1da177e4 LT |
248 | static inline unsigned int fib_info_hashfn(const struct fib_info *fi) |
249 | { | |
123b9731 | 250 | unsigned int mask = (fib_info_hash_size - 1); |
1da177e4 LT |
251 | unsigned int val = fi->fib_nhs; |
252 | ||
37e826c5 | 253 | val ^= (fi->fib_protocol << 8) | fi->fib_scope; |
81f7bf6c | 254 | val ^= (__force u32)fi->fib_prefsrc; |
1da177e4 | 255 | val ^= fi->fib_priority; |
88ebc72f DM |
256 | for_nexthops(fi) { |
257 | val ^= fib_devindex_hashfn(nh->nh_oif); | |
258 | } endfor_nexthops(fi) | |
1da177e4 LT |
259 | |
260 | return (val ^ (val >> 7) ^ (val >> 12)) & mask; | |
261 | } | |
262 | ||
263 | static struct fib_info *fib_find_info(const struct fib_info *nfi) | |
264 | { | |
265 | struct hlist_head *head; | |
266 | struct hlist_node *node; | |
267 | struct fib_info *fi; | |
268 | unsigned int hash; | |
269 | ||
270 | hash = fib_info_hashfn(nfi); | |
271 | head = &fib_info_hash[hash]; | |
272 | ||
273 | hlist_for_each_entry(fi, node, head, fib_hash) { | |
09ad9bc7 | 274 | if (!net_eq(fi->fib_net, nfi->fib_net)) |
4814bdbd | 275 | continue; |
1da177e4 LT |
276 | if (fi->fib_nhs != nfi->fib_nhs) |
277 | continue; | |
278 | if (nfi->fib_protocol == fi->fib_protocol && | |
37e826c5 | 279 | nfi->fib_scope == fi->fib_scope && |
1da177e4 LT |
280 | nfi->fib_prefsrc == fi->fib_prefsrc && |
281 | nfi->fib_priority == fi->fib_priority && | |
282 | memcmp(nfi->fib_metrics, fi->fib_metrics, | |
fcd13f42 | 283 | sizeof(u32) * RTAX_MAX) == 0 && |
6a31d2a9 | 284 | ((nfi->fib_flags ^ fi->fib_flags) & ~RTNH_F_DEAD) == 0 && |
1da177e4 LT |
285 | (nfi->fib_nhs == 0 || nh_comp(fi, nfi) == 0)) |
286 | return fi; | |
287 | } | |
288 | ||
289 | return NULL; | |
290 | } | |
291 | ||
1da177e4 | 292 | /* Check, that the gateway is already configured. |
6a31d2a9 | 293 | * Used only by redirect accept routine. |
1da177e4 | 294 | */ |
d878e72e | 295 | int ip_fib_check_default(__be32 gw, struct net_device *dev) |
1da177e4 LT |
296 | { |
297 | struct hlist_head *head; | |
298 | struct hlist_node *node; | |
299 | struct fib_nh *nh; | |
300 | unsigned int hash; | |
301 | ||
832b4c5e | 302 | spin_lock(&fib_info_lock); |
1da177e4 LT |
303 | |
304 | hash = fib_devindex_hashfn(dev->ifindex); | |
305 | head = &fib_info_devhash[hash]; | |
306 | hlist_for_each_entry(nh, node, head, nh_hash) { | |
307 | if (nh->nh_dev == dev && | |
308 | nh->nh_gw == gw && | |
6a31d2a9 | 309 | !(nh->nh_flags & RTNH_F_DEAD)) { |
832b4c5e | 310 | spin_unlock(&fib_info_lock); |
1da177e4 LT |
311 | return 0; |
312 | } | |
313 | } | |
314 | ||
832b4c5e | 315 | spin_unlock(&fib_info_lock); |
1da177e4 LT |
316 | |
317 | return -1; | |
318 | } | |
319 | ||
339bf98f TG |
320 | static inline size_t fib_nlmsg_size(struct fib_info *fi) |
321 | { | |
322 | size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg)) | |
323 | + nla_total_size(4) /* RTA_TABLE */ | |
324 | + nla_total_size(4) /* RTA_DST */ | |
325 | + nla_total_size(4) /* RTA_PRIORITY */ | |
326 | + nla_total_size(4); /* RTA_PREFSRC */ | |
327 | ||
328 | /* space for nested metrics */ | |
329 | payload += nla_total_size((RTAX_MAX * nla_total_size(4))); | |
330 | ||
331 | if (fi->fib_nhs) { | |
332 | /* Also handles the special case fib_nhs == 1 */ | |
333 | ||
334 | /* each nexthop is packed in an attribute */ | |
335 | size_t nhsize = nla_total_size(sizeof(struct rtnexthop)); | |
336 | ||
337 | /* may contain flow and gateway attribute */ | |
338 | nhsize += 2 * nla_total_size(4); | |
339 | ||
340 | /* all nexthops are packed in a nested attribute */ | |
341 | payload += nla_total_size(fi->fib_nhs * nhsize); | |
342 | } | |
343 | ||
344 | return payload; | |
345 | } | |
346 | ||
81f7bf6c | 347 | void rtmsg_fib(int event, __be32 key, struct fib_alias *fa, |
b8f55831 MK |
348 | int dst_len, u32 tb_id, struct nl_info *info, |
349 | unsigned int nlm_flags) | |
1da177e4 LT |
350 | { |
351 | struct sk_buff *skb; | |
4e902c57 | 352 | u32 seq = info->nlh ? info->nlh->nlmsg_seq : 0; |
f21c7bc5 | 353 | int err = -ENOBUFS; |
1da177e4 | 354 | |
339bf98f | 355 | skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL); |
f21c7bc5 TG |
356 | if (skb == NULL) |
357 | goto errout; | |
1da177e4 | 358 | |
4e902c57 | 359 | err = fib_dump_info(skb, info->pid, seq, event, tb_id, |
37e826c5 | 360 | fa->fa_type, key, dst_len, |
b8f55831 | 361 | fa->fa_tos, fa->fa_info, nlm_flags); |
26932566 PM |
362 | if (err < 0) { |
363 | /* -EMSGSIZE implies BUG in fib_nlmsg_size() */ | |
364 | WARN_ON(err == -EMSGSIZE); | |
365 | kfree_skb(skb); | |
366 | goto errout; | |
367 | } | |
1ce85fe4 PNA |
368 | rtnl_notify(skb, info->nl_net, info->pid, RTNLGRP_IPV4_ROUTE, |
369 | info->nlh, GFP_KERNEL); | |
370 | return; | |
f21c7bc5 TG |
371 | errout: |
372 | if (err < 0) | |
4d1169c1 | 373 | rtnl_set_sk_err(info->nl_net, RTNLGRP_IPV4_ROUTE, err); |
1da177e4 LT |
374 | } |
375 | ||
376 | /* Return the first fib alias matching TOS with | |
377 | * priority less than or equal to PRIO. | |
378 | */ | |
379 | struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio) | |
380 | { | |
381 | if (fah) { | |
382 | struct fib_alias *fa; | |
383 | list_for_each_entry(fa, fah, fa_list) { | |
384 | if (fa->fa_tos > tos) | |
385 | continue; | |
386 | if (fa->fa_info->fib_priority >= prio || | |
387 | fa->fa_tos < tos) | |
388 | return fa; | |
389 | } | |
390 | } | |
391 | return NULL; | |
392 | } | |
393 | ||
394 | int fib_detect_death(struct fib_info *fi, int order, | |
c17860a0 | 395 | struct fib_info **last_resort, int *last_idx, int dflt) |
1da177e4 LT |
396 | { |
397 | struct neighbour *n; | |
398 | int state = NUD_NONE; | |
399 | ||
400 | n = neigh_lookup(&arp_tbl, &fi->fib_nh[0].nh_gw, fi->fib_dev); | |
401 | if (n) { | |
402 | state = n->nud_state; | |
403 | neigh_release(n); | |
404 | } | |
d9319100 | 405 | if (state == NUD_REACHABLE) |
1da177e4 | 406 | return 0; |
6a31d2a9 | 407 | if ((state & NUD_VALID) && order != dflt) |
1da177e4 | 408 | return 0; |
6a31d2a9 ED |
409 | if ((state & NUD_VALID) || |
410 | (*last_idx < 0 && order > dflt)) { | |
1da177e4 LT |
411 | *last_resort = fi; |
412 | *last_idx = order; | |
413 | } | |
414 | return 1; | |
415 | } | |
416 | ||
417 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
418 | ||
4e902c57 | 419 | static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining) |
1da177e4 LT |
420 | { |
421 | int nhs = 0; | |
1da177e4 | 422 | |
4e902c57 | 423 | while (rtnh_ok(rtnh, remaining)) { |
1da177e4 | 424 | nhs++; |
4e902c57 TG |
425 | rtnh = rtnh_next(rtnh, &remaining); |
426 | } | |
427 | ||
428 | /* leftover implies invalid nexthop configuration, discard it */ | |
429 | return remaining > 0 ? 0 : nhs; | |
1da177e4 LT |
430 | } |
431 | ||
4e902c57 TG |
432 | static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh, |
433 | int remaining, struct fib_config *cfg) | |
1da177e4 | 434 | { |
1da177e4 | 435 | change_nexthops(fi) { |
4e902c57 TG |
436 | int attrlen; |
437 | ||
438 | if (!rtnh_ok(rtnh, remaining)) | |
1da177e4 | 439 | return -EINVAL; |
4e902c57 | 440 | |
71fceff0 DM |
441 | nexthop_nh->nh_flags = |
442 | (cfg->fc_flags & ~0xFF) | rtnh->rtnh_flags; | |
443 | nexthop_nh->nh_oif = rtnh->rtnh_ifindex; | |
444 | nexthop_nh->nh_weight = rtnh->rtnh_hops + 1; | |
4e902c57 TG |
445 | |
446 | attrlen = rtnh_attrlen(rtnh); | |
447 | if (attrlen > 0) { | |
448 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); | |
449 | ||
450 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); | |
71fceff0 | 451 | nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0; |
c7066f70 | 452 | #ifdef CONFIG_IP_ROUTE_CLASSID |
4e902c57 | 453 | nla = nla_find(attrs, attrlen, RTA_FLOW); |
71fceff0 | 454 | nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0; |
7a9bc9b8 | 455 | if (nexthop_nh->nh_tclassid) |
f4530fa5 | 456 | fi->fib_net->ipv4.fib_num_tclassid_users++; |
1da177e4 LT |
457 | #endif |
458 | } | |
4e902c57 TG |
459 | |
460 | rtnh = rtnh_next(rtnh, &remaining); | |
1da177e4 | 461 | } endfor_nexthops(fi); |
4e902c57 | 462 | |
1da177e4 LT |
463 | return 0; |
464 | } | |
465 | ||
466 | #endif | |
467 | ||
4e902c57 | 468 | int fib_nh_match(struct fib_config *cfg, struct fib_info *fi) |
1da177e4 LT |
469 | { |
470 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
4e902c57 TG |
471 | struct rtnexthop *rtnh; |
472 | int remaining; | |
1da177e4 LT |
473 | #endif |
474 | ||
4e902c57 | 475 | if (cfg->fc_priority && cfg->fc_priority != fi->fib_priority) |
1da177e4 LT |
476 | return 1; |
477 | ||
4e902c57 TG |
478 | if (cfg->fc_oif || cfg->fc_gw) { |
479 | if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) && | |
480 | (!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw)) | |
1da177e4 LT |
481 | return 0; |
482 | return 1; | |
483 | } | |
484 | ||
485 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
4e902c57 | 486 | if (cfg->fc_mp == NULL) |
1da177e4 | 487 | return 0; |
4e902c57 TG |
488 | |
489 | rtnh = cfg->fc_mp; | |
490 | remaining = cfg->fc_mp_len; | |
e905a9ed | 491 | |
1da177e4 | 492 | for_nexthops(fi) { |
4e902c57 | 493 | int attrlen; |
1da177e4 | 494 | |
4e902c57 | 495 | if (!rtnh_ok(rtnh, remaining)) |
1da177e4 | 496 | return -EINVAL; |
4e902c57 TG |
497 | |
498 | if (rtnh->rtnh_ifindex && rtnh->rtnh_ifindex != nh->nh_oif) | |
1da177e4 | 499 | return 1; |
4e902c57 TG |
500 | |
501 | attrlen = rtnh_attrlen(rtnh); | |
502 | if (attrlen < 0) { | |
503 | struct nlattr *nla, *attrs = rtnh_attrs(rtnh); | |
504 | ||
505 | nla = nla_find(attrs, attrlen, RTA_GATEWAY); | |
17fb2c64 | 506 | if (nla && nla_get_be32(nla) != nh->nh_gw) |
1da177e4 | 507 | return 1; |
c7066f70 | 508 | #ifdef CONFIG_IP_ROUTE_CLASSID |
4e902c57 TG |
509 | nla = nla_find(attrs, attrlen, RTA_FLOW); |
510 | if (nla && nla_get_u32(nla) != nh->nh_tclassid) | |
1da177e4 LT |
511 | return 1; |
512 | #endif | |
513 | } | |
4e902c57 TG |
514 | |
515 | rtnh = rtnh_next(rtnh, &remaining); | |
1da177e4 LT |
516 | } endfor_nexthops(fi); |
517 | #endif | |
518 | return 0; | |
519 | } | |
520 | ||
521 | ||
522 | /* | |
6a31d2a9 ED |
523 | * Picture |
524 | * ------- | |
525 | * | |
526 | * Semantics of nexthop is very messy by historical reasons. | |
527 | * We have to take into account, that: | |
528 | * a) gateway can be actually local interface address, | |
529 | * so that gatewayed route is direct. | |
530 | * b) gateway must be on-link address, possibly | |
531 | * described not by an ifaddr, but also by a direct route. | |
532 | * c) If both gateway and interface are specified, they should not | |
533 | * contradict. | |
534 | * d) If we use tunnel routes, gateway could be not on-link. | |
535 | * | |
536 | * Attempt to reconcile all of these (alas, self-contradictory) conditions | |
537 | * results in pretty ugly and hairy code with obscure logic. | |
538 | * | |
539 | * I chose to generalized it instead, so that the size | |
540 | * of code does not increase practically, but it becomes | |
541 | * much more general. | |
542 | * Every prefix is assigned a "scope" value: "host" is local address, | |
543 | * "link" is direct route, | |
544 | * [ ... "site" ... "interior" ... ] | |
545 | * and "universe" is true gateway route with global meaning. | |
546 | * | |
547 | * Every prefix refers to a set of "nexthop"s (gw, oif), | |
548 | * where gw must have narrower scope. This recursion stops | |
549 | * when gw has LOCAL scope or if "nexthop" is declared ONLINK, | |
550 | * which means that gw is forced to be on link. | |
551 | * | |
552 | * Code is still hairy, but now it is apparently logically | |
553 | * consistent and very flexible. F.e. as by-product it allows | |
554 | * to co-exists in peace independent exterior and interior | |
555 | * routing processes. | |
556 | * | |
557 | * Normally it looks as following. | |
558 | * | |
559 | * {universe prefix} -> (gw, oif) [scope link] | |
560 | * | | |
561 | * |-> {link prefix} -> (gw, oif) [scope local] | |
562 | * | | |
563 | * |-> {local prefix} (terminal node) | |
1da177e4 | 564 | */ |
4e902c57 TG |
565 | static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi, |
566 | struct fib_nh *nh) | |
1da177e4 LT |
567 | { |
568 | int err; | |
86167a37 | 569 | struct net *net; |
6a31d2a9 | 570 | struct net_device *dev; |
1da177e4 | 571 | |
86167a37 | 572 | net = cfg->fc_nlinfo.nl_net; |
1da177e4 LT |
573 | if (nh->nh_gw) { |
574 | struct fib_result res; | |
575 | ||
6a31d2a9 | 576 | if (nh->nh_flags & RTNH_F_ONLINK) { |
1da177e4 | 577 | |
4e902c57 | 578 | if (cfg->fc_scope >= RT_SCOPE_LINK) |
1da177e4 | 579 | return -EINVAL; |
86167a37 | 580 | if (inet_addr_type(net, nh->nh_gw) != RTN_UNICAST) |
1da177e4 | 581 | return -EINVAL; |
6a31d2a9 ED |
582 | dev = __dev_get_by_index(net, nh->nh_oif); |
583 | if (!dev) | |
1da177e4 | 584 | return -ENODEV; |
6a31d2a9 | 585 | if (!(dev->flags & IFF_UP)) |
1da177e4 LT |
586 | return -ENETDOWN; |
587 | nh->nh_dev = dev; | |
588 | dev_hold(dev); | |
589 | nh->nh_scope = RT_SCOPE_LINK; | |
590 | return 0; | |
591 | } | |
ebc0ffae | 592 | rcu_read_lock(); |
1da177e4 | 593 | { |
9ade2286 DM |
594 | struct flowi4 fl4 = { |
595 | .daddr = nh->nh_gw, | |
596 | .flowi4_scope = cfg->fc_scope + 1, | |
597 | .flowi4_oif = nh->nh_oif, | |
4e902c57 | 598 | }; |
1da177e4 LT |
599 | |
600 | /* It is not necessary, but requires a bit of thinking */ | |
9ade2286 DM |
601 | if (fl4.flowi4_scope < RT_SCOPE_LINK) |
602 | fl4.flowi4_scope = RT_SCOPE_LINK; | |
603 | err = fib_lookup(net, &fl4, &res); | |
ebc0ffae ED |
604 | if (err) { |
605 | rcu_read_unlock(); | |
1da177e4 | 606 | return err; |
ebc0ffae | 607 | } |
1da177e4 LT |
608 | } |
609 | err = -EINVAL; | |
610 | if (res.type != RTN_UNICAST && res.type != RTN_LOCAL) | |
611 | goto out; | |
612 | nh->nh_scope = res.scope; | |
613 | nh->nh_oif = FIB_RES_OIF(res); | |
6a31d2a9 ED |
614 | nh->nh_dev = dev = FIB_RES_DEV(res); |
615 | if (!dev) | |
1da177e4 | 616 | goto out; |
6a31d2a9 | 617 | dev_hold(dev); |
8723e1b4 | 618 | err = (dev->flags & IFF_UP) ? 0 : -ENETDOWN; |
1da177e4 LT |
619 | } else { |
620 | struct in_device *in_dev; | |
621 | ||
6a31d2a9 | 622 | if (nh->nh_flags & (RTNH_F_PERVASIVE | RTNH_F_ONLINK)) |
1da177e4 LT |
623 | return -EINVAL; |
624 | ||
8723e1b4 ED |
625 | rcu_read_lock(); |
626 | err = -ENODEV; | |
86167a37 | 627 | in_dev = inetdev_by_index(net, nh->nh_oif); |
1da177e4 | 628 | if (in_dev == NULL) |
8723e1b4 ED |
629 | goto out; |
630 | err = -ENETDOWN; | |
631 | if (!(in_dev->dev->flags & IFF_UP)) | |
632 | goto out; | |
1da177e4 LT |
633 | nh->nh_dev = in_dev->dev; |
634 | dev_hold(nh->nh_dev); | |
635 | nh->nh_scope = RT_SCOPE_HOST; | |
8723e1b4 | 636 | err = 0; |
1da177e4 | 637 | } |
8723e1b4 ED |
638 | out: |
639 | rcu_read_unlock(); | |
640 | return err; | |
1da177e4 LT |
641 | } |
642 | ||
81f7bf6c | 643 | static inline unsigned int fib_laddr_hashfn(__be32 val) |
1da177e4 | 644 | { |
123b9731 | 645 | unsigned int mask = (fib_info_hash_size - 1); |
1da177e4 | 646 | |
6a31d2a9 ED |
647 | return ((__force u32)val ^ |
648 | ((__force u32)val >> 7) ^ | |
649 | ((__force u32)val >> 14)) & mask; | |
1da177e4 LT |
650 | } |
651 | ||
123b9731 | 652 | static struct hlist_head *fib_info_hash_alloc(int bytes) |
1da177e4 LT |
653 | { |
654 | if (bytes <= PAGE_SIZE) | |
88f83491 | 655 | return kzalloc(bytes, GFP_KERNEL); |
1da177e4 LT |
656 | else |
657 | return (struct hlist_head *) | |
6a31d2a9 ED |
658 | __get_free_pages(GFP_KERNEL | __GFP_ZERO, |
659 | get_order(bytes)); | |
1da177e4 LT |
660 | } |
661 | ||
123b9731 | 662 | static void fib_info_hash_free(struct hlist_head *hash, int bytes) |
1da177e4 LT |
663 | { |
664 | if (!hash) | |
665 | return; | |
666 | ||
667 | if (bytes <= PAGE_SIZE) | |
668 | kfree(hash); | |
669 | else | |
670 | free_pages((unsigned long) hash, get_order(bytes)); | |
671 | } | |
672 | ||
123b9731 DM |
673 | static void fib_info_hash_move(struct hlist_head *new_info_hash, |
674 | struct hlist_head *new_laddrhash, | |
675 | unsigned int new_size) | |
1da177e4 | 676 | { |
b7656e7f | 677 | struct hlist_head *old_info_hash, *old_laddrhash; |
123b9731 | 678 | unsigned int old_size = fib_info_hash_size; |
b7656e7f | 679 | unsigned int i, bytes; |
1da177e4 | 680 | |
832b4c5e | 681 | spin_lock_bh(&fib_info_lock); |
b7656e7f DM |
682 | old_info_hash = fib_info_hash; |
683 | old_laddrhash = fib_info_laddrhash; | |
123b9731 | 684 | fib_info_hash_size = new_size; |
1da177e4 LT |
685 | |
686 | for (i = 0; i < old_size; i++) { | |
687 | struct hlist_head *head = &fib_info_hash[i]; | |
688 | struct hlist_node *node, *n; | |
689 | struct fib_info *fi; | |
690 | ||
691 | hlist_for_each_entry_safe(fi, node, n, head, fib_hash) { | |
692 | struct hlist_head *dest; | |
693 | unsigned int new_hash; | |
694 | ||
695 | hlist_del(&fi->fib_hash); | |
696 | ||
697 | new_hash = fib_info_hashfn(fi); | |
698 | dest = &new_info_hash[new_hash]; | |
699 | hlist_add_head(&fi->fib_hash, dest); | |
700 | } | |
701 | } | |
702 | fib_info_hash = new_info_hash; | |
703 | ||
704 | for (i = 0; i < old_size; i++) { | |
705 | struct hlist_head *lhead = &fib_info_laddrhash[i]; | |
706 | struct hlist_node *node, *n; | |
707 | struct fib_info *fi; | |
708 | ||
709 | hlist_for_each_entry_safe(fi, node, n, lhead, fib_lhash) { | |
710 | struct hlist_head *ldest; | |
711 | unsigned int new_hash; | |
712 | ||
713 | hlist_del(&fi->fib_lhash); | |
714 | ||
715 | new_hash = fib_laddr_hashfn(fi->fib_prefsrc); | |
716 | ldest = &new_laddrhash[new_hash]; | |
717 | hlist_add_head(&fi->fib_lhash, ldest); | |
718 | } | |
719 | } | |
720 | fib_info_laddrhash = new_laddrhash; | |
721 | ||
832b4c5e | 722 | spin_unlock_bh(&fib_info_lock); |
b7656e7f DM |
723 | |
724 | bytes = old_size * sizeof(struct hlist_head *); | |
123b9731 DM |
725 | fib_info_hash_free(old_info_hash, bytes); |
726 | fib_info_hash_free(old_laddrhash, bytes); | |
1da177e4 LT |
727 | } |
728 | ||
436c3b66 DM |
729 | __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh) |
730 | { | |
731 | nh->nh_saddr = inet_select_addr(nh->nh_dev, | |
732 | nh->nh_gw, | |
37e826c5 | 733 | nh->nh_parent->fib_scope); |
436c3b66 DM |
734 | nh->nh_saddr_genid = atomic_read(&net->ipv4.dev_addr_genid); |
735 | ||
736 | return nh->nh_saddr; | |
737 | } | |
738 | ||
4e902c57 | 739 | struct fib_info *fib_create_info(struct fib_config *cfg) |
1da177e4 LT |
740 | { |
741 | int err; | |
742 | struct fib_info *fi = NULL; | |
743 | struct fib_info *ofi; | |
1da177e4 | 744 | int nhs = 1; |
7462bd74 | 745 | struct net *net = cfg->fc_nlinfo.nl_net; |
1da177e4 | 746 | |
4c8237cd DM |
747 | if (cfg->fc_type > RTN_MAX) |
748 | goto err_inval; | |
749 | ||
1da177e4 | 750 | /* Fast check to catch the most weird cases */ |
4e902c57 | 751 | if (fib_props[cfg->fc_type].scope > cfg->fc_scope) |
1da177e4 LT |
752 | goto err_inval; |
753 | ||
754 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
4e902c57 TG |
755 | if (cfg->fc_mp) { |
756 | nhs = fib_count_nexthops(cfg->fc_mp, cfg->fc_mp_len); | |
1da177e4 LT |
757 | if (nhs == 0) |
758 | goto err_inval; | |
759 | } | |
760 | #endif | |
1da177e4 LT |
761 | |
762 | err = -ENOBUFS; | |
123b9731 DM |
763 | if (fib_info_cnt >= fib_info_hash_size) { |
764 | unsigned int new_size = fib_info_hash_size << 1; | |
1da177e4 LT |
765 | struct hlist_head *new_info_hash; |
766 | struct hlist_head *new_laddrhash; | |
767 | unsigned int bytes; | |
768 | ||
769 | if (!new_size) | |
770 | new_size = 1; | |
771 | bytes = new_size * sizeof(struct hlist_head *); | |
123b9731 DM |
772 | new_info_hash = fib_info_hash_alloc(bytes); |
773 | new_laddrhash = fib_info_hash_alloc(bytes); | |
1da177e4 | 774 | if (!new_info_hash || !new_laddrhash) { |
123b9731 DM |
775 | fib_info_hash_free(new_info_hash, bytes); |
776 | fib_info_hash_free(new_laddrhash, bytes); | |
88f83491 | 777 | } else |
123b9731 | 778 | fib_info_hash_move(new_info_hash, new_laddrhash, new_size); |
1da177e4 | 779 | |
123b9731 | 780 | if (!fib_info_hash_size) |
1da177e4 LT |
781 | goto failure; |
782 | } | |
783 | ||
0da974f4 | 784 | fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL); |
1da177e4 LT |
785 | if (fi == NULL) |
786 | goto failure; | |
725d1e1b DM |
787 | if (cfg->fc_mx) { |
788 | fi->fib_metrics = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); | |
789 | if (!fi->fib_metrics) | |
790 | goto failure; | |
791 | } else | |
792 | fi->fib_metrics = (u32 *) dst_default_metrics; | |
1da177e4 | 793 | fib_info_cnt++; |
1da177e4 | 794 | |
57d7a600 | 795 | fi->fib_net = hold_net(net); |
4e902c57 | 796 | fi->fib_protocol = cfg->fc_protocol; |
37e826c5 | 797 | fi->fib_scope = cfg->fc_scope; |
4e902c57 TG |
798 | fi->fib_flags = cfg->fc_flags; |
799 | fi->fib_priority = cfg->fc_priority; | |
800 | fi->fib_prefsrc = cfg->fc_prefsrc; | |
1da177e4 LT |
801 | |
802 | fi->fib_nhs = nhs; | |
803 | change_nexthops(fi) { | |
71fceff0 | 804 | nexthop_nh->nh_parent = fi; |
1da177e4 LT |
805 | } endfor_nexthops(fi) |
806 | ||
4e902c57 TG |
807 | if (cfg->fc_mx) { |
808 | struct nlattr *nla; | |
809 | int remaining; | |
810 | ||
811 | nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { | |
8f4c1f9b | 812 | int type = nla_type(nla); |
4e902c57 TG |
813 | |
814 | if (type) { | |
6fac2625 DM |
815 | u32 val; |
816 | ||
4e902c57 | 817 | if (type > RTAX_MAX) |
1da177e4 | 818 | goto err_inval; |
6fac2625 DM |
819 | val = nla_get_u32(nla); |
820 | if (type == RTAX_ADVMSS && val > 65535 - 40) | |
821 | val = 65535 - 40; | |
710ab6c0 DM |
822 | if (type == RTAX_MTU && val > 65535 - 15) |
823 | val = 65535 - 15; | |
6fac2625 | 824 | fi->fib_metrics[type - 1] = val; |
1da177e4 | 825 | } |
1da177e4 LT |
826 | } |
827 | } | |
1da177e4 | 828 | |
4e902c57 | 829 | if (cfg->fc_mp) { |
1da177e4 | 830 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
4e902c57 TG |
831 | err = fib_get_nhs(fi, cfg->fc_mp, cfg->fc_mp_len, cfg); |
832 | if (err != 0) | |
1da177e4 | 833 | goto failure; |
4e902c57 | 834 | if (cfg->fc_oif && fi->fib_nh->nh_oif != cfg->fc_oif) |
1da177e4 | 835 | goto err_inval; |
4e902c57 | 836 | if (cfg->fc_gw && fi->fib_nh->nh_gw != cfg->fc_gw) |
1da177e4 | 837 | goto err_inval; |
c7066f70 | 838 | #ifdef CONFIG_IP_ROUTE_CLASSID |
4e902c57 | 839 | if (cfg->fc_flow && fi->fib_nh->nh_tclassid != cfg->fc_flow) |
1da177e4 LT |
840 | goto err_inval; |
841 | #endif | |
842 | #else | |
843 | goto err_inval; | |
844 | #endif | |
845 | } else { | |
846 | struct fib_nh *nh = fi->fib_nh; | |
4e902c57 TG |
847 | |
848 | nh->nh_oif = cfg->fc_oif; | |
849 | nh->nh_gw = cfg->fc_gw; | |
850 | nh->nh_flags = cfg->fc_flags; | |
c7066f70 | 851 | #ifdef CONFIG_IP_ROUTE_CLASSID |
4e902c57 | 852 | nh->nh_tclassid = cfg->fc_flow; |
7a9bc9b8 | 853 | if (nh->nh_tclassid) |
f4530fa5 | 854 | fi->fib_net->ipv4.fib_num_tclassid_users++; |
1da177e4 | 855 | #endif |
1da177e4 LT |
856 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
857 | nh->nh_weight = 1; | |
858 | #endif | |
859 | } | |
860 | ||
4e902c57 TG |
861 | if (fib_props[cfg->fc_type].error) { |
862 | if (cfg->fc_gw || cfg->fc_oif || cfg->fc_mp) | |
1da177e4 LT |
863 | goto err_inval; |
864 | goto link_it; | |
4c8237cd DM |
865 | } else { |
866 | switch (cfg->fc_type) { | |
867 | case RTN_UNICAST: | |
868 | case RTN_LOCAL: | |
869 | case RTN_BROADCAST: | |
870 | case RTN_ANYCAST: | |
871 | case RTN_MULTICAST: | |
872 | break; | |
873 | default: | |
874 | goto err_inval; | |
875 | } | |
1da177e4 LT |
876 | } |
877 | ||
4e902c57 | 878 | if (cfg->fc_scope > RT_SCOPE_HOST) |
1da177e4 LT |
879 | goto err_inval; |
880 | ||
4e902c57 | 881 | if (cfg->fc_scope == RT_SCOPE_HOST) { |
1da177e4 LT |
882 | struct fib_nh *nh = fi->fib_nh; |
883 | ||
884 | /* Local address is added. */ | |
885 | if (nhs != 1 || nh->nh_gw) | |
886 | goto err_inval; | |
887 | nh->nh_scope = RT_SCOPE_NOWHERE; | |
7462bd74 | 888 | nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif); |
1da177e4 LT |
889 | err = -ENODEV; |
890 | if (nh->nh_dev == NULL) | |
891 | goto failure; | |
892 | } else { | |
893 | change_nexthops(fi) { | |
6a31d2a9 ED |
894 | err = fib_check_nh(cfg, fi, nexthop_nh); |
895 | if (err != 0) | |
1da177e4 LT |
896 | goto failure; |
897 | } endfor_nexthops(fi) | |
898 | } | |
899 | ||
900 | if (fi->fib_prefsrc) { | |
4e902c57 TG |
901 | if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst || |
902 | fi->fib_prefsrc != cfg->fc_dst) | |
7462bd74 | 903 | if (inet_addr_type(net, fi->fib_prefsrc) != RTN_LOCAL) |
1da177e4 LT |
904 | goto err_inval; |
905 | } | |
906 | ||
1fc050a1 | 907 | change_nexthops(fi) { |
436c3b66 | 908 | fib_info_update_nh_saddr(net, nexthop_nh); |
1fc050a1 DM |
909 | } endfor_nexthops(fi) |
910 | ||
1da177e4 | 911 | link_it: |
6a31d2a9 ED |
912 | ofi = fib_find_info(fi); |
913 | if (ofi) { | |
1da177e4 LT |
914 | fi->fib_dead = 1; |
915 | free_fib_info(fi); | |
916 | ofi->fib_treeref++; | |
917 | return ofi; | |
918 | } | |
919 | ||
920 | fi->fib_treeref++; | |
921 | atomic_inc(&fi->fib_clntref); | |
832b4c5e | 922 | spin_lock_bh(&fib_info_lock); |
1da177e4 LT |
923 | hlist_add_head(&fi->fib_hash, |
924 | &fib_info_hash[fib_info_hashfn(fi)]); | |
925 | if (fi->fib_prefsrc) { | |
926 | struct hlist_head *head; | |
927 | ||
928 | head = &fib_info_laddrhash[fib_laddr_hashfn(fi->fib_prefsrc)]; | |
929 | hlist_add_head(&fi->fib_lhash, head); | |
930 | } | |
931 | change_nexthops(fi) { | |
932 | struct hlist_head *head; | |
933 | unsigned int hash; | |
934 | ||
71fceff0 | 935 | if (!nexthop_nh->nh_dev) |
1da177e4 | 936 | continue; |
71fceff0 | 937 | hash = fib_devindex_hashfn(nexthop_nh->nh_dev->ifindex); |
1da177e4 | 938 | head = &fib_info_devhash[hash]; |
71fceff0 | 939 | hlist_add_head(&nexthop_nh->nh_hash, head); |
1da177e4 | 940 | } endfor_nexthops(fi) |
832b4c5e | 941 | spin_unlock_bh(&fib_info_lock); |
1da177e4 LT |
942 | return fi; |
943 | ||
944 | err_inval: | |
945 | err = -EINVAL; | |
946 | ||
947 | failure: | |
e905a9ed | 948 | if (fi) { |
1da177e4 LT |
949 | fi->fib_dead = 1; |
950 | free_fib_info(fi); | |
951 | } | |
4e902c57 TG |
952 | |
953 | return ERR_PTR(err); | |
1da177e4 LT |
954 | } |
955 | ||
be403ea1 | 956 | int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event, |
37e826c5 | 957 | u32 tb_id, u8 type, __be32 dst, int dst_len, u8 tos, |
be403ea1 | 958 | struct fib_info *fi, unsigned int flags) |
1da177e4 | 959 | { |
be403ea1 | 960 | struct nlmsghdr *nlh; |
1da177e4 | 961 | struct rtmsg *rtm; |
1da177e4 | 962 | |
be403ea1 TG |
963 | nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), flags); |
964 | if (nlh == NULL) | |
26932566 | 965 | return -EMSGSIZE; |
be403ea1 TG |
966 | |
967 | rtm = nlmsg_data(nlh); | |
1da177e4 LT |
968 | rtm->rtm_family = AF_INET; |
969 | rtm->rtm_dst_len = dst_len; | |
970 | rtm->rtm_src_len = 0; | |
971 | rtm->rtm_tos = tos; | |
709772e6 KPO |
972 | if (tb_id < 256) |
973 | rtm->rtm_table = tb_id; | |
974 | else | |
975 | rtm->rtm_table = RT_TABLE_COMPAT; | |
f3756b79 DM |
976 | if (nla_put_u32(skb, RTA_TABLE, tb_id)) |
977 | goto nla_put_failure; | |
1da177e4 LT |
978 | rtm->rtm_type = type; |
979 | rtm->rtm_flags = fi->fib_flags; | |
37e826c5 | 980 | rtm->rtm_scope = fi->fib_scope; |
1da177e4 | 981 | rtm->rtm_protocol = fi->fib_protocol; |
be403ea1 | 982 | |
f3756b79 DM |
983 | if (rtm->rtm_dst_len && |
984 | nla_put_be32(skb, RTA_DST, dst)) | |
985 | goto nla_put_failure; | |
986 | if (fi->fib_priority && | |
987 | nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority)) | |
988 | goto nla_put_failure; | |
1da177e4 | 989 | if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0) |
be403ea1 TG |
990 | goto nla_put_failure; |
991 | ||
f3756b79 DM |
992 | if (fi->fib_prefsrc && |
993 | nla_put_be32(skb, RTA_PREFSRC, fi->fib_prefsrc)) | |
994 | goto nla_put_failure; | |
1da177e4 | 995 | if (fi->fib_nhs == 1) { |
f3756b79 DM |
996 | if (fi->fib_nh->nh_gw && |
997 | nla_put_be32(skb, RTA_GATEWAY, fi->fib_nh->nh_gw)) | |
998 | goto nla_put_failure; | |
999 | if (fi->fib_nh->nh_oif && | |
1000 | nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif)) | |
1001 | goto nla_put_failure; | |
c7066f70 | 1002 | #ifdef CONFIG_IP_ROUTE_CLASSID |
f3756b79 DM |
1003 | if (fi->fib_nh[0].nh_tclassid && |
1004 | nla_put_u32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid)) | |
1005 | goto nla_put_failure; | |
8265abc0 | 1006 | #endif |
1da177e4 LT |
1007 | } |
1008 | #ifdef CONFIG_IP_ROUTE_MULTIPATH | |
1009 | if (fi->fib_nhs > 1) { | |
be403ea1 TG |
1010 | struct rtnexthop *rtnh; |
1011 | struct nlattr *mp; | |
1012 | ||
1013 | mp = nla_nest_start(skb, RTA_MULTIPATH); | |
1014 | if (mp == NULL) | |
1015 | goto nla_put_failure; | |
1da177e4 LT |
1016 | |
1017 | for_nexthops(fi) { | |
be403ea1 TG |
1018 | rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); |
1019 | if (rtnh == NULL) | |
1020 | goto nla_put_failure; | |
1021 | ||
1022 | rtnh->rtnh_flags = nh->nh_flags & 0xFF; | |
1023 | rtnh->rtnh_hops = nh->nh_weight - 1; | |
1024 | rtnh->rtnh_ifindex = nh->nh_oif; | |
1025 | ||
f3756b79 DM |
1026 | if (nh->nh_gw && |
1027 | nla_put_be32(skb, RTA_GATEWAY, nh->nh_gw)) | |
1028 | goto nla_put_failure; | |
c7066f70 | 1029 | #ifdef CONFIG_IP_ROUTE_CLASSID |
f3756b79 DM |
1030 | if (nh->nh_tclassid && |
1031 | nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid)) | |
1032 | goto nla_put_failure; | |
8265abc0 | 1033 | #endif |
be403ea1 TG |
1034 | /* length of rtnetlink header + attributes */ |
1035 | rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh; | |
1da177e4 | 1036 | } endfor_nexthops(fi); |
be403ea1 TG |
1037 | |
1038 | nla_nest_end(skb, mp); | |
1da177e4 LT |
1039 | } |
1040 | #endif | |
be403ea1 | 1041 | return nlmsg_end(skb, nlh); |
1da177e4 | 1042 | |
be403ea1 | 1043 | nla_put_failure: |
26932566 PM |
1044 | nlmsg_cancel(skb, nlh); |
1045 | return -EMSGSIZE; | |
1da177e4 LT |
1046 | } |
1047 | ||
1da177e4 | 1048 | /* |
6a31d2a9 ED |
1049 | * Update FIB if: |
1050 | * - local address disappeared -> we must delete all the entries | |
1051 | * referring to it. | |
1052 | * - device went down -> we must shutdown all nexthops going via it. | |
1da177e4 | 1053 | */ |
4814bdbd | 1054 | int fib_sync_down_addr(struct net *net, __be32 local) |
1da177e4 LT |
1055 | { |
1056 | int ret = 0; | |
85326fa5 DL |
1057 | unsigned int hash = fib_laddr_hashfn(local); |
1058 | struct hlist_head *head = &fib_info_laddrhash[hash]; | |
1059 | struct hlist_node *node; | |
1060 | struct fib_info *fi; | |
1da177e4 | 1061 | |
85326fa5 DL |
1062 | if (fib_info_laddrhash == NULL || local == 0) |
1063 | return 0; | |
1da177e4 | 1064 | |
85326fa5 | 1065 | hlist_for_each_entry(fi, node, head, fib_lhash) { |
09ad9bc7 | 1066 | if (!net_eq(fi->fib_net, net)) |
4814bdbd | 1067 | continue; |
85326fa5 DL |
1068 | if (fi->fib_prefsrc == local) { |
1069 | fi->fib_flags |= RTNH_F_DEAD; | |
1070 | ret++; | |
1da177e4 LT |
1071 | } |
1072 | } | |
85326fa5 DL |
1073 | return ret; |
1074 | } | |
1075 | ||
1076 | int fib_sync_down_dev(struct net_device *dev, int force) | |
1077 | { | |
1078 | int ret = 0; | |
1079 | int scope = RT_SCOPE_NOWHERE; | |
1080 | struct fib_info *prev_fi = NULL; | |
1081 | unsigned int hash = fib_devindex_hashfn(dev->ifindex); | |
1082 | struct hlist_head *head = &fib_info_devhash[hash]; | |
1083 | struct hlist_node *node; | |
1084 | struct fib_nh *nh; | |
1da177e4 | 1085 | |
85326fa5 DL |
1086 | if (force) |
1087 | scope = -1; | |
1da177e4 | 1088 | |
85326fa5 DL |
1089 | hlist_for_each_entry(nh, node, head, nh_hash) { |
1090 | struct fib_info *fi = nh->nh_parent; | |
1091 | int dead; | |
1da177e4 | 1092 | |
85326fa5 DL |
1093 | BUG_ON(!fi->fib_nhs); |
1094 | if (nh->nh_dev != dev || fi == prev_fi) | |
1095 | continue; | |
1096 | prev_fi = fi; | |
1097 | dead = 0; | |
1098 | change_nexthops(fi) { | |
6a31d2a9 | 1099 | if (nexthop_nh->nh_flags & RTNH_F_DEAD) |
85326fa5 | 1100 | dead++; |
71fceff0 DM |
1101 | else if (nexthop_nh->nh_dev == dev && |
1102 | nexthop_nh->nh_scope != scope) { | |
1103 | nexthop_nh->nh_flags |= RTNH_F_DEAD; | |
1da177e4 | 1104 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
85326fa5 | 1105 | spin_lock_bh(&fib_multipath_lock); |
71fceff0 DM |
1106 | fi->fib_power -= nexthop_nh->nh_power; |
1107 | nexthop_nh->nh_power = 0; | |
85326fa5 | 1108 | spin_unlock_bh(&fib_multipath_lock); |
1da177e4 | 1109 | #endif |
85326fa5 DL |
1110 | dead++; |
1111 | } | |
1da177e4 | 1112 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
71fceff0 | 1113 | if (force > 1 && nexthop_nh->nh_dev == dev) { |
85326fa5 DL |
1114 | dead = fi->fib_nhs; |
1115 | break; | |
1da177e4 | 1116 | } |
85326fa5 DL |
1117 | #endif |
1118 | } endfor_nexthops(fi) | |
1119 | if (dead == fi->fib_nhs) { | |
1120 | fi->fib_flags |= RTNH_F_DEAD; | |
1121 | ret++; | |
1da177e4 LT |
1122 | } |
1123 | } | |
1124 | ||
1125 | return ret; | |
1126 | } | |
1127 | ||
0c838ff1 DM |
1128 | /* Must be invoked inside of an RCU protected region. */ |
1129 | void fib_select_default(struct fib_result *res) | |
1130 | { | |
1131 | struct fib_info *fi = NULL, *last_resort = NULL; | |
1132 | struct list_head *fa_head = res->fa_head; | |
1133 | struct fib_table *tb = res->table; | |
1134 | int order = -1, last_idx = -1; | |
1135 | struct fib_alias *fa; | |
1136 | ||
1137 | list_for_each_entry_rcu(fa, fa_head, fa_list) { | |
1138 | struct fib_info *next_fi = fa->fa_info; | |
1139 | ||
37e826c5 | 1140 | if (next_fi->fib_scope != res->scope || |
0c838ff1 DM |
1141 | fa->fa_type != RTN_UNICAST) |
1142 | continue; | |
1143 | ||
1144 | if (next_fi->fib_priority > res->fi->fib_priority) | |
1145 | break; | |
1146 | if (!next_fi->fib_nh[0].nh_gw || | |
1147 | next_fi->fib_nh[0].nh_scope != RT_SCOPE_LINK) | |
1148 | continue; | |
1149 | ||
1150 | fib_alias_accessed(fa); | |
1151 | ||
1152 | if (fi == NULL) { | |
1153 | if (next_fi != res->fi) | |
1154 | break; | |
1155 | } else if (!fib_detect_death(fi, order, &last_resort, | |
1156 | &last_idx, tb->tb_default)) { | |
1157 | fib_result_assign(res, fi); | |
1158 | tb->tb_default = order; | |
1159 | goto out; | |
1160 | } | |
1161 | fi = next_fi; | |
1162 | order++; | |
1163 | } | |
1164 | ||
1165 | if (order <= 0 || fi == NULL) { | |
1166 | tb->tb_default = -1; | |
1167 | goto out; | |
1168 | } | |
1169 | ||
1170 | if (!fib_detect_death(fi, order, &last_resort, &last_idx, | |
1171 | tb->tb_default)) { | |
1172 | fib_result_assign(res, fi); | |
1173 | tb->tb_default = order; | |
1174 | goto out; | |
1175 | } | |
1176 | ||
1177 | if (last_idx >= 0) | |
1178 | fib_result_assign(res, last_resort); | |
1179 | tb->tb_default = last_idx; | |
1180 | out: | |
31d40937 | 1181 | return; |
0c838ff1 DM |
1182 | } |
1183 | ||
1da177e4 LT |
1184 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
1185 | ||
1186 | /* | |
6a31d2a9 ED |
1187 | * Dead device goes up. We wake up dead nexthops. |
1188 | * It takes sense only on multipath routes. | |
1da177e4 | 1189 | */ |
1da177e4 LT |
1190 | int fib_sync_up(struct net_device *dev) |
1191 | { | |
1192 | struct fib_info *prev_fi; | |
1193 | unsigned int hash; | |
1194 | struct hlist_head *head; | |
1195 | struct hlist_node *node; | |
1196 | struct fib_nh *nh; | |
1197 | int ret; | |
1198 | ||
6a31d2a9 | 1199 | if (!(dev->flags & IFF_UP)) |
1da177e4 LT |
1200 | return 0; |
1201 | ||
1202 | prev_fi = NULL; | |
1203 | hash = fib_devindex_hashfn(dev->ifindex); | |
1204 | head = &fib_info_devhash[hash]; | |
1205 | ret = 0; | |
1206 | ||
1207 | hlist_for_each_entry(nh, node, head, nh_hash) { | |
1208 | struct fib_info *fi = nh->nh_parent; | |
1209 | int alive; | |
1210 | ||
1211 | BUG_ON(!fi->fib_nhs); | |
1212 | if (nh->nh_dev != dev || fi == prev_fi) | |
1213 | continue; | |
1214 | ||
1215 | prev_fi = fi; | |
1216 | alive = 0; | |
1217 | change_nexthops(fi) { | |
6a31d2a9 | 1218 | if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) { |
1da177e4 LT |
1219 | alive++; |
1220 | continue; | |
1221 | } | |
71fceff0 | 1222 | if (nexthop_nh->nh_dev == NULL || |
6a31d2a9 | 1223 | !(nexthop_nh->nh_dev->flags & IFF_UP)) |
1da177e4 | 1224 | continue; |
71fceff0 DM |
1225 | if (nexthop_nh->nh_dev != dev || |
1226 | !__in_dev_get_rtnl(dev)) | |
1da177e4 LT |
1227 | continue; |
1228 | alive++; | |
1229 | spin_lock_bh(&fib_multipath_lock); | |
71fceff0 DM |
1230 | nexthop_nh->nh_power = 0; |
1231 | nexthop_nh->nh_flags &= ~RTNH_F_DEAD; | |
1da177e4 LT |
1232 | spin_unlock_bh(&fib_multipath_lock); |
1233 | } endfor_nexthops(fi) | |
1234 | ||
1235 | if (alive > 0) { | |
1236 | fi->fib_flags &= ~RTNH_F_DEAD; | |
1237 | ret++; | |
1238 | } | |
1239 | } | |
1240 | ||
1241 | return ret; | |
1242 | } | |
1243 | ||
1244 | /* | |
6a31d2a9 ED |
1245 | * The algorithm is suboptimal, but it provides really |
1246 | * fair weighted route distribution. | |
1da177e4 | 1247 | */ |
1b7fe593 | 1248 | void fib_select_multipath(struct fib_result *res) |
1da177e4 LT |
1249 | { |
1250 | struct fib_info *fi = res->fi; | |
1251 | int w; | |
1252 | ||
1253 | spin_lock_bh(&fib_multipath_lock); | |
1254 | if (fi->fib_power <= 0) { | |
1255 | int power = 0; | |
1256 | change_nexthops(fi) { | |
6a31d2a9 | 1257 | if (!(nexthop_nh->nh_flags & RTNH_F_DEAD)) { |
71fceff0 DM |
1258 | power += nexthop_nh->nh_weight; |
1259 | nexthop_nh->nh_power = nexthop_nh->nh_weight; | |
1da177e4 LT |
1260 | } |
1261 | } endfor_nexthops(fi); | |
1262 | fi->fib_power = power; | |
1263 | if (power <= 0) { | |
1264 | spin_unlock_bh(&fib_multipath_lock); | |
1265 | /* Race condition: route has just become dead. */ | |
1266 | res->nh_sel = 0; | |
1267 | return; | |
1268 | } | |
1269 | } | |
1270 | ||
1271 | ||
1272 | /* w should be random number [0..fi->fib_power-1], | |
6a31d2a9 | 1273 | * it is pretty bad approximation. |
1da177e4 LT |
1274 | */ |
1275 | ||
1276 | w = jiffies % fi->fib_power; | |
1277 | ||
1278 | change_nexthops(fi) { | |
6a31d2a9 | 1279 | if (!(nexthop_nh->nh_flags & RTNH_F_DEAD) && |
71fceff0 | 1280 | nexthop_nh->nh_power) { |
6a31d2a9 ED |
1281 | w -= nexthop_nh->nh_power; |
1282 | if (w <= 0) { | |
71fceff0 | 1283 | nexthop_nh->nh_power--; |
1da177e4 LT |
1284 | fi->fib_power--; |
1285 | res->nh_sel = nhsel; | |
1286 | spin_unlock_bh(&fib_multipath_lock); | |
1287 | return; | |
1288 | } | |
1289 | } | |
1290 | } endfor_nexthops(fi); | |
1291 | ||
1292 | /* Race condition: route has just become dead. */ | |
1293 | res->nh_sel = 0; | |
1294 | spin_unlock_bh(&fib_multipath_lock); | |
1295 | } | |
1296 | #endif |