af_inet.c 45 KB

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