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