Lines Matching refs:sk
47 static u32 sk_ehashfn(const struct sock *sk)
50 if (sk->sk_family == AF_INET6 &&
51 !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
52 return inet6_ehashfn(sock_net(sk),
53 &sk->sk_v6_rcv_saddr, sk->sk_num,
54 &sk->sk_v6_daddr, sk->sk_dport);
57 if (sk->sk_family == AF_NINET) {
60 CALL_HCK_LITE_HOOK(nip_ninet_ehashfn_lhck, sk, &ret);
64 return inet_ehashfn(sock_net(sk),
65 sk->sk_rcv_saddr, sk->sk_num,
66 sk->sk_daddr, sk->sk_dport);
104 void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
107 inet_sk(sk)->inet_num = snum;
108 sk_add_bind_node(sk, &tb->owners);
109 inet_csk(sk)->icsk_bind_hash = tb;
115 static void __inet_put_port(struct sock *sk)
117 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
118 const int bhash = inet_bhashfn(sock_net(sk), inet_sk(sk)->inet_num,
124 tb = inet_csk(sk)->icsk_bind_hash;
125 __sk_del_bind_node(sk);
126 inet_csk(sk)->icsk_bind_hash = NULL;
127 inet_sk(sk)->inet_num = 0;
132 void inet_put_port(struct sock *sk)
135 __inet_put_port(sk);
140 int __inet_inherit_port(const struct sock *sk, struct sock *child)
142 struct inet_hashinfo *table = sk->sk_prot->h.hashinfo;
144 const int bhash = inet_bhashfn(sock_net(sk), port,
151 tb = inet_csk(sk)->icsk_bind_hash;
157 l3mdev = inet_sk_bound_l3mdev(sk);
165 if (net_eq(ib_net(tb), sock_net(sk)) &&
171 sock_net(sk), head, port,
188 inet_lhash2_bucket_sk(struct inet_hashinfo *h, struct sock *sk)
193 if (sk->sk_family == AF_INET6)
194 hash = ipv6_portaddr_hash(sock_net(sk),
195 &sk->sk_v6_rcv_saddr,
196 inet_sk(sk)->inet_num);
199 hash = ipv4_portaddr_hash(sock_net(sk),
200 inet_sk(sk)->inet_rcv_saddr,
201 inet_sk(sk)->inet_num);
205 static void inet_hash2(struct inet_hashinfo *h, struct sock *sk)
212 ilb2 = inet_lhash2_bucket_sk(h, sk);
215 if (sk->sk_reuseport && sk->sk_family == AF_INET6)
216 hlist_add_tail_rcu(&inet_csk(sk)->icsk_listen_portaddr_node,
219 hlist_add_head_rcu(&inet_csk(sk)->icsk_listen_portaddr_node,
225 static void inet_unhash2(struct inet_hashinfo *h, struct sock *sk)
230 WARN_ON_ONCE(hlist_unhashed(&inet_csk(sk)->icsk_listen_portaddr_node)))
233 ilb2 = inet_lhash2_bucket_sk(h, sk);
236 hlist_del_init_rcu(&inet_csk(sk)->icsk_listen_portaddr_node);
241 static inline int compute_score(struct sock *sk, struct net *net,
247 if (net_eq(sock_net(sk), net) && sk->sk_num == hnum &&
248 !ipv6_only_sock(sk)) {
249 if (sk->sk_rcv_saddr != daddr)
252 if (!inet_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif))
254 score = sk->sk_bound_dev_if ? 2 : 1;
256 if (sk->sk_family == PF_INET)
258 if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
264 static inline struct sock *lookup_reuseport(struct net *net, struct sock *sk,
272 if (sk->sk_reuseport) {
274 reuse_sk = reuseport_select_sock(sk, phash, skb, doff);
295 struct sock *sk, *result = NULL;
299 sk = (struct sock *)icsk;
300 score = compute_score(sk, net, hnum, daddr, dif, sdif);
302 result = lookup_reuseport(net, sk, skb, doff,
307 result = sk;
321 struct sock *sk, *reuse_sk;
328 saddr, sport, daddr, hnum, &sk);
329 if (no_reuseport || IS_ERR_OR_NULL(sk))
330 return sk;
332 reuse_sk = lookup_reuseport(net, sk, skb, doff, saddr, sport, daddr, hnum);
334 sk = reuse_sk;
335 return sk;
381 void sock_gen_put(struct sock *sk)
383 if (!refcount_dec_and_test(&sk->sk_refcnt))
386 if (sk->sk_state == TCP_TIME_WAIT)
387 inet_twsk_free(inet_twsk(sk));
388 else if (sk->sk_state == TCP_NEW_SYN_RECV)
389 reqsk_free(inet_reqsk(sk));
391 sk_free(sk);
397 sock_gen_put(skb->sk);
409 struct sock *sk;
419 sk_nulls_for_each_rcu(sk, node, &head->chain) {
420 if (sk->sk_hash != hash)
422 if (likely(INET_MATCH(net, sk, acookie, ports, dif, sdif))) {
423 if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
425 if (unlikely(!INET_MATCH(net, sk, acookie,
427 sock_gen_put(sk);
441 sk = NULL;
443 return sk;
449 struct sock *sk, __u16 lport,
453 struct inet_sock *inet = inet_sk(sk);
456 int dif = sk->sk_bound_dev_if;
457 struct net *net = sock_net(sk);
478 if (twsk_unique(sk, sk2, twp))
490 sk->sk_hash = hash;
491 WARN_ON(!sk_unhashed(sk));
492 __sk_nulls_add_node_rcu(sk, &head->chain);
498 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
513 static u64 inet_sk_port_offset(const struct sock *sk)
515 const struct inet_sock *inet = inet_sk(sk);
525 static bool inet_ehash_lookup_by_sk(struct sock *sk,
528 const __portpair ports = INET_COMBINED_PORTS(sk->sk_dport, sk->sk_num);
529 const int sdif = sk->sk_bound_dev_if;
530 const int dif = sk->sk_bound_dev_if;
532 struct net *net = sock_net(sk);
535 INET_ADDR_COOKIE(acookie, sk->sk_daddr, sk->sk_rcv_saddr);
538 if (esk->sk_hash != sk->sk_hash)
540 if (sk->sk_family == AF_INET) {
547 else if (sk->sk_family == AF_INET6) {
549 &sk->sk_v6_daddr,
550 &sk->sk_v6_rcv_saddr,
562 * If an existing socket already exists, socket sk is not inserted,
565 bool inet_ehash_insert(struct sock *sk, struct sock *osk, bool *found_dup_sk)
567 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
573 WARN_ON_ONCE(!sk_unhashed(sk));
575 sk->sk_hash = sk_ehashfn(sk);
576 head = inet_ehash_bucket(hashinfo, sk->sk_hash);
578 lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
582 WARN_ON_ONCE(sk->sk_hash != osk->sk_hash);
585 *found_dup_sk = inet_ehash_lookup_by_sk(sk, list);
591 __sk_nulls_add_node_rcu(sk, list);
598 bool inet_ehash_nolisten(struct sock *sk, struct sock *osk, bool *found_dup_sk)
600 bool ok = inet_ehash_insert(sk, osk, found_dup_sk);
603 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
605 this_cpu_inc(*sk->sk_prot->orphan_count);
606 inet_sk_set_state(sk, TCP_CLOSE);
607 sock_set_flag(sk, SOCK_DEAD);
608 inet_csk_destroy_sock(sk);
614 static int inet_reuseport_add_sock(struct sock *sk,
617 struct inet_bind_bucket *tb = inet_csk(sk)->icsk_bind_hash;
620 kuid_t uid = sock_i_uid(sk);
623 if (sk2 != sk &&
624 sk2->sk_family == sk->sk_family &&
625 ipv6_only_sock(sk2) == ipv6_only_sock(sk) &&
626 sk2->sk_bound_dev_if == sk->sk_bound_dev_if &&
629 inet_rcv_saddr_equal(sk, sk2, false))
630 return reuseport_add_sock(sk, sk2,
631 inet_rcv_saddr_any(sk));
634 return reuseport_alloc(sk, inet_rcv_saddr_any(sk));
637 int __inet_hash(struct sock *sk, struct sock *osk)
639 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
643 if (sk->sk_state != TCP_LISTEN) {
645 inet_ehash_nolisten(sk, osk, NULL);
649 WARN_ON(!sk_unhashed(sk));
650 ilb = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
653 if (sk->sk_reuseport) {
654 err = inet_reuseport_add_sock(sk, ilb);
658 if (IS_ENABLED(CONFIG_IPV6) && sk->sk_reuseport &&
659 sk->sk_family == AF_INET6)
660 __sk_nulls_add_node_tail_rcu(sk, &ilb->nulls_head);
662 __sk_nulls_add_node_rcu(sk, &ilb->nulls_head);
663 inet_hash2(hashinfo, sk);
665 sock_set_flag(sk, SOCK_RCU_FREE);
666 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
674 int inet_hash(struct sock *sk)
678 if (sk->sk_state != TCP_CLOSE)
679 err = __inet_hash(sk, NULL);
685 static void __inet_unhash(struct sock *sk, struct inet_listen_hashbucket *ilb)
687 if (sk_unhashed(sk))
690 if (rcu_access_pointer(sk->sk_reuseport_cb))
691 reuseport_detach_sock(sk);
693 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
695 inet_unhash2(hashinfo, sk);
698 __sk_nulls_del_node_init_rcu(sk);
699 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
702 void inet_unhash(struct sock *sk)
704 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
706 if (sk_unhashed(sk))
709 if (sk->sk_state == TCP_LISTEN) {
712 ilb = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
717 __inet_unhash(sk, ilb);
720 spinlock_t *lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
723 __inet_unhash(sk, NULL);
743 struct sock *sk, u64 port_offset,
750 int port = inet_sk(sk)->inet_num;
751 struct net *net = sock_net(sk);
760 ret = check_established(death_row, sk, port, NULL);
765 l3mdev = inet_sk_bound_l3mdev(sk);
804 if (!check_established(death_row, sk,
841 inet_bind_hash(sk, tb, port);
842 if (sk_unhashed(sk)) {
843 inet_sk(sk)->inet_sport = htons(port);
844 inet_ehash_nolisten(sk, (struct sock *)tw, NULL);
859 struct sock *sk)
863 if (!inet_sk(sk)->inet_num)
864 port_offset = inet_sk_port_offset(sk);
865 return __inet_hash_connect(death_row, sk, port_offset,