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