af_inet.c 44 KB

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