xref: /OK3568_Linux_fs/kernel/include/net/inet6_hashtables.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * INET		An implementation of the TCP/IP protocol suite for the LINUX
4*4882a593Smuzhiyun  *		operating system.  INET is implemented using the BSD Socket
5*4882a593Smuzhiyun  *		interface as the means of communication with the user level.
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Authors:	Lotsa people, from code originally in tcp
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #ifndef _INET6_HASHTABLES_H
11*4882a593Smuzhiyun #define _INET6_HASHTABLES_H
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_IPV6)
15*4882a593Smuzhiyun #include <linux/in6.h>
16*4882a593Smuzhiyun #include <linux/ipv6.h>
17*4882a593Smuzhiyun #include <linux/types.h>
18*4882a593Smuzhiyun #include <linux/jhash.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include <net/inet_sock.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #include <net/ipv6.h>
23*4882a593Smuzhiyun #include <net/netns/hash.h>
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun struct inet_hashinfo;
26*4882a593Smuzhiyun 
__inet6_ehashfn(const u32 lhash,const u16 lport,const u32 fhash,const __be16 fport,const u32 initval)27*4882a593Smuzhiyun static inline unsigned int __inet6_ehashfn(const u32 lhash,
28*4882a593Smuzhiyun 				    const u16 lport,
29*4882a593Smuzhiyun 				    const u32 fhash,
30*4882a593Smuzhiyun 				    const __be16 fport,
31*4882a593Smuzhiyun 				    const u32 initval)
32*4882a593Smuzhiyun {
33*4882a593Smuzhiyun 	const u32 ports = (((u32)lport) << 16) | (__force u32)fport;
34*4882a593Smuzhiyun 	return jhash_3words(lhash, fhash, ports, initval);
35*4882a593Smuzhiyun }
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun /*
38*4882a593Smuzhiyun  * Sockets in TCP_CLOSE state are _always_ taken out of the hash, so
39*4882a593Smuzhiyun  * we need not check it for TCP lookups anymore, thanks Alexey. -DaveM
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  * The sockhash lock must be held as a reader here.
42*4882a593Smuzhiyun  */
43*4882a593Smuzhiyun struct sock *__inet6_lookup_established(struct net *net,
44*4882a593Smuzhiyun 					struct inet_hashinfo *hashinfo,
45*4882a593Smuzhiyun 					const struct in6_addr *saddr,
46*4882a593Smuzhiyun 					const __be16 sport,
47*4882a593Smuzhiyun 					const struct in6_addr *daddr,
48*4882a593Smuzhiyun 					const u16 hnum, const int dif,
49*4882a593Smuzhiyun 					const int sdif);
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun struct sock *inet6_lookup_listener(struct net *net,
52*4882a593Smuzhiyun 				   struct inet_hashinfo *hashinfo,
53*4882a593Smuzhiyun 				   struct sk_buff *skb, int doff,
54*4882a593Smuzhiyun 				   const struct in6_addr *saddr,
55*4882a593Smuzhiyun 				   const __be16 sport,
56*4882a593Smuzhiyun 				   const struct in6_addr *daddr,
57*4882a593Smuzhiyun 				   const unsigned short hnum,
58*4882a593Smuzhiyun 				   const int dif, const int sdif);
59*4882a593Smuzhiyun 
__inet6_lookup(struct net * net,struct inet_hashinfo * hashinfo,struct sk_buff * skb,int doff,const struct in6_addr * saddr,const __be16 sport,const struct in6_addr * daddr,const u16 hnum,const int dif,const int sdif,bool * refcounted)60*4882a593Smuzhiyun static inline struct sock *__inet6_lookup(struct net *net,
61*4882a593Smuzhiyun 					  struct inet_hashinfo *hashinfo,
62*4882a593Smuzhiyun 					  struct sk_buff *skb, int doff,
63*4882a593Smuzhiyun 					  const struct in6_addr *saddr,
64*4882a593Smuzhiyun 					  const __be16 sport,
65*4882a593Smuzhiyun 					  const struct in6_addr *daddr,
66*4882a593Smuzhiyun 					  const u16 hnum,
67*4882a593Smuzhiyun 					  const int dif, const int sdif,
68*4882a593Smuzhiyun 					  bool *refcounted)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	struct sock *sk = __inet6_lookup_established(net, hashinfo, saddr,
71*4882a593Smuzhiyun 						     sport, daddr, hnum,
72*4882a593Smuzhiyun 						     dif, sdif);
73*4882a593Smuzhiyun 	*refcounted = true;
74*4882a593Smuzhiyun 	if (sk)
75*4882a593Smuzhiyun 		return sk;
76*4882a593Smuzhiyun 	*refcounted = false;
77*4882a593Smuzhiyun 	return inet6_lookup_listener(net, hashinfo, skb, doff, saddr, sport,
78*4882a593Smuzhiyun 				     daddr, hnum, dif, sdif);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun 
__inet6_lookup_skb(struct inet_hashinfo * hashinfo,struct sk_buff * skb,int doff,const __be16 sport,const __be16 dport,int iif,int sdif,bool * refcounted)81*4882a593Smuzhiyun static inline struct sock *__inet6_lookup_skb(struct inet_hashinfo *hashinfo,
82*4882a593Smuzhiyun 					      struct sk_buff *skb, int doff,
83*4882a593Smuzhiyun 					      const __be16 sport,
84*4882a593Smuzhiyun 					      const __be16 dport,
85*4882a593Smuzhiyun 					      int iif, int sdif,
86*4882a593Smuzhiyun 					      bool *refcounted)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	struct sock *sk = skb_steal_sock(skb, refcounted);
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	if (sk)
91*4882a593Smuzhiyun 		return sk;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	return __inet6_lookup(dev_net(skb_dst(skb)->dev), hashinfo, skb,
94*4882a593Smuzhiyun 			      doff, &ipv6_hdr(skb)->saddr, sport,
95*4882a593Smuzhiyun 			      &ipv6_hdr(skb)->daddr, ntohs(dport),
96*4882a593Smuzhiyun 			      iif, sdif, refcounted);
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun struct sock *inet6_lookup(struct net *net, struct inet_hashinfo *hashinfo,
100*4882a593Smuzhiyun 			  struct sk_buff *skb, int doff,
101*4882a593Smuzhiyun 			  const struct in6_addr *saddr, const __be16 sport,
102*4882a593Smuzhiyun 			  const struct in6_addr *daddr, const __be16 dport,
103*4882a593Smuzhiyun 			  const int dif);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun int inet6_hash(struct sock *sk);
106*4882a593Smuzhiyun 
inet6_match(struct net * net,const struct sock * sk,const struct in6_addr * saddr,const struct in6_addr * daddr,const __portpair ports,const int dif,const int sdif)107*4882a593Smuzhiyun static inline bool inet6_match(struct net *net, const struct sock *sk,
108*4882a593Smuzhiyun 			       const struct in6_addr *saddr,
109*4882a593Smuzhiyun 			       const struct in6_addr *daddr,
110*4882a593Smuzhiyun 			       const __portpair ports,
111*4882a593Smuzhiyun 			       const int dif, const int sdif)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun 	if (!net_eq(sock_net(sk), net) ||
114*4882a593Smuzhiyun 	    sk->sk_family != AF_INET6 ||
115*4882a593Smuzhiyun 	    sk->sk_portpair != ports ||
116*4882a593Smuzhiyun 	    !ipv6_addr_equal(&sk->sk_v6_daddr, saddr) ||
117*4882a593Smuzhiyun 	    !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
118*4882a593Smuzhiyun 		return false;
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	/* READ_ONCE() paired with WRITE_ONCE() in sock_bindtoindex_locked() */
121*4882a593Smuzhiyun 	return inet_sk_bound_dev_eq(net, READ_ONCE(sk->sk_bound_dev_if), dif,
122*4882a593Smuzhiyun 				    sdif);
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun #endif /* IS_ENABLED(CONFIG_IPV6) */
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun #endif /* _INET6_HASHTABLES_H */
127