af_inet.c 37 KB

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