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.1.1.1 2005/04/11 02:51:13 jack 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 static 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 static 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 *uaddr_len = sizeof(*sin);
733 int inet_recvmsg(struct socket *sock, struct msghdr *msg, int size,
734 int flags, struct scm_cookie *scm)
736 struct sock *sk = sock->sk;
740 err = sk->prot->recvmsg(sk, msg, size, flags&MSG_DONTWAIT,
741 flags&~MSG_DONTWAIT, &addr_len);
743 msg->msg_namelen = addr_len;
748 int inet_sendmsg(struct socket *sock, struct msghdr *msg, int size,
749 struct scm_cookie *scm)
751 struct sock *sk = sock->sk;
753 /* We may need to bind the socket. */
754 if (sk->num==0 && inet_autobind(sk) != 0)
757 return sk->prot->sendmsg(sk, msg, size);
760 int inet_shutdown(struct socket *sock, int how)
762 struct sock *sk = sock->sk;
765 /* This should really check to make sure
766 * the socket is a TCP socket. (WHY AC...)
768 how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
771 if ((how & ~SHUTDOWN_MASK) || how==0) /* MAXINT->0 */
775 if (sock->state == SS_CONNECTING) {
776 if ((1<<sk->state)&(TCPF_SYN_SENT|TCPF_SYN_RECV|TCPF_CLOSE))
777 sock->state = SS_DISCONNECTING;
779 sock->state = SS_CONNECTED;
785 /* Hack to wake up other listeners, who can poll for
786 POLLHUP, even on eg. unconnected UDP sockets -- RR */
789 if (sk->prot->shutdown)
790 sk->prot->shutdown(sk, how);
793 /* Remaining two branches are temporary solution for missing
794 * close() in multithreaded environment. It is _not_ a good idea,
795 * but we have no choice until close() is repaired at VFS level.
798 if (!(how & RCV_SHUTDOWN))
802 err = sk->prot->disconnect(sk, O_NONBLOCK);
803 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
807 /* Wake up anyone sleeping in poll. */
808 sk->state_change(sk);
814 * ioctl() calls you can issue on an INET socket. Most of these are
815 * device configuration and stuff and very rarely used. Some ioctls
816 * pass on to the socket itself.
818 * NOTE: I like the idea of a module for the config stuff. ie ifconfig
819 * loads the devconfigure module does its configuring and unloads it.
820 * There's a good 20K of config code hanging around the kernel.
823 static int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
825 struct sock *sk = sock->sk;
832 err = get_user(pid, (int *) arg);
835 if (current->pid != pid && current->pgrp != -pid &&
836 !capable(CAP_NET_ADMIN))
842 return put_user(sk->proc, (int *)arg);
844 if(sk->stamp.tv_sec==0)
846 err = copy_to_user((void *)arg,&sk->stamp,sizeof(struct timeval));
853 return(ip_rt_ioctl(cmd,(void *) arg));
857 return(arp_ioctl(cmd,(void *) arg));
869 return(devinet_ioctl(cmd,(void *) arg));
872 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
874 if (br_ioctl_hook == NULL)
875 request_module("bridge");
877 if (br_ioctl_hook != NULL)
878 return br_ioctl_hook(arg);
884 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
886 if (vlan_ioctl_hook == NULL)
887 request_module("8021q");
889 if (vlan_ioctl_hook != NULL)
890 return vlan_ioctl_hook(arg);
896 #ifdef CONFIG_NET_DIVERT
897 return divert_ioctl(cmd, (struct divert_cf *) arg);
900 #endif /* CONFIG_NET_DIVERT */
906 err = dlci_ioctl(cmd, (void *) arg);
911 #ifdef CONFIG_DLCI_MODULE
914 if (dlci_ioctl_hook == NULL)
915 request_module("dlci");
918 if (dlci_ioctl_hook) {
920 err = (*dlci_ioctl_hook)(cmd, (void *) arg);
928 if ((cmd >= SIOCDEVPRIVATE) &&
929 (cmd <= (SIOCDEVPRIVATE + 15)))
930 return(dev_ioctl(cmd,(void *) arg));
933 if((cmd >= SIOCIWFIRST) && (cmd <= SIOCIWLAST))
934 return(dev_ioctl(cmd,(void *) arg));
935 #endif /* WIRELESS_EXT */
937 if (sk->prot->ioctl==NULL || (err=sk->prot->ioctl(sk, cmd, arg))==-ENOIOCTLCMD)
938 return(dev_ioctl(cmd,(void *) arg));
945 struct proto_ops inet_stream_ops = {
948 release: inet_release,
950 connect: inet_stream_connect,
951 socketpair: sock_no_socketpair,
953 getname: inet_getname,
957 shutdown: inet_shutdown,
958 setsockopt: inet_setsockopt,
959 getsockopt: inet_getsockopt,
960 sendmsg: inet_sendmsg,
961 recvmsg: inet_recvmsg,
963 sendpage: tcp_sendpage
966 struct proto_ops inet_dgram_ops = {
969 release: inet_release,
971 connect: inet_dgram_connect,
972 socketpair: sock_no_socketpair,
973 accept: sock_no_accept,
974 getname: inet_getname,
977 listen: sock_no_listen,
978 shutdown: inet_shutdown,
979 setsockopt: inet_setsockopt,
980 getsockopt: inet_getsockopt,
981 sendmsg: inet_sendmsg,
982 recvmsg: inet_recvmsg,
984 sendpage: sock_no_sendpage,
987 struct net_proto_family inet_family_ops = {
993 extern void tcp_init(void);
994 extern void tcp_v4_init(struct net_proto_family *);
996 /* Upon startup we insert all the elements in inetsw_array[] into
997 * the linked list inetsw.
999 static struct inet_protosw inetsw_array[] =
1003 protocol: IPPROTO_TCP,
1005 ops: &inet_stream_ops,
1008 flags: INET_PROTOSW_PERMANENT,
1013 protocol: IPPROTO_UDP,
1015 ops: &inet_dgram_ops,
1017 no_check: UDP_CSUM_DEFAULT,
1018 flags: INET_PROTOSW_PERMANENT,
1024 protocol: IPPROTO_IP, /* wild card */
1026 ops: &inet_dgram_ops,
1027 capability: CAP_NET_RAW,
1028 no_check: UDP_CSUM_DEFAULT,
1029 flags: INET_PROTOSW_REUSE,
1033 #define INETSW_ARRAY_LEN (sizeof(inetsw_array) / sizeof(struct inet_protosw))
1036 inet_register_protosw(struct inet_protosw *p)
1038 struct list_head *lh;
1039 struct inet_protosw *answer;
1040 int protocol = p->protocol;
1042 br_write_lock_bh(BR_NETPROTO_LOCK);
1044 if (p->type > SOCK_MAX)
1047 /* If we are trying to override a permanent protocol, bail. */
1049 list_for_each(lh, &inetsw[p->type]) {
1050 answer = list_entry(lh, struct inet_protosw, list);
1052 /* Check only the non-wild match. */
1053 if (protocol == answer->protocol &&
1054 (INET_PROTOSW_PERMANENT & answer->flags))
1062 /* Add to the BEGINNING so that we override any existing
1063 * entry. This means that when we remove this entry, the
1064 * system automatically returns to the old behavior.
1066 list_add(&p->list, &inetsw[p->type]);
1068 br_write_unlock_bh(BR_NETPROTO_LOCK);
1072 printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
1078 "Ignoring attempt to register illegal socket type %d.\n",
1084 inet_unregister_protosw(struct inet_protosw *p)
1086 if (INET_PROTOSW_PERMANENT & p->flags) {
1088 "Attempt to unregister permanent protocol %d.\n",
1091 br_write_lock_bh(BR_NETPROTO_LOCK);
1093 br_write_unlock_bh(BR_NETPROTO_LOCK);
1099 * Called by socket.c on kernel startup.
1102 static int __init inet_init(void)
1104 struct sk_buff *dummy_skb;
1105 struct inet_protocol *p;
1106 struct inet_protosw *q;
1107 struct list_head *r;
1109 printk(KERN_INFO "NET4: Linux TCP/IP 1.0 for NET4.0\n");
1111 if (sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb)) {
1112 printk(KERN_CRIT "inet_proto_init: panic\n");
1117 * Tell SOCKET that we are alive...
1120 (void) sock_register(&inet_family_ops);
1123 * Add all the protocols.
1126 printk(KERN_INFO "IP Protocols: ");
1127 for (p = inet_protocol_base; p != NULL;) {
1128 struct inet_protocol *tmp = (struct inet_protocol *) p->next;
1129 inet_add_protocol(p);
1130 printk("%s%s",p->name,tmp?", ":"\n");
1134 /* Register the socket-side information for inet_create. */
1135 for(r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
1138 for(q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
1139 inet_register_protosw(q);
1142 * Set the ARP module up
1148 * Set the IP module up
1153 tcp_v4_init(&inet_family_ops);
1155 /* Setup TCP slab cache for open requests. */
1160 * Set the ICMP layer up
1163 icmp_init(&inet_family_ops);
1165 /* I wish inet_add_protocol had no constructor hook...
1166 I had to move IPIP from net/ipv4/protocol.c :-( --ANK
1168 #ifdef CONFIG_NET_IPIP
1171 #ifdef CONFIG_NET_IPGRE
1176 * Initialise the multicast router
1178 #if defined(CONFIG_IP_MROUTE)
1183 * Create all the /proc entries.
1185 #ifdef CONFIG_PROC_FS
1186 proc_net_create ("raw", 0, raw_get_info);
1187 proc_net_create ("netstat", 0, netstat_get_info);
1188 proc_net_create ("snmp", 0, snmp_get_info);
1189 proc_net_create ("sockstat", 0, afinet_get_info);
1190 proc_net_create ("tcp", 0, tcp_get_info);
1191 proc_net_create ("udp", 0, udp_get_info);
1192 #endif /* CONFIG_PROC_FS */
1195 module_init(inet_init);