af_inet.c 44 KB

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