af_inet.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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. static int __init inet_init(void)
  875. {
  876. struct sk_buff *dummy_skb;
  877. struct inet_protosw *q;
  878. struct list_head *r;
  879. int rc = -EINVAL;
  880. if (sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb)) {
  881. printk(KERN_CRIT "%s: panic\n", __FUNCTION__);
  882. goto out;
  883. }
  884. rc = proto_register(&tcp_prot, 1);
  885. if (rc)
  886. goto out;
  887. rc = proto_register(&udp_prot, 1);
  888. if (rc)
  889. goto out_unregister_tcp_proto;
  890. rc = proto_register(&raw_prot, 1);
  891. if (rc)
  892. goto out_unregister_udp_proto;
  893. /*
  894. * Tell SOCKET that we are alive...
  895. */
  896. (void)sock_register(&inet_family_ops);
  897. /*
  898. * Add all the base protocols.
  899. */
  900. if (inet_add_protocol(&icmp_protocol, IPPROTO_ICMP) < 0)
  901. printk(KERN_CRIT "inet_init: Cannot add ICMP protocol\n");
  902. if (inet_add_protocol(&udp_protocol, IPPROTO_UDP) < 0)
  903. printk(KERN_CRIT "inet_init: Cannot add UDP protocol\n");
  904. if (inet_add_protocol(&tcp_protocol, IPPROTO_TCP) < 0)
  905. printk(KERN_CRIT "inet_init: Cannot add TCP protocol\n");
  906. #ifdef CONFIG_IP_MULTICAST
  907. if (inet_add_protocol(&igmp_protocol, IPPROTO_IGMP) < 0)
  908. printk(KERN_CRIT "inet_init: Cannot add IGMP protocol\n");
  909. #endif
  910. /* Register the socket-side information for inet_create. */
  911. for (r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
  912. INIT_LIST_HEAD(r);
  913. for (q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
  914. inet_register_protosw(q);
  915. /*
  916. * Set the ARP module up
  917. */
  918. arp_init();
  919. /*
  920. * Set the IP module up
  921. */
  922. ip_init();
  923. tcp_v4_init(&inet_family_ops);
  924. /* Setup TCP slab cache for open requests. */
  925. tcp_init();
  926. /*
  927. * Set the ICMP layer up
  928. */
  929. icmp_init(&inet_family_ops);
  930. /*
  931. * Initialise the multicast router
  932. */
  933. #if defined(CONFIG_IP_MROUTE)
  934. ip_mr_init();
  935. #endif
  936. /*
  937. * Initialise per-cpu ipv4 mibs
  938. */
  939. if(init_ipv4_mibs())
  940. printk(KERN_CRIT "inet_init: Cannot init ipv4 mibs\n"); ;
  941. ipv4_proc_init();
  942. ipfrag_init();
  943. rc = 0;
  944. out:
  945. return rc;
  946. out_unregister_tcp_proto:
  947. proto_unregister(&tcp_prot);
  948. out_unregister_udp_proto:
  949. proto_unregister(&udp_prot);
  950. goto out;
  951. }
  952. module_init(inet_init);
  953. /* ------------------------------------------------------------------------ */
  954. #ifdef CONFIG_PROC_FS
  955. extern int fib_proc_init(void);
  956. extern void fib_proc_exit(void);
  957. #ifdef CONFIG_IP_FIB_TRIE
  958. extern int fib_stat_proc_init(void);
  959. extern void fib_stat_proc_exit(void);
  960. #endif
  961. extern int ip_misc_proc_init(void);
  962. extern int raw_proc_init(void);
  963. extern void raw_proc_exit(void);
  964. extern int tcp4_proc_init(void);
  965. extern void tcp4_proc_exit(void);
  966. extern int udp4_proc_init(void);
  967. extern void udp4_proc_exit(void);
  968. static int __init ipv4_proc_init(void)
  969. {
  970. int rc = 0;
  971. if (raw_proc_init())
  972. goto out_raw;
  973. if (tcp4_proc_init())
  974. goto out_tcp;
  975. if (udp4_proc_init())
  976. goto out_udp;
  977. if (fib_proc_init())
  978. goto out_fib;
  979. #ifdef CONFIG_IP_FIB_TRIE
  980. if (fib_stat_proc_init())
  981. goto out_fib_stat;
  982. #endif
  983. if (ip_misc_proc_init())
  984. goto out_misc;
  985. out:
  986. return rc;
  987. out_misc:
  988. #ifdef CONFIG_IP_FIB_TRIE
  989. fib_stat_proc_exit();
  990. out_fib_stat:
  991. #endif
  992. fib_proc_exit();
  993. out_fib:
  994. udp4_proc_exit();
  995. out_udp:
  996. tcp4_proc_exit();
  997. out_tcp:
  998. raw_proc_exit();
  999. out_raw:
  1000. rc = -ENOMEM;
  1001. goto out;
  1002. }
  1003. #else /* CONFIG_PROC_FS */
  1004. static int __init ipv4_proc_init(void)
  1005. {
  1006. return 0;
  1007. }
  1008. #endif /* CONFIG_PROC_FS */
  1009. MODULE_ALIAS_NETPROTO(PF_INET);
  1010. EXPORT_SYMBOL(inet_accept);
  1011. EXPORT_SYMBOL(inet_bind);
  1012. EXPORT_SYMBOL(inet_dgram_connect);
  1013. EXPORT_SYMBOL(inet_dgram_ops);
  1014. EXPORT_SYMBOL(inet_getname);
  1015. EXPORT_SYMBOL(inet_ioctl);
  1016. EXPORT_SYMBOL(inet_listen);
  1017. EXPORT_SYMBOL(inet_register_protosw);
  1018. EXPORT_SYMBOL(inet_release);
  1019. EXPORT_SYMBOL(inet_sendmsg);
  1020. EXPORT_SYMBOL(inet_shutdown);
  1021. EXPORT_SYMBOL(inet_sock_destruct);
  1022. EXPORT_SYMBOL(inet_stream_connect);
  1023. EXPORT_SYMBOL(inet_stream_ops);
  1024. EXPORT_SYMBOL(inet_unregister_protosw);
  1025. EXPORT_SYMBOL(net_statistics);
  1026. EXPORT_SYMBOL(sysctl_ip_nonlocal_bind);
  1027. #ifdef INET_REFCNT_DEBUG
  1028. EXPORT_SYMBOL(inet_sock_nr);
  1029. #endif