vserver 1.9.3
[linux-2.6.git] / net / ipv6 / anycast.c
1 /*
2  *      Anycast support for IPv6
3  *      Linux INET6 implementation 
4  *
5  *      Authors:
6  *      David L Stevens (dlstevens@us.ibm.com)
7  *
8  *      based heavily on net/ipv6/mcast.c
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/errno.h>
19 #include <linux/types.h>
20 #include <linux/random.h>
21 #include <linux/string.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.h>
24 #include <linux/sched.h>
25 #include <linux/net.h>
26 #include <linux/in6.h>
27 #include <linux/netdevice.h>
28 #include <linux/if_arp.h>
29 #include <linux/route.h>
30 #include <linux/init.h>
31 #include <linux/proc_fs.h>
32 #include <linux/seq_file.h>
33
34 #include <net/sock.h>
35 #include <net/snmp.h>
36
37 #include <net/ipv6.h>
38 #include <net/protocol.h>
39 #include <net/if_inet6.h>
40 #include <net/ndisc.h>
41 #include <net/addrconf.h>
42 #include <net/ip6_route.h>
43
44 #include <net/checksum.h>
45
46 /* Big ac list lock for all the sockets */
47 static rwlock_t ipv6_sk_ac_lock = RW_LOCK_UNLOCKED;
48
49 /* XXX ip6_addr_match() and ip6_onlink() really belong in net/core.c */
50
51 static int
52 ip6_addr_match(struct in6_addr *addr1, struct in6_addr *addr2, int prefix)
53 {
54         __u32   mask;
55         int     i;
56
57         if (prefix > 128 || prefix < 0)
58                 return 0;
59         if (prefix == 0)
60                 return 1;
61         for (i=0; i<4; ++i) {
62                 if (prefix >= 32)
63                         mask = ~0;
64                 else
65                         mask = htonl(~0 << (32 - prefix));
66                 if ((addr1->s6_addr32[i] ^ addr2->s6_addr32[i]) & mask)
67                         return 0;
68                 prefix -= 32;
69                 if (prefix <= 0)
70                         break;
71         }
72         return 1;
73 }
74
75 static int
76 ip6_onlink(struct in6_addr *addr, struct net_device *dev)
77 {
78         struct inet6_dev        *idev;
79         struct inet6_ifaddr     *ifa;
80         int     onlink;
81
82         onlink = 0;
83         read_lock(&addrconf_lock);
84         idev = __in6_dev_get(dev);
85         if (idev) {
86                 read_lock_bh(&idev->lock);
87                 for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) {
88                         onlink = ip6_addr_match(addr, &ifa->addr,
89                                         ifa->prefix_len);
90                         if (onlink)
91                                 break;
92                 }
93                 read_unlock_bh(&idev->lock);
94         }
95         read_unlock(&addrconf_lock);
96         return onlink;
97 }
98
99 /*
100  *      socket join an anycast group
101  */
102
103 int ipv6_sock_ac_join(struct sock *sk, int ifindex, struct in6_addr *addr)
104 {
105         struct ipv6_pinfo *np = inet6_sk(sk);
106         struct net_device *dev = NULL;
107         struct inet6_dev *idev;
108         struct ipv6_ac_socklist *pac;
109         int     ishost = !ipv6_devconf.forwarding;
110         int     err = 0;
111
112         if (!capable(CAP_NET_ADMIN))
113                 return -EPERM;
114         if (ipv6_addr_is_multicast(addr))
115                 return -EINVAL;
116         if (ipv6_chk_addr(addr, NULL, 0))
117                 return -EINVAL;
118
119         pac = sock_kmalloc(sk, sizeof(struct ipv6_ac_socklist), GFP_KERNEL);
120         if (pac == NULL)
121                 return -ENOMEM;
122         pac->acl_next = NULL;
123         ipv6_addr_copy(&pac->acl_addr, addr);
124
125         if (ifindex == 0) {
126                 struct rt6_info *rt;
127
128                 rt = rt6_lookup(addr, NULL, 0, 0);
129                 if (rt) {
130                         dev = rt->rt6i_dev;
131                         dev_hold(dev);
132                         dst_release(&rt->u.dst);
133                 } else if (ishost) {
134                         err = -EADDRNOTAVAIL;
135                         goto out_free_pac;
136                 } else {
137                         /* router, no matching interface: just pick one */
138
139                         dev = dev_get_by_flags(IFF_UP, IFF_UP|IFF_LOOPBACK);
140                 }
141         } else
142                 dev = dev_get_by_index(ifindex);
143
144         if (dev == NULL) {
145                 err = -ENODEV;
146                 goto out_free_pac;
147         }
148
149         idev = in6_dev_get(dev);
150         if (!idev) {
151                 if (ifindex)
152                         err = -ENODEV;
153                 else
154                         err = -EADDRNOTAVAIL;
155                 goto out_dev_put;
156         }
157         /* reset ishost, now that we have a specific device */
158         ishost = !idev->cnf.forwarding;
159         in6_dev_put(idev);
160
161         pac->acl_ifindex = dev->ifindex;
162
163         /* XXX
164          * For hosts, allow link-local or matching prefix anycasts.
165          * This obviates the need for propagating anycast routes while
166          * still allowing some non-router anycast participation.
167          */
168         if (!ip6_onlink(addr, dev)) {
169                 if (ishost)
170                         err = -EADDRNOTAVAIL;
171                 if (err)
172                         goto out_dev_put;
173         }
174
175         err = ipv6_dev_ac_inc(dev, addr);
176         if (err)
177                 goto out_dev_put;
178
179         write_lock_bh(&ipv6_sk_ac_lock);
180         pac->acl_next = np->ipv6_ac_list;
181         np->ipv6_ac_list = pac;
182         write_unlock_bh(&ipv6_sk_ac_lock);
183
184         dev_put(dev);
185
186         return 0;
187
188 out_dev_put:
189         dev_put(dev);
190 out_free_pac:
191         sock_kfree_s(sk, pac, sizeof(*pac));
192         return err;
193 }
194
195 /*
196  *      socket leave an anycast group
197  */
198 int ipv6_sock_ac_drop(struct sock *sk, int ifindex, struct in6_addr *addr)
199 {
200         struct ipv6_pinfo *np = inet6_sk(sk);
201         struct net_device *dev;
202         struct ipv6_ac_socklist *pac, *prev_pac;
203
204         write_lock_bh(&ipv6_sk_ac_lock);
205         prev_pac = NULL;
206         for (pac = np->ipv6_ac_list; pac; pac = pac->acl_next) {
207                 if ((ifindex == 0 || pac->acl_ifindex == ifindex) &&
208                      ipv6_addr_cmp(&pac->acl_addr, addr) == 0)
209                         break;
210                 prev_pac = pac;
211         }
212         if (!pac) {
213                 write_unlock_bh(&ipv6_sk_ac_lock);
214                 return -ENOENT;
215         }
216         if (prev_pac)
217                 prev_pac->acl_next = pac->acl_next;
218         else
219                 np->ipv6_ac_list = pac->acl_next;
220
221         write_unlock_bh(&ipv6_sk_ac_lock);
222
223         dev = dev_get_by_index(pac->acl_ifindex);
224         if (dev) {
225                 ipv6_dev_ac_dec(dev, &pac->acl_addr);
226                 dev_put(dev);
227         }
228         sock_kfree_s(sk, pac, sizeof(*pac));
229         return 0;
230 }
231
232 void ipv6_sock_ac_close(struct sock *sk)
233 {
234         struct ipv6_pinfo *np = inet6_sk(sk);
235         struct net_device *dev = NULL;
236         struct ipv6_ac_socklist *pac;
237         int     prev_index;
238
239         write_lock_bh(&ipv6_sk_ac_lock);
240         pac = np->ipv6_ac_list;
241         np->ipv6_ac_list = NULL;
242         write_unlock_bh(&ipv6_sk_ac_lock);
243
244         prev_index = 0;
245         while (pac) {
246                 struct ipv6_ac_socklist *next = pac->acl_next;
247
248                 if (pac->acl_ifindex != prev_index) {
249                         if (dev)
250                                 dev_put(dev);
251                         dev = dev_get_by_index(pac->acl_ifindex);
252                         prev_index = pac->acl_ifindex;
253                 }
254                 if (dev)
255                         ipv6_dev_ac_dec(dev, &pac->acl_addr);
256                 sock_kfree_s(sk, pac, sizeof(*pac));
257                 pac = next;
258         }
259         if (dev)
260                 dev_put(dev);
261 }
262
263 #if 0
264 /* The function is not used, which is funny. Apparently, author
265  * supposed to use it to filter out datagrams inside udp/raw but forgot.
266  *
267  * It is OK, anycasts are not special comparing to delivery to unicasts.
268  */
269
270 int inet6_ac_check(struct sock *sk, struct in6_addr *addr, int ifindex)
271 {
272         struct ipv6_ac_socklist *pac;
273         struct ipv6_pinfo *np = inet6_sk(sk);
274         int     found;
275
276         found = 0;
277         read_lock(&ipv6_sk_ac_lock);
278         for (pac=np->ipv6_ac_list; pac; pac=pac->acl_next) {
279                 if (ifindex && pac->acl_ifindex != ifindex)
280                         continue;
281                 found = ipv6_addr_cmp(&pac->acl_addr, addr) == 0;
282                 if (found)
283                         break;
284         }
285         read_unlock(&ipv6_sk_ac_lock);
286
287         return found;
288 }
289
290 #endif
291
292 static void aca_put(struct ifacaddr6 *ac)
293 {
294         if (atomic_dec_and_test(&ac->aca_refcnt)) {
295                 in6_dev_put(ac->aca_idev);
296                 dst_release(&ac->aca_rt->u.dst);
297                 kfree(ac);
298         }
299 }
300
301 /*
302  *      device anycast group inc (add if not found)
303  */
304 int ipv6_dev_ac_inc(struct net_device *dev, struct in6_addr *addr)
305 {
306         struct ifacaddr6 *aca;
307         struct inet6_dev *idev;
308         struct rt6_info *rt;
309         int err;
310
311         idev = in6_dev_get(dev);
312
313         if (idev == NULL)
314                 return -EINVAL;
315
316         write_lock_bh(&idev->lock);
317         if (idev->dead) {
318                 err = -ENODEV;
319                 goto out;
320         }
321
322         for (aca = idev->ac_list; aca; aca = aca->aca_next) {
323                 if (ipv6_addr_cmp(&aca->aca_addr, addr) == 0) {
324                         aca->aca_users++;
325                         err = 0;
326                         goto out;
327                 }
328         }
329
330         /*
331          *      not found: create a new one.
332          */
333
334         aca = kmalloc(sizeof(struct ifacaddr6), GFP_ATOMIC);
335
336         if (aca == NULL) {
337                 err = -ENOMEM;
338                 goto out;
339         }
340
341         rt = addrconf_dst_alloc(idev, addr, 1);
342         if (IS_ERR(rt)) {
343                 kfree(aca);
344                 err = PTR_ERR(rt);
345                 goto out;
346         }
347
348         memset(aca, 0, sizeof(struct ifacaddr6));
349
350         ipv6_addr_copy(&aca->aca_addr, addr);
351         aca->aca_idev = idev;
352         aca->aca_rt = rt;
353         aca->aca_users = 1;
354         /* aca_tstamp should be updated upon changes */
355         aca->aca_cstamp = aca->aca_tstamp = jiffies;
356         atomic_set(&aca->aca_refcnt, 2);
357         aca->aca_lock = SPIN_LOCK_UNLOCKED;
358
359         aca->aca_next = idev->ac_list;
360         idev->ac_list = aca;
361         write_unlock_bh(&idev->lock);
362
363         dst_hold(&rt->u.dst);
364         if (ip6_ins_rt(rt, NULL, NULL))
365                 dst_release(&rt->u.dst);
366
367         addrconf_join_solict(dev, &aca->aca_addr);
368
369         aca_put(aca);
370         return 0;
371 out:
372         write_unlock_bh(&idev->lock);
373         in6_dev_put(idev);
374         return err;
375 }
376
377 /*
378  *      device anycast group decrement
379  */
380 int __ipv6_dev_ac_dec(struct inet6_dev *idev, struct in6_addr *addr)
381 {
382         struct ifacaddr6 *aca, *prev_aca;
383
384         write_lock_bh(&idev->lock);
385         prev_aca = NULL;
386         for (aca = idev->ac_list; aca; aca = aca->aca_next) {
387                 if (ipv6_addr_cmp(&aca->aca_addr, addr) == 0)
388                         break;
389                 prev_aca = aca;
390         }
391         if (!aca) {
392                 write_unlock_bh(&idev->lock);
393                 return -ENOENT;
394         }
395         if (--aca->aca_users > 0) {
396                 write_unlock_bh(&idev->lock);
397                 return 0;
398         }
399         if (prev_aca)
400                 prev_aca->aca_next = aca->aca_next;
401         else
402                 idev->ac_list = aca->aca_next;
403         write_unlock_bh(&idev->lock);
404         addrconf_leave_solict(idev, &aca->aca_addr);
405
406         dst_hold(&aca->aca_rt->u.dst);
407         if (ip6_del_rt(aca->aca_rt, NULL, NULL))
408                 dst_free(&aca->aca_rt->u.dst);
409         else
410                 dst_release(&aca->aca_rt->u.dst);
411
412         aca_put(aca);
413         return 0;
414 }
415
416 int ipv6_dev_ac_dec(struct net_device *dev, struct in6_addr *addr)
417 {
418         int ret;
419         struct inet6_dev *idev = in6_dev_get(dev);
420         if (idev == NULL)
421                 return -ENODEV;
422         ret = __ipv6_dev_ac_dec(idev, addr);
423         in6_dev_put(idev);
424         return ret;
425 }
426         
427 /*
428  *      check if the interface has this anycast address
429  */
430 static int ipv6_chk_acast_dev(struct net_device *dev, struct in6_addr *addr)
431 {
432         struct inet6_dev *idev;
433         struct ifacaddr6 *aca;
434
435         idev = in6_dev_get(dev);
436         if (idev) {
437                 read_lock_bh(&idev->lock);
438                 for (aca = idev->ac_list; aca; aca = aca->aca_next)
439                         if (ipv6_addr_cmp(&aca->aca_addr, addr) == 0)
440                                 break;
441                 read_unlock_bh(&idev->lock);
442                 in6_dev_put(idev);
443                 return aca != 0;
444         }
445         return 0;
446 }
447
448 /*
449  *      check if given interface (or any, if dev==0) has this anycast address
450  */
451 int ipv6_chk_acast_addr(struct net_device *dev, struct in6_addr *addr)
452 {
453         if (dev)
454                 return ipv6_chk_acast_dev(dev, addr);
455         read_lock(&dev_base_lock);
456         for (dev=dev_base; dev; dev=dev->next)
457                 if (ipv6_chk_acast_dev(dev, addr))
458                         break;
459         read_unlock(&dev_base_lock);
460         return dev != 0;
461 }
462
463
464 #ifdef CONFIG_PROC_FS
465 struct ac6_iter_state {
466         struct net_device *dev;
467         struct inet6_dev *idev;
468 };
469
470 #define ac6_seq_private(seq)    ((struct ac6_iter_state *)(seq)->private)
471
472 static inline struct ifacaddr6 *ac6_get_first(struct seq_file *seq)
473 {
474         struct ifacaddr6 *im = NULL;
475         struct ac6_iter_state *state = ac6_seq_private(seq);
476
477         for (state->dev = dev_base, state->idev = NULL;
478              state->dev;
479              state->dev = state->dev->next) {
480                 struct inet6_dev *idev;
481                 idev = in6_dev_get(state->dev);
482                 if (!idev)
483                         continue;
484                 read_lock_bh(&idev->lock);
485                 im = idev->ac_list;
486                 if (im) {
487                         state->idev = idev;
488                         break;
489                 }
490                 read_unlock_bh(&idev->lock);
491         }
492         return im;
493 }
494
495 static struct ifacaddr6 *ac6_get_next(struct seq_file *seq, struct ifacaddr6 *im)
496 {
497         struct ac6_iter_state *state = ac6_seq_private(seq);
498
499         im = im->aca_next;
500         while (!im) {
501                 if (likely(state->idev != NULL)) {
502                         read_unlock_bh(&state->idev->lock);
503                         in6_dev_put(state->idev);
504                 }
505                 state->dev = state->dev->next;
506                 if (!state->dev) {
507                         state->idev = NULL;
508                         break;
509                 }
510                 state->idev = in6_dev_get(state->dev);
511                 if (!state->idev)
512                         continue;
513                 read_lock_bh(&state->idev->lock);
514                 im = state->idev->ac_list;
515         }
516         return im;
517 }
518
519 static struct ifacaddr6 *ac6_get_idx(struct seq_file *seq, loff_t pos)
520 {
521         struct ifacaddr6 *im = ac6_get_first(seq);
522         if (im)
523                 while (pos && (im = ac6_get_next(seq, im)) != NULL)
524                         --pos;
525         return pos ? NULL : im;
526 }
527
528 static void *ac6_seq_start(struct seq_file *seq, loff_t *pos)
529 {
530         read_lock(&dev_base_lock);
531         return ac6_get_idx(seq, *pos);
532 }
533
534 static void *ac6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
535 {
536         struct ifacaddr6 *im;
537         im = ac6_get_next(seq, v);
538         ++*pos;
539         return im;
540 }
541
542 static void ac6_seq_stop(struct seq_file *seq, void *v)
543 {
544         struct ac6_iter_state *state = ac6_seq_private(seq);
545         if (likely(state->idev != NULL)) {
546                 read_unlock_bh(&state->idev->lock);
547                 in6_dev_put(state->idev);
548         }
549         read_unlock(&dev_base_lock);
550 }
551
552 static int ac6_seq_show(struct seq_file *seq, void *v)
553 {
554         struct ifacaddr6 *im = (struct ifacaddr6 *)v;
555         struct ac6_iter_state *state = ac6_seq_private(seq);
556
557         seq_printf(seq,
558                    "%-4d %-15s "
559                    "%04x%04x%04x%04x%04x%04x%04x%04x "
560                    "%5d\n",
561                    state->dev->ifindex, state->dev->name,
562                    NIP6(im->aca_addr),
563                    im->aca_users);
564         return 0;
565 }
566
567 static struct seq_operations ac6_seq_ops = {
568         .start  =       ac6_seq_start,
569         .next   =       ac6_seq_next,
570         .stop   =       ac6_seq_stop,
571         .show   =       ac6_seq_show,
572 };
573
574 static int ac6_seq_open(struct inode *inode, struct file *file)
575 {
576         struct seq_file *seq;
577         int rc = -ENOMEM;
578         struct ac6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
579
580         if (!s)
581                 goto out;
582
583         rc = seq_open(file, &ac6_seq_ops);
584         if (rc)
585                 goto out_kfree;
586
587         seq = file->private_data;
588         seq->private = s;
589         memset(s, 0, sizeof(*s));
590 out:
591         return rc;
592 out_kfree:
593         kfree(s);
594         goto out;
595 }
596
597 static struct file_operations ac6_seq_fops = {
598         .owner          =       THIS_MODULE,
599         .open           =       ac6_seq_open,
600         .read           =       seq_read,
601         .llseek         =       seq_lseek,
602         .release        =       seq_release_private,
603 };
604
605 int __init ac6_proc_init(void)
606 {
607         if (!proc_net_fops_create("anycast6", S_IRUGO, &ac6_seq_fops))
608                 return -ENOMEM;
609
610         return 0;
611 }
612
613 void ac6_proc_exit(void)
614 {
615         proc_net_remove("anycast6");
616 }
617 #endif
618