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 clr_vx_info(&sk->sk_vx_info);
164 clr_nx_info(&sk->sk_nx_info);
167 dst_release(sk->sk_dst_cache);
168 #ifdef INET_REFCNT_DEBUG
169 atomic_dec(&inet_sock_nr);
170 printk(KERN_DEBUG "INET socket %p released, %d are still alive\n",
171 sk, atomic_read(&inet_sock_nr));
175 void inet_sock_release(struct sock *sk)
177 if (sk->sk_prot->destroy)
178 sk->sk_prot->destroy(sk);
180 /* Observation: when inet_sock_release is called, processes have
181 * no access to socket. But net still has.
182 * Step one, detach it from networking:
184 * A. Remove from hash tables.
187 sk->sk_prot->unhash(sk);
189 /* In this point socket cannot receive new packets,
190 * but it is possible that some packets are in flight
191 * because some CPU runs receiver and did hash table lookup
192 * before we unhashed socket. They will achieve receive queue
193 * and will be purged by socket destructor.
195 * Also we still have packets pending on receive
196 * queue and probably, our own packets waiting in device queues.
197 * sock_destroy will drain receive queue, but transmitted
198 * packets will delay socket destruction until the last reference
204 xfrm_sk_free_policy(sk);
206 #ifdef INET_REFCNT_DEBUG
207 if (atomic_read(&sk->sk_refcnt) != 1)
208 printk(KERN_DEBUG "Destruction inet %p delayed, c=%d\n",
209 sk, atomic_read(&sk->sk_refcnt));
216 * The routines beyond this point handle the behaviour of an AF_INET
217 * socket object. Mostly it punts to the subprotocols of IP to do
222 * Set socket options on an inet socket.
224 int inet_setsockopt(struct socket *sock, int level, int optname,
225 char __user *optval, int optlen)
227 struct sock *sk = sock->sk;
229 return sk->sk_prot->setsockopt(sk, level, optname, optval, optlen);
233 * Get a socket option on an AF_INET socket.
235 * FIX: POSIX 1003.1g is very ambiguous here. It states that
236 * asynchronous errors should be reported by getsockopt. We assume
237 * this means if you specify SO_ERROR (otherwise whats the point of it).
240 int inet_getsockopt(struct socket *sock, int level, int optname,
241 char __user *optval, int __user *optlen)
243 struct sock *sk = sock->sk;
245 return sk->sk_prot->getsockopt(sk, level, optname, optval, optlen);
249 * Automatically bind an unbound socket.
252 static int inet_autobind(struct sock *sk)
254 struct inet_opt *inet;
255 /* We may need to bind the socket. */
259 if (sk->sk_prot->get_port(sk, 0)) {
263 inet->sport = htons(inet->num);
270 * Move a socket into listening state.
272 int inet_listen(struct socket *sock, int backlog)
274 struct sock *sk = sock->sk;
275 unsigned char old_state;
281 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
284 old_state = sk->sk_state;
285 if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
288 /* Really, if the socket is already in listen state
289 * we can only allow the backlog to be adjusted.
291 if (old_state != TCP_LISTEN) {
292 err = tcp_listen_start(sk);
296 sk->sk_max_ack_backlog = backlog;
304 static __inline__ kmem_cache_t *inet_sk_slab(int protocol)
306 kmem_cache_t* rc = tcp_sk_cachep;
308 if (protocol == IPPROTO_UDP)
310 else if (protocol == IPPROTO_RAW)
315 static __inline__ int inet_sk_size(int protocol)
317 int rc = sizeof(struct tcp_sock);
319 if (protocol == IPPROTO_UDP)
320 rc = sizeof(struct udp_sock);
321 else if (protocol == IPPROTO_RAW)
322 rc = sizeof(struct raw_sock);
327 * Create an inet socket.
330 static int inet_create(struct socket *sock, int protocol)
334 struct inet_protosw *answer;
335 struct inet_opt *inet;
338 sock->state = SS_UNCONNECTED;
339 sk = sk_alloc(PF_INET, GFP_KERNEL, inet_sk_size(protocol),
340 inet_sk_slab(protocol));
344 /* Look for the requested type/protocol pair. */
347 list_for_each_rcu(p, &inetsw[sock->type]) {
348 answer = list_entry(p, struct inet_protosw, list);
350 /* Check the non-wild match. */
351 if (protocol == answer->protocol) {
352 if (protocol != IPPROTO_IP)
355 /* Check for the two wild cases. */
356 if (IPPROTO_IP == protocol) {
357 protocol = answer->protocol;
360 if (IPPROTO_IP == answer->protocol)
366 err = -ESOCKTNOSUPPORT;
370 if ((protocol == IPPROTO_ICMP) && vx_ccaps(VXC_RAW_ICMP))
372 if (answer->capability > 0 && !capable(answer->capability))
375 err = -EPROTONOSUPPORT;
379 sock->ops = answer->ops;
380 sk->sk_prot = answer->prot;
381 sk->sk_no_check = answer->no_check;
382 if (INET_PROTOSW_REUSE & answer->flags)
388 if (SOCK_RAW == sock->type) {
389 inet->num = protocol;
390 if (IPPROTO_RAW == protocol)
394 if (ipv4_config.no_pmtu_disc)
395 inet->pmtudisc = IP_PMTUDISC_DONT;
397 inet->pmtudisc = IP_PMTUDISC_WANT;
401 sock_init_data(sock, sk);
402 sk_set_owner(sk, THIS_MODULE);
404 sk->sk_destruct = inet_sock_destruct;
406 sk->sk_family = PF_INET;
407 sk->sk_protocol = protocol;
408 sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
410 set_vx_info(&sk->sk_vx_info, current->vx_info);
411 sk->sk_xid = vx_current_xid();
412 set_nx_info(&sk->sk_nx_info, current->nx_info);
413 sk->sk_nid = nx_current_nid();
419 inet->mc_list = NULL;
421 #ifdef INET_REFCNT_DEBUG
422 atomic_inc(&inet_sock_nr);
426 /* It assumes that any protocol which allows
427 * the user to assign a number at socket
428 * creation time automatically
431 inet->sport = htons(inet->num);
432 /* Add to protocol hash chains. */
433 sk->sk_prot->hash(sk);
436 if (sk->sk_prot->init) {
437 err = sk->sk_prot->init(sk);
439 inet_sock_release(sk);
451 * The peer socket should always be NULL (or else). When we call this
452 * function we are destroying the object and from then on nobody
453 * should refer to it.
455 int inet_release(struct socket *sock)
457 struct sock *sk = sock->sk;
462 /* Applications forget to leave groups before exiting */
463 ip_mc_drop_socket(sk);
465 /* If linger is set, we don't return until the close
466 * is complete. Otherwise we return immediately. The
467 * actually closing is done the same either way.
469 * If the close is due to the process exiting, we never
473 if (sock_flag(sk, SOCK_LINGER) &&
474 !(current->flags & PF_EXITING))
475 timeout = sk->sk_lingertime;
477 clr_vx_info(&sk->sk_vx_info);
479 clr_nx_info(&sk->sk_nx_info);
481 sk->sk_prot->close(sk, timeout);
486 /* It is off by default, see below. */
487 int sysctl_ip_nonlocal_bind;
489 int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
491 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
492 struct sock *sk = sock->sk;
493 struct inet_opt *inet = inet_sk(sk);
497 __u32 s_addr; /* Address used for validation */
499 __u32 s_addr2 = 0xffffffffl; /* Optional address of the socket */
500 struct nx_info *nxi = sk->sk_nx_info;
502 /* If the socket has its own bind function then use it. (RAW) */
503 if (sk->sk_prot->bind) {
504 err = sk->sk_prot->bind(sk, uaddr, addr_len);
508 if (addr_len < sizeof(struct sockaddr_in))
511 s_addr = s_addr1 = addr->sin_addr.s_addr;
512 nxdprintk("inet_bind(%p) %p,%p;%lx\n",
513 sk, nx_info, sk->sk_socket,
514 (sk->sk_socket?sk->sk_socket->flags:0));
516 __u32 v4_bcast = nxi->v4_bcast;
517 __u32 ipv4root = nxi->ipv4[0];
518 int nbipv4 = nxi->nbipv4;
527 } else if (s_addr == 0x0100007f) {
528 s_addr = s_addr1 = ipv4root;
529 } else if (s_addr != v4_bcast) {
531 for (i=0; i<nbipv4; i++) {
532 if (s_addr == nxi->ipv4[i])
536 return -EADDRNOTAVAIL;
540 chk_addr_ret = inet_addr_type(s_addr);
542 /* Not specified by any standard per-se, however it breaks too
543 * many applications when removed. It is unfortunate since
544 * allowing applications to make a non-local bind solves
545 * several problems with systems using dynamic addressing.
546 * (ie. your servers still start up even if your ISDN link
547 * is temporarily down)
549 err = -EADDRNOTAVAIL;
550 if (!sysctl_ip_nonlocal_bind &&
552 s_addr != INADDR_ANY &&
553 chk_addr_ret != RTN_LOCAL &&
554 chk_addr_ret != RTN_MULTICAST &&
555 chk_addr_ret != RTN_BROADCAST)
558 snum = ntohs(addr->sin_port);
560 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
563 /* We keep a pair of addresses. rcv_saddr is the one
564 * used by hash lookups, and saddr is used for transmit.
566 * In the BSD API these are the same except where it
567 * would be illegal to use them (multicast/broadcast) in
568 * which case the sending device address is used.
572 /* Check these errors (active socket, double bind). */
574 if (sk->sk_state != TCP_CLOSE || inet->num)
575 goto out_release_sock;
577 inet->rcv_saddr = inet->saddr = s_addr1;
578 inet->rcv_saddr2 = s_addr2;
579 if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
580 inet->saddr = 0; /* Use device */
582 /* Make sure we are allowed to bind here. */
583 if (sk->sk_prot->get_port(sk, snum)) {
584 inet->saddr = inet->rcv_saddr = 0;
586 goto out_release_sock;
590 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
592 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
593 inet->sport = htons(inet->num);
604 int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
605 int addr_len, int flags)
607 struct sock *sk = sock->sk;
609 if (uaddr->sa_family == AF_UNSPEC)
610 return sk->sk_prot->disconnect(sk, flags);
612 if (!inet_sk(sk)->num && inet_autobind(sk))
614 return sk->sk_prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
617 static long inet_wait_for_connect(struct sock *sk, long timeo)
621 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
623 /* Basic assumption: if someone sets sk->sk_err, he _must_
624 * change state of the socket from TCP_SYN_*.
625 * Connect() does not allow to get error notifications
626 * without closing the socket.
628 while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
630 timeo = schedule_timeout(timeo);
632 if (signal_pending(current) || !timeo)
634 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
636 finish_wait(sk->sk_sleep, &wait);
641 * Connect to a remote host. There is regrettably still a little
642 * TCP 'magic' in here.
644 int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
645 int addr_len, int flags)
647 struct sock *sk = sock->sk;
653 if (uaddr->sa_family == AF_UNSPEC) {
654 err = sk->sk_prot->disconnect(sk, flags);
655 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
659 switch (sock->state) {
668 /* Fall out of switch with err, set for this state */
672 if (sk->sk_state != TCP_CLOSE)
675 err = sk->sk_prot->connect(sk, uaddr, addr_len);
679 sock->state = SS_CONNECTING;
681 /* Just entered SS_CONNECTING state; the only
682 * difference is that return value in non-blocking
683 * case is EINPROGRESS, rather than EALREADY.
689 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
691 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
692 /* Error code is set above */
693 if (!timeo || !inet_wait_for_connect(sk, timeo))
696 err = sock_intr_errno(timeo);
697 if (signal_pending(current))
701 /* Connection was closed by RST, timeout, ICMP error
702 * or another process disconnected us.
704 if (sk->sk_state == TCP_CLOSE)
707 /* sk->sk_err may be not zero now, if RECVERR was ordered by user
708 * and error was received after socket entered established state.
709 * Hence, it is handled normally after connect() return successfully.
712 sock->state = SS_CONNECTED;
719 err = sock_error(sk) ? : -ECONNABORTED;
720 sock->state = SS_UNCONNECTED;
721 if (sk->sk_prot->disconnect(sk, flags))
722 sock->state = SS_DISCONNECTING;
727 * Accept a pending connection. The TCP layer now gives BSD semantics.
730 int inet_accept(struct socket *sock, struct socket *newsock, int flags)
732 struct sock *sk1 = sock->sk;
734 struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err);
741 BUG_TRAP((1 << sk2->sk_state) &
742 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_CLOSE));
744 sock_graft(sk2, newsock);
746 newsock->state = SS_CONNECTED;
755 * This does both peername and sockname.
757 int inet_getname(struct socket *sock, struct sockaddr *uaddr,
758 int *uaddr_len, int peer)
760 struct sock *sk = sock->sk;
761 struct inet_opt *inet = inet_sk(sk);
762 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
764 sin->sin_family = AF_INET;
767 (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
770 sin->sin_port = inet->dport;
771 sin->sin_addr.s_addr = inet->daddr;
773 __u32 addr = inet->rcv_saddr;
776 sin->sin_port = inet->sport;
777 sin->sin_addr.s_addr = addr;
779 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
780 *uaddr_len = sizeof(*sin);
785 int inet_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
786 size_t size, int flags)
788 struct sock *sk = sock->sk;
792 err = sk->sk_prot->recvmsg(iocb, sk, msg, size, flags & MSG_DONTWAIT,
793 flags & ~MSG_DONTWAIT, &addr_len);
795 msg->msg_namelen = addr_len;
800 int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
803 struct sock *sk = sock->sk;
805 /* We may need to bind the socket. */
806 if (!inet_sk(sk)->num && inet_autobind(sk))
809 return sk->sk_prot->sendmsg(iocb, sk, msg, size);
813 ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
815 struct sock *sk = sock->sk;
817 /* We may need to bind the socket. */
818 if (!inet_sk(sk)->num && inet_autobind(sk))
821 if (sk->sk_prot->sendpage)
822 return sk->sk_prot->sendpage(sk, page, offset, size, flags);
823 return sock_no_sendpage(sock, page, offset, size, flags);
827 int inet_shutdown(struct socket *sock, int how)
829 struct sock *sk = sock->sk;
832 /* This should really check to make sure
833 * the socket is a TCP socket. (WHY AC...)
835 how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
838 if ((how & ~SHUTDOWN_MASK) || !how) /* MAXINT->0 */
842 if (sock->state == SS_CONNECTING) {
843 if ((1 << sk->sk_state) &
844 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
845 sock->state = SS_DISCONNECTING;
847 sock->state = SS_CONNECTED;
850 switch (sk->sk_state) {
853 /* Hack to wake up other listeners, who can poll for
854 POLLHUP, even on eg. unconnected UDP sockets -- RR */
856 sk->sk_shutdown |= how;
857 if (sk->sk_prot->shutdown)
858 sk->sk_prot->shutdown(sk, how);
861 /* Remaining two branches are temporary solution for missing
862 * close() in multithreaded environment. It is _not_ a good idea,
863 * but we have no choice until close() is repaired at VFS level.
866 if (!(how & RCV_SHUTDOWN))
870 err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
871 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
875 /* Wake up anyone sleeping in poll. */
876 sk->sk_state_change(sk);
882 * ioctl() calls you can issue on an INET socket. Most of these are
883 * device configuration and stuff and very rarely used. Some ioctls
884 * pass on to the socket itself.
886 * NOTE: I like the idea of a module for the config stuff. ie ifconfig
887 * loads the devconfigure module does its configuring and unloads it.
888 * There's a good 20K of config code hanging around the kernel.
891 int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
893 struct sock *sk = sock->sk;
898 err = sock_get_timestamp(sk, (struct timeval __user *)arg);
903 err = ip_rt_ioctl(cmd, (void *)arg);
908 err = arp_ioctl(cmd, (void __user *)arg);
921 err = devinet_ioctl(cmd, (void __user *)arg);
924 if (!sk->sk_prot->ioctl ||
925 (err = sk->sk_prot->ioctl(sk, cmd, arg)) ==
927 err = dev_ioctl(cmd, (void __user *)arg);
933 struct proto_ops inet_stream_ops = {
935 .owner = THIS_MODULE,
936 .release = inet_release,
938 .connect = inet_stream_connect,
939 .socketpair = sock_no_socketpair,
940 .accept = inet_accept,
941 .getname = inet_getname,
944 .listen = inet_listen,
945 .shutdown = inet_shutdown,
946 .setsockopt = inet_setsockopt,
947 .getsockopt = inet_getsockopt,
948 .sendmsg = inet_sendmsg,
949 .recvmsg = inet_recvmsg,
950 .mmap = sock_no_mmap,
951 .sendpage = tcp_sendpage
954 struct proto_ops inet_dgram_ops = {
956 .owner = THIS_MODULE,
957 .release = inet_release,
959 .connect = inet_dgram_connect,
960 .socketpair = sock_no_socketpair,
961 .accept = sock_no_accept,
962 .getname = inet_getname,
963 .poll = datagram_poll,
965 .listen = sock_no_listen,
966 .shutdown = inet_shutdown,
967 .setsockopt = inet_setsockopt,
968 .getsockopt = inet_getsockopt,
969 .sendmsg = inet_sendmsg,
970 .recvmsg = inet_recvmsg,
971 .mmap = sock_no_mmap,
972 .sendpage = inet_sendpage,
975 struct net_proto_family inet_family_ops = {
977 .create = inet_create,
978 .owner = THIS_MODULE,
982 extern void tcp_init(void);
983 extern void tcp_v4_init(struct net_proto_family *);
985 /* Upon startup we insert all the elements in inetsw_array[] into
986 * the linked list inetsw.
988 static struct inet_protosw inetsw_array[] =
992 .protocol = IPPROTO_TCP,
994 .ops = &inet_stream_ops,
997 .flags = INET_PROTOSW_PERMANENT,
1002 .protocol = IPPROTO_UDP,
1004 .ops = &inet_dgram_ops,
1006 .no_check = UDP_CSUM_DEFAULT,
1007 .flags = INET_PROTOSW_PERMANENT,
1013 .protocol = IPPROTO_IP, /* wild card */
1015 .ops = &inet_dgram_ops,
1016 .capability = CAP_NET_RAW,
1017 .no_check = UDP_CSUM_DEFAULT,
1018 .flags = INET_PROTOSW_REUSE,
1022 #define INETSW_ARRAY_LEN (sizeof(inetsw_array) / sizeof(struct inet_protosw))
1024 void inet_register_protosw(struct inet_protosw *p)
1026 struct list_head *lh;
1027 struct inet_protosw *answer;
1028 int protocol = p->protocol;
1029 struct list_head *last_perm;
1031 spin_lock_bh(&inetsw_lock);
1033 if (p->type >= SOCK_MAX)
1036 /* If we are trying to override a permanent protocol, bail. */
1038 last_perm = &inetsw[p->type];
1039 list_for_each(lh, &inetsw[p->type]) {
1040 answer = list_entry(lh, struct inet_protosw, list);
1042 /* Check only the non-wild match. */
1043 if (INET_PROTOSW_PERMANENT & answer->flags) {
1044 if (protocol == answer->protocol)
1054 /* Add the new entry after the last permanent entry if any, so that
1055 * the new entry does not override a permanent entry when matched with
1056 * a wild-card protocol. But it is allowed to override any existing
1057 * non-permanent entry. This means that when we remove this entry, the
1058 * system automatically returns to the old behavior.
1060 list_add_rcu(&p->list, last_perm);
1062 spin_unlock_bh(&inetsw_lock);
1069 printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
1075 "Ignoring attempt to register invalid socket type %d.\n",
1080 void inet_unregister_protosw(struct inet_protosw *p)
1082 if (INET_PROTOSW_PERMANENT & p->flags) {
1084 "Attempt to unregister permanent protocol %d.\n",
1087 spin_lock_bh(&inetsw_lock);
1088 list_del_rcu(&p->list);
1089 spin_unlock_bh(&inetsw_lock);
1095 #ifdef CONFIG_IP_MULTICAST
1096 static struct inet_protocol igmp_protocol = {
1097 .handler = igmp_rcv,
1101 static struct inet_protocol tcp_protocol = {
1102 .handler = tcp_v4_rcv,
1103 .err_handler = tcp_v4_err,
1107 static struct inet_protocol udp_protocol = {
1109 .err_handler = udp_err,
1113 static struct inet_protocol icmp_protocol = {
1114 .handler = icmp_rcv,
1117 static int __init init_ipv4_mibs(void)
1119 net_statistics[0] = alloc_percpu(struct linux_mib);
1120 net_statistics[1] = alloc_percpu(struct linux_mib);
1121 ip_statistics[0] = alloc_percpu(struct ipstats_mib);
1122 ip_statistics[1] = alloc_percpu(struct ipstats_mib);
1123 icmp_statistics[0] = alloc_percpu(struct icmp_mib);
1124 icmp_statistics[1] = alloc_percpu(struct icmp_mib);
1125 tcp_statistics[0] = alloc_percpu(struct tcp_mib);
1126 tcp_statistics[1] = alloc_percpu(struct tcp_mib);
1127 udp_statistics[0] = alloc_percpu(struct udp_mib);
1128 udp_statistics[1] = alloc_percpu(struct udp_mib);
1130 (net_statistics[0] && net_statistics[1] && ip_statistics[0]
1131 && ip_statistics[1] && tcp_statistics[0] && tcp_statistics[1]
1132 && udp_statistics[0] && udp_statistics[1]))
1135 (void) tcp_mib_init();
1140 int ipv4_proc_init(void);
1141 extern void ipfrag_init(void);
1143 static int __init inet_init(void)
1145 struct sk_buff *dummy_skb;
1146 struct inet_protosw *q;
1147 struct list_head *r;
1149 if (sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb)) {
1150 printk(KERN_CRIT "%s: panic\n", __FUNCTION__);
1154 tcp_sk_cachep = kmem_cache_create("tcp_sock",
1155 sizeof(struct tcp_sock), 0,
1156 SLAB_HWCACHE_ALIGN, 0, 0);
1157 udp_sk_cachep = kmem_cache_create("udp_sock",
1158 sizeof(struct udp_sock), 0,
1159 SLAB_HWCACHE_ALIGN, 0, 0);
1160 raw4_sk_cachep = kmem_cache_create("raw4_sock",
1161 sizeof(struct raw_sock), 0,
1162 SLAB_HWCACHE_ALIGN, 0, 0);
1163 if (!tcp_sk_cachep || !udp_sk_cachep || !raw4_sk_cachep)
1165 "inet_init: Can't create protocol sock SLAB caches!\n");
1167 * Tell SOCKET that we are alive...
1170 (void)sock_register(&inet_family_ops);
1173 * Add all the base protocols.
1176 if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
1177 printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
1178 if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
1179 printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
1180 if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
1181 printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n");
1182 #ifdef CONFIG_IP_MULTICAST
1183 if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
1184 printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n");
1187 /* Register the socket-side information for inet_create. */
1188 for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
1191 for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
1192 inet_register_protosw(q);
1195 * Set the ARP module up
1201 * Set the IP module up
1206 tcp_v4_init(&inet_family_ops);
1208 /* Setup TCP slab cache for open requests. */
1213 * Set the ICMP layer up
1216 icmp_init(&inet_family_ops);
1219 * Initialise the multicast router
1221 #if defined(CONFIG_IP_MROUTE)
1225 * Initialise per-cpu ipv4 mibs
1228 if(init_ipv4_mibs())
1229 printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n"); ;
1238 module_init(inet_init);
1240 /* ------------------------------------------------------------------------ */
1242 #ifdef CONFIG_PROC_FS
1243 extern int fib_proc_init(void);
1244 extern void fib_proc_exit(void);
1245 extern int ip_misc_proc_init(void);
1246 extern int raw_proc_init(void);
1247 extern void raw_proc_exit(void);
1248 extern int tcp4_proc_init(void);
1249 extern void tcp4_proc_exit(void);
1250 extern int udp4_proc_init(void);
1251 extern void udp4_proc_exit(void);
1253 int __init ipv4_proc_init(void)
1257 if (raw_proc_init())
1259 if (tcp4_proc_init())
1261 if (udp4_proc_init())
1263 if (fib_proc_init())
1265 if (ip_misc_proc_init())
1282 #else /* CONFIG_PROC_FS */
1283 int __init ipv4_proc_init(void)
1287 #endif /* CONFIG_PROC_FS */
1289 MODULE_ALIAS_NETPROTO(PF_INET);
1291 EXPORT_SYMBOL(inet_accept);
1292 EXPORT_SYMBOL(inet_bind);
1293 EXPORT_SYMBOL(inet_dgram_connect);
1294 EXPORT_SYMBOL(inet_dgram_ops);
1295 EXPORT_SYMBOL(inet_family_ops);
1296 EXPORT_SYMBOL(inet_getname);
1297 EXPORT_SYMBOL(inet_getsockopt);
1298 EXPORT_SYMBOL(inet_ioctl);
1299 EXPORT_SYMBOL(inet_listen);
1300 EXPORT_SYMBOL(inet_recvmsg);
1301 EXPORT_SYMBOL(inet_register_protosw);
1302 EXPORT_SYMBOL(inet_release);
1303 EXPORT_SYMBOL(inet_sendmsg);
1304 EXPORT_SYMBOL(inet_setsockopt);
1305 EXPORT_SYMBOL(inet_shutdown);
1306 EXPORT_SYMBOL(inet_sock_destruct);
1307 EXPORT_SYMBOL(inet_sock_release);
1308 EXPORT_SYMBOL(inet_stream_connect);
1309 EXPORT_SYMBOL(inet_stream_ops);
1310 EXPORT_SYMBOL(inet_unregister_protosw);
1311 EXPORT_SYMBOL(net_statistics);
1312 EXPORT_SYMBOL(tcp_protocol);
1313 EXPORT_SYMBOL(udp_protocol);
1315 #ifdef INET_REFCNT_DEBUG
1316 EXPORT_SYMBOL(inet_sock_nr);