xref: /openbmc/linux/include/net/ipv6.h (revision 5804c19b80bf625c6a9925317f845e497434d6d3)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *	Linux INET6 implementation
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *	Authors:
61da177e4SLinus Torvalds  *	Pedro Roque		<roque@di.fc.ul.pt>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #ifndef _NET_IPV6_H
101da177e4SLinus Torvalds #define _NET_IPV6_H
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/ipv6.h>
131da177e4SLinus Torvalds #include <linux/hardirq.h>
1408dcdbf6SEric Dumazet #include <linux/jhash.h>
150aeea21aSReshetova, Elena #include <linux/refcount.h>
1659c820b2SWillem de Bruijn #include <linux/jump_label_ratelimit.h>
1720283d84SHerbert Xu #include <net/if_inet6.h>
181da177e4SLinus Torvalds #include <net/flow.h>
191bd758ebSJiri Pirko #include <net/flow_dissector.h>
20a410a0cfSGuillaume Nault #include <net/inet_dscp.h>
211da177e4SLinus Torvalds #include <net/snmp.h>
22f0b1e64cSMartin KaFai Lau #include <net/netns/hash.h>
231da177e4SLinus Torvalds 
24b6459415SJakub Kicinski struct ip_tunnel_info;
25b6459415SJakub Kicinski 
261da177e4SLinus Torvalds #define SIN6_LEN_RFC2133	24
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds #define IPV6_MAXPLEN		65535
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds /*
311da177e4SLinus Torvalds  *	NextHeader field of IPv6 header
321da177e4SLinus Torvalds  */
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #define NEXTHDR_HOP		0	/* Hop-by-hop option header. */
35ee90c6baSJulien Massonneau #define NEXTHDR_IPV4		4	/* IPv4 in IPv6 */
361da177e4SLinus Torvalds #define NEXTHDR_TCP		6	/* TCP segment. */
371da177e4SLinus Torvalds #define NEXTHDR_UDP		17	/* UDP message. */
381da177e4SLinus Torvalds #define NEXTHDR_IPV6		41	/* IPv6 in IPv6 */
391da177e4SLinus Torvalds #define NEXTHDR_ROUTING		43	/* Routing header. */
401da177e4SLinus Torvalds #define NEXTHDR_FRAGMENT	44	/* Fragmentation/reassembly header. */
41c12b395aSxeb@mail.ru #define NEXTHDR_GRE		47	/* GRE header. */
421da177e4SLinus Torvalds #define NEXTHDR_ESP		50	/* Encapsulating security payload. */
431da177e4SLinus Torvalds #define NEXTHDR_AUTH		51	/* Authentication header. */
441da177e4SLinus Torvalds #define NEXTHDR_ICMP		58	/* ICMP for IPv6. */
451da177e4SLinus Torvalds #define NEXTHDR_NONE		59	/* No next header */
461da177e4SLinus Torvalds #define NEXTHDR_DEST		60	/* Destination options header. */
47280c571eSJoe Stringer #define NEXTHDR_SCTP		132	/* SCTP message. */
482b741653SMasahide NAKAMURA #define NEXTHDR_MOBILITY	135	/* Mobility header. */
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #define NEXTHDR_MAX		255
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #define IPV6_DEFAULT_HOPLIMIT   64
531da177e4SLinus Torvalds #define IPV6_DEFAULT_MCASTHOPS	1
541da177e4SLinus Torvalds 
5547d3d7acSTom Herbert /* Limits on Hop-by-Hop and Destination options.
5647d3d7acSTom Herbert  *
5747d3d7acSTom Herbert  * Per RFC8200 there is no limit on the maximum number or lengths of options in
5847d3d7acSTom Herbert  * Hop-by-Hop or Destination options other then the packet must fit in an MTU.
5947d3d7acSTom Herbert  * We allow configurable limits in order to mitigate potential denial of
6047d3d7acSTom Herbert  * service attacks.
6147d3d7acSTom Herbert  *
6247d3d7acSTom Herbert  * There are three limits that may be set:
6347d3d7acSTom Herbert  *   - Limit the number of options in a Hop-by-Hop or Destination options
6447d3d7acSTom Herbert  *     extension header
6547d3d7acSTom Herbert  *   - Limit the byte length of a Hop-by-Hop or Destination options extension
6647d3d7acSTom Herbert  *     header
6747d3d7acSTom Herbert  *   - Disallow unknown options
6847d3d7acSTom Herbert  *
6947d3d7acSTom Herbert  * The limits are expressed in corresponding sysctls:
7047d3d7acSTom Herbert  *
7147d3d7acSTom Herbert  * ipv6.sysctl.max_dst_opts_cnt
7247d3d7acSTom Herbert  * ipv6.sysctl.max_hbh_opts_cnt
7347d3d7acSTom Herbert  * ipv6.sysctl.max_dst_opts_len
7447d3d7acSTom Herbert  * ipv6.sysctl.max_hbh_opts_len
7547d3d7acSTom Herbert  *
7647d3d7acSTom Herbert  * max_*_opts_cnt is the number of TLVs that are allowed for Destination
7747d3d7acSTom Herbert  * options or Hop-by-Hop options. If the number is less than zero then unknown
7847d3d7acSTom Herbert  * TLVs are disallowed and the number of known options that are allowed is the
7947d3d7acSTom Herbert  * absolute value. Setting the value to INT_MAX indicates no limit.
8047d3d7acSTom Herbert  *
8147d3d7acSTom Herbert  * max_*_opts_len is the length limit in bytes of a Destination or
8247d3d7acSTom Herbert  * Hop-by-Hop options extension header. Setting the value to INT_MAX
8347d3d7acSTom Herbert  * indicates no length limit.
8447d3d7acSTom Herbert  *
8547d3d7acSTom Herbert  * If a limit is exceeded when processing an extension header the packet is
8647d3d7acSTom Herbert  * silently discarded.
8747d3d7acSTom Herbert  */
8847d3d7acSTom Herbert 
8947d3d7acSTom Herbert /* Default limits for Hop-by-Hop and Destination options */
9047d3d7acSTom Herbert #define IP6_DEFAULT_MAX_DST_OPTS_CNT	 8
9147d3d7acSTom Herbert #define IP6_DEFAULT_MAX_HBH_OPTS_CNT	 8
9247d3d7acSTom Herbert #define IP6_DEFAULT_MAX_DST_OPTS_LEN	 INT_MAX /* No limit */
9347d3d7acSTom Herbert #define IP6_DEFAULT_MAX_HBH_OPTS_LEN	 INT_MAX /* No limit */
9447d3d7acSTom Herbert 
951da177e4SLinus Torvalds /*
961da177e4SLinus Torvalds  *	Addr type
971da177e4SLinus Torvalds  *
981da177e4SLinus Torvalds  *	type	-	unicast | multicast
991da177e4SLinus Torvalds  *	scope	-	local	| site	    | global
1001da177e4SLinus Torvalds  *	v4	-	compat
1011da177e4SLinus Torvalds  *	v4mapped
1021da177e4SLinus Torvalds  *	any
1031da177e4SLinus Torvalds  *	loopback
1041da177e4SLinus Torvalds  */
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds #define IPV6_ADDR_ANY		0x0000U
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds #define IPV6_ADDR_UNICAST	0x0001U
1091da177e4SLinus Torvalds #define IPV6_ADDR_MULTICAST	0x0002U
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds #define IPV6_ADDR_LOOPBACK	0x0010U
1121da177e4SLinus Torvalds #define IPV6_ADDR_LINKLOCAL	0x0020U
1131da177e4SLinus Torvalds #define IPV6_ADDR_SITELOCAL	0x0040U
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds #define IPV6_ADDR_COMPATv4	0x0080U
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_MASK	0x00f0U
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds #define IPV6_ADDR_MAPPED	0x1000U
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds /*
1221da177e4SLinus Torvalds  *	Addr scopes
1231da177e4SLinus Torvalds  */
1241da177e4SLinus Torvalds #define IPV6_ADDR_MC_SCOPE(a)	\
1251da177e4SLinus Torvalds 	((a)->s6_addr[1] & 0x0f)	/* nonstandard */
1261da177e4SLinus Torvalds #define __IPV6_ADDR_SCOPE_INVALID	-1
1271da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_NODELOCAL	0x01
1281da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_LINKLOCAL	0x02
1291da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_SITELOCAL	0x05
1301da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_ORGLOCAL	0x08
1311da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_GLOBAL		0x0e
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds /*
1345ced1339SLinus Lüssing  *	Addr flags
1355ced1339SLinus Lüssing  */
1365ced1339SLinus Lüssing #define IPV6_ADDR_MC_FLAG_TRANSIENT(a)	\
1375ced1339SLinus Lüssing 	((a)->s6_addr[1] & 0x10)
1385ced1339SLinus Lüssing #define IPV6_ADDR_MC_FLAG_PREFIX(a)	\
1395ced1339SLinus Lüssing 	((a)->s6_addr[1] & 0x20)
1405ced1339SLinus Lüssing #define IPV6_ADDR_MC_FLAG_RENDEZVOUS(a)	\
1415ced1339SLinus Lüssing 	((a)->s6_addr[1] & 0x40)
1425ced1339SLinus Lüssing 
1435ced1339SLinus Lüssing /*
1441da177e4SLinus Torvalds  *	fragmentation header
1451da177e4SLinus Torvalds  */
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds struct frag_hdr {
14844473a6bSAl Viro 	__u8	nexthdr;
14944473a6bSAl Viro 	__u8	reserved;
15044473a6bSAl Viro 	__be16	frag_off;
15144473a6bSAl Viro 	__be32	identification;
1521da177e4SLinus Torvalds };
1531da177e4SLinus Torvalds 
1547c96d8ecSEric Dumazet /*
1557c96d8ecSEric Dumazet  * Jumbo payload option, as described in RFC 2675 2.
1567c96d8ecSEric Dumazet  */
1577c96d8ecSEric Dumazet struct hop_jumbo_hdr {
1587c96d8ecSEric Dumazet 	u8	nexthdr;
1597c96d8ecSEric Dumazet 	u8	hdrlen;
1607c96d8ecSEric Dumazet 	u8	tlv_type;	/* IPV6_TLV_JUMBO, 0xC2 */
1617c96d8ecSEric Dumazet 	u8	tlv_len;	/* 4 */
1627c96d8ecSEric Dumazet 	__be32	jumbo_payload_len;
1637c96d8ecSEric Dumazet };
1647c96d8ecSEric Dumazet 
1651da177e4SLinus Torvalds #define	IP6_MF		0x0001
1661431fb31SPaul Durrant #define	IP6_OFFSET	0xFFF8
1671da177e4SLinus Torvalds 
1680feca619SPablo Neira Ayuso struct ip6_fraglist_iter {
1690feca619SPablo Neira Ayuso 	struct ipv6hdr	*tmp_hdr;
1700feca619SPablo Neira Ayuso 	struct sk_buff	*frag;
1710feca619SPablo Neira Ayuso 	int		offset;
1720feca619SPablo Neira Ayuso 	unsigned int	hlen;
1730feca619SPablo Neira Ayuso 	__be32		frag_id;
1740feca619SPablo Neira Ayuso 	u8		nexthdr;
1750feca619SPablo Neira Ayuso };
1760feca619SPablo Neira Ayuso 
1770feca619SPablo Neira Ayuso int ip6_fraglist_init(struct sk_buff *skb, unsigned int hlen, u8 *prevhdr,
1780feca619SPablo Neira Ayuso 		      u8 nexthdr, __be32 frag_id,
1790feca619SPablo Neira Ayuso 		      struct ip6_fraglist_iter *iter);
1800feca619SPablo Neira Ayuso void ip6_fraglist_prepare(struct sk_buff *skb, struct ip6_fraglist_iter *iter);
1810feca619SPablo Neira Ayuso 
ip6_fraglist_next(struct ip6_fraglist_iter * iter)1820feca619SPablo Neira Ayuso static inline struct sk_buff *ip6_fraglist_next(struct ip6_fraglist_iter *iter)
1830feca619SPablo Neira Ayuso {
1840feca619SPablo Neira Ayuso 	struct sk_buff *skb = iter->frag;
1850feca619SPablo Neira Ayuso 
1860feca619SPablo Neira Ayuso 	iter->frag = skb->next;
1870feca619SPablo Neira Ayuso 	skb_mark_not_on_list(skb);
1880feca619SPablo Neira Ayuso 
1890feca619SPablo Neira Ayuso 	return skb;
1900feca619SPablo Neira Ayuso }
1910feca619SPablo Neira Ayuso 
1928a6a1f17SPablo Neira Ayuso struct ip6_frag_state {
1938a6a1f17SPablo Neira Ayuso 	u8		*prevhdr;
1948a6a1f17SPablo Neira Ayuso 	unsigned int	hlen;
1958a6a1f17SPablo Neira Ayuso 	unsigned int	mtu;
1968a6a1f17SPablo Neira Ayuso 	unsigned int	left;
1978a6a1f17SPablo Neira Ayuso 	int		offset;
1988a6a1f17SPablo Neira Ayuso 	int		ptr;
1998a6a1f17SPablo Neira Ayuso 	int		hroom;
2008a6a1f17SPablo Neira Ayuso 	int		troom;
2018a6a1f17SPablo Neira Ayuso 	__be32		frag_id;
2028a6a1f17SPablo Neira Ayuso 	u8		nexthdr;
2038a6a1f17SPablo Neira Ayuso };
2048a6a1f17SPablo Neira Ayuso 
2058a6a1f17SPablo Neira Ayuso void ip6_frag_init(struct sk_buff *skb, unsigned int hlen, unsigned int mtu,
2068a6a1f17SPablo Neira Ayuso 		   unsigned short needed_tailroom, int hdr_room, u8 *prevhdr,
2078a6a1f17SPablo Neira Ayuso 		   u8 nexthdr, __be32 frag_id, struct ip6_frag_state *state);
2088a6a1f17SPablo Neira Ayuso struct sk_buff *ip6_frag_next(struct sk_buff *skb,
2098a6a1f17SPablo Neira Ayuso 			      struct ip6_frag_state *state);
2108a6a1f17SPablo Neira Ayuso 
211e110861fSLorenzo Colitti #define IP6_REPLY_MARK(net, mark) \
212e110861fSLorenzo Colitti 	((net)->ipv6.sysctl.fwmark_reflect ? (mark) : 0)
213e110861fSLorenzo Colitti 
2141da177e4SLinus Torvalds #include <net/sock.h>
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds /* sysctls */
2171da177e4SLinus Torvalds extern int sysctl_mld_max_msf;
2182f711939SHannes Frederic Sowa extern int sysctl_mld_qrv;
2193d7cc2baSPavel Emelyanov 
22013415e46SEric Dumazet #define _DEVINC(net, statname, mod, idev, field)			\
22114878f75SDavid L Stevens ({									\
22214878f75SDavid L Stevens 	struct inet6_dev *_idev = (idev);				\
22314878f75SDavid L Stevens 	if (likely(_idev != NULL))					\
22413415e46SEric Dumazet 		mod##SNMP_INC_STATS64((_idev)->stats.statname, (field));\
22513415e46SEric Dumazet 	mod##SNMP_INC_STATS64((net)->mib.statname##_statistics, (field));\
22614878f75SDavid L Stevens })
22714878f75SDavid L Stevens 
228be281e55SEric Dumazet /* per device counters are atomic_long_t */
22913415e46SEric Dumazet #define _DEVINCATOMIC(net, statname, mod, idev, field)			\
230be281e55SEric Dumazet ({									\
231be281e55SEric Dumazet 	struct inet6_dev *_idev = (idev);				\
232be281e55SEric Dumazet 	if (likely(_idev != NULL))					\
233be281e55SEric Dumazet 		SNMP_INC_STATS_ATOMIC_LONG((_idev)->stats.statname##dev, (field)); \
23413415e46SEric Dumazet 	mod##SNMP_INC_STATS((net)->mib.statname##_statistics, (field));\
235be281e55SEric Dumazet })
236be281e55SEric Dumazet 
2372a24444fSEric Dumazet /* per device and per net counters are atomic_long_t */
2382a24444fSEric Dumazet #define _DEVINC_ATOMIC_ATOMIC(net, statname, idev, field)		\
2392a24444fSEric Dumazet ({									\
2402a24444fSEric Dumazet 	struct inet6_dev *_idev = (idev);				\
2412a24444fSEric Dumazet 	if (likely(_idev != NULL))					\
2422a24444fSEric Dumazet 		SNMP_INC_STATS_ATOMIC_LONG((_idev)->stats.statname##dev, (field)); \
2432a24444fSEric Dumazet 	SNMP_INC_STATS_ATOMIC_LONG((net)->mib.statname##_statistics, (field));\
2442a24444fSEric Dumazet })
2452a24444fSEric Dumazet 
24613415e46SEric Dumazet #define _DEVADD(net, statname, mod, idev, field, val)			\
2478e7999c4SPavel Emelyanov ({									\
2488e7999c4SPavel Emelyanov 	struct inet6_dev *_idev = (idev);				\
2498e7999c4SPavel Emelyanov 	if (likely(_idev != NULL))					\
25013415e46SEric Dumazet 		mod##SNMP_ADD_STATS((_idev)->stats.statname, (field), (val)); \
25113415e46SEric Dumazet 	mod##SNMP_ADD_STATS((net)->mib.statname##_statistics, (field), (val));\
2528e7999c4SPavel Emelyanov })
2538e7999c4SPavel Emelyanov 
25413415e46SEric Dumazet #define _DEVUPD(net, statname, mod, idev, field, val)			\
255edf391ffSNeil Horman ({									\
256edf391ffSNeil Horman 	struct inet6_dev *_idev = (idev);				\
257edf391ffSNeil Horman 	if (likely(_idev != NULL))					\
25813415e46SEric Dumazet 		mod##SNMP_UPD_PO_STATS((_idev)->stats.statname, field, (val)); \
25913415e46SEric Dumazet 	mod##SNMP_UPD_PO_STATS((net)->mib.statname##_statistics, field, (val));\
260edf391ffSNeil Horman })
261edf391ffSNeil Horman 
2621da177e4SLinus Torvalds /* MIBs */
26314878f75SDavid L Stevens 
264087fe240SDenis V. Lunev #define IP6_INC_STATS(net, idev,field)		\
26513415e46SEric Dumazet 		_DEVINC(net, ipv6, , idev, field)
2661d015503SEric Dumazet #define __IP6_INC_STATS(net, idev,field)	\
26713415e46SEric Dumazet 		_DEVINC(net, ipv6, __, idev, field)
268edf391ffSNeil Horman #define IP6_ADD_STATS(net, idev,field,val)	\
26913415e46SEric Dumazet 		_DEVADD(net, ipv6, , idev, field, val)
2701d015503SEric Dumazet #define __IP6_ADD_STATS(net, idev,field,val)	\
27113415e46SEric Dumazet 		_DEVADD(net, ipv6, __, idev, field, val)
272edf391ffSNeil Horman #define IP6_UPD_PO_STATS(net, idev,field,val)   \
27313415e46SEric Dumazet 		_DEVUPD(net, ipv6, , idev, field, val)
274c2005eb0SEric Dumazet #define __IP6_UPD_PO_STATS(net, idev,field,val)   \
27513415e46SEric Dumazet 		_DEVUPD(net, ipv6, __, idev, field, val)
276087fe240SDenis V. Lunev #define ICMP6_INC_STATS(net, idev, field)	\
277be281e55SEric Dumazet 		_DEVINCATOMIC(net, icmpv6, , idev, field)
278a16292a0SEric Dumazet #define __ICMP6_INC_STATS(net, idev, field)	\
27913415e46SEric Dumazet 		_DEVINCATOMIC(net, icmpv6, __, idev, field)
28014878f75SDavid L Stevens 
281087fe240SDenis V. Lunev #define ICMP6MSGOUT_INC_STATS(net, idev, field)		\
2822a24444fSEric Dumazet 	_DEVINC_ATOMIC_ATOMIC(net, icmpv6msg, idev, field +256)
283f3832ed2SEric Dumazet #define ICMP6MSGIN_INC_STATS(net, idev, field)	\
2842a24444fSEric Dumazet 	_DEVINC_ATOMIC_ATOMIC(net, icmpv6msg, idev, field)
28514878f75SDavid L Stevens 
286fd2c3ef7SEric Dumazet struct ip6_ra_chain {
2871da177e4SLinus Torvalds 	struct ip6_ra_chain	*next;
2881da177e4SLinus Torvalds 	struct sock		*sk;
2891da177e4SLinus Torvalds 	int			sel;
2901da177e4SLinus Torvalds 	void			(*destructor)(struct sock *);
2911da177e4SLinus Torvalds };
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds extern struct ip6_ra_chain	*ip6_ra_chain;
2941da177e4SLinus Torvalds extern rwlock_t ip6_ra_lock;
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds /*
2971da177e4SLinus Torvalds    This structure is prepared by protocol, when parsing
2981da177e4SLinus Torvalds    ancillary data and passed to IPv6.
2991da177e4SLinus Torvalds  */
3001da177e4SLinus Torvalds 
301fd2c3ef7SEric Dumazet struct ipv6_txoptions {
3020aeea21aSReshetova, Elena 	refcount_t		refcnt;
3031da177e4SLinus Torvalds 	/* Length of this structure */
3041da177e4SLinus Torvalds 	int			tot_len;
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	/* length of extension headers   */
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	__u16			opt_flen;	/* after fragment hdr */
3091da177e4SLinus Torvalds 	__u16			opt_nflen;	/* before fragment hdr */
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	struct ipv6_opt_hdr	*hopopt;
3121da177e4SLinus Torvalds 	struct ipv6_opt_hdr	*dst0opt;
3131da177e4SLinus Torvalds 	struct ipv6_rt_hdr	*srcrt;	/* Routing Header */
3141da177e4SLinus Torvalds 	struct ipv6_opt_hdr	*dst1opt;
31545f6fad8SEric Dumazet 	struct rcu_head		rcu;
3161da177e4SLinus Torvalds 	/* Option buffer, as read by IPV6_PKTOPTIONS, starts here. */
3171da177e4SLinus Torvalds };
3181da177e4SLinus Torvalds 
319a346abe0SEric Dumazet /* flowlabel_reflect sysctl values */
320a346abe0SEric Dumazet enum flowlabel_reflect {
321a346abe0SEric Dumazet 	FLOWLABEL_REFLECT_ESTABLISHED		= 1,
322a346abe0SEric Dumazet 	FLOWLABEL_REFLECT_TCP_RESET		= 2,
323a346abe0SEric Dumazet 	FLOWLABEL_REFLECT_ICMPV6_ECHO_REPLIES	= 4,
324a346abe0SEric Dumazet };
325a346abe0SEric Dumazet 
326fd2c3ef7SEric Dumazet struct ip6_flowlabel {
3277f0e44acSEric Dumazet 	struct ip6_flowlabel __rcu *next;
32890bcaf7bSAl Viro 	__be32			label;
329db3459d1SEric Dumazet 	atomic_t		users;
3301da177e4SLinus Torvalds 	struct in6_addr		dst;
3311da177e4SLinus Torvalds 	struct ipv6_txoptions	*opt;
3321da177e4SLinus Torvalds 	unsigned long		linger;
333d3aedd5eSYOSHIFUJI Hideaki / 吉藤英明 	struct rcu_head		rcu;
3341da177e4SLinus Torvalds 	u8			share;
3354f82f457SEric W. Biederman 	union {
3364f82f457SEric W. Biederman 		struct pid *pid;
3374f82f457SEric W. Biederman 		kuid_t uid;
3384f82f457SEric W. Biederman 	} owner;
3391da177e4SLinus Torvalds 	unsigned long		lastuse;
3401da177e4SLinus Torvalds 	unsigned long		expires;
34160e8fbc4SBenjamin Thery 	struct net		*fl_net;
3421da177e4SLinus Torvalds };
3431da177e4SLinus Torvalds 
344f3a7c66bSHarvey Harrison #define IPV6_FLOWINFO_MASK		cpu_to_be32(0x0FFFFFFF)
345f3a7c66bSHarvey Harrison #define IPV6_FLOWLABEL_MASK		cpu_to_be32(0x000FFFFF)
34682a584b7STom Herbert #define IPV6_FLOWLABEL_STATELESS_FLAG	cpu_to_be32(0x00080000)
34782a584b7STom Herbert 
34837cfee90SFlorent Fourcot #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)
349d76ed22bSLi RongQing #define IPV6_TCLASS_SHIFT	20
3501da177e4SLinus Torvalds 
351fd2c3ef7SEric Dumazet struct ipv6_fl_socklist {
3527f0e44acSEric Dumazet 	struct ipv6_fl_socklist	__rcu	*next;
3531da177e4SLinus Torvalds 	struct ip6_flowlabel		*fl;
35418367681SYOSHIFUJI Hideaki / 吉藤英明 	struct rcu_head			rcu;
3551da177e4SLinus Torvalds };
3561da177e4SLinus Torvalds 
35726879da5SWei Wang struct ipcm6_cookie {
3585fdaa88dSWillem de Bruijn 	struct sockcm_cookie sockc;
35926879da5SWei Wang 	__s16 hlimit;
36026879da5SWei Wang 	__s16 tclass;
3611b31debcSEric Dumazet 	__u16 gso_size;
36226879da5SWei Wang 	__s8  dontfrag;
36326879da5SWei Wang 	struct ipv6_txoptions *opt;
36426879da5SWei Wang };
36526879da5SWei Wang 
ipcm6_init(struct ipcm6_cookie * ipc6)366b515430aSWillem de Bruijn static inline void ipcm6_init(struct ipcm6_cookie *ipc6)
367b515430aSWillem de Bruijn {
368b515430aSWillem de Bruijn 	*ipc6 = (struct ipcm6_cookie) {
369b515430aSWillem de Bruijn 		.hlimit = -1,
370b515430aSWillem de Bruijn 		.tclass = -1,
371b515430aSWillem de Bruijn 		.dontfrag = -1,
372b515430aSWillem de Bruijn 	};
373b515430aSWillem de Bruijn }
374b515430aSWillem de Bruijn 
ipcm6_init_sk(struct ipcm6_cookie * ipc6,const struct ipv6_pinfo * np)375b515430aSWillem de Bruijn static inline void ipcm6_init_sk(struct ipcm6_cookie *ipc6,
376b515430aSWillem de Bruijn 				 const struct ipv6_pinfo *np)
377b515430aSWillem de Bruijn {
378b515430aSWillem de Bruijn 	*ipc6 = (struct ipcm6_cookie) {
379b515430aSWillem de Bruijn 		.hlimit = -1,
380b515430aSWillem de Bruijn 		.tclass = np->tclass,
381b515430aSWillem de Bruijn 		.dontfrag = np->dontfrag,
382b515430aSWillem de Bruijn 	};
383b515430aSWillem de Bruijn }
384b515430aSWillem de Bruijn 
txopt_get(const struct ipv6_pinfo * np)38545f6fad8SEric Dumazet static inline struct ipv6_txoptions *txopt_get(const struct ipv6_pinfo *np)
38645f6fad8SEric Dumazet {
38745f6fad8SEric Dumazet 	struct ipv6_txoptions *opt;
38845f6fad8SEric Dumazet 
38945f6fad8SEric Dumazet 	rcu_read_lock();
39045f6fad8SEric Dumazet 	opt = rcu_dereference(np->opt);
391e550785cSBenjamin Poirier 	if (opt) {
3920aeea21aSReshetova, Elena 		if (!refcount_inc_not_zero(&opt->refcnt))
39345f6fad8SEric Dumazet 			opt = NULL;
394e550785cSBenjamin Poirier 		else
395e550785cSBenjamin Poirier 			opt = rcu_pointer_handoff(opt);
396e550785cSBenjamin Poirier 	}
39745f6fad8SEric Dumazet 	rcu_read_unlock();
39845f6fad8SEric Dumazet 	return opt;
39945f6fad8SEric Dumazet }
40045f6fad8SEric Dumazet 
txopt_put(struct ipv6_txoptions * opt)40145f6fad8SEric Dumazet static inline void txopt_put(struct ipv6_txoptions *opt)
40245f6fad8SEric Dumazet {
4030aeea21aSReshetova, Elena 	if (opt && refcount_dec_and_test(&opt->refcnt))
40445f6fad8SEric Dumazet 		kfree_rcu(opt, rcu);
40545f6fad8SEric Dumazet }
40645f6fad8SEric Dumazet 
4070b0dff5bSWillem de Bruijn #if IS_ENABLED(CONFIG_IPV6)
40859c820b2SWillem de Bruijn struct ip6_flowlabel *__fl6_sock_lookup(struct sock *sk, __be32 label);
40959c820b2SWillem de Bruijn 
41059c820b2SWillem de Bruijn extern struct static_key_false_deferred ipv6_flowlabel_exclusive;
fl6_sock_lookup(struct sock * sk,__be32 label)41159c820b2SWillem de Bruijn static inline struct ip6_flowlabel *fl6_sock_lookup(struct sock *sk,
41259c820b2SWillem de Bruijn 						    __be32 label)
41359c820b2SWillem de Bruijn {
4140b0dff5bSWillem de Bruijn 	if (static_branch_unlikely(&ipv6_flowlabel_exclusive.key) &&
4150b0dff5bSWillem de Bruijn 	    READ_ONCE(sock_net(sk)->ipv6.flowlabel_has_excl))
41659c820b2SWillem de Bruijn 		return __fl6_sock_lookup(sk, label) ? : ERR_PTR(-ENOENT);
41759c820b2SWillem de Bruijn 
41859c820b2SWillem de Bruijn 	return NULL;
41959c820b2SWillem de Bruijn }
4200b0dff5bSWillem de Bruijn #endif
42159c820b2SWillem de Bruijn 
4225c3a0fd7SJoe Perches struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions *opt_space,
4231da177e4SLinus Torvalds 					 struct ip6_flowlabel *fl,
4241da177e4SLinus Torvalds 					 struct ipv6_txoptions *fopt);
4255c3a0fd7SJoe Perches void fl6_free_socklist(struct sock *sk);
42686298285SChristoph Hellwig int ipv6_flowlabel_opt(struct sock *sk, sockptr_t optval, int optlen);
42746e5f401SFlorent Fourcot int ipv6_flowlabel_opt_get(struct sock *sk, struct in6_flowlabel_req *freq,
42846e5f401SFlorent Fourcot 			   int flags);
4295c3a0fd7SJoe Perches int ip6_flowlabel_init(void);
4305c3a0fd7SJoe Perches void ip6_flowlabel_cleanup(void);
431e9191ffbSBen Hutchings bool ip6_autoflowlabel(struct net *net, const struct ipv6_pinfo *np);
4321da177e4SLinus Torvalds 
fl6_sock_release(struct ip6_flowlabel * fl)4331da177e4SLinus Torvalds static inline void fl6_sock_release(struct ip6_flowlabel *fl)
4341da177e4SLinus Torvalds {
4351da177e4SLinus Torvalds 	if (fl)
4361da177e4SLinus Torvalds 		atomic_dec(&fl->users);
4371da177e4SLinus Torvalds }
4381da177e4SLinus Torvalds 
43930c89badSEric Dumazet enum skb_drop_reason icmpv6_notify(struct sk_buff *skb, u8 type,
44030c89badSEric Dumazet 				   u8 code, __be32 info);
441b94f1c09SDavid S. Miller 
4424e64b1edSJoe Perches void icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
4436d0bfe22SLorenzo Colitti 				struct icmp6hdr *thdr, int len);
4446d0bfe22SLorenzo Colitti 
4455c3a0fd7SJoe Perches int ip6_ra_control(struct sock *sk, int sel);
4461da177e4SLinus Torvalds 
4475c3a0fd7SJoe Perches int ipv6_parse_hopopts(struct sk_buff *skb);
4481da177e4SLinus Torvalds 
4495c3a0fd7SJoe Perches struct ipv6_txoptions *ipv6_dup_options(struct sock *sk,
4505c3a0fd7SJoe Perches 					struct ipv6_txoptions *opt);
4515c3a0fd7SJoe Perches struct ipv6_txoptions *ipv6_renew_options(struct sock *sk,
4525c3a0fd7SJoe Perches 					  struct ipv6_txoptions *opt,
453333fad53SYOSHIFUJI Hideaki 					  int newtype,
454a9ba23d4SPaul Moore 					  struct ipv6_opt_hdr *newopt);
45531ed2261SPavel Begunkov struct ipv6_txoptions *__ipv6_fixup_options(struct ipv6_txoptions *opt_space,
456df9890c3SYOSHIFUJI Hideaki 					    struct ipv6_txoptions *opt);
4571da177e4SLinus Torvalds 
45831ed2261SPavel Begunkov static inline struct ipv6_txoptions *
ipv6_fixup_options(struct ipv6_txoptions * opt_space,struct ipv6_txoptions * opt)45931ed2261SPavel Begunkov ipv6_fixup_options(struct ipv6_txoptions *opt_space, struct ipv6_txoptions *opt)
46031ed2261SPavel Begunkov {
46131ed2261SPavel Begunkov 	if (!opt)
46231ed2261SPavel Begunkov 		return NULL;
46331ed2261SPavel Begunkov 	return __ipv6_fixup_options(opt_space, opt);
46431ed2261SPavel Begunkov }
46531ed2261SPavel Begunkov 
466a224772dSEric Dumazet bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb,
467a224772dSEric Dumazet 		       const struct inet6_skb_parm *opt);
468ceba1832SHuw Davies struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
469ceba1832SHuw Davies 					   struct ipv6_txoptions *opt);
470399c07deSArnaldo Carvalho de Melo 
47109f3d1a3SEric Dumazet /* This helper is specialized for BIG TCP needs.
47209f3d1a3SEric Dumazet  * It assumes the hop_jumbo_hdr will immediately follow the IPV6 header.
47309f3d1a3SEric Dumazet  * It assumes headers are already in skb->head.
47409f3d1a3SEric Dumazet  * Returns 0, or IPPROTO_TCP if a BIG TCP packet is there.
47509f3d1a3SEric Dumazet  */
ipv6_has_hopopt_jumbo(const struct sk_buff * skb)47609f3d1a3SEric Dumazet static inline int ipv6_has_hopopt_jumbo(const struct sk_buff *skb)
47709f3d1a3SEric Dumazet {
47809f3d1a3SEric Dumazet 	const struct hop_jumbo_hdr *jhdr;
47909f3d1a3SEric Dumazet 	const struct ipv6hdr *nhdr;
48009f3d1a3SEric Dumazet 
4810fe79f28SAlexander Duyck 	if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
48209f3d1a3SEric Dumazet 		return 0;
48309f3d1a3SEric Dumazet 
48409f3d1a3SEric Dumazet 	if (skb->protocol != htons(ETH_P_IPV6))
48509f3d1a3SEric Dumazet 		return 0;
48609f3d1a3SEric Dumazet 
48709f3d1a3SEric Dumazet 	if (skb_network_offset(skb) +
48809f3d1a3SEric Dumazet 	    sizeof(struct ipv6hdr) +
48909f3d1a3SEric Dumazet 	    sizeof(struct hop_jumbo_hdr) > skb_headlen(skb))
49009f3d1a3SEric Dumazet 		return 0;
49109f3d1a3SEric Dumazet 
49209f3d1a3SEric Dumazet 	nhdr = ipv6_hdr(skb);
49309f3d1a3SEric Dumazet 
49409f3d1a3SEric Dumazet 	if (nhdr->nexthdr != NEXTHDR_HOP)
49509f3d1a3SEric Dumazet 		return 0;
49609f3d1a3SEric Dumazet 
49709f3d1a3SEric Dumazet 	jhdr = (const struct hop_jumbo_hdr *) (nhdr + 1);
49809f3d1a3SEric Dumazet 	if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
49909f3d1a3SEric Dumazet 	    jhdr->nexthdr != IPPROTO_TCP)
50009f3d1a3SEric Dumazet 		return 0;
50109f3d1a3SEric Dumazet 	return jhdr->nexthdr;
50209f3d1a3SEric Dumazet }
50309f3d1a3SEric Dumazet 
50489300468SCoco Li /* Return 0 if HBH header is successfully removed
50589300468SCoco Li  * Or if HBH removal is unnecessary (packet is not big TCP)
50689300468SCoco Li  * Return error to indicate dropping the packet
50789300468SCoco Li  */
ipv6_hopopt_jumbo_remove(struct sk_buff * skb)50889300468SCoco Li static inline int ipv6_hopopt_jumbo_remove(struct sk_buff *skb)
50989300468SCoco Li {
51089300468SCoco Li 	const int hophdr_len = sizeof(struct hop_jumbo_hdr);
51189300468SCoco Li 	int nexthdr = ipv6_has_hopopt_jumbo(skb);
51289300468SCoco Li 	struct ipv6hdr *h6;
51389300468SCoco Li 
51489300468SCoco Li 	if (!nexthdr)
51589300468SCoco Li 		return 0;
51689300468SCoco Li 
51789300468SCoco Li 	if (skb_cow_head(skb, 0))
51889300468SCoco Li 		return -1;
51989300468SCoco Li 
52089300468SCoco Li 	/* Remove the HBH header.
52189300468SCoco Li 	 * Layout: [Ethernet header][IPv6 header][HBH][L4 Header]
52289300468SCoco Li 	 */
52389300468SCoco Li 	memmove(skb_mac_header(skb) + hophdr_len, skb_mac_header(skb),
52489300468SCoco Li 		skb_network_header(skb) - skb_mac_header(skb) +
52589300468SCoco Li 		sizeof(struct ipv6hdr));
52689300468SCoco Li 
52789300468SCoco Li 	__skb_pull(skb, hophdr_len);
52889300468SCoco Li 	skb->network_header += hophdr_len;
52989300468SCoco Li 	skb->mac_header += hophdr_len;
53089300468SCoco Li 
53189300468SCoco Li 	h6 = ipv6_hdr(skb);
53289300468SCoco Li 	h6->nexthdr = nexthdr;
53389300468SCoco Li 
53489300468SCoco Li 	return 0;
53589300468SCoco Li }
53689300468SCoco Li 
ipv6_accept_ra(struct inet6_dev * idev)537aeaf6e9dSShmulik Ladkani static inline bool ipv6_accept_ra(struct inet6_dev *idev)
538aeaf6e9dSShmulik Ladkani {
539aeaf6e9dSShmulik Ladkani 	/* If forwarding is enabled, RA are not accepted unless the special
540aeaf6e9dSShmulik Ladkani 	 * hybrid mode (accept_ra=2) is enabled.
541aeaf6e9dSShmulik Ladkani 	 */
542aeaf6e9dSShmulik Ladkani 	return idev->cnf.forwarding ? idev->cnf.accept_ra == 2 :
543aeaf6e9dSShmulik Ladkani 	    idev->cnf.accept_ra;
544aeaf6e9dSShmulik Ladkani }
545aeaf6e9dSShmulik Ladkani 
546c2a93660SJesper Dangaard Brouer #define IPV6_FRAG_HIGH_THRESH	(4 * 1024*1024)	/* 4194304 */
547c2a93660SJesper Dangaard Brouer #define IPV6_FRAG_LOW_THRESH	(3 * 1024*1024)	/* 3145728 */
5481da177e4SLinus Torvalds #define IPV6_FRAG_TIMEOUT	(60 * HZ)	/* 60 seconds */
5491da177e4SLinus Torvalds 
5505c3a0fd7SJoe Perches int __ipv6_addr_type(const struct in6_addr *addr);
ipv6_addr_type(const struct in6_addr * addr)551b1cacb68SYOSHIFUJI Hideaki static inline int ipv6_addr_type(const struct in6_addr *addr)
552b1cacb68SYOSHIFUJI Hideaki {
553b1cacb68SYOSHIFUJI Hideaki 	return __ipv6_addr_type(addr) & 0xffff;
554b1cacb68SYOSHIFUJI Hideaki }
5551da177e4SLinus Torvalds 
ipv6_addr_scope(const struct in6_addr * addr)5561da177e4SLinus Torvalds static inline int ipv6_addr_scope(const struct in6_addr *addr)
5571da177e4SLinus Torvalds {
558b1cacb68SYOSHIFUJI Hideaki 	return __ipv6_addr_type(addr) & IPV6_ADDR_SCOPE_MASK;
559b1cacb68SYOSHIFUJI Hideaki }
560b1cacb68SYOSHIFUJI Hideaki 
__ipv6_addr_src_scope(int type)561b1cacb68SYOSHIFUJI Hideaki static inline int __ipv6_addr_src_scope(int type)
562b1cacb68SYOSHIFUJI Hideaki {
563a02cec21SEric Dumazet 	return (type == IPV6_ADDR_ANY) ? __IPV6_ADDR_SCOPE_INVALID : (type >> 16);
564b1cacb68SYOSHIFUJI Hideaki }
565b1cacb68SYOSHIFUJI Hideaki 
ipv6_addr_src_scope(const struct in6_addr * addr)566b1cacb68SYOSHIFUJI Hideaki static inline int ipv6_addr_src_scope(const struct in6_addr *addr)
567b1cacb68SYOSHIFUJI Hideaki {
568b1cacb68SYOSHIFUJI Hideaki 	return __ipv6_addr_src_scope(__ipv6_addr_type(addr));
5691da177e4SLinus Torvalds }
5701da177e4SLinus Torvalds 
__ipv6_addr_needs_scope_id(int type)571b7ef213eSHannes Frederic Sowa static inline bool __ipv6_addr_needs_scope_id(int type)
572b7ef213eSHannes Frederic Sowa {
573b7ef213eSHannes Frederic Sowa 	return type & IPV6_ADDR_LINKLOCAL ||
574b7ef213eSHannes Frederic Sowa 	       (type & IPV6_ADDR_MULTICAST &&
575b7ef213eSHannes Frederic Sowa 		(type & (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)));
576b7ef213eSHannes Frederic Sowa }
577b7ef213eSHannes Frederic Sowa 
ipv6_iface_scope_id(const struct in6_addr * addr,int iface)578b7ef213eSHannes Frederic Sowa static inline __u32 ipv6_iface_scope_id(const struct in6_addr *addr, int iface)
579b7ef213eSHannes Frederic Sowa {
580b7ef213eSHannes Frederic Sowa 	return __ipv6_addr_needs_scope_id(__ipv6_addr_type(addr)) ? iface : 0;
581b7ef213eSHannes Frederic Sowa }
582b7ef213eSHannes Frederic Sowa 
ipv6_addr_cmp(const struct in6_addr * a1,const struct in6_addr * a2)5831da177e4SLinus Torvalds static inline int ipv6_addr_cmp(const struct in6_addr *a1, const struct in6_addr *a2)
5841da177e4SLinus Torvalds {
585db3459d1SEric Dumazet 	return memcmp(a1, a2, sizeof(struct in6_addr));
5861da177e4SLinus Torvalds }
5871da177e4SLinus Torvalds 
5881a203cb3SEric Dumazet static inline bool
ipv6_masked_addr_cmp(const struct in6_addr * a1,const struct in6_addr * m,const struct in6_addr * a2)589f2ffd9eeSPatrick McHardy ipv6_masked_addr_cmp(const struct in6_addr *a1, const struct in6_addr *m,
590f2ffd9eeSPatrick McHardy 		     const struct in6_addr *a2)
591f2ffd9eeSPatrick McHardy {
5921a203cb3SEric Dumazet #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
5931a203cb3SEric Dumazet 	const unsigned long *ul1 = (const unsigned long *)a1;
5941a203cb3SEric Dumazet 	const unsigned long *ulm = (const unsigned long *)m;
5951a203cb3SEric Dumazet 	const unsigned long *ul2 = (const unsigned long *)a2;
5961a203cb3SEric Dumazet 
5971a203cb3SEric Dumazet 	return !!(((ul1[0] ^ ul2[0]) & ulm[0]) |
5981a203cb3SEric Dumazet 		  ((ul1[1] ^ ul2[1]) & ulm[1]));
5991a203cb3SEric Dumazet #else
600a02cec21SEric Dumazet 	return !!(((a1->s6_addr32[0] ^ a2->s6_addr32[0]) & m->s6_addr32[0]) |
601fed85383SYOSHIFUJI Hideaki 		  ((a1->s6_addr32[1] ^ a2->s6_addr32[1]) & m->s6_addr32[1]) |
602fed85383SYOSHIFUJI Hideaki 		  ((a1->s6_addr32[2] ^ a2->s6_addr32[2]) & m->s6_addr32[2]) |
603a02cec21SEric Dumazet 		  ((a1->s6_addr32[3] ^ a2->s6_addr32[3]) & m->s6_addr32[3]));
6041a203cb3SEric Dumazet #endif
605f2ffd9eeSPatrick McHardy }
606f2ffd9eeSPatrick McHardy 
ipv6_addr_prefix(struct in6_addr * pfx,const struct in6_addr * addr,int plen)6071da177e4SLinus Torvalds static inline void ipv6_addr_prefix(struct in6_addr *pfx,
6081da177e4SLinus Torvalds 				    const struct in6_addr *addr,
6091da177e4SLinus Torvalds 				    int plen)
6101da177e4SLinus Torvalds {
6111da177e4SLinus Torvalds 	/* caller must guarantee 0 <= plen <= 128 */
6121da177e4SLinus Torvalds 	int o = plen >> 3,
6131da177e4SLinus Torvalds 	    b = plen & 0x7;
6141da177e4SLinus Torvalds 
615db3459d1SEric Dumazet 	memset(pfx->s6_addr, 0, sizeof(pfx->s6_addr));
6161da177e4SLinus Torvalds 	memcpy(pfx->s6_addr, addr, o);
617db3459d1SEric Dumazet 	if (b != 0)
6181da177e4SLinus Torvalds 		pfx->s6_addr[o] = addr->s6_addr[o] & (0xff00 >> b);
6191da177e4SLinus Torvalds }
6201da177e4SLinus Torvalds 
ipv6_addr_prefix_copy(struct in6_addr * addr,const struct in6_addr * pfx,int plen)621818f1f3eSAlexander Aring static inline void ipv6_addr_prefix_copy(struct in6_addr *addr,
622818f1f3eSAlexander Aring 					 const struct in6_addr *pfx,
623818f1f3eSAlexander Aring 					 int plen)
624818f1f3eSAlexander Aring {
625818f1f3eSAlexander Aring 	/* caller must guarantee 0 <= plen <= 128 */
626818f1f3eSAlexander Aring 	int o = plen >> 3,
627818f1f3eSAlexander Aring 	    b = plen & 0x7;
628818f1f3eSAlexander Aring 
629818f1f3eSAlexander Aring 	memcpy(addr->s6_addr, pfx, o);
630818f1f3eSAlexander Aring 	if (b != 0) {
631818f1f3eSAlexander Aring 		addr->s6_addr[o] &= ~(0xff00 >> b);
632818f1f3eSAlexander Aring 		addr->s6_addr[o] |= (pfx->s6_addr[o] & (0xff00 >> b));
633818f1f3eSAlexander Aring 	}
634818f1f3eSAlexander Aring }
635818f1f3eSAlexander Aring 
__ipv6_addr_set_half(__be32 * addr,__be32 wh,__be32 wl)6365206c579SYOSHIFUJI Hideaki / 吉藤英明 static inline void __ipv6_addr_set_half(__be32 *addr,
6375206c579SYOSHIFUJI Hideaki / 吉藤英明 					__be32 wh, __be32 wl)
6385206c579SYOSHIFUJI Hideaki / 吉藤英明 {
6395206c579SYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
6405206c579SYOSHIFUJI Hideaki / 吉藤英明 #if defined(__BIG_ENDIAN)
6415206c579SYOSHIFUJI Hideaki / 吉藤英明 	if (__builtin_constant_p(wh) && __builtin_constant_p(wl)) {
6425206c579SYOSHIFUJI Hideaki / 吉藤英明 		*(__force u64 *)addr = ((__force u64)(wh) << 32 | (__force u64)(wl));
6435206c579SYOSHIFUJI Hideaki / 吉藤英明 		return;
6445206c579SYOSHIFUJI Hideaki / 吉藤英明 	}
6455206c579SYOSHIFUJI Hideaki / 吉藤英明 #elif defined(__LITTLE_ENDIAN)
6465206c579SYOSHIFUJI Hideaki / 吉藤英明 	if (__builtin_constant_p(wl) && __builtin_constant_p(wh)) {
6475206c579SYOSHIFUJI Hideaki / 吉藤英明 		*(__force u64 *)addr = ((__force u64)(wl) << 32 | (__force u64)(wh));
6485206c579SYOSHIFUJI Hideaki / 吉藤英明 		return;
6495206c579SYOSHIFUJI Hideaki / 吉藤英明 	}
6505206c579SYOSHIFUJI Hideaki / 吉藤英明 #endif
6515206c579SYOSHIFUJI Hideaki / 吉藤英明 #endif
6525206c579SYOSHIFUJI Hideaki / 吉藤英明 	addr[0] = wh;
6535206c579SYOSHIFUJI Hideaki / 吉藤英明 	addr[1] = wl;
6545206c579SYOSHIFUJI Hideaki / 吉藤英明 }
6555206c579SYOSHIFUJI Hideaki / 吉藤英明 
ipv6_addr_set(struct in6_addr * addr,__be32 w1,__be32 w2,__be32 w3,__be32 w4)6561da177e4SLinus Torvalds static inline void ipv6_addr_set(struct in6_addr *addr,
65748818f82SAl Viro 				     __be32 w1, __be32 w2,
65848818f82SAl Viro 				     __be32 w3, __be32 w4)
6591da177e4SLinus Torvalds {
6605206c579SYOSHIFUJI Hideaki / 吉藤英明 	__ipv6_addr_set_half(&addr->s6_addr32[0], w1, w2);
6615206c579SYOSHIFUJI Hideaki / 吉藤英明 	__ipv6_addr_set_half(&addr->s6_addr32[2], w3, w4);
6621da177e4SLinus Torvalds }
6631da177e4SLinus Torvalds 
ipv6_addr_equal(const struct in6_addr * a1,const struct in6_addr * a2)66492113bfdSEric Dumazet static inline bool ipv6_addr_equal(const struct in6_addr *a1,
6651da177e4SLinus Torvalds 				   const struct in6_addr *a2)
6661da177e4SLinus Torvalds {
6671a203cb3SEric Dumazet #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
6681a203cb3SEric Dumazet 	const unsigned long *ul1 = (const unsigned long *)a1;
6691a203cb3SEric Dumazet 	const unsigned long *ul2 = (const unsigned long *)a2;
6701a203cb3SEric Dumazet 
6711a203cb3SEric Dumazet 	return ((ul1[0] ^ ul2[0]) | (ul1[1] ^ ul2[1])) == 0UL;
6721a203cb3SEric Dumazet #else
673a02cec21SEric Dumazet 	return ((a1->s6_addr32[0] ^ a2->s6_addr32[0]) |
674fed85383SYOSHIFUJI Hideaki 		(a1->s6_addr32[1] ^ a2->s6_addr32[1]) |
675fed85383SYOSHIFUJI Hideaki 		(a1->s6_addr32[2] ^ a2->s6_addr32[2]) |
676a02cec21SEric Dumazet 		(a1->s6_addr32[3] ^ a2->s6_addr32[3])) == 0;
6771a203cb3SEric Dumazet #endif
6781da177e4SLinus Torvalds }
6791da177e4SLinus Torvalds 
68038675170SYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
__ipv6_prefix_equal64_half(const __be64 * a1,const __be64 * a2,unsigned int len)68138675170SYOSHIFUJI Hideaki / 吉藤英明 static inline bool __ipv6_prefix_equal64_half(const __be64 *a1,
68238675170SYOSHIFUJI Hideaki / 吉藤英明 					      const __be64 *a2,
68338675170SYOSHIFUJI Hideaki / 吉藤英明 					      unsigned int len)
68438675170SYOSHIFUJI Hideaki / 吉藤英明 {
685512613d7SFabio Baltieri 	if (len && ((*a1 ^ *a2) & cpu_to_be64((~0UL) << (64 - len))))
68638675170SYOSHIFUJI Hideaki / 吉藤英明 		return false;
68738675170SYOSHIFUJI Hideaki / 吉藤英明 	return true;
68838675170SYOSHIFUJI Hideaki / 吉藤英明 }
68938675170SYOSHIFUJI Hideaki / 吉藤英明 
ipv6_prefix_equal(const struct in6_addr * addr1,const struct in6_addr * addr2,unsigned int prefixlen)69038675170SYOSHIFUJI Hideaki / 吉藤英明 static inline bool ipv6_prefix_equal(const struct in6_addr *addr1,
69138675170SYOSHIFUJI Hideaki / 吉藤英明 				     const struct in6_addr *addr2,
69238675170SYOSHIFUJI Hideaki / 吉藤英明 				     unsigned int prefixlen)
69338675170SYOSHIFUJI Hideaki / 吉藤英明 {
69438675170SYOSHIFUJI Hideaki / 吉藤英明 	const __be64 *a1 = (const __be64 *)addr1;
69538675170SYOSHIFUJI Hideaki / 吉藤英明 	const __be64 *a2 = (const __be64 *)addr2;
69638675170SYOSHIFUJI Hideaki / 吉藤英明 
69738675170SYOSHIFUJI Hideaki / 吉藤英明 	if (prefixlen >= 64) {
69838675170SYOSHIFUJI Hideaki / 吉藤英明 		if (a1[0] ^ a2[0])
69938675170SYOSHIFUJI Hideaki / 吉藤英明 			return false;
70038675170SYOSHIFUJI Hideaki / 吉藤英明 		return __ipv6_prefix_equal64_half(a1 + 1, a2 + 1, prefixlen - 64);
70138675170SYOSHIFUJI Hideaki / 吉藤英明 	}
70238675170SYOSHIFUJI Hideaki / 吉藤英明 	return __ipv6_prefix_equal64_half(a1, a2, prefixlen);
70338675170SYOSHIFUJI Hideaki / 吉藤英明 }
70438675170SYOSHIFUJI Hideaki / 吉藤英明 #else
ipv6_prefix_equal(const struct in6_addr * addr1,const struct in6_addr * addr2,unsigned int prefixlen)7052ef97332SYOSHIFUJI Hideaki / 吉藤英明 static inline bool ipv6_prefix_equal(const struct in6_addr *addr1,
7062ef97332SYOSHIFUJI Hideaki / 吉藤英明 				     const struct in6_addr *addr2,
7071da177e4SLinus Torvalds 				     unsigned int prefixlen)
7081da177e4SLinus Torvalds {
7092ef97332SYOSHIFUJI Hideaki / 吉藤英明 	const __be32 *a1 = addr1->s6_addr32;
7102ef97332SYOSHIFUJI Hideaki / 吉藤英明 	const __be32 *a2 = addr2->s6_addr32;
71195c96174SEric Dumazet 	unsigned int pdw, pbi;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	/* check complete u32 in prefix */
7141da177e4SLinus Torvalds 	pdw = prefixlen >> 5;
7151da177e4SLinus Torvalds 	if (pdw && memcmp(a1, a2, pdw << 2))
71692113bfdSEric Dumazet 		return false;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	/* check incomplete u32 in prefix */
7191da177e4SLinus Torvalds 	pbi = prefixlen & 0x1f;
7201da177e4SLinus Torvalds 	if (pbi && ((a1[pdw] ^ a2[pdw]) & htonl((0xffffffff) << (32 - pbi))))
72192113bfdSEric Dumazet 		return false;
7221da177e4SLinus Torvalds 
72392113bfdSEric Dumazet 	return true;
7241da177e4SLinus Torvalds }
72538675170SYOSHIFUJI Hideaki / 吉藤英明 #endif
7261da177e4SLinus Torvalds 
ipv6_addr_any(const struct in6_addr * a)72792113bfdSEric Dumazet static inline bool ipv6_addr_any(const struct in6_addr *a)
7281da177e4SLinus Torvalds {
7291a203cb3SEric Dumazet #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
7301a203cb3SEric Dumazet 	const unsigned long *ul = (const unsigned long *)a;
7311a203cb3SEric Dumazet 
7321a203cb3SEric Dumazet 	return (ul[0] | ul[1]) == 0UL;
7331a203cb3SEric Dumazet #else
734a02cec21SEric Dumazet 	return (a->s6_addr32[0] | a->s6_addr32[1] |
735a02cec21SEric Dumazet 		a->s6_addr32[2] | a->s6_addr32[3]) == 0;
7361a203cb3SEric Dumazet #endif
7371da177e4SLinus Torvalds }
7381da177e4SLinus Torvalds 
ipv6_addr_hash(const struct in6_addr * a)739ddbe5032SEric Dumazet static inline u32 ipv6_addr_hash(const struct in6_addr *a)
740ddbe5032SEric Dumazet {
741ddbe5032SEric Dumazet #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
742ddbe5032SEric Dumazet 	const unsigned long *ul = (const unsigned long *)a;
743ddbe5032SEric Dumazet 	unsigned long x = ul[0] ^ ul[1];
744ddbe5032SEric Dumazet 
745ddbe5032SEric Dumazet 	return (u32)(x ^ (x >> 32));
746ddbe5032SEric Dumazet #else
747ddbe5032SEric Dumazet 	return (__force u32)(a->s6_addr32[0] ^ a->s6_addr32[1] ^
748ddbe5032SEric Dumazet 			     a->s6_addr32[2] ^ a->s6_addr32[3]);
749ddbe5032SEric Dumazet #endif
750ddbe5032SEric Dumazet }
751ddbe5032SEric Dumazet 
75208dcdbf6SEric Dumazet /* more secured version of ipv6_addr_hash() */
__ipv6_addr_jhash(const struct in6_addr * a,const u32 initval)753b50026b5SHannes Frederic Sowa static inline u32 __ipv6_addr_jhash(const struct in6_addr *a, const u32 initval)
75408dcdbf6SEric Dumazet {
755d11b0df7SStewart Smith 	return jhash2((__force const u32 *)a->s6_addr32,
756d11b0df7SStewart Smith 		      ARRAY_SIZE(a->s6_addr32), initval);
75708dcdbf6SEric Dumazet }
75808dcdbf6SEric Dumazet 
ipv6_addr_loopback(const struct in6_addr * a)75992113bfdSEric Dumazet static inline bool ipv6_addr_loopback(const struct in6_addr *a)
760f630e43aSYOSHIFUJI Hideaki {
761e287656bSYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
7621373a773SJeff Layton 	const __be64 *be = (const __be64 *)a;
763e287656bSYOSHIFUJI Hideaki / 吉藤英明 
7641373a773SJeff Layton 	return (be[0] | (be[1] ^ cpu_to_be64(1))) == 0UL;
765e287656bSYOSHIFUJI Hideaki / 吉藤英明 #else
766a02cec21SEric Dumazet 	return (a->s6_addr32[0] | a->s6_addr32[1] |
7671373a773SJeff Layton 		a->s6_addr32[2] | (a->s6_addr32[3] ^ cpu_to_be32(1))) == 0;
768e287656bSYOSHIFUJI Hideaki / 吉藤英明 #endif
769f630e43aSYOSHIFUJI Hideaki }
770f630e43aSYOSHIFUJI Hideaki 
7711373a773SJeff Layton /*
7721373a773SJeff Layton  * Note that we must __force cast these to unsigned long to make sparse happy,
7731373a773SJeff Layton  * since all of the endian-annotated types are fixed size regardless of arch.
7741373a773SJeff Layton  */
ipv6_addr_v4mapped(const struct in6_addr * a)77592113bfdSEric Dumazet static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
776e773e4faSBrian Haley {
777a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 	return (
778a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
7791373a773SJeff Layton 		*(unsigned long *)a |
780a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 #else
7811373a773SJeff Layton 		(__force unsigned long)(a->s6_addr32[0] | a->s6_addr32[1]) |
782a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 #endif
7831373a773SJeff Layton 		(__force unsigned long)(a->s6_addr32[2] ^
7841373a773SJeff Layton 					cpu_to_be32(0x0000ffff))) == 0UL;
785e773e4faSBrian Haley }
786e773e4faSBrian Haley 
ipv6_addr_v4mapped_any(const struct in6_addr * a)787*aa99e5f8SKuniyuki Iwashima static inline bool ipv6_addr_v4mapped_any(const struct in6_addr *a)
788*aa99e5f8SKuniyuki Iwashima {
789*aa99e5f8SKuniyuki Iwashima 	return ipv6_addr_v4mapped(a) && ipv4_is_zeronet(a->s6_addr32[3]);
790*aa99e5f8SKuniyuki Iwashima }
791*aa99e5f8SKuniyuki Iwashima 
ipv6_addr_v4mapped_loopback(const struct in6_addr * a)792be2644aaSEric Dumazet static inline bool ipv6_addr_v4mapped_loopback(const struct in6_addr *a)
793be2644aaSEric Dumazet {
794be2644aaSEric Dumazet 	return ipv6_addr_v4mapped(a) && ipv4_is_loopback(a->s6_addr32[3]);
795be2644aaSEric Dumazet }
796be2644aaSEric Dumazet 
ipv6_portaddr_hash(const struct net * net,const struct in6_addr * addr6,unsigned int port)797f0b1e64cSMartin KaFai Lau static inline u32 ipv6_portaddr_hash(const struct net *net,
798f0b1e64cSMartin KaFai Lau 				     const struct in6_addr *addr6,
799f0b1e64cSMartin KaFai Lau 				     unsigned int port)
800f0b1e64cSMartin KaFai Lau {
801f0b1e64cSMartin KaFai Lau 	unsigned int hash, mix = net_hash_mix(net);
802f0b1e64cSMartin KaFai Lau 
803f0b1e64cSMartin KaFai Lau 	if (ipv6_addr_any(addr6))
804f0b1e64cSMartin KaFai Lau 		hash = jhash_1word(0, mix);
805f0b1e64cSMartin KaFai Lau 	else if (ipv6_addr_v4mapped(addr6))
806f0b1e64cSMartin KaFai Lau 		hash = jhash_1word((__force u32)addr6->s6_addr32[3], mix);
807f0b1e64cSMartin KaFai Lau 	else
808f0b1e64cSMartin KaFai Lau 		hash = jhash2((__force u32 *)addr6->s6_addr32, 4, mix);
809f0b1e64cSMartin KaFai Lau 
810f0b1e64cSMartin KaFai Lau 	return hash ^ port;
811f0b1e64cSMartin KaFai Lau }
812f0b1e64cSMartin KaFai Lau 
8131da177e4SLinus Torvalds /*
81499cd07a5SJuha-Matti Tapio  * Check for a RFC 4843 ORCHID address
81599cd07a5SJuha-Matti Tapio  * (Overlay Routable Cryptographic Hash Identifiers)
81699cd07a5SJuha-Matti Tapio  */
ipv6_addr_orchid(const struct in6_addr * a)81792113bfdSEric Dumazet static inline bool ipv6_addr_orchid(const struct in6_addr *a)
81899cd07a5SJuha-Matti Tapio {
819a02cec21SEric Dumazet 	return (a->s6_addr32[0] & htonl(0xfffffff0)) == htonl(0x20010010);
82099cd07a5SJuha-Matti Tapio }
82199cd07a5SJuha-Matti Tapio 
ipv6_addr_is_multicast(const struct in6_addr * addr)8225c98631cSLorenzo Colitti static inline bool ipv6_addr_is_multicast(const struct in6_addr *addr)
8235c98631cSLorenzo Colitti {
8245c98631cSLorenzo Colitti 	return (addr->s6_addr32[0] & htonl(0xFF000000)) == htonl(0xFF000000);
8255c98631cSLorenzo Colitti }
8265c98631cSLorenzo Colitti 
ipv6_addr_set_v4mapped(const __be32 addr,struct in6_addr * v4mapped)827f15364bdSAurélien Charbon static inline void ipv6_addr_set_v4mapped(const __be32 addr,
828f15364bdSAurélien Charbon 					  struct in6_addr *v4mapped)
829f15364bdSAurélien Charbon {
830f15364bdSAurélien Charbon 	ipv6_addr_set(v4mapped,
831f15364bdSAurélien Charbon 			0, 0,
832f15364bdSAurélien Charbon 			htonl(0x0000FFFF),
833f15364bdSAurélien Charbon 			addr);
834f15364bdSAurélien Charbon }
835f15364bdSAurélien Charbon 
83699cd07a5SJuha-Matti Tapio /*
837971f359dSYOSHIFUJI Hideaki  * find the first different bit between two addresses
838971f359dSYOSHIFUJI Hideaki  * length of address must be a multiple of 32bits
839971f359dSYOSHIFUJI Hideaki  */
__ipv6_addr_diff32(const void * token1,const void * token2,int addrlen)8409f2e7334SYOSHIFUJI Hideaki / 吉藤英明 static inline int __ipv6_addr_diff32(const void *token1, const void *token2, int addrlen)
841971f359dSYOSHIFUJI Hideaki {
842ef296f56SAl Viro 	const __be32 *a1 = token1, *a2 = token2;
843971f359dSYOSHIFUJI Hideaki 	int i;
844971f359dSYOSHIFUJI Hideaki 
845971f359dSYOSHIFUJI Hideaki 	addrlen >>= 2;
846971f359dSYOSHIFUJI Hideaki 
847971f359dSYOSHIFUJI Hideaki 	for (i = 0; i < addrlen; i++) {
848ef296f56SAl Viro 		__be32 xb = a1[i] ^ a2[i];
849ef296f56SAl Viro 		if (xb)
850d57b8fb8SYOSHIFUJI Hideaki / 吉藤英明 			return i * 32 + 31 - __fls(ntohl(xb));
851971f359dSYOSHIFUJI Hideaki 	}
852971f359dSYOSHIFUJI Hideaki 
853971f359dSYOSHIFUJI Hideaki 	/*
854971f359dSYOSHIFUJI Hideaki 	 *	we should *never* get to this point since that
855971f359dSYOSHIFUJI Hideaki 	 *	would mean the addrs are equal
856971f359dSYOSHIFUJI Hideaki 	 *
857971f359dSYOSHIFUJI Hideaki 	 *	However, we do get to it 8) And exacly, when
858971f359dSYOSHIFUJI Hideaki 	 *	addresses are equal 8)
859971f359dSYOSHIFUJI Hideaki 	 *
860971f359dSYOSHIFUJI Hideaki 	 *	ip route add 1111::/128 via ...
861971f359dSYOSHIFUJI Hideaki 	 *	ip route add 1111::/64 via ...
862971f359dSYOSHIFUJI Hideaki 	 *	and we are here.
863971f359dSYOSHIFUJI Hideaki 	 *
864971f359dSYOSHIFUJI Hideaki 	 *	Ideally, this function should stop comparison
865971f359dSYOSHIFUJI Hideaki 	 *	at prefix length. It does not, but it is still OK,
866971f359dSYOSHIFUJI Hideaki 	 *	if returned value is greater than prefix length.
867971f359dSYOSHIFUJI Hideaki 	 *					--ANK (980803)
868971f359dSYOSHIFUJI Hideaki 	 */
869a02cec21SEric Dumazet 	return addrlen << 5;
870971f359dSYOSHIFUJI Hideaki }
871971f359dSYOSHIFUJI Hideaki 
8729f2e7334SYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
__ipv6_addr_diff64(const void * token1,const void * token2,int addrlen)8739f2e7334SYOSHIFUJI Hideaki / 吉藤英明 static inline int __ipv6_addr_diff64(const void *token1, const void *token2, int addrlen)
8749f2e7334SYOSHIFUJI Hideaki / 吉藤英明 {
8759f2e7334SYOSHIFUJI Hideaki / 吉藤英明 	const __be64 *a1 = token1, *a2 = token2;
8769f2e7334SYOSHIFUJI Hideaki / 吉藤英明 	int i;
8779f2e7334SYOSHIFUJI Hideaki / 吉藤英明 
8789f2e7334SYOSHIFUJI Hideaki / 吉藤英明 	addrlen >>= 3;
8799f2e7334SYOSHIFUJI Hideaki / 吉藤英明 
8809f2e7334SYOSHIFUJI Hideaki / 吉藤英明 	for (i = 0; i < addrlen; i++) {
8819f2e7334SYOSHIFUJI Hideaki / 吉藤英明 		__be64 xb = a1[i] ^ a2[i];
8829f2e7334SYOSHIFUJI Hideaki / 吉藤英明 		if (xb)
8839f2e7334SYOSHIFUJI Hideaki / 吉藤英明 			return i * 64 + 63 - __fls(be64_to_cpu(xb));
8849f2e7334SYOSHIFUJI Hideaki / 吉藤英明 	}
8859f2e7334SYOSHIFUJI Hideaki / 吉藤英明 
8869f2e7334SYOSHIFUJI Hideaki / 吉藤英明 	return addrlen << 6;
8879f2e7334SYOSHIFUJI Hideaki / 吉藤英明 }
8889f2e7334SYOSHIFUJI Hideaki / 吉藤英明 #endif
8899f2e7334SYOSHIFUJI Hideaki / 吉藤英明 
__ipv6_addr_diff(const void * token1,const void * token2,int addrlen)8909f2e7334SYOSHIFUJI Hideaki / 吉藤英明 static inline int __ipv6_addr_diff(const void *token1, const void *token2, int addrlen)
8919f2e7334SYOSHIFUJI Hideaki / 吉藤英明 {
8929f2e7334SYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
8939f2e7334SYOSHIFUJI Hideaki / 吉藤英明 	if (__builtin_constant_p(addrlen) && !(addrlen & 7))
8949f2e7334SYOSHIFUJI Hideaki / 吉藤英明 		return __ipv6_addr_diff64(token1, token2, addrlen);
8959f2e7334SYOSHIFUJI Hideaki / 吉藤英明 #endif
8969f2e7334SYOSHIFUJI Hideaki / 吉藤英明 	return __ipv6_addr_diff32(token1, token2, addrlen);
8979f2e7334SYOSHIFUJI Hideaki / 吉藤英明 }
8989f2e7334SYOSHIFUJI Hideaki / 吉藤英明 
ipv6_addr_diff(const struct in6_addr * a1,const struct in6_addr * a2)899971f359dSYOSHIFUJI Hideaki static inline int ipv6_addr_diff(const struct in6_addr *a1, const struct in6_addr *a2)
900971f359dSYOSHIFUJI Hideaki {
901971f359dSYOSHIFUJI Hideaki 	return __ipv6_addr_diff(a1, a2, sizeof(struct in6_addr));
902971f359dSYOSHIFUJI Hideaki }
903971f359dSYOSHIFUJI Hideaki 
9047f159867SEric Dumazet __be32 ipv6_select_ident(struct net *net,
905fd0273d7SMartin KaFai Lau 			 const struct in6_addr *daddr,
906fd0273d7SMartin KaFai Lau 			 const struct in6_addr *saddr);
9070c19f846SWillem de Bruijn __be32 ipv6_proxy_select_ident(struct net *net, struct sk_buff *skb);
9085188cd44SBen Hutchings 
9095c3a0fd7SJoe Perches int ip6_dst_hoplimit(struct dst_entry *dst);
9103ce9b35fSCong Wang 
ip6_sk_dst_hoplimit(struct ipv6_pinfo * np,struct flowi6 * fl6,struct dst_entry * dst)9115c98631cSLorenzo Colitti static inline int ip6_sk_dst_hoplimit(struct ipv6_pinfo *np, struct flowi6 *fl6,
9125c98631cSLorenzo Colitti 				      struct dst_entry *dst)
9135c98631cSLorenzo Colitti {
9145c98631cSLorenzo Colitti 	int hlimit;
9155c98631cSLorenzo Colitti 
9165c98631cSLorenzo Colitti 	if (ipv6_addr_is_multicast(&fl6->daddr))
9175c98631cSLorenzo Colitti 		hlimit = np->mcast_hops;
9185c98631cSLorenzo Colitti 	else
9195c98631cSLorenzo Colitti 		hlimit = np->hop_limit;
9205c98631cSLorenzo Colitti 	if (hlimit < 0)
9215c98631cSLorenzo Colitti 		hlimit = ip6_dst_hoplimit(dst);
9225c98631cSLorenzo Colitti 	return hlimit;
9235c98631cSLorenzo Colitti }
9245c98631cSLorenzo Colitti 
925c3f83241STom Herbert /* copy IPv6 saddr & daddr to flow_keys, possibly using 64bit load/store
926c3f83241STom Herbert  * Equivalent to :	flow->v6addrs.src = iph->saddr;
927c3f83241STom Herbert  *			flow->v6addrs.dst = iph->daddr;
928c3f83241STom Herbert  */
iph_to_flow_copy_v6addrs(struct flow_keys * flow,const struct ipv6hdr * iph)929c3f83241STom Herbert static inline void iph_to_flow_copy_v6addrs(struct flow_keys *flow,
930c3f83241STom Herbert 					    const struct ipv6hdr *iph)
931c3f83241STom Herbert {
932c3f83241STom Herbert 	BUILD_BUG_ON(offsetof(typeof(flow->addrs), v6addrs.dst) !=
933c3f83241STom Herbert 		     offsetof(typeof(flow->addrs), v6addrs.src) +
934c3f83241STom Herbert 		     sizeof(flow->addrs.v6addrs.src));
93558e0be1eSHangbin Liu 	memcpy(&flow->addrs.v6addrs, &iph->addrs, sizeof(flow->addrs.v6addrs));
936c3f83241STom Herbert 	flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
937c3f83241STom Herbert }
938c3f83241STom Herbert 
939a37934fcSFlorian Fainelli #if IS_ENABLED(CONFIG_IPV6)
94042240901STom Herbert 
ipv6_can_nonlocal_bind(struct net * net,struct inet_sock * inet)941db57dc7cSVincent Bernat static inline bool ipv6_can_nonlocal_bind(struct net *net,
942db57dc7cSVincent Bernat 					  struct inet_sock *inet)
943db57dc7cSVincent Bernat {
944db57dc7cSVincent Bernat 	return net->ipv6.sysctl.ip_nonlocal_bind ||
9453f7e7532SEric Dumazet 		test_bit(INET_FLAGS_FREEBIND, &inet->inet_flags) ||
9464bd0623fSEric Dumazet 		test_bit(INET_FLAGS_TRANSPARENT, &inet->inet_flags);
947db57dc7cSVincent Bernat }
948db57dc7cSVincent Bernat 
94942240901STom Herbert /* Sysctl settings for net ipv6.auto_flowlabels */
95042240901STom Herbert #define IP6_AUTO_FLOW_LABEL_OFF		0
95142240901STom Herbert #define IP6_AUTO_FLOW_LABEL_OPTOUT	1
95242240901STom Herbert #define IP6_AUTO_FLOW_LABEL_OPTIN	2
95342240901STom Herbert #define IP6_AUTO_FLOW_LABEL_FORCED	3
95442240901STom Herbert 
95542240901STom Herbert #define IP6_AUTO_FLOW_LABEL_MAX		IP6_AUTO_FLOW_LABEL_FORCED
95642240901STom Herbert 
957b5677416STom Herbert #define IP6_DEFAULT_AUTO_FLOW_LABELS	IP6_AUTO_FLOW_LABEL_OPTOUT
95842240901STom Herbert 
ip6_make_flowlabel(struct net * net,struct sk_buff * skb,__be32 flowlabel,bool autolabel,struct flowi6 * fl6)959cb1ce2efSTom Herbert static inline __be32 ip6_make_flowlabel(struct net *net, struct sk_buff *skb,
96067800f9bSTom Herbert 					__be32 flowlabel, bool autolabel,
96167800f9bSTom Herbert 					struct flowi6 *fl6)
962cb1ce2efSTom Herbert {
96367765146SEric Dumazet 	u32 hash;
964cb1ce2efSTom Herbert 
96590427ef5SDimitris Michailidis 	/* @flowlabel may include more than a flow label, eg, the traffic class.
96690427ef5SDimitris Michailidis 	 * Here we want only the flow label value.
96790427ef5SDimitris Michailidis 	 */
96890427ef5SDimitris Michailidis 	flowlabel &= IPV6_FLOWLABEL_MASK;
96990427ef5SDimitris Michailidis 
97042240901STom Herbert 	if (flowlabel ||
97142240901STom Herbert 	    net->ipv6.sysctl.auto_flowlabels == IP6_AUTO_FLOW_LABEL_OFF ||
97242240901STom Herbert 	    (!autolabel &&
97342240901STom Herbert 	     net->ipv6.sysctl.auto_flowlabels != IP6_AUTO_FLOW_LABEL_FORCED))
97442240901STom Herbert 		return flowlabel;
97542240901STom Herbert 
97667800f9bSTom Herbert 	hash = skb_get_hash_flowi6(skb, fl6);
977cb1ce2efSTom Herbert 
978cb1ce2efSTom Herbert 	/* Since this is being sent on the wire obfuscate hash a bit
979cb1ce2efSTom Herbert 	 * to minimize possbility that any useful information to an
980cb1ce2efSTom Herbert 	 * attacker is leaked. Only lower 20 bits are relevant.
981cb1ce2efSTom Herbert 	 */
982169dc027SColin Ian King 	hash = rol32(hash, 16);
983cb1ce2efSTom Herbert 
98467765146SEric Dumazet 	flowlabel = (__force __be32)hash & IPV6_FLOWLABEL_MASK;
98582a584b7STom Herbert 
98682a584b7STom Herbert 	if (net->ipv6.sysctl.flowlabel_state_ranges)
98782a584b7STom Herbert 		flowlabel |= IPV6_FLOWLABEL_STATELESS_FLAG;
988cb1ce2efSTom Herbert 
989cb1ce2efSTom Herbert 	return flowlabel;
990cb1ce2efSTom Herbert }
99142240901STom Herbert 
ip6_default_np_autolabel(struct net * net)99242240901STom Herbert static inline int ip6_default_np_autolabel(struct net *net)
99342240901STom Herbert {
99442240901STom Herbert 	switch (net->ipv6.sysctl.auto_flowlabels) {
99542240901STom Herbert 	case IP6_AUTO_FLOW_LABEL_OFF:
99642240901STom Herbert 	case IP6_AUTO_FLOW_LABEL_OPTIN:
99742240901STom Herbert 	default:
99842240901STom Herbert 		return 0;
99942240901STom Herbert 	case IP6_AUTO_FLOW_LABEL_OPTOUT:
100042240901STom Herbert 	case IP6_AUTO_FLOW_LABEL_FORCED:
100142240901STom Herbert 		return 1;
100242240901STom Herbert 	}
100342240901STom Herbert }
1004a37934fcSFlorian Fainelli #else
ip6_make_flowlabel(struct net * net,struct sk_buff * skb,__be32 flowlabel,bool autolabel,struct flowi6 * fl6)1005a37934fcSFlorian Fainelli static inline __be32 ip6_make_flowlabel(struct net *net, struct sk_buff *skb,
100642240901STom Herbert 					__be32 flowlabel, bool autolabel,
100742240901STom Herbert 					struct flowi6 *fl6)
1008a37934fcSFlorian Fainelli {
1009a37934fcSFlorian Fainelli 	return flowlabel;
1010a37934fcSFlorian Fainelli }
ip6_default_np_autolabel(struct net * net)101142240901STom Herbert static inline int ip6_default_np_autolabel(struct net *net)
101242240901STom Herbert {
101342240901STom Herbert 	return 0;
101442240901STom Herbert }
1015a37934fcSFlorian Fainelli #endif
1016a37934fcSFlorian Fainelli 
1017918ee507SPetr Machata #if IS_ENABLED(CONFIG_IPV6)
ip6_multipath_hash_policy(const struct net * net)1018918ee507SPetr Machata static inline int ip6_multipath_hash_policy(const struct net *net)
1019918ee507SPetr Machata {
1020918ee507SPetr Machata 	return net->ipv6.sysctl.multipath_hash_policy;
1021918ee507SPetr Machata }
ip6_multipath_hash_fields(const struct net * net)1022ed13923fSIdo Schimmel static inline u32 ip6_multipath_hash_fields(const struct net *net)
1023ed13923fSIdo Schimmel {
1024ed13923fSIdo Schimmel 	return net->ipv6.sysctl.multipath_hash_fields;
1025ed13923fSIdo Schimmel }
1026918ee507SPetr Machata #else
ip6_multipath_hash_policy(const struct net * net)1027918ee507SPetr Machata static inline int ip6_multipath_hash_policy(const struct net *net)
1028918ee507SPetr Machata {
1029918ee507SPetr Machata 	return 0;
1030918ee507SPetr Machata }
ip6_multipath_hash_fields(const struct net * net)1031ed13923fSIdo Schimmel static inline u32 ip6_multipath_hash_fields(const struct net *net)
1032ed13923fSIdo Schimmel {
1033ed13923fSIdo Schimmel 	return 0;
1034ed13923fSIdo Schimmel }
1035918ee507SPetr Machata #endif
1036cb1ce2efSTom Herbert 
1037971f359dSYOSHIFUJI Hideaki /*
10383e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明  *	Header manipulation
10393e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明  */
ip6_flow_hdr(struct ipv6hdr * hdr,unsigned int tclass,__be32 flowlabel)10403e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 static inline void ip6_flow_hdr(struct ipv6hdr *hdr, unsigned int tclass,
10413e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 				__be32 flowlabel)
10423e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 {
104307f623d3SYOSHIFUJI Hideaki 	*(__be32 *)hdr = htonl(0x60000000 | (tclass << 20)) | flowlabel;
10443e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 }
10453e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 
ip6_flowinfo(const struct ipv6hdr * hdr)10466502ca52SYOSHIFUJI Hideaki / 吉藤英明 static inline __be32 ip6_flowinfo(const struct ipv6hdr *hdr)
10476502ca52SYOSHIFUJI Hideaki / 吉藤英明 {
10486502ca52SYOSHIFUJI Hideaki / 吉藤英明 	return *(__be32 *)hdr & IPV6_FLOWINFO_MASK;
10496502ca52SYOSHIFUJI Hideaki / 吉藤英明 }
10506502ca52SYOSHIFUJI Hideaki / 吉藤英明 
ip6_flowlabel(const struct ipv6hdr * hdr)10513308de2bSFlorent Fourcot static inline __be32 ip6_flowlabel(const struct ipv6hdr *hdr)
10523308de2bSFlorent Fourcot {
10533308de2bSFlorent Fourcot 	return *(__be32 *)hdr & IPV6_FLOWLABEL_MASK;
10543308de2bSFlorent Fourcot }
10553308de2bSFlorent Fourcot 
ip6_tclass(__be32 flowinfo)1056d76ed22bSLi RongQing static inline u8 ip6_tclass(__be32 flowinfo)
1057d76ed22bSLi RongQing {
1058d76ed22bSLi RongQing 	return ntohl(flowinfo & IPV6_TCLASS_MASK) >> IPV6_TCLASS_SHIFT;
1059d76ed22bSLi RongQing }
1060eaa93bf4SDaniel Borkmann 
ip6_dscp(__be32 flowinfo)1061a410a0cfSGuillaume Nault static inline dscp_t ip6_dscp(__be32 flowinfo)
1062a410a0cfSGuillaume Nault {
1063a410a0cfSGuillaume Nault 	return inet_dsfield_to_dscp(ip6_tclass(flowinfo));
1064a410a0cfSGuillaume Nault }
1065a410a0cfSGuillaume Nault 
ip6_make_flowinfo(unsigned int tclass,__be32 flowlabel)1066eaa93bf4SDaniel Borkmann static inline __be32 ip6_make_flowinfo(unsigned int tclass, __be32 flowlabel)
1067eaa93bf4SDaniel Borkmann {
1068eaa93bf4SDaniel Borkmann 	return htonl(tclass << IPV6_TCLASS_SHIFT) | flowlabel;
1069eaa93bf4SDaniel Borkmann }
1070eaa93bf4SDaniel Borkmann 
flowi6_get_flowlabel(const struct flowi6 * fl6)1071fa1be7e0SMichal Kubecek static inline __be32 flowi6_get_flowlabel(const struct flowi6 *fl6)
1072fa1be7e0SMichal Kubecek {
1073fa1be7e0SMichal Kubecek 	return fl6->flowlabel & IPV6_FLOWLABEL_MASK;
1074fa1be7e0SMichal Kubecek }
1075fa1be7e0SMichal Kubecek 
10763e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 /*
10771da177e4SLinus Torvalds  *	Prototypes exported by ipv6
10781da177e4SLinus Torvalds  */
10791da177e4SLinus Torvalds 
10801da177e4SLinus Torvalds /*
10811da177e4SLinus Torvalds  *	rcv function (called from netdevice level)
10821da177e4SLinus Torvalds  */
10831da177e4SLinus Torvalds 
10845c3a0fd7SJoe Perches int ipv6_rcv(struct sk_buff *skb, struct net_device *dev,
10855c3a0fd7SJoe Perches 	     struct packet_type *pt, struct net_device *orig_dev);
1086d8269e2cSEdward Cree void ipv6_list_rcv(struct list_head *head, struct packet_type *pt,
1087d8269e2cSEdward Cree 		   struct net_device *orig_dev);
10881da177e4SLinus Torvalds 
10890c4b51f0SEric W. Biederman int ip6_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
1090b05e1066SPatrick McHardy 
10911da177e4SLinus Torvalds /*
10921da177e4SLinus Torvalds  *	upper-layer output functions
10931da177e4SLinus Torvalds  */
10941c1e9d2bSEric Dumazet int ip6_xmit(const struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
10954f6570d7SEric Dumazet 	     __u32 mark, struct ipv6_txoptions *opt, int tclass, u32 priority);
10961da177e4SLinus Torvalds 
10975c3a0fd7SJoe Perches int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr);
10981da177e4SLinus Torvalds 
10995c3a0fd7SJoe Perches int ip6_append_data(struct sock *sk,
11005c3a0fd7SJoe Perches 		    int getfrag(void *from, char *to, int offset, int len,
11015c3a0fd7SJoe Perches 				int odd, struct sk_buff *skb),
1102f93431c8SWang Yufen 		    void *from, size_t length, int transhdrlen,
110326879da5SWei Wang 		    struct ipcm6_cookie *ipc6, struct flowi6 *fl6,
11045fdaa88dSWillem de Bruijn 		    struct rt6_info *rt, unsigned int flags);
11051da177e4SLinus Torvalds 
11065c3a0fd7SJoe Perches int ip6_push_pending_frames(struct sock *sk);
11071da177e4SLinus Torvalds 
11085c3a0fd7SJoe Perches void ip6_flush_pending_frames(struct sock *sk);
11091da177e4SLinus Torvalds 
11106422398cSVlad Yasevich int ip6_send_skb(struct sk_buff *skb);
11116422398cSVlad Yasevich 
11126422398cSVlad Yasevich struct sk_buff *__ip6_make_skb(struct sock *sk, struct sk_buff_head *queue,
11136422398cSVlad Yasevich 			       struct inet_cork_full *cork,
11146422398cSVlad Yasevich 			       struct inet6_cork *v6_cork);
11156422398cSVlad Yasevich struct sk_buff *ip6_make_skb(struct sock *sk,
11166422398cSVlad Yasevich 			     int getfrag(void *from, char *to, int offset,
11176422398cSVlad Yasevich 					 int len, int odd, struct sk_buff *skb),
1118f93431c8SWang Yufen 			     void *from, size_t length, int transhdrlen,
1119f37a4cc6SPavel Begunkov 			     struct ipcm6_cookie *ipc6,
112026879da5SWei Wang 			     struct rt6_info *rt, unsigned int flags,
11215fdaa88dSWillem de Bruijn 			     struct inet_cork_full *cork);
11226422398cSVlad Yasevich 
ip6_finish_skb(struct sock * sk)11236422398cSVlad Yasevich static inline struct sk_buff *ip6_finish_skb(struct sock *sk)
11246422398cSVlad Yasevich {
11256422398cSVlad Yasevich 	return __ip6_make_skb(sk, &sk->sk_write_queue, &inet_sk(sk)->cork,
11266422398cSVlad Yasevich 			      &inet6_sk(sk)->cork);
11276422398cSVlad Yasevich }
11286422398cSVlad Yasevich 
1129343d60aaSRoopa Prabhu int ip6_dst_lookup(struct net *net, struct sock *sk, struct dst_entry **dst,
1130343d60aaSRoopa Prabhu 		   struct flowi6 *fl6);
1131c4e85f73SSabrina Dubroca struct dst_entry *ip6_dst_lookup_flow(struct net *net, const struct sock *sk, struct flowi6 *fl6,
11320e0d44abSSteffen Klassert 				      const struct in6_addr *final_dst);
11335c3a0fd7SJoe Perches struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
113496818159SAlexey Kodanev 					 const struct in6_addr *final_dst,
113596818159SAlexey Kodanev 					 bool connected);
1136571912c6SMartin Varghese struct dst_entry *ip6_dst_lookup_tunnel(struct sk_buff *skb,
1137571912c6SMartin Varghese 					struct net_device *dev,
1138571912c6SMartin Varghese 					struct net *net, struct socket *sock,
1139571912c6SMartin Varghese 					struct in6_addr *saddr,
1140571912c6SMartin Varghese 					const struct ip_tunnel_info *info,
1141571912c6SMartin Varghese 					u8 protocol, bool use_cache);
11425c3a0fd7SJoe Perches struct dst_entry *ip6_blackhole_route(struct net *net,
114369ead7afSDavid S. Miller 				      struct dst_entry *orig_dst);
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds /*
11461da177e4SLinus Torvalds  *	skb processing functions
11471da177e4SLinus Torvalds  */
11481da177e4SLinus Torvalds 
1149ede2059dSEric W. Biederman int ip6_output(struct net *net, struct sock *sk, struct sk_buff *skb);
11505c3a0fd7SJoe Perches int ip6_forward(struct sk_buff *skb);
11515c3a0fd7SJoe Perches int ip6_input(struct sk_buff *skb);
11525c3a0fd7SJoe Perches int ip6_mc_input(struct sk_buff *skb);
115380bde363SPaolo Abeni void ip6_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int nexthdr,
115480bde363SPaolo Abeni 			      bool have_final);
11551da177e4SLinus Torvalds 
1156cf91a99dSEric W. Biederman int __ip6_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
115733224b16SEric W. Biederman int ip6_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
1158ef76bc23SHerbert Xu 
11591da177e4SLinus Torvalds /*
11601da177e4SLinus Torvalds  *	Extension header (options) processing
11611da177e4SLinus Torvalds  */
11621da177e4SLinus Torvalds 
11635c3a0fd7SJoe Perches void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
1164613fa3caSDavid Lebrun 			  u8 *proto, struct in6_addr **daddr_p,
1165613fa3caSDavid Lebrun 			  struct in6_addr *saddr);
11665c3a0fd7SJoe Perches void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
11671da177e4SLinus Torvalds 			 u8 *proto);
11681da177e4SLinus Torvalds 
11695c3a0fd7SJoe Perches int ipv6_skip_exthdr(const struct sk_buff *, int start, u8 *nexthdrp,
11705c3a0fd7SJoe Perches 		     __be16 *frag_offp);
11711da177e4SLinus Torvalds 
11725c3a0fd7SJoe Perches bool ipv6_ext_hdr(u8 nexthdr);
11731da177e4SLinus Torvalds 
1174f8f62675SJesse Gross enum {
1175f8f62675SJesse Gross 	IP6_FH_F_FRAG		= (1 << 0),
1176f8f62675SJesse Gross 	IP6_FH_F_AUTH		= (1 << 1),
11779195bb8eSAnsis Atteka 	IP6_FH_F_SKIP_RH	= (1 << 2),
1178f8f62675SJesse Gross };
1179f8f62675SJesse Gross 
1180f8f62675SJesse Gross /* find specified header and get offset to it */
11815c3a0fd7SJoe Perches int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset, int target,
11825c3a0fd7SJoe Perches 		  unsigned short *fragoff, int *fragflg);
1183f8f62675SJesse Gross 
11840868383bSHuw Davies int ipv6_find_tlv(const struct sk_buff *skb, int offset, int type);
1185c61a4043SMasahide NAKAMURA 
11865c3a0fd7SJoe Perches struct in6_addr *fl6_update_dst(struct flowi6 *fl6,
118720c59de2SArnaud Ebalard 				const struct ipv6_txoptions *opt,
118820c59de2SArnaud Ebalard 				struct in6_addr *orig);
118920c59de2SArnaud Ebalard 
11901da177e4SLinus Torvalds /*
11911da177e4SLinus Torvalds  *	socket options (ipv6_sockglue.c)
11921da177e4SLinus Torvalds  */
1193790eb673SEric Dumazet DECLARE_STATIC_KEY_FALSE(ip6_min_hopcount);
11941da177e4SLinus Torvalds 
119575b64b68SMartin KaFai Lau int do_ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
119675b64b68SMartin KaFai Lau 		       unsigned int optlen);
1197a7b75c5aSChristoph Hellwig int ipv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1198a7b75c5aSChristoph Hellwig 		    unsigned int optlen);
119938566ec0SMartin KaFai Lau int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
120038566ec0SMartin KaFai Lau 		       sockptr_t optval, sockptr_t optlen);
12015c3a0fd7SJoe Perches int ipv6_getsockopt(struct sock *sk, int level, int optname,
12025c3a0fd7SJoe Perches 		    char __user *optval, int __user *optlen);
12031da177e4SLinus Torvalds 
12040382a25aSGuillaume Nault int __ip6_datagram_connect(struct sock *sk, struct sockaddr *addr,
12050382a25aSGuillaume Nault 			   int addr_len);
12065c3a0fd7SJoe Perches int ip6_datagram_connect(struct sock *sk, struct sockaddr *addr, int addr_len);
120782b276cdSHannes Frederic Sowa int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr *addr,
120882b276cdSHannes Frederic Sowa 				 int addr_len);
120933c162a9SMartin KaFai Lau int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr);
1210e646b657SMartin KaFai Lau void ip6_datagram_release_cb(struct sock *sk);
12111da177e4SLinus Torvalds 
121285fbaa75SHannes Frederic Sowa int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len,
121385fbaa75SHannes Frederic Sowa 		    int *addr_len);
121485fbaa75SHannes Frederic Sowa int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
121585fbaa75SHannes Frederic Sowa 		     int *addr_len);
12165c3a0fd7SJoe Perches void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err, __be16 port,
12171da177e4SLinus Torvalds 		     u32 info, u8 *payload);
12185c3a0fd7SJoe Perches void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info);
12195c3a0fd7SJoe Perches void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu);
12201da177e4SLinus Torvalds 
122121985f43SKuniyuki Iwashima void inet6_cleanup_sock(struct sock *sk);
1222d38afeecSKuniyuki Iwashima void inet6_sock_destruct(struct sock *sk);
12235c3a0fd7SJoe Perches int inet6_release(struct socket *sock);
12245c3a0fd7SJoe Perches int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len);
1225e6d360ffSPaolo Abeni int inet6_bind_sk(struct sock *sk, struct sockaddr *uaddr, int addr_len);
12269b2c45d4SDenys Vlasenko int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
12275c3a0fd7SJoe Perches 		  int peer);
12285c3a0fd7SJoe Perches int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
12293986912fSChristoph Hellwig int inet6_compat_ioctl(struct socket *sock, unsigned int cmd,
12303986912fSChristoph Hellwig 		unsigned long arg);
12311da177e4SLinus Torvalds 
12325c3a0fd7SJoe Perches int inet6_hash_connect(struct inet_timewait_death_row *death_row,
1233d8313f5cSArnaldo Carvalho de Melo 			      struct sock *sk);
1234e42f1ac6SFlorian Westphal int inet6_sendmsg(struct socket *sock, struct msghdr *msg, size_t size);
1235e42f1ac6SFlorian Westphal int inet6_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1236e42f1ac6SFlorian Westphal 		  int flags);
1237d8313f5cSArnaldo Carvalho de Melo 
12381da177e4SLinus Torvalds /*
12391da177e4SLinus Torvalds  * reassembly.c
12401da177e4SLinus Torvalds  */
124190ddc4f0SEric Dumazet extern const struct proto_ops inet6_stream_ops;
124290ddc4f0SEric Dumazet extern const struct proto_ops inet6_dgram_ops;
124377d4b1d3SEric Dumazet extern const struct proto_ops inet6_sockraw_ops;
124420380731SArnaldo Carvalho de Melo 
124514c85021SArnaldo Carvalho de Melo struct group_source_req;
124614c85021SArnaldo Carvalho de Melo struct group_filter;
124714c85021SArnaldo Carvalho de Melo 
12485c3a0fd7SJoe Perches int ip6_mc_source(int add, int omode, struct sock *sk,
124920380731SArnaldo Carvalho de Melo 		  struct group_source_req *pgsr);
1250d59eb177SAl Viro int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf,
1251d59eb177SAl Viro 		  struct sockaddr_storage *list);
12525c3a0fd7SJoe Perches int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
12536dadbe4bSMartin KaFai Lau 		  sockptr_t optval, size_t ss_offset);
125420380731SArnaldo Carvalho de Melo 
125520380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS
12565c3a0fd7SJoe Perches int ac6_proc_init(struct net *net);
12575c3a0fd7SJoe Perches void ac6_proc_exit(struct net *net);
12585c3a0fd7SJoe Perches int raw6_proc_init(void);
12595c3a0fd7SJoe Perches void raw6_proc_exit(void);
12605c3a0fd7SJoe Perches int tcp6_proc_init(struct net *net);
12615c3a0fd7SJoe Perches void tcp6_proc_exit(struct net *net);
12625c3a0fd7SJoe Perches int udp6_proc_init(struct net *net);
12635c3a0fd7SJoe Perches void udp6_proc_exit(struct net *net);
12645c3a0fd7SJoe Perches int udplite6_proc_init(void);
12655c3a0fd7SJoe Perches void udplite6_proc_exit(void);
12665c3a0fd7SJoe Perches int ipv6_misc_proc_init(void);
12675c3a0fd7SJoe Perches void ipv6_misc_proc_exit(void);
12685c3a0fd7SJoe Perches int snmp6_register_dev(struct inet6_dev *idev);
12695c3a0fd7SJoe Perches int snmp6_unregister_dev(struct inet6_dev *idev);
127020380731SArnaldo Carvalho de Melo 
12717f7d9a6bSHerbert Xu #else
ac6_proc_init(struct net * net)12726ab57e7eSDaniel Lezcano static inline int ac6_proc_init(struct net *net) { return 0; }
ac6_proc_exit(struct net * net)12736ab57e7eSDaniel Lezcano static inline void ac6_proc_exit(struct net *net) { }
snmp6_register_dev(struct inet6_dev * idev)12746ab57e7eSDaniel Lezcano static inline int snmp6_register_dev(struct inet6_dev *idev) { return 0; }
snmp6_unregister_dev(struct inet6_dev * idev)12756ab57e7eSDaniel Lezcano static inline int snmp6_unregister_dev(struct inet6_dev *idev) { return 0; }
127620380731SArnaldo Carvalho de Melo #endif
127720380731SArnaldo Carvalho de Melo 
127820380731SArnaldo Carvalho de Melo #ifdef CONFIG_SYSCTL
12795c3a0fd7SJoe Perches struct ctl_table *ipv6_icmp_sysctl_init(struct net *net);
1280c899710fSJoel Granados size_t ipv6_icmp_sysctl_table_size(void);
12815c3a0fd7SJoe Perches struct ctl_table *ipv6_route_sysctl_init(struct net *net);
1282c899710fSJoel Granados size_t ipv6_route_sysctl_table_size(struct net *net);
12835c3a0fd7SJoe Perches int ipv6_sysctl_register(void);
12845c3a0fd7SJoe Perches void ipv6_sysctl_unregister(void);
128520380731SArnaldo Carvalho de Melo #endif
128620380731SArnaldo Carvalho de Melo 
128746a4dee0SMadhu Challa int ipv6_sock_mc_join(struct sock *sk, int ifindex,
128846a4dee0SMadhu Challa 		      const struct in6_addr *addr);
1289c7ea20c9SHangbin Liu int ipv6_sock_mc_join_ssm(struct sock *sk, int ifindex,
1290c7ea20c9SHangbin Liu 			  const struct in6_addr *addr, unsigned int mode);
129146a4dee0SMadhu Challa int ipv6_sock_mc_drop(struct sock *sk, int ifindex,
129246a4dee0SMadhu Challa 		      const struct in6_addr *addr);
12939b115749SChristoph Hellwig 
ip6_sock_set_v6only(struct sock * sk)12949b115749SChristoph Hellwig static inline int ip6_sock_set_v6only(struct sock *sk)
12959b115749SChristoph Hellwig {
12969b115749SChristoph Hellwig 	if (inet_sk(sk)->inet_num)
12979b115749SChristoph Hellwig 		return -EINVAL;
12989b115749SChristoph Hellwig 	lock_sock(sk);
12999b115749SChristoph Hellwig 	sk->sk_ipv6only = true;
13009b115749SChristoph Hellwig 	release_sock(sk);
13019b115749SChristoph Hellwig 	return 0;
13029b115749SChristoph Hellwig }
13039b115749SChristoph Hellwig 
ip6_sock_set_recverr(struct sock * sk)1304fce93494SChristoph Hellwig static inline void ip6_sock_set_recverr(struct sock *sk)
1305fce93494SChristoph Hellwig {
1306fce93494SChristoph Hellwig 	lock_sock(sk);
1307fce93494SChristoph Hellwig 	inet6_sk(sk)->recverr = true;
1308fce93494SChristoph Hellwig 	release_sock(sk);
1309fce93494SChristoph Hellwig }
1310fce93494SChristoph Hellwig 
__ip6_sock_set_addr_preferences(struct sock * sk,int val)131118d5ad62SChristoph Hellwig static inline int __ip6_sock_set_addr_preferences(struct sock *sk, int val)
131218d5ad62SChristoph Hellwig {
131318d5ad62SChristoph Hellwig 	unsigned int pref = 0;
131418d5ad62SChristoph Hellwig 	unsigned int prefmask = ~0;
131518d5ad62SChristoph Hellwig 
131618d5ad62SChristoph Hellwig 	/* check PUBLIC/TMP/PUBTMP_DEFAULT conflicts */
131718d5ad62SChristoph Hellwig 	switch (val & (IPV6_PREFER_SRC_PUBLIC |
131818d5ad62SChristoph Hellwig 		       IPV6_PREFER_SRC_TMP |
131918d5ad62SChristoph Hellwig 		       IPV6_PREFER_SRC_PUBTMP_DEFAULT)) {
132018d5ad62SChristoph Hellwig 	case IPV6_PREFER_SRC_PUBLIC:
132118d5ad62SChristoph Hellwig 		pref |= IPV6_PREFER_SRC_PUBLIC;
132218d5ad62SChristoph Hellwig 		prefmask &= ~(IPV6_PREFER_SRC_PUBLIC |
132318d5ad62SChristoph Hellwig 			      IPV6_PREFER_SRC_TMP);
132418d5ad62SChristoph Hellwig 		break;
132518d5ad62SChristoph Hellwig 	case IPV6_PREFER_SRC_TMP:
132618d5ad62SChristoph Hellwig 		pref |= IPV6_PREFER_SRC_TMP;
132718d5ad62SChristoph Hellwig 		prefmask &= ~(IPV6_PREFER_SRC_PUBLIC |
132818d5ad62SChristoph Hellwig 			      IPV6_PREFER_SRC_TMP);
132918d5ad62SChristoph Hellwig 		break;
133018d5ad62SChristoph Hellwig 	case IPV6_PREFER_SRC_PUBTMP_DEFAULT:
133118d5ad62SChristoph Hellwig 		prefmask &= ~(IPV6_PREFER_SRC_PUBLIC |
133218d5ad62SChristoph Hellwig 			      IPV6_PREFER_SRC_TMP);
133318d5ad62SChristoph Hellwig 		break;
133418d5ad62SChristoph Hellwig 	case 0:
133518d5ad62SChristoph Hellwig 		break;
133618d5ad62SChristoph Hellwig 	default:
133718d5ad62SChristoph Hellwig 		return -EINVAL;
133818d5ad62SChristoph Hellwig 	}
133918d5ad62SChristoph Hellwig 
134018d5ad62SChristoph Hellwig 	/* check HOME/COA conflicts */
134118d5ad62SChristoph Hellwig 	switch (val & (IPV6_PREFER_SRC_HOME | IPV6_PREFER_SRC_COA)) {
134218d5ad62SChristoph Hellwig 	case IPV6_PREFER_SRC_HOME:
134318d5ad62SChristoph Hellwig 		prefmask &= ~IPV6_PREFER_SRC_COA;
134418d5ad62SChristoph Hellwig 		break;
134518d5ad62SChristoph Hellwig 	case IPV6_PREFER_SRC_COA:
134618d5ad62SChristoph Hellwig 		pref |= IPV6_PREFER_SRC_COA;
134718d5ad62SChristoph Hellwig 		break;
134818d5ad62SChristoph Hellwig 	case 0:
134918d5ad62SChristoph Hellwig 		break;
135018d5ad62SChristoph Hellwig 	default:
135118d5ad62SChristoph Hellwig 		return -EINVAL;
135218d5ad62SChristoph Hellwig 	}
135318d5ad62SChristoph Hellwig 
135418d5ad62SChristoph Hellwig 	/* check CGA/NONCGA conflicts */
135518d5ad62SChristoph Hellwig 	switch (val & (IPV6_PREFER_SRC_CGA|IPV6_PREFER_SRC_NONCGA)) {
135618d5ad62SChristoph Hellwig 	case IPV6_PREFER_SRC_CGA:
135718d5ad62SChristoph Hellwig 	case IPV6_PREFER_SRC_NONCGA:
135818d5ad62SChristoph Hellwig 	case 0:
135918d5ad62SChristoph Hellwig 		break;
136018d5ad62SChristoph Hellwig 	default:
136118d5ad62SChristoph Hellwig 		return -EINVAL;
136218d5ad62SChristoph Hellwig 	}
136318d5ad62SChristoph Hellwig 
136418d5ad62SChristoph Hellwig 	inet6_sk(sk)->srcprefs = (inet6_sk(sk)->srcprefs & prefmask) | pref;
136518d5ad62SChristoph Hellwig 	return 0;
136618d5ad62SChristoph Hellwig }
136718d5ad62SChristoph Hellwig 
ip6_sock_set_addr_preferences(struct sock * sk,int val)13688cdd9f1aSEric Dumazet static inline int ip6_sock_set_addr_preferences(struct sock *sk, int val)
136918d5ad62SChristoph Hellwig {
137018d5ad62SChristoph Hellwig 	int ret;
137118d5ad62SChristoph Hellwig 
137218d5ad62SChristoph Hellwig 	lock_sock(sk);
137318d5ad62SChristoph Hellwig 	ret = __ip6_sock_set_addr_preferences(sk, val);
137418d5ad62SChristoph Hellwig 	release_sock(sk);
137518d5ad62SChristoph Hellwig 	return ret;
137618d5ad62SChristoph Hellwig }
137718d5ad62SChristoph Hellwig 
ip6_sock_set_recvpktinfo(struct sock * sk)13787d7207c2SChristoph Hellwig static inline void ip6_sock_set_recvpktinfo(struct sock *sk)
13797d7207c2SChristoph Hellwig {
13807d7207c2SChristoph Hellwig 	lock_sock(sk);
13817d7207c2SChristoph Hellwig 	inet6_sk(sk)->rxopt.bits.rxinfo = true;
13827d7207c2SChristoph Hellwig 	release_sock(sk);
13837d7207c2SChristoph Hellwig }
13847d7207c2SChristoph Hellwig 
13851da177e4SLinus Torvalds #endif /* _NET_IPV6_H */
1386