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 * Authors: Ross Biro
9 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
10 * Florian La Roche, <flla@stud.uni-sb.de>
11 * Alan Cox, <A.Cox@swansea.ac.uk>
12 *
13 * Changes (see also sock.c)
14 *
15 * piggy,
16 * Karl Knutson : Socket protocol table
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
23 * structures
24 * when accept() ed
25 * Alan Cox : Semantics of SO_LINGER aren't state
26 * moved to close when you look carefully.
27 * With this fixed and the accept bug fixed
28 * some RPC stuff seems happier.
29 * Niibe Yutaka : 4.4BSD style write async I/O
30 * Alan Cox,
31 * Tony Gale : Fixed reuse semantics.
32 * Alan Cox : bind() shouldn't abort existing but dead
33 * sockets. Stops FTP netin:.. I hope.
34 * Alan Cox : bind() works correctly for RAW sockets.
35 * Note that FreeBSD at least was broken
36 * in this respect so be careful with
37 * compatibility tests...
38 * Alan Cox : routing cache support
39 * Alan Cox : memzero the socket structure for
40 * compactness.
41 * Matt Day : nonblock connect error handler
42 * Alan Cox : Allow large numbers of pending sockets
43 * (eg for big web sites), but only if
44 * specifically application requested.
45 * Alan Cox : New buffering throughout IP. Used
46 * dumbly.
47 * Alan Cox : New buffering now used smartly.
48 * Alan Cox : BSD rather than common sense
49 * interpretation of listen.
50 * Germano Caronni : Assorted small races.
51 * Alan Cox : sendmsg/recvmsg basic support.
52 * Alan Cox : Only sendmsg/recvmsg now supported.
53 * Alan Cox : Locked down bind (see security list).
54 * Alan Cox : Loosened bind a little.
55 * Mike McLagan : ADD/DEL DLCI Ioctls
56 * Willy Konynenberg : Transparent proxying support.
57 * David S. Miller : New socket lookup architecture.
58 * Some other random speedups.
59 * Cyrus Durgin : Cleaned up file for kmod hacks.
60 * Andi Kleen : Fix inet_stream_connect TCP race.
61 *
62 * This program is free software; you can redistribute it and/or
63 * modify it under the terms of the GNU General Public License
64 * as published by the Free Software Foundation; either version
65 * 2 of the License, or (at your option) any later version.
66 */
67
68 #include <linux/err.h>
69 #include <linux/errno.h>
70 #include <linux/types.h>
71 #include <linux/socket.h>
72 #include <linux/in.h>
73 #include <linux/kernel.h>
74 #include <linux/module.h>
75 #include <linux/sched.h>
76 #include <linux/timer.h>
77 #include <linux/string.h>
78 #include <linux/sockios.h>
79 #include <linux/net.h>
80 #include <linux/capability.h>
81 #include <linux/fcntl.h>
82 #include <linux/mm.h>
83 #include <linux/interrupt.h>
84 #include <linux/stat.h>
85 #include <linux/init.h>
86 #include <linux/poll.h>
87 #include <linux/netfilter_ipv4.h>
88 #include <linux/random.h>
89
90 #include <asm/uaccess.h>
91 #include <asm/system.h>
92
93 #include <linux/inet.h>
94 #include <linux/igmp.h>
95 #include <linux/inetdevice.h>
96 #include <linux/netdevice.h>
97 #include <net/checksum.h>
98 #include <net/ip.h>
99 #include <net/protocol.h>
100 #include <net/arp.h>
101 #include <net/route.h>
102 #include <net/ip_fib.h>
103 #include <net/inet_connection_sock.h>
104 #include <net/tcp.h>
105 #include <net/udp.h>
106 #include <net/udplite.h>
107 #include <linux/skbuff.h>
108 #include <net/sock.h>
109 #include <net/raw.h>
110 #include <net/icmp.h>
111 #include <net/ipip.h>
112 #include <net/inet_common.h>
113 #include <net/xfrm.h>
114 #include <net/net_namespace.h>
115 #ifdef CONFIG_IP_MROUTE
116 #include <linux/mroute.h>
117 #endif
118
119 #ifdef CONFIG_ANDROID_PARANOID_NETWORK
120 #include <linux/android_aid.h>
121
current_has_network(void)122 static inline int current_has_network(void)
123 {
124 return in_egroup_p(AID_INET) || capable(CAP_NET_RAW);
125 }
126 #else
current_has_network(void)127 static inline int current_has_network(void)
128 {
129 return 1;
130 }
131 #endif
132
133 extern void ip_mc_drop_socket(struct sock *sk);
134
135 /* The inetsw table contains everything that inet_create needs to
136 * build a new socket.
137 */
138 static struct list_head inetsw[SOCK_MAX];
139 static DEFINE_SPINLOCK(inetsw_lock);
140
141 struct ipv4_config ipv4_config;
142
143 EXPORT_SYMBOL(ipv4_config);
144
145 /* New destruction routine */
146
inet_sock_destruct(struct sock * sk)147 void inet_sock_destruct(struct sock *sk)
148 {
149 struct inet_sock *inet = inet_sk(sk);
150
151 __skb_queue_purge(&sk->sk_receive_queue);
152 __skb_queue_purge(&sk->sk_error_queue);
153
154 sk_mem_reclaim(sk);
155
156 if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
157 printk("Attempt to release TCP socket in state %d %p\n",
158 sk->sk_state, sk);
159 return;
160 }
161 if (!sock_flag(sk, SOCK_DEAD)) {
162 printk("Attempt to release alive inet socket %p\n", sk);
163 return;
164 }
165
166 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
167 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
168 WARN_ON(sk->sk_wmem_queued);
169 WARN_ON(sk->sk_forward_alloc);
170
171 kfree(inet->opt);
172 dst_release(sk->sk_dst_cache);
173 sk_refcnt_debug_dec(sk);
174 }
175
176 /*
177 * The routines beyond this point handle the behaviour of an AF_INET
178 * socket object. Mostly it punts to the subprotocols of IP to do
179 * the work.
180 */
181
182 /*
183 * Automatically bind an unbound socket.
184 */
185
inet_autobind(struct sock * sk)186 static int inet_autobind(struct sock *sk)
187 {
188 struct inet_sock *inet;
189 /* We may need to bind the socket. */
190 lock_sock(sk);
191 inet = inet_sk(sk);
192 if (!inet->num) {
193 if (sk->sk_prot->get_port(sk, 0)) {
194 release_sock(sk);
195 return -EAGAIN;
196 }
197 inet->sport = htons(inet->num);
198 }
199 release_sock(sk);
200 return 0;
201 }
202
203 /*
204 * Move a socket into listening state.
205 */
inet_listen(struct socket * sock,int backlog)206 int inet_listen(struct socket *sock, int backlog)
207 {
208 struct sock *sk = sock->sk;
209 unsigned char old_state;
210 int err;
211
212 lock_sock(sk);
213
214 err = -EINVAL;
215 if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
216 goto out;
217
218 old_state = sk->sk_state;
219 if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
220 goto out;
221
222 /* Really, if the socket is already in listen state
223 * we can only allow the backlog to be adjusted.
224 */
225 if (old_state != TCP_LISTEN) {
226 err = inet_csk_listen_start(sk, backlog);
227 if (err)
228 goto out;
229 }
230 sk->sk_max_ack_backlog = backlog;
231 err = 0;
232
233 out:
234 release_sock(sk);
235 return err;
236 }
237
238 u32 inet_ehash_secret __read_mostly;
239 EXPORT_SYMBOL(inet_ehash_secret);
240
241 /*
242 * inet_ehash_secret must be set exactly once
243 * Instead of using a dedicated spinlock, we (ab)use inetsw_lock
244 */
build_ehash_secret(void)245 void build_ehash_secret(void)
246 {
247 u32 rnd;
248 do {
249 get_random_bytes(&rnd, sizeof(rnd));
250 } while (rnd == 0);
251 spin_lock_bh(&inetsw_lock);
252 if (!inet_ehash_secret)
253 inet_ehash_secret = rnd;
254 spin_unlock_bh(&inetsw_lock);
255 }
256 EXPORT_SYMBOL(build_ehash_secret);
257
inet_netns_ok(struct net * net,int protocol)258 static inline int inet_netns_ok(struct net *net, int protocol)
259 {
260 int hash;
261 struct net_protocol *ipprot;
262
263 if (net_eq(net, &init_net))
264 return 1;
265
266 hash = protocol & (MAX_INET_PROTOS - 1);
267 ipprot = rcu_dereference(inet_protos[hash]);
268
269 if (ipprot == NULL)
270 /* raw IP is OK */
271 return 1;
272 return ipprot->netns_ok;
273 }
274
275
276 /*
277 * Create an inet socket.
278 */
279
inet_create(struct net * net,struct socket * sock,int protocol)280 static int inet_create(struct net *net, struct socket *sock, int protocol)
281 {
282 struct sock *sk;
283 struct inet_protosw *answer;
284 struct inet_sock *inet;
285 struct proto *answer_prot;
286 unsigned char answer_flags;
287 char answer_no_check;
288 int try_loading_module = 0;
289 int err;
290
291 if (!current_has_network())
292 return -EACCES;
293
294 if (unlikely(!inet_ehash_secret))
295 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
296 build_ehash_secret();
297
298 sock->state = SS_UNCONNECTED;
299
300 /* Look for the requested type/protocol pair. */
301 lookup_protocol:
302 err = -ESOCKTNOSUPPORT;
303 rcu_read_lock();
304 list_for_each_entry_rcu(answer, &inetsw[sock->type], list) {
305
306 err = 0;
307 /* Check the non-wild match. */
308 if (protocol == answer->protocol) {
309 if (protocol != IPPROTO_IP)
310 break;
311 } else {
312 /* Check for the two wild cases. */
313 if (IPPROTO_IP == protocol) {
314 protocol = answer->protocol;
315 break;
316 }
317 if (IPPROTO_IP == answer->protocol)
318 break;
319 }
320 err = -EPROTONOSUPPORT;
321 }
322
323 if (unlikely(err)) {
324 if (try_loading_module < 2) {
325 rcu_read_unlock();
326 /*
327 * Be more specific, e.g. net-pf-2-proto-132-type-1
328 * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM)
329 */
330 if (++try_loading_module == 1)
331 request_module("net-pf-%d-proto-%d-type-%d",
332 PF_INET, protocol, sock->type);
333 /*
334 * Fall back to generic, e.g. net-pf-2-proto-132
335 * (net-pf-PF_INET-proto-IPPROTO_SCTP)
336 */
337 else
338 request_module("net-pf-%d-proto-%d",
339 PF_INET, protocol);
340 goto lookup_protocol;
341 } else
342 goto out_rcu_unlock;
343 }
344
345 err = -EPERM;
346 if (answer->capability > 0 && !capable(answer->capability))
347 goto out_rcu_unlock;
348
349 err = -EAFNOSUPPORT;
350 if (!inet_netns_ok(net, protocol))
351 goto out_rcu_unlock;
352
353 sock->ops = answer->ops;
354 answer_prot = answer->prot;
355 answer_no_check = answer->no_check;
356 answer_flags = answer->flags;
357 rcu_read_unlock();
358
359 WARN_ON(answer_prot->slab == NULL);
360
361 err = -ENOBUFS;
362 sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot);
363 if (sk == NULL)
364 goto out;
365
366 err = 0;
367 sk->sk_no_check = answer_no_check;
368 if (INET_PROTOSW_REUSE & answer_flags)
369 sk->sk_reuse = 1;
370
371 inet = inet_sk(sk);
372 inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
373
374 if (SOCK_RAW == sock->type) {
375 inet->num = protocol;
376 if (IPPROTO_RAW == protocol)
377 inet->hdrincl = 1;
378 }
379
380 if (ipv4_config.no_pmtu_disc)
381 inet->pmtudisc = IP_PMTUDISC_DONT;
382 else
383 inet->pmtudisc = IP_PMTUDISC_WANT;
384
385 inet->id = 0;
386
387 sock_init_data(sock, sk);
388
389 sk->sk_destruct = inet_sock_destruct;
390 sk->sk_family = PF_INET;
391 sk->sk_protocol = protocol;
392 sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
393
394 inet->uc_ttl = -1;
395 inet->mc_loop = 1;
396 inet->mc_ttl = 1;
397 inet->mc_index = 0;
398 inet->mc_list = NULL;
399
400 sk_refcnt_debug_inc(sk);
401
402 if (inet->num) {
403 /* It assumes that any protocol which allows
404 * the user to assign a number at socket
405 * creation time automatically
406 * shares.
407 */
408 inet->sport = htons(inet->num);
409 /* Add to protocol hash chains. */
410 sk->sk_prot->hash(sk);
411 }
412
413 if (sk->sk_prot->init) {
414 err = sk->sk_prot->init(sk);
415 if (err)
416 sk_common_release(sk);
417 }
418 out:
419 return err;
420 out_rcu_unlock:
421 rcu_read_unlock();
422 goto out;
423 }
424
425
426 /*
427 * The peer socket should always be NULL (or else). When we call this
428 * function we are destroying the object and from then on nobody
429 * should refer to it.
430 */
inet_release(struct socket * sock)431 int inet_release(struct socket *sock)
432 {
433 struct sock *sk = sock->sk;
434
435 if (sk) {
436 long timeout;
437
438 /* Applications forget to leave groups before exiting */
439 ip_mc_drop_socket(sk);
440
441 /* If linger is set, we don't return until the close
442 * is complete. Otherwise we return immediately. The
443 * actually closing is done the same either way.
444 *
445 * If the close is due to the process exiting, we never
446 * linger..
447 */
448 timeout = 0;
449 if (sock_flag(sk, SOCK_LINGER) &&
450 !(current->flags & PF_EXITING))
451 timeout = sk->sk_lingertime;
452 sock->sk = NULL;
453 sk->sk_prot->close(sk, timeout);
454 }
455 return 0;
456 }
457
458 /* It is off by default, see below. */
459 int sysctl_ip_nonlocal_bind __read_mostly;
460
inet_bind(struct socket * sock,struct sockaddr * uaddr,int addr_len)461 int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
462 {
463 struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
464 struct sock *sk = sock->sk;
465 struct inet_sock *inet = inet_sk(sk);
466 unsigned short snum;
467 int chk_addr_ret;
468 int err;
469
470 /* If the socket has its own bind function then use it. (RAW) */
471 if (sk->sk_prot->bind) {
472 err = sk->sk_prot->bind(sk, uaddr, addr_len);
473 goto out;
474 }
475 err = -EINVAL;
476 if (addr_len < sizeof(struct sockaddr_in))
477 goto out;
478
479 chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
480
481 /* Not specified by any standard per-se, however it breaks too
482 * many applications when removed. It is unfortunate since
483 * allowing applications to make a non-local bind solves
484 * several problems with systems using dynamic addressing.
485 * (ie. your servers still start up even if your ISDN link
486 * is temporarily down)
487 */
488 err = -EADDRNOTAVAIL;
489 if (!sysctl_ip_nonlocal_bind &&
490 !(inet->freebind || inet->transparent) &&
491 addr->sin_addr.s_addr != htonl(INADDR_ANY) &&
492 chk_addr_ret != RTN_LOCAL &&
493 chk_addr_ret != RTN_MULTICAST &&
494 chk_addr_ret != RTN_BROADCAST)
495 goto out;
496
497 snum = ntohs(addr->sin_port);
498 err = -EACCES;
499 if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
500 goto out;
501
502 /* We keep a pair of addresses. rcv_saddr is the one
503 * used by hash lookups, and saddr is used for transmit.
504 *
505 * In the BSD API these are the same except where it
506 * would be illegal to use them (multicast/broadcast) in
507 * which case the sending device address is used.
508 */
509 lock_sock(sk);
510
511 /* Check these errors (active socket, double bind). */
512 err = -EINVAL;
513 if (sk->sk_state != TCP_CLOSE || inet->num)
514 goto out_release_sock;
515
516 inet->rcv_saddr = inet->saddr = addr->sin_addr.s_addr;
517 if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
518 inet->saddr = 0; /* Use device */
519
520 /* Make sure we are allowed to bind here. */
521 if (sk->sk_prot->get_port(sk, snum)) {
522 inet->saddr = inet->rcv_saddr = 0;
523 err = -EADDRINUSE;
524 goto out_release_sock;
525 }
526
527 if (inet->rcv_saddr)
528 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
529 if (snum)
530 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
531 inet->sport = htons(inet->num);
532 inet->daddr = 0;
533 inet->dport = 0;
534 sk_dst_reset(sk);
535 err = 0;
536 out_release_sock:
537 release_sock(sk);
538 out:
539 return err;
540 }
541
inet_dgram_connect(struct socket * sock,struct sockaddr * uaddr,int addr_len,int flags)542 int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
543 int addr_len, int flags)
544 {
545 struct sock *sk = sock->sk;
546
547 if (uaddr->sa_family == AF_UNSPEC)
548 return sk->sk_prot->disconnect(sk, flags);
549
550 if (!inet_sk(sk)->num && inet_autobind(sk))
551 return -EAGAIN;
552 return sk->sk_prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
553 }
554
inet_wait_for_connect(struct sock * sk,long timeo)555 static long inet_wait_for_connect(struct sock *sk, long timeo)
556 {
557 DEFINE_WAIT(wait);
558
559 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
560
561 /* Basic assumption: if someone sets sk->sk_err, he _must_
562 * change state of the socket from TCP_SYN_*.
563 * Connect() does not allow to get error notifications
564 * without closing the socket.
565 */
566 while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
567 release_sock(sk);
568 timeo = schedule_timeout(timeo);
569 lock_sock(sk);
570 if (signal_pending(current) || !timeo)
571 break;
572 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
573 }
574 finish_wait(sk->sk_sleep, &wait);
575 return timeo;
576 }
577
578 /*
579 * Connect to a remote host. There is regrettably still a little
580 * TCP 'magic' in here.
581 */
inet_stream_connect(struct socket * sock,struct sockaddr * uaddr,int addr_len,int flags)582 int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
583 int addr_len, int flags)
584 {
585 struct sock *sk = sock->sk;
586 int err;
587 long timeo;
588
589 lock_sock(sk);
590
591 if (uaddr->sa_family == AF_UNSPEC) {
592 err = sk->sk_prot->disconnect(sk, flags);
593 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
594 goto out;
595 }
596
597 switch (sock->state) {
598 default:
599 err = -EINVAL;
600 goto out;
601 case SS_CONNECTED:
602 err = -EISCONN;
603 goto out;
604 case SS_CONNECTING:
605 err = -EALREADY;
606 /* Fall out of switch with err, set for this state */
607 break;
608 case SS_UNCONNECTED:
609 err = -EISCONN;
610 if (sk->sk_state != TCP_CLOSE)
611 goto out;
612
613 err = sk->sk_prot->connect(sk, uaddr, addr_len);
614 if (err < 0)
615 goto out;
616
617 sock->state = SS_CONNECTING;
618
619 /* Just entered SS_CONNECTING state; the only
620 * difference is that return value in non-blocking
621 * case is EINPROGRESS, rather than EALREADY.
622 */
623 err = -EINPROGRESS;
624 break;
625 }
626
627 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
628
629 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
630 /* Error code is set above */
631 if (!timeo || !inet_wait_for_connect(sk, timeo))
632 goto out;
633
634 err = sock_intr_errno(timeo);
635 if (signal_pending(current))
636 goto out;
637 }
638
639 /* Connection was closed by RST, timeout, ICMP error
640 * or another process disconnected us.
641 */
642 if (sk->sk_state == TCP_CLOSE)
643 goto sock_error;
644
645 /* sk->sk_err may be not zero now, if RECVERR was ordered by user
646 * and error was received after socket entered established state.
647 * Hence, it is handled normally after connect() return successfully.
648 */
649
650 sock->state = SS_CONNECTED;
651 err = 0;
652 out:
653 release_sock(sk);
654 return err;
655
656 sock_error:
657 err = sock_error(sk) ? : -ECONNABORTED;
658 sock->state = SS_UNCONNECTED;
659 if (sk->sk_prot->disconnect(sk, flags))
660 sock->state = SS_DISCONNECTING;
661 goto out;
662 }
663
664 /*
665 * Accept a pending connection. The TCP layer now gives BSD semantics.
666 */
667
inet_accept(struct socket * sock,struct socket * newsock,int flags)668 int inet_accept(struct socket *sock, struct socket *newsock, int flags)
669 {
670 struct sock *sk1 = sock->sk;
671 int err = -EINVAL;
672 struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err);
673
674 if (!sk2)
675 goto do_err;
676
677 lock_sock(sk2);
678
679 WARN_ON(!((1 << sk2->sk_state) &
680 (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_CLOSE)));
681
682 sock_graft(sk2, newsock);
683
684 newsock->state = SS_CONNECTED;
685 err = 0;
686 release_sock(sk2);
687 do_err:
688 return err;
689 }
690
691
692 /*
693 * This does both peername and sockname.
694 */
inet_getname(struct socket * sock,struct sockaddr * uaddr,int * uaddr_len,int peer)695 int inet_getname(struct socket *sock, struct sockaddr *uaddr,
696 int *uaddr_len, int peer)
697 {
698 struct sock *sk = sock->sk;
699 struct inet_sock *inet = inet_sk(sk);
700 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
701
702 sin->sin_family = AF_INET;
703 if (peer) {
704 if (!inet->dport ||
705 (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
706 peer == 1))
707 return -ENOTCONN;
708 sin->sin_port = inet->dport;
709 sin->sin_addr.s_addr = inet->daddr;
710 } else {
711 __be32 addr = inet->rcv_saddr;
712 if (!addr)
713 addr = inet->saddr;
714 sin->sin_port = inet->sport;
715 sin->sin_addr.s_addr = addr;
716 }
717 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
718 *uaddr_len = sizeof(*sin);
719 return 0;
720 }
721
inet_sendmsg(struct kiocb * iocb,struct socket * sock,struct msghdr * msg,size_t size)722 int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
723 size_t size)
724 {
725 struct sock *sk = sock->sk;
726
727 /* We may need to bind the socket. */
728 if (!inet_sk(sk)->num && inet_autobind(sk))
729 return -EAGAIN;
730
731 return sk->sk_prot->sendmsg(iocb, sk, msg, size);
732 }
733
734
inet_sendpage(struct socket * sock,struct page * page,int offset,size_t size,int flags)735 static ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
736 {
737 struct sock *sk = sock->sk;
738
739 /* We may need to bind the socket. */
740 if (!inet_sk(sk)->num && inet_autobind(sk))
741 return -EAGAIN;
742
743 if (sk->sk_prot->sendpage)
744 return sk->sk_prot->sendpage(sk, page, offset, size, flags);
745 return sock_no_sendpage(sock, page, offset, size, flags);
746 }
747
748
inet_shutdown(struct socket * sock,int how)749 int inet_shutdown(struct socket *sock, int how)
750 {
751 struct sock *sk = sock->sk;
752 int err = 0;
753
754 /* This should really check to make sure
755 * the socket is a TCP socket. (WHY AC...)
756 */
757 how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
758 1->2 bit 2 snds.
759 2->3 */
760 if ((how & ~SHUTDOWN_MASK) || !how) /* MAXINT->0 */
761 return -EINVAL;
762
763 lock_sock(sk);
764 if (sock->state == SS_CONNECTING) {
765 if ((1 << sk->sk_state) &
766 (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
767 sock->state = SS_DISCONNECTING;
768 else
769 sock->state = SS_CONNECTED;
770 }
771
772 switch (sk->sk_state) {
773 case TCP_CLOSE:
774 err = -ENOTCONN;
775 /* Hack to wake up other listeners, who can poll for
776 POLLHUP, even on eg. unconnected UDP sockets -- RR */
777 default:
778 sk->sk_shutdown |= how;
779 if (sk->sk_prot->shutdown)
780 sk->sk_prot->shutdown(sk, how);
781 break;
782
783 /* Remaining two branches are temporary solution for missing
784 * close() in multithreaded environment. It is _not_ a good idea,
785 * but we have no choice until close() is repaired at VFS level.
786 */
787 case TCP_LISTEN:
788 if (!(how & RCV_SHUTDOWN))
789 break;
790 /* Fall through */
791 case TCP_SYN_SENT:
792 err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
793 sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
794 break;
795 }
796
797 /* Wake up anyone sleeping in poll. */
798 sk->sk_state_change(sk);
799 release_sock(sk);
800 return err;
801 }
802
803 /*
804 * ioctl() calls you can issue on an INET socket. Most of these are
805 * device configuration and stuff and very rarely used. Some ioctls
806 * pass on to the socket itself.
807 *
808 * NOTE: I like the idea of a module for the config stuff. ie ifconfig
809 * loads the devconfigure module does its configuring and unloads it.
810 * There's a good 20K of config code hanging around the kernel.
811 */
812
inet_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)813 int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
814 {
815 struct sock *sk = sock->sk;
816 int err = 0;
817 struct net *net = sock_net(sk);
818
819 switch (cmd) {
820 case SIOCGSTAMP:
821 err = sock_get_timestamp(sk, (struct timeval __user *)arg);
822 break;
823 case SIOCGSTAMPNS:
824 err = sock_get_timestampns(sk, (struct timespec __user *)arg);
825 break;
826 case SIOCADDRT:
827 case SIOCDELRT:
828 case SIOCRTMSG:
829 err = ip_rt_ioctl(net, cmd, (void __user *)arg);
830 break;
831 case SIOCDARP:
832 case SIOCGARP:
833 case SIOCSARP:
834 err = arp_ioctl(net, cmd, (void __user *)arg);
835 break;
836 case SIOCGIFADDR:
837 case SIOCSIFADDR:
838 case SIOCGIFBRDADDR:
839 case SIOCSIFBRDADDR:
840 case SIOCGIFNETMASK:
841 case SIOCSIFNETMASK:
842 case SIOCGIFDSTADDR:
843 case SIOCSIFDSTADDR:
844 case SIOCSIFPFLAGS:
845 case SIOCGIFPFLAGS:
846 case SIOCSIFFLAGS:
847 case SIOCKILLADDR:
848 err = devinet_ioctl(net, cmd, (void __user *)arg);
849 break;
850 default:
851 if (sk->sk_prot->ioctl)
852 err = sk->sk_prot->ioctl(sk, cmd, arg);
853 else
854 err = -ENOIOCTLCMD;
855 break;
856 }
857 return err;
858 }
859
860 const struct proto_ops inet_stream_ops = {
861 .family = PF_INET,
862 .owner = THIS_MODULE,
863 .release = inet_release,
864 .bind = inet_bind,
865 .connect = inet_stream_connect,
866 .socketpair = sock_no_socketpair,
867 .accept = inet_accept,
868 .getname = inet_getname,
869 .poll = tcp_poll,
870 .ioctl = inet_ioctl,
871 .listen = inet_listen,
872 .shutdown = inet_shutdown,
873 .setsockopt = sock_common_setsockopt,
874 .getsockopt = sock_common_getsockopt,
875 .sendmsg = tcp_sendmsg,
876 .recvmsg = sock_common_recvmsg,
877 .mmap = sock_no_mmap,
878 .sendpage = tcp_sendpage,
879 .splice_read = tcp_splice_read,
880 #ifdef CONFIG_COMPAT
881 .compat_setsockopt = compat_sock_common_setsockopt,
882 .compat_getsockopt = compat_sock_common_getsockopt,
883 #endif
884 };
885
886 const struct proto_ops inet_dgram_ops = {
887 .family = PF_INET,
888 .owner = THIS_MODULE,
889 .release = inet_release,
890 .bind = inet_bind,
891 .connect = inet_dgram_connect,
892 .socketpair = sock_no_socketpair,
893 .accept = sock_no_accept,
894 .getname = inet_getname,
895 .poll = udp_poll,
896 .ioctl = inet_ioctl,
897 .listen = sock_no_listen,
898 .shutdown = inet_shutdown,
899 .setsockopt = sock_common_setsockopt,
900 .getsockopt = sock_common_getsockopt,
901 .sendmsg = inet_sendmsg,
902 .recvmsg = sock_common_recvmsg,
903 .mmap = sock_no_mmap,
904 .sendpage = inet_sendpage,
905 #ifdef CONFIG_COMPAT
906 .compat_setsockopt = compat_sock_common_setsockopt,
907 .compat_getsockopt = compat_sock_common_getsockopt,
908 #endif
909 };
910
911 /*
912 * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without
913 * udp_poll
914 */
915 static const struct proto_ops inet_sockraw_ops = {
916 .family = PF_INET,
917 .owner = THIS_MODULE,
918 .release = inet_release,
919 .bind = inet_bind,
920 .connect = inet_dgram_connect,
921 .socketpair = sock_no_socketpair,
922 .accept = sock_no_accept,
923 .getname = inet_getname,
924 .poll = datagram_poll,
925 .ioctl = inet_ioctl,
926 .listen = sock_no_listen,
927 .shutdown = inet_shutdown,
928 .setsockopt = sock_common_setsockopt,
929 .getsockopt = sock_common_getsockopt,
930 .sendmsg = inet_sendmsg,
931 .recvmsg = sock_common_recvmsg,
932 .mmap = sock_no_mmap,
933 .sendpage = inet_sendpage,
934 #ifdef CONFIG_COMPAT
935 .compat_setsockopt = compat_sock_common_setsockopt,
936 .compat_getsockopt = compat_sock_common_getsockopt,
937 #endif
938 };
939
940 static struct net_proto_family inet_family_ops = {
941 .family = PF_INET,
942 .create = inet_create,
943 .owner = THIS_MODULE,
944 };
945
946 /* Upon startup we insert all the elements in inetsw_array[] into
947 * the linked list inetsw.
948 */
949 static struct inet_protosw inetsw_array[] =
950 {
951 {
952 .type = SOCK_STREAM,
953 .protocol = IPPROTO_TCP,
954 .prot = &tcp_prot,
955 .ops = &inet_stream_ops,
956 .capability = -1,
957 .no_check = 0,
958 .flags = INET_PROTOSW_PERMANENT |
959 INET_PROTOSW_ICSK,
960 },
961
962 {
963 .type = SOCK_DGRAM,
964 .protocol = IPPROTO_UDP,
965 .prot = &udp_prot,
966 .ops = &inet_dgram_ops,
967 .capability = -1,
968 .no_check = UDP_CSUM_DEFAULT,
969 .flags = INET_PROTOSW_PERMANENT,
970 },
971
972
973 {
974 .type = SOCK_RAW,
975 .protocol = IPPROTO_IP, /* wild card */
976 .prot = &raw_prot,
977 .ops = &inet_sockraw_ops,
978 .capability = CAP_NET_RAW,
979 .no_check = UDP_CSUM_DEFAULT,
980 .flags = INET_PROTOSW_REUSE,
981 }
982 };
983
984 #define INETSW_ARRAY_LEN ARRAY_SIZE(inetsw_array)
985
inet_register_protosw(struct inet_protosw * p)986 void inet_register_protosw(struct inet_protosw *p)
987 {
988 struct list_head *lh;
989 struct inet_protosw *answer;
990 int protocol = p->protocol;
991 struct list_head *last_perm;
992
993 spin_lock_bh(&inetsw_lock);
994
995 if (p->type >= SOCK_MAX)
996 goto out_illegal;
997
998 /* If we are trying to override a permanent protocol, bail. */
999 answer = NULL;
1000 last_perm = &inetsw[p->type];
1001 list_for_each(lh, &inetsw[p->type]) {
1002 answer = list_entry(lh, struct inet_protosw, list);
1003
1004 /* Check only the non-wild match. */
1005 if (INET_PROTOSW_PERMANENT & answer->flags) {
1006 if (protocol == answer->protocol)
1007 break;
1008 last_perm = lh;
1009 }
1010
1011 answer = NULL;
1012 }
1013 if (answer)
1014 goto out_permanent;
1015
1016 /* Add the new entry after the last permanent entry if any, so that
1017 * the new entry does not override a permanent entry when matched with
1018 * a wild-card protocol. But it is allowed to override any existing
1019 * non-permanent entry. This means that when we remove this entry, the
1020 * system automatically returns to the old behavior.
1021 */
1022 list_add_rcu(&p->list, last_perm);
1023 out:
1024 spin_unlock_bh(&inetsw_lock);
1025
1026 synchronize_net();
1027
1028 return;
1029
1030 out_permanent:
1031 printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
1032 protocol);
1033 goto out;
1034
1035 out_illegal:
1036 printk(KERN_ERR
1037 "Ignoring attempt to register invalid socket type %d.\n",
1038 p->type);
1039 goto out;
1040 }
1041
inet_unregister_protosw(struct inet_protosw * p)1042 void inet_unregister_protosw(struct inet_protosw *p)
1043 {
1044 if (INET_PROTOSW_PERMANENT & p->flags) {
1045 printk(KERN_ERR
1046 "Attempt to unregister permanent protocol %d.\n",
1047 p->protocol);
1048 } else {
1049 spin_lock_bh(&inetsw_lock);
1050 list_del_rcu(&p->list);
1051 spin_unlock_bh(&inetsw_lock);
1052
1053 synchronize_net();
1054 }
1055 }
1056
1057 /*
1058 * Shall we try to damage output packets if routing dev changes?
1059 */
1060
1061 int sysctl_ip_dynaddr __read_mostly;
1062
inet_sk_reselect_saddr(struct sock * sk)1063 static int inet_sk_reselect_saddr(struct sock *sk)
1064 {
1065 struct inet_sock *inet = inet_sk(sk);
1066 int err;
1067 struct rtable *rt;
1068 __be32 old_saddr = inet->saddr;
1069 __be32 new_saddr;
1070 __be32 daddr = inet->daddr;
1071
1072 if (inet->opt && inet->opt->srr)
1073 daddr = inet->opt->faddr;
1074
1075 /* Query new route. */
1076 err = ip_route_connect(&rt, daddr, 0,
1077 RT_CONN_FLAGS(sk),
1078 sk->sk_bound_dev_if,
1079 sk->sk_protocol,
1080 inet->sport, inet->dport, sk, 0);
1081 if (err)
1082 return err;
1083
1084 sk_setup_caps(sk, &rt->u.dst);
1085
1086 new_saddr = rt->rt_src;
1087
1088 if (new_saddr == old_saddr)
1089 return 0;
1090
1091 if (sysctl_ip_dynaddr > 1) {
1092 printk(KERN_INFO "%s(): shifting inet->saddr from %pI4 to %pI4\n",
1093 __func__, &old_saddr, &new_saddr);
1094 }
1095
1096 inet->saddr = inet->rcv_saddr = new_saddr;
1097
1098 /*
1099 * XXX The only one ugly spot where we need to
1100 * XXX really change the sockets identity after
1101 * XXX it has entered the hashes. -DaveM
1102 *
1103 * Besides that, it does not check for connection
1104 * uniqueness. Wait for troubles.
1105 */
1106 __sk_prot_rehash(sk);
1107 return 0;
1108 }
1109
inet_sk_rebuild_header(struct sock * sk)1110 int inet_sk_rebuild_header(struct sock *sk)
1111 {
1112 struct inet_sock *inet = inet_sk(sk);
1113 struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0);
1114 __be32 daddr;
1115 int err;
1116
1117 /* Route is OK, nothing to do. */
1118 if (rt)
1119 return 0;
1120
1121 /* Reroute. */
1122 daddr = inet->daddr;
1123 if (inet->opt && inet->opt->srr)
1124 daddr = inet->opt->faddr;
1125 {
1126 struct flowi fl = {
1127 .oif = sk->sk_bound_dev_if,
1128 .nl_u = {
1129 .ip4_u = {
1130 .daddr = daddr,
1131 .saddr = inet->saddr,
1132 .tos = RT_CONN_FLAGS(sk),
1133 },
1134 },
1135 .proto = sk->sk_protocol,
1136 .flags = inet_sk_flowi_flags(sk),
1137 .uli_u = {
1138 .ports = {
1139 .sport = inet->sport,
1140 .dport = inet->dport,
1141 },
1142 },
1143 };
1144
1145 security_sk_classify_flow(sk, &fl);
1146 err = ip_route_output_flow(sock_net(sk), &rt, &fl, sk, 0);
1147 }
1148 if (!err)
1149 sk_setup_caps(sk, &rt->u.dst);
1150 else {
1151 /* Routing failed... */
1152 sk->sk_route_caps = 0;
1153 /*
1154 * Other protocols have to map its equivalent state to TCP_SYN_SENT.
1155 * DCCP maps its DCCP_REQUESTING state to TCP_SYN_SENT. -acme
1156 */
1157 if (!sysctl_ip_dynaddr ||
1158 sk->sk_state != TCP_SYN_SENT ||
1159 (sk->sk_userlocks & SOCK_BINDADDR_LOCK) ||
1160 (err = inet_sk_reselect_saddr(sk)) != 0)
1161 sk->sk_err_soft = -err;
1162 }
1163
1164 return err;
1165 }
1166
1167 EXPORT_SYMBOL(inet_sk_rebuild_header);
1168
inet_gso_send_check(struct sk_buff * skb)1169 static int inet_gso_send_check(struct sk_buff *skb)
1170 {
1171 struct iphdr *iph;
1172 struct net_protocol *ops;
1173 int proto;
1174 int ihl;
1175 int err = -EINVAL;
1176
1177 if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
1178 goto out;
1179
1180 iph = ip_hdr(skb);
1181 ihl = iph->ihl * 4;
1182 if (ihl < sizeof(*iph))
1183 goto out;
1184
1185 if (unlikely(!pskb_may_pull(skb, ihl)))
1186 goto out;
1187
1188 __skb_pull(skb, ihl);
1189 skb_reset_transport_header(skb);
1190 iph = ip_hdr(skb);
1191 proto = iph->protocol & (MAX_INET_PROTOS - 1);
1192 err = -EPROTONOSUPPORT;
1193
1194 rcu_read_lock();
1195 ops = rcu_dereference(inet_protos[proto]);
1196 if (likely(ops && ops->gso_send_check))
1197 err = ops->gso_send_check(skb);
1198 rcu_read_unlock();
1199
1200 out:
1201 return err;
1202 }
1203
inet_gso_segment(struct sk_buff * skb,int features)1204 static struct sk_buff *inet_gso_segment(struct sk_buff *skb, int features)
1205 {
1206 struct sk_buff *segs = ERR_PTR(-EINVAL);
1207 struct iphdr *iph;
1208 struct net_protocol *ops;
1209 int proto;
1210 int ihl;
1211 int id;
1212
1213 if (!(features & NETIF_F_V4_CSUM))
1214 features &= ~NETIF_F_SG;
1215
1216 if (unlikely(skb_shinfo(skb)->gso_type &
1217 ~(SKB_GSO_TCPV4 |
1218 SKB_GSO_UDP |
1219 SKB_GSO_DODGY |
1220 SKB_GSO_TCP_ECN |
1221 0)))
1222 goto out;
1223
1224 if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
1225 goto out;
1226
1227 iph = ip_hdr(skb);
1228 ihl = iph->ihl * 4;
1229 if (ihl < sizeof(*iph))
1230 goto out;
1231
1232 if (unlikely(!pskb_may_pull(skb, ihl)))
1233 goto out;
1234
1235 __skb_pull(skb, ihl);
1236 skb_reset_transport_header(skb);
1237 iph = ip_hdr(skb);
1238 id = ntohs(iph->id);
1239 proto = iph->protocol & (MAX_INET_PROTOS - 1);
1240 segs = ERR_PTR(-EPROTONOSUPPORT);
1241
1242 rcu_read_lock();
1243 ops = rcu_dereference(inet_protos[proto]);
1244 if (likely(ops && ops->gso_segment))
1245 segs = ops->gso_segment(skb, features);
1246 rcu_read_unlock();
1247
1248 if (!segs || IS_ERR(segs))
1249 goto out;
1250
1251 skb = segs;
1252 do {
1253 iph = ip_hdr(skb);
1254 iph->id = htons(id++);
1255 iph->tot_len = htons(skb->len - skb->mac_len);
1256 iph->check = 0;
1257 iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl);
1258 } while ((skb = skb->next));
1259
1260 out:
1261 return segs;
1262 }
1263
inet_gro_receive(struct sk_buff ** head,struct sk_buff * skb)1264 static struct sk_buff **inet_gro_receive(struct sk_buff **head,
1265 struct sk_buff *skb)
1266 {
1267 struct net_protocol *ops;
1268 struct sk_buff **pp = NULL;
1269 struct sk_buff *p;
1270 struct iphdr *iph;
1271 int flush = 1;
1272 int proto;
1273 int id;
1274
1275 if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
1276 goto out;
1277
1278 iph = ip_hdr(skb);
1279 proto = iph->protocol & (MAX_INET_PROTOS - 1);
1280
1281 rcu_read_lock();
1282 ops = rcu_dereference(inet_protos[proto]);
1283 if (!ops || !ops->gro_receive)
1284 goto out_unlock;
1285
1286 if (iph->version != 4 || iph->ihl != 5)
1287 goto out_unlock;
1288
1289 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1290 goto out_unlock;
1291
1292 flush = ntohs(iph->tot_len) != skb->len ||
1293 iph->frag_off != htons(IP_DF);
1294 id = ntohs(iph->id);
1295
1296 for (p = *head; p; p = p->next) {
1297 struct iphdr *iph2;
1298
1299 if (!NAPI_GRO_CB(p)->same_flow)
1300 continue;
1301
1302 iph2 = ip_hdr(p);
1303
1304 if (iph->protocol != iph2->protocol ||
1305 iph->tos != iph2->tos ||
1306 memcmp(&iph->saddr, &iph2->saddr, 8)) {
1307 NAPI_GRO_CB(p)->same_flow = 0;
1308 continue;
1309 }
1310
1311 /* All fields must match except length and checksum. */
1312 NAPI_GRO_CB(p)->flush |=
1313 memcmp(&iph->frag_off, &iph2->frag_off, 4) ||
1314 (u16)(ntohs(iph2->id) + NAPI_GRO_CB(p)->count) != id;
1315
1316 NAPI_GRO_CB(p)->flush |= flush;
1317 }
1318
1319 NAPI_GRO_CB(skb)->flush |= flush;
1320 __skb_pull(skb, sizeof(*iph));
1321 skb_reset_transport_header(skb);
1322
1323 pp = ops->gro_receive(head, skb);
1324
1325 out_unlock:
1326 rcu_read_unlock();
1327
1328 out:
1329 NAPI_GRO_CB(skb)->flush |= flush;
1330
1331 return pp;
1332 }
1333
inet_gro_complete(struct sk_buff * skb)1334 static int inet_gro_complete(struct sk_buff *skb)
1335 {
1336 struct net_protocol *ops;
1337 struct iphdr *iph = ip_hdr(skb);
1338 int proto = iph->protocol & (MAX_INET_PROTOS - 1);
1339 int err = -ENOSYS;
1340 __be16 newlen = htons(skb->len - skb_network_offset(skb));
1341
1342 csum_replace2(&iph->check, iph->tot_len, newlen);
1343 iph->tot_len = newlen;
1344
1345 rcu_read_lock();
1346 ops = rcu_dereference(inet_protos[proto]);
1347 if (WARN_ON(!ops || !ops->gro_complete))
1348 goto out_unlock;
1349
1350 err = ops->gro_complete(skb);
1351
1352 out_unlock:
1353 rcu_read_unlock();
1354
1355 return err;
1356 }
1357
inet_ctl_sock_create(struct sock ** sk,unsigned short family,unsigned short type,unsigned char protocol,struct net * net)1358 int inet_ctl_sock_create(struct sock **sk, unsigned short family,
1359 unsigned short type, unsigned char protocol,
1360 struct net *net)
1361 {
1362 struct socket *sock;
1363 int rc = sock_create_kern(family, type, protocol, &sock);
1364
1365 if (rc == 0) {
1366 *sk = sock->sk;
1367 (*sk)->sk_allocation = GFP_ATOMIC;
1368 /*
1369 * Unhash it so that IP input processing does not even see it,
1370 * we do not wish this socket to see incoming packets.
1371 */
1372 (*sk)->sk_prot->unhash(*sk);
1373
1374 sk_change_net(*sk, net);
1375 }
1376 return rc;
1377 }
1378
1379 EXPORT_SYMBOL_GPL(inet_ctl_sock_create);
1380
snmp_fold_field(void * mib[],int offt)1381 unsigned long snmp_fold_field(void *mib[], int offt)
1382 {
1383 unsigned long res = 0;
1384 int i;
1385
1386 for_each_possible_cpu(i) {
1387 res += *(((unsigned long *) per_cpu_ptr(mib[0], i)) + offt);
1388 res += *(((unsigned long *) per_cpu_ptr(mib[1], i)) + offt);
1389 }
1390 return res;
1391 }
1392 EXPORT_SYMBOL_GPL(snmp_fold_field);
1393
snmp_mib_init(void * ptr[2],size_t mibsize)1394 int snmp_mib_init(void *ptr[2], size_t mibsize)
1395 {
1396 BUG_ON(ptr == NULL);
1397 ptr[0] = __alloc_percpu(mibsize);
1398 if (!ptr[0])
1399 goto err0;
1400 ptr[1] = __alloc_percpu(mibsize);
1401 if (!ptr[1])
1402 goto err1;
1403 return 0;
1404 err1:
1405 free_percpu(ptr[0]);
1406 ptr[0] = NULL;
1407 err0:
1408 return -ENOMEM;
1409 }
1410 EXPORT_SYMBOL_GPL(snmp_mib_init);
1411
snmp_mib_free(void * ptr[2])1412 void snmp_mib_free(void *ptr[2])
1413 {
1414 BUG_ON(ptr == NULL);
1415 free_percpu(ptr[0]);
1416 free_percpu(ptr[1]);
1417 ptr[0] = ptr[1] = NULL;
1418 }
1419 EXPORT_SYMBOL_GPL(snmp_mib_free);
1420
1421 #ifdef CONFIG_IP_MULTICAST
1422 static struct net_protocol igmp_protocol = {
1423 .handler = igmp_rcv,
1424 .netns_ok = 1,
1425 };
1426 #endif
1427
1428 static struct net_protocol tcp_protocol = {
1429 .handler = tcp_v4_rcv,
1430 .err_handler = tcp_v4_err,
1431 .gso_send_check = tcp_v4_gso_send_check,
1432 .gso_segment = tcp_tso_segment,
1433 .gro_receive = tcp4_gro_receive,
1434 .gro_complete = tcp4_gro_complete,
1435 .no_policy = 1,
1436 .netns_ok = 1,
1437 };
1438
1439 static struct net_protocol udp_protocol = {
1440 .handler = udp_rcv,
1441 .err_handler = udp_err,
1442 .no_policy = 1,
1443 .netns_ok = 1,
1444 };
1445
1446 static struct net_protocol icmp_protocol = {
1447 .handler = icmp_rcv,
1448 .no_policy = 1,
1449 .netns_ok = 1,
1450 };
1451
ipv4_mib_init_net(struct net * net)1452 static __net_init int ipv4_mib_init_net(struct net *net)
1453 {
1454 if (snmp_mib_init((void **)net->mib.tcp_statistics,
1455 sizeof(struct tcp_mib)) < 0)
1456 goto err_tcp_mib;
1457 if (snmp_mib_init((void **)net->mib.ip_statistics,
1458 sizeof(struct ipstats_mib)) < 0)
1459 goto err_ip_mib;
1460 if (snmp_mib_init((void **)net->mib.net_statistics,
1461 sizeof(struct linux_mib)) < 0)
1462 goto err_net_mib;
1463 if (snmp_mib_init((void **)net->mib.udp_statistics,
1464 sizeof(struct udp_mib)) < 0)
1465 goto err_udp_mib;
1466 if (snmp_mib_init((void **)net->mib.udplite_statistics,
1467 sizeof(struct udp_mib)) < 0)
1468 goto err_udplite_mib;
1469 if (snmp_mib_init((void **)net->mib.icmp_statistics,
1470 sizeof(struct icmp_mib)) < 0)
1471 goto err_icmp_mib;
1472 if (snmp_mib_init((void **)net->mib.icmpmsg_statistics,
1473 sizeof(struct icmpmsg_mib)) < 0)
1474 goto err_icmpmsg_mib;
1475
1476 tcp_mib_init(net);
1477 return 0;
1478
1479 err_icmpmsg_mib:
1480 snmp_mib_free((void **)net->mib.icmp_statistics);
1481 err_icmp_mib:
1482 snmp_mib_free((void **)net->mib.udplite_statistics);
1483 err_udplite_mib:
1484 snmp_mib_free((void **)net->mib.udp_statistics);
1485 err_udp_mib:
1486 snmp_mib_free((void **)net->mib.net_statistics);
1487 err_net_mib:
1488 snmp_mib_free((void **)net->mib.ip_statistics);
1489 err_ip_mib:
1490 snmp_mib_free((void **)net->mib.tcp_statistics);
1491 err_tcp_mib:
1492 return -ENOMEM;
1493 }
1494
ipv4_mib_exit_net(struct net * net)1495 static __net_exit void ipv4_mib_exit_net(struct net *net)
1496 {
1497 snmp_mib_free((void **)net->mib.icmpmsg_statistics);
1498 snmp_mib_free((void **)net->mib.icmp_statistics);
1499 snmp_mib_free((void **)net->mib.udplite_statistics);
1500 snmp_mib_free((void **)net->mib.udp_statistics);
1501 snmp_mib_free((void **)net->mib.net_statistics);
1502 snmp_mib_free((void **)net->mib.ip_statistics);
1503 snmp_mib_free((void **)net->mib.tcp_statistics);
1504 }
1505
1506 static __net_initdata struct pernet_operations ipv4_mib_ops = {
1507 .init = ipv4_mib_init_net,
1508 .exit = ipv4_mib_exit_net,
1509 };
1510
init_ipv4_mibs(void)1511 static int __init init_ipv4_mibs(void)
1512 {
1513 return register_pernet_subsys(&ipv4_mib_ops);
1514 }
1515
1516 static int ipv4_proc_init(void);
1517
1518 /*
1519 * IP protocol layer initialiser
1520 */
1521
1522 static struct packet_type ip_packet_type = {
1523 .type = __constant_htons(ETH_P_IP),
1524 .func = ip_rcv,
1525 .gso_send_check = inet_gso_send_check,
1526 .gso_segment = inet_gso_segment,
1527 .gro_receive = inet_gro_receive,
1528 .gro_complete = inet_gro_complete,
1529 };
1530
inet_init(void)1531 static int __init inet_init(void)
1532 {
1533 struct sk_buff *dummy_skb;
1534 struct inet_protosw *q;
1535 struct list_head *r;
1536 int rc = -EINVAL;
1537
1538 BUILD_BUG_ON(sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb));
1539
1540 rc = proto_register(&tcp_prot, 1);
1541 if (rc)
1542 goto out;
1543
1544 rc = proto_register(&udp_prot, 1);
1545 if (rc)
1546 goto out_unregister_tcp_proto;
1547
1548 rc = proto_register(&raw_prot, 1);
1549 if (rc)
1550 goto out_unregister_udp_proto;
1551
1552 /*
1553 * Tell SOCKET that we are alive...
1554 */
1555
1556 (void)sock_register(&inet_family_ops);
1557
1558 #ifdef CONFIG_SYSCTL
1559 ip_static_sysctl_init();
1560 #endif
1561
1562 /*
1563 * Add all the base protocols.
1564 */
1565
1566 if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
1567 printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
1568 if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
1569 printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
1570 if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
1571 printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n");
1572 #ifdef CONFIG_IP_MULTICAST
1573 if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
1574 printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n");
1575 #endif
1576
1577 /* Register the socket-side information for inet_create. */
1578 for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
1579 INIT_LIST_HEAD(r);
1580
1581 for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
1582 inet_register_protosw(q);
1583
1584 /*
1585 * Set the ARP module up
1586 */
1587
1588 arp_init();
1589
1590 /*
1591 * Set the IP module up
1592 */
1593
1594 ip_init();
1595
1596 tcp_v4_init();
1597
1598 /* Setup TCP slab cache for open requests. */
1599 tcp_init();
1600
1601 /* Setup UDP memory threshold */
1602 udp_init();
1603
1604 /* Add UDP-Lite (RFC 3828) */
1605 udplite4_register();
1606
1607 /*
1608 * Set the ICMP layer up
1609 */
1610
1611 if (icmp_init() < 0)
1612 panic("Failed to create the ICMP control socket.\n");
1613
1614 /*
1615 * Initialise the multicast router
1616 */
1617 #if defined(CONFIG_IP_MROUTE)
1618 if (ip_mr_init())
1619 printk(KERN_CRIT "inet_init: Cannot init ipv4 mroute\n");
1620 #endif
1621 /*
1622 * Initialise per-cpu ipv4 mibs
1623 */
1624
1625 if (init_ipv4_mibs())
1626 printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n");
1627
1628 ipv4_proc_init();
1629
1630 ipfrag_init();
1631
1632 dev_add_pack(&ip_packet_type);
1633
1634 rc = 0;
1635 out:
1636 return rc;
1637 out_unregister_udp_proto:
1638 proto_unregister(&udp_prot);
1639 out_unregister_tcp_proto:
1640 proto_unregister(&tcp_prot);
1641 goto out;
1642 }
1643
1644 fs_initcall(inet_init);
1645
1646 /* ------------------------------------------------------------------------ */
1647
1648 #ifdef CONFIG_PROC_FS
ipv4_proc_init(void)1649 static int __init ipv4_proc_init(void)
1650 {
1651 int rc = 0;
1652
1653 if (raw_proc_init())
1654 goto out_raw;
1655 if (tcp4_proc_init())
1656 goto out_tcp;
1657 if (udp4_proc_init())
1658 goto out_udp;
1659 if (ip_misc_proc_init())
1660 goto out_misc;
1661 out:
1662 return rc;
1663 out_misc:
1664 udp4_proc_exit();
1665 out_udp:
1666 tcp4_proc_exit();
1667 out_tcp:
1668 raw_proc_exit();
1669 out_raw:
1670 rc = -ENOMEM;
1671 goto out;
1672 }
1673
1674 #else /* CONFIG_PROC_FS */
ipv4_proc_init(void)1675 static int __init ipv4_proc_init(void)
1676 {
1677 return 0;
1678 }
1679 #endif /* CONFIG_PROC_FS */
1680
1681 MODULE_ALIAS_NETPROTO(PF_INET);
1682
1683 EXPORT_SYMBOL(inet_accept);
1684 EXPORT_SYMBOL(inet_bind);
1685 EXPORT_SYMBOL(inet_dgram_connect);
1686 EXPORT_SYMBOL(inet_dgram_ops);
1687 EXPORT_SYMBOL(inet_getname);
1688 EXPORT_SYMBOL(inet_ioctl);
1689 EXPORT_SYMBOL(inet_listen);
1690 EXPORT_SYMBOL(inet_register_protosw);
1691 EXPORT_SYMBOL(inet_release);
1692 EXPORT_SYMBOL(inet_sendmsg);
1693 EXPORT_SYMBOL(inet_shutdown);
1694 EXPORT_SYMBOL(inet_sock_destruct);
1695 EXPORT_SYMBOL(inet_stream_connect);
1696 EXPORT_SYMBOL(inet_stream_ops);
1697 EXPORT_SYMBOL(inet_unregister_protosw);
1698 EXPORT_SYMBOL(sysctl_ip_nonlocal_bind);
1699