18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * INET An implementation of the TCP/IP protocol suite for the LINUX 48c2ecf20Sopenharmony_ci * operating system. INET is implemented using the BSD Socket 58c2ecf20Sopenharmony_ci * interface as the means of communication with the user level. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Authors: Lotsa people, from code originally in tcp 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#ifndef _INET6_HASHTABLES_H 118c2ecf20Sopenharmony_ci#define _INET6_HASHTABLES_H 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_IPV6) 158c2ecf20Sopenharmony_ci#include <linux/in6.h> 168c2ecf20Sopenharmony_ci#include <linux/ipv6.h> 178c2ecf20Sopenharmony_ci#include <linux/types.h> 188c2ecf20Sopenharmony_ci#include <linux/jhash.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#include <net/inet_sock.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#include <net/ipv6.h> 238c2ecf20Sopenharmony_ci#include <net/netns/hash.h> 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_cistruct inet_hashinfo; 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_cistatic inline unsigned int __inet6_ehashfn(const u32 lhash, 288c2ecf20Sopenharmony_ci const u16 lport, 298c2ecf20Sopenharmony_ci const u32 fhash, 308c2ecf20Sopenharmony_ci const __be16 fport, 318c2ecf20Sopenharmony_ci const u32 initval) 328c2ecf20Sopenharmony_ci{ 338c2ecf20Sopenharmony_ci const u32 ports = (((u32)lport) << 16) | (__force u32)fport; 348c2ecf20Sopenharmony_ci return jhash_3words(lhash, fhash, ports, initval); 358c2ecf20Sopenharmony_ci} 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci/* 388c2ecf20Sopenharmony_ci * Sockets in TCP_CLOSE state are _always_ taken out of the hash, so 398c2ecf20Sopenharmony_ci * we need not check it for TCP lookups anymore, thanks Alexey. -DaveM 408c2ecf20Sopenharmony_ci * 418c2ecf20Sopenharmony_ci * The sockhash lock must be held as a reader here. 428c2ecf20Sopenharmony_ci */ 438c2ecf20Sopenharmony_cistruct sock *__inet6_lookup_established(struct net *net, 448c2ecf20Sopenharmony_ci struct inet_hashinfo *hashinfo, 458c2ecf20Sopenharmony_ci const struct in6_addr *saddr, 468c2ecf20Sopenharmony_ci const __be16 sport, 478c2ecf20Sopenharmony_ci const struct in6_addr *daddr, 488c2ecf20Sopenharmony_ci const u16 hnum, const int dif, 498c2ecf20Sopenharmony_ci const int sdif); 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_cistruct sock *inet6_lookup_listener(struct net *net, 528c2ecf20Sopenharmony_ci struct inet_hashinfo *hashinfo, 538c2ecf20Sopenharmony_ci struct sk_buff *skb, int doff, 548c2ecf20Sopenharmony_ci const struct in6_addr *saddr, 558c2ecf20Sopenharmony_ci const __be16 sport, 568c2ecf20Sopenharmony_ci const struct in6_addr *daddr, 578c2ecf20Sopenharmony_ci const unsigned short hnum, 588c2ecf20Sopenharmony_ci const int dif, const int sdif); 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_cistatic inline struct sock *__inet6_lookup(struct net *net, 618c2ecf20Sopenharmony_ci struct inet_hashinfo *hashinfo, 628c2ecf20Sopenharmony_ci struct sk_buff *skb, int doff, 638c2ecf20Sopenharmony_ci const struct in6_addr *saddr, 648c2ecf20Sopenharmony_ci const __be16 sport, 658c2ecf20Sopenharmony_ci const struct in6_addr *daddr, 668c2ecf20Sopenharmony_ci const u16 hnum, 678c2ecf20Sopenharmony_ci const int dif, const int sdif, 688c2ecf20Sopenharmony_ci bool *refcounted) 698c2ecf20Sopenharmony_ci{ 708c2ecf20Sopenharmony_ci struct sock *sk = __inet6_lookup_established(net, hashinfo, saddr, 718c2ecf20Sopenharmony_ci sport, daddr, hnum, 728c2ecf20Sopenharmony_ci dif, sdif); 738c2ecf20Sopenharmony_ci *refcounted = true; 748c2ecf20Sopenharmony_ci if (sk) 758c2ecf20Sopenharmony_ci return sk; 768c2ecf20Sopenharmony_ci *refcounted = false; 778c2ecf20Sopenharmony_ci return inet6_lookup_listener(net, hashinfo, skb, doff, saddr, sport, 788c2ecf20Sopenharmony_ci daddr, hnum, dif, sdif); 798c2ecf20Sopenharmony_ci} 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_cistatic inline struct sock *__inet6_lookup_skb(struct inet_hashinfo *hashinfo, 828c2ecf20Sopenharmony_ci struct sk_buff *skb, int doff, 838c2ecf20Sopenharmony_ci const __be16 sport, 848c2ecf20Sopenharmony_ci const __be16 dport, 858c2ecf20Sopenharmony_ci int iif, int sdif, 868c2ecf20Sopenharmony_ci bool *refcounted) 878c2ecf20Sopenharmony_ci{ 888c2ecf20Sopenharmony_ci struct sock *sk = skb_steal_sock(skb, refcounted); 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci if (sk) 918c2ecf20Sopenharmony_ci return sk; 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci return __inet6_lookup(dev_net(skb_dst(skb)->dev), hashinfo, skb, 948c2ecf20Sopenharmony_ci doff, &ipv6_hdr(skb)->saddr, sport, 958c2ecf20Sopenharmony_ci &ipv6_hdr(skb)->daddr, ntohs(dport), 968c2ecf20Sopenharmony_ci iif, sdif, refcounted); 978c2ecf20Sopenharmony_ci} 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_cistruct sock *inet6_lookup(struct net *net, struct inet_hashinfo *hashinfo, 1008c2ecf20Sopenharmony_ci struct sk_buff *skb, int doff, 1018c2ecf20Sopenharmony_ci const struct in6_addr *saddr, const __be16 sport, 1028c2ecf20Sopenharmony_ci const struct in6_addr *daddr, const __be16 dport, 1038c2ecf20Sopenharmony_ci const int dif); 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ciint inet6_hash(struct sock *sk); 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_cistatic inline bool inet6_match(struct net *net, const struct sock *sk, 1088c2ecf20Sopenharmony_ci const struct in6_addr *saddr, 1098c2ecf20Sopenharmony_ci const struct in6_addr *daddr, 1108c2ecf20Sopenharmony_ci const __portpair ports, 1118c2ecf20Sopenharmony_ci const int dif, const int sdif) 1128c2ecf20Sopenharmony_ci{ 1138c2ecf20Sopenharmony_ci if (!net_eq(sock_net(sk), net) || 1148c2ecf20Sopenharmony_ci sk->sk_family != AF_INET6 || 1158c2ecf20Sopenharmony_ci sk->sk_portpair != ports || 1168c2ecf20Sopenharmony_ci !ipv6_addr_equal(&sk->sk_v6_daddr, saddr) || 1178c2ecf20Sopenharmony_ci !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr)) 1188c2ecf20Sopenharmony_ci return false; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci /* READ_ONCE() paired with WRITE_ONCE() in sock_bindtoindex_locked() */ 1218c2ecf20Sopenharmony_ci return inet_sk_bound_dev_eq(net, READ_ONCE(sk->sk_bound_dev_if), dif, 1228c2ecf20Sopenharmony_ci sdif); 1238c2ecf20Sopenharmony_ci} 1248c2ecf20Sopenharmony_ci#endif /* IS_ENABLED(CONFIG_IPV6) */ 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci#endif /* _INET6_HASHTABLES_H */ 127