af_inet.c 45 KB

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