af_inet.c 29 KB

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