blob: 7b462a39aa4da9773015670e2c6b558a9d789656 [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: policy rules.
7 *
8 * Version: $Id: fib_rules.c,v 1.17 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 * Fixes:
18 * Rani Assaf : local_rule cannot be deleted
19 * Marc Boucher : routing by fwmark
20 */
21
22#include <linux/config.h>
23#include <asm/uaccess.h>
24#include <asm/system.h>
25#include <linux/bitops.h>
26#include <linux/types.h>
27#include <linux/kernel.h>
28#include <linux/sched.h>
29#include <linux/mm.h>
30#include <linux/string.h>
31#include <linux/socket.h>
32#include <linux/sockios.h>
33#include <linux/errno.h>
34#include <linux/in.h>
35#include <linux/inet.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020036#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/netdevice.h>
38#include <linux/if_arp.h>
39#include <linux/proc_fs.h>
40#include <linux/skbuff.h>
41#include <linux/netlink.h>
42#include <linux/init.h>
Robert Olsson7b204af2006-03-20 17:18:53 -080043#include <linux/list.h>
44#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <net/ip.h>
47#include <net/protocol.h>
48#include <net/route.h>
49#include <net/tcp.h>
50#include <net/sock.h>
51#include <net/ip_fib.h>
52
53#define FRprintk(a...)
54
55struct fib_rule
56{
Robert Olsson7b204af2006-03-20 17:18:53 -080057 struct hlist_node hlist;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 atomic_t r_clntref;
59 u32 r_preference;
60 unsigned char r_table;
61 unsigned char r_action;
62 unsigned char r_dst_len;
63 unsigned char r_src_len;
64 u32 r_src;
65 u32 r_srcmask;
66 u32 r_dst;
67 u32 r_dstmask;
68 u32 r_srcmap;
69 u8 r_flags;
70 u8 r_tos;
71#ifdef CONFIG_IP_ROUTE_FWMARK
72 u32 r_fwmark;
73#endif
74 int r_ifindex;
75#ifdef CONFIG_NET_CLS_ROUTE
76 __u32 r_tclassid;
77#endif
78 char r_ifname[IFNAMSIZ];
79 int r_dead;
Robert Olsson7b204af2006-03-20 17:18:53 -080080 struct rcu_head rcu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081};
82
83static struct fib_rule default_rule = {
84 .r_clntref = ATOMIC_INIT(2),
85 .r_preference = 0x7FFF,
86 .r_table = RT_TABLE_DEFAULT,
87 .r_action = RTN_UNICAST,
88};
89
90static struct fib_rule main_rule = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 .r_clntref = ATOMIC_INIT(2),
92 .r_preference = 0x7FFE,
93 .r_table = RT_TABLE_MAIN,
94 .r_action = RTN_UNICAST,
95};
96
97static struct fib_rule local_rule = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 .r_clntref = ATOMIC_INIT(2),
99 .r_table = RT_TABLE_LOCAL,
100 .r_action = RTN_UNICAST,
101};
102
Robert Olsson7b204af2006-03-20 17:18:53 -0800103struct hlist_head fib_rules;
104
105/* writer func called from netlink -- rtnl_sem hold*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107int inet_rtm_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
108{
109 struct rtattr **rta = arg;
110 struct rtmsg *rtm = NLMSG_DATA(nlh);
Robert Olsson7b204af2006-03-20 17:18:53 -0800111 struct fib_rule *r;
112 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 int err = -ESRCH;
114
Robert Olsson7b204af2006-03-20 17:18:53 -0800115 hlist_for_each_entry(r, node, &fib_rules, hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if ((!rta[RTA_SRC-1] || memcmp(RTA_DATA(rta[RTA_SRC-1]), &r->r_src, 4) == 0) &&
117 rtm->rtm_src_len == r->r_src_len &&
118 rtm->rtm_dst_len == r->r_dst_len &&
119 (!rta[RTA_DST-1] || memcmp(RTA_DATA(rta[RTA_DST-1]), &r->r_dst, 4) == 0) &&
120 rtm->rtm_tos == r->r_tos &&
121#ifdef CONFIG_IP_ROUTE_FWMARK
122 (!rta[RTA_PROTOINFO-1] || memcmp(RTA_DATA(rta[RTA_PROTOINFO-1]), &r->r_fwmark, 4) == 0) &&
123#endif
124 (!rtm->rtm_type || rtm->rtm_type == r->r_action) &&
125 (!rta[RTA_PRIORITY-1] || memcmp(RTA_DATA(rta[RTA_PRIORITY-1]), &r->r_preference, 4) == 0) &&
126 (!rta[RTA_IIF-1] || rtattr_strcmp(rta[RTA_IIF-1], r->r_ifname) == 0) &&
127 (!rtm->rtm_table || (r && rtm->rtm_table == r->r_table))) {
128 err = -EPERM;
129 if (r == &local_rule)
130 break;
131
Robert Olsson7b204af2006-03-20 17:18:53 -0800132 hlist_del_rcu(&r->hlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 r->r_dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 fib_rule_put(r);
135 err = 0;
136 break;
137 }
138 }
139 return err;
140}
141
142/* Allocate new unique table id */
143
144static struct fib_table *fib_empty_table(void)
145{
146 int id;
147
148 for (id = 1; id <= RT_TABLE_MAX; id++)
149 if (fib_tables[id] == NULL)
150 return __fib_new_table(id);
151 return NULL;
152}
153
Robert Olsson7b204af2006-03-20 17:18:53 -0800154static inline void fib_rule_put_rcu(struct rcu_head *head)
155{
156 struct fib_rule *r = container_of(head, struct fib_rule, rcu);
157 kfree(r);
158}
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160void fib_rule_put(struct fib_rule *r)
161{
162 if (atomic_dec_and_test(&r->r_clntref)) {
163 if (r->r_dead)
Robert Olsson7b204af2006-03-20 17:18:53 -0800164 call_rcu(&r->rcu, fib_rule_put_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 else
166 printk("Freeing alive rule %p\n", r);
167 }
168}
169
Robert Olsson7b204af2006-03-20 17:18:53 -0800170/* writer func called from netlink -- rtnl_sem hold*/
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172int inet_rtm_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
173{
174 struct rtattr **rta = arg;
175 struct rtmsg *rtm = NLMSG_DATA(nlh);
Robert Olsson7b204af2006-03-20 17:18:53 -0800176 struct fib_rule *r, *new_r, *last = NULL;
177 struct hlist_node *node = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 unsigned char table_id;
179
180 if (rtm->rtm_src_len > 32 || rtm->rtm_dst_len > 32 ||
181 (rtm->rtm_tos & ~IPTOS_TOS_MASK))
182 return -EINVAL;
183
184 if (rta[RTA_IIF-1] && RTA_PAYLOAD(rta[RTA_IIF-1]) > IFNAMSIZ)
185 return -EINVAL;
186
187 table_id = rtm->rtm_table;
188 if (table_id == RT_TABLE_UNSPEC) {
189 struct fib_table *table;
190 if (rtm->rtm_type == RTN_UNICAST) {
191 if ((table = fib_empty_table()) == NULL)
192 return -ENOBUFS;
193 table_id = table->tb_id;
194 }
195 }
196
197 new_r = kmalloc(sizeof(*new_r), GFP_KERNEL);
198 if (!new_r)
199 return -ENOMEM;
200 memset(new_r, 0, sizeof(*new_r));
Robert Olsson7b204af2006-03-20 17:18:53 -0800201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (rta[RTA_SRC-1])
203 memcpy(&new_r->r_src, RTA_DATA(rta[RTA_SRC-1]), 4);
204 if (rta[RTA_DST-1])
205 memcpy(&new_r->r_dst, RTA_DATA(rta[RTA_DST-1]), 4);
206 if (rta[RTA_GATEWAY-1])
207 memcpy(&new_r->r_srcmap, RTA_DATA(rta[RTA_GATEWAY-1]), 4);
208 new_r->r_src_len = rtm->rtm_src_len;
209 new_r->r_dst_len = rtm->rtm_dst_len;
210 new_r->r_srcmask = inet_make_mask(rtm->rtm_src_len);
211 new_r->r_dstmask = inet_make_mask(rtm->rtm_dst_len);
212 new_r->r_tos = rtm->rtm_tos;
213#ifdef CONFIG_IP_ROUTE_FWMARK
214 if (rta[RTA_PROTOINFO-1])
215 memcpy(&new_r->r_fwmark, RTA_DATA(rta[RTA_PROTOINFO-1]), 4);
216#endif
217 new_r->r_action = rtm->rtm_type;
218 new_r->r_flags = rtm->rtm_flags;
219 if (rta[RTA_PRIORITY-1])
220 memcpy(&new_r->r_preference, RTA_DATA(rta[RTA_PRIORITY-1]), 4);
221 new_r->r_table = table_id;
222 if (rta[RTA_IIF-1]) {
223 struct net_device *dev;
224 rtattr_strlcpy(new_r->r_ifname, rta[RTA_IIF-1], IFNAMSIZ);
225 new_r->r_ifindex = -1;
226 dev = __dev_get_by_name(new_r->r_ifname);
227 if (dev)
228 new_r->r_ifindex = dev->ifindex;
229 }
230#ifdef CONFIG_NET_CLS_ROUTE
231 if (rta[RTA_FLOW-1])
232 memcpy(&new_r->r_tclassid, RTA_DATA(rta[RTA_FLOW-1]), 4);
233#endif
Robert Olsson7b204af2006-03-20 17:18:53 -0800234 r = container_of(fib_rules.first, struct fib_rule, hlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (!new_r->r_preference) {
Robert Olsson7b204af2006-03-20 17:18:53 -0800237 if (r && r->hlist.next != NULL) {
238 r = container_of(r->hlist.next, struct fib_rule, hlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (r->r_preference)
240 new_r->r_preference = r->r_preference - 1;
241 }
242 }
243
Robert Olsson7b204af2006-03-20 17:18:53 -0800244 hlist_for_each_entry(r, node, &fib_rules, hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (r->r_preference > new_r->r_preference)
246 break;
Robert Olsson7b204af2006-03-20 17:18:53 -0800247 last = r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 atomic_inc(&new_r->r_clntref);
Robert Olsson7b204af2006-03-20 17:18:53 -0800250
251 if (last)
252 hlist_add_after_rcu(&last->hlist, &new_r->hlist);
253 else
254 hlist_add_before_rcu(&new_r->hlist, &r->hlist);
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return 0;
257}
258
259#ifdef CONFIG_NET_CLS_ROUTE
260u32 fib_rules_tclass(struct fib_result *res)
261{
262 if (res->r)
263 return res->r->r_tclassid;
264 return 0;
265}
266#endif
267
Robert Olsson7b204af2006-03-20 17:18:53 -0800268/* callers should hold rtnl semaphore */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270static void fib_rules_detach(struct net_device *dev)
271{
Robert Olsson7b204af2006-03-20 17:18:53 -0800272 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 struct fib_rule *r;
274
Robert Olsson7b204af2006-03-20 17:18:53 -0800275 hlist_for_each_entry(r, node, &fib_rules, hlist) {
276 if (r->r_ifindex == dev->ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 r->r_ifindex = -1;
Robert Olsson7b204af2006-03-20 17:18:53 -0800278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280}
281
Robert Olsson7b204af2006-03-20 17:18:53 -0800282/* callers should hold rtnl semaphore */
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284static void fib_rules_attach(struct net_device *dev)
285{
Robert Olsson7b204af2006-03-20 17:18:53 -0800286 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 struct fib_rule *r;
288
Robert Olsson7b204af2006-03-20 17:18:53 -0800289 hlist_for_each_entry(r, node, &fib_rules, hlist) {
290 if (r->r_ifindex == -1 && strcmp(dev->name, r->r_ifname) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 r->r_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293}
294
295int fib_lookup(const struct flowi *flp, struct fib_result *res)
296{
297 int err;
298 struct fib_rule *r, *policy;
299 struct fib_table *tb;
Robert Olsson7b204af2006-03-20 17:18:53 -0800300 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 u32 daddr = flp->fl4_dst;
303 u32 saddr = flp->fl4_src;
304
305FRprintk("Lookup: %u.%u.%u.%u <- %u.%u.%u.%u ",
306 NIPQUAD(flp->fl4_dst), NIPQUAD(flp->fl4_src));
Robert Olsson7b204af2006-03-20 17:18:53 -0800307
308 rcu_read_lock();
309
310 hlist_for_each_entry_rcu(r, node, &fib_rules, hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (((saddr^r->r_src) & r->r_srcmask) ||
312 ((daddr^r->r_dst) & r->r_dstmask) ||
313 (r->r_tos && r->r_tos != flp->fl4_tos) ||
314#ifdef CONFIG_IP_ROUTE_FWMARK
315 (r->r_fwmark && r->r_fwmark != flp->fl4_fwmark) ||
316#endif
317 (r->r_ifindex && r->r_ifindex != flp->iif))
318 continue;
319
320FRprintk("tb %d r %d ", r->r_table, r->r_action);
321 switch (r->r_action) {
322 case RTN_UNICAST:
323 policy = r;
324 break;
325 case RTN_UNREACHABLE:
Robert Olsson7b204af2006-03-20 17:18:53 -0800326 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return -ENETUNREACH;
328 default:
329 case RTN_BLACKHOLE:
Robert Olsson7b204af2006-03-20 17:18:53 -0800330 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return -EINVAL;
332 case RTN_PROHIBIT:
Robert Olsson7b204af2006-03-20 17:18:53 -0800333 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return -EACCES;
335 }
336
337 if ((tb = fib_get_table(r->r_table)) == NULL)
338 continue;
339 err = tb->tb_lookup(tb, flp, res);
340 if (err == 0) {
341 res->r = policy;
342 if (policy)
343 atomic_inc(&policy->r_clntref);
Robert Olsson7b204af2006-03-20 17:18:53 -0800344 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return 0;
346 }
347 if (err < 0 && err != -EAGAIN) {
Robert Olsson7b204af2006-03-20 17:18:53 -0800348 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return err;
350 }
351 }
352FRprintk("FAILURE\n");
Robert Olsson7b204af2006-03-20 17:18:53 -0800353 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return -ENETUNREACH;
355}
356
357void fib_select_default(const struct flowi *flp, struct fib_result *res)
358{
359 if (res->r && res->r->r_action == RTN_UNICAST &&
360 FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) {
361 struct fib_table *tb;
362 if ((tb = fib_get_table(res->r->r_table)) != NULL)
363 tb->tb_select_default(tb, flp, res);
364 }
365}
366
367static int fib_rules_event(struct notifier_block *this, unsigned long event, void *ptr)
368{
369 struct net_device *dev = ptr;
370
371 if (event == NETDEV_UNREGISTER)
372 fib_rules_detach(dev);
373 else if (event == NETDEV_REGISTER)
374 fib_rules_attach(dev);
375 return NOTIFY_DONE;
376}
377
378
379static struct notifier_block fib_rules_notifier = {
380 .notifier_call =fib_rules_event,
381};
382
383static __inline__ int inet_fill_rule(struct sk_buff *skb,
384 struct fib_rule *r,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -0700385 struct netlink_callback *cb,
386 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 struct rtmsg *rtm;
389 struct nlmsghdr *nlh;
390 unsigned char *b = skb->tail;
391
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -0700392 nlh = NLMSG_NEW_ANSWER(skb, cb, RTM_NEWRULE, sizeof(*rtm), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 rtm = NLMSG_DATA(nlh);
394 rtm->rtm_family = AF_INET;
395 rtm->rtm_dst_len = r->r_dst_len;
396 rtm->rtm_src_len = r->r_src_len;
397 rtm->rtm_tos = r->r_tos;
398#ifdef CONFIG_IP_ROUTE_FWMARK
399 if (r->r_fwmark)
400 RTA_PUT(skb, RTA_PROTOINFO, 4, &r->r_fwmark);
401#endif
402 rtm->rtm_table = r->r_table;
403 rtm->rtm_protocol = 0;
404 rtm->rtm_scope = 0;
405 rtm->rtm_type = r->r_action;
406 rtm->rtm_flags = r->r_flags;
407
408 if (r->r_dst_len)
409 RTA_PUT(skb, RTA_DST, 4, &r->r_dst);
410 if (r->r_src_len)
411 RTA_PUT(skb, RTA_SRC, 4, &r->r_src);
412 if (r->r_ifname[0])
413 RTA_PUT(skb, RTA_IIF, IFNAMSIZ, &r->r_ifname);
414 if (r->r_preference)
415 RTA_PUT(skb, RTA_PRIORITY, 4, &r->r_preference);
416 if (r->r_srcmap)
417 RTA_PUT(skb, RTA_GATEWAY, 4, &r->r_srcmap);
418#ifdef CONFIG_NET_CLS_ROUTE
419 if (r->r_tclassid)
420 RTA_PUT(skb, RTA_FLOW, 4, &r->r_tclassid);
421#endif
422 nlh->nlmsg_len = skb->tail - b;
423 return skb->len;
424
425nlmsg_failure:
426rtattr_failure:
427 skb_trim(skb, b - skb->data);
428 return -1;
429}
430
Robert Olsson7b204af2006-03-20 17:18:53 -0800431/* callers should hold rtnl semaphore */
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433int inet_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
434{
Robert Olsson7b204af2006-03-20 17:18:53 -0800435 int idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 int s_idx = cb->args[0];
437 struct fib_rule *r;
Robert Olsson7b204af2006-03-20 17:18:53 -0800438 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Robert Olsson7b204af2006-03-20 17:18:53 -0800440 rcu_read_lock();
441 hlist_for_each_entry(r, node, &fib_rules, hlist) {
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (idx < s_idx)
444 continue;
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -0700445 if (inet_fill_rule(skb, r, cb, NLM_F_MULTI) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 break;
Robert Olsson7b204af2006-03-20 17:18:53 -0800447 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
Robert Olsson7b204af2006-03-20 17:18:53 -0800449 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 cb->args[0] = idx;
451
452 return skb->len;
453}
454
455void __init fib_rules_init(void)
456{
Robert Olsson7b204af2006-03-20 17:18:53 -0800457 INIT_HLIST_HEAD(&fib_rules);
458 hlist_add_head(&local_rule.hlist, &fib_rules);
459 hlist_add_after(&local_rule.hlist, &main_rule.hlist);
460 hlist_add_after(&main_rule.hlist, &default_rule.hlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 register_netdevice_notifier(&fib_rules_notifier);
462}