af_inet.c 43 KB

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