af_inet.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  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. unsigned long snmp_fold_field(void *mib[], int offt)
  1058. {
  1059. unsigned long res = 0;
  1060. int i;
  1061. for_each_possible_cpu(i) {
  1062. res += *(((unsigned long *) per_cpu_ptr(mib[0], i)) + offt);
  1063. res += *(((unsigned long *) per_cpu_ptr(mib[1], i)) + offt);
  1064. }
  1065. return res;
  1066. }
  1067. EXPORT_SYMBOL_GPL(snmp_fold_field);
  1068. int snmp_mib_init(void *ptr[2], size_t mibsize, size_t mibalign)
  1069. {
  1070. BUG_ON(ptr == NULL);
  1071. ptr[0] = __alloc_percpu(mibsize);
  1072. if (!ptr[0])
  1073. goto err0;
  1074. ptr[1] = __alloc_percpu(mibsize);
  1075. if (!ptr[1])
  1076. goto err1;
  1077. return 0;
  1078. err1:
  1079. free_percpu(ptr[0]);
  1080. ptr[0] = NULL;
  1081. err0:
  1082. return -ENOMEM;
  1083. }
  1084. EXPORT_SYMBOL_GPL(snmp_mib_init);
  1085. void snmp_mib_free(void *ptr[2])
  1086. {
  1087. BUG_ON(ptr == NULL);
  1088. free_percpu(ptr[0]);
  1089. free_percpu(ptr[1]);
  1090. ptr[0] = ptr[1] = NULL;
  1091. }
  1092. EXPORT_SYMBOL_GPL(snmp_mib_free);
  1093. #ifdef CONFIG_IP_MULTICAST
  1094. static struct net_protocol igmp_protocol = {
  1095. .handler = igmp_rcv,
  1096. };
  1097. #endif
  1098. static struct net_protocol tcp_protocol = {
  1099. .handler = tcp_v4_rcv,
  1100. .err_handler = tcp_v4_err,
  1101. .gso_send_check = tcp_v4_gso_send_check,
  1102. .gso_segment = tcp_tso_segment,
  1103. .no_policy = 1,
  1104. };
  1105. static struct net_protocol udp_protocol = {
  1106. .handler = udp_rcv,
  1107. .err_handler = udp_err,
  1108. .no_policy = 1,
  1109. };
  1110. static struct net_protocol icmp_protocol = {
  1111. .handler = icmp_rcv,
  1112. };
  1113. static int __init init_ipv4_mibs(void)
  1114. {
  1115. if (snmp_mib_init((void **)net_statistics,
  1116. sizeof(struct linux_mib),
  1117. __alignof__(struct linux_mib)) < 0)
  1118. goto err_net_mib;
  1119. if (snmp_mib_init((void **)ip_statistics,
  1120. sizeof(struct ipstats_mib),
  1121. __alignof__(struct ipstats_mib)) < 0)
  1122. goto err_ip_mib;
  1123. if (snmp_mib_init((void **)icmp_statistics,
  1124. sizeof(struct icmp_mib),
  1125. __alignof__(struct icmp_mib)) < 0)
  1126. goto err_icmp_mib;
  1127. if (snmp_mib_init((void **)tcp_statistics,
  1128. sizeof(struct tcp_mib),
  1129. __alignof__(struct tcp_mib)) < 0)
  1130. goto err_tcp_mib;
  1131. if (snmp_mib_init((void **)udp_statistics,
  1132. sizeof(struct udp_mib),
  1133. __alignof__(struct udp_mib)) < 0)
  1134. goto err_udp_mib;
  1135. if (snmp_mib_init((void **)udplite_statistics,
  1136. sizeof(struct udp_mib),
  1137. __alignof__(struct udp_mib)) < 0)
  1138. goto err_udplite_mib;
  1139. tcp_mib_init();
  1140. return 0;
  1141. err_udplite_mib:
  1142. snmp_mib_free((void **)udp_statistics);
  1143. err_udp_mib:
  1144. snmp_mib_free((void **)tcp_statistics);
  1145. err_tcp_mib:
  1146. snmp_mib_free((void **)icmp_statistics);
  1147. err_icmp_mib:
  1148. snmp_mib_free((void **)ip_statistics);
  1149. err_ip_mib:
  1150. snmp_mib_free((void **)net_statistics);
  1151. err_net_mib:
  1152. return -ENOMEM;
  1153. }
  1154. static int ipv4_proc_init(void);
  1155. /*
  1156. * IP protocol layer initialiser
  1157. */
  1158. static struct packet_type ip_packet_type = {
  1159. .type = __constant_htons(ETH_P_IP),
  1160. .func = ip_rcv,
  1161. .gso_send_check = inet_gso_send_check,
  1162. .gso_segment = inet_gso_segment,
  1163. };
  1164. static int __init inet_init(void)
  1165. {
  1166. struct sk_buff *dummy_skb;
  1167. struct inet_protosw *q;
  1168. struct list_head *r;
  1169. int rc = -EINVAL;
  1170. BUILD_BUG_ON(sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb));
  1171. rc = proto_register(&tcp_prot, 1);
  1172. if (rc)
  1173. goto out;
  1174. rc = proto_register(&udp_prot, 1);
  1175. if (rc)
  1176. goto out_unregister_tcp_proto;
  1177. rc = proto_register(&raw_prot, 1);
  1178. if (rc)
  1179. goto out_unregister_udp_proto;
  1180. /*
  1181. * Tell SOCKET that we are alive...
  1182. */
  1183. (void)sock_register(&inet_family_ops);
  1184. /*
  1185. * Add all the base protocols.
  1186. */
  1187. if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
  1188. printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
  1189. if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
  1190. printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
  1191. if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
  1192. printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n");
  1193. #ifdef CONFIG_IP_MULTICAST
  1194. if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
  1195. printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n");
  1196. #endif
  1197. /* Register the socket-side information for inet_create. */
  1198. for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
  1199. INIT_LIST_HEAD(r);
  1200. for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
  1201. inet_register_protosw(q);
  1202. /*
  1203. * Set the ARP module up
  1204. */
  1205. arp_init();
  1206. /*
  1207. * Set the IP module up
  1208. */
  1209. ip_init();
  1210. tcp_v4_init(&inet_family_ops);
  1211. /* Setup TCP slab cache for open requests. */
  1212. tcp_init();
  1213. /* Add UDP-Lite (RFC 3828) */
  1214. udplite4_register();
  1215. /*
  1216. * Set the ICMP layer up
  1217. */
  1218. icmp_init(&inet_family_ops);
  1219. /*
  1220. * Initialise the multicast router
  1221. */
  1222. #if defined(CONFIG_IP_MROUTE)
  1223. ip_mr_init();
  1224. #endif
  1225. /*
  1226. * Initialise per-cpu ipv4 mibs
  1227. */
  1228. if (init_ipv4_mibs())
  1229. printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n"); ;
  1230. ipv4_proc_init();
  1231. ipfrag_init();
  1232. dev_add_pack(&ip_packet_type);
  1233. rc = 0;
  1234. out:
  1235. return rc;
  1236. out_unregister_udp_proto:
  1237. proto_unregister(&udp_prot);
  1238. out_unregister_tcp_proto:
  1239. proto_unregister(&tcp_prot);
  1240. goto out;
  1241. }
  1242. fs_initcall(inet_init);
  1243. /* ------------------------------------------------------------------------ */
  1244. #ifdef CONFIG_PROC_FS
  1245. static int __init ipv4_proc_init(void)
  1246. {
  1247. int rc = 0;
  1248. if (raw_proc_init())
  1249. goto out_raw;
  1250. if (tcp4_proc_init())
  1251. goto out_tcp;
  1252. if (udp4_proc_init())
  1253. goto out_udp;
  1254. if (fib_proc_init())
  1255. goto out_fib;
  1256. if (ip_misc_proc_init())
  1257. goto out_misc;
  1258. out:
  1259. return rc;
  1260. out_misc:
  1261. fib_proc_exit();
  1262. out_fib:
  1263. udp4_proc_exit();
  1264. out_udp:
  1265. tcp4_proc_exit();
  1266. out_tcp:
  1267. raw_proc_exit();
  1268. out_raw:
  1269. rc = -ENOMEM;
  1270. goto out;
  1271. }
  1272. #else /* CONFIG_PROC_FS */
  1273. static int __init ipv4_proc_init(void)
  1274. {
  1275. return 0;
  1276. }
  1277. #endif /* CONFIG_PROC_FS */
  1278. MODULE_ALIAS_NETPROTO(PF_INET);
  1279. EXPORT_SYMBOL(inet_accept);
  1280. EXPORT_SYMBOL(inet_bind);
  1281. EXPORT_SYMBOL(inet_dgram_connect);
  1282. EXPORT_SYMBOL(inet_dgram_ops);
  1283. EXPORT_SYMBOL(inet_getname);
  1284. EXPORT_SYMBOL(inet_ioctl);
  1285. EXPORT_SYMBOL(inet_listen);
  1286. EXPORT_SYMBOL(inet_register_protosw);
  1287. EXPORT_SYMBOL(inet_release);
  1288. EXPORT_SYMBOL(inet_sendmsg);
  1289. EXPORT_SYMBOL(inet_shutdown);
  1290. EXPORT_SYMBOL(inet_sock_destruct);
  1291. EXPORT_SYMBOL(inet_stream_connect);
  1292. EXPORT_SYMBOL(inet_stream_ops);
  1293. EXPORT_SYMBOL(inet_unregister_protosw);
  1294. EXPORT_SYMBOL(net_statistics);
  1295. EXPORT_SYMBOL(sysctl_ip_nonlocal_bind);