[TG3]: Add tagged status support.
[linux-2.6/verdex.git] / net / decnet / dn_rules.c
blob597587d170d8ce851349b0a5868522aedffcedbf
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.
7 * DECnet Routing Forwarding Information Base (Rules)
9 * Author: Steve Whitehouse <SteveW@ACM.org>
10 * Mostly copied from Alexey Kuznetsov's ipv4/fib_rules.c
13 * Changes:
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>
40 struct dn_fib_rule
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;
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
70 static struct dn_fib_rule *dn_fib_rules = &default_rule;
71 static DEFINE_RWLOCK(dn_fib_rules_lock);
74 int dn_fib_rtm_delrule(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
76 struct rtattr **rta = arg;
77 struct rtmsg *rtm = NLMSG_DATA(nlh);
78 struct dn_fib_rule *r, **rp;
79 int err = -ESRCH;
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))) {
94 err = -EPERM;
95 if (r == &default_rule)
96 break;
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;
108 return err;
111 void dn_fib_rule_put(struct dn_fib_rule *r)
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");
122 int dn_fib_rtm_newrule(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
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;
129 if (rtm->rtm_src_len > 16 || rtm->rtm_dst_len > 16)
130 return -EINVAL;
132 if (rta[RTA_IIF-1] && RTA_PAYLOAD(rta[RTA_IIF-1]) > IFNAMSIZ)
133 return -EINVAL;
135 if (rtm->rtm_type == RTN_NAT)
136 return -EINVAL;
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;
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);
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;
192 while((r=*rp) != NULL) {
193 if (r->r_preference > new_r->r_preference)
194 break;
195 rp = &r->r_next;
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;
207 int dn_fib_lookup(const struct flowi *flp, struct dn_fib_res *res)
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;
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;
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;
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;
252 if (err < 0 && err != -EAGAIN) {
253 read_unlock(&dn_fib_rules_lock);
254 return err;
258 read_unlock(&dn_fib_rules_lock);
259 return -ESRCH;
262 unsigned dnet_addr_type(__u16 addr)
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];
269 res.r = NULL;
271 if (tb) {
272 if (!tb->lookup(tb, &fl, &res)) {
273 ret = res.type;
274 dn_fib_res_put(&res);
277 return ret;
280 __u16 dn_fib_rules_policy(__u16 saddr, struct dn_fib_res *res, unsigned *flags)
282 struct dn_fib_rule *r = res->r;
284 if (r->r_action == RTN_NAT) {
285 int addrtype = dnet_addr_type(r->r_srcmap);
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;
295 return saddr;
298 static void dn_fib_rules_detach(struct net_device *dev)
300 struct dn_fib_rule *r;
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);
311 static void dn_fib_rules_attach(struct net_device *dev)
313 struct dn_fib_rule *r;
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);
324 static int dn_fib_rules_event(struct notifier_block *this, unsigned long event, void *ptr)
326 struct net_device *dev = ptr;
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);
337 return NOTIFY_DONE;
341 static struct notifier_block dn_fib_rules_notifier = {
342 .notifier_call = dn_fib_rules_event,
345 static int dn_fib_fill_rule(struct sk_buff *skb, struct dn_fib_rule *r, struct netlink_callback *cb)
347 struct rtmsg *rtm;
348 struct nlmsghdr *nlh;
349 unsigned char *b = skb->tail;
352 nlh = NLMSG_PUT(skb, NETLINK_CREDS(cb->skb)->pid, cb->nlh->nlmsg_seq, RTM_NEWRULE, sizeof(*rtm));
353 rtm = NLMSG_DATA(nlh);
354 rtm->rtm_family = AF_DECnet;
355 rtm->rtm_dst_len = r->r_dst_len;
356 rtm->rtm_src_len = r->r_src_len;
357 rtm->rtm_tos = 0;
358 #ifdef CONFIG_DECNET_ROUTE_FWMARK
359 if (r->r_fwmark)
360 RTA_PUT(skb, RTA_PROTOINFO, 4, &r->r_fwmark);
361 #endif
362 rtm->rtm_table = r->r_table;
363 rtm->rtm_protocol = 0;
364 rtm->rtm_scope = 0;
365 rtm->rtm_type = r->r_action;
366 rtm->rtm_flags = r->r_flags;
368 if (r->r_dst_len)
369 RTA_PUT(skb, RTA_DST, 2, &r->r_dst);
370 if (r->r_src_len)
371 RTA_PUT(skb, RTA_SRC, 2, &r->r_src);
372 if (r->r_ifname[0])
373 RTA_PUT(skb, RTA_IIF, IFNAMSIZ, &r->r_ifname);
374 if (r->r_preference)
375 RTA_PUT(skb, RTA_PRIORITY, 4, &r->r_preference);
376 if (r->r_srcmap)
377 RTA_PUT(skb, RTA_GATEWAY, 2, &r->r_srcmap);
378 nlh->nlmsg_len = skb->tail - b;
379 return skb->len;
381 nlmsg_failure:
382 rtattr_failure:
383 skb_trim(skb, b - skb->data);
384 return -1;
387 int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
389 int idx;
390 int s_idx = cb->args[0];
391 struct dn_fib_rule *r;
393 read_lock(&dn_fib_rules_lock);
394 for(r = dn_fib_rules, idx = 0; r; r = r->r_next, idx++) {
395 if (idx < s_idx)
396 continue;
397 if (dn_fib_fill_rule(skb, r, cb) < 0)
398 break;
400 read_unlock(&dn_fib_rules_lock);
401 cb->args[0] = idx;
403 return skb->len;
406 void __init dn_fib_rules_init(void)
408 register_netdevice_notifier(&dn_fib_rules_notifier);
411 void __exit dn_fib_rules_cleanup(void)
413 unregister_netdevice_notifier(&dn_fib_rules_notifier);