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: policy rules.
7 *
8 * Version: $Id: fib_rules.c,v 1.15 2000/04/15 01:48:10 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 <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>
42
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>
49
50 #define FRprintk(a...)
51
52 struct fib_rule
53 {
54 struct fib_rule *r_next;
55 atomic_t r_clntref;
56 u32 r_preference;
57 unsigned char r_table;
58 unsigned char r_action;
59 unsigned char r_dst_len;
60 unsigned char r_src_len;
61 u32 r_src;
62 u32 r_srcmask;
63 u32 r_dst;
64 u32 r_dstmask;
65 u32 r_srcmap;
66 u8 r_flags;
67 u8 r_tos;
68 #ifdef CONFIG_IP_ROUTE_FWMARK
69 u32 r_fwmark;
70 #endif
71 int r_ifindex;
72 #ifdef CONFIG_NET_CLS_ROUTE
73 __u32 r_tclassid;
74 #endif
75 char r_ifname[IFNAMSIZ];
76 int r_dead;
77 };
78
79 static struct fib_rule default_rule = { NULL, ATOMIC_INIT(2), 0x7FFF, RT_TABLE_DEFAULT, RTN_UNICAST, };
80 static struct fib_rule main_rule = { &default_rule, ATOMIC_INIT(2), 0x7FFE, RT_TABLE_MAIN, RTN_UNICAST, };
81 static struct fib_rule local_rule = { &main_rule, ATOMIC_INIT(2), 0, RT_TABLE_LOCAL, RTN_UNICAST, };
82
83 static struct fib_rule *fib_rules = &local_rule;
84 static rwlock_t fib_rules_lock = RW_LOCK_UNLOCKED;
85
86 int inet_rtm_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
87 {
88 struct rtattr **rta = arg;
89 struct rtmsg *rtm = NLMSG_DATA(nlh);
90 struct fib_rule *r, **rp;
91 int err = -ESRCH;
92
93 for (rp=&fib_rules; (r=*rp) != NULL; rp=&r->r_next) {
94 if ((!rta[RTA_SRC-1] || memcmp(RTA_DATA(rta[RTA_SRC-1]), &r->r_src, 4) == 0) &&
95 rtm->rtm_src_len == r->r_src_len &&
96 rtm->rtm_dst_len == r->r_dst_len &&
97 (!rta[RTA_DST-1] || memcmp(RTA_DATA(rta[RTA_DST-1]), &r->r_dst, 4) == 0) &&
98 rtm->rtm_tos == r->r_tos &&
99 #ifdef CONFIG_IP_ROUTE_FWMARK
100 (!rta[RTA_PROTOINFO-1] || memcmp(RTA_DATA(rta[RTA_PROTOINFO-1]), &r->r_fwmark, 4) == 0) &&
101 #endif
102 (!rtm->rtm_type || rtm->rtm_type == r->r_action) &&
103 (!rta[RTA_PRIORITY-1] || memcmp(RTA_DATA(rta[RTA_PRIORITY-1]), &r->r_preference, 4) == 0) &&
104 (!rta[RTA_IIF-1] || strcmp(RTA_DATA(rta[RTA_IIF-1]), r->r_ifname) == 0) &&
105 (!rtm->rtm_table || (r && rtm->rtm_table == r->r_table))) {
106 err = -EPERM;
107 if (r == &local_rule)
108 break;
109
110 write_lock_bh(&fib_rules_lock);
111 *rp = r->r_next;
112 r->r_dead = 1;
113 write_unlock_bh(&fib_rules_lock);
114 fib_rule_put(r);
115 err = 0;
116 break;
117 }
118 }
119 return err;
120 }
121
122 /* Allocate new unique table id */
123
124 static struct fib_table *fib_empty_table(void)
125 {
126 int id;
127
128 for (id = 1; id <= RT_TABLE_MAX; id++)
129 if (fib_tables[id] == NULL)
130 return __fib_new_table(id);
131 return NULL;
132 }
133
134 void fib_rule_put(struct fib_rule *r)
135 {
136 if (atomic_dec_and_test(&r->r_clntref)) {
137 if (r->r_dead)
138 kfree(r);
139 else
140 printk("Freeing alive rule %p\n", r);
141 }
142 }
143
144 int inet_rtm_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
145 {
146 struct rtattr **rta = arg;
147 struct rtmsg *rtm = NLMSG_DATA(nlh);
148 struct fib_rule *r, *new_r, **rp;
149 unsigned char table_id;
150
151 if (rtm->rtm_src_len > 32 || rtm->rtm_dst_len > 32 ||
152 (rtm->rtm_tos & ~IPTOS_TOS_MASK))
153 return -EINVAL;
154
155 if (rta[RTA_IIF-1] && RTA_PAYLOAD(rta[RTA_IIF-1]) > IFNAMSIZ)
156 return -EINVAL;
157
158 table_id = rtm->rtm_table;
159 if (table_id == RT_TABLE_UNSPEC) {
160 struct fib_table *table;
161 if (rtm->rtm_type == RTN_UNICAST || rtm->rtm_type == RTN_NAT) {
162 if ((table = fib_empty_table()) == NULL)
163 return -ENOBUFS;
164 table_id = table->tb_id;
165 }
166 }
167
168 new_r = kmalloc(sizeof(*new_r), GFP_KERNEL);
169 if (!new_r)
170 return -ENOMEM;
171 memset(new_r, 0, sizeof(*new_r));
172 if (rta[RTA_SRC-1])
173 memcpy(&new_r->r_src, RTA_DATA(rta[RTA_SRC-1]), 4);
174 if (rta[RTA_DST-1])
175 memcpy(&new_r->r_dst, RTA_DATA(rta[RTA_DST-1]), 4);
176 if (rta[RTA_GATEWAY-1])
177 memcpy(&new_r->r_srcmap, RTA_DATA(rta[RTA_GATEWAY-1]), 4);
178 new_r->r_src_len = rtm->rtm_src_len;
179 new_r->r_dst_len = rtm->rtm_dst_len;
180 new_r->r_srcmask = inet_make_mask(rtm->rtm_src_len);
181 new_r->r_dstmask = inet_make_mask(rtm->rtm_dst_len);
182 new_r->r_tos = rtm->rtm_tos;
183 #ifdef CONFIG_IP_ROUTE_FWMARK
184 if (rta[RTA_PROTOINFO-1])
185 memcpy(&new_r->r_fwmark, RTA_DATA(rta[RTA_PROTOINFO-1]), 4);
186 #endif
187 new_r->r_action = rtm->rtm_type;
188 new_r->r_flags = rtm->rtm_flags;
189 if (rta[RTA_PRIORITY-1])
190 memcpy(&new_r->r_preference, RTA_DATA(rta[RTA_PRIORITY-1]), 4);
191 new_r->r_table = table_id;
192 if (rta[RTA_IIF-1]) {
193 struct net_device *dev;
194 memcpy(new_r->r_ifname, RTA_DATA(rta[RTA_IIF-1]), IFNAMSIZ);
195 new_r->r_ifname[IFNAMSIZ-1] = 0;
196 new_r->r_ifindex = -1;
197 dev = __dev_get_by_name(new_r->r_ifname);
198 if (dev)
199 new_r->r_ifindex = dev->ifindex;
200 }
201 #ifdef CONFIG_NET_CLS_ROUTE
202 if (rta[RTA_FLOW-1])
203 memcpy(&new_r->r_tclassid, RTA_DATA(rta[RTA_FLOW-1]), 4);
204 #endif
205
206 rp = &fib_rules;
207 if (!new_r->r_preference) {
208 r = fib_rules;
209 if (r && (r = r->r_next) != NULL) {
210 rp = &fib_rules->r_next;
211 if (r->r_preference)
212 new_r->r_preference = r->r_preference - 1;
213 }
214 }
215
216 while ( (r = *rp) != NULL ) {
217 if (r->r_preference > new_r->r_preference)
218 break;
219 rp = &r->r_next;
220 }
221
222 new_r->r_next = r;
223 atomic_inc(&new_r->r_clntref);
224 write_lock_bh(&fib_rules_lock);
225 *rp = new_r;
226 write_unlock_bh(&fib_rules_lock);
227 return 0;
228 }
229
230 u32 fib_rules_map_destination(u32 daddr, struct fib_result *res)
231 {
232 u32 mask = inet_make_mask(res->prefixlen);
233 return (daddr&~mask)|res->fi->fib_nh->nh_gw;
234 }
235
236 u32 fib_rules_policy(u32 saddr, struct fib_result *res, unsigned *flags)
237 {
238 struct fib_rule *r = res->r;
239
240 if (r->r_action == RTN_NAT) {
241 int addrtype = inet_addr_type(r->r_srcmap);
242
243 if (addrtype == RTN_NAT) {
244 /* Packet is from translated source; remember it */
245 saddr = (saddr&~r->r_srcmask)|r->r_srcmap;
246 *flags |= RTCF_SNAT;
247 } else if (addrtype == RTN_LOCAL || r->r_srcmap == 0) {
248 /* Packet is from masqueraded source; remember it */
249 saddr = r->r_srcmap;
250 *flags |= RTCF_MASQ;
251 }
252 }
253 return saddr;
254 }
255
256 #ifdef CONFIG_NET_CLS_ROUTE
257 u32 fib_rules_tclass(struct fib_result *res)
258 {
259 if (res->r)
260 return res->r->r_tclassid;
261 return 0;
262 }
263 #endif
264
265
266 static void fib_rules_detach(struct net_device *dev)
267 {
268 struct fib_rule *r;
269
270 for (r=fib_rules; r; r=r->r_next) {
271 if (r->r_ifindex == dev->ifindex) {
272 write_lock_bh(&fib_rules_lock);
273 r->r_ifindex = -1;
274 write_unlock_bh(&fib_rules_lock);
275 }
276 }
277 }
278
279 static void fib_rules_attach(struct net_device *dev)
280 {
281 struct fib_rule *r;
282
283 for (r=fib_rules; r; r=r->r_next) {
284 if (r->r_ifindex == -1 && strcmp(dev->name, r->r_ifname) == 0) {
285 write_lock_bh(&fib_rules_lock);
286 r->r_ifindex = dev->ifindex;
287 write_unlock_bh(&fib_rules_lock);
288 }
289 }
290 }
291
292 int fib_lookup(const struct rt_key *key, struct fib_result *res)
293 {
294 int err;
295 struct fib_rule *r, *policy;
296 struct fib_table *tb;
297
298 u32 daddr = key->dst;
299 u32 saddr = key->src;
300
301 FRprintk("Lookup: %u.%u.%u.%u <- %u.%u.%u.%u ",
302 NIPQUAD(key->dst), NIPQUAD(key->src));
303 read_lock(&fib_rules_lock);
304 for (r = fib_rules; r; r=r->r_next) {
305 if (((saddr^r->r_src) & r->r_srcmask) ||
306 ((daddr^r->r_dst) & r->r_dstmask) ||
307 #ifdef CONFIG_IP_ROUTE_TOS
308 (r->r_tos && r->r_tos != key->tos) ||
309 #endif
310 #ifdef CONFIG_IP_ROUTE_FWMARK
311 (r->r_fwmark && r->r_fwmark != key->fwmark) ||
312 #endif
313 (r->r_ifindex && r->r_ifindex != key->iif))
314 continue;
315
316 FRprintk("tb %d r %d ", r->r_table, r->r_action);
317 switch (r->r_action) {
318 case RTN_UNICAST:
319 case RTN_NAT:
320 policy = r;
321 break;
322 case RTN_UNREACHABLE:
323 read_unlock(&fib_rules_lock);
324 return -ENETUNREACH;
325 default:
326 case RTN_BLACKHOLE:
327 read_unlock(&fib_rules_lock);
328 return -EINVAL;
329 case RTN_PROHIBIT:
330 read_unlock(&fib_rules_lock);
331 return -EACCES;
332 }
333
334 if ((tb = fib_get_table(r->r_table)) == NULL)
335 continue;
336 err = tb->tb_lookup(tb, key, res);
337 if (err == 0) {
338 res->r = policy;
339 if (policy)
340 atomic_inc(&policy->r_clntref);
341 read_unlock(&fib_rules_lock);
342 return 0;
343 }
344 if (err < 0 && err != -EAGAIN) {
345 read_unlock(&fib_rules_lock);
346 return err;
347 }
348 }
349 FRprintk("FAILURE\n");
350 read_unlock(&fib_rules_lock);
351 return -ENETUNREACH;
352 }
353
354 void fib_select_default(const struct rt_key *key, struct fib_result *res)
355 {
356 if (res->r && res->r->r_action == RTN_UNICAST &&
357 FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) {
358 struct fib_table *tb;
359 if ((tb = fib_get_table(res->r->r_table)) != NULL)
360 tb->tb_select_default(tb, key, res);
361 }
362 }
363
364 static int fib_rules_event(struct notifier_block *this, unsigned long event, void *ptr)
365 {
366 struct net_device *dev = ptr;
367
368 if (event == NETDEV_UNREGISTER)
369 fib_rules_detach(dev);
370 else if (event == NETDEV_REGISTER)
371 fib_rules_attach(dev);
372 return NOTIFY_DONE;
373 }
374
375
376 struct notifier_block fib_rules_notifier = {
377 fib_rules_event,
378 NULL,
379 0
380 };
381
382 #ifdef CONFIG_RTNETLINK
383
384 extern __inline__ int inet_fill_rule(struct sk_buff *skb,
385 struct fib_rule *r,
386 struct netlink_callback *cb)
387 {
388 struct rtmsg *rtm;
389 struct nlmsghdr *nlh;
390 unsigned char *b = skb->tail;
391
392 nlh = NLMSG_PUT(skb, NETLINK_CREDS(cb->skb)->pid, cb->nlh->nlmsg_seq, RTM_NEWRULE, sizeof(*rtm));
393 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
425 nlmsg_failure:
426 rtattr_failure:
427 skb_put(skb, b - skb->tail);
428 return -1;
429 }
430
431 int inet_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
432 {
433 int idx;
434 int s_idx = cb->args[0];
435 struct fib_rule *r;
436
437 read_lock(&fib_rules_lock);
438 for (r=fib_rules, idx=0; r; r = r->r_next, idx++) {
439 if (idx < s_idx)
440 continue;
441 if (inet_fill_rule(skb, r, cb) < 0)
442 break;
443 }
444 read_unlock(&fib_rules_lock);
445 cb->args[0] = idx;
446
447 return skb->len;
448 }
449
450 #endif /* CONFIG_RTNETLINK */
451
452 void __init fib_rules_init(void)
453 {
454 register_netdevice_notifier(&fib_rules_notifier);
455 }
456
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.