projects/linux·blob master · refs

571 lines · 15.5 KiB

1/* SPDX-License-Identifier: GPL-2.0 */
2#ifndef _ADDRCONF_H
3#define _ADDRCONF_H
4
5#define MAX_RTR_SOLICITATIONS -1 /* unlimited */
6#define RTR_SOLICITATION_INTERVAL (4*HZ)
7#define RTR_SOLICITATION_MAX_INTERVAL (3600*HZ) /* 1 hour */
8
9#define MIN_VALID_LIFETIME (2*3600) /* 2 hours */
10
11/* TEMP_VALID_LIFETIME default value as specified in RFC 8981 3.8 */
12#define TEMP_VALID_LIFETIME (2*86400) /* 2 days */
13#define TEMP_PREFERRED_LIFETIME (86400) /* 24 hours */
14#define REGEN_MIN_ADVANCE (2) /* 2 seconds */
15#define REGEN_MAX_RETRY (3)
16#define MAX_DESYNC_FACTOR (600)
17
18#define ADDR_CHECK_FREQUENCY (120*HZ)
19
20#define IPV6_MAX_ADDRESSES 16
21
22#define ADDRCONF_TIMER_FUZZ_MINUS (HZ > 50 ? HZ / 50 : 1)
23#define ADDRCONF_TIMER_FUZZ (HZ / 4)
24#define ADDRCONF_TIMER_FUZZ_MAX (HZ)
25
26#define ADDRCONF_NOTIFY_PRIORITY 0
27
28#include <linux/in.h>
29#include <linux/in6.h>
30
31struct prefix_info {
32 __u8 type;
33 __u8 length;
34 __u8 prefix_len;
35
36 union __packed {
37 __u8 flags;
38 struct __packed {
39#if defined(__BIG_ENDIAN_BITFIELD)
40 __u8 onlink : 1,
41 autoconf : 1,
42 routeraddr : 1,
43 preferpd : 1,
44 reserved : 4;
45#elif defined(__LITTLE_ENDIAN_BITFIELD)
46 __u8 reserved : 4,
47 preferpd : 1,
48 routeraddr : 1,
49 autoconf : 1,
50 onlink : 1;
51#else
52#error "Please fix <asm/byteorder.h>"
53#endif
54 };
55 };
56 __be32 valid;
57 __be32 prefered;
58 __be32 reserved2;
59
60 struct in6_addr prefix;
61};
62
63/* rfc4861 4.6.2: IPv6 PIO is 32 bytes in size */
64static_assert(sizeof(struct prefix_info) == 32);
65
66#include <linux/ipv6.h>
67#include <linux/netdevice.h>
68#include <net/if_inet6.h>
69#include <net/ipv6.h>
70
71struct in6_validator_info {
72 struct in6_addr i6vi_addr;
73 struct inet6_dev *i6vi_dev;
74 struct netlink_ext_ack *extack;
75};
76
77struct ifa6_config {
78 const struct in6_addr *pfx;
79 unsigned int plen;
80
81 u8 ifa_proto;
82
83 const struct in6_addr *peer_pfx;
84
85 u32 rt_priority;
86 u32 ifa_flags;
87 u32 preferred_lft;
88 u32 valid_lft;
89 u16 scope;
90};
91
92enum addr_type_t {
93 UNICAST_ADDR,
94 MULTICAST_ADDR,
95 ANYCAST_ADDR,
96};
97
98struct inet6_fill_args {
99 u32 portid;
100 u32 seq;
101 int event;
102 unsigned int flags;
103 int netnsid;
104 int ifindex;
105 enum addr_type_t type;
106 bool force_rt_scope_universe;
107};
108
109int addrconf_init(void);
110void addrconf_cleanup(void);
111
112int addrconf_add_ifaddr(struct net *net, void __user *arg);
113int addrconf_del_ifaddr(struct net *net, void __user *arg);
114int addrconf_set_dstaddr(struct net *net, void __user *arg);
115
116int ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
117 const struct net_device *dev, int strict);
118int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
119 const struct net_device *dev, bool skip_dev_check,
120 int strict, u32 banned_flags);
121
122#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
123int ipv6_chk_home_addr(struct net *net, const struct in6_addr *addr);
124#endif
125
126int ipv6_chk_rpl_srh_loop(struct net *net, const struct in6_addr *segs,
127 unsigned char nsegs);
128
129bool ipv6_chk_custom_prefix(const struct in6_addr *addr,
130 const unsigned int prefix_len,
131 struct net_device *dev);
132
133int ipv6_chk_prefix(const struct in6_addr *addr, struct net_device *dev);
134
135struct net_device *ipv6_dev_find(struct net *net, const struct in6_addr *addr,
136 struct net_device *dev);
137
138struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net,
139 const struct in6_addr *addr,
140 struct net_device *dev, int strict);
141
142int ipv6_dev_get_saddr(struct net *net, const struct net_device *dev,
143 const struct in6_addr *daddr, unsigned int srcprefs,
144 struct in6_addr *saddr);
145int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
146 u32 banned_flags);
147bool inet_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2,
148 bool match_wildcard);
149bool inet_rcv_saddr_any(const struct sock *sk);
150void addrconf_join_solict(struct net_device *dev, const struct in6_addr *addr);
151void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr);
152
153void addrconf_add_linklocal(struct inet6_dev *idev,
154 const struct in6_addr *addr, u32 flags);
155
156int addrconf_prefix_rcv_add_addr(struct net *net, struct net_device *dev,
157 const struct prefix_info *pinfo,
158 struct inet6_dev *in6_dev,
159 const struct in6_addr *addr, int addr_type,
160 u32 addr_flags, bool sllao, bool tokenized,
161 __u32 valid_lft, u32 prefered_lft);
162
163static inline void addrconf_addr_eui48_base(u8 *eui, const char *const addr)
164{
165 memcpy(eui, addr, 3);
166 eui[3] = 0xFF;
167 eui[4] = 0xFE;
168 memcpy(eui + 5, addr + 3, 3);
169}
170
171static inline void addrconf_addr_eui48(u8 *eui, const char *const addr)
172{
173 addrconf_addr_eui48_base(eui, addr);
174 eui[0] ^= 2;
175}
176
177static inline int addrconf_ifid_eui48(u8 *eui, struct net_device *dev)
178{
179 if (dev->addr_len != ETH_ALEN)
180 return -1;
181
182 /*
183 * The zSeries OSA network cards can be shared among various
184 * OS instances, but the OSA cards have only one MAC address.
185 * This leads to duplicate address conflicts in conjunction
186 * with IPv6 if more than one instance uses the same card.
187 *
188 * The driver for these cards can deliver a unique 16-bit
189 * identifier for each instance sharing the same card. It is
190 * placed instead of 0xFFFE in the interface identifier. The
191 * "u" bit of the interface identifier is not inverted in this
192 * case. Hence the resulting interface identifier has local
193 * scope according to RFC2373.
194 */
195
196 addrconf_addr_eui48_base(eui, dev->dev_addr);
197
198 if (dev->dev_id) {
199 eui[3] = (dev->dev_id >> 8) & 0xFF;
200 eui[4] = dev->dev_id & 0xFF;
201 } else {
202 eui[0] ^= 2;
203 }
204
205 return 0;
206}
207
208#define INFINITY_LIFE_TIME 0xFFFFFFFF
209
210static inline unsigned long addrconf_timeout_fixup(u32 timeout,
211 unsigned int unit)
212{
213 if (timeout == INFINITY_LIFE_TIME)
214 return ~0UL;
215
216 /*
217 * Avoid arithmetic overflow.
218 * Assuming unit is constant and non-zero, this "if" statement
219 * will go away on 64bit archs.
220 */
221 if (0xfffffffe > LONG_MAX / unit && timeout > LONG_MAX / unit)
222 return LONG_MAX / unit;
223
224 return timeout;
225}
226
227static inline int addrconf_finite_timeout(unsigned long timeout)
228{
229 return ~timeout;
230}
231
232/*
233 * IPv6 Address Label subsystem (addrlabel.c)
234 */
235int ipv6_addr_label_init(void);
236void ipv6_addr_label_cleanup(void);
237int ipv6_addr_label_rtnl_register(void);
238u32 ipv6_addr_label(struct net *net, const struct in6_addr *addr,
239 int type, int ifindex);
240
241/*
242 * multicast prototypes (mcast.c)
243 */
244static inline bool ipv6_mc_may_pull(struct sk_buff *skb,
245 unsigned int len)
246{
247 if (skb_transport_offset(skb) + ipv6_transport_len(skb) < len)
248 return false;
249
250 return pskb_may_pull(skb, len);
251}
252
253int ipv6_sock_mc_join(struct sock *sk, int ifindex,
254 const struct in6_addr *addr);
255int ipv6_sock_mc_drop(struct sock *sk, int ifindex,
256 const struct in6_addr *addr);
257void __ipv6_sock_mc_close(struct sock *sk);
258void ipv6_sock_mc_close(struct sock *sk);
259bool inet6_mc_check(const struct sock *sk, const struct in6_addr *mc_addr,
260 const struct in6_addr *src_addr);
261
262int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr);
263int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr);
264int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr);
265void ipv6_mc_up(struct inet6_dev *idev);
266void ipv6_mc_down(struct inet6_dev *idev);
267void ipv6_mc_unmap(struct inet6_dev *idev);
268void ipv6_mc_remap(struct inet6_dev *idev);
269void ipv6_mc_init_dev(struct inet6_dev *idev);
270void ipv6_mc_destroy_dev(struct inet6_dev *idev);
271int ipv6_mc_check_mld(struct sk_buff *skb);
272void addrconf_dad_failure(struct sk_buff *skb, struct inet6_ifaddr *ifp);
273
274bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group,
275 const struct in6_addr *src_addr);
276
277void ipv6_mc_dad_complete(struct inet6_dev *idev);
278
279/*
280 * identify MLD packets for MLD filter exceptions
281 */
282static inline bool ipv6_is_mld(struct sk_buff *skb, int nexthdr, int offset)
283{
284 struct icmp6hdr *hdr;
285
286 if (nexthdr != IPPROTO_ICMPV6 ||
287 !pskb_network_may_pull(skb, offset + sizeof(struct icmp6hdr)))
288 return false;
289
290 hdr = (struct icmp6hdr *)(skb_network_header(skb) + offset);
291
292 switch (hdr->icmp6_type) {
293 case ICMPV6_MGM_QUERY:
294 case ICMPV6_MGM_REPORT:
295 case ICMPV6_MGM_REDUCTION:
296 case ICMPV6_MLD2_REPORT:
297 return true;
298 default:
299 break;
300 }
301 return false;
302}
303
304void addrconf_prefix_rcv(struct net_device *dev,
305 u8 *opt, int len, bool sllao);
306
307/*
308 * anycast prototypes (anycast.c)
309 */
310int ipv6_sock_ac_join(struct sock *sk, int ifindex,
311 const struct in6_addr *addr);
312int ipv6_sock_ac_drop(struct sock *sk, int ifindex,
313 const struct in6_addr *addr);
314void __ipv6_sock_ac_close(struct sock *sk);
315void ipv6_sock_ac_close(struct sock *sk);
316
317int __ipv6_dev_ac_inc(struct inet6_dev *idev, const struct in6_addr *addr);
318int __ipv6_dev_ac_dec(struct inet6_dev *idev, const struct in6_addr *addr);
319void ipv6_ac_destroy_dev(struct inet6_dev *idev);
320bool ipv6_chk_acast_addr(struct net *net, struct net_device *dev,
321 const struct in6_addr *addr);
322bool ipv6_chk_acast_addr_src(struct net *net, struct net_device *dev,
323 const struct in6_addr *addr);
324int ipv6_anycast_init(void);
325void ipv6_anycast_cleanup(void);
326
327/* Device notifier */
328int register_inet6addr_notifier(struct notifier_block *nb);
329int unregister_inet6addr_notifier(struct notifier_block *nb);
330int inet6addr_notifier_call_chain(unsigned long val, void *v);
331
332int register_inet6addr_validator_notifier(struct notifier_block *nb);
333int unregister_inet6addr_validator_notifier(struct notifier_block *nb);
334int inet6addr_validator_notifier_call_chain(unsigned long val, void *v);
335
336void inet6_netconf_notify_devconf(struct net *net, int event, int type,
337 int ifindex, struct ipv6_devconf *devconf);
338
339/**
340 * __in6_dev_get - get inet6_dev pointer from netdevice
341 * @dev: network device
342 *
343 * Caller must hold rcu_read_lock or RTNL, because this function
344 * does not take a reference on the inet6_dev.
345 */
346static inline struct inet6_dev *__in6_dev_get(const struct net_device *dev)
347{
348 return rcu_dereference_rtnl(dev->ip6_ptr);
349}
350
351static inline struct inet6_dev *in6_dev_rcu(const struct net_device *dev)
352{
353 return rcu_dereference(dev->ip6_ptr);
354}
355
356static inline struct inet6_dev *__in6_dev_get_rtnl_net(const struct net_device *dev)
357{
358 return rtnl_net_dereference(dev_net(dev), dev->ip6_ptr);
359}
360
361/**
362 * __in6_dev_stats_get - get inet6_dev pointer for stats
363 * @dev: network device
364 * @skb: skb for original incoming interface if needed
365 *
366 * Caller must hold rcu_read_lock or RTNL, because this function
367 * does not take a reference on the inet6_dev.
368 */
369static inline struct inet6_dev *__in6_dev_stats_get(const struct net_device *dev,
370 const struct sk_buff *skb)
371{
372 if (netif_is_l3_master(dev)) {
373 dev = dev_get_by_index_rcu(dev_net(dev), inet6_iif(skb));
374 if (!dev)
375 return NULL;
376 }
377 return __in6_dev_get(dev);
378}
379
380/**
381 * __in6_dev_get_safely - get inet6_dev pointer from netdevice
382 * @dev: network device
383 *
384 * This is a safer version of __in6_dev_get
385 */
386static inline struct inet6_dev *__in6_dev_get_safely(const struct net_device *dev)
387{
388 if (likely(dev))
389 return rcu_dereference_rtnl(dev->ip6_ptr);
390 else
391 return NULL;
392}
393
394/**
395 * in6_dev_get - get inet6_dev pointer from netdevice
396 * @dev: network device
397 *
398 * This version can be used in any context, and takes a reference
399 * on the inet6_dev. Callers must use in6_dev_put() later to
400 * release this reference.
401 */
402static inline struct inet6_dev *in6_dev_get(const struct net_device *dev)
403{
404 struct inet6_dev *idev;
405
406 rcu_read_lock();
407 idev = rcu_dereference(dev->ip6_ptr);
408 if (idev && !refcount_inc_not_zero(&idev->refcnt))
409 idev = NULL;
410 rcu_read_unlock();
411 return idev;
412}
413
414static inline struct neigh_parms *__in6_dev_nd_parms_get_rcu(const struct net_device *dev)
415{
416 struct inet6_dev *idev = __in6_dev_get(dev);
417
418 return idev ? idev->nd_parms : NULL;
419}
420
421void in6_dev_finish_destroy(struct inet6_dev *idev);
422
423static inline void in6_dev_put(struct inet6_dev *idev)
424{
425 if (refcount_dec_and_test(&idev->refcnt))
426 in6_dev_finish_destroy(idev);
427}
428
429static inline void in6_dev_put_clear(struct inet6_dev **pidev)
430{
431 struct inet6_dev *idev = *pidev;
432
433 if (idev) {
434 in6_dev_put(idev);
435 *pidev = NULL;
436 }
437}
438
439static inline void __in6_dev_put(struct inet6_dev *idev)
440{
441 refcount_dec(&idev->refcnt);
442}
443
444static inline void in6_dev_hold(struct inet6_dev *idev)
445{
446 refcount_inc(&idev->refcnt);
447}
448
449static inline bool in6_dev_hold_safe(struct inet6_dev *idev)
450{
451 return refcount_inc_not_zero(&idev->refcnt);
452}
453
454/* called with rcu_read_lock held */
455static inline bool ip6_ignore_linkdown(const struct net_device *dev)
456{
457 const struct inet6_dev *idev = __in6_dev_get(dev);
458
459 if (unlikely(!idev))
460 return true;
461
462 return !!READ_ONCE(idev->cnf.ignore_routes_with_linkdown);
463}
464
465void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp);
466
467static inline void in6_ifa_put(struct inet6_ifaddr *ifp)
468{
469 if (refcount_dec_and_test(&ifp->refcnt))
470 inet6_ifa_finish_destroy(ifp);
471}
472
473static inline void __in6_ifa_put(struct inet6_ifaddr *ifp)
474{
475 refcount_dec(&ifp->refcnt);
476}
477
478static inline void in6_ifa_hold(struct inet6_ifaddr *ifp)
479{
480 refcount_inc(&ifp->refcnt);
481}
482
483static inline bool in6_ifa_hold_safe(struct inet6_ifaddr *ifp)
484{
485 return refcount_inc_not_zero(&ifp->refcnt);
486}
487
488/*
489 * compute link-local solicited-node multicast address
490 */
491
492static inline void addrconf_addr_solict_mult(const struct in6_addr *addr,
493 struct in6_addr *solicited)
494{
495 ipv6_addr_set(solicited,
496 htonl(0xFF020000), 0,
497 htonl(0x1),
498 htonl(0xFF000000) | addr->s6_addr32[3]);
499}
500
501static inline bool ipv6_addr_is_ll_all_nodes(const struct in6_addr *addr)
502{
503#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
504 __be64 *p = (__force __be64 *)addr;
505 return ((p[0] ^ cpu_to_be64(0xff02000000000000UL)) | (p[1] ^ cpu_to_be64(1))) == 0UL;
506#else
507 return ((addr->s6_addr32[0] ^ htonl(0xff020000)) |
508 addr->s6_addr32[1] | addr->s6_addr32[2] |
509 (addr->s6_addr32[3] ^ htonl(0x00000001))) == 0;
510#endif
511}
512
513static inline bool ipv6_addr_is_ll_all_routers(const struct in6_addr *addr)
514{
515#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
516 __be64 *p = (__force __be64 *)addr;
517 return ((p[0] ^ cpu_to_be64(0xff02000000000000UL)) | (p[1] ^ cpu_to_be64(2))) == 0UL;
518#else
519 return ((addr->s6_addr32[0] ^ htonl(0xff020000)) |
520 addr->s6_addr32[1] | addr->s6_addr32[2] |
521 (addr->s6_addr32[3] ^ htonl(0x00000002))) == 0;
522#endif
523}
524
525static inline bool ipv6_addr_is_isatap(const struct in6_addr *addr)
526{
527 return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
528}
529
530static inline bool ipv6_addr_is_solict_mult(const struct in6_addr *addr)
531{
532#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
533 __be64 *p = (__force __be64 *)addr;
534 return ((p[0] ^ cpu_to_be64(0xff02000000000000UL)) |
535 ((p[1] ^ cpu_to_be64(0x00000001ff000000UL)) &
536 cpu_to_be64(0xffffffffff000000UL))) == 0UL;
537#else
538 return ((addr->s6_addr32[0] ^ htonl(0xff020000)) |
539 addr->s6_addr32[1] |
540 (addr->s6_addr32[2] ^ htonl(0x00000001)) |
541 (addr->s6_addr[12] ^ 0xff)) == 0;
542#endif
543}
544
545static inline bool ipv6_addr_is_all_snoopers(const struct in6_addr *addr)
546{
547#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
548 __be64 *p = (__force __be64 *)addr;
549
550 return ((p[0] ^ cpu_to_be64(0xff02000000000000UL)) |
551 (p[1] ^ cpu_to_be64(0x6a))) == 0UL;
552#else
553 return ((addr->s6_addr32[0] ^ htonl(0xff020000)) |
554 addr->s6_addr32[1] | addr->s6_addr32[2] |
555 (addr->s6_addr32[3] ^ htonl(0x0000006a))) == 0;
556#endif
557}
558
559#ifdef CONFIG_PROC_FS
560int if6_proc_init(void);
561void if6_proc_exit(void);
562#endif
563
564int inet6_fill_ifmcaddr(struct sk_buff *skb,
565 const struct ifmcaddr6 *ifmca,
566 struct inet6_fill_args *args);
567
568int inet6_fill_ifacaddr(struct sk_buff *skb,
569 const struct ifacaddr6 *ifaca,
570 struct inet6_fill_args *args);
571#endif