af_inet.c 44 KB

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