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