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 $
10 * Authors: Ross Biro, <bir7@leland.Stanford.Edu>
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/major.h>
77 #include <linux/module.h>
78 #include <linux/sched.h>
79 #include <linux/timer.h>
80 #include <linux/string.h>
81 #include <linux/sockios.h>
82 #include <linux/net.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/netdevice.h>
99 #include <net/protocol.h>
101 #include <net/route.h>
102 #include <net/ip_fib.h>
105 #include <linux/skbuff.h>
106 #include <net/sock.h>
108 #include <net/icmp.h>
109 #include <net/ipip.h>
110 #include <net/inet_common.h>
111 #include <net/xfrm.h>
112 #ifdef CONFIG_IP_MROUTE
113 #include <linux/mroute.h>
116 DEFINE_SNMP_STAT(struct linux_mib, net_statistics);
118 #ifdef INET_REFCNT_DEBUG
119 atomic_t inet_sock_nr;
122 extern void ip_mc_drop_socket(struct sock *sk);
124 /* Per protocol sock slabcache */
125 kmem_cache_t *tcp_sk_cachep;
126 static kmem_cache_t *udp_sk_cachep;
127 static kmem_cache_t *raw4_sk_cachep;
129 /* The inetsw table contains everything that inet_create needs to
130 * build a new socket.
132 static struct list_head inetsw[SOCK_MAX];
133 static spinlock_t inetsw_lock = SPIN_LOCK_UNLOCKED;
135 /* New destruction routine */
137 void inet_sock_destruct(struct sock *sk)
139 struct inet_opt *inet = inet_sk(sk);
141 __skb_queue_purge(&sk->sk_receive_queue);
142 __skb_queue_purge(&sk->sk_error_queue);
144 if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
145 printk("Attempt to release TCP socket in state %d %p\n",
149 if (!sock_flag(sk, SOCK_DEAD)) {
150 printk("Attempt to release alive inet socket %p\n", sk);
154 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
155 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
156 BUG_TRAP(!sk->sk_wmem_queued);
157 BUG_TRAP(!sk->sk_forward_alloc);
162 BUG_ON(sk->sk_nx_info);
163 BUG_ON(sk->sk_vx_info);
164 dst_release(sk->sk_dst_cache);
165 #ifdef INET_REFCNT_DEBUG
166 atomic_dec(&inet_sock_nr);
167 printk(KERN_DEBUG "INET socket %p released, %d are still alive\n",
168 sk, atomic_read(&inet_sock_nr));
172 void inet_sock_release(struct sock *sk)
174 if (sk->sk_prot->destroy)
175 sk->sk_prot->destroy(sk);
177 /* Observation: when inet_sock_release is called, processes have
178 * no access to socket. But net still has.
179 * Step one, detach it from networking:
181 * A. Remove from hash tables.
184 sk->sk_prot->unhash(sk);
186 /* In this point socket cannot receive new packets,
187 * but it is possible that some packets are in flight
188 * because some CPU runs receiver and did hash table lookup
189 * before we unhashed socket. They will achieve receive queue
190 * and will be purged by socket destructor.
192 * Also we still have packets pending on receive
193 * queue and probably, our own packets waiting in device queues.
194 * sock_destroy will drain receive queue, but transmitted
195 * packets will delay socket destruction until the last reference
201 xfrm_sk_free_policy(sk);
203 #ifdef INET_REFCNT_DEBUG
204 if (atomic_read(&sk->sk_refcnt) != 1)
205 printk(KERN_DEBUG "Destruction inet %p delayed, c=%d\n",
206 sk, atomic_read(&sk->sk_refcnt));
213 * The routines beyond this point handle the behaviour of an AF_INET
214 * socket object. Mostly it punts to the subprotocols of IP to do
219 * Set socket options on an inet socket.
221 int inet_setsockopt(struct socket *sock, int level, int optname,
222 char __user *optval, int optlen)
224 struct sock *sk = sock->sk;
226 return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
230 * Get a socket option on an AF_INET socket.
232 * FIX: POSIX 1003.1g is very ambiguous here. It states that
233 * asynchronous errors should be reported by getsockopt. We assume
234 * this means if you specify SO_ERROR (otherwise whats the point of it).
237 int inet_getsockopt(struct socket *sock, int level, int optname,
238 char __user *optval, int __user *optlen)
240 struct sock *sk = sock->sk;
242 return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
246 * Automatically bind an unbound socket.
249 static int inet_autobind(struct sock *sk)
251 struct inet_opt *inet;
252 /* We may need to bind the socket. */
256 if (sk->sk_prot->get_port(sk, 0)) {
260 inet->sport = htons(inet->num);
267 * Move a socket into listening state.
269 int inet_listen(struct socket *sock, int backlog)
271 struct sock *sk = sock->sk;
272 unsigned char old_state;
278 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
281 old_state = sk->sk_state;
282 if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
285 /* Really, if the socket is already in listen state
286 * we can only allow the backlog to be adjusted.
288 if (old_state != TCP_LISTEN) {
289 err = tcp_listen_start(sk);
293 sk->sk_max_ack_backlog = backlog;
301 static __inline__ kmem_cache_t *inet_sk_slab(int protocol)
303 kmem_cache_t* rc = tcp_sk_cachep;
305 if (protocol == IPPROTO_UDP)
307 else if (protocol == IPPROTO_RAW)
312 static __inline__ int inet_sk_size(int protocol)
314 int rc = sizeof(struct tcp_sock);
316 if (protocol == IPPROTO_UDP)
317 rc = sizeof(struct udp_sock);
318 else if (protocol == IPPROTO_RAW)
319 rc = sizeof(struct raw_sock);
324 * Create an inet socket.
327 static int inet_create(struct socket *sock, int protocol)
331 struct inet_protosw *answer;
332 struct inet_opt *inet;
335 sock->state = SS_UNCONNECTED;
336 sk = sk_alloc(PF_INET, GFP_KERNEL, inet_sk_size(protocol),
337 inet_sk_slab(protocol));
341 /* Look for the requested type/protocol pair. */
344 list_for_each_rcu(p, &inetsw[sock->type]) {
345 answer = list_entry(p, struct inet_protosw, list);
347 /* Check the non-wild match. */
348 if (protocol == answer->protocol) {
349 if (protocol != IPPROTO_IP)
352 /* Check for the two wild cases. */
353 if (IPPROTO_IP == protocol) {
354 protocol = answer->protocol;
357 if (IPPROTO_IP == answer->protocol)
363 err = -ESOCKTNOSUPPORT;
367 if (answer->capability > 0 && !capable(answer->capability))
369 err = -EPROTONOSUPPORT;
373 sock->ops = answer->ops;
374 sk->sk_prot = answer->prot;
375 sk->sk_no_check = answer->no_check;
376 if (INET_PROTOSW_REUSE & answer->flags)
382 if (SOCK_RAW == sock->type) {
383 inet->num = protocol;
384 if (IPPROTO_RAW == protocol)
388 if (ipv4_config.no_pmtu_disc)
389 inet->pmtudisc = IP_PMTUDISC_DONT;
391 inet->pmtudisc = IP_PMTUDISC_WANT;
395 sock_init_data(sock, sk);
396 sk_set_owner(sk, THIS_MODULE);
398 sk->sk_destruct = inet_sock_destruct;
400 sk->sk_family = PF_INET;
401 sk->sk_protocol = protocol;
402 sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
404 set_vx_info(&sk->sk_vx_info, current->vx_info);
405 sk->sk_xid = vx_current_xid();
406 set_nx_info(&sk->sk_nx_info, current->nx_info);
407 sk->sk_nid = nx_current_nid();
413 inet->mc_list = NULL;
415 #ifdef INET_REFCNT_DEBUG
416 atomic_inc(&inet_sock_nr);
420 /* It assumes that any protocol which allows
421 * the user to assign a number at socket
422 * creation time automatically
425 inet->sport = htons(inet->num);
426 /* Add to protocol hash chains. */
427 sk->sk_prot->hash(sk);
430 if (sk->sk_prot->init) {
431 err = sk->sk_prot->init(sk);
433 /* sk->sk_vx_info = NULL;
434 put_vx_info(current->vx_info);
435 sk->sk_nx_info = NULL;
436 put_nx_info(current->nx_info);
437 */ inet_sock_release(sk);
450 * The peer socket should always be NULL (or else). When we call this
451 * function we are destroying the object and from then on nobody
452 * should refer to it.
454 int inet_release(struct socket *sock)
456 struct sock *sk = sock->sk;
461 /* Applications forget to leave groups before exiting */
462 ip_mc_drop_socket(sk);
464 /* If linger is set, we don't return until the close
465 * is complete. Otherwise we return immediately. The
466 * actually closing is done the same either way.
468 * If the close is due to the process exiting, we never
472 if (sock_flag(sk, SOCK_LINGER) &&
473 !(current->flags & PF_EXITING))
474 timeout = sk->sk_lingertime;
476 clr_vx_info(&sk->sk_vx_info);
477 clr_nx_info(&sk->sk_nx_info);
478 sk->sk_prot->close(sk, timeout);
483 /* It is off by default, see below. */
484 int sysctl_ip_nonlocal_bind;
486 int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
488 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
489 struct sock *sk = sock->sk;
490 struct inet_opt *inet = inet_sk(sk);
494 __u32 s_addr; /* Address used for validation */
496 __u32 s_addr2 = 0xffffffffl; /* Optional address of the socket */
497 struct nx_info *nxi = sk->sk_nx_info;
499 /* If the socket has its own bind function then use it. (RAW) */
500 if (sk->sk_prot->bind) {
501 err = sk->sk_prot->bind(sk, uaddr, addr_len);
505 if (addr_len < sizeof(struct sockaddr_in))
508 s_addr = s_addr1 = addr->sin_addr.s_addr;
509 nxdprintk("inet_bind(%p) %p,%p;%lx\n",
510 sk, nx_info, sk->sk_socket,
511 (sk->sk_socket?sk->sk_socket->flags:0));
513 __u32 v4_bcast = nxi->v4_bcast;
514 __u32 ipv4root = nxi->ipv4[0];
515 int nbipv4 = nxi->nbipv4;
524 } else if (s_addr == 0x0100007f) {
525 s_addr = s_addr1 = ipv4root;
526 } else if (s_addr != v4_bcast) {
528 for (i=0; i<nbipv4; i++) {
529 if (s_addr == nxi->ipv4[i])
533 return -EADDRNOTAVAIL;
537 chk_addr_ret = inet_addr_type(s_addr);
539 /* Not specified by any standard per-se, however it breaks too
540 * many applications when removed. It is unfortunate since
541 * allowing applications to make a non-local bind solves
542 * several problems with systems using dynamic addressing.
543 * (ie. your servers still start up even if your ISDN link
544 * is temporarily down)
546 err = -EADDRNOTAVAIL;
547 if (!sysctl_ip_nonlocal_bind &&
549 s_addr != INADDR_ANY &&
550 chk_addr_ret != RTN_LOCAL &&
551 chk_addr_ret != RTN_MULTICAST &&
552 chk_addr_ret != RTN_BROADCAST)
555 snum = ntohs(addr->sin_port);
557 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
560 /* We keep a pair of addresses. rcv_saddr is the one
561 * used by hash lookups, and saddr is used for transmit.
563 * In the BSD API these are the same except where it
564 * would be illegal to use them (multicast/broadcast) in
565 * which case the sending device address is used.
569 /* Check these errors (active socket, double bind). */
571 if (sk->sk_state != TCP_CLOSE || inet->num)
572 goto out_release_sock;
574 inet->rcv_saddr = inet->saddr = s_addr1;
575 inet->rcv_saddr2 = s_addr2;
576 if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
577 inet->saddr = 0; /* Use device */
579 /* Make sure we are allowed to bind here. */
580 if (sk->sk_prot->get_port(sk, snum)) {
581 inet->saddr = inet->rcv_saddr = 0;
583 goto out_release_sock;
587 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
589 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
590 inet->sport = htons(inet->num);
601 int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
602 int addr_len, int flags)
604 struct sock *sk = sock->sk;
606 if (uaddr->sa_family == AF_UNSPEC)
607 return sk->sk_prot->disconnect(sk, flags);
609 if (!inet_sk(sk)->num && inet_autobind(sk))
611 return sk->sk_prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
614 static long inet_wait_for_connect(struct sock *sk, long timeo)
618 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
620 /* Basic assumption: if someone sets sk->sk_err, he _must_
621 * change state of the socket from TCP_SYN_*.
622 * Connect() does not allow to get error notifications
623 * without closing the socket.
625 while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
627 timeo = schedule_timeout(timeo);
629 if (signal_pending(current) || !timeo)
631 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
633 finish_wait(sk->sk_sleep, &wait);
638 * Connect to a remote host. There is regrettably still a little
639 * TCP 'magic' in here.
641 int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
642 int addr_len, int flags)
644 struct sock *sk = sock->sk;
650 if (uaddr->sa_family == AF_UNSPEC) {
651 err = sk->sk_prot->disconnect(sk, flags);
652 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
656 switch (sock->state) {
665 /* Fall out of switch with err, set for this state */
669 if (sk->sk_state != TCP_CLOSE)
672 err = sk->sk_prot->connect(sk, uaddr, addr_len);
676 sock->state = SS_CONNECTING;
678 /* Just entered SS_CONNECTING state; the only
679 * difference is that return value in non-blocking
680 * case is EINPROGRESS, rather than EALREADY.
686 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
688 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
689 /* Error code is set above */
690 if (!timeo || !inet_wait_for_connect(sk, timeo))
693 err = sock_intr_errno(timeo);
694 if (signal_pending(current))
698 /* Connection was closed by RST, timeout, ICMP error
699 * or another process disconnected us.
701 if (sk->sk_state == TCP_CLOSE)
704 /* sk->sk_err may be not zero now, if RECVERR was ordered by user
705 * and error was received after socket entered established state.
706 * Hence, it is handled normally after connect() return successfully.
709 sock->state = SS_CONNECTED;
716 err = sock_error(sk) ? : -ECONNABORTED;
717 sock->state = SS_UNCONNECTED;
718 if (sk->sk_prot->disconnect(sk, flags))
719 sock->state = SS_DISCONNECTING;
724 * Accept a pending connection. The TCP layer now gives BSD semantics.
727 int inet_accept(struct socket *sock, struct socket *newsock, int flags)
729 struct sock *sk1 = sock->sk;
731 struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err);
738 BUG_TRAP((1 << sk2->sk_state) &
739 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_CLOSE));
741 sock_graft(sk2, newsock);
743 newsock->state = SS_CONNECTED;
752 * This does both peername and sockname.
754 int inet_getname(struct socket *sock, struct sockaddr *uaddr,
755 int *uaddr_len, int peer)
757 struct sock *sk = sock->sk;
758 struct inet_opt *inet = inet_sk(sk);
759 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
761 sin->sin_family = AF_INET;
764 (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
767 sin->sin_port = inet->dport;
768 sin->sin_addr.s_addr = inet->daddr;
770 __u32 addr = inet->rcv_saddr;
773 sin->sin_port = inet->sport;
774 sin->sin_addr.s_addr = addr;
776 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
777 *uaddr_len = sizeof(*sin);
782 int inet_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
783 size_t size, int flags)
785 struct sock *sk = sock->sk;
789 err = sk->sk_prot->recvmsg(iocb, sk, msg, size, flags & MSG_DONTWAIT,
790 flags & ~MSG_DONTWAIT, &addr_len);
792 msg->msg_namelen = addr_len;
797 int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
800 struct sock *sk = sock->sk;
802 /* We may need to bind the socket. */
803 if (!inet_sk(sk)->num && inet_autobind(sk))
806 return sk->sk_prot->sendmsg(iocb, sk, msg, size);
810 ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
812 struct sock *sk = sock->sk;
814 /* We may need to bind the socket. */
815 if (!inet_sk(sk)->num && inet_autobind(sk))
818 if (sk->sk_prot->sendpage)
819 return sk->sk_prot->sendpage(sk, page, offset, size, flags);
820 return sock_no_sendpage(sock, page, offset, size, flags);
824 int inet_shutdown(struct socket *sock, int how)
826 struct sock *sk = sock->sk;
829 /* This should really check to make sure
830 * the socket is a TCP socket. (WHY AC...)
832 how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
835 if ((how & ~SHUTDOWN_MASK) || !how) /* MAXINT->0 */
839 if (sock->state == SS_CONNECTING) {
840 if ((1 << sk->sk_state) &
841 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
842 sock->state = SS_DISCONNECTING;
844 sock->state = SS_CONNECTED;
847 switch (sk->sk_state) {
850 /* Hack to wake up other listeners, who can poll for
851 POLLHUP, even on eg. unconnected UDP sockets -- RR */
853 sk->sk_shutdown |= how;
854 if (sk->sk_prot->shutdown)
855 sk->sk_prot->shutdown(sk, how);
858 /* Remaining two branches are temporary solution for missing
859 * close() in multithreaded environment. It is _not_ a good idea,
860 * but we have no choice until close() is repaired at VFS level.
863 if (!(how & RCV_SHUTDOWN))
867 err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
868 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
872 /* Wake up anyone sleeping in poll. */
873 sk->sk_state_change(sk);
879 * ioctl() calls you can issue on an INET socket. Most of these are
880 * device configuration and stuff and very rarely used. Some ioctls
881 * pass on to the socket itself.
883 * NOTE: I like the idea of a module for the config stuff. ie ifconfig
884 * loads the devconfigure module does its configuring and unloads it.
885 * There's a good 20K of config code hanging around the kernel.
888 int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
890 struct sock *sk = sock->sk;
895 err = sock_get_timestamp(sk, (struct timeval __user *)arg);
900 err = ip_rt_ioctl(cmd, (void *)arg);
905 err = arp_ioctl(cmd, (void __user *)arg);
918 err = devinet_ioctl(cmd, (void __user *)arg);
921 if (!sk->sk_prot->ioctl ||
922 (err = sk->sk_prot->ioctl(sk, cmd, arg)) ==
924 err = dev_ioctl(cmd, (void __user *)arg);
930 struct proto_ops inet_stream_ops = {
932 .owner = THIS_MODULE,
933 .release = inet_release,
935 .connect = inet_stream_connect,
936 .socketpair = sock_no_socketpair,
937 .accept = inet_accept,
938 .getname = inet_getname,
941 .listen = inet_listen,
942 .shutdown = inet_shutdown,
943 .setsockopt = inet_setsockopt,
944 .getsockopt = inet_getsockopt,
945 .sendmsg = inet_sendmsg,
946 .recvmsg = inet_recvmsg,
947 .mmap = sock_no_mmap,
948 .sendpage = tcp_sendpage
951 struct proto_ops inet_dgram_ops = {
953 .owner = THIS_MODULE,
954 .release = inet_release,
956 .connect = inet_dgram_connect,
957 .socketpair = sock_no_socketpair,
958 .accept = sock_no_accept,
959 .getname = inet_getname,
960 .poll = datagram_poll,
962 .listen = sock_no_listen,
963 .shutdown = inet_shutdown,
964 .setsockopt = inet_setsockopt,
965 .getsockopt = inet_getsockopt,
966 .sendmsg = inet_sendmsg,
967 .recvmsg = inet_recvmsg,
968 .mmap = sock_no_mmap,
969 .sendpage = inet_sendpage,
972 struct net_proto_family inet_family_ops = {
974 .create = inet_create,
975 .owner = THIS_MODULE,
979 extern void tcp_init(void);
980 extern void tcp_v4_init(struct net_proto_family *);
982 /* Upon startup we insert all the elements in inetsw_array[] into
983 * the linked list inetsw.
985 static struct inet_protosw inetsw_array[] =
989 .protocol = IPPROTO_TCP,
991 .ops = &inet_stream_ops,
994 .flags = INET_PROTOSW_PERMANENT,
999 .protocol = IPPROTO_UDP,
1001 .ops = &inet_dgram_ops,
1003 .no_check = UDP_CSUM_DEFAULT,
1004 .flags = INET_PROTOSW_PERMANENT,
1010 .protocol = IPPROTO_IP, /* wild card */
1012 .ops = &inet_dgram_ops,
1013 .capability = CAP_NET_RAW,
1014 .no_check = UDP_CSUM_DEFAULT,
1015 .flags = INET_PROTOSW_REUSE,
1019 #define INETSW_ARRAY_LEN (sizeof(inetsw_array) / sizeof(struct inet_protosw))
1021 void inet_register_protosw(struct inet_protosw *p)
1023 struct list_head *lh;
1024 struct inet_protosw *answer;
1025 int protocol = p->protocol;
1026 struct list_head *last_perm;
1028 spin_lock_bh(&inetsw_lock);
1030 if (p->type >= SOCK_MAX)
1033 /* If we are trying to override a permanent protocol, bail. */
1035 last_perm = &inetsw[p->type];
1036 list_for_each(lh, &inetsw[p->type]) {
1037 answer = list_entry(lh, struct inet_protosw, list);
1039 /* Check only the non-wild match. */
1040 if (INET_PROTOSW_PERMANENT & answer->flags) {
1041 if (protocol == answer->protocol)
1051 /* Add the new entry after the last permanent entry if any, so that
1052 * the new entry does not override a permanent entry when matched with
1053 * a wild-card protocol. But it is allowed to override any existing
1054 * non-permanent entry. This means that when we remove this entry, the
1055 * system automatically returns to the old behavior.
1057 list_add_rcu(&p->list, last_perm);
1059 spin_unlock_bh(&inetsw_lock);
1066 printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
1072 "Ignoring attempt to register invalid socket type %d.\n",
1077 void inet_unregister_protosw(struct inet_protosw *p)
1079 if (INET_PROTOSW_PERMANENT & p->flags) {
1081 "Attempt to unregister permanent protocol %d.\n",
1084 spin_lock_bh(&inetsw_lock);
1085 list_del_rcu(&p->list);
1086 spin_unlock_bh(&inetsw_lock);
1092 #ifdef CONFIG_IP_MULTICAST
1093 static struct inet_protocol igmp_protocol = {
1094 .handler = igmp_rcv,
1098 static struct inet_protocol tcp_protocol = {
1099 .handler = tcp_v4_rcv,
1100 .err_handler = tcp_v4_err,
1104 static struct inet_protocol udp_protocol = {
1106 .err_handler = udp_err,
1110 static struct inet_protocol icmp_protocol = {
1111 .handler = icmp_rcv,
1114 static int __init init_ipv4_mibs(void)
1116 net_statistics[0] = alloc_percpu(struct linux_mib);
1117 net_statistics[1] = alloc_percpu(struct linux_mib);
1118 ip_statistics[0] = alloc_percpu(struct ip_mib);
1119 ip_statistics[1] = alloc_percpu(struct ip_mib);
1120 icmp_statistics[0] = alloc_percpu(struct icmp_mib);
1121 icmp_statistics[1] = alloc_percpu(struct icmp_mib);
1122 tcp_statistics[0] = alloc_percpu(struct tcp_mib);
1123 tcp_statistics[1] = alloc_percpu(struct tcp_mib);
1124 udp_statistics[0] = alloc_percpu(struct udp_mib);
1125 udp_statistics[1] = alloc_percpu(struct udp_mib);
1127 (net_statistics[0] && net_statistics[1] && ip_statistics[0]
1128 && ip_statistics[1] && tcp_statistics[0] && tcp_statistics[1]
1129 && udp_statistics[0] && udp_statistics[1]))
1132 (void) tcp_mib_init();
1137 int ipv4_proc_init(void);
1138 extern void ipfrag_init(void);
1140 static int __init inet_init(void)
1142 struct sk_buff *dummy_skb;
1143 struct inet_protosw *q;
1144 struct list_head *r;
1146 if (sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb)) {
1147 printk(KERN_CRIT "%s: panic\n", __FUNCTION__);
1151 tcp_sk_cachep = kmem_cache_create("tcp_sock",
1152 sizeof(struct tcp_sock), 0,
1153 SLAB_HWCACHE_ALIGN, 0, 0);
1154 udp_sk_cachep = kmem_cache_create("udp_sock",
1155 sizeof(struct udp_sock), 0,
1156 SLAB_HWCACHE_ALIGN, 0, 0);
1157 raw4_sk_cachep = kmem_cache_create("raw4_sock",
1158 sizeof(struct raw_sock), 0,
1159 SLAB_HWCACHE_ALIGN, 0, 0);
1160 if (!tcp_sk_cachep || !udp_sk_cachep || !raw4_sk_cachep)
1162 "inet_init: Can't create protocol sock SLAB caches!\n");
1164 * Tell SOCKET that we are alive...
1167 (void)sock_register(&inet_family_ops);
1170 * Add all the base protocols.
1173 if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
1174 printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
1175 if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
1176 printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
1177 if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
1178 printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n");
1179 #ifdef CONFIG_IP_MULTICAST
1180 if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
1181 printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n");
1184 /* Register the socket-side information for inet_create. */
1185 for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
1188 for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
1189 inet_register_protosw(q);
1192 * Set the ARP module up
1198 * Set the IP module up
1203 tcp_v4_init(&inet_family_ops);
1205 /* Setup TCP slab cache for open requests. */
1210 * Set the ICMP layer up
1213 icmp_init(&inet_family_ops);
1216 * Initialise the multicast router
1218 #if defined(CONFIG_IP_MROUTE)
1222 * Initialise per-cpu ipv4 mibs
1225 if(init_ipv4_mibs())
1226 printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n"); ;
1235 module_init(inet_init);
1237 /* ------------------------------------------------------------------------ */
1239 #ifdef CONFIG_PROC_FS
1240 extern int fib_proc_init(void);
1241 extern void fib_proc_exit(void);
1242 extern int ip_misc_proc_init(void);
1243 extern int raw_proc_init(void);
1244 extern void raw_proc_exit(void);
1245 extern int tcp4_proc_init(void);
1246 extern void tcp4_proc_exit(void);
1247 extern int udp4_proc_init(void);
1248 extern void udp4_proc_exit(void);
1250 int __init ipv4_proc_init(void)
1254 if (raw_proc_init())
1256 if (tcp4_proc_init())
1258 if (udp4_proc_init())
1260 if (fib_proc_init())
1262 if (ip_misc_proc_init())
1279 #else /* CONFIG_PROC_FS */
1280 int __init ipv4_proc_init(void)
1284 #endif /* CONFIG_PROC_FS */
1286 MODULE_ALIAS_NETPROTO(PF_INET);
1288 EXPORT_SYMBOL(inet_accept);
1289 EXPORT_SYMBOL(inet_bind);
1290 EXPORT_SYMBOL(inet_dgram_connect);
1291 EXPORT_SYMBOL(inet_dgram_ops);
1292 EXPORT_SYMBOL(inet_family_ops);
1293 EXPORT_SYMBOL(inet_getname);
1294 EXPORT_SYMBOL(inet_getsockopt);
1295 EXPORT_SYMBOL(inet_ioctl);
1296 EXPORT_SYMBOL(inet_listen);
1297 EXPORT_SYMBOL(inet_recvmsg);
1298 EXPORT_SYMBOL(inet_register_protosw);
1299 EXPORT_SYMBOL(inet_release);
1300 EXPORT_SYMBOL(inet_sendmsg);
1301 EXPORT_SYMBOL(inet_setsockopt);
1302 EXPORT_SYMBOL(inet_shutdown);
1303 EXPORT_SYMBOL(inet_sock_destruct);
1304 EXPORT_SYMBOL(inet_sock_release);
1305 EXPORT_SYMBOL(inet_stream_connect);
1306 EXPORT_SYMBOL(inet_stream_ops);
1307 EXPORT_SYMBOL(inet_unregister_protosw);
1308 EXPORT_SYMBOL(net_statistics);
1309 EXPORT_SYMBOL(tcp_protocol);
1310 EXPORT_SYMBOL(udp_protocol);
1312 #ifdef INET_REFCNT_DEBUG
1313 EXPORT_SYMBOL(inet_sock_nr);