af_inet.c 42 KB

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