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.136 2001/11/06 22:21:08 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 structures
26 * Alan Cox : Semantics of SO_LINGER aren't state moved
27 * to close when you look carefully. With
28 * this fixed and the accept bug fixed
29 * some RPC stuff seems happier.
30 * Niibe Yutaka : 4.4BSD style write async I/O
32 * Tony Gale : Fixed reuse semantics.
33 * Alan Cox : bind() shouldn't abort existing but dead
34 * sockets. Stops FTP netin:.. I hope.
35 * Alan Cox : bind() works correctly for RAW sockets. Note
36 * that FreeBSD at least was broken in this respect
37 * so be careful with compatibility tests...
38 * Alan Cox : routing cache support
39 * Alan Cox : memzero the socket structure for compactness.
40 * Matt Day : nonblock connect error handler
41 * Alan Cox : Allow large numbers of pending sockets
42 * (eg for big web sites), but only if
43 * specifically application requested.
44 * Alan Cox : New buffering throughout IP. Used dumbly.
45 * Alan Cox : New buffering now used smartly.
46 * Alan Cox : BSD rather than common sense interpretation of
48 * Germano Caronni : Assorted small races.
49 * Alan Cox : sendmsg/recvmsg basic support.
50 * Alan Cox : Only sendmsg/recvmsg now supported.
51 * Alan Cox : Locked down bind (see security list).
52 * Alan Cox : Loosened bind a little.
53 * Mike McLagan : ADD/DEL DLCI Ioctls
54 * Willy Konynenberg : Transparent proxying support.
55 * David S. Miller : New socket lookup architecture.
56 * Some other random speedups.
57 * Cyrus Durgin : Cleaned up file for kmod hacks.
58 * Andi Kleen : Fix inet_stream_connect TCP race.
60 * This program is free software; you can redistribute it and/or
61 * modify it under the terms of the GNU General Public License
62 * as published by the Free Software Foundation; either version
63 * 2 of the License, or (at your option) any later version.
66 #include <linux/config.h>
67 #include <linux/errno.h>
68 #include <linux/types.h>
69 #include <linux/socket.h>
71 #include <linux/kernel.h>
72 #include <linux/major.h>
73 #include <linux/sched.h>
74 #include <linux/timer.h>
75 #include <linux/string.h>
76 #include <linux/sockios.h>
77 #include <linux/net.h>
78 #include <linux/fcntl.h>
80 #include <linux/interrupt.h>
81 #include <linux/proc_fs.h>
82 #include <linux/stat.h>
83 #include <linux/init.h>
84 #include <linux/poll.h>
85 #include <linux/netfilter_ipv4.h>
87 #include <asm/uaccess.h>
88 #include <asm/system.h>
90 #include <linux/smp_lock.h>
91 #include <linux/inet.h>
92 #include <linux/netdevice.h>
93 #include <linux/brlock.h>
95 #include <net/protocol.h>
97 #include <net/route.h>
100 #include <linux/skbuff.h>
101 #include <net/sock.h>
103 #include <net/icmp.h>
104 #include <net/ipip.h>
105 #include <net/inet_common.h>
106 #ifdef CONFIG_IP_MROUTE
107 #include <linux/mroute.h>
109 #include <linux/if_bridge.h>
111 #include <linux/kmod.h>
113 #ifdef CONFIG_NET_DIVERT
114 #include <linux/divert.h>
115 #endif /* CONFIG_NET_DIVERT */
116 #if defined(CONFIG_NET_RADIO) || defined(CONFIG_NET_PCMCIA_RADIO)
117 #include <linux/wireless.h> /* Note : will define WIRELESS_EXT */
118 #endif /* CONFIG_NET_RADIO || CONFIG_NET_PCMCIA_RADIO */
120 struct linux_mib net_statistics[NR_CPUS*2];
122 #ifdef INET_REFCNT_DEBUG
123 atomic_t inet_sock_nr;
126 extern int raw_get_info(char *, char **, off_t, int);
127 extern int snmp_get_info(char *, char **, off_t, int);
128 extern int netstat_get_info(char *, char **, off_t, int);
129 extern int afinet_get_info(char *, char **, off_t, int);
130 extern int tcp_get_info(char *, char **, off_t, int);
131 extern int udp_get_info(char *, char **, off_t, int);
132 extern void ip_mc_drop_socket(struct sock *sk);
135 extern int dlci_ioctl(unsigned int, void*);
138 #ifdef CONFIG_DLCI_MODULE
139 int (*dlci_ioctl_hook)(unsigned int, void *);
142 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
143 int (*br_ioctl_hook)(unsigned long);
146 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
147 int (*vlan_ioctl_hook)(unsigned long arg);
150 /* The inetsw table contains everything that inet_create needs to
151 * build a new socket.
153 struct list_head inetsw[SOCK_MAX];
155 /* New destruction routine */
157 void inet_sock_destruct(struct sock *sk)
159 __skb_queue_purge(&sk->receive_queue);
160 __skb_queue_purge(&sk->error_queue);
162 if (sk->type == SOCK_STREAM && sk->state != TCP_CLOSE) {
163 printk("Attempt to release TCP socket in state %d %p\n",
169 printk("Attempt to release alive inet socket %p\n", sk);
173 BUG_TRAP(atomic_read(&sk->rmem_alloc) == 0);
174 BUG_TRAP(atomic_read(&sk->wmem_alloc) == 0);
175 BUG_TRAP(sk->wmem_queued == 0);
176 BUG_TRAP(sk->forward_alloc == 0);
178 if (sk->protinfo.af_inet.opt)
179 kfree(sk->protinfo.af_inet.opt);
180 dst_release(sk->dst_cache);
181 #ifdef INET_REFCNT_DEBUG
182 atomic_dec(&inet_sock_nr);
183 printk(KERN_DEBUG "INET socket %p released, %d are still alive\n", sk, atomic_read(&inet_sock_nr));
187 void inet_sock_release(struct sock *sk)
189 if (sk->prot->destroy)
190 sk->prot->destroy(sk);
192 /* Observation: when inet_sock_release is called, processes have
193 * no access to socket. But net still has.
194 * Step one, detach it from networking:
196 * A. Remove from hash tables.
199 sk->prot->unhash(sk);
201 /* In this point socket cannot receive new packets,
202 * but it is possible that some packets are in flight
203 * because some CPU runs receiver and did hash table lookup
204 * before we unhashed socket. They will achieve receive queue
205 * and will be purged by socket destructor.
207 * Also we still have packets pending on receive
208 * queue and probably, our own packets waiting in device queues.
209 * sock_destroy will drain receive queue, but transmitted
210 * packets will delay socket destruction until the last reference
216 #ifdef INET_REFCNT_DEBUG
217 if (atomic_read(&sk->refcnt) != 1) {
218 printk(KERN_DEBUG "Destruction inet %p delayed, c=%d\n", sk, atomic_read(&sk->refcnt));
226 * The routines beyond this point handle the behaviour of an AF_INET
227 * socket object. Mostly it punts to the subprotocols of IP to do
233 * Set socket options on an inet socket.
236 int inet_setsockopt(struct socket *sock, int level, int optname,
237 char *optval, int optlen)
239 struct sock *sk=sock->sk;
241 return sk->prot->setsockopt(sk,level,optname,optval,optlen);
245 * Get a socket option on an AF_INET socket.
247 * FIX: POSIX 1003.1g is very ambiguous here. It states that
248 * asynchronous errors should be reported by getsockopt. We assume
249 * this means if you specify SO_ERROR (otherwise whats the point of it).
252 int inet_getsockopt(struct socket *sock, int level, int optname,
253 char *optval, int *optlen)
255 struct sock *sk=sock->sk;
257 return sk->prot->getsockopt(sk,level,optname,optval,optlen);
261 * Automatically bind an unbound socket.
264 static int inet_autobind(struct sock *sk)
266 /* We may need to bind the socket. */
269 if (sk->prot->get_port(sk, 0) != 0) {
273 sk->sport = htons(sk->num);
280 * Move a socket into listening state.
283 int inet_listen(struct socket *sock, int backlog)
285 struct sock *sk = sock->sk;
286 unsigned char old_state;
292 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
295 old_state = sk->state;
296 if (!((1<<old_state)&(TCPF_CLOSE|TCPF_LISTEN)))
299 /* Really, if the socket is already in listen state
300 * we can only allow the backlog to be adjusted.
302 if (old_state != TCP_LISTEN) {
303 err = tcp_listen_start(sk);
307 sk->max_ack_backlog = backlog;
316 * Create an inet socket.
319 static int inet_create(struct socket *sock, int protocol)
323 struct inet_protosw *answer;
325 sock->state = SS_UNCONNECTED;
326 sk = sk_alloc(PF_INET, GFP_KERNEL, 1);
330 /* Look for the requested type/protocol pair. */
332 br_read_lock_bh(BR_NETPROTO_LOCK);
333 list_for_each(p, &inetsw[sock->type]) {
334 answer = list_entry(p, struct inet_protosw, list);
336 /* Check the non-wild match. */
337 if (protocol == answer->protocol) {
338 if (protocol != IPPROTO_IP)
341 /* Check for the two wild cases. */
342 if (IPPROTO_IP == protocol) {
343 protocol = answer->protocol;
346 if (IPPROTO_IP == answer->protocol)
351 br_read_unlock_bh(BR_NETPROTO_LOCK);
354 goto free_and_badtype;
355 if (answer->capability > 0 && !capable(answer->capability))
356 goto free_and_badperm;
358 goto free_and_noproto;
360 sock->ops = answer->ops;
361 sk->prot = answer->prot;
362 sk->no_check = answer->no_check;
363 if (INET_PROTOSW_REUSE & answer->flags)
366 if (SOCK_RAW == sock->type) {
368 if (IPPROTO_RAW == protocol)
369 sk->protinfo.af_inet.hdrincl = 1;
372 if (ipv4_config.no_pmtu_disc)
373 sk->protinfo.af_inet.pmtudisc = IP_PMTUDISC_DONT;
375 sk->protinfo.af_inet.pmtudisc = IP_PMTUDISC_WANT;
377 sk->protinfo.af_inet.id = 0;
379 sock_init_data(sock,sk);
381 sk->destruct = inet_sock_destruct;
384 sk->family = PF_INET;
385 sk->protocol = protocol;
387 sk->backlog_rcv = sk->prot->backlog_rcv;
389 sk->protinfo.af_inet.ttl = sysctl_ip_default_ttl;
391 sk->protinfo.af_inet.mc_loop = 1;
392 sk->protinfo.af_inet.mc_ttl = 1;
393 sk->protinfo.af_inet.mc_index = 0;
394 sk->protinfo.af_inet.mc_list = NULL;
396 #ifdef INET_REFCNT_DEBUG
397 atomic_inc(&inet_sock_nr);
401 /* It assumes that any protocol which allows
402 * the user to assign a number at socket
403 * creation time automatically
406 sk->sport = htons(sk->num);
408 /* Add to protocol hash chains. */
412 if (sk->prot->init) {
413 int err = sk->prot->init(sk);
415 inet_sock_release(sk);
423 return -ESOCKTNOSUPPORT;
431 return -EPROTONOSUPPORT;
439 * The peer socket should always be NULL (or else). When we call this
440 * function we are destroying the object and from then on nobody
441 * should refer to it.
444 int inet_release(struct socket *sock)
446 struct sock *sk = sock->sk;
451 /* Applications forget to leave groups before exiting */
452 ip_mc_drop_socket(sk);
454 /* If linger is set, we don't return until the close
455 * is complete. Otherwise we return immediately. The
456 * actually closing is done the same either way.
458 * If the close is due to the process exiting, we never
462 if (sk->linger && !(current->flags & PF_EXITING))
463 timeout = sk->lingertime;
465 sk->prot->close(sk, timeout);
470 /* It is off by default, see below. */
471 int sysctl_ip_nonlocal_bind;
473 int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
475 struct sockaddr_in *addr=(struct sockaddr_in *)uaddr;
476 struct sock *sk=sock->sk;
481 /* If the socket has its own bind function then use it. (RAW) */
483 return sk->prot->bind(sk, uaddr, addr_len);
485 if (addr_len < sizeof(struct sockaddr_in))
488 chk_addr_ret = inet_addr_type(addr->sin_addr.s_addr);
490 /* Not specified by any standard per-se, however it breaks too
491 * many applications when removed. It is unfortunate since
492 * allowing applications to make a non-local bind solves
493 * several problems with systems using dynamic addressing.
494 * (ie. your servers still start up even if your ISDN link
495 * is temporarily down)
497 if (sysctl_ip_nonlocal_bind == 0 &&
498 sk->protinfo.af_inet.freebind == 0 &&
499 addr->sin_addr.s_addr != INADDR_ANY &&
500 chk_addr_ret != RTN_LOCAL &&
501 chk_addr_ret != RTN_MULTICAST &&
502 chk_addr_ret != RTN_BROADCAST)
503 return -EADDRNOTAVAIL;
505 snum = ntohs(addr->sin_port);
506 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
509 /* We keep a pair of addresses. rcv_saddr is the one
510 * used by hash lookups, and saddr is used for transmit.
512 * In the BSD API these are the same except where it
513 * would be illegal to use them (multicast/broadcast) in
514 * which case the sending device address is used.
518 /* Check these errors (active socket, double bind). */
520 if ((sk->state != TCP_CLOSE) ||
524 sk->rcv_saddr = sk->saddr = addr->sin_addr.s_addr;
525 if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
526 sk->saddr = 0; /* Use device */
528 /* Make sure we are allowed to bind here. */
529 if (sk->prot->get_port(sk, snum) != 0) {
530 sk->saddr = sk->rcv_saddr = 0;
536 sk->userlocks |= SOCK_BINDADDR_LOCK;
538 sk->userlocks |= SOCK_BINDPORT_LOCK;
539 sk->sport = htons(sk->num);
549 int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
550 int addr_len, int flags)
552 struct sock *sk=sock->sk;
554 if (uaddr->sa_family == AF_UNSPEC)
555 return sk->prot->disconnect(sk, flags);
557 if (sk->num==0 && inet_autobind(sk) != 0)
559 return sk->prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
562 static long inet_wait_for_connect(struct sock *sk, long timeo)
564 DECLARE_WAITQUEUE(wait, current);
566 __set_current_state(TASK_INTERRUPTIBLE);
567 add_wait_queue(sk->sleep, &wait);
569 /* Basic assumption: if someone sets sk->err, he _must_
570 * change state of the socket from TCP_SYN_*.
571 * Connect() does not allow to get error notifications
572 * without closing the socket.
574 while ((1<<sk->state)&(TCPF_SYN_SENT|TCPF_SYN_RECV)) {
576 timeo = schedule_timeout(timeo);
578 if (signal_pending(current) || !timeo)
580 set_current_state(TASK_INTERRUPTIBLE);
582 __set_current_state(TASK_RUNNING);
583 remove_wait_queue(sk->sleep, &wait);
588 * Connect to a remote host. There is regrettably still a little
589 * TCP 'magic' in here.
592 int inet_stream_connect(struct socket *sock, struct sockaddr * uaddr,
593 int addr_len, int flags)
595 struct sock *sk=sock->sk;
601 if (uaddr->sa_family == AF_UNSPEC) {
602 err = sk->prot->disconnect(sk, flags);
603 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
607 switch (sock->state) {
616 /* Fall out of switch with err, set for this state */
620 if (sk->state != TCP_CLOSE)
623 err = sk->prot->connect(sk, uaddr, addr_len);
627 sock->state = SS_CONNECTING;
629 /* Just entered SS_CONNECTING state; the only
630 * difference is that return value in non-blocking
631 * case is EINPROGRESS, rather than EALREADY.
637 timeo = sock_sndtimeo(sk, flags&O_NONBLOCK);
639 if ((1<<sk->state)&(TCPF_SYN_SENT|TCPF_SYN_RECV)) {
640 /* Error code is set above */
641 if (!timeo || !inet_wait_for_connect(sk, timeo))
644 err = sock_intr_errno(timeo);
645 if (signal_pending(current))
649 /* Connection was closed by RST, timeout, ICMP error
650 * or another process disconnected us.
652 if (sk->state == TCP_CLOSE)
655 /* sk->err may be not zero now, if RECVERR was ordered by user
656 * and error was received after socket entered established state.
657 * Hence, it is handled normally after connect() return successfully.
660 sock->state = SS_CONNECTED;
667 err = sock_error(sk) ? : -ECONNABORTED;
668 sock->state = SS_UNCONNECTED;
669 if (sk->prot->disconnect(sk, flags))
670 sock->state = SS_DISCONNECTING;
675 * Accept a pending connection. The TCP layer now gives BSD semantics.
678 int inet_accept(struct socket *sock, struct socket *newsock, int flags)
680 struct sock *sk1 = sock->sk;
684 if((sk2 = sk1->prot->accept(sk1,flags,&err)) == NULL)
689 BUG_TRAP((1<<sk2->state)&(TCPF_ESTABLISHED|TCPF_CLOSE_WAIT|TCPF_CLOSE));
691 sock_graft(sk2, newsock);
693 newsock->state = SS_CONNECTED;
703 * This does both peername and sockname.
706 int inet_getname(struct socket *sock, struct sockaddr *uaddr,
707 int *uaddr_len, int peer)
709 struct sock *sk = sock->sk;
710 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
712 sin->sin_family = AF_INET;
716 if (((1<<sk->state)&(TCPF_CLOSE|TCPF_SYN_SENT)) && peer == 1)
718 sin->sin_port = sk->dport;
719 sin->sin_addr.s_addr = sk->daddr;
721 __u32 addr = sk->rcv_saddr;
724 sin->sin_port = sk->sport;
725 sin->sin_addr.s_addr = addr;
727 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
728 *uaddr_len = sizeof(*sin);
734 int inet_recvmsg(struct socket *sock, struct msghdr *msg, int size,
735 int flags, struct scm_cookie *scm)
737 struct sock *sk = sock->sk;
741 err = sk->prot->recvmsg(sk, msg, size, flags&MSG_DONTWAIT,
742 flags&~MSG_DONTWAIT, &addr_len);
744 msg->msg_namelen = addr_len;
749 int inet_sendmsg(struct socket *sock, struct msghdr *msg, int size,
750 struct scm_cookie *scm)
752 struct sock *sk = sock->sk;
754 /* We may need to bind the socket. */
755 if (sk->num==0 && inet_autobind(sk) != 0)
758 return sk->prot->sendmsg(sk, msg, size);
761 int inet_shutdown(struct socket *sock, int how)
763 struct sock *sk = sock->sk;
766 /* This should really check to make sure
767 * the socket is a TCP socket. (WHY AC...)
769 how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
772 if ((how & ~SHUTDOWN_MASK) || how==0) /* MAXINT->0 */
776 if (sock->state == SS_CONNECTING) {
777 if ((1<<sk->state)&(TCPF_SYN_SENT|TCPF_SYN_RECV|TCPF_CLOSE))
778 sock->state = SS_DISCONNECTING;
780 sock->state = SS_CONNECTED;
786 /* Hack to wake up other listeners, who can poll for
787 POLLHUP, even on eg. unconnected UDP sockets -- RR */
790 if (sk->prot->shutdown)
791 sk->prot->shutdown(sk, how);
794 /* Remaining two branches are temporary solution for missing
795 * close() in multithreaded environment. It is _not_ a good idea,
796 * but we have no choice until close() is repaired at VFS level.
799 if (!(how & RCV_SHUTDOWN))
803 err = sk->prot->disconnect(sk, O_NONBLOCK);
804 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
808 /* Wake up anyone sleeping in poll. */
809 sk->state_change(sk);
815 * ioctl() calls you can issue on an INET socket. Most of these are
816 * device configuration and stuff and very rarely used. Some ioctls
817 * pass on to the socket itself.
819 * NOTE: I like the idea of a module for the config stuff. ie ifconfig
820 * loads the devconfigure module does its configuring and unloads it.
821 * There's a good 20K of config code hanging around the kernel.
824 int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
826 struct sock *sk = sock->sk;
833 err = get_user(pid, (int *) arg);
836 if (current->pid != pid && current->pgrp != -pid &&
837 !capable(CAP_NET_ADMIN))
843 return put_user(sk->proc, (int *)arg);
845 if(sk->stamp.tv_sec==0)
847 err = copy_to_user((void *)arg,&sk->stamp,sizeof(struct timeval));
854 return(ip_rt_ioctl(cmd,(void *) arg));
858 return(arp_ioctl(cmd,(void *) arg));
870 return(devinet_ioctl(cmd,(void *) arg));
873 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
875 if (br_ioctl_hook == NULL)
876 request_module("bridge");
878 if (br_ioctl_hook != NULL)
879 return br_ioctl_hook(arg);
885 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
887 if (vlan_ioctl_hook == NULL)
888 request_module("8021q");
890 if (vlan_ioctl_hook != NULL)
891 return vlan_ioctl_hook(arg);
897 #ifdef CONFIG_NET_DIVERT
898 return divert_ioctl(cmd, (struct divert_cf *) arg);
901 #endif /* CONFIG_NET_DIVERT */
907 err = dlci_ioctl(cmd, (void *) arg);
912 #ifdef CONFIG_DLCI_MODULE
915 if (dlci_ioctl_hook == NULL)
916 request_module("dlci");
919 if (dlci_ioctl_hook) {
921 err = (*dlci_ioctl_hook)(cmd, (void *) arg);
929 if ((cmd >= SIOCDEVPRIVATE) &&
930 (cmd <= (SIOCDEVPRIVATE + 15)))
931 return(dev_ioctl(cmd,(void *) arg));
934 if((cmd >= SIOCIWFIRST) && (cmd <= SIOCIWLAST))
935 return(dev_ioctl(cmd,(void *) arg));
936 #endif /* WIRELESS_EXT */
938 if (sk->prot->ioctl==NULL || (err=sk->prot->ioctl(sk, cmd, arg))==-ENOIOCTLCMD)
939 return(dev_ioctl(cmd,(void *) arg));
946 struct proto_ops inet_stream_ops = {
949 release: inet_release,
951 connect: inet_stream_connect,
952 socketpair: sock_no_socketpair,
954 getname: inet_getname,
958 shutdown: inet_shutdown,
959 setsockopt: inet_setsockopt,
960 getsockopt: inet_getsockopt,
961 sendmsg: inet_sendmsg,
962 recvmsg: inet_recvmsg,
964 sendpage: tcp_sendpage
967 struct proto_ops inet_dgram_ops = {
970 release: inet_release,
972 connect: inet_dgram_connect,
973 socketpair: sock_no_socketpair,
974 accept: sock_no_accept,
975 getname: inet_getname,
978 listen: sock_no_listen,
979 shutdown: inet_shutdown,
980 setsockopt: inet_setsockopt,
981 getsockopt: inet_getsockopt,
982 sendmsg: inet_sendmsg,
983 recvmsg: inet_recvmsg,
985 sendpage: sock_no_sendpage,
988 struct proto_ops inet_sockraw_ops = {
991 release: inet_release,
993 connect: inet_dgram_connect,
994 socketpair: sock_no_socketpair,
995 accept: sock_no_accept,
996 getname: inet_getname,
999 listen: sock_no_listen,
1000 shutdown: inet_shutdown,
1001 setsockopt: inet_setsockopt,
1002 getsockopt: inet_getsockopt,
1003 sendmsg: inet_sendmsg,
1004 recvmsg: inet_recvmsg,
1006 sendpage: sock_no_sendpage,
1009 struct net_proto_family inet_family_ops = {
1015 extern void tcp_init(void);
1016 extern void tcp_v4_init(struct net_proto_family *);
1018 /* Upon startup we insert all the elements in inetsw_array[] into
1019 * the linked list inetsw.
1021 static struct inet_protosw inetsw_array[] =
1025 protocol: IPPROTO_TCP,
1027 ops: &inet_stream_ops,
1030 flags: INET_PROTOSW_PERMANENT,
1035 protocol: IPPROTO_UDP,
1037 ops: &inet_dgram_ops,
1039 no_check: UDP_CSUM_DEFAULT,
1040 flags: INET_PROTOSW_PERMANENT,
1046 protocol: IPPROTO_IP, /* wild card */
1048 ops: &inet_sockraw_ops,
1049 capability: CAP_NET_RAW,
1050 no_check: UDP_CSUM_DEFAULT,
1051 flags: INET_PROTOSW_REUSE,
1055 #define INETSW_ARRAY_LEN (sizeof(inetsw_array) / sizeof(struct inet_protosw))
1058 inet_register_protosw(struct inet_protosw *p)
1060 struct list_head *lh;
1061 struct inet_protosw *answer;
1062 int protocol = p->protocol;
1063 struct list_head *last_perm;
1065 br_write_lock_bh(BR_NETPROTO_LOCK);
1067 if (p->type >= SOCK_MAX)
1070 /* If we are trying to override a permanent protocol, bail. */
1072 last_perm = &inetsw[p->type];
1073 list_for_each(lh, &inetsw[p->type]) {
1074 answer = list_entry(lh, struct inet_protosw, list);
1076 /* Check only the non-wild match. */
1077 if (INET_PROTOSW_PERMANENT & answer->flags) {
1078 if (protocol == answer->protocol)
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.
1094 list_add(&p->list, last_perm);
1096 br_write_unlock_bh(BR_NETPROTO_LOCK);
1100 printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
1106 "Ignoring attempt to register illegal socket type %d.\n",
1112 inet_unregister_protosw(struct inet_protosw *p)
1114 if (INET_PROTOSW_PERMANENT & p->flags) {
1116 "Attempt to unregister permanent protocol %d.\n",
1119 br_write_lock_bh(BR_NETPROTO_LOCK);
1121 br_write_unlock_bh(BR_NETPROTO_LOCK);
1125 extern void ipfrag_init(void);
1128 * Called by socket.c on kernel startup.
1131 static int __init inet_init(void)
1133 struct sk_buff *dummy_skb;
1134 struct inet_protocol *p;
1135 struct inet_protosw *q;
1136 struct list_head *r;
1138 printk(KERN_INFO "NET4: Linux TCP/IP 1.0 for NET4.0\n");
1140 if (sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb)) {
1141 printk(KERN_CRIT "inet_proto_init: panic\n");
1146 * Tell SOCKET that we are alive...
1149 (void) sock_register(&inet_family_ops);
1152 * Add all the protocols.
1155 printk(KERN_INFO "IP Protocols: ");
1156 for (p = inet_protocol_base; p != NULL;) {
1157 struct inet_protocol *tmp = (struct inet_protocol *) p->next;
1158 inet_add_protocol(p);
1159 printk("%s%s",p->name,tmp?", ":"\n");
1163 /* Register the socket-side information for inet_create. */
1164 for(r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
1167 for(q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
1168 inet_register_protosw(q);
1171 * Set the ARP module up
1177 * Set the IP module up
1182 tcp_v4_init(&inet_family_ops);
1184 /* Setup TCP slab cache for open requests. */
1189 * Set the ICMP layer up
1192 icmp_init(&inet_family_ops);
1194 /* I wish inet_add_protocol had no constructor hook...
1195 I had to move IPIP from net/ipv4/protocol.c :-( --ANK
1197 #ifdef CONFIG_NET_IPIP
1200 #ifdef CONFIG_NET_IPGRE
1205 * Initialise the multicast router
1207 #if defined(CONFIG_IP_MROUTE)
1212 * Create all the /proc entries.
1214 #ifdef CONFIG_PROC_FS
1215 proc_net_create ("raw", 0, raw_get_info);
1216 proc_net_create ("netstat", 0, netstat_get_info);
1217 proc_net_create ("snmp", 0, snmp_get_info);
1218 proc_net_create ("sockstat", 0, afinet_get_info);
1219 proc_net_create ("tcp", 0, tcp_get_info);
1220 proc_net_create ("udp", 0, udp_get_info);
1221 #endif /* CONFIG_PROC_FS */
1227 module_init(inet_init);