update atp870u driver to 0.78 from D-Link source
[linux-2.4.git] / net / decnet / dn_fib.c
1 /*
2  * DECnet       An implementation of the DECnet protocol suite for the LINUX
3  *              operating system.  DECnet is implemented using the  BSD Socket
4  *              interface as the means of communication with the user level.
5  *
6  *              DECnet Routing Forwarding Information Base (Glue/Info List)
7  *
8  * Author:      Steve Whitehouse <SteveW@ACM.org>
9  *
10  *
11  * Changes:
12  *              Alexey Kuznetsov : SMP locking changes
13  *              Steve Whitehouse : Rewrote it... Well to be more correct, I
14  *                                 copied most of it from the ipv4 fib code.
15  *
16  */
17 #include <linux/config.h>
18 #include <linux/string.h>
19 #include <linux/net.h>
20 #include <linux/socket.h>
21 #include <linux/sockios.h>
22 #include <linux/init.h>
23 #include <linux/skbuff.h>
24 #include <linux/netlink.h>
25 #include <linux/rtnetlink.h>
26 #include <linux/proc_fs.h>
27 #include <linux/netdevice.h>
28 #include <linux/timer.h>
29 #include <linux/spinlock.h>
30 #include <asm/atomic.h>
31 #include <asm/uaccess.h>
32 #include <net/neighbour.h>
33 #include <net/dst.h>
34 #include <net/dn.h>
35 #include <net/dn_route.h>
36 #include <net/dn_fib.h>
37 #include <net/dn_neigh.h>
38 #include <net/dn_dev.h>
39
40
41 #define for_fib_info() { struct dn_fib_info *fi;\
42         for(fi = dn_fib_info_list; fi; fi = fi->fib_next)
43 #define endfor_fib_info() }
44
45 #define for_nexthops(fi) { int nhsel; const struct dn_fib_nh *nh;\
46         for(nhsel = 0, nh = (fi)->fib_nh; nhsel < (fi)->fib_nhs; nh++, nhsel++)
47
48 #define change_nexthops(fi) { int nhsel; struct dn_fib_nh *nh;\
49         for(nhsel = 0, nh = (struct dn_fib_nh *)((fi)->fib_nh); nhsel < (fi)->fib_nhs; nh++, nhsel++)
50
51 #define endfor_nexthops(fi) }
52
53 extern int dn_cache_dump(struct sk_buff *skb, struct netlink_callback *cb);
54
55
56 static struct dn_fib_info *dn_fib_info_list;
57 static rwlock_t dn_fib_info_lock = RW_LOCK_UNLOCKED;
58 int dn_fib_info_cnt;
59
60 static struct
61 {
62         int error;
63         u8 scope;
64 } dn_fib_props[RTN_MAX+1] = {
65         { 0, RT_SCOPE_NOWHERE },                /* RTN_UNSPEC */
66         { 0, RT_SCOPE_UNIVERSE },               /* RTN_UNICAST */
67         { 0, RT_SCOPE_HOST },                   /* RTN_LOCAL */
68         { -EINVAL, RT_SCOPE_NOWHERE },          /* RTN_BROADCAST */
69         { -EINVAL, RT_SCOPE_NOWHERE },          /* RTN_ANYCAST */
70         { -EINVAL, RT_SCOPE_NOWHERE },          /* RTN_MULTICAST */
71         { -EINVAL, RT_SCOPE_UNIVERSE },         /* RTN_BLACKHOLE */
72         { -EHOSTUNREACH, RT_SCOPE_UNIVERSE },   /* RTN_UNREACHABLE */
73         { -EACCES, RT_SCOPE_UNIVERSE },         /* RTN_PROHIBIT */
74         { -EAGAIN, RT_SCOPE_UNIVERSE },         /* RTN_THROW */
75         { -EINVAL, RT_SCOPE_NOWHERE },          /* RTN_NAT */
76         { -EINVAL, RT_SCOPE_NOWHERE }           /* RTN_XRESOLVE */
77 };
78
79 void dn_fib_free_info(struct dn_fib_info *fi)
80 {
81         if (fi->fib_dead == 0) {
82                 printk(KERN_DEBUG "DECnet: BUG! Attempt to free alive dn_fib_info\n");
83                 return;
84         }
85
86         change_nexthops(fi) {
87                 if (nh->nh_dev)
88                         dev_put(nh->nh_dev);
89                 nh->nh_dev = NULL;
90         } endfor_nexthops(fi);
91         dn_fib_info_cnt--;
92         kfree(fi);
93 }
94
95 void dn_fib_release_info(struct dn_fib_info *fi)
96 {
97         write_lock(&dn_fib_info_lock);
98         if (fi && --fi->fib_treeref == 0) {
99                 if (fi->fib_next)
100                         fi->fib_next->fib_prev = fi->fib_prev;
101                 if (fi->fib_prev)
102                         fi->fib_prev->fib_next = fi->fib_next;
103                 if (fi == dn_fib_info_list)
104                         dn_fib_info_list = fi->fib_next;
105                 fi->fib_dead = 1;
106                 dn_fib_info_put(fi);
107         }
108         write_unlock(&dn_fib_info_lock);
109 }
110
111 static __inline__ int dn_fib_nh_comp(const struct dn_fib_info *fi, const struct dn_fib_info *ofi)
112 {
113         const struct dn_fib_nh *onh = ofi->fib_nh;
114
115         for_nexthops(fi) {
116                 if (nh->nh_oif != onh->nh_oif ||
117                         nh->nh_gw != onh->nh_gw ||
118                         nh->nh_scope != onh->nh_scope ||
119                         nh->nh_weight != onh->nh_weight ||
120                         ((nh->nh_flags^onh->nh_flags)&~RTNH_F_DEAD))
121                                 return -1;
122                 onh++;
123         } endfor_nexthops(fi);
124         return 0;
125 }
126
127 static __inline__ struct dn_fib_info *dn_fib_find_info(const struct dn_fib_info *nfi)
128 {
129         for_fib_info() {
130                 if (fi->fib_nhs != nfi->fib_nhs)
131                         continue;
132                 if (nfi->fib_protocol == fi->fib_protocol &&
133                         nfi->fib_prefsrc == fi->fib_prefsrc &&
134                         nfi->fib_priority == fi->fib_priority &&
135                         ((nfi->fib_flags^fi->fib_flags)&~RTNH_F_DEAD) == 0 &&
136                         (nfi->fib_nhs == 0 || dn_fib_nh_comp(fi, nfi) == 0))
137                                 return fi;
138         } endfor_fib_info();
139         return NULL;
140 }
141
142 u16 dn_fib_get_attr16(struct rtattr *attr, int attrlen, int type)
143 {
144         while(RTA_OK(attr,attrlen)) {
145                 if (attr->rta_type == type)
146                         return *(u16*)RTA_DATA(attr);
147                 attr = RTA_NEXT(attr, attrlen);
148         }
149
150         return 0;
151 }
152
153 static int dn_fib_count_nhs(struct rtattr *rta)
154 {
155         int nhs = 0;
156         struct rtnexthop *nhp = RTA_DATA(rta);
157         int nhlen = RTA_PAYLOAD(rta);
158
159         while(nhlen >= (int)sizeof(struct rtnexthop)) {
160                 if ((nhlen -= nhp->rtnh_len) < 0)
161                         return 0;
162                 nhs++;
163                 nhp = RTNH_NEXT(nhp);
164         }
165
166         return nhs;
167 }
168
169 static int dn_fib_get_nhs(struct dn_fib_info *fi, const struct rtattr *rta, const struct rtmsg *r)
170 {
171         struct rtnexthop *nhp = RTA_DATA(rta);
172         int nhlen = RTA_PAYLOAD(rta);
173
174         change_nexthops(fi) {
175                 int attrlen = nhlen - sizeof(struct rtnexthop);
176                 if (attrlen < 0 || (nhlen -= nhp->rtnh_len) < 0)
177                         return -EINVAL;
178
179                 nh->nh_flags  = (r->rtm_flags&~0xFF) | nhp->rtnh_flags;
180                 nh->nh_oif    = nhp->rtnh_ifindex;
181                 nh->nh_weight = nhp->rtnh_hops + 1;
182
183                 if (attrlen) {
184                         nh->nh_gw = dn_fib_get_attr16(RTNH_DATA(nhp), attrlen, RTA_GATEWAY);
185                 }
186                 nhp = RTNH_NEXT(nhp);
187         } endfor_nexthops(fi);
188
189         return 0;
190 }
191
192
193 static int dn_fib_check_nh(const struct rtmsg *r, struct dn_fib_info *fi, struct dn_fib_nh *nh)
194 {
195         int err;
196
197         if (nh->nh_gw) {
198                 struct dn_fib_key key;
199                 struct dn_fib_res res;
200
201                 if (nh->nh_flags&RTNH_F_ONLINK) {
202                         struct net_device *dev;
203
204                         if (r->rtm_scope >= RT_SCOPE_LINK)
205                                 return -EINVAL;
206                         if ((dev = __dev_get_by_index(nh->nh_oif)) == NULL)
207                                 return -ENODEV;
208                         if (!(dev->flags&IFF_UP))
209                                 return -ENETDOWN;
210                         nh->nh_dev = dev;
211                         atomic_inc(&dev->refcnt);
212                         nh->nh_scope = RT_SCOPE_LINK;
213                         return 0;
214                 }
215
216                 memset(&key, 0, sizeof(key));
217                 key.dst = nh->nh_gw;
218                 key.oif = nh->nh_oif;
219                 key.scope = r->rtm_scope + 1;
220
221                 if (key.scope < RT_SCOPE_LINK)
222                         key.scope = RT_SCOPE_LINK;
223
224                 if ((err = dn_fib_lookup(&key, &res)) != 0)
225                         return err;
226
227                 nh->nh_scope = res.scope;
228                 nh->nh_oif = DN_FIB_RES_OIF(res);
229                 nh->nh_dev = DN_FIB_RES_DEV(res);
230                 if (nh->nh_dev)
231                         atomic_inc(&nh->nh_dev->refcnt);
232                 dn_fib_res_put(&res);
233         } else {
234                 struct net_device *dev;
235
236                 if (nh->nh_flags&(RTNH_F_PERVASIVE|RTNH_F_ONLINK))
237                         return -EINVAL;
238
239                 dev = __dev_get_by_index(nh->nh_oif);
240                 if (dev == NULL || dev->dn_ptr == NULL)
241                         return -ENODEV;
242                 if (!(dev->flags&IFF_UP))
243                         return -ENETDOWN;
244                 nh->nh_dev = dev;
245                 atomic_inc(&nh->nh_dev->refcnt);
246                 nh->nh_scope = RT_SCOPE_HOST;
247         }
248
249         return 0;
250 }
251
252
253 struct dn_fib_info *dn_fib_create_info(const struct rtmsg *r, struct dn_kern_rta *rta, const struct nlmsghdr *nlh, int *errp)
254 {
255         int err;
256         struct dn_fib_info *fi = NULL;
257         struct dn_fib_info *ofi;
258         int nhs = 1;
259
260         if (r->rtm_type > RTN_MAX)
261                 goto err_inval;
262
263         if (dn_fib_props[r->rtm_type].scope > r->rtm_scope)
264                 goto err_inval;
265
266         if (rta->rta_mp) {
267                 nhs = dn_fib_count_nhs(rta->rta_mp);
268                 if (nhs == 0)
269                         goto err_inval;
270         }
271
272         fi = kmalloc(sizeof(*fi)+nhs*sizeof(struct dn_fib_nh), GFP_KERNEL);
273         err = -ENOBUFS;
274         if (fi == NULL)
275                 goto failure;
276         memset(fi, 0, sizeof(*fi)+nhs*sizeof(struct dn_fib_nh));
277
278         fi->fib_protocol = r->rtm_protocol;
279         fi->fib_nhs = nhs;
280         fi->fib_flags = r->rtm_flags;
281         if (rta->rta_priority)
282                 fi->fib_priority = *rta->rta_priority;
283         if (rta->rta_prefsrc)
284                 memcpy(&fi->fib_prefsrc, rta->rta_prefsrc, 2);
285
286         if (rta->rta_mp) {
287                 if ((err = dn_fib_get_nhs(fi, rta->rta_mp, r)) != 0)
288                         goto failure;
289                 if (rta->rta_oif && fi->fib_nh->nh_oif != *rta->rta_oif)
290                         goto err_inval;
291                 if (rta->rta_gw && memcmp(&fi->fib_nh->nh_gw, rta->rta_gw, 2))
292                         goto err_inval;
293         } else {
294                 struct dn_fib_nh *nh = fi->fib_nh;
295                 if (rta->rta_oif)
296                         nh->nh_oif = *rta->rta_oif;
297                 if (rta->rta_gw)
298                         memcpy(&nh->nh_gw, rta->rta_gw, 2);
299                 nh->nh_flags = r->rtm_flags;
300                 nh->nh_weight = 1;
301         }
302
303         if (dn_fib_props[r->rtm_type].error) {
304                 if (rta->rta_gw || rta->rta_oif || rta->rta_mp)
305                         goto err_inval;
306                 goto link_it;
307         }
308
309         if (r->rtm_scope > RT_SCOPE_HOST)
310                 goto err_inval;
311
312         if (r->rtm_scope == RT_SCOPE_HOST) {
313                 struct dn_fib_nh *nh = fi->fib_nh;
314
315                 /* Local address is added */
316                 if (nhs != 1 || nh->nh_gw)
317                         goto err_inval;
318                 nh->nh_scope = RT_SCOPE_NOWHERE;
319                 nh->nh_dev = dev_get_by_index(fi->fib_nh->nh_oif);
320                 err = -ENODEV;
321                 if (nh->nh_dev == NULL)
322                         goto failure;
323         } else {
324                 change_nexthops(fi) {
325                         if ((err = dn_fib_check_nh(r, fi, nh)) != 0)
326                                 goto failure;
327                 } endfor_nexthops(fi)
328         }
329
330 #if I_GET_AROUND_TO_FIXING_PREFSRC
331         if (fi->fib_prefsrc) {
332                 if (r->rtm_type != RTN_LOCAL || rta->rta_dst == NULL ||
333                     memcmp(&fi->fib_prefsrc, rta->rta_dst, 2))
334                         if (dn_addr_type(fi->fib_prefsrc) != RTN_LOCAL)
335                                 goto err_inval;
336         }
337 #endif
338
339 link_it:
340         if ((ofi = dn_fib_find_info(fi)) != NULL) {
341                 fi->fib_dead = 1;
342                 dn_fib_free_info(fi);
343                 ofi->fib_treeref++;
344                 return ofi;
345         }
346
347         fi->fib_treeref++;
348         atomic_inc(&fi->fib_clntref);
349         write_lock(&dn_fib_info_lock);
350         fi->fib_next = dn_fib_info_list;
351         fi->fib_prev = NULL;
352         if (dn_fib_info_list)
353                 dn_fib_info_list->fib_prev = fi;
354         dn_fib_info_list = fi;
355         dn_fib_info_cnt++;
356         write_unlock(&dn_fib_info_lock);
357         return fi;
358
359 err_inval:
360         err = -EINVAL;
361
362 failure:
363         *errp = err;
364         if (fi) {
365                 fi->fib_dead = 1;
366                 dn_fib_free_info(fi);
367         }
368
369         return NULL;
370 }
371
372
373 void dn_fib_select_multipath(const struct dn_fib_key *key, struct dn_fib_res *res)
374 {
375         struct dn_fib_info *fi = res->fi;
376         int w;
377
378         if (fi->fib_power <= 0) {
379                 int power = 0;
380                 change_nexthops(fi) {
381                         if (!(nh->nh_flags&RTNH_F_DEAD)) {
382                                 power += nh->nh_weight;
383                                 nh->nh_power = nh->nh_weight;
384                         }
385                 } endfor_nexthops(fi);
386                 fi->fib_power = power;
387         }
388
389         w = jiffies % fi->fib_power;
390
391         change_nexthops(fi) {
392                 if (!(nh->nh_flags&RTNH_F_DEAD) && nh->nh_power) {
393                         if ((w -= nh->nh_power) <= 0) {
394                                 nh->nh_power--;
395                                 fi->fib_power--;
396                                 res->nh_sel = nhsel;
397                                 return;
398                         }
399                 }
400         } endfor_nexthops(fi);
401
402         printk(KERN_DEBUG "DECnet: BUG! dn_fib_select_multipath\n");
403 }
404
405
406
407 /*
408  * Punt to user via netlink for example, but for now
409  * we just drop it.
410  */
411 int dn_fib_rt_message(struct sk_buff *skb)
412 {
413         kfree_skb(skb);
414
415         return 0;
416 }
417
418
419 static int dn_fib_check_attr(struct rtmsg *r, struct rtattr **rta)
420 {
421         int i;
422
423         for(i = 1; i <= RTA_MAX; i++) {
424                 struct rtattr *attr = rta[i-1];
425                 if (attr) {
426                         if (RTA_PAYLOAD(attr) < 4 && RTA_PAYLOAD(attr) != 2)
427                                 return -EINVAL;
428                         if (i != RTA_MULTIPATH && i != RTA_METRICS)
429                                 rta[i-1] = (struct rtattr *)RTA_DATA(attr);
430                 }
431         }
432
433         return 0;
434 }
435
436 int dn_fib_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
437 {
438         struct dn_fib_table *tb;
439         struct rtattr **rta = arg;
440         struct rtmsg *r = NLMSG_DATA(nlh);
441
442         if (dn_fib_check_attr(r, rta))
443                 return -EINVAL;
444
445         tb = dn_fib_get_table(r->rtm_table, 0);
446         if (tb)
447                 return tb->delete(tb, r, (struct dn_kern_rta *)rta, nlh, &NETLINK_CB(skb));
448
449         return -ESRCH;
450 }
451
452 int dn_fib_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
453 {
454         struct dn_fib_table *tb;
455         struct rtattr **rta = arg;
456         struct rtmsg *r = NLMSG_DATA(nlh);
457
458         if (dn_fib_check_attr(r, rta))
459                 return -EINVAL;
460
461         tb = dn_fib_get_table(r->rtm_table, 1);
462         if (tb) 
463                 return tb->insert(tb, r, (struct dn_kern_rta *)rta, nlh, &NETLINK_CB(skb));
464
465         return -ENOBUFS;
466 }
467
468
469 int dn_fib_dump(struct sk_buff *skb, struct netlink_callback *cb)
470 {
471         int t;
472         int s_t;
473         struct dn_fib_table *tb;
474
475         if (NLMSG_PAYLOAD(cb->nlh, 0) >= sizeof(struct rtmsg) &&
476                 ((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED)
477                         return dn_cache_dump(skb, cb);
478
479         s_t = cb->args[0];
480         if (s_t == 0)
481                 s_t = cb->args[0] = DN_MIN_TABLE;
482
483         for(t = s_t; t < DN_NUM_TABLES; t++) {
484                 if (t < s_t)
485                         continue;
486                 if (t > s_t)
487                         memset(&cb->args[1], 0, sizeof(cb->args)-sizeof(int));
488                 tb = dn_fib_get_table(t, 0);
489                 if (tb == NULL)
490                         continue;
491                 if (tb->dump(tb, skb, cb) < 0)
492                         break;
493         }
494
495         cb->args[0] = t;
496
497         return skb->len;
498 }
499
500 int dn_fib_sync_down(dn_address local, struct net_device *dev, int force)
501 {
502         int ret = 0;
503         int scope = RT_SCOPE_NOWHERE;
504
505         if (force)
506                 scope = -1;
507
508         for_fib_info() {
509                 /* 
510                  * This makes no sense for DECnet.... we will almost
511                  * certainly have more than one local address the same
512                  * over all our interfaces. It needs thinking about
513                  * some more.
514                  */
515                 if (local && fi->fib_prefsrc == local) {
516                         fi->fib_flags |= RTNH_F_DEAD;
517                         ret++;
518                 } else if (dev && fi->fib_nhs) {
519                         int dead = 0;
520
521                         change_nexthops(fi) {
522                                 if (nh->nh_flags&RTNH_F_DEAD)
523                                         dead++;
524                                 else if (nh->nh_dev == dev &&
525                                                 nh->nh_scope != scope) {
526                                         nh->nh_flags |= RTNH_F_DEAD;
527                                         fi->fib_power -= nh->nh_power;
528                                         nh->nh_power = 0;
529                                         dead++;
530                                 }
531                         } endfor_nexthops(fi)
532                         if (dead == fi->fib_nhs) {
533                                 fi->fib_flags |= RTNH_F_DEAD;
534                                 ret++;
535                         }
536                 }
537         } endfor_fib_info();
538         return ret;
539 }
540
541
542 int dn_fib_sync_up(struct net_device *dev)
543 {
544         int ret = 0;
545
546         if (!(dev->flags&IFF_UP))
547                 return 0;
548
549         for_fib_info() {
550                 int alive = 0;
551
552                 change_nexthops(fi) {
553                         if (!(nh->nh_flags&RTNH_F_DEAD)) {
554                                 alive++;
555                                 continue;
556                         }
557                         if (nh->nh_dev == NULL || !(nh->nh_dev->flags&IFF_UP))
558                                 continue;
559                         if (nh->nh_dev != dev || dev->dn_ptr == NULL)
560                                 continue;
561                         alive++;
562                         nh->nh_power = 0;
563                         nh->nh_flags &= ~RTNH_F_DEAD;
564                 } endfor_nexthops(fi);
565
566                 if (alive == fi->fib_nhs) {
567                         fi->fib_flags &= ~RTNH_F_DEAD;
568                         ret++;
569                 }
570         } endfor_fib_info();
571         return ret;
572 }
573
574 void dn_fib_flush(void)
575 {
576         int flushed = 0;
577         struct dn_fib_table *tb;
578         int id;
579
580         for(id = DN_NUM_TABLES; id > 0; id--) {
581                 if ((tb = dn_fib_get_table(id, 0)) == NULL)
582                         continue;
583                 flushed += tb->flush(tb);
584         }
585
586         if (flushed)
587                 dn_rt_cache_flush(-1);
588 }
589
590 int dn_fib_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
591 {
592
593         if (!capable(CAP_NET_ADMIN))
594                 return -EPERM;
595
596         switch(cmd) {
597                 case SIOCADDRT:
598                 case SIOCDELRT:
599                         return 0;
600         }
601
602         return -EINVAL;
603 }
604
605 #ifdef CONFIG_PROC_FS
606
607 static int decnet_rt_get_info(char *buffer, char **start, off_t offset, int length)
608 {
609         int first = offset / 128;
610         char *ptr = buffer;
611         int count = (length + 127) / 128;
612         int len;
613         int i;
614         struct dn_fib_table *tb;
615
616         *start = buffer + (offset % 128);
617
618         if (--first < 0) {
619                 sprintf(buffer, "%-127s\n", "Iface\tDest\tGW  \tFlags\tRefCnt\tUse\tMetric\tMask\t\tMTU\tWindow\tIRTT");
620                 --count;
621                 ptr += 128;
622                 first = 0;
623         }
624
625
626         for(i = DN_MIN_TABLE; (i <= DN_NUM_TABLES) && (count > 0); i++) {
627                 if ((tb = dn_fib_get_table(i, 0)) != NULL) {
628                         int n = tb->get_info(tb, ptr, first, count);
629                         count -= n;
630                         ptr += n * 128;
631                 }
632         }
633
634         len = ptr - *start;
635         if (len >= length)
636                 return length;
637         if (len >= 0)
638                 return len;
639
640         return 0;
641 }
642 #endif /* CONFIG_PROC_FS */
643
644 void __exit dn_fib_cleanup(void)
645 {
646         proc_net_remove("decnet_route");
647
648         dn_fib_table_cleanup();
649         dn_fib_rules_cleanup();
650 }
651
652
653 void __init dn_fib_init(void)
654 {
655
656 #ifdef CONFIG_PROC_FS
657         proc_net_create("decnet_route", 0, decnet_rt_get_info);
658 #endif
659
660         dn_fib_table_init();
661         dn_fib_rules_init();
662 }
663
664