af_inet.c 44 KB

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