~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/net/ipv4/af_inet.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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.127 2000/12/22 19:51:50 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  *              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.
 57  *
 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.
 62  */
 63 
 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>
 83 #include <linux/netfilter_ipv4.h>
 84 
 85 #include <asm/uaccess.h>
 86 #include <asm/system.h>
 87 
 88 #include <linux/smp_lock.h>
 89 #include <linux/inet.h>
 90 #include <linux/netdevice.h>
 91 #include <net/ip.h>
 92 #include <net/protocol.h>
 93 #include <net/arp.h>
 94 #include <net/route.h>
 95 #include <net/tcp.h>
 96 #include <net/udp.h>
 97 #include <linux/skbuff.h>
 98 #include <net/sock.h>
 99 #include <net/raw.h>
100 #include <net/icmp.h>
101 #include <net/ipip.h>
102 #include <net/inet_common.h>
103 #ifdef CONFIG_IP_MROUTE
104 #include <linux/mroute.h>
105 #endif
106 #include <linux/if_bridge.h>
107 #ifdef CONFIG_KMOD
108 #include <linux/kmod.h>
109 #endif
110 #ifdef CONFIG_NET_DIVERT
111 #include <linux/divert.h>
112 #endif /* CONFIG_NET_DIVERT */
113 #if defined(CONFIG_NET_RADIO) || defined(CONFIG_NET_PCMCIA_RADIO)
114 #include <linux/wireless.h>             /* Note : will define WIRELESS_EXT */
115 #endif  /* CONFIG_NET_RADIO || CONFIG_NET_PCMCIA_RADIO */
116 
117 #define min(a,b)        ((a)<(b)?(a):(b))
118 
119 struct linux_mib net_statistics[NR_CPUS*2];
120 
121 #ifdef INET_REFCNT_DEBUG
122 atomic_t inet_sock_nr;
123 #endif
124 
125 extern int raw_get_info(char *, char **, off_t, int);
126 extern int snmp_get_info(char *, char **, off_t, int);
127 extern int netstat_get_info(char *, char **, off_t, int);
128 extern int afinet_get_info(char *, char **, off_t, int);
129 extern int tcp_get_info(char *, char **, off_t, int);
130 extern int udp_get_info(char *, char **, off_t, int);
131 extern void ip_mc_drop_socket(struct sock *sk);
132 
133 #ifdef CONFIG_DLCI
134 extern int dlci_ioctl(unsigned int, void*);
135 #endif
136 
137 #ifdef CONFIG_DLCI_MODULE
138 int (*dlci_ioctl_hook)(unsigned int, void *) = NULL;
139 #endif
140 
141 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
142 int (*br_ioctl_hook)(unsigned long) = NULL;
143 #endif
144 
145 /* New destruction routine */
146 
147 void inet_sock_destruct(struct sock *sk)
148 {
149         __skb_queue_purge(&sk->receive_queue);
150         __skb_queue_purge(&sk->error_queue);
151 
152         if (sk->type == SOCK_STREAM && sk->state != TCP_CLOSE) {
153                 printk("Attempt to release TCP socket in state %d %p\n",
154                        sk->state,
155                        sk);
156                 return;
157         }
158         if (!sk->dead) {
159                 printk("Attempt to release alive inet socket %p\n", sk);
160                 return;
161         }
162 
163         BUG_TRAP(atomic_read(&sk->rmem_alloc) == 0);
164         BUG_TRAP(atomic_read(&sk->wmem_alloc) == 0);
165         BUG_TRAP(sk->wmem_queued == 0);
166         BUG_TRAP(sk->forward_alloc == 0);
167 
168         if (sk->protinfo.af_inet.opt)
169                 kfree(sk->protinfo.af_inet.opt);
170         dst_release(sk->dst_cache);
171 #ifdef INET_REFCNT_DEBUG
172         atomic_dec(&inet_sock_nr);
173         printk(KERN_DEBUG "INET socket %p released, %d are still alive\n", sk, atomic_read(&inet_sock_nr));
174 #endif
175 }
176 
177 void inet_sock_release(struct sock *sk)
178 {
179         if (sk->prot->destroy)
180                 sk->prot->destroy(sk);
181 
182         /* Observation: when inet_sock_release is called, processes have
183          * no access to socket. But net still has.
184          * Step one, detach it from networking:
185          *
186          * A. Remove from hash tables.
187          */
188 
189         sk->prot->unhash(sk);
190 
191         /* In this point socket cannot receive new packets,
192          * but it is possible that some packets are in flight
193          * because some CPU runs receiver and did hash table lookup
194          * before we unhashed socket. They will achieve receive queue
195          * and will be purged by socket destructor.
196          *
197          * Also we still have packets pending on receive
198          * queue and probably, our own packets waiting in device queues.
199          * sock_destroy will drain receive queue, but transmitted
200          * packets will delay socket destruction until the last reference
201          * will be released.
202          */
203 
204         sock_orphan(sk);
205 
206 #ifdef INET_REFCNT_DEBUG
207         if (atomic_read(&sk->refcnt) != 1) {
208                 printk(KERN_DEBUG "Destruction inet %p delayed, c=%d\n", sk, atomic_read(&sk->refcnt));
209         }
210 #endif
211         sock_put(sk);
212 }
213 
214 
215 /*
216  *      The routines beyond this point handle the behaviour of an AF_INET
217  *      socket object. Mostly it punts to the subprotocols of IP to do
218  *      the work.
219  */
220  
221 
222 /*
223  *      Set socket options on an inet socket.
224  */
225  
226 int inet_setsockopt(struct socket *sock, int level, int optname,
227                     char *optval, int optlen)
228 {
229         struct sock *sk=sock->sk;
230 
231         return sk->prot->setsockopt(sk,level,optname,optval,optlen);
232 }
233 
234 /*
235  *      Get a socket option on an AF_INET socket.
236  *
237  *      FIX: POSIX 1003.1g is very ambiguous here. It states that
238  *      asynchronous errors should be reported by getsockopt. We assume
239  *      this means if you specify SO_ERROR (otherwise whats the point of it).
240  */
241 
242 int inet_getsockopt(struct socket *sock, int level, int optname,
243                     char *optval, int *optlen)
244 {
245         struct sock *sk=sock->sk;
246 
247         return sk->prot->getsockopt(sk,level,optname,optval,optlen);
248 }
249 
250 /*
251  *      Automatically bind an unbound socket.
252  */
253 
254 static int inet_autobind(struct sock *sk)
255 {
256         /* We may need to bind the socket. */
257         lock_sock(sk);
258         if (sk->num == 0) {
259                 if (sk->prot->get_port(sk, 0) != 0) {
260                         release_sock(sk);
261                         return -EAGAIN;
262                 }
263                 sk->sport = htons(sk->num);
264         }
265         release_sock(sk);
266         return 0;
267 }
268 
269 /*
270  *      Move a socket into listening state.
271  */
272  
273 int inet_listen(struct socket *sock, int backlog)
274 {
275         struct sock *sk = sock->sk;
276         unsigned char old_state;
277         int err;
278 
279         lock_sock(sk);
280 
281         err = -EINVAL;
282         if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
283                 goto out;
284 
285         old_state = sk->state;
286         if (!((1<<old_state)&(TCPF_CLOSE|TCPF_LISTEN)))
287                 goto out;
288 
289         /* Really, if the socket is already in listen state
290          * we can only allow the backlog to be adjusted.
291          */
292         if (old_state != TCP_LISTEN) {
293                 err = tcp_listen_start(sk);
294                 if (err)
295                         goto out;
296         }
297         sk->max_ack_backlog = backlog;
298         err = 0;
299 
300 out:
301         release_sock(sk);
302         return err;
303 }
304 
305 /*
306  *      Create an inet socket.
307  */
308 
309 static int inet_create(struct socket *sock, int protocol)
310 {
311         struct sock *sk;
312         struct proto *prot;
313 
314         sock->state = SS_UNCONNECTED;
315         sk = sk_alloc(PF_INET, GFP_KERNEL, 1);
316         if (sk == NULL) 
317                 goto do_oom;
318 
319         switch (sock->type) {
320         case SOCK_STREAM:
321                 if (protocol && protocol != IPPROTO_TCP)
322                         goto free_and_noproto;
323                 protocol = IPPROTO_TCP;
324                 prot = &tcp_prot;
325                 sock->ops = &inet_stream_ops;
326                 break;
327         case SOCK_SEQPACKET:
328                 goto free_and_badtype;
329         case SOCK_DGRAM:
330                 if (protocol && protocol != IPPROTO_UDP)
331                         goto free_and_noproto;
332                 protocol = IPPROTO_UDP;
333                 sk->no_check = UDP_CSUM_DEFAULT;
334                 prot=&udp_prot;
335                 sock->ops = &inet_dgram_ops;
336                 break;
337         case SOCK_RAW:
338                 if (!capable(CAP_NET_RAW))
339                         goto free_and_badperm;
340                 if (!protocol)
341                         goto free_and_noproto;
342                 prot = &raw_prot;
343                 sk->reuse = 1;
344                 sk->num = protocol;
345                 sock->ops = &inet_dgram_ops;
346                 if (protocol == IPPROTO_RAW)
347                         sk->protinfo.af_inet.hdrincl = 1;
348                 break;
349         default:
350                 goto free_and_badtype;
351         }
352 
353         if (ipv4_config.no_pmtu_disc)
354                 sk->protinfo.af_inet.pmtudisc = IP_PMTUDISC_DONT;
355         else
356                 sk->protinfo.af_inet.pmtudisc = IP_PMTUDISC_WANT;
357 
358         sock_init_data(sock,sk);
359 
360         sk->destruct = inet_sock_destruct;
361 
362         sk->zapped = 0;
363         sk->family = PF_INET;
364         sk->protocol = protocol;
365 
366         sk->prot = prot;
367         sk->backlog_rcv = prot->backlog_rcv;
368 
369         sk->protinfo.af_inet.ttl=sysctl_ip_default_ttl;
370 
371         sk->protinfo.af_inet.mc_loop=1;
372         sk->protinfo.af_inet.mc_ttl=1;
373         sk->protinfo.af_inet.mc_index=0;
374         sk->protinfo.af_inet.mc_list=NULL;
375 
376 #ifdef INET_REFCNT_DEBUG
377         atomic_inc(&inet_sock_nr);
378 #endif
379 
380         if (sk->num) {
381                 /* It assumes that any protocol which allows
382                  * the user to assign a number at socket
383                  * creation time automatically
384                  * shares.
385                  */
386                 sk->sport = htons(sk->num);
387 
388                 /* Add to protocol hash chains. */
389                 sk->prot->hash(sk);
390         }
391 
392         if (sk->prot->init) {
393                 int err = sk->prot->init(sk);
394                 if (err != 0) {
395                         inet_sock_release(sk);
396                         return(err);
397                 }
398         }
399         return(0);
400 
401 free_and_badtype:
402         sk_free(sk);
403         return -ESOCKTNOSUPPORT;
404 
405 free_and_badperm:
406         sk_free(sk);
407         return -EPERM;
408 
409 free_and_noproto:
410         sk_free(sk);
411         return -EPROTONOSUPPORT;
412 
413 do_oom:
414         return -ENOBUFS;
415 }
416 
417 
418 /*
419  *      The peer socket should always be NULL (or else). When we call this
420  *      function we are destroying the object and from then on nobody
421  *      should refer to it.
422  */
423  
424 int inet_release(struct socket *sock)
425 {
426         struct sock *sk = sock->sk;
427 
428         if (sk) {
429                 long timeout;
430 
431                 /* Applications forget to leave groups before exiting */
432                 ip_mc_drop_socket(sk);
433 
434                 /* If linger is set, we don't return until the close
435                  * is complete.  Otherwise we return immediately. The
436                  * actually closing is done the same either way.
437                  *
438                  * If the close is due to the process exiting, we never
439                  * linger..
440                  */
441                 timeout = 0;
442                 if (sk->linger && !(current->flags & PF_EXITING))
443                         timeout = sk->lingertime;
444                 sock->sk = NULL;
445                 sk->prot->close(sk, timeout);
446         }
447         return(0);
448 }
449 
450 /* It is off by default, see below. */
451 int sysctl_ip_nonlocal_bind;
452 
453 static int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
454 {
455         struct sockaddr_in *addr=(struct sockaddr_in *)uaddr;
456         struct sock *sk=sock->sk;
457         unsigned short snum;
458         int chk_addr_ret;
459         int err;
460 
461         /* If the socket has its own bind function then use it. (RAW) */
462         if(sk->prot->bind)
463                 return sk->prot->bind(sk, uaddr, addr_len);
464 
465         if (addr_len < sizeof(struct sockaddr_in))
466                 return -EINVAL;
467 
468         chk_addr_ret = inet_addr_type(addr->sin_addr.s_addr);
469 
470         /* Not specified by any standard per-se, however it breaks too
471          * many applications when removed.  It is unfortunate since
472          * allowing applications to make a non-local bind solves
473          * several problems with systems using dynamic addressing.
474          * (ie. your servers still start up even if your ISDN link
475          *  is temporarily down)
476          */
477         if (sysctl_ip_nonlocal_bind == 0 && 
478             sk->protinfo.af_inet.freebind == 0 &&
479             addr->sin_addr.s_addr != INADDR_ANY &&
480             chk_addr_ret != RTN_LOCAL &&
481             chk_addr_ret != RTN_MULTICAST &&
482             chk_addr_ret != RTN_BROADCAST)
483                 return -EADDRNOTAVAIL;
484 
485         snum = ntohs(addr->sin_port);
486         if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
487                 return -EACCES;
488 
489         /*      We keep a pair of addresses. rcv_saddr is the one
490          *      used by hash lookups, and saddr is used for transmit.
491          *
492          *      In the BSD API these are the same except where it
493          *      would be illegal to use them (multicast/broadcast) in
494          *      which case the sending device address is used.
495          */
496         lock_sock(sk);
497 
498         /* Check these errors (active socket, double bind). */
499         err = -EINVAL;
500         if ((sk->state != TCP_CLOSE)                    ||
501             (sk->num != 0))
502                 goto out;
503 
504         sk->rcv_saddr = sk->saddr = addr->sin_addr.s_addr;
505         if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
506                 sk->saddr = 0;  /* Use device */
507 
508         /* Make sure we are allowed to bind here. */
509         if (sk->prot->get_port(sk, snum) != 0) {
510                 sk->saddr = sk->rcv_saddr = 0;
511                 err = -EADDRINUSE;
512                 goto out;
513         }
514 
515         if (sk->rcv_saddr)
516                 sk->userlocks |= SOCK_BINDADDR_LOCK;
517         if (snum)
518                 sk->userlocks |= SOCK_BINDPORT_LOCK;
519         sk->sport = htons(sk->num);
520         sk->daddr = 0;
521         sk->dport = 0;
522         sk_dst_reset(sk);
523         err = 0;
524 out:
525         release_sock(sk);
526         return err;
527 }
528 
529 int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
530                        int addr_len, int flags)
531 {
532         struct sock *sk=sock->sk;
533 
534         if (uaddr->sa_family == AF_UNSPEC)
535                 return sk->prot->disconnect(sk, flags);
536 
537         if (sk->num==0 && inet_autobind(sk) != 0)
538                 return -EAGAIN;
539         return sk->prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
540 }
541 
542 static long inet_wait_for_connect(struct sock *sk, long timeo)
543 {
544         DECLARE_WAITQUEUE(wait, current);
545 
546         __set_current_state(TASK_INTERRUPTIBLE);
547         add_wait_queue(sk->sleep, &wait);
548 
549         /* Basic assumption: if someone sets sk->err, he _must_
550          * change state of the socket from TCP_SYN_*.
551          * Connect() does not allow to get error notifications
552          * without closing the socket.
553          */
554         while ((1<<sk->state)&(TCPF_SYN_SENT|TCPF_SYN_RECV)) {
555                 release_sock(sk);
556                 timeo = schedule_timeout(timeo);
557                 lock_sock(sk);
558                 if (signal_pending(current) || !timeo)
559                         break;
560                 set_current_state(TASK_INTERRUPTIBLE);
561         }
562         __set_current_state(TASK_RUNNING);
563         remove_wait_queue(sk->sleep, &wait);
564         return timeo;
565 }
566 
567 /*
568  *      Connect to a remote host. There is regrettably still a little
569  *      TCP 'magic' in here.
570  */
571  
572 int inet_stream_connect(struct socket *sock, struct sockaddr * uaddr,
573                         int addr_len, int flags)
574 {
575         struct sock *sk=sock->sk;
576         int err;
577         long timeo;
578 
579         lock_sock(sk);
580 
581         if (uaddr->sa_family == AF_UNSPEC) {
582                 err = sk->prot->disconnect(sk, flags);
583                 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
584                 goto out;
585         }
586 
587         switch (sock->state) {
588         default:
589                 err = -EINVAL;
590                 goto out;
591         case SS_CONNECTED:
592                 err = -EISCONN;
593                 goto out;
594         case SS_CONNECTING:
595                 err = -EALREADY;
596                 /* Fall out of switch with err, set for this state */
597                 break;
598         case SS_UNCONNECTED:
599                 err = -EISCONN;
600                 if (sk->state != TCP_CLOSE) 
601                         goto out;
602 
603                 err = -EAGAIN;
604                 if (sk->num == 0) {
605                         if (sk->prot->get_port(sk, 0) != 0)
606                                 goto out;
607                         sk->sport = htons(sk->num);
608                 }
609 
610                 err = sk->prot->connect(sk, uaddr, addr_len);
611                 if (err < 0)
612                         goto out;
613 
614                 sock->state = SS_CONNECTING;
615 
616                 /* Just entered SS_CONNECTING state; the only
617                  * difference is that return value in non-blocking
618                  * case is EINPROGRESS, rather than EALREADY.
619                  */
620                 err = -EINPROGRESS;
621                 break;
622         }
623 
624         timeo = sock_sndtimeo(sk, flags&O_NONBLOCK);
625 
626         if ((1<<sk->state)&(TCPF_SYN_SENT|TCPF_SYN_RECV)) {
627                 /* Error code is set above */
628                 if (!timeo || !inet_wait_for_connect(sk, timeo))
629                         goto out;
630 
631                 err = sock_intr_errno(timeo);
632                 if (signal_pending(current))
633                         goto out;
634         }
635 
636         /* Connection was closed by RST, timeout, ICMP error
637          * or another process disconnected us.
638          */
639         if (sk->state == TCP_CLOSE)
640                 goto sock_error;
641 
642         /* sk->err may be not zero now, if RECVERR was ordered by user
643          * and error was received after socket entered established state.
644          * Hence, it is handled normally after connect() return successfully.
645          */
646 
647         sock->state = SS_CONNECTED;
648         err = 0;
649 out:
650         release_sock(sk);
651         return err;
652 
653 sock_error:
654         err = sock_error(sk) ? : -ECONNABORTED;
655         sock->state = SS_UNCONNECTED;
656         if (sk->prot->disconnect(sk, flags))
657                 sock->state = SS_DISCONNECTING;
658         goto out;
659 }
660 
661 /*
662  *      Accept a pending connection. The TCP layer now gives BSD semantics.
663  */
664 
665 int inet_accept(struct socket *sock, struct socket *newsock, int flags)
666 {
667         struct sock *sk1 = sock->sk;
668         struct sock *sk2;
669         int err = -EINVAL;
670 
671         if((sk2 = sk1->prot->accept(sk1,flags,&err)) == NULL)
672                 goto do_err;
673 
674         lock_sock(sk2);
675 
676         BUG_TRAP((1<<sk2->state)&(TCPF_ESTABLISHED|TCPF_CLOSE_WAIT|TCPF_CLOSE));
677 
678         sock_graft(sk2, newsock);
679 
680         newsock->state = SS_CONNECTED;
681         release_sock(sk2);
682         return 0;
683 
684 do_err:
685         return err;
686 }
687 
688 
689 /*
690  *      This does both peername and sockname.
691  */
692  
693 static int inet_getname(struct socket *sock, struct sockaddr *uaddr,
694                  int *uaddr_len, int peer)
695 {
696         struct sock *sk         = sock->sk;
697         struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
698   
699         sin->sin_family = AF_INET;
700         if (peer) {
701                 if (!sk->dport)
702                         return -ENOTCONN;
703                 if (((1<<sk->state)&(TCPF_CLOSE|TCPF_SYN_SENT)) && peer == 1)
704                         return -ENOTCONN;
705                 sin->sin_port = sk->dport;
706                 sin->sin_addr.s_addr = sk->daddr;
707         } else {
708                 __u32 addr = sk->rcv_saddr;
709                 if (!addr)
710                         addr = sk->saddr;
711                 sin->sin_port = sk->sport;
712                 sin->sin_addr.s_addr = addr;
713         }
714         *uaddr_len = sizeof(*sin);
715         return(0);
716 }
717 
718 
719 
720 int inet_recvmsg(struct socket *sock, struct msghdr *msg, int size,
721                  int flags, struct scm_cookie *scm)
722 {
723         struct sock *sk = sock->sk;
724         int addr_len = 0;
725         int err;
726 
727         err = sk->prot->recvmsg(sk, msg, size, flags&MSG_DONTWAIT,
728                                 flags&~MSG_DONTWAIT, &addr_len);
729         if (err >= 0)
730                 msg->msg_namelen = addr_len;
731         return err;
732 }
733 
734 
735 int inet_sendmsg(struct socket *sock, struct msghdr *msg, int size,
736                  struct scm_cookie *scm)
737 {
738         struct sock *sk = sock->sk;
739 
740         /* We may need to bind the socket. */
741         if (sk->num==0 && inet_autobind(sk) != 0)
742                 return -EAGAIN;
743 
744         return sk->prot->sendmsg(sk, msg, size);
745 }
746 
747 int inet_shutdown(struct socket *sock, int how)
748 {
749         struct sock *sk = sock->sk;
750         int err = 0;
751 
752         /* This should really check to make sure
753          * the socket is a TCP socket. (WHY AC...)
754          */
755         how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
756                        1->2 bit 2 snds.
757                        2->3 */
758         if ((how & ~SHUTDOWN_MASK) || how==0)   /* MAXINT->0 */
759                 return -EINVAL;
760 
761         lock_sock(sk);
762         if (sock->state == SS_CONNECTING) {
763                 if ((1<<sk->state)&(TCPF_SYN_SENT|TCPF_SYN_RECV|TCPF_CLOSE))
764                         sock->state = SS_DISCONNECTING;
765                 else
766                         sock->state = SS_CONNECTED;
767         }
768 
769         switch (sk->state) {
770         case TCP_CLOSE:
771                 err = -ENOTCONN;
772                 /* Hack to wake up other listeners, who can poll for
773                    POLLHUP, even on eg. unconnected UDP sockets -- RR */
774         default:
775                 sk->shutdown |= how;
776                 if (sk->prot->shutdown)
777                         sk->prot->shutdown(sk, how);
778                 break;
779 
780         /* Remaining two branches are temporary solution for missing
781          * close() in multithreaded environment. It is _not_ a good idea,
782          * but we have no choice until close() is repaired at VFS level.
783          */
784         case TCP_LISTEN:
785                 if (!(how & RCV_SHUTDOWN))
786                         break;
787                 /* Fall through */
788         case TCP_SYN_SENT:
789                 err = sk->prot->disconnect(sk, O_NONBLOCK);
790                 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
791                 break;
792         }
793 
794         /* Wake up anyone sleeping in poll. */
795         sk->state_change(sk);
796         release_sock(sk);
797         return err;
798 }
799 
800 /*
801  *      ioctl() calls you can issue on an INET socket. Most of these are
802  *      device configuration and stuff and very rarely used. Some ioctls
803  *      pass on to the socket itself.
804  *
805  *      NOTE: I like the idea of a module for the config stuff. ie ifconfig
806  *      loads the devconfigure module does its configuring and unloads it.
807  *      There's a good 20K of config code hanging around the kernel.
808  */
809 
810 static int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
811 {
812         struct sock *sk = sock->sk;
813         int err;
814         int pid;
815 
816         switch(cmd) 
817         {
818                 case FIOSETOWN:
819                 case SIOCSPGRP:
820                         err = get_user(pid, (int *) arg);
821                         if (err)
822                                 return err; 
823                         if (current->pid != pid && current->pgrp != -pid && 
824                             !capable(CAP_NET_ADMIN))
825                                 return -EPERM;
826                         sk->proc = pid;
827                         return(0);
828                 case FIOGETOWN:
829                 case SIOCGPGRP:
830                         return put_user(sk->proc, (int *)arg);
831                 case SIOCGSTAMP:
832                         if(sk->stamp.tv_sec==0)
833                                 return -ENOENT;
834                         err = copy_to_user((void *)arg,&sk->stamp,sizeof(struct timeval));
835                         if (err)
836                                 err = -EFAULT;
837                         return err;
838                 case SIOCADDRT:
839                 case SIOCDELRT:
840                 case SIOCRTMSG:
841                         return(ip_rt_ioctl(cmd,(void *) arg));
842                 case SIOCDARP:
843                 case SIOCGARP:
844                 case SIOCSARP:
845                         return(arp_ioctl(cmd,(void *) arg));
846                 case SIOCGIFADDR:
847                 case SIOCSIFADDR:
848                 case SIOCGIFBRDADDR:
849                 case SIOCSIFBRDADDR:
850                 case SIOCGIFNETMASK:
851                 case SIOCSIFNETMASK:
852                 case SIOCGIFDSTADDR:
853                 case SIOCSIFDSTADDR:
854                 case SIOCSIFPFLAGS:     
855                 case SIOCGIFPFLAGS:     
856                 case SIOCSIFFLAGS:
857                         return(devinet_ioctl(cmd,(void *) arg));
858                 case SIOCGIFBR:
859                 case SIOCSIFBR:
860 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
861 #ifdef CONFIG_KMOD
862                         if (br_ioctl_hook == NULL)
863                                 request_module("bridge");
864 #endif
865                         if (br_ioctl_hook != NULL)
866                                 return br_ioctl_hook(arg);
867 #endif
868                 case SIOCGIFDIVERT:
869                 case SIOCSIFDIVERT:
870 #ifdef CONFIG_NET_DIVERT
871                         return(divert_ioctl(cmd, (struct divert_cf *) arg));
872 #else
873                         return -ENOPKG;
874 #endif  /* CONFIG_NET_DIVERT */
875                         return -ENOPKG;
876                         
877                 case SIOCADDDLCI:
878                 case SIOCDELDLCI:
879 #ifdef CONFIG_DLCI
880                         lock_kernel();
881                         err = dlci_ioctl(cmd, (void *) arg);
882                         unlock_kernel();
883                         return err;
884 #endif
885 
886 #ifdef CONFIG_DLCI_MODULE
887 
888 #ifdef CONFIG_KMOD
889                         if (dlci_ioctl_hook == NULL)
890                                 request_module("dlci");
891 #endif
892 
893                         if (dlci_ioctl_hook) {
894                                 lock_kernel();
895                                 err = (*dlci_ioctl_hook)(cmd, (void *) arg);
896                                 unlock_kernel();
897                                 return err;
898                         }
899 #endif
900                         return -ENOPKG;
901 
902                 default:
903                         if ((cmd >= SIOCDEVPRIVATE) &&
904                             (cmd <= (SIOCDEVPRIVATE + 15)))
905                                 return(dev_ioctl(cmd,(void *) arg));
906 
907 #ifdef WIRELESS_EXT
908                         if((cmd >= SIOCIWFIRST) && (cmd <= SIOCIWLAST))
909                                 return(dev_ioctl(cmd,(void *) arg));
910 #endif  /* WIRELESS_EXT */
911 
912                         if (sk->prot->ioctl==NULL || (err=sk->prot->ioctl(sk, cmd, arg))==-ENOIOCTLCMD)
913                                 return(dev_ioctl(cmd,(void *) arg));            
914                         return err;
915         }
916         /*NOTREACHED*/
917         return(0);
918 }
919 
920 struct proto_ops inet_stream_ops = {
921         family:         PF_INET,
922 
923         release:        inet_release,
924         bind:           inet_bind,
925         connect:        inet_stream_connect,
926         socketpair:     sock_no_socketpair,
927         accept:         inet_accept,
928         getname:        inet_getname, 
929         poll:           tcp_poll,
930         ioctl:          inet_ioctl,
931         listen:         inet_listen,
932         shutdown:       inet_shutdown,
933         setsockopt:     inet_setsockopt,
934         getsockopt:     inet_getsockopt,
935         sendmsg:        inet_sendmsg,
936         recvmsg:        inet_recvmsg,
937         mmap:           sock_no_mmap
938 };
939 
940 struct proto_ops inet_dgram_ops = {
941         family:         PF_INET,
942 
943         release:        inet_release,
944         bind:           inet_bind,
945         connect:        inet_dgram_connect,
946         socketpair:     sock_no_socketpair,
947         accept:         sock_no_accept,
948         getname:        inet_getname, 
949         poll:           datagram_poll,
950         ioctl:          inet_ioctl,
951         listen:         sock_no_listen,
952         shutdown:       inet_shutdown,
953         setsockopt:     inet_setsockopt,
954         getsockopt:     inet_getsockopt,
955         sendmsg:        inet_sendmsg,
956         recvmsg:        inet_recvmsg,
957         mmap:           sock_no_mmap,
958 };
959 
960 struct net_proto_family inet_family_ops = {
961         PF_INET,
962         inet_create
963 };
964 
965 
966 extern void tcp_init(void);
967 extern void tcp_v4_init(struct net_proto_family *);
968 
969 
970 /*
971  *      Called by socket.c on kernel startup.  
972  */
973  
974 static int __init inet_init(void)
975 {
976         struct sk_buff *dummy_skb;
977         struct inet_protocol *p;
978 
979         printk(KERN_INFO "NET4: Linux TCP/IP 1.0 for NET4.0\n");
980 
981         if (sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb))
982         {
983                 printk(KERN_CRIT "inet_proto_init: panic\n");
984                 return -EINVAL;
985         }
986 
987         /*
988          *      Tell SOCKET that we are alive... 
989          */
990    
991         (void) sock_register(&inet_family_ops);
992 
993         /*
994          *      Add all the protocols. 
995          */
996 
997         printk(KERN_INFO "IP Protocols: ");
998         for(p = inet_protocol_base; p != NULL;) 
999         {
1000                 struct inet_protocol *tmp = (struct inet_protocol *) p->next;
1001                 inet_add_protocol(p);
1002                 printk("%s%s",p->name,tmp?", ":"\n");
1003                 p = tmp;
1004         }
1005 
1006         /*
1007          *      Set the ARP module up
1008          */
1009 
1010         arp_init();
1011 
1012         /*
1013          *      Set the IP module up
1014          */
1015 
1016         ip_init();
1017 
1018         tcp_v4_init(&inet_family_ops);
1019 
1020         /* Setup TCP slab cache for open requests. */
1021         tcp_init();
1022 
1023 
1024         /*
1025          *      Set the ICMP layer up
1026          */
1027 
1028         icmp_init(&inet_family_ops);
1029 
1030         /* I wish inet_add_protocol had no constructor hook...
1031            I had to move IPIP from net/ipv4/protocol.c :-( --ANK
1032          */
1033 #ifdef CONFIG_NET_IPIP
1034         ipip_init();
1035 #endif
1036 #ifdef CONFIG_NET_IPGRE
1037         ipgre_init();
1038 #endif
1039 
1040         /*
1041          *      Initialise the multicast router
1042          */
1043 #if defined(CONFIG_IP_MROUTE)
1044         ip_mr_init();
1045 #endif
1046 
1047         /*
1048          *      Create all the /proc entries.
1049          */
1050 #ifdef CONFIG_PROC_FS
1051         proc_net_create ("raw", 0, raw_get_info);
1052         proc_net_create ("netstat", 0, netstat_get_info);
1053         proc_net_create ("snmp", 0, snmp_get_info);
1054         proc_net_create ("sockstat", 0, afinet_get_info);
1055         proc_net_create ("tcp", 0, tcp_get_info);
1056         proc_net_create ("udp", 0, udp_get_info);
1057 #endif          /* CONFIG_PROC_FS */
1058         return 0;
1059 }
1060 module_init(inet_init);
1061 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.