ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 = 0;
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 = 0;
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 = 0;
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                 kfree(ac);
297         }
298 }
299
300 /*
301  *      device anycast group inc (add if not found)
302  */
303 int ipv6_dev_ac_inc(struct net_device *dev, struct in6_addr *addr)
304 {
305         struct ifacaddr6 *aca;
306         struct inet6_dev *idev;
307
308         idev = in6_dev_get(dev);
309
310         if (idev == NULL)
311                 return -EINVAL;
312
313         write_lock_bh(&idev->lock);
314         if (idev->dead) {
315                 write_unlock_bh(&idev->lock);
316                 in6_dev_put(idev);
317                 return -ENODEV;
318         }
319
320         for (aca = idev->ac_list; aca; aca = aca->aca_next) {
321                 if (ipv6_addr_cmp(&aca->aca_addr, addr) == 0) {
322                         aca->aca_users++;
323                         write_unlock_bh(&idev->lock);
324                         in6_dev_put(idev);
325                         return 0;
326                 }
327         }
328
329         /*
330          *      not found: create a new one.
331          */
332
333         aca = kmalloc(sizeof(struct ifacaddr6), GFP_ATOMIC);
334
335         if (aca == NULL) {
336                 write_unlock_bh(&idev->lock);
337                 in6_dev_put(idev);
338                 return -ENOMEM;
339         }
340
341         memset(aca, 0, sizeof(struct ifacaddr6));
342
343         ipv6_addr_copy(&aca->aca_addr, addr);
344         aca->aca_idev = idev;
345         aca->aca_users = 1;
346         /* aca_tstamp should be updated upon changes */
347         aca->aca_cstamp = aca->aca_tstamp = jiffies;
348         atomic_set(&aca->aca_refcnt, 2);
349         aca->aca_lock = SPIN_LOCK_UNLOCKED;
350
351         aca->aca_next = idev->ac_list;
352         idev->ac_list = aca;
353         write_unlock_bh(&idev->lock);
354
355         ip6_rt_addr_add(&aca->aca_addr, dev, 1);
356
357         addrconf_join_solict(dev, &aca->aca_addr);
358
359         aca_put(aca);
360         return 0;
361 }
362
363 /*
364  *      device anycast group decrement
365  */
366 int ipv6_dev_ac_dec(struct net_device *dev, struct in6_addr *addr)
367 {
368         struct inet6_dev *idev;
369         struct ifacaddr6 *aca, *prev_aca;
370
371         idev = in6_dev_get(dev);
372         if (idev == NULL)
373                 return -ENODEV;
374
375         write_lock_bh(&idev->lock);
376         prev_aca = 0;
377         for (aca = idev->ac_list; aca; aca = aca->aca_next) {
378                 if (ipv6_addr_cmp(&aca->aca_addr, addr) == 0)
379                         break;
380                 prev_aca = aca;
381         }
382         if (!aca) {
383                 write_unlock_bh(&idev->lock);
384                 in6_dev_put(idev);
385                 return -ENOENT;
386         }
387         if (--aca->aca_users > 0) {
388                 write_unlock_bh(&idev->lock);
389                 in6_dev_put(idev);
390                 return 0;
391         }
392         if (prev_aca)
393                 prev_aca->aca_next = aca->aca_next;
394         else
395                 idev->ac_list = aca->aca_next;
396         write_unlock_bh(&idev->lock);
397         addrconf_leave_solict(dev, &aca->aca_addr);
398
399         ip6_rt_addr_del(&aca->aca_addr, dev);
400
401         aca_put(aca);
402         in6_dev_put(idev);
403         return 0;
404 }
405
406 /*
407  *      check if the interface has this anycast address
408  */
409 static int ipv6_chk_acast_dev(struct net_device *dev, struct in6_addr *addr)
410 {
411         struct inet6_dev *idev;
412         struct ifacaddr6 *aca;
413
414         idev = in6_dev_get(dev);
415         if (idev) {
416                 read_lock_bh(&idev->lock);
417                 for (aca = idev->ac_list; aca; aca = aca->aca_next)
418                         if (ipv6_addr_cmp(&aca->aca_addr, addr) == 0)
419                                 break;
420                 read_unlock_bh(&idev->lock);
421                 in6_dev_put(idev);
422                 return aca != 0;
423         }
424         return 0;
425 }
426
427 /*
428  *      check if given interface (or any, if dev==0) has this anycast address
429  */
430 int ipv6_chk_acast_addr(struct net_device *dev, struct in6_addr *addr)
431 {
432         if (dev)
433                 return ipv6_chk_acast_dev(dev, addr);
434         read_lock(&dev_base_lock);
435         for (dev=dev_base; dev; dev=dev->next)
436                 if (ipv6_chk_acast_dev(dev, addr))
437                         break;
438         read_unlock(&dev_base_lock);
439         return dev != 0;
440 }
441
442
443 #ifdef CONFIG_PROC_FS
444 struct ac6_iter_state {
445         struct net_device *dev;
446         struct inet6_dev *idev;
447 };
448
449 #define ac6_seq_private(seq)    ((struct ac6_iter_state *)(seq)->private)
450
451 static inline struct ifacaddr6 *ac6_get_first(struct seq_file *seq)
452 {
453         struct ifacaddr6 *im = NULL;
454         struct ac6_iter_state *state = ac6_seq_private(seq);
455
456         for (state->dev = dev_base, state->idev = NULL;
457              state->dev;
458              state->dev = state->dev->next) {
459                 struct inet6_dev *idev;
460                 idev = in6_dev_get(state->dev);
461                 if (!idev)
462                         continue;
463                 read_lock_bh(&idev->lock);
464                 im = idev->ac_list;
465                 if (im) {
466                         state->idev = idev;
467                         break;
468                 }
469                 read_unlock_bh(&idev->lock);
470         }
471         return im;
472 }
473
474 static struct ifacaddr6 *ac6_get_next(struct seq_file *seq, struct ifacaddr6 *im)
475 {
476         struct ac6_iter_state *state = ac6_seq_private(seq);
477
478         im = im->aca_next;
479         while (!im) {
480                 if (likely(state->idev != NULL)) {
481                         read_unlock_bh(&state->idev->lock);
482                         in6_dev_put(state->idev);
483                 }
484                 state->dev = state->dev->next;
485                 if (!state->dev) {
486                         state->idev = NULL;
487                         break;
488                 }
489                 state->idev = in6_dev_get(state->dev);
490                 if (!state->idev)
491                         continue;
492                 read_lock_bh(&state->idev->lock);
493                 im = state->idev->ac_list;
494         }
495         return im;
496 }
497
498 static struct ifacaddr6 *ac6_get_idx(struct seq_file *seq, loff_t pos)
499 {
500         struct ifacaddr6 *im = ac6_get_first(seq);
501         if (im)
502                 while (pos && (im = ac6_get_next(seq, im)) != NULL)
503                         --pos;
504         return pos ? NULL : im;
505 }
506
507 static void *ac6_seq_start(struct seq_file *seq, loff_t *pos)
508 {
509         read_lock(&dev_base_lock);
510         return ac6_get_idx(seq, *pos);
511 }
512
513 static void *ac6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
514 {
515         struct ifacaddr6 *im;
516         im = ac6_get_next(seq, v);
517         ++*pos;
518         return im;
519 }
520
521 static void ac6_seq_stop(struct seq_file *seq, void *v)
522 {
523         struct ac6_iter_state *state = ac6_seq_private(seq);
524         if (likely(state->idev != NULL)) {
525                 read_unlock_bh(&state->idev->lock);
526                 in6_dev_put(state->idev);
527         }
528         read_unlock(&dev_base_lock);
529 }
530
531 static int ac6_seq_show(struct seq_file *seq, void *v)
532 {
533         struct ifacaddr6 *im = (struct ifacaddr6 *)v;
534         struct ac6_iter_state *state = ac6_seq_private(seq);
535
536         seq_printf(seq,
537                    "%-4d %-15s "
538                    "%04x%04x%04x%04x%04x%04x%04x%04x "
539                    "%5d\n",
540                    state->dev->ifindex, state->dev->name,
541                    NIP6(im->aca_addr),
542                    im->aca_users);
543         return 0;
544 }
545
546 static struct seq_operations ac6_seq_ops = {
547         .start  =       ac6_seq_start,
548         .next   =       ac6_seq_next,
549         .stop   =       ac6_seq_stop,
550         .show   =       ac6_seq_show,
551 };
552
553 static int ac6_seq_open(struct inode *inode, struct file *file)
554 {
555         struct seq_file *seq;
556         int rc = -ENOMEM;
557         struct ac6_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
558
559         if (!s)
560                 goto out;
561
562         rc = seq_open(file, &ac6_seq_ops);
563         if (rc)
564                 goto out_kfree;
565
566         seq = file->private_data;
567         seq->private = s;
568         memset(s, 0, sizeof(*s));
569 out:
570         return rc;
571 out_kfree:
572         kfree(s);
573         goto out;
574 }
575
576 static struct file_operations ac6_seq_fops = {
577         .owner          =       THIS_MODULE,
578         .open           =       ac6_seq_open,
579         .read           =       seq_read,
580         .llseek         =       seq_lseek,
581         .release        =       seq_release_private,
582 };
583
584 int __init ac6_proc_init(void)
585 {
586         if (!proc_net_fops_create("anycast6", S_IRUGO, &ac6_seq_fops))
587                 return -ENOMEM;
588
589         return 0;
590 }
591
592 void ac6_proc_exit(void)
593 {
594         proc_net_remove("anycast6");
595 }
596 #endif
597