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