af_inet.c 41 KB

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