2004-02-24 18:22:01 +03:00
|
|
|
/* $NetBSD: ns_pcb.c,v 1.20 2004/02/24 15:22:01 wiz Exp $ */
|
1994-06-29 10:39:25 +04:00
|
|
|
|
1993-04-09 16:00:07 +04:00
|
|
|
/*
|
1994-05-13 10:10:16 +04:00
|
|
|
* Copyright (c) 1984, 1985, 1986, 1987, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-04-09 16:00:07 +04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 20:26:28 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-04-09 16:00:07 +04:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1994-06-29 10:39:25 +04:00
|
|
|
* @(#)ns_pcb.c 8.1 (Berkeley) 6/10/93
|
1993-04-09 16:00:07 +04:00
|
|
|
*/
|
|
|
|
|
2001-11-13 03:56:55 +03:00
|
|
|
#include <sys/cdefs.h>
|
2004-02-24 18:22:01 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: ns_pcb.c,v 1.20 2004/02/24 15:22:01 wiz Exp $");
|
2001-11-13 03:56:55 +03:00
|
|
|
|
1993-12-18 03:40:47 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/socketvar.h>
|
|
|
|
#include <sys/protosw.h>
|
1996-05-22 17:54:55 +04:00
|
|
|
#include <sys/proc.h>
|
1993-04-09 16:00:07 +04:00
|
|
|
|
1993-12-18 03:40:47 +03:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/route.h>
|
1993-04-09 16:00:07 +04:00
|
|
|
|
1993-12-18 03:40:47 +03:00
|
|
|
#include <netns/ns.h>
|
|
|
|
#include <netns/ns_if.h>
|
|
|
|
#include <netns/ns_pcb.h>
|
1996-02-14 01:13:43 +03:00
|
|
|
#include <netns/ns_var.h>
|
1993-04-09 16:00:07 +04:00
|
|
|
|
2001-06-19 11:37:16 +04:00
|
|
|
static const struct ns_addr zerons_addr;
|
1993-04-09 16:00:07 +04:00
|
|
|
|
1996-02-14 01:13:43 +03:00
|
|
|
int
|
1993-04-09 16:00:07 +04:00
|
|
|
ns_pcballoc(so, head)
|
|
|
|
struct socket *so;
|
|
|
|
struct nspcb *head;
|
|
|
|
{
|
2000-03-30 16:51:13 +04:00
|
|
|
struct nspcb *nsp;
|
1993-04-09 16:00:07 +04:00
|
|
|
|
1995-08-16 04:32:40 +04:00
|
|
|
nsp = malloc(sizeof(*nsp), M_PCB, M_NOWAIT);
|
|
|
|
if (nsp == 0)
|
1993-04-09 16:00:07 +04:00
|
|
|
return (ENOBUFS);
|
1995-08-16 04:32:40 +04:00
|
|
|
bzero((caddr_t)nsp, sizeof(*nsp));
|
1993-04-09 16:00:07 +04:00
|
|
|
nsp->nsp_socket = so;
|
|
|
|
insque(nsp, head);
|
1995-08-17 06:57:20 +04:00
|
|
|
so->so_pcb = nsp;
|
1993-04-09 16:00:07 +04:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1996-02-14 01:13:43 +03:00
|
|
|
int
|
1996-05-22 17:54:55 +04:00
|
|
|
ns_pcbbind(nsp, nam, p)
|
2000-03-30 16:51:13 +04:00
|
|
|
struct nspcb *nsp;
|
1993-04-09 16:00:07 +04:00
|
|
|
struct mbuf *nam;
|
1996-05-22 17:54:55 +04:00
|
|
|
struct proc *p;
|
1993-04-09 16:00:07 +04:00
|
|
|
{
|
2000-03-30 16:51:13 +04:00
|
|
|
struct sockaddr_ns *sns;
|
1997-07-18 23:30:33 +04:00
|
|
|
u_int16_t lport = 0;
|
1993-04-09 16:00:07 +04:00
|
|
|
|
|
|
|
if (nsp->nsp_lport || !ns_nullhost(nsp->nsp_laddr))
|
|
|
|
return (EINVAL);
|
|
|
|
if (nam == 0)
|
|
|
|
goto noname;
|
|
|
|
sns = mtod(nam, struct sockaddr_ns *);
|
|
|
|
if (nam->m_len != sizeof (*sns))
|
|
|
|
return (EINVAL);
|
|
|
|
if (!ns_nullhost(sns->sns_addr)) {
|
|
|
|
int tport = sns->sns_port;
|
|
|
|
|
|
|
|
sns->sns_port = 0; /* yech... */
|
1995-06-13 12:36:58 +04:00
|
|
|
if (ifa_ifwithaddr(snstosa(sns)) == 0)
|
1993-04-09 16:00:07 +04:00
|
|
|
return (EADDRNOTAVAIL);
|
|
|
|
sns->sns_port = tport;
|
|
|
|
}
|
|
|
|
lport = sns->sns_port;
|
|
|
|
if (lport) {
|
|
|
|
|
1996-05-22 17:54:55 +04:00
|
|
|
if (ntohs(lport) < NSPORT_RESERVED &&
|
2003-01-20 04:38:43 +03:00
|
|
|
(p == 0 || suser(p->p_ucred, &p->p_acflag)))
|
1993-04-09 16:00:07 +04:00
|
|
|
return (EACCES);
|
|
|
|
if (ns_pcblookup(&zerons_addr, lport, 0))
|
|
|
|
return (EADDRINUSE);
|
|
|
|
}
|
|
|
|
nsp->nsp_laddr = sns->sns_addr;
|
|
|
|
noname:
|
|
|
|
if (lport == 0)
|
|
|
|
do {
|
|
|
|
if (nspcb.nsp_lport++ < NSPORT_RESERVED)
|
|
|
|
nspcb.nsp_lport = NSPORT_RESERVED;
|
|
|
|
lport = htons(nspcb.nsp_lport);
|
|
|
|
} while (ns_pcblookup(&zerons_addr, lport, 0));
|
|
|
|
nsp->nsp_lport = lport;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Connect from a socket to a specified address.
|
|
|
|
* Both address and port must be specified in argument sns.
|
|
|
|
* If don't have a local address for this socket yet,
|
|
|
|
* then pick one.
|
|
|
|
*/
|
1996-02-14 01:13:43 +03:00
|
|
|
int
|
1993-04-09 16:00:07 +04:00
|
|
|
ns_pcbconnect(nsp, nam)
|
|
|
|
struct nspcb *nsp;
|
|
|
|
struct mbuf *nam;
|
|
|
|
{
|
|
|
|
struct ns_ifaddr *ia;
|
2000-03-30 16:51:13 +04:00
|
|
|
struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
|
|
|
|
struct ns_addr *dst;
|
|
|
|
struct route *ro;
|
1993-04-09 16:00:07 +04:00
|
|
|
struct ifnet *ifp;
|
|
|
|
|
|
|
|
if (nam->m_len != sizeof (*sns))
|
|
|
|
return (EINVAL);
|
|
|
|
if (sns->sns_family != AF_NS)
|
|
|
|
return (EAFNOSUPPORT);
|
|
|
|
if (sns->sns_port==0 || ns_nullhost(sns->sns_addr))
|
|
|
|
return (EADDRNOTAVAIL);
|
|
|
|
/*
|
|
|
|
* If we haven't bound which network number to use as ours,
|
|
|
|
* we will use the number of the outgoing interface.
|
|
|
|
* This depends on having done a routing lookup, which
|
|
|
|
* we will probably have to do anyway, so we might
|
|
|
|
* as well do it now. On the other hand if we are
|
|
|
|
* sending to multiple destinations we may have already
|
|
|
|
* done the lookup, so see if we can use the route
|
|
|
|
* from before. In any case, we only
|
|
|
|
* chose a port number once, even if sending to multiple
|
|
|
|
* destinations.
|
|
|
|
*/
|
|
|
|
ro = &nsp->nsp_route;
|
|
|
|
dst = &satons_addr(ro->ro_dst);
|
|
|
|
if (nsp->nsp_socket->so_options & SO_DONTROUTE)
|
|
|
|
goto flush;
|
|
|
|
if (!ns_neteq(nsp->nsp_lastdst, sns->sns_addr))
|
|
|
|
goto flush;
|
|
|
|
if (!ns_hosteq(nsp->nsp_lastdst, sns->sns_addr)) {
|
|
|
|
if (ro->ro_rt && ! (ro->ro_rt->rt_flags & RTF_HOST)) {
|
|
|
|
/* can patch route to avoid rtalloc */
|
|
|
|
*dst = sns->sns_addr;
|
|
|
|
} else {
|
|
|
|
flush:
|
|
|
|
if (ro->ro_rt)
|
|
|
|
RTFREE(ro->ro_rt);
|
|
|
|
ro->ro_rt = (struct rtentry *)0;
|
|
|
|
nsp->nsp_laddr.x_net = ns_zeronet;
|
|
|
|
}
|
|
|
|
}/* else cached route is ok; do nothing */
|
|
|
|
nsp->nsp_lastdst = sns->sns_addr;
|
|
|
|
if ((nsp->nsp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
|
|
|
|
(ro->ro_rt == (struct rtentry *)0 ||
|
|
|
|
ro->ro_rt->rt_ifp == (struct ifnet *)0)) {
|
|
|
|
/* No route yet, so try to acquire one */
|
|
|
|
ro->ro_dst.sa_family = AF_NS;
|
|
|
|
ro->ro_dst.sa_len = sizeof(ro->ro_dst);
|
|
|
|
*dst = sns->sns_addr;
|
|
|
|
dst->x_port = 0;
|
|
|
|
rtalloc(ro);
|
|
|
|
}
|
|
|
|
if (ns_neteqnn(nsp->nsp_laddr.x_net, ns_zeronet)) {
|
|
|
|
/*
|
|
|
|
* If route is known or can be allocated now,
|
|
|
|
* our src addr is taken from the i/f, else punt.
|
|
|
|
*/
|
|
|
|
|
1995-06-13 12:36:58 +04:00
|
|
|
ia = 0;
|
1993-04-09 16:00:07 +04:00
|
|
|
/*
|
|
|
|
* If we found a route, use the address
|
|
|
|
* corresponding to the outgoing interface
|
|
|
|
*/
|
2000-02-01 03:18:29 +03:00
|
|
|
if (ro->ro_rt && (ifp = ro->ro_rt->rt_ifp)) {
|
1995-06-13 12:36:58 +04:00
|
|
|
for (ia = ns_ifaddr.tqh_first; ia != 0;
|
2000-02-01 03:18:29 +03:00
|
|
|
ia = ia->ia_list.tqe_next) {
|
1993-04-09 16:00:07 +04:00
|
|
|
if (ia->ia_ifp == ifp)
|
|
|
|
break;
|
2000-02-01 03:18:29 +03:00
|
|
|
}
|
|
|
|
}
|
1993-04-09 16:00:07 +04:00
|
|
|
if (ia == 0) {
|
1997-07-18 23:30:33 +04:00
|
|
|
u_int16_t fport = sns->sns_addr.x_port;
|
1993-04-09 16:00:07 +04:00
|
|
|
sns->sns_addr.x_port = 0;
|
|
|
|
ia = (struct ns_ifaddr *)
|
1995-06-13 12:36:58 +04:00
|
|
|
ifa_ifwithdstaddr(snstosa(sns));
|
1993-04-09 16:00:07 +04:00
|
|
|
sns->sns_addr.x_port = fport;
|
|
|
|
if (ia == 0)
|
|
|
|
ia = ns_iaonnetof(&sns->sns_addr);
|
|
|
|
if (ia == 0)
|
1995-06-13 12:36:58 +04:00
|
|
|
ia = ns_ifaddr.tqh_first;
|
1993-04-09 16:00:07 +04:00
|
|
|
if (ia == 0)
|
|
|
|
return (EADDRNOTAVAIL);
|
|
|
|
}
|
|
|
|
nsp->nsp_laddr.x_net = satons_addr(ia->ia_addr).x_net;
|
|
|
|
}
|
|
|
|
if (ns_pcblookup(&sns->sns_addr, nsp->nsp_lport, 0))
|
|
|
|
return (EADDRINUSE);
|
|
|
|
if (ns_nullhost(nsp->nsp_laddr)) {
|
|
|
|
if (nsp->nsp_lport == 0)
|
1996-05-22 17:54:55 +04:00
|
|
|
(void) ns_pcbbind(nsp, (struct mbuf *)0,
|
|
|
|
(struct proc *)0);
|
1993-04-09 16:00:07 +04:00
|
|
|
nsp->nsp_laddr.x_host = ns_thishost;
|
|
|
|
}
|
|
|
|
nsp->nsp_faddr = sns->sns_addr;
|
|
|
|
/* Includes nsp->nsp_fport = sns->sns_port; */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1996-02-14 01:13:43 +03:00
|
|
|
void
|
1993-04-09 16:00:07 +04:00
|
|
|
ns_pcbdisconnect(nsp)
|
|
|
|
struct nspcb *nsp;
|
|
|
|
{
|
|
|
|
|
|
|
|
nsp->nsp_faddr = zerons_addr;
|
|
|
|
if (nsp->nsp_socket->so_state & SS_NOFDREF)
|
|
|
|
ns_pcbdetach(nsp);
|
|
|
|
}
|
|
|
|
|
1996-02-14 01:13:43 +03:00
|
|
|
void
|
1993-04-09 16:00:07 +04:00
|
|
|
ns_pcbdetach(nsp)
|
|
|
|
struct nspcb *nsp;
|
|
|
|
{
|
|
|
|
struct socket *so = nsp->nsp_socket;
|
|
|
|
|
|
|
|
so->so_pcb = 0;
|
|
|
|
sofree(so);
|
|
|
|
if (nsp->nsp_route.ro_rt)
|
|
|
|
rtfree(nsp->nsp_route.ro_rt);
|
|
|
|
remque(nsp);
|
1995-08-16 04:32:40 +04:00
|
|
|
free(nsp, M_PCB);
|
1993-04-09 16:00:07 +04:00
|
|
|
}
|
|
|
|
|
1996-02-14 01:13:43 +03:00
|
|
|
void
|
1993-04-09 16:00:07 +04:00
|
|
|
ns_setsockaddr(nsp, nam)
|
2000-03-30 16:51:13 +04:00
|
|
|
struct nspcb *nsp;
|
1993-04-09 16:00:07 +04:00
|
|
|
struct mbuf *nam;
|
|
|
|
{
|
2000-03-30 16:51:13 +04:00
|
|
|
struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
|
1993-04-09 16:00:07 +04:00
|
|
|
|
|
|
|
nam->m_len = sizeof (*sns);
|
|
|
|
sns = mtod(nam, struct sockaddr_ns *);
|
|
|
|
bzero((caddr_t)sns, sizeof (*sns));
|
|
|
|
sns->sns_len = sizeof(*sns);
|
|
|
|
sns->sns_family = AF_NS;
|
|
|
|
sns->sns_addr = nsp->nsp_laddr;
|
|
|
|
}
|
|
|
|
|
1996-02-14 01:13:43 +03:00
|
|
|
void
|
1993-04-09 16:00:07 +04:00
|
|
|
ns_setpeeraddr(nsp, nam)
|
2000-03-30 16:51:13 +04:00
|
|
|
struct nspcb *nsp;
|
1993-04-09 16:00:07 +04:00
|
|
|
struct mbuf *nam;
|
|
|
|
{
|
2000-03-30 16:51:13 +04:00
|
|
|
struct sockaddr_ns *sns = mtod(nam, struct sockaddr_ns *);
|
1993-04-09 16:00:07 +04:00
|
|
|
|
|
|
|
nam->m_len = sizeof (*sns);
|
|
|
|
sns = mtod(nam, struct sockaddr_ns *);
|
|
|
|
bzero((caddr_t)sns, sizeof (*sns));
|
|
|
|
sns->sns_len = sizeof(*sns);
|
|
|
|
sns->sns_family = AF_NS;
|
|
|
|
sns->sns_addr = nsp->nsp_faddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pass some notification to all connections of a protocol
|
|
|
|
* associated with address dst. Call the
|
|
|
|
* protocol specific routine to handle each connection.
|
2004-02-24 18:22:01 +03:00
|
|
|
* Also pass an extra parameter via the nspcb. (which may in fact
|
1993-04-09 16:00:07 +04:00
|
|
|
* be a parameter list!)
|
|
|
|
*/
|
1996-02-14 01:13:43 +03:00
|
|
|
void
|
1993-04-09 16:00:07 +04:00
|
|
|
ns_pcbnotify(dst, errno, notify, param)
|
2000-03-30 16:51:13 +04:00
|
|
|
struct ns_addr *dst;
|
1993-04-09 16:00:07 +04:00
|
|
|
long param;
|
1996-02-14 01:13:43 +03:00
|
|
|
int errno;
|
|
|
|
void (*notify) __P((struct nspcb *));
|
1993-04-09 16:00:07 +04:00
|
|
|
{
|
2000-03-30 16:51:13 +04:00
|
|
|
struct nspcb *nsp, *oinp;
|
2001-04-14 03:29:55 +04:00
|
|
|
int s = splnet();
|
1993-04-09 16:00:07 +04:00
|
|
|
|
|
|
|
for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb);) {
|
|
|
|
if (!ns_hosteq(*dst,nsp->nsp_faddr)) {
|
|
|
|
next:
|
|
|
|
nsp = nsp->nsp_next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (nsp->nsp_socket == 0)
|
|
|
|
goto next;
|
|
|
|
if (errno)
|
|
|
|
nsp->nsp_socket->so_error = errno;
|
|
|
|
oinp = nsp;
|
|
|
|
nsp = nsp->nsp_next;
|
|
|
|
oinp->nsp_notify_param = param;
|
|
|
|
(*notify)(oinp);
|
|
|
|
}
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* After a routing change, flush old routing
|
|
|
|
* and allocate a (hopefully) better one.
|
|
|
|
*/
|
1996-02-14 01:13:43 +03:00
|
|
|
void
|
1993-04-09 16:00:07 +04:00
|
|
|
ns_rtchange(nsp)
|
|
|
|
struct nspcb *nsp;
|
|
|
|
{
|
|
|
|
if (nsp->nsp_route.ro_rt) {
|
|
|
|
rtfree(nsp->nsp_route.ro_rt);
|
|
|
|
nsp->nsp_route.ro_rt = 0;
|
|
|
|
/*
|
|
|
|
* A new route can be allocated the next time
|
|
|
|
* output is attempted.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
/* SHOULD NOTIFY HIGHER-LEVEL PROTOCOLS */
|
|
|
|
}
|
|
|
|
|
|
|
|
struct nspcb *
|
|
|
|
ns_pcblookup(faddr, lport, wildp)
|
2001-06-19 11:37:16 +04:00
|
|
|
const struct ns_addr *faddr;
|
1997-07-18 23:30:33 +04:00
|
|
|
u_int16_t lport;
|
1996-02-14 01:13:43 +03:00
|
|
|
int wildp;
|
1993-04-09 16:00:07 +04:00
|
|
|
{
|
2000-03-30 16:51:13 +04:00
|
|
|
struct nspcb *nsp, *match = 0;
|
1993-04-09 16:00:07 +04:00
|
|
|
int matchwild = 3, wildcard;
|
1997-07-18 23:30:33 +04:00
|
|
|
u_int16_t fport;
|
1993-04-09 16:00:07 +04:00
|
|
|
|
|
|
|
fport = faddr->x_port;
|
|
|
|
for (nsp = (&nspcb)->nsp_next; nsp != (&nspcb); nsp = nsp->nsp_next) {
|
|
|
|
if (nsp->nsp_lport != lport)
|
|
|
|
continue;
|
|
|
|
wildcard = 0;
|
|
|
|
if (ns_nullhost(nsp->nsp_faddr)) {
|
|
|
|
if (!ns_nullhost(*faddr))
|
|
|
|
wildcard++;
|
|
|
|
} else {
|
|
|
|
if (ns_nullhost(*faddr))
|
|
|
|
wildcard++;
|
|
|
|
else {
|
|
|
|
if (!ns_hosteq(nsp->nsp_faddr, *faddr))
|
|
|
|
continue;
|
|
|
|
if (nsp->nsp_fport != fport) {
|
|
|
|
if (nsp->nsp_fport != 0)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
wildcard++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (wildcard && wildp==0)
|
|
|
|
continue;
|
|
|
|
if (wildcard < matchwild) {
|
|
|
|
match = nsp;
|
|
|
|
matchwild = wildcard;
|
|
|
|
if (wildcard == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (match);
|
|
|
|
}
|