^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * INET An implementation of the TCP/IP protocol suite for the LINUX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * operating system. INET is implemented using the BSD Socket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * interface as the means of communication with the user level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * PF_INET protocol family socket handler.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Authors: Ross Biro
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Florian La Roche, <flla@stud.uni-sb.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Alan Cox, <A.Cox@swansea.ac.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Changes (see also sock.c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * piggy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Karl Knutson : Socket protocol table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * A.N.Kuznetsov : Socket death error in accept().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * John Richardson : Fix non blocking error in connect()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * so sockets that fail to connect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * don't return -EINPROGRESS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * Alan Cox : Asynchronous I/O support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Alan Cox : Keep correct socket pointer on sock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * structures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * when accept() ed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * Alan Cox : Semantics of SO_LINGER aren't state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * moved to close when you look carefully.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * With this fixed and the accept bug fixed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * some RPC stuff seems happier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * Niibe Yutaka : 4.4BSD style write async I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * Alan Cox,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * Tony Gale : Fixed reuse semantics.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * Alan Cox : bind() shouldn't abort existing but dead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * sockets. Stops FTP netin:.. I hope.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * Alan Cox : bind() works correctly for RAW sockets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * Note that FreeBSD at least was broken
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * in this respect so be careful with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * compatibility tests...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * Alan Cox : routing cache support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * Alan Cox : memzero the socket structure for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * compactness.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * Matt Day : nonblock connect error handler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * Alan Cox : Allow large numbers of pending sockets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * (eg for big web sites), but only if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * specifically application requested.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * Alan Cox : New buffering throughout IP. Used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * dumbly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * Alan Cox : New buffering now used smartly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * Alan Cox : BSD rather than common sense
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * interpretation of listen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * Germano Caronni : Assorted small races.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * Alan Cox : sendmsg/recvmsg basic support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * Alan Cox : Only sendmsg/recvmsg now supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * Alan Cox : Locked down bind (see security list).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * Alan Cox : Loosened bind a little.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * Mike McLagan : ADD/DEL DLCI Ioctls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * Willy Konynenberg : Transparent proxying support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * David S. Miller : New socket lookup architecture.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * Some other random speedups.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * Cyrus Durgin : Cleaned up file for kmod hacks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * Andi Kleen : Fix inet_stream_connect TCP race.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #define pr_fmt(fmt) "IPv4: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #include <linux/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #include <linux/kmod.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #include <linux/sockios.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #include <linux/fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #include <linux/netfilter_ipv4.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #include <linux/random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) #include <linux/inet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) #include <linux/igmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #include <linux/inetdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #include <net/checksum.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #include <net/protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) #include <net/arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #include <net/route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #include <net/ip_fib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #include <net/inet_connection_sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #include <net/tcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #include <net/udp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #include <net/udplite.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #include <net/ping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #include <net/raw.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #include <net/icmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #include <net/inet_common.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) #include <net/ip_tunnels.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #include <net/xfrm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) #include <net/net_namespace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) #include <net/secure_seq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) #ifdef CONFIG_IP_MROUTE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) #include <linux/mroute.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) #include <net/l3mdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) #include <net/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #include <trace/events/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* The inetsw table contains everything that inet_create needs to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * build a new socket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static struct list_head inetsw[SOCK_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static DEFINE_SPINLOCK(inetsw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) /* New destruction routine */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) void inet_sock_destruct(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct inet_sock *inet = inet_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) __skb_queue_purge(&sk->sk_receive_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (sk->sk_rx_skb_cache) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) __kfree_skb(sk->sk_rx_skb_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) sk->sk_rx_skb_cache = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) __skb_queue_purge(&sk->sk_error_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) sk_mem_reclaim(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) pr_err("Attempt to release TCP socket in state %d %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) sk->sk_state, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (!sock_flag(sk, SOCK_DEAD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) pr_err("Attempt to release alive inet socket %p\n", sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) WARN_ON(atomic_read(&sk->sk_rmem_alloc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) WARN_ON(refcount_read(&sk->sk_wmem_alloc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) WARN_ON(sk->sk_wmem_queued);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) WARN_ON(sk->sk_forward_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) kfree(rcu_dereference_protected(inet->inet_opt, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) dst_release(rcu_dereference_protected(sk->sk_dst_cache, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) dst_release(sk->sk_rx_dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) sk_refcnt_debug_dec(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) EXPORT_SYMBOL(inet_sock_destruct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * The routines beyond this point handle the behaviour of an AF_INET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * socket object. Mostly it punts to the subprotocols of IP to do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * the work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * Automatically bind an unbound socket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static int inet_autobind(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct inet_sock *inet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /* We may need to bind the socket. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) inet = inet_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (!inet->inet_num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (sk->sk_prot->get_port(sk, 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) inet->inet_sport = htons(inet->inet_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * Move a socket into listening state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) int inet_listen(struct socket *sock, int backlog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) unsigned char old_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) int err, tcp_fastopen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) old_state = sk->sk_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) WRITE_ONCE(sk->sk_max_ack_backlog, backlog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) /* Really, if the socket is already in listen state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * we can only allow the backlog to be adjusted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (old_state != TCP_LISTEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) /* Enable TFO w/o requiring TCP_FASTOPEN socket option.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * Note that only TCP sockets (SOCK_STREAM) will reach here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * Also fastopen backlog may already been set via the option
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * because the socket was in TCP_LISTEN state previously but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * was shutdown() rather than close().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) tcp_fastopen = sock_net(sk)->ipv4.sysctl_tcp_fastopen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if ((tcp_fastopen & TFO_SERVER_WO_SOCKOPT1) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) (tcp_fastopen & TFO_SERVER_ENABLE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) !inet_csk(sk)->icsk_accept_queue.fastopenq.max_qlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) fastopen_queue_tune(sk, backlog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) tcp_fastopen_init_key_once(sock_net(sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) err = inet_csk_listen_start(sk, backlog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) tcp_call_bpf(sk, BPF_SOCK_OPS_TCP_LISTEN_CB, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) EXPORT_SYMBOL(inet_listen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * Create an inet socket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static int inet_create(struct net *net, struct socket *sock, int protocol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) int kern)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) struct sock *sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) struct inet_protosw *answer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) struct inet_sock *inet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) struct proto *answer_prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) unsigned char answer_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) int try_loading_module = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (protocol < 0 || protocol >= IPPROTO_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) sock->state = SS_UNCONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) /* Look for the requested type/protocol pair. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) lookup_protocol:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) err = -ESOCKTNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) list_for_each_entry_rcu(answer, &inetsw[sock->type], list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) /* Check the non-wild match. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (protocol == answer->protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) if (protocol != IPPROTO_IP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) /* Check for the two wild cases. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (IPPROTO_IP == protocol) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) protocol = answer->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (IPPROTO_IP == answer->protocol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) err = -EPROTONOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (unlikely(err)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (try_loading_module < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * Be more specific, e.g. net-pf-2-proto-132-type-1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (++try_loading_module == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) request_module("net-pf-%d-proto-%d-type-%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) PF_INET, protocol, sock->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * Fall back to generic, e.g. net-pf-2-proto-132
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * (net-pf-PF_INET-proto-IPPROTO_SCTP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) request_module("net-pf-%d-proto-%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) PF_INET, protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) goto lookup_protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) goto out_rcu_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (sock->type == SOCK_RAW && !kern &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) !ns_capable(net->user_ns, CAP_NET_RAW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) goto out_rcu_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) sock->ops = answer->ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) answer_prot = answer->prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) answer_flags = answer->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) WARN_ON(!answer_prot->slab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) err = -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot, kern);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (!sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (INET_PROTOSW_REUSE & answer_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) sk->sk_reuse = SK_CAN_REUSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) inet = inet_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) inet->nodefrag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (SOCK_RAW == sock->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) inet->inet_num = protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (IPPROTO_RAW == protocol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) inet->hdrincl = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (net->ipv4.sysctl_ip_no_pmtu_disc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) inet->pmtudisc = IP_PMTUDISC_DONT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) inet->pmtudisc = IP_PMTUDISC_WANT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) inet->inet_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) sock_init_data(sock, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) sk->sk_destruct = inet_sock_destruct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) sk->sk_protocol = protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) inet->uc_ttl = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) inet->mc_loop = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) inet->mc_ttl = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) inet->mc_all = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) inet->mc_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) inet->mc_list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) inet->rcv_tos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) sk_refcnt_debug_inc(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (inet->inet_num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) /* It assumes that any protocol which allows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * the user to assign a number at socket
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * creation time automatically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * shares.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) inet->inet_sport = htons(inet->inet_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) /* Add to protocol hash chains. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) err = sk->sk_prot->hash(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) sk_common_release(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (sk->sk_prot->init) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) err = sk->sk_prot->init(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) sk_common_release(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (!kern) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) err = BPF_CGROUP_RUN_PROG_INET_SOCK(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) sk_common_release(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) out_rcu_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * The peer socket should always be NULL (or else). When we call this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * function we are destroying the object and from then on nobody
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * should refer to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) int inet_release(struct socket *sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) if (sk) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) long timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) if (!sk->sk_kern_sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) BPF_CGROUP_RUN_PROG_INET_SOCK_RELEASE(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) /* Applications forget to leave groups before exiting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) ip_mc_drop_socket(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) /* If linger is set, we don't return until the close
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) * is complete. Otherwise we return immediately. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * actually closing is done the same either way.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * If the close is due to the process exiting, we never
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) * linger..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) timeout = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) if (sock_flag(sk, SOCK_LINGER) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) !(current->flags & PF_EXITING))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) timeout = sk->sk_lingertime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) sk->sk_prot->close(sk, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) sock->sk = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) EXPORT_SYMBOL(inet_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) /* If the socket has its own bind function then use it. (RAW) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) if (sk->sk_prot->bind) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) return sk->sk_prot->bind(sk, uaddr, addr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) if (addr_len < sizeof(struct sockaddr_in))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) /* BPF prog is run before any checks are done so that if the prog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * changes context in a wrong way it will be caught.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) err = BPF_CGROUP_RUN_PROG_INET4_BIND(sk, uaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) return __inet_bind(sk, uaddr, addr_len, BIND_WITH_LOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) EXPORT_SYMBOL(inet_bind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) int __inet_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) u32 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) struct inet_sock *inet = inet_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) struct net *net = sock_net(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) unsigned short snum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) int chk_addr_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) u32 tb_id = RT_TABLE_LOCAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) if (addr->sin_family != AF_INET) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) /* Compatibility games : accept AF_UNSPEC (mapped to AF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) * only if s_addr is INADDR_ANY.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) err = -EAFNOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) if (addr->sin_family != AF_UNSPEC ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) addr->sin_addr.s_addr != htonl(INADDR_ANY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) tb_id = l3mdev_fib_table_by_index(net, sk->sk_bound_dev_if) ? : tb_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) chk_addr_ret = inet_addr_type_table(net, addr->sin_addr.s_addr, tb_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) /* Not specified by any standard per-se, however it breaks too
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) * many applications when removed. It is unfortunate since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) * allowing applications to make a non-local bind solves
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) * several problems with systems using dynamic addressing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) * (ie. your servers still start up even if your ISDN link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) * is temporarily down)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) err = -EADDRNOTAVAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) if (!inet_can_nonlocal_bind(net, inet) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) addr->sin_addr.s_addr != htonl(INADDR_ANY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) chk_addr_ret != RTN_LOCAL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) chk_addr_ret != RTN_MULTICAST &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) chk_addr_ret != RTN_BROADCAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) snum = ntohs(addr->sin_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (snum && inet_is_local_unbindable_port(net, snum))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) err = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (snum && inet_port_requires_bind_service(net, snum) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) !ns_capable(net->user_ns, CAP_NET_BIND_SERVICE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) /* We keep a pair of addresses. rcv_saddr is the one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) * used by hash lookups, and saddr is used for transmit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) * In the BSD API these are the same except where it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) * would be illegal to use them (multicast/broadcast) in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) * which case the sending device address is used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) if (flags & BIND_WITH_LOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) /* Check these errors (active socket, double bind). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) if (sk->sk_state != TCP_CLOSE || inet->inet_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) goto out_release_sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) inet->inet_saddr = 0; /* Use device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) /* Make sure we are allowed to bind here. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (snum || !(inet->bind_address_no_port ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) (flags & BIND_FORCE_ADDRESS_NO_PORT))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) if (sk->sk_prot->get_port(sk, snum)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) inet->inet_saddr = inet->inet_rcv_saddr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) err = -EADDRINUSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) goto out_release_sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) if (!(flags & BIND_FROM_BPF)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) err = BPF_CGROUP_RUN_PROG_INET4_POST_BIND(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) inet->inet_saddr = inet->inet_rcv_saddr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) goto out_release_sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) if (inet->inet_rcv_saddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) if (snum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) inet->inet_sport = htons(inet->inet_num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) inet->inet_daddr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) inet->inet_dport = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) sk_dst_reset(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) out_release_sock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) if (flags & BIND_WITH_LOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) int inet_dgram_connect(struct socket *sock, struct sockaddr *uaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) int addr_len, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) if (addr_len < sizeof(uaddr->sa_family))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) if (uaddr->sa_family == AF_UNSPEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) return sk->sk_prot->disconnect(sk, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) if (BPF_CGROUP_PRE_CONNECT_ENABLED(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) err = sk->sk_prot->pre_connect(sk, uaddr, addr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) if (data_race(!inet_sk(sk)->inet_num) && inet_autobind(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) return sk->sk_prot->connect(sk, uaddr, addr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) EXPORT_SYMBOL(inet_dgram_connect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) static long inet_wait_for_connect(struct sock *sk, long timeo, int writebias)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) DEFINE_WAIT_FUNC(wait, woken_wake_function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) add_wait_queue(sk_sleep(sk), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) sk->sk_write_pending += writebias;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) /* Basic assumption: if someone sets sk->sk_err, he _must_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) * change state of the socket from TCP_SYN_*.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) * Connect() does not allow to get error notifications
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) * without closing the socket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) if (signal_pending(current) || !timeo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) remove_wait_queue(sk_sleep(sk), &wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) sk->sk_write_pending -= writebias;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) return timeo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) * Connect to a remote host. There is regrettably still a little
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) * TCP 'magic' in here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) int __inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) int addr_len, int flags, int is_sendmsg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) long timeo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) * uaddr can be NULL and addr_len can be 0 if:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) * sk is a TCP fastopen active socket and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) * TCP_FASTOPEN_CONNECT sockopt is set and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) * we already have a valid cookie for this socket.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) * In this case, user can call write() after connect().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) * write() will invoke tcp_sendmsg_fastopen() which calls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) * __inet_stream_connect().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) if (uaddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) if (addr_len < sizeof(uaddr->sa_family))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) if (uaddr->sa_family == AF_UNSPEC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) err = sk->sk_prot->disconnect(sk, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) switch (sock->state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) case SS_CONNECTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) err = -EISCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) case SS_CONNECTING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) if (inet_sk(sk)->defer_connect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) err = is_sendmsg ? -EINPROGRESS : -EISCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) err = -EALREADY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) /* Fall out of switch with err, set for this state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) case SS_UNCONNECTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) err = -EISCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) if (sk->sk_state != TCP_CLOSE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) if (BPF_CGROUP_PRE_CONNECT_ENABLED(sk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) err = sk->sk_prot->pre_connect(sk, uaddr, addr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) err = sk->sk_prot->connect(sk, uaddr, addr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) sock->state = SS_CONNECTING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) if (!err && inet_sk(sk)->defer_connect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) /* Just entered SS_CONNECTING state; the only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) * difference is that return value in non-blocking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) * case is EINPROGRESS, rather than EALREADY.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) err = -EINPROGRESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) int writebias = (sk->sk_protocol == IPPROTO_TCP) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) tcp_sk(sk)->fastopen_req &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) tcp_sk(sk)->fastopen_req->data ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) /* Error code is set above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) if (!timeo || !inet_wait_for_connect(sk, timeo, writebias))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) err = sock_intr_errno(timeo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) if (signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) /* Connection was closed by RST, timeout, ICMP error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) * or another process disconnected us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) if (sk->sk_state == TCP_CLOSE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) goto sock_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) /* sk->sk_err may be not zero now, if RECVERR was ordered by user
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) * and error was received after socket entered established state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) * Hence, it is handled normally after connect() return successfully.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) sock->state = SS_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) sock_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) err = sock_error(sk) ? : -ECONNABORTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) sock->state = SS_UNCONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) if (sk->sk_prot->disconnect(sk, flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) sock->state = SS_DISCONNECTING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) EXPORT_SYMBOL(__inet_stream_connect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) int addr_len, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) lock_sock(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) err = __inet_stream_connect(sock, uaddr, addr_len, flags, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) release_sock(sock->sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) EXPORT_SYMBOL(inet_stream_connect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) * Accept a pending connection. The TCP layer now gives BSD semantics.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) int inet_accept(struct socket *sock, struct socket *newsock, int flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) bool kern)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) struct sock *sk1 = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) int err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err, kern);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) if (!sk2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) goto do_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) lock_sock(sk2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) sock_rps_record_flow(sk2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) WARN_ON(!((1 << sk2->sk_state) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) (TCPF_ESTABLISHED | TCPF_SYN_RECV |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) TCPF_CLOSE_WAIT | TCPF_CLOSE)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) sock_graft(sk2, newsock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) newsock->state = SS_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) release_sock(sk2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) do_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) EXPORT_SYMBOL(inet_accept);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) * This does both peername and sockname.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) int inet_getname(struct socket *sock, struct sockaddr *uaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) int peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) struct inet_sock *inet = inet_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) DECLARE_SOCKADDR(struct sockaddr_in *, sin, uaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) sin->sin_family = AF_INET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) if (peer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) if (!inet->inet_dport ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) peer == 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) return -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) sin->sin_port = inet->inet_dport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) sin->sin_addr.s_addr = inet->inet_daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) __be32 addr = inet->inet_rcv_saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) if (!addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) addr = inet->inet_saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) sin->sin_port = inet->inet_sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) sin->sin_addr.s_addr = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) if (cgroup_bpf_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) BPF_CGROUP_RUN_SA_PROG_LOCK(sk, (struct sockaddr *)sin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) peer ? BPF_CGROUP_INET4_GETPEERNAME :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) BPF_CGROUP_INET4_GETSOCKNAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) return sizeof(*sin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) EXPORT_SYMBOL(inet_getname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) int inet_send_prepare(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) sock_rps_record_flow(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) /* We may need to bind the socket. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) if (data_race(!inet_sk(sk)->inet_num) && !sk->sk_prot->no_autobind &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) inet_autobind(sk))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) EXPORT_SYMBOL_GPL(inet_send_prepare);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) int inet_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) if (unlikely(inet_send_prepare(sk)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) return INDIRECT_CALL_2(sk->sk_prot->sendmsg, tcp_sendmsg, udp_sendmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) sk, msg, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) EXPORT_SYMBOL(inet_sendmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) if (unlikely(inet_send_prepare(sk)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) if (sk->sk_prot->sendpage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) return sk->sk_prot->sendpage(sk, page, offset, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) return sock_no_sendpage(sock, page, offset, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) EXPORT_SYMBOL(inet_sendpage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) INDIRECT_CALLABLE_DECLARE(int udp_recvmsg(struct sock *, struct msghdr *,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) size_t, int, int, int *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) int inet_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) int addr_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) if (likely(!(flags & MSG_ERRQUEUE)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) sock_rps_record_flow(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) err = INDIRECT_CALL_2(sk->sk_prot->recvmsg, tcp_recvmsg, udp_recvmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) sk, msg, size, flags & MSG_DONTWAIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) flags & ~MSG_DONTWAIT, &addr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) if (err >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) msg->msg_namelen = addr_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) EXPORT_SYMBOL(inet_recvmsg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) int inet_shutdown(struct socket *sock, int how)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) /* This should really check to make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) * the socket is a TCP socket. (WHY AC...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 1->2 bit 2 snds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 2->3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) if ((how & ~SHUTDOWN_MASK) || !how) /* MAXINT->0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) if (sock->state == SS_CONNECTING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) if ((1 << sk->sk_state) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) sock->state = SS_DISCONNECTING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) sock->state = SS_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) switch (sk->sk_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) case TCP_CLOSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) err = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) /* Hack to wake up other listeners, who can poll for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) EPOLLHUP, even on eg. unconnected UDP sockets -- RR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) sk->sk_shutdown |= how;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) if (sk->sk_prot->shutdown)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) sk->sk_prot->shutdown(sk, how);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) /* Remaining two branches are temporary solution for missing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) * close() in multithreaded environment. It is _not_ a good idea,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) * but we have no choice until close() is repaired at VFS level.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) case TCP_LISTEN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) if (!(how & RCV_SHUTDOWN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) case TCP_SYN_SENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) /* Wake up anyone sleeping in poll. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) sk->sk_state_change(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) EXPORT_SYMBOL(inet_shutdown);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) * ioctl() calls you can issue on an INET socket. Most of these are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) * device configuration and stuff and very rarely used. Some ioctls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) * pass on to the socket itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) * NOTE: I like the idea of a module for the config stuff. ie ifconfig
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) * loads the devconfigure module does its configuring and unloads it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) * There's a good 20K of config code hanging around the kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) struct net *net = sock_net(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) void __user *p = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) struct ifreq ifr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) struct rtentry rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) case SIOCADDRT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) case SIOCDELRT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) if (copy_from_user(&rt, p, sizeof(struct rtentry)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) err = ip_rt_ioctl(net, cmd, &rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) case SIOCRTMSG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) case SIOCDARP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) case SIOCGARP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) case SIOCSARP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) err = arp_ioctl(net, cmd, (void __user *)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) case SIOCGIFADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) case SIOCGIFBRDADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) case SIOCGIFNETMASK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) case SIOCGIFDSTADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) case SIOCGIFPFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) if (copy_from_user(&ifr, p, sizeof(struct ifreq)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) err = devinet_ioctl(net, cmd, &ifr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) if (!err && copy_to_user(p, &ifr, sizeof(struct ifreq)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) case SIOCSIFADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) case SIOCSIFBRDADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) case SIOCSIFNETMASK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) case SIOCSIFDSTADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) case SIOCSIFPFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) case SIOCSIFFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) if (copy_from_user(&ifr, p, sizeof(struct ifreq)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) err = devinet_ioctl(net, cmd, &ifr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) if (sk->sk_prot->ioctl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) err = sk->sk_prot->ioctl(sk, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) err = -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) EXPORT_SYMBOL(inet_ioctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) static int inet_compat_routing_ioctl(struct sock *sk, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) struct compat_rtentry __user *ur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) compat_uptr_t rtdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) struct rtentry rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) if (copy_from_user(&rt.rt_dst, &ur->rt_dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) 3 * sizeof(struct sockaddr)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) get_user(rt.rt_flags, &ur->rt_flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) get_user(rt.rt_metric, &ur->rt_metric) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) get_user(rt.rt_mtu, &ur->rt_mtu) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) get_user(rt.rt_window, &ur->rt_window) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) get_user(rt.rt_irtt, &ur->rt_irtt) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) get_user(rtdev, &ur->rt_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) rt.rt_dev = compat_ptr(rtdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) return ip_rt_ioctl(sock_net(sk), cmd, &rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) static int inet_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) void __user *argp = compat_ptr(arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) case SIOCADDRT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) case SIOCDELRT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) return inet_compat_routing_ioctl(sk, cmd, argp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) if (!sk->sk_prot->compat_ioctl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) return -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) return sk->sk_prot->compat_ioctl(sk, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) #endif /* CONFIG_COMPAT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) const struct proto_ops inet_stream_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) .family = PF_INET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) .flags = PROTO_CMSG_DATA_ONLY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) .release = inet_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) .bind = inet_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) .connect = inet_stream_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) .socketpair = sock_no_socketpair,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) .accept = inet_accept,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) .getname = inet_getname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) .poll = tcp_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) .ioctl = inet_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) .gettstamp = sock_gettstamp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) .listen = inet_listen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) .shutdown = inet_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) .setsockopt = sock_common_setsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) .getsockopt = sock_common_getsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) .sendmsg = inet_sendmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) .recvmsg = inet_recvmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) #ifdef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) .mmap = tcp_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) .sendpage = inet_sendpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) .splice_read = tcp_splice_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) .read_sock = tcp_read_sock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) .sendmsg_locked = tcp_sendmsg_locked,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) .sendpage_locked = tcp_sendpage_locked,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) .peek_len = tcp_peek_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) .compat_ioctl = inet_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) .set_rcvlowat = tcp_set_rcvlowat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) EXPORT_SYMBOL(inet_stream_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) const struct proto_ops inet_dgram_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) .family = PF_INET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) .release = inet_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) .bind = inet_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) .connect = inet_dgram_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) .socketpair = sock_no_socketpair,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) .accept = sock_no_accept,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) .getname = inet_getname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) .poll = udp_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) .ioctl = inet_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) .gettstamp = sock_gettstamp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) .listen = sock_no_listen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) .shutdown = inet_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) .setsockopt = sock_common_setsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) .getsockopt = sock_common_getsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) .sendmsg = inet_sendmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) .recvmsg = inet_recvmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) .mmap = sock_no_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) .sendpage = inet_sendpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) .set_peek_off = sk_set_peek_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) .compat_ioctl = inet_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) EXPORT_SYMBOL(inet_dgram_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) * udp_poll
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) static const struct proto_ops inet_sockraw_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) .family = PF_INET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) .release = inet_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) .bind = inet_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) .connect = inet_dgram_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) .socketpair = sock_no_socketpair,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) .accept = sock_no_accept,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) .getname = inet_getname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) .poll = datagram_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) .ioctl = inet_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) .gettstamp = sock_gettstamp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) .listen = sock_no_listen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) .shutdown = inet_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) .setsockopt = sock_common_setsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) .getsockopt = sock_common_getsockopt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) .sendmsg = inet_sendmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) .recvmsg = inet_recvmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) .mmap = sock_no_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) .sendpage = inet_sendpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) .compat_ioctl = inet_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) static const struct net_proto_family inet_family_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) .family = PF_INET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) .create = inet_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) /* Upon startup we insert all the elements in inetsw_array[] into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) * the linked list inetsw.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) static struct inet_protosw inetsw_array[] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) .type = SOCK_STREAM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) .protocol = IPPROTO_TCP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) .prot = &tcp_prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) .ops = &inet_stream_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) .flags = INET_PROTOSW_PERMANENT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) INET_PROTOSW_ICSK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) .type = SOCK_DGRAM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) .protocol = IPPROTO_UDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) .prot = &udp_prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) .ops = &inet_dgram_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) .flags = INET_PROTOSW_PERMANENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) .type = SOCK_DGRAM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) .protocol = IPPROTO_ICMP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) .prot = &ping_prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) .ops = &inet_sockraw_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) .flags = INET_PROTOSW_REUSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) .type = SOCK_RAW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) .protocol = IPPROTO_IP, /* wild card */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) .prot = &raw_prot,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) .ops = &inet_sockraw_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) .flags = INET_PROTOSW_REUSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) #define INETSW_ARRAY_LEN ARRAY_SIZE(inetsw_array)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) void inet_register_protosw(struct inet_protosw *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) struct list_head *lh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) struct inet_protosw *answer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) int protocol = p->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) struct list_head *last_perm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) spin_lock_bh(&inetsw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) if (p->type >= SOCK_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) goto out_illegal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) /* If we are trying to override a permanent protocol, bail. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) last_perm = &inetsw[p->type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) list_for_each(lh, &inetsw[p->type]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) answer = list_entry(lh, struct inet_protosw, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) /* Check only the non-wild match. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) if ((INET_PROTOSW_PERMANENT & answer->flags) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) if (protocol == answer->protocol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) goto out_permanent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) last_perm = lh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) /* Add the new entry after the last permanent entry if any, so that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) * the new entry does not override a permanent entry when matched with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) * a wild-card protocol. But it is allowed to override any existing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) * non-permanent entry. This means that when we remove this entry, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) * system automatically returns to the old behavior.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) list_add_rcu(&p->list, last_perm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) spin_unlock_bh(&inetsw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) out_permanent:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) pr_err("Attempt to override permanent protocol %d\n", protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) out_illegal:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) pr_err("Ignoring attempt to register invalid socket type %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) p->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) EXPORT_SYMBOL(inet_register_protosw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) void inet_unregister_protosw(struct inet_protosw *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) if (INET_PROTOSW_PERMANENT & p->flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) pr_err("Attempt to unregister permanent protocol %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) p->protocol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) spin_lock_bh(&inetsw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) list_del_rcu(&p->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) spin_unlock_bh(&inetsw_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) synchronize_net();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) EXPORT_SYMBOL(inet_unregister_protosw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) static int inet_sk_reselect_saddr(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) struct inet_sock *inet = inet_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) __be32 old_saddr = inet->inet_saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) __be32 daddr = inet->inet_daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) struct flowi4 *fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) struct rtable *rt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) __be32 new_saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) struct ip_options_rcu *inet_opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) inet_opt = rcu_dereference_protected(inet->inet_opt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) lockdep_sock_is_held(sk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) if (inet_opt && inet_opt->opt.srr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) daddr = inet_opt->opt.faddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) /* Query new route. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) fl4 = &inet->cork.fl.u.ip4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) rt = ip_route_connect(fl4, daddr, 0, RT_CONN_FLAGS(sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) sk->sk_bound_dev_if, sk->sk_protocol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) inet->inet_sport, inet->inet_dport, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) if (IS_ERR(rt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) return PTR_ERR(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) sk_setup_caps(sk, &rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) new_saddr = fl4->saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) if (new_saddr == old_saddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) if (sock_net(sk)->ipv4.sysctl_ip_dynaddr > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) pr_info("%s(): shifting inet->saddr from %pI4 to %pI4\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) __func__, &old_saddr, &new_saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) inet->inet_saddr = inet->inet_rcv_saddr = new_saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) * XXX The only one ugly spot where we need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) * XXX really change the sockets identity after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) * XXX it has entered the hashes. -DaveM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) * Besides that, it does not check for connection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) * uniqueness. Wait for troubles.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) return __sk_prot_rehash(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) int inet_sk_rebuild_header(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) struct inet_sock *inet = inet_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) __be32 daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) struct ip_options_rcu *inet_opt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) struct flowi4 *fl4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) /* Route is OK, nothing to do. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) if (rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) /* Reroute. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) inet_opt = rcu_dereference(inet->inet_opt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) daddr = inet->inet_daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) if (inet_opt && inet_opt->opt.srr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) daddr = inet_opt->opt.faddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) fl4 = &inet->cork.fl.u.ip4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) rt = ip_route_output_ports(sock_net(sk), fl4, sk, daddr, inet->inet_saddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) inet->inet_dport, inet->inet_sport,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) sk->sk_protocol, RT_CONN_FLAGS(sk),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) sk->sk_bound_dev_if);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) if (!IS_ERR(rt)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) sk_setup_caps(sk, &rt->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) err = PTR_ERR(rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) /* Routing failed... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) sk->sk_route_caps = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) * Other protocols have to map its equivalent state to TCP_SYN_SENT.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) * DCCP maps its DCCP_REQUESTING state to TCP_SYN_SENT. -acme
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) if (!sock_net(sk)->ipv4.sysctl_ip_dynaddr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) sk->sk_state != TCP_SYN_SENT ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) (sk->sk_userlocks & SOCK_BINDADDR_LOCK) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) (err = inet_sk_reselect_saddr(sk)) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) sk->sk_err_soft = -err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) EXPORT_SYMBOL(inet_sk_rebuild_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) void inet_sk_set_state(struct sock *sk, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) trace_inet_sock_set_state(sk, sk->sk_state, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) sk->sk_state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) EXPORT_SYMBOL(inet_sk_set_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) void inet_sk_state_store(struct sock *sk, int newstate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) trace_inet_sock_set_state(sk, sk->sk_state, newstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) smp_store_release(&sk->sk_state, newstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) struct sk_buff *inet_gso_segment(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) netdev_features_t features)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) bool udpfrag = false, fixedid = false, gso_partial, encap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) struct sk_buff *segs = ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) const struct net_offload *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) unsigned int offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) struct iphdr *iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) int proto, tot_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) int nhoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) int ihl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) nhoff = skb_network_header(skb) - skb_mac_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) iph = ip_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) ihl = iph->ihl * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) if (ihl < sizeof(*iph))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) id = ntohs(iph->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) proto = iph->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) /* Warning: after this point, iph might be no longer valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) if (unlikely(!pskb_may_pull(skb, ihl)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) __skb_pull(skb, ihl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) encap = SKB_GSO_CB(skb)->encap_level > 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) if (encap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) features &= skb->dev->hw_enc_features;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) SKB_GSO_CB(skb)->encap_level += ihl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) skb_reset_transport_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) segs = ERR_PTR(-EPROTONOSUPPORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) if (!skb->encapsulation || encap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) udpfrag = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) fixedid = !!(skb_shinfo(skb)->gso_type & SKB_GSO_TCP_FIXEDID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) /* fixed ID is invalid if DF bit is not set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) if (fixedid && !(ip_hdr(skb)->frag_off & htons(IP_DF)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) ops = rcu_dereference(inet_offloads[proto]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) if (likely(ops && ops->callbacks.gso_segment)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) segs = ops->callbacks.gso_segment(skb, features);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) if (!segs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) skb->network_header = skb_mac_header(skb) + nhoff - skb->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) if (IS_ERR_OR_NULL(segs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) gso_partial = !!(skb_shinfo(segs)->gso_type & SKB_GSO_PARTIAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) skb = segs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) iph = (struct iphdr *)(skb_mac_header(skb) + nhoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) if (udpfrag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) iph->frag_off = htons(offset >> 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) if (skb->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) iph->frag_off |= htons(IP_MF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) offset += skb->len - nhoff - ihl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) tot_len = skb->len - nhoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) } else if (skb_is_gso(skb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) if (!fixedid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) iph->id = htons(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) id += skb_shinfo(skb)->gso_segs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) if (gso_partial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) tot_len = skb_shinfo(skb)->gso_size +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) SKB_GSO_CB(skb)->data_offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) skb->head - (unsigned char *)iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) tot_len = skb->len - nhoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) if (!fixedid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) iph->id = htons(id++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) tot_len = skb->len - nhoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) iph->tot_len = htons(tot_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) ip_send_check(iph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) if (encap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) skb_reset_inner_headers(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) skb->network_header = (u8 *)iph - skb->head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) skb_reset_mac_len(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) } while ((skb = skb->next));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) return segs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) EXPORT_SYMBOL(inet_gso_segment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) static struct sk_buff *ipip_gso_segment(struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) netdev_features_t features)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) if (!(skb_shinfo(skb)->gso_type & SKB_GSO_IPXIP4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) return inet_gso_segment(skb, features);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) struct sk_buff *inet_gro_receive(struct list_head *head, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) const struct net_offload *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) struct sk_buff *pp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) const struct iphdr *iph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) struct sk_buff *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) unsigned int hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) unsigned int off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) unsigned int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) int flush = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) int proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) off = skb_gro_offset(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) hlen = off + sizeof(*iph);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) iph = skb_gro_header_fast(skb, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) if (skb_gro_header_hard(skb, hlen)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) iph = skb_gro_header_slow(skb, hlen, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) if (unlikely(!iph))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) proto = iph->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) ops = rcu_dereference(inet_offloads[proto]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) if (!ops || !ops->callbacks.gro_receive)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) if (*(u8 *)iph != 0x45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) if (ip_is_fragment(iph))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) if (unlikely(ip_fast_csum((u8 *)iph, 5)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) id = ntohl(*(__be32 *)&iph->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) flush = (u16)((ntohl(*(__be32 *)iph) ^ skb_gro_len(skb)) | (id & ~IP_DF));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) id >>= 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) list_for_each_entry(p, head, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) struct iphdr *iph2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) u16 flush_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) if (!NAPI_GRO_CB(p)->same_flow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) iph2 = (struct iphdr *)(p->data + off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) /* The above works because, with the exception of the top
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) * (inner most) layer, we only aggregate pkts with the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) * hdr length so all the hdrs we'll need to verify will start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) * at the same offset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) if ((iph->protocol ^ iph2->protocol) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) ((__force u32)iph->saddr ^ (__force u32)iph2->saddr) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) ((__force u32)iph->daddr ^ (__force u32)iph2->daddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) NAPI_GRO_CB(p)->same_flow = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) /* All fields must match except length and checksum. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) NAPI_GRO_CB(p)->flush |=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) (iph->ttl ^ iph2->ttl) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) (iph->tos ^ iph2->tos) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) ((iph->frag_off ^ iph2->frag_off) & htons(IP_DF));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) NAPI_GRO_CB(p)->flush |= flush;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) /* We need to store of the IP ID check to be included later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) * when we can verify that this packet does in fact belong
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) * to a given flow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) flush_id = (u16)(id - ntohs(iph2->id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) /* This bit of code makes it much easier for us to identify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) * the cases where we are doing atomic vs non-atomic IP ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) * checks. Specifically an atomic check can return IP ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) * values 0 - 0xFFFF, while a non-atomic check can only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) * return 0 or 0xFFFF.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) if (!NAPI_GRO_CB(p)->is_atomic ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) !(iph->frag_off & htons(IP_DF))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) flush_id ^= NAPI_GRO_CB(p)->count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) flush_id = flush_id ? 0xFFFF : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) /* If the previous IP ID value was based on an atomic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) * datagram we can overwrite the value and ignore it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) if (NAPI_GRO_CB(skb)->is_atomic)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) NAPI_GRO_CB(p)->flush_id = flush_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) NAPI_GRO_CB(p)->flush_id |= flush_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) NAPI_GRO_CB(skb)->is_atomic = !!(iph->frag_off & htons(IP_DF));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) NAPI_GRO_CB(skb)->flush |= flush;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) skb_set_network_header(skb, off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) /* The above will be needed by the transport layer if there is one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) * immediately following this IP hdr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) /* Note : No need to call skb_gro_postpull_rcsum() here,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) * as we already checked checksum over ipv4 header was 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) skb_gro_pull(skb, sizeof(*iph));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) skb_set_transport_header(skb, skb_gro_offset(skb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) pp = indirect_call_gro_receive(tcp4_gro_receive, udp4_gro_receive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) ops->callbacks.gro_receive, head, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) skb_gro_flush_final(skb, pp, flush);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) return pp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) EXPORT_SYMBOL(inet_gro_receive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) static struct sk_buff *ipip_gro_receive(struct list_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) if (NAPI_GRO_CB(skb)->encap_mark) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) NAPI_GRO_CB(skb)->flush = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) NAPI_GRO_CB(skb)->encap_mark = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) return inet_gro_receive(head, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) #define SECONDS_PER_DAY 86400
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) /* inet_current_timestamp - Return IP network timestamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) * Return milliseconds since midnight in network byte order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) __be32 inet_current_timestamp(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) u32 secs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) u32 msecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) struct timespec64 ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) ktime_get_real_ts64(&ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) /* Get secs since midnight. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) (void)div_u64_rem(ts.tv_sec, SECONDS_PER_DAY, &secs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) /* Convert to msecs. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) msecs = secs * MSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) /* Convert nsec to msec. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) msecs += (u32)ts.tv_nsec / NSEC_PER_MSEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) /* Convert to network byte order. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) return htonl(msecs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) EXPORT_SYMBOL(inet_current_timestamp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) int inet_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) if (sk->sk_family == AF_INET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) return ip_recv_error(sk, msg, len, addr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) #if IS_ENABLED(CONFIG_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) if (sk->sk_family == AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) return pingv6_ops.ipv6_recv_error(sk, msg, len, addr_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) int inet_gro_complete(struct sk_buff *skb, int nhoff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) __be16 newlen = htons(skb->len - nhoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) struct iphdr *iph = (struct iphdr *)(skb->data + nhoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) const struct net_offload *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) int proto = iph->protocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) int err = -ENOSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) if (skb->encapsulation) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) skb_set_inner_protocol(skb, cpu_to_be16(ETH_P_IP));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) skb_set_inner_network_header(skb, nhoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) csum_replace2(&iph->check, iph->tot_len, newlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) iph->tot_len = newlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) ops = rcu_dereference(inet_offloads[proto]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) if (WARN_ON(!ops || !ops->callbacks.gro_complete))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) /* Only need to add sizeof(*iph) to get to the next hdr below
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) * because any hdr with option will have been flushed in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) * inet_gro_receive().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) err = INDIRECT_CALL_2(ops->callbacks.gro_complete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) tcp4_gro_complete, udp4_gro_complete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) skb, nhoff + sizeof(*iph));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) EXPORT_SYMBOL(inet_gro_complete);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) static int ipip_gro_complete(struct sk_buff *skb, int nhoff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) skb->encapsulation = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) skb_shinfo(skb)->gso_type |= SKB_GSO_IPXIP4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) return inet_gro_complete(skb, nhoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) int inet_ctl_sock_create(struct sock **sk, unsigned short family,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) unsigned short type, unsigned char protocol,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) struct socket *sock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) int rc = sock_create_kern(net, family, type, protocol, &sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) if (rc == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) (*sk)->sk_allocation = GFP_ATOMIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) * Unhash it so that IP input processing does not even see it,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) * we do not wish this socket to see incoming packets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) (*sk)->sk_prot->unhash(*sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) EXPORT_SYMBOL_GPL(inet_ctl_sock_create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) u64 snmp_get_cpu_field(void __percpu *mib, int cpu, int offt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) return *(((unsigned long *)per_cpu_ptr(mib, cpu)) + offt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) EXPORT_SYMBOL_GPL(snmp_get_cpu_field);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) unsigned long snmp_fold_field(void __percpu *mib, int offt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) unsigned long res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) for_each_possible_cpu(i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) res += snmp_get_cpu_field(mib, i, offt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) EXPORT_SYMBOL_GPL(snmp_fold_field);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) #if BITS_PER_LONG==32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) u64 snmp_get_cpu_field64(void __percpu *mib, int cpu, int offt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) size_t syncp_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) void *bhptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) struct u64_stats_sync *syncp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) u64 v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) unsigned int start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) bhptr = per_cpu_ptr(mib, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) syncp = (struct u64_stats_sync *)(bhptr + syncp_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) start = u64_stats_fetch_begin_irq(syncp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) v = *(((u64 *)bhptr) + offt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) } while (u64_stats_fetch_retry_irq(syncp, start));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) return v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) EXPORT_SYMBOL_GPL(snmp_get_cpu_field64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) u64 snmp_fold_field64(void __percpu *mib, int offt, size_t syncp_offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) u64 res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) res += snmp_get_cpu_field64(mib, cpu, offt, syncp_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) EXPORT_SYMBOL_GPL(snmp_fold_field64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) #ifdef CONFIG_IP_MULTICAST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) static const struct net_protocol igmp_protocol = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) .handler = igmp_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) .netns_ok = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) /* thinking of making this const? Don't.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) * early_demux can change based on sysctl.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) static struct net_protocol tcp_protocol = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) .early_demux = tcp_v4_early_demux,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) .early_demux_handler = tcp_v4_early_demux,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) .handler = tcp_v4_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) .err_handler = tcp_v4_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) .no_policy = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) .netns_ok = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) .icmp_strict_tag_validation = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) /* thinking of making this const? Don't.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) * early_demux can change based on sysctl.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) static struct net_protocol udp_protocol = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) .early_demux = udp_v4_early_demux,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) .early_demux_handler = udp_v4_early_demux,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) .handler = udp_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) .err_handler = udp_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) .no_policy = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) .netns_ok = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) static const struct net_protocol icmp_protocol = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) .handler = icmp_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) .err_handler = icmp_err,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) .no_policy = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) .netns_ok = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) static __net_init int ipv4_mib_init_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) net->mib.tcp_statistics = alloc_percpu(struct tcp_mib);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) if (!net->mib.tcp_statistics)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) goto err_tcp_mib;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) net->mib.ip_statistics = alloc_percpu(struct ipstats_mib);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) if (!net->mib.ip_statistics)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) goto err_ip_mib;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) for_each_possible_cpu(i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) struct ipstats_mib *af_inet_stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) af_inet_stats = per_cpu_ptr(net->mib.ip_statistics, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) u64_stats_init(&af_inet_stats->syncp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) net->mib.net_statistics = alloc_percpu(struct linux_mib);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) if (!net->mib.net_statistics)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) goto err_net_mib;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) net->mib.udp_statistics = alloc_percpu(struct udp_mib);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) if (!net->mib.udp_statistics)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) goto err_udp_mib;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) net->mib.udplite_statistics = alloc_percpu(struct udp_mib);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) if (!net->mib.udplite_statistics)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) goto err_udplite_mib;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) net->mib.icmp_statistics = alloc_percpu(struct icmp_mib);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) if (!net->mib.icmp_statistics)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) goto err_icmp_mib;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) net->mib.icmpmsg_statistics = kzalloc(sizeof(struct icmpmsg_mib),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) if (!net->mib.icmpmsg_statistics)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) goto err_icmpmsg_mib;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) tcp_mib_init(net);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) err_icmpmsg_mib:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) free_percpu(net->mib.icmp_statistics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) err_icmp_mib:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) free_percpu(net->mib.udplite_statistics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) err_udplite_mib:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) free_percpu(net->mib.udp_statistics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) err_udp_mib:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) free_percpu(net->mib.net_statistics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) err_net_mib:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) free_percpu(net->mib.ip_statistics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) err_ip_mib:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) free_percpu(net->mib.tcp_statistics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) err_tcp_mib:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) static __net_exit void ipv4_mib_exit_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) kfree(net->mib.icmpmsg_statistics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) free_percpu(net->mib.icmp_statistics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) free_percpu(net->mib.udplite_statistics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) free_percpu(net->mib.udp_statistics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) free_percpu(net->mib.net_statistics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) free_percpu(net->mib.ip_statistics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) free_percpu(net->mib.tcp_statistics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) #ifdef CONFIG_MPTCP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) /* allocated on demand, see mptcp_init_sock() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) free_percpu(net->mib.mptcp_statistics);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) static __net_initdata struct pernet_operations ipv4_mib_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) .init = ipv4_mib_init_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) .exit = ipv4_mib_exit_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) static int __init init_ipv4_mibs(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) return register_pernet_subsys(&ipv4_mib_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) static __net_init int inet_init_net(struct net *net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) * Set defaults for local port range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) seqlock_init(&net->ipv4.ip_local_ports.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) net->ipv4.ip_local_ports.range[0] = 32768;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) net->ipv4.ip_local_ports.range[1] = 60999;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) seqlock_init(&net->ipv4.ping_group_range.lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) * Sane defaults - nobody may create ping sockets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) * Boot scripts should set this to distro-specific group.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) net->ipv4.ping_group_range.range[0] = make_kgid(&init_user_ns, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) net->ipv4.ping_group_range.range[1] = make_kgid(&init_user_ns, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) /* Default values for sysctl-controlled parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) * We set them here, in case sysctl is not compiled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) net->ipv4.sysctl_ip_default_ttl = IPDEFTTL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) net->ipv4.sysctl_ip_fwd_update_priority = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) net->ipv4.sysctl_ip_dynaddr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) net->ipv4.sysctl_ip_early_demux = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) net->ipv4.sysctl_udp_early_demux = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) net->ipv4.sysctl_tcp_early_demux = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) net->ipv4.sysctl_nexthop_compat_mode = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) #ifdef CONFIG_SYSCTL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) net->ipv4.sysctl_ip_prot_sock = PROT_SOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) /* Some igmp sysctl, whose values are always used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) net->ipv4.sysctl_igmp_max_memberships = 20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) net->ipv4.sysctl_igmp_max_msf = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) /* IGMP reports for link-local multicast groups are enabled by default */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) net->ipv4.sysctl_igmp_llm_reports = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) net->ipv4.sysctl_igmp_qrv = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) static __net_initdata struct pernet_operations af_inet_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) .init = inet_init_net,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) static int __init init_inet_pernet_ops(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) return register_pernet_subsys(&af_inet_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) static int ipv4_proc_init(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) * IP protocol layer initialiser
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) static struct packet_offload ip_packet_offload __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) .type = cpu_to_be16(ETH_P_IP),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) .callbacks = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) .gso_segment = inet_gso_segment,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) .gro_receive = inet_gro_receive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) .gro_complete = inet_gro_complete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) static const struct net_offload ipip_offload = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) .callbacks = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) .gso_segment = ipip_gso_segment,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) .gro_receive = ipip_gro_receive,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) .gro_complete = ipip_gro_complete,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) static int __init ipip_offload_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) return inet_add_offload(&ipip_offload, IPPROTO_IPIP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) static int __init ipv4_offload_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) * Add offloads
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) if (udpv4_offload_init() < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) pr_crit("%s: Cannot add UDP protocol offload\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) if (tcpv4_offload_init() < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) if (ipip_offload_init() < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) pr_crit("%s: Cannot add IPIP protocol offload\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) dev_add_offload(&ip_packet_offload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) fs_initcall(ipv4_offload_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) static struct packet_type ip_packet_type __read_mostly = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) .type = cpu_to_be16(ETH_P_IP),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) .func = ip_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) .list_func = ip_list_rcv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) static int __init inet_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) struct inet_protosw *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) struct list_head *r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) sock_skb_cb_check_size(sizeof(struct inet_skb_parm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) rc = proto_register(&tcp_prot, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) rc = proto_register(&udp_prot, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) goto out_unregister_tcp_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) rc = proto_register(&raw_prot, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) goto out_unregister_udp_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) rc = proto_register(&ping_prot, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) goto out_unregister_raw_proto;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) * Tell SOCKET that we are alive...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) (void)sock_register(&inet_family_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) #ifdef CONFIG_SYSCTL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) ip_static_sysctl_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) * Add all the base protocols.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) pr_crit("%s: Cannot add ICMP protocol\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) pr_crit("%s: Cannot add UDP protocol\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) pr_crit("%s: Cannot add TCP protocol\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) #ifdef CONFIG_IP_MULTICAST
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) pr_crit("%s: Cannot add IGMP protocol\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) /* Register the socket-side information for inet_create. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) INIT_LIST_HEAD(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) inet_register_protosw(q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) * Set the ARP module up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) arp_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) * Set the IP module up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) ip_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) /* Initialise per-cpu ipv4 mibs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) if (init_ipv4_mibs())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) panic("%s: Cannot init ipv4 mibs\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) /* Setup TCP slab cache for open requests. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) tcp_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) /* Setup UDP memory threshold */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) udp_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) /* Add UDP-Lite (RFC 3828) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) udplite4_register();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) raw_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) ping_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) * Set the ICMP layer up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) if (icmp_init() < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) panic("Failed to create the ICMP control socket.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) * Initialise the multicast router
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) #if defined(CONFIG_IP_MROUTE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) if (ip_mr_init())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) pr_crit("%s: Cannot init ipv4 mroute\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) if (init_inet_pernet_ops())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) pr_crit("%s: Cannot init ipv4 inet pernet ops\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) ipv4_proc_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) ipfrag_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) dev_add_pack(&ip_packet_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) ip_tunnel_core_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) out_unregister_raw_proto:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) proto_unregister(&raw_prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) out_unregister_udp_proto:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) proto_unregister(&udp_prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) out_unregister_tcp_proto:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) proto_unregister(&tcp_prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) fs_initcall(inet_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) /* ------------------------------------------------------------------------ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) #ifdef CONFIG_PROC_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) static int __init ipv4_proc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) if (raw_proc_init())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) goto out_raw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) if (tcp4_proc_init())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) goto out_tcp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) if (udp4_proc_init())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) goto out_udp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) if (ping_proc_init())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) goto out_ping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) if (ip_misc_proc_init())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) goto out_misc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) out_misc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) ping_proc_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) out_ping:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) udp4_proc_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) out_udp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) tcp4_proc_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) out_tcp:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) raw_proc_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) out_raw:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) #else /* CONFIG_PROC_FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) static int __init ipv4_proc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) #endif /* CONFIG_PROC_FS */