af_inet.c 44 KB

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