af_inet.c 39 KB

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