ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / decnet / dn_rules.c
1
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
40 struct 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
63 static 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
70 static struct dn_fib_rule *dn_fib_rules = &default_rule;
71 static rwlock_t dn_fib_rules_lock = RW_LOCK_UNLOCKED;
72
73
74 int 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] || strcmp(RTA_DATA(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
111 void 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
122 int 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                 memcpy(new_r->r_ifname, RTA_DATA(rta[RTA_IIF-1]), IFNAMSIZ);
174                 new_r->r_ifname[IFNAMSIZ-1] = 0;
175                 new_r->r_ifindex = -1;
176                 dev = dev_get_by_name(new_r->r_ifname);
177                 if (dev) {
178                         new_r->r_ifindex = dev->ifindex;
179                         dev_put(dev);
180                 }
181         }
182
183         rp = &dn_fib_rules;
184         if (!new_r->r_preference) {
185                 r = dn_fib_rules;
186                 if (r && (r = r->r_next) != NULL) {
187                         rp = &dn_fib_rules->r_next;
188                         if (r->r_preference)
189                                 new_r->r_preference = r->r_preference - 1;
190                 }
191         }
192
193         while((r=*rp) != NULL) {
194                 if (r->r_preference > new_r->r_preference)
195                         break;
196                 rp = &r->r_next;
197         }
198
199         new_r->r_next = r;
200         atomic_inc(&new_r->r_clntref);
201         write_lock_bh(&dn_fib_rules_lock);
202         *rp = new_r;
203         write_unlock_bh(&dn_fib_rules_lock);
204         return 0;
205 }
206
207
208 int dn_fib_lookup(const struct flowi *flp, struct dn_fib_res *res)
209 {
210         struct dn_fib_rule *r, *policy;
211         struct dn_fib_table *tb;
212         dn_address saddr = flp->fld_src;
213         dn_address daddr = flp->fld_dst;
214         int err;
215
216         read_lock(&dn_fib_rules_lock);
217         for(r = dn_fib_rules; r; r = r->r_next) {
218                 if (((saddr^r->r_src) & r->r_srcmask) ||
219                     ((daddr^r->r_dst) & r->r_dstmask) ||
220 #ifdef CONFIG_DECNET_ROUTE_FWMARK
221                     (r->r_fwmark && r->r_fwmark != flp->fld_fwmark) ||
222 #endif
223                     (r->r_ifindex && r->r_ifindex != flp->iif))
224                         continue;
225
226                 switch(r->r_action) {
227                         case RTN_UNICAST:
228                         case RTN_NAT:
229                                 policy = r;
230                                 break;
231                         case RTN_UNREACHABLE:
232                                 read_unlock(&dn_fib_rules_lock);
233                                 return -ENETUNREACH;
234                         default:
235                         case RTN_BLACKHOLE:
236                                 read_unlock(&dn_fib_rules_lock);
237                                 return -EINVAL;
238                         case RTN_PROHIBIT:
239                                 read_unlock(&dn_fib_rules_lock);
240                                 return -EACCES;
241                 }
242
243                 if ((tb = dn_fib_get_table(r->r_table, 0)) == NULL)
244                         continue;
245                 err = tb->lookup(tb, flp, res);
246                 if (err == 0) {
247                         res->r = policy;
248                         if (policy)
249                                 atomic_inc(&policy->r_clntref);
250                         read_unlock(&dn_fib_rules_lock);
251                         return 0;
252                 }
253                 if (err < 0 && err != -EAGAIN) {
254                         read_unlock(&dn_fib_rules_lock);
255                         return err;
256                 }
257         }
258
259         read_unlock(&dn_fib_rules_lock);
260         return -ESRCH;
261 }
262
263 unsigned dnet_addr_type(__u16 addr)
264 {
265         struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
266         struct dn_fib_res res;
267         unsigned ret = RTN_UNICAST;
268         struct dn_fib_table *tb = dn_fib_tables[RT_TABLE_LOCAL];
269
270         res.r = NULL;
271
272         if (tb) {
273                 if (!tb->lookup(tb, &fl, &res)) {
274                         ret = res.type;
275                         dn_fib_res_put(&res);
276                 }
277         }
278         return ret;
279 }
280
281 __u16 dn_fib_rules_policy(__u16 saddr, struct dn_fib_res *res, unsigned *flags)
282 {
283         struct dn_fib_rule *r = res->r;
284
285         if (r->r_action == RTN_NAT) {
286                 int addrtype = dnet_addr_type(r->r_srcmap);
287
288                 if (addrtype == RTN_NAT) {
289                         saddr = (saddr&~r->r_srcmask)|r->r_srcmap;
290                         *flags |= RTCF_SNAT;
291                 } else if (addrtype == RTN_LOCAL || r->r_srcmap == 0) {
292                         saddr = r->r_srcmap;
293                         *flags |= RTCF_MASQ;
294                 }
295         }
296         return saddr;
297 }
298
299 static void dn_fib_rules_detach(struct net_device *dev)
300 {
301         struct dn_fib_rule *r;
302
303         for(r = dn_fib_rules; r; r = r->r_next) {
304                 if (r->r_ifindex == dev->ifindex) {
305                         write_lock_bh(&dn_fib_rules_lock);
306                         r->r_ifindex = -1;
307                         write_unlock_bh(&dn_fib_rules_lock);
308                 }
309         }
310 }
311
312 static void dn_fib_rules_attach(struct net_device *dev)
313 {
314         struct dn_fib_rule *r;
315
316         for(r = dn_fib_rules; r; r = r->r_next) {
317                 if (r->r_ifindex == -1 && strcmp(dev->name, r->r_ifname) == 0) {
318                         write_lock_bh(&dn_fib_rules_lock);
319                         r->r_ifindex = dev->ifindex;
320                         write_unlock_bh(&dn_fib_rules_lock);
321                 }
322         }
323 }
324
325 static int dn_fib_rules_event(struct notifier_block *this, unsigned long event, void *ptr)
326 {
327         struct net_device *dev = ptr;
328
329         switch(event) {
330                 case NETDEV_UNREGISTER:
331                         dn_fib_rules_detach(dev);
332                         dn_fib_sync_down(0, dev, 1);
333                 case NETDEV_REGISTER:
334                         dn_fib_rules_attach(dev);
335                         dn_fib_sync_up(dev);
336         }
337
338         return NOTIFY_DONE;
339 }
340
341
342 static struct notifier_block dn_fib_rules_notifier = {
343         .notifier_call =        dn_fib_rules_event,
344 };
345
346 static int dn_fib_fill_rule(struct sk_buff *skb, struct dn_fib_rule *r, struct netlink_callback *cb)
347 {
348         struct rtmsg *rtm;
349         struct nlmsghdr *nlh;
350         unsigned char *b = skb->tail;
351
352
353         nlh = NLMSG_PUT(skb, NETLINK_CREDS(cb->skb)->pid, cb->nlh->nlmsg_seq, RTM_NEWRULE, sizeof(*rtm));
354         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
382 nlmsg_failure:
383 rtattr_failure:
384         skb_trim(skb, b - skb->data);
385         return -1;
386 }
387
388 int 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;
398                 if (dn_fib_fill_rule(skb, r, cb) < 0)
399                         break;
400         }
401         read_unlock(&dn_fib_rules_lock);
402         cb->args[0] = idx;
403
404         return skb->len;
405 }
406
407 void __init dn_fib_rules_init(void)
408 {
409         register_netdevice_notifier(&dn_fib_rules_notifier);
410 }
411
412 void __exit dn_fib_rules_cleanup(void)
413 {
414         unregister_netdevice_notifier(&dn_fib_rules_notifier);
415 }
416
417