af_inet.c 31 KB

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