af_inet.c 34 KB

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