Merge branch 'upstream-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/linvil...
[linux-2.6] / 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/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>
29 #include <linux/list.h>
30 #include <linux/rcupdate.h>
31 #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
41 struct dn_fib_rule
42 {
43         struct hlist_node       r_hlist;
44         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;
50         __le16                  r_src;
51         __le16                  r_srcmask;
52         __le16                  r_dst;
53         __le16                  r_dstmask;
54         __le16                  r_srcmap;
55         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;
62         struct rcu_head         rcu;
63 };
64
65 static 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
72 static struct hlist_head dn_fib_rules;
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;
79         struct hlist_node *node;
80         int err = -ESRCH;
81
82         hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) {
83                 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
99                         hlist_del_rcu(&r->r_hlist);
100                         r->r_dead = 1;
101                         dn_fib_rule_put(r);
102                         err = 0;
103                         break;
104                 }
105         }
106
107         return err;
108 }
109
110 static 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
116 void dn_fib_rule_put(struct dn_fib_rule *r)
117 {
118         if (atomic_dec_and_test(&r->r_clntref)) {
119                 if (r->r_dead)
120                         call_rcu(&r->rcu, dn_fib_rule_put_rcu);
121                 else
122                         printk(KERN_DEBUG "Attempt to free alive dn_fib_rule\n");
123         }
124 }
125
126
127 int 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);
131         struct dn_fib_rule *r, *new_r, *last = NULL;
132         struct hlist_node *node = NULL;
133         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
154         new_r = kmalloc(sizeof(*new_r), GFP_KERNEL);
155         if (!new_r)
156                 return -ENOMEM;
157         memset(new_r, 0, sizeof(*new_r));
158
159         if (rta[RTA_SRC-1])
160                 memcpy(&new_r->r_src, RTA_DATA(rta[RTA_SRC-1]), 2);
161         if (rta[RTA_DST-1])
162                 memcpy(&new_r->r_dst, RTA_DATA(rta[RTA_DST-1]), 2);
163         if (rta[RTA_GATEWAY-1])
164                 memcpy(&new_r->r_srcmap, RTA_DATA(rta[RTA_GATEWAY-1]), 2);
165         new_r->r_src_len = rtm->rtm_src_len;
166         new_r->r_dst_len = rtm->rtm_dst_len;
167         new_r->r_srcmask = dnet_make_mask(rtm->rtm_src_len);
168         new_r->r_dstmask = dnet_make_mask(rtm->rtm_dst_len);
169 #ifdef CONFIG_DECNET_ROUTE_FWMARK
170         if (rta[RTA_PROTOINFO-1])
171                 memcpy(&new_r->r_fwmark, RTA_DATA(rta[RTA_PROTOINFO-1]), 4);
172 #endif
173         new_r->r_action = rtm->rtm_type;
174         new_r->r_flags = rtm->rtm_flags;
175         if (rta[RTA_PRIORITY-1])
176                 memcpy(&new_r->r_preference, RTA_DATA(rta[RTA_PRIORITY-1]), 4);
177         new_r->r_table = table_id;
178         if (rta[RTA_IIF-1]) {
179                 struct net_device *dev;
180                 rtattr_strlcpy(new_r->r_ifname, rta[RTA_IIF-1], IFNAMSIZ);
181                 new_r->r_ifindex = -1;
182                 dev = dev_get_by_name(new_r->r_ifname);
183                 if (dev) {
184                         new_r->r_ifindex = dev->ifindex;
185                         dev_put(dev);
186                 }
187         }
188
189         r = container_of(dn_fib_rules.first, struct dn_fib_rule, r_hlist);
190         if (!new_r->r_preference) {
191                 if (r && r->r_hlist.next != NULL) {
192                         r = container_of(r->r_hlist.next, struct dn_fib_rule, r_hlist);
193                         if (r->r_preference)
194                                 new_r->r_preference = r->r_preference - 1;
195                 }
196         }
197
198         hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) {
199                 if (r->r_preference > new_r->r_preference)
200                         break;
201                 last = r;
202         }
203         atomic_inc(&new_r->r_clntref);
204
205         if (last)
206                 hlist_add_after_rcu(&last->r_hlist, &new_r->r_hlist);
207         else
208                 hlist_add_before_rcu(&new_r->r_hlist, &r->r_hlist);
209         return 0;
210 }
211
212
213 int dn_fib_lookup(const struct flowi *flp, struct dn_fib_res *res)
214 {
215         struct dn_fib_rule *r, *policy;
216         struct dn_fib_table *tb;
217         __le16 saddr = flp->fld_src;
218         __le16 daddr = flp->fld_dst;
219         struct hlist_node *node;
220         int err;
221
222         rcu_read_lock();
223
224         hlist_for_each_entry_rcu(r, node, &dn_fib_rules, r_hlist) {
225                 if (((saddr^r->r_src) & r->r_srcmask) ||
226                     ((daddr^r->r_dst) & r->r_dstmask) ||
227 #ifdef CONFIG_DECNET_ROUTE_FWMARK
228                     (r->r_fwmark && r->r_fwmark != flp->fld_fwmark) ||
229 #endif
230                     (r->r_ifindex && r->r_ifindex != flp->iif))
231                         continue;
232
233                 switch(r->r_action) {
234                         case RTN_UNICAST:
235                         case RTN_NAT:
236                                 policy = r;
237                                 break;
238                         case RTN_UNREACHABLE:
239                                 rcu_read_unlock();
240                                 return -ENETUNREACH;
241                         default:
242                         case RTN_BLACKHOLE:
243                                 rcu_read_unlock();
244                                 return -EINVAL;
245                         case RTN_PROHIBIT:
246                                 rcu_read_unlock();
247                                 return -EACCES;
248                 }
249
250                 if ((tb = dn_fib_get_table(r->r_table, 0)) == NULL)
251                         continue;
252                 err = tb->lookup(tb, flp, res);
253                 if (err == 0) {
254                         res->r = policy;
255                         if (policy)
256                                 atomic_inc(&policy->r_clntref);
257                         rcu_read_unlock();
258                         return 0;
259                 }
260                 if (err < 0 && err != -EAGAIN) {
261                         rcu_read_unlock();
262                         return err;
263                 }
264         }
265
266         rcu_read_unlock();
267         return -ESRCH;
268 }
269
270 unsigned dnet_addr_type(__le16 addr)
271 {
272         struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
273         struct dn_fib_res res;
274         unsigned ret = RTN_UNICAST;
275         struct dn_fib_table *tb = dn_fib_tables[RT_TABLE_LOCAL];
276
277         res.r = NULL;
278
279         if (tb) {
280                 if (!tb->lookup(tb, &fl, &res)) {
281                         ret = res.type;
282                         dn_fib_res_put(&res);
283                 }
284         }
285         return ret;
286 }
287
288 __le16 dn_fib_rules_policy(__le16 saddr, struct dn_fib_res *res, unsigned *flags)
289 {
290         struct dn_fib_rule *r = res->r;
291
292         if (r->r_action == RTN_NAT) {
293                 int addrtype = dnet_addr_type(r->r_srcmap);
294
295                 if (addrtype == RTN_NAT) {
296                         saddr = (saddr&~r->r_srcmask)|r->r_srcmap;
297                         *flags |= RTCF_SNAT;
298                 } else if (addrtype == RTN_LOCAL || r->r_srcmap == 0) {
299                         saddr = r->r_srcmap;
300                         *flags |= RTCF_MASQ;
301                 }
302         }
303         return saddr;
304 }
305
306 static void dn_fib_rules_detach(struct net_device *dev)
307 {
308         struct hlist_node *node;
309         struct dn_fib_rule *r;
310
311         hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) {
312                 if (r->r_ifindex == dev->ifindex)
313                         r->r_ifindex = -1;
314         }
315 }
316
317 static void dn_fib_rules_attach(struct net_device *dev)
318 {
319         struct hlist_node *node;
320         struct dn_fib_rule *r;
321
322         hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) {
323                 if (r->r_ifindex == -1 && strcmp(dev->name, r->r_ifname) == 0)
324                         r->r_ifindex = dev->ifindex;
325         }
326 }
327
328 static int dn_fib_rules_event(struct notifier_block *this, unsigned long event, void *ptr)
329 {
330         struct net_device *dev = ptr;
331
332         switch(event) {
333                 case NETDEV_UNREGISTER:
334                         dn_fib_rules_detach(dev);
335                         dn_fib_sync_down(0, dev, 1);
336                 case NETDEV_REGISTER:
337                         dn_fib_rules_attach(dev);
338                         dn_fib_sync_up(dev);
339         }
340
341         return NOTIFY_DONE;
342 }
343
344
345 static struct notifier_block dn_fib_rules_notifier = {
346         .notifier_call =        dn_fib_rules_event,
347 };
348
349 static int dn_fib_fill_rule(struct sk_buff *skb, struct dn_fib_rule *r,
350                             struct netlink_callback *cb, unsigned int flags)
351 {
352         struct rtmsg *rtm;
353         struct nlmsghdr *nlh;
354         unsigned char *b = skb->tail;
355
356
357         nlh = NLMSG_NEW_ANSWER(skb, cb, RTM_NEWRULE, sizeof(*rtm), flags);
358         rtm = NLMSG_DATA(nlh);
359         rtm->rtm_family = AF_DECnet;
360         rtm->rtm_dst_len = r->r_dst_len;
361         rtm->rtm_src_len = r->r_src_len;
362         rtm->rtm_tos = 0;
363 #ifdef CONFIG_DECNET_ROUTE_FWMARK
364         if (r->r_fwmark)
365                 RTA_PUT(skb, RTA_PROTOINFO, 4, &r->r_fwmark);
366 #endif
367         rtm->rtm_table = r->r_table;
368         rtm->rtm_protocol = 0;
369         rtm->rtm_scope = 0;
370         rtm->rtm_type = r->r_action;
371         rtm->rtm_flags = r->r_flags;
372
373         if (r->r_dst_len)
374                 RTA_PUT(skb, RTA_DST, 2, &r->r_dst);
375         if (r->r_src_len)
376                 RTA_PUT(skb, RTA_SRC, 2, &r->r_src);
377         if (r->r_ifname[0])
378                 RTA_PUT(skb, RTA_IIF, IFNAMSIZ, &r->r_ifname);
379         if (r->r_preference)
380                 RTA_PUT(skb, RTA_PRIORITY, 4, &r->r_preference);
381         if (r->r_srcmap)
382                 RTA_PUT(skb, RTA_GATEWAY, 2, &r->r_srcmap);
383         nlh->nlmsg_len = skb->tail - b;
384         return skb->len;
385
386 nlmsg_failure:
387 rtattr_failure:
388         skb_trim(skb, b - skb->data);
389         return -1;
390 }
391
392 int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
393 {
394         int idx = 0;
395         int s_idx = cb->args[0];
396         struct dn_fib_rule *r;
397         struct hlist_node *node;
398
399         rcu_read_lock();
400         hlist_for_each_entry(r, node, &dn_fib_rules, r_hlist) {
401                 if (idx < s_idx)
402                         goto next;
403                 if (dn_fib_fill_rule(skb, r, cb, NLM_F_MULTI) < 0)
404                         break;
405 next:
406                 idx++;
407         }
408         rcu_read_unlock();
409         cb->args[0] = idx;
410
411         return skb->len;
412 }
413
414 void __init dn_fib_rules_init(void)
415 {
416         INIT_HLIST_HEAD(&dn_fib_rules);
417         hlist_add_head(&default_rule.r_hlist, &dn_fib_rules);
418         register_netdevice_notifier(&dn_fib_rules_notifier);
419 }
420
421 void __exit dn_fib_rules_cleanup(void)
422 {
423         unregister_netdevice_notifier(&dn_fib_rules_notifier);
424 }
425
426