af_inet.c 35 KB

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