af_inet.c 28 KB

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