af_inet.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  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. * Version: $Id: af_inet.c,v 1.137 2002/02/01 22:01:03 davem Exp $
  9. *
  10. * Authors: Ross Biro
  11. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12. * Florian La Roche, <flla@stud.uni-sb.de>
  13. * Alan Cox, <A.Cox@swansea.ac.uk>
  14. *
  15. * Changes (see also sock.c)
  16. *
  17. * piggy,
  18. * Karl Knutson : Socket protocol table
  19. * A.N.Kuznetsov : Socket death error in accept().
  20. * John Richardson : Fix non blocking error in connect()
  21. * so sockets that fail to connect
  22. * don't return -EINPROGRESS.
  23. * Alan Cox : Asynchronous I/O support
  24. * Alan Cox : Keep correct socket pointer on sock
  25. * structures
  26. * when accept() ed
  27. * Alan Cox : Semantics of SO_LINGER aren't state
  28. * moved to close when you look carefully.
  29. * With this fixed and the accept bug fixed
  30. * some RPC stuff seems happier.
  31. * Niibe Yutaka : 4.4BSD style write async I/O
  32. * Alan Cox,
  33. * Tony Gale : Fixed reuse semantics.
  34. * Alan Cox : bind() shouldn't abort existing but dead
  35. * sockets. Stops FTP netin:.. I hope.
  36. * Alan Cox : bind() works correctly for RAW sockets.
  37. * Note that FreeBSD at least was broken
  38. * in this respect so be careful with
  39. * compatibility tests...
  40. * Alan Cox : routing cache support
  41. * Alan Cox : memzero the socket structure for
  42. * compactness.
  43. * Matt Day : nonblock connect error handler
  44. * Alan Cox : Allow large numbers of pending sockets
  45. * (eg for big web sites), but only if
  46. * specifically application requested.
  47. * Alan Cox : New buffering throughout IP. Used
  48. * dumbly.
  49. * Alan Cox : New buffering now used smartly.
  50. * Alan Cox : BSD rather than common sense
  51. * interpretation of listen.
  52. * Germano Caronni : Assorted small races.
  53. * Alan Cox : sendmsg/recvmsg basic support.
  54. * Alan Cox : Only sendmsg/recvmsg now supported.
  55. * Alan Cox : Locked down bind (see security list).
  56. * Alan Cox : Loosened bind a little.
  57. * Mike McLagan : ADD/DEL DLCI Ioctls
  58. * Willy Konynenberg : Transparent proxying support.
  59. * David S. Miller : New socket lookup architecture.
  60. * Some other random speedups.
  61. * Cyrus Durgin : Cleaned up file for kmod hacks.
  62. * Andi Kleen : Fix inet_stream_connect TCP race.
  63. *
  64. * This program is free software; you can redistribute it and/or
  65. * modify it under the terms of the GNU General Public License
  66. * as published by the Free Software Foundation; either version
  67. * 2 of the License, or (at your option) any later version.
  68. */
  69. #include <linux/config.h>
  70. #include <linux/errno.h>
  71. #include <linux/types.h>
  72. #include <linux/socket.h>
  73. #include <linux/in.h>
  74. #include <linux/kernel.h>
  75. #include <linux/module.h>
  76. #include <linux/sched.h>
  77. #include <linux/timer.h>
  78. #include <linux/string.h>
  79. #include <linux/sockios.h>
  80. #include <linux/net.h>
  81. #include <linux/fcntl.h>
  82. #include <linux/mm.h>
  83. #include <linux/interrupt.h>
  84. #include <linux/stat.h>
  85. #include <linux/init.h>
  86. #include <linux/poll.h>
  87. #include <linux/netfilter_ipv4.h>
  88. #include <asm/uaccess.h>
  89. #include <asm/system.h>
  90. #include <linux/smp_lock.h>
  91. #include <linux/inet.h>
  92. #include <linux/igmp.h>
  93. #include <linux/netdevice.h>
  94. #include <net/ip.h>
  95. #include <net/protocol.h>
  96. #include <net/arp.h>
  97. #include <net/route.h>
  98. #include <net/ip_fib.h>
  99. #include <net/inet_connection_sock.h>
  100. #include <net/tcp.h>
  101. #include <net/udp.h>
  102. #include <linux/skbuff.h>
  103. #include <net/sock.h>
  104. #include <net/raw.h>
  105. #include <net/icmp.h>
  106. #include <net/ipip.h>
  107. #include <net/inet_common.h>
  108. #include <net/xfrm.h>
  109. #ifdef CONFIG_IP_MROUTE
  110. #include <linux/mroute.h>
  111. #endif
  112. DEFINE_SNMP_STAT(struct linux_mib, net_statistics) __read_mostly;
  113. extern void ip_mc_drop_socket(struct sock *sk);
  114. /* The inetsw table contains everything that inet_create needs to
  115. * build a new socket.
  116. */
  117. static struct list_head inetsw[SOCK_MAX];
  118. static DEFINE_SPINLOCK(inetsw_lock);
  119. /* New destruction routine */
  120. void inet_sock_destruct(struct sock *sk)
  121. {
  122. struct inet_sock *inet = inet_sk(sk);
  123. __skb_queue_purge(&sk->sk_receive_queue);
  124. __skb_queue_purge(&sk->sk_error_queue);
  125. if (sk->sk_type == SOCK_STREAM && sk->sk_state != TCP_CLOSE) {
  126. printk("Attempt to release TCP socket in state %d %p\n",
  127. sk->sk_state, sk);
  128. return;
  129. }
  130. if (!sock_flag(sk, SOCK_DEAD)) {
  131. printk("Attempt to release alive inet socket %p\n", sk);
  132. return;
  133. }
  134. BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
  135. BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
  136. BUG_TRAP(!sk->sk_wmem_queued);
  137. BUG_TRAP(!sk->sk_forward_alloc);
  138. kfree(inet->opt);
  139. dst_release(sk->sk_dst_cache);
  140. sk_refcnt_debug_dec(sk);
  141. }
  142. /*
  143. * The routines beyond this point handle the behaviour of an AF_INET
  144. * socket object. Mostly it punts to the subprotocols of IP to do
  145. * the work.
  146. */
  147. /*
  148. * Automatically bind an unbound socket.
  149. */
  150. static int inet_autobind(struct sock *sk)
  151. {
  152. struct inet_sock *inet;
  153. /* We may need to bind the socket. */
  154. lock_sock(sk);
  155. inet = inet_sk(sk);
  156. if (!inet->num) {
  157. if (sk->sk_prot->get_port(sk, 0)) {
  158. release_sock(sk);
  159. return -EAGAIN;
  160. }
  161. inet->sport = htons(inet->num);
  162. }
  163. release_sock(sk);
  164. return 0;
  165. }
  166. /*
  167. * Move a socket into listening state.
  168. */
  169. int inet_listen(struct socket *sock, int backlog)
  170. {
  171. struct sock *sk = sock->sk;
  172. unsigned char old_state;
  173. int err;
  174. lock_sock(sk);
  175. err = -EINVAL;
  176. if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
  177. goto out;
  178. old_state = sk->sk_state;
  179. if (!((1 << old_state) & (TCPF_CLOSE | TCPF_LISTEN)))
  180. goto out;
  181. /* Really, if the socket is already in listen state
  182. * we can only allow the backlog to be adjusted.
  183. */
  184. if (old_state != TCP_LISTEN) {
  185. err = inet_csk_listen_start(sk, TCP_SYNQ_HSIZE);
  186. if (err)
  187. goto out;
  188. }
  189. sk->sk_max_ack_backlog = backlog;
  190. err = 0;
  191. out:
  192. release_sock(sk);
  193. return err;
  194. }
  195. /*
  196. * Create an inet socket.
  197. */
  198. static int inet_create(struct socket *sock, int protocol)
  199. {
  200. struct sock *sk;
  201. struct list_head *p;
  202. struct inet_protosw *answer;
  203. struct inet_sock *inet;
  204. struct proto *answer_prot;
  205. unsigned char answer_flags;
  206. char answer_no_check;
  207. int try_loading_module = 0;
  208. int err = -ESOCKTNOSUPPORT;
  209. sock->state = SS_UNCONNECTED;
  210. /* Look for the requested type/protocol pair. */
  211. answer = NULL;
  212. lookup_protocol:
  213. rcu_read_lock();
  214. list_for_each_rcu(p, &inetsw[sock->type]) {
  215. answer = list_entry(p, struct inet_protosw, list);
  216. /* Check the non-wild match. */
  217. if (protocol == answer->protocol) {
  218. if (protocol != IPPROTO_IP)
  219. break;
  220. } else {
  221. /* Check for the two wild cases. */
  222. if (IPPROTO_IP == protocol) {
  223. protocol = answer->protocol;
  224. break;
  225. }
  226. if (IPPROTO_IP == answer->protocol)
  227. break;
  228. }
  229. answer = NULL;
  230. }
  231. if (unlikely(answer == NULL)) {
  232. if (try_loading_module < 2) {
  233. rcu_read_unlock();
  234. /*
  235. * Be more specific, e.g. net-pf-2-proto-132-type-1
  236. * (net-pf-PF_INET-proto-IPPROTO_SCTP-type-SOCK_STREAM)
  237. */
  238. if (++try_loading_module == 1)
  239. request_module("net-pf-%d-proto-%d-type-%d",
  240. PF_INET, protocol, sock->type);
  241. /*
  242. * Fall back to generic, e.g. net-pf-2-proto-132
  243. * (net-pf-PF_INET-proto-IPPROTO_SCTP)
  244. */
  245. else
  246. request_module("net-pf-%d-proto-%d",
  247. PF_INET, protocol);
  248. goto lookup_protocol;
  249. } else
  250. goto out_rcu_unlock;
  251. }
  252. err = -EPERM;
  253. if (answer->capability > 0 && !capable(answer->capability))
  254. goto out_rcu_unlock;
  255. err = -EPROTONOSUPPORT;
  256. if (!protocol)
  257. goto out_rcu_unlock;
  258. sock->ops = answer->ops;
  259. answer_prot = answer->prot;
  260. answer_no_check = answer->no_check;
  261. answer_flags = answer->flags;
  262. rcu_read_unlock();
  263. BUG_TRAP(answer_prot->slab != NULL);
  264. err = -ENOBUFS;
  265. sk = sk_alloc(PF_INET, GFP_KERNEL, answer_prot, 1);
  266. if (sk == NULL)
  267. goto out;
  268. err = 0;
  269. sk->sk_no_check = answer_no_check;
  270. if (INET_PROTOSW_REUSE & answer_flags)
  271. sk->sk_reuse = 1;
  272. inet = inet_sk(sk);
  273. if (SOCK_RAW == sock->type) {
  274. inet->num = protocol;
  275. if (IPPROTO_RAW == protocol)
  276. inet->hdrincl = 1;
  277. }
  278. if (ipv4_config.no_pmtu_disc)
  279. inet->pmtudisc = IP_PMTUDISC_DONT;
  280. else
  281. inet->pmtudisc = IP_PMTUDISC_WANT;
  282. inet->id = 0;
  283. sock_init_data(sock, sk);
  284. sk->sk_destruct = inet_sock_destruct;
  285. sk->sk_family = PF_INET;
  286. sk->sk_protocol = protocol;
  287. sk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
  288. inet->uc_ttl = -1;
  289. inet->mc_loop = 1;
  290. inet->mc_ttl = 1;
  291. inet->mc_index = 0;
  292. inet->mc_list = NULL;
  293. sk_refcnt_debug_inc(sk);
  294. if (inet->num) {
  295. /* It assumes that any protocol which allows
  296. * the user to assign a number at socket
  297. * creation time automatically
  298. * shares.
  299. */
  300. inet->sport = htons(inet->num);
  301. /* Add to protocol hash chains. */
  302. sk->sk_prot->hash(sk);
  303. }
  304. if (sk->sk_prot->init) {
  305. err = sk->sk_prot->init(sk);
  306. if (err)
  307. sk_common_release(sk);
  308. }
  309. out:
  310. return err;
  311. out_rcu_unlock:
  312. rcu_read_unlock();
  313. goto out;
  314. }
  315. /*
  316. * The peer socket should always be NULL (or else). When we call this
  317. * function we are destroying the object and from then on nobody
  318. * should refer to it.
  319. */
  320. int inet_release(struct socket *sock)
  321. {
  322. struct sock *sk = sock->sk;
  323. if (sk) {
  324. long timeout;
  325. /* Applications forget to leave groups before exiting */
  326. ip_mc_drop_socket(sk);
  327. /* If linger is set, we don't return until the close
  328. * is complete. Otherwise we return immediately. The
  329. * actually closing is done the same either way.
  330. *
  331. * If the close is due to the process exiting, we never
  332. * linger..
  333. */
  334. timeout = 0;
  335. if (sock_flag(sk, SOCK_LINGER) &&
  336. !(current->flags & PF_EXITING))
  337. timeout = sk->sk_lingertime;
  338. sock->sk = NULL;
  339. sk->sk_prot->close(sk, timeout);
  340. }
  341. return 0;
  342. }
  343. /* It is off by default, see below. */
  344. int sysctl_ip_nonlocal_bind;
  345. int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  346. {
  347. struct sockaddr_in *addr = (struct sockaddr_in *)uaddr;
  348. struct sock *sk = sock->sk;
  349. struct inet_sock *inet = inet_sk(sk);
  350. unsigned short snum;
  351. int chk_addr_ret;
  352. int err;
  353. /* If the socket has its own bind function then use it. (RAW) */
  354. if (sk->sk_prot->bind) {
  355. err = sk->sk_prot->bind(sk, uaddr, addr_len);
  356. goto out;
  357. }
  358. err = -EINVAL;
  359. if (addr_len < sizeof(struct sockaddr_in))
  360. goto out;
  361. chk_addr_ret = inet_addr_type(addr->sin_addr.s_addr);
  362. /* Not specified by any standard per-se, however it breaks too
  363. * many applications when removed. It is unfortunate since
  364. * allowing applications to make a non-local bind solves
  365. * several problems with systems using dynamic addressing.
  366. * (ie. your servers still start up even if your ISDN link
  367. * is temporarily down)
  368. */
  369. err = -EADDRNOTAVAIL;
  370. if (!sysctl_ip_nonlocal_bind &&
  371. !inet->freebind &&
  372. addr->sin_addr.s_addr != INADDR_ANY &&
  373. chk_addr_ret != RTN_LOCAL &&
  374. chk_addr_ret != RTN_MULTICAST &&
  375. chk_addr_ret != RTN_BROADCAST)
  376. goto out;
  377. snum = ntohs(addr->sin_port);
  378. err = -EACCES;
  379. if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
  380. goto out;
  381. /* We keep a pair of addresses. rcv_saddr is the one
  382. * used by hash lookups, and saddr is used for transmit.
  383. *
  384. * In the BSD API these are the same except where it
  385. * would be illegal to use them (multicast/broadcast) in
  386. * which case the sending device address is used.
  387. */
  388. lock_sock(sk);
  389. /* Check these errors (active socket, double bind). */
  390. err = -EINVAL;
  391. if (sk->sk_state != TCP_CLOSE || inet->num)
  392. goto out_release_sock;
  393. inet->rcv_saddr = inet->saddr = addr->sin_addr.s_addr;
  394. if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
  395. inet->saddr = 0; /* Use device */
  396. /* Make sure we are allowed to bind here. */
  397. if (sk->sk_prot->get_port(sk, snum)) {
  398. inet->saddr = inet->rcv_saddr = 0;
  399. err = -EADDRINUSE;
  400. goto out_release_sock;
  401. }
  402. if (inet->rcv_saddr)
  403. sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
  404. if (snum)
  405. sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
  406. inet->sport = htons(inet->num);
  407. inet->daddr = 0;
  408. inet->dport = 0;
  409. sk_dst_reset(sk);
  410. err = 0;
  411. out_release_sock:
  412. release_sock(sk);
  413. out:
  414. return err;
  415. }
  416. int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
  417. int addr_len, int flags)
  418. {
  419. struct sock *sk = sock->sk;
  420. if (uaddr->sa_family == AF_UNSPEC)
  421. return sk->sk_prot->disconnect(sk, flags);
  422. if (!inet_sk(sk)->num && inet_autobind(sk))
  423. return -EAGAIN;
  424. return sk->sk_prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
  425. }
  426. static long inet_wait_for_connect(struct sock *sk, long timeo)
  427. {
  428. DEFINE_WAIT(wait);
  429. prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  430. /* Basic assumption: if someone sets sk->sk_err, he _must_
  431. * change state of the socket from TCP_SYN_*.
  432. * Connect() does not allow to get error notifications
  433. * without closing the socket.
  434. */
  435. while ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
  436. release_sock(sk);
  437. timeo = schedule_timeout(timeo);
  438. lock_sock(sk);
  439. if (signal_pending(current) || !timeo)
  440. break;
  441. prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
  442. }
  443. finish_wait(sk->sk_sleep, &wait);
  444. return timeo;
  445. }
  446. /*
  447. * Connect to a remote host. There is regrettably still a little
  448. * TCP 'magic' in here.
  449. */
  450. int inet_stream_connect(struct socket *sock, struct sockaddr *uaddr,
  451. int addr_len, int flags)
  452. {
  453. struct sock *sk = sock->sk;
  454. int err;
  455. long timeo;
  456. lock_sock(sk);
  457. if (uaddr->sa_family == AF_UNSPEC) {
  458. err = sk->sk_prot->disconnect(sk, flags);
  459. sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
  460. goto out;
  461. }
  462. switch (sock->state) {
  463. default:
  464. err = -EINVAL;
  465. goto out;
  466. case SS_CONNECTED:
  467. err = -EISCONN;
  468. goto out;
  469. case SS_CONNECTING:
  470. err = -EALREADY;
  471. /* Fall out of switch with err, set for this state */
  472. break;
  473. case SS_UNCONNECTED:
  474. err = -EISCONN;
  475. if (sk->sk_state != TCP_CLOSE)
  476. goto out;
  477. err = sk->sk_prot->connect(sk, uaddr, addr_len);
  478. if (err < 0)
  479. goto out;
  480. sock->state = SS_CONNECTING;
  481. /* Just entered SS_CONNECTING state; the only
  482. * difference is that return value in non-blocking
  483. * case is EINPROGRESS, rather than EALREADY.
  484. */
  485. err = -EINPROGRESS;
  486. break;
  487. }
  488. timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
  489. if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
  490. /* Error code is set above */
  491. if (!timeo || !inet_wait_for_connect(sk, timeo))
  492. goto out;
  493. err = sock_intr_errno(timeo);
  494. if (signal_pending(current))
  495. goto out;
  496. }
  497. /* Connection was closed by RST, timeout, ICMP error
  498. * or another process disconnected us.
  499. */
  500. if (sk->sk_state == TCP_CLOSE)
  501. goto sock_error;
  502. /* sk->sk_err may be not zero now, if RECVERR was ordered by user
  503. * and error was received after socket entered established state.
  504. * Hence, it is handled normally after connect() return successfully.
  505. */
  506. sock->state = SS_CONNECTED;
  507. err = 0;
  508. out:
  509. release_sock(sk);
  510. return err;
  511. sock_error:
  512. err = sock_error(sk) ? : -ECONNABORTED;
  513. sock->state = SS_UNCONNECTED;
  514. if (sk->sk_prot->disconnect(sk, flags))
  515. sock->state = SS_DISCONNECTING;
  516. goto out;
  517. }
  518. /*
  519. * Accept a pending connection. The TCP layer now gives BSD semantics.
  520. */
  521. int inet_accept(struct socket *sock, struct socket *newsock, int flags)
  522. {
  523. struct sock *sk1 = sock->sk;
  524. int err = -EINVAL;
  525. struct sock *sk2 = sk1->sk_prot->accept(sk1, flags, &err);
  526. if (!sk2)
  527. goto do_err;
  528. lock_sock(sk2);
  529. BUG_TRAP((1 << sk2->sk_state) &
  530. (TCPF_ESTABLISHED | TCPF_CLOSE_WAIT | TCPF_CLOSE));
  531. sock_graft(sk2, newsock);
  532. newsock->state = SS_CONNECTED;
  533. err = 0;
  534. release_sock(sk2);
  535. do_err:
  536. return err;
  537. }
  538. /*
  539. * This does both peername and sockname.
  540. */
  541. int inet_getname(struct socket *sock, struct sockaddr *uaddr,
  542. int *uaddr_len, int peer)
  543. {
  544. struct sock *sk = sock->sk;
  545. struct inet_sock *inet = inet_sk(sk);
  546. struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
  547. sin->sin_family = AF_INET;
  548. if (peer) {
  549. if (!inet->dport ||
  550. (((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_SYN_SENT)) &&
  551. peer == 1))
  552. return -ENOTCONN;
  553. sin->sin_port = inet->dport;
  554. sin->sin_addr.s_addr = inet->daddr;
  555. } else {
  556. __u32 addr = inet->rcv_saddr;
  557. if (!addr)
  558. addr = inet->saddr;
  559. sin->sin_port = inet->sport;
  560. sin->sin_addr.s_addr = addr;
  561. }
  562. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  563. *uaddr_len = sizeof(*sin);
  564. return 0;
  565. }
  566. int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
  567. size_t size)
  568. {
  569. struct sock *sk = sock->sk;
  570. /* We may need to bind the socket. */
  571. if (!inet_sk(sk)->num && inet_autobind(sk))
  572. return -EAGAIN;
  573. return sk->sk_prot->sendmsg(iocb, sk, msg, size);
  574. }
  575. static ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
  576. {
  577. struct sock *sk = sock->sk;
  578. /* We may need to bind the socket. */
  579. if (!inet_sk(sk)->num && inet_autobind(sk))
  580. return -EAGAIN;
  581. if (sk->sk_prot->sendpage)
  582. return sk->sk_prot->sendpage(sk, page, offset, size, flags);
  583. return sock_no_sendpage(sock, page, offset, size, flags);
  584. }
  585. int inet_shutdown(struct socket *sock, int how)
  586. {
  587. struct sock *sk = sock->sk;
  588. int err = 0;
  589. /* This should really check to make sure
  590. * the socket is a TCP socket. (WHY AC...)
  591. */
  592. how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
  593. 1->2 bit 2 snds.
  594. 2->3 */
  595. if ((how & ~SHUTDOWN_MASK) || !how) /* MAXINT->0 */
  596. return -EINVAL;
  597. lock_sock(sk);
  598. if (sock->state == SS_CONNECTING) {
  599. if ((1 << sk->sk_state) &
  600. (TCPF_SYN_SENT | TCPF_SYN_RECV | TCPF_CLOSE))
  601. sock->state = SS_DISCONNECTING;
  602. else
  603. sock->state = SS_CONNECTED;
  604. }
  605. switch (sk->sk_state) {
  606. case TCP_CLOSE:
  607. err = -ENOTCONN;
  608. /* Hack to wake up other listeners, who can poll for
  609. POLLHUP, even on eg. unconnected UDP sockets -- RR */
  610. default:
  611. sk->sk_shutdown |= how;
  612. if (sk->sk_prot->shutdown)
  613. sk->sk_prot->shutdown(sk, how);
  614. break;
  615. /* Remaining two branches are temporary solution for missing
  616. * close() in multithreaded environment. It is _not_ a good idea,
  617. * but we have no choice until close() is repaired at VFS level.
  618. */
  619. case TCP_LISTEN:
  620. if (!(how & RCV_SHUTDOWN))
  621. break;
  622. /* Fall through */
  623. case TCP_SYN_SENT:
  624. err = sk->sk_prot->disconnect(sk, O_NONBLOCK);
  625. sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
  626. break;
  627. }
  628. /* Wake up anyone sleeping in poll. */
  629. sk->sk_state_change(sk);
  630. release_sock(sk);
  631. return err;
  632. }
  633. /*
  634. * ioctl() calls you can issue on an INET socket. Most of these are
  635. * device configuration and stuff and very rarely used. Some ioctls
  636. * pass on to the socket itself.
  637. *
  638. * NOTE: I like the idea of a module for the config stuff. ie ifconfig
  639. * loads the devconfigure module does its configuring and unloads it.
  640. * There's a good 20K of config code hanging around the kernel.
  641. */
  642. int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  643. {
  644. struct sock *sk = sock->sk;
  645. int err = 0;
  646. switch (cmd) {
  647. case SIOCGSTAMP:
  648. err = sock_get_timestamp(sk, (struct timeval __user *)arg);
  649. break;
  650. case SIOCADDRT:
  651. case SIOCDELRT:
  652. case SIOCRTMSG:
  653. err = ip_rt_ioctl(cmd, (void __user *)arg);
  654. break;
  655. case SIOCDARP:
  656. case SIOCGARP:
  657. case SIOCSARP:
  658. err = arp_ioctl(cmd, (void __user *)arg);
  659. break;
  660. case SIOCGIFADDR:
  661. case SIOCSIFADDR:
  662. case SIOCGIFBRDADDR:
  663. case SIOCSIFBRDADDR:
  664. case SIOCGIFNETMASK:
  665. case SIOCSIFNETMASK:
  666. case SIOCGIFDSTADDR:
  667. case SIOCSIFDSTADDR:
  668. case SIOCSIFPFLAGS:
  669. case SIOCGIFPFLAGS:
  670. case SIOCSIFFLAGS:
  671. err = devinet_ioctl(cmd, (void __user *)arg);
  672. break;
  673. default:
  674. if (!sk->sk_prot->ioctl ||
  675. (err = sk->sk_prot->ioctl(sk, cmd, arg)) ==
  676. -ENOIOCTLCMD)
  677. err = dev_ioctl(cmd, (void __user *)arg);
  678. break;
  679. }
  680. return err;
  681. }
  682. struct proto_ops inet_stream_ops = {
  683. .family = PF_INET,
  684. .owner = THIS_MODULE,
  685. .release = inet_release,
  686. .bind = inet_bind,
  687. .connect = inet_stream_connect,
  688. .socketpair = sock_no_socketpair,
  689. .accept = inet_accept,
  690. .getname = inet_getname,
  691. .poll = tcp_poll,
  692. .ioctl = inet_ioctl,
  693. .listen = inet_listen,
  694. .shutdown = inet_shutdown,
  695. .setsockopt = sock_common_setsockopt,
  696. .getsockopt = sock_common_getsockopt,
  697. .sendmsg = inet_sendmsg,
  698. .recvmsg = sock_common_recvmsg,
  699. .mmap = sock_no_mmap,
  700. .sendpage = tcp_sendpage
  701. };
  702. struct proto_ops inet_dgram_ops = {
  703. .family = PF_INET,
  704. .owner = THIS_MODULE,
  705. .release = inet_release,
  706. .bind = inet_bind,
  707. .connect = inet_dgram_connect,
  708. .socketpair = sock_no_socketpair,
  709. .accept = sock_no_accept,
  710. .getname = inet_getname,
  711. .poll = udp_poll,
  712. .ioctl = inet_ioctl,
  713. .listen = sock_no_listen,
  714. .shutdown = inet_shutdown,
  715. .setsockopt = sock_common_setsockopt,
  716. .getsockopt = sock_common_getsockopt,
  717. .sendmsg = inet_sendmsg,
  718. .recvmsg = sock_common_recvmsg,
  719. .mmap = sock_no_mmap,
  720. .sendpage = inet_sendpage,
  721. };
  722. /*
  723. * For SOCK_RAW sockets; should be the same as inet_dgram_ops but without
  724. * udp_poll
  725. */
  726. static struct proto_ops inet_sockraw_ops = {
  727. .family = PF_INET,
  728. .owner = THIS_MODULE,
  729. .release = inet_release,
  730. .bind = inet_bind,
  731. .connect = inet_dgram_connect,
  732. .socketpair = sock_no_socketpair,
  733. .accept = sock_no_accept,
  734. .getname = inet_getname,
  735. .poll = datagram_poll,
  736. .ioctl = inet_ioctl,
  737. .listen = sock_no_listen,
  738. .shutdown = inet_shutdown,
  739. .setsockopt = sock_common_setsockopt,
  740. .getsockopt = sock_common_getsockopt,
  741. .sendmsg = inet_sendmsg,
  742. .recvmsg = sock_common_recvmsg,
  743. .mmap = sock_no_mmap,
  744. .sendpage = inet_sendpage,
  745. };
  746. static struct net_proto_family inet_family_ops = {
  747. .family = PF_INET,
  748. .create = inet_create,
  749. .owner = THIS_MODULE,
  750. };
  751. /* Upon startup we insert all the elements in inetsw_array[] into
  752. * the linked list inetsw.
  753. */
  754. static struct inet_protosw inetsw_array[] =
  755. {
  756. {
  757. .type = SOCK_STREAM,
  758. .protocol = IPPROTO_TCP,
  759. .prot = &tcp_prot,
  760. .ops = &inet_stream_ops,
  761. .capability = -1,
  762. .no_check = 0,
  763. .flags = INET_PROTOSW_PERMANENT,
  764. },
  765. {
  766. .type = SOCK_DGRAM,
  767. .protocol = IPPROTO_UDP,
  768. .prot = &udp_prot,
  769. .ops = &inet_dgram_ops,
  770. .capability = -1,
  771. .no_check = UDP_CSUM_DEFAULT,
  772. .flags = INET_PROTOSW_PERMANENT,
  773. },
  774. {
  775. .type = SOCK_RAW,
  776. .protocol = IPPROTO_IP, /* wild card */
  777. .prot = &raw_prot,
  778. .ops = &inet_sockraw_ops,
  779. .capability = CAP_NET_RAW,
  780. .no_check = UDP_CSUM_DEFAULT,
  781. .flags = INET_PROTOSW_REUSE,
  782. }
  783. };
  784. #define INETSW_ARRAY_LEN (sizeof(inetsw_array) / sizeof(struct inet_protosw))
  785. void inet_register_protosw(struct inet_protosw *p)
  786. {
  787. struct list_head *lh;
  788. struct inet_protosw *answer;
  789. int protocol = p->protocol;
  790. struct list_head *last_perm;
  791. spin_lock_bh(&inetsw_lock);
  792. if (p->type >= SOCK_MAX)
  793. goto out_illegal;
  794. /* If we are trying to override a permanent protocol, bail. */
  795. answer = NULL;
  796. last_perm = &inetsw[p->type];
  797. list_for_each(lh, &inetsw[p->type]) {
  798. answer = list_entry(lh, struct inet_protosw, list);
  799. /* Check only the non-wild match. */
  800. if (INET_PROTOSW_PERMANENT & answer->flags) {
  801. if (protocol == answer->protocol)
  802. break;
  803. last_perm = lh;
  804. }
  805. answer = NULL;
  806. }
  807. if (answer)
  808. goto out_permanent;
  809. /* Add the new entry after the last permanent entry if any, so that
  810. * the new entry does not override a permanent entry when matched with
  811. * a wild-card protocol. But it is allowed to override any existing
  812. * non-permanent entry. This means that when we remove this entry, the
  813. * system automatically returns to the old behavior.
  814. */
  815. list_add_rcu(&p->list, last_perm);
  816. out:
  817. spin_unlock_bh(&inetsw_lock);
  818. synchronize_net();
  819. return;
  820. out_permanent:
  821. printk(KERN_ERR "Attempt to override permanent protocol %d.\n",
  822. protocol);
  823. goto out;
  824. out_illegal:
  825. printk(KERN_ERR
  826. "Ignoring attempt to register invalid socket type %d.\n",
  827. p->type);
  828. goto out;
  829. }
  830. void inet_unregister_protosw(struct inet_protosw *p)
  831. {
  832. if (INET_PROTOSW_PERMANENT & p->flags) {
  833. printk(KERN_ERR
  834. "Attempt to unregister permanent protocol %d.\n",
  835. p->protocol);
  836. } else {
  837. spin_lock_bh(&inetsw_lock);
  838. list_del_rcu(&p->list);
  839. spin_unlock_bh(&inetsw_lock);
  840. synchronize_net();
  841. }
  842. }
  843. /*
  844. * Shall we try to damage output packets if routing dev changes?
  845. */
  846. int sysctl_ip_dynaddr;
  847. static int inet_sk_reselect_saddr(struct sock *sk)
  848. {
  849. struct inet_sock *inet = inet_sk(sk);
  850. int err;
  851. struct rtable *rt;
  852. __u32 old_saddr = inet->saddr;
  853. __u32 new_saddr;
  854. __u32 daddr = inet->daddr;
  855. if (inet->opt && inet->opt->srr)
  856. daddr = inet->opt->faddr;
  857. /* Query new route. */
  858. err = ip_route_connect(&rt, daddr, 0,
  859. RT_CONN_FLAGS(sk),
  860. sk->sk_bound_dev_if,
  861. sk->sk_protocol,
  862. inet->sport, inet->dport, sk);
  863. if (err)
  864. return err;
  865. sk_setup_caps(sk, &rt->u.dst);
  866. new_saddr = rt->rt_src;
  867. if (new_saddr == old_saddr)
  868. return 0;
  869. if (sysctl_ip_dynaddr > 1) {
  870. printk(KERN_INFO "%s(): shifting inet->"
  871. "saddr from %d.%d.%d.%d to %d.%d.%d.%d\n",
  872. __FUNCTION__,
  873. NIPQUAD(old_saddr),
  874. NIPQUAD(new_saddr));
  875. }
  876. inet->saddr = inet->rcv_saddr = new_saddr;
  877. /*
  878. * XXX The only one ugly spot where we need to
  879. * XXX really change the sockets identity after
  880. * XXX it has entered the hashes. -DaveM
  881. *
  882. * Besides that, it does not check for connection
  883. * uniqueness. Wait for troubles.
  884. */
  885. __sk_prot_rehash(sk);
  886. return 0;
  887. }
  888. int inet_sk_rebuild_header(struct sock *sk)
  889. {
  890. struct inet_sock *inet = inet_sk(sk);
  891. struct rtable *rt = (struct rtable *)__sk_dst_check(sk, 0);
  892. u32 daddr;
  893. int err;
  894. /* Route is OK, nothing to do. */
  895. if (rt)
  896. return 0;
  897. /* Reroute. */
  898. daddr = inet->daddr;
  899. if (inet->opt && inet->opt->srr)
  900. daddr = inet->opt->faddr;
  901. {
  902. struct flowi fl = {
  903. .oif = sk->sk_bound_dev_if,
  904. .nl_u = {
  905. .ip4_u = {
  906. .daddr = daddr,
  907. .saddr = inet->saddr,
  908. .tos = RT_CONN_FLAGS(sk),
  909. },
  910. },
  911. .proto = sk->sk_protocol,
  912. .uli_u = {
  913. .ports = {
  914. .sport = inet->sport,
  915. .dport = inet->dport,
  916. },
  917. },
  918. };
  919. err = ip_route_output_flow(&rt, &fl, sk, 0);
  920. }
  921. if (!err)
  922. sk_setup_caps(sk, &rt->u.dst);
  923. else {
  924. /* Routing failed... */
  925. sk->sk_route_caps = 0;
  926. /*
  927. * Other protocols have to map its equivalent state to TCP_SYN_SENT.
  928. * DCCP maps its DCCP_REQUESTING state to TCP_SYN_SENT. -acme
  929. */
  930. if (!sysctl_ip_dynaddr ||
  931. sk->sk_state != TCP_SYN_SENT ||
  932. (sk->sk_userlocks & SOCK_BINDADDR_LOCK) ||
  933. (err = inet_sk_reselect_saddr(sk)) != 0)
  934. sk->sk_err_soft = -err;
  935. }
  936. return err;
  937. }
  938. EXPORT_SYMBOL(inet_sk_rebuild_header);
  939. #ifdef CONFIG_IP_MULTICAST
  940. static struct net_protocol igmp_protocol = {
  941. .handler = igmp_rcv,
  942. };
  943. #endif
  944. static struct net_protocol tcp_protocol = {
  945. .handler = tcp_v4_rcv,
  946. .err_handler = tcp_v4_err,
  947. .no_policy = 1,
  948. };
  949. static struct net_protocol udp_protocol = {
  950. .handler = udp_rcv,
  951. .err_handler = udp_err,
  952. .no_policy = 1,
  953. };
  954. static struct net_protocol icmp_protocol = {
  955. .handler = icmp_rcv,
  956. };
  957. static int __init init_ipv4_mibs(void)
  958. {
  959. net_statistics[0] = alloc_percpu(struct linux_mib);
  960. net_statistics[1] = alloc_percpu(struct linux_mib);
  961. ip_statistics[0] = alloc_percpu(struct ipstats_mib);
  962. ip_statistics[1] = alloc_percpu(struct ipstats_mib);
  963. icmp_statistics[0] = alloc_percpu(struct icmp_mib);
  964. icmp_statistics[1] = alloc_percpu(struct icmp_mib);
  965. tcp_statistics[0] = alloc_percpu(struct tcp_mib);
  966. tcp_statistics[1] = alloc_percpu(struct tcp_mib);
  967. udp_statistics[0] = alloc_percpu(struct udp_mib);
  968. udp_statistics[1] = alloc_percpu(struct udp_mib);
  969. if (!
  970. (net_statistics[0] && net_statistics[1] && ip_statistics[0]
  971. && ip_statistics[1] && tcp_statistics[0] && tcp_statistics[1]
  972. && udp_statistics[0] && udp_statistics[1]))
  973. return -ENOMEM;
  974. (void) tcp_mib_init();
  975. return 0;
  976. }
  977. static int ipv4_proc_init(void);
  978. /*
  979. * IP protocol layer initialiser
  980. */
  981. static struct packet_type ip_packet_type = {
  982. .type = __constant_htons(ETH_P_IP),
  983. .func = ip_rcv,
  984. };
  985. static int __init inet_init(void)
  986. {
  987. struct sk_buff *dummy_skb;
  988. struct inet_protosw *q;
  989. struct list_head *r;
  990. int rc = -EINVAL;
  991. if (sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb)) {
  992. printk(KERN_CRIT "%s: panic\n", __FUNCTION__);
  993. goto out;
  994. }
  995. rc = proto_register(&tcp_prot, 1);
  996. if (rc)
  997. goto out;
  998. rc = proto_register(&udp_prot, 1);
  999. if (rc)
  1000. goto out_unregister_tcp_proto;
  1001. rc = proto_register(&raw_prot, 1);
  1002. if (rc)
  1003. goto out_unregister_udp_proto;
  1004. /*
  1005. * Tell SOCKET that we are alive...
  1006. */
  1007. (void)sock_register(&inet_family_ops);
  1008. /*
  1009. * Add all the base protocols.
  1010. */
  1011. if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
  1012. printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
  1013. if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
  1014. printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
  1015. if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
  1016. printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n");
  1017. #ifdef CONFIG_IP_MULTICAST
  1018. if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
  1019. printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n");
  1020. #endif
  1021. /* Register the socket-side information for inet_create. */
  1022. for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
  1023. INIT_LIST_HEAD(r);
  1024. for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
  1025. inet_register_protosw(q);
  1026. /*
  1027. * Set the ARP module up
  1028. */
  1029. arp_init();
  1030. /*
  1031. * Set the IP module up
  1032. */
  1033. ip_init();
  1034. tcp_v4_init(&inet_family_ops);
  1035. /* Setup TCP slab cache for open requests. */
  1036. tcp_init();
  1037. /*
  1038. * Set the ICMP layer up
  1039. */
  1040. icmp_init(&inet_family_ops);
  1041. /*
  1042. * Initialise the multicast router
  1043. */
  1044. #if defined(CONFIG_IP_MROUTE)
  1045. ip_mr_init();
  1046. #endif
  1047. /*
  1048. * Initialise per-cpu ipv4 mibs
  1049. */
  1050. if(init_ipv4_mibs())
  1051. printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n"); ;
  1052. ipv4_proc_init();
  1053. ipfrag_init();
  1054. dev_add_pack(&ip_packet_type);
  1055. rc = 0;
  1056. out:
  1057. return rc;
  1058. out_unregister_tcp_proto:
  1059. proto_unregister(&tcp_prot);
  1060. out_unregister_udp_proto:
  1061. proto_unregister(&udp_prot);
  1062. goto out;
  1063. }
  1064. module_init(inet_init);
  1065. /* ------------------------------------------------------------------------ */
  1066. #ifdef CONFIG_PROC_FS
  1067. static int __init ipv4_proc_init(void)
  1068. {
  1069. int rc = 0;
  1070. if (raw_proc_init())
  1071. goto out_raw;
  1072. if (tcp4_proc_init())
  1073. goto out_tcp;
  1074. if (udp4_proc_init())
  1075. goto out_udp;
  1076. if (fib_proc_init())
  1077. goto out_fib;
  1078. if (ip_misc_proc_init())
  1079. goto out_misc;
  1080. out:
  1081. return rc;
  1082. out_misc:
  1083. fib_proc_exit();
  1084. out_fib:
  1085. udp4_proc_exit();
  1086. out_udp:
  1087. tcp4_proc_exit();
  1088. out_tcp:
  1089. raw_proc_exit();
  1090. out_raw:
  1091. rc = -ENOMEM;
  1092. goto out;
  1093. }
  1094. #else /* CONFIG_PROC_FS */
  1095. static int __init ipv4_proc_init(void)
  1096. {
  1097. return 0;
  1098. }
  1099. #endif /* CONFIG_PROC_FS */
  1100. MODULE_ALIAS_NETPROTO(PF_INET);
  1101. EXPORT_SYMBOL(inet_accept);
  1102. EXPORT_SYMBOL(inet_bind);
  1103. EXPORT_SYMBOL(inet_dgram_connect);
  1104. EXPORT_SYMBOL(inet_dgram_ops);
  1105. EXPORT_SYMBOL(inet_getname);
  1106. EXPORT_SYMBOL(inet_ioctl);
  1107. EXPORT_SYMBOL(inet_listen);
  1108. EXPORT_SYMBOL(inet_register_protosw);
  1109. EXPORT_SYMBOL(inet_release);
  1110. EXPORT_SYMBOL(inet_sendmsg);
  1111. EXPORT_SYMBOL(inet_shutdown);
  1112. EXPORT_SYMBOL(inet_sock_destruct);
  1113. EXPORT_SYMBOL(inet_stream_connect);
  1114. EXPORT_SYMBOL(inet_stream_ops);
  1115. EXPORT_SYMBOL(inet_unregister_protosw);
  1116. EXPORT_SYMBOL(net_statistics);
  1117. EXPORT_SYMBOL(sysctl_ip_nonlocal_bind);