af_inet.c 35 KB

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