af_inet.c 31 KB

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