]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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.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> | |
14c85021 | 36 | #include <linux/inetdevice.h> |
1da177e4 LT |
37 | #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> | |
43 | ||
44 | #include <net/ip.h> | |
45 | #include <net/protocol.h> | |
46 | #include <net/route.h> | |
47 | #include <net/tcp.h> | |
48 | #include <net/sock.h> | |
49 | #include <net/ip_fib.h> | |
50 | ||
51 | #define FRprintk(a...) | |
52 | ||
53 | struct fib_rule | |
54 | { | |
55 | struct fib_rule *r_next; | |
56 | atomic_t r_clntref; | |
57 | u32 r_preference; | |
58 | unsigned char r_table; | |
59 | unsigned char r_action; | |
60 | unsigned char r_dst_len; | |
61 | unsigned char r_src_len; | |
62 | u32 r_src; | |
63 | u32 r_srcmask; | |
64 | u32 r_dst; | |
65 | u32 r_dstmask; | |
66 | u32 r_srcmap; | |
67 | u8 r_flags; | |
68 | u8 r_tos; | |
69 | #ifdef CONFIG_IP_ROUTE_FWMARK | |
70 | u32 r_fwmark; | |
71 | #endif | |
72 | int r_ifindex; | |
73 | #ifdef CONFIG_NET_CLS_ROUTE | |
74 | __u32 r_tclassid; | |
75 | #endif | |
76 | char r_ifname[IFNAMSIZ]; | |
77 | int r_dead; | |
78 | }; | |
79 | ||
80 | static struct fib_rule default_rule = { | |
81 | .r_clntref = ATOMIC_INIT(2), | |
82 | .r_preference = 0x7FFF, | |
83 | .r_table = RT_TABLE_DEFAULT, | |
84 | .r_action = RTN_UNICAST, | |
85 | }; | |
86 | ||
87 | static struct fib_rule main_rule = { | |
88 | .r_next = &default_rule, | |
89 | .r_clntref = ATOMIC_INIT(2), | |
90 | .r_preference = 0x7FFE, | |
91 | .r_table = RT_TABLE_MAIN, | |
92 | .r_action = RTN_UNICAST, | |
93 | }; | |
94 | ||
95 | static struct fib_rule local_rule = { | |
96 | .r_next = &main_rule, | |
97 | .r_clntref = ATOMIC_INIT(2), | |
98 | .r_table = RT_TABLE_LOCAL, | |
99 | .r_action = RTN_UNICAST, | |
100 | }; | |
101 | ||
102 | static struct fib_rule *fib_rules = &local_rule; | |
103 | static DEFINE_RWLOCK(fib_rules_lock); | |
104 | ||
105 | int inet_rtm_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) | |
106 | { | |
107 | struct rtattr **rta = arg; | |
108 | struct rtmsg *rtm = NLMSG_DATA(nlh); | |
109 | struct fib_rule *r, **rp; | |
110 | int err = -ESRCH; | |
111 | ||
112 | for (rp=&fib_rules; (r=*rp) != NULL; rp=&r->r_next) { | |
113 | if ((!rta[RTA_SRC-1] || memcmp(RTA_DATA(rta[RTA_SRC-1]), &r->r_src, 4) == 0) && | |
114 | rtm->rtm_src_len == r->r_src_len && | |
115 | rtm->rtm_dst_len == r->r_dst_len && | |
116 | (!rta[RTA_DST-1] || memcmp(RTA_DATA(rta[RTA_DST-1]), &r->r_dst, 4) == 0) && | |
117 | rtm->rtm_tos == r->r_tos && | |
118 | #ifdef CONFIG_IP_ROUTE_FWMARK | |
119 | (!rta[RTA_PROTOINFO-1] || memcmp(RTA_DATA(rta[RTA_PROTOINFO-1]), &r->r_fwmark, 4) == 0) && | |
120 | #endif | |
121 | (!rtm->rtm_type || rtm->rtm_type == r->r_action) && | |
122 | (!rta[RTA_PRIORITY-1] || memcmp(RTA_DATA(rta[RTA_PRIORITY-1]), &r->r_preference, 4) == 0) && | |
123 | (!rta[RTA_IIF-1] || rtattr_strcmp(rta[RTA_IIF-1], r->r_ifname) == 0) && | |
124 | (!rtm->rtm_table || (r && rtm->rtm_table == r->r_table))) { | |
125 | err = -EPERM; | |
126 | if (r == &local_rule) | |
127 | break; | |
128 | ||
129 | write_lock_bh(&fib_rules_lock); | |
130 | *rp = r->r_next; | |
131 | r->r_dead = 1; | |
132 | write_unlock_bh(&fib_rules_lock); | |
133 | fib_rule_put(r); | |
134 | err = 0; | |
135 | break; | |
136 | } | |
137 | } | |
138 | return err; | |
139 | } | |
140 | ||
141 | /* Allocate new unique table id */ | |
142 | ||
143 | static struct fib_table *fib_empty_table(void) | |
144 | { | |
145 | int id; | |
146 | ||
147 | for (id = 1; id <= RT_TABLE_MAX; id++) | |
148 | if (fib_tables[id] == NULL) | |
149 | return __fib_new_table(id); | |
150 | return NULL; | |
151 | } | |
152 | ||
153 | void fib_rule_put(struct fib_rule *r) | |
154 | { | |
155 | if (atomic_dec_and_test(&r->r_clntref)) { | |
156 | if (r->r_dead) | |
157 | kfree(r); | |
158 | else | |
159 | printk("Freeing alive rule %p\n", r); | |
160 | } | |
161 | } | |
162 | ||
163 | int inet_rtm_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) | |
164 | { | |
165 | struct rtattr **rta = arg; | |
166 | struct rtmsg *rtm = NLMSG_DATA(nlh); | |
167 | struct fib_rule *r, *new_r, **rp; | |
168 | unsigned char table_id; | |
169 | ||
170 | if (rtm->rtm_src_len > 32 || rtm->rtm_dst_len > 32 || | |
171 | (rtm->rtm_tos & ~IPTOS_TOS_MASK)) | |
172 | return -EINVAL; | |
173 | ||
174 | if (rta[RTA_IIF-1] && RTA_PAYLOAD(rta[RTA_IIF-1]) > IFNAMSIZ) | |
175 | return -EINVAL; | |
176 | ||
177 | table_id = rtm->rtm_table; | |
178 | if (table_id == RT_TABLE_UNSPEC) { | |
179 | struct fib_table *table; | |
180 | if (rtm->rtm_type == RTN_UNICAST) { | |
181 | if ((table = fib_empty_table()) == NULL) | |
182 | return -ENOBUFS; | |
183 | table_id = table->tb_id; | |
184 | } | |
185 | } | |
186 | ||
187 | new_r = kmalloc(sizeof(*new_r), GFP_KERNEL); | |
188 | if (!new_r) | |
189 | return -ENOMEM; | |
190 | memset(new_r, 0, sizeof(*new_r)); | |
191 | if (rta[RTA_SRC-1]) | |
192 | memcpy(&new_r->r_src, RTA_DATA(rta[RTA_SRC-1]), 4); | |
193 | if (rta[RTA_DST-1]) | |
194 | memcpy(&new_r->r_dst, RTA_DATA(rta[RTA_DST-1]), 4); | |
195 | if (rta[RTA_GATEWAY-1]) | |
196 | memcpy(&new_r->r_srcmap, RTA_DATA(rta[RTA_GATEWAY-1]), 4); | |
197 | new_r->r_src_len = rtm->rtm_src_len; | |
198 | new_r->r_dst_len = rtm->rtm_dst_len; | |
199 | new_r->r_srcmask = inet_make_mask(rtm->rtm_src_len); | |
200 | new_r->r_dstmask = inet_make_mask(rtm->rtm_dst_len); | |
201 | new_r->r_tos = rtm->rtm_tos; | |
202 | #ifdef CONFIG_IP_ROUTE_FWMARK | |
203 | if (rta[RTA_PROTOINFO-1]) | |
204 | memcpy(&new_r->r_fwmark, RTA_DATA(rta[RTA_PROTOINFO-1]), 4); | |
205 | #endif | |
206 | new_r->r_action = rtm->rtm_type; | |
207 | new_r->r_flags = rtm->rtm_flags; | |
208 | if (rta[RTA_PRIORITY-1]) | |
209 | memcpy(&new_r->r_preference, RTA_DATA(rta[RTA_PRIORITY-1]), 4); | |
210 | new_r->r_table = table_id; | |
211 | if (rta[RTA_IIF-1]) { | |
212 | struct net_device *dev; | |
213 | rtattr_strlcpy(new_r->r_ifname, rta[RTA_IIF-1], IFNAMSIZ); | |
214 | new_r->r_ifindex = -1; | |
215 | dev = __dev_get_by_name(new_r->r_ifname); | |
216 | if (dev) | |
217 | new_r->r_ifindex = dev->ifindex; | |
218 | } | |
219 | #ifdef CONFIG_NET_CLS_ROUTE | |
220 | if (rta[RTA_FLOW-1]) | |
221 | memcpy(&new_r->r_tclassid, RTA_DATA(rta[RTA_FLOW-1]), 4); | |
222 | #endif | |
223 | ||
224 | rp = &fib_rules; | |
225 | if (!new_r->r_preference) { | |
226 | r = fib_rules; | |
227 | if (r && (r = r->r_next) != NULL) { | |
228 | rp = &fib_rules->r_next; | |
229 | if (r->r_preference) | |
230 | new_r->r_preference = r->r_preference - 1; | |
231 | } | |
232 | } | |
233 | ||
234 | while ( (r = *rp) != NULL ) { | |
235 | if (r->r_preference > new_r->r_preference) | |
236 | break; | |
237 | rp = &r->r_next; | |
238 | } | |
239 | ||
240 | new_r->r_next = r; | |
241 | atomic_inc(&new_r->r_clntref); | |
242 | write_lock_bh(&fib_rules_lock); | |
243 | *rp = new_r; | |
244 | write_unlock_bh(&fib_rules_lock); | |
245 | return 0; | |
246 | } | |
247 | ||
248 | #ifdef CONFIG_NET_CLS_ROUTE | |
249 | u32 fib_rules_tclass(struct fib_result *res) | |
250 | { | |
251 | if (res->r) | |
252 | return res->r->r_tclassid; | |
253 | return 0; | |
254 | } | |
255 | #endif | |
256 | ||
257 | ||
258 | static void fib_rules_detach(struct net_device *dev) | |
259 | { | |
260 | struct fib_rule *r; | |
261 | ||
262 | for (r=fib_rules; r; r=r->r_next) { | |
263 | if (r->r_ifindex == dev->ifindex) { | |
264 | write_lock_bh(&fib_rules_lock); | |
265 | r->r_ifindex = -1; | |
266 | write_unlock_bh(&fib_rules_lock); | |
267 | } | |
268 | } | |
269 | } | |
270 | ||
271 | static void fib_rules_attach(struct net_device *dev) | |
272 | { | |
273 | struct fib_rule *r; | |
274 | ||
275 | for (r=fib_rules; r; r=r->r_next) { | |
276 | if (r->r_ifindex == -1 && strcmp(dev->name, r->r_ifname) == 0) { | |
277 | write_lock_bh(&fib_rules_lock); | |
278 | r->r_ifindex = dev->ifindex; | |
279 | write_unlock_bh(&fib_rules_lock); | |
280 | } | |
281 | } | |
282 | } | |
283 | ||
284 | int fib_lookup(const struct flowi *flp, struct fib_result *res) | |
285 | { | |
286 | int err; | |
287 | struct fib_rule *r, *policy; | |
288 | struct fib_table *tb; | |
289 | ||
290 | u32 daddr = flp->fl4_dst; | |
291 | u32 saddr = flp->fl4_src; | |
292 | ||
293 | FRprintk("Lookup: %u.%u.%u.%u <- %u.%u.%u.%u ", | |
294 | NIPQUAD(flp->fl4_dst), NIPQUAD(flp->fl4_src)); | |
295 | read_lock(&fib_rules_lock); | |
296 | for (r = fib_rules; r; r=r->r_next) { | |
297 | if (((saddr^r->r_src) & r->r_srcmask) || | |
298 | ((daddr^r->r_dst) & r->r_dstmask) || | |
299 | (r->r_tos && r->r_tos != flp->fl4_tos) || | |
300 | #ifdef CONFIG_IP_ROUTE_FWMARK | |
301 | (r->r_fwmark && r->r_fwmark != flp->fl4_fwmark) || | |
302 | #endif | |
303 | (r->r_ifindex && r->r_ifindex != flp->iif)) | |
304 | continue; | |
305 | ||
306 | FRprintk("tb %d r %d ", r->r_table, r->r_action); | |
307 | switch (r->r_action) { | |
308 | case RTN_UNICAST: | |
309 | policy = r; | |
310 | break; | |
311 | case RTN_UNREACHABLE: | |
312 | read_unlock(&fib_rules_lock); | |
313 | return -ENETUNREACH; | |
314 | default: | |
315 | case RTN_BLACKHOLE: | |
316 | read_unlock(&fib_rules_lock); | |
317 | return -EINVAL; | |
318 | case RTN_PROHIBIT: | |
319 | read_unlock(&fib_rules_lock); | |
320 | return -EACCES; | |
321 | } | |
322 | ||
323 | if ((tb = fib_get_table(r->r_table)) == NULL) | |
324 | continue; | |
325 | err = tb->tb_lookup(tb, flp, res); | |
326 | if (err == 0) { | |
327 | res->r = policy; | |
328 | if (policy) | |
329 | atomic_inc(&policy->r_clntref); | |
330 | read_unlock(&fib_rules_lock); | |
331 | return 0; | |
332 | } | |
333 | if (err < 0 && err != -EAGAIN) { | |
334 | read_unlock(&fib_rules_lock); | |
335 | return err; | |
336 | } | |
337 | } | |
338 | FRprintk("FAILURE\n"); | |
339 | read_unlock(&fib_rules_lock); | |
340 | return -ENETUNREACH; | |
341 | } | |
342 | ||
343 | void fib_select_default(const struct flowi *flp, struct fib_result *res) | |
344 | { | |
345 | if (res->r && res->r->r_action == RTN_UNICAST && | |
346 | FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) { | |
347 | struct fib_table *tb; | |
348 | if ((tb = fib_get_table(res->r->r_table)) != NULL) | |
349 | tb->tb_select_default(tb, flp, res); | |
350 | } | |
351 | } | |
352 | ||
353 | static int fib_rules_event(struct notifier_block *this, unsigned long event, void *ptr) | |
354 | { | |
355 | struct net_device *dev = ptr; | |
356 | ||
357 | if (event == NETDEV_UNREGISTER) | |
358 | fib_rules_detach(dev); | |
359 | else if (event == NETDEV_REGISTER) | |
360 | fib_rules_attach(dev); | |
361 | return NOTIFY_DONE; | |
362 | } | |
363 | ||
364 | ||
365 | static struct notifier_block fib_rules_notifier = { | |
366 | .notifier_call =fib_rules_event, | |
367 | }; | |
368 | ||
369 | static __inline__ int inet_fill_rule(struct sk_buff *skb, | |
370 | struct fib_rule *r, | |
b6544c0b JHS |
371 | struct netlink_callback *cb, |
372 | unsigned int flags) | |
1da177e4 LT |
373 | { |
374 | struct rtmsg *rtm; | |
375 | struct nlmsghdr *nlh; | |
376 | unsigned char *b = skb->tail; | |
377 | ||
b6544c0b | 378 | nlh = NLMSG_NEW_ANSWER(skb, cb, RTM_NEWRULE, sizeof(*rtm), flags); |
1da177e4 LT |
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; | |
393 | ||
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; | |
410 | ||
411 | nlmsg_failure: | |
412 | rtattr_failure: | |
413 | skb_trim(skb, b - skb->data); | |
414 | return -1; | |
415 | } | |
416 | ||
417 | int inet_dump_rules(struct sk_buff *skb, struct netlink_callback *cb) | |
418 | { | |
419 | int idx; | |
420 | int s_idx = cb->args[0]; | |
421 | struct fib_rule *r; | |
422 | ||
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; | |
b6544c0b | 427 | if (inet_fill_rule(skb, r, cb, NLM_F_MULTI) < 0) |
1da177e4 LT |
428 | break; |
429 | } | |
430 | read_unlock(&fib_rules_lock); | |
431 | cb->args[0] = idx; | |
432 | ||
433 | return skb->len; | |
434 | } | |
435 | ||
436 | void __init fib_rules_init(void) | |
437 | { | |
438 | register_netdevice_notifier(&fib_rules_notifier); | |
439 | } |