blob: 446faafe206596b11ec35bfbbaa3cc0b4cefa76b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2/*
3 * DECnet An implementation of the DECnet protocol suite for the LINUX
4 * operating system. DECnet is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * DECnet Routing Forwarding Information Base (Rules)
8 *
9 * Author: Steve Whitehouse <SteveW@ACM.org>
10 * Mostly copied from Alexey Kuznetsov's ipv4/fib_rules.c
11 *
12 *
13 * Changes:
14 *
15 */
16#include <linux/config.h>
17#include <linux/string.h>
18#include <linux/net.h>
19#include <linux/socket.h>
20#include <linux/sockios.h>
21#include <linux/init.h>
22#include <linux/skbuff.h>
23#include <linux/netlink.h>
24#include <linux/rtnetlink.h>
25#include <linux/proc_fs.h>
26#include <linux/netdevice.h>
27#include <linux/timer.h>
28#include <linux/spinlock.h>
29#include <linux/in_route.h>
Steven Whitehouseecba3202006-03-20 22:43:28 -080030#include <linux/list.h>
31#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/atomic.h>
33#include <asm/uaccess.h>
34#include <net/neighbour.h>
35#include <net/dst.h>
36#include <net/flow.h>
37#include <net/dn.h>
38#include <net/dn_fib.h>
39#include <net/dn_neigh.h>
40#include <net/dn_dev.h>
41
42struct dn_fib_rule
43{
Steven Whitehouseecba3202006-03-20 22:43:28 -080044 struct hlist_node r_hlist;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 atomic_t r_clntref;
46 u32 r_preference;
47 unsigned char r_table;
48 unsigned char r_action;
49 unsigned char r_dst_len;
50 unsigned char r_src_len;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -080051 __le16 r_src;
52 __le16 r_srcmask;
53 __le16 r_dst;
54 __le16 r_dstmask;
55 __le16 r_srcmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 u8 r_flags;
57#ifdef CONFIG_DECNET_ROUTE_FWMARK
58 u32 r_fwmark;
59#endif
60 int r_ifindex;
61 char r_ifname[IFNAMSIZ];
62 int r_dead;
Steven Whitehouseecba3202006-03-20 22:43:28 -080063 struct rcu_head rcu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
66static struct dn_fib_rule default_rule = {
67 .r_clntref = ATOMIC_INIT(2),
68 .r_preference = 0x7fff,
69 .r_table = RT_TABLE_MAIN,
70 .r_action = RTN_UNICAST
71};
72
Steven Whitehouseecba3202006-03-20 22:43:28 -080073static struct hlist_head dn_fib_rules;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75int dn_fib_rtm_delrule(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
76{
77 struct rtattr **rta = arg;
78 struct rtmsg *rtm = NLMSG_DATA(nlh);
Steven Whitehouseecba3202006-03-20 22:43:28 -080079 struct dn_fib_rule *r;
80 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 int err = -ESRCH;
82
Steven Whitehouseecba3202006-03-20 22:43:28 -080083 hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if ((!rta[RTA_SRC-1] || memcmp(RTA_DATA(rta[RTA_SRC-1]), &r->r_src, 2) == 0) &&
85 rtm->rtm_src_len == r->r_src_len &&
86 rtm->rtm_dst_len == r->r_dst_len &&
87 (!rta[RTA_DST-1] || memcmp(RTA_DATA(rta[RTA_DST-1]), &r->r_dst, 2) == 0) &&
88#ifdef CONFIG_DECNET_ROUTE_FWMARK
89 (!rta[RTA_PROTOINFO-1] || memcmp(RTA_DATA(rta[RTA_PROTOINFO-1]), &r->r_fwmark, 4) == 0) &&
90#endif
91 (!rtm->rtm_type || rtm->rtm_type == r->r_action) &&
92 (!rta[RTA_PRIORITY-1] || memcmp(RTA_DATA(rta[RTA_PRIORITY-1]), &r->r_preference, 4) == 0) &&
93 (!rta[RTA_IIF-1] || rtattr_strcmp(rta[RTA_IIF-1], r->r_ifname) == 0) &&
94 (!rtm->rtm_table || (r && rtm->rtm_table == r->r_table))) {
95
96 err = -EPERM;
97 if (r == &default_rule)
98 break;
99
Steven Whitehouseecba3202006-03-20 22:43:28 -0800100 hlist_del_rcu(&r->r_hlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 r->r_dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 dn_fib_rule_put(r);
103 err = 0;
104 break;
105 }
106 }
107
108 return err;
109}
110
Steven Whitehouseecba3202006-03-20 22:43:28 -0800111static inline void dn_fib_rule_put_rcu(struct rcu_head *head)
112{
113 struct dn_fib_rule *r = container_of(head, struct dn_fib_rule, rcu);
114 kfree(r);
115}
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117void dn_fib_rule_put(struct dn_fib_rule *r)
118{
119 if (atomic_dec_and_test(&r->r_clntref)) {
120 if (r->r_dead)
Steven Whitehouseecba3202006-03-20 22:43:28 -0800121 call_rcu(&r->rcu, dn_fib_rule_put_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 else
123 printk(KERN_DEBUG "Attempt to free alive dn_fib_rule\n");
124 }
125}
126
127
128int dn_fib_rtm_newrule(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
129{
130 struct rtattr **rta = arg;
131 struct rtmsg *rtm = NLMSG_DATA(nlh);
Steven Whitehouseecba3202006-03-20 22:43:28 -0800132 struct dn_fib_rule *r, *new_r, *last = NULL;
133 struct hlist_node *node = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 unsigned char table_id;
135
136 if (rtm->rtm_src_len > 16 || rtm->rtm_dst_len > 16)
137 return -EINVAL;
138
139 if (rta[RTA_IIF-1] && RTA_PAYLOAD(rta[RTA_IIF-1]) > IFNAMSIZ)
140 return -EINVAL;
141
142 if (rtm->rtm_type == RTN_NAT)
143 return -EINVAL;
144
145 table_id = rtm->rtm_table;
146 if (table_id == RT_TABLE_UNSPEC) {
147 struct dn_fib_table *tb;
148 if (rtm->rtm_type == RTN_UNICAST) {
149 if ((tb = dn_fib_empty_table()) == NULL)
150 return -ENOBUFS;
151 table_id = tb->n;
152 }
153 }
154
155 new_r = kmalloc(sizeof(*new_r), GFP_KERNEL);
156 if (!new_r)
157 return -ENOMEM;
158 memset(new_r, 0, sizeof(*new_r));
Steven Whitehouseecba3202006-03-20 22:43:28 -0800159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (rta[RTA_SRC-1])
161 memcpy(&new_r->r_src, RTA_DATA(rta[RTA_SRC-1]), 2);
162 if (rta[RTA_DST-1])
163 memcpy(&new_r->r_dst, RTA_DATA(rta[RTA_DST-1]), 2);
164 if (rta[RTA_GATEWAY-1])
165 memcpy(&new_r->r_srcmap, RTA_DATA(rta[RTA_GATEWAY-1]), 2);
166 new_r->r_src_len = rtm->rtm_src_len;
167 new_r->r_dst_len = rtm->rtm_dst_len;
168 new_r->r_srcmask = dnet_make_mask(rtm->rtm_src_len);
169 new_r->r_dstmask = dnet_make_mask(rtm->rtm_dst_len);
170#ifdef CONFIG_DECNET_ROUTE_FWMARK
171 if (rta[RTA_PROTOINFO-1])
172 memcpy(&new_r->r_fwmark, RTA_DATA(rta[RTA_PROTOINFO-1]), 4);
173#endif
174 new_r->r_action = rtm->rtm_type;
175 new_r->r_flags = rtm->rtm_flags;
176 if (rta[RTA_PRIORITY-1])
177 memcpy(&new_r->r_preference, RTA_DATA(rta[RTA_PRIORITY-1]), 4);
178 new_r->r_table = table_id;
179 if (rta[RTA_IIF-1]) {
180 struct net_device *dev;
181 rtattr_strlcpy(new_r->r_ifname, rta[RTA_IIF-1], IFNAMSIZ);
182 new_r->r_ifindex = -1;
183 dev = dev_get_by_name(new_r->r_ifname);
184 if (dev) {
185 new_r->r_ifindex = dev->ifindex;
186 dev_put(dev);
187 }
188 }
189
Steven Whitehouseecba3202006-03-20 22:43:28 -0800190 r = container_of(dn_fib_rules.first, struct dn_fib_rule, r_hlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (!new_r->r_preference) {
Steven Whitehouseecba3202006-03-20 22:43:28 -0800192 if (r && r->r_hlist.next != NULL) {
193 r = container_of(r->r_hlist.next, struct dn_fib_rule, r_hlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (r->r_preference)
195 new_r->r_preference = r->r_preference - 1;
196 }
197 }
198
Steven Whitehouseecba3202006-03-20 22:43:28 -0800199 hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (r->r_preference > new_r->r_preference)
201 break;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800202 last = r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 atomic_inc(&new_r->r_clntref);
Steven Whitehouseecba3202006-03-20 22:43:28 -0800205
206 if (last)
207 hlist_add_after_rcu(&last->r_hlist, &new_r->r_hlist);
208 else
209 hlist_add_before_rcu(&new_r->r_hlist, &r->r_hlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return 0;
211}
212
213
214int dn_fib_lookup(const struct flowi *flp, struct dn_fib_res *res)
215{
216 struct dn_fib_rule *r, *policy;
217 struct dn_fib_table *tb;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800218 __le16 saddr = flp->fld_src;
219 __le16 daddr = flp->fld_dst;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800220 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 int err;
222
Steven Whitehouseecba3202006-03-20 22:43:28 -0800223 rcu_read_lock();
224
225 hlist_for_each_entry_rcu(r, node, &dn_fib_rules, r_hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (((saddr^r->r_src) & r->r_srcmask) ||
227 ((daddr^r->r_dst) & r->r_dstmask) ||
228#ifdef CONFIG_DECNET_ROUTE_FWMARK
229 (r->r_fwmark && r->r_fwmark != flp->fld_fwmark) ||
230#endif
231 (r->r_ifindex && r->r_ifindex != flp->iif))
232 continue;
233
234 switch(r->r_action) {
235 case RTN_UNICAST:
236 case RTN_NAT:
237 policy = r;
238 break;
239 case RTN_UNREACHABLE:
Steven Whitehouseecba3202006-03-20 22:43:28 -0800240 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return -ENETUNREACH;
242 default:
243 case RTN_BLACKHOLE:
Steven Whitehouseecba3202006-03-20 22:43:28 -0800244 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return -EINVAL;
246 case RTN_PROHIBIT:
Steven Whitehouseecba3202006-03-20 22:43:28 -0800247 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return -EACCES;
249 }
250
251 if ((tb = dn_fib_get_table(r->r_table, 0)) == NULL)
252 continue;
253 err = tb->lookup(tb, flp, res);
254 if (err == 0) {
255 res->r = policy;
256 if (policy)
257 atomic_inc(&policy->r_clntref);
Steven Whitehouseecba3202006-03-20 22:43:28 -0800258 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return 0;
260 }
261 if (err < 0 && err != -EAGAIN) {
Steven Whitehouseecba3202006-03-20 22:43:28 -0800262 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return err;
264 }
265 }
266
Steven Whitehouseecba3202006-03-20 22:43:28 -0800267 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return -ESRCH;
269}
270
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800271unsigned dnet_addr_type(__le16 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
274 struct dn_fib_res res;
275 unsigned ret = RTN_UNICAST;
276 struct dn_fib_table *tb = dn_fib_tables[RT_TABLE_LOCAL];
277
278 res.r = NULL;
279
280 if (tb) {
281 if (!tb->lookup(tb, &fl, &res)) {
282 ret = res.type;
283 dn_fib_res_put(&res);
284 }
285 }
286 return ret;
287}
288
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800289__le16 dn_fib_rules_policy(__le16 saddr, struct dn_fib_res *res, unsigned *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 struct dn_fib_rule *r = res->r;
292
293 if (r->r_action == RTN_NAT) {
294 int addrtype = dnet_addr_type(r->r_srcmap);
295
296 if (addrtype == RTN_NAT) {
297 saddr = (saddr&~r->r_srcmask)|r->r_srcmap;
298 *flags |= RTCF_SNAT;
299 } else if (addrtype == RTN_LOCAL || r->r_srcmap == 0) {
300 saddr = r->r_srcmap;
301 *flags |= RTCF_MASQ;
302 }
303 }
304 return saddr;
305}
306
307static void dn_fib_rules_detach(struct net_device *dev)
308{
Steven Whitehouseecba3202006-03-20 22:43:28 -0800309 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 struct dn_fib_rule *r;
311
Steven Whitehouseecba3202006-03-20 22:43:28 -0800312 hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) {
313 if (r->r_ifindex == dev->ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 r->r_ifindex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316}
317
318static void dn_fib_rules_attach(struct net_device *dev)
319{
Steven Whitehouseecba3202006-03-20 22:43:28 -0800320 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 struct dn_fib_rule *r;
322
Steven Whitehouseecba3202006-03-20 22:43:28 -0800323 hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) {
324 if (r->r_ifindex == -1 && strcmp(dev->name, r->r_ifname) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 r->r_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327}
328
329static int dn_fib_rules_event(struct notifier_block *this, unsigned long event, void *ptr)
330{
331 struct net_device *dev = ptr;
332
333 switch(event) {
334 case NETDEV_UNREGISTER:
335 dn_fib_rules_detach(dev);
336 dn_fib_sync_down(0, dev, 1);
337 case NETDEV_REGISTER:
338 dn_fib_rules_attach(dev);
339 dn_fib_sync_up(dev);
340 }
341
342 return NOTIFY_DONE;
343}
344
345
346static struct notifier_block dn_fib_rules_notifier = {
347 .notifier_call = dn_fib_rules_event,
348};
349
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -0700350static int dn_fib_fill_rule(struct sk_buff *skb, struct dn_fib_rule *r,
351 struct netlink_callback *cb, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 struct rtmsg *rtm;
354 struct nlmsghdr *nlh;
355 unsigned char *b = skb->tail;
356
357
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -0700358 nlh = NLMSG_NEW_ANSWER(skb, cb, RTM_NEWRULE, sizeof(*rtm), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 rtm = NLMSG_DATA(nlh);
360 rtm->rtm_family = AF_DECnet;
361 rtm->rtm_dst_len = r->r_dst_len;
362 rtm->rtm_src_len = r->r_src_len;
363 rtm->rtm_tos = 0;
364#ifdef CONFIG_DECNET_ROUTE_FWMARK
365 if (r->r_fwmark)
366 RTA_PUT(skb, RTA_PROTOINFO, 4, &r->r_fwmark);
367#endif
368 rtm->rtm_table = r->r_table;
369 rtm->rtm_protocol = 0;
370 rtm->rtm_scope = 0;
371 rtm->rtm_type = r->r_action;
372 rtm->rtm_flags = r->r_flags;
373
374 if (r->r_dst_len)
375 RTA_PUT(skb, RTA_DST, 2, &r->r_dst);
376 if (r->r_src_len)
377 RTA_PUT(skb, RTA_SRC, 2, &r->r_src);
378 if (r->r_ifname[0])
379 RTA_PUT(skb, RTA_IIF, IFNAMSIZ, &r->r_ifname);
380 if (r->r_preference)
381 RTA_PUT(skb, RTA_PRIORITY, 4, &r->r_preference);
382 if (r->r_srcmap)
383 RTA_PUT(skb, RTA_GATEWAY, 2, &r->r_srcmap);
384 nlh->nlmsg_len = skb->tail - b;
385 return skb->len;
386
387nlmsg_failure:
388rtattr_failure:
389 skb_trim(skb, b - skb->data);
390 return -1;
391}
392
393int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
394{
Steven Whitehouseecba3202006-03-20 22:43:28 -0800395 int idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 int s_idx = cb->args[0];
397 struct dn_fib_rule *r;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800398 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Steven Whitehouseecba3202006-03-20 22:43:28 -0800400 rcu_read_lock();
401 hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (idx < s_idx)
403 continue;
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -0700404 if (dn_fib_fill_rule(skb, r, cb, NLM_F_MULTI) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 break;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800406 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
Steven Whitehouseecba3202006-03-20 22:43:28 -0800408 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 cb->args[0] = idx;
410
411 return skb->len;
412}
413
414void __init dn_fib_rules_init(void)
415{
Steven Whitehouseecba3202006-03-20 22:43:28 -0800416 INIT_HLIST_HEAD(&dn_fib_rules);
417 hlist_add_head(&default_rule.r_hlist, &dn_fib_rules);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 register_netdevice_notifier(&dn_fib_rules_notifier);
419}
420
421void __exit dn_fib_rules_cleanup(void)
422{
423 unregister_netdevice_notifier(&dn_fib_rules_notifier);
424}
425
426