af_inet.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684
  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. /* The inetsw table contains everything that inet_create needs to
  116. * build a new socket.
  117. */
  118. static struct list_head inetsw[SOCK_MAX];
  119. static DEFINE_SPINLOCK(inetsw_lock);
  120. struct ipv4_config ipv4_config;
  121. EXPORT_SYMBOL(ipv4_config);
  122. /* New destruction routine */
  123. void inet_sock_destruct(struct sock *sk)
  124. {
  125. struct inet_sock *inet = inet_sk(sk);
  126. __skb_queue_purge(&sk->sk_receive_queue);
  127. __skb_queue_purge(&sk->sk_error_queue);
  128. sk_mem_reclaim(sk);
  129. if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
  130. printk("Attempt to release TCP socket in state %d %p\n",
  131. sk->sk_state, sk);
  132. return;
  133. }
  134. if (!sock_flag(sk, SOCK_DEAD)) {
  135. printk("Attempt to release alive inet socket %p\n", sk);
  136. return;
  137. }
  138. WARN_ON(atomic_read(&sk->sk_rmem_alloc));
  139. WARN_ON(atomic_read(&sk->sk_wmem_alloc));
  140. WARN_ON(sk->sk_wmem_queued);
  141. WARN_ON(sk->sk_forward_alloc);
  142. kfree(inet->opt);
  143. dst_release(sk->sk_dst_cache);
  144. sk_refcnt_debug_dec(sk);
  145. }
  146. /*
  147. * The routines beyond this point handle the behaviour of an AF_INET
  148. * socket object. Mostly it punts to the subprotocols of IP to do
  149. * the work.
  150. */
  151. /*
  152. * Automatically bind an unbound socket.
  153. */
  154. static int inet_autobind(struct sock *sk)
  155. {
  156. struct inet_sock *inet;
  157. /* We may need to bind the socket. */
  158. lock_sock(sk);
  159. inet = inet_sk(sk);
  160. if (!inet->num) {
  161. if (sk->sk_prot->get_port(sk, 0)) {
  162. release_sock(sk);
  163. return -EAGAIN;
  164. }
  165. inet->sport = htons(inet->num);
  166. }
  167. release_sock(sk);
  168. return 0;
  169. }
  170. /*
  171. * Move a socket into listening state.
  172. */
  173. int inet_listen(struct socket *sock, int backlog)
  174. {
  175. struct sock *sk = sock->sk;
  176. unsigned char old_state;
  177. int err;
  178. lock_sock(sk);
  179. err = -EINVAL;
  180. if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
  181. goto out;
  182. old_state = sk->sk_state;
  183. if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
  184. goto out;
  185. /* Really, if the socket is already in listen state
  186. * we can only allow the backlog to be adjusted.
  187. */
  188. if (old_state != TCP_LISTEN) {
  189. err = inet_csk_listen_start(sk, backlog);
  190. if (err)
  191. goto out;
  192. }
  193. sk->sk_max_ack_backlog = backlog;
  194. err = 0;
  195. out:
  196. release_sock(sk);
  197. return err;
  198. }
  199. u32 inet_ehash_secret __read_mostly;
  200. EXPORT_SYMBOL(inet_ehash_secret);
  201. /*
  202. * inet_ehash_secret must be set exactly once
  203. * Instead of using a dedicated spinlock, we (ab)use inetsw_lock
  204. */
  205. void build_ehash_secret(void)
  206. {
  207. u32 rnd;
  208. do {
  209. get_random_bytes(&rnd, sizeof(rnd));
  210. } while (rnd == 0);
  211. spin_lock_bh(&inetsw_lock);
  212. if (!inet_ehash_secret)
  213. inet_ehash_secret = rnd;
  214. spin_unlock_bh(&inetsw_lock);
  215. }
  216. EXPORT_SYMBOL(build_ehash_secret);
  217. static inline int inet_netns_ok(struct net *net, int protocol)
  218. {
  219. int hash;
  220. struct net_protocol *ipprot;
  221. if (net_eq(net, &init_net))
  222. return 1;
  223. hash = protocol & (MAX_INET_PROTOS - 1);
  224. ipprot = rcu_dereference(inet_protos[hash]);
  225. if (ipprot == NULL)
  226. /* raw IP is OK */
  227. return 1;
  228. return ipprot->netns_ok;
  229. }
  230. /*
  231. * Create an inet socket.
  232. */
  233. static int inet_create(struct net *net, struct socket *sock, int protocol)
  234. {
  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 (answer->capability > 0 && !capable(answer->capability))
  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. if (SOCK_RAW == sock->type) {
  312. inet->num = protocol;
  313. if (IPPROTO_RAW == protocol)
  314. inet->hdrincl = 1;
  315. }
  316. if (ipv4_config.no_pmtu_disc)
  317. inet->pmtudisc = IP_PMTUDISC_DONT;
  318. else
  319. inet->pmtudisc = IP_PMTUDISC_WANT;
  320. inet->id = 0;
  321. sock_init_data(sock, sk);
  322. sk->sk_destruct = inet_sock_destruct;
  323. sk->sk_protocol = protocol;
  324. sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
  325. inet->uc_ttl = -1;
  326. inet->mc_loop = 1;
  327. inet->mc_ttl = 1;
  328. inet->mc_all = 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. unsigned int hlen;
  1078. unsigned int off;
  1079. unsigned int id;
  1080. int flush = 1;
  1081. int proto;
  1082. off = skb_gro_offset(skb);
  1083. hlen = off + sizeof(*iph);
  1084. iph = skb_gro_header_fast(skb, off);
  1085. if (skb_gro_header_hard(skb, hlen)) {
  1086. iph = skb_gro_header_slow(skb, hlen, off);
  1087. if (unlikely(!iph))
  1088. goto out;
  1089. }
  1090. proto = iph->protocol & (MAX_INET_PROTOS - 1);
  1091. rcu_read_lock();
  1092. ops = rcu_dereference(inet_protos[proto]);
  1093. if (!ops || !ops->gro_receive)
  1094. goto out_unlock;
  1095. if (*(u8 *)iph != 0x45)
  1096. goto out_unlock;
  1097. if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
  1098. goto out_unlock;
  1099. id = ntohl(*(u32 *)&iph->id);
  1100. flush = (u16)((ntohl(*(u32 *)iph) ^ skb_gro_len(skb)) | (id ^ IP_DF));
  1101. id >>= 16;
  1102. for (p = *head; p; p = p->next) {
  1103. struct iphdr *iph2;
  1104. if (!NAPI_GRO_CB(p)->same_flow)
  1105. continue;
  1106. iph2 = ip_hdr(p);
  1107. if ((iph->protocol ^ iph2->protocol) |
  1108. (iph->tos ^ iph2->tos) |
  1109. (iph->saddr ^ iph2->saddr) |
  1110. (iph->daddr ^ iph2->daddr)) {
  1111. NAPI_GRO_CB(p)->same_flow = 0;
  1112. continue;
  1113. }
  1114. /* All fields must match except length and checksum. */
  1115. NAPI_GRO_CB(p)->flush |=
  1116. (iph->ttl ^ iph2->ttl) |
  1117. ((u16)(ntohs(iph2->id) + NAPI_GRO_CB(p)->count) ^ id);
  1118. NAPI_GRO_CB(p)->flush |= flush;
  1119. }
  1120. NAPI_GRO_CB(skb)->flush |= flush;
  1121. skb_gro_pull(skb, sizeof(*iph));
  1122. skb_set_transport_header(skb, skb_gro_offset(skb));
  1123. pp = ops->gro_receive(head, skb);
  1124. out_unlock:
  1125. rcu_read_unlock();
  1126. out:
  1127. NAPI_GRO_CB(skb)->flush |= flush;
  1128. return pp;
  1129. }
  1130. static int inet_gro_complete(struct sk_buff *skb)
  1131. {
  1132. struct net_protocol *ops;
  1133. struct iphdr *iph = ip_hdr(skb);
  1134. int proto = iph->protocol & (MAX_INET_PROTOS - 1);
  1135. int err = -ENOSYS;
  1136. __be16 newlen = htons(skb->len - skb_network_offset(skb));
  1137. csum_replace2(&iph->check, iph->tot_len, newlen);
  1138. iph->tot_len = newlen;
  1139. rcu_read_lock();
  1140. ops = rcu_dereference(inet_protos[proto]);
  1141. if (WARN_ON(!ops || !ops->gro_complete))
  1142. goto out_unlock;
  1143. err = ops->gro_complete(skb);
  1144. out_unlock:
  1145. rcu_read_unlock();
  1146. return err;
  1147. }
  1148. int inet_ctl_sock_create(struct sock **sk, unsigned short family,
  1149. unsigned short type, unsigned char protocol,
  1150. struct net *net)
  1151. {
  1152. struct socket *sock;
  1153. int rc = sock_create_kern(family, type, protocol, &sock);
  1154. if (rc == 0) {
  1155. *sk = sock->sk;
  1156. (*sk)->sk_allocation = GFP_ATOMIC;
  1157. /*
  1158. * Unhash it so that IP input processing does not even see it,
  1159. * we do not wish this socket to see incoming packets.
  1160. */
  1161. (*sk)->sk_prot->unhash(*sk);
  1162. sk_change_net(*sk, net);
  1163. }
  1164. return rc;
  1165. }
  1166. EXPORT_SYMBOL_GPL(inet_ctl_sock_create);
  1167. unsigned long snmp_fold_field(void *mib[], int offt)
  1168. {
  1169. unsigned long res = 0;
  1170. int i;
  1171. for_each_possible_cpu(i) {
  1172. res += *(((unsigned long *) per_cpu_ptr(mib[0], i)) + offt);
  1173. res += *(((unsigned long *) per_cpu_ptr(mib[1], i)) + offt);
  1174. }
  1175. return res;
  1176. }
  1177. EXPORT_SYMBOL_GPL(snmp_fold_field);
  1178. int snmp_mib_init(void *ptr[2], size_t mibsize)
  1179. {
  1180. BUG_ON(ptr == NULL);
  1181. ptr[0] = __alloc_percpu(mibsize, __alignof__(unsigned long long));
  1182. if (!ptr[0])
  1183. goto err0;
  1184. ptr[1] = __alloc_percpu(mibsize, __alignof__(unsigned long long));
  1185. if (!ptr[1])
  1186. goto err1;
  1187. return 0;
  1188. err1:
  1189. free_percpu(ptr[0]);
  1190. ptr[0] = NULL;
  1191. err0:
  1192. return -ENOMEM;
  1193. }
  1194. EXPORT_SYMBOL_GPL(snmp_mib_init);
  1195. void snmp_mib_free(void *ptr[2])
  1196. {
  1197. BUG_ON(ptr == NULL);
  1198. free_percpu(ptr[0]);
  1199. free_percpu(ptr[1]);
  1200. ptr[0] = ptr[1] = NULL;
  1201. }
  1202. EXPORT_SYMBOL_GPL(snmp_mib_free);
  1203. #ifdef CONFIG_IP_MULTICAST
  1204. static struct net_protocol igmp_protocol = {
  1205. .handler = igmp_rcv,
  1206. .netns_ok = 1,
  1207. };
  1208. #endif
  1209. static struct net_protocol tcp_protocol = {
  1210. .handler = tcp_v4_rcv,
  1211. .err_handler = tcp_v4_err,
  1212. .gso_send_check = tcp_v4_gso_send_check,
  1213. .gso_segment = tcp_tso_segment,
  1214. .gro_receive = tcp4_gro_receive,
  1215. .gro_complete = tcp4_gro_complete,
  1216. .no_policy = 1,
  1217. .netns_ok = 1,
  1218. };
  1219. static struct net_protocol udp_protocol = {
  1220. .handler = udp_rcv,
  1221. .err_handler = udp_err,
  1222. .no_policy = 1,
  1223. .netns_ok = 1,
  1224. };
  1225. static struct net_protocol icmp_protocol = {
  1226. .handler = icmp_rcv,
  1227. .no_policy = 1,
  1228. .netns_ok = 1,
  1229. };
  1230. static __net_init int ipv4_mib_init_net(struct net *net)
  1231. {
  1232. if (snmp_mib_init((void **)net->mib.tcp_statistics,
  1233. sizeof(struct tcp_mib)) < 0)
  1234. goto err_tcp_mib;
  1235. if (snmp_mib_init((void **)net->mib.ip_statistics,
  1236. sizeof(struct ipstats_mib)) < 0)
  1237. goto err_ip_mib;
  1238. if (snmp_mib_init((void **)net->mib.net_statistics,
  1239. sizeof(struct linux_mib)) < 0)
  1240. goto err_net_mib;
  1241. if (snmp_mib_init((void **)net->mib.udp_statistics,
  1242. sizeof(struct udp_mib)) < 0)
  1243. goto err_udp_mib;
  1244. if (snmp_mib_init((void **)net->mib.udplite_statistics,
  1245. sizeof(struct udp_mib)) < 0)
  1246. goto err_udplite_mib;
  1247. if (snmp_mib_init((void **)net->mib.icmp_statistics,
  1248. sizeof(struct icmp_mib)) < 0)
  1249. goto err_icmp_mib;
  1250. if (snmp_mib_init((void **)net->mib.icmpmsg_statistics,
  1251. sizeof(struct icmpmsg_mib)) < 0)
  1252. goto err_icmpmsg_mib;
  1253. tcp_mib_init(net);
  1254. return 0;
  1255. err_icmpmsg_mib:
  1256. snmp_mib_free((void **)net->mib.icmp_statistics);
  1257. err_icmp_mib:
  1258. snmp_mib_free((void **)net->mib.udplite_statistics);
  1259. err_udplite_mib:
  1260. snmp_mib_free((void **)net->mib.udp_statistics);
  1261. err_udp_mib:
  1262. snmp_mib_free((void **)net->mib.net_statistics);
  1263. err_net_mib:
  1264. snmp_mib_free((void **)net->mib.ip_statistics);
  1265. err_ip_mib:
  1266. snmp_mib_free((void **)net->mib.tcp_statistics);
  1267. err_tcp_mib:
  1268. return -ENOMEM;
  1269. }
  1270. static __net_exit void ipv4_mib_exit_net(struct net *net)
  1271. {
  1272. snmp_mib_free((void **)net->mib.icmpmsg_statistics);
  1273. snmp_mib_free((void **)net->mib.icmp_statistics);
  1274. snmp_mib_free((void **)net->mib.udplite_statistics);
  1275. snmp_mib_free((void **)net->mib.udp_statistics);
  1276. snmp_mib_free((void **)net->mib.net_statistics);
  1277. snmp_mib_free((void **)net->mib.ip_statistics);
  1278. snmp_mib_free((void **)net->mib.tcp_statistics);
  1279. }
  1280. static __net_initdata struct pernet_operations ipv4_mib_ops = {
  1281. .init = ipv4_mib_init_net,
  1282. .exit = ipv4_mib_exit_net,
  1283. };
  1284. static int __init init_ipv4_mibs(void)
  1285. {
  1286. return register_pernet_subsys(&ipv4_mib_ops);
  1287. }
  1288. static int ipv4_proc_init(void);
  1289. /*
  1290. * IP protocol layer initialiser
  1291. */
  1292. static struct packet_type ip_packet_type __read_mostly = {
  1293. .type = cpu_to_be16(ETH_P_IP),
  1294. .func = ip_rcv,
  1295. .gso_send_check = inet_gso_send_check,
  1296. .gso_segment = inet_gso_segment,
  1297. .gro_receive = inet_gro_receive,
  1298. .gro_complete = inet_gro_complete,
  1299. };
  1300. static int __init inet_init(void)
  1301. {
  1302. struct sk_buff *dummy_skb;
  1303. struct inet_protosw *q;
  1304. struct list_head *r;
  1305. int rc = -EINVAL;
  1306. BUILD_BUG_ON(sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb));
  1307. rc = proto_register(&tcp_prot, 1);
  1308. if (rc)
  1309. goto out;
  1310. rc = proto_register(&udp_prot, 1);
  1311. if (rc)
  1312. goto out_unregister_tcp_proto;
  1313. rc = proto_register(&raw_prot, 1);
  1314. if (rc)
  1315. goto out_unregister_udp_proto;
  1316. /*
  1317. * Tell SOCKET that we are alive...
  1318. */
  1319. (void)sock_register(&inet_family_ops);
  1320. #ifdef CONFIG_SYSCTL
  1321. ip_static_sysctl_init();
  1322. #endif
  1323. /*
  1324. * Add all the base protocols.
  1325. */
  1326. if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
  1327. printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
  1328. if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
  1329. printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
  1330. if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
  1331. printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n");
  1332. #ifdef CONFIG_IP_MULTICAST
  1333. if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
  1334. printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n");
  1335. #endif
  1336. /* Register the socket-side information for inet_create. */
  1337. for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
  1338. INIT_LIST_HEAD(r);
  1339. for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
  1340. inet_register_protosw(q);
  1341. /*
  1342. * Set the ARP module up
  1343. */
  1344. arp_init();
  1345. /*
  1346. * Set the IP module up
  1347. */
  1348. ip_init();
  1349. tcp_v4_init();
  1350. /* Setup TCP slab cache for open requests. */
  1351. tcp_init();
  1352. /* Setup UDP memory threshold */
  1353. udp_init();
  1354. /* Add UDP-Lite (RFC 3828) */
  1355. udplite4_register();
  1356. /*
  1357. * Set the ICMP layer up
  1358. */
  1359. if (icmp_init() < 0)
  1360. panic("Failed to create the ICMP control socket.\n");
  1361. /*
  1362. * Initialise the multicast router
  1363. */
  1364. #if defined(CONFIG_IP_MROUTE)
  1365. if (ip_mr_init())
  1366. printk(KERN_CRIT "inet_init: Cannot init ipv4 mroute\n");
  1367. #endif
  1368. /*
  1369. * Initialise per-cpu ipv4 mibs
  1370. */
  1371. if (init_ipv4_mibs())
  1372. printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n");
  1373. ipv4_proc_init();
  1374. ipfrag_init();
  1375. dev_add_pack(&ip_packet_type);
  1376. rc = 0;
  1377. out:
  1378. return rc;
  1379. out_unregister_udp_proto:
  1380. proto_unregister(&udp_prot);
  1381. out_unregister_tcp_proto:
  1382. proto_unregister(&tcp_prot);
  1383. goto out;
  1384. }
  1385. fs_initcall(inet_init);
  1386. /* ------------------------------------------------------------------------ */
  1387. #ifdef CONFIG_PROC_FS
  1388. static int __init ipv4_proc_init(void)
  1389. {
  1390. int rc = 0;
  1391. if (raw_proc_init())
  1392. goto out_raw;
  1393. if (tcp4_proc_init())
  1394. goto out_tcp;
  1395. if (udp4_proc_init())
  1396. goto out_udp;
  1397. if (ip_misc_proc_init())
  1398. goto out_misc;
  1399. out:
  1400. return rc;
  1401. out_misc:
  1402. udp4_proc_exit();
  1403. out_udp:
  1404. tcp4_proc_exit();
  1405. out_tcp:
  1406. raw_proc_exit();
  1407. out_raw:
  1408. rc = -ENOMEM;
  1409. goto out;
  1410. }
  1411. #else /* CONFIG_PROC_FS */
  1412. static int __init ipv4_proc_init(void)
  1413. {
  1414. return 0;
  1415. }
  1416. #endif /* CONFIG_PROC_FS */
  1417. MODULE_ALIAS_NETPROTO(PF_INET);
  1418. EXPORT_SYMBOL(inet_accept);
  1419. EXPORT_SYMBOL(inet_bind);
  1420. EXPORT_SYMBOL(inet_dgram_connect);
  1421. EXPORT_SYMBOL(inet_dgram_ops);
  1422. EXPORT_SYMBOL(inet_getname);
  1423. EXPORT_SYMBOL(inet_ioctl);
  1424. EXPORT_SYMBOL(inet_listen);
  1425. EXPORT_SYMBOL(inet_register_protosw);
  1426. EXPORT_SYMBOL(inet_release);
  1427. EXPORT_SYMBOL(inet_sendmsg);
  1428. EXPORT_SYMBOL(inet_shutdown);
  1429. EXPORT_SYMBOL(inet_sock_destruct);
  1430. EXPORT_SYMBOL(inet_stream_connect);
  1431. EXPORT_SYMBOL(inet_stream_ops);
  1432. EXPORT_SYMBOL(inet_unregister_protosw);
  1433. EXPORT_SYMBOL(sysctl_ip_nonlocal_bind);