Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 */
8
9#ifndef _NET_IPV6_H
10#define _NET_IPV6_H
11
12#include <linux/ipv6.h>
13#include <linux/hardirq.h>
14#include <linux/jhash.h>
15#include <linux/refcount.h>
16#include <linux/jump_label_ratelimit.h>
17#include <net/if_inet6.h>
18#include <net/flow.h>
19#include <net/flow_dissector.h>
20#include <net/inet_dscp.h>
21#include <net/snmp.h>
22#include <net/netns/hash.h>
23
24struct ip_tunnel_info;
25
26#define SIN6_LEN_RFC2133 24
27
28/*
29 * NextHeader field of IPv6 header
30 */
31
32#define NEXTHDR_HOP 0 /* Hop-by-hop option header. */
33#define NEXTHDR_IPV4 4 /* IPv4 in IPv6 */
34#define NEXTHDR_TCP 6 /* TCP segment. */
35#define NEXTHDR_UDP 17 /* UDP message. */
36#define NEXTHDR_IPV6 41 /* IPv6 in IPv6 */
37#define NEXTHDR_ROUTING 43 /* Routing header. */
38#define NEXTHDR_FRAGMENT 44 /* Fragmentation/reassembly header. */
39#define NEXTHDR_GRE 47 /* GRE header. */
40#define NEXTHDR_ESP 50 /* Encapsulating security payload. */
41#define NEXTHDR_AUTH 51 /* Authentication header. */
42#define NEXTHDR_ICMP 58 /* ICMP for IPv6. */
43#define NEXTHDR_NONE 59 /* No next header */
44#define NEXTHDR_DEST 60 /* Destination options header. */
45#define NEXTHDR_SCTP 132 /* SCTP message. */
46#define NEXTHDR_MOBILITY 135 /* Mobility header. */
47
48#define NEXTHDR_MAX 255
49
50#define IPV6_DEFAULT_HOPLIMIT 64
51#define IPV6_DEFAULT_MCASTHOPS 1
52
53/* Limits on Hop-by-Hop and Destination options.
54 *
55 * Per RFC8200 there is no limit on the maximum number or lengths of options in
56 * Hop-by-Hop or Destination options other then the packet must fit in an MTU.
57 * We allow configurable limits in order to mitigate potential denial of
58 * service attacks.
59 *
60 * There are three limits that may be set:
61 * - Limit the number of options in a Hop-by-Hop or Destination options
62 * extension header
63 * - Limit the byte length of a Hop-by-Hop or Destination options extension
64 * header
65 * - Disallow unknown options
66 *
67 * The limits are expressed in corresponding sysctls:
68 *
69 * ipv6.sysctl.max_dst_opts_cnt
70 * ipv6.sysctl.max_hbh_opts_cnt
71 * ipv6.sysctl.max_dst_opts_len
72 * ipv6.sysctl.max_hbh_opts_len
73 *
74 * max_*_opts_cnt is the number of TLVs that are allowed for Destination
75 * options or Hop-by-Hop options. If the number is less than zero then unknown
76 * TLVs are disallowed and the number of known options that are allowed is the
77 * absolute value. Setting the value to INT_MAX indicates no limit.
78 *
79 * max_*_opts_len is the length limit in bytes of a Destination or
80 * Hop-by-Hop options extension header. Setting the value to INT_MAX
81 * indicates no length limit.
82 *
83 * If a limit is exceeded when processing an extension header the packet is
84 * silently discarded.
85 */
86
87/* Default limits for Hop-by-Hop and Destination options */
88#define IP6_DEFAULT_MAX_DST_OPTS_CNT 8
89#define IP6_DEFAULT_MAX_HBH_OPTS_CNT 8
90#define IP6_DEFAULT_MAX_DST_OPTS_LEN INT_MAX /* No limit */
91#define IP6_DEFAULT_MAX_HBH_OPTS_LEN INT_MAX /* No limit */
92
93/*
94 * Addr type
95 *
96 * type - unicast | multicast
97 * scope - local | site | global
98 * v4 - compat
99 * v4mapped
100 * any
101 * loopback
102 */
103
104#define IPV6_ADDR_ANY 0x0000U
105
106#define IPV6_ADDR_UNICAST 0x0001U
107#define IPV6_ADDR_MULTICAST 0x0002U
108
109#define IPV6_ADDR_LOOPBACK 0x0010U
110#define IPV6_ADDR_LINKLOCAL 0x0020U
111#define IPV6_ADDR_SITELOCAL 0x0040U
112
113#define IPV6_ADDR_COMPATv4 0x0080U
114
115#define IPV6_ADDR_SCOPE_MASK 0x00f0U
116
117#define IPV6_ADDR_MAPPED 0x1000U
118
119/*
120 * Addr scopes
121 */
122#define IPV6_ADDR_MC_SCOPE(a) \
123 ((a)->s6_addr[1] & 0x0f) /* nonstandard */
124#define __IPV6_ADDR_SCOPE_INVALID -1
125#define IPV6_ADDR_SCOPE_NODELOCAL 0x01
126#define IPV6_ADDR_SCOPE_LINKLOCAL 0x02
127#define IPV6_ADDR_SCOPE_SITELOCAL 0x05
128#define IPV6_ADDR_SCOPE_ORGLOCAL 0x08
129#define IPV6_ADDR_SCOPE_GLOBAL 0x0e
130
131/*
132 * Addr flags
133 */
134#define IPV6_ADDR_MC_FLAG_TRANSIENT(a) \
135 ((a)->s6_addr[1] & 0x10)
136#define IPV6_ADDR_MC_FLAG_PREFIX(a) \
137 ((a)->s6_addr[1] & 0x20)
138#define IPV6_ADDR_MC_FLAG_RENDEZVOUS(a) \
139 ((a)->s6_addr[1] & 0x40)
140
141/*
142 * fragmentation header
143 */
144
145struct frag_hdr {
146 __u8 nexthdr;
147 __u8 reserved;
148 __be16 frag_off;
149 __be32 identification;
150};
151
152#define IP6_MF 0x0001
153#define IP6_OFFSET 0xFFF8
154
155struct ip6_fraglist_iter {
156 struct ipv6hdr *tmp_hdr;
157 struct sk_buff *frag;
158 int offset;
159 unsigned int hlen;
160 __be32 frag_id;
161 u8 nexthdr;
162};
163
164int ip6_fraglist_init(struct sk_buff *skb, unsigned int hlen, u8 *prevhdr,
165 u8 nexthdr, __be32 frag_id,
166 struct ip6_fraglist_iter *iter);
167void ip6_fraglist_prepare(struct sk_buff *skb, struct ip6_fraglist_iter *iter);
168
169static inline struct sk_buff *ip6_fraglist_next(struct ip6_fraglist_iter *iter)
170{
171 struct sk_buff *skb = iter->frag;
172
173 iter->frag = skb->next;
174 skb_mark_not_on_list(skb);
175
176 return skb;
177}
178
179struct ip6_frag_state {
180 u8 *prevhdr;
181 unsigned int hlen;
182 unsigned int mtu;
183 unsigned int left;
184 int offset;
185 int ptr;
186 int hroom;
187 int troom;
188 __be32 frag_id;
189 u8 nexthdr;
190};
191
192void ip6_frag_init(struct sk_buff *skb, unsigned int hlen, unsigned int mtu,
193 unsigned short needed_tailroom, int hdr_room, u8 *prevhdr,
194 u8 nexthdr, __be32 frag_id, struct ip6_frag_state *state);
195struct sk_buff *ip6_frag_next(struct sk_buff *skb,
196 struct ip6_frag_state *state);
197
198#define IP6_REPLY_MARK(net, mark) \
199 ((net)->ipv6.sysctl.fwmark_reflect ? (mark) : 0)
200
201#include <net/sock.h>
202
203/* sysctls */
204extern int sysctl_mld_max_msf;
205extern int sysctl_mld_qrv;
206
207#define _DEVINC(net, statname, mod, idev, field) \
208({ \
209 struct inet6_dev *_idev = (idev); \
210 if (likely(_idev != NULL)) \
211 mod##SNMP_INC_STATS64((_idev)->stats.statname, (field));\
212 mod##SNMP_INC_STATS64((net)->mib.statname##_statistics, (field));\
213})
214
215/* per device counters are atomic_long_t */
216#define _DEVINCATOMIC(net, statname, mod, idev, field) \
217({ \
218 struct inet6_dev *_idev = (idev); \
219 if (likely(_idev != NULL)) \
220 SNMP_INC_STATS_ATOMIC_LONG((_idev)->stats.statname##dev, (field)); \
221 mod##SNMP_INC_STATS((net)->mib.statname##_statistics, (field));\
222})
223
224/* per device and per net counters are atomic_long_t */
225#define _DEVINC_ATOMIC_ATOMIC(net, statname, idev, field) \
226({ \
227 struct inet6_dev *_idev = (idev); \
228 if (likely(_idev != NULL)) \
229 SNMP_INC_STATS_ATOMIC_LONG((_idev)->stats.statname##dev, (field)); \
230 SNMP_INC_STATS_ATOMIC_LONG((net)->mib.statname##_statistics, (field));\
231})
232
233#define _DEVADD(net, statname, mod, idev, field, val) \
234({ \
235 struct inet6_dev *_idev = (idev); \
236 unsigned long _field = (field); \
237 unsigned long _val = (val); \
238 if (likely(_idev != NULL)) \
239 mod##SNMP_ADD_STATS((_idev)->stats.statname, _field, _val); \
240 mod##SNMP_ADD_STATS((net)->mib.statname##_statistics, _field, _val);\
241})
242
243#define _DEVUPD(net, statname, mod, idev, field, val) \
244({ \
245 struct inet6_dev *_idev = (idev); \
246 unsigned long _val = (val); \
247 if (likely(_idev != NULL)) \
248 mod##SNMP_UPD_PO_STATS((_idev)->stats.statname, field, _val); \
249 mod##SNMP_UPD_PO_STATS((net)->mib.statname##_statistics, field, _val);\
250})
251
252/* MIBs */
253
254#define IP6_INC_STATS(net, idev,field) \
255 _DEVINC(net, ipv6, , idev, field)
256#define __IP6_INC_STATS(net, idev,field) \
257 _DEVINC(net, ipv6, __, idev, field)
258#define IP6_ADD_STATS(net, idev,field,val) \
259 _DEVADD(net, ipv6, , idev, field, val)
260#define __IP6_ADD_STATS(net, idev,field,val) \
261 _DEVADD(net, ipv6, __, idev, field, val)
262#define IP6_UPD_PO_STATS(net, idev,field,val) \
263 _DEVUPD(net, ipv6, , idev, field, val)
264#define __IP6_UPD_PO_STATS(net, idev,field,val) \
265 _DEVUPD(net, ipv6, __, idev, field, val)
266#define ICMP6_INC_STATS(net, idev, field) \
267 _DEVINCATOMIC(net, icmpv6, , idev, field)
268#define __ICMP6_INC_STATS(net, idev, field) \
269 _DEVINCATOMIC(net, icmpv6, __, idev, field)
270
271#define ICMP6MSGOUT_INC_STATS(net, idev, field) \
272 _DEVINC_ATOMIC_ATOMIC(net, icmpv6msg, idev, field +256)
273#define ICMP6MSGIN_INC_STATS(net, idev, field) \
274 _DEVINC_ATOMIC_ATOMIC(net, icmpv6msg, idev, field)
275
276struct ip6_ra_chain {
277 struct ip6_ra_chain *next;
278 struct sock *sk;
279 int sel;
280 void (*destructor)(struct sock *);
281};
282
283extern struct ip6_ra_chain *ip6_ra_chain;
284extern rwlock_t ip6_ra_lock;
285
286/*
287 This structure is prepared by protocol, when parsing
288 ancillary data and passed to IPv6.
289 */
290
291struct ipv6_txoptions {
292 refcount_t refcnt;
293 /* Length of this structure */
294 int tot_len;
295
296 /* length of extension headers */
297
298 __u16 opt_flen; /* after fragment hdr */
299 __u16 opt_nflen; /* before fragment hdr */
300
301 struct ipv6_opt_hdr *hopopt;
302 struct ipv6_opt_hdr *dst0opt;
303 struct ipv6_rt_hdr *srcrt; /* Routing Header */
304 struct ipv6_opt_hdr *dst1opt;
305 struct rcu_head rcu;
306 /* Option buffer, as read by IPV6_PKTOPTIONS, starts here. */
307};
308
309/* flowlabel_reflect sysctl values */
310enum flowlabel_reflect {
311 FLOWLABEL_REFLECT_ESTABLISHED = 1,
312 FLOWLABEL_REFLECT_TCP_RESET = 2,
313 FLOWLABEL_REFLECT_ICMPV6_ECHO_REPLIES = 4,
314};
315
316struct ip6_flowlabel {
317 struct ip6_flowlabel __rcu *next;
318 __be32 label;
319 atomic_t users;
320 struct in6_addr dst;
321 struct ipv6_txoptions *opt;
322 unsigned long linger;
323 struct rcu_head rcu;
324 u8 share;
325 union {
326 struct pid *pid;
327 kuid_t uid;
328 } owner;
329 unsigned long lastuse;
330 unsigned long expires;
331 struct net *fl_net;
332};
333
334#define IPV6_FLOWINFO_MASK cpu_to_be32(0x0FFFFFFF)
335#define IPV6_FLOWLABEL_MASK cpu_to_be32(0x000FFFFF)
336#define IPV6_FLOWLABEL_STATELESS_FLAG cpu_to_be32(0x00080000)
337
338#define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
339#define IPV6_TCLASS_SHIFT 20
340
341struct ipv6_fl_socklist {
342 struct ipv6_fl_socklist __rcu *next;
343 struct ip6_flowlabel *fl;
344 struct rcu_head rcu;
345};
346
347struct ipcm6_cookie {
348 struct sockcm_cookie sockc;
349 __s16 hlimit;
350 __s16 tclass;
351 __u16 gso_size;
352 __s8 dontfrag;
353 struct ipv6_txoptions *opt;
354};
355
356static inline void ipcm6_init_sk(struct ipcm6_cookie *ipc6,
357 const struct sock *sk)
358{
359 *ipc6 = (struct ipcm6_cookie) {
360 .hlimit = -1,
361 .tclass = inet6_sk(sk)->tclass,
362 .dontfrag = inet6_test_bit(DONTFRAG, sk),
363 };
364
365 sockcm_init(&ipc6->sockc, sk);
366}
367
368static inline struct ipv6_txoptions *txopt_get(const struct ipv6_pinfo *np)
369{
370 struct ipv6_txoptions *opt;
371
372 rcu_read_lock();
373 opt = rcu_dereference(np->opt);
374 if (opt) {
375 if (!refcount_inc_not_zero(&opt->refcnt))
376 opt = NULL;
377 else
378 opt = rcu_pointer_handoff(opt);
379 }
380 rcu_read_unlock();
381 return opt;
382}
383
384static inline void txopt_put(struct ipv6_txoptions *opt)
385{
386 if (opt && refcount_dec_and_test(&opt->refcnt))
387 kfree_rcu(opt, rcu);
388}
389
390#if IS_ENABLED(CONFIG_IPV6)
391struct ip6_flowlabel *__fl6_sock_lookup(struct sock *sk, __be32 label);
392
393extern struct static_key_false_deferred ipv6_flowlabel_exclusive;
394static inline struct ip6_flowlabel *fl6_sock_lookup(struct sock *sk,
395 __be32 label)
396{
397 if (static_branch_unlikely(&ipv6_flowlabel_exclusive.key) &&
398 READ_ONCE(sock_net(sk)->ipv6.flowlabel_has_excl))
399 return __fl6_sock_lookup(sk, label) ? : ERR_PTR(-ENOENT);
400
401 return NULL;
402}
403#endif
404
405struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions *opt_space,
406 struct ip6_flowlabel *fl,
407 struct ipv6_txoptions *fopt);
408void fl6_free_socklist(struct sock *sk);
409int ipv6_flowlabel_opt(struct sock *sk, sockptr_t optval, int optlen);
410int ipv6_flowlabel_opt_get(struct sock *sk, struct in6_flowlabel_req *freq,
411 int flags);
412int ip6_flowlabel_init(void);
413void ip6_flowlabel_cleanup(void);
414bool ip6_autoflowlabel(struct net *net, const struct sock *sk);
415
416static inline void fl6_sock_release(struct ip6_flowlabel *fl)
417{
418 if (fl)
419 atomic_dec(&fl->users);
420}
421
422enum skb_drop_reason icmpv6_notify(struct sk_buff *skb, u8 type,
423 u8 code, __be32 info);
424
425void icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
426 struct icmp6hdr *thdr, int len);
427
428int ip6_ra_control(struct sock *sk, int sel);
429
430int ipv6_parse_hopopts(struct sk_buff *skb);
431
432struct ipv6_txoptions *ipv6_dup_options(struct sock *sk,
433 struct ipv6_txoptions *opt);
434struct ipv6_txoptions *ipv6_renew_options(struct sock *sk,
435 struct ipv6_txoptions *opt,
436 int newtype,
437 struct ipv6_opt_hdr *newopt);
438struct ipv6_txoptions *__ipv6_fixup_options(struct ipv6_txoptions *opt_space,
439 struct ipv6_txoptions *opt);
440
441static inline struct ipv6_txoptions *
442ipv6_fixup_options(struct ipv6_txoptions *opt_space, struct ipv6_txoptions *opt)
443{
444 if (!opt)
445 return NULL;
446 return __ipv6_fixup_options(opt_space, opt);
447}
448
449bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb,
450 const struct inet6_skb_parm *opt);
451struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
452 struct ipv6_txoptions *opt);
453
454static inline bool ipv6_accept_ra(const struct inet6_dev *idev)
455{
456 s32 accept_ra = READ_ONCE(idev->cnf.accept_ra);
457
458 /* If forwarding is enabled, RA are not accepted unless the special
459 * hybrid mode (accept_ra=2) is enabled.
460 */
461 return READ_ONCE(idev->cnf.forwarding) ? accept_ra == 2 :
462 accept_ra;
463}
464
465#define IPV6_FRAG_HIGH_THRESH (4 * 1024*1024) /* 4194304 */
466#define IPV6_FRAG_LOW_THRESH (3 * 1024*1024) /* 3145728 */
467#define IPV6_FRAG_TIMEOUT (60 * HZ) /* 60 seconds */
468
469int __ipv6_addr_type(const struct in6_addr *addr);
470static inline int ipv6_addr_type(const struct in6_addr *addr)
471{
472 return __ipv6_addr_type(addr) & 0xffff;
473}
474
475static inline int ipv6_addr_scope(const struct in6_addr *addr)
476{
477 return __ipv6_addr_type(addr) & IPV6_ADDR_SCOPE_MASK;
478}
479
480static inline int __ipv6_addr_src_scope(int type)
481{
482 return (type == IPV6_ADDR_ANY) ? __IPV6_ADDR_SCOPE_INVALID : (type >> 16);
483}
484
485static inline int ipv6_addr_src_scope(const struct in6_addr *addr)
486{
487 return __ipv6_addr_src_scope(__ipv6_addr_type(addr));
488}
489
490static inline bool __ipv6_addr_needs_scope_id(int type)
491{
492 return type & IPV6_ADDR_LINKLOCAL ||
493 (type & IPV6_ADDR_MULTICAST &&
494 (type & (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)));
495}
496
497static inline __u32 ipv6_iface_scope_id(const struct in6_addr *addr, int iface)
498{
499 return __ipv6_addr_needs_scope_id(__ipv6_addr_type(addr)) ? iface : 0;
500}
501
502static inline int ipv6_addr_cmp(const struct in6_addr *a1, const struct in6_addr *a2)
503{
504 return memcmp(a1, a2, sizeof(struct in6_addr));
505}
506
507static inline bool
508ipv6_masked_addr_cmp(const struct in6_addr *a1, const struct in6_addr *m,
509 const struct in6_addr *a2)
510{
511#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
512 const unsigned long *ul1 = (const unsigned long *)a1;
513 const unsigned long *ulm = (const unsigned long *)m;
514 const unsigned long *ul2 = (const unsigned long *)a2;
515
516 return !!(((ul1[0] ^ ul2[0]) & ulm[0]) |
517 ((ul1[1] ^ ul2[1]) & ulm[1]));
518#else
519 return !!(((a1->s6_addr32[0] ^ a2->s6_addr32[0]) & m->s6_addr32[0]) |
520 ((a1->s6_addr32[1] ^ a2->s6_addr32[1]) & m->s6_addr32[1]) |
521 ((a1->s6_addr32[2] ^ a2->s6_addr32[2]) & m->s6_addr32[2]) |
522 ((a1->s6_addr32[3] ^ a2->s6_addr32[3]) & m->s6_addr32[3]));
523#endif
524}
525
526static inline void ipv6_addr_prefix(struct in6_addr *pfx,
527 const struct in6_addr *addr,
528 int plen)
529{
530 /* caller must guarantee 0 <= plen <= 128 */
531 int o = plen >> 3,
532 b = plen & 0x7;
533
534 memset(pfx->s6_addr, 0, sizeof(pfx->s6_addr));
535 memcpy(pfx->s6_addr, addr, o);
536 if (b != 0)
537 pfx->s6_addr[o] = addr->s6_addr[o] & (0xff00 >> b);
538}
539
540static inline void ipv6_addr_prefix_copy(struct in6_addr *addr,
541 const struct in6_addr *pfx,
542 int plen)
543{
544 /* caller must guarantee 0 <= plen <= 128 */
545 int o = plen >> 3,
546 b = plen & 0x7;
547
548 memcpy(addr->s6_addr, pfx, o);
549 if (b != 0) {
550 addr->s6_addr[o] &= ~(0xff00 >> b);
551 addr->s6_addr[o] |= (pfx->s6_addr[o] & (0xff00 >> b));
552 }
553}
554
555static inline void __ipv6_addr_set_half(__be32 *addr,
556 __be32 wh, __be32 wl)
557{
558#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
559#if defined(__BIG_ENDIAN)
560 if (__builtin_constant_p(wh) && __builtin_constant_p(wl)) {
561 *(__force u64 *)addr = ((__force u64)(wh) << 32 | (__force u64)(wl));
562 return;
563 }
564#elif defined(__LITTLE_ENDIAN)
565 if (__builtin_constant_p(wl) && __builtin_constant_p(wh)) {
566 *(__force u64 *)addr = ((__force u64)(wl) << 32 | (__force u64)(wh));
567 return;
568 }
569#endif
570#endif
571 addr[0] = wh;
572 addr[1] = wl;
573}
574
575static inline void ipv6_addr_set(struct in6_addr *addr,
576 __be32 w1, __be32 w2,
577 __be32 w3, __be32 w4)
578{
579 __ipv6_addr_set_half(&addr->s6_addr32[0], w1, w2);
580 __ipv6_addr_set_half(&addr->s6_addr32[2], w3, w4);
581}
582
583static inline bool ipv6_addr_equal(const struct in6_addr *a1,
584 const struct in6_addr *a2)
585{
586#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
587 const unsigned long *ul1 = (const unsigned long *)a1;
588 const unsigned long *ul2 = (const unsigned long *)a2;
589
590 return ((ul1[0] ^ ul2[0]) | (ul1[1] ^ ul2[1])) == 0UL;
591#else
592 return ((a1->s6_addr32[0] ^ a2->s6_addr32[0]) |
593 (a1->s6_addr32[1] ^ a2->s6_addr32[1]) |
594 (a1->s6_addr32[2] ^ a2->s6_addr32[2]) |
595 (a1->s6_addr32[3] ^ a2->s6_addr32[3])) == 0;
596#endif
597}
598
599#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
600static inline bool __ipv6_prefix_equal64_half(const __be64 *a1,
601 const __be64 *a2,
602 unsigned int len)
603{
604 if (len && ((*a1 ^ *a2) & cpu_to_be64((~0UL) << (64 - len))))
605 return false;
606 return true;
607}
608
609static inline bool ipv6_prefix_equal(const struct in6_addr *addr1,
610 const struct in6_addr *addr2,
611 unsigned int prefixlen)
612{
613 const __be64 *a1 = (const __be64 *)addr1;
614 const __be64 *a2 = (const __be64 *)addr2;
615
616 if (prefixlen >= 64) {
617 if (a1[0] ^ a2[0])
618 return false;
619 return __ipv6_prefix_equal64_half(a1 + 1, a2 + 1, prefixlen - 64);
620 }
621 return __ipv6_prefix_equal64_half(a1, a2, prefixlen);
622}
623#else
624static inline bool ipv6_prefix_equal(const struct in6_addr *addr1,
625 const struct in6_addr *addr2,
626 unsigned int prefixlen)
627{
628 const __be32 *a1 = addr1->s6_addr32;
629 const __be32 *a2 = addr2->s6_addr32;
630 unsigned int pdw, pbi;
631
632 /* check complete u32 in prefix */
633 pdw = prefixlen >> 5;
634 if (pdw && memcmp(a1, a2, pdw << 2))
635 return false;
636
637 /* check incomplete u32 in prefix */
638 pbi = prefixlen & 0x1f;
639 if (pbi && ((a1[pdw] ^ a2[pdw]) & htonl((0xffffffff) << (32 - pbi))))
640 return false;
641
642 return true;
643}
644#endif
645
646static inline bool ipv6_addr_any(const struct in6_addr *a)
647{
648#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
649 const unsigned long *ul = (const unsigned long *)a;
650
651 return (ul[0] | ul[1]) == 0UL;
652#else
653 return (a->s6_addr32[0] | a->s6_addr32[1] |
654 a->s6_addr32[2] | a->s6_addr32[3]) == 0;
655#endif
656}
657
658static inline u32 ipv6_addr_hash(const struct in6_addr *a)
659{
660#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
661 const unsigned long *ul = (const unsigned long *)a;
662 unsigned long x = ul[0] ^ ul[1];
663
664 return (u32)(x ^ (x >> 32));
665#else
666 return (__force u32)(a->s6_addr32[0] ^ a->s6_addr32[1] ^
667 a->s6_addr32[2] ^ a->s6_addr32[3]);
668#endif
669}
670
671/* more secured version of ipv6_addr_hash() */
672static inline u32 __ipv6_addr_jhash(const struct in6_addr *a, const u32 initval)
673{
674 return jhash2((__force const u32 *)a->s6_addr32,
675 ARRAY_SIZE(a->s6_addr32), initval);
676}
677
678static inline bool ipv6_addr_loopback(const struct in6_addr *a)
679{
680#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
681 const __be64 *be = (const __be64 *)a;
682
683 return (be[0] | (be[1] ^ cpu_to_be64(1))) == 0UL;
684#else
685 return (a->s6_addr32[0] | a->s6_addr32[1] |
686 a->s6_addr32[2] | (a->s6_addr32[3] ^ cpu_to_be32(1))) == 0;
687#endif
688}
689
690/*
691 * Note that we must __force cast these to unsigned long to make sparse happy,
692 * since all of the endian-annotated types are fixed size regardless of arch.
693 */
694static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
695{
696 return (
697#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
698 *(unsigned long *)a |
699#else
700 (__force unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
701#endif
702 (__force unsigned long)(a->s6_addr32[2] ^
703 cpu_to_be32(0x0000ffff))) == 0UL;
704}
705
706static inline bool ipv6_addr_v4mapped_loopback(const struct in6_addr *a)
707{
708 return ipv6_addr_v4mapped(a) && ipv4_is_loopback(a->s6_addr32[3]);
709}
710
711static inline u32 ipv6_portaddr_hash(const struct net *net,
712 const struct in6_addr *addr6,
713 unsigned int port)
714{
715 unsigned int hash, mix = net_hash_mix(net);
716
717 if (ipv6_addr_any(addr6))
718 hash = jhash_1word(0, mix);
719 else if (ipv6_addr_v4mapped(addr6))
720 hash = jhash_1word((__force u32)addr6->s6_addr32[3], mix);
721 else
722 hash = jhash2((__force u32 *)addr6->s6_addr32, 4, mix);
723
724 return hash ^ port;
725}
726
727/*
728 * Check for a RFC 4843 ORCHID address
729 * (Overlay Routable Cryptographic Hash Identifiers)
730 */
731static inline bool ipv6_addr_orchid(const struct in6_addr *a)
732{
733 return (a->s6_addr32[0] & htonl(0xfffffff0)) == htonl(0x20010010);
734}
735
736static inline bool ipv6_addr_is_multicast(const struct in6_addr *addr)
737{
738 return (addr->s6_addr32[0] & htonl(0xFF000000)) == htonl(0xFF000000);
739}
740
741static inline void ipv6_addr_set_v4mapped(const __be32 addr,
742 struct in6_addr *v4mapped)
743{
744 ipv6_addr_set(v4mapped,
745 0, 0,
746 htonl(0x0000FFFF),
747 addr);
748}
749
750/*
751 * find the first different bit between two addresses
752 * length of address must be a multiple of 32bits
753 */
754static inline int __ipv6_addr_diff32(const void *token1, const void *token2, int addrlen)
755{
756 const __be32 *a1 = token1, *a2 = token2;
757 int i;
758
759 addrlen >>= 2;
760
761 for (i = 0; i < addrlen; i++) {
762 __be32 xb = a1[i] ^ a2[i];
763 if (xb)
764 return i * 32 + 31 - __fls(ntohl(xb));
765 }
766
767 /*
768 * we should *never* get to this point since that
769 * would mean the addrs are equal
770 *
771 * However, we do get to it 8) And exactly, when
772 * addresses are equal 8)
773 *
774 * ip route add 1111::/128 via ...
775 * ip route add 1111::/64 via ...
776 * and we are here.
777 *
778 * Ideally, this function should stop comparison
779 * at prefix length. It does not, but it is still OK,
780 * if returned value is greater than prefix length.
781 * --ANK (980803)
782 */
783 return addrlen << 5;
784}
785
786#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
787static inline int __ipv6_addr_diff64(const void *token1, const void *token2, int addrlen)
788{
789 const __be64 *a1 = token1, *a2 = token2;
790 int i;
791
792 addrlen >>= 3;
793
794 for (i = 0; i < addrlen; i++) {
795 __be64 xb = a1[i] ^ a2[i];
796 if (xb)
797 return i * 64 + 63 - __fls(be64_to_cpu(xb));
798 }
799
800 return addrlen << 6;
801}
802#endif
803
804static inline int __ipv6_addr_diff(const void *token1, const void *token2, int addrlen)
805{
806#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
807 if (__builtin_constant_p(addrlen) && !(addrlen & 7))
808 return __ipv6_addr_diff64(token1, token2, addrlen);
809#endif
810 return __ipv6_addr_diff32(token1, token2, addrlen);
811}
812
813static inline int ipv6_addr_diff(const struct in6_addr *a1, const struct in6_addr *a2)
814{
815 return __ipv6_addr_diff(a1, a2, sizeof(struct in6_addr));
816}
817
818__be32 ipv6_select_ident(struct net *net,
819 const struct in6_addr *daddr,
820 const struct in6_addr *saddr);
821__be32 ipv6_proxy_select_ident(struct net *net, struct sk_buff *skb);
822
823int ip6_dst_hoplimit(struct dst_entry *dst);
824
825static inline int ip6_sk_dst_hoplimit(struct ipv6_pinfo *np, struct flowi6 *fl6,
826 struct dst_entry *dst)
827{
828 int hlimit;
829
830 if (ipv6_addr_is_multicast(&fl6->daddr))
831 hlimit = READ_ONCE(np->mcast_hops);
832 else
833 hlimit = READ_ONCE(np->hop_limit);
834 if (hlimit < 0)
835 hlimit = ip6_dst_hoplimit(dst);
836 return hlimit;
837}
838
839/* copy IPv6 saddr & daddr to flow_keys, possibly using 64bit load/store
840 * Equivalent to : flow->v6addrs.src = iph->saddr;
841 * flow->v6addrs.dst = iph->daddr;
842 */
843static inline void iph_to_flow_copy_v6addrs(struct flow_keys *flow,
844 const struct ipv6hdr *iph)
845{
846 BUILD_BUG_ON(offsetof(typeof(flow->addrs), v6addrs.dst) !=
847 offsetof(typeof(flow->addrs), v6addrs.src) +
848 sizeof(flow->addrs.v6addrs.src));
849 memcpy(&flow->addrs.v6addrs, &iph->addrs, sizeof(flow->addrs.v6addrs));
850 flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
851}
852
853#if IS_ENABLED(CONFIG_IPV6)
854
855static inline bool ipv6_can_nonlocal_bind(const struct net *net,
856 const struct inet_sock *inet)
857{
858 return READ_ONCE(net->ipv6.sysctl.ip_nonlocal_bind) ||
859 test_bit(INET_FLAGS_FREEBIND, &inet->inet_flags) ||
860 test_bit(INET_FLAGS_TRANSPARENT, &inet->inet_flags);
861}
862
863/* Sysctl settings for net ipv6.auto_flowlabels */
864#define IP6_AUTO_FLOW_LABEL_OFF 0
865#define IP6_AUTO_FLOW_LABEL_OPTOUT 1
866#define IP6_AUTO_FLOW_LABEL_OPTIN 2
867#define IP6_AUTO_FLOW_LABEL_FORCED 3
868
869#define IP6_AUTO_FLOW_LABEL_MAX IP6_AUTO_FLOW_LABEL_FORCED
870
871#define IP6_DEFAULT_AUTO_FLOW_LABELS IP6_AUTO_FLOW_LABEL_OPTOUT
872
873static inline __be32 ip6_make_flowlabel(const struct net *net,
874 struct sk_buff *skb,
875 __be32 flowlabel, bool autolabel,
876 struct flowi6 *fl6)
877{
878 u8 auto_flowlabels;
879 u32 hash;
880
881 /* @flowlabel may include more than a flow label, eg, the traffic class.
882 * Here we want only the flow label value.
883 */
884 flowlabel &= IPV6_FLOWLABEL_MASK;
885
886 if (flowlabel)
887 return flowlabel;
888
889 auto_flowlabels = READ_ONCE(net->ipv6.sysctl.auto_flowlabels);
890 if (auto_flowlabels == IP6_AUTO_FLOW_LABEL_OFF ||
891 (!autolabel && auto_flowlabels != IP6_AUTO_FLOW_LABEL_FORCED))
892 return flowlabel;
893
894 hash = skb_get_hash_flowi6(skb, fl6);
895
896 /* Since this is being sent on the wire obfuscate hash a bit
897 * to minimize possibility that any useful information to an
898 * attacker is leaked. Only lower 20 bits are relevant.
899 */
900 hash = rol32(hash, 16);
901
902 flowlabel = (__force __be32)hash & IPV6_FLOWLABEL_MASK;
903
904 if (READ_ONCE(net->ipv6.sysctl.flowlabel_state_ranges))
905 flowlabel |= IPV6_FLOWLABEL_STATELESS_FLAG;
906
907 return flowlabel;
908}
909
910static inline int ip6_default_np_autolabel(const struct net *net)
911{
912 switch (READ_ONCE(net->ipv6.sysctl.auto_flowlabels)) {
913 case IP6_AUTO_FLOW_LABEL_OFF:
914 case IP6_AUTO_FLOW_LABEL_OPTIN:
915 default:
916 return 0;
917 case IP6_AUTO_FLOW_LABEL_OPTOUT:
918 case IP6_AUTO_FLOW_LABEL_FORCED:
919 return 1;
920 }
921}
922#else
923static inline __be32 ip6_make_flowlabel(const struct net *net, struct sk_buff *skb,
924 __be32 flowlabel, bool autolabel,
925 struct flowi6 *fl6)
926{
927 return flowlabel;
928}
929static inline int ip6_default_np_autolabel(const struct net *net)
930{
931 return 0;
932}
933#endif
934
935#if IS_ENABLED(CONFIG_IPV6)
936static inline int ip6_multipath_hash_policy(const struct net *net)
937{
938 return READ_ONCE(net->ipv6.sysctl.multipath_hash_policy);
939}
940static inline u32 ip6_multipath_hash_fields(const struct net *net)
941{
942 return READ_ONCE(net->ipv6.sysctl.multipath_hash_fields);
943}
944#else
945static inline int ip6_multipath_hash_policy(const struct net *net)
946{
947 return 0;
948}
949static inline u32 ip6_multipath_hash_fields(const struct net *net)
950{
951 return 0;
952}
953#endif
954
955/*
956 * Header manipulation
957 */
958static inline void ip6_flow_hdr(struct ipv6hdr *hdr, unsigned int tclass,
959 __be32 flowlabel)
960{
961 *(__be32 *)hdr = htonl(0x60000000 | (tclass << 20)) | flowlabel;
962}
963
964static inline __be32 ip6_flowinfo(const struct ipv6hdr *hdr)
965{
966 return *(__be32 *)hdr & IPV6_FLOWINFO_MASK;
967}
968
969static inline __be32 ip6_flowlabel(const struct ipv6hdr *hdr)
970{
971 return *(__be32 *)hdr & IPV6_FLOWLABEL_MASK;
972}
973
974static inline u8 ip6_tclass(__be32 flowinfo)
975{
976 return ntohl(flowinfo & IPV6_TCLASS_MASK) >> IPV6_TCLASS_SHIFT;
977}
978
979static inline dscp_t ip6_dscp(__be32 flowinfo)
980{
981 return inet_dsfield_to_dscp(ip6_tclass(flowinfo));
982}
983
984static inline __be32 ip6_make_flowinfo(unsigned int tclass, __be32 flowlabel)
985{
986 return htonl(tclass << IPV6_TCLASS_SHIFT) | flowlabel;
987}
988
989static inline __be32 flowi6_get_flowlabel(const struct flowi6 *fl6)
990{
991 return fl6->flowlabel & IPV6_FLOWLABEL_MASK;
992}
993
994/*
995 * Prototypes exported by ipv6
996 */
997
998/*
999 * rcv function (called from netdevice level)
1000 */
1001
1002int ipv6_rcv(struct sk_buff *skb, struct net_device *dev,
1003 struct packet_type *pt, struct net_device *orig_dev);
1004void ipv6_list_rcv(struct list_head *head, struct packet_type *pt,
1005 struct net_device *orig_dev);
1006
1007int ip6_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
1008
1009/*
1010 * upper-layer output functions
1011 */
1012int ip6_xmit(const struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
1013 __u32 mark, struct ipv6_txoptions *opt, int tclass, u32 priority);
1014
1015int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr);
1016
1017int ip6_append_data(struct sock *sk,
1018 int getfrag(void *from, char *to, int offset, int len,
1019 int odd, struct sk_buff *skb),
1020 void *from, size_t length, int transhdrlen,
1021 struct ipcm6_cookie *ipc6, struct flowi6 *fl6,
1022 struct rt6_info *rt, unsigned int flags);
1023
1024int ip6_push_pending_frames(struct sock *sk);
1025
1026void ip6_flush_pending_frames(struct sock *sk);
1027
1028int ip6_send_skb(struct sk_buff *skb);
1029
1030struct sk_buff *__ip6_make_skb(struct sock *sk, struct sk_buff_head *queue,
1031 struct inet_cork_full *cork);
1032struct sk_buff *ip6_make_skb(struct sock *sk,
1033 int getfrag(void *from, char *to, int offset,
1034 int len, int odd, struct sk_buff *skb),
1035 void *from, size_t length, int transhdrlen,
1036 struct ipcm6_cookie *ipc6,
1037 struct rt6_info *rt, unsigned int flags,
1038 struct inet_cork_full *cork);
1039
1040static inline struct sk_buff *ip6_finish_skb(struct sock *sk)
1041{
1042 return __ip6_make_skb(sk, &sk->sk_write_queue, &inet_sk(sk)->cork);
1043}
1044
1045int ip6_dst_lookup(struct net *net, struct sock *sk, struct dst_entry **dst,
1046 struct flowi6 *fl6);
1047struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struct sock *sk, struct flowi6 *fl6,
1048 const struct in6_addr *final_dst);
1049struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
1050 const struct in6_addr *final_dst,
1051 bool connected);
1052struct dst_entry *ip6_blackhole_route(struct net *net,
1053 struct dst_entry *orig_dst);
1054
1055/*
1056 * skb processing functions
1057 */
1058
1059int ip6_output(struct net *net, struct sock *sk, struct sk_buff *skb);
1060int ip6_forward(struct sk_buff *skb);
1061int ip6_input(struct sk_buff *skb);
1062int ip6_mc_input(struct sk_buff *skb);
1063void ip6_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int nexthdr,
1064 bool have_final);
1065
1066int __ip6_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
1067int ip6_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
1068
1069/*
1070 * Extension header (options) processing
1071 */
1072
1073u8 ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
1074 u8 proto, struct in6_addr **daddr_p,
1075 struct in6_addr *saddr);
1076u8 ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
1077 u8 proto);
1078
1079int ipv6_skip_exthdr(const struct sk_buff *, int start, u8 *nexthdrp,
1080 __be16 *frag_offp);
1081
1082bool ipv6_ext_hdr(u8 nexthdr);
1083
1084enum {
1085 IP6_FH_F_FRAG = (1 << 0),
1086 IP6_FH_F_AUTH = (1 << 1),
1087 IP6_FH_F_SKIP_RH = (1 << 2),
1088};
1089
1090/* find specified header and get offset to it */
1091int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset, int target,
1092 unsigned short *fragoff, int *fragflg);
1093
1094int ipv6_find_tlv(const struct sk_buff *skb, int offset, int type);
1095
1096struct in6_addr *__fl6_update_dst(struct flowi6 *fl6,
1097 const struct ipv6_txoptions *opt,
1098 struct in6_addr *orig);
1099
1100static inline struct in6_addr *
1101fl6_update_dst(struct flowi6 *fl6, const struct ipv6_txoptions *opt,
1102 struct in6_addr *orig)
1103{
1104 if (likely(!opt))
1105 return NULL;
1106
1107 return __fl6_update_dst(fl6, opt, orig);
1108}
1109
1110/*
1111 * socket options (ipv6_sockglue.c)
1112 */
1113DECLARE_STATIC_KEY_FALSE(ip6_min_hopcount);
1114
1115int do_ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1116 unsigned int optlen);
1117int ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1118 unsigned int optlen);
1119int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
1120 sockptr_t optval, sockptr_t optlen);
1121int ipv6_getsockopt(struct sock *sk, int level, int optname,
1122 char __user *optval, int __user *optlen);
1123
1124int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *addr,
1125 int addr_len);
1126int ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *addr, int addr_len);
1127int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr_unsized *addr,
1128 int addr_len);
1129int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr);
1130void ip6_datagram_release_cb(struct sock *sk);
1131
1132int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len,
1133 int *addr_len);
1134int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
1135 int *addr_len);
1136void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err, __be16 port,
1137 u32 info, u8 *payload);
1138void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info);
1139void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu);
1140
1141void inet6_cleanup_sock(struct sock *sk);
1142void inet6_sock_destruct(struct sock *sk);
1143int inet6_release(struct socket *sock);
1144int inet6_bind(struct socket *sock, struct sockaddr_unsized *uaddr, int addr_len);
1145int inet6_bind_sk(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len);
1146int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
1147 int peer);
1148int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
1149int inet6_compat_ioctl(struct socket *sock, unsigned int cmd,
1150 unsigned long arg);
1151
1152int inet6_hash_connect(struct inet_timewait_death_row *death_row,
1153 struct sock *sk);
1154int inet6_sendmsg(struct socket *sock, struct msghdr *msg, size_t size);
1155int inet6_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1156 int flags);
1157
1158/*
1159 * reassembly.c
1160 */
1161extern const struct proto_ops inet6_stream_ops;
1162extern const struct proto_ops inet6_dgram_ops;
1163extern const struct proto_ops inet6_sockraw_ops;
1164
1165struct group_source_req;
1166struct group_filter;
1167
1168int ip6_mc_source(int add, int omode, struct sock *sk,
1169 struct group_source_req *pgsr);
1170int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf,
1171 struct sockaddr_storage *list);
1172int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
1173 sockptr_t optval, size_t ss_offset);
1174
1175#ifdef CONFIG_PROC_FS
1176int ac6_proc_init(struct net *net);
1177void ac6_proc_exit(struct net *net);
1178int raw6_proc_init(void);
1179void raw6_proc_exit(void);
1180int tcp6_proc_init(struct net *net);
1181void tcp6_proc_exit(struct net *net);
1182int udp6_proc_init(struct net *net);
1183void udp6_proc_exit(struct net *net);
1184int udplite6_proc_init(void);
1185void udplite6_proc_exit(void);
1186int ipv6_misc_proc_init(void);
1187void ipv6_misc_proc_exit(void);
1188int snmp6_register_dev(struct inet6_dev *idev);
1189int snmp6_unregister_dev(struct inet6_dev *idev);
1190
1191#else
1192static inline int ac6_proc_init(struct net *net) { return 0; }
1193static inline void ac6_proc_exit(struct net *net) { }
1194static inline int snmp6_register_dev(struct inet6_dev *idev) { return 0; }
1195static inline int snmp6_unregister_dev(struct inet6_dev *idev) { return 0; }
1196#endif
1197
1198#ifdef CONFIG_SYSCTL
1199struct ctl_table *ipv6_icmp_sysctl_init(struct net *net);
1200size_t ipv6_icmp_sysctl_table_size(void);
1201struct ctl_table *ipv6_route_sysctl_init(struct net *net);
1202size_t ipv6_route_sysctl_table_size(struct net *net);
1203int ipv6_sysctl_register(void);
1204void ipv6_sysctl_unregister(void);
1205#endif
1206
1207int ipv6_sock_mc_join(struct sock *sk, int ifindex,
1208 const struct in6_addr *addr);
1209int ipv6_sock_mc_join_ssm(struct sock *sk, int ifindex,
1210 const struct in6_addr *addr, unsigned int mode);
1211int ipv6_sock_mc_drop(struct sock *sk, int ifindex,
1212 const struct in6_addr *addr);
1213
1214static inline int ip6_sock_set_v6only(struct sock *sk)
1215{
1216 int ret = 0;
1217
1218 lock_sock(sk);
1219 if (inet_sk(sk)->inet_num)
1220 ret = -EINVAL;
1221 else
1222 sk->sk_ipv6only = true;
1223 release_sock(sk);
1224 return ret;
1225}
1226
1227static inline void ip6_sock_set_recverr(struct sock *sk)
1228{
1229 inet6_set_bit(RECVERR6, sk);
1230}
1231
1232#define IPV6_PREFER_SRC_MASK (IPV6_PREFER_SRC_TMP | IPV6_PREFER_SRC_PUBLIC | \
1233 IPV6_PREFER_SRC_COA)
1234
1235static inline int ip6_sock_set_addr_preferences(struct sock *sk, int val)
1236{
1237 unsigned int prefmask = ~IPV6_PREFER_SRC_MASK;
1238 unsigned int pref = 0;
1239
1240 /* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
1241 switch (val & (IPV6_PREFER_SRC_PUBLIC |
1242 IPV6_PREFER_SRC_TMP |
1243 IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
1244 case IPV6_PREFER_SRC_PUBLIC:
1245 pref |= IPV6_PREFER_SRC_PUBLIC;
1246 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC |
1247 IPV6_PREFER_SRC_TMP);
1248 break;
1249 case IPV6_PREFER_SRC_TMP:
1250 pref |= IPV6_PREFER_SRC_TMP;
1251 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC |
1252 IPV6_PREFER_SRC_TMP);
1253 break;
1254 case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
1255 prefmask &= ~(IPV6_PREFER_SRC_PUBLIC |
1256 IPV6_PREFER_SRC_TMP);
1257 break;
1258 case 0:
1259 break;
1260 default:
1261 return -EINVAL;
1262 }
1263
1264 /* check HOME/COA conflicts */
1265 switch (val & (IPV6_PREFER_SRC_HOME | IPV6_PREFER_SRC_COA)) {
1266 case IPV6_PREFER_SRC_HOME:
1267 prefmask &= ~IPV6_PREFER_SRC_COA;
1268 break;
1269 case IPV6_PREFER_SRC_COA:
1270 pref |= IPV6_PREFER_SRC_COA;
1271 break;
1272 case 0:
1273 break;
1274 default:
1275 return -EINVAL;
1276 }
1277
1278 /* check CGA/NONCGA conflicts */
1279 switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
1280 case IPV6_PREFER_SRC_CGA:
1281 case IPV6_PREFER_SRC_NONCGA:
1282 case 0:
1283 break;
1284 default:
1285 return -EINVAL;
1286 }
1287
1288 WRITE_ONCE(inet6_sk(sk)->srcprefs,
1289 (READ_ONCE(inet6_sk(sk)->srcprefs) & prefmask) | pref);
1290 return 0;
1291}
1292
1293static inline void ip6_sock_set_recvpktinfo(struct sock *sk)
1294{
1295 lock_sock(sk);
1296 inet6_sk(sk)->rxopt.bits.rxinfo = true;
1297 release_sock(sk);
1298}
1299
1300#define IPV6_ADDR_WORDS 4
1301
1302static inline void ipv6_addr_cpu_to_be32(__be32 *dst, const u32 *src)
1303{
1304 cpu_to_be32_array(dst, src, IPV6_ADDR_WORDS);
1305}
1306
1307static inline void ipv6_addr_be32_to_cpu(u32 *dst, const __be32 *src)
1308{
1309 be32_to_cpu_array(dst, src, IPV6_ADDR_WORDS);
1310}
1311
1312#endif /* _NET_IPV6_H */