www.usr.com/support/gpl/USR9113_release1.0.tar.gz
[bcm963xx.git] / kernel / linux / net / ipv4 / af_inet.c
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.
5  *
6  *              PF_INET protocol family socket handler.
7  *
8  * Version:     $Id: af_inet.c,v 1.137 2002/02/01 22:01:03 davem Exp $
9  *
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>
14  *
15  * Changes (see also sock.c)
16  *
17  *              piggy,
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
25  *                                      structures
26  *                                      when accept() ed
27  *              Alan Cox        :       Semantics of SO_LINGER aren't state
28  *                                      moved to close when you look carefully.
29  *                                      With this fixed and the accept bug fixed
30  *                                      some RPC stuff seems happier.
31  *              Niibe Yutaka    :       4.4BSD style write async I/O
32  *              Alan Cox,
33  *              Tony Gale       :       Fixed reuse semantics.
34  *              Alan Cox        :       bind() shouldn't abort existing but dead
35  *                                      sockets. Stops FTP netin:.. I hope.
36  *              Alan Cox        :       bind() works correctly for RAW sockets.
37  *                                      Note that FreeBSD at least was broken
38  *                                      in this respect so be careful with
39  *                                      compatibility tests...
40  *              Alan Cox        :       routing cache support
41  *              Alan Cox        :       memzero the socket structure for
42  *                                      compactness.
43  *              Matt Day        :       nonblock connect error handler
44  *              Alan Cox        :       Allow large numbers of pending sockets
45  *                                      (eg for big web sites), but only if
46  *                                      specifically application requested.
47  *              Alan Cox        :       New buffering throughout IP. Used
48  *                                      dumbly.
49  *              Alan Cox        :       New buffering now used smartly.
50  *              Alan Cox        :       BSD rather than common sense
51  *                                      interpretation of listen.
52  *              Germano Caronni :       Assorted small races.
53  *              Alan Cox        :       sendmsg/recvmsg basic support.
54  *              Alan Cox        :       Only sendmsg/recvmsg now supported.
55  *              Alan Cox        :       Locked down bind (see security list).
56  *              Alan Cox        :       Loosened bind a little.
57  *              Mike McLagan    :       ADD/DEL DLCI Ioctls
58  *      Willy Konynenberg       :       Transparent proxying support.
59  *              David S. Miller :       New socket lookup architecture.
60  *                                      Some other random speedups.
61  *              Cyrus Durgin    :       Cleaned up file for kmod hacks.
62  *              Andi Kleen      :       Fix inet_stream_connect TCP race.
63  *
64  *              This program is free software; you can redistribute it and/or
65  *              modify it under the terms of the GNU General Public License
66  *              as published by the Free Software Foundation; either version
67  *              2 of the License, or (at your option) any later version.
68  */
69
70 #include <linux/config.h>
71 #include <linux/errno.h>
72 #include <linux/types.h>
73 #include <linux/socket.h>
74 #include <linux/in.h>
75 #include <linux/kernel.h>
76 #include <linux/major.h>
77 #include <linux/module.h>
78 #include <linux/sched.h>
79 #include <linux/timer.h>
80 #include <linux/string.h>
81 #include <linux/sockios.h>
82 #include <linux/net.h>
83 #include <linux/fcntl.h>
84 #include <linux/mm.h>
85 #include <linux/interrupt.h>
86 #include <linux/stat.h>
87 #include <linux/init.h>
88 #include <linux/poll.h>
89 #include <linux/netfilter_ipv4.h>
90
91 #include <asm/uaccess.h>
92 #include <asm/system.h>
93
94 #include <linux/smp_lock.h>
95 #include <linux/inet.h>
96 #include <linux/igmp.h>
97 #include <linux/netdevice.h>
98 #include <net/ip.h>
99 #include <net/protocol.h>
100 #include <net/arp.h>
101 #include <net/route.h>
102 #include <net/ip_fib.h>
103 #include <net/tcp.h>
104 #include <net/udp.h>
105 #include <linux/skbuff.h>
106 #include <net/sock.h>
107 #include <net/raw.h>
108 #include <net/icmp.h>
109 #include <net/ipip.h>
110 #include <net/inet_common.h>
111 #include <net/xfrm.h>
112 #ifdef CONFIG_IP_MROUTE
113 #include <linux/mroute.h>
114 #endif
115
116 DEFINE_SNMP_STAT(struct linux_mib, net_statistics);
117
118 #ifdef INET_REFCNT_DEBUG
119 atomic_t inet_sock_nr;
120 #endif
121
122 extern void ip_mc_drop_socket(struct sock *sk);
123
124 /* Per protocol sock slabcache */
125 kmem_cache_t *tcp_sk_cachep;
126 static kmem_cache_t *udp_sk_cachep;
127 static kmem_cache_t *raw4_sk_cachep;
128
129 /* The inetsw table contains everything that inet_create needs to
130  * build a new socket.
131  */
132 static struct list_head inetsw[SOCK_MAX];
133 static spinlock_t inetsw_lock = SPIN_LOCK_UNLOCKED;
134
135 /* New destruction routine */
136
137 void inet_sock_destruct(struct sock *sk)
138 {
139         struct inet_opt *inet = inet_sk(sk);
140
141         __skb_queue_purge(&sk->sk_receive_queue);
142         __skb_queue_purge(&sk->sk_error_queue);
143
144         if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
145                 printk("Attempt to release TCP socket in state %d %p\n",
146                        sk->sk_state, sk);
147                 return;
148         }
149         if (!sock_flag(sk, SOCK_DEAD)) {
150                 printk("Attempt to release alive inet socket %p\n", sk);
151                 return;
152         }
153
154         BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
155         BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
156         BUG_TRAP(!sk->sk_wmem_queued);
157         BUG_TRAP(!sk->sk_forward_alloc);
158
159         if (inet->opt)
160                 kfree(inet->opt);
161         dst_release(sk->sk_dst_cache);
162 #ifdef INET_REFCNT_DEBUG
163         atomic_dec(&inet_sock_nr);
164         printk(KERN_DEBUG "INET socket %p released, %d are still alive\n",
165                sk, atomic_read(&inet_sock_nr));
166 #endif
167 }
168
169 /*
170  *      The routines beyond this point handle the behaviour of an AF_INET
171  *      socket object. Mostly it punts to the subprotocols of IP to do
172  *      the work.
173  */
174
175 /*
176  *      Automatically bind an unbound socket.
177  */
178
179 static int inet_autobind(struct sock *sk)
180 {
181         struct inet_opt *inet;
182         /* We may need to bind the socket. */
183         lock_sock(sk);
184         inet = inet_sk(sk);
185         if (!inet->num) {
186                 if (sk->sk_prot->get_port(sk, 0)) {
187                         release_sock(sk);
188                         return -EAGAIN;
189                 }
190                 inet->sport = htons(inet->num);
191         }
192         release_sock(sk);
193         return 0;
194 }
195
196 /*
197  *      Move a socket into listening state.
198  */
199 int inet_listen(struct socket *sock, int backlog)
200 {
201         struct sock *sk = sock->sk;
202         unsigned char old_state;
203         int err;
204
205         lock_sock(sk);
206
207         err = -EINVAL;
208         if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
209                 goto out;
210
211         old_state = sk->sk_state;
212         if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
213                 goto out;
214
215         /* Really, if the socket is already in listen state
216          * we can only allow the backlog to be adjusted.
217          */
218         if (old_state != TCP_LISTEN) {
219                 err = tcp_listen_start(sk);
220                 if (err)
221                         goto out;
222         }
223         sk->sk_max_ack_backlog = backlog;
224         err = 0;
225
226 out:
227         release_sock(sk);
228         return err;
229 }
230
231 static __inline__ kmem_cache_t *inet_sk_slab(int protocol)
232 {
233         kmem_cache_t* rc = tcp_sk_cachep;
234
235         if (protocol == IPPROTO_UDP)
236                 rc = udp_sk_cachep;
237         else if (protocol == IPPROTO_RAW)
238                 rc = raw4_sk_cachep;
239         return rc;
240 }
241
242 static __inline__ int inet_sk_size(int protocol)
243 {
244         int rc = sizeof(struct tcp_sock);
245
246         if (protocol == IPPROTO_UDP)
247                 rc = sizeof(struct udp_sock);
248         else if (protocol == IPPROTO_RAW)
249                 rc = sizeof(struct raw_sock);
250         return rc;
251 }
252
253 /*
254  *      Create an inet socket.
255  */
256
257 static int inet_create(struct socket *sock, int protocol)
258 {
259         struct sock *sk;
260         struct list_head *p;
261         struct inet_protosw *answer;
262         struct inet_opt *inet;
263         int err = -ENOBUFS;
264
265         sock->state = SS_UNCONNECTED;
266         sk = sk_alloc(PF_INET, GFP_KERNEL, inet_sk_size(protocol),
267                       inet_sk_slab(protocol));
268         if (!sk)
269                 goto out;
270
271         /* Look for the requested type/protocol pair. */
272         answer = NULL;
273         rcu_read_lock();
274         list_for_each_rcu(p, &inetsw[sock->type]) {
275                 answer = list_entry(p, struct inet_protosw, list);
276
277                 /* Check the non-wild match. */
278                 if (protocol == answer->protocol) {
279                         if (protocol != IPPROTO_IP)
280                                 break;
281                 } else {
282                         /* Check for the two wild cases. */
283                         if (IPPROTO_IP == protocol) {
284                                 protocol = answer->protocol;
285                                 break;
286                         }
287                         if (IPPROTO_IP == answer->protocol)
288                                 break;
289                 }
290                 answer = NULL;
291         }
292
293         err = -ESOCKTNOSUPPORT;
294         if (!answer)
295                 goto out_sk_free;
296         err = -EPERM;
297         if (answer->capability > 0 && !capable(answer->capability))
298                 goto out_sk_free;
299         err = -EPROTONOSUPPORT;
300         if (!protocol)
301                 goto out_sk_free;
302         err = 0;
303         sock->ops = answer->ops;
304         sk->sk_prot = answer->prot;
305         sk->sk_no_check = answer->no_check;
306         if (INET_PROTOSW_REUSE & answer->flags)
307                 sk->sk_reuse = 1;
308         rcu_read_unlock();
309
310         inet = inet_sk(sk);
311
312         if (SOCK_RAW == sock->type) {
313                 inet->num = protocol;
314                 if (IPPROTO_RAW == protocol)
315                         inet->hdrincl = 1;
316         }
317
318         if (ipv4_config.no_pmtu_disc)
319                 inet->pmtudisc = IP_PMTUDISC_DONT;
320         else
321                 inet->pmtudisc = IP_PMTUDISC_WANT;
322
323         inet->id = 0;
324
325         sock_init_data(sock, sk);
326         sk_set_owner(sk, THIS_MODULE);
327
328         sk->sk_destruct    = inet_sock_destruct;
329         sk->sk_zapped      = 0;
330         sk->sk_family      = PF_INET;
331         sk->sk_protocol    = protocol;
332         sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
333
334         inet->uc_ttl    = -1;
335         inet->mc_loop   = 1;
336         inet->mc_ttl    = 1;
337         inet->mc_index  = 0;
338         inet->mc_list   = NULL;
339
340 #ifdef INET_REFCNT_DEBUG
341         atomic_inc(&inet_sock_nr);
342 #endif
343
344         if (inet->num) {
345                 /* It assumes that any protocol which allows
346                  * the user to assign a number at socket
347                  * creation time automatically
348                  * shares.
349                  */
350                 inet->sport = htons(inet->num);
351                 /* Add to protocol hash chains. */
352                 sk->sk_prot->hash(sk);
353         }
354
355         if (sk->sk_prot->init) {
356                 err = sk->sk_prot->init(sk);
357                 if (err)
358                         sk_common_release(sk);
359         }
360 out:
361         return err;
362 out_sk_free:
363         rcu_read_unlock();
364         sk_free(sk);
365         goto out;
366 }
367
368
369 /*
370  *      The peer socket should always be NULL (or else). When we call this
371  *      function we are destroying the object and from then on nobody
372  *      should refer to it.
373  */
374 int inet_release(struct socket *sock)
375 {
376         struct sock *sk = sock->sk;
377
378         if (sk) {
379                 long timeout;
380
381                 /* Applications forget to leave groups before exiting */
382                 ip_mc_drop_socket(sk);
383
384                 /* If linger is set, we don't return until the close
385                  * is complete.  Otherwise we return immediately. The
386                  * actually closing is done the same either way.
387                  *
388                  * If the close is due to the process exiting, we never
389                  * linger..
390                  */
391                 timeout = 0;
392                 if (sock_flag(sk, SOCK_LINGER) &&
393                     !(current->flags & PF_EXITING))
394                         timeout = sk->sk_lingertime;
395                 sock->sk = NULL;
396                 sk->sk_prot->close(sk, timeout);
397         }
398         return 0;
399 }
400
401 /* It is off by default, see below. */
402 int sysctl_ip_nonlocal_bind;
403
404 int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
405 {
406         struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
407         struct sock *sk = sock->sk;
408         struct inet_opt *inet = inet_sk(sk);
409         unsigned short snum;
410         int chk_addr_ret;
411         int err;
412
413         /* If the socket has its own bind function then use it. (RAW) */
414         if (sk->sk_prot->bind) {
415                 err = sk->sk_prot->bind(sk, uaddr, addr_len);
416                 goto out;
417         }
418         err = -EINVAL;
419         if (addr_len < sizeof(struct sockaddr_in))
420                 goto out;
421
422         chk_addr_ret = inet_addr_type(addr->sin_addr.s_addr);
423
424         /* Not specified by any standard per-se, however it breaks too
425          * many applications when removed.  It is unfortunate since
426          * allowing applications to make a non-local bind solves
427          * several problems with systems using dynamic addressing.
428          * (ie. your servers still start up even if your ISDN link
429          *  is temporarily down)
430          */
431         err = -EADDRNOTAVAIL;
432         if (!sysctl_ip_nonlocal_bind &&
433             !inet->freebind &&
434             addr->sin_addr.s_addr != INADDR_ANY &&
435             chk_addr_ret != RTN_LOCAL &&
436             chk_addr_ret != RTN_MULTICAST &&
437             chk_addr_ret != RTN_BROADCAST)
438                 goto out;
439
440         snum = ntohs(addr->sin_port);
441         err = -EACCES;
442         if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
443                 goto out;
444
445         /*      We keep a pair of addresses. rcv_saddr is the one
446          *      used by hash lookups, and saddr is used for transmit.
447          *
448          *      In the BSD API these are the same except where it
449          *      would be illegal to use them (multicast/broadcast) in
450          *      which case the sending device address is used.
451          */
452         lock_sock(sk);
453
454         /* Check these errors (active socket, double bind). */
455         err = -EINVAL;
456         if (sk->sk_state != TCP_CLOSE || inet->num)
457                 goto out_release_sock;
458
459         inet->rcv_saddr = inet->saddr = addr->sin_addr.s_addr;
460         if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
461                 inet->saddr = 0;  /* Use device */
462
463         /* Make sure we are allowed to bind here. */
464         if (sk->sk_prot->get_port(sk, snum)) {
465                 inet->saddr = inet->rcv_saddr = 0;
466                 err = -EADDRINUSE;
467                 goto out_release_sock;
468         }
469
470         if (inet->rcv_saddr)
471                 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
472         if (snum)
473                 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
474         inet->sport = htons(inet->num);
475         inet->daddr = 0;
476         inet->dport = 0;
477         sk_dst_reset(sk);
478         err = 0;
479 out_release_sock:
480         release_sock(sk);
481 out:
482         return err;
483 }
484
485 int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
486                        int addr_len, int flags)
487 {
488         struct sock *sk = sock->sk;
489
490         if (uaddr->sa_family == AF_UNSPEC)
491                 return sk->sk_prot->disconnect(sk, flags);
492
493         if (!inet_sk(sk)->num && inet_autobind(sk))
494                 return -EAGAIN;
495         return sk->sk_prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
496 }
497
498 static long inet_wait_for_connect(struct sock *sk, long timeo)
499 {
500         DEFINE_WAIT(wait);
501
502         prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
503
504         /* Basic assumption: if someone sets sk->sk_err, he _must_
505          * change state of the socket from TCP_SYN_*.
506          * Connect() does not allow to get error notifications
507          * without closing the socket.
508          */
509         while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
510                 release_sock(sk);
511                 timeo = schedule_timeout(timeo);
512                 lock_sock(sk);
513                 if (signal_pending(current) || !timeo)
514                         break;
515                 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
516         }
517         finish_wait(sk->sk_sleep, &wait);
518         return timeo;
519 }
520
521 /*
522  *      Connect to a remote host. There is regrettably still a little
523  *      TCP 'magic' in here.
524  */
525 int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
526                         int addr_len, int flags)
527 {
528         struct sock *sk = sock->sk;
529         int err;
530         long timeo;
531
532         lock_sock(sk);
533
534         if (uaddr->sa_family == AF_UNSPEC) {
535                 err = sk->sk_prot->disconnect(sk, flags);
536                 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
537                 goto out;
538         }
539
540         switch (sock->state) {
541         default:
542                 err = -EINVAL;
543                 goto out;
544         case SS_CONNECTED:
545                 err = -EISCONN;
546                 goto out;
547         case SS_CONNECTING:
548                 err = -EALREADY;
549                 /* Fall out of switch with err, set for this state */
550                 break;
551         case SS_UNCONNECTED:
552                 err = -EISCONN;
553                 if (sk->sk_state != TCP_CLOSE)
554                         goto out;
555
556                 err = sk->sk_prot->connect(sk, uaddr, addr_len);
557                 if (err < 0)
558                         goto out;
559
560                 sock->state = SS_CONNECTING;
561
562                 /* Just entered SS_CONNECTING state; the only
563                  * difference is that return value in non-blocking
564                  * case is EINPROGRESS, rather than EALREADY.
565                  */
566                 err = -EINPROGRESS;
567                 break;
568         }
569
570         timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
571
572         if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
573                 /* Error code is set above */
574                 if (!timeo || !inet_wait_for_connect(sk, timeo))
575                         goto out;
576
577                 err = sock_intr_errno(timeo);
578                 if (signal_pending(current))
579                         goto out;
580         }
581
582         /* Connection was closed by RST, timeout, ICMP error
583          * or another process disconnected us.
584          */
585         if (sk->sk_state == TCP_CLOSE)
586                 goto sock_error;
587
588         /* sk->sk_err may be not zero now, if RECVERR was ordered by user
589          * and error was received after socket entered established state.
590          * Hence, it is handled normally after connect() return successfully.
591          */
592
593         sock->state = SS_CONNECTED;
594         err = 0;
595 out:
596         release_sock(sk);
597         return err;
598
599 sock_error:
600         err = sock_error(sk) ? : -ECONNABORTED;
601         sock->state = SS_UNCONNECTED;
602         if (sk->sk_prot->disconnect(sk, flags))
603                 sock->state = SS_DISCONNECTING;
604         goto out;
605 }
606
607 /*
608  *      Accept a pending connection. The TCP layer now gives BSD semantics.
609  */
610
611 int inet_accept(struct socket *sock, struct socket *newsock, int flags)
612 {
613         struct sock *sk1 = sock->sk;
614         int err = -EINVAL;
615         struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err);
616
617         if (!sk2)
618                 goto do_err;
619
620         lock_sock(sk2);
621
622         BUG_TRAP((1 << sk2->sk_state) &
623                  (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_CLOSE));
624
625         sock_graft(sk2, newsock);
626
627         newsock->state = SS_CONNECTED;
628         err = 0;
629         release_sock(sk2);
630 do_err:
631         return err;
632 }
633
634
635 /*
636  *      This does both peername and sockname.
637  */
638 int inet_getname(struct socket *sock, struct sockaddr *uaddr,
639                         int *uaddr_len, int peer)
640 {
641         struct sock *sk         = sock->sk;
642         struct inet_opt *inet   = inet_sk(sk);
643         struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
644
645         sin->sin_family = AF_INET;
646         if (peer) {
647                 if (!inet->dport ||
648                     (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
649                      peer == 1))
650                         return -ENOTCONN;
651                 sin->sin_port = inet->dport;
652                 sin->sin_addr.s_addr = inet->daddr;
653         } else {
654                 __u32 addr = inet->rcv_saddr;
655                 if (!addr)
656                         addr = inet->saddr;
657                 sin->sin_port = inet->sport;
658                 sin->sin_addr.s_addr = addr;
659         }
660         memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
661         *uaddr_len = sizeof(*sin);
662         return 0;
663 }
664
665 int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
666                  size_t size)
667 {
668         struct sock *sk = sock->sk;
669
670         /* We may need to bind the socket. */
671         if (!inet_sk(sk)->num && inet_autobind(sk))
672                 return -EAGAIN;
673
674         return sk->sk_prot->sendmsg(iocb, sk, msg, size);
675 }
676
677
678 ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
679 {
680         struct sock *sk = sock->sk;
681
682         /* We may need to bind the socket. */
683         if (!inet_sk(sk)->num && inet_autobind(sk))
684                 return -EAGAIN;
685
686         if (sk->sk_prot->sendpage)
687                 return sk->sk_prot->sendpage(sk, page, offset, size, flags);
688         return sock_no_sendpage(sock, page, offset, size, flags);
689 }
690
691
692 int inet_shutdown(struct socket *sock, int how)
693 {
694         struct sock *sk = sock->sk;
695         int err = 0;
696
697         /* This should really check to make sure
698          * the socket is a TCP socket. (WHY AC...)
699          */
700         how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
701                        1->2 bit 2 snds.
702                        2->3 */
703         if ((how & ~SHUTDOWN_MASK) || !how)     /* MAXINT->0 */
704                 return -EINVAL;
705
706         lock_sock(sk);
707         if (sock->state == SS_CONNECTING) {
708                 if ((1 << sk->sk_state) &
709                     (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
710                         sock->state = SS_DISCONNECTING;
711                 else
712                         sock->state = SS_CONNECTED;
713         }
714
715         switch (sk->sk_state) {
716         case TCP_CLOSE:
717                 err = -ENOTCONN;
718                 /* Hack to wake up other listeners, who can poll for
719                    POLLHUP, even on eg. unconnected UDP sockets -- RR */
720         default:
721                 sk->sk_shutdown |= how;
722                 if (sk->sk_prot->shutdown)
723                         sk->sk_prot->shutdown(sk, how);
724                 break;
725
726         /* Remaining two branches are temporary solution for missing
727          * close() in multithreaded environment. It is _not_ a good idea,
728          * but we have no choice until close() is repaired at VFS level.
729          */
730         case TCP_LISTEN:
731                 if (!(how & RCV_SHUTDOWN))
732                         break;
733                 /* Fall through */
734         case TCP_SYN_SENT:
735                 err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
736                 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
737                 break;
738         }
739
740         /* Wake up anyone sleeping in poll. */
741         sk->sk_state_change(sk);
742         release_sock(sk);
743         return err;
744 }
745
746 #if defined(CONFIG_MIPS_BRCM)
747 #define SIOCGEXTIF      0x8908
748 #endif
749
750 /*
751  *      ioctl() calls you can issue on an INET socket. Most of these are
752  *      device configuration and stuff and very rarely used. Some ioctls
753  *      pass on to the socket itself.
754  *
755  *      NOTE: I like the idea of a module for the config stuff. ie ifconfig
756  *      loads the devconfigure module does its configuring and unloads it.
757  *      There's a good 20K of config code hanging around the kernel.
758  */
759
760 int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
761 {
762         struct sock *sk = sock->sk;
763         int err = 0;
764
765         switch (cmd) {
766                 case SIOCGSTAMP:
767                         err = sock_get_timestamp(sk, (struct timeval __user *)arg);
768                         break;
769                 case SIOCADDRT:
770                 case SIOCDELRT:
771                 case SIOCRTMSG:
772                         err = ip_rt_ioctl(cmd, (void __user *)arg);
773                         break;
774                 case SIOCDARP:
775                 case SIOCGARP:
776                 case SIOCSARP:
777                         err = arp_ioctl(cmd, (void __user *)arg);
778                         break;
779 #if defined(CONFIG_MIPS_BRCM)
780                 case SIOCGEXTIF:
781                         err = sock_get_extif(sk, (char __user *)arg);
782                         break;
783 #endif
784                 case SIOCGIFADDR:
785                 case SIOCSIFADDR:
786                 case SIOCGIFBRDADDR:
787                 case SIOCSIFBRDADDR:
788                 case SIOCGIFNETMASK:
789                 case SIOCSIFNETMASK:
790                 case SIOCGIFDSTADDR:
791                 case SIOCSIFDSTADDR:
792                 case SIOCSIFPFLAGS:
793                 case SIOCGIFPFLAGS:
794                 case SIOCSIFFLAGS:
795                         err = devinet_ioctl(cmd, (void __user *)arg);
796                         break;
797                 default:
798                         if (!sk->sk_prot->ioctl ||
799                             (err = sk->sk_prot->ioctl(sk, cmd, arg)) ==
800                                                                 -ENOIOCTLCMD)
801                                 err = dev_ioctl(cmd, (void __user *)arg);
802                         break;
803         }
804         return err;
805 }
806
807 struct proto_ops inet_stream_ops = {
808         .family =       PF_INET,
809         .owner =        THIS_MODULE,
810         .release =      inet_release,
811         .bind =         inet_bind,
812         .connect =      inet_stream_connect,
813         .socketpair =   sock_no_socketpair,
814         .accept =       inet_accept,
815         .getname =      inet_getname,
816         .poll =         tcp_poll,
817         .ioctl =        inet_ioctl,
818         .listen =       inet_listen,
819         .shutdown =     inet_shutdown,
820         .setsockopt =   sock_common_setsockopt,
821         .getsockopt =   sock_common_getsockopt,
822         .sendmsg =      inet_sendmsg,
823         .recvmsg =      sock_common_recvmsg,
824         .mmap =         sock_no_mmap,
825         .sendpage =     tcp_sendpage
826 };
827
828 struct proto_ops inet_dgram_ops = {
829         .family =       PF_INET,
830         .owner =        THIS_MODULE,
831         .release =      inet_release,
832         .bind =         inet_bind,
833         .connect =      inet_dgram_connect,
834         .socketpair =   sock_no_socketpair,
835         .accept =       sock_no_accept,
836         .getname =      inet_getname,
837         .poll =         datagram_poll,
838         .ioctl =        inet_ioctl,
839         .listen =       sock_no_listen,
840         .shutdown =     inet_shutdown,
841         .setsockopt =   sock_common_setsockopt,
842         .getsockopt =   sock_common_getsockopt,
843         .sendmsg =      inet_sendmsg,
844         .recvmsg =      sock_common_recvmsg,
845         .mmap =         sock_no_mmap,
846         .sendpage =     inet_sendpage,
847 };
848
849 struct net_proto_family inet_family_ops = {
850         .family = PF_INET,
851         .create = inet_create,
852         .owner  = THIS_MODULE,
853 };
854
855
856 extern void tcp_init(void);
857 extern void tcp_v4_init(struct net_proto_family *);
858
859 /* Upon startup we insert all the elements in inetsw_array[] into
860  * the linked list inetsw.
861  */
862 static struct inet_protosw inetsw_array[] =
863 {
864         {
865                 .type =       SOCK_STREAM,
866                 .protocol =   IPPROTO_TCP,
867                 .prot =       &tcp_prot,
868                 .ops =        &inet_stream_ops,
869                 .capability = -1,
870                 .no_check =   0,
871                 .flags =      INET_PROTOSW_PERMANENT,
872         },
873
874         {
875                 .type =       SOCK_DGRAM,
876                 .protocol =   IPPROTO_UDP,
877                 .prot =       &udp_prot,
878                 .ops =        &inet_dgram_ops,
879                 .capability = -1,
880                 .no_check =   UDP_CSUM_DEFAULT,
881                 .flags =      INET_PROTOSW_PERMANENT,
882        },
883         
884
885        {
886                .type =       SOCK_RAW,
887                .protocol =   IPPROTO_IP,        /* wild card */
888                .prot =       &raw_prot,
889                .ops =        &inet_dgram_ops,
890                .capability = CAP_NET_RAW,
891                .no_check =   UDP_CSUM_DEFAULT,
892                .flags =      INET_PROTOSW_REUSE,
893        }
894 };
895
896 #define INETSW_ARRAY_LEN (sizeof(inetsw_array) / sizeof(struct inet_protosw))
897
898 void inet_register_protosw(struct inet_protosw *p)
899 {
900         struct list_head *lh;
901         struct inet_protosw *answer;
902         int protocol = p->protocol;
903         struct list_head *last_perm;
904
905         spin_lock_bh(&inetsw_lock);
906
907         if (p->type >= SOCK_MAX)
908                 goto out_illegal;
909
910         /* If we are trying to override a permanent protocol, bail. */
911         answer = NULL;
912         last_perm = &inetsw[p->type];
913         list_for_each(lh, &inetsw[p->type]) {
914                 answer = list_entry(lh, struct inet_protosw, list);
915
916                 /* Check only the non-wild match. */
917                 if (INET_PROTOSW_PERMANENT & answer->flags) {
918                         if (protocol == answer->protocol)
919                                 break;
920                         last_perm = lh;
921                 }
922
923                 answer = NULL;
924         }
925         if (answer)
926                 goto out_permanent;
927
928         /* Add the new entry after the last permanent entry if any, so that
929          * the new entry does not override a permanent entry when matched with
930          * a wild-card protocol. But it is allowed to override any existing
931          * non-permanent entry.  This means that when we remove this entry, the 
932          * system automatically returns to the old behavior.
933          */
934         list_add_rcu(&p->list, last_perm);
935 out:
936         spin_unlock_bh(&inetsw_lock);
937
938         synchronize_net();
939
940         return;
941
942 out_permanent:
943         printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
944                protocol);
945         goto out;
946
947 out_illegal:
948         printk(KERN_ERR
949                "Ignoring attempt to register invalid socket type %d.\n",
950                p->type);
951         goto out;
952 }
953
954 void inet_unregister_protosw(struct inet_protosw *p)
955 {
956         if (INET_PROTOSW_PERMANENT & p->flags) {
957                 printk(KERN_ERR
958                        "Attempt to unregister permanent protocol %d.\n",
959                        p->protocol);
960         } else {
961                 spin_lock_bh(&inetsw_lock);
962                 list_del_rcu(&p->list);
963                 spin_unlock_bh(&inetsw_lock);
964
965                 synchronize_net();
966         }
967 }
968
969 #ifdef CONFIG_IP_MULTICAST
970 static struct net_protocol igmp_protocol = {
971         .handler =      igmp_rcv,
972 };
973 #endif
974
975 static struct net_protocol tcp_protocol = {
976         .handler =      tcp_v4_rcv,
977         .err_handler =  tcp_v4_err,
978         .no_policy =    1,
979 };
980
981 static struct net_protocol udp_protocol = {
982         .handler =      udp_rcv,
983         .err_handler =  udp_err,
984         .no_policy =    1,
985 };
986
987 static struct net_protocol icmp_protocol = {
988         .handler =      icmp_rcv,
989 };
990
991 static int __init init_ipv4_mibs(void)
992 {
993         net_statistics[0] = alloc_percpu(struct linux_mib);
994         net_statistics[1] = alloc_percpu(struct linux_mib);
995         ip_statistics[0] = alloc_percpu(struct ipstats_mib);
996         ip_statistics[1] = alloc_percpu(struct ipstats_mib);
997         icmp_statistics[0] = alloc_percpu(struct icmp_mib);
998         icmp_statistics[1] = alloc_percpu(struct icmp_mib);
999         tcp_statistics[0] = alloc_percpu(struct tcp_mib);
1000         tcp_statistics[1] = alloc_percpu(struct tcp_mib);
1001         udp_statistics[0] = alloc_percpu(struct udp_mib);
1002         udp_statistics[1] = alloc_percpu(struct udp_mib);
1003         if (!
1004             (net_statistics[0] && net_statistics[1] && ip_statistics[0]
1005              && ip_statistics[1] && tcp_statistics[0] && tcp_statistics[1]
1006              && udp_statistics[0] && udp_statistics[1]))
1007                 return -ENOMEM;
1008
1009         (void) tcp_mib_init();
1010
1011         return 0;
1012 }
1013
1014 int ipv4_proc_init(void);
1015 extern void ipfrag_init(void);
1016
1017 static int __init inet_init(void)
1018 {
1019         struct sk_buff *dummy_skb;
1020         struct inet_protosw *q;
1021         struct list_head *r;
1022
1023         if (sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb)) {
1024                 printk(KERN_CRIT "%s: panic\n", __FUNCTION__);
1025                 return -EINVAL;
1026         }
1027
1028         tcp_sk_cachep = kmem_cache_create("tcp_sock",
1029                                           sizeof(struct tcp_sock), 0,
1030                                           SLAB_HWCACHE_ALIGN, NULL, NULL);
1031         udp_sk_cachep = kmem_cache_create("udp_sock",
1032                                           sizeof(struct udp_sock), 0,
1033                                           SLAB_HWCACHE_ALIGN, NULL, NULL);
1034         raw4_sk_cachep = kmem_cache_create("raw4_sock",
1035                                            sizeof(struct raw_sock), 0,
1036                                            SLAB_HWCACHE_ALIGN, NULL, NULL);
1037         if (!tcp_sk_cachep || !udp_sk_cachep || !raw4_sk_cachep)
1038                 printk(KERN_CRIT
1039                        "inet_init: Can't create protocol sock SLAB caches!\n");
1040         /*
1041          *      Tell SOCKET that we are alive... 
1042          */
1043
1044         (void)sock_register(&inet_family_ops);
1045
1046         /*
1047          *      Add all the base protocols.
1048          */
1049
1050         if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
1051                 printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
1052         if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
1053                 printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
1054         if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
1055                 printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n");
1056 #ifdef CONFIG_IP_MULTICAST
1057         if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
1058                 printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n");
1059 #endif
1060
1061         /* Register the socket-side information for inet_create. */
1062         for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
1063                 INIT_LIST_HEAD(r);
1064
1065         for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
1066                 inet_register_protosw(q);
1067
1068         /*
1069          *      Set the ARP module up
1070          */
1071
1072         arp_init();
1073
1074         /*
1075          *      Set the IP module up
1076          */
1077
1078         ip_init();
1079
1080         tcp_v4_init(&inet_family_ops);
1081
1082         /* Setup TCP slab cache for open requests. */
1083         tcp_init();
1084
1085
1086         /*
1087          *      Set the ICMP layer up
1088          */
1089
1090         icmp_init(&inet_family_ops);
1091
1092         /*
1093          *      Initialise the multicast router
1094          */
1095 #if defined(CONFIG_IP_MROUTE)
1096         ip_mr_init();
1097 #endif
1098         /*
1099          *      Initialise per-cpu ipv4 mibs
1100          */ 
1101
1102         if(init_ipv4_mibs())
1103                 printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n"); ;
1104         
1105         ipv4_proc_init();
1106
1107         ipfrag_init();
1108
1109         return 0;
1110 }
1111
1112 module_init(inet_init);
1113
1114 /* ------------------------------------------------------------------------ */
1115
1116 #ifdef CONFIG_PROC_FS
1117 extern int  fib_proc_init(void);
1118 extern void fib_proc_exit(void);
1119 extern int  ip_misc_proc_init(void);
1120 extern int  raw_proc_init(void);
1121 extern void raw_proc_exit(void);
1122 extern int  tcp4_proc_init(void);
1123 extern void tcp4_proc_exit(void);
1124 extern int  udp4_proc_init(void);
1125 extern void udp4_proc_exit(void);
1126
1127 int __init ipv4_proc_init(void)
1128 {
1129         int rc = 0;
1130
1131         if (raw_proc_init())
1132                 goto out_raw;
1133         if (tcp4_proc_init())
1134                 goto out_tcp;
1135         if (udp4_proc_init())
1136                 goto out_udp;
1137         if (fib_proc_init())
1138                 goto out_fib;
1139         if (ip_misc_proc_init())
1140                 goto out_misc;
1141 out:
1142         return rc;
1143 out_misc:
1144         fib_proc_exit();
1145 out_fib:
1146         udp4_proc_exit();
1147 out_udp:
1148         tcp4_proc_exit();
1149 out_tcp:
1150         raw_proc_exit();
1151 out_raw:
1152         rc = -ENOMEM;
1153         goto out;
1154 }
1155
1156 #else /* CONFIG_PROC_FS */
1157 int __init ipv4_proc_init(void)
1158 {
1159         return 0;
1160 }
1161 #endif /* CONFIG_PROC_FS */
1162
1163 MODULE_ALIAS_NETPROTO(PF_INET);
1164
1165 EXPORT_SYMBOL(inet_accept);
1166 EXPORT_SYMBOL(inet_bind);
1167 EXPORT_SYMBOL(inet_dgram_connect);
1168 EXPORT_SYMBOL(inet_dgram_ops);
1169 EXPORT_SYMBOL(inet_family_ops);
1170 EXPORT_SYMBOL(inet_getname);
1171 EXPORT_SYMBOL(inet_ioctl);
1172 EXPORT_SYMBOL(inet_listen);
1173 EXPORT_SYMBOL(inet_register_protosw);
1174 EXPORT_SYMBOL(inet_release);
1175 EXPORT_SYMBOL(inet_sendmsg);
1176 EXPORT_SYMBOL(inet_shutdown);
1177 EXPORT_SYMBOL(inet_sock_destruct);
1178 EXPORT_SYMBOL(inet_stream_connect);
1179 EXPORT_SYMBOL(inet_stream_ops);
1180 EXPORT_SYMBOL(inet_unregister_protosw);
1181 EXPORT_SYMBOL(net_statistics);
1182 EXPORT_SYMBOL(tcp_protocol);
1183 EXPORT_SYMBOL(udp_protocol);
1184
1185 #ifdef INET_REFCNT_DEBUG
1186 EXPORT_SYMBOL(inet_sock_nr);
1187 #endif