af_inet.c 33 KB

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