Root/
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: FIB frontend. |
7 | * |
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 | |
16 | #include <linux/module.h> |
17 | #include <asm/uaccess.h> |
18 | #include <asm/system.h> |
19 | #include <linux/bitops.h> |
20 | #include <linux/capability.h> |
21 | #include <linux/types.h> |
22 | #include <linux/kernel.h> |
23 | #include <linux/mm.h> |
24 | #include <linux/string.h> |
25 | #include <linux/socket.h> |
26 | #include <linux/sockios.h> |
27 | #include <linux/errno.h> |
28 | #include <linux/in.h> |
29 | #include <linux/inet.h> |
30 | #include <linux/inetdevice.h> |
31 | #include <linux/netdevice.h> |
32 | #include <linux/if_addr.h> |
33 | #include <linux/if_arp.h> |
34 | #include <linux/skbuff.h> |
35 | #include <linux/init.h> |
36 | #include <linux/list.h> |
37 | |
38 | #include <net/ip.h> |
39 | #include <net/protocol.h> |
40 | #include <net/route.h> |
41 | #include <net/tcp.h> |
42 | #include <net/sock.h> |
43 | #include <net/arp.h> |
44 | #include <net/ip_fib.h> |
45 | #include <net/rtnetlink.h> |
46 | |
47 | #ifndef CONFIG_IP_MULTIPLE_TABLES |
48 | |
49 | static int __net_init fib4_rules_init(struct net *net) |
50 | { |
51 | struct fib_table *local_table, *main_table; |
52 | |
53 | local_table = fib_hash_table(RT_TABLE_LOCAL); |
54 | if (local_table == NULL) |
55 | return -ENOMEM; |
56 | |
57 | main_table = fib_hash_table(RT_TABLE_MAIN); |
58 | if (main_table == NULL) |
59 | goto fail; |
60 | |
61 | hlist_add_head_rcu(&local_table->tb_hlist, |
62 | &net->ipv4.fib_table_hash[TABLE_LOCAL_INDEX]); |
63 | hlist_add_head_rcu(&main_table->tb_hlist, |
64 | &net->ipv4.fib_table_hash[TABLE_MAIN_INDEX]); |
65 | return 0; |
66 | |
67 | fail: |
68 | kfree(local_table); |
69 | return -ENOMEM; |
70 | } |
71 | #else |
72 | |
73 | struct fib_table *fib_new_table(struct net *net, u32 id) |
74 | { |
75 | struct fib_table *tb; |
76 | unsigned int h; |
77 | |
78 | if (id == 0) |
79 | id = RT_TABLE_MAIN; |
80 | tb = fib_get_table(net, id); |
81 | if (tb) |
82 | return tb; |
83 | |
84 | tb = fib_hash_table(id); |
85 | if (!tb) |
86 | return NULL; |
87 | h = id & (FIB_TABLE_HASHSZ - 1); |
88 | hlist_add_head_rcu(&tb->tb_hlist, &net->ipv4.fib_table_hash[h]); |
89 | return tb; |
90 | } |
91 | |
92 | struct fib_table *fib_get_table(struct net *net, u32 id) |
93 | { |
94 | struct fib_table *tb; |
95 | struct hlist_node *node; |
96 | struct hlist_head *head; |
97 | unsigned int h; |
98 | |
99 | if (id == 0) |
100 | id = RT_TABLE_MAIN; |
101 | h = id & (FIB_TABLE_HASHSZ - 1); |
102 | |
103 | rcu_read_lock(); |
104 | head = &net->ipv4.fib_table_hash[h]; |
105 | hlist_for_each_entry_rcu(tb, node, head, tb_hlist) { |
106 | if (tb->tb_id == id) { |
107 | rcu_read_unlock(); |
108 | return tb; |
109 | } |
110 | } |
111 | rcu_read_unlock(); |
112 | return NULL; |
113 | } |
114 | #endif /* CONFIG_IP_MULTIPLE_TABLES */ |
115 | |
116 | void fib_select_default(struct net *net, |
117 | const struct flowi *flp, struct fib_result *res) |
118 | { |
119 | struct fib_table *tb; |
120 | int table = RT_TABLE_MAIN; |
121 | #ifdef CONFIG_IP_MULTIPLE_TABLES |
122 | if (res->r == NULL || res->r->action != FR_ACT_TO_TBL) |
123 | return; |
124 | table = res->r->table; |
125 | #endif |
126 | tb = fib_get_table(net, table); |
127 | if (FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) |
128 | tb->tb_select_default(tb, flp, res); |
129 | } |
130 | |
131 | static void fib_flush(struct net *net) |
132 | { |
133 | int flushed = 0; |
134 | struct fib_table *tb; |
135 | struct hlist_node *node; |
136 | struct hlist_head *head; |
137 | unsigned int h; |
138 | |
139 | for (h = 0; h < FIB_TABLE_HASHSZ; h++) { |
140 | head = &net->ipv4.fib_table_hash[h]; |
141 | hlist_for_each_entry(tb, node, head, tb_hlist) |
142 | flushed += tb->tb_flush(tb); |
143 | } |
144 | |
145 | if (flushed) |
146 | rt_cache_flush(net, -1); |
147 | } |
148 | |
149 | /* |
150 | * Find the first device with a given source address. |
151 | */ |
152 | |
153 | struct net_device * ip_dev_find(struct net *net, __be32 addr) |
154 | { |
155 | struct flowi fl = { .nl_u = { .ip4_u = { .daddr = addr } } }; |
156 | struct fib_result res; |
157 | struct net_device *dev = NULL; |
158 | struct fib_table *local_table; |
159 | |
160 | #ifdef CONFIG_IP_MULTIPLE_TABLES |
161 | res.r = NULL; |
162 | #endif |
163 | |
164 | local_table = fib_get_table(net, RT_TABLE_LOCAL); |
165 | if (!local_table || local_table->tb_lookup(local_table, &fl, &res)) |
166 | return NULL; |
167 | if (res.type != RTN_LOCAL) |
168 | goto out; |
169 | dev = FIB_RES_DEV(res); |
170 | |
171 | if (dev) |
172 | dev_hold(dev); |
173 | out: |
174 | fib_res_put(&res); |
175 | return dev; |
176 | } |
177 | |
178 | /* |
179 | * Find address type as if only "dev" was present in the system. If |
180 | * on_dev is NULL then all interfaces are taken into consideration. |
181 | */ |
182 | static inline unsigned __inet_dev_addr_type(struct net *net, |
183 | const struct net_device *dev, |
184 | __be32 addr) |
185 | { |
186 | struct flowi fl = { .nl_u = { .ip4_u = { .daddr = addr } } }; |
187 | struct fib_result res; |
188 | unsigned ret = RTN_BROADCAST; |
189 | struct fib_table *local_table; |
190 | |
191 | if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr)) |
192 | return RTN_BROADCAST; |
193 | if (ipv4_is_multicast(addr)) |
194 | return RTN_MULTICAST; |
195 | |
196 | #ifdef CONFIG_IP_MULTIPLE_TABLES |
197 | res.r = NULL; |
198 | #endif |
199 | |
200 | local_table = fib_get_table(net, RT_TABLE_LOCAL); |
201 | if (local_table) { |
202 | ret = RTN_UNICAST; |
203 | if (!local_table->tb_lookup(local_table, &fl, &res)) { |
204 | if (!dev || dev == res.fi->fib_dev) |
205 | ret = res.type; |
206 | fib_res_put(&res); |
207 | } |
208 | } |
209 | return ret; |
210 | } |
211 | |
212 | unsigned int inet_addr_type(struct net *net, __be32 addr) |
213 | { |
214 | return __inet_dev_addr_type(net, NULL, addr); |
215 | } |
216 | |
217 | unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev, |
218 | __be32 addr) |
219 | { |
220 | return __inet_dev_addr_type(net, dev, addr); |
221 | } |
222 | |
223 | /* Given (packet source, input interface) and optional (dst, oif, tos): |
224 | - (main) check, that source is valid i.e. not broadcast or our local |
225 | address. |
226 | - figure out what "logical" interface this packet arrived |
227 | and calculate "specific destination" address. |
228 | - check, that packet arrived from expected physical interface. |
229 | */ |
230 | |
231 | int fib_validate_source(__be32 src, __be32 dst, u8 tos, int oif, |
232 | struct net_device *dev, __be32 *spec_dst, |
233 | u32 *itag, u32 mark) |
234 | { |
235 | struct in_device *in_dev; |
236 | struct flowi fl = { .nl_u = { .ip4_u = |
237 | { .daddr = src, |
238 | .saddr = dst, |
239 | .tos = tos } }, |
240 | .mark = mark, |
241 | .iif = oif }; |
242 | |
243 | struct fib_result res; |
244 | int no_addr, rpf; |
245 | int ret; |
246 | struct net *net; |
247 | |
248 | no_addr = rpf = 0; |
249 | rcu_read_lock(); |
250 | in_dev = __in_dev_get_rcu(dev); |
251 | if (in_dev) { |
252 | no_addr = in_dev->ifa_list == NULL; |
253 | rpf = IN_DEV_RPFILTER(in_dev); |
254 | if (mark && !IN_DEV_SRC_VMARK(in_dev)) |
255 | fl.mark = 0; |
256 | } |
257 | rcu_read_unlock(); |
258 | |
259 | if (in_dev == NULL) |
260 | goto e_inval; |
261 | |
262 | net = dev_net(dev); |
263 | if (fib_lookup(net, &fl, &res)) |
264 | goto last_resort; |
265 | if (res.type != RTN_UNICAST) |
266 | goto e_inval_res; |
267 | *spec_dst = FIB_RES_PREFSRC(res); |
268 | fib_combine_itag(itag, &res); |
269 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
270 | if (FIB_RES_DEV(res) == dev || res.fi->fib_nhs > 1) |
271 | #else |
272 | if (FIB_RES_DEV(res) == dev) |
273 | #endif |
274 | { |
275 | ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST; |
276 | fib_res_put(&res); |
277 | return ret; |
278 | } |
279 | fib_res_put(&res); |
280 | if (no_addr) |
281 | goto last_resort; |
282 | if (rpf == 1) |
283 | goto e_inval; |
284 | fl.oif = dev->ifindex; |
285 | |
286 | ret = 0; |
287 | if (fib_lookup(net, &fl, &res) == 0) { |
288 | if (res.type == RTN_UNICAST) { |
289 | *spec_dst = FIB_RES_PREFSRC(res); |
290 | ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST; |
291 | } |
292 | fib_res_put(&res); |
293 | } |
294 | return ret; |
295 | |
296 | last_resort: |
297 | if (rpf) |
298 | goto e_inval; |
299 | *spec_dst = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE); |
300 | *itag = 0; |
301 | return 0; |
302 | |
303 | e_inval_res: |
304 | fib_res_put(&res); |
305 | e_inval: |
306 | return -EINVAL; |
307 | } |
308 | |
309 | static inline __be32 sk_extract_addr(struct sockaddr *addr) |
310 | { |
311 | return ((struct sockaddr_in *) addr)->sin_addr.s_addr; |
312 | } |
313 | |
314 | static int put_rtax(struct nlattr *mx, int len, int type, u32 value) |
315 | { |
316 | struct nlattr *nla; |
317 | |
318 | nla = (struct nlattr *) ((char *) mx + len); |
319 | nla->nla_type = type; |
320 | nla->nla_len = nla_attr_size(4); |
321 | *(u32 *) nla_data(nla) = value; |
322 | |
323 | return len + nla_total_size(4); |
324 | } |
325 | |
326 | static int rtentry_to_fib_config(struct net *net, int cmd, struct rtentry *rt, |
327 | struct fib_config *cfg) |
328 | { |
329 | __be32 addr; |
330 | int plen; |
331 | |
332 | memset(cfg, 0, sizeof(*cfg)); |
333 | cfg->fc_nlinfo.nl_net = net; |
334 | |
335 | if (rt->rt_dst.sa_family != AF_INET) |
336 | return -EAFNOSUPPORT; |
337 | |
338 | /* |
339 | * Check mask for validity: |
340 | * a) it must be contiguous. |
341 | * b) destination must have all host bits clear. |
342 | * c) if application forgot to set correct family (AF_INET), |
343 | * reject request unless it is absolutely clear i.e. |
344 | * both family and mask are zero. |
345 | */ |
346 | plen = 32; |
347 | addr = sk_extract_addr(&rt->rt_dst); |
348 | if (!(rt->rt_flags & RTF_HOST)) { |
349 | __be32 mask = sk_extract_addr(&rt->rt_genmask); |
350 | |
351 | if (rt->rt_genmask.sa_family != AF_INET) { |
352 | if (mask || rt->rt_genmask.sa_family) |
353 | return -EAFNOSUPPORT; |
354 | } |
355 | |
356 | if (bad_mask(mask, addr)) |
357 | return -EINVAL; |
358 | |
359 | plen = inet_mask_len(mask); |
360 | } |
361 | |
362 | cfg->fc_dst_len = plen; |
363 | cfg->fc_dst = addr; |
364 | |
365 | if (cmd != SIOCDELRT) { |
366 | cfg->fc_nlflags = NLM_F_CREATE; |
367 | cfg->fc_protocol = RTPROT_BOOT; |
368 | } |
369 | |
370 | if (rt->rt_metric) |
371 | cfg->fc_priority = rt->rt_metric - 1; |
372 | |
373 | if (rt->rt_flags & RTF_REJECT) { |
374 | cfg->fc_scope = RT_SCOPE_HOST; |
375 | cfg->fc_type = RTN_UNREACHABLE; |
376 | return 0; |
377 | } |
378 | |
379 | cfg->fc_scope = RT_SCOPE_NOWHERE; |
380 | cfg->fc_type = RTN_UNICAST; |
381 | |
382 | if (rt->rt_dev) { |
383 | char *colon; |
384 | struct net_device *dev; |
385 | char devname[IFNAMSIZ]; |
386 | |
387 | if (copy_from_user(devname, rt->rt_dev, IFNAMSIZ-1)) |
388 | return -EFAULT; |
389 | |
390 | devname[IFNAMSIZ-1] = 0; |
391 | colon = strchr(devname, ':'); |
392 | if (colon) |
393 | *colon = 0; |
394 | dev = __dev_get_by_name(net, devname); |
395 | if (!dev) |
396 | return -ENODEV; |
397 | cfg->fc_oif = dev->ifindex; |
398 | if (colon) { |
399 | struct in_ifaddr *ifa; |
400 | struct in_device *in_dev = __in_dev_get_rtnl(dev); |
401 | if (!in_dev) |
402 | return -ENODEV; |
403 | *colon = ':'; |
404 | for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) |
405 | if (strcmp(ifa->ifa_label, devname) == 0) |
406 | break; |
407 | if (ifa == NULL) |
408 | return -ENODEV; |
409 | cfg->fc_prefsrc = ifa->ifa_local; |
410 | } |
411 | } |
412 | |
413 | addr = sk_extract_addr(&rt->rt_gateway); |
414 | if (rt->rt_gateway.sa_family == AF_INET && addr) { |
415 | cfg->fc_gw = addr; |
416 | if (rt->rt_flags & RTF_GATEWAY && |
417 | inet_addr_type(net, addr) == RTN_UNICAST) |
418 | cfg->fc_scope = RT_SCOPE_UNIVERSE; |
419 | } |
420 | |
421 | if (cmd == SIOCDELRT) |
422 | return 0; |
423 | |
424 | if (rt->rt_flags & RTF_GATEWAY && !cfg->fc_gw) |
425 | return -EINVAL; |
426 | |
427 | if (cfg->fc_scope == RT_SCOPE_NOWHERE) |
428 | cfg->fc_scope = RT_SCOPE_LINK; |
429 | |
430 | if (rt->rt_flags & (RTF_MTU | RTF_WINDOW | RTF_IRTT)) { |
431 | struct nlattr *mx; |
432 | int len = 0; |
433 | |
434 | mx = kzalloc(3 * nla_total_size(4), GFP_KERNEL); |
435 | if (mx == NULL) |
436 | return -ENOMEM; |
437 | |
438 | if (rt->rt_flags & RTF_MTU) |
439 | len = put_rtax(mx, len, RTAX_ADVMSS, rt->rt_mtu - 40); |
440 | |
441 | if (rt->rt_flags & RTF_WINDOW) |
442 | len = put_rtax(mx, len, RTAX_WINDOW, rt->rt_window); |
443 | |
444 | if (rt->rt_flags & RTF_IRTT) |
445 | len = put_rtax(mx, len, RTAX_RTT, rt->rt_irtt << 3); |
446 | |
447 | cfg->fc_mx = mx; |
448 | cfg->fc_mx_len = len; |
449 | } |
450 | |
451 | return 0; |
452 | } |
453 | |
454 | /* |
455 | * Handle IP routing ioctl calls. These are used to manipulate the routing tables |
456 | */ |
457 | |
458 | int ip_rt_ioctl(struct net *net, unsigned int cmd, void __user *arg) |
459 | { |
460 | struct fib_config cfg; |
461 | struct rtentry rt; |
462 | int err; |
463 | |
464 | switch (cmd) { |
465 | case SIOCADDRT: /* Add a route */ |
466 | case SIOCDELRT: /* Delete a route */ |
467 | if (!capable(CAP_NET_ADMIN)) |
468 | return -EPERM; |
469 | |
470 | if (copy_from_user(&rt, arg, sizeof(rt))) |
471 | return -EFAULT; |
472 | |
473 | rtnl_lock(); |
474 | err = rtentry_to_fib_config(net, cmd, &rt, &cfg); |
475 | if (err == 0) { |
476 | struct fib_table *tb; |
477 | |
478 | if (cmd == SIOCDELRT) { |
479 | tb = fib_get_table(net, cfg.fc_table); |
480 | if (tb) |
481 | err = tb->tb_delete(tb, &cfg); |
482 | else |
483 | err = -ESRCH; |
484 | } else { |
485 | tb = fib_new_table(net, cfg.fc_table); |
486 | if (tb) |
487 | err = tb->tb_insert(tb, &cfg); |
488 | else |
489 | err = -ENOBUFS; |
490 | } |
491 | |
492 | /* allocated by rtentry_to_fib_config() */ |
493 | kfree(cfg.fc_mx); |
494 | } |
495 | rtnl_unlock(); |
496 | return err; |
497 | } |
498 | return -EINVAL; |
499 | } |
500 | |
501 | const struct nla_policy rtm_ipv4_policy[RTA_MAX+1] = { |
502 | [RTA_DST] = { .type = NLA_U32 }, |
503 | [RTA_SRC] = { .type = NLA_U32 }, |
504 | [RTA_IIF] = { .type = NLA_U32 }, |
505 | [RTA_OIF] = { .type = NLA_U32 }, |
506 | [RTA_GATEWAY] = { .type = NLA_U32 }, |
507 | [RTA_PRIORITY] = { .type = NLA_U32 }, |
508 | [RTA_PREFSRC] = { .type = NLA_U32 }, |
509 | [RTA_METRICS] = { .type = NLA_NESTED }, |
510 | [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, |
511 | [RTA_FLOW] = { .type = NLA_U32 }, |
512 | }; |
513 | |
514 | static int rtm_to_fib_config(struct net *net, struct sk_buff *skb, |
515 | struct nlmsghdr *nlh, struct fib_config *cfg) |
516 | { |
517 | struct nlattr *attr; |
518 | int err, remaining; |
519 | struct rtmsg *rtm; |
520 | |
521 | err = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipv4_policy); |
522 | if (err < 0) |
523 | goto errout; |
524 | |
525 | memset(cfg, 0, sizeof(*cfg)); |
526 | |
527 | rtm = nlmsg_data(nlh); |
528 | cfg->fc_dst_len = rtm->rtm_dst_len; |
529 | cfg->fc_tos = rtm->rtm_tos; |
530 | cfg->fc_table = rtm->rtm_table; |
531 | cfg->fc_protocol = rtm->rtm_protocol; |
532 | cfg->fc_scope = rtm->rtm_scope; |
533 | cfg->fc_type = rtm->rtm_type; |
534 | cfg->fc_flags = rtm->rtm_flags; |
535 | cfg->fc_nlflags = nlh->nlmsg_flags; |
536 | |
537 | cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid; |
538 | cfg->fc_nlinfo.nlh = nlh; |
539 | cfg->fc_nlinfo.nl_net = net; |
540 | |
541 | if (cfg->fc_type > RTN_MAX) { |
542 | err = -EINVAL; |
543 | goto errout; |
544 | } |
545 | |
546 | nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) { |
547 | switch (nla_type(attr)) { |
548 | case RTA_DST: |
549 | cfg->fc_dst = nla_get_be32(attr); |
550 | break; |
551 | case RTA_OIF: |
552 | cfg->fc_oif = nla_get_u32(attr); |
553 | break; |
554 | case RTA_GATEWAY: |
555 | cfg->fc_gw = nla_get_be32(attr); |
556 | break; |
557 | case RTA_PRIORITY: |
558 | cfg->fc_priority = nla_get_u32(attr); |
559 | break; |
560 | case RTA_PREFSRC: |
561 | cfg->fc_prefsrc = nla_get_be32(attr); |
562 | break; |
563 | case RTA_METRICS: |
564 | cfg->fc_mx = nla_data(attr); |
565 | cfg->fc_mx_len = nla_len(attr); |
566 | break; |
567 | case RTA_MULTIPATH: |
568 | cfg->fc_mp = nla_data(attr); |
569 | cfg->fc_mp_len = nla_len(attr); |
570 | break; |
571 | case RTA_FLOW: |
572 | cfg->fc_flow = nla_get_u32(attr); |
573 | break; |
574 | case RTA_TABLE: |
575 | cfg->fc_table = nla_get_u32(attr); |
576 | break; |
577 | } |
578 | } |
579 | |
580 | return 0; |
581 | errout: |
582 | return err; |
583 | } |
584 | |
585 | static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
586 | { |
587 | struct net *net = sock_net(skb->sk); |
588 | struct fib_config cfg; |
589 | struct fib_table *tb; |
590 | int err; |
591 | |
592 | err = rtm_to_fib_config(net, skb, nlh, &cfg); |
593 | if (err < 0) |
594 | goto errout; |
595 | |
596 | tb = fib_get_table(net, cfg.fc_table); |
597 | if (tb == NULL) { |
598 | err = -ESRCH; |
599 | goto errout; |
600 | } |
601 | |
602 | err = tb->tb_delete(tb, &cfg); |
603 | errout: |
604 | return err; |
605 | } |
606 | |
607 | static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg) |
608 | { |
609 | struct net *net = sock_net(skb->sk); |
610 | struct fib_config cfg; |
611 | struct fib_table *tb; |
612 | int err; |
613 | |
614 | err = rtm_to_fib_config(net, skb, nlh, &cfg); |
615 | if (err < 0) |
616 | goto errout; |
617 | |
618 | tb = fib_new_table(net, cfg.fc_table); |
619 | if (tb == NULL) { |
620 | err = -ENOBUFS; |
621 | goto errout; |
622 | } |
623 | |
624 | err = tb->tb_insert(tb, &cfg); |
625 | errout: |
626 | return err; |
627 | } |
628 | |
629 | static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb) |
630 | { |
631 | struct net *net = sock_net(skb->sk); |
632 | unsigned int h, s_h; |
633 | unsigned int e = 0, s_e; |
634 | struct fib_table *tb; |
635 | struct hlist_node *node; |
636 | struct hlist_head *head; |
637 | int dumped = 0; |
638 | |
639 | if (nlmsg_len(cb->nlh) >= sizeof(struct rtmsg) && |
640 | ((struct rtmsg *) nlmsg_data(cb->nlh))->rtm_flags & RTM_F_CLONED) |
641 | return ip_rt_dump(skb, cb); |
642 | |
643 | s_h = cb->args[0]; |
644 | s_e = cb->args[1]; |
645 | |
646 | for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) { |
647 | e = 0; |
648 | head = &net->ipv4.fib_table_hash[h]; |
649 | hlist_for_each_entry(tb, node, head, tb_hlist) { |
650 | if (e < s_e) |
651 | goto next; |
652 | if (dumped) |
653 | memset(&cb->args[2], 0, sizeof(cb->args) - |
654 | 2 * sizeof(cb->args[0])); |
655 | if (tb->tb_dump(tb, skb, cb) < 0) |
656 | goto out; |
657 | dumped = 1; |
658 | next: |
659 | e++; |
660 | } |
661 | } |
662 | out: |
663 | cb->args[1] = e; |
664 | cb->args[0] = h; |
665 | |
666 | return skb->len; |
667 | } |
668 | |
669 | /* Prepare and feed intra-kernel routing request. |
670 | Really, it should be netlink message, but :-( netlink |
671 | can be not configured, so that we feed it directly |
672 | to fib engine. It is legal, because all events occur |
673 | only when netlink is already locked. |
674 | */ |
675 | |
676 | static void fib_magic(int cmd, int type, __be32 dst, int dst_len, struct in_ifaddr *ifa) |
677 | { |
678 | struct net *net = dev_net(ifa->ifa_dev->dev); |
679 | struct fib_table *tb; |
680 | struct fib_config cfg = { |
681 | .fc_protocol = RTPROT_KERNEL, |
682 | .fc_type = type, |
683 | .fc_dst = dst, |
684 | .fc_dst_len = dst_len, |
685 | .fc_prefsrc = ifa->ifa_local, |
686 | .fc_oif = ifa->ifa_dev->dev->ifindex, |
687 | .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND, |
688 | .fc_nlinfo = { |
689 | .nl_net = net, |
690 | }, |
691 | }; |
692 | |
693 | if (type == RTN_UNICAST) |
694 | tb = fib_new_table(net, RT_TABLE_MAIN); |
695 | else |
696 | tb = fib_new_table(net, RT_TABLE_LOCAL); |
697 | |
698 | if (tb == NULL) |
699 | return; |
700 | |
701 | cfg.fc_table = tb->tb_id; |
702 | |
703 | if (type != RTN_LOCAL) |
704 | cfg.fc_scope = RT_SCOPE_LINK; |
705 | else |
706 | cfg.fc_scope = RT_SCOPE_HOST; |
707 | |
708 | if (cmd == RTM_NEWROUTE) |
709 | tb->tb_insert(tb, &cfg); |
710 | else |
711 | tb->tb_delete(tb, &cfg); |
712 | } |
713 | |
714 | void fib_add_ifaddr(struct in_ifaddr *ifa) |
715 | { |
716 | struct in_device *in_dev = ifa->ifa_dev; |
717 | struct net_device *dev = in_dev->dev; |
718 | struct in_ifaddr *prim = ifa; |
719 | __be32 mask = ifa->ifa_mask; |
720 | __be32 addr = ifa->ifa_local; |
721 | __be32 prefix = ifa->ifa_address&mask; |
722 | |
723 | if (ifa->ifa_flags&IFA_F_SECONDARY) { |
724 | prim = inet_ifa_byprefix(in_dev, prefix, mask); |
725 | if (prim == NULL) { |
726 | printk(KERN_WARNING "fib_add_ifaddr: bug: prim == NULL\n"); |
727 | return; |
728 | } |
729 | } |
730 | |
731 | fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim); |
732 | |
733 | if (!(dev->flags&IFF_UP)) |
734 | return; |
735 | |
736 | /* Add broadcast address, if it is explicitly assigned. */ |
737 | if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF)) |
738 | fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim); |
739 | |
740 | if (!ipv4_is_zeronet(prefix) && !(ifa->ifa_flags&IFA_F_SECONDARY) && |
741 | (prefix != addr || ifa->ifa_prefixlen < 32)) { |
742 | fib_magic(RTM_NEWROUTE, dev->flags&IFF_LOOPBACK ? RTN_LOCAL : |
743 | RTN_UNICAST, prefix, ifa->ifa_prefixlen, prim); |
744 | |
745 | /* Add network specific broadcasts, when it takes a sense */ |
746 | if (ifa->ifa_prefixlen < 31) { |
747 | fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix, 32, prim); |
748 | fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix|~mask, 32, prim); |
749 | } |
750 | } |
751 | } |
752 | |
753 | static void fib_del_ifaddr(struct in_ifaddr *ifa) |
754 | { |
755 | struct in_device *in_dev = ifa->ifa_dev; |
756 | struct net_device *dev = in_dev->dev; |
757 | struct in_ifaddr *ifa1; |
758 | struct in_ifaddr *prim = ifa; |
759 | __be32 brd = ifa->ifa_address|~ifa->ifa_mask; |
760 | __be32 any = ifa->ifa_address&ifa->ifa_mask; |
761 | #define LOCAL_OK 1 |
762 | #define BRD_OK 2 |
763 | #define BRD0_OK 4 |
764 | #define BRD1_OK 8 |
765 | unsigned ok = 0; |
766 | |
767 | if (!(ifa->ifa_flags&IFA_F_SECONDARY)) |
768 | fib_magic(RTM_DELROUTE, dev->flags&IFF_LOOPBACK ? RTN_LOCAL : |
769 | RTN_UNICAST, any, ifa->ifa_prefixlen, prim); |
770 | else { |
771 | prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask); |
772 | if (prim == NULL) { |
773 | printk(KERN_WARNING "fib_del_ifaddr: bug: prim == NULL\n"); |
774 | return; |
775 | } |
776 | } |
777 | |
778 | /* Deletion is more complicated than add. |
779 | We should take care of not to delete too much :-) |
780 | |
781 | Scan address list to be sure that addresses are really gone. |
782 | */ |
783 | |
784 | for (ifa1 = in_dev->ifa_list; ifa1; ifa1 = ifa1->ifa_next) { |
785 | if (ifa->ifa_local == ifa1->ifa_local) |
786 | ok |= LOCAL_OK; |
787 | if (ifa->ifa_broadcast == ifa1->ifa_broadcast) |
788 | ok |= BRD_OK; |
789 | if (brd == ifa1->ifa_broadcast) |
790 | ok |= BRD1_OK; |
791 | if (any == ifa1->ifa_broadcast) |
792 | ok |= BRD0_OK; |
793 | } |
794 | |
795 | if (!(ok&BRD_OK)) |
796 | fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim); |
797 | if (!(ok&BRD1_OK)) |
798 | fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32, prim); |
799 | if (!(ok&BRD0_OK)) |
800 | fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32, prim); |
801 | if (!(ok&LOCAL_OK)) { |
802 | fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim); |
803 | |
804 | /* Check, that this local address finally disappeared. */ |
805 | if (inet_addr_type(dev_net(dev), ifa->ifa_local) != RTN_LOCAL) { |
806 | /* And the last, but not the least thing. |
807 | We must flush stray FIB entries. |
808 | |
809 | First of all, we scan fib_info list searching |
810 | for stray nexthop entries, then ignite fib_flush. |
811 | */ |
812 | if (fib_sync_down_addr(dev_net(dev), ifa->ifa_local)) |
813 | fib_flush(dev_net(dev)); |
814 | } |
815 | } |
816 | #undef LOCAL_OK |
817 | #undef BRD_OK |
818 | #undef BRD0_OK |
819 | #undef BRD1_OK |
820 | } |
821 | |
822 | static void nl_fib_lookup(struct fib_result_nl *frn, struct fib_table *tb ) |
823 | { |
824 | |
825 | struct fib_result res; |
826 | struct flowi fl = { .mark = frn->fl_mark, |
827 | .nl_u = { .ip4_u = { .daddr = frn->fl_addr, |
828 | .tos = frn->fl_tos, |
829 | .scope = frn->fl_scope } } }; |
830 | |
831 | #ifdef CONFIG_IP_MULTIPLE_TABLES |
832 | res.r = NULL; |
833 | #endif |
834 | |
835 | frn->err = -ENOENT; |
836 | if (tb) { |
837 | local_bh_disable(); |
838 | |
839 | frn->tb_id = tb->tb_id; |
840 | frn->err = tb->tb_lookup(tb, &fl, &res); |
841 | |
842 | if (!frn->err) { |
843 | frn->prefixlen = res.prefixlen; |
844 | frn->nh_sel = res.nh_sel; |
845 | frn->type = res.type; |
846 | frn->scope = res.scope; |
847 | fib_res_put(&res); |
848 | } |
849 | local_bh_enable(); |
850 | } |
851 | } |
852 | |
853 | static void nl_fib_input(struct sk_buff *skb) |
854 | { |
855 | struct net *net; |
856 | struct fib_result_nl *frn; |
857 | struct nlmsghdr *nlh; |
858 | struct fib_table *tb; |
859 | u32 pid; |
860 | |
861 | net = sock_net(skb->sk); |
862 | nlh = nlmsg_hdr(skb); |
863 | if (skb->len < NLMSG_SPACE(0) || skb->len < nlh->nlmsg_len || |
864 | nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*frn))) |
865 | return; |
866 | |
867 | skb = skb_clone(skb, GFP_KERNEL); |
868 | if (skb == NULL) |
869 | return; |
870 | nlh = nlmsg_hdr(skb); |
871 | |
872 | frn = (struct fib_result_nl *) NLMSG_DATA(nlh); |
873 | tb = fib_get_table(net, frn->tb_id_in); |
874 | |
875 | nl_fib_lookup(frn, tb); |
876 | |
877 | pid = NETLINK_CB(skb).pid; /* pid of sending process */ |
878 | NETLINK_CB(skb).pid = 0; /* from kernel */ |
879 | NETLINK_CB(skb).dst_group = 0; /* unicast */ |
880 | netlink_unicast(net->ipv4.fibnl, skb, pid, MSG_DONTWAIT); |
881 | } |
882 | |
883 | static int nl_fib_lookup_init(struct net *net) |
884 | { |
885 | struct sock *sk; |
886 | sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, 0, |
887 | nl_fib_input, NULL, THIS_MODULE); |
888 | if (sk == NULL) |
889 | return -EAFNOSUPPORT; |
890 | net->ipv4.fibnl = sk; |
891 | return 0; |
892 | } |
893 | |
894 | static void nl_fib_lookup_exit(struct net *net) |
895 | { |
896 | netlink_kernel_release(net->ipv4.fibnl); |
897 | net->ipv4.fibnl = NULL; |
898 | } |
899 | |
900 | static void fib_disable_ip(struct net_device *dev, int force) |
901 | { |
902 | if (fib_sync_down_dev(dev, force)) |
903 | fib_flush(dev_net(dev)); |
904 | rt_cache_flush(dev_net(dev), 0); |
905 | arp_ifdown(dev); |
906 | } |
907 | |
908 | static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr) |
909 | { |
910 | struct in_ifaddr *ifa = (struct in_ifaddr *)ptr; |
911 | struct net_device *dev = ifa->ifa_dev->dev; |
912 | |
913 | switch (event) { |
914 | case NETDEV_UP: |
915 | fib_add_ifaddr(ifa); |
916 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
917 | fib_sync_up(dev); |
918 | #endif |
919 | rt_cache_flush(dev_net(dev), -1); |
920 | break; |
921 | case NETDEV_DOWN: |
922 | fib_del_ifaddr(ifa); |
923 | if (ifa->ifa_dev->ifa_list == NULL) { |
924 | /* Last address was deleted from this interface. |
925 | Disable IP. |
926 | */ |
927 | fib_disable_ip(dev, 1); |
928 | } else { |
929 | rt_cache_flush(dev_net(dev), -1); |
930 | } |
931 | break; |
932 | } |
933 | return NOTIFY_DONE; |
934 | } |
935 | |
936 | static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr) |
937 | { |
938 | struct net_device *dev = ptr; |
939 | struct in_device *in_dev = __in_dev_get_rtnl(dev); |
940 | |
941 | if (event == NETDEV_UNREGISTER) { |
942 | fib_disable_ip(dev, 2); |
943 | return NOTIFY_DONE; |
944 | } |
945 | |
946 | if (!in_dev) |
947 | return NOTIFY_DONE; |
948 | |
949 | switch (event) { |
950 | case NETDEV_UP: |
951 | for_ifa(in_dev) { |
952 | fib_add_ifaddr(ifa); |
953 | } endfor_ifa(in_dev); |
954 | #ifdef CONFIG_IP_ROUTE_MULTIPATH |
955 | fib_sync_up(dev); |
956 | #endif |
957 | rt_cache_flush(dev_net(dev), -1); |
958 | break; |
959 | case NETDEV_DOWN: |
960 | fib_disable_ip(dev, 0); |
961 | break; |
962 | case NETDEV_CHANGEMTU: |
963 | case NETDEV_CHANGE: |
964 | rt_cache_flush(dev_net(dev), 0); |
965 | break; |
966 | } |
967 | return NOTIFY_DONE; |
968 | } |
969 | |
970 | static struct notifier_block fib_inetaddr_notifier = { |
971 | .notifier_call = fib_inetaddr_event, |
972 | }; |
973 | |
974 | static struct notifier_block fib_netdev_notifier = { |
975 | .notifier_call = fib_netdev_event, |
976 | }; |
977 | |
978 | static int __net_init ip_fib_net_init(struct net *net) |
979 | { |
980 | int err; |
981 | unsigned int i; |
982 | |
983 | net->ipv4.fib_table_hash = kzalloc( |
984 | sizeof(struct hlist_head)*FIB_TABLE_HASHSZ, GFP_KERNEL); |
985 | if (net->ipv4.fib_table_hash == NULL) |
986 | return -ENOMEM; |
987 | |
988 | for (i = 0; i < FIB_TABLE_HASHSZ; i++) |
989 | INIT_HLIST_HEAD(&net->ipv4.fib_table_hash[i]); |
990 | |
991 | err = fib4_rules_init(net); |
992 | if (err < 0) |
993 | goto fail; |
994 | return 0; |
995 | |
996 | fail: |
997 | kfree(net->ipv4.fib_table_hash); |
998 | return err; |
999 | } |
1000 | |
1001 | static void __net_exit ip_fib_net_exit(struct net *net) |
1002 | { |
1003 | unsigned int i; |
1004 | |
1005 | #ifdef CONFIG_IP_MULTIPLE_TABLES |
1006 | fib4_rules_exit(net); |
1007 | #endif |
1008 | |
1009 | for (i = 0; i < FIB_TABLE_HASHSZ; i++) { |
1010 | struct fib_table *tb; |
1011 | struct hlist_head *head; |
1012 | struct hlist_node *node, *tmp; |
1013 | |
1014 | head = &net->ipv4.fib_table_hash[i]; |
1015 | hlist_for_each_entry_safe(tb, node, tmp, head, tb_hlist) { |
1016 | hlist_del(node); |
1017 | tb->tb_flush(tb); |
1018 | kfree(tb); |
1019 | } |
1020 | } |
1021 | kfree(net->ipv4.fib_table_hash); |
1022 | } |
1023 | |
1024 | static int __net_init fib_net_init(struct net *net) |
1025 | { |
1026 | int error; |
1027 | |
1028 | error = ip_fib_net_init(net); |
1029 | if (error < 0) |
1030 | goto out; |
1031 | error = nl_fib_lookup_init(net); |
1032 | if (error < 0) |
1033 | goto out_nlfl; |
1034 | error = fib_proc_init(net); |
1035 | if (error < 0) |
1036 | goto out_proc; |
1037 | out: |
1038 | return error; |
1039 | |
1040 | out_proc: |
1041 | nl_fib_lookup_exit(net); |
1042 | out_nlfl: |
1043 | ip_fib_net_exit(net); |
1044 | goto out; |
1045 | } |
1046 | |
1047 | static void __net_exit fib_net_exit(struct net *net) |
1048 | { |
1049 | fib_proc_exit(net); |
1050 | nl_fib_lookup_exit(net); |
1051 | ip_fib_net_exit(net); |
1052 | } |
1053 | |
1054 | static struct pernet_operations fib_net_ops = { |
1055 | .init = fib_net_init, |
1056 | .exit = fib_net_exit, |
1057 | }; |
1058 | |
1059 | void __init ip_fib_init(void) |
1060 | { |
1061 | rtnl_register(PF_INET, RTM_NEWROUTE, inet_rtm_newroute, NULL); |
1062 | rtnl_register(PF_INET, RTM_DELROUTE, inet_rtm_delroute, NULL); |
1063 | rtnl_register(PF_INET, RTM_GETROUTE, NULL, inet_dump_fib); |
1064 | |
1065 | register_pernet_subsys(&fib_net_ops); |
1066 | register_netdevice_notifier(&fib_netdev_notifier); |
1067 | register_inetaddr_notifier(&fib_inetaddr_notifier); |
1068 | |
1069 | fib_hash_init(); |
1070 | } |
1071 | |
1072 | EXPORT_SYMBOL(inet_addr_type); |
1073 | EXPORT_SYMBOL(inet_dev_addr_type); |
1074 | EXPORT_SYMBOL(ip_dev_find); |
1075 |
Branches:
ben-wpan
ben-wpan-stefan
javiroman/ks7010
jz-2.6.34
jz-2.6.34-rc5
jz-2.6.34-rc6
jz-2.6.34-rc7
jz-2.6.35
jz-2.6.36
jz-2.6.37
jz-2.6.38
jz-2.6.39
jz-3.0
jz-3.1
jz-3.11
jz-3.12
jz-3.13
jz-3.15
jz-3.16
jz-3.18-dt
jz-3.2
jz-3.3
jz-3.4
jz-3.5
jz-3.6
jz-3.6-rc2-pwm
jz-3.9
jz-3.9-clk
jz-3.9-rc8
jz47xx
jz47xx-2.6.38
master
Tags:
od-2011-09-04
od-2011-09-18
v2.6.34-rc5
v2.6.34-rc6
v2.6.34-rc7
v3.9