af_inet.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:28k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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.136 2001/11/06 22:21:08 davem Exp $
  9.  *
  10.  * Authors: Ross Biro, <bir7@leland.Stanford.Edu>
  11.  * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12.  * Florian La Roche, <flla@stud.uni-sb.de>
  13.  * Alan Cox, <A.Cox@swansea.ac.uk>
  14.  *
  15.  * Changes (see also sock.c)
  16.  *
  17.  * piggy,
  18.  * Karl Knutson : Socket protocol table
  19.  * A.N.Kuznetsov : Socket death error in accept().
  20.  * John Richardson : Fix non blocking error in connect()
  21.  * so sockets that fail to connect
  22.  * don't return -EINPROGRESS.
  23.  * Alan Cox : Asynchronous I/O support
  24.  * Alan Cox : Keep correct socket pointer on sock structures
  25.  * when accept() ed
  26.  * Alan Cox : Semantics of SO_LINGER aren't state moved
  27.  * to close when you look carefully. With
  28.  * this fixed and the accept bug fixed 
  29.  * some RPC stuff seems happier.
  30.  * Niibe Yutaka : 4.4BSD style write async I/O
  31.  * Alan Cox, 
  32.  * Tony Gale  : Fixed reuse semantics.
  33.  * Alan Cox : bind() shouldn't abort existing but dead
  34.  * sockets. Stops FTP netin:.. I hope.
  35.  * Alan Cox : bind() works correctly for RAW sockets. Note
  36.  * that FreeBSD at least was broken in this respect
  37.  * so be careful with compatibility tests...
  38.  * Alan Cox : routing cache support
  39.  * Alan Cox : memzero the socket structure for compactness.
  40.  * Matt Day : nonblock connect error handler
  41.  * Alan Cox : Allow large numbers of pending sockets
  42.  * (eg for big web sites), but only if
  43.  * specifically application requested.
  44.  * Alan Cox : New buffering throughout IP. Used dumbly.
  45.  * Alan Cox : New buffering now used smartly.
  46.  * Alan Cox : BSD rather than common sense interpretation of
  47.  * listen.
  48.  * Germano Caronni : Assorted small races.
  49.  * Alan Cox : sendmsg/recvmsg basic support.
  50.  * Alan Cox : Only sendmsg/recvmsg now supported.
  51.  * Alan Cox : Locked down bind (see security list).
  52.  * Alan Cox : Loosened bind a little.
  53.  * Mike McLagan : ADD/DEL DLCI Ioctls
  54.  * Willy Konynenberg : Transparent proxying support.
  55.  * David S. Miller : New socket lookup architecture.
  56.  * Some other random speedups.
  57.  * Cyrus Durgin : Cleaned up file for kmod hacks.
  58.  * Andi Kleen : Fix inet_stream_connect TCP race.
  59.  *
  60.  * This program is free software; you can redistribute it and/or
  61.  * modify it under the terms of the GNU General Public License
  62.  * as published by the Free Software Foundation; either version
  63.  * 2 of the License, or (at your option) any later version.
  64.  */
  65. #include <linux/config.h>
  66. #include <linux/errno.h>
  67. #include <linux/types.h>
  68. #include <linux/socket.h>
  69. #include <linux/in.h>
  70. #include <linux/kernel.h>
  71. #include <linux/major.h>
  72. #include <linux/sched.h>
  73. #include <linux/timer.h>
  74. #include <linux/string.h>
  75. #include <linux/sockios.h>
  76. #include <linux/net.h>
  77. #include <linux/fcntl.h>
  78. #include <linux/mm.h>
  79. #include <linux/interrupt.h>
  80. #include <linux/proc_fs.h>
  81. #include <linux/stat.h>
  82. #include <linux/init.h>
  83. #include <linux/poll.h>
  84. #include <linux/netfilter_ipv4.h>
  85. #include <asm/uaccess.h>
  86. #include <asm/system.h>
  87. #include <linux/smp_lock.h>
  88. #include <linux/inet.h>
  89. #include <linux/netdevice.h>
  90. #include <linux/brlock.h>
  91. #include <net/ip.h>
  92. #include <net/protocol.h>
  93. #include <net/arp.h>
  94. #include <net/route.h>
  95. #include <net/tcp.h>
  96. #include <net/udp.h>
  97. #include <linux/skbuff.h>
  98. #include <net/sock.h>
  99. #include <net/raw.h>
  100. #include <net/icmp.h>
  101. #include <net/ipip.h>
  102. #include <net/inet_common.h>
  103. #ifdef CONFIG_IP_MROUTE
  104. #include <linux/mroute.h>
  105. #endif
  106. #include <linux/if_bridge.h>
  107. #ifdef CONFIG_KMOD
  108. #include <linux/kmod.h>
  109. #endif
  110. #ifdef CONFIG_NET_DIVERT
  111. #include <linux/divert.h>
  112. #endif /* CONFIG_NET_DIVERT */
  113. #if defined(CONFIG_NET_RADIO) || defined(CONFIG_NET_PCMCIA_RADIO)
  114. #include <linux/wireless.h> /* Note : will define WIRELESS_EXT */
  115. #endif /* CONFIG_NET_RADIO || CONFIG_NET_PCMCIA_RADIO */
  116. struct linux_mib net_statistics[NR_CPUS*2];
  117. #ifdef INET_REFCNT_DEBUG
  118. atomic_t inet_sock_nr;
  119. #endif
  120. extern int raw_get_info(char *, char **, off_t, int);
  121. extern int snmp_get_info(char *, char **, off_t, int);
  122. extern int netstat_get_info(char *, char **, off_t, int);
  123. extern int afinet_get_info(char *, char **, off_t, int);
  124. extern int tcp_get_info(char *, char **, off_t, int);
  125. extern int udp_get_info(char *, char **, off_t, int);
  126. extern void ip_mc_drop_socket(struct sock *sk);
  127. #ifdef CONFIG_DLCI
  128. extern int dlci_ioctl(unsigned int, void*);
  129. #endif
  130. #ifdef CONFIG_DLCI_MODULE
  131. int (*dlci_ioctl_hook)(unsigned int, void *);
  132. #endif
  133. #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
  134. int (*br_ioctl_hook)(unsigned long);
  135. #endif
  136. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  137. int (*vlan_ioctl_hook)(unsigned long arg);
  138. #endif
  139. /* The inetsw table contains everything that inet_create needs to
  140.  * build a new socket.
  141.  */
  142. struct list_head inetsw[SOCK_MAX];
  143. /* New destruction routine */
  144. void inet_sock_destruct(struct sock *sk)
  145. {
  146. __skb_queue_purge(&sk->receive_queue);
  147. __skb_queue_purge(&sk->error_queue);
  148. if (sk->type == SOCK_STREAM && sk->state != TCP_CLOSE) {
  149. printk("Attempt to release TCP socket in state %d %pn",
  150.        sk->state,
  151.        sk);
  152. return;
  153. }
  154. if (!sk->dead) {
  155. printk("Attempt to release alive inet socket %pn", sk);
  156. return;
  157. }
  158. BUG_TRAP(atomic_read(&sk->rmem_alloc) == 0);
  159. BUG_TRAP(atomic_read(&sk->wmem_alloc) == 0);
  160. BUG_TRAP(sk->wmem_queued == 0);
  161. BUG_TRAP(sk->forward_alloc == 0);
  162. if (sk->protinfo.af_inet.opt)
  163. kfree(sk->protinfo.af_inet.opt);
  164. dst_release(sk->dst_cache);
  165. #ifdef INET_REFCNT_DEBUG
  166. atomic_dec(&inet_sock_nr);
  167. printk(KERN_DEBUG "INET socket %p released, %d are still aliven", sk, atomic_read(&inet_sock_nr));
  168. #endif
  169. }
  170. void inet_sock_release(struct sock *sk)
  171. {
  172. if (sk->prot->destroy)
  173. sk->prot->destroy(sk);
  174. /* Observation: when inet_sock_release is called, processes have
  175.  * no access to socket. But net still has.
  176.  * Step one, detach it from networking:
  177.  *
  178.  * A. Remove from hash tables.
  179.  */
  180. sk->prot->unhash(sk);
  181. /* In this point socket cannot receive new packets,
  182.  * but it is possible that some packets are in flight
  183.  * because some CPU runs receiver and did hash table lookup
  184.  * before we unhashed socket. They will achieve receive queue
  185.  * and will be purged by socket destructor.
  186.  *
  187.  * Also we still have packets pending on receive
  188.  * queue and probably, our own packets waiting in device queues.
  189.  * sock_destroy will drain receive queue, but transmitted
  190.  * packets will delay socket destruction until the last reference
  191.  * will be released.
  192.  */
  193. sock_orphan(sk);
  194. #ifdef INET_REFCNT_DEBUG
  195. if (atomic_read(&sk->refcnt) != 1) {
  196. printk(KERN_DEBUG "Destruction inet %p delayed, c=%dn", sk, atomic_read(&sk->refcnt));
  197. }
  198. #endif
  199. sock_put(sk);
  200. }
  201. /*
  202.  * The routines beyond this point handle the behaviour of an AF_INET
  203.  * socket object. Mostly it punts to the subprotocols of IP to do
  204.  * the work.
  205.  */
  206.  
  207. /*
  208.  * Set socket options on an inet socket.
  209.  */
  210.  
  211. int inet_setsockopt(struct socket *sock, int level, int optname,
  212.     char *optval, int optlen)
  213. {
  214. struct sock *sk=sock->sk;
  215. return sk->prot->setsockopt(sk,level,optname,optval,optlen);
  216. }
  217. /*
  218.  * Get a socket option on an AF_INET socket.
  219.  *
  220.  * FIX: POSIX 1003.1g is very ambiguous here. It states that
  221.  * asynchronous errors should be reported by getsockopt. We assume
  222.  * this means if you specify SO_ERROR (otherwise whats the point of it).
  223.  */
  224. int inet_getsockopt(struct socket *sock, int level, int optname,
  225.     char *optval, int *optlen)
  226. {
  227. struct sock *sk=sock->sk;
  228. return sk->prot->getsockopt(sk,level,optname,optval,optlen);
  229. }
  230. /*
  231.  * Automatically bind an unbound socket.
  232.  */
  233. static int inet_autobind(struct sock *sk)
  234. {
  235. /* We may need to bind the socket. */
  236. lock_sock(sk);
  237. if (sk->num == 0) {
  238. if (sk->prot->get_port(sk, 0) != 0) {
  239. release_sock(sk);
  240. return -EAGAIN;
  241. }
  242. sk->sport = htons(sk->num);
  243. }
  244. release_sock(sk);
  245. return 0;
  246. }
  247. /*
  248.  * Move a socket into listening state.
  249.  */
  250.  
  251. int inet_listen(struct socket *sock, int backlog)
  252. {
  253. struct sock *sk = sock->sk;
  254. unsigned char old_state;
  255. int err;
  256. lock_sock(sk);
  257. err = -EINVAL;
  258. if (sock->state != SS_UNCONNECTED || sock->type != SOCK_STREAM)
  259. goto out;
  260. old_state = sk->state;
  261. if (!((1<<old_state)&(TCPF_CLOSE|TCPF_LISTEN)))
  262. goto out;
  263. /* Really, if the socket is already in listen state
  264.  * we can only allow the backlog to be adjusted.
  265.  */
  266. if (old_state != TCP_LISTEN) {
  267. err = tcp_listen_start(sk);
  268. if (err)
  269. goto out;
  270. }
  271. sk->max_ack_backlog = backlog;
  272. err = 0;
  273. out:
  274. release_sock(sk);
  275. return err;
  276. }
  277. /*
  278.  * Create an inet socket.
  279.  */
  280. static int inet_create(struct socket *sock, int protocol)
  281. {
  282. struct sock *sk;
  283.         struct list_head *p;
  284.         struct inet_protosw *answer;
  285. sock->state = SS_UNCONNECTED;
  286. sk = sk_alloc(PF_INET, GFP_KERNEL, 1);
  287. if (sk == NULL) 
  288. goto do_oom;
  289.   
  290. /* Look for the requested type/protocol pair. */
  291. answer = NULL;
  292. br_read_lock_bh(BR_NETPROTO_LOCK);
  293. list_for_each(p, &inetsw[sock->type]) {
  294. answer = list_entry(p, struct inet_protosw, list);
  295. /* Check the non-wild match. */
  296. if (protocol == answer->protocol) {
  297. if (protocol != IPPROTO_IP)
  298. break;
  299. } else {
  300. /* Check for the two wild cases. */
  301. if (IPPROTO_IP == protocol) {
  302. protocol = answer->protocol;
  303. break;
  304. }
  305. if (IPPROTO_IP == answer->protocol)
  306. break;
  307. }
  308. answer = NULL;
  309. }
  310. br_read_unlock_bh(BR_NETPROTO_LOCK);
  311. if (!answer)
  312. goto free_and_badtype;
  313. if (answer->capability > 0 && !capable(answer->capability))
  314. goto free_and_badperm;
  315. if (!protocol)
  316. goto free_and_noproto;
  317. sock->ops = answer->ops;
  318. sk->prot = answer->prot;
  319. sk->no_check = answer->no_check;
  320. if (INET_PROTOSW_REUSE & answer->flags)
  321. sk->reuse = 1;
  322. if (SOCK_RAW == sock->type) {
  323. sk->num = protocol;
  324. if (IPPROTO_RAW == protocol)
  325. sk->protinfo.af_inet.hdrincl = 1;
  326. }
  327. if (ipv4_config.no_pmtu_disc)
  328. sk->protinfo.af_inet.pmtudisc = IP_PMTUDISC_DONT;
  329. else
  330. sk->protinfo.af_inet.pmtudisc = IP_PMTUDISC_WANT;
  331. sk->protinfo.af_inet.id = 0;
  332. sock_init_data(sock,sk);
  333. sk->destruct = inet_sock_destruct;
  334. sk->zapped = 0;
  335. sk->family = PF_INET;
  336. sk->protocol = protocol;
  337. sk->backlog_rcv = sk->prot->backlog_rcv;
  338. sk->protinfo.af_inet.ttl = sysctl_ip_default_ttl;
  339. sk->protinfo.af_inet.mc_loop = 1;
  340. sk->protinfo.af_inet.mc_ttl = 1;
  341. sk->protinfo.af_inet.mc_index = 0;
  342. sk->protinfo.af_inet.mc_list = NULL;
  343. #ifdef INET_REFCNT_DEBUG
  344. atomic_inc(&inet_sock_nr);
  345. #endif
  346. if (sk->num) {
  347. /* It assumes that any protocol which allows
  348.  * the user to assign a number at socket
  349.  * creation time automatically
  350.  * shares.
  351.  */
  352. sk->sport = htons(sk->num);
  353. /* Add to protocol hash chains. */
  354. sk->prot->hash(sk);
  355. }
  356. if (sk->prot->init) {
  357. int err = sk->prot->init(sk);
  358. if (err != 0) {
  359. inet_sock_release(sk);
  360. return err;
  361. }
  362. }
  363. return 0;
  364. free_and_badtype:
  365. sk_free(sk);
  366. return -ESOCKTNOSUPPORT;
  367. free_and_badperm:
  368. sk_free(sk);
  369. return -EPERM;
  370. free_and_noproto:
  371. sk_free(sk);
  372. return -EPROTONOSUPPORT;
  373. do_oom:
  374. return -ENOBUFS;
  375. }
  376. /*
  377.  * The peer socket should always be NULL (or else). When we call this
  378.  * function we are destroying the object and from then on nobody
  379.  * should refer to it.
  380.  */
  381.  
  382. int inet_release(struct socket *sock)
  383. {
  384. struct sock *sk = sock->sk;
  385. if (sk) {
  386. long timeout;
  387. /* Applications forget to leave groups before exiting */
  388. ip_mc_drop_socket(sk);
  389. /* If linger is set, we don't return until the close
  390.  * is complete.  Otherwise we return immediately. The
  391.  * actually closing is done the same either way.
  392.  *
  393.  * If the close is due to the process exiting, we never
  394.  * linger..
  395.  */
  396. timeout = 0;
  397. if (sk->linger && !(current->flags & PF_EXITING))
  398. timeout = sk->lingertime;
  399. sock->sk = NULL;
  400. sk->prot->close(sk, timeout);
  401. }
  402. return(0);
  403. }
  404. /* It is off by default, see below. */
  405. int sysctl_ip_nonlocal_bind;
  406. static int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
  407. {
  408. struct sockaddr_in *addr=(struct sockaddr_in *)uaddr;
  409. struct sock *sk=sock->sk;
  410. unsigned short snum;
  411. int chk_addr_ret;
  412. int err;
  413. /* If the socket has its own bind function then use it. (RAW) */
  414. if(sk->prot->bind)
  415. return sk->prot->bind(sk, uaddr, addr_len);
  416. if (addr_len < sizeof(struct sockaddr_in))
  417. return -EINVAL;
  418. chk_addr_ret = inet_addr_type(addr->sin_addr.s_addr);
  419. /* Not specified by any standard per-se, however it breaks too
  420.  * many applications when removed.  It is unfortunate since
  421.  * allowing applications to make a non-local bind solves
  422.  * several problems with systems using dynamic addressing.
  423.  * (ie. your servers still start up even if your ISDN link
  424.  *  is temporarily down)
  425.  */
  426. if (sysctl_ip_nonlocal_bind == 0 && 
  427.     sk->protinfo.af_inet.freebind == 0 &&
  428.     addr->sin_addr.s_addr != INADDR_ANY &&
  429.     chk_addr_ret != RTN_LOCAL &&
  430.     chk_addr_ret != RTN_MULTICAST &&
  431.     chk_addr_ret != RTN_BROADCAST)
  432. return -EADDRNOTAVAIL;
  433. snum = ntohs(addr->sin_port);
  434. if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
  435. return -EACCES;
  436. /*      We keep a pair of addresses. rcv_saddr is the one
  437.  *      used by hash lookups, and saddr is used for transmit.
  438.  *
  439.  *      In the BSD API these are the same except where it
  440.  *      would be illegal to use them (multicast/broadcast) in
  441.  *      which case the sending device address is used.
  442.  */
  443. lock_sock(sk);
  444. /* Check these errors (active socket, double bind). */
  445. err = -EINVAL;
  446. if ((sk->state != TCP_CLOSE) ||
  447.     (sk->num != 0))
  448. goto out;
  449. sk->rcv_saddr = sk->saddr = addr->sin_addr.s_addr;
  450. if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
  451. sk->saddr = 0;  /* Use device */
  452. /* Make sure we are allowed to bind here. */
  453. if (sk->prot->get_port(sk, snum) != 0) {
  454. sk->saddr = sk->rcv_saddr = 0;
  455. err = -EADDRINUSE;
  456. goto out;
  457. }
  458. if (sk->rcv_saddr)
  459. sk->userlocks |= SOCK_BINDADDR_LOCK;
  460. if (snum)
  461. sk->userlocks |= SOCK_BINDPORT_LOCK;
  462. sk->sport = htons(sk->num);
  463. sk->daddr = 0;
  464. sk->dport = 0;
  465. sk_dst_reset(sk);
  466. err = 0;
  467. out:
  468. release_sock(sk);
  469. return err;
  470. }
  471. int inet_dgram_connect(struct socket *sock, struct sockaddr * uaddr,
  472.        int addr_len, int flags)
  473. {
  474. struct sock *sk=sock->sk;
  475. if (uaddr->sa_family == AF_UNSPEC)
  476. return sk->prot->disconnect(sk, flags);
  477. if (sk->num==0 && inet_autobind(sk) != 0)
  478. return -EAGAIN;
  479. return sk->prot->connect(sk, (struct sockaddr *)uaddr, addr_len);
  480. }
  481. static long inet_wait_for_connect(struct sock *sk, long timeo)
  482. {
  483. DECLARE_WAITQUEUE(wait, current);
  484. __set_current_state(TASK_INTERRUPTIBLE);
  485. add_wait_queue(sk->sleep, &wait);
  486. /* Basic assumption: if someone sets sk->err, he _must_
  487.  * change state of the socket from TCP_SYN_*.
  488.  * Connect() does not allow to get error notifications
  489.  * without closing the socket.
  490.  */
  491. while ((1<<sk->state)&(TCPF_SYN_SENT|TCPF_SYN_RECV)) {
  492. release_sock(sk);
  493. timeo = schedule_timeout(timeo);
  494. lock_sock(sk);
  495. if (signal_pending(current) || !timeo)
  496. break;
  497. set_current_state(TASK_INTERRUPTIBLE);
  498. }
  499. __set_current_state(TASK_RUNNING);
  500. remove_wait_queue(sk->sleep, &wait);
  501. return timeo;
  502. }
  503. /*
  504.  * Connect to a remote host. There is regrettably still a little
  505.  * TCP 'magic' in here.
  506.  */
  507.  
  508. int inet_stream_connect(struct socket *sock, struct sockaddr * uaddr,
  509. int addr_len, int flags)
  510. {
  511. struct sock *sk=sock->sk;
  512. int err;
  513. long timeo;
  514. lock_sock(sk);
  515. if (uaddr->sa_family == AF_UNSPEC) {
  516. err = sk->prot->disconnect(sk, flags);
  517. sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
  518. goto out;
  519. }
  520. switch (sock->state) {
  521. default:
  522. err = -EINVAL;
  523. goto out;
  524. case SS_CONNECTED:
  525. err = -EISCONN;
  526. goto out;
  527. case SS_CONNECTING:
  528. err = -EALREADY;
  529. /* Fall out of switch with err, set for this state */
  530. break;
  531. case SS_UNCONNECTED:
  532. err = -EISCONN;
  533. if (sk->state != TCP_CLOSE) 
  534. goto out;
  535. err = sk->prot->connect(sk, uaddr, addr_len);
  536. if (err < 0)
  537. goto out;
  538.    sock->state = SS_CONNECTING;
  539. /* Just entered SS_CONNECTING state; the only
  540.  * difference is that return value in non-blocking
  541.  * case is EINPROGRESS, rather than EALREADY.
  542.  */
  543. err = -EINPROGRESS;
  544. break;
  545. }
  546. timeo = sock_sndtimeo(sk, flags&O_NONBLOCK);
  547. if ((1<<sk->state)&(TCPF_SYN_SENT|TCPF_SYN_RECV)) {
  548. /* Error code is set above */
  549. if (!timeo || !inet_wait_for_connect(sk, timeo))
  550. goto out;
  551. err = sock_intr_errno(timeo);
  552. if (signal_pending(current))
  553. goto out;
  554. }
  555. /* Connection was closed by RST, timeout, ICMP error
  556.  * or another process disconnected us.
  557.  */
  558. if (sk->state == TCP_CLOSE)
  559. goto sock_error;
  560. /* sk->err may be not zero now, if RECVERR was ordered by user
  561.  * and error was received after socket entered established state.
  562.  * Hence, it is handled normally after connect() return successfully.
  563.  */
  564. sock->state = SS_CONNECTED;
  565. err = 0;
  566. out:
  567. release_sock(sk);
  568. return err;
  569. sock_error:
  570. err = sock_error(sk) ? : -ECONNABORTED;
  571. sock->state = SS_UNCONNECTED;
  572. if (sk->prot->disconnect(sk, flags))
  573. sock->state = SS_DISCONNECTING;
  574. goto out;
  575. }
  576. /*
  577.  * Accept a pending connection. The TCP layer now gives BSD semantics.
  578.  */
  579. int inet_accept(struct socket *sock, struct socket *newsock, int flags)
  580. {
  581. struct sock *sk1 = sock->sk;
  582. struct sock *sk2;
  583. int err = -EINVAL;
  584. if((sk2 = sk1->prot->accept(sk1,flags,&err)) == NULL)
  585. goto do_err;
  586. lock_sock(sk2);
  587. BUG_TRAP((1<<sk2->state)&(TCPF_ESTABLISHED|TCPF_CLOSE_WAIT|TCPF_CLOSE));
  588. sock_graft(sk2, newsock);
  589. newsock->state = SS_CONNECTED;
  590. release_sock(sk2);
  591. return 0;
  592. do_err:
  593. return err;
  594. }
  595. /*
  596.  * This does both peername and sockname.
  597.  */
  598.  
  599. static int inet_getname(struct socket *sock, struct sockaddr *uaddr,
  600.  int *uaddr_len, int peer)
  601. {
  602. struct sock *sk = sock->sk;
  603. struct sockaddr_in *sin = (struct sockaddr_in *)uaddr;
  604.   
  605. sin->sin_family = AF_INET;
  606. if (peer) {
  607. if (!sk->dport)
  608. return -ENOTCONN;
  609. if (((1<<sk->state)&(TCPF_CLOSE|TCPF_SYN_SENT)) && peer == 1)
  610. return -ENOTCONN;
  611. sin->sin_port = sk->dport;
  612. sin->sin_addr.s_addr = sk->daddr;
  613. } else {
  614. __u32 addr = sk->rcv_saddr;
  615. if (!addr)
  616. addr = sk->saddr;
  617. sin->sin_port = sk->sport;
  618. sin->sin_addr.s_addr = addr;
  619. }
  620. *uaddr_len = sizeof(*sin);
  621. return(0);
  622. }
  623. int inet_recvmsg(struct socket *sock, struct msghdr *msg, int size,
  624.  int flags, struct scm_cookie *scm)
  625. {
  626. struct sock *sk = sock->sk;
  627. int addr_len = 0;
  628. int err;
  629. err = sk->prot->recvmsg(sk, msg, size, flags&MSG_DONTWAIT,
  630. flags&~MSG_DONTWAIT, &addr_len);
  631. if (err >= 0)
  632. msg->msg_namelen = addr_len;
  633. return err;
  634. }
  635. int inet_sendmsg(struct socket *sock, struct msghdr *msg, int size,
  636.  struct scm_cookie *scm)
  637. {
  638. struct sock *sk = sock->sk;
  639. /* We may need to bind the socket. */
  640. if (sk->num==0 && inet_autobind(sk) != 0)
  641. return -EAGAIN;
  642. return sk->prot->sendmsg(sk, msg, size);
  643. }
  644. int inet_shutdown(struct socket *sock, int how)
  645. {
  646. struct sock *sk = sock->sk;
  647. int err = 0;
  648. /* This should really check to make sure
  649.  * the socket is a TCP socket. (WHY AC...)
  650.  */
  651. how++; /* maps 0->1 has the advantage of making bit 1 rcvs and
  652.        1->2 bit 2 snds.
  653.        2->3 */
  654. if ((how & ~SHUTDOWN_MASK) || how==0) /* MAXINT->0 */
  655. return -EINVAL;
  656. lock_sock(sk);
  657. if (sock->state == SS_CONNECTING) {
  658. if ((1<<sk->state)&(TCPF_SYN_SENT|TCPF_SYN_RECV|TCPF_CLOSE))
  659. sock->state = SS_DISCONNECTING;
  660. else
  661. sock->state = SS_CONNECTED;
  662. }
  663. switch (sk->state) {
  664. case TCP_CLOSE:
  665. err = -ENOTCONN;
  666. /* Hack to wake up other listeners, who can poll for
  667.    POLLHUP, even on eg. unconnected UDP sockets -- RR */
  668. default:
  669. sk->shutdown |= how;
  670. if (sk->prot->shutdown)
  671. sk->prot->shutdown(sk, how);
  672. break;
  673. /* Remaining two branches are temporary solution for missing
  674.  * close() in multithreaded environment. It is _not_ a good idea,
  675.  * but we have no choice until close() is repaired at VFS level.
  676.  */
  677. case TCP_LISTEN:
  678. if (!(how & RCV_SHUTDOWN))
  679. break;
  680. /* Fall through */
  681. case TCP_SYN_SENT:
  682. err = sk->prot->disconnect(sk, O_NONBLOCK);
  683. sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
  684. break;
  685. }
  686. /* Wake up anyone sleeping in poll. */
  687. sk->state_change(sk);
  688. release_sock(sk);
  689. return err;
  690. }
  691. /*
  692.  * ioctl() calls you can issue on an INET socket. Most of these are
  693.  * device configuration and stuff and very rarely used. Some ioctls
  694.  * pass on to the socket itself.
  695.  *
  696.  * NOTE: I like the idea of a module for the config stuff. ie ifconfig
  697.  * loads the devconfigure module does its configuring and unloads it.
  698.  * There's a good 20K of config code hanging around the kernel.
  699.  */
  700. static int inet_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
  701. {
  702. struct sock *sk = sock->sk;
  703. int err;
  704. int pid;
  705. switch(cmd) {
  706. case FIOSETOWN:
  707. case SIOCSPGRP:
  708. err = get_user(pid, (int *) arg);
  709. if (err)
  710. return err; 
  711. if (current->pid != pid && current->pgrp != -pid && 
  712.     !capable(CAP_NET_ADMIN))
  713. return -EPERM;
  714. sk->proc = pid;
  715. return(0);
  716. case FIOGETOWN:
  717. case SIOCGPGRP:
  718. return put_user(sk->proc, (int *)arg);
  719. case SIOCGSTAMP:
  720. if(sk->stamp.tv_sec==0)
  721. return -ENOENT;
  722. err = copy_to_user((void *)arg,&sk->stamp,sizeof(struct timeval));
  723. if (err)
  724. err = -EFAULT;
  725. return err;
  726. case SIOCADDRT:
  727. case SIOCDELRT:
  728. case SIOCRTMSG:
  729. return(ip_rt_ioctl(cmd,(void *) arg));
  730. case SIOCDARP:
  731. case SIOCGARP:
  732. case SIOCSARP:
  733. return(arp_ioctl(cmd,(void *) arg));
  734. case SIOCGIFADDR:
  735. case SIOCSIFADDR:
  736. case SIOCGIFBRDADDR:
  737. case SIOCSIFBRDADDR:
  738. case SIOCGIFNETMASK:
  739. case SIOCSIFNETMASK:
  740. case SIOCGIFDSTADDR:
  741. case SIOCSIFDSTADDR:
  742. case SIOCSIFPFLAGS:
  743. case SIOCGIFPFLAGS:
  744. case SIOCSIFFLAGS:
  745. return(devinet_ioctl(cmd,(void *) arg));
  746. case SIOCGIFBR:
  747. case SIOCSIFBR:
  748. #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
  749. #ifdef CONFIG_KMOD
  750. if (br_ioctl_hook == NULL)
  751. request_module("bridge");
  752. #endif
  753. if (br_ioctl_hook != NULL)
  754. return br_ioctl_hook(arg);
  755. #endif
  756. return -ENOPKG;
  757. case SIOCGIFVLAN:
  758. case SIOCSIFVLAN:
  759. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  760. #ifdef CONFIG_KMOD
  761. if (vlan_ioctl_hook == NULL)
  762. request_module("8021q");
  763. #endif
  764. if (vlan_ioctl_hook != NULL)
  765. return vlan_ioctl_hook(arg);
  766. #endif
  767. return -ENOPKG;
  768. case SIOCGIFDIVERT:
  769. case SIOCSIFDIVERT:
  770. #ifdef CONFIG_NET_DIVERT
  771. return divert_ioctl(cmd, (struct divert_cf *) arg);
  772. #else
  773. return -ENOPKG;
  774. #endif /* CONFIG_NET_DIVERT */
  775. case SIOCADDDLCI:
  776. case SIOCDELDLCI:
  777. #ifdef CONFIG_DLCI
  778. lock_kernel();
  779. err = dlci_ioctl(cmd, (void *) arg);
  780. unlock_kernel();
  781. return err;
  782. #endif
  783. #ifdef CONFIG_DLCI_MODULE
  784. #ifdef CONFIG_KMOD
  785. if (dlci_ioctl_hook == NULL)
  786. request_module("dlci");
  787. #endif
  788. if (dlci_ioctl_hook) {
  789. lock_kernel();
  790. err = (*dlci_ioctl_hook)(cmd, (void *) arg);
  791. unlock_kernel();
  792. return err;
  793. }
  794. #endif
  795. return -ENOPKG;
  796. default:
  797. if ((cmd >= SIOCDEVPRIVATE) &&
  798.     (cmd <= (SIOCDEVPRIVATE + 15)))
  799. return(dev_ioctl(cmd,(void *) arg));
  800. #ifdef WIRELESS_EXT
  801. if((cmd >= SIOCIWFIRST) && (cmd <= SIOCIWLAST))
  802. return(dev_ioctl(cmd,(void *) arg));
  803. #endif /* WIRELESS_EXT */
  804. if (sk->prot->ioctl==NULL || (err=sk->prot->ioctl(sk, cmd, arg))==-ENOIOCTLCMD)
  805. return(dev_ioctl(cmd,(void *) arg));
  806. return err;
  807. }
  808. /*NOTREACHED*/
  809. return(0);
  810. }
  811. struct proto_ops inet_stream_ops = {
  812. family: PF_INET,
  813. release: inet_release,
  814. bind: inet_bind,
  815. connect: inet_stream_connect,
  816. socketpair: sock_no_socketpair,
  817. accept: inet_accept,
  818. getname: inet_getname, 
  819. poll: tcp_poll,
  820. ioctl: inet_ioctl,
  821. listen: inet_listen,
  822. shutdown: inet_shutdown,
  823. setsockopt: inet_setsockopt,
  824. getsockopt: inet_getsockopt,
  825. sendmsg: inet_sendmsg,
  826. recvmsg: inet_recvmsg,
  827. mmap: sock_no_mmap,
  828. sendpage: tcp_sendpage
  829. };
  830. struct proto_ops inet_dgram_ops = {
  831. family: PF_INET,
  832. release: inet_release,
  833. bind: inet_bind,
  834. connect: inet_dgram_connect,
  835. socketpair: sock_no_socketpair,
  836. accept: sock_no_accept,
  837. getname: inet_getname, 
  838. poll: datagram_poll,
  839. ioctl: inet_ioctl,
  840. listen: sock_no_listen,
  841. shutdown: inet_shutdown,
  842. setsockopt: inet_setsockopt,
  843. getsockopt: inet_getsockopt,
  844. sendmsg: inet_sendmsg,
  845. recvmsg: inet_recvmsg,
  846. mmap: sock_no_mmap,
  847. sendpage: sock_no_sendpage,
  848. };
  849. struct net_proto_family inet_family_ops = {
  850. family: PF_INET,
  851. create: inet_create
  852. };
  853. extern void tcp_init(void);
  854. extern void tcp_v4_init(struct net_proto_family *);
  855. /* Upon startup we insert all the elements in inetsw_array[] into
  856.  * the linked list inetsw.
  857.  */
  858. static struct inet_protosw inetsw_array[] =
  859. {
  860.         {
  861.                 type:        SOCK_STREAM,
  862.                 protocol:    IPPROTO_TCP,
  863.                 prot:        &tcp_prot,
  864.                 ops:         &inet_stream_ops,
  865.                 capability:  -1,
  866.                 no_check:    0,
  867.                 flags:       INET_PROTOSW_PERMANENT,
  868.         },
  869.         {
  870.                 type:        SOCK_DGRAM,
  871.                 protocol:    IPPROTO_UDP,
  872.                 prot:        &udp_prot,
  873.                 ops:         &inet_dgram_ops,
  874.                 capability:  -1,
  875.                 no_check:    UDP_CSUM_DEFAULT,
  876.                 flags:       INET_PROTOSW_PERMANENT,
  877.        },
  878.         
  879.        {
  880.                type:        SOCK_RAW,
  881.                protocol:    IPPROTO_IP, /* wild card */
  882.                prot:        &raw_prot,
  883.                ops:         &inet_dgram_ops,
  884.                capability:  CAP_NET_RAW,
  885.                no_check:    UDP_CSUM_DEFAULT,
  886.                flags:       INET_PROTOSW_REUSE,
  887.        }
  888. };
  889. #define INETSW_ARRAY_LEN (sizeof(inetsw_array) / sizeof(struct inet_protosw))
  890. void
  891. inet_register_protosw(struct inet_protosw *p)
  892. {
  893. struct list_head *lh;
  894. struct inet_protosw *answer;
  895. int protocol = p->protocol;
  896. br_write_lock_bh(BR_NETPROTO_LOCK);
  897. if (p->type > SOCK_MAX)
  898. goto out_illegal;
  899. /* If we are trying to override a permanent protocol, bail. */
  900. answer = NULL;
  901. list_for_each(lh, &inetsw[p->type]) {
  902. answer = list_entry(lh, struct inet_protosw, list);
  903. /* Check only the non-wild match. */
  904. if (protocol == answer->protocol &&
  905.     (INET_PROTOSW_PERMANENT & answer->flags))
  906. break;
  907. answer = NULL;
  908. }
  909. if (answer)
  910. goto out_permanent;
  911. /* Add to the BEGINNING so that we override any existing
  912.  * entry.  This means that when we remove this entry, the
  913.  * system automatically returns to the old behavior.
  914.  */
  915. list_add(&p->list, &inetsw[p->type]);
  916. out:
  917. br_write_unlock_bh(BR_NETPROTO_LOCK);
  918. return;
  919. out_permanent:
  920. printk(KERN_ERR "Attempt to override permanent protocol %d.n",
  921.        protocol);
  922. goto out;
  923. out_illegal:
  924. printk(KERN_ERR
  925.        "Ignoring attempt to register illegal socket type %d.n",
  926.        p->type);
  927. goto out;
  928. }
  929. void
  930. inet_unregister_protosw(struct inet_protosw *p)
  931. {
  932. if (INET_PROTOSW_PERMANENT & p->flags) {
  933. printk(KERN_ERR
  934.        "Attempt to unregister permanent protocol %d.n",
  935.        p->protocol);
  936. } else {
  937. br_write_lock_bh(BR_NETPROTO_LOCK);
  938. list_del(&p->list);
  939. br_write_unlock_bh(BR_NETPROTO_LOCK);
  940. }
  941. }
  942. /*
  943.  * Called by socket.c on kernel startup.  
  944.  */
  945.  
  946. static int __init inet_init(void)
  947. {
  948. struct sk_buff *dummy_skb;
  949. struct inet_protocol *p;
  950. struct inet_protosw *q;
  951. struct list_head *r;
  952. printk(KERN_INFO "NET4: Linux TCP/IP 1.0 for NET4.0n");
  953. if (sizeof(struct inet_skb_parm) > sizeof(dummy_skb->cb)) {
  954. printk(KERN_CRIT "inet_proto_init: panicn");
  955. return -EINVAL;
  956. }
  957. /*
  958.  * Tell SOCKET that we are alive... 
  959.  */
  960.    
  961.    (void) sock_register(&inet_family_ops);
  962. /*
  963.  * Add all the protocols. 
  964.  */
  965. printk(KERN_INFO "IP Protocols: ");
  966. for (p = inet_protocol_base; p != NULL;) {
  967. struct inet_protocol *tmp = (struct inet_protocol *) p->next;
  968. inet_add_protocol(p);
  969. printk("%s%s",p->name,tmp?", ":"n");
  970. p = tmp;
  971. }
  972. /* Register the socket-side information for inet_create. */
  973. for(r = &inetsw[0]; r < &inetsw[SOCK_MAX]; ++r)
  974. INIT_LIST_HEAD(r);
  975. for(q = inetsw_array; q < &inetsw_array[INETSW_ARRAY_LEN]; ++q)
  976. inet_register_protosw(q);
  977. /*
  978.  * Set the ARP module up
  979.  */
  980. arp_init();
  981.    /*
  982.     * Set the IP module up
  983.     */
  984. ip_init();
  985. tcp_v4_init(&inet_family_ops);
  986. /* Setup TCP slab cache for open requests. */
  987. tcp_init();
  988. /*
  989.  * Set the ICMP layer up
  990.  */
  991. icmp_init(&inet_family_ops);
  992. /* I wish inet_add_protocol had no constructor hook...
  993.    I had to move IPIP from net/ipv4/protocol.c :-( --ANK
  994.  */
  995. #ifdef CONFIG_NET_IPIP
  996. ipip_init();
  997. #endif
  998. #ifdef CONFIG_NET_IPGRE
  999. ipgre_init();
  1000. #endif
  1001. /*
  1002.  * Initialise the multicast router
  1003.  */
  1004. #if defined(CONFIG_IP_MROUTE)
  1005. ip_mr_init();
  1006. #endif
  1007. /*
  1008.  * Create all the /proc entries.
  1009.  */
  1010. #ifdef CONFIG_PROC_FS
  1011. proc_net_create ("raw", 0, raw_get_info);
  1012. proc_net_create ("netstat", 0, netstat_get_info);
  1013. proc_net_create ("snmp", 0, snmp_get_info);
  1014. proc_net_create ("sockstat", 0, afinet_get_info);
  1015. proc_net_create ("tcp", 0, tcp_get_info);
  1016. proc_net_create ("udp", 0, udp_get_info);
  1017. #endif /* CONFIG_PROC_FS */
  1018. return 0;
  1019. }
  1020. module_init(inet_init);