xref: /linux-6.15/include/net/ipv6.h (revision 5c3a0fd7)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	Linux INET6 implementation
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *	Authors:
51da177e4SLinus Torvalds  *	Pedro Roque		<[email protected]>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *	This program is free software; you can redistribute it and/or
81da177e4SLinus Torvalds  *      modify it under the terms of the GNU General Public License
91da177e4SLinus Torvalds  *      as published by the Free Software Foundation; either version
101da177e4SLinus Torvalds  *      2 of the License, or (at your option) any later version.
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #ifndef _NET_IPV6_H
141da177e4SLinus Torvalds #define _NET_IPV6_H
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <linux/ipv6.h>
171da177e4SLinus Torvalds #include <linux/hardirq.h>
1808dcdbf6SEric Dumazet #include <linux/jhash.h>
1920283d84SHerbert Xu #include <net/if_inet6.h>
201da177e4SLinus Torvalds #include <net/ndisc.h>
211da177e4SLinus Torvalds #include <net/flow.h>
221da177e4SLinus Torvalds #include <net/snmp.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #define SIN6_LEN_RFC2133	24
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds #define IPV6_MAXPLEN		65535
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds /*
291da177e4SLinus Torvalds  *	NextHeader field of IPv6 header
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #define NEXTHDR_HOP		0	/* Hop-by-hop option header. */
331da177e4SLinus Torvalds #define NEXTHDR_TCP		6	/* TCP segment. */
341da177e4SLinus Torvalds #define NEXTHDR_UDP		17	/* UDP message. */
351da177e4SLinus Torvalds #define NEXTHDR_IPV6		41	/* IPv6 in IPv6 */
361da177e4SLinus Torvalds #define NEXTHDR_ROUTING		43	/* Routing header. */
371da177e4SLinus Torvalds #define NEXTHDR_FRAGMENT	44	/* Fragmentation/reassembly header. */
38c12b395aS[email protected] #define NEXTHDR_GRE		47	/* GRE header. */
391da177e4SLinus Torvalds #define NEXTHDR_ESP		50	/* Encapsulating security payload. */
401da177e4SLinus Torvalds #define NEXTHDR_AUTH		51	/* Authentication header. */
411da177e4SLinus Torvalds #define NEXTHDR_ICMP		58	/* ICMP for IPv6. */
421da177e4SLinus Torvalds #define NEXTHDR_NONE		59	/* No next header */
431da177e4SLinus Torvalds #define NEXTHDR_DEST		60	/* Destination options header. */
44280c571eSJoe Stringer #define NEXTHDR_SCTP		132	/* SCTP message. */
452b741653SMasahide NAKAMURA #define NEXTHDR_MOBILITY	135	/* Mobility header. */
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #define NEXTHDR_MAX		255
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #define IPV6_DEFAULT_HOPLIMIT   64
521da177e4SLinus Torvalds #define IPV6_DEFAULT_MCASTHOPS	1
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds /*
551da177e4SLinus Torvalds  *	Addr type
561da177e4SLinus Torvalds  *
571da177e4SLinus Torvalds  *	type	-	unicast | multicast
581da177e4SLinus Torvalds  *	scope	-	local	| site	    | global
591da177e4SLinus Torvalds  *	v4	-	compat
601da177e4SLinus Torvalds  *	v4mapped
611da177e4SLinus Torvalds  *	any
621da177e4SLinus Torvalds  *	loopback
631da177e4SLinus Torvalds  */
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds #define IPV6_ADDR_ANY		0x0000U
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds #define IPV6_ADDR_UNICAST      	0x0001U
681da177e4SLinus Torvalds #define IPV6_ADDR_MULTICAST    	0x0002U
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds #define IPV6_ADDR_LOOPBACK	0x0010U
711da177e4SLinus Torvalds #define IPV6_ADDR_LINKLOCAL	0x0020U
721da177e4SLinus Torvalds #define IPV6_ADDR_SITELOCAL	0x0040U
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds #define IPV6_ADDR_COMPATv4	0x0080U
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_MASK	0x00f0U
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds #define IPV6_ADDR_MAPPED	0x1000U
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds /*
811da177e4SLinus Torvalds  *	Addr scopes
821da177e4SLinus Torvalds  */
831da177e4SLinus Torvalds #define IPV6_ADDR_MC_SCOPE(a)	\
841da177e4SLinus Torvalds 	((a)->s6_addr[1] & 0x0f)	/* nonstandard */
851da177e4SLinus Torvalds #define __IPV6_ADDR_SCOPE_INVALID	-1
861da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_NODELOCAL	0x01
871da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_LINKLOCAL	0x02
881da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_SITELOCAL	0x05
891da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_ORGLOCAL	0x08
901da177e4SLinus Torvalds #define IPV6_ADDR_SCOPE_GLOBAL		0x0e
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds /*
935ced1339SLinus Lüssing  *	Addr flags
945ced1339SLinus Lüssing  */
955ced1339SLinus Lüssing #define IPV6_ADDR_MC_FLAG_TRANSIENT(a)	\
965ced1339SLinus Lüssing 	((a)->s6_addr[1] & 0x10)
975ced1339SLinus Lüssing #define IPV6_ADDR_MC_FLAG_PREFIX(a)	\
985ced1339SLinus Lüssing 	((a)->s6_addr[1] & 0x20)
995ced1339SLinus Lüssing #define IPV6_ADDR_MC_FLAG_RENDEZVOUS(a)	\
1005ced1339SLinus Lüssing 	((a)->s6_addr[1] & 0x40)
1015ced1339SLinus Lüssing 
1025ced1339SLinus Lüssing /*
1031da177e4SLinus Torvalds  *	fragmentation header
1041da177e4SLinus Torvalds  */
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds struct frag_hdr {
10744473a6bSAl Viro 	__u8	nexthdr;
10844473a6bSAl Viro 	__u8	reserved;
10944473a6bSAl Viro 	__be16	frag_off;
11044473a6bSAl Viro 	__be32	identification;
1111da177e4SLinus Torvalds };
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds #define	IP6_MF	0x0001
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds #include <net/sock.h>
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds /* sysctls */
1181da177e4SLinus Torvalds extern int sysctl_mld_max_msf;
1193d7cc2baSPavel Emelyanov 
120087fe240SDenis V. Lunev #define _DEVINC(net, statname, modifier, idev, field)			\
12114878f75SDavid L Stevens ({									\
12214878f75SDavid L Stevens 	struct inet6_dev *_idev = (idev);				\
12314878f75SDavid L Stevens 	if (likely(_idev != NULL))					\
12414878f75SDavid L Stevens 		SNMP_INC_STATS##modifier((_idev)->stats.statname, (field)); \
1259261e537SDenis V. Lunev 	SNMP_INC_STATS##modifier((net)->mib.statname##_statistics, (field));\
12614878f75SDavid L Stevens })
12714878f75SDavid L Stevens 
128be281e55SEric Dumazet /* per device counters are atomic_long_t */
129be281e55SEric Dumazet #define _DEVINCATOMIC(net, statname, modifier, idev, field)		\
130be281e55SEric Dumazet ({									\
131be281e55SEric Dumazet 	struct inet6_dev *_idev = (idev);				\
132be281e55SEric Dumazet 	if (likely(_idev != NULL))					\
133be281e55SEric Dumazet 		SNMP_INC_STATS_ATOMIC_LONG((_idev)->stats.statname##dev, (field)); \
134be281e55SEric Dumazet 	SNMP_INC_STATS##modifier((net)->mib.statname##_statistics, (field));\
135be281e55SEric Dumazet })
136be281e55SEric Dumazet 
1372a24444fSEric Dumazet /* per device and per net counters are atomic_long_t */
1382a24444fSEric Dumazet #define _DEVINC_ATOMIC_ATOMIC(net, statname, idev, field)		\
1392a24444fSEric Dumazet ({									\
1402a24444fSEric Dumazet 	struct inet6_dev *_idev = (idev);				\
1412a24444fSEric Dumazet 	if (likely(_idev != NULL))					\
1422a24444fSEric Dumazet 		SNMP_INC_STATS_ATOMIC_LONG((_idev)->stats.statname##dev, (field)); \
1432a24444fSEric Dumazet 	SNMP_INC_STATS_ATOMIC_LONG((net)->mib.statname##_statistics, (field));\
1442a24444fSEric Dumazet })
1452a24444fSEric Dumazet 
146087fe240SDenis V. Lunev #define _DEVADD(net, statname, modifier, idev, field, val)		\
1478e7999c4SPavel Emelyanov ({									\
1488e7999c4SPavel Emelyanov 	struct inet6_dev *_idev = (idev);				\
1498e7999c4SPavel Emelyanov 	if (likely(_idev != NULL))					\
1508e7999c4SPavel Emelyanov 		SNMP_ADD_STATS##modifier((_idev)->stats.statname, (field), (val)); \
1519261e537SDenis V. Lunev 	SNMP_ADD_STATS##modifier((net)->mib.statname##_statistics, (field), (val));\
1528e7999c4SPavel Emelyanov })
1538e7999c4SPavel Emelyanov 
154edf391ffSNeil Horman #define _DEVUPD(net, statname, modifier, idev, field, val)		\
155edf391ffSNeil Horman ({									\
156edf391ffSNeil Horman 	struct inet6_dev *_idev = (idev);				\
157edf391ffSNeil Horman 	if (likely(_idev != NULL))					\
158edf391ffSNeil Horman 		SNMP_UPD_PO_STATS##modifier((_idev)->stats.statname, field, (val)); \
159edf391ffSNeil Horman 	SNMP_UPD_PO_STATS##modifier((net)->mib.statname##_statistics, field, (val));\
160edf391ffSNeil Horman })
161edf391ffSNeil Horman 
1621da177e4SLinus Torvalds /* MIBs */
16314878f75SDavid L Stevens 
164087fe240SDenis V. Lunev #define IP6_INC_STATS(net, idev,field)		\
1654ce3c183SEric Dumazet 		_DEVINC(net, ipv6, 64, idev, field)
166087fe240SDenis V. Lunev #define IP6_INC_STATS_BH(net, idev,field)	\
1674ce3c183SEric Dumazet 		_DEVINC(net, ipv6, 64_BH, idev, field)
168edf391ffSNeil Horman #define IP6_ADD_STATS(net, idev,field,val)	\
1694ce3c183SEric Dumazet 		_DEVADD(net, ipv6, 64, idev, field, val)
170087fe240SDenis V. Lunev #define IP6_ADD_STATS_BH(net, idev,field,val)	\
1714ce3c183SEric Dumazet 		_DEVADD(net, ipv6, 64_BH, idev, field, val)
172edf391ffSNeil Horman #define IP6_UPD_PO_STATS(net, idev,field,val)   \
1734ce3c183SEric Dumazet 		_DEVUPD(net, ipv6, 64, idev, field, val)
174edf391ffSNeil Horman #define IP6_UPD_PO_STATS_BH(net, idev,field,val)   \
1754ce3c183SEric Dumazet 		_DEVUPD(net, ipv6, 64_BH, idev, field, val)
176087fe240SDenis V. Lunev #define ICMP6_INC_STATS(net, idev, field)	\
177be281e55SEric Dumazet 		_DEVINCATOMIC(net, icmpv6, , idev, field)
178087fe240SDenis V. Lunev #define ICMP6_INC_STATS_BH(net, idev, field)	\
179be281e55SEric Dumazet 		_DEVINCATOMIC(net, icmpv6, _BH, idev, field)
18014878f75SDavid L Stevens 
181087fe240SDenis V. Lunev #define ICMP6MSGOUT_INC_STATS(net, idev, field)		\
1822a24444fSEric Dumazet 	_DEVINC_ATOMIC_ATOMIC(net, icmpv6msg, idev, field +256)
183087fe240SDenis V. Lunev #define ICMP6MSGOUT_INC_STATS_BH(net, idev, field)	\
1842a24444fSEric Dumazet 	_DEVINC_ATOMIC_ATOMIC(net, icmpv6msg, idev, field +256)
185087fe240SDenis V. Lunev #define ICMP6MSGIN_INC_STATS_BH(net, idev, field)	\
1862a24444fSEric Dumazet 	_DEVINC_ATOMIC_ATOMIC(net, icmpv6msg, idev, field)
18714878f75SDavid L Stevens 
188fd2c3ef7SEric Dumazet struct ip6_ra_chain {
1891da177e4SLinus Torvalds 	struct ip6_ra_chain	*next;
1901da177e4SLinus Torvalds 	struct sock		*sk;
1911da177e4SLinus Torvalds 	int			sel;
1921da177e4SLinus Torvalds 	void			(*destructor)(struct sock *);
1931da177e4SLinus Torvalds };
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds extern struct ip6_ra_chain	*ip6_ra_chain;
1961da177e4SLinus Torvalds extern rwlock_t ip6_ra_lock;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds /*
1991da177e4SLinus Torvalds    This structure is prepared by protocol, when parsing
2001da177e4SLinus Torvalds    ancillary data and passed to IPv6.
2011da177e4SLinus Torvalds  */
2021da177e4SLinus Torvalds 
203fd2c3ef7SEric Dumazet struct ipv6_txoptions {
2041da177e4SLinus Torvalds 	/* Length of this structure */
2051da177e4SLinus Torvalds 	int			tot_len;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	/* length of extension headers   */
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	__u16			opt_flen;	/* after fragment hdr */
2101da177e4SLinus Torvalds 	__u16			opt_nflen;	/* before fragment hdr */
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	struct ipv6_opt_hdr	*hopopt;
2131da177e4SLinus Torvalds 	struct ipv6_opt_hdr	*dst0opt;
2141da177e4SLinus Torvalds 	struct ipv6_rt_hdr	*srcrt;	/* Routing Header */
2151da177e4SLinus Torvalds 	struct ipv6_opt_hdr	*dst1opt;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	/* Option buffer, as read by IPV6_PKTOPTIONS, starts here. */
2181da177e4SLinus Torvalds };
2191da177e4SLinus Torvalds 
220fd2c3ef7SEric Dumazet struct ip6_flowlabel {
2217f0e44acSEric Dumazet 	struct ip6_flowlabel __rcu *next;
22290bcaf7bSAl Viro 	__be32			label;
223db3459d1SEric Dumazet 	atomic_t		users;
2241da177e4SLinus Torvalds 	struct in6_addr		dst;
2251da177e4SLinus Torvalds 	struct ipv6_txoptions	*opt;
2261da177e4SLinus Torvalds 	unsigned long		linger;
227d3aedd5eSYOSHIFUJI Hideaki / 吉藤英明 	struct rcu_head		rcu;
2281da177e4SLinus Torvalds 	u8			share;
2294f82f457SEric W. Biederman 	union {
2304f82f457SEric W. Biederman 		struct pid *pid;
2314f82f457SEric W. Biederman 		kuid_t uid;
2324f82f457SEric W. Biederman 	} owner;
2331da177e4SLinus Torvalds 	unsigned long		lastuse;
2341da177e4SLinus Torvalds 	unsigned long		expires;
23560e8fbc4SBenjamin Thery 	struct net		*fl_net;
2361da177e4SLinus Torvalds };
2371da177e4SLinus Torvalds 
238f3a7c66bSHarvey Harrison #define IPV6_FLOWINFO_MASK	cpu_to_be32(0x0FFFFFFF)
239f3a7c66bSHarvey Harrison #define IPV6_FLOWLABEL_MASK	cpu_to_be32(0x000FFFFF)
2401da177e4SLinus Torvalds 
241fd2c3ef7SEric Dumazet struct ipv6_fl_socklist {
2427f0e44acSEric Dumazet 	struct ipv6_fl_socklist	__rcu	*next;
2431da177e4SLinus Torvalds 	struct ip6_flowlabel		*fl;
24418367681SYOSHIFUJI Hideaki / 吉藤英明 	struct rcu_head			rcu;
2451da177e4SLinus Torvalds };
2461da177e4SLinus Torvalds 
247*5c3a0fd7SJoe Perches struct ip6_flowlabel *fl6_sock_lookup(struct sock *sk, __be32 label);
248*5c3a0fd7SJoe Perches struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions *opt_space,
2491da177e4SLinus Torvalds 					 struct ip6_flowlabel *fl,
2501da177e4SLinus Torvalds 					 struct ipv6_txoptions *fopt);
251*5c3a0fd7SJoe Perches void fl6_free_socklist(struct sock *sk);
252*5c3a0fd7SJoe Perches int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen);
253*5c3a0fd7SJoe Perches int ip6_flowlabel_init(void);
254*5c3a0fd7SJoe Perches void ip6_flowlabel_cleanup(void);
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds static inline void fl6_sock_release(struct ip6_flowlabel *fl)
2571da177e4SLinus Torvalds {
2581da177e4SLinus Torvalds 	if (fl)
2591da177e4SLinus Torvalds 		atomic_dec(&fl->users);
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
262*5c3a0fd7SJoe Perches void icmpv6_notify(struct sk_buff *skb, u8 type, u8 code, __be32 info);
263b94f1c09SDavid S. Miller 
2646d0bfe22SLorenzo Colitti int icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
2656d0bfe22SLorenzo Colitti 			       struct icmp6hdr *thdr, int len);
2666d0bfe22SLorenzo Colitti 
2676d0bfe22SLorenzo Colitti struct dst_entry *icmpv6_route_lookup(struct net *net, struct sk_buff *skb,
2686d0bfe22SLorenzo Colitti 				      struct sock *sk, struct flowi6 *fl6);
2696d0bfe22SLorenzo Colitti 
270*5c3a0fd7SJoe Perches int ip6_ra_control(struct sock *sk, int sel);
2711da177e4SLinus Torvalds 
272*5c3a0fd7SJoe Perches int ipv6_parse_hopopts(struct sk_buff *skb);
2731da177e4SLinus Torvalds 
274*5c3a0fd7SJoe Perches struct ipv6_txoptions *ipv6_dup_options(struct sock *sk,
275*5c3a0fd7SJoe Perches 					struct ipv6_txoptions *opt);
276*5c3a0fd7SJoe Perches struct ipv6_txoptions *ipv6_renew_options(struct sock *sk,
277*5c3a0fd7SJoe Perches 					  struct ipv6_txoptions *opt,
278333fad53SYOSHIFUJI Hideaki 					  int newtype,
279333fad53SYOSHIFUJI Hideaki 					  struct ipv6_opt_hdr __user *newopt,
280333fad53SYOSHIFUJI Hideaki 					  int newoptlen);
281df9890c3SYOSHIFUJI Hideaki struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space,
282df9890c3SYOSHIFUJI Hideaki 					  struct ipv6_txoptions *opt);
2831da177e4SLinus Torvalds 
284*5c3a0fd7SJoe Perches bool ipv6_opt_accepted(const struct sock *sk, const struct sk_buff *skb);
285399c07deSArnaldo Carvalho de Melo 
286aeaf6e9dSShmulik Ladkani static inline bool ipv6_accept_ra(struct inet6_dev *idev)
287aeaf6e9dSShmulik Ladkani {
288aeaf6e9dSShmulik Ladkani 	/* If forwarding is enabled, RA are not accepted unless the special
289aeaf6e9dSShmulik Ladkani 	 * hybrid mode (accept_ra=2) is enabled.
290aeaf6e9dSShmulik Ladkani 	 */
291aeaf6e9dSShmulik Ladkani 	return idev->cnf.forwarding ? idev->cnf.accept_ra == 2 :
292aeaf6e9dSShmulik Ladkani 	    idev->cnf.accept_ra;
293aeaf6e9dSShmulik Ladkani }
294aeaf6e9dSShmulik Ladkani 
295d4915c08SAmerigo Wang #if IS_ENABLED(CONFIG_IPV6)
296d4915c08SAmerigo Wang static inline int ip6_frag_nqueues(struct net *net)
297d4915c08SAmerigo Wang {
298d4915c08SAmerigo Wang 	return net->ipv6.frags.nqueues;
299d4915c08SAmerigo Wang }
300d4915c08SAmerigo Wang 
301d4915c08SAmerigo Wang static inline int ip6_frag_mem(struct net *net)
302d4915c08SAmerigo Wang {
303d433673eSJesper Dangaard Brouer 	return sum_frag_mem_limit(&net->ipv6.frags);
304d4915c08SAmerigo Wang }
305d4915c08SAmerigo Wang #endif
3061da177e4SLinus Torvalds 
307c2a93660SJesper Dangaard Brouer #define IPV6_FRAG_HIGH_THRESH	(4 * 1024*1024)	/* 4194304 */
308c2a93660SJesper Dangaard Brouer #define IPV6_FRAG_LOW_THRESH	(3 * 1024*1024)	/* 3145728 */
3091da177e4SLinus Torvalds #define IPV6_FRAG_TIMEOUT	(60 * HZ)	/* 60 seconds */
3101da177e4SLinus Torvalds 
311*5c3a0fd7SJoe Perches int __ipv6_addr_type(const struct in6_addr *addr);
312b1cacb68SYOSHIFUJI Hideaki static inline int ipv6_addr_type(const struct in6_addr *addr)
313b1cacb68SYOSHIFUJI Hideaki {
314b1cacb68SYOSHIFUJI Hideaki 	return __ipv6_addr_type(addr) & 0xffff;
315b1cacb68SYOSHIFUJI Hideaki }
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds static inline int ipv6_addr_scope(const struct in6_addr *addr)
3181da177e4SLinus Torvalds {
319b1cacb68SYOSHIFUJI Hideaki 	return __ipv6_addr_type(addr) & IPV6_ADDR_SCOPE_MASK;
320b1cacb68SYOSHIFUJI Hideaki }
321b1cacb68SYOSHIFUJI Hideaki 
322b1cacb68SYOSHIFUJI Hideaki static inline int __ipv6_addr_src_scope(int type)
323b1cacb68SYOSHIFUJI Hideaki {
324a02cec21SEric Dumazet 	return (type == IPV6_ADDR_ANY) ? __IPV6_ADDR_SCOPE_INVALID : (type >> 16);
325b1cacb68SYOSHIFUJI Hideaki }
326b1cacb68SYOSHIFUJI Hideaki 
327b1cacb68SYOSHIFUJI Hideaki static inline int ipv6_addr_src_scope(const struct in6_addr *addr)
328b1cacb68SYOSHIFUJI Hideaki {
329b1cacb68SYOSHIFUJI Hideaki 	return __ipv6_addr_src_scope(__ipv6_addr_type(addr));
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
332b7ef213eSHannes Frederic Sowa static inline bool __ipv6_addr_needs_scope_id(int type)
333b7ef213eSHannes Frederic Sowa {
334b7ef213eSHannes Frederic Sowa 	return type & IPV6_ADDR_LINKLOCAL ||
335b7ef213eSHannes Frederic Sowa 	       (type & IPV6_ADDR_MULTICAST &&
336b7ef213eSHannes Frederic Sowa 		(type & (IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL)));
337b7ef213eSHannes Frederic Sowa }
338b7ef213eSHannes Frederic Sowa 
339b7ef213eSHannes Frederic Sowa static inline __u32 ipv6_iface_scope_id(const struct in6_addr *addr, int iface)
340b7ef213eSHannes Frederic Sowa {
341b7ef213eSHannes Frederic Sowa 	return __ipv6_addr_needs_scope_id(__ipv6_addr_type(addr)) ? iface : 0;
342b7ef213eSHannes Frederic Sowa }
343b7ef213eSHannes Frederic Sowa 
3441da177e4SLinus Torvalds static inline int ipv6_addr_cmp(const struct in6_addr *a1, const struct in6_addr *a2)
3451da177e4SLinus Torvalds {
346db3459d1SEric Dumazet 	return memcmp(a1, a2, sizeof(struct in6_addr));
3471da177e4SLinus Torvalds }
3481da177e4SLinus Torvalds 
3491a203cb3SEric Dumazet static inline bool
350f2ffd9eeSPatrick McHardy ipv6_masked_addr_cmp(const struct in6_addr *a1, const struct in6_addr *m,
351f2ffd9eeSPatrick McHardy 		     const struct in6_addr *a2)
352f2ffd9eeSPatrick McHardy {
3531a203cb3SEric Dumazet #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
3541a203cb3SEric Dumazet 	const unsigned long *ul1 = (const unsigned long *)a1;
3551a203cb3SEric Dumazet 	const unsigned long *ulm = (const unsigned long *)m;
3561a203cb3SEric Dumazet 	const unsigned long *ul2 = (const unsigned long *)a2;
3571a203cb3SEric Dumazet 
3581a203cb3SEric Dumazet 	return !!(((ul1[0] ^ ul2[0]) & ulm[0]) |
3591a203cb3SEric Dumazet 		  ((ul1[1] ^ ul2[1]) & ulm[1]));
3601a203cb3SEric Dumazet #else
361a02cec21SEric Dumazet 	return !!(((a1->s6_addr32[0] ^ a2->s6_addr32[0]) & m->s6_addr32[0]) |
362fed85383SYOSHIFUJI Hideaki 		  ((a1->s6_addr32[1] ^ a2->s6_addr32[1]) & m->s6_addr32[1]) |
363fed85383SYOSHIFUJI Hideaki 		  ((a1->s6_addr32[2] ^ a2->s6_addr32[2]) & m->s6_addr32[2]) |
364a02cec21SEric Dumazet 		  ((a1->s6_addr32[3] ^ a2->s6_addr32[3]) & m->s6_addr32[3]));
3651a203cb3SEric Dumazet #endif
366f2ffd9eeSPatrick McHardy }
367f2ffd9eeSPatrick McHardy 
3681da177e4SLinus Torvalds static inline void ipv6_addr_prefix(struct in6_addr *pfx,
3691da177e4SLinus Torvalds 				    const struct in6_addr *addr,
3701da177e4SLinus Torvalds 				    int plen)
3711da177e4SLinus Torvalds {
3721da177e4SLinus Torvalds 	/* caller must guarantee 0 <= plen <= 128 */
3731da177e4SLinus Torvalds 	int o = plen >> 3,
3741da177e4SLinus Torvalds 	    b = plen & 0x7;
3751da177e4SLinus Torvalds 
376db3459d1SEric Dumazet 	memset(pfx->s6_addr, 0, sizeof(pfx->s6_addr));
3771da177e4SLinus Torvalds 	memcpy(pfx->s6_addr, addr, o);
378db3459d1SEric Dumazet 	if (b != 0)
3791da177e4SLinus Torvalds 		pfx->s6_addr[o] = addr->s6_addr[o] & (0xff00 >> b);
3801da177e4SLinus Torvalds }
3811da177e4SLinus Torvalds 
3825206c579SYOSHIFUJI Hideaki / 吉藤英明 static inline void __ipv6_addr_set_half(__be32 *addr,
3835206c579SYOSHIFUJI Hideaki / 吉藤英明 					__be32 wh, __be32 wl)
3845206c579SYOSHIFUJI Hideaki / 吉藤英明 {
3855206c579SYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
3865206c579SYOSHIFUJI Hideaki / 吉藤英明 #if defined(__BIG_ENDIAN)
3875206c579SYOSHIFUJI Hideaki / 吉藤英明 	if (__builtin_constant_p(wh) && __builtin_constant_p(wl)) {
3885206c579SYOSHIFUJI Hideaki / 吉藤英明 		*(__force u64 *)addr = ((__force u64)(wh) << 32 | (__force u64)(wl));
3895206c579SYOSHIFUJI Hideaki / 吉藤英明 		return;
3905206c579SYOSHIFUJI Hideaki / 吉藤英明 	}
3915206c579SYOSHIFUJI Hideaki / 吉藤英明 #elif defined(__LITTLE_ENDIAN)
3925206c579SYOSHIFUJI Hideaki / 吉藤英明 	if (__builtin_constant_p(wl) && __builtin_constant_p(wh)) {
3935206c579SYOSHIFUJI Hideaki / 吉藤英明 		*(__force u64 *)addr = ((__force u64)(wl) << 32 | (__force u64)(wh));
3945206c579SYOSHIFUJI Hideaki / 吉藤英明 		return;
3955206c579SYOSHIFUJI Hideaki / 吉藤英明 	}
3965206c579SYOSHIFUJI Hideaki / 吉藤英明 #endif
3975206c579SYOSHIFUJI Hideaki / 吉藤英明 #endif
3985206c579SYOSHIFUJI Hideaki / 吉藤英明 	addr[0] = wh;
3995206c579SYOSHIFUJI Hideaki / 吉藤英明 	addr[1] = wl;
4005206c579SYOSHIFUJI Hideaki / 吉藤英明 }
4015206c579SYOSHIFUJI Hideaki / 吉藤英明 
4021da177e4SLinus Torvalds static inline void ipv6_addr_set(struct in6_addr *addr,
40348818f82SAl Viro 				     __be32 w1, __be32 w2,
40448818f82SAl Viro 				     __be32 w3, __be32 w4)
4051da177e4SLinus Torvalds {
4065206c579SYOSHIFUJI Hideaki / 吉藤英明 	__ipv6_addr_set_half(&addr->s6_addr32[0], w1, w2);
4075206c579SYOSHIFUJI Hideaki / 吉藤英明 	__ipv6_addr_set_half(&addr->s6_addr32[2], w3, w4);
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds 
41092113bfdSEric Dumazet static inline bool ipv6_addr_equal(const struct in6_addr *a1,
4111da177e4SLinus Torvalds 				   const struct in6_addr *a2)
4121da177e4SLinus Torvalds {
4131a203cb3SEric Dumazet #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
4141a203cb3SEric Dumazet 	const unsigned long *ul1 = (const unsigned long *)a1;
4151a203cb3SEric Dumazet 	const unsigned long *ul2 = (const unsigned long *)a2;
4161a203cb3SEric Dumazet 
4171a203cb3SEric Dumazet 	return ((ul1[0] ^ ul2[0]) | (ul1[1] ^ ul2[1])) == 0UL;
4181a203cb3SEric Dumazet #else
419a02cec21SEric Dumazet 	return ((a1->s6_addr32[0] ^ a2->s6_addr32[0]) |
420fed85383SYOSHIFUJI Hideaki 		(a1->s6_addr32[1] ^ a2->s6_addr32[1]) |
421fed85383SYOSHIFUJI Hideaki 		(a1->s6_addr32[2] ^ a2->s6_addr32[2]) |
422a02cec21SEric Dumazet 		(a1->s6_addr32[3] ^ a2->s6_addr32[3])) == 0;
4231a203cb3SEric Dumazet #endif
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
42638675170SYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
42738675170SYOSHIFUJI Hideaki / 吉藤英明 static inline bool __ipv6_prefix_equal64_half(const __be64 *a1,
42838675170SYOSHIFUJI Hideaki / 吉藤英明 					      const __be64 *a2,
42938675170SYOSHIFUJI Hideaki / 吉藤英明 					      unsigned int len)
43038675170SYOSHIFUJI Hideaki / 吉藤英明 {
431512613d7SFabio Baltieri 	if (len && ((*a1 ^ *a2) & cpu_to_be64((~0UL) << (64 - len))))
43238675170SYOSHIFUJI Hideaki / 吉藤英明 		return false;
43338675170SYOSHIFUJI Hideaki / 吉藤英明 	return true;
43438675170SYOSHIFUJI Hideaki / 吉藤英明 }
43538675170SYOSHIFUJI Hideaki / 吉藤英明 
43638675170SYOSHIFUJI Hideaki / 吉藤英明 static inline bool ipv6_prefix_equal(const struct in6_addr *addr1,
43738675170SYOSHIFUJI Hideaki / 吉藤英明 				     const struct in6_addr *addr2,
43838675170SYOSHIFUJI Hideaki / 吉藤英明 				     unsigned int prefixlen)
43938675170SYOSHIFUJI Hideaki / 吉藤英明 {
44038675170SYOSHIFUJI Hideaki / 吉藤英明 	const __be64 *a1 = (const __be64 *)addr1;
44138675170SYOSHIFUJI Hideaki / 吉藤英明 	const __be64 *a2 = (const __be64 *)addr2;
44238675170SYOSHIFUJI Hideaki / 吉藤英明 
44338675170SYOSHIFUJI Hideaki / 吉藤英明 	if (prefixlen >= 64) {
44438675170SYOSHIFUJI Hideaki / 吉藤英明 		if (a1[0] ^ a2[0])
44538675170SYOSHIFUJI Hideaki / 吉藤英明 			return false;
44638675170SYOSHIFUJI Hideaki / 吉藤英明 		return __ipv6_prefix_equal64_half(a1 + 1, a2 + 1, prefixlen - 64);
44738675170SYOSHIFUJI Hideaki / 吉藤英明 	}
44838675170SYOSHIFUJI Hideaki / 吉藤英明 	return __ipv6_prefix_equal64_half(a1, a2, prefixlen);
44938675170SYOSHIFUJI Hideaki / 吉藤英明 }
45038675170SYOSHIFUJI Hideaki / 吉藤英明 #else
4512ef97332SYOSHIFUJI Hideaki / 吉藤英明 static inline bool ipv6_prefix_equal(const struct in6_addr *addr1,
4522ef97332SYOSHIFUJI Hideaki / 吉藤英明 				     const struct in6_addr *addr2,
4531da177e4SLinus Torvalds 				     unsigned int prefixlen)
4541da177e4SLinus Torvalds {
4552ef97332SYOSHIFUJI Hideaki / 吉藤英明 	const __be32 *a1 = addr1->s6_addr32;
4562ef97332SYOSHIFUJI Hideaki / 吉藤英明 	const __be32 *a2 = addr2->s6_addr32;
45795c96174SEric Dumazet 	unsigned int pdw, pbi;
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	/* check complete u32 in prefix */
4601da177e4SLinus Torvalds 	pdw = prefixlen >> 5;
4611da177e4SLinus Torvalds 	if (pdw && memcmp(a1, a2, pdw << 2))
46292113bfdSEric Dumazet 		return false;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	/* check incomplete u32 in prefix */
4651da177e4SLinus Torvalds 	pbi = prefixlen & 0x1f;
4661da177e4SLinus Torvalds 	if (pbi && ((a1[pdw] ^ a2[pdw]) & htonl((0xffffffff) << (32 - pbi))))
46792113bfdSEric Dumazet 		return false;
4681da177e4SLinus Torvalds 
46992113bfdSEric Dumazet 	return true;
4701da177e4SLinus Torvalds }
47138675170SYOSHIFUJI Hideaki / 吉藤英明 #endif
4721da177e4SLinus Torvalds 
4732588fe1dSPavel Emelyanov struct inet_frag_queue;
4742588fe1dSPavel Emelyanov 
4750b5ccb2eSPatrick McHardy enum ip6_defrag_users {
4760b5ccb2eSPatrick McHardy 	IP6_DEFRAG_LOCAL_DELIVER,
4770b5ccb2eSPatrick McHardy 	IP6_DEFRAG_CONNTRACK_IN,
4784be929beSAlexey Dobriyan 	__IP6_DEFRAG_CONNTRACK_IN	= IP6_DEFRAG_CONNTRACK_IN + USHRT_MAX,
4790b5ccb2eSPatrick McHardy 	IP6_DEFRAG_CONNTRACK_OUT,
4804be929beSAlexey Dobriyan 	__IP6_DEFRAG_CONNTRACK_OUT	= IP6_DEFRAG_CONNTRACK_OUT + USHRT_MAX,
4818fa9ff68SPatrick McHardy 	IP6_DEFRAG_CONNTRACK_BRIDGE_IN,
4824be929beSAlexey Dobriyan 	__IP6_DEFRAG_CONNTRACK_BRIDGE_IN = IP6_DEFRAG_CONNTRACK_BRIDGE_IN + USHRT_MAX,
4830b5ccb2eSPatrick McHardy };
4840b5ccb2eSPatrick McHardy 
485c6fda282SPavel Emelyanov struct ip6_create_arg {
486c6fda282SPavel Emelyanov 	__be32 id;
4870b5ccb2eSPatrick McHardy 	u32 user;
488b71d1d42SEric Dumazet 	const struct in6_addr *src;
489b71d1d42SEric Dumazet 	const struct in6_addr *dst;
490eec2e618SHannes Frederic Sowa 	u8 ecn;
491c6fda282SPavel Emelyanov };
492c6fda282SPavel Emelyanov 
493c6fda282SPavel Emelyanov void ip6_frag_init(struct inet_frag_queue *q, void *a);
494cbc264caSEric Dumazet bool ip6_frag_match(struct inet_frag_queue *q, void *a);
495c6fda282SPavel Emelyanov 
496b836c99fSAmerigo Wang /*
497b836c99fSAmerigo Wang  *	Equivalent of ipv4 struct ip
498b836c99fSAmerigo Wang  */
499b836c99fSAmerigo Wang struct frag_queue {
500b836c99fSAmerigo Wang 	struct inet_frag_queue	q;
501b836c99fSAmerigo Wang 
502b836c99fSAmerigo Wang 	__be32			id;		/* fragment id		*/
503b836c99fSAmerigo Wang 	u32			user;
504b836c99fSAmerigo Wang 	struct in6_addr		saddr;
505b836c99fSAmerigo Wang 	struct in6_addr		daddr;
506b836c99fSAmerigo Wang 
507b836c99fSAmerigo Wang 	int			iif;
508b836c99fSAmerigo Wang 	unsigned int		csum;
509b836c99fSAmerigo Wang 	__u16			nhoffset;
510eec2e618SHannes Frederic Sowa 	u8			ecn;
511b836c99fSAmerigo Wang };
512b836c99fSAmerigo Wang 
513b836c99fSAmerigo Wang void ip6_expire_frag_queue(struct net *net, struct frag_queue *fq,
514b836c99fSAmerigo Wang 			   struct inet_frags *frags);
515b836c99fSAmerigo Wang 
51692113bfdSEric Dumazet static inline bool ipv6_addr_any(const struct in6_addr *a)
5171da177e4SLinus Torvalds {
5181a203cb3SEric Dumazet #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
5191a203cb3SEric Dumazet 	const unsigned long *ul = (const unsigned long *)a;
5201a203cb3SEric Dumazet 
5211a203cb3SEric Dumazet 	return (ul[0] | ul[1]) == 0UL;
5221a203cb3SEric Dumazet #else
523a02cec21SEric Dumazet 	return (a->s6_addr32[0] | a->s6_addr32[1] |
524a02cec21SEric Dumazet 		a->s6_addr32[2] | a->s6_addr32[3]) == 0;
5251a203cb3SEric Dumazet #endif
5261da177e4SLinus Torvalds }
5271da177e4SLinus Torvalds 
528ddbe5032SEric Dumazet static inline u32 ipv6_addr_hash(const struct in6_addr *a)
529ddbe5032SEric Dumazet {
530ddbe5032SEric Dumazet #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
531ddbe5032SEric Dumazet 	const unsigned long *ul = (const unsigned long *)a;
532ddbe5032SEric Dumazet 	unsigned long x = ul[0] ^ ul[1];
533ddbe5032SEric Dumazet 
534ddbe5032SEric Dumazet 	return (u32)(x ^ (x >> 32));
535ddbe5032SEric Dumazet #else
536ddbe5032SEric Dumazet 	return (__force u32)(a->s6_addr32[0] ^ a->s6_addr32[1] ^
537ddbe5032SEric Dumazet 			     a->s6_addr32[2] ^ a->s6_addr32[3]);
538ddbe5032SEric Dumazet #endif
539ddbe5032SEric Dumazet }
540ddbe5032SEric Dumazet 
54108dcdbf6SEric Dumazet /* more secured version of ipv6_addr_hash() */
54208dcdbf6SEric Dumazet static inline u32 ipv6_addr_jhash(const struct in6_addr *a)
54308dcdbf6SEric Dumazet {
54408dcdbf6SEric Dumazet 	u32 v = (__force u32)a->s6_addr32[0] ^ (__force u32)a->s6_addr32[1];
54508dcdbf6SEric Dumazet 
54608dcdbf6SEric Dumazet 	return jhash_3words(v,
54708dcdbf6SEric Dumazet 			    (__force u32)a->s6_addr32[2],
54808dcdbf6SEric Dumazet 			    (__force u32)a->s6_addr32[3],
54908dcdbf6SEric Dumazet 			    ipv6_hash_secret);
55008dcdbf6SEric Dumazet }
55108dcdbf6SEric Dumazet 
55292113bfdSEric Dumazet static inline bool ipv6_addr_loopback(const struct in6_addr *a)
553f630e43aSYOSHIFUJI Hideaki {
554e287656bSYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
555e287656bSYOSHIFUJI Hideaki / 吉藤英明 	const unsigned long *ul = (const unsigned long *)a;
556e287656bSYOSHIFUJI Hideaki / 吉藤英明 
557e287656bSYOSHIFUJI Hideaki / 吉藤英明 	return (ul[0] | (ul[1] ^ cpu_to_be64(1))) == 0UL;
558e287656bSYOSHIFUJI Hideaki / 吉藤英明 #else
559a02cec21SEric Dumazet 	return (a->s6_addr32[0] | a->s6_addr32[1] |
560a02cec21SEric Dumazet 		a->s6_addr32[2] | (a->s6_addr32[3] ^ htonl(1))) == 0;
561e287656bSYOSHIFUJI Hideaki / 吉藤英明 #endif
562f630e43aSYOSHIFUJI Hideaki }
563f630e43aSYOSHIFUJI Hideaki 
56492113bfdSEric Dumazet static inline bool ipv6_addr_v4mapped(const struct in6_addr *a)
565e773e4faSBrian Haley {
566a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 	return (
567a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
568a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 		*(__be64 *)a |
569a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 #else
570a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 		(a->s6_addr32[0] | a->s6_addr32[1]) |
571a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 #endif
572a04d40b8SYOSHIFUJI Hideaki / 吉藤英明 		(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0UL;
573e773e4faSBrian Haley }
574e773e4faSBrian Haley 
5751da177e4SLinus Torvalds /*
57699cd07a5SJuha-Matti Tapio  * Check for a RFC 4843 ORCHID address
57799cd07a5SJuha-Matti Tapio  * (Overlay Routable Cryptographic Hash Identifiers)
57899cd07a5SJuha-Matti Tapio  */
57992113bfdSEric Dumazet static inline bool ipv6_addr_orchid(const struct in6_addr *a)
58099cd07a5SJuha-Matti Tapio {
581a02cec21SEric Dumazet 	return (a->s6_addr32[0] & htonl(0xfffffff0)) == htonl(0x20010010);
58299cd07a5SJuha-Matti Tapio }
58399cd07a5SJuha-Matti Tapio 
584f15364bdSAurélien Charbon static inline void ipv6_addr_set_v4mapped(const __be32 addr,
585f15364bdSAurélien Charbon 					  struct in6_addr *v4mapped)
586f15364bdSAurélien Charbon {
587f15364bdSAurélien Charbon 	ipv6_addr_set(v4mapped,
588f15364bdSAurélien Charbon 			0, 0,
589f15364bdSAurélien Charbon 			htonl(0x0000FFFF),
590f15364bdSAurélien Charbon 			addr);
591f15364bdSAurélien Charbon }
592f15364bdSAurélien Charbon 
59399cd07a5SJuha-Matti Tapio /*
594971f359dSYOSHIFUJI Hideaki  * find the first different bit between two addresses
595971f359dSYOSHIFUJI Hideaki  * length of address must be a multiple of 32bits
596971f359dSYOSHIFUJI Hideaki  */
5979f2e7334SYOSHIFUJI Hideaki / 吉藤英明 static inline int __ipv6_addr_diff32(const void *token1, const void *token2, int addrlen)
598971f359dSYOSHIFUJI Hideaki {
599ef296f56SAl Viro 	const __be32 *a1 = token1, *a2 = token2;
600971f359dSYOSHIFUJI Hideaki 	int i;
601971f359dSYOSHIFUJI Hideaki 
602971f359dSYOSHIFUJI Hideaki 	addrlen >>= 2;
603971f359dSYOSHIFUJI Hideaki 
604971f359dSYOSHIFUJI Hideaki 	for (i = 0; i < addrlen; i++) {
605ef296f56SAl Viro 		__be32 xb = a1[i] ^ a2[i];
606ef296f56SAl Viro 		if (xb)
607d57b8fb8SYOSHIFUJI Hideaki / 吉藤英明 			return i * 32 + 31 - __fls(ntohl(xb));
608971f359dSYOSHIFUJI Hideaki 	}
609971f359dSYOSHIFUJI Hideaki 
610971f359dSYOSHIFUJI Hideaki 	/*
611971f359dSYOSHIFUJI Hideaki 	 *	we should *never* get to this point since that
612971f359dSYOSHIFUJI Hideaki 	 *	would mean the addrs are equal
613971f359dSYOSHIFUJI Hideaki 	 *
614971f359dSYOSHIFUJI Hideaki 	 *	However, we do get to it 8) And exacly, when
615971f359dSYOSHIFUJI Hideaki 	 *	addresses are equal 8)
616971f359dSYOSHIFUJI Hideaki 	 *
617971f359dSYOSHIFUJI Hideaki 	 *	ip route add 1111::/128 via ...
618971f359dSYOSHIFUJI Hideaki 	 *	ip route add 1111::/64 via ...
619971f359dSYOSHIFUJI Hideaki 	 *	and we are here.
620971f359dSYOSHIFUJI Hideaki 	 *
621971f359dSYOSHIFUJI Hideaki 	 *	Ideally, this function should stop comparison
622971f359dSYOSHIFUJI Hideaki 	 *	at prefix length. It does not, but it is still OK,
623971f359dSYOSHIFUJI Hideaki 	 *	if returned value is greater than prefix length.
624971f359dSYOSHIFUJI Hideaki 	 *					--ANK (980803)
625971f359dSYOSHIFUJI Hideaki 	 */
626a02cec21SEric Dumazet 	return addrlen << 5;
627971f359dSYOSHIFUJI Hideaki }
628971f359dSYOSHIFUJI Hideaki 
6299f2e7334SYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
6309f2e7334SYOSHIFUJI Hideaki / 吉藤英明 static inline int __ipv6_addr_diff64(const void *token1, const void *token2, int addrlen)
6319f2e7334SYOSHIFUJI Hideaki / 吉藤英明 {
6329f2e7334SYOSHIFUJI Hideaki / 吉藤英明 	const __be64 *a1 = token1, *a2 = token2;
6339f2e7334SYOSHIFUJI Hideaki / 吉藤英明 	int i;
6349f2e7334SYOSHIFUJI Hideaki / 吉藤英明 
6359f2e7334SYOSHIFUJI Hideaki / 吉藤英明 	addrlen >>= 3;
6369f2e7334SYOSHIFUJI Hideaki / 吉藤英明 
6379f2e7334SYOSHIFUJI Hideaki / 吉藤英明 	for (i = 0; i < addrlen; i++) {
6389f2e7334SYOSHIFUJI Hideaki / 吉藤英明 		__be64 xb = a1[i] ^ a2[i];
6399f2e7334SYOSHIFUJI Hideaki / 吉藤英明 		if (xb)
6409f2e7334SYOSHIFUJI Hideaki / 吉藤英明 			return i * 64 + 63 - __fls(be64_to_cpu(xb));
6419f2e7334SYOSHIFUJI Hideaki / 吉藤英明 	}
6429f2e7334SYOSHIFUJI Hideaki / 吉藤英明 
6439f2e7334SYOSHIFUJI Hideaki / 吉藤英明 	return addrlen << 6;
6449f2e7334SYOSHIFUJI Hideaki / 吉藤英明 }
6459f2e7334SYOSHIFUJI Hideaki / 吉藤英明 #endif
6469f2e7334SYOSHIFUJI Hideaki / 吉藤英明 
6479f2e7334SYOSHIFUJI Hideaki / 吉藤英明 static inline int __ipv6_addr_diff(const void *token1, const void *token2, int addrlen)
6489f2e7334SYOSHIFUJI Hideaki / 吉藤英明 {
6499f2e7334SYOSHIFUJI Hideaki / 吉藤英明 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64
6509f2e7334SYOSHIFUJI Hideaki / 吉藤英明 	if (__builtin_constant_p(addrlen) && !(addrlen & 7))
6519f2e7334SYOSHIFUJI Hideaki / 吉藤英明 		return __ipv6_addr_diff64(token1, token2, addrlen);
6529f2e7334SYOSHIFUJI Hideaki / 吉藤英明 #endif
6539f2e7334SYOSHIFUJI Hideaki / 吉藤英明 	return __ipv6_addr_diff32(token1, token2, addrlen);
6549f2e7334SYOSHIFUJI Hideaki / 吉藤英明 }
6559f2e7334SYOSHIFUJI Hideaki / 吉藤英明 
656971f359dSYOSHIFUJI Hideaki static inline int ipv6_addr_diff(const struct in6_addr *a1, const struct in6_addr *a2)
657971f359dSYOSHIFUJI Hideaki {
658971f359dSYOSHIFUJI Hideaki 	return __ipv6_addr_diff(a1, a2, sizeof(struct in6_addr));
659971f359dSYOSHIFUJI Hideaki }
660971f359dSYOSHIFUJI Hideaki 
661*5c3a0fd7SJoe Perches void ipv6_select_ident(struct frag_hdr *fhdr, struct rt6_info *rt);
6627ea2f2c5SSridhar Samudrala 
663*5c3a0fd7SJoe Perches int ip6_dst_hoplimit(struct dst_entry *dst);
6643ce9b35fSCong Wang 
665971f359dSYOSHIFUJI Hideaki /*
6663e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明  *	Header manipulation
6673e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明  */
6683e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 static inline void ip6_flow_hdr(struct ipv6hdr *hdr, unsigned int tclass,
6693e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 				__be32 flowlabel)
6703e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 {
67107f623d3SYOSHIFUJI Hideaki 	*(__be32 *)hdr = htonl(0x60000000 | (tclass << 20)) | flowlabel;
6723e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 }
6733e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 
6746502ca52SYOSHIFUJI Hideaki / 吉藤英明 static inline __be32 ip6_flowinfo(const struct ipv6hdr *hdr)
6756502ca52SYOSHIFUJI Hideaki / 吉藤英明 {
6766502ca52SYOSHIFUJI Hideaki / 吉藤英明 	return *(__be32 *)hdr & IPV6_FLOWINFO_MASK;
6776502ca52SYOSHIFUJI Hideaki / 吉藤英明 }
6786502ca52SYOSHIFUJI Hideaki / 吉藤英明 
6793e4e4c1fSYOSHIFUJI Hideaki / 吉藤英明 /*
6801da177e4SLinus Torvalds  *	Prototypes exported by ipv6
6811da177e4SLinus Torvalds  */
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds /*
6841da177e4SLinus Torvalds  *	rcv function (called from netdevice level)
6851da177e4SLinus Torvalds  */
6861da177e4SLinus Torvalds 
687*5c3a0fd7SJoe Perches int ipv6_rcv(struct sk_buff *skb, struct net_device *dev,
688*5c3a0fd7SJoe Perches 	     struct packet_type *pt, struct net_device *orig_dev);
6891da177e4SLinus Torvalds 
690*5c3a0fd7SJoe Perches int ip6_rcv_finish(struct sk_buff *skb);
691b05e1066SPatrick McHardy 
6921da177e4SLinus Torvalds /*
6931da177e4SLinus Torvalds  *	upper-layer output functions
6941da177e4SLinus Torvalds  */
695*5c3a0fd7SJoe Perches int ip6_xmit(struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
696*5c3a0fd7SJoe Perches 	     struct ipv6_txoptions *opt, int tclass);
6971da177e4SLinus Torvalds 
698*5c3a0fd7SJoe Perches int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr);
6991da177e4SLinus Torvalds 
700*5c3a0fd7SJoe Perches int ip6_append_data(struct sock *sk,
701*5c3a0fd7SJoe Perches 		    int getfrag(void *from, char *to, int offset, int len,
702*5c3a0fd7SJoe Perches 				int odd, struct sk_buff *skb),
703*5c3a0fd7SJoe Perches 		    void *from, int length, int transhdrlen, int hlimit,
704*5c3a0fd7SJoe Perches 		    int tclass, struct ipv6_txoptions *opt, struct flowi6 *fl6,
705*5c3a0fd7SJoe Perches 		    struct rt6_info *rt, unsigned int flags, int dontfrag);
7061da177e4SLinus Torvalds 
707*5c3a0fd7SJoe Perches int ip6_push_pending_frames(struct sock *sk);
7081da177e4SLinus Torvalds 
709*5c3a0fd7SJoe Perches void ip6_flush_pending_frames(struct sock *sk);
7101da177e4SLinus Torvalds 
711*5c3a0fd7SJoe Perches int ip6_dst_lookup(struct sock *sk, struct dst_entry **dst, struct flowi6 *fl6);
712*5c3a0fd7SJoe Perches struct dst_entry *ip6_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
71368d0c6d3SDavid S. Miller 				      const struct in6_addr *final_dst,
714a1414715SDavid S. Miller 				      bool can_sleep);
715*5c3a0fd7SJoe Perches struct dst_entry *ip6_sk_dst_lookup_flow(struct sock *sk, struct flowi6 *fl6,
71668d0c6d3SDavid S. Miller 					 const struct in6_addr *final_dst,
717a1414715SDavid S. Miller 					 bool can_sleep);
718*5c3a0fd7SJoe Perches struct dst_entry *ip6_blackhole_route(struct net *net,
71969ead7afSDavid S. Miller 				      struct dst_entry *orig_dst);
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds /*
7221da177e4SLinus Torvalds  *	skb processing functions
7231da177e4SLinus Torvalds  */
7241da177e4SLinus Torvalds 
725*5c3a0fd7SJoe Perches int ip6_output(struct sk_buff *skb);
726*5c3a0fd7SJoe Perches int ip6_forward(struct sk_buff *skb);
727*5c3a0fd7SJoe Perches int ip6_input(struct sk_buff *skb);
728*5c3a0fd7SJoe Perches int ip6_mc_input(struct sk_buff *skb);
7291da177e4SLinus Torvalds 
730*5c3a0fd7SJoe Perches int __ip6_local_out(struct sk_buff *skb);
731*5c3a0fd7SJoe Perches int ip6_local_out(struct sk_buff *skb);
732ef76bc23SHerbert Xu 
7331da177e4SLinus Torvalds /*
7341da177e4SLinus Torvalds  *	Extension header (options) processing
7351da177e4SLinus Torvalds  */
7361da177e4SLinus Torvalds 
737*5c3a0fd7SJoe Perches void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
738*5c3a0fd7SJoe Perches 			  u8 *proto, struct in6_addr **daddr_p);
739*5c3a0fd7SJoe Perches void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt,
7401da177e4SLinus Torvalds 			 u8 *proto);
7411da177e4SLinus Torvalds 
742*5c3a0fd7SJoe Perches int ipv6_skip_exthdr(const struct sk_buff *, int start, u8 *nexthdrp,
743*5c3a0fd7SJoe Perches 		     __be16 *frag_offp);
7441da177e4SLinus Torvalds 
745*5c3a0fd7SJoe Perches bool ipv6_ext_hdr(u8 nexthdr);
7461da177e4SLinus Torvalds 
747f8f62675SJesse Gross enum {
748f8f62675SJesse Gross 	IP6_FH_F_FRAG		= (1 << 0),
749f8f62675SJesse Gross 	IP6_FH_F_AUTH		= (1 << 1),
7509195bb8eSAnsis Atteka 	IP6_FH_F_SKIP_RH	= (1 << 2),
751f8f62675SJesse Gross };
752f8f62675SJesse Gross 
753f8f62675SJesse Gross /* find specified header and get offset to it */
754*5c3a0fd7SJoe Perches int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset, int target,
755*5c3a0fd7SJoe Perches 		  unsigned short *fragoff, int *fragflg);
756f8f62675SJesse Gross 
757*5c3a0fd7SJoe Perches int ipv6_find_tlv(struct sk_buff *skb, int offset, int type);
758c61a4043SMasahide NAKAMURA 
759*5c3a0fd7SJoe Perches struct in6_addr *fl6_update_dst(struct flowi6 *fl6,
76020c59de2SArnaud Ebalard 				const struct ipv6_txoptions *opt,
76120c59de2SArnaud Ebalard 				struct in6_addr *orig);
76220c59de2SArnaud Ebalard 
7631da177e4SLinus Torvalds /*
7641da177e4SLinus Torvalds  *	socket options (ipv6_sockglue.c)
7651da177e4SLinus Torvalds  */
7661da177e4SLinus Torvalds 
767*5c3a0fd7SJoe Perches int ipv6_setsockopt(struct sock *sk, int level, int optname,
768*5c3a0fd7SJoe Perches 		    char __user *optval, unsigned int optlen);
769*5c3a0fd7SJoe Perches int ipv6_getsockopt(struct sock *sk, int level, int optname,
770*5c3a0fd7SJoe Perches 		    char __user *optval, int __user *optlen);
771*5c3a0fd7SJoe Perches int compat_ipv6_setsockopt(struct sock *sk, int level, int optname,
772*5c3a0fd7SJoe Perches 			   char __user *optval, unsigned int optlen);
773*5c3a0fd7SJoe Perches int compat_ipv6_getsockopt(struct sock *sk, int level, int optname,
774*5c3a0fd7SJoe Perches 			   char __user *optval, int __user *optlen);
7751da177e4SLinus Torvalds 
776*5c3a0fd7SJoe Perches int ip6_datagram_connect(struct sock *sk, struct sockaddr *addr, int addr_len);
7771da177e4SLinus Torvalds 
778*5c3a0fd7SJoe Perches int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len);
779*5c3a0fd7SJoe Perches int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len);
780*5c3a0fd7SJoe Perches void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err, __be16 port,
7811da177e4SLinus Torvalds 		     u32 info, u8 *payload);
782*5c3a0fd7SJoe Perches void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info);
783*5c3a0fd7SJoe Perches void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu);
7841da177e4SLinus Torvalds 
785*5c3a0fd7SJoe Perches int inet6_release(struct socket *sock);
786*5c3a0fd7SJoe Perches int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len);
787*5c3a0fd7SJoe Perches int inet6_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len,
788*5c3a0fd7SJoe Perches 		  int peer);
789*5c3a0fd7SJoe Perches int inet6_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
7901da177e4SLinus Torvalds 
791*5c3a0fd7SJoe Perches int inet6_hash_connect(struct inet_timewait_death_row *death_row,
792d8313f5cSArnaldo Carvalho de Melo 			      struct sock *sk);
793d8313f5cSArnaldo Carvalho de Melo 
7941da177e4SLinus Torvalds /*
7951da177e4SLinus Torvalds  * reassembly.c
7961da177e4SLinus Torvalds  */
79790ddc4f0SEric Dumazet extern const struct proto_ops inet6_stream_ops;
79890ddc4f0SEric Dumazet extern const struct proto_ops inet6_dgram_ops;
79920380731SArnaldo Carvalho de Melo 
80014c85021SArnaldo Carvalho de Melo struct group_source_req;
80114c85021SArnaldo Carvalho de Melo struct group_filter;
80214c85021SArnaldo Carvalho de Melo 
803*5c3a0fd7SJoe Perches int ip6_mc_source(int add, int omode, struct sock *sk,
80420380731SArnaldo Carvalho de Melo 		  struct group_source_req *pgsr);
805*5c3a0fd7SJoe Perches int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf);
806*5c3a0fd7SJoe Perches int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf,
807*5c3a0fd7SJoe Perches 		  struct group_filter __user *optval, int __user *optlen);
808*5c3a0fd7SJoe Perches unsigned int inet6_hash_frag(__be32 id, const struct in6_addr *saddr,
80993c8b90fSIlpo Järvinen 			     const struct in6_addr *daddr, u32 rnd);
81020380731SArnaldo Carvalho de Melo 
81120380731SArnaldo Carvalho de Melo #ifdef CONFIG_PROC_FS
812*5c3a0fd7SJoe Perches int ac6_proc_init(struct net *net);
813*5c3a0fd7SJoe Perches void ac6_proc_exit(struct net *net);
814*5c3a0fd7SJoe Perches int raw6_proc_init(void);
815*5c3a0fd7SJoe Perches void raw6_proc_exit(void);
816*5c3a0fd7SJoe Perches int tcp6_proc_init(struct net *net);
817*5c3a0fd7SJoe Perches void tcp6_proc_exit(struct net *net);
818*5c3a0fd7SJoe Perches int udp6_proc_init(struct net *net);
819*5c3a0fd7SJoe Perches void udp6_proc_exit(struct net *net);
820*5c3a0fd7SJoe Perches int udplite6_proc_init(void);
821*5c3a0fd7SJoe Perches void udplite6_proc_exit(void);
822*5c3a0fd7SJoe Perches int ipv6_misc_proc_init(void);
823*5c3a0fd7SJoe Perches void ipv6_misc_proc_exit(void);
824*5c3a0fd7SJoe Perches int snmp6_register_dev(struct inet6_dev *idev);
825*5c3a0fd7SJoe Perches int snmp6_unregister_dev(struct inet6_dev *idev);
82620380731SArnaldo Carvalho de Melo 
8277f7d9a6bSHerbert Xu #else
8286ab57e7eSDaniel Lezcano static inline int ac6_proc_init(struct net *net) { return 0; }
8296ab57e7eSDaniel Lezcano static inline void ac6_proc_exit(struct net *net) { }
8306ab57e7eSDaniel Lezcano static inline int snmp6_register_dev(struct inet6_dev *idev) { return 0; }
8316ab57e7eSDaniel Lezcano static inline int snmp6_unregister_dev(struct inet6_dev *idev) { return 0; }
83220380731SArnaldo Carvalho de Melo #endif
83320380731SArnaldo Carvalho de Melo 
83420380731SArnaldo Carvalho de Melo #ifdef CONFIG_SYSCTL
8359e8cda3bSJoe Perches extern struct ctl_table ipv6_route_table_template[];
8369e8cda3bSJoe Perches extern struct ctl_table ipv6_icmp_table_template[];
83720380731SArnaldo Carvalho de Melo 
838*5c3a0fd7SJoe Perches struct ctl_table *ipv6_icmp_sysctl_init(struct net *net);
839*5c3a0fd7SJoe Perches struct ctl_table *ipv6_route_sysctl_init(struct net *net);
840*5c3a0fd7SJoe Perches int ipv6_sysctl_register(void);
841*5c3a0fd7SJoe Perches void ipv6_sysctl_unregister(void);
84220380731SArnaldo Carvalho de Melo #endif
84320380731SArnaldo Carvalho de Melo 
8441da177e4SLinus Torvalds #endif /* _NET_IPV6_H */
845