Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * INET     An implementation of the TCP/IP protocol suite for the LINUX
0004  *      operating system.  INET is implemented using the BSD Socket
0005  *      interface as the means of communication with the user level.
0006  *
0007  *      Generic INET6 transport hashtables
0008  *
0009  * Authors: Lotsa people, from code originally in tcp, generalised here
0010  *      by Arnaldo Carvalho de Melo <acme@mandriva.com>
0011  */
0012 
0013 #include <linux/module.h>
0014 #include <linux/random.h>
0015 
0016 #include <net/addrconf.h>
0017 #include <net/inet_connection_sock.h>
0018 #include <net/inet_hashtables.h>
0019 #include <net/inet6_hashtables.h>
0020 #include <net/secure_seq.h>
0021 #include <net/ip.h>
0022 #include <net/sock_reuseport.h>
0023 
0024 extern struct inet_hashinfo tcp_hashinfo;
0025 
0026 u32 inet6_ehashfn(const struct net *net,
0027           const struct in6_addr *laddr, const u16 lport,
0028           const struct in6_addr *faddr, const __be16 fport)
0029 {
0030     static u32 inet6_ehash_secret __read_mostly;
0031     static u32 ipv6_hash_secret __read_mostly;
0032 
0033     u32 lhash, fhash;
0034 
0035     net_get_random_once(&inet6_ehash_secret, sizeof(inet6_ehash_secret));
0036     net_get_random_once(&ipv6_hash_secret, sizeof(ipv6_hash_secret));
0037 
0038     lhash = (__force u32)laddr->s6_addr32[3];
0039     fhash = __ipv6_addr_jhash(faddr, ipv6_hash_secret);
0040 
0041     return __inet6_ehashfn(lhash, lport, fhash, fport,
0042                    inet6_ehash_secret + net_hash_mix(net));
0043 }
0044 
0045 /*
0046  * Sockets in TCP_CLOSE state are _always_ taken out of the hash, so
0047  * we need not check it for TCP lookups anymore, thanks Alexey. -DaveM
0048  *
0049  * The sockhash lock must be held as a reader here.
0050  */
0051 struct sock *__inet6_lookup_established(struct net *net,
0052                     struct inet_hashinfo *hashinfo,
0053                        const struct in6_addr *saddr,
0054                        const __be16 sport,
0055                        const struct in6_addr *daddr,
0056                        const u16 hnum,
0057                        const int dif, const int sdif)
0058 {
0059     struct sock *sk;
0060     const struct hlist_nulls_node *node;
0061     const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
0062     /* Optimize here for direct hit, only listening connections can
0063      * have wildcards anyways.
0064      */
0065     unsigned int hash = inet6_ehashfn(net, daddr, hnum, saddr, sport);
0066     unsigned int slot = hash & hashinfo->ehash_mask;
0067     struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
0068 
0069 
0070 begin:
0071     sk_nulls_for_each_rcu(sk, node, &head->chain) {
0072         if (sk->sk_hash != hash)
0073             continue;
0074         if (!inet6_match(net, sk, saddr, daddr, ports, dif, sdif))
0075             continue;
0076         if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
0077             goto out;
0078 
0079         if (unlikely(!inet6_match(net, sk, saddr, daddr, ports, dif, sdif))) {
0080             sock_gen_put(sk);
0081             goto begin;
0082         }
0083         goto found;
0084     }
0085     if (get_nulls_value(node) != slot)
0086         goto begin;
0087 out:
0088     sk = NULL;
0089 found:
0090     return sk;
0091 }
0092 EXPORT_SYMBOL(__inet6_lookup_established);
0093 
0094 static inline int compute_score(struct sock *sk, struct net *net,
0095                 const unsigned short hnum,
0096                 const struct in6_addr *daddr,
0097                 const int dif, const int sdif)
0098 {
0099     int score = -1;
0100 
0101     if (net_eq(sock_net(sk), net) && inet_sk(sk)->inet_num == hnum &&
0102         sk->sk_family == PF_INET6) {
0103         if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
0104             return -1;
0105 
0106         if (!inet_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
0107             return -1;
0108 
0109         score =  sk->sk_bound_dev_if ? 2 : 1;
0110         if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
0111             score++;
0112     }
0113     return score;
0114 }
0115 
0116 static inline struct sock *lookup_reuseport(struct net *net, struct sock *sk,
0117                         struct sk_buff *skb, int doff,
0118                         const struct in6_addr *saddr,
0119                         __be16 sport,
0120                         const struct in6_addr *daddr,
0121                         unsigned short hnum)
0122 {
0123     struct sock *reuse_sk = NULL;
0124     u32 phash;
0125 
0126     if (sk->sk_reuseport) {
0127         phash = inet6_ehashfn(net, daddr, hnum, saddr, sport);
0128         reuse_sk = reuseport_select_sock(sk, phash, skb, doff);
0129     }
0130     return reuse_sk;
0131 }
0132 
0133 /* called with rcu_read_lock() */
0134 static struct sock *inet6_lhash2_lookup(struct net *net,
0135         struct inet_listen_hashbucket *ilb2,
0136         struct sk_buff *skb, int doff,
0137         const struct in6_addr *saddr,
0138         const __be16 sport, const struct in6_addr *daddr,
0139         const unsigned short hnum, const int dif, const int sdif)
0140 {
0141     struct sock *sk, *result = NULL;
0142     struct hlist_nulls_node *node;
0143     int score, hiscore = 0;
0144 
0145     sk_nulls_for_each_rcu(sk, node, &ilb2->nulls_head) {
0146         score = compute_score(sk, net, hnum, daddr, dif, sdif);
0147         if (score > hiscore) {
0148             result = lookup_reuseport(net, sk, skb, doff,
0149                           saddr, sport, daddr, hnum);
0150             if (result)
0151                 return result;
0152 
0153             result = sk;
0154             hiscore = score;
0155         }
0156     }
0157 
0158     return result;
0159 }
0160 
0161 static inline struct sock *inet6_lookup_run_bpf(struct net *net,
0162                         struct inet_hashinfo *hashinfo,
0163                         struct sk_buff *skb, int doff,
0164                         const struct in6_addr *saddr,
0165                         const __be16 sport,
0166                         const struct in6_addr *daddr,
0167                         const u16 hnum, const int dif)
0168 {
0169     struct sock *sk, *reuse_sk;
0170     bool no_reuseport;
0171 
0172     if (hashinfo != &tcp_hashinfo)
0173         return NULL; /* only TCP is supported */
0174 
0175     no_reuseport = bpf_sk_lookup_run_v6(net, IPPROTO_TCP, saddr, sport,
0176                         daddr, hnum, dif, &sk);
0177     if (no_reuseport || IS_ERR_OR_NULL(sk))
0178         return sk;
0179 
0180     reuse_sk = lookup_reuseport(net, sk, skb, doff, saddr, sport, daddr, hnum);
0181     if (reuse_sk)
0182         sk = reuse_sk;
0183     return sk;
0184 }
0185 
0186 struct sock *inet6_lookup_listener(struct net *net,
0187         struct inet_hashinfo *hashinfo,
0188         struct sk_buff *skb, int doff,
0189         const struct in6_addr *saddr,
0190         const __be16 sport, const struct in6_addr *daddr,
0191         const unsigned short hnum, const int dif, const int sdif)
0192 {
0193     struct inet_listen_hashbucket *ilb2;
0194     struct sock *result = NULL;
0195     unsigned int hash2;
0196 
0197     /* Lookup redirect from BPF */
0198     if (static_branch_unlikely(&bpf_sk_lookup_enabled)) {
0199         result = inet6_lookup_run_bpf(net, hashinfo, skb, doff,
0200                           saddr, sport, daddr, hnum, dif);
0201         if (result)
0202             goto done;
0203     }
0204 
0205     hash2 = ipv6_portaddr_hash(net, daddr, hnum);
0206     ilb2 = inet_lhash2_bucket(hashinfo, hash2);
0207 
0208     result = inet6_lhash2_lookup(net, ilb2, skb, doff,
0209                      saddr, sport, daddr, hnum,
0210                      dif, sdif);
0211     if (result)
0212         goto done;
0213 
0214     /* Lookup lhash2 with in6addr_any */
0215     hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum);
0216     ilb2 = inet_lhash2_bucket(hashinfo, hash2);
0217 
0218     result = inet6_lhash2_lookup(net, ilb2, skb, doff,
0219                      saddr, sport, &in6addr_any, hnum,
0220                      dif, sdif);
0221 done:
0222     if (IS_ERR(result))
0223         return NULL;
0224     return result;
0225 }
0226 EXPORT_SYMBOL_GPL(inet6_lookup_listener);
0227 
0228 struct sock *inet6_lookup(struct net *net, struct inet_hashinfo *hashinfo,
0229               struct sk_buff *skb, int doff,
0230               const struct in6_addr *saddr, const __be16 sport,
0231               const struct in6_addr *daddr, const __be16 dport,
0232               const int dif)
0233 {
0234     struct sock *sk;
0235     bool refcounted;
0236 
0237     sk = __inet6_lookup(net, hashinfo, skb, doff, saddr, sport, daddr,
0238                 ntohs(dport), dif, 0, &refcounted);
0239     if (sk && !refcounted && !refcount_inc_not_zero(&sk->sk_refcnt))
0240         sk = NULL;
0241     return sk;
0242 }
0243 EXPORT_SYMBOL_GPL(inet6_lookup);
0244 
0245 static int __inet6_check_established(struct inet_timewait_death_row *death_row,
0246                      struct sock *sk, const __u16 lport,
0247                      struct inet_timewait_sock **twp)
0248 {
0249     struct inet_hashinfo *hinfo = death_row->hashinfo;
0250     struct inet_sock *inet = inet_sk(sk);
0251     const struct in6_addr *daddr = &sk->sk_v6_rcv_saddr;
0252     const struct in6_addr *saddr = &sk->sk_v6_daddr;
0253     const int dif = sk->sk_bound_dev_if;
0254     struct net *net = sock_net(sk);
0255     const int sdif = l3mdev_master_ifindex_by_index(net, dif);
0256     const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport);
0257     const unsigned int hash = inet6_ehashfn(net, daddr, lport, saddr,
0258                         inet->inet_dport);
0259     struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
0260     spinlock_t *lock = inet_ehash_lockp(hinfo, hash);
0261     struct sock *sk2;
0262     const struct hlist_nulls_node *node;
0263     struct inet_timewait_sock *tw = NULL;
0264 
0265     spin_lock(lock);
0266 
0267     sk_nulls_for_each(sk2, node, &head->chain) {
0268         if (sk2->sk_hash != hash)
0269             continue;
0270 
0271         if (likely(inet6_match(net, sk2, saddr, daddr, ports,
0272                        dif, sdif))) {
0273             if (sk2->sk_state == TCP_TIME_WAIT) {
0274                 tw = inet_twsk(sk2);
0275                 if (twsk_unique(sk, sk2, twp))
0276                     break;
0277             }
0278             goto not_unique;
0279         }
0280     }
0281 
0282     /* Must record num and sport now. Otherwise we will see
0283      * in hash table socket with a funny identity.
0284      */
0285     inet->inet_num = lport;
0286     inet->inet_sport = htons(lport);
0287     sk->sk_hash = hash;
0288     WARN_ON(!sk_unhashed(sk));
0289     __sk_nulls_add_node_rcu(sk, &head->chain);
0290     if (tw) {
0291         sk_nulls_del_node_init_rcu((struct sock *)tw);
0292         __NET_INC_STATS(net, LINUX_MIB_TIMEWAITRECYCLED);
0293     }
0294     spin_unlock(lock);
0295     sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
0296 
0297     if (twp) {
0298         *twp = tw;
0299     } else if (tw) {
0300         /* Silly. Should hash-dance instead... */
0301         inet_twsk_deschedule_put(tw);
0302     }
0303     return 0;
0304 
0305 not_unique:
0306     spin_unlock(lock);
0307     return -EADDRNOTAVAIL;
0308 }
0309 
0310 static u64 inet6_sk_port_offset(const struct sock *sk)
0311 {
0312     const struct inet_sock *inet = inet_sk(sk);
0313 
0314     return secure_ipv6_port_ephemeral(sk->sk_v6_rcv_saddr.s6_addr32,
0315                       sk->sk_v6_daddr.s6_addr32,
0316                       inet->inet_dport);
0317 }
0318 
0319 int inet6_hash_connect(struct inet_timewait_death_row *death_row,
0320                struct sock *sk)
0321 {
0322     u64 port_offset = 0;
0323 
0324     if (!inet_sk(sk)->inet_num)
0325         port_offset = inet6_sk_port_offset(sk);
0326     return __inet_hash_connect(death_row, sk, port_offset,
0327                    __inet6_check_established);
0328 }
0329 EXPORT_SYMBOL_GPL(inet6_hash_connect);
0330 
0331 int inet6_hash(struct sock *sk)
0332 {
0333     int err = 0;
0334 
0335     if (sk->sk_state != TCP_CLOSE)
0336         err = __inet_hash(sk, NULL);
0337 
0338     return err;
0339 }
0340 EXPORT_SYMBOL_GPL(inet6_hash);