af_inet.c 45 KB

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