af_inet.c 40 KB

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