1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Based on include/net/inet6_hashtables.h
4 * Authors: Lotsa people, from code originally in tcp
5 *
6 * NewIP INET
7 * An implementation of the TCP/IP protocol suite for the LINUX
8 * operating system. NewIP INET is implemented using the BSD Socket
9 * interface as the means of communication with the user level.
10 */
11 #ifndef NINET_HASHTABLES_H
12 #define NINET_HASHTABLES_H
13
14 #if IS_ENABLED(CONFIG_NEWIP)
15 #include <linux/nip.h>
16 #include <linux/types.h>
17 #include <linux/jhash.h>
18
19 #include <net/inet_sock.h>
20
21 #include <net/nip.h>
22 #include <net/netns/hash.h>
23
24 struct inet_hashinfo;
25
26 int ninet_hash(struct sock *sk);
27 void ninet_unhash(struct sock *sk);
28 int ninet_hash_connect(struct inet_timewait_death_row *death_row,
29 struct sock *sk);
30
31 int __ninet_hash(struct sock *sk, struct sock *osk);
32
33
__ninet_ehashfn(const u32 lhash,const u16 lport,const u32 fhash,const __be16 fport,const u32 initval)34 static inline unsigned int __ninet_ehashfn(const u32 lhash,
35 const u16 lport,
36 const u32 fhash,
37 const __be16 fport,
38 const u32 initval)
39 {
40 const u32 ports = (((u32) lport) << 16) | (__force u32) fport;
41
42 return jhash_3words(lhash, fhash, ports, initval);
43 }
44
45 struct sock *__ninet_lookup_established(struct net *net,
46 struct inet_hashinfo *hashinfo,
47 const struct nip_addr *saddr,
48 const __be16 sport,
49 const struct nip_addr *daddr,
50 const u16 hnum, const int dif);
51
52 struct sock *ninet_lookup_listener(struct net *net,
53 struct inet_hashinfo *hashinfo,
54 struct sk_buff *skb, int doff,
55 const struct nip_addr *saddr,
56 const __be16 sport,
57 const struct nip_addr *daddr,
58 const unsigned short hnum, const int dif, const int sdif);
59
__ninet_lookup(struct net * net,struct inet_hashinfo * hashinfo,struct sk_buff * skb,int doff,const struct nip_addr * saddr,const __be16 sport,const struct nip_addr * daddr,const u16 hnum,const int dif,bool * refcounted)60 static inline struct sock *__ninet_lookup(struct net *net,
61 struct inet_hashinfo *hashinfo,
62 struct sk_buff *skb, int doff,
63 const struct nip_addr *saddr,
64 const __be16 sport,
65 const struct nip_addr *daddr,
66 const u16 hnum,
67 const int dif, bool *refcounted)
68 {
69 struct sock *sk = __ninet_lookup_established(net, hashinfo, saddr,
70 sport, daddr, hnum, dif);
71 *refcounted = true;
72 if (sk)
73 return sk;
74 *refcounted = false;
75 return ninet_lookup_listener(net, hashinfo, skb, doff, saddr, sport,
76 daddr, hnum, dif, 0);
77 }
78
__ninet_lookup_skb(struct inet_hashinfo * hashinfo,struct sk_buff * skb,int doff,const __be16 sport,const __be16 dport,int iif,bool * refcounted)79 static inline struct sock *__ninet_lookup_skb(struct inet_hashinfo *hashinfo,
80 struct sk_buff *skb, int doff,
81 const __be16 sport,
82 const __be16 dport,
83 int iif, bool *refcounted)
84 {
85 struct sock *sk;
86
87 *refcounted = true;
88 sk = skb_steal_sock(skb, refcounted);
89 if (sk)
90 return sk;
91
92 return __ninet_lookup(dev_net(skb->dev), hashinfo, skb,
93 doff, &(nipcb(skb)->srcaddr), sport,
94 &(nipcb(skb)->dstaddr), ntohs(dport),
95 iif, refcounted);
96 }
97
98 #define ninet_match(__sk, __net, __saddr, __daddr, __ports, __dif) \
99 (((__sk)->sk_portpair == (__ports)) && \
100 ((__sk)->sk_family == AF_NINET) && \
101 nip_addr_eq(&(__sk)->SK_NIP_DADDR, (__saddr)) && \
102 nip_addr_eq(&(__sk)->SK_NIP_RCV_SADDR, (__daddr)) && \
103 (!(__sk)->sk_bound_dev_if || \
104 ((__sk)->sk_bound_dev_if == (__dif))) && \
105 net_eq(sock_net(__sk), (__net)))
106
107 int ninet_hash_connect(struct inet_timewait_death_row *death_row,
108 struct sock *sk);
109
110 u64 secure_newip_port_ephemeral(const __be32 *saddr, const __be32 *daddr,
111 __be16 dport);
112 __u32 secure_tcp_nip_sequence_number(const __be32 *saddr, const __be32 *daddr,
113 __be16 sport, __be16 dport);
114
115 u32 ninet_ehashfn(const struct net *net,
116 const struct nip_addr *laddr, const u16 lport,
117 const struct nip_addr *faddr, const __be16 fport);
118
119 #endif /* IS_ENABLED(CONFIG_NEWIP) */
120 #endif /* _NINET_HASHTABLES_H */
121