af_inet.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803
  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 <linux/slab.h>
  89. #include <asm/uaccess.h>
  90. #include <asm/system.h>
  91. #include <linux/inet.h>
  92. #include <linux/igmp.h>
  93. #include <linux/inetdevice.h>
  94. #include <linux/netdevice.h>
  95. #include <net/checksum.h>
  96. #include <net/ip.h>
  97. #include <net/protocol.h>
  98. #include <net/arp.h>
  99. #include <net/route.h>
  100. #include <net/ip_fib.h>
  101. #include <net/inet_connection_sock.h>
  102. #include <net/tcp.h>
  103. #include <net/udp.h>
  104. #include <net/udplite.h>
  105. #include <net/ping.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. #include <net/net_namespace.h>
  114. #ifdef CONFIG_IP_MROUTE
  115. #include <linux/mroute.h>
  116. #endif
  117. /* The inetsw table contains everything that inet_create needs to
  118. * build a new socket.
  119. */
  120. static struct list_head inetsw[SOCK_MAX];
  121. static DEFINE_SPINLOCK(inetsw_lock);
  122. struct ipv4_config ipv4_config;
  123. EXPORT_SYMBOL(ipv4_config);
  124. /* New destruction routine */
  125. void inet_sock_destruct(struct sock *sk)
  126. {
  127. struct inet_sock *inet = inet_sk(sk);
  128. __skb_queue_purge(&sk->sk_receive_queue);
  129. __skb_queue_purge(&sk->sk_error_queue);
  130. sk_mem_reclaim(sk);
  131. if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
  132. pr_err("Attempt to release TCP socket in state %d %p\n",
  133. sk->sk_state, sk);
  134. return;
  135. }
  136. if (!sock_flag(sk, SOCK_DEAD)) {
  137. pr_err("Attempt to release alive inet socket %p\n", sk);
  138. return;
  139. }
  140. WARN_ON(atomic_read(&sk->sk_rmem_alloc));
  141. WARN_ON(atomic_read(&sk->sk_wmem_alloc));
  142. WARN_ON(sk->sk_wmem_queued);
  143. WARN_ON(sk->sk_forward_alloc);
  144. kfree(rcu_dereference_protected(inet->inet_opt, 1));
  145. dst_release(rcu_dereference_check(sk->sk_dst_cache, 1));
  146. sk_refcnt_debug_dec(sk);
  147. }
  148. EXPORT_SYMBOL(inet_sock_destruct);
  149. /*
  150. * The routines beyond this point handle the behaviour of an AF_INET
  151. * socket object. Mostly it punts to the subprotocols of IP to do
  152. * the work.
  153. */
  154. /*
  155. * Automatically bind an unbound socket.
  156. */
  157. static int inet_autobind(struct sock *sk)
  158. {
  159. struct inet_sock *inet;
  160. /* We may need to bind the socket. */
  161. lock_sock(sk);
  162. inet = inet_sk(sk);
  163. if (!inet->inet_num) {
  164. if (sk->sk_prot->get_port(sk, 0)) {
  165. release_sock(sk);
  166. return -EAGAIN;
  167. }
  168. inet->inet_sport = htons(inet->inet_num);
  169. }
  170. release_sock(sk);
  171. return 0;
  172. }
  173. /*
  174. * Move a socket into listening state.
  175. */
  176. int inet_listen(struct socket *sock, int backlog)
  177. {
  178. struct sock *sk = sock->sk;
  179. unsigned char old_state;
  180. int err;
  181. lock_sock(sk);
  182. err = -EINVAL;
  183. if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
  184. goto out;
  185. old_state = sk->sk_state;
  186. if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
  187. goto out;
  188. /* Really, if the socket is already in listen state
  189. * we can only allow the backlog to be adjusted.
  190. */
  191. if (old_state != TCP_LISTEN) {
  192. err = inet_csk_listen_start(sk, backlog);
  193. if (err)
  194. goto out;
  195. }
  196. sk->sk_max_ack_backlog = backlog;
  197. err = 0;
  198. out:
  199. release_sock(sk);
  200. return err;
  201. }
  202. EXPORT_SYMBOL(inet_listen);
  203. u32 inet_ehash_secret __read_mostly;
  204. EXPORT_SYMBOL(inet_ehash_secret);
  205. /*
  206. * inet_ehash_secret must be set exactly once
  207. */
  208. void build_ehash_secret(void)
  209. {
  210. u32 rnd;
  211. do {
  212. get_random_bytes(&rnd, sizeof(rnd));
  213. } while (rnd == 0);
  214. cmpxchg(&inet_ehash_secret, 0, rnd);
  215. }
  216. EXPORT_SYMBOL(build_ehash_secret);
  217. static inline int inet_netns_ok(struct net *net, int protocol)
  218. {
  219. int hash;
  220. const struct net_protocol *ipprot;
  221. if (net_eq(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. int kern)
  235. {
  236. struct sock *sk;
  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 (unlikely(!inet_ehash_secret))
  245. if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
  246. build_ehash_secret();
  247. sock->state = SS_UNCONNECTED;
  248. /* Look for the requested type/protocol pair. */
  249. lookup_protocol:
  250. err = -ESOCKTNOSUPPORT;
  251. rcu_read_lock();
  252. list_for_each_entry_rcu(answer, &inetsw[sock->type], list) {
  253. err = 0;
  254. /* Check the non-wild match. */
  255. if (protocol == answer->protocol) {
  256. if (protocol != IPPROTO_IP)
  257. break;
  258. } else {
  259. /* Check for the two wild cases. */
  260. if (IPPROTO_IP == protocol) {
  261. protocol = answer->protocol;
  262. break;
  263. }
  264. if (IPPROTO_IP == answer->protocol)
  265. break;
  266. }
  267. err = -EPROTONOSUPPORT;
  268. }
  269. if (unlikely(err)) {
  270. if (try_loading_module < 2) {
  271. rcu_read_unlock();
  272. /*
  273. * Be more specific, e.g. net-pf-2-proto-132-type-1
  274. * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM)
  275. */
  276. if (++try_loading_module == 1)
  277. request_module("net-pf-%d-proto-%d-type-%d",
  278. PF_INET, protocol, sock->type);
  279. /*
  280. * Fall back to generic, e.g. net-pf-2-proto-132
  281. * (net-pf-PF_INET-proto-IPPROTO_SCTP)
  282. */
  283. else
  284. request_module("net-pf-%d-proto-%d",
  285. PF_INET, protocol);
  286. goto lookup_protocol;
  287. } else
  288. goto out_rcu_unlock;
  289. }
  290. err = -EPERM;
  291. if (sock->type == SOCK_RAW && !kern && !capable(CAP_NET_RAW))
  292. goto out_rcu_unlock;
  293. err = -EAFNOSUPPORT;
  294. if (!inet_netns_ok(net, protocol))
  295. goto out_rcu_unlock;
  296. sock->ops = answer->ops;
  297. answer_prot = answer->prot;
  298. answer_no_check = answer->no_check;
  299. answer_flags = answer->flags;
  300. rcu_read_unlock();
  301. WARN_ON(answer_prot->slab == NULL);
  302. err = -ENOBUFS;
  303. sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot);
  304. if (sk == NULL)
  305. goto out;
  306. err = 0;
  307. sk->sk_no_check = answer_no_check;
  308. if (INET_PROTOSW_REUSE & answer_flags)
  309. sk->sk_reuse = 1;
  310. inet = inet_sk(sk);
  311. inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
  312. inet->nodefrag = 0;
  313. if (SOCK_RAW == sock->type) {
  314. inet->inet_num = protocol;
  315. if (IPPROTO_RAW == protocol)
  316. inet->hdrincl = 1;
  317. }
  318. if (ipv4_config.no_pmtu_disc)
  319. inet->pmtudisc = IP_PMTUDISC_DONT;
  320. else
  321. inet->pmtudisc = IP_PMTUDISC_WANT;
  322. inet->inet_id = 0;
  323. sock_init_data(sock, sk);
  324. sk->sk_destruct = inet_sock_destruct;
  325. sk->sk_protocol = protocol;
  326. sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
  327. inet->uc_ttl = -1;
  328. inet->mc_loop = 1;
  329. inet->mc_ttl = 1;
  330. inet->mc_all = 1;
  331. inet->mc_index = 0;
  332. inet->mc_list = NULL;
  333. sk_refcnt_debug_inc(sk);
  334. if (inet->inet_num) {
  335. /* It assumes that any protocol which allows
  336. * the user to assign a number at socket
  337. * creation time automatically
  338. * shares.
  339. */
  340. inet->inet_sport = htons(inet->inet_num);
  341. /* Add to protocol hash chains. */
  342. sk->sk_prot->hash(sk);
  343. }
  344. if (sk->sk_prot->init) {
  345. err = sk->sk_prot->init(sk);
  346. if (err)
  347. sk_common_release(sk);
  348. }
  349. out:
  350. return err;
  351. out_rcu_unlock:
  352. rcu_read_unlock();
  353. goto out;
  354. }
  355. /*
  356. * The peer socket should always be NULL (or else). When we call this
  357. * function we are destroying the object and from then on nobody
  358. * should refer to it.
  359. */
  360. int inet_release(struct socket *sock)
  361. {
  362. struct sock *sk = sock->sk;
  363. if (sk) {
  364. long timeout;
  365. sock_rps_reset_flow(sk);
  366. /* Applications forget to leave groups before exiting */
  367. ip_mc_drop_socket(sk);
  368. /* If linger is set, we don't return until the close
  369. * is complete. Otherwise we return immediately. The
  370. * actually closing is done the same either way.
  371. *
  372. * If the close is due to the process exiting, we never
  373. * linger..
  374. */
  375. timeout = 0;
  376. if (sock_flag(sk, SOCK_LINGER) &&
  377. !(current->flags & PF_EXITING))
  378. timeout = sk->sk_lingertime;
  379. sock->sk = NULL;
  380. sk->sk_prot->close(sk, timeout);
  381. }
  382. return 0;
  383. }
  384. EXPORT_SYMBOL(inet_release);
  385. /* It is off by default, see below. */
  386. int sysctl_ip_nonlocal_bind __read_mostly;
  387. EXPORT_SYMBOL(sysctl_ip_nonlocal_bind);
  388. int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  389. {
  390. struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
  391. struct sock *sk = sock->sk;
  392. struct inet_sock *inet = inet_sk(sk);
  393. unsigned short snum;
  394. int chk_addr_ret;
  395. int err;
  396. /* If the socket has its own bind function then use it. (RAW) */
  397. if (sk->sk_prot->bind) {
  398. err = sk->sk_prot->bind(sk, uaddr, addr_len);
  399. goto out;
  400. }
  401. err = -EINVAL;
  402. if (addr_len < sizeof(struct sockaddr_in))
  403. goto out;
  404. if (addr->sin_family != AF_INET)
  405. goto out;
  406. chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
  407. /* Not specified by any standard per-se, however it breaks too
  408. * many applications when removed. It is unfortunate since
  409. * allowing applications to make a non-local bind solves
  410. * several problems with systems using dynamic addressing.
  411. * (ie. your servers still start up even if your ISDN link
  412. * is temporarily down)
  413. */
  414. err = -EADDRNOTAVAIL;
  415. if (!sysctl_ip_nonlocal_bind &&
  416. !(inet->freebind || inet->transparent) &&
  417. addr->sin_addr.s_addr != htonl(INADDR_ANY) &&
  418. chk_addr_ret != RTN_LOCAL &&
  419. chk_addr_ret != RTN_MULTICAST &&
  420. chk_addr_ret != RTN_BROADCAST)
  421. goto out;
  422. snum = ntohs(addr->sin_port);
  423. err = -EACCES;
  424. if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
  425. goto out;
  426. /* We keep a pair of addresses. rcv_saddr is the one
  427. * used by hash lookups, and saddr is used for transmit.
  428. *
  429. * In the BSD API these are the same except where it
  430. * would be illegal to use them (multicast/broadcast) in
  431. * which case the sending device address is used.
  432. */
  433. lock_sock(sk);
  434. /* Check these errors (active socket, double bind). */
  435. err = -EINVAL;
  436. if (sk->sk_state != TCP_CLOSE || inet->inet_num)
  437. goto out_release_sock;
  438. inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
  439. if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
  440. inet->inet_saddr = 0; /* Use device */
  441. /* Make sure we are allowed to bind here. */
  442. if (sk->sk_prot->get_port(sk, snum)) {
  443. inet->inet_saddr = inet->inet_rcv_saddr = 0;
  444. err = -EADDRINUSE;
  445. goto out_release_sock;
  446. }
  447. if (inet->inet_rcv_saddr)
  448. sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
  449. if (snum)
  450. sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
  451. inet->inet_sport = htons(inet->inet_num);
  452. inet->inet_daddr = 0;
  453. inet->inet_dport = 0;
  454. sk_dst_reset(sk);
  455. err = 0;
  456. out_release_sock:
  457. release_sock(sk);
  458. out:
  459. return err;
  460. }
  461. EXPORT_SYMBOL(inet_bind);
  462. int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
  463. int addr_len, int flags)
  464. {
  465. struct sock *sk = sock->sk;
  466. if (addr_len < sizeof(uaddr->sa_family))
  467. return -EINVAL;
  468. if (uaddr->sa_family == AF_UNSPEC)
  469. return sk->sk_prot->disconnect(sk, flags);
  470. if (!inet_sk(sk)->inet_num && inet_autobind(sk))
  471. return -EAGAIN;
  472. return sk->sk_prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
  473. }
  474. EXPORT_SYMBOL(inet_dgram_connect);
  475. static long inet_wait_for_connect(struct sock *sk, long timeo)
  476. {
  477. DEFINE_WAIT(wait);
  478. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  479. /* Basic assumption: if someone sets sk->sk_err, he _must_
  480. * change state of the socket from TCP_SYN_*.
  481. * Connect() does not allow to get error notifications
  482. * without closing the socket.
  483. */
  484. while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
  485. release_sock(sk);
  486. timeo = schedule_timeout(timeo);
  487. lock_sock(sk);
  488. if (signal_pending(current) || !timeo)
  489. break;
  490. prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
  491. }
  492. finish_wait(sk_sleep(sk), &wait);
  493. return timeo;
  494. }
  495. /*
  496. * Connect to a remote host. There is regrettably still a little
  497. * TCP 'magic' in here.
  498. */
  499. int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
  500. int addr_len, int flags)
  501. {
  502. struct sock *sk = sock->sk;
  503. int err;
  504. long timeo;
  505. if (addr_len < sizeof(uaddr->sa_family))
  506. return -EINVAL;
  507. lock_sock(sk);
  508. if (uaddr->sa_family == AF_UNSPEC) {
  509. err = sk->sk_prot->disconnect(sk, flags);
  510. sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
  511. goto out;
  512. }
  513. switch (sock->state) {
  514. default:
  515. err = -EINVAL;
  516. goto out;
  517. case SS_CONNECTED:
  518. err = -EISCONN;
  519. goto out;
  520. case SS_CONNECTING:
  521. err = -EALREADY;
  522. /* Fall out of switch with err, set for this state */
  523. break;
  524. case SS_UNCONNECTED:
  525. err = -EISCONN;
  526. if (sk->sk_state != TCP_CLOSE)
  527. goto out;
  528. err = sk->sk_prot->connect(sk, uaddr, addr_len);
  529. if (err < 0)
  530. goto out;
  531. sock->state = SS_CONNECTING;
  532. /* Just entered SS_CONNECTING state; the only
  533. * difference is that return value in non-blocking
  534. * case is EINPROGRESS, rather than EALREADY.
  535. */
  536. err = -EINPROGRESS;
  537. break;
  538. }
  539. timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
  540. if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
  541. /* Error code is set above */
  542. if (!timeo || !inet_wait_for_connect(sk, timeo))
  543. goto out;
  544. err = sock_intr_errno(timeo);
  545. if (signal_pending(current))
  546. goto out;
  547. }
  548. /* Connection was closed by RST, timeout, ICMP error
  549. * or another process disconnected us.
  550. */
  551. if (sk->sk_state == TCP_CLOSE)
  552. goto sock_error;
  553. /* sk->sk_err may be not zero now, if RECVERR was ordered by user
  554. * and error was received after socket entered established state.
  555. * Hence, it is handled normally after connect() return successfully.
  556. */
  557. sock->state = SS_CONNECTED;
  558. err = 0;
  559. out:
  560. release_sock(sk);
  561. return err;
  562. sock_error:
  563. err = sock_error(sk) ? : -ECONNABORTED;
  564. sock->state = SS_UNCONNECTED;
  565. if (sk->sk_prot->disconnect(sk, flags))
  566. sock->state = SS_DISCONNECTING;
  567. goto out;
  568. }
  569. EXPORT_SYMBOL(inet_stream_connect);
  570. /*
  571. * Accept a pending connection. The TCP layer now gives BSD semantics.
  572. */
  573. int inet_accept(struct socket *sock, struct socket *newsock, int flags)
  574. {
  575. struct sock *sk1 = sock->sk;
  576. int err = -EINVAL;
  577. struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err);
  578. if (!sk2)
  579. goto do_err;
  580. lock_sock(sk2);
  581. sock_rps_record_flow(sk2);
  582. WARN_ON(!((1 << sk2->sk_state) &
  583. (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_CLOSE)));
  584. sock_graft(sk2, newsock);
  585. newsock->state = SS_CONNECTED;
  586. err = 0;
  587. release_sock(sk2);
  588. do_err:
  589. return err;
  590. }
  591. EXPORT_SYMBOL(inet_accept);
  592. /*
  593. * This does both peername and sockname.
  594. */
  595. int inet_getname(struct socket *sock, struct sockaddr *uaddr,
  596. int *uaddr_len, int peer)
  597. {
  598. struct sock *sk = sock->sk;
  599. struct inet_sock *inet = inet_sk(sk);
  600. DECLARE_SOCKADDR(struct sockaddr_in *, sin, uaddr);
  601. sin->sin_family = AF_INET;
  602. if (peer) {
  603. if (!inet->inet_dport ||
  604. (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
  605. peer == 1))
  606. return -ENOTCONN;
  607. sin->sin_port = inet->inet_dport;
  608. sin->sin_addr.s_addr = inet->inet_daddr;
  609. } else {
  610. __be32 addr = inet->inet_rcv_saddr;
  611. if (!addr)
  612. addr = inet->inet_saddr;
  613. sin->sin_port = inet->inet_sport;
  614. sin->sin_addr.s_addr = addr;
  615. }
  616. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  617. *uaddr_len = sizeof(*sin);
  618. return 0;
  619. }
  620. EXPORT_SYMBOL(inet_getname);
  621. int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
  622. size_t size)
  623. {
  624. struct sock *sk = sock->sk;
  625. sock_rps_record_flow(sk);
  626. /* We may need to bind the socket. */
  627. if (!inet_sk(sk)->inet_num && !sk->sk_prot->no_autobind &&
  628. inet_autobind(sk))
  629. return -EAGAIN;
  630. return sk->sk_prot->sendmsg(iocb, sk, msg, size);
  631. }
  632. EXPORT_SYMBOL(inet_sendmsg);
  633. ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset,
  634. size_t size, int flags)
  635. {
  636. struct sock *sk = sock->sk;
  637. sock_rps_record_flow(sk);
  638. /* We may need to bind the socket. */
  639. if (!inet_sk(sk)->inet_num && !sk->sk_prot->no_autobind &&
  640. inet_autobind(sk))
  641. return -EAGAIN;
  642. if (sk->sk_prot->sendpage)
  643. return sk->sk_prot->sendpage(sk, page, offset, size, flags);
  644. return sock_no_sendpage(sock, page, offset, size, flags);
  645. }
  646. EXPORT_SYMBOL(inet_sendpage);
  647. int inet_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
  648. size_t size, int flags)
  649. {
  650. struct sock *sk = sock->sk;
  651. int addr_len = 0;
  652. int err;
  653. sock_rps_record_flow(sk);
  654. err = sk->sk_prot->recvmsg(iocb, sk, msg, size, flags & MSG_DONTWAIT,
  655. flags & ~MSG_DONTWAIT, &addr_len);
  656. if (err >= 0)
  657. msg->msg_namelen = addr_len;
  658. return err;
  659. }
  660. EXPORT_SYMBOL(inet_recvmsg);
  661. int inet_shutdown(struct socket *sock, int how)
  662. {
  663. struct sock *sk = sock->sk;
  664. int err = 0;
  665. /* This should really check to make sure
  666. * the socket is a TCP socket. (WHY AC...)
  667. */
  668. how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
  669. 1->2 bit 2 snds.
  670. 2->3 */
  671. if ((how & ~SHUTDOWN_MASK) || !how) /* MAXINT->0 */
  672. return -EINVAL;
  673. lock_sock(sk);
  674. if (sock->state == SS_CONNECTING) {
  675. if ((1 << sk->sk_state) &
  676. (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
  677. sock->state = SS_DISCONNECTING;
  678. else
  679. sock->state = SS_CONNECTED;
  680. }
  681. switch (sk->sk_state) {
  682. case TCP_CLOSE:
  683. err = -ENOTCONN;
  684. /* Hack to wake up other listeners, who can poll for
  685. POLLHUP, even on eg. unconnected UDP sockets -- RR */
  686. default:
  687. sk->sk_shutdown |= how;
  688. if (sk->sk_prot->shutdown)
  689. sk->sk_prot->shutdown(sk, how);
  690. break;
  691. /* Remaining two branches are temporary solution for missing
  692. * close() in multithreaded environment. It is _not_ a good idea,
  693. * but we have no choice until close() is repaired at VFS level.
  694. */
  695. case TCP_LISTEN:
  696. if (!(how & RCV_SHUTDOWN))
  697. break;
  698. /* Fall through */
  699. case TCP_SYN_SENT:
  700. err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
  701. sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
  702. break;
  703. }
  704. /* Wake up anyone sleeping in poll. */
  705. sk->sk_state_change(sk);
  706. release_sock(sk);
  707. return err;
  708. }
  709. EXPORT_SYMBOL(inet_shutdown);
  710. /*
  711. * ioctl() calls you can issue on an INET socket. Most of these are
  712. * device configuration and stuff and very rarely used. Some ioctls
  713. * pass on to the socket itself.
  714. *
  715. * NOTE: I like the idea of a module for the config stuff. ie ifconfig
  716. * loads the devconfigure module does its configuring and unloads it.
  717. * There's a good 20K of config code hanging around the kernel.
  718. */
  719. int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  720. {
  721. struct sock *sk = sock->sk;
  722. int err = 0;
  723. struct net *net = sock_net(sk);
  724. switch (cmd) {
  725. case SIOCGSTAMP:
  726. err = sock_get_timestamp(sk, (struct timeval __user *)arg);
  727. break;
  728. case SIOCGSTAMPNS:
  729. err = sock_get_timestampns(sk, (struct timespec __user *)arg);
  730. break;
  731. case SIOCADDRT:
  732. case SIOCDELRT:
  733. case SIOCRTMSG:
  734. err = ip_rt_ioctl(net, cmd, (void __user *)arg);
  735. break;
  736. case SIOCDARP:
  737. case SIOCGARP:
  738. case SIOCSARP:
  739. err = arp_ioctl(net, cmd, (void __user *)arg);
  740. break;
  741. case SIOCGIFADDR:
  742. case SIOCSIFADDR:
  743. case SIOCGIFBRDADDR:
  744. case SIOCSIFBRDADDR:
  745. case SIOCGIFNETMASK:
  746. case SIOCSIFNETMASK:
  747. case SIOCGIFDSTADDR:
  748. case SIOCSIFDSTADDR:
  749. case SIOCSIFPFLAGS:
  750. case SIOCGIFPFLAGS:
  751. case SIOCSIFFLAGS:
  752. err = devinet_ioctl(net, cmd, (void __user *)arg);
  753. break;
  754. default:
  755. if (sk->sk_prot->ioctl)
  756. err = sk->sk_prot->ioctl(sk, cmd, arg);
  757. else
  758. err = -ENOIOCTLCMD;
  759. break;
  760. }
  761. return err;
  762. }
  763. EXPORT_SYMBOL(inet_ioctl);
  764. #ifdef CONFIG_COMPAT
  765. int inet_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  766. {
  767. struct sock *sk = sock->sk;
  768. int err = -ENOIOCTLCMD;
  769. if (sk->sk_prot->compat_ioctl)
  770. err = sk->sk_prot->compat_ioctl(sk, cmd, arg);
  771. return err;
  772. }
  773. #endif
  774. const struct proto_ops inet_stream_ops = {
  775. .family = PF_INET,
  776. .owner = THIS_MODULE,
  777. .release = inet_release,
  778. .bind = inet_bind,
  779. .connect = inet_stream_connect,
  780. .socketpair = sock_no_socketpair,
  781. .accept = inet_accept,
  782. .getname = inet_getname,
  783. .poll = tcp_poll,
  784. .ioctl = inet_ioctl,
  785. .listen = inet_listen,
  786. .shutdown = inet_shutdown,
  787. .setsockopt = sock_common_setsockopt,
  788. .getsockopt = sock_common_getsockopt,
  789. .sendmsg = inet_sendmsg,
  790. .recvmsg = inet_recvmsg,
  791. .mmap = sock_no_mmap,
  792. .sendpage = inet_sendpage,
  793. .splice_read = tcp_splice_read,
  794. #ifdef CONFIG_COMPAT
  795. .compat_setsockopt = compat_sock_common_setsockopt,
  796. .compat_getsockopt = compat_sock_common_getsockopt,
  797. .compat_ioctl = inet_compat_ioctl,
  798. #endif
  799. };
  800. EXPORT_SYMBOL(inet_stream_ops);
  801. const struct proto_ops inet_dgram_ops = {
  802. .family = PF_INET,
  803. .owner = THIS_MODULE,
  804. .release = inet_release,
  805. .bind = inet_bind,
  806. .connect = inet_dgram_connect,
  807. .socketpair = sock_no_socketpair,
  808. .accept = sock_no_accept,
  809. .getname = inet_getname,
  810. .poll = udp_poll,
  811. .ioctl = inet_ioctl,
  812. .listen = sock_no_listen,
  813. .shutdown = inet_shutdown,
  814. .setsockopt = sock_common_setsockopt,
  815. .getsockopt = sock_common_getsockopt,
  816. .sendmsg = inet_sendmsg,
  817. .recvmsg = inet_recvmsg,
  818. .mmap = sock_no_mmap,
  819. .sendpage = inet_sendpage,
  820. #ifdef CONFIG_COMPAT
  821. .compat_setsockopt = compat_sock_common_setsockopt,
  822. .compat_getsockopt = compat_sock_common_getsockopt,
  823. .compat_ioctl = inet_compat_ioctl,
  824. #endif
  825. };
  826. EXPORT_SYMBOL(inet_dgram_ops);
  827. /*
  828. * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without
  829. * udp_poll
  830. */
  831. static const struct proto_ops inet_sockraw_ops = {
  832. .family = PF_INET,
  833. .owner = THIS_MODULE,
  834. .release = inet_release,
  835. .bind = inet_bind,
  836. .connect = inet_dgram_connect,
  837. .socketpair = sock_no_socketpair,
  838. .accept = sock_no_accept,
  839. .getname = inet_getname,
  840. .poll = datagram_poll,
  841. .ioctl = inet_ioctl,
  842. .listen = sock_no_listen,
  843. .shutdown = inet_shutdown,
  844. .setsockopt = sock_common_setsockopt,
  845. .getsockopt = sock_common_getsockopt,
  846. .sendmsg = inet_sendmsg,
  847. .recvmsg = inet_recvmsg,
  848. .mmap = sock_no_mmap,
  849. .sendpage = inet_sendpage,
  850. #ifdef CONFIG_COMPAT
  851. .compat_setsockopt = compat_sock_common_setsockopt,
  852. .compat_getsockopt = compat_sock_common_getsockopt,
  853. .compat_ioctl = inet_compat_ioctl,
  854. #endif
  855. };
  856. static const struct net_proto_family inet_family_ops = {
  857. .family = PF_INET,
  858. .create = inet_create,
  859. .owner = THIS_MODULE,
  860. };
  861. /* Upon startup we insert all the elements in inetsw_array[] into
  862. * the linked list inetsw.
  863. */
  864. static struct inet_protosw inetsw_array[] =
  865. {
  866. {
  867. .type = SOCK_STREAM,
  868. .protocol = IPPROTO_TCP,
  869. .prot = &tcp_prot,
  870. .ops = &inet_stream_ops,
  871. .no_check = 0,
  872. .flags = INET_PROTOSW_PERMANENT |
  873. INET_PROTOSW_ICSK,
  874. },
  875. {
  876. .type = SOCK_DGRAM,
  877. .protocol = IPPROTO_UDP,
  878. .prot = &udp_prot,
  879. .ops = &inet_dgram_ops,
  880. .no_check = UDP_CSUM_DEFAULT,
  881. .flags = INET_PROTOSW_PERMANENT,
  882. },
  883. {
  884. .type = SOCK_DGRAM,
  885. .protocol = IPPROTO_ICMP,
  886. .prot = &ping_prot,
  887. .ops = &inet_dgram_ops,
  888. .no_check = UDP_CSUM_DEFAULT,
  889. .flags = INET_PROTOSW_REUSE,
  890. },
  891. {
  892. .type = SOCK_RAW,
  893. .protocol = IPPROTO_IP, /* wild card */
  894. .prot = &raw_prot,
  895. .ops = &inet_sockraw_ops,
  896. .no_check = UDP_CSUM_DEFAULT,
  897. .flags = INET_PROTOSW_REUSE,
  898. }
  899. };
  900. #define INETSW_ARRAY_LEN ARRAY_SIZE(inetsw_array)
  901. void inet_register_protosw(struct inet_protosw *p)
  902. {
  903. struct list_head *lh;
  904. struct inet_protosw *answer;
  905. int protocol = p->protocol;
  906. struct list_head *last_perm;
  907. spin_lock_bh(&inetsw_lock);
  908. if (p->type >= SOCK_MAX)
  909. goto out_illegal;
  910. /* If we are trying to override a permanent protocol, bail. */
  911. answer = NULL;
  912. last_perm = &inetsw[p->type];
  913. list_for_each(lh, &inetsw[p->type]) {
  914. answer = list_entry(lh, struct inet_protosw, list);
  915. /* Check only the non-wild match. */
  916. if (INET_PROTOSW_PERMANENT & answer->flags) {
  917. if (protocol == answer->protocol)
  918. break;
  919. last_perm = lh;
  920. }
  921. answer = NULL;
  922. }
  923. if (answer)
  924. goto out_permanent;
  925. /* Add the new entry after the last permanent entry if any, so that
  926. * the new entry does not override a permanent entry when matched with
  927. * a wild-card protocol. But it is allowed to override any existing
  928. * non-permanent entry. This means that when we remove this entry, the
  929. * system automatically returns to the old behavior.
  930. */
  931. list_add_rcu(&p->list, last_perm);
  932. out:
  933. spin_unlock_bh(&inetsw_lock);
  934. return;
  935. out_permanent:
  936. printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
  937. protocol);
  938. goto out;
  939. out_illegal:
  940. printk(KERN_ERR
  941. "Ignoring attempt to register invalid socket type %d.\n",
  942. p->type);
  943. goto out;
  944. }
  945. EXPORT_SYMBOL(inet_register_protosw);
  946. void inet_unregister_protosw(struct inet_protosw *p)
  947. {
  948. if (INET_PROTOSW_PERMANENT & p->flags) {
  949. printk(KERN_ERR
  950. "Attempt to unregister permanent protocol %d.\n",
  951. p->protocol);
  952. } else {
  953. spin_lock_bh(&inetsw_lock);
  954. list_del_rcu(&p->list);
  955. spin_unlock_bh(&inetsw_lock);
  956. synchronize_net();
  957. }
  958. }
  959. EXPORT_SYMBOL(inet_unregister_protosw);
  960. /*
  961. * Shall we try to damage output packets if routing dev changes?
  962. */
  963. int sysctl_ip_dynaddr __read_mostly;
  964. static int inet_sk_reselect_saddr(struct sock *sk)
  965. {
  966. struct inet_sock *inet = inet_sk(sk);
  967. __be32 old_saddr = inet->inet_saddr;
  968. __be32 daddr = inet->inet_daddr;
  969. struct flowi4 *fl4;
  970. struct rtable *rt;
  971. __be32 new_saddr;
  972. struct ip_options_rcu *inet_opt;
  973. inet_opt = rcu_dereference_protected(inet->inet_opt,
  974. sock_owned_by_user(sk));
  975. if (inet_opt && inet_opt->opt.srr)
  976. daddr = inet_opt->opt.faddr;
  977. /* Query new route. */
  978. fl4 = &inet->cork.fl.u.ip4;
  979. rt = ip_route_connect(fl4, daddr, 0, RT_CONN_FLAGS(sk),
  980. sk->sk_bound_dev_if, sk->sk_protocol,
  981. inet->inet_sport, inet->inet_dport, sk, false);
  982. if (IS_ERR(rt))
  983. return PTR_ERR(rt);
  984. sk_setup_caps(sk, &rt->dst);
  985. new_saddr = fl4->saddr;
  986. if (new_saddr == old_saddr)
  987. return 0;
  988. if (sysctl_ip_dynaddr > 1) {
  989. printk(KERN_INFO "%s(): shifting inet->saddr from %pI4 to %pI4\n",
  990. __func__, &old_saddr, &new_saddr);
  991. }
  992. inet->inet_saddr = inet->inet_rcv_saddr = new_saddr;
  993. /*
  994. * XXX The only one ugly spot where we need to
  995. * XXX really change the sockets identity after
  996. * XXX it has entered the hashes. -DaveM
  997. *
  998. * Besides that, it does not check for connection
  999. * uniqueness. Wait for troubles.
  1000. */
  1001. __sk_prot_rehash(sk);
  1002. return 0;
  1003. }
  1004. int inet_sk_rebuild_header(struct sock *sk)
  1005. {
  1006. struct inet_sock *inet = inet_sk(sk);
  1007. struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0);
  1008. __be32 daddr;
  1009. struct ip_options_rcu *inet_opt;
  1010. struct flowi4 *fl4;
  1011. int err;
  1012. /* Route is OK, nothing to do. */
  1013. if (rt)
  1014. return 0;
  1015. /* Reroute. */
  1016. rcu_read_lock();
  1017. inet_opt = rcu_dereference(inet->inet_opt);
  1018. daddr = inet->inet_daddr;
  1019. if (inet_opt && inet_opt->opt.srr)
  1020. daddr = inet_opt->opt.faddr;
  1021. rcu_read_unlock();
  1022. fl4 = &inet->cork.fl.u.ip4;
  1023. rt = ip_route_output_ports(sock_net(sk), fl4, sk, daddr, inet->inet_saddr,
  1024. inet->inet_dport, inet->inet_sport,
  1025. sk->sk_protocol, RT_CONN_FLAGS(sk),
  1026. sk->sk_bound_dev_if);
  1027. if (!IS_ERR(rt)) {
  1028. err = 0;
  1029. sk_setup_caps(sk, &rt->dst);
  1030. } else {
  1031. err = PTR_ERR(rt);
  1032. /* Routing failed... */
  1033. sk->sk_route_caps = 0;
  1034. /*
  1035. * Other protocols have to map its equivalent state to TCP_SYN_SENT.
  1036. * DCCP maps its DCCP_REQUESTING state to TCP_SYN_SENT. -acme
  1037. */
  1038. if (!sysctl_ip_dynaddr ||
  1039. sk->sk_state != TCP_SYN_SENT ||
  1040. (sk->sk_userlocks & SOCK_BINDADDR_LOCK) ||
  1041. (err = inet_sk_reselect_saddr(sk)) != 0)
  1042. sk->sk_err_soft = -err;
  1043. }
  1044. return err;
  1045. }
  1046. EXPORT_SYMBOL(inet_sk_rebuild_header);
  1047. static int inet_gso_send_check(struct sk_buff *skb)
  1048. {
  1049. const struct iphdr *iph;
  1050. const struct net_protocol *ops;
  1051. int proto;
  1052. int ihl;
  1053. int err = -EINVAL;
  1054. if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
  1055. goto out;
  1056. iph = ip_hdr(skb);
  1057. ihl = iph->ihl * 4;
  1058. if (ihl < sizeof(*iph))
  1059. goto out;
  1060. if (unlikely(!pskb_may_pull(skb, ihl)))
  1061. goto out;
  1062. __skb_pull(skb, ihl);
  1063. skb_reset_transport_header(skb);
  1064. iph = ip_hdr(skb);
  1065. proto = iph->protocol & (MAX_INET_PROTOS - 1);
  1066. err = -EPROTONOSUPPORT;
  1067. rcu_read_lock();
  1068. ops = rcu_dereference(inet_protos[proto]);
  1069. if (likely(ops && ops->gso_send_check))
  1070. err = ops->gso_send_check(skb);
  1071. rcu_read_unlock();
  1072. out:
  1073. return err;
  1074. }
  1075. static struct sk_buff *inet_gso_segment(struct sk_buff *skb, u32 features)
  1076. {
  1077. struct sk_buff *segs = ERR_PTR(-EINVAL);
  1078. struct iphdr *iph;
  1079. const struct net_protocol *ops;
  1080. int proto;
  1081. int ihl;
  1082. int id;
  1083. unsigned int offset = 0;
  1084. if (!(features & NETIF_F_V4_CSUM))
  1085. features &= ~NETIF_F_SG;
  1086. if (unlikely(skb_shinfo(skb)->gso_type &
  1087. ~(SKB_GSO_TCPV4 |
  1088. SKB_GSO_UDP |
  1089. SKB_GSO_DODGY |
  1090. SKB_GSO_TCP_ECN |
  1091. 0)))
  1092. goto out;
  1093. if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
  1094. goto out;
  1095. iph = ip_hdr(skb);
  1096. ihl = iph->ihl * 4;
  1097. if (ihl < sizeof(*iph))
  1098. goto out;
  1099. if (unlikely(!pskb_may_pull(skb, ihl)))
  1100. goto out;
  1101. __skb_pull(skb, ihl);
  1102. skb_reset_transport_header(skb);
  1103. iph = ip_hdr(skb);
  1104. id = ntohs(iph->id);
  1105. proto = iph->protocol & (MAX_INET_PROTOS - 1);
  1106. segs = ERR_PTR(-EPROTONOSUPPORT);
  1107. rcu_read_lock();
  1108. ops = rcu_dereference(inet_protos[proto]);
  1109. if (likely(ops && ops->gso_segment))
  1110. segs = ops->gso_segment(skb, features);
  1111. rcu_read_unlock();
  1112. if (!segs || IS_ERR(segs))
  1113. goto out;
  1114. skb = segs;
  1115. do {
  1116. iph = ip_hdr(skb);
  1117. if (proto == IPPROTO_UDP) {
  1118. iph->id = htons(id);
  1119. iph->frag_off = htons(offset >> 3);
  1120. if (skb->next != NULL)
  1121. iph->frag_off |= htons(IP_MF);
  1122. offset += (skb->len - skb->mac_len - iph->ihl * 4);
  1123. } else
  1124. iph->id = htons(id++);
  1125. iph->tot_len = htons(skb->len - skb->mac_len);
  1126. iph->check = 0;
  1127. iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl);
  1128. } while ((skb = skb->next));
  1129. out:
  1130. return segs;
  1131. }
  1132. static struct sk_buff **inet_gro_receive(struct sk_buff **head,
  1133. struct sk_buff *skb)
  1134. {
  1135. const struct net_protocol *ops;
  1136. struct sk_buff **pp = NULL;
  1137. struct sk_buff *p;
  1138. const struct iphdr *iph;
  1139. unsigned int hlen;
  1140. unsigned int off;
  1141. unsigned int id;
  1142. int flush = 1;
  1143. int proto;
  1144. off = skb_gro_offset(skb);
  1145. hlen = off + sizeof(*iph);
  1146. iph = skb_gro_header_fast(skb, off);
  1147. if (skb_gro_header_hard(skb, hlen)) {
  1148. iph = skb_gro_header_slow(skb, hlen, off);
  1149. if (unlikely(!iph))
  1150. goto out;
  1151. }
  1152. proto = iph->protocol & (MAX_INET_PROTOS - 1);
  1153. rcu_read_lock();
  1154. ops = rcu_dereference(inet_protos[proto]);
  1155. if (!ops || !ops->gro_receive)
  1156. goto out_unlock;
  1157. if (*(u8 *)iph != 0x45)
  1158. goto out_unlock;
  1159. if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
  1160. goto out_unlock;
  1161. id = ntohl(*(__be32 *)&iph->id);
  1162. flush = (u16)((ntohl(*(__be32 *)iph) ^ skb_gro_len(skb)) | (id ^ IP_DF));
  1163. id >>= 16;
  1164. for (p = *head; p; p = p->next) {
  1165. struct iphdr *iph2;
  1166. if (!NAPI_GRO_CB(p)->same_flow)
  1167. continue;
  1168. iph2 = ip_hdr(p);
  1169. if ((iph->protocol ^ iph2->protocol) |
  1170. (iph->tos ^ iph2->tos) |
  1171. ((__force u32)iph->saddr ^ (__force u32)iph2->saddr) |
  1172. ((__force u32)iph->daddr ^ (__force u32)iph2->daddr)) {
  1173. NAPI_GRO_CB(p)->same_flow = 0;
  1174. continue;
  1175. }
  1176. /* All fields must match except length and checksum. */
  1177. NAPI_GRO_CB(p)->flush |=
  1178. (iph->ttl ^ iph2->ttl) |
  1179. ((u16)(ntohs(iph2->id) + NAPI_GRO_CB(p)->count) ^ id);
  1180. NAPI_GRO_CB(p)->flush |= flush;
  1181. }
  1182. NAPI_GRO_CB(skb)->flush |= flush;
  1183. skb_gro_pull(skb, sizeof(*iph));
  1184. skb_set_transport_header(skb, skb_gro_offset(skb));
  1185. pp = ops->gro_receive(head, skb);
  1186. out_unlock:
  1187. rcu_read_unlock();
  1188. out:
  1189. NAPI_GRO_CB(skb)->flush |= flush;
  1190. return pp;
  1191. }
  1192. static int inet_gro_complete(struct sk_buff *skb)
  1193. {
  1194. const struct net_protocol *ops;
  1195. struct iphdr *iph = ip_hdr(skb);
  1196. int proto = iph->protocol & (MAX_INET_PROTOS - 1);
  1197. int err = -ENOSYS;
  1198. __be16 newlen = htons(skb->len - skb_network_offset(skb));
  1199. csum_replace2(&iph->check, iph->tot_len, newlen);
  1200. iph->tot_len = newlen;
  1201. rcu_read_lock();
  1202. ops = rcu_dereference(inet_protos[proto]);
  1203. if (WARN_ON(!ops || !ops->gro_complete))
  1204. goto out_unlock;
  1205. err = ops->gro_complete(skb);
  1206. out_unlock:
  1207. rcu_read_unlock();
  1208. return err;
  1209. }
  1210. int inet_ctl_sock_create(struct sock **sk, unsigned short family,
  1211. unsigned short type, unsigned char protocol,
  1212. struct net *net)
  1213. {
  1214. struct socket *sock;
  1215. int rc = sock_create_kern(family, type, protocol, &sock);
  1216. if (rc == 0) {
  1217. *sk = sock->sk;
  1218. (*sk)->sk_allocation = GFP_ATOMIC;
  1219. /*
  1220. * Unhash it so that IP input processing does not even see it,
  1221. * we do not wish this socket to see incoming packets.
  1222. */
  1223. (*sk)->sk_prot->unhash(*sk);
  1224. sk_change_net(*sk, net);
  1225. }
  1226. return rc;
  1227. }
  1228. EXPORT_SYMBOL_GPL(inet_ctl_sock_create);
  1229. unsigned long snmp_fold_field(void __percpu *mib[], int offt)
  1230. {
  1231. unsigned long res = 0;
  1232. int i;
  1233. for_each_possible_cpu(i) {
  1234. res += *(((unsigned long *) per_cpu_ptr(mib[0], i)) + offt);
  1235. res += *(((unsigned long *) per_cpu_ptr(mib[1], i)) + offt);
  1236. }
  1237. return res;
  1238. }
  1239. EXPORT_SYMBOL_GPL(snmp_fold_field);
  1240. #if BITS_PER_LONG==32
  1241. u64 snmp_fold_field64(void __percpu *mib[], int offt, size_t syncp_offset)
  1242. {
  1243. u64 res = 0;
  1244. int cpu;
  1245. for_each_possible_cpu(cpu) {
  1246. void *bhptr, *userptr;
  1247. struct u64_stats_sync *syncp;
  1248. u64 v_bh, v_user;
  1249. unsigned int start;
  1250. /* first mib used by softirq context, we must use _bh() accessors */
  1251. bhptr = per_cpu_ptr(SNMP_STAT_BHPTR(mib), cpu);
  1252. syncp = (struct u64_stats_sync *)(bhptr + syncp_offset);
  1253. do {
  1254. start = u64_stats_fetch_begin_bh(syncp);
  1255. v_bh = *(((u64 *) bhptr) + offt);
  1256. } while (u64_stats_fetch_retry_bh(syncp, start));
  1257. /* second mib used in USER context */
  1258. userptr = per_cpu_ptr(SNMP_STAT_USRPTR(mib), cpu);
  1259. syncp = (struct u64_stats_sync *)(userptr + syncp_offset);
  1260. do {
  1261. start = u64_stats_fetch_begin(syncp);
  1262. v_user = *(((u64 *) userptr) + offt);
  1263. } while (u64_stats_fetch_retry(syncp, start));
  1264. res += v_bh + v_user;
  1265. }
  1266. return res;
  1267. }
  1268. EXPORT_SYMBOL_GPL(snmp_fold_field64);
  1269. #endif
  1270. int snmp_mib_init(void __percpu *ptr[2], size_t mibsize, size_t align)
  1271. {
  1272. BUG_ON(ptr == NULL);
  1273. ptr[0] = __alloc_percpu(mibsize, align);
  1274. if (!ptr[0])
  1275. goto err0;
  1276. ptr[1] = __alloc_percpu(mibsize, align);
  1277. if (!ptr[1])
  1278. goto err1;
  1279. return 0;
  1280. err1:
  1281. free_percpu(ptr[0]);
  1282. ptr[0] = NULL;
  1283. err0:
  1284. return -ENOMEM;
  1285. }
  1286. EXPORT_SYMBOL_GPL(snmp_mib_init);
  1287. void snmp_mib_free(void __percpu *ptr[2])
  1288. {
  1289. BUG_ON(ptr == NULL);
  1290. free_percpu(ptr[0]);
  1291. free_percpu(ptr[1]);
  1292. ptr[0] = ptr[1] = NULL;
  1293. }
  1294. EXPORT_SYMBOL_GPL(snmp_mib_free);
  1295. #ifdef CONFIG_IP_MULTICAST
  1296. static const struct net_protocol igmp_protocol = {
  1297. .handler = igmp_rcv,
  1298. .netns_ok = 1,
  1299. };
  1300. #endif
  1301. static const struct net_protocol tcp_protocol = {
  1302. .handler = tcp_v4_rcv,
  1303. .err_handler = tcp_v4_err,
  1304. .gso_send_check = tcp_v4_gso_send_check,
  1305. .gso_segment = tcp_tso_segment,
  1306. .gro_receive = tcp4_gro_receive,
  1307. .gro_complete = tcp4_gro_complete,
  1308. .no_policy = 1,
  1309. .netns_ok = 1,
  1310. };
  1311. static const struct net_protocol udp_protocol = {
  1312. .handler = udp_rcv,
  1313. .err_handler = udp_err,
  1314. .gso_send_check = udp4_ufo_send_check,
  1315. .gso_segment = udp4_ufo_fragment,
  1316. .no_policy = 1,
  1317. .netns_ok = 1,
  1318. };
  1319. static const struct net_protocol icmp_protocol = {
  1320. .handler = icmp_rcv,
  1321. .err_handler = ping_err,
  1322. .no_policy = 1,
  1323. .netns_ok = 1,
  1324. };
  1325. static __net_init int ipv4_mib_init_net(struct net *net)
  1326. {
  1327. if (snmp_mib_init((void __percpu **)net->mib.tcp_statistics,
  1328. sizeof(struct tcp_mib),
  1329. __alignof__(struct tcp_mib)) < 0)
  1330. goto err_tcp_mib;
  1331. if (snmp_mib_init((void __percpu **)net->mib.ip_statistics,
  1332. sizeof(struct ipstats_mib),
  1333. __alignof__(struct ipstats_mib)) < 0)
  1334. goto err_ip_mib;
  1335. if (snmp_mib_init((void __percpu **)net->mib.net_statistics,
  1336. sizeof(struct linux_mib),
  1337. __alignof__(struct linux_mib)) < 0)
  1338. goto err_net_mib;
  1339. if (snmp_mib_init((void __percpu **)net->mib.udp_statistics,
  1340. sizeof(struct udp_mib),
  1341. __alignof__(struct udp_mib)) < 0)
  1342. goto err_udp_mib;
  1343. if (snmp_mib_init((void __percpu **)net->mib.udplite_statistics,
  1344. sizeof(struct udp_mib),
  1345. __alignof__(struct udp_mib)) < 0)
  1346. goto err_udplite_mib;
  1347. if (snmp_mib_init((void __percpu **)net->mib.icmp_statistics,
  1348. sizeof(struct icmp_mib),
  1349. __alignof__(struct icmp_mib)) < 0)
  1350. goto err_icmp_mib;
  1351. if (snmp_mib_init((void __percpu **)net->mib.icmpmsg_statistics,
  1352. sizeof(struct icmpmsg_mib),
  1353. __alignof__(struct icmpmsg_mib)) < 0)
  1354. goto err_icmpmsg_mib;
  1355. tcp_mib_init(net);
  1356. return 0;
  1357. err_icmpmsg_mib:
  1358. snmp_mib_free((void __percpu **)net->mib.icmp_statistics);
  1359. err_icmp_mib:
  1360. snmp_mib_free((void __percpu **)net->mib.udplite_statistics);
  1361. err_udplite_mib:
  1362. snmp_mib_free((void __percpu **)net->mib.udp_statistics);
  1363. err_udp_mib:
  1364. snmp_mib_free((void __percpu **)net->mib.net_statistics);
  1365. err_net_mib:
  1366. snmp_mib_free((void __percpu **)net->mib.ip_statistics);
  1367. err_ip_mib:
  1368. snmp_mib_free((void __percpu **)net->mib.tcp_statistics);
  1369. err_tcp_mib:
  1370. return -ENOMEM;
  1371. }
  1372. static __net_exit void ipv4_mib_exit_net(struct net *net)
  1373. {
  1374. snmp_mib_free((void __percpu **)net->mib.icmpmsg_statistics);
  1375. snmp_mib_free((void __percpu **)net->mib.icmp_statistics);
  1376. snmp_mib_free((void __percpu **)net->mib.udplite_statistics);
  1377. snmp_mib_free((void __percpu **)net->mib.udp_statistics);
  1378. snmp_mib_free((void __percpu **)net->mib.net_statistics);
  1379. snmp_mib_free((void __percpu **)net->mib.ip_statistics);
  1380. snmp_mib_free((void __percpu **)net->mib.tcp_statistics);
  1381. }
  1382. static __net_initdata struct pernet_operations ipv4_mib_ops = {
  1383. .init = ipv4_mib_init_net,
  1384. .exit = ipv4_mib_exit_net,
  1385. };
  1386. static int __init init_ipv4_mibs(void)
  1387. {
  1388. return register_pernet_subsys(&ipv4_mib_ops);
  1389. }
  1390. static int ipv4_proc_init(void);
  1391. /*
  1392. * IP protocol layer initialiser
  1393. */
  1394. static struct packet_type ip_packet_type __read_mostly = {
  1395. .type = cpu_to_be16(ETH_P_IP),
  1396. .func = ip_rcv,
  1397. .gso_send_check = inet_gso_send_check,
  1398. .gso_segment = inet_gso_segment,
  1399. .gro_receive = inet_gro_receive,
  1400. .gro_complete = inet_gro_complete,
  1401. };
  1402. static int __init inet_init(void)
  1403. {
  1404. struct sk_buff *dummy_skb;
  1405. struct inet_protosw *q;
  1406. struct list_head *r;
  1407. int rc = -EINVAL;
  1408. BUILD_BUG_ON(sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb));
  1409. sysctl_local_reserved_ports = kzalloc(65536 / 8, GFP_KERNEL);
  1410. if (!sysctl_local_reserved_ports)
  1411. goto out;
  1412. rc = proto_register(&tcp_prot, 1);
  1413. if (rc)
  1414. goto out_free_reserved_ports;
  1415. rc = proto_register(&udp_prot, 1);
  1416. if (rc)
  1417. goto out_unregister_tcp_proto;
  1418. rc = proto_register(&raw_prot, 1);
  1419. if (rc)
  1420. goto out_unregister_udp_proto;
  1421. rc = proto_register(&ping_prot, 1);
  1422. if (rc)
  1423. goto out_unregister_raw_proto;
  1424. /*
  1425. * Tell SOCKET that we are alive...
  1426. */
  1427. (void)sock_register(&inet_family_ops);
  1428. #ifdef CONFIG_SYSCTL
  1429. ip_static_sysctl_init();
  1430. #endif
  1431. /*
  1432. * Add all the base protocols.
  1433. */
  1434. if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
  1435. printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
  1436. if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
  1437. printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
  1438. if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
  1439. printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n");
  1440. #ifdef CONFIG_IP_MULTICAST
  1441. if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
  1442. printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n");
  1443. #endif
  1444. /* Register the socket-side information for inet_create. */
  1445. for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
  1446. INIT_LIST_HEAD(r);
  1447. for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
  1448. inet_register_protosw(q);
  1449. /*
  1450. * Set the ARP module up
  1451. */
  1452. arp_init();
  1453. /*
  1454. * Set the IP module up
  1455. */
  1456. ip_init();
  1457. tcp_v4_init();
  1458. /* Setup TCP slab cache for open requests. */
  1459. tcp_init();
  1460. /* Setup UDP memory threshold */
  1461. udp_init();
  1462. /* Add UDP-Lite (RFC 3828) */
  1463. udplite4_register();
  1464. ping_init();
  1465. /*
  1466. * Set the ICMP layer up
  1467. */
  1468. if (icmp_init() < 0)
  1469. panic("Failed to create the ICMP control socket.\n");
  1470. /*
  1471. * Initialise the multicast router
  1472. */
  1473. #if defined(CONFIG_IP_MROUTE)
  1474. if (ip_mr_init())
  1475. printk(KERN_CRIT "inet_init: Cannot init ipv4 mroute\n");
  1476. #endif
  1477. /*
  1478. * Initialise per-cpu ipv4 mibs
  1479. */
  1480. if (init_ipv4_mibs())
  1481. printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n");
  1482. ipv4_proc_init();
  1483. ipfrag_init();
  1484. dev_add_pack(&ip_packet_type);
  1485. rc = 0;
  1486. out:
  1487. return rc;
  1488. out_unregister_raw_proto:
  1489. proto_unregister(&raw_prot);
  1490. out_unregister_udp_proto:
  1491. proto_unregister(&udp_prot);
  1492. out_unregister_tcp_proto:
  1493. proto_unregister(&tcp_prot);
  1494. out_free_reserved_ports:
  1495. kfree(sysctl_local_reserved_ports);
  1496. goto out;
  1497. }
  1498. fs_initcall(inet_init);
  1499. /* ------------------------------------------------------------------------ */
  1500. #ifdef CONFIG_PROC_FS
  1501. static int __init ipv4_proc_init(void)
  1502. {
  1503. int rc = 0;
  1504. if (raw_proc_init())
  1505. goto out_raw;
  1506. if (tcp4_proc_init())
  1507. goto out_tcp;
  1508. if (udp4_proc_init())
  1509. goto out_udp;
  1510. if (ping_proc_init())
  1511. goto out_ping;
  1512. if (ip_misc_proc_init())
  1513. goto out_misc;
  1514. out:
  1515. return rc;
  1516. out_misc:
  1517. ping_proc_exit();
  1518. out_ping:
  1519. udp4_proc_exit();
  1520. out_udp:
  1521. tcp4_proc_exit();
  1522. out_tcp:
  1523. raw_proc_exit();
  1524. out_raw:
  1525. rc = -ENOMEM;
  1526. goto out;
  1527. }
  1528. #else /* CONFIG_PROC_FS */
  1529. static int __init ipv4_proc_init(void)
  1530. {
  1531. return 0;
  1532. }
  1533. #endif /* CONFIG_PROC_FS */
  1534. MODULE_ALIAS_NETPROTO(PF_INET);