af_inet.c 35 KB

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