af_inet.c 31 KB

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