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