af_inet.c 42 KB

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