af_inet.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679
  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. extern void ip_mc_drop_socket(struct sock *sk);
  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. printk("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. printk("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. /*
  148. * The routines beyond this point handle the behaviour of an AF_INET
  149. * socket object. Mostly it punts to the subprotocols of IP to do
  150. * the work.
  151. */
  152. /*
  153. * Automatically bind an unbound socket.
  154. */
  155. static int inet_autobind(struct sock *sk)
  156. {
  157. struct inet_sock *inet;
  158. /* We may need to bind the socket. */
  159. lock_sock(sk);
  160. inet = inet_sk(sk);
  161. if (!inet->num) {
  162. if (sk->sk_prot->get_port(sk, 0)) {
  163. release_sock(sk);
  164. return -EAGAIN;
  165. }
  166. inet->sport = htons(inet->num);
  167. }
  168. release_sock(sk);
  169. return 0;
  170. }
  171. /*
  172. * Move a socket into listening state.
  173. */
  174. int inet_listen(struct socket *sock, int backlog)
  175. {
  176. struct sock *sk = sock->sk;
  177. unsigned char old_state;
  178. int err;
  179. lock_sock(sk);
  180. err = -EINVAL;
  181. if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
  182. goto out;
  183. old_state = sk->sk_state;
  184. if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
  185. goto out;
  186. /* Really, if the socket is already in listen state
  187. * we can only allow the backlog to be adjusted.
  188. */
  189. if (old_state != TCP_LISTEN) {
  190. err = inet_csk_listen_start(sk, backlog);
  191. if (err)
  192. goto out;
  193. }
  194. sk->sk_max_ack_backlog = backlog;
  195. err = 0;
  196. out:
  197. release_sock(sk);
  198. return err;
  199. }
  200. u32 inet_ehash_secret __read_mostly;
  201. EXPORT_SYMBOL(inet_ehash_secret);
  202. /*
  203. * inet_ehash_secret must be set exactly once
  204. * Instead of using a dedicated spinlock, we (ab)use inetsw_lock
  205. */
  206. void build_ehash_secret(void)
  207. {
  208. u32 rnd;
  209. do {
  210. get_random_bytes(&rnd, sizeof(rnd));
  211. } while (rnd == 0);
  212. spin_lock_bh(&inetsw_lock);
  213. if (!inet_ehash_secret)
  214. inet_ehash_secret = rnd;
  215. spin_unlock_bh(&inetsw_lock);
  216. }
  217. EXPORT_SYMBOL(build_ehash_secret);
  218. static inline int inet_netns_ok(struct net *net, int protocol)
  219. {
  220. int hash;
  221. struct net_protocol *ipprot;
  222. if (net_eq(net, &init_net))
  223. return 1;
  224. hash = protocol & (MAX_INET_PROTOS - 1);
  225. ipprot = rcu_dereference(inet_protos[hash]);
  226. if (ipprot == NULL)
  227. /* raw IP is OK */
  228. return 1;
  229. return ipprot->netns_ok;
  230. }
  231. /*
  232. * Create an inet socket.
  233. */
  234. static int inet_create(struct net *net, struct socket *sock, int protocol)
  235. {
  236. struct sock *sk;
  237. struct inet_protosw *answer;
  238. struct inet_sock *inet;
  239. struct proto *answer_prot;
  240. unsigned char answer_flags;
  241. char answer_no_check;
  242. int try_loading_module = 0;
  243. int err;
  244. if (unlikely(!inet_ehash_secret))
  245. if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
  246. build_ehash_secret();
  247. sock->state = SS_UNCONNECTED;
  248. /* Look for the requested type/protocol pair. */
  249. lookup_protocol:
  250. err = -ESOCKTNOSUPPORT;
  251. rcu_read_lock();
  252. list_for_each_entry_rcu(answer, &inetsw[sock->type], list) {
  253. err = 0;
  254. /* Check the non-wild match. */
  255. if (protocol == answer->protocol) {
  256. if (protocol != IPPROTO_IP)
  257. break;
  258. } else {
  259. /* Check for the two wild cases. */
  260. if (IPPROTO_IP == protocol) {
  261. protocol = answer->protocol;
  262. break;
  263. }
  264. if (IPPROTO_IP == answer->protocol)
  265. break;
  266. }
  267. err = -EPROTONOSUPPORT;
  268. }
  269. if (unlikely(err)) {
  270. if (try_loading_module < 2) {
  271. rcu_read_unlock();
  272. /*
  273. * Be more specific, e.g. net-pf-2-proto-132-type-1
  274. * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM)
  275. */
  276. if (++try_loading_module == 1)
  277. request_module("net-pf-%d-proto-%d-type-%d",
  278. PF_INET, protocol, sock->type);
  279. /*
  280. * Fall back to generic, e.g. net-pf-2-proto-132
  281. * (net-pf-PF_INET-proto-IPPROTO_SCTP)
  282. */
  283. else
  284. request_module("net-pf-%d-proto-%d",
  285. PF_INET, protocol);
  286. goto lookup_protocol;
  287. } else
  288. goto out_rcu_unlock;
  289. }
  290. err = -EPERM;
  291. if (answer->capability > 0 && !capable(answer->capability))
  292. goto out_rcu_unlock;
  293. err = -EAFNOSUPPORT;
  294. if (!inet_netns_ok(net, protocol))
  295. goto out_rcu_unlock;
  296. sock->ops = answer->ops;
  297. answer_prot = answer->prot;
  298. answer_no_check = answer->no_check;
  299. answer_flags = answer->flags;
  300. rcu_read_unlock();
  301. WARN_ON(answer_prot->slab == NULL);
  302. err = -ENOBUFS;
  303. sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot);
  304. if (sk == NULL)
  305. goto out;
  306. err = 0;
  307. sk->sk_no_check = answer_no_check;
  308. if (INET_PROTOSW_REUSE & answer_flags)
  309. sk->sk_reuse = 1;
  310. inet = inet_sk(sk);
  311. inet->is_icsk = (INET_PROTOSW_ICSK & answer_flags) != 0;
  312. if (SOCK_RAW == sock->type) {
  313. inet->num = protocol;
  314. if (IPPROTO_RAW == protocol)
  315. inet->hdrincl = 1;
  316. }
  317. if (ipv4_config.no_pmtu_disc)
  318. inet->pmtudisc = IP_PMTUDISC_DONT;
  319. else
  320. inet->pmtudisc = IP_PMTUDISC_WANT;
  321. inet->id = 0;
  322. sock_init_data(sock, sk);
  323. sk->sk_destruct = inet_sock_destruct;
  324. sk->sk_protocol = protocol;
  325. sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
  326. inet->uc_ttl = -1;
  327. inet->mc_loop = 1;
  328. inet->mc_ttl = 1;
  329. inet->mc_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. synchronize_net();
  875. return;
  876. out_permanent:
  877. printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
  878. protocol);
  879. goto out;
  880. out_illegal:
  881. printk(KERN_ERR
  882. "Ignoring attempt to register invalid socket type %d.\n",
  883. p->type);
  884. goto out;
  885. }
  886. void inet_unregister_protosw(struct inet_protosw *p)
  887. {
  888. if (INET_PROTOSW_PERMANENT & p->flags) {
  889. printk(KERN_ERR
  890. "Attempt to unregister permanent protocol %d.\n",
  891. p->protocol);
  892. } else {
  893. spin_lock_bh(&inetsw_lock);
  894. list_del_rcu(&p->list);
  895. spin_unlock_bh(&inetsw_lock);
  896. synchronize_net();
  897. }
  898. }
  899. /*
  900. * Shall we try to damage output packets if routing dev changes?
  901. */
  902. int sysctl_ip_dynaddr __read_mostly;
  903. static int inet_sk_reselect_saddr(struct sock *sk)
  904. {
  905. struct inet_sock *inet = inet_sk(sk);
  906. int err;
  907. struct rtable *rt;
  908. __be32 old_saddr = inet->saddr;
  909. __be32 new_saddr;
  910. __be32 daddr = inet->daddr;
  911. if (inet->opt && inet->opt->srr)
  912. daddr = inet->opt->faddr;
  913. /* Query new route. */
  914. err = ip_route_connect(&rt, daddr, 0,
  915. RT_CONN_FLAGS(sk),
  916. sk->sk_bound_dev_if,
  917. sk->sk_protocol,
  918. inet->sport, inet->dport, sk, 0);
  919. if (err)
  920. return err;
  921. sk_setup_caps(sk, &rt->u.dst);
  922. new_saddr = rt->rt_src;
  923. if (new_saddr == old_saddr)
  924. return 0;
  925. if (sysctl_ip_dynaddr > 1) {
  926. printk(KERN_INFO "%s(): shifting inet->saddr from %pI4 to %pI4\n",
  927. __func__, &old_saddr, &new_saddr);
  928. }
  929. inet->saddr = inet->rcv_saddr = new_saddr;
  930. /*
  931. * XXX The only one ugly spot where we need to
  932. * XXX really change the sockets identity after
  933. * XXX it has entered the hashes. -DaveM
  934. *
  935. * Besides that, it does not check for connection
  936. * uniqueness. Wait for troubles.
  937. */
  938. __sk_prot_rehash(sk);
  939. return 0;
  940. }
  941. int inet_sk_rebuild_header(struct sock *sk)
  942. {
  943. struct inet_sock *inet = inet_sk(sk);
  944. struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0);
  945. __be32 daddr;
  946. int err;
  947. /* Route is OK, nothing to do. */
  948. if (rt)
  949. return 0;
  950. /* Reroute. */
  951. daddr = inet->daddr;
  952. if (inet->opt && inet->opt->srr)
  953. daddr = inet->opt->faddr;
  954. {
  955. struct flowi fl = {
  956. .oif = sk->sk_bound_dev_if,
  957. .nl_u = {
  958. .ip4_u = {
  959. .daddr = daddr,
  960. .saddr = inet->saddr,
  961. .tos = RT_CONN_FLAGS(sk),
  962. },
  963. },
  964. .proto = sk->sk_protocol,
  965. .flags = inet_sk_flowi_flags(sk),
  966. .uli_u = {
  967. .ports = {
  968. .sport = inet->sport,
  969. .dport = inet->dport,
  970. },
  971. },
  972. };
  973. security_sk_classify_flow(sk, &fl);
  974. err = ip_route_output_flow(sock_net(sk), &rt, &fl, sk, 0);
  975. }
  976. if (!err)
  977. sk_setup_caps(sk, &rt->u.dst);
  978. else {
  979. /* Routing failed... */
  980. sk->sk_route_caps = 0;
  981. /*
  982. * Other protocols have to map its equivalent state to TCP_SYN_SENT.
  983. * DCCP maps its DCCP_REQUESTING state to TCP_SYN_SENT. -acme
  984. */
  985. if (!sysctl_ip_dynaddr ||
  986. sk->sk_state != TCP_SYN_SENT ||
  987. (sk->sk_userlocks & SOCK_BINDADDR_LOCK) ||
  988. (err = inet_sk_reselect_saddr(sk)) != 0)
  989. sk->sk_err_soft = -err;
  990. }
  991. return err;
  992. }
  993. EXPORT_SYMBOL(inet_sk_rebuild_header);
  994. static int inet_gso_send_check(struct sk_buff *skb)
  995. {
  996. struct iphdr *iph;
  997. struct net_protocol *ops;
  998. int proto;
  999. int ihl;
  1000. int err = -EINVAL;
  1001. if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
  1002. goto out;
  1003. iph = ip_hdr(skb);
  1004. ihl = iph->ihl * 4;
  1005. if (ihl < sizeof(*iph))
  1006. goto out;
  1007. if (unlikely(!pskb_may_pull(skb, ihl)))
  1008. goto out;
  1009. __skb_pull(skb, ihl);
  1010. skb_reset_transport_header(skb);
  1011. iph = ip_hdr(skb);
  1012. proto = iph->protocol & (MAX_INET_PROTOS - 1);
  1013. err = -EPROTONOSUPPORT;
  1014. rcu_read_lock();
  1015. ops = rcu_dereference(inet_protos[proto]);
  1016. if (likely(ops && ops->gso_send_check))
  1017. err = ops->gso_send_check(skb);
  1018. rcu_read_unlock();
  1019. out:
  1020. return err;
  1021. }
  1022. static struct sk_buff *inet_gso_segment(struct sk_buff *skb, int features)
  1023. {
  1024. struct sk_buff *segs = ERR_PTR(-EINVAL);
  1025. struct iphdr *iph;
  1026. struct net_protocol *ops;
  1027. int proto;
  1028. int ihl;
  1029. int id;
  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. iph->id = htons(id++);
  1064. iph->tot_len = htons(skb->len - skb->mac_len);
  1065. iph->check = 0;
  1066. iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl);
  1067. } while ((skb = skb->next));
  1068. out:
  1069. return segs;
  1070. }
  1071. static struct sk_buff **inet_gro_receive(struct sk_buff **head,
  1072. struct sk_buff *skb)
  1073. {
  1074. struct net_protocol *ops;
  1075. struct sk_buff **pp = NULL;
  1076. struct sk_buff *p;
  1077. struct iphdr *iph;
  1078. int flush = 1;
  1079. int proto;
  1080. int id;
  1081. iph = skb_gro_header(skb, sizeof(*iph));
  1082. if (unlikely(!iph))
  1083. goto out;
  1084. proto = iph->protocol & (MAX_INET_PROTOS - 1);
  1085. rcu_read_lock();
  1086. ops = rcu_dereference(inet_protos[proto]);
  1087. if (!ops || !ops->gro_receive)
  1088. goto out_unlock;
  1089. if (*(u8 *)iph != 0x45)
  1090. goto out_unlock;
  1091. if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
  1092. goto out_unlock;
  1093. flush = ntohs(iph->tot_len) != skb_gro_len(skb) ||
  1094. iph->frag_off != htons(IP_DF);
  1095. id = ntohs(iph->id);
  1096. for (p = *head; p; p = p->next) {
  1097. struct iphdr *iph2;
  1098. if (!NAPI_GRO_CB(p)->same_flow)
  1099. continue;
  1100. iph2 = ip_hdr(p);
  1101. if ((iph->protocol ^ iph2->protocol) |
  1102. (iph->tos ^ iph2->tos) |
  1103. (iph->saddr ^ iph2->saddr) |
  1104. (iph->daddr ^ iph2->daddr)) {
  1105. NAPI_GRO_CB(p)->same_flow = 0;
  1106. continue;
  1107. }
  1108. /* All fields must match except length and checksum. */
  1109. NAPI_GRO_CB(p)->flush |=
  1110. (iph->ttl ^ iph2->ttl) |
  1111. ((u16)(ntohs(iph2->id) + NAPI_GRO_CB(p)->count) ^ id);
  1112. NAPI_GRO_CB(p)->flush |= flush;
  1113. }
  1114. NAPI_GRO_CB(skb)->flush |= flush;
  1115. skb_gro_pull(skb, sizeof(*iph));
  1116. skb_set_transport_header(skb, skb_gro_offset(skb));
  1117. pp = ops->gro_receive(head, skb);
  1118. out_unlock:
  1119. rcu_read_unlock();
  1120. out:
  1121. NAPI_GRO_CB(skb)->flush |= flush;
  1122. return pp;
  1123. }
  1124. static int inet_gro_complete(struct sk_buff *skb)
  1125. {
  1126. struct net_protocol *ops;
  1127. struct iphdr *iph = ip_hdr(skb);
  1128. int proto = iph->protocol & (MAX_INET_PROTOS - 1);
  1129. int err = -ENOSYS;
  1130. __be16 newlen = htons(skb->len - skb_network_offset(skb));
  1131. csum_replace2(&iph->check, iph->tot_len, newlen);
  1132. iph->tot_len = newlen;
  1133. rcu_read_lock();
  1134. ops = rcu_dereference(inet_protos[proto]);
  1135. if (WARN_ON(!ops || !ops->gro_complete))
  1136. goto out_unlock;
  1137. err = ops->gro_complete(skb);
  1138. out_unlock:
  1139. rcu_read_unlock();
  1140. return err;
  1141. }
  1142. int inet_ctl_sock_create(struct sock **sk, unsigned short family,
  1143. unsigned short type, unsigned char protocol,
  1144. struct net *net)
  1145. {
  1146. struct socket *sock;
  1147. int rc = sock_create_kern(family, type, protocol, &sock);
  1148. if (rc == 0) {
  1149. *sk = sock->sk;
  1150. (*sk)->sk_allocation = GFP_ATOMIC;
  1151. /*
  1152. * Unhash it so that IP input processing does not even see it,
  1153. * we do not wish this socket to see incoming packets.
  1154. */
  1155. (*sk)->sk_prot->unhash(*sk);
  1156. sk_change_net(*sk, net);
  1157. }
  1158. return rc;
  1159. }
  1160. EXPORT_SYMBOL_GPL(inet_ctl_sock_create);
  1161. unsigned long snmp_fold_field(void *mib[], int offt)
  1162. {
  1163. unsigned long res = 0;
  1164. int i;
  1165. for_each_possible_cpu(i) {
  1166. res += *(((unsigned long *) per_cpu_ptr(mib[0], i)) + offt);
  1167. res += *(((unsigned long *) per_cpu_ptr(mib[1], i)) + offt);
  1168. }
  1169. return res;
  1170. }
  1171. EXPORT_SYMBOL_GPL(snmp_fold_field);
  1172. int snmp_mib_init(void *ptr[2], size_t mibsize)
  1173. {
  1174. BUG_ON(ptr == NULL);
  1175. ptr[0] = __alloc_percpu(mibsize, __alignof__(unsigned long long));
  1176. if (!ptr[0])
  1177. goto err0;
  1178. ptr[1] = __alloc_percpu(mibsize, __alignof__(unsigned long long));
  1179. if (!ptr[1])
  1180. goto err1;
  1181. return 0;
  1182. err1:
  1183. free_percpu(ptr[0]);
  1184. ptr[0] = NULL;
  1185. err0:
  1186. return -ENOMEM;
  1187. }
  1188. EXPORT_SYMBOL_GPL(snmp_mib_init);
  1189. void snmp_mib_free(void *ptr[2])
  1190. {
  1191. BUG_ON(ptr == NULL);
  1192. free_percpu(ptr[0]);
  1193. free_percpu(ptr[1]);
  1194. ptr[0] = ptr[1] = NULL;
  1195. }
  1196. EXPORT_SYMBOL_GPL(snmp_mib_free);
  1197. #ifdef CONFIG_IP_MULTICAST
  1198. static struct net_protocol igmp_protocol = {
  1199. .handler = igmp_rcv,
  1200. .netns_ok = 1,
  1201. };
  1202. #endif
  1203. static struct net_protocol tcp_protocol = {
  1204. .handler = tcp_v4_rcv,
  1205. .err_handler = tcp_v4_err,
  1206. .gso_send_check = tcp_v4_gso_send_check,
  1207. .gso_segment = tcp_tso_segment,
  1208. .gro_receive = tcp4_gro_receive,
  1209. .gro_complete = tcp4_gro_complete,
  1210. .no_policy = 1,
  1211. .netns_ok = 1,
  1212. };
  1213. static struct net_protocol udp_protocol = {
  1214. .handler = udp_rcv,
  1215. .err_handler = udp_err,
  1216. .no_policy = 1,
  1217. .netns_ok = 1,
  1218. };
  1219. static struct net_protocol icmp_protocol = {
  1220. .handler = icmp_rcv,
  1221. .no_policy = 1,
  1222. .netns_ok = 1,
  1223. };
  1224. static __net_init int ipv4_mib_init_net(struct net *net)
  1225. {
  1226. if (snmp_mib_init((void **)net->mib.tcp_statistics,
  1227. sizeof(struct tcp_mib)) < 0)
  1228. goto err_tcp_mib;
  1229. if (snmp_mib_init((void **)net->mib.ip_statistics,
  1230. sizeof(struct ipstats_mib)) < 0)
  1231. goto err_ip_mib;
  1232. if (snmp_mib_init((void **)net->mib.net_statistics,
  1233. sizeof(struct linux_mib)) < 0)
  1234. goto err_net_mib;
  1235. if (snmp_mib_init((void **)net->mib.udp_statistics,
  1236. sizeof(struct udp_mib)) < 0)
  1237. goto err_udp_mib;
  1238. if (snmp_mib_init((void **)net->mib.udplite_statistics,
  1239. sizeof(struct udp_mib)) < 0)
  1240. goto err_udplite_mib;
  1241. if (snmp_mib_init((void **)net->mib.icmp_statistics,
  1242. sizeof(struct icmp_mib)) < 0)
  1243. goto err_icmp_mib;
  1244. if (snmp_mib_init((void **)net->mib.icmpmsg_statistics,
  1245. sizeof(struct icmpmsg_mib)) < 0)
  1246. goto err_icmpmsg_mib;
  1247. tcp_mib_init(net);
  1248. return 0;
  1249. err_icmpmsg_mib:
  1250. snmp_mib_free((void **)net->mib.icmp_statistics);
  1251. err_icmp_mib:
  1252. snmp_mib_free((void **)net->mib.udplite_statistics);
  1253. err_udplite_mib:
  1254. snmp_mib_free((void **)net->mib.udp_statistics);
  1255. err_udp_mib:
  1256. snmp_mib_free((void **)net->mib.net_statistics);
  1257. err_net_mib:
  1258. snmp_mib_free((void **)net->mib.ip_statistics);
  1259. err_ip_mib:
  1260. snmp_mib_free((void **)net->mib.tcp_statistics);
  1261. err_tcp_mib:
  1262. return -ENOMEM;
  1263. }
  1264. static __net_exit void ipv4_mib_exit_net(struct net *net)
  1265. {
  1266. snmp_mib_free((void **)net->mib.icmpmsg_statistics);
  1267. snmp_mib_free((void **)net->mib.icmp_statistics);
  1268. snmp_mib_free((void **)net->mib.udplite_statistics);
  1269. snmp_mib_free((void **)net->mib.udp_statistics);
  1270. snmp_mib_free((void **)net->mib.net_statistics);
  1271. snmp_mib_free((void **)net->mib.ip_statistics);
  1272. snmp_mib_free((void **)net->mib.tcp_statistics);
  1273. }
  1274. static __net_initdata struct pernet_operations ipv4_mib_ops = {
  1275. .init = ipv4_mib_init_net,
  1276. .exit = ipv4_mib_exit_net,
  1277. };
  1278. static int __init init_ipv4_mibs(void)
  1279. {
  1280. return register_pernet_subsys(&ipv4_mib_ops);
  1281. }
  1282. static int ipv4_proc_init(void);
  1283. /*
  1284. * IP protocol layer initialiser
  1285. */
  1286. static struct packet_type ip_packet_type __read_mostly = {
  1287. .type = cpu_to_be16(ETH_P_IP),
  1288. .func = ip_rcv,
  1289. .gso_send_check = inet_gso_send_check,
  1290. .gso_segment = inet_gso_segment,
  1291. .gro_receive = inet_gro_receive,
  1292. .gro_complete = inet_gro_complete,
  1293. };
  1294. static int __init inet_init(void)
  1295. {
  1296. struct sk_buff *dummy_skb;
  1297. struct inet_protosw *q;
  1298. struct list_head *r;
  1299. int rc = -EINVAL;
  1300. BUILD_BUG_ON(sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb));
  1301. rc = proto_register(&tcp_prot, 1);
  1302. if (rc)
  1303. goto out;
  1304. rc = proto_register(&udp_prot, 1);
  1305. if (rc)
  1306. goto out_unregister_tcp_proto;
  1307. rc = proto_register(&raw_prot, 1);
  1308. if (rc)
  1309. goto out_unregister_udp_proto;
  1310. /*
  1311. * Tell SOCKET that we are alive...
  1312. */
  1313. (void)sock_register(&inet_family_ops);
  1314. #ifdef CONFIG_SYSCTL
  1315. ip_static_sysctl_init();
  1316. #endif
  1317. /*
  1318. * Add all the base protocols.
  1319. */
  1320. if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
  1321. printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
  1322. if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
  1323. printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
  1324. if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
  1325. printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n");
  1326. #ifdef CONFIG_IP_MULTICAST
  1327. if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
  1328. printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n");
  1329. #endif
  1330. /* Register the socket-side information for inet_create. */
  1331. for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
  1332. INIT_LIST_HEAD(r);
  1333. for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
  1334. inet_register_protosw(q);
  1335. /*
  1336. * Set the ARP module up
  1337. */
  1338. arp_init();
  1339. /*
  1340. * Set the IP module up
  1341. */
  1342. ip_init();
  1343. tcp_v4_init();
  1344. /* Setup TCP slab cache for open requests. */
  1345. tcp_init();
  1346. /* Setup UDP memory threshold */
  1347. udp_init();
  1348. /* Add UDP-Lite (RFC 3828) */
  1349. udplite4_register();
  1350. /*
  1351. * Set the ICMP layer up
  1352. */
  1353. if (icmp_init() < 0)
  1354. panic("Failed to create the ICMP control socket.\n");
  1355. /*
  1356. * Initialise the multicast router
  1357. */
  1358. #if defined(CONFIG_IP_MROUTE)
  1359. if (ip_mr_init())
  1360. printk(KERN_CRIT "inet_init: Cannot init ipv4 mroute\n");
  1361. #endif
  1362. /*
  1363. * Initialise per-cpu ipv4 mibs
  1364. */
  1365. if (init_ipv4_mibs())
  1366. printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n");
  1367. ipv4_proc_init();
  1368. ipfrag_init();
  1369. dev_add_pack(&ip_packet_type);
  1370. rc = 0;
  1371. out:
  1372. return rc;
  1373. out_unregister_udp_proto:
  1374. proto_unregister(&udp_prot);
  1375. out_unregister_tcp_proto:
  1376. proto_unregister(&tcp_prot);
  1377. goto out;
  1378. }
  1379. fs_initcall(inet_init);
  1380. /* ------------------------------------------------------------------------ */
  1381. #ifdef CONFIG_PROC_FS
  1382. static int __init ipv4_proc_init(void)
  1383. {
  1384. int rc = 0;
  1385. if (raw_proc_init())
  1386. goto out_raw;
  1387. if (tcp4_proc_init())
  1388. goto out_tcp;
  1389. if (udp4_proc_init())
  1390. goto out_udp;
  1391. if (ip_misc_proc_init())
  1392. goto out_misc;
  1393. out:
  1394. return rc;
  1395. out_misc:
  1396. udp4_proc_exit();
  1397. out_udp:
  1398. tcp4_proc_exit();
  1399. out_tcp:
  1400. raw_proc_exit();
  1401. out_raw:
  1402. rc = -ENOMEM;
  1403. goto out;
  1404. }
  1405. #else /* CONFIG_PROC_FS */
  1406. static int __init ipv4_proc_init(void)
  1407. {
  1408. return 0;
  1409. }
  1410. #endif /* CONFIG_PROC_FS */
  1411. MODULE_ALIAS_NETPROTO(PF_INET);
  1412. EXPORT_SYMBOL(inet_accept);
  1413. EXPORT_SYMBOL(inet_bind);
  1414. EXPORT_SYMBOL(inet_dgram_connect);
  1415. EXPORT_SYMBOL(inet_dgram_ops);
  1416. EXPORT_SYMBOL(inet_getname);
  1417. EXPORT_SYMBOL(inet_ioctl);
  1418. EXPORT_SYMBOL(inet_listen);
  1419. EXPORT_SYMBOL(inet_register_protosw);
  1420. EXPORT_SYMBOL(inet_release);
  1421. EXPORT_SYMBOL(inet_sendmsg);
  1422. EXPORT_SYMBOL(inet_shutdown);
  1423. EXPORT_SYMBOL(inet_sock_destruct);
  1424. EXPORT_SYMBOL(inet_stream_connect);
  1425. EXPORT_SYMBOL(inet_stream_ops);
  1426. EXPORT_SYMBOL(inet_unregister_protosw);
  1427. EXPORT_SYMBOL(sysctl_ip_nonlocal_bind);