af_inet.c 45 KB

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