af_inet.c 28 KB

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