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.
6 * PF_INET protocol family socket handler.
8 * Version: $Id: af_inet.c,v 1.137 2002/02/01 22:01:03 davem Exp $
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Florian La Roche, <flla@stud.uni-sb.de>
13 * Alan Cox, <A.Cox@swansea.ac.uk>
15 * Changes (see also sock.c)
18 * Karl Knutson : Socket protocol table
19 * A.N.Kuznetsov : Socket death error in accept().
20 * John Richardson : Fix non blocking error in connect()
21 * so sockets that fail to connect
22 * don't return -EINPROGRESS.
23 * Alan Cox : Asynchronous I/O support
24 * Alan Cox : Keep correct socket pointer on sock
27 * Alan Cox : Semantics of SO_LINGER aren't state
28 * moved to close when you look carefully.
29 * With this fixed and the accept bug fixed
30 * some RPC stuff seems happier.
31 * Niibe Yutaka : 4.4BSD style write async I/O
33 * Tony Gale : Fixed reuse semantics.
34 * Alan Cox : bind() shouldn't abort existing but dead
35 * sockets. Stops FTP netin:.. I hope.
36 * Alan Cox : bind() works correctly for RAW sockets.
37 * Note that FreeBSD at least was broken
38 * in this respect so be careful with
39 * compatibility tests...
40 * Alan Cox : routing cache support
41 * Alan Cox : memzero the socket structure for
43 * Matt Day : nonblock connect error handler
44 * Alan Cox : Allow large numbers of pending sockets
45 * (eg for big web sites), but only if
46 * specifically application requested.
47 * Alan Cox : New buffering throughout IP. Used
49 * Alan Cox : New buffering now used smartly.
50 * Alan Cox : BSD rather than common sense
51 * interpretation of listen.
52 * Germano Caronni : Assorted small races.
53 * Alan Cox : sendmsg/recvmsg basic support.
54 * Alan Cox : Only sendmsg/recvmsg now supported.
55 * Alan Cox : Locked down bind (see security list).
56 * Alan Cox : Loosened bind a little.
57 * Mike McLagan : ADD/DEL DLCI Ioctls
58 * Willy Konynenberg : Transparent proxying support.
59 * David S. Miller : New socket lookup architecture.
60 * Some other random speedups.
61 * Cyrus Durgin : Cleaned up file for kmod hacks.
62 * Andi Kleen : Fix inet_stream_connect TCP race.
64 * This program is free software; you can redistribute it and/or
65 * modify it under the terms of the GNU General Public License
66 * as published by the Free Software Foundation; either version
67 * 2 of the License, or (at your option) any later version.
70 #include <linux/config.h>
71 #include <linux/errno.h>
72 #include <linux/types.h>
73 #include <linux/socket.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>
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>
91 #include <asm/uaccess.h>
92 #include <asm/system.h>
94 #include <linux/smp_lock.h>
95 #include <linux/inet.h>
96 #include <linux/igmp.h>
97 #include <linux/inetdevice.h>
98 #include <linux/netdevice.h>
100 #include <net/protocol.h>
102 #include <net/route.h>
103 #include <net/ip_fib.h>
104 #include <net/inet_connection_sock.h>
107 #include <linux/skbuff.h>
108 #include <net/sock.h>
110 #include <net/icmp.h>
111 #include <net/ipip.h>
112 #include <net/inet_common.h>
113 #include <net/xfrm.h>
114 #ifdef CONFIG_IP_MROUTE
115 #include <linux/mroute.h>
117 #include <linux/vs_limit.h>
119 DEFINE_SNMP_STAT(struct linux_mib, net_statistics) __read_mostly;
121 extern void ip_mc_drop_socket(struct sock *sk);
123 /* The inetsw table contains everything that inet_create needs to
124 * build a new socket.
126 static struct list_head inetsw[SOCK_MAX];
127 static DEFINE_SPINLOCK(inetsw_lock);
129 /* New destruction routine */
131 void inet_sock_destruct(struct sock *sk)
133 struct inet_sock *inet = inet_sk(sk);
135 __skb_queue_purge(&sk->sk_receive_queue);
136 __skb_queue_purge(&sk->sk_error_queue);
138 if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
139 printk("Attempt to release TCP socket in state %d %p\n",
143 if (!sock_flag(sk, SOCK_DEAD)) {
144 printk("Attempt to release alive inet socket %p\n", sk);
148 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
149 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
150 BUG_TRAP(!sk->sk_wmem_queued);
151 BUG_TRAP(!sk->sk_forward_alloc);
154 dst_release(sk->sk_dst_cache);
155 sk_refcnt_debug_dec(sk);
159 * The routines beyond this point handle the behaviour of an AF_INET
160 * socket object. Mostly it punts to the subprotocols of IP to do
165 * Automatically bind an unbound socket.
168 static int inet_autobind(struct sock *sk)
170 struct inet_sock *inet;
171 /* We may need to bind the socket. */
175 if (sk->sk_prot->get_port(sk, 0)) {
179 inet->sport = htons(inet->num);
186 * Move a socket into listening state.
188 int inet_listen(struct socket *sock, int backlog)
190 struct sock *sk = sock->sk;
191 unsigned char old_state;
197 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
200 old_state = sk->sk_state;
201 if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
204 /* Really, if the socket is already in listen state
205 * we can only allow the backlog to be adjusted.
207 if (old_state != TCP_LISTEN) {
208 err = inet_csk_listen_start(sk, TCP_SYNQ_HSIZE);
212 sk->sk_max_ack_backlog = backlog;
221 * Create an inet socket.
224 static int inet_create(struct socket *sock, int protocol)
228 struct inet_protosw *answer;
229 struct inet_sock *inet;
230 struct proto *answer_prot;
231 unsigned char answer_flags;
232 char answer_no_check;
233 int try_loading_module = 0;
236 sock->state = SS_UNCONNECTED;
238 /* Look for the requested type/protocol pair. */
241 err = -ESOCKTNOSUPPORT;
243 list_for_each_rcu(p, &inetsw[sock->type]) {
244 answer = list_entry(p, struct inet_protosw, list);
246 /* Check the non-wild match. */
247 if (protocol == answer->protocol) {
248 if (protocol != IPPROTO_IP)
251 /* Check for the two wild cases. */
252 if (IPPROTO_IP == protocol) {
253 protocol = answer->protocol;
256 if (IPPROTO_IP == answer->protocol)
259 err = -EPROTONOSUPPORT;
263 if (unlikely(answer == NULL)) {
264 if (try_loading_module < 2) {
267 * Be more specific, e.g. net-pf-2-proto-132-type-1
268 * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM)
270 if (++try_loading_module == 1)
271 request_module("net-pf-%d-proto-%d-type-%d",
272 PF_INET, protocol, sock->type);
274 * Fall back to generic, e.g. net-pf-2-proto-132
275 * (net-pf-PF_INET-proto-IPPROTO_SCTP)
278 request_module("net-pf-%d-proto-%d",
280 goto lookup_protocol;
286 if ((protocol == IPPROTO_ICMP) && vx_ccaps(VXC_RAW_ICMP))
288 if (answer->capability > 0 && !capable(answer->capability))
291 sock->ops = answer->ops;
292 answer_prot = answer->prot;
293 answer_no_check = answer->no_check;
294 answer_flags = answer->flags;
297 BUG_TRAP(answer_prot->slab != NULL);
300 sk = sk_alloc(PF_INET, GFP_KERNEL, answer_prot, 1);
305 sk->sk_no_check = answer_no_check;
306 if (INET_PROTOSW_REUSE & answer_flags)
310 inet->is_icsk = INET_PROTOSW_ICSK & answer_flags;
312 if (SOCK_RAW == sock->type) {
313 inet->num = protocol;
314 if (IPPROTO_RAW == protocol)
318 if (ipv4_config.no_pmtu_disc)
319 inet->pmtudisc = IP_PMTUDISC_DONT;
321 inet->pmtudisc = IP_PMTUDISC_WANT;
325 sock_init_data(sock, sk);
327 sk->sk_destruct = inet_sock_destruct;
328 sk->sk_family = PF_INET;
329 sk->sk_protocol = protocol;
330 sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
336 inet->mc_list = NULL;
338 sk_refcnt_debug_inc(sk);
341 /* It assumes that any protocol which allows
342 * the user to assign a number at socket
343 * creation time automatically
346 inet->sport = htons(inet->num);
347 /* Add to protocol hash chains. */
348 sk->sk_prot->hash(sk);
351 if (sk->sk_prot->init) {
352 err = sk->sk_prot->init(sk);
354 sk_common_release(sk);
365 * The peer socket should always be NULL (or else). When we call this
366 * function we are destroying the object and from then on nobody
367 * should refer to it.
369 int inet_release(struct socket *sock)
371 struct sock *sk = sock->sk;
376 /* Applications forget to leave groups before exiting */
377 ip_mc_drop_socket(sk);
379 /* If linger is set, we don't return until the close
380 * is complete. Otherwise we return immediately. The
381 * actually closing is done the same either way.
383 * If the close is due to the process exiting, we never
387 if (sock_flag(sk, SOCK_LINGER) &&
388 !(current->flags & PF_EXITING))
389 timeout = sk->sk_lingertime;
391 sk->sk_prot->close(sk, timeout);
396 /* It is off by default, see below. */
397 int sysctl_ip_nonlocal_bind;
399 int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
401 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
402 struct sock *sk = sock->sk;
403 struct inet_sock *inet = inet_sk(sk);
407 __u32 s_addr; /* Address used for validation */
408 __u32 s_addr1; /* Address used for socket */
409 __u32 s_addr2; /* Broadcast address for the socket */
410 struct nx_info *nxi = sk->sk_nx_info;
412 /* If the socket has its own bind function then use it. (RAW) */
413 if (sk->sk_prot->bind) {
414 err = sk->sk_prot->bind(sk, uaddr, addr_len);
418 if (addr_len < sizeof(struct sockaddr_in))
421 s_addr = addr->sin_addr.s_addr;
423 s_addr2 = 0xffffffffl;
425 vxdprintk(VXD_CBIT(net, 3),
426 "inet_bind(%p)* %p,%p;%lx %d.%d.%d.%d",
427 sk, sk->sk_nx_info, sk->sk_socket,
428 (sk->sk_socket?sk->sk_socket->flags:0),
431 __u32 v4_bcast = nxi->v4_bcast;
432 __u32 ipv4root = nxi->ipv4[0];
433 int nbipv4 = nxi->nbipv4;
436 /* bind to any for 1-n */
438 s_addr1 = (nbipv4 > 1) ? 0 : s_addr;
440 } else if (s_addr == IPI_LOOPBACK) {
441 /* rewrite localhost to ipv4root */
444 } else if (s_addr != v4_bcast) {
445 /* normal address bind */
446 if (!addr_in_nx_info(nxi, s_addr))
447 return -EADDRNOTAVAIL;
450 chk_addr_ret = inet_addr_type(s_addr);
452 vxdprintk(VXD_CBIT(net, 3),
453 "inet_bind(%p) %d.%d.%d.%d, %d.%d.%d.%d, %d.%d.%d.%d",
454 sk, VXD_QUAD(s_addr), VXD_QUAD(s_addr1), VXD_QUAD(s_addr2));
456 /* Not specified by any standard per-se, however it breaks too
457 * many applications when removed. It is unfortunate since
458 * allowing applications to make a non-local bind solves
459 * several problems with systems using dynamic addressing.
460 * (ie. your servers still start up even if your ISDN link
461 * is temporarily down)
463 err = -EADDRNOTAVAIL;
464 if (!sysctl_ip_nonlocal_bind &&
466 s_addr != INADDR_ANY &&
467 chk_addr_ret != RTN_LOCAL &&
468 chk_addr_ret != RTN_MULTICAST &&
469 chk_addr_ret != RTN_BROADCAST)
472 snum = ntohs(addr->sin_port);
474 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
477 /* We keep a pair of addresses. rcv_saddr is the one
478 * used by hash lookups, and saddr is used for transmit.
480 * In the BSD API these are the same except where it
481 * would be illegal to use them (multicast/broadcast) in
482 * which case the sending device address is used.
486 /* Check these errors (active socket, double bind). */
488 if (sk->sk_state != TCP_CLOSE || inet->num)
489 goto out_release_sock;
491 inet->rcv_saddr = inet->saddr = s_addr1;
492 inet->rcv_saddr2 = s_addr2;
493 if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
494 inet->saddr = 0; /* Use device */
496 /* Make sure we are allowed to bind here. */
497 if (sk->sk_prot->get_port(sk, snum)) {
498 inet->saddr = inet->rcv_saddr = 0;
500 goto out_release_sock;
504 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
506 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
507 inet->sport = htons(inet->num);
518 int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
519 int addr_len, int flags)
521 struct sock *sk = sock->sk;
523 if (uaddr->sa_family == AF_UNSPEC)
524 return sk->sk_prot->disconnect(sk, flags);
526 if (!inet_sk(sk)->num && inet_autobind(sk))
528 return sk->sk_prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
531 static long inet_wait_for_connect(struct sock *sk, long timeo)
535 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
537 /* Basic assumption: if someone sets sk->sk_err, he _must_
538 * change state of the socket from TCP_SYN_*.
539 * Connect() does not allow to get error notifications
540 * without closing the socket.
542 while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
544 timeo = schedule_timeout(timeo);
546 if (signal_pending(current) || !timeo)
548 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
550 finish_wait(sk->sk_sleep, &wait);
555 * Connect to a remote host. There is regrettably still a little
556 * TCP 'magic' in here.
558 int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
559 int addr_len, int flags)
561 struct sock *sk = sock->sk;
567 if (uaddr->sa_family == AF_UNSPEC) {
568 err = sk->sk_prot->disconnect(sk, flags);
569 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
573 switch (sock->state) {
582 /* Fall out of switch with err, set for this state */
586 if (sk->sk_state != TCP_CLOSE)
589 err = sk->sk_prot->connect(sk, uaddr, addr_len);
593 sock->state = SS_CONNECTING;
595 /* Just entered SS_CONNECTING state; the only
596 * difference is that return value in non-blocking
597 * case is EINPROGRESS, rather than EALREADY.
603 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
605 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
606 /* Error code is set above */
607 if (!timeo || !inet_wait_for_connect(sk, timeo))
610 err = sock_intr_errno(timeo);
611 if (signal_pending(current))
615 /* Connection was closed by RST, timeout, ICMP error
616 * or another process disconnected us.
618 if (sk->sk_state == TCP_CLOSE)
621 /* sk->sk_err may be not zero now, if RECVERR was ordered by user
622 * and error was received after socket entered established state.
623 * Hence, it is handled normally after connect() return successfully.
626 sock->state = SS_CONNECTED;
633 err = sock_error(sk) ? : -ECONNABORTED;
634 sock->state = SS_UNCONNECTED;
635 if (sk->sk_prot->disconnect(sk, flags))
636 sock->state = SS_DISCONNECTING;
641 * Accept a pending connection. The TCP layer now gives BSD semantics.
644 int inet_accept(struct socket *sock, struct socket *newsock, int flags)
646 struct sock *sk1 = sock->sk;
648 struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err);
655 BUG_TRAP((1 << sk2->sk_state) &
656 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_CLOSE));
658 sock_graft(sk2, newsock);
660 newsock->state = SS_CONNECTED;
669 * This does both peername and sockname.
671 int inet_getname(struct socket *sock, struct sockaddr *uaddr,
672 int *uaddr_len, int peer)
674 struct sock *sk = sock->sk;
675 struct inet_sock *inet = inet_sk(sk);
676 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
678 sin->sin_family = AF_INET;
681 (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
684 sin->sin_port = inet->dport;
685 sin->sin_addr.s_addr = inet->daddr;
687 __u32 addr = inet->rcv_saddr;
690 sin->sin_port = inet->sport;
691 sin->sin_addr.s_addr = addr;
693 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
694 *uaddr_len = sizeof(*sin);
698 int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
701 struct sock *sk = sock->sk;
703 /* We may need to bind the socket. */
704 if (!inet_sk(sk)->num && inet_autobind(sk))
707 return sk->sk_prot->sendmsg(iocb, sk, msg, size);
711 static ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
713 struct sock *sk = sock->sk;
715 /* We may need to bind the socket. */
716 if (!inet_sk(sk)->num && inet_autobind(sk))
719 if (sk->sk_prot->sendpage)
720 return sk->sk_prot->sendpage(sk, page, offset, size, flags);
721 return sock_no_sendpage(sock, page, offset, size, flags);
725 int inet_shutdown(struct socket *sock, int how)
727 struct sock *sk = sock->sk;
730 /* This should really check to make sure
731 * the socket is a TCP socket. (WHY AC...)
733 how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
736 if ((how & ~SHUTDOWN_MASK) || !how) /* MAXINT->0 */
740 if (sock->state == SS_CONNECTING) {
741 if ((1 << sk->sk_state) &
742 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
743 sock->state = SS_DISCONNECTING;
745 sock->state = SS_CONNECTED;
748 switch (sk->sk_state) {
751 /* Hack to wake up other listeners, who can poll for
752 POLLHUP, even on eg. unconnected UDP sockets -- RR */
754 sk->sk_shutdown |= how;
755 if (sk->sk_prot->shutdown)
756 sk->sk_prot->shutdown(sk, how);
759 /* Remaining two branches are temporary solution for missing
760 * close() in multithreaded environment. It is _not_ a good idea,
761 * but we have no choice until close() is repaired at VFS level.
764 if (!(how & RCV_SHUTDOWN))
768 err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
769 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
773 /* Wake up anyone sleeping in poll. */
774 sk->sk_state_change(sk);
780 * ioctl() calls you can issue on an INET socket. Most of these are
781 * device configuration and stuff and very rarely used. Some ioctls
782 * pass on to the socket itself.
784 * NOTE: I like the idea of a module for the config stuff. ie ifconfig
785 * loads the devconfigure module does its configuring and unloads it.
786 * There's a good 20K of config code hanging around the kernel.
789 int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
791 struct sock *sk = sock->sk;
796 err = sock_get_timestamp(sk, (struct timeval __user *)arg);
801 err = ip_rt_ioctl(cmd, (void __user *)arg);
806 err = arp_ioctl(cmd, (void __user *)arg);
819 err = devinet_ioctl(cmd, (void __user *)arg);
822 if (sk->sk_prot->ioctl)
823 err = sk->sk_prot->ioctl(sk, cmd, arg);
831 #if !defined(CONFIG_VNET) && !defined(CONFIG_VNET_MODULE)
834 struct proto_ops inet_stream_ops = {
836 .owner = THIS_MODULE,
837 .release = inet_release,
839 .connect = inet_stream_connect,
840 .socketpair = sock_no_socketpair,
841 .accept = inet_accept,
842 .getname = inet_getname,
845 .listen = inet_listen,
846 .shutdown = inet_shutdown,
847 .setsockopt = sock_common_setsockopt,
848 .getsockopt = sock_common_getsockopt,
849 .sendmsg = inet_sendmsg,
850 .recvmsg = sock_common_recvmsg,
851 .mmap = sock_no_mmap,
852 .sendpage = tcp_sendpage
855 #if !defined(CONFIG_VNET) && !defined(CONFIG_VNET_MODULE)
858 struct proto_ops inet_dgram_ops = {
860 .owner = THIS_MODULE,
861 .release = inet_release,
863 .connect = inet_dgram_connect,
864 .socketpair = sock_no_socketpair,
865 .accept = sock_no_accept,
866 .getname = inet_getname,
869 .listen = sock_no_listen,
870 .shutdown = inet_shutdown,
871 .setsockopt = sock_common_setsockopt,
872 .getsockopt = sock_common_getsockopt,
873 .sendmsg = inet_sendmsg,
874 .recvmsg = sock_common_recvmsg,
875 .mmap = sock_no_mmap,
876 .sendpage = inet_sendpage,
880 * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without
883 #if defined(CONFIG_VNET) || defined(CONFIG_VNET_MODULE)
884 struct proto_ops inet_sockraw_ops;
885 EXPORT_SYMBOL(inet_sockraw_ops);
889 struct proto_ops inet_sockraw_ops = {
891 .owner = THIS_MODULE,
892 .release = inet_release,
894 .connect = inet_dgram_connect,
895 .socketpair = sock_no_socketpair,
896 .accept = sock_no_accept,
897 .getname = inet_getname,
898 .poll = datagram_poll,
900 .listen = sock_no_listen,
901 .shutdown = inet_shutdown,
902 .setsockopt = sock_common_setsockopt,
903 .getsockopt = sock_common_getsockopt,
904 .sendmsg = inet_sendmsg,
905 .recvmsg = sock_common_recvmsg,
906 .mmap = sock_no_mmap,
907 .sendpage = inet_sendpage,
910 #if defined(CONFIG_VNET) || defined(CONFIG_VNET_MODULE)
912 EXPORT_SYMBOL(vnet_active);
913 struct net_proto_family inet_family_ops;
914 EXPORT_SYMBOL(inet_family_ops);
918 struct net_proto_family inet_family_ops = {
920 .create = inet_create,
921 .owner = THIS_MODULE,
924 /* Upon startup we insert all the elements in inetsw_array[] into
925 * the linked list inetsw.
927 static struct inet_protosw inetsw_array[] =
931 .protocol = IPPROTO_TCP,
933 .ops = &inet_stream_ops,
936 .flags = INET_PROTOSW_PERMANENT |
942 .protocol = IPPROTO_UDP,
944 .ops = &inet_dgram_ops,
946 .no_check = UDP_CSUM_DEFAULT,
947 .flags = INET_PROTOSW_PERMANENT,
953 .protocol = IPPROTO_IP, /* wild card */
955 .ops = &inet_sockraw_ops,
956 .capability = CAP_NET_RAW,
957 .no_check = UDP_CSUM_DEFAULT,
958 .flags = INET_PROTOSW_REUSE,
962 #define INETSW_ARRAY_LEN (sizeof(inetsw_array) / sizeof(struct inet_protosw))
964 void inet_register_protosw(struct inet_protosw *p)
966 struct list_head *lh;
967 struct inet_protosw *answer;
968 int protocol = p->protocol;
969 struct list_head *last_perm;
971 spin_lock_bh(&inetsw_lock);
973 if (p->type >= SOCK_MAX)
976 /* If we are trying to override a permanent protocol, bail. */
978 last_perm = &inetsw[p->type];
979 list_for_each(lh, &inetsw[p->type]) {
980 answer = list_entry(lh, struct inet_protosw, list);
982 /* Check only the non-wild match. */
983 if (INET_PROTOSW_PERMANENT & answer->flags) {
984 if (protocol == answer->protocol)
994 /* Add the new entry after the last permanent entry if any, so that
995 * the new entry does not override a permanent entry when matched with
996 * a wild-card protocol. But it is allowed to override any existing
997 * non-permanent entry. This means that when we remove this entry, the
998 * system automatically returns to the old behavior.
1000 list_add_rcu(&p->list, last_perm);
1002 spin_unlock_bh(&inetsw_lock);
1009 printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
1015 "Ignoring attempt to register invalid socket type %d.\n",
1020 void inet_unregister_protosw(struct inet_protosw *p)
1022 if (INET_PROTOSW_PERMANENT & p->flags) {
1024 "Attempt to unregister permanent protocol %d.\n",
1027 spin_lock_bh(&inetsw_lock);
1028 list_del_rcu(&p->list);
1029 spin_unlock_bh(&inetsw_lock);
1036 * Shall we try to damage output packets if routing dev changes?
1039 int sysctl_ip_dynaddr;
1041 static int inet_sk_reselect_saddr(struct sock *sk)
1043 struct inet_sock *inet = inet_sk(sk);
1046 __u32 old_saddr = inet->saddr;
1048 __u32 daddr = inet->daddr;
1050 if (inet->opt && inet->opt->srr)
1051 daddr = inet->opt->faddr;
1053 /* Query new route. */
1054 err = ip_route_connect(&rt, daddr, 0,
1056 sk->sk_bound_dev_if,
1058 inet->sport, inet->dport, sk);
1062 sk_setup_caps(sk, &rt->u.dst);
1064 new_saddr = rt->rt_src;
1066 if (new_saddr == old_saddr)
1069 if (sysctl_ip_dynaddr > 1) {
1070 printk(KERN_INFO "%s(): shifting inet->"
1071 "saddr from %d.%d.%d.%d to %d.%d.%d.%d\n",
1074 NIPQUAD(new_saddr));
1077 inet->saddr = inet->rcv_saddr = new_saddr;
1080 * XXX The only one ugly spot where we need to
1081 * XXX really change the sockets identity after
1082 * XXX it has entered the hashes. -DaveM
1084 * Besides that, it does not check for connection
1085 * uniqueness. Wait for troubles.
1087 __sk_prot_rehash(sk);
1091 int inet_sk_rebuild_header(struct sock *sk)
1093 struct inet_sock *inet = inet_sk(sk);
1094 struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0);
1098 /* Route is OK, nothing to do. */
1103 daddr = inet->daddr;
1104 if (inet->opt && inet->opt->srr)
1105 daddr = inet->opt->faddr;
1108 .oif = sk->sk_bound_dev_if,
1112 .saddr = inet->saddr,
1113 .tos = RT_CONN_FLAGS(sk),
1116 .proto = sk->sk_protocol,
1119 .sport = inet->sport,
1120 .dport = inet->dport,
1125 err = ip_route_output_flow(&rt, &fl, sk, 0);
1128 sk_setup_caps(sk, &rt->u.dst);
1130 /* Routing failed... */
1131 sk->sk_route_caps = 0;
1133 * Other protocols have to map its equivalent state to TCP_SYN_SENT.
1134 * DCCP maps its DCCP_REQUESTING state to TCP_SYN_SENT. -acme
1136 if (!sysctl_ip_dynaddr ||
1137 sk->sk_state != TCP_SYN_SENT ||
1138 (sk->sk_userlocks & SOCK_BINDADDR_LOCK) ||
1139 (err = inet_sk_reselect_saddr(sk)) != 0)
1140 sk->sk_err_soft = -err;
1146 EXPORT_SYMBOL(inet_sk_rebuild_header);
1148 #ifdef CONFIG_IP_MULTICAST
1149 static struct net_protocol igmp_protocol = {
1150 .handler = igmp_rcv,
1154 static struct net_protocol tcp_protocol = {
1155 .handler = tcp_v4_rcv,
1156 .err_handler = tcp_v4_err,
1160 static struct net_protocol udp_protocol = {
1162 .err_handler = udp_err,
1166 static struct net_protocol icmp_protocol = {
1167 .handler = icmp_rcv,
1170 static int __init init_ipv4_mibs(void)
1172 net_statistics[0] = alloc_percpu(struct linux_mib);
1173 net_statistics[1] = alloc_percpu(struct linux_mib);
1174 ip_statistics[0] = alloc_percpu(struct ipstats_mib);
1175 ip_statistics[1] = alloc_percpu(struct ipstats_mib);
1176 icmp_statistics[0] = alloc_percpu(struct icmp_mib);
1177 icmp_statistics[1] = alloc_percpu(struct icmp_mib);
1178 tcp_statistics[0] = alloc_percpu(struct tcp_mib);
1179 tcp_statistics[1] = alloc_percpu(struct tcp_mib);
1180 udp_statistics[0] = alloc_percpu(struct udp_mib);
1181 udp_statistics[1] = alloc_percpu(struct udp_mib);
1183 (net_statistics[0] && net_statistics[1] && ip_statistics[0]
1184 && ip_statistics[1] && tcp_statistics[0] && tcp_statistics[1]
1185 && udp_statistics[0] && udp_statistics[1]))
1188 (void) tcp_mib_init();
1193 static int ipv4_proc_init(void);
1196 * IP protocol layer initialiser
1199 static struct packet_type ip_packet_type = {
1200 .type = __constant_htons(ETH_P_IP),
1204 static int __init inet_init(void)
1206 struct sk_buff *dummy_skb;
1207 struct inet_protosw *q;
1208 struct list_head *r;
1211 if (sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb)) {
1212 printk(KERN_CRIT "%s: panic\n", __FUNCTION__);
1216 rc = proto_register(&tcp_prot, 1);
1220 rc = proto_register(&udp_prot, 1);
1222 goto out_unregister_tcp_proto;
1224 rc = proto_register(&raw_prot, 1);
1226 goto out_unregister_udp_proto;
1229 * Tell SOCKET that we are alive...
1232 (void)sock_register(&inet_family_ops);
1235 * Add all the base protocols.
1238 if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
1239 printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
1240 if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
1241 printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
1242 if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
1243 printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n");
1244 #ifdef CONFIG_IP_MULTICAST
1245 if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
1246 printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n");
1249 /* Register the socket-side information for inet_create. */
1250 for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
1253 for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
1254 inet_register_protosw(q);
1257 * Set the ARP module up
1263 * Set the IP module up
1268 tcp_v4_init(&inet_family_ops);
1270 /* Setup TCP slab cache for open requests. */
1275 * Set the ICMP layer up
1278 icmp_init(&inet_family_ops);
1281 * Initialise the multicast router
1283 #if defined(CONFIG_IP_MROUTE)
1287 * Initialise per-cpu ipv4 mibs
1290 if(init_ipv4_mibs())
1291 printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n"); ;
1297 dev_add_pack(&ip_packet_type);
1302 out_unregister_tcp_proto:
1303 proto_unregister(&tcp_prot);
1304 out_unregister_udp_proto:
1305 proto_unregister(&udp_prot);
1309 module_init(inet_init);
1311 /* ------------------------------------------------------------------------ */
1313 #ifdef CONFIG_PROC_FS
1314 static int __init ipv4_proc_init(void)
1318 if (raw_proc_init())
1320 if (tcp4_proc_init())
1322 if (udp4_proc_init())
1324 if (fib_proc_init())
1326 if (ip_misc_proc_init())
1343 #else /* CONFIG_PROC_FS */
1344 static int __init ipv4_proc_init(void)
1348 #endif /* CONFIG_PROC_FS */
1350 MODULE_ALIAS_NETPROTO(PF_INET);
1352 EXPORT_SYMBOL(inet_accept);
1353 EXPORT_SYMBOL(inet_bind);
1354 EXPORT_SYMBOL(inet_dgram_connect);
1355 EXPORT_SYMBOL(inet_dgram_ops);
1356 EXPORT_SYMBOL(inet_getname);
1357 EXPORT_SYMBOL(inet_ioctl);
1358 EXPORT_SYMBOL(inet_listen);
1359 EXPORT_SYMBOL(inet_register_protosw);
1360 EXPORT_SYMBOL(inet_release);
1361 EXPORT_SYMBOL(inet_sendmsg);
1362 EXPORT_SYMBOL(inet_shutdown);
1363 EXPORT_SYMBOL(inet_sock_destruct);
1364 EXPORT_SYMBOL(inet_stream_connect);
1365 EXPORT_SYMBOL(inet_stream_ops);
1366 EXPORT_SYMBOL(inet_unregister_protosw);
1367 EXPORT_SYMBOL(net_statistics);
1368 EXPORT_SYMBOL(sysctl_ip_nonlocal_bind);