Linux 2.2.0
[davej-history.git] / net / ipv4 / af_inet.c
blob3520b0c52d7409ae502d068614e11a4994355180
1 /*
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.82 1999/01/04 20:36:44 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)
17 * A.N.Kuznetsov : Socket death error in accept().
18 * John Richardson : Fix non blocking error in connect()
19 * so sockets that fail to connect
20 * don't return -EINPROGRESS.
21 * Alan Cox : Asynchronous I/O support
22 * Alan Cox : Keep correct socket pointer on sock structures
23 * when accept() ed
24 * Alan Cox : Semantics of SO_LINGER aren't state moved
25 * to close when you look carefully. With
26 * this fixed and the accept bug fixed
27 * some RPC stuff seems happier.
28 * Niibe Yutaka : 4.4BSD style write async I/O
29 * Alan Cox,
30 * Tony Gale : Fixed reuse semantics.
31 * Alan Cox : bind() shouldn't abort existing but dead
32 * sockets. Stops FTP netin:.. I hope.
33 * Alan Cox : bind() works correctly for RAW sockets. Note
34 * that FreeBSD at least was broken in this respect
35 * so be careful with compatibility tests...
36 * Alan Cox : routing cache support
37 * Alan Cox : memzero the socket structure for compactness.
38 * Matt Day : nonblock connect error handler
39 * Alan Cox : Allow large numbers of pending sockets
40 * (eg for big web sites), but only if
41 * specifically application requested.
42 * Alan Cox : New buffering throughout IP. Used dumbly.
43 * Alan Cox : New buffering now used smartly.
44 * Alan Cox : BSD rather than common sense interpretation of
45 * listen.
46 * Germano Caronni : Assorted small races.
47 * Alan Cox : sendmsg/recvmsg basic support.
48 * Alan Cox : Only sendmsg/recvmsg now supported.
49 * Alan Cox : Locked down bind (see security list).
50 * Alan Cox : Loosened bind a little.
51 * Mike McLagan : ADD/DEL DLCI Ioctls
52 * Willy Konynenberg : Transparent proxying support.
53 * David S. Miller : New socket lookup architecture.
54 * Some other random speedups.
55 * Cyrus Durgin : Cleaned up file for kmod hacks.
57 * This program is free software; you can redistribute it and/or
58 * modify it under the terms of the GNU General Public License
59 * as published by the Free Software Foundation; either version
60 * 2 of the License, or (at your option) any later version.
63 #include <linux/config.h>
64 #include <linux/errno.h>
65 #include <linux/types.h>
66 #include <linux/socket.h>
67 #include <linux/in.h>
68 #include <linux/kernel.h>
69 #include <linux/major.h>
70 #include <linux/sched.h>
71 #include <linux/timer.h>
72 #include <linux/string.h>
73 #include <linux/sockios.h>
74 #include <linux/net.h>
75 #include <linux/fcntl.h>
76 #include <linux/mm.h>
77 #include <linux/interrupt.h>
78 #include <linux/proc_fs.h>
79 #include <linux/stat.h>
80 #include <linux/init.h>
81 #include <linux/poll.h>
83 #include <asm/uaccess.h>
84 #include <asm/system.h>
86 #include <linux/inet.h>
87 #include <linux/netdevice.h>
88 #include <net/ip.h>
89 #include <net/protocol.h>
90 #include <net/arp.h>
91 #include <net/rarp.h>
92 #include <net/route.h>
93 #include <net/tcp.h>
94 #include <net/udp.h>
95 #include <linux/skbuff.h>
96 #include <net/sock.h>
97 #include <net/raw.h>
98 #include <net/icmp.h>
99 #include <net/ipip.h>
100 #include <net/inet_common.h>
101 #include <linux/ip_fw.h>
102 #ifdef CONFIG_IP_MROUTE
103 #include <linux/mroute.h>
104 #endif
105 #ifdef CONFIG_IP_MASQUERADE
106 #include <net/ip_masq.h>
107 #endif
108 #ifdef CONFIG_BRIDGE
109 #include <net/br.h>
110 #endif
111 #ifdef CONFIG_KMOD
112 #include <linux/kmod.h>
113 #endif
114 #ifdef CONFIG_NET_RADIO
115 #include <linux/wireless.h>
116 #endif /* CONFIG_NET_RADIO */
118 #define min(a,b) ((a)<(b)?(a):(b))
120 struct linux_mib net_statistics;
122 extern int raw_get_info(char *, char **, off_t, int, int);
123 extern int snmp_get_info(char *, char **, off_t, int, int);
124 extern int netstat_get_info(char *, char **, off_t, int, int);
125 extern int afinet_get_info(char *, char **, off_t, int, int);
126 extern int tcp_get_info(char *, char **, off_t, int, int);
127 extern int udp_get_info(char *, char **, off_t, int, int);
128 extern void ip_mc_drop_socket(struct sock *sk);
130 #ifdef CONFIG_DLCI
131 extern int dlci_ioctl(unsigned int, void*);
132 #endif
134 #ifdef CONFIG_DLCI_MODULE
135 int (*dlci_ioctl_hook)(unsigned int, void *) = NULL;
136 #endif
138 int (*rarp_ioctl_hook)(unsigned int,void*) = NULL;
141 * Destroy an AF_INET socket
144 static __inline__ void kill_sk_queues(struct sock *sk)
146 struct sk_buff *skb;
148 /* First the read buffer. */
149 while((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
150 /* This will take care of closing sockets that were
151 * listening and didn't accept everything.
153 if (skb->sk != NULL && skb->sk != sk)
154 skb->sk->prot->close(skb->sk, 0);
155 kfree_skb(skb);
158 /* Next, the error queue. */
159 while((skb = skb_dequeue(&sk->error_queue)) != NULL)
160 kfree_skb(skb);
162 /* Now the backlog. */
163 while((skb=skb_dequeue(&sk->back_log)) != NULL)
164 kfree_skb(skb);
167 static __inline__ void kill_sk_now(struct sock *sk)
169 /* No longer exists. */
170 del_from_prot_sklist(sk);
172 /* Remove from protocol hash chains. */
173 sk->prot->unhash(sk);
175 if(sk->opt)
176 kfree(sk->opt);
177 dst_release(sk->dst_cache);
178 if (atomic_read(&sk->omem_alloc))
179 printk(KERN_DEBUG "kill_sk_now: optmem leakage (%d bytes) detected.\n", atomic_read(&sk->omem_alloc));
180 sk_free(sk);
183 static __inline__ void kill_sk_later(struct sock *sk)
185 /* this should never happen. */
186 /* actually it can if an ack has just been sent. */
188 * It's more normal than that...
189 * It can happen because a skb is still in the device queues
190 * [PR]
193 NETDEBUG(printk(KERN_DEBUG "Socket destroy delayed (r=%d w=%d)\n",
194 atomic_read(&sk->rmem_alloc),
195 atomic_read(&sk->wmem_alloc)));
197 sk->destroy = 1;
198 sk->ack_backlog = 0;
199 release_sock(sk);
200 net_reset_timer(sk, TIME_DESTROY, SOCK_DESTROY_TIME);
203 void destroy_sock(struct sock *sk)
205 lock_sock(sk); /* just to be safe. */
207 /* Now we can no longer get new packets or once the
208 * timers are killed, send them.
210 net_delete_timer(sk);
212 if (sk->prot->destroy)
213 sk->prot->destroy(sk);
215 kill_sk_queues(sk);
217 /* Now if it has a half accepted/ closed socket. */
218 if (sk->pair) {
219 sk->pair->prot->close(sk->pair, 0);
220 sk->pair = NULL;
223 /* Now if everything is gone we can free the socket
224 * structure, otherwise we need to keep it around until
225 * everything is gone.
227 if (atomic_read(&sk->rmem_alloc) == 0 && atomic_read(&sk->wmem_alloc) == 0)
228 kill_sk_now(sk);
229 else
230 kill_sk_later(sk);
234 * The routines beyond this point handle the behaviour of an AF_INET
235 * socket object. Mostly it punts to the subprotocols of IP to do
236 * the work.
241 * Set socket options on an inet socket.
244 int inet_setsockopt(struct socket *sock, int level, int optname,
245 char *optval, int optlen)
247 struct sock *sk=sock->sk;
248 if (sk->prot->setsockopt==NULL)
249 return(-EOPNOTSUPP);
250 return sk->prot->setsockopt(sk,level,optname,optval,optlen);
254 * Get a socket option on an AF_INET socket.
256 * FIX: POSIX 1003.1g is very ambiguous here. It states that
257 * asynchronous errors should be reported by getsockopt. We assume
258 * this means if you specify SO_ERROR (otherwise whats the point of it).
261 int inet_getsockopt(struct socket *sock, int level, int optname,
262 char *optval, int *optlen)
264 struct sock *sk=sock->sk;
265 if (sk->prot->getsockopt==NULL)
266 return(-EOPNOTSUPP);
267 return sk->prot->getsockopt(sk,level,optname,optval,optlen);
271 * Automatically bind an unbound socket.
274 static int inet_autobind(struct sock *sk)
276 /* We may need to bind the socket. */
277 if (sk->num == 0) {
278 sk->num = sk->prot->good_socknum();
279 if (sk->num == 0)
280 return(-EAGAIN);
281 sk->sport = htons(sk->num);
282 sk->prot->hash(sk);
283 add_to_prot_sklist(sk);
285 return 0;
289 * Move a socket into listening state.
292 int inet_listen(struct socket *sock, int backlog)
294 struct sock *sk = sock->sk;
296 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
297 return(-EINVAL);
299 if (inet_autobind(sk) != 0)
300 return -EAGAIN;
302 /* We might as well re use these. */
303 if ((unsigned) backlog == 0) /* BSDism */
304 backlog = 1;
305 if ((unsigned) backlog > SOMAXCONN)
306 backlog = SOMAXCONN;
307 sk->max_ack_backlog = backlog;
308 if (sk->state != TCP_LISTEN) {
309 sk->ack_backlog = 0;
310 sk->state = TCP_LISTEN;
311 dst_release(xchg(&sk->dst_cache, NULL));
312 sk->prot->rehash(sk);
313 add_to_prot_sklist(sk);
315 sk->socket->flags |= SO_ACCEPTCON;
316 return(0);
320 * Create an inet socket.
322 * FIXME: Gcc would generate much better code if we set the parameters
323 * up in in-memory structure order. Gcc68K even more so
326 static int inet_create(struct socket *sock, int protocol)
328 struct sock *sk;
329 struct proto *prot;
331 /* Compatibility */
332 if (sock->type == SOCK_PACKET) {
333 static int warned;
334 if (net_families[PF_PACKET]==NULL)
336 #if defined(CONFIG_KMOD) && defined(CONFIG_PACKET_MODULE)
337 char module_name[30];
338 sprintf(module_name,"net-pf-%d", PF_PACKET);
339 request_module(module_name);
340 if (net_families[PF_PACKET] == NULL)
341 #endif
342 return -ESOCKTNOSUPPORT;
344 if (!warned++)
345 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n", current->comm);
346 return net_families[PF_PACKET]->create(sock, protocol);
349 sock->state = SS_UNCONNECTED;
350 sk = sk_alloc(PF_INET, GFP_KERNEL, 1);
351 if (sk == NULL)
352 goto do_oom;
354 switch (sock->type) {
355 case SOCK_STREAM:
356 if (protocol && protocol != IPPROTO_TCP)
357 goto free_and_noproto;
358 protocol = IPPROTO_TCP;
359 if (ipv4_config.no_pmtu_disc)
360 sk->ip_pmtudisc = IP_PMTUDISC_DONT;
361 else
362 sk->ip_pmtudisc = IP_PMTUDISC_WANT;
363 prot = &tcp_prot;
364 sock->ops = &inet_stream_ops;
365 break;
366 case SOCK_SEQPACKET:
367 goto free_and_badtype;
368 case SOCK_DGRAM:
369 if (protocol && protocol != IPPROTO_UDP)
370 goto free_and_noproto;
371 protocol = IPPROTO_UDP;
372 sk->no_check = UDP_NO_CHECK;
373 sk->ip_pmtudisc = IP_PMTUDISC_DONT;
374 prot=&udp_prot;
375 sock->ops = &inet_dgram_ops;
376 break;
377 case SOCK_RAW:
378 if (!capable(CAP_NET_RAW))
379 goto free_and_badperm;
380 if (!protocol)
381 goto free_and_noproto;
382 prot = &raw_prot;
383 sk->reuse = 1;
384 sk->ip_pmtudisc = IP_PMTUDISC_DONT;
385 sk->num = protocol;
386 sock->ops = &inet_dgram_ops;
387 if (protocol == IPPROTO_RAW)
388 sk->ip_hdrincl = 1;
389 break;
390 default:
391 goto free_and_badtype;
394 sock_init_data(sock,sk);
396 sk->destruct = NULL;
398 sk->zapped=0;
399 #ifdef CONFIG_TCP_NAGLE_OFF
400 sk->nonagle = 1;
401 #endif
402 sk->family = PF_INET;
403 sk->protocol = protocol;
405 sk->prot = prot;
406 sk->backlog_rcv = prot->backlog_rcv;
408 sk->timer.data = (unsigned long)sk;
409 sk->timer.function = &net_timer;
411 sk->ip_ttl=ip_statistics.IpDefaultTTL;
413 sk->ip_mc_loop=1;
414 sk->ip_mc_ttl=1;
415 sk->ip_mc_index=0;
416 sk->ip_mc_list=NULL;
418 if (sk->num) {
419 /* It assumes that any protocol which allows
420 * the user to assign a number at socket
421 * creation time automatically
422 * shares.
424 sk->sport = htons(sk->num);
426 /* Add to protocol hash chains. */
427 sk->prot->hash(sk);
428 add_to_prot_sklist(sk);
431 if (sk->prot->init) {
432 int err = sk->prot->init(sk);
433 if (err != 0) {
434 destroy_sock(sk);
435 return(err);
438 return(0);
440 free_and_badtype:
441 sk_free(sk);
442 return -ESOCKTNOSUPPORT;
444 free_and_badperm:
445 sk_free(sk);
446 return -EPERM;
448 free_and_noproto:
449 sk_free(sk);
450 return -EPROTONOSUPPORT;
452 do_oom:
453 return -ENOBUFS;
458 * The peer socket should always be NULL (or else). When we call this
459 * function we are destroying the object and from then on nobody
460 * should refer to it.
463 int inet_release(struct socket *sock, struct socket *peersock)
465 struct sock *sk = sock->sk;
467 if (sk) {
468 long timeout;
470 /* Begin closedown and wake up sleepers. */
471 if (sock->state != SS_UNCONNECTED)
472 sock->state = SS_DISCONNECTING;
473 sk->state_change(sk);
475 /* Applications forget to leave groups before exiting */
476 ip_mc_drop_socket(sk);
478 /* If linger is set, we don't return until the close
479 * is complete. Otherwise we return immediately. The
480 * actually closing is done the same either way.
482 * If the close is due to the process exiting, we never
483 * linger..
485 timeout = 0;
486 if (sk->linger && !(current->flags & PF_EXITING)) {
487 timeout = MAX_SCHEDULE_TIMEOUT;
489 /* XXX This makes no sense whatsoever... -DaveM */
490 if (!sk->lingertime)
491 timeout = HZ*sk->lingertime;
493 sock->sk = NULL;
494 sk->socket = NULL;
495 sk->prot->close(sk, timeout);
497 return(0);
500 static int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
502 struct sockaddr_in *addr=(struct sockaddr_in *)uaddr;
503 struct sock *sk=sock->sk;
504 unsigned short snum;
505 int chk_addr_ret;
507 /* If the socket has its own bind function then use it. (RAW) */
508 if(sk->prot->bind)
509 return sk->prot->bind(sk, uaddr, addr_len);
511 /* Check these errors (active socket, bad address length, double bind). */
512 if ((sk->state != TCP_CLOSE) ||
513 (addr_len < sizeof(struct sockaddr_in)) ||
514 (sk->num != 0))
515 return -EINVAL;
517 snum = ntohs(addr->sin_port);
518 #ifdef CONFIG_IP_MASQUERADE
519 /* The kernel masquerader needs some ports. */
520 if((snum >= PORT_MASQ_BEGIN) && (snum <= PORT_MASQ_END))
521 return -EADDRINUSE;
522 #endif
523 if (snum == 0)
524 snum = sk->prot->good_socknum();
525 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
526 return(-EACCES);
528 chk_addr_ret = inet_addr_type(addr->sin_addr.s_addr);
529 if (addr->sin_addr.s_addr != 0 && chk_addr_ret != RTN_LOCAL &&
530 chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST) {
531 #ifdef CONFIG_IP_TRANSPARENT_PROXY
532 /* Superuser may bind to any address to allow transparent proxying. */
533 if(chk_addr_ret != RTN_UNICAST || !capable(CAP_NET_ADMIN))
534 #endif
535 return -EADDRNOTAVAIL; /* Source address MUST be ours! */
538 /* We keep a pair of addresses. rcv_saddr is the one
539 * used by hash lookups, and saddr is used for transmit.
541 * In the BSD API these are the same except where it
542 * would be illegal to use them (multicast/broadcast) in
543 * which case the sending device address is used.
545 sk->rcv_saddr = sk->saddr = addr->sin_addr.s_addr;
546 if(chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
547 sk->saddr = 0; /* Use device */
549 /* Make sure we are allowed to bind here. */
550 if(sk->prot->verify_bind(sk, snum))
551 return -EADDRINUSE;
553 sk->num = snum;
554 sk->sport = htons(snum);
555 sk->daddr = 0;
556 sk->dport = 0;
557 sk->prot->rehash(sk);
558 add_to_prot_sklist(sk);
559 dst_release(sk->dst_cache);
560 sk->dst_cache=NULL;
561 return(0);
564 int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
565 int addr_len, int flags)
567 struct sock *sk=sock->sk;
568 int err;
570 if (inet_autobind(sk) != 0)
571 return(-EAGAIN);
572 if (sk->prot->connect == NULL)
573 return(-EOPNOTSUPP);
574 err = sk->prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
575 if (err < 0)
576 return(err);
577 return(0);
580 static void inet_wait_for_connect(struct sock *sk)
582 struct wait_queue wait = { current, NULL };
584 add_wait_queue(sk->sleep, &wait);
585 current->state = TASK_INTERRUPTIBLE;
586 while (sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV) {
587 if (signal_pending(current))
588 break;
589 if (sk->err)
590 break;
591 schedule();
592 current->state = TASK_INTERRUPTIBLE;
594 current->state = TASK_RUNNING;
595 remove_wait_queue(sk->sleep, &wait);
599 * Connect to a remote host. There is regrettably still a little
600 * TCP 'magic' in here.
603 int inet_stream_connect(struct socket *sock, struct sockaddr * uaddr,
604 int addr_len, int flags)
606 struct sock *sk=sock->sk;
607 int err;
609 if(sock->state != SS_UNCONNECTED && sock->state != SS_CONNECTING) {
610 if(sock->state == SS_CONNECTED)
611 return -EISCONN;
612 return -EINVAL;
615 if(sock->state == SS_CONNECTING) {
616 if(tcp_connected(sk->state)) {
617 sock->state = SS_CONNECTED;
618 return 0;
620 if(sk->protocol == IPPROTO_TCP && (flags & O_NONBLOCK)) {
621 if(sk->err)
622 return sock_error(sk);
623 return -EALREADY;
625 } else {
626 /* We may need to bind the socket. */
627 if (inet_autobind(sk) != 0)
628 return(-EAGAIN);
629 if (sk->prot->connect == NULL)
630 return(-EOPNOTSUPP);
631 err = sk->prot->connect(sk, uaddr, addr_len);
632 if (err < 0)
633 return(err);
634 sock->state = SS_CONNECTING;
637 if (sk->state > TCP_FIN_WAIT2 && sock->state == SS_CONNECTING) {
638 sock->state = SS_UNCONNECTED;
639 return sock_error(sk);
642 if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
643 return (-EINPROGRESS);
645 if (sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV) {
646 inet_wait_for_connect(sk);
647 if (signal_pending(current))
648 return -ERESTARTSYS;
651 sock->state = SS_CONNECTED;
652 if ((sk->state != TCP_ESTABLISHED) && sk->err) {
653 /* This is ugly but needed to fix a race in the ICMP error handler */
654 if (sk->protocol == IPPROTO_TCP && sk->zapped) {
655 lock_sock(sk);
656 tcp_set_state(sk, TCP_CLOSE);
657 release_sock(sk);
659 sock->state = SS_UNCONNECTED;
660 return sock_error(sk);
662 return(0);
666 * Accept a pending connection. The TCP layer now gives BSD semantics.
669 int inet_accept(struct socket *sock, struct socket *newsock, int flags)
671 struct sock *sk1 = sock->sk, *sk2;
672 struct sock *newsk = newsock->sk;
673 int err = -EINVAL;
675 if (sock->state != SS_UNCONNECTED || !(sock->flags & SO_ACCEPTCON))
676 goto do_err;
678 err = -EOPNOTSUPP;
679 if (sk1->prot->accept == NULL)
680 goto do_err;
682 /* Restore the state if we have been interrupted, and then returned. */
683 if (sk1->pair != NULL) {
684 sk2 = sk1->pair;
685 sk1->pair = NULL;
686 } else {
687 if((sk2 = sk1->prot->accept(sk1,flags)) == NULL)
688 goto do_sk1_err;
692 * We've been passed an extra socket.
693 * We need to free it up because the tcp module creates
694 * its own when it accepts one.
696 sk2->sleep = newsk->sleep;
698 newsock->sk = sk2;
699 sk2->socket = newsock;
700 newsk->socket = NULL;
702 if (flags & O_NONBLOCK)
703 goto do_half_success;
705 if(sk2->state == TCP_ESTABLISHED)
706 goto do_full_success;
707 if(sk2->err > 0)
708 goto do_connect_err;
709 err = -ECONNABORTED;
710 if (sk2->state == TCP_CLOSE)
711 goto do_bad_connection;
712 do_full_success:
713 destroy_sock(newsk);
714 newsock->state = SS_CONNECTED;
715 return 0;
717 do_half_success:
718 destroy_sock(newsk);
719 return(0);
721 do_connect_err:
722 err = sock_error(sk2);
723 do_bad_connection:
724 sk2->sleep = NULL;
725 sk2->socket = NULL;
726 destroy_sock(sk2);
727 newsock->sk = newsk;
728 newsk->socket = newsock;
729 return err;
731 do_sk1_err:
732 err = sock_error(sk1);
733 do_err:
734 return err;
739 * This does both peername and sockname.
742 static int inet_getname(struct socket *sock, struct sockaddr *uaddr,
743 int *uaddr_len, int peer)
745 struct sock *sk = sock->sk;
746 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
748 sin->sin_family = AF_INET;
749 if (peer) {
750 if (!tcp_connected(sk->state))
751 return(-ENOTCONN);
752 sin->sin_port = sk->dport;
753 sin->sin_addr.s_addr = sk->daddr;
754 } else {
755 __u32 addr = sk->rcv_saddr;
756 if (!addr)
757 addr = sk->saddr;
758 sin->sin_port = sk->sport;
759 sin->sin_addr.s_addr = addr;
761 *uaddr_len = sizeof(*sin);
762 return(0);
767 int inet_recvmsg(struct socket *sock, struct msghdr *msg, int size,
768 int flags, struct scm_cookie *scm)
770 struct sock *sk = sock->sk;
771 int addr_len = 0;
772 int err;
774 if (sock->flags & SO_ACCEPTCON)
775 return(-EINVAL);
776 if (sk->prot->recvmsg == NULL)
777 return(-EOPNOTSUPP);
778 /* We may need to bind the socket. */
779 if (inet_autobind(sk) != 0)
780 return(-EAGAIN);
781 err = sk->prot->recvmsg(sk, msg, size, flags&MSG_DONTWAIT,
782 flags&~MSG_DONTWAIT, &addr_len);
783 if (err >= 0)
784 msg->msg_namelen = addr_len;
785 return err;
789 int inet_sendmsg(struct socket *sock, struct msghdr *msg, int size,
790 struct scm_cookie *scm)
792 struct sock *sk = sock->sk;
794 if (sk->shutdown & SEND_SHUTDOWN) {
795 if (!(msg->msg_flags&MSG_NOSIGNAL))
796 send_sig(SIGPIPE, current, 1);
797 return(-EPIPE);
799 if (sk->prot->sendmsg == NULL)
800 return(-EOPNOTSUPP);
801 if(sk->err)
802 return sock_error(sk);
804 /* We may need to bind the socket. */
805 if(inet_autobind(sk) != 0)
806 return -EAGAIN;
808 return sk->prot->sendmsg(sk, msg, size);
812 int inet_shutdown(struct socket *sock, int how)
814 struct sock *sk = sock->sk;
816 /* This should really check to make sure
817 * the socket is a TCP socket. (WHY AC...)
819 how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
820 1->2 bit 2 snds.
821 2->3 */
822 if ((how & ~SHUTDOWN_MASK) || how==0) /* MAXINT->0 */
823 return(-EINVAL);
824 if (sock->state == SS_CONNECTING && sk->state == TCP_ESTABLISHED)
825 sock->state = SS_CONNECTED;
826 if (!sk || !tcp_connected(sk->state))
827 return(-ENOTCONN);
828 sk->shutdown |= how;
829 if (sk->prot->shutdown)
830 sk->prot->shutdown(sk, how);
831 return(0);
835 unsigned int inet_poll(struct file * file, struct socket *sock, poll_table *wait)
837 struct sock *sk = sock->sk;
839 if (sk->prot->poll == NULL)
840 return(0);
841 return sk->prot->poll(file, sock, wait);
845 * ioctl() calls you can issue on an INET socket. Most of these are
846 * device configuration and stuff and very rarely used. Some ioctls
847 * pass on to the socket itself.
849 * NOTE: I like the idea of a module for the config stuff. ie ifconfig
850 * loads the devconfigure module does its configuring and unloads it.
851 * There's a good 20K of config code hanging around the kernel.
854 static int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
856 struct sock *sk = sock->sk;
857 int err;
858 int pid;
860 switch(cmd)
862 case FIOSETOWN:
863 case SIOCSPGRP:
864 err = get_user(pid, (int *) arg);
865 if (err)
866 return err;
867 if (current->pid != pid && current->pgrp != -pid &&
868 !capable(CAP_NET_ADMIN))
869 return -EPERM;
870 sk->proc = pid;
871 return(0);
872 case FIOGETOWN:
873 case SIOCGPGRP:
874 return put_user(sk->proc, (int *)arg);
875 case SIOCGSTAMP:
876 if(sk->stamp.tv_sec==0)
877 return -ENOENT;
878 err = copy_to_user((void *)arg,&sk->stamp,sizeof(struct timeval));
879 if (err)
880 err = -EFAULT;
881 return err;
882 case SIOCADDRT:
883 case SIOCDELRT:
884 case SIOCRTMSG:
885 return(ip_rt_ioctl(cmd,(void *) arg));
886 case SIOCDARP:
887 case SIOCGARP:
888 case SIOCSARP:
889 return(arp_ioctl(cmd,(void *) arg));
890 case SIOCDRARP:
891 case SIOCGRARP:
892 case SIOCSRARP:
893 #ifdef CONFIG_KMOD
894 if (rarp_ioctl_hook == NULL)
895 request_module("rarp");
896 #endif
897 if (rarp_ioctl_hook != NULL)
898 return(rarp_ioctl_hook(cmd,(void *) arg));
899 case SIOCGIFADDR:
900 case SIOCSIFADDR:
901 case SIOCGIFBRDADDR:
902 case SIOCSIFBRDADDR:
903 case SIOCGIFNETMASK:
904 case SIOCSIFNETMASK:
905 case SIOCGIFDSTADDR:
906 case SIOCSIFDSTADDR:
907 case SIOCSIFPFLAGS:
908 case SIOCGIFPFLAGS:
909 case SIOCSIFFLAGS:
910 return(devinet_ioctl(cmd,(void *) arg));
911 case SIOCGIFBR:
912 case SIOCSIFBR:
913 #ifdef CONFIG_BRIDGE
914 return(br_ioctl(cmd,(void *) arg));
915 #else
916 return -ENOPKG;
917 #endif
919 case SIOCADDDLCI:
920 case SIOCDELDLCI:
921 #ifdef CONFIG_DLCI
922 return(dlci_ioctl(cmd, (void *) arg));
923 #endif
925 #ifdef CONFIG_DLCI_MODULE
927 #ifdef CONFIG_KMOD
928 if (dlci_ioctl_hook == NULL)
929 request_module("dlci");
930 #endif
932 if (dlci_ioctl_hook)
933 return((*dlci_ioctl_hook)(cmd, (void *) arg));
934 #endif
935 return -ENOPKG;
937 default:
938 if ((cmd >= SIOCDEVPRIVATE) &&
939 (cmd <= (SIOCDEVPRIVATE + 15)))
940 return(dev_ioctl(cmd,(void *) arg));
942 #ifdef CONFIG_NET_RADIO
943 if((cmd >= SIOCIWFIRST) && (cmd <= SIOCIWLAST))
944 return(dev_ioctl(cmd,(void *) arg));
945 #endif
947 if (sk->prot->ioctl==NULL || (err=sk->prot->ioctl(sk, cmd, arg))==-ENOIOCTLCMD)
948 return(dev_ioctl(cmd,(void *) arg));
949 return err;
951 /*NOTREACHED*/
952 return(0);
955 struct proto_ops inet_stream_ops = {
956 PF_INET,
958 sock_no_dup,
959 inet_release,
960 inet_bind,
961 inet_stream_connect,
962 sock_no_socketpair,
963 inet_accept,
964 inet_getname,
965 inet_poll,
966 inet_ioctl,
967 inet_listen,
968 inet_shutdown,
969 inet_setsockopt,
970 inet_getsockopt,
971 sock_no_fcntl,
972 inet_sendmsg,
973 inet_recvmsg
976 struct proto_ops inet_dgram_ops = {
977 PF_INET,
979 sock_no_dup,
980 inet_release,
981 inet_bind,
982 inet_dgram_connect,
983 sock_no_socketpair,
984 sock_no_accept,
985 inet_getname,
986 datagram_poll,
987 inet_ioctl,
988 sock_no_listen,
989 inet_shutdown,
990 inet_setsockopt,
991 inet_getsockopt,
992 sock_no_fcntl,
993 inet_sendmsg,
994 inet_recvmsg
997 struct net_proto_family inet_family_ops = {
998 PF_INET,
999 inet_create
1003 #ifdef CONFIG_PROC_FS
1004 #ifdef CONFIG_INET_RARP
1005 static struct proc_dir_entry proc_net_rarp = {
1006 PROC_NET_RARP, 4, "rarp",
1007 S_IFREG | S_IRUGO, 1, 0, 0,
1008 0, &proc_net_inode_operations,
1009 rarp_get_info
1011 #endif /* RARP */
1012 static struct proc_dir_entry proc_net_raw = {
1013 PROC_NET_RAW, 3, "raw",
1014 S_IFREG | S_IRUGO, 1, 0, 0,
1015 0, &proc_net_inode_operations,
1016 raw_get_info
1018 static struct proc_dir_entry proc_net_netstat = {
1019 PROC_NET_NETSTAT, 7, "netstat",
1020 S_IFREG | S_IRUGO, 1, 0, 0,
1021 0, &proc_net_inode_operations,
1022 netstat_get_info
1024 static struct proc_dir_entry proc_net_snmp = {
1025 PROC_NET_SNMP, 4, "snmp",
1026 S_IFREG | S_IRUGO, 1, 0, 0,
1027 0, &proc_net_inode_operations,
1028 snmp_get_info
1030 static struct proc_dir_entry proc_net_sockstat = {
1031 PROC_NET_SOCKSTAT, 8, "sockstat",
1032 S_IFREG | S_IRUGO, 1, 0, 0,
1033 0, &proc_net_inode_operations,
1034 afinet_get_info
1036 static struct proc_dir_entry proc_net_tcp = {
1037 PROC_NET_TCP, 3, "tcp",
1038 S_IFREG | S_IRUGO, 1, 0, 0,
1039 0, &proc_net_inode_operations,
1040 tcp_get_info
1042 static struct proc_dir_entry proc_net_udp = {
1043 PROC_NET_UDP, 3, "udp",
1044 S_IFREG | S_IRUGO, 1, 0, 0,
1045 0, &proc_net_inode_operations,
1046 udp_get_info
1048 #endif /* CONFIG_PROC_FS */
1050 extern void tcp_init(void);
1051 extern void tcp_v4_init(struct net_proto_family *);
1055 * Called by socket.c on kernel startup.
1058 __initfunc(void inet_proto_init(struct net_proto *pro))
1060 struct sk_buff *dummy_skb;
1061 struct inet_protocol *p;
1063 printk(KERN_INFO "NET4: Linux TCP/IP 1.0 for NET4.0\n");
1065 if (sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb))
1067 printk(KERN_CRIT "inet_proto_init: panic\n");
1068 return;
1072 * Tell SOCKET that we are alive...
1075 (void) sock_register(&inet_family_ops);
1078 * Add all the protocols.
1081 printk(KERN_INFO "IP Protocols: ");
1082 for(p = inet_protocol_base; p != NULL;)
1084 struct inet_protocol *tmp = (struct inet_protocol *) p->next;
1085 inet_add_protocol(p);
1086 printk("%s%s",p->name,tmp?", ":"\n");
1087 p = tmp;
1091 * Set the ARP module up
1094 arp_init();
1097 * Set the IP module up
1100 ip_init();
1102 tcp_v4_init(&inet_family_ops);
1104 /* Setup TCP slab cache for open requests. */
1105 tcp_init();
1109 * Set the ICMP layer up
1112 icmp_init(&inet_family_ops);
1114 /* I wish inet_add_protocol had no constructor hook...
1115 I had to move IPIP from net/ipv4/protocol.c :-( --ANK
1117 #ifdef CONFIG_NET_IPIP
1118 ipip_init();
1119 #endif
1120 #ifdef CONFIG_NET_IPGRE
1121 ipgre_init();
1122 #endif
1125 * Set the firewalling up
1127 #if defined(CONFIG_IP_FIREWALL)
1128 ip_fw_init();
1129 #endif
1131 #ifdef CONFIG_IP_MASQUERADE
1132 ip_masq_init();
1133 #endif
1136 * Initialise the multicast router
1138 #if defined(CONFIG_IP_MROUTE)
1139 ip_mr_init();
1140 #endif
1142 #ifdef CONFIG_INET_RARP
1143 rarp_ioctl_hook = rarp_ioctl;
1144 #endif
1146 * Create all the /proc entries.
1149 #ifdef CONFIG_PROC_FS
1150 #ifdef CONFIG_INET_RARP
1151 proc_net_register(&proc_net_rarp);
1152 #endif /* RARP */
1153 proc_net_register(&proc_net_raw);
1154 proc_net_register(&proc_net_snmp);
1155 proc_net_register(&proc_net_netstat);
1156 proc_net_register(&proc_net_sockstat);
1157 proc_net_register(&proc_net_tcp);
1158 proc_net_register(&proc_net_udp);
1159 #endif /* CONFIG_PROC_FS */