af_inet.c 39 KB

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