Import 2.3.13
[davej-history.git] / net / ipv4 / fib_rules.c
blob97074198eecf77258010c14fcafce72743eb48ab
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.
6 * IPv4 Forwarding Information Base: policy rules.
8 * Version: $Id: fib_rules.c,v 1.11 1999/06/09 10:10:47 davem Exp $
10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
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.
17 * Fixes:
18 * Rani Assaf : local_rule cannot be deleted
19 * Marc Boucher : routing by fwmark
22 #include <linux/config.h>
23 #include <asm/uaccess.h>
24 #include <asm/system.h>
25 #include <asm/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>
36 #include <linux/netdevice.h>
37 #include <linux/if_arp.h>
38 #include <linux/proc_fs.h>
39 #include <linux/skbuff.h>
40 #include <linux/netlink.h>
41 #include <linux/init.h>
43 #include <net/ip.h>
44 #include <net/protocol.h>
45 #include <net/route.h>
46 #include <net/tcp.h>
47 #include <net/sock.h>
48 #include <net/ip_fib.h>
50 #define FRprintk(a...)
52 struct fib_rule
54 struct fib_rule *r_next;
55 u32 r_preference;
56 unsigned char r_table;
57 unsigned char r_action;
58 unsigned char r_dst_len;
59 unsigned char r_src_len;
60 u32 r_src;
61 u32 r_srcmask;
62 u32 r_dst;
63 u32 r_dstmask;
64 u32 r_srcmap;
65 u8 r_flags;
66 u8 r_tos;
67 #ifdef CONFIG_IP_ROUTE_FWMARK
68 u32 r_fwmark;
69 #endif
70 int r_ifindex;
71 #ifdef CONFIG_NET_CLS_ROUTE
72 __u32 r_tclassid;
73 #endif
74 char r_ifname[IFNAMSIZ];
77 static struct fib_rule default_rule = { NULL, 0x7FFF, RT_TABLE_DEFAULT, RTN_UNICAST, };
78 static struct fib_rule main_rule = { &default_rule, 0x7FFE, RT_TABLE_MAIN, RTN_UNICAST, };
79 static struct fib_rule local_rule = { &main_rule, 0, RT_TABLE_LOCAL, RTN_UNICAST, };
81 static struct fib_rule *fib_rules = &local_rule;
82 static rwlock_t fib_rules_lock = RW_LOCK_UNLOCKED;
84 int inet_rtm_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
86 struct rtattr **rta = arg;
87 struct rtmsg *rtm = NLMSG_DATA(nlh);
88 struct fib_rule *r, **rp;
89 int err = -ESRCH;
91 for (rp=&fib_rules; (r=*rp) != NULL; rp=&r->r_next) {
92 if ((!rta[RTA_SRC-1] || memcmp(RTA_DATA(rta[RTA_SRC-1]), &r->r_src, 4) == 0) &&
93 rtm->rtm_src_len == r->r_src_len &&
94 rtm->rtm_dst_len == r->r_dst_len &&
95 (!rta[RTA_DST-1] || memcmp(RTA_DATA(rta[RTA_DST-1]), &r->r_dst, 4) == 0) &&
96 rtm->rtm_tos == r->r_tos &&
97 #ifdef CONFIG_IP_ROUTE_FWMARK
98 (!rta[RTA_PROTOINFO-1] || memcmp(RTA_DATA(rta[RTA_PROTOINFO-1]), &r->r_fwmark, 4) == 0) &&
99 #endif
100 (!rtm->rtm_type || rtm->rtm_type == r->r_action) &&
101 (!rta[RTA_PRIORITY-1] || memcmp(RTA_DATA(rta[RTA_PRIORITY-1]), &r->r_preference, 4) == 0) &&
102 (!rta[RTA_IIF-1] || strcmp(RTA_DATA(rta[RTA_IIF-1]), r->r_ifname) == 0) &&
103 (!rtm->rtm_table || (r && rtm->rtm_table == r->r_table))) {
104 err = -EPERM;
105 if (r == &local_rule)
106 break;
108 write_lock_bh(&fib_rules_lock);
109 *rp = r->r_next;
110 write_unlock_bh(&fib_rules_lock);
111 if (r != &default_rule && r != &main_rule)
112 kfree(r);
113 err = 0;
114 break;
117 return err;
120 /* Allocate new unique table id */
122 static struct fib_table *fib_empty_table(void)
124 int id;
126 for (id = 1; id <= RT_TABLE_MAX; id++)
127 if (fib_tables[id] == NULL)
128 return __fib_new_table(id);
129 return NULL;
133 int inet_rtm_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
135 struct rtattr **rta = arg;
136 struct rtmsg *rtm = NLMSG_DATA(nlh);
137 struct fib_rule *r, *new_r, **rp;
138 unsigned char table_id;
140 if (rtm->rtm_src_len > 32 || rtm->rtm_dst_len > 32 ||
141 (rtm->rtm_tos & ~IPTOS_TOS_MASK))
142 return -EINVAL;
144 if (rta[RTA_IIF-1] && RTA_PAYLOAD(rta[RTA_IIF-1]) > IFNAMSIZ)
145 return -EINVAL;
147 table_id = rtm->rtm_table;
148 if (table_id == RT_TABLE_UNSPEC) {
149 struct fib_table *table;
150 if (rtm->rtm_type == RTN_UNICAST || rtm->rtm_type == RTN_NAT) {
151 if ((table = fib_empty_table()) == NULL)
152 return -ENOBUFS;
153 table_id = table->tb_id;
157 new_r = kmalloc(sizeof(*new_r), GFP_KERNEL);
158 if (!new_r)
159 return -ENOMEM;
160 memset(new_r, 0, sizeof(*new_r));
161 if (rta[RTA_SRC-1])
162 memcpy(&new_r->r_src, RTA_DATA(rta[RTA_SRC-1]), 4);
163 if (rta[RTA_DST-1])
164 memcpy(&new_r->r_dst, RTA_DATA(rta[RTA_DST-1]), 4);
165 if (rta[RTA_GATEWAY-1])
166 memcpy(&new_r->r_srcmap, RTA_DATA(rta[RTA_GATEWAY-1]), 4);
167 new_r->r_src_len = rtm->rtm_src_len;
168 new_r->r_dst_len = rtm->rtm_dst_len;
169 new_r->r_srcmask = inet_make_mask(rtm->rtm_src_len);
170 new_r->r_dstmask = inet_make_mask(rtm->rtm_dst_len);
171 new_r->r_tos = rtm->rtm_tos;
172 #ifdef CONFIG_IP_ROUTE_FWMARK
173 if (rta[RTA_PROTOINFO-1])
174 memcpy(&new_r->r_fwmark, RTA_DATA(rta[RTA_PROTOINFO-1]), 4);
175 #endif
176 new_r->r_action = rtm->rtm_type;
177 new_r->r_flags = rtm->rtm_flags;
178 if (rta[RTA_PRIORITY-1])
179 memcpy(&new_r->r_preference, RTA_DATA(rta[RTA_PRIORITY-1]), 4);
180 new_r->r_table = table_id;
181 if (rta[RTA_IIF-1]) {
182 struct device *dev;
183 memcpy(new_r->r_ifname, RTA_DATA(rta[RTA_IIF-1]), IFNAMSIZ);
184 new_r->r_ifname[IFNAMSIZ-1] = 0;
185 new_r->r_ifindex = -1;
186 dev = dev_get(new_r->r_ifname);
187 if (dev)
188 new_r->r_ifindex = dev->ifindex;
190 #ifdef CONFIG_NET_CLS_ROUTE
191 if (rta[RTA_FLOW-1])
192 memcpy(&new_r->r_tclassid, RTA_DATA(rta[RTA_FLOW-1]), 4);
193 #endif
195 rp = &fib_rules;
196 if (!new_r->r_preference) {
197 r = fib_rules;
198 if (r && (r = r->r_next) != NULL) {
199 rp = &fib_rules->r_next;
200 if (r->r_preference)
201 new_r->r_preference = r->r_preference - 1;
205 while ( (r = *rp) != NULL ) {
206 if (r->r_preference > new_r->r_preference)
207 break;
208 rp = &r->r_next;
211 new_r->r_next = r;
212 write_lock_bh(&fib_rules_lock);
213 *rp = new_r;
214 write_unlock_bh(&fib_rules_lock);
215 return 0;
218 u32 fib_rules_map_destination(u32 daddr, struct fib_result *res)
220 u32 mask = inet_make_mask(res->prefixlen);
221 return (daddr&~mask)|res->fi->fib_nh->nh_gw;
224 u32 fib_rules_policy(u32 saddr, struct fib_result *res, unsigned *flags)
226 struct fib_rule *r = res->r;
228 if (r->r_action == RTN_NAT) {
229 int addrtype = inet_addr_type(r->r_srcmap);
231 if (addrtype == RTN_NAT) {
232 /* Packet is from translated source; remember it */
233 saddr = (saddr&~r->r_srcmask)|r->r_srcmap;
234 *flags |= RTCF_SNAT;
235 } else if (addrtype == RTN_LOCAL || r->r_srcmap == 0) {
236 /* Packet is from masqueraded source; remember it */
237 saddr = r->r_srcmap;
238 *flags |= RTCF_MASQ;
241 return saddr;
244 #ifdef CONFIG_NET_CLS_ROUTE
245 u32 fib_rules_tclass(struct fib_result *res)
247 if (res->r)
248 return res->r->r_tclassid;
249 return 0;
251 #endif
254 static void fib_rules_detach(struct device *dev)
256 struct fib_rule *r;
258 for (r=fib_rules; r; r=r->r_next) {
259 if (r->r_ifindex == dev->ifindex) {
260 write_lock_bh(&fib_rules_lock);
261 r->r_ifindex = -1;
262 write_unlock_bh(&fib_rules_lock);
267 static void fib_rules_attach(struct device *dev)
269 struct fib_rule *r;
271 for (r=fib_rules; r; r=r->r_next) {
272 if (r->r_ifindex == -1 && strcmp(dev->name, r->r_ifname) == 0) {
273 write_lock_bh(&fib_rules_lock);
274 r->r_ifindex = dev->ifindex;
275 write_unlock_bh(&fib_rules_lock);
280 int fib_lookup(const struct rt_key *key, struct fib_result *res)
282 int err;
283 struct fib_rule *r, *policy;
284 struct fib_table *tb;
286 u32 daddr = key->dst;
287 u32 saddr = key->src;
289 FRprintk("Lookup: %08x <- %08x ", key->dst, key->src);
290 read_lock(&fib_rules_lock);
291 for (r = fib_rules; r; r=r->r_next) {
292 if (((saddr^r->r_src) & r->r_srcmask) ||
293 ((daddr^r->r_dst) & r->r_dstmask) ||
294 #ifdef CONFIG_IP_ROUTE_TOS
295 (r->r_tos && r->r_tos != key->tos) ||
296 #endif
297 #ifdef CONFIG_IP_ROUTE_FWMARK
298 (r->r_fwmark && r->r_fwmark != key->fwmark) ||
299 #endif
300 (r->r_ifindex && r->r_ifindex != key->iif))
301 continue;
303 FRprintk("tb %d r %d ", r->r_table, r->r_action);
304 switch (r->r_action) {
305 case RTN_UNICAST:
306 case RTN_NAT:
307 policy = r;
308 break;
309 case RTN_UNREACHABLE:
310 read_unlock(&fib_rules_lock);
311 return -ENETUNREACH;
312 default:
313 case RTN_BLACKHOLE:
314 read_unlock(&fib_rules_lock);
315 return -EINVAL;
316 case RTN_PROHIBIT:
317 read_unlock(&fib_rules_lock);
318 return -EACCES;
321 if ((tb = fib_get_table(r->r_table)) == NULL)
322 continue;
323 err = tb->tb_lookup(tb, key, res);
324 if (err == 0) {
325 FRprintk("ok\n");
326 res->r = policy;
327 read_unlock(&fib_rules_lock);
328 return 0;
330 if (err < 0 && err != -EAGAIN) {
331 read_unlock(&fib_rules_lock);
332 return err;
335 FRprintk("FAILURE\n");
336 read_unlock(&fib_rules_lock);
337 return -ENETUNREACH;
340 void fib_select_default(const struct rt_key *key, struct fib_result *res)
342 if (res->r && res->r->r_action == RTN_UNICAST &&
343 FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) {
344 struct fib_table *tb;
345 if ((tb = fib_get_table(res->r->r_table)) != NULL)
346 tb->tb_select_default(tb, key, res);
350 static int fib_rules_event(struct notifier_block *this, unsigned long event, void *ptr)
352 struct device *dev = ptr;
354 if (event == NETDEV_UNREGISTER)
355 fib_rules_detach(dev);
356 else if (event == NETDEV_REGISTER)
357 fib_rules_attach(dev);
358 return NOTIFY_DONE;
362 struct notifier_block fib_rules_notifier = {
363 fib_rules_event,
364 NULL,
368 #ifdef CONFIG_RTNETLINK
370 extern __inline__ int inet_fill_rule(struct sk_buff *skb,
371 struct fib_rule *r,
372 struct netlink_callback *cb)
374 struct rtmsg *rtm;
375 struct nlmsghdr *nlh;
376 unsigned char *b = skb->tail;
378 nlh = NLMSG_PUT(skb, NETLINK_CREDS(cb->skb)->pid, cb->nlh->nlmsg_seq, RTM_NEWRULE, sizeof(*rtm));
379 rtm = NLMSG_DATA(nlh);
380 rtm->rtm_family = AF_INET;
381 rtm->rtm_dst_len = r->r_dst_len;
382 rtm->rtm_src_len = r->r_src_len;
383 rtm->rtm_tos = r->r_tos;
384 #ifdef CONFIG_IP_ROUTE_FWMARK
385 if (r->r_fwmark)
386 RTA_PUT(skb, RTA_PROTOINFO, 4, &r->r_fwmark);
387 #endif
388 rtm->rtm_table = r->r_table;
389 rtm->rtm_protocol = 0;
390 rtm->rtm_scope = 0;
391 rtm->rtm_type = r->r_action;
392 rtm->rtm_flags = r->r_flags;
394 if (r->r_dst_len)
395 RTA_PUT(skb, RTA_DST, 4, &r->r_dst);
396 if (r->r_src_len)
397 RTA_PUT(skb, RTA_SRC, 4, &r->r_src);
398 if (r->r_ifname[0])
399 RTA_PUT(skb, RTA_IIF, IFNAMSIZ, &r->r_ifname);
400 if (r->r_preference)
401 RTA_PUT(skb, RTA_PRIORITY, 4, &r->r_preference);
402 if (r->r_srcmap)
403 RTA_PUT(skb, RTA_GATEWAY, 4, &r->r_srcmap);
404 #ifdef CONFIG_NET_CLS_ROUTE
405 if (r->r_tclassid)
406 RTA_PUT(skb, RTA_FLOW, 4, &r->r_tclassid);
407 #endif
408 nlh->nlmsg_len = skb->tail - b;
409 return skb->len;
411 nlmsg_failure:
412 rtattr_failure:
413 skb_put(skb, b - skb->tail);
414 return -1;
417 int inet_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
419 int idx;
420 int s_idx = cb->args[0];
421 struct fib_rule *r;
423 read_lock(&fib_rules_lock);
424 for (r=fib_rules, idx=0; r; r = r->r_next, idx++) {
425 if (idx < s_idx)
426 continue;
427 if (inet_fill_rule(skb, r, cb) < 0)
428 break;
430 read_unlock(&fib_rules_lock);
431 cb->args[0] = idx;
433 return skb->len;
436 #endif /* CONFIG_RTNETLINK */
438 __initfunc(void fib_rules_init(void))
440 register_netdevice_notifier(&fib_rules_notifier);