blob: 1060de70bc0cdea5fec0eb7a6afe317138aa57d7 [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>
30#include <asm/atomic.h>
31#include <asm/uaccess.h>
32#include <net/neighbour.h>
33#include <net/dst.h>
34#include <net/flow.h>
35#include <net/dn.h>
36#include <net/dn_fib.h>
37#include <net/dn_neigh.h>
38#include <net/dn_dev.h>
39
40struct dn_fib_rule
41{
42 struct dn_fib_rule *r_next;
43 atomic_t r_clntref;
44 u32 r_preference;
45 unsigned char r_table;
46 unsigned char r_action;
47 unsigned char r_dst_len;
48 unsigned char r_src_len;
49 dn_address r_src;
50 dn_address r_srcmask;
51 dn_address r_dst;
52 dn_address r_dstmask;
53 dn_address r_srcmap;
54 u8 r_flags;
55#ifdef CONFIG_DECNET_ROUTE_FWMARK
56 u32 r_fwmark;
57#endif
58 int r_ifindex;
59 char r_ifname[IFNAMSIZ];
60 int r_dead;
61};
62
63static struct dn_fib_rule default_rule = {
64 .r_clntref = ATOMIC_INIT(2),
65 .r_preference = 0x7fff,
66 .r_table = RT_TABLE_MAIN,
67 .r_action = RTN_UNICAST
68};
69
70static struct dn_fib_rule *dn_fib_rules = &default_rule;
71static DEFINE_RWLOCK(dn_fib_rules_lock);
72
73
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);
78 struct dn_fib_rule *r, **rp;
79 int err = -ESRCH;
80
81 for(rp=&dn_fib_rules; (r=*rp) != NULL; rp = &r->r_next) {
82 if ((!rta[RTA_SRC-1] || memcmp(RTA_DATA(rta[RTA_SRC-1]), &r->r_src, 2) == 0) &&
83 rtm->rtm_src_len == r->r_src_len &&
84 rtm->rtm_dst_len == r->r_dst_len &&
85 (!rta[RTA_DST-1] || memcmp(RTA_DATA(rta[RTA_DST-1]), &r->r_dst, 2) == 0) &&
86#ifdef CONFIG_DECNET_ROUTE_FWMARK
87 (!rta[RTA_PROTOINFO-1] || memcmp(RTA_DATA(rta[RTA_PROTOINFO-1]), &r->r_fwmark, 4) == 0) &&
88#endif
89 (!rtm->rtm_type || rtm->rtm_type == r->r_action) &&
90 (!rta[RTA_PRIORITY-1] || memcmp(RTA_DATA(rta[RTA_PRIORITY-1]), &r->r_preference, 4) == 0) &&
91 (!rta[RTA_IIF-1] || rtattr_strcmp(rta[RTA_IIF-1], r->r_ifname) == 0) &&
92 (!rtm->rtm_table || (r && rtm->rtm_table == r->r_table))) {
93
94 err = -EPERM;
95 if (r == &default_rule)
96 break;
97
98 write_lock_bh(&dn_fib_rules_lock);
99 *rp = r->r_next;
100 r->r_dead = 1;
101 write_unlock_bh(&dn_fib_rules_lock);
102 dn_fib_rule_put(r);
103 err = 0;
104 break;
105 }
106 }
107
108 return err;
109}
110
111void dn_fib_rule_put(struct dn_fib_rule *r)
112{
113 if (atomic_dec_and_test(&r->r_clntref)) {
114 if (r->r_dead)
115 kfree(r);
116 else
117 printk(KERN_DEBUG "Attempt to free alive dn_fib_rule\n");
118 }
119}
120
121
122int dn_fib_rtm_newrule(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
123{
124 struct rtattr **rta = arg;
125 struct rtmsg *rtm = NLMSG_DATA(nlh);
126 struct dn_fib_rule *r, *new_r, **rp;
127 unsigned char table_id;
128
129 if (rtm->rtm_src_len > 16 || rtm->rtm_dst_len > 16)
130 return -EINVAL;
131
132 if (rta[RTA_IIF-1] && RTA_PAYLOAD(rta[RTA_IIF-1]) > IFNAMSIZ)
133 return -EINVAL;
134
135 if (rtm->rtm_type == RTN_NAT)
136 return -EINVAL;
137
138 table_id = rtm->rtm_table;
139 if (table_id == RT_TABLE_UNSPEC) {
140 struct dn_fib_table *tb;
141 if (rtm->rtm_type == RTN_UNICAST) {
142 if ((tb = dn_fib_empty_table()) == NULL)
143 return -ENOBUFS;
144 table_id = tb->n;
145 }
146 }
147
148 new_r = kmalloc(sizeof(*new_r), GFP_KERNEL);
149 if (!new_r)
150 return -ENOMEM;
151 memset(new_r, 0, sizeof(*new_r));
152 if (rta[RTA_SRC-1])
153 memcpy(&new_r->r_src, RTA_DATA(rta[RTA_SRC-1]), 2);
154 if (rta[RTA_DST-1])
155 memcpy(&new_r->r_dst, RTA_DATA(rta[RTA_DST-1]), 2);
156 if (rta[RTA_GATEWAY-1])
157 memcpy(&new_r->r_srcmap, RTA_DATA(rta[RTA_GATEWAY-1]), 2);
158 new_r->r_src_len = rtm->rtm_src_len;
159 new_r->r_dst_len = rtm->rtm_dst_len;
160 new_r->r_srcmask = dnet_make_mask(rtm->rtm_src_len);
161 new_r->r_dstmask = dnet_make_mask(rtm->rtm_dst_len);
162#ifdef CONFIG_DECNET_ROUTE_FWMARK
163 if (rta[RTA_PROTOINFO-1])
164 memcpy(&new_r->r_fwmark, RTA_DATA(rta[RTA_PROTOINFO-1]), 4);
165#endif
166 new_r->r_action = rtm->rtm_type;
167 new_r->r_flags = rtm->rtm_flags;
168 if (rta[RTA_PRIORITY-1])
169 memcpy(&new_r->r_preference, RTA_DATA(rta[RTA_PRIORITY-1]), 4);
170 new_r->r_table = table_id;
171 if (rta[RTA_IIF-1]) {
172 struct net_device *dev;
173 rtattr_strlcpy(new_r->r_ifname, rta[RTA_IIF-1], IFNAMSIZ);
174 new_r->r_ifindex = -1;
175 dev = dev_get_by_name(new_r->r_ifname);
176 if (dev) {
177 new_r->r_ifindex = dev->ifindex;
178 dev_put(dev);
179 }
180 }
181
182 rp = &dn_fib_rules;
183 if (!new_r->r_preference) {
184 r = dn_fib_rules;
185 if (r && (r = r->r_next) != NULL) {
186 rp = &dn_fib_rules->r_next;
187 if (r->r_preference)
188 new_r->r_preference = r->r_preference - 1;
189 }
190 }
191
192 while((r=*rp) != NULL) {
193 if (r->r_preference > new_r->r_preference)
194 break;
195 rp = &r->r_next;
196 }
197
198 new_r->r_next = r;
199 atomic_inc(&new_r->r_clntref);
200 write_lock_bh(&dn_fib_rules_lock);
201 *rp = new_r;
202 write_unlock_bh(&dn_fib_rules_lock);
203 return 0;
204}
205
206
207int dn_fib_lookup(const struct flowi *flp, struct dn_fib_res *res)
208{
209 struct dn_fib_rule *r, *policy;
210 struct dn_fib_table *tb;
211 dn_address saddr = flp->fld_src;
212 dn_address daddr = flp->fld_dst;
213 int err;
214
215 read_lock(&dn_fib_rules_lock);
216 for(r = dn_fib_rules; r; r = r->r_next) {
217 if (((saddr^r->r_src) & r->r_srcmask) ||
218 ((daddr^r->r_dst) & r->r_dstmask) ||
219#ifdef CONFIG_DECNET_ROUTE_FWMARK
220 (r->r_fwmark && r->r_fwmark != flp->fld_fwmark) ||
221#endif
222 (r->r_ifindex && r->r_ifindex != flp->iif))
223 continue;
224
225 switch(r->r_action) {
226 case RTN_UNICAST:
227 case RTN_NAT:
228 policy = r;
229 break;
230 case RTN_UNREACHABLE:
231 read_unlock(&dn_fib_rules_lock);
232 return -ENETUNREACH;
233 default:
234 case RTN_BLACKHOLE:
235 read_unlock(&dn_fib_rules_lock);
236 return -EINVAL;
237 case RTN_PROHIBIT:
238 read_unlock(&dn_fib_rules_lock);
239 return -EACCES;
240 }
241
242 if ((tb = dn_fib_get_table(r->r_table, 0)) == NULL)
243 continue;
244 err = tb->lookup(tb, flp, res);
245 if (err == 0) {
246 res->r = policy;
247 if (policy)
248 atomic_inc(&policy->r_clntref);
249 read_unlock(&dn_fib_rules_lock);
250 return 0;
251 }
252 if (err < 0 && err != -EAGAIN) {
253 read_unlock(&dn_fib_rules_lock);
254 return err;
255 }
256 }
257
258 read_unlock(&dn_fib_rules_lock);
259 return -ESRCH;
260}
261
262unsigned dnet_addr_type(__u16 addr)
263{
264 struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
265 struct dn_fib_res res;
266 unsigned ret = RTN_UNICAST;
267 struct dn_fib_table *tb = dn_fib_tables[RT_TABLE_LOCAL];
268
269 res.r = NULL;
270
271 if (tb) {
272 if (!tb->lookup(tb, &fl, &res)) {
273 ret = res.type;
274 dn_fib_res_put(&res);
275 }
276 }
277 return ret;
278}
279
280__u16 dn_fib_rules_policy(__u16 saddr, struct dn_fib_res *res, unsigned *flags)
281{
282 struct dn_fib_rule *r = res->r;
283
284 if (r->r_action == RTN_NAT) {
285 int addrtype = dnet_addr_type(r->r_srcmap);
286
287 if (addrtype == RTN_NAT) {
288 saddr = (saddr&~r->r_srcmask)|r->r_srcmap;
289 *flags |= RTCF_SNAT;
290 } else if (addrtype == RTN_LOCAL || r->r_srcmap == 0) {
291 saddr = r->r_srcmap;
292 *flags |= RTCF_MASQ;
293 }
294 }
295 return saddr;
296}
297
298static void dn_fib_rules_detach(struct net_device *dev)
299{
300 struct dn_fib_rule *r;
301
302 for(r = dn_fib_rules; r; r = r->r_next) {
303 if (r->r_ifindex == dev->ifindex) {
304 write_lock_bh(&dn_fib_rules_lock);
305 r->r_ifindex = -1;
306 write_unlock_bh(&dn_fib_rules_lock);
307 }
308 }
309}
310
311static void dn_fib_rules_attach(struct net_device *dev)
312{
313 struct dn_fib_rule *r;
314
315 for(r = dn_fib_rules; r; r = r->r_next) {
316 if (r->r_ifindex == -1 && strcmp(dev->name, r->r_ifname) == 0) {
317 write_lock_bh(&dn_fib_rules_lock);
318 r->r_ifindex = dev->ifindex;
319 write_unlock_bh(&dn_fib_rules_lock);
320 }
321 }
322}
323
324static int dn_fib_rules_event(struct notifier_block *this, unsigned long event, void *ptr)
325{
326 struct net_device *dev = ptr;
327
328 switch(event) {
329 case NETDEV_UNREGISTER:
330 dn_fib_rules_detach(dev);
331 dn_fib_sync_down(0, dev, 1);
332 case NETDEV_REGISTER:
333 dn_fib_rules_attach(dev);
334 dn_fib_sync_up(dev);
335 }
336
337 return NOTIFY_DONE;
338}
339
340
341static struct notifier_block dn_fib_rules_notifier = {
342 .notifier_call = dn_fib_rules_event,
343};
344
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -0700345static int dn_fib_fill_rule(struct sk_buff *skb, struct dn_fib_rule *r,
346 struct netlink_callback *cb, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 struct rtmsg *rtm;
349 struct nlmsghdr *nlh;
350 unsigned char *b = skb->tail;
351
352
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -0700353 nlh = NLMSG_NEW_ANSWER(skb, cb, RTM_NEWRULE, sizeof(*rtm), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 rtm = NLMSG_DATA(nlh);
355 rtm->rtm_family = AF_DECnet;
356 rtm->rtm_dst_len = r->r_dst_len;
357 rtm->rtm_src_len = r->r_src_len;
358 rtm->rtm_tos = 0;
359#ifdef CONFIG_DECNET_ROUTE_FWMARK
360 if (r->r_fwmark)
361 RTA_PUT(skb, RTA_PROTOINFO, 4, &r->r_fwmark);
362#endif
363 rtm->rtm_table = r->r_table;
364 rtm->rtm_protocol = 0;
365 rtm->rtm_scope = 0;
366 rtm->rtm_type = r->r_action;
367 rtm->rtm_flags = r->r_flags;
368
369 if (r->r_dst_len)
370 RTA_PUT(skb, RTA_DST, 2, &r->r_dst);
371 if (r->r_src_len)
372 RTA_PUT(skb, RTA_SRC, 2, &r->r_src);
373 if (r->r_ifname[0])
374 RTA_PUT(skb, RTA_IIF, IFNAMSIZ, &r->r_ifname);
375 if (r->r_preference)
376 RTA_PUT(skb, RTA_PRIORITY, 4, &r->r_preference);
377 if (r->r_srcmap)
378 RTA_PUT(skb, RTA_GATEWAY, 2, &r->r_srcmap);
379 nlh->nlmsg_len = skb->tail - b;
380 return skb->len;
381
382nlmsg_failure:
383rtattr_failure:
384 skb_trim(skb, b - skb->data);
385 return -1;
386}
387
388int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
389{
390 int idx;
391 int s_idx = cb->args[0];
392 struct dn_fib_rule *r;
393
394 read_lock(&dn_fib_rules_lock);
395 for(r = dn_fib_rules, idx = 0; r; r = r->r_next, idx++) {
396 if (idx < s_idx)
397 continue;
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -0700398 if (dn_fib_fill_rule(skb, r, cb, NLM_F_MULTI) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 break;
400 }
401 read_unlock(&dn_fib_rules_lock);
402 cb->args[0] = idx;
403
404 return skb->len;
405}
406
407void __init dn_fib_rules_init(void)
408{
409 register_netdevice_notifier(&dn_fib_rules_notifier);
410}
411
412void __exit dn_fib_rules_cleanup(void)
413{
414 unregister_netdevice_notifier(&dn_fib_rules_notifier);
415}
416
417