af_inet.c 40 KB

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