af_inet.c 42 KB

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