blob: 6986be754ef284cb0f023e3d08c3c4321ad31af9 [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/string.h>
17#include <linux/net.h>
18#include <linux/socket.h>
19#include <linux/sockios.h>
20#include <linux/init.h>
21#include <linux/skbuff.h>
22#include <linux/netlink.h>
23#include <linux/rtnetlink.h>
24#include <linux/proc_fs.h>
25#include <linux/netdevice.h>
26#include <linux/timer.h>
27#include <linux/spinlock.h>
28#include <linux/in_route.h>
Steven Whitehouseecba3202006-03-20 22:43:28 -080029#include <linux/list.h>
30#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/atomic.h>
32#include <asm/uaccess.h>
33#include <net/neighbour.h>
34#include <net/dst.h>
35#include <net/flow.h>
36#include <net/dn.h>
37#include <net/dn_fib.h>
38#include <net/dn_neigh.h>
39#include <net/dn_dev.h>
40
41struct dn_fib_rule
42{
Steven Whitehouseecba3202006-03-20 22:43:28 -080043 struct hlist_node r_hlist;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 atomic_t r_clntref;
45 u32 r_preference;
46 unsigned char r_table;
47 unsigned char r_action;
48 unsigned char r_dst_len;
49 unsigned char r_src_len;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -080050 __le16 r_src;
51 __le16 r_srcmask;
52 __le16 r_dst;
53 __le16 r_dstmask;
54 __le16 r_srcmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 u8 r_flags;
56#ifdef CONFIG_DECNET_ROUTE_FWMARK
57 u32 r_fwmark;
58#endif
59 int r_ifindex;
60 char r_ifname[IFNAMSIZ];
61 int r_dead;
Steven Whitehouseecba3202006-03-20 22:43:28 -080062 struct rcu_head rcu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
65static struct dn_fib_rule default_rule = {
66 .r_clntref = ATOMIC_INIT(2),
67 .r_preference = 0x7fff,
68 .r_table = RT_TABLE_MAIN,
69 .r_action = RTN_UNICAST
70};
71
Steven Whitehouseecba3202006-03-20 22:43:28 -080072static struct hlist_head dn_fib_rules;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74int dn_fib_rtm_delrule(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
75{
76 struct rtattr **rta = arg;
77 struct rtmsg *rtm = NLMSG_DATA(nlh);
Steven Whitehouseecba3202006-03-20 22:43:28 -080078 struct dn_fib_rule *r;
79 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 int err = -ESRCH;
81
Steven Whitehouseecba3202006-03-20 22:43:28 -080082 hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if ((!rta[RTA_SRC-1] || memcmp(RTA_DATA(rta[RTA_SRC-1]), &r->r_src, 2) == 0) &&
84 rtm->rtm_src_len == r->r_src_len &&
85 rtm->rtm_dst_len == r->r_dst_len &&
86 (!rta[RTA_DST-1] || memcmp(RTA_DATA(rta[RTA_DST-1]), &r->r_dst, 2) == 0) &&
87#ifdef CONFIG_DECNET_ROUTE_FWMARK
88 (!rta[RTA_PROTOINFO-1] || memcmp(RTA_DATA(rta[RTA_PROTOINFO-1]), &r->r_fwmark, 4) == 0) &&
89#endif
90 (!rtm->rtm_type || rtm->rtm_type == r->r_action) &&
91 (!rta[RTA_PRIORITY-1] || memcmp(RTA_DATA(rta[RTA_PRIORITY-1]), &r->r_preference, 4) == 0) &&
92 (!rta[RTA_IIF-1] || rtattr_strcmp(rta[RTA_IIF-1], r->r_ifname) == 0) &&
93 (!rtm->rtm_table || (r && rtm->rtm_table == r->r_table))) {
94
95 err = -EPERM;
96 if (r == &default_rule)
97 break;
98
Steven Whitehouseecba3202006-03-20 22:43:28 -080099 hlist_del_rcu(&r->r_hlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 r->r_dead = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 dn_fib_rule_put(r);
102 err = 0;
103 break;
104 }
105 }
106
107 return err;
108}
109
Steven Whitehouseecba3202006-03-20 22:43:28 -0800110static inline void dn_fib_rule_put_rcu(struct rcu_head *head)
111{
112 struct dn_fib_rule *r = container_of(head, struct dn_fib_rule, rcu);
113 kfree(r);
114}
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116void dn_fib_rule_put(struct dn_fib_rule *r)
117{
118 if (atomic_dec_and_test(&r->r_clntref)) {
119 if (r->r_dead)
Steven Whitehouseecba3202006-03-20 22:43:28 -0800120 call_rcu(&r->rcu, dn_fib_rule_put_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 else
122 printk(KERN_DEBUG "Attempt to free alive dn_fib_rule\n");
123 }
124}
125
126
127int dn_fib_rtm_newrule(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
128{
129 struct rtattr **rta = arg;
130 struct rtmsg *rtm = NLMSG_DATA(nlh);
Steven Whitehouseecba3202006-03-20 22:43:28 -0800131 struct dn_fib_rule *r, *new_r, *last = NULL;
132 struct hlist_node *node = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 unsigned char table_id;
134
135 if (rtm->rtm_src_len > 16 || rtm->rtm_dst_len > 16)
136 return -EINVAL;
137
138 if (rta[RTA_IIF-1] && RTA_PAYLOAD(rta[RTA_IIF-1]) > IFNAMSIZ)
139 return -EINVAL;
140
141 if (rtm->rtm_type == RTN_NAT)
142 return -EINVAL;
143
144 table_id = rtm->rtm_table;
145 if (table_id == RT_TABLE_UNSPEC) {
146 struct dn_fib_table *tb;
147 if (rtm->rtm_type == RTN_UNICAST) {
148 if ((tb = dn_fib_empty_table()) == NULL)
149 return -ENOBUFS;
150 table_id = tb->n;
151 }
152 }
153
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700154 new_r = kzalloc(sizeof(*new_r), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (!new_r)
156 return -ENOMEM;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (rta[RTA_SRC-1])
159 memcpy(&new_r->r_src, RTA_DATA(rta[RTA_SRC-1]), 2);
160 if (rta[RTA_DST-1])
161 memcpy(&new_r->r_dst, RTA_DATA(rta[RTA_DST-1]), 2);
162 if (rta[RTA_GATEWAY-1])
163 memcpy(&new_r->r_srcmap, RTA_DATA(rta[RTA_GATEWAY-1]), 2);
164 new_r->r_src_len = rtm->rtm_src_len;
165 new_r->r_dst_len = rtm->rtm_dst_len;
166 new_r->r_srcmask = dnet_make_mask(rtm->rtm_src_len);
167 new_r->r_dstmask = dnet_make_mask(rtm->rtm_dst_len);
168#ifdef CONFIG_DECNET_ROUTE_FWMARK
169 if (rta[RTA_PROTOINFO-1])
170 memcpy(&new_r->r_fwmark, RTA_DATA(rta[RTA_PROTOINFO-1]), 4);
171#endif
172 new_r->r_action = rtm->rtm_type;
173 new_r->r_flags = rtm->rtm_flags;
174 if (rta[RTA_PRIORITY-1])
175 memcpy(&new_r->r_preference, RTA_DATA(rta[RTA_PRIORITY-1]), 4);
176 new_r->r_table = table_id;
177 if (rta[RTA_IIF-1]) {
178 struct net_device *dev;
179 rtattr_strlcpy(new_r->r_ifname, rta[RTA_IIF-1], IFNAMSIZ);
180 new_r->r_ifindex = -1;
181 dev = dev_get_by_name(new_r->r_ifname);
182 if (dev) {
183 new_r->r_ifindex = dev->ifindex;
184 dev_put(dev);
185 }
186 }
187
Steven Whitehouseecba3202006-03-20 22:43:28 -0800188 r = container_of(dn_fib_rules.first, struct dn_fib_rule, r_hlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (!new_r->r_preference) {
Steven Whitehouseecba3202006-03-20 22:43:28 -0800190 if (r && r->r_hlist.next != NULL) {
191 r = container_of(r->r_hlist.next, struct dn_fib_rule, r_hlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (r->r_preference)
193 new_r->r_preference = r->r_preference - 1;
194 }
195 }
196
Steven Whitehouseecba3202006-03-20 22:43:28 -0800197 hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (r->r_preference > new_r->r_preference)
199 break;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800200 last = r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 atomic_inc(&new_r->r_clntref);
Steven Whitehouseecba3202006-03-20 22:43:28 -0800203
204 if (last)
205 hlist_add_after_rcu(&last->r_hlist, &new_r->r_hlist);
206 else
207 hlist_add_before_rcu(&new_r->r_hlist, &r->r_hlist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return 0;
209}
210
211
212int dn_fib_lookup(const struct flowi *flp, struct dn_fib_res *res)
213{
214 struct dn_fib_rule *r, *policy;
215 struct dn_fib_table *tb;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800216 __le16 saddr = flp->fld_src;
217 __le16 daddr = flp->fld_dst;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800218 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 int err;
220
Steven Whitehouseecba3202006-03-20 22:43:28 -0800221 rcu_read_lock();
222
223 hlist_for_each_entry_rcu(r, node, &dn_fib_rules, r_hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (((saddr^r->r_src) & r->r_srcmask) ||
225 ((daddr^r->r_dst) & r->r_dstmask) ||
226#ifdef CONFIG_DECNET_ROUTE_FWMARK
227 (r->r_fwmark && r->r_fwmark != flp->fld_fwmark) ||
228#endif
229 (r->r_ifindex && r->r_ifindex != flp->iif))
230 continue;
231
232 switch(r->r_action) {
233 case RTN_UNICAST:
234 case RTN_NAT:
235 policy = r;
236 break;
237 case RTN_UNREACHABLE:
Steven Whitehouseecba3202006-03-20 22:43:28 -0800238 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return -ENETUNREACH;
240 default:
241 case RTN_BLACKHOLE:
Steven Whitehouseecba3202006-03-20 22:43:28 -0800242 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return -EINVAL;
244 case RTN_PROHIBIT:
Steven Whitehouseecba3202006-03-20 22:43:28 -0800245 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return -EACCES;
247 }
248
249 if ((tb = dn_fib_get_table(r->r_table, 0)) == NULL)
250 continue;
251 err = tb->lookup(tb, flp, res);
252 if (err == 0) {
253 res->r = policy;
254 if (policy)
255 atomic_inc(&policy->r_clntref);
Steven Whitehouseecba3202006-03-20 22:43:28 -0800256 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return 0;
258 }
259 if (err < 0 && err != -EAGAIN) {
Steven Whitehouseecba3202006-03-20 22:43:28 -0800260 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return err;
262 }
263 }
264
Steven Whitehouseecba3202006-03-20 22:43:28 -0800265 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return -ESRCH;
267}
268
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800269unsigned dnet_addr_type(__le16 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
272 struct dn_fib_res res;
273 unsigned ret = RTN_UNICAST;
274 struct dn_fib_table *tb = dn_fib_tables[RT_TABLE_LOCAL];
275
276 res.r = NULL;
277
278 if (tb) {
279 if (!tb->lookup(tb, &fl, &res)) {
280 ret = res.type;
281 dn_fib_res_put(&res);
282 }
283 }
284 return ret;
285}
286
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800287__le16 dn_fib_rules_policy(__le16 saddr, struct dn_fib_res *res, unsigned *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct dn_fib_rule *r = res->r;
290
291 if (r->r_action == RTN_NAT) {
292 int addrtype = dnet_addr_type(r->r_srcmap);
293
294 if (addrtype == RTN_NAT) {
295 saddr = (saddr&~r->r_srcmask)|r->r_srcmap;
296 *flags |= RTCF_SNAT;
297 } else if (addrtype == RTN_LOCAL || r->r_srcmap == 0) {
298 saddr = r->r_srcmap;
299 *flags |= RTCF_MASQ;
300 }
301 }
302 return saddr;
303}
304
305static void dn_fib_rules_detach(struct net_device *dev)
306{
Steven Whitehouseecba3202006-03-20 22:43:28 -0800307 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct dn_fib_rule *r;
309
Steven Whitehouseecba3202006-03-20 22:43:28 -0800310 hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) {
311 if (r->r_ifindex == dev->ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 r->r_ifindex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314}
315
316static void dn_fib_rules_attach(struct net_device *dev)
317{
Steven Whitehouseecba3202006-03-20 22:43:28 -0800318 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 struct dn_fib_rule *r;
320
Steven Whitehouseecba3202006-03-20 22:43:28 -0800321 hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) {
322 if (r->r_ifindex == -1 && strcmp(dev->name, r->r_ifname) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 r->r_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325}
326
327static int dn_fib_rules_event(struct notifier_block *this, unsigned long event, void *ptr)
328{
329 struct net_device *dev = ptr;
330
331 switch(event) {
332 case NETDEV_UNREGISTER:
333 dn_fib_rules_detach(dev);
334 dn_fib_sync_down(0, dev, 1);
335 case NETDEV_REGISTER:
336 dn_fib_rules_attach(dev);
337 dn_fib_sync_up(dev);
338 }
339
340 return NOTIFY_DONE;
341}
342
343
344static struct notifier_block dn_fib_rules_notifier = {
345 .notifier_call = dn_fib_rules_event,
346};
347
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -0700348static int dn_fib_fill_rule(struct sk_buff *skb, struct dn_fib_rule *r,
349 struct netlink_callback *cb, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 struct rtmsg *rtm;
352 struct nlmsghdr *nlh;
353 unsigned char *b = skb->tail;
354
355
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -0700356 nlh = NLMSG_NEW_ANSWER(skb, cb, RTM_NEWRULE, sizeof(*rtm), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 rtm = NLMSG_DATA(nlh);
358 rtm->rtm_family = AF_DECnet;
359 rtm->rtm_dst_len = r->r_dst_len;
360 rtm->rtm_src_len = r->r_src_len;
361 rtm->rtm_tos = 0;
362#ifdef CONFIG_DECNET_ROUTE_FWMARK
363 if (r->r_fwmark)
364 RTA_PUT(skb, RTA_PROTOINFO, 4, &r->r_fwmark);
365#endif
366 rtm->rtm_table = r->r_table;
367 rtm->rtm_protocol = 0;
368 rtm->rtm_scope = 0;
369 rtm->rtm_type = r->r_action;
370 rtm->rtm_flags = r->r_flags;
371
372 if (r->r_dst_len)
373 RTA_PUT(skb, RTA_DST, 2, &r->r_dst);
374 if (r->r_src_len)
375 RTA_PUT(skb, RTA_SRC, 2, &r->r_src);
376 if (r->r_ifname[0])
377 RTA_PUT(skb, RTA_IIF, IFNAMSIZ, &r->r_ifname);
378 if (r->r_preference)
379 RTA_PUT(skb, RTA_PRIORITY, 4, &r->r_preference);
380 if (r->r_srcmap)
381 RTA_PUT(skb, RTA_GATEWAY, 2, &r->r_srcmap);
382 nlh->nlmsg_len = skb->tail - b;
383 return skb->len;
384
385nlmsg_failure:
386rtattr_failure:
387 skb_trim(skb, b - skb->data);
388 return -1;
389}
390
391int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
392{
Steven Whitehouseecba3202006-03-20 22:43:28 -0800393 int idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 int s_idx = cb->args[0];
395 struct dn_fib_rule *r;
Steven Whitehouseecba3202006-03-20 22:43:28 -0800396 struct hlist_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Steven Whitehouseecba3202006-03-20 22:43:28 -0800398 rcu_read_lock();
399 hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (idx < s_idx)
Patrick McHardy26e0fd1ce2006-07-08 13:38:55 -0700401 goto next;
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -0700402 if (dn_fib_fill_rule(skb, r, cb, NLM_F_MULTI) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 break;
Patrick McHardy26e0fd1ce2006-07-08 13:38:55 -0700404next:
Steven Whitehouseecba3202006-03-20 22:43:28 -0800405 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
Steven Whitehouseecba3202006-03-20 22:43:28 -0800407 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 cb->args[0] = idx;
409
410 return skb->len;
411}
412
413void __init dn_fib_rules_init(void)
414{
Steven Whitehouseecba3202006-03-20 22:43:28 -0800415 INIT_HLIST_HEAD(&dn_fib_rules);
416 hlist_add_head(&default_rule.r_hlist, &dn_fib_rules);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 register_netdevice_notifier(&dn_fib_rules_notifier);
418}
419
420void __exit dn_fib_rules_cleanup(void)
421{
422 unregister_netdevice_notifier(&dn_fib_rules_notifier);
423}
424
425