af_inet.c 43 KB

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