af_inet.c 44 KB

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