af_inet.c 43 KB

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