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