pre-2.3.4..
[davej-history.git] / net / ipv4 / af_inet.c
bloba34ed8a9e411d1fa00b106befb206305bd34c36a
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.88 1999/05/12 11:24:27 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.
56 * Andi Kleen : Fix inet_stream_connect TCP race.
58 * This program is free software; you can redistribute it and/or
59 * modify it under the terms of the GNU General Public License
60 * as published by the Free Software Foundation; either version
61 * 2 of the License, or (at your option) any later version.
64 #include <linux/config.h>
65 #include <linux/errno.h>
66 #include <linux/types.h>
67 #include <linux/socket.h>
68 #include <linux/in.h>
69 #include <linux/kernel.h>
70 #include <linux/major.h>
71 #include <linux/sched.h>
72 #include <linux/timer.h>
73 #include <linux/string.h>
74 #include <linux/sockios.h>
75 #include <linux/net.h>
76 #include <linux/fcntl.h>
77 #include <linux/mm.h>
78 #include <linux/interrupt.h>
79 #include <linux/proc_fs.h>
80 #include <linux/stat.h>
81 #include <linux/init.h>
82 #include <linux/poll.h>
84 #include <asm/uaccess.h>
85 #include <asm/system.h>
87 #include <linux/inet.h>
88 #include <linux/netdevice.h>
89 #include <net/ip.h>
90 #include <net/protocol.h>
91 #include <net/arp.h>
92 #include <net/rarp.h>
93 #include <net/route.h>
94 #include <net/tcp.h>
95 #include <net/udp.h>
96 #include <linux/skbuff.h>
97 #include <net/sock.h>
98 #include <net/raw.h>
99 #include <net/icmp.h>
100 #include <net/ipip.h>
101 #include <net/inet_common.h>
102 #include <linux/ip_fw.h>
103 #ifdef CONFIG_IP_MROUTE
104 #include <linux/mroute.h>
105 #endif
106 #ifdef CONFIG_IP_MASQUERADE
107 #include <net/ip_masq.h>
108 #endif
109 #ifdef CONFIG_BRIDGE
110 #include <net/br.h>
111 #endif
112 #ifdef CONFIG_KMOD
113 #include <linux/kmod.h>
114 #endif
115 #ifdef CONFIG_NET_RADIO
116 #include <linux/wireless.h>
117 #endif /* CONFIG_NET_RADIO */
119 #define min(a,b) ((a)<(b)?(a):(b))
121 struct linux_mib net_statistics;
123 extern int raw_get_info(char *, char **, off_t, int, int);
124 extern int snmp_get_info(char *, char **, off_t, int, int);
125 extern int netstat_get_info(char *, char **, off_t, int, int);
126 extern int afinet_get_info(char *, char **, off_t, int, int);
127 extern int tcp_get_info(char *, char **, off_t, int, int);
128 extern int udp_get_info(char *, char **, off_t, int, int);
129 extern void ip_mc_drop_socket(struct sock *sk);
131 #ifdef CONFIG_DLCI
132 extern int dlci_ioctl(unsigned int, void*);
133 #endif
135 #ifdef CONFIG_DLCI_MODULE
136 int (*dlci_ioctl_hook)(unsigned int, void *) = NULL;
137 #endif
139 int (*rarp_ioctl_hook)(unsigned int,void*) = NULL;
142 * Destroy an AF_INET socket
145 static __inline__ void kill_sk_queues(struct sock *sk)
147 struct sk_buff *skb;
149 /* First the read buffer. */
150 while((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
151 /* This will take care of closing sockets that were
152 * listening and didn't accept everything.
154 if (skb->sk != NULL && skb->sk != sk)
155 skb->sk->prot->close(skb->sk, 0);
156 kfree_skb(skb);
159 /* Next, the error queue. */
160 while((skb = skb_dequeue(&sk->error_queue)) != NULL)
161 kfree_skb(skb);
163 /* It is _impossible_ for the backlog to contain anything
164 * when we get here. All user references to this socket
165 * have gone away, only the net layer knows can touch it.
169 static __inline__ void kill_sk_now(struct sock *sk)
171 /* No longer exists. */
172 del_from_prot_sklist(sk);
174 /* Remove from protocol hash chains. */
175 sk->prot->unhash(sk);
177 if(sk->opt)
178 kfree(sk->opt);
179 dst_release(sk->dst_cache);
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 bh_unlock_sock(sk);
200 net_reset_timer(sk, TIME_DESTROY, SOCK_DESTROY_TIME);
203 /* Callers must hold the BH spinlock.
205 * At this point, there should be no process reference to this
206 * socket, and thus no user references at all. Therefore we
207 * can assume the socket waitqueue is inactive and nobody will
208 * try to jump onto it.
210 void destroy_sock(struct sock *sk)
212 /* Now we can no longer get new packets or once the
213 * timers are killed, send them.
215 net_delete_timer(sk);
217 if (sk->prot->destroy)
218 sk->prot->destroy(sk);
220 kill_sk_queues(sk);
222 /* Now if it has a half accepted/ closed socket. */
223 if (sk->pair) {
224 sk->pair->prot->close(sk->pair, 0);
225 sk->pair = NULL;
228 /* Now if everything is gone we can free the socket
229 * structure, otherwise we need to keep it around until
230 * everything is gone.
232 if (atomic_read(&sk->rmem_alloc) == 0 && atomic_read(&sk->wmem_alloc) == 0)
233 kill_sk_now(sk);
234 else
235 kill_sk_later(sk);
239 * The routines beyond this point handle the behaviour of an AF_INET
240 * socket object. Mostly it punts to the subprotocols of IP to do
241 * the work.
246 * Set socket options on an inet socket.
249 int inet_setsockopt(struct socket *sock, int level, int optname,
250 char *optval, int optlen)
252 struct sock *sk=sock->sk;
253 if (sk->prot->setsockopt==NULL)
254 return(-EOPNOTSUPP);
255 return sk->prot->setsockopt(sk,level,optname,optval,optlen);
259 * Get a socket option on an AF_INET socket.
261 * FIX: POSIX 1003.1g is very ambiguous here. It states that
262 * asynchronous errors should be reported by getsockopt. We assume
263 * this means if you specify SO_ERROR (otherwise whats the point of it).
266 int inet_getsockopt(struct socket *sock, int level, int optname,
267 char *optval, int *optlen)
269 struct sock *sk=sock->sk;
270 if (sk->prot->getsockopt==NULL)
271 return(-EOPNOTSUPP);
272 return sk->prot->getsockopt(sk,level,optname,optval,optlen);
276 * Automatically bind an unbound socket.
279 static int inet_autobind(struct sock *sk)
281 /* We may need to bind the socket. */
282 if (sk->num == 0) {
283 sk->num = sk->prot->good_socknum();
284 if (sk->num == 0)
285 return(-EAGAIN);
286 sk->sport = htons(sk->num);
287 sk->prot->hash(sk);
288 add_to_prot_sklist(sk);
290 return 0;
294 * Move a socket into listening state.
297 int inet_listen(struct socket *sock, int backlog)
299 struct sock *sk = sock->sk;
301 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
302 return(-EINVAL);
304 if (inet_autobind(sk) != 0)
305 return -EAGAIN;
307 /* We might as well re use these. */
308 if ((unsigned) backlog == 0) /* BSDism */
309 backlog = 1;
310 if ((unsigned) backlog > SOMAXCONN)
311 backlog = SOMAXCONN;
312 sk->max_ack_backlog = backlog;
313 if (sk->state != TCP_LISTEN) {
314 sk->ack_backlog = 0;
315 sk->state = TCP_LISTEN;
316 dst_release(xchg(&sk->dst_cache, NULL));
317 sk->prot->rehash(sk);
318 add_to_prot_sklist(sk);
320 sk->socket->flags |= SO_ACCEPTCON;
321 return(0);
325 * Create an inet socket.
327 * FIXME: Gcc would generate much better code if we set the parameters
328 * up in in-memory structure order. Gcc68K even more so
331 static int inet_create(struct socket *sock, int protocol)
333 struct sock *sk;
334 struct proto *prot;
336 /* Compatibility */
337 if (sock->type == SOCK_PACKET) {
338 static int warned;
339 if (net_families[PF_PACKET]==NULL)
341 #if defined(CONFIG_KMOD) && defined(CONFIG_PACKET_MODULE)
342 char module_name[30];
343 sprintf(module_name,"net-pf-%d", PF_PACKET);
344 request_module(module_name);
345 if (net_families[PF_PACKET] == NULL)
346 #endif
347 return -ESOCKTNOSUPPORT;
349 if (!warned++)
350 printk(KERN_INFO "%s uses obsolete (PF_INET,SOCK_PACKET)\n", current->comm);
351 return net_families[PF_PACKET]->create(sock, protocol);
354 sock->state = SS_UNCONNECTED;
355 sk = sk_alloc(PF_INET, GFP_KERNEL, 1);
356 if (sk == NULL)
357 goto do_oom;
359 switch (sock->type) {
360 case SOCK_STREAM:
361 if (protocol && protocol != IPPROTO_TCP)
362 goto free_and_noproto;
363 protocol = IPPROTO_TCP;
364 if (ipv4_config.no_pmtu_disc)
365 sk->ip_pmtudisc = IP_PMTUDISC_DONT;
366 else
367 sk->ip_pmtudisc = IP_PMTUDISC_WANT;
368 prot = &tcp_prot;
369 sock->ops = &inet_stream_ops;
370 break;
371 case SOCK_SEQPACKET:
372 goto free_and_badtype;
373 case SOCK_DGRAM:
374 if (protocol && protocol != IPPROTO_UDP)
375 goto free_and_noproto;
376 protocol = IPPROTO_UDP;
377 sk->no_check = UDP_NO_CHECK;
378 sk->ip_pmtudisc = IP_PMTUDISC_DONT;
379 prot=&udp_prot;
380 sock->ops = &inet_dgram_ops;
381 break;
382 case SOCK_RAW:
383 if (!capable(CAP_NET_RAW))
384 goto free_and_badperm;
385 if (!protocol)
386 goto free_and_noproto;
387 prot = &raw_prot;
388 sk->reuse = 1;
389 sk->ip_pmtudisc = IP_PMTUDISC_DONT;
390 sk->num = protocol;
391 sock->ops = &inet_dgram_ops;
392 if (protocol == IPPROTO_RAW)
393 sk->ip_hdrincl = 1;
394 break;
395 default:
396 goto free_and_badtype;
399 sock_init_data(sock,sk);
401 sk->destruct = NULL;
403 sk->zapped=0;
404 #ifdef CONFIG_TCP_NAGLE_OFF
405 sk->nonagle = 1;
406 #endif
407 sk->family = PF_INET;
408 sk->protocol = protocol;
410 sk->prot = prot;
411 sk->backlog_rcv = prot->backlog_rcv;
413 sk->timer.data = (unsigned long)sk;
414 sk->timer.function = &net_timer;
416 sk->ip_ttl=ip_statistics.IpDefaultTTL;
418 sk->ip_mc_loop=1;
419 sk->ip_mc_ttl=1;
420 sk->ip_mc_index=0;
421 sk->ip_mc_list=NULL;
423 if (sk->num) {
424 /* It assumes that any protocol which allows
425 * the user to assign a number at socket
426 * creation time automatically
427 * shares.
429 sk->sport = htons(sk->num);
431 /* Add to protocol hash chains. */
432 sk->prot->hash(sk);
433 add_to_prot_sklist(sk);
436 if (sk->prot->init) {
437 int err = sk->prot->init(sk);
438 if (err != 0) {
439 destroy_sock(sk);
440 return(err);
443 return(0);
445 free_and_badtype:
446 sk_free(sk);
447 return -ESOCKTNOSUPPORT;
449 free_and_badperm:
450 sk_free(sk);
451 return -EPERM;
453 free_and_noproto:
454 sk_free(sk);
455 return -EPROTONOSUPPORT;
457 do_oom:
458 return -ENOBUFS;
463 * The peer socket should always be NULL (or else). When we call this
464 * function we are destroying the object and from then on nobody
465 * should refer to it.
468 int inet_release(struct socket *sock, struct socket *peersock)
470 struct sock *sk = sock->sk;
472 if (sk) {
473 long timeout;
475 /* Begin closedown and wake up sleepers. */
476 if (sock->state != SS_UNCONNECTED)
477 sock->state = SS_DISCONNECTING;
478 sk->state_change(sk);
480 /* Applications forget to leave groups before exiting */
481 ip_mc_drop_socket(sk);
483 /* If linger is set, we don't return until the close
484 * is complete. Otherwise we return immediately. The
485 * actually closing is done the same either way.
487 * If the close is due to the process exiting, we never
488 * linger..
490 timeout = 0;
491 if (sk->linger && !(current->flags & PF_EXITING)) {
492 timeout = MAX_SCHEDULE_TIMEOUT;
494 /* XXX This makes no sense whatsoever... -DaveM */
495 if (!sk->lingertime)
496 timeout = HZ*sk->lingertime;
498 sock->sk = NULL;
499 sk->socket = NULL;
500 sk->prot->close(sk, timeout);
502 return(0);
505 static int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
507 struct sockaddr_in *addr=(struct sockaddr_in *)uaddr;
508 struct sock *sk=sock->sk;
509 unsigned short snum;
510 int chk_addr_ret;
512 /* If the socket has its own bind function then use it. (RAW) */
513 if(sk->prot->bind)
514 return sk->prot->bind(sk, uaddr, addr_len);
516 /* Check these errors (active socket, bad address length, double bind). */
517 if ((sk->state != TCP_CLOSE) ||
518 (addr_len < sizeof(struct sockaddr_in)) ||
519 (sk->num != 0))
520 return -EINVAL;
522 chk_addr_ret = inet_addr_type(addr->sin_addr.s_addr);
523 if (addr->sin_addr.s_addr != 0 && chk_addr_ret != RTN_LOCAL &&
524 chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST) {
525 #ifdef CONFIG_IP_TRANSPARENT_PROXY
526 /* Superuser may bind to any address to allow transparent proxying. */
527 if(chk_addr_ret != RTN_UNICAST || !capable(CAP_NET_ADMIN))
528 #endif
529 return -EADDRNOTAVAIL; /* Source address MUST be ours! */
532 /* We keep a pair of addresses. rcv_saddr is the one
533 * used by hash lookups, and saddr is used for transmit.
535 * In the BSD API these are the same except where it
536 * would be illegal to use them (multicast/broadcast) in
537 * which case the sending device address is used.
539 sk->rcv_saddr = sk->saddr = addr->sin_addr.s_addr;
540 if(chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
541 sk->saddr = 0; /* Use device */
543 snum = ntohs(addr->sin_port);
544 #ifdef CONFIG_IP_MASQUERADE
545 /* The kernel masquerader needs some ports. */
546 if((snum >= PORT_MASQ_BEGIN) && (snum <= PORT_MASQ_END))
547 return -EADDRINUSE;
548 #endif
549 if (snum == 0)
550 snum = sk->prot->good_socknum();
551 if (snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
552 return(-EACCES);
554 /* Make sure we are allowed to bind here. */
555 if(sk->prot->verify_bind(sk, snum))
556 return -EADDRINUSE;
558 sk->num = snum;
559 sk->sport = htons(snum);
560 sk->daddr = 0;
561 sk->dport = 0;
562 sk->prot->rehash(sk);
563 add_to_prot_sklist(sk);
564 dst_release(sk->dst_cache);
565 sk->dst_cache=NULL;
566 return(0);
569 int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
570 int addr_len, int flags)
572 struct sock *sk=sock->sk;
573 int err;
575 if (inet_autobind(sk) != 0)
576 return(-EAGAIN);
577 if (sk->prot->connect == NULL)
578 return(-EOPNOTSUPP);
579 err = sk->prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
580 if (err < 0)
581 return(err);
582 return(0);
585 static void inet_wait_for_connect(struct sock *sk)
587 DECLARE_WAITQUEUE(wait, current);
589 add_wait_queue(sk->sleep, &wait);
590 current->state = TASK_INTERRUPTIBLE;
591 while (sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV) {
592 if (signal_pending(current))
593 break;
594 if (sk->err)
595 break;
596 schedule();
597 current->state = TASK_INTERRUPTIBLE;
599 current->state = TASK_RUNNING;
600 remove_wait_queue(sk->sleep, &wait);
604 * Connect to a remote host. There is regrettably still a little
605 * TCP 'magic' in here.
608 int inet_stream_connect(struct socket *sock, struct sockaddr * uaddr,
609 int addr_len, int flags)
611 struct sock *sk=sock->sk;
612 int err;
614 if(sock->state != SS_UNCONNECTED && sock->state != SS_CONNECTING) {
615 if(sock->state == SS_CONNECTED)
616 return -EISCONN;
617 return -EINVAL;
620 if(sock->state == SS_CONNECTING) {
621 /* Note: tcp_connected contains SYN_RECV, which may cause
622 bogus results here. -AK */
623 if(tcp_connected(sk->state)) {
624 sock->state = SS_CONNECTED;
625 return 0;
627 if (sk->zapped || sk->err)
628 goto sock_error;
629 if (flags & O_NONBLOCK)
630 return -EALREADY;
631 } else {
632 /* We may need to bind the socket. */
633 if (inet_autobind(sk) != 0)
634 return(-EAGAIN);
635 if (sk->prot->connect == NULL)
636 return(-EOPNOTSUPP);
637 err = sk->prot->connect(sk, uaddr, addr_len);
638 /* Note: there is a theoretical race here when an wake up
639 occurred before inet_wait_for_connect is entered. In 2.3
640 the wait queue setup should be moved before the low level
641 connect call. -AK*/
642 if (err < 0)
643 return(err);
644 sock->state = SS_CONNECTING;
647 if (sk->state > TCP_FIN_WAIT2 && sock->state == SS_CONNECTING)
648 goto sock_error;
650 if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
651 return (-EINPROGRESS);
653 if (sk->state == TCP_SYN_SENT || sk->state == TCP_SYN_RECV) {
654 inet_wait_for_connect(sk);
655 if (signal_pending(current))
656 return -ERESTARTSYS;
659 sock->state = SS_CONNECTED;
660 if ((sk->state != TCP_ESTABLISHED) && sk->err)
661 goto sock_error;
662 return(0);
664 sock_error:
665 /* This is ugly but needed to fix a race in the ICMP error handler */
666 if (sk->zapped && sk->state != TCP_CLOSE) {
667 lock_sock(sk);
668 tcp_set_state(sk, TCP_CLOSE);
669 release_sock(sk);
670 sk->zapped = 0;
672 sock->state = SS_UNCONNECTED;
673 return sock_error(sk);
677 * Accept a pending connection. The TCP layer now gives BSD semantics.
680 int inet_accept(struct socket *sock, struct socket *newsock, int flags)
682 struct sock *sk1 = sock->sk, *sk2;
683 struct sock *newsk = newsock->sk;
684 int err = -EINVAL;
686 if (sock->state != SS_UNCONNECTED || !(sock->flags & SO_ACCEPTCON))
687 goto do_err;
689 err = -EOPNOTSUPP;
690 if (sk1->prot->accept == NULL)
691 goto do_err;
693 /* Restore the state if we have been interrupted, and then returned. */
694 if (sk1->pair != NULL) {
695 sk2 = sk1->pair;
696 sk1->pair = NULL;
697 } else {
698 if((sk2 = sk1->prot->accept(sk1,flags)) == NULL)
699 goto do_sk1_err;
703 * We've been passed an extra socket.
704 * We need to free it up because the tcp module creates
705 * its own when it accepts one.
707 sk2->sleep = newsk->sleep;
709 newsock->sk = sk2;
710 sk2->socket = newsock;
711 newsk->socket = NULL;
713 if (flags & O_NONBLOCK)
714 goto do_half_success;
716 if(sk2->state == TCP_ESTABLISHED)
717 goto do_full_success;
718 if(sk2->err > 0)
719 goto do_connect_err;
720 err = -ECONNABORTED;
721 if (sk2->state == TCP_CLOSE)
722 goto do_bad_connection;
723 do_full_success:
724 destroy_sock(newsk);
725 newsock->state = SS_CONNECTED;
726 return 0;
728 do_half_success:
729 destroy_sock(newsk);
730 return(0);
732 do_connect_err:
733 err = sock_error(sk2);
734 do_bad_connection:
735 sk2->sleep = NULL;
736 sk2->socket = NULL;
737 destroy_sock(sk2);
738 newsock->sk = newsk;
739 newsk->socket = newsock;
740 return err;
742 do_sk1_err:
743 err = sock_error(sk1);
744 do_err:
745 return err;
750 * This does both peername and sockname.
753 static int inet_getname(struct socket *sock, struct sockaddr *uaddr,
754 int *uaddr_len, int peer)
756 struct sock *sk = sock->sk;
757 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
759 sin->sin_family = AF_INET;
760 if (peer) {
761 if (!tcp_connected(sk->state))
762 return(-ENOTCONN);
763 sin->sin_port = sk->dport;
764 sin->sin_addr.s_addr = sk->daddr;
765 } else {
766 __u32 addr = sk->rcv_saddr;
767 if (!addr)
768 addr = sk->saddr;
769 sin->sin_port = sk->sport;
770 sin->sin_addr.s_addr = addr;
772 *uaddr_len = sizeof(*sin);
773 return(0);
778 int inet_recvmsg(struct socket *sock, struct msghdr *msg, int size,
779 int flags, struct scm_cookie *scm)
781 struct sock *sk = sock->sk;
782 int addr_len = 0;
783 int err;
785 if (sock->flags & SO_ACCEPTCON)
786 return(-EINVAL);
787 if (sk->prot->recvmsg == NULL)
788 return(-EOPNOTSUPP);
789 /* We may need to bind the socket. */
790 if (inet_autobind(sk) != 0)
791 return(-EAGAIN);
792 err = sk->prot->recvmsg(sk, msg, size, flags&MSG_DONTWAIT,
793 flags&~MSG_DONTWAIT, &addr_len);
794 if (err >= 0)
795 msg->msg_namelen = addr_len;
796 return err;
800 int inet_sendmsg(struct socket *sock, struct msghdr *msg, int size,
801 struct scm_cookie *scm)
803 struct sock *sk = sock->sk;
805 if (sk->shutdown & SEND_SHUTDOWN) {
806 if (!(msg->msg_flags&MSG_NOSIGNAL))
807 send_sig(SIGPIPE, current, 1);
808 return(-EPIPE);
810 if (sk->prot->sendmsg == NULL)
811 return(-EOPNOTSUPP);
812 if(sk->err)
813 return sock_error(sk);
815 /* We may need to bind the socket. */
816 if(inet_autobind(sk) != 0)
817 return -EAGAIN;
819 return sk->prot->sendmsg(sk, msg, size);
823 int inet_shutdown(struct socket *sock, int how)
825 struct sock *sk = sock->sk;
827 /* This should really check to make sure
828 * the socket is a TCP socket. (WHY AC...)
830 how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
831 1->2 bit 2 snds.
832 2->3 */
833 if ((how & ~SHUTDOWN_MASK) || how==0) /* MAXINT->0 */
834 return(-EINVAL);
835 if (sock->state == SS_CONNECTING && sk->state == TCP_ESTABLISHED)
836 sock->state = SS_CONNECTED;
837 if (!sk || !tcp_connected(sk->state))
838 return(-ENOTCONN);
839 sk->shutdown |= how;
840 if (sk->prot->shutdown)
841 sk->prot->shutdown(sk, how);
842 /* Wake up anyone sleeping in poll. */
843 sk->state_change(sk);
844 return(0);
848 unsigned int inet_poll(struct file * file, struct socket *sock, poll_table *wait)
850 struct sock *sk = sock->sk;
852 if (sk->prot->poll == NULL)
853 return(0);
854 return sk->prot->poll(file, sock, wait);
858 * ioctl() calls you can issue on an INET socket. Most of these are
859 * device configuration and stuff and very rarely used. Some ioctls
860 * pass on to the socket itself.
862 * NOTE: I like the idea of a module for the config stuff. ie ifconfig
863 * loads the devconfigure module does its configuring and unloads it.
864 * There's a good 20K of config code hanging around the kernel.
867 static int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
869 struct sock *sk = sock->sk;
870 int err;
871 int pid;
873 switch(cmd)
875 case FIOSETOWN:
876 case SIOCSPGRP:
877 err = get_user(pid, (int *) arg);
878 if (err)
879 return err;
880 if (current->pid != pid && current->pgrp != -pid &&
881 !capable(CAP_NET_ADMIN))
882 return -EPERM;
883 sk->proc = pid;
884 return(0);
885 case FIOGETOWN:
886 case SIOCGPGRP:
887 return put_user(sk->proc, (int *)arg);
888 case SIOCGSTAMP:
889 if(sk->stamp.tv_sec==0)
890 return -ENOENT;
891 err = copy_to_user((void *)arg,&sk->stamp,sizeof(struct timeval));
892 if (err)
893 err = -EFAULT;
894 return err;
895 case SIOCADDRT:
896 case SIOCDELRT:
897 case SIOCRTMSG:
898 return(ip_rt_ioctl(cmd,(void *) arg));
899 case SIOCDARP:
900 case SIOCGARP:
901 case SIOCSARP:
902 return(arp_ioctl(cmd,(void *) arg));
903 case SIOCDRARP:
904 case SIOCGRARP:
905 case SIOCSRARP:
906 #ifdef CONFIG_KMOD
907 if (rarp_ioctl_hook == NULL)
908 request_module("rarp");
909 #endif
910 if (rarp_ioctl_hook != NULL)
911 return(rarp_ioctl_hook(cmd,(void *) arg));
912 case SIOCGIFADDR:
913 case SIOCSIFADDR:
914 case SIOCGIFBRDADDR:
915 case SIOCSIFBRDADDR:
916 case SIOCGIFNETMASK:
917 case SIOCSIFNETMASK:
918 case SIOCGIFDSTADDR:
919 case SIOCSIFDSTADDR:
920 case SIOCSIFPFLAGS:
921 case SIOCGIFPFLAGS:
922 case SIOCSIFFLAGS:
923 return(devinet_ioctl(cmd,(void *) arg));
924 case SIOCGIFBR:
925 case SIOCSIFBR:
926 #ifdef CONFIG_BRIDGE
927 return(br_ioctl(cmd,(void *) arg));
928 #else
929 return -ENOPKG;
930 #endif
932 case SIOCADDDLCI:
933 case SIOCDELDLCI:
934 #ifdef CONFIG_DLCI
935 return(dlci_ioctl(cmd, (void *) arg));
936 #endif
938 #ifdef CONFIG_DLCI_MODULE
940 #ifdef CONFIG_KMOD
941 if (dlci_ioctl_hook == NULL)
942 request_module("dlci");
943 #endif
945 if (dlci_ioctl_hook)
946 return((*dlci_ioctl_hook)(cmd, (void *) arg));
947 #endif
948 return -ENOPKG;
950 default:
951 if ((cmd >= SIOCDEVPRIVATE) &&
952 (cmd <= (SIOCDEVPRIVATE + 15)))
953 return(dev_ioctl(cmd,(void *) arg));
955 #ifdef CONFIG_NET_RADIO
956 if((cmd >= SIOCIWFIRST) && (cmd <= SIOCIWLAST))
957 return(dev_ioctl(cmd,(void *) arg));
958 #endif
960 if (sk->prot->ioctl==NULL || (err=sk->prot->ioctl(sk, cmd, arg))==-ENOIOCTLCMD)
961 return(dev_ioctl(cmd,(void *) arg));
962 return err;
964 /*NOTREACHED*/
965 return(0);
968 struct proto_ops inet_stream_ops = {
969 PF_INET,
971 sock_no_dup,
972 inet_release,
973 inet_bind,
974 inet_stream_connect,
975 sock_no_socketpair,
976 inet_accept,
977 inet_getname,
978 inet_poll,
979 inet_ioctl,
980 inet_listen,
981 inet_shutdown,
982 inet_setsockopt,
983 inet_getsockopt,
984 sock_no_fcntl,
985 inet_sendmsg,
986 inet_recvmsg
989 struct proto_ops inet_dgram_ops = {
990 PF_INET,
992 sock_no_dup,
993 inet_release,
994 inet_bind,
995 inet_dgram_connect,
996 sock_no_socketpair,
997 sock_no_accept,
998 inet_getname,
999 datagram_poll,
1000 inet_ioctl,
1001 sock_no_listen,
1002 inet_shutdown,
1003 inet_setsockopt,
1004 inet_getsockopt,
1005 sock_no_fcntl,
1006 inet_sendmsg,
1007 inet_recvmsg
1010 struct net_proto_family inet_family_ops = {
1011 PF_INET,
1012 inet_create
1016 #ifdef CONFIG_PROC_FS
1017 #ifdef CONFIG_INET_RARP
1018 static struct proc_dir_entry proc_net_rarp = {
1019 PROC_NET_RARP, 4, "rarp",
1020 S_IFREG | S_IRUGO, 1, 0, 0,
1021 0, &proc_net_inode_operations,
1022 rarp_get_info
1024 #endif /* RARP */
1025 static struct proc_dir_entry proc_net_raw = {
1026 PROC_NET_RAW, 3, "raw",
1027 S_IFREG | S_IRUGO, 1, 0, 0,
1028 0, &proc_net_inode_operations,
1029 raw_get_info
1031 static struct proc_dir_entry proc_net_netstat = {
1032 PROC_NET_NETSTAT, 7, "netstat",
1033 S_IFREG | S_IRUGO, 1, 0, 0,
1034 0, &proc_net_inode_operations,
1035 netstat_get_info
1037 static struct proc_dir_entry proc_net_snmp = {
1038 PROC_NET_SNMP, 4, "snmp",
1039 S_IFREG | S_IRUGO, 1, 0, 0,
1040 0, &proc_net_inode_operations,
1041 snmp_get_info
1043 static struct proc_dir_entry proc_net_sockstat = {
1044 PROC_NET_SOCKSTAT, 8, "sockstat",
1045 S_IFREG | S_IRUGO, 1, 0, 0,
1046 0, &proc_net_inode_operations,
1047 afinet_get_info
1049 static struct proc_dir_entry proc_net_tcp = {
1050 PROC_NET_TCP, 3, "tcp",
1051 S_IFREG | S_IRUGO, 1, 0, 0,
1052 0, &proc_net_inode_operations,
1053 tcp_get_info
1055 static struct proc_dir_entry proc_net_udp = {
1056 PROC_NET_UDP, 3, "udp",
1057 S_IFREG | S_IRUGO, 1, 0, 0,
1058 0, &proc_net_inode_operations,
1059 udp_get_info
1061 #endif /* CONFIG_PROC_FS */
1063 extern void tcp_init(void);
1064 extern void tcp_v4_init(struct net_proto_family *);
1068 * Called by socket.c on kernel startup.
1071 __initfunc(void inet_proto_init(struct net_proto *pro))
1073 struct sk_buff *dummy_skb;
1074 struct inet_protocol *p;
1076 printk(KERN_INFO "NET4: Linux TCP/IP 1.0 for NET4.0\n");
1078 if (sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb))
1080 printk(KERN_CRIT "inet_proto_init: panic\n");
1081 return;
1085 * Tell SOCKET that we are alive...
1088 (void) sock_register(&inet_family_ops);
1091 * Add all the protocols.
1094 printk(KERN_INFO "IP Protocols: ");
1095 for(p = inet_protocol_base; p != NULL;)
1097 struct inet_protocol *tmp = (struct inet_protocol *) p->next;
1098 inet_add_protocol(p);
1099 printk("%s%s",p->name,tmp?", ":"\n");
1100 p = tmp;
1104 * Set the ARP module up
1107 arp_init();
1110 * Set the IP module up
1113 ip_init();
1115 tcp_v4_init(&inet_family_ops);
1117 /* Setup TCP slab cache for open requests. */
1118 tcp_init();
1122 * Set the ICMP layer up
1125 icmp_init(&inet_family_ops);
1127 /* I wish inet_add_protocol had no constructor hook...
1128 I had to move IPIP from net/ipv4/protocol.c :-( --ANK
1130 #ifdef CONFIG_NET_IPIP
1131 ipip_init();
1132 #endif
1133 #ifdef CONFIG_NET_IPGRE
1134 ipgre_init();
1135 #endif
1138 * Set the firewalling up
1140 #if defined(CONFIG_IP_FIREWALL)
1141 ip_fw_init();
1142 #endif
1144 #ifdef CONFIG_IP_MASQUERADE
1145 ip_masq_init();
1146 #endif
1149 * Initialise the multicast router
1151 #if defined(CONFIG_IP_MROUTE)
1152 ip_mr_init();
1153 #endif
1155 #ifdef CONFIG_INET_RARP
1156 rarp_ioctl_hook = rarp_ioctl;
1157 #endif
1159 * Create all the /proc entries.
1162 #ifdef CONFIG_PROC_FS
1163 #ifdef CONFIG_INET_RARP
1164 proc_net_register(&proc_net_rarp);
1165 #endif /* RARP */
1166 proc_net_register(&proc_net_raw);
1167 proc_net_register(&proc_net_snmp);
1168 proc_net_register(&proc_net_netstat);
1169 proc_net_register(&proc_net_sockstat);
1170 proc_net_register(&proc_net_tcp);
1171 proc_net_register(&proc_net_udp);
1172 #endif /* CONFIG_PROC_FS */