af_inet.c 31 KB

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