]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv4/af_inet.c
[TG3]: Fix bug in setting a tg3_flag
[net-next-2.6.git] / net / ipv4 / af_inet.c
CommitLineData
1da177e4
LT
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 *
02c30a84 10 * Authors: Ross Biro
1da177e4
LT
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>
1da177e4
LT
76#include <linux/module.h>
77#include <linux/sched.h>
78#include <linux/timer.h>
79#include <linux/string.h>
80#include <linux/sockios.h>
81#include <linux/net.h>
82#include <linux/fcntl.h>
83#include <linux/mm.h>
84#include <linux/interrupt.h>
85#include <linux/stat.h>
86#include <linux/init.h>
87#include <linux/poll.h>
88#include <linux/netfilter_ipv4.h>
89
90#include <asm/uaccess.h>
91#include <asm/system.h>
92
93#include <linux/smp_lock.h>
94#include <linux/inet.h>
95#include <linux/igmp.h>
96#include <linux/netdevice.h>
97#include <net/ip.h>
98#include <net/protocol.h>
99#include <net/arp.h>
100#include <net/route.h>
101#include <net/ip_fib.h>
295f7324 102#include <net/inet_connection_sock.h>
1da177e4
LT
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
116DEFINE_SNMP_STAT(struct linux_mib, net_statistics);
117
1da177e4
LT
118extern void ip_mc_drop_socket(struct sock *sk);
119
120/* The inetsw table contains everything that inet_create needs to
121 * build a new socket.
122 */
123static struct list_head inetsw[SOCK_MAX];
124static DEFINE_SPINLOCK(inetsw_lock);
125
126/* New destruction routine */
127
128void inet_sock_destruct(struct sock *sk)
129{
130 struct inet_sock *inet = inet_sk(sk);
131
132 __skb_queue_purge(&sk->sk_receive_queue);
133 __skb_queue_purge(&sk->sk_error_queue);
134
135 if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
136 printk("Attempt to release TCP socket in state %d %p\n",
137 sk->sk_state, sk);
138 return;
139 }
140 if (!sock_flag(sk, SOCK_DEAD)) {
141 printk("Attempt to release alive inet socket %p\n", sk);
142 return;
143 }
144
145 BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
146 BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
147 BUG_TRAP(!sk->sk_wmem_queued);
148 BUG_TRAP(!sk->sk_forward_alloc);
149
150 if (inet->opt)
151 kfree(inet->opt);
152 dst_release(sk->sk_dst_cache);
e6848976 153 sk_refcnt_debug_dec(sk);
1da177e4
LT
154}
155
156/*
157 * The routines beyond this point handle the behaviour of an AF_INET
158 * socket object. Mostly it punts to the subprotocols of IP to do
159 * the work.
160 */
161
162/*
163 * Automatically bind an unbound socket.
164 */
165
166static int inet_autobind(struct sock *sk)
167{
168 struct inet_sock *inet;
169 /* We may need to bind the socket. */
170 lock_sock(sk);
171 inet = inet_sk(sk);
172 if (!inet->num) {
173 if (sk->sk_prot->get_port(sk, 0)) {
174 release_sock(sk);
175 return -EAGAIN;
176 }
177 inet->sport = htons(inet->num);
178 }
179 release_sock(sk);
180 return 0;
181}
182
183/*
184 * Move a socket into listening state.
185 */
186int inet_listen(struct socket *sock, int backlog)
187{
188 struct sock *sk = sock->sk;
189 unsigned char old_state;
190 int err;
191
192 lock_sock(sk);
193
194 err = -EINVAL;
195 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
196 goto out;
197
198 old_state = sk->sk_state;
199 if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
200 goto out;
201
202 /* Really, if the socket is already in listen state
203 * we can only allow the backlog to be adjusted.
204 */
205 if (old_state != TCP_LISTEN) {
0a5578cf 206 err = inet_csk_listen_start(sk, TCP_SYNQ_HSIZE);
1da177e4
LT
207 if (err)
208 goto out;
209 }
210 sk->sk_max_ack_backlog = backlog;
211 err = 0;
212
213out:
214 release_sock(sk);
215 return err;
216}
217
218/*
219 * Create an inet socket.
220 */
221
222static int inet_create(struct socket *sock, int protocol)
223{
224 struct sock *sk;
225 struct list_head *p;
226 struct inet_protosw *answer;
227 struct inet_sock *inet;
228 struct proto *answer_prot;
229 unsigned char answer_flags;
230 char answer_no_check;
231 int err;
232
233 sock->state = SS_UNCONNECTED;
234
235 /* Look for the requested type/protocol pair. */
236 answer = NULL;
237 rcu_read_lock();
238 list_for_each_rcu(p, &inetsw[sock->type]) {
239 answer = list_entry(p, struct inet_protosw, list);
240
241 /* Check the non-wild match. */
242 if (protocol == answer->protocol) {
243 if (protocol != IPPROTO_IP)
244 break;
245 } else {
246 /* Check for the two wild cases. */
247 if (IPPROTO_IP == protocol) {
248 protocol = answer->protocol;
249 break;
250 }
251 if (IPPROTO_IP == answer->protocol)
252 break;
253 }
254 answer = NULL;
255 }
256
257 err = -ESOCKTNOSUPPORT;
258 if (!answer)
259 goto out_rcu_unlock;
260 err = -EPERM;
261 if (answer->capability > 0 && !capable(answer->capability))
262 goto out_rcu_unlock;
263 err = -EPROTONOSUPPORT;
264 if (!protocol)
265 goto out_rcu_unlock;
266
267 sock->ops = answer->ops;
268 answer_prot = answer->prot;
269 answer_no_check = answer->no_check;
270 answer_flags = answer->flags;
271 rcu_read_unlock();
272
273 BUG_TRAP(answer_prot->slab != NULL);
274
275 err = -ENOBUFS;
276 sk = sk_alloc(PF_INET, GFP_KERNEL, answer_prot, 1);
277 if (sk == NULL)
278 goto out;
279
280 err = 0;
281 sk->sk_no_check = answer_no_check;
282 if (INET_PROTOSW_REUSE & answer_flags)
283 sk->sk_reuse = 1;
284
285 inet = inet_sk(sk);
286
287 if (SOCK_RAW == sock->type) {
288 inet->num = protocol;
289 if (IPPROTO_RAW == protocol)
290 inet->hdrincl = 1;
291 }
292
293 if (ipv4_config.no_pmtu_disc)
294 inet->pmtudisc = IP_PMTUDISC_DONT;
295 else
296 inet->pmtudisc = IP_PMTUDISC_WANT;
297
298 inet->id = 0;
299
300 sock_init_data(sock, sk);
301
302 sk->sk_destruct = inet_sock_destruct;
303 sk->sk_family = PF_INET;
304 sk->sk_protocol = protocol;
305 sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
306
307 inet->uc_ttl = -1;
308 inet->mc_loop = 1;
309 inet->mc_ttl = 1;
310 inet->mc_index = 0;
311 inet->mc_list = NULL;
312
e6848976 313 sk_refcnt_debug_inc(sk);
1da177e4
LT
314
315 if (inet->num) {
316 /* It assumes that any protocol which allows
317 * the user to assign a number at socket
318 * creation time automatically
319 * shares.
320 */
321 inet->sport = htons(inet->num);
322 /* Add to protocol hash chains. */
323 sk->sk_prot->hash(sk);
324 }
325
326 if (sk->sk_prot->init) {
327 err = sk->sk_prot->init(sk);
328 if (err)
329 sk_common_release(sk);
330 }
331out:
332 return err;
333out_rcu_unlock:
334 rcu_read_unlock();
335 goto out;
336}
337
338
339/*
340 * The peer socket should always be NULL (or else). When we call this
341 * function we are destroying the object and from then on nobody
342 * should refer to it.
343 */
344int inet_release(struct socket *sock)
345{
346 struct sock *sk = sock->sk;
347
348 if (sk) {
349 long timeout;
350
351 /* Applications forget to leave groups before exiting */
352 ip_mc_drop_socket(sk);
353
354 /* If linger is set, we don't return until the close
355 * is complete. Otherwise we return immediately. The
356 * actually closing is done the same either way.
357 *
358 * If the close is due to the process exiting, we never
359 * linger..
360 */
361 timeout = 0;
362 if (sock_flag(sk, SOCK_LINGER) &&
363 !(current->flags & PF_EXITING))
364 timeout = sk->sk_lingertime;
365 sock->sk = NULL;
366 sk->sk_prot->close(sk, timeout);
367 }
368 return 0;
369}
370
371/* It is off by default, see below. */
372int sysctl_ip_nonlocal_bind;
373
374int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
375{
376 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
377 struct sock *sk = sock->sk;
378 struct inet_sock *inet = inet_sk(sk);
379 unsigned short snum;
380 int chk_addr_ret;
381 int err;
382
383 /* If the socket has its own bind function then use it. (RAW) */
384 if (sk->sk_prot->bind) {
385 err = sk->sk_prot->bind(sk, uaddr, addr_len);
386 goto out;
387 }
388 err = -EINVAL;
389 if (addr_len < sizeof(struct sockaddr_in))
390 goto out;
391
392 chk_addr_ret = inet_addr_type(addr->sin_addr.s_addr);
393
394 /* Not specified by any standard per-se, however it breaks too
395 * many applications when removed. It is unfortunate since
396 * allowing applications to make a non-local bind solves
397 * several problems with systems using dynamic addressing.
398 * (ie. your servers still start up even if your ISDN link
399 * is temporarily down)
400 */
401 err = -EADDRNOTAVAIL;
402 if (!sysctl_ip_nonlocal_bind &&
403 !inet->freebind &&
404 addr->sin_addr.s_addr != INADDR_ANY &&
405 chk_addr_ret != RTN_LOCAL &&
406 chk_addr_ret != RTN_MULTICAST &&
407 chk_addr_ret != RTN_BROADCAST)
408 goto out;
409
410 snum = ntohs(addr->sin_port);
411 err = -EACCES;
412 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
413 goto out;
414
415 /* We keep a pair of addresses. rcv_saddr is the one
416 * used by hash lookups, and saddr is used for transmit.
417 *
418 * In the BSD API these are the same except where it
419 * would be illegal to use them (multicast/broadcast) in
420 * which case the sending device address is used.
421 */
422 lock_sock(sk);
423
424 /* Check these errors (active socket, double bind). */
425 err = -EINVAL;
426 if (sk->sk_state != TCP_CLOSE || inet->num)
427 goto out_release_sock;
428
429 inet->rcv_saddr = inet->saddr = addr->sin_addr.s_addr;
430 if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
431 inet->saddr = 0; /* Use device */
432
433 /* Make sure we are allowed to bind here. */
434 if (sk->sk_prot->get_port(sk, snum)) {
435 inet->saddr = inet->rcv_saddr = 0;
436 err = -EADDRINUSE;
437 goto out_release_sock;
438 }
439
440 if (inet->rcv_saddr)
441 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
442 if (snum)
443 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
444 inet->sport = htons(inet->num);
445 inet->daddr = 0;
446 inet->dport = 0;
447 sk_dst_reset(sk);
448 err = 0;
449out_release_sock:
450 release_sock(sk);
451out:
452 return err;
453}
454
455int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
456 int addr_len, int flags)
457{
458 struct sock *sk = sock->sk;
459
460 if (uaddr->sa_family == AF_UNSPEC)
461 return sk->sk_prot->disconnect(sk, flags);
462
463 if (!inet_sk(sk)->num && inet_autobind(sk))
464 return -EAGAIN;
465 return sk->sk_prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
466}
467
468static long inet_wait_for_connect(struct sock *sk, long timeo)
469{
470 DEFINE_WAIT(wait);
471
472 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
473
474 /* Basic assumption: if someone sets sk->sk_err, he _must_
475 * change state of the socket from TCP_SYN_*.
476 * Connect() does not allow to get error notifications
477 * without closing the socket.
478 */
479 while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
480 release_sock(sk);
481 timeo = schedule_timeout(timeo);
482 lock_sock(sk);
483 if (signal_pending(current) || !timeo)
484 break;
485 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
486 }
487 finish_wait(sk->sk_sleep, &wait);
488 return timeo;
489}
490
491/*
492 * Connect to a remote host. There is regrettably still a little
493 * TCP 'magic' in here.
494 */
495int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
496 int addr_len, int flags)
497{
498 struct sock *sk = sock->sk;
499 int err;
500 long timeo;
501
502 lock_sock(sk);
503
504 if (uaddr->sa_family == AF_UNSPEC) {
505 err = sk->sk_prot->disconnect(sk, flags);
506 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
507 goto out;
508 }
509
510 switch (sock->state) {
511 default:
512 err = -EINVAL;
513 goto out;
514 case SS_CONNECTED:
515 err = -EISCONN;
516 goto out;
517 case SS_CONNECTING:
518 err = -EALREADY;
519 /* Fall out of switch with err, set for this state */
520 break;
521 case SS_UNCONNECTED:
522 err = -EISCONN;
523 if (sk->sk_state != TCP_CLOSE)
524 goto out;
525
526 err = sk->sk_prot->connect(sk, uaddr, addr_len);
527 if (err < 0)
528 goto out;
529
530 sock->state = SS_CONNECTING;
531
532 /* Just entered SS_CONNECTING state; the only
533 * difference is that return value in non-blocking
534 * case is EINPROGRESS, rather than EALREADY.
535 */
536 err = -EINPROGRESS;
537 break;
538 }
539
540 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
541
542 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
543 /* Error code is set above */
544 if (!timeo || !inet_wait_for_connect(sk, timeo))
545 goto out;
546
547 err = sock_intr_errno(timeo);
548 if (signal_pending(current))
549 goto out;
550 }
551
552 /* Connection was closed by RST, timeout, ICMP error
553 * or another process disconnected us.
554 */
555 if (sk->sk_state == TCP_CLOSE)
556 goto sock_error;
557
558 /* sk->sk_err may be not zero now, if RECVERR was ordered by user
559 * and error was received after socket entered established state.
560 * Hence, it is handled normally after connect() return successfully.
561 */
562
563 sock->state = SS_CONNECTED;
564 err = 0;
565out:
566 release_sock(sk);
567 return err;
568
569sock_error:
570 err = sock_error(sk) ? : -ECONNABORTED;
571 sock->state = SS_UNCONNECTED;
572 if (sk->sk_prot->disconnect(sk, flags))
573 sock->state = SS_DISCONNECTING;
574 goto out;
575}
576
577/*
578 * Accept a pending connection. The TCP layer now gives BSD semantics.
579 */
580
581int inet_accept(struct socket *sock, struct socket *newsock, int flags)
582{
583 struct sock *sk1 = sock->sk;
584 int err = -EINVAL;
585 struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err);
586
587 if (!sk2)
588 goto do_err;
589
590 lock_sock(sk2);
591
592 BUG_TRAP((1 << sk2->sk_state) &
593 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_CLOSE));
594
595 sock_graft(sk2, newsock);
596
597 newsock->state = SS_CONNECTED;
598 err = 0;
599 release_sock(sk2);
600do_err:
601 return err;
602}
603
604
605/*
606 * This does both peername and sockname.
607 */
608int inet_getname(struct socket *sock, struct sockaddr *uaddr,
609 int *uaddr_len, int peer)
610{
611 struct sock *sk = sock->sk;
612 struct inet_sock *inet = inet_sk(sk);
613 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
614
615 sin->sin_family = AF_INET;
616 if (peer) {
617 if (!inet->dport ||
618 (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
619 peer == 1))
620 return -ENOTCONN;
621 sin->sin_port = inet->dport;
622 sin->sin_addr.s_addr = inet->daddr;
623 } else {
624 __u32 addr = inet->rcv_saddr;
625 if (!addr)
626 addr = inet->saddr;
627 sin->sin_port = inet->sport;
628 sin->sin_addr.s_addr = addr;
629 }
630 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
631 *uaddr_len = sizeof(*sin);
632 return 0;
633}
634
635int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
636 size_t size)
637{
638 struct sock *sk = sock->sk;
639
640 /* We may need to bind the socket. */
641 if (!inet_sk(sk)->num && inet_autobind(sk))
642 return -EAGAIN;
643
644 return sk->sk_prot->sendmsg(iocb, sk, msg, size);
645}
646
647
648static ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
649{
650 struct sock *sk = sock->sk;
651
652 /* We may need to bind the socket. */
653 if (!inet_sk(sk)->num && inet_autobind(sk))
654 return -EAGAIN;
655
656 if (sk->sk_prot->sendpage)
657 return sk->sk_prot->sendpage(sk, page, offset, size, flags);
658 return sock_no_sendpage(sock, page, offset, size, flags);
659}
660
661
662int inet_shutdown(struct socket *sock, int how)
663{
664 struct sock *sk = sock->sk;
665 int err = 0;
666
667 /* This should really check to make sure
668 * the socket is a TCP socket. (WHY AC...)
669 */
670 how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
671 1->2 bit 2 snds.
672 2->3 */
673 if ((how & ~SHUTDOWN_MASK) || !how) /* MAXINT->0 */
674 return -EINVAL;
675
676 lock_sock(sk);
677 if (sock->state == SS_CONNECTING) {
678 if ((1 << sk->sk_state) &
679 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
680 sock->state = SS_DISCONNECTING;
681 else
682 sock->state = SS_CONNECTED;
683 }
684
685 switch (sk->sk_state) {
686 case TCP_CLOSE:
687 err = -ENOTCONN;
688 /* Hack to wake up other listeners, who can poll for
689 POLLHUP, even on eg. unconnected UDP sockets -- RR */
690 default:
691 sk->sk_shutdown |= how;
692 if (sk->sk_prot->shutdown)
693 sk->sk_prot->shutdown(sk, how);
694 break;
695
696 /* Remaining two branches are temporary solution for missing
697 * close() in multithreaded environment. It is _not_ a good idea,
698 * but we have no choice until close() is repaired at VFS level.
699 */
700 case TCP_LISTEN:
701 if (!(how & RCV_SHUTDOWN))
702 break;
703 /* Fall through */
704 case TCP_SYN_SENT:
705 err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
706 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
707 break;
708 }
709
710 /* Wake up anyone sleeping in poll. */
711 sk->sk_state_change(sk);
712 release_sock(sk);
713 return err;
714}
715
716/*
717 * ioctl() calls you can issue on an INET socket. Most of these are
718 * device configuration and stuff and very rarely used. Some ioctls
719 * pass on to the socket itself.
720 *
721 * NOTE: I like the idea of a module for the config stuff. ie ifconfig
722 * loads the devconfigure module does its configuring and unloads it.
723 * There's a good 20K of config code hanging around the kernel.
724 */
725
726int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
727{
728 struct sock *sk = sock->sk;
729 int err = 0;
730
731 switch (cmd) {
732 case SIOCGSTAMP:
733 err = sock_get_timestamp(sk, (struct timeval __user *)arg);
734 break;
735 case SIOCADDRT:
736 case SIOCDELRT:
737 case SIOCRTMSG:
738 err = ip_rt_ioctl(cmd, (void __user *)arg);
739 break;
740 case SIOCDARP:
741 case SIOCGARP:
742 case SIOCSARP:
743 err = arp_ioctl(cmd, (void __user *)arg);
744 break;
745 case SIOCGIFADDR:
746 case SIOCSIFADDR:
747 case SIOCGIFBRDADDR:
748 case SIOCSIFBRDADDR:
749 case SIOCGIFNETMASK:
750 case SIOCSIFNETMASK:
751 case SIOCGIFDSTADDR:
752 case SIOCSIFDSTADDR:
753 case SIOCSIFPFLAGS:
754 case SIOCGIFPFLAGS:
755 case SIOCSIFFLAGS:
756 err = devinet_ioctl(cmd, (void __user *)arg);
757 break;
758 default:
759 if (!sk->sk_prot->ioctl ||
760 (err = sk->sk_prot->ioctl(sk, cmd, arg)) ==
761 -ENOIOCTLCMD)
762 err = dev_ioctl(cmd, (void __user *)arg);
763 break;
764 }
765 return err;
766}
767
768struct proto_ops inet_stream_ops = {
769 .family = PF_INET,
770 .owner = THIS_MODULE,
771 .release = inet_release,
772 .bind = inet_bind,
773 .connect = inet_stream_connect,
774 .socketpair = sock_no_socketpair,
775 .accept = inet_accept,
776 .getname = inet_getname,
777 .poll = tcp_poll,
778 .ioctl = inet_ioctl,
779 .listen = inet_listen,
780 .shutdown = inet_shutdown,
781 .setsockopt = sock_common_setsockopt,
782 .getsockopt = sock_common_getsockopt,
783 .sendmsg = inet_sendmsg,
784 .recvmsg = sock_common_recvmsg,
785 .mmap = sock_no_mmap,
786 .sendpage = tcp_sendpage
787};
788
789struct proto_ops inet_dgram_ops = {
790 .family = PF_INET,
791 .owner = THIS_MODULE,
792 .release = inet_release,
793 .bind = inet_bind,
794 .connect = inet_dgram_connect,
795 .socketpair = sock_no_socketpair,
796 .accept = sock_no_accept,
797 .getname = inet_getname,
798 .poll = udp_poll,
799 .ioctl = inet_ioctl,
800 .listen = sock_no_listen,
801 .shutdown = inet_shutdown,
802 .setsockopt = sock_common_setsockopt,
803 .getsockopt = sock_common_getsockopt,
804 .sendmsg = inet_sendmsg,
805 .recvmsg = sock_common_recvmsg,
806 .mmap = sock_no_mmap,
807 .sendpage = inet_sendpage,
808};
809
810/*
811 * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without
812 * udp_poll
813 */
814static struct proto_ops inet_sockraw_ops = {
815 .family = PF_INET,
816 .owner = THIS_MODULE,
817 .release = inet_release,
818 .bind = inet_bind,
819 .connect = inet_dgram_connect,
820 .socketpair = sock_no_socketpair,
821 .accept = sock_no_accept,
822 .getname = inet_getname,
823 .poll = datagram_poll,
824 .ioctl = inet_ioctl,
825 .listen = sock_no_listen,
826 .shutdown = inet_shutdown,
827 .setsockopt = sock_common_setsockopt,
828 .getsockopt = sock_common_getsockopt,
829 .sendmsg = inet_sendmsg,
830 .recvmsg = sock_common_recvmsg,
831 .mmap = sock_no_mmap,
832 .sendpage = inet_sendpage,
833};
834
835static struct net_proto_family inet_family_ops = {
836 .family = PF_INET,
837 .create = inet_create,
838 .owner = THIS_MODULE,
839};
840
841
842extern void tcp_init(void);
843extern void tcp_v4_init(struct net_proto_family *);
844
845/* Upon startup we insert all the elements in inetsw_array[] into
846 * the linked list inetsw.
847 */
848static struct inet_protosw inetsw_array[] =
849{
850 {
851 .type = SOCK_STREAM,
852 .protocol = IPPROTO_TCP,
853 .prot = &tcp_prot,
854 .ops = &inet_stream_ops,
855 .capability = -1,
856 .no_check = 0,
857 .flags = INET_PROTOSW_PERMANENT,
858 },
859
860 {
861 .type = SOCK_DGRAM,
862 .protocol = IPPROTO_UDP,
863 .prot = &udp_prot,
864 .ops = &inet_dgram_ops,
865 .capability = -1,
866 .no_check = UDP_CSUM_DEFAULT,
867 .flags = INET_PROTOSW_PERMANENT,
868 },
869
870
871 {
872 .type = SOCK_RAW,
873 .protocol = IPPROTO_IP, /* wild card */
874 .prot = &raw_prot,
875 .ops = &inet_sockraw_ops,
876 .capability = CAP_NET_RAW,
877 .no_check = UDP_CSUM_DEFAULT,
878 .flags = INET_PROTOSW_REUSE,
879 }
880};
881
882#define INETSW_ARRAY_LEN (sizeof(inetsw_array) / sizeof(struct inet_protosw))
883
884void inet_register_protosw(struct inet_protosw *p)
885{
886 struct list_head *lh;
887 struct inet_protosw *answer;
888 int protocol = p->protocol;
889 struct list_head *last_perm;
890
891 spin_lock_bh(&inetsw_lock);
892
893 if (p->type >= SOCK_MAX)
894 goto out_illegal;
895
896 /* If we are trying to override a permanent protocol, bail. */
897 answer = NULL;
898 last_perm = &inetsw[p->type];
899 list_for_each(lh, &inetsw[p->type]) {
900 answer = list_entry(lh, struct inet_protosw, list);
901
902 /* Check only the non-wild match. */
903 if (INET_PROTOSW_PERMANENT & answer->flags) {
904 if (protocol == answer->protocol)
905 break;
906 last_perm = lh;
907 }
908
909 answer = NULL;
910 }
911 if (answer)
912 goto out_permanent;
913
914 /* Add the new entry after the last permanent entry if any, so that
915 * the new entry does not override a permanent entry when matched with
916 * a wild-card protocol. But it is allowed to override any existing
917 * non-permanent entry. This means that when we remove this entry, the
918 * system automatically returns to the old behavior.
919 */
920 list_add_rcu(&p->list, last_perm);
921out:
922 spin_unlock_bh(&inetsw_lock);
923
924 synchronize_net();
925
926 return;
927
928out_permanent:
929 printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
930 protocol);
931 goto out;
932
933out_illegal:
934 printk(KERN_ERR
935 "Ignoring attempt to register invalid socket type %d.\n",
936 p->type);
937 goto out;
938}
939
940void inet_unregister_protosw(struct inet_protosw *p)
941{
942 if (INET_PROTOSW_PERMANENT & p->flags) {
943 printk(KERN_ERR
944 "Attempt to unregister permanent protocol %d.\n",
945 p->protocol);
946 } else {
947 spin_lock_bh(&inetsw_lock);
948 list_del_rcu(&p->list);
949 spin_unlock_bh(&inetsw_lock);
950
951 synchronize_net();
952 }
953}
954
32519f11
ACM
955/*
956 * Shall we try to damage output packets if routing dev changes?
957 */
958
959int sysctl_ip_dynaddr;
960
961static int inet_sk_reselect_saddr(struct sock *sk)
962{
963 struct inet_sock *inet = inet_sk(sk);
964 int err;
965 struct rtable *rt;
966 __u32 old_saddr = inet->saddr;
967 __u32 new_saddr;
968 __u32 daddr = inet->daddr;
969
970 if (inet->opt && inet->opt->srr)
971 daddr = inet->opt->faddr;
972
973 /* Query new route. */
974 err = ip_route_connect(&rt, daddr, 0,
975 RT_CONN_FLAGS(sk),
976 sk->sk_bound_dev_if,
977 sk->sk_protocol,
978 inet->sport, inet->dport, sk);
979 if (err)
980 return err;
981
982 sk_setup_caps(sk, &rt->u.dst);
983
984 new_saddr = rt->rt_src;
985
986 if (new_saddr == old_saddr)
987 return 0;
988
989 if (sysctl_ip_dynaddr > 1) {
990 printk(KERN_INFO "%s(): shifting inet->"
991 "saddr from %d.%d.%d.%d to %d.%d.%d.%d\n",
992 __FUNCTION__,
993 NIPQUAD(old_saddr),
994 NIPQUAD(new_saddr));
995 }
996
997 inet->saddr = inet->rcv_saddr = new_saddr;
998
999 /*
1000 * XXX The only one ugly spot where we need to
1001 * XXX really change the sockets identity after
1002 * XXX it has entered the hashes. -DaveM
1003 *
1004 * Besides that, it does not check for connection
1005 * uniqueness. Wait for troubles.
1006 */
1007 __sk_prot_rehash(sk);
1008 return 0;
1009}
1010
1011int inet_sk_rebuild_header(struct sock *sk)
1012{
1013 struct inet_sock *inet = inet_sk(sk);
1014 struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0);
1015 u32 daddr;
1016 int err;
1017
1018 /* Route is OK, nothing to do. */
1019 if (rt)
1020 return 0;
1021
1022 /* Reroute. */
1023 daddr = inet->daddr;
1024 if (inet->opt && inet->opt->srr)
1025 daddr = inet->opt->faddr;
1026{
1027 struct flowi fl = {
1028 .oif = sk->sk_bound_dev_if,
1029 .nl_u = {
1030 .ip4_u = {
1031 .daddr = daddr,
1032 .saddr = inet->saddr,
1033 .tos = RT_CONN_FLAGS(sk),
1034 },
1035 },
1036 .proto = sk->sk_protocol,
1037 .uli_u = {
1038 .ports = {
1039 .sport = inet->sport,
1040 .dport = inet->dport,
1041 },
1042 },
1043 };
1044
1045 err = ip_route_output_flow(&rt, &fl, sk, 0);
1046}
1047 if (!err)
1048 sk_setup_caps(sk, &rt->u.dst);
1049 else {
1050 /* Routing failed... */
1051 sk->sk_route_caps = 0;
1052 /*
1053 * Other protocols have to map its equivalent state to TCP_SYN_SENT.
1054 * DCCP maps its DCCP_REQUESTING state to TCP_SYN_SENT. -acme
1055 */
1056 if (!sysctl_ip_dynaddr ||
1057 sk->sk_state != TCP_SYN_SENT ||
1058 (sk->sk_userlocks & SOCK_BINDADDR_LOCK) ||
1059 (err = inet_sk_reselect_saddr(sk)) != 0)
1060 sk->sk_err_soft = -err;
1061 }
1062
1063 return err;
1064}
1065
1066EXPORT_SYMBOL(inet_sk_rebuild_header);
1067
1da177e4
LT
1068#ifdef CONFIG_IP_MULTICAST
1069static struct net_protocol igmp_protocol = {
1070 .handler = igmp_rcv,
1071};
1072#endif
1073
1074static struct net_protocol tcp_protocol = {
1075 .handler = tcp_v4_rcv,
1076 .err_handler = tcp_v4_err,
1077 .no_policy = 1,
1078};
1079
1080static struct net_protocol udp_protocol = {
1081 .handler = udp_rcv,
1082 .err_handler = udp_err,
1083 .no_policy = 1,
1084};
1085
1086static struct net_protocol icmp_protocol = {
1087 .handler = icmp_rcv,
1088};
1089
1090static int __init init_ipv4_mibs(void)
1091{
1092 net_statistics[0] = alloc_percpu(struct linux_mib);
1093 net_statistics[1] = alloc_percpu(struct linux_mib);
1094 ip_statistics[0] = alloc_percpu(struct ipstats_mib);
1095 ip_statistics[1] = alloc_percpu(struct ipstats_mib);
1096 icmp_statistics[0] = alloc_percpu(struct icmp_mib);
1097 icmp_statistics[1] = alloc_percpu(struct icmp_mib);
1098 tcp_statistics[0] = alloc_percpu(struct tcp_mib);
1099 tcp_statistics[1] = alloc_percpu(struct tcp_mib);
1100 udp_statistics[0] = alloc_percpu(struct udp_mib);
1101 udp_statistics[1] = alloc_percpu(struct udp_mib);
1102 if (!
1103 (net_statistics[0] && net_statistics[1] && ip_statistics[0]
1104 && ip_statistics[1] && tcp_statistics[0] && tcp_statistics[1]
1105 && udp_statistics[0] && udp_statistics[1]))
1106 return -ENOMEM;
1107
1108 (void) tcp_mib_init();
1109
1110 return 0;
1111}
1112
1113static int ipv4_proc_init(void);
1114extern void ipfrag_init(void);
1115
30e224d7
HX
1116/*
1117 * IP protocol layer initialiser
1118 */
1119
1120static struct packet_type ip_packet_type = {
1121 .type = __constant_htons(ETH_P_IP),
1122 .func = ip_rcv,
1123};
1124
1da177e4
LT
1125static int __init inet_init(void)
1126{
1127 struct sk_buff *dummy_skb;
1128 struct inet_protosw *q;
1129 struct list_head *r;
1130 int rc = -EINVAL;
1131
1132 if (sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb)) {
1133 printk(KERN_CRIT "%s: panic\n", __FUNCTION__);
1134 goto out;
1135 }
1136
1137 rc = proto_register(&tcp_prot, 1);
1138 if (rc)
1139 goto out;
1140
1141 rc = proto_register(&udp_prot, 1);
1142 if (rc)
1143 goto out_unregister_tcp_proto;
1144
1145 rc = proto_register(&raw_prot, 1);
1146 if (rc)
1147 goto out_unregister_udp_proto;
1148
1149 /*
1150 * Tell SOCKET that we are alive...
1151 */
1152
1153 (void)sock_register(&inet_family_ops);
1154
1155 /*
1156 * Add all the base protocols.
1157 */
1158
1159 if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
1160 printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
1161 if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
1162 printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
1163 if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
1164 printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n");
1165#ifdef CONFIG_IP_MULTICAST
1166 if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
1167 printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n");
1168#endif
1169
1170 /* Register the socket-side information for inet_create. */
1171 for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
1172 INIT_LIST_HEAD(r);
1173
1174 for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
1175 inet_register_protosw(q);
1176
1177 /*
1178 * Set the ARP module up
1179 */
1180
1181 arp_init();
1182
1183 /*
1184 * Set the IP module up
1185 */
1186
1187 ip_init();
1188
1189 tcp_v4_init(&inet_family_ops);
1190
1191 /* Setup TCP slab cache for open requests. */
1192 tcp_init();
1193
1194
1195 /*
1196 * Set the ICMP layer up
1197 */
1198
1199 icmp_init(&inet_family_ops);
1200
1201 /*
1202 * Initialise the multicast router
1203 */
1204#if defined(CONFIG_IP_MROUTE)
1205 ip_mr_init();
1206#endif
1207 /*
1208 * Initialise per-cpu ipv4 mibs
1209 */
1210
1211 if(init_ipv4_mibs())
1212 printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n"); ;
1213
1214 ipv4_proc_init();
1215
1216 ipfrag_init();
1217
30e224d7
HX
1218 dev_add_pack(&ip_packet_type);
1219
1da177e4
LT
1220 rc = 0;
1221out:
1222 return rc;
1223out_unregister_tcp_proto:
1224 proto_unregister(&tcp_prot);
1225out_unregister_udp_proto:
1226 proto_unregister(&udp_prot);
1227 goto out;
1228}
1229
1230module_init(inet_init);
1231
1232/* ------------------------------------------------------------------------ */
1233
1234#ifdef CONFIG_PROC_FS
1235extern int fib_proc_init(void);
1236extern void fib_proc_exit(void);
19baf839
RO
1237#ifdef CONFIG_IP_FIB_TRIE
1238extern int fib_stat_proc_init(void);
1239extern void fib_stat_proc_exit(void);
1240#endif
1da177e4
LT
1241extern int ip_misc_proc_init(void);
1242extern int raw_proc_init(void);
1243extern void raw_proc_exit(void);
1244extern int tcp4_proc_init(void);
1245extern void tcp4_proc_exit(void);
1246extern int udp4_proc_init(void);
1247extern void udp4_proc_exit(void);
1248
1249static int __init ipv4_proc_init(void)
1250{
1251 int rc = 0;
1252
1253 if (raw_proc_init())
1254 goto out_raw;
1255 if (tcp4_proc_init())
1256 goto out_tcp;
1257 if (udp4_proc_init())
1258 goto out_udp;
1259 if (fib_proc_init())
1260 goto out_fib;
19baf839
RO
1261#ifdef CONFIG_IP_FIB_TRIE
1262 if (fib_stat_proc_init())
1263 goto out_fib_stat;
c877efb2 1264#endif
1da177e4
LT
1265 if (ip_misc_proc_init())
1266 goto out_misc;
1267out:
1268 return rc;
1269out_misc:
19baf839
RO
1270#ifdef CONFIG_IP_FIB_TRIE
1271 fib_stat_proc_exit();
1272out_fib_stat:
1273#endif
1da177e4
LT
1274 fib_proc_exit();
1275out_fib:
1276 udp4_proc_exit();
1277out_udp:
1278 tcp4_proc_exit();
1279out_tcp:
1280 raw_proc_exit();
1281out_raw:
1282 rc = -ENOMEM;
1283 goto out;
1284}
1285
1286#else /* CONFIG_PROC_FS */
1287static int __init ipv4_proc_init(void)
1288{
1289 return 0;
1290}
1291#endif /* CONFIG_PROC_FS */
1292
1293MODULE_ALIAS_NETPROTO(PF_INET);
1294
1295EXPORT_SYMBOL(inet_accept);
1296EXPORT_SYMBOL(inet_bind);
1297EXPORT_SYMBOL(inet_dgram_connect);
1298EXPORT_SYMBOL(inet_dgram_ops);
1299EXPORT_SYMBOL(inet_getname);
1300EXPORT_SYMBOL(inet_ioctl);
1301EXPORT_SYMBOL(inet_listen);
1302EXPORT_SYMBOL(inet_register_protosw);
1303EXPORT_SYMBOL(inet_release);
1304EXPORT_SYMBOL(inet_sendmsg);
1305EXPORT_SYMBOL(inet_shutdown);
1306EXPORT_SYMBOL(inet_sock_destruct);
1307EXPORT_SYMBOL(inet_stream_connect);
1308EXPORT_SYMBOL(inet_stream_ops);
1309EXPORT_SYMBOL(inet_unregister_protosw);
1310EXPORT_SYMBOL(net_statistics);
cdac4e07 1311EXPORT_SYMBOL(sysctl_ip_nonlocal_bind);