blob: 7718823711e3ba178f95af76a1aa235313c72de3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 * Version: $Id: fib_frontend.c,v 1.26 2001/10/31 21:55:54 davem Exp $
9 *
10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <asm/uaccess.h>
20#include <asm/system.h>
21#include <linux/bitops.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080022#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/types.h>
24#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/mm.h>
26#include <linux/string.h>
27#include <linux/socket.h>
28#include <linux/sockios.h>
29#include <linux/errno.h>
30#include <linux/in.h>
31#include <linux/inet.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020032#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/netdevice.h>
Thomas Graf18237302006-08-04 23:04:54 -070034#include <linux/if_addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/if_arp.h>
36#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/init.h>
Patrick McHardy1af5a8c2006-08-10 23:10:46 -070038#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <net/ip.h>
41#include <net/protocol.h>
42#include <net/route.h>
43#include <net/tcp.h>
44#include <net/sock.h>
45#include <net/icmp.h>
46#include <net/arp.h>
47#include <net/ip_fib.h>
Thomas Graf63f34442007-03-22 11:55:17 -070048#include <net/rtnetlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50#define FFprint(a...) printk(KERN_DEBUG a)
51
David S. Miller28f7b0362007-10-10 21:32:39 -070052static struct sock *fibnl;
Denis V. Lunev93456b62008-01-10 03:23:38 -080053struct hlist_head fib_table_hash[FIB_TABLE_HASHSZ];
David S. Miller28f7b0362007-10-10 21:32:39 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#ifndef CONFIG_IP_MULTIPLE_TABLES
56
Denis V. Lunev7b1a74f2008-01-10 03:22:17 -080057static int __net_init fib4_rules_init(struct net *net)
Pavel Emelyanovc3e9a352007-11-06 23:34:04 -080058{
Denis V. Lunev93456b62008-01-10 03:23:38 -080059 struct fib_table *local_table, *main_table;
60
61 local_table = fib_hash_init(RT_TABLE_LOCAL);
62 if (local_table == NULL)
Denis V. Lunevdbb50162008-01-10 03:21:49 -080063 return -ENOMEM;
64
Denis V. Lunev93456b62008-01-10 03:23:38 -080065 main_table = fib_hash_init(RT_TABLE_MAIN);
66 if (main_table == NULL)
Denis V. Lunevdbb50162008-01-10 03:21:49 -080067 goto fail;
68
Denis V. Lunev93456b62008-01-10 03:23:38 -080069 hlist_add_head_rcu(&local_table->tb_hlist,
70 &fib_table_hash[TABLE_LOCAL_INDEX]);
71 hlist_add_head_rcu(&main_table->tb_hlist,
72 &fib_table_hash[TABLE_MAIN_INDEX]);
Denis V. Lunevdbb50162008-01-10 03:21:49 -080073 return 0;
74
75fail:
Denis V. Lunev93456b62008-01-10 03:23:38 -080076 kfree(local_table);
Denis V. Lunevdbb50162008-01-10 03:21:49 -080077 return -ENOMEM;
Pavel Emelyanovc3e9a352007-11-06 23:34:04 -080078}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#else
80
Denis V. Lunev8ad49422008-01-10 03:24:11 -080081struct fib_table *fib_new_table(struct net *net, u32 id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 struct fib_table *tb;
Patrick McHardy1af5a8c2006-08-10 23:10:46 -070084 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Patrick McHardy1af5a8c2006-08-10 23:10:46 -070086 if (id == 0)
87 id = RT_TABLE_MAIN;
Denis V. Lunev8ad49422008-01-10 03:24:11 -080088 tb = fib_get_table(net, id);
Patrick McHardy1af5a8c2006-08-10 23:10:46 -070089 if (tb)
90 return tb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 tb = fib_hash_init(id);
92 if (!tb)
93 return NULL;
Patrick McHardy1af5a8c2006-08-10 23:10:46 -070094 h = id & (FIB_TABLE_HASHSZ - 1);
95 hlist_add_head_rcu(&tb->tb_hlist, &fib_table_hash[h]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return tb;
97}
98
Denis V. Lunev8ad49422008-01-10 03:24:11 -080099struct fib_table *fib_get_table(struct net *net, u32 id)
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700100{
101 struct fib_table *tb;
102 struct hlist_node *node;
103 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700105 if (id == 0)
106 id = RT_TABLE_MAIN;
107 h = id & (FIB_TABLE_HASHSZ - 1);
108 rcu_read_lock();
109 hlist_for_each_entry_rcu(tb, node, &fib_table_hash[h], tb_hlist) {
110 if (tb->tb_id == id) {
111 rcu_read_unlock();
112 return tb;
113 }
114 }
115 rcu_read_unlock();
116 return NULL;
117}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#endif /* CONFIG_IP_MULTIPLE_TABLES */
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120static void fib_flush(void)
121{
122 int flushed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 struct fib_table *tb;
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700124 struct hlist_node *node;
125 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700127 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
128 hlist_for_each_entry(tb, node, &fib_table_hash[h], tb_hlist)
129 flushed += tb->tb_flush(tb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 if (flushed)
133 rt_cache_flush(-1);
134}
135
136/*
137 * Find the first device with a given source address.
138 */
139
Al Viro60cad5d2006-09-26 22:17:09 -0700140struct net_device * ip_dev_find(__be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 struct flowi fl = { .nl_u = { .ip4_u = { .daddr = addr } } };
143 struct fib_result res;
144 struct net_device *dev = NULL;
Pavel Emelyanov03cf7862007-10-23 21:17:27 -0700145 struct fib_table *local_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147#ifdef CONFIG_IP_MULTIPLE_TABLES
148 res.r = NULL;
149#endif
150
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800151 local_table = fib_get_table(&init_net, RT_TABLE_LOCAL);
Pavel Emelyanov03cf7862007-10-23 21:17:27 -0700152 if (!local_table || local_table->tb_lookup(local_table, &fl, &res))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return NULL;
154 if (res.type != RTN_LOCAL)
155 goto out;
156 dev = FIB_RES_DEV(res);
157
158 if (dev)
159 dev_hold(dev);
160out:
161 fib_res_put(&res);
162 return dev;
163}
164
Laszlo Attila Toth05538112007-12-04 23:28:46 -0800165/*
166 * Find address type as if only "dev" was present in the system. If
167 * on_dev is NULL then all interfaces are taken into consideration.
168 */
169static inline unsigned __inet_dev_addr_type(const struct net_device *dev,
170 __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 struct flowi fl = { .nl_u = { .ip4_u = { .daddr = addr } } };
173 struct fib_result res;
174 unsigned ret = RTN_BROADCAST;
Pavel Emelyanov03cf7862007-10-23 21:17:27 -0700175 struct fib_table *local_table;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Joe Perchesf97c1e02007-12-16 13:45:43 -0800177 if (ipv4_is_zeronet(addr) || ipv4_is_badclass(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return RTN_BROADCAST;
Joe Perchesf97c1e02007-12-16 13:45:43 -0800179 if (ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return RTN_MULTICAST;
181
182#ifdef CONFIG_IP_MULTIPLE_TABLES
183 res.r = NULL;
184#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900185
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800186 local_table = fib_get_table(&init_net, RT_TABLE_LOCAL);
Pavel Emelyanov03cf7862007-10-23 21:17:27 -0700187 if (local_table) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 ret = RTN_UNICAST;
Pavel Emelyanov03cf7862007-10-23 21:17:27 -0700189 if (!local_table->tb_lookup(local_table, &fl, &res)) {
Laszlo Attila Toth05538112007-12-04 23:28:46 -0800190 if (!dev || dev == res.fi->fib_dev)
191 ret = res.type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 fib_res_put(&res);
193 }
194 }
195 return ret;
196}
197
Laszlo Attila Toth05538112007-12-04 23:28:46 -0800198unsigned int inet_addr_type(__be32 addr)
199{
200 return __inet_dev_addr_type(NULL, addr);
201}
202
203unsigned int inet_dev_addr_type(const struct net_device *dev, __be32 addr)
204{
205 return __inet_dev_addr_type(dev, addr);
206}
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/* Given (packet source, input interface) and optional (dst, oif, tos):
209 - (main) check, that source is valid i.e. not broadcast or our local
210 address.
211 - figure out what "logical" interface this packet arrived
212 and calculate "specific destination" address.
213 - check, that packet arrived from expected physical interface.
214 */
215
Al Virod9c9df82006-09-26 21:28:14 -0700216int fib_validate_source(__be32 src, __be32 dst, u8 tos, int oif,
217 struct net_device *dev, __be32 *spec_dst, u32 *itag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 struct in_device *in_dev;
220 struct flowi fl = { .nl_u = { .ip4_u =
221 { .daddr = src,
222 .saddr = dst,
223 .tos = tos } },
224 .iif = oif };
225 struct fib_result res;
226 int no_addr, rpf;
227 int ret;
228
229 no_addr = rpf = 0;
230 rcu_read_lock();
Herbert Xue5ed6392005-10-03 14:35:55 -0700231 in_dev = __in_dev_get_rcu(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (in_dev) {
233 no_addr = in_dev->ifa_list == NULL;
234 rpf = IN_DEV_RPFILTER(in_dev);
235 }
236 rcu_read_unlock();
237
238 if (in_dev == NULL)
239 goto e_inval;
240
241 if (fib_lookup(&fl, &res))
242 goto last_resort;
243 if (res.type != RTN_UNICAST)
244 goto e_inval_res;
245 *spec_dst = FIB_RES_PREFSRC(res);
246 fib_combine_itag(itag, &res);
247#ifdef CONFIG_IP_ROUTE_MULTIPATH
248 if (FIB_RES_DEV(res) == dev || res.fi->fib_nhs > 1)
249#else
250 if (FIB_RES_DEV(res) == dev)
251#endif
252 {
253 ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
254 fib_res_put(&res);
255 return ret;
256 }
257 fib_res_put(&res);
258 if (no_addr)
259 goto last_resort;
260 if (rpf)
261 goto e_inval;
262 fl.oif = dev->ifindex;
263
264 ret = 0;
265 if (fib_lookup(&fl, &res) == 0) {
266 if (res.type == RTN_UNICAST) {
267 *spec_dst = FIB_RES_PREFSRC(res);
268 ret = FIB_RES_NH(res).nh_scope >= RT_SCOPE_HOST;
269 }
270 fib_res_put(&res);
271 }
272 return ret;
273
274last_resort:
275 if (rpf)
276 goto e_inval;
277 *spec_dst = inet_select_addr(dev, 0, RT_SCOPE_UNIVERSE);
278 *itag = 0;
279 return 0;
280
281e_inval_res:
282 fib_res_put(&res);
283e_inval:
284 return -EINVAL;
285}
286
Al Viro81f7bf62006-09-27 18:40:00 -0700287static inline __be32 sk_extract_addr(struct sockaddr *addr)
Thomas Graf4e902c52006-08-17 18:14:52 -0700288{
289 return ((struct sockaddr_in *) addr)->sin_addr.s_addr;
290}
291
292static int put_rtax(struct nlattr *mx, int len, int type, u32 value)
293{
294 struct nlattr *nla;
295
296 nla = (struct nlattr *) ((char *) mx + len);
297 nla->nla_type = type;
298 nla->nla_len = nla_attr_size(4);
299 *(u32 *) nla_data(nla) = value;
300
301 return len + nla_total_size(4);
302}
303
304static int rtentry_to_fib_config(int cmd, struct rtentry *rt,
305 struct fib_config *cfg)
306{
Al Viro6d85c102006-09-26 22:15:46 -0700307 __be32 addr;
Thomas Graf4e902c52006-08-17 18:14:52 -0700308 int plen;
309
310 memset(cfg, 0, sizeof(*cfg));
311
312 if (rt->rt_dst.sa_family != AF_INET)
313 return -EAFNOSUPPORT;
314
315 /*
316 * Check mask for validity:
317 * a) it must be contiguous.
318 * b) destination must have all host bits clear.
319 * c) if application forgot to set correct family (AF_INET),
320 * reject request unless it is absolutely clear i.e.
321 * both family and mask are zero.
322 */
323 plen = 32;
324 addr = sk_extract_addr(&rt->rt_dst);
325 if (!(rt->rt_flags & RTF_HOST)) {
Al Viro81f7bf62006-09-27 18:40:00 -0700326 __be32 mask = sk_extract_addr(&rt->rt_genmask);
Thomas Graf4e902c52006-08-17 18:14:52 -0700327
328 if (rt->rt_genmask.sa_family != AF_INET) {
329 if (mask || rt->rt_genmask.sa_family)
330 return -EAFNOSUPPORT;
331 }
332
333 if (bad_mask(mask, addr))
334 return -EINVAL;
335
336 plen = inet_mask_len(mask);
337 }
338
339 cfg->fc_dst_len = plen;
340 cfg->fc_dst = addr;
341
342 if (cmd != SIOCDELRT) {
343 cfg->fc_nlflags = NLM_F_CREATE;
344 cfg->fc_protocol = RTPROT_BOOT;
345 }
346
347 if (rt->rt_metric)
348 cfg->fc_priority = rt->rt_metric - 1;
349
350 if (rt->rt_flags & RTF_REJECT) {
351 cfg->fc_scope = RT_SCOPE_HOST;
352 cfg->fc_type = RTN_UNREACHABLE;
353 return 0;
354 }
355
356 cfg->fc_scope = RT_SCOPE_NOWHERE;
357 cfg->fc_type = RTN_UNICAST;
358
359 if (rt->rt_dev) {
360 char *colon;
361 struct net_device *dev;
362 char devname[IFNAMSIZ];
363
364 if (copy_from_user(devname, rt->rt_dev, IFNAMSIZ-1))
365 return -EFAULT;
366
367 devname[IFNAMSIZ-1] = 0;
368 colon = strchr(devname, ':');
369 if (colon)
370 *colon = 0;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700371 dev = __dev_get_by_name(&init_net, devname);
Thomas Graf4e902c52006-08-17 18:14:52 -0700372 if (!dev)
373 return -ENODEV;
374 cfg->fc_oif = dev->ifindex;
375 if (colon) {
376 struct in_ifaddr *ifa;
377 struct in_device *in_dev = __in_dev_get_rtnl(dev);
378 if (!in_dev)
379 return -ENODEV;
380 *colon = ':';
381 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next)
382 if (strcmp(ifa->ifa_label, devname) == 0)
383 break;
384 if (ifa == NULL)
385 return -ENODEV;
386 cfg->fc_prefsrc = ifa->ifa_local;
387 }
388 }
389
390 addr = sk_extract_addr(&rt->rt_gateway);
391 if (rt->rt_gateway.sa_family == AF_INET && addr) {
392 cfg->fc_gw = addr;
393 if (rt->rt_flags & RTF_GATEWAY &&
394 inet_addr_type(addr) == RTN_UNICAST)
395 cfg->fc_scope = RT_SCOPE_UNIVERSE;
396 }
397
398 if (cmd == SIOCDELRT)
399 return 0;
400
401 if (rt->rt_flags & RTF_GATEWAY && !cfg->fc_gw)
402 return -EINVAL;
403
404 if (cfg->fc_scope == RT_SCOPE_NOWHERE)
405 cfg->fc_scope = RT_SCOPE_LINK;
406
407 if (rt->rt_flags & (RTF_MTU | RTF_WINDOW | RTF_IRTT)) {
408 struct nlattr *mx;
409 int len = 0;
410
411 mx = kzalloc(3 * nla_total_size(4), GFP_KERNEL);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900412 if (mx == NULL)
Thomas Graf4e902c52006-08-17 18:14:52 -0700413 return -ENOMEM;
414
415 if (rt->rt_flags & RTF_MTU)
416 len = put_rtax(mx, len, RTAX_ADVMSS, rt->rt_mtu - 40);
417
418 if (rt->rt_flags & RTF_WINDOW)
419 len = put_rtax(mx, len, RTAX_WINDOW, rt->rt_window);
420
421 if (rt->rt_flags & RTF_IRTT)
422 len = put_rtax(mx, len, RTAX_RTT, rt->rt_irtt << 3);
423
424 cfg->fc_mx = mx;
425 cfg->fc_mx_len = len;
426 }
427
428 return 0;
429}
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/*
432 * Handle IP routing ioctl calls. These are used to manipulate the routing tables
433 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435int ip_rt_ioctl(unsigned int cmd, void __user *arg)
436{
Thomas Graf4e902c52006-08-17 18:14:52 -0700437 struct fib_config cfg;
438 struct rtentry rt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 switch (cmd) {
442 case SIOCADDRT: /* Add a route */
443 case SIOCDELRT: /* Delete a route */
444 if (!capable(CAP_NET_ADMIN))
445 return -EPERM;
Thomas Graf4e902c52006-08-17 18:14:52 -0700446
447 if (copy_from_user(&rt, arg, sizeof(rt)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return -EFAULT;
Thomas Graf4e902c52006-08-17 18:14:52 -0700449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 rtnl_lock();
Thomas Graf4e902c52006-08-17 18:14:52 -0700451 err = rtentry_to_fib_config(cmd, &rt, &cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (err == 0) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700453 struct fib_table *tb;
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (cmd == SIOCDELRT) {
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800456 tb = fib_get_table(&init_net, cfg.fc_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (tb)
Thomas Graf4e902c52006-08-17 18:14:52 -0700458 err = tb->tb_delete(tb, &cfg);
459 else
460 err = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 } else {
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800462 tb = fib_new_table(&init_net, cfg.fc_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (tb)
Thomas Graf4e902c52006-08-17 18:14:52 -0700464 err = tb->tb_insert(tb, &cfg);
465 else
466 err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700468
469 /* allocated by rtentry_to_fib_config() */
470 kfree(cfg.fc_mx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472 rtnl_unlock();
473 return err;
474 }
475 return -EINVAL;
476}
477
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700478const struct nla_policy rtm_ipv4_policy[RTA_MAX+1] = {
Thomas Graf4e902c52006-08-17 18:14:52 -0700479 [RTA_DST] = { .type = NLA_U32 },
480 [RTA_SRC] = { .type = NLA_U32 },
481 [RTA_IIF] = { .type = NLA_U32 },
482 [RTA_OIF] = { .type = NLA_U32 },
483 [RTA_GATEWAY] = { .type = NLA_U32 },
484 [RTA_PRIORITY] = { .type = NLA_U32 },
485 [RTA_PREFSRC] = { .type = NLA_U32 },
486 [RTA_METRICS] = { .type = NLA_NESTED },
Thomas Graf5176f912006-08-26 20:13:18 -0700487 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
Thomas Graf4e902c52006-08-17 18:14:52 -0700488 [RTA_PROTOINFO] = { .type = NLA_U32 },
489 [RTA_FLOW] = { .type = NLA_U32 },
Thomas Graf4e902c52006-08-17 18:14:52 -0700490};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Thomas Graf4e902c52006-08-17 18:14:52 -0700492static int rtm_to_fib_config(struct sk_buff *skb, struct nlmsghdr *nlh,
493 struct fib_config *cfg)
494{
495 struct nlattr *attr;
496 int err, remaining;
497 struct rtmsg *rtm;
498
499 err = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipv4_policy);
500 if (err < 0)
501 goto errout;
502
503 memset(cfg, 0, sizeof(*cfg));
504
505 rtm = nlmsg_data(nlh);
Thomas Graf4e902c52006-08-17 18:14:52 -0700506 cfg->fc_dst_len = rtm->rtm_dst_len;
Thomas Graf4e902c52006-08-17 18:14:52 -0700507 cfg->fc_tos = rtm->rtm_tos;
508 cfg->fc_table = rtm->rtm_table;
509 cfg->fc_protocol = rtm->rtm_protocol;
510 cfg->fc_scope = rtm->rtm_scope;
511 cfg->fc_type = rtm->rtm_type;
512 cfg->fc_flags = rtm->rtm_flags;
513 cfg->fc_nlflags = nlh->nlmsg_flags;
514
515 cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid;
516 cfg->fc_nlinfo.nlh = nlh;
517
Thomas Grafa0ee18b2007-03-24 20:32:54 -0700518 if (cfg->fc_type > RTN_MAX) {
519 err = -EINVAL;
520 goto errout;
521 }
522
Thomas Graf4e902c52006-08-17 18:14:52 -0700523 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), remaining) {
Thomas Graf8f4c1f92007-09-12 14:44:36 +0200524 switch (nla_type(attr)) {
Thomas Graf4e902c52006-08-17 18:14:52 -0700525 case RTA_DST:
Al Viro17fb2c62006-09-26 22:15:25 -0700526 cfg->fc_dst = nla_get_be32(attr);
Thomas Graf4e902c52006-08-17 18:14:52 -0700527 break;
Thomas Graf4e902c52006-08-17 18:14:52 -0700528 case RTA_OIF:
529 cfg->fc_oif = nla_get_u32(attr);
530 break;
531 case RTA_GATEWAY:
Al Viro17fb2c62006-09-26 22:15:25 -0700532 cfg->fc_gw = nla_get_be32(attr);
Thomas Graf4e902c52006-08-17 18:14:52 -0700533 break;
534 case RTA_PRIORITY:
535 cfg->fc_priority = nla_get_u32(attr);
536 break;
537 case RTA_PREFSRC:
Al Viro17fb2c62006-09-26 22:15:25 -0700538 cfg->fc_prefsrc = nla_get_be32(attr);
Thomas Graf4e902c52006-08-17 18:14:52 -0700539 break;
540 case RTA_METRICS:
541 cfg->fc_mx = nla_data(attr);
542 cfg->fc_mx_len = nla_len(attr);
543 break;
544 case RTA_MULTIPATH:
545 cfg->fc_mp = nla_data(attr);
546 cfg->fc_mp_len = nla_len(attr);
547 break;
548 case RTA_FLOW:
549 cfg->fc_flow = nla_get_u32(attr);
550 break;
Thomas Graf4e902c52006-08-17 18:14:52 -0700551 case RTA_TABLE:
552 cfg->fc_table = nla_get_u32(attr);
553 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555 }
Thomas Graf4e902c52006-08-17 18:14:52 -0700556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return 0;
Thomas Graf4e902c52006-08-17 18:14:52 -0700558errout:
559 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
Thomas Graf63f34442007-03-22 11:55:17 -0700562static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Denis V. Lunevb8542722007-12-01 00:21:31 +1100564 struct net *net = skb->sk->sk_net;
Thomas Graf4e902c52006-08-17 18:14:52 -0700565 struct fib_config cfg;
566 struct fib_table *tb;
567 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Denis V. Lunevb8542722007-12-01 00:21:31 +1100569 if (net != &init_net)
570 return -EINVAL;
571
Thomas Graf4e902c52006-08-17 18:14:52 -0700572 err = rtm_to_fib_config(skb, nlh, &cfg);
573 if (err < 0)
574 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800576 tb = fib_get_table(net, cfg.fc_table);
Thomas Graf4e902c52006-08-17 18:14:52 -0700577 if (tb == NULL) {
578 err = -ESRCH;
579 goto errout;
580 }
581
582 err = tb->tb_delete(tb, &cfg);
583errout:
584 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Thomas Graf63f34442007-03-22 11:55:17 -0700587static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Denis V. Lunevb8542722007-12-01 00:21:31 +1100589 struct net *net = skb->sk->sk_net;
Thomas Graf4e902c52006-08-17 18:14:52 -0700590 struct fib_config cfg;
591 struct fib_table *tb;
592 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Denis V. Lunevb8542722007-12-01 00:21:31 +1100594 if (net != &init_net)
595 return -EINVAL;
596
Thomas Graf4e902c52006-08-17 18:14:52 -0700597 err = rtm_to_fib_config(skb, nlh, &cfg);
598 if (err < 0)
599 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800601 tb = fib_new_table(&init_net, cfg.fc_table);
Thomas Graf4e902c52006-08-17 18:14:52 -0700602 if (tb == NULL) {
603 err = -ENOBUFS;
604 goto errout;
605 }
606
607 err = tb->tb_insert(tb, &cfg);
608errout:
609 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Thomas Graf63f34442007-03-22 11:55:17 -0700612static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Denis V. Lunevb8542722007-12-01 00:21:31 +1100614 struct net *net = skb->sk->sk_net;
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700615 unsigned int h, s_h;
616 unsigned int e = 0, s_e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 struct fib_table *tb;
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700618 struct hlist_node *node;
619 int dumped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Denis V. Lunevb8542722007-12-01 00:21:31 +1100621 if (net != &init_net)
622 return 0;
623
Thomas Grafbe403ea2006-08-17 18:15:17 -0700624 if (nlmsg_len(cb->nlh) >= sizeof(struct rtmsg) &&
625 ((struct rtmsg *) nlmsg_data(cb->nlh))->rtm_flags & RTM_F_CLONED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return ip_rt_dump(skb, cb);
627
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700628 s_h = cb->args[0];
629 s_e = cb->args[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700631 for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
632 e = 0;
633 hlist_for_each_entry(tb, node, &fib_table_hash[h], tb_hlist) {
634 if (e < s_e)
635 goto next;
636 if (dumped)
637 memset(&cb->args[2], 0, sizeof(cb->args) -
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900638 2 * sizeof(cb->args[0]));
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700639 if (tb->tb_dump(tb, skb, cb) < 0)
640 goto out;
641 dumped = 1;
642next:
643 e++;
644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700646out:
647 cb->args[1] = e;
648 cb->args[0] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 return skb->len;
651}
652
653/* Prepare and feed intra-kernel routing request.
654 Really, it should be netlink message, but :-( netlink
655 can be not configured, so that we feed it directly
656 to fib engine. It is legal, because all events occur
657 only when netlink is already locked.
658 */
659
Al Viro81f7bf62006-09-27 18:40:00 -0700660static void fib_magic(int cmd, int type, __be32 dst, int dst_len, struct in_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Thomas Graf4e902c52006-08-17 18:14:52 -0700662 struct fib_table *tb;
663 struct fib_config cfg = {
664 .fc_protocol = RTPROT_KERNEL,
665 .fc_type = type,
666 .fc_dst = dst,
667 .fc_dst_len = dst_len,
668 .fc_prefsrc = ifa->ifa_local,
669 .fc_oif = ifa->ifa_dev->dev->ifindex,
670 .fc_nlflags = NLM_F_CREATE | NLM_F_APPEND,
671 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 if (type == RTN_UNICAST)
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800674 tb = fib_new_table(&init_net, RT_TABLE_MAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 else
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800676 tb = fib_new_table(&init_net, RT_TABLE_LOCAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 if (tb == NULL)
679 return;
680
Thomas Graf4e902c52006-08-17 18:14:52 -0700681 cfg.fc_table = tb->tb_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Thomas Graf4e902c52006-08-17 18:14:52 -0700683 if (type != RTN_LOCAL)
684 cfg.fc_scope = RT_SCOPE_LINK;
685 else
686 cfg.fc_scope = RT_SCOPE_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 if (cmd == RTM_NEWROUTE)
Thomas Graf4e902c52006-08-17 18:14:52 -0700689 tb->tb_insert(tb, &cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 else
Thomas Graf4e902c52006-08-17 18:14:52 -0700691 tb->tb_delete(tb, &cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
Jamal Hadi Salim0ff60a42005-11-22 14:47:37 -0800694void fib_add_ifaddr(struct in_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 struct in_device *in_dev = ifa->ifa_dev;
697 struct net_device *dev = in_dev->dev;
698 struct in_ifaddr *prim = ifa;
Al Viroa144ea42006-09-28 18:00:55 -0700699 __be32 mask = ifa->ifa_mask;
700 __be32 addr = ifa->ifa_local;
701 __be32 prefix = ifa->ifa_address&mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 if (ifa->ifa_flags&IFA_F_SECONDARY) {
704 prim = inet_ifa_byprefix(in_dev, prefix, mask);
705 if (prim == NULL) {
706 printk(KERN_DEBUG "fib_add_ifaddr: bug: prim == NULL\n");
707 return;
708 }
709 }
710
711 fib_magic(RTM_NEWROUTE, RTN_LOCAL, addr, 32, prim);
712
713 if (!(dev->flags&IFF_UP))
714 return;
715
716 /* Add broadcast address, if it is explicitly assigned. */
Al Viroa144ea42006-09-28 18:00:55 -0700717 if (ifa->ifa_broadcast && ifa->ifa_broadcast != htonl(0xFFFFFFFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
719
Joe Perchesf97c1e02007-12-16 13:45:43 -0800720 if (!ipv4_is_zeronet(prefix) && !(ifa->ifa_flags&IFA_F_SECONDARY) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 (prefix != addr || ifa->ifa_prefixlen < 32)) {
722 fib_magic(RTM_NEWROUTE, dev->flags&IFF_LOOPBACK ? RTN_LOCAL :
723 RTN_UNICAST, prefix, ifa->ifa_prefixlen, prim);
724
725 /* Add network specific broadcasts, when it takes a sense */
726 if (ifa->ifa_prefixlen < 31) {
727 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix, 32, prim);
728 fib_magic(RTM_NEWROUTE, RTN_BROADCAST, prefix|~mask, 32, prim);
729 }
730 }
731}
732
733static void fib_del_ifaddr(struct in_ifaddr *ifa)
734{
735 struct in_device *in_dev = ifa->ifa_dev;
736 struct net_device *dev = in_dev->dev;
737 struct in_ifaddr *ifa1;
738 struct in_ifaddr *prim = ifa;
Al Viroa144ea42006-09-28 18:00:55 -0700739 __be32 brd = ifa->ifa_address|~ifa->ifa_mask;
740 __be32 any = ifa->ifa_address&ifa->ifa_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741#define LOCAL_OK 1
742#define BRD_OK 2
743#define BRD0_OK 4
744#define BRD1_OK 8
745 unsigned ok = 0;
746
747 if (!(ifa->ifa_flags&IFA_F_SECONDARY))
748 fib_magic(RTM_DELROUTE, dev->flags&IFF_LOOPBACK ? RTN_LOCAL :
749 RTN_UNICAST, any, ifa->ifa_prefixlen, prim);
750 else {
751 prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
752 if (prim == NULL) {
753 printk(KERN_DEBUG "fib_del_ifaddr: bug: prim == NULL\n");
754 return;
755 }
756 }
757
758 /* Deletion is more complicated than add.
759 We should take care of not to delete too much :-)
760
761 Scan address list to be sure that addresses are really gone.
762 */
763
764 for (ifa1 = in_dev->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
765 if (ifa->ifa_local == ifa1->ifa_local)
766 ok |= LOCAL_OK;
767 if (ifa->ifa_broadcast == ifa1->ifa_broadcast)
768 ok |= BRD_OK;
769 if (brd == ifa1->ifa_broadcast)
770 ok |= BRD1_OK;
771 if (any == ifa1->ifa_broadcast)
772 ok |= BRD0_OK;
773 }
774
775 if (!(ok&BRD_OK))
776 fib_magic(RTM_DELROUTE, RTN_BROADCAST, ifa->ifa_broadcast, 32, prim);
777 if (!(ok&BRD1_OK))
778 fib_magic(RTM_DELROUTE, RTN_BROADCAST, brd, 32, prim);
779 if (!(ok&BRD0_OK))
780 fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32, prim);
781 if (!(ok&LOCAL_OK)) {
782 fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim);
783
784 /* Check, that this local address finally disappeared. */
785 if (inet_addr_type(ifa->ifa_local) != RTN_LOCAL) {
786 /* And the last, but not the least thing.
787 We must flush stray FIB entries.
788
789 First of all, we scan fib_info list searching
790 for stray nexthop entries, then ignite fib_flush.
791 */
792 if (fib_sync_down(ifa->ifa_local, NULL, 0))
793 fib_flush();
794 }
795 }
796#undef LOCAL_OK
797#undef BRD_OK
798#undef BRD0_OK
799#undef BRD1_OK
800}
801
Robert Olsson246955f2005-06-20 13:36:39 -0700802static void nl_fib_lookup(struct fib_result_nl *frn, struct fib_table *tb )
803{
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900804
Robert Olsson246955f2005-06-20 13:36:39 -0700805 struct fib_result res;
Thomas Graf5f3008932006-11-09 15:21:41 -0800806 struct flowi fl = { .mark = frn->fl_mark,
Thomas Graf47dcf0c2006-11-09 15:20:38 -0800807 .nl_u = { .ip4_u = { .daddr = frn->fl_addr,
Robert Olsson246955f2005-06-20 13:36:39 -0700808 .tos = frn->fl_tos,
809 .scope = frn->fl_scope } } };
Alexey Kuznetsov1194ed02007-04-25 13:07:28 -0700810
Sergey Vlasov912a41a2007-04-27 02:17:19 -0700811#ifdef CONFIG_IP_MULTIPLE_TABLES
812 res.r = NULL;
813#endif
814
Alexey Kuznetsov1194ed02007-04-25 13:07:28 -0700815 frn->err = -ENOENT;
Robert Olsson246955f2005-06-20 13:36:39 -0700816 if (tb) {
817 local_bh_disable();
818
819 frn->tb_id = tb->tb_id;
820 frn->err = tb->tb_lookup(tb, &fl, &res);
821
822 if (!frn->err) {
823 frn->prefixlen = res.prefixlen;
824 frn->nh_sel = res.nh_sel;
825 frn->type = res.type;
826 frn->scope = res.scope;
Alexey Kuznetsov1194ed02007-04-25 13:07:28 -0700827 fib_res_put(&res);
Robert Olsson246955f2005-06-20 13:36:39 -0700828 }
829 local_bh_enable();
830 }
831}
832
David S. Miller28f7b0362007-10-10 21:32:39 -0700833static void nl_fib_input(struct sk_buff *skb)
Robert Olsson246955f2005-06-20 13:36:39 -0700834{
Robert Olsson246955f2005-06-20 13:36:39 -0700835 struct fib_result_nl *frn;
David S. Miller28f7b0362007-10-10 21:32:39 -0700836 struct nlmsghdr *nlh;
Robert Olsson246955f2005-06-20 13:36:39 -0700837 struct fib_table *tb;
David S. Miller28f7b0362007-10-10 21:32:39 -0700838 u32 pid;
Alexey Kuznetsov1194ed02007-04-25 13:07:28 -0700839
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -0700840 nlh = nlmsg_hdr(skb);
Thomas Grafea865752005-12-01 14:30:00 -0800841 if (skb->len < NLMSG_SPACE(0) || skb->len < nlh->nlmsg_len ||
Denis V. Lunevd883a032007-12-21 02:01:53 -0800842 nlh->nlmsg_len < NLMSG_LENGTH(sizeof(*frn)))
Thomas Grafea865752005-12-01 14:30:00 -0800843 return;
Denis V. Lunevd883a032007-12-21 02:01:53 -0800844
845 skb = skb_clone(skb, GFP_KERNEL);
846 if (skb == NULL)
847 return;
848 nlh = nlmsg_hdr(skb);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900849
Robert Olsson246955f2005-06-20 13:36:39 -0700850 frn = (struct fib_result_nl *) NLMSG_DATA(nlh);
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800851 tb = fib_get_table(&init_net, frn->tb_id_in);
Robert Olsson246955f2005-06-20 13:36:39 -0700852
853 nl_fib_lookup(frn, tb);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900854
Alexey Kuznetsov1194ed02007-04-25 13:07:28 -0700855 pid = NETLINK_CB(skb).pid; /* pid of sending process */
Robert Olsson246955f2005-06-20 13:36:39 -0700856 NETLINK_CB(skb).pid = 0; /* from kernel */
Patrick McHardyac6d4392005-08-14 19:29:52 -0700857 NETLINK_CB(skb).dst_group = 0; /* unicast */
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700858 netlink_unicast(fibnl, skb, pid, MSG_DONTWAIT);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900859}
Robert Olsson246955f2005-06-20 13:36:39 -0700860
Denis V. Lunev7b1a74f2008-01-10 03:22:17 -0800861static int nl_fib_lookup_init(struct net *net)
Robert Olsson246955f2005-06-20 13:36:39 -0700862{
Denis V. Lunev7b1a74f2008-01-10 03:22:17 -0800863 fibnl = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, 0,
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700864 nl_fib_input, NULL, THIS_MODULE);
Denis V. Lunev7b1a74f2008-01-10 03:22:17 -0800865 if (fibnl == NULL)
866 return -EAFNOSUPPORT;
867 return 0;
868}
869
870static void nl_fib_lookup_exit(struct net *net)
871{
872 sock_put(fibnl);
Robert Olsson246955f2005-06-20 13:36:39 -0700873}
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875static void fib_disable_ip(struct net_device *dev, int force)
876{
877 if (fib_sync_down(0, dev, force))
878 fib_flush();
879 rt_cache_flush(0);
880 arp_ifdown(dev);
881}
882
883static int fib_inetaddr_event(struct notifier_block *this, unsigned long event, void *ptr)
884{
885 struct in_ifaddr *ifa = (struct in_ifaddr*)ptr;
886
887 switch (event) {
888 case NETDEV_UP:
889 fib_add_ifaddr(ifa);
890#ifdef CONFIG_IP_ROUTE_MULTIPATH
891 fib_sync_up(ifa->ifa_dev->dev);
892#endif
893 rt_cache_flush(-1);
894 break;
895 case NETDEV_DOWN:
896 fib_del_ifaddr(ifa);
Jayachandran C9fcc2e82005-10-27 15:10:01 -0700897 if (ifa->ifa_dev->ifa_list == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 /* Last address was deleted from this interface.
899 Disable IP.
900 */
901 fib_disable_ip(ifa->ifa_dev->dev, 1);
902 } else {
903 rt_cache_flush(-1);
904 }
905 break;
906 }
907 return NOTIFY_DONE;
908}
909
910static int fib_netdev_event(struct notifier_block *this, unsigned long event, void *ptr)
911{
912 struct net_device *dev = ptr;
Herbert Xue5ed6392005-10-03 14:35:55 -0700913 struct in_device *in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200915 if (dev->nd_net != &init_net)
916 return NOTIFY_DONE;
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (event == NETDEV_UNREGISTER) {
919 fib_disable_ip(dev, 2);
920 return NOTIFY_DONE;
921 }
922
923 if (!in_dev)
924 return NOTIFY_DONE;
925
926 switch (event) {
927 case NETDEV_UP:
928 for_ifa(in_dev) {
929 fib_add_ifaddr(ifa);
930 } endfor_ifa(in_dev);
931#ifdef CONFIG_IP_ROUTE_MULTIPATH
932 fib_sync_up(dev);
933#endif
934 rt_cache_flush(-1);
935 break;
936 case NETDEV_DOWN:
937 fib_disable_ip(dev, 0);
938 break;
939 case NETDEV_CHANGEMTU:
940 case NETDEV_CHANGE:
941 rt_cache_flush(0);
942 break;
943 }
944 return NOTIFY_DONE;
945}
946
947static struct notifier_block fib_inetaddr_notifier = {
948 .notifier_call =fib_inetaddr_event,
949};
950
951static struct notifier_block fib_netdev_notifier = {
952 .notifier_call =fib_netdev_event,
953};
954
Denis V. Lunev7b1a74f2008-01-10 03:22:17 -0800955static int __net_init ip_fib_net_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700957 unsigned int i;
958
959 for (i = 0; i < FIB_TABLE_HASHSZ; i++)
960 INIT_HLIST_HEAD(&fib_table_hash[i]);
Pavel Emelyanovc3e9a352007-11-06 23:34:04 -0800961
Denis V. Lunev7b1a74f2008-01-10 03:22:17 -0800962 return fib4_rules_init(net);
963}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Denis V. Lunev7b1a74f2008-01-10 03:22:17 -0800965static void __net_exit ip_fib_net_exit(struct net *net)
966{
967 unsigned int i;
Thomas Graf63f34442007-03-22 11:55:17 -0700968
Denis V. Lunev7b1a74f2008-01-10 03:22:17 -0800969#ifdef CONFIG_IP_MULTIPLE_TABLES
970 fib4_rules_exit(net);
971#endif
972
973 for (i = 0; i < FIB_TABLE_HASHSZ; i++) {
974 struct fib_table *tb;
975 struct hlist_head *head;
976 struct hlist_node *node, *tmp;
977
978 head = &fib_table_hash[i];
979 hlist_for_each_entry_safe(tb, node, tmp, head, tb_hlist) {
980 hlist_del(node);
981 tb->tb_flush(tb);
982 kfree(tb);
983 }
984 }
985}
986
987static int __net_init fib_net_init(struct net *net)
988{
989 int error;
990
991 error = 0;
992 if (net != &init_net)
993 goto out;
994
995 error = ip_fib_net_init(net);
996 if (error < 0)
997 goto out;
998 error = nl_fib_lookup_init(net);
999 if (error < 0)
1000 goto out_nlfl;
1001 error = fib_proc_init(net);
1002 if (error < 0)
1003 goto out_proc;
1004out:
1005 return error;
1006
1007out_proc:
1008 nl_fib_lookup_exit(net);
1009out_nlfl:
1010 ip_fib_net_exit(net);
1011 goto out;
1012}
1013
1014static void __net_exit fib_net_exit(struct net *net)
1015{
1016 fib_proc_exit(net);
1017 nl_fib_lookup_exit(net);
1018 ip_fib_net_exit(net);
1019}
1020
1021static struct pernet_operations fib_net_ops = {
1022 .init = fib_net_init,
1023 .exit = fib_net_exit,
1024};
1025
1026void __init ip_fib_init(void)
1027{
Thomas Graf63f34442007-03-22 11:55:17 -07001028 rtnl_register(PF_INET, RTM_NEWROUTE, inet_rtm_newroute, NULL);
1029 rtnl_register(PF_INET, RTM_DELROUTE, inet_rtm_delroute, NULL);
1030 rtnl_register(PF_INET, RTM_GETROUTE, NULL, inet_dump_fib);
Denis V. Lunev7b1a74f2008-01-10 03:22:17 -08001031
1032 register_pernet_subsys(&fib_net_ops);
1033 register_netdevice_notifier(&fib_netdev_notifier);
1034 register_inetaddr_notifier(&fib_inetaddr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
1037EXPORT_SYMBOL(inet_addr_type);
Laszlo Attila Toth05538112007-12-04 23:28:46 -08001038EXPORT_SYMBOL(inet_dev_addr_type);
Sean Heftya1e87332006-06-17 20:37:28 -07001039EXPORT_SYMBOL(ip_dev_find);