af_inet.c 39 KB

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