af_inet.c 43 KB

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