GNU Linux-libre 4.19.264-gnu1
[releases.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/capability.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/net.h>
25 #include <linux/in6.h>
26 #include <linux/netdevice.h>
27 #include <linux/if_arp.h>
28 #include <linux/route.h>
29 #include <linux/init.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <linux/slab.h>
33
34 #include <net/net_namespace.h>
35 #include <net/sock.h>
36 #include <net/snmp.h>
37
38 #include <net/ipv6.h>
39 #include <net/protocol.h>
40 #include <net/if_inet6.h>
41 #include <net/ndisc.h>
42 #include <net/addrconf.h>
43 #include <net/ip6_route.h>
44
45 #include <net/checksum.h>
46
47 static int ipv6_dev_ac_dec(struct net_device *dev, const struct in6_addr *addr);
48
49 /*
50  *      socket join an anycast group
51  */
52
53 int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
54 {
55         struct ipv6_pinfo *np = inet6_sk(sk);
56         struct net_device *dev = NULL;
57         struct inet6_dev *idev;
58         struct ipv6_ac_socklist *pac;
59         struct net *net = sock_net(sk);
60         int     ishost = !net->ipv6.devconf_all->forwarding;
61         int     err = 0;
62
63         ASSERT_RTNL();
64
65         if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
66                 return -EPERM;
67         if (ipv6_addr_is_multicast(addr))
68                 return -EINVAL;
69
70         if (ifindex)
71                 dev = __dev_get_by_index(net, ifindex);
72
73         if (ipv6_chk_addr_and_flags(net, addr, dev, true, 0, IFA_F_TENTATIVE))
74                 return -EINVAL;
75
76         pac = sock_kmalloc(sk, sizeof(struct ipv6_ac_socklist), GFP_KERNEL);
77         if (!pac)
78                 return -ENOMEM;
79         pac->acl_next = NULL;
80         pac->acl_addr = *addr;
81
82         if (ifindex == 0) {
83                 struct rt6_info *rt;
84
85                 rt = rt6_lookup(net, addr, NULL, 0, NULL, 0);
86                 if (rt) {
87                         dev = rt->dst.dev;
88                         ip6_rt_put(rt);
89                 } else if (ishost) {
90                         err = -EADDRNOTAVAIL;
91                         goto error;
92                 } else {
93                         /* router, no matching interface: just pick one */
94                         dev = __dev_get_by_flags(net, IFF_UP,
95                                                  IFF_UP | IFF_LOOPBACK);
96                 }
97         }
98
99         if (!dev) {
100                 err = -ENODEV;
101                 goto error;
102         }
103
104         idev = __in6_dev_get(dev);
105         if (!idev) {
106                 if (ifindex)
107                         err = -ENODEV;
108                 else
109                         err = -EADDRNOTAVAIL;
110                 goto error;
111         }
112         /* reset ishost, now that we have a specific device */
113         ishost = !idev->cnf.forwarding;
114
115         pac->acl_ifindex = dev->ifindex;
116
117         /* XXX
118          * For hosts, allow link-local or matching prefix anycasts.
119          * This obviates the need for propagating anycast routes while
120          * still allowing some non-router anycast participation.
121          */
122         if (!ipv6_chk_prefix(addr, dev)) {
123                 if (ishost)
124                         err = -EADDRNOTAVAIL;
125                 if (err)
126                         goto error;
127         }
128
129         err = __ipv6_dev_ac_inc(idev, addr);
130         if (!err) {
131                 pac->acl_next = np->ipv6_ac_list;
132                 np->ipv6_ac_list = pac;
133                 pac = NULL;
134         }
135
136 error:
137         if (pac)
138                 sock_kfree_s(sk, pac, sizeof(*pac));
139         return err;
140 }
141
142 /*
143  *      socket leave an anycast group
144  */
145 int ipv6_sock_ac_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
146 {
147         struct ipv6_pinfo *np = inet6_sk(sk);
148         struct net_device *dev;
149         struct ipv6_ac_socklist *pac, *prev_pac;
150         struct net *net = sock_net(sk);
151
152         ASSERT_RTNL();
153
154         prev_pac = NULL;
155         for (pac = np->ipv6_ac_list; pac; pac = pac->acl_next) {
156                 if ((ifindex == 0 || pac->acl_ifindex == ifindex) &&
157                      ipv6_addr_equal(&pac->acl_addr, addr))
158                         break;
159                 prev_pac = pac;
160         }
161         if (!pac)
162                 return -ENOENT;
163         if (prev_pac)
164                 prev_pac->acl_next = pac->acl_next;
165         else
166                 np->ipv6_ac_list = pac->acl_next;
167
168         dev = __dev_get_by_index(net, pac->acl_ifindex);
169         if (dev)
170                 ipv6_dev_ac_dec(dev, &pac->acl_addr);
171
172         sock_kfree_s(sk, pac, sizeof(*pac));
173         return 0;
174 }
175
176 void __ipv6_sock_ac_close(struct sock *sk)
177 {
178         struct ipv6_pinfo *np = inet6_sk(sk);
179         struct net_device *dev = NULL;
180         struct ipv6_ac_socklist *pac;
181         struct net *net = sock_net(sk);
182         int     prev_index;
183
184         ASSERT_RTNL();
185         pac = np->ipv6_ac_list;
186         np->ipv6_ac_list = NULL;
187
188         prev_index = 0;
189         while (pac) {
190                 struct ipv6_ac_socklist *next = pac->acl_next;
191
192                 if (pac->acl_ifindex != prev_index) {
193                         dev = __dev_get_by_index(net, pac->acl_ifindex);
194                         prev_index = pac->acl_ifindex;
195                 }
196                 if (dev)
197                         ipv6_dev_ac_dec(dev, &pac->acl_addr);
198                 sock_kfree_s(sk, pac, sizeof(*pac));
199                 pac = next;
200         }
201 }
202
203 void ipv6_sock_ac_close(struct sock *sk)
204 {
205         struct ipv6_pinfo *np = inet6_sk(sk);
206
207         if (!np->ipv6_ac_list)
208                 return;
209         rtnl_lock();
210         __ipv6_sock_ac_close(sk);
211         rtnl_unlock();
212 }
213
214 static void aca_get(struct ifacaddr6 *aca)
215 {
216         refcount_inc(&aca->aca_refcnt);
217 }
218
219 static void aca_put(struct ifacaddr6 *ac)
220 {
221         if (refcount_dec_and_test(&ac->aca_refcnt)) {
222                 fib6_info_release(ac->aca_rt);
223                 kfree(ac);
224         }
225 }
226
227 static struct ifacaddr6 *aca_alloc(struct fib6_info *f6i,
228                                    const struct in6_addr *addr)
229 {
230         struct ifacaddr6 *aca;
231
232         aca = kzalloc(sizeof(*aca), GFP_ATOMIC);
233         if (!aca)
234                 return NULL;
235
236         aca->aca_addr = *addr;
237         fib6_info_hold(f6i);
238         aca->aca_rt = f6i;
239         aca->aca_users = 1;
240         /* aca_tstamp should be updated upon changes */
241         aca->aca_cstamp = aca->aca_tstamp = jiffies;
242         refcount_set(&aca->aca_refcnt, 1);
243
244         return aca;
245 }
246
247 /*
248  *      device anycast group inc (add if not found)
249  */
250 int __ipv6_dev_ac_inc(struct inet6_dev *idev, const struct in6_addr *addr)
251 {
252         struct ifacaddr6 *aca;
253         struct fib6_info *f6i;
254         struct net *net;
255         int err;
256
257         ASSERT_RTNL();
258
259         write_lock_bh(&idev->lock);
260         if (idev->dead) {
261                 err = -ENODEV;
262                 goto out;
263         }
264
265         for (aca = idev->ac_list; aca; aca = aca->aca_next) {
266                 if (ipv6_addr_equal(&aca->aca_addr, addr)) {
267                         aca->aca_users++;
268                         err = 0;
269                         goto out;
270                 }
271         }
272
273         net = dev_net(idev->dev);
274         f6i = addrconf_f6i_alloc(net, idev, addr, true, GFP_ATOMIC);
275         if (IS_ERR(f6i)) {
276                 err = PTR_ERR(f6i);
277                 goto out;
278         }
279         aca = aca_alloc(f6i, addr);
280         if (!aca) {
281                 fib6_info_release(f6i);
282                 err = -ENOMEM;
283                 goto out;
284         }
285
286         aca->aca_next = idev->ac_list;
287         idev->ac_list = aca;
288
289         /* Hold this for addrconf_join_solict() below before we unlock,
290          * it is already exposed via idev->ac_list.
291          */
292         aca_get(aca);
293         write_unlock_bh(&idev->lock);
294
295         ip6_ins_rt(net, f6i);
296
297         addrconf_join_solict(idev->dev, &aca->aca_addr);
298
299         aca_put(aca);
300         return 0;
301 out:
302         write_unlock_bh(&idev->lock);
303         return err;
304 }
305
306 /*
307  *      device anycast group decrement
308  */
309 int __ipv6_dev_ac_dec(struct inet6_dev *idev, const struct in6_addr *addr)
310 {
311         struct ifacaddr6 *aca, *prev_aca;
312
313         ASSERT_RTNL();
314
315         write_lock_bh(&idev->lock);
316         prev_aca = NULL;
317         for (aca = idev->ac_list; aca; aca = aca->aca_next) {
318                 if (ipv6_addr_equal(&aca->aca_addr, addr))
319                         break;
320                 prev_aca = aca;
321         }
322         if (!aca) {
323                 write_unlock_bh(&idev->lock);
324                 return -ENOENT;
325         }
326         if (--aca->aca_users > 0) {
327                 write_unlock_bh(&idev->lock);
328                 return 0;
329         }
330         if (prev_aca)
331                 prev_aca->aca_next = aca->aca_next;
332         else
333                 idev->ac_list = aca->aca_next;
334         write_unlock_bh(&idev->lock);
335         addrconf_leave_solict(idev, &aca->aca_addr);
336
337         ip6_del_rt(dev_net(idev->dev), aca->aca_rt);
338
339         aca_put(aca);
340         return 0;
341 }
342
343 /* called with rtnl_lock() */
344 static int ipv6_dev_ac_dec(struct net_device *dev, const struct in6_addr *addr)
345 {
346         struct inet6_dev *idev = __in6_dev_get(dev);
347
348         if (!idev)
349                 return -ENODEV;
350         return __ipv6_dev_ac_dec(idev, addr);
351 }
352
353 void ipv6_ac_destroy_dev(struct inet6_dev *idev)
354 {
355         struct ifacaddr6 *aca;
356
357         write_lock_bh(&idev->lock);
358         while ((aca = idev->ac_list) != NULL) {
359                 idev->ac_list = aca->aca_next;
360                 write_unlock_bh(&idev->lock);
361
362                 addrconf_leave_solict(idev, &aca->aca_addr);
363
364                 ip6_del_rt(dev_net(idev->dev), aca->aca_rt);
365
366                 aca_put(aca);
367
368                 write_lock_bh(&idev->lock);
369         }
370         write_unlock_bh(&idev->lock);
371 }
372
373 /*
374  *      check if the interface has this anycast address
375  *      called with rcu_read_lock()
376  */
377 static bool ipv6_chk_acast_dev(struct net_device *dev, const struct in6_addr *addr)
378 {
379         struct inet6_dev *idev;
380         struct ifacaddr6 *aca;
381
382         idev = __in6_dev_get(dev);
383         if (idev) {
384                 read_lock_bh(&idev->lock);
385                 for (aca = idev->ac_list; aca; aca = aca->aca_next)
386                         if (ipv6_addr_equal(&aca->aca_addr, addr))
387                                 break;
388                 read_unlock_bh(&idev->lock);
389                 return aca != NULL;
390         }
391         return false;
392 }
393
394 /*
395  *      check if given interface (or any, if dev==0) has this anycast address
396  */
397 bool ipv6_chk_acast_addr(struct net *net, struct net_device *dev,
398                          const struct in6_addr *addr)
399 {
400         bool found = false;
401
402         rcu_read_lock();
403         if (dev)
404                 found = ipv6_chk_acast_dev(dev, addr);
405         else
406                 for_each_netdev_rcu(net, dev)
407                         if (ipv6_chk_acast_dev(dev, addr)) {
408                                 found = true;
409                                 break;
410                         }
411         rcu_read_unlock();
412         return found;
413 }
414
415 /*      check if this anycast address is link-local on given interface or
416  *      is global
417  */
418 bool ipv6_chk_acast_addr_src(struct net *net, struct net_device *dev,
419                              const struct in6_addr *addr)
420 {
421         return ipv6_chk_acast_addr(net,
422                                    (ipv6_addr_type(addr) & IPV6_ADDR_LINKLOCAL ?
423                                     dev : NULL),
424                                    addr);
425 }
426
427 #ifdef CONFIG_PROC_FS
428 struct ac6_iter_state {
429         struct seq_net_private p;
430         struct net_device *dev;
431         struct inet6_dev *idev;
432 };
433
434 #define ac6_seq_private(seq)    ((struct ac6_iter_state *)(seq)->private)
435
436 static inline struct ifacaddr6 *ac6_get_first(struct seq_file *seq)
437 {
438         struct ifacaddr6 *im = NULL;
439         struct ac6_iter_state *state = ac6_seq_private(seq);
440         struct net *net = seq_file_net(seq);
441
442         state->idev = NULL;
443         for_each_netdev_rcu(net, state->dev) {
444                 struct inet6_dev *idev;
445                 idev = __in6_dev_get(state->dev);
446                 if (!idev)
447                         continue;
448                 read_lock_bh(&idev->lock);
449                 im = idev->ac_list;
450                 if (im) {
451                         state->idev = idev;
452                         break;
453                 }
454                 read_unlock_bh(&idev->lock);
455         }
456         return im;
457 }
458
459 static struct ifacaddr6 *ac6_get_next(struct seq_file *seq, struct ifacaddr6 *im)
460 {
461         struct ac6_iter_state *state = ac6_seq_private(seq);
462
463         im = im->aca_next;
464         while (!im) {
465                 if (likely(state->idev != NULL))
466                         read_unlock_bh(&state->idev->lock);
467
468                 state->dev = next_net_device_rcu(state->dev);
469                 if (!state->dev) {
470                         state->idev = NULL;
471                         break;
472                 }
473                 state->idev = __in6_dev_get(state->dev);
474                 if (!state->idev)
475                         continue;
476                 read_lock_bh(&state->idev->lock);
477                 im = state->idev->ac_list;
478         }
479         return im;
480 }
481
482 static struct ifacaddr6 *ac6_get_idx(struct seq_file *seq, loff_t pos)
483 {
484         struct ifacaddr6 *im = ac6_get_first(seq);
485         if (im)
486                 while (pos && (im = ac6_get_next(seq, im)) != NULL)
487                         --pos;
488         return pos ? NULL : im;
489 }
490
491 static void *ac6_seq_start(struct seq_file *seq, loff_t *pos)
492         __acquires(RCU)
493 {
494         rcu_read_lock();
495         return ac6_get_idx(seq, *pos);
496 }
497
498 static void *ac6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
499 {
500         struct ifacaddr6 *im = ac6_get_next(seq, v);
501
502         ++*pos;
503         return im;
504 }
505
506 static void ac6_seq_stop(struct seq_file *seq, void *v)
507         __releases(RCU)
508 {
509         struct ac6_iter_state *state = ac6_seq_private(seq);
510
511         if (likely(state->idev != NULL)) {
512                 read_unlock_bh(&state->idev->lock);
513                 state->idev = NULL;
514         }
515         rcu_read_unlock();
516 }
517
518 static int ac6_seq_show(struct seq_file *seq, void *v)
519 {
520         struct ifacaddr6 *im = (struct ifacaddr6 *)v;
521         struct ac6_iter_state *state = ac6_seq_private(seq);
522
523         seq_printf(seq, "%-4d %-15s %pi6 %5d\n",
524                    state->dev->ifindex, state->dev->name,
525                    &im->aca_addr, im->aca_users);
526         return 0;
527 }
528
529 static const struct seq_operations ac6_seq_ops = {
530         .start  =       ac6_seq_start,
531         .next   =       ac6_seq_next,
532         .stop   =       ac6_seq_stop,
533         .show   =       ac6_seq_show,
534 };
535
536 int __net_init ac6_proc_init(struct net *net)
537 {
538         if (!proc_create_net("anycast6", 0444, net->proc_net, &ac6_seq_ops,
539                         sizeof(struct ac6_iter_state)))
540                 return -ENOMEM;
541
542         return 0;
543 }
544
545 void ac6_proc_exit(struct net *net)
546 {
547         remove_proc_entry("anycast6", net->proc_net);
548 }
549 #endif