af_inet.c 40 KB

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