af_inet.c 44 KB

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