af_inet.c 42 KB

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