NetBSD/sys/netccitt/pk_usrreq.c

632 lines
15 KiB
C
Raw Normal View History

2000-03-30 17:53:32 +04:00
/* $NetBSD: pk_usrreq.c,v 1.19 2000/03/30 13:53:37 augustss Exp $ */
/*
* Copyright (c) 1984 University of British Columbia.
* Copyright (c) 1992 Computer Science Department IV,
* University of Erlangen-Nuremberg, Germany.
* Copyright (c) 1991, 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by the
* Laboratory for Computation Vision and the Computer Science Department
2000-03-14 02:52:25 +03:00
* of the University of British Columbia and the Computer Science
* Department (IV) of the University of Erlangen-Nuremberg, Germany.
*
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* 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.
*
1998-03-01 05:20:01 +03:00
* @(#)pk_usrreq.c 8.2 (Berkeley) 1/9/95
*/
1993-12-18 03:40:47 +03:00
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/protosw.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/proc.h>
1993-12-18 03:40:47 +03:00
#include <net/if.h>
#include <net/if_types.h>
1993-12-18 03:40:47 +03:00
#include <net/route.h>
1993-12-18 03:40:47 +03:00
#include <netccitt/x25.h>
#include <netccitt/pk.h>
#include <netccitt/pk_var.h>
1996-02-14 01:04:21 +03:00
#include <netccitt/pk_extern.h>
static void pk_setsockaddr __P((struct pklcd *, struct mbuf *));
static void pk_setpeeraddr __P((struct pklcd *, struct mbuf *));
1996-02-14 01:04:21 +03:00
static void old_to_new __P((struct mbuf *));
static void new_to_old __P((struct mbuf *));
static void
pk_setsockaddr(lcp, nam)
struct pklcd *lcp;
struct mbuf *nam;
{
nam->m_len = sizeof(struct sockaddr_x25);
bcopy(lcp->lcd_ceaddr, mtod(nam, caddr_t), (size_t)nam->m_len);
if (lcp->lcd_flags & X25_OLDSOCKADDR)
new_to_old(nam);
}
static void
pk_setpeeraddr(lcp, nam)
struct pklcd *lcp;
struct mbuf *nam;
{
nam->m_len = sizeof(struct sockaddr_x25);
bcopy(lcp->lcd_craddr, mtod(nam, caddr_t), (size_t)nam->m_len);
if (lcp->lcd_flags & X25_OLDSOCKADDR)
new_to_old(nam);
}
/*
1996-02-14 01:04:21 +03:00
*
* X.25 Packet level protocol interface to socket abstraction.
*
* Process an X.25 user request on a logical channel. If this is a send
* request then m is the mbuf chain of the send data. If this is a timer
* expiration (called from the software clock routine) them timertype is
* the particular timer.
*
*/
1996-02-14 01:04:21 +03:00
int
pk_usrreq(so, req, m, nam, control, p)
struct socket *so;
int req;
struct mbuf *m, *nam, *control;
struct proc *p;
{
2000-03-30 17:53:32 +04:00
struct pklcd *lcp;
int s;
2000-03-30 17:53:32 +04:00
int error = 0;
if (req == PRU_CONTROL)
return (pk_control(so, (long)m, (caddr_t)nam,
(struct ifnet *)control, p));
s = splsoftnet();
lcp = (struct pklcd *)so->so_pcb;
#ifdef DIAGNOSTIC
if (req != PRU_SEND && req != PRU_SENDOOB && control)
panic("pk_usrreq: unexpected control mbuf");
#endif
if (lcp == 0 && req != PRU_ATTACH) {
error = EINVAL;
goto release;
}
1996-02-14 01:04:21 +03:00
/*
pk_trace (pkcbhead, TR_USER, (struct pklcd *)0,
req, (struct x25_packet *)0);
*/
switch (req) {
1996-02-14 01:04:21 +03:00
/*
* X.25 attaches to socket via PRU_ATTACH and allocates a
* logical channel descriptor. If the socket is to receive
* connections, then the LISTEN state is entered.
*/
case PRU_ATTACH:
if (lcp != 0) {
error = EISCONN;
break;
}
1996-02-14 01:04:21 +03:00
lcp = pk_attach(so);
if (lcp == 0)
error = ENOBUFS;
break;
1996-02-14 01:04:21 +03:00
/*
* Detach a logical channel from the socket. If the state of
* the channel is embryonic, simply discard it. Otherwise we
* have to initiate a PRU_DISCONNECT which will finish later.
*/
case PRU_DETACH:
pk_disconnect(lcp);
break;
1996-02-14 01:04:21 +03:00
/*
* Give the socket an address.
*/
case PRU_BIND:
if (nam->m_len == sizeof(struct x25_sockaddr))
old_to_new(nam);
error = pk_bind(lcp, nam);
break;
1996-02-14 01:04:21 +03:00
/*
* Prepare to accept connections.
*/
case PRU_LISTEN:
error = pk_listen(lcp);
break;
1996-02-14 01:04:21 +03:00
/*
* Initiate a CALL REQUEST to peer entity. Enter state
* SENT_CALL and mark the socket as connecting. Set timer
* waiting for CALL ACCEPT or CLEAR.
*/
case PRU_CONNECT:
if (nam->m_len == sizeof(struct x25_sockaddr))
old_to_new(nam);
if (pk_checksockaddr(nam)) {
error = EINVAL;
break;
}
1996-02-14 01:04:21 +03:00
error = pk_connect(lcp, mtod(nam, struct sockaddr_x25 *));
break;
case PRU_CONNECT2:
error = EOPNOTSUPP;
break;
1996-02-14 01:04:21 +03:00
/*
* Initiate a disconnect to peer entity via a CLEAR REQUEST
* packet. The socket will be disconnected when we receive a
* confirmation or a clear collision.
*/
case PRU_DISCONNECT:
pk_disconnect(lcp);
break;
1996-02-14 01:04:21 +03:00
/*
* Accept an INCOMING CALL. Most of the work has already been
* done by pk_input. Just return the callers address to the
* user.
*/
case PRU_ACCEPT:
if (lcp->lcd_craddr == NULL)
break;
pk_setpeeraddr(lcp, nam);
break;
case PRU_SHUTDOWN:
socantsendmore(so);
break;
1996-02-14 01:04:21 +03:00
/*
* After a receive, we should send a RR.
*/
case PRU_RCVD:
pk_flowcontrol(lcp, /* sbspace (&so -> so_rcv) <= */ 0, 1);
break;
1996-02-14 01:04:21 +03:00
/*
* Do send by placing data on the socket output queue.
*/
case PRU_SEND:
if (control) {
2000-03-30 17:53:32 +04:00
struct cmsghdr *ch = mtod(m, struct cmsghdr *);
1996-02-14 01:04:21 +03:00
control->m_len -= sizeof(*ch);
control->m_data += sizeof(*ch);
error = pk_ctloutput(PRCO_SETOPT, so, ch->cmsg_level,
ch->cmsg_type, &control);
if (error)
break;
}
if (m)
1996-02-14 01:04:21 +03:00
error = pk_send(m, lcp);
break;
1996-02-14 01:04:21 +03:00
/*
* Abort a virtual circuit. For example all completed calls
* waiting acceptance.
*/
case PRU_ABORT:
pk_disconnect(lcp);
break;
1996-02-14 01:04:21 +03:00
case PRU_SENSE:
/*
* stat: don't bother with a blocksize.
*/
splx(s);
return (0);
1996-02-14 01:04:21 +03:00
/*
* Receive INTERRUPT packet.
*/
case PRU_RCVOOB:
if (so->so_options & SO_OOBINLINE) {
2000-03-30 17:53:32 +04:00
struct mbuf *n = so->so_rcv.sb_mb;
1996-02-14 01:04:21 +03:00
if (n && n->m_type == MT_OOBDATA) {
unsigned len = n->m_pkthdr.len;
so->so_rcv.sb_mb = n->m_nextpkt;
if (len != n->m_len &&
(n = m_pullup(n, len)) == 0)
break;
1996-02-14 01:04:21 +03:00
m->m_len = len;
bcopy(mtod(m, caddr_t), mtod(n, caddr_t), len);
m_freem(n);
}
break;
}
1996-02-14 01:04:21 +03:00
m->m_len = 1;
*mtod(m, char *) = lcp->lcd_intrdata;
break;
/*
* Send INTERRUPT packet.
*/
case PRU_SENDOOB:
if (control) {
2000-03-30 17:53:32 +04:00
struct cmsghdr *ch = mtod(m, struct cmsghdr *);
control->m_len -= sizeof(*ch);
control->m_data += sizeof(*ch);
error = pk_ctloutput(PRCO_SETOPT, so, ch->cmsg_level,
ch->cmsg_type, &control);
if (error)
break;
}
if (m) {
MCHTYPE(m, MT_OOBDATA);
error = pk_send(m, lcp);
}
break;
case PRU_SOCKADDR:
if (lcp->lcd_ceaddr == 0) {
error = EADDRNOTAVAIL;
break;
}
pk_setsockaddr(lcp, nam);
break;
case PRU_PEERADDR:
if (lcp->lcd_state != DATA_TRANSFER) {
error = ENOTCONN;
break;
}
pk_setpeeraddr(lcp, nam);
break;
1996-02-14 01:04:21 +03:00
default:
panic("pk_usrreq");
}
release:
splx(s);
return (error);
}
1996-02-14 01:04:21 +03:00
/*
* If you want to use UBC X.25 level 3 in conjunction with some other X.25
* level 2 driver, have the ifp -> if_ioctl routine assign pk_start to ia ->
* ia_start when called with SIOCSIFCONF_X25.
*/
/* ARGSUSED */
1996-02-14 01:04:21 +03:00
int
pk_start(lcp)
2000-03-30 17:53:32 +04:00
struct pklcd *lcp;
{
1996-02-14 01:04:21 +03:00
pk_output(lcp);
return (0); /* XXX pk_output should return a value */
}
struct sockaddr_x25 pk_sockmask = {
1998-01-28 05:35:10 +03:00
offsetof(struct sockaddr_x25, x25_addr[0]), /* x25_len */
1996-02-14 01:04:21 +03:00
0, /* x25_family */
-1, /* x25_net id */
};
1996-02-14 01:04:21 +03:00
/* ARGSUSED */
int
pk_control(so, cmd, data, ifp, p)
struct socket *so;
u_long cmd;
caddr_t data;
2000-03-30 17:53:32 +04:00
struct ifnet *ifp;
struct proc *p;
{
2000-03-30 17:53:32 +04:00
struct ifreq_x25 *ifr = (struct ifreq_x25 *) data;
struct ifaddr *ifa = 0;
struct x25_ifaddr *ia = 0;
1996-02-14 01:04:21 +03:00
int error = 0, s, old_maxlcn;
/*
* Find address for this interface, if it exists.
*/
if (ifp)
for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
1996-02-14 01:04:21 +03:00
ifa = ifa->ifa_list.tqe_next)
if (ifa->ifa_addr->sa_family == AF_CCITT)
break;
1996-02-14 01:04:21 +03:00
ia = (struct x25_ifaddr *) ifa;
switch (cmd) {
case SIOCGIFCONF_X25:
if (ifa == 0)
return (EADDRNOTAVAIL);
1996-02-14 01:04:21 +03:00
ifr->ifr_xc = ia->ia_xc;
return (0);
case SIOCSIFCONF_X25:
if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
return (EPERM);
if (ifp == 0)
1996-02-14 01:04:21 +03:00
panic("pk_control");
if (ifa == (struct ifaddr *) 0) {
MALLOC(ia, struct x25_ifaddr *, sizeof(*ia),
M_IFADDR, M_WAITOK);
if (ia == 0)
return (ENOBUFS);
1996-02-14 01:04:21 +03:00
bzero((caddr_t) ia, sizeof(*ia));
TAILQ_INSERT_TAIL(&ifp->if_addrlist, &ia->ia_ifa,
1996-02-14 01:04:21 +03:00
ifa_list);
ifa = &ia->ia_ifa;
IFAREF(ifa);
1996-02-14 01:04:21 +03:00
ifa->ifa_netmask = (struct sockaddr *) & pk_sockmask;
ifa->ifa_addr = (struct sockaddr *) & ia->ia_xc.xc_addr;
ifa->ifa_dstaddr = (struct sockaddr *) & ia->ia_dstaddr; /* XXX */
ia->ia_ifp = ifp;
ia->ia_dstaddr.x25_family = AF_CCITT;
ia->ia_dstaddr.x25_len = pk_sockmask.x25_len;
} else if (ISISO8802(ifp) == 0) {
1996-02-14 01:04:21 +03:00
rtinit(ifa, (int) RTM_DELETE, 0);
}
1996-02-14 01:04:21 +03:00
old_maxlcn = ia->ia_maxlcn;
ia->ia_xc = ifr->ifr_xc;
ia->ia_dstaddr.x25_net = ia->ia_xc.xc_addr.x25_net;
if (ia->ia_maxlcn != old_maxlcn && old_maxlcn != 0) {
/* VERY messy XXX */
2000-03-30 17:53:32 +04:00
struct pkcb *pkp;
FOR_ALL_PKCBS(pkp)
1996-02-14 01:04:21 +03:00
if (pkp->pk_ia == ia)
pk_resize(pkp);
}
/*
* Give the interface a chance to initialize if this
p * is its first address, and to validate the address.
*/
1996-02-14 01:04:21 +03:00
ia->ia_start = pk_start;
s = splimp();
1996-02-14 01:04:21 +03:00
if (ifp->if_ioctl)
error = (*ifp->if_ioctl) (ifp, SIOCSIFCONF_X25,
(caddr_t) ifa);
if (error)
1996-02-14 01:04:21 +03:00
ifp->if_flags &= ~IFF_UP;
else if (ISISO8802(ifp) == 0)
1996-02-14 01:04:21 +03:00
error = rtinit(ifa, (int) RTM_ADD, RTF_UP);
splx(s);
return (error);
default:
1996-02-14 01:04:21 +03:00
if (ifp == 0 || ifp->if_ioctl == 0)
return (EOPNOTSUPP);
1996-02-14 01:04:21 +03:00
return ((*ifp->if_ioctl) (ifp, cmd, data));
}
}
1996-02-14 01:04:21 +03:00
int
pk_ctloutput(cmd, so, level, optname, mp)
struct socket *so;
struct mbuf **mp;
int cmd, level, optname;
{
struct proc *p = curproc; /* XXX */
2000-03-30 17:53:32 +04:00
struct mbuf *m = *mp;
struct pklcd *lcp = (struct pklcd *) so->so_pcb;
1996-02-14 01:04:21 +03:00
int error = EOPNOTSUPP;
if (m == 0)
return (EINVAL);
1996-02-14 01:04:21 +03:00
if (cmd == PRCO_SETOPT)
switch (optname) {
case PK_FACILITIES:
if (m == 0)
return (EINVAL);
lcp->lcd_facilities = m;
*mp = 0;
return (0);
case PK_ACCTFILE:
if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
1996-02-14 01:04:21 +03:00
error = EPERM;
else if (m->m_len)
error = pk_accton(mtod(m, char *));
else
error = pk_accton((char *) 0);
break;
1996-02-14 01:04:21 +03:00
case PK_RTATTACH:
error = pk_rtattach(so, m);
break;
1996-02-14 01:04:21 +03:00
case PK_PRLISTEN:
error = pk_user_protolisten(mtod(m, u_char *));
}
if (*mp) {
1996-02-14 01:04:21 +03:00
(void) m_freem(*mp);
*mp = 0;
}
return (error);
}
/*
* Do an in-place conversion of an "old style"
* socket address to the new style
*/
1996-02-14 01:04:21 +03:00
static void
old_to_new(m)
2000-03-30 17:53:32 +04:00
struct mbuf *m;
{
2000-03-30 17:53:32 +04:00
struct x25_sockaddr *oldp;
struct sockaddr_x25 *newp;
char *ocp, *ncp;
struct sockaddr_x25 new;
1996-02-14 01:04:21 +03:00
oldp = mtod(m, struct x25_sockaddr *);
newp = &new;
1996-02-14 01:04:21 +03:00
bzero((caddr_t) newp, sizeof(*newp));
1996-02-14 01:04:21 +03:00
newp->x25_family = AF_CCITT;
newp->x25_len = sizeof(*newp);
newp->x25_opts.op_flags = (oldp->xaddr_facilities & X25_REVERSE_CHARGE)
| X25_MQBIT | X25_OLDSOCKADDR;
1996-02-14 01:04:21 +03:00
if (oldp->xaddr_facilities & XS_HIPRIO) /* Datapac specific */
newp->x25_opts.op_psize = X25_PS128;
bcopy((caddr_t) oldp->xaddr_addr, newp->x25_addr,
(unsigned) min(oldp->xaddr_len, sizeof(newp->x25_addr) - 1));
if (bcmp((caddr_t) oldp->xaddr_proto, newp->x25_udata, 4) != 0) {
bcopy((caddr_t) oldp->xaddr_proto, newp->x25_udata, 4);
newp->x25_udlen = 4;
}
1996-02-14 01:04:21 +03:00
ocp = (caddr_t) oldp->xaddr_userdata;
ncp = newp->x25_udata + 4;
while (*ocp && ocp < (caddr_t) oldp->xaddr_userdata + 12) {
if (newp->x25_udlen == 0)
newp->x25_udlen = 4;
*ncp++ = *ocp++;
1996-02-14 01:04:21 +03:00
newp->x25_udlen++;
}
1996-02-14 01:04:21 +03:00
bcopy((caddr_t) newp, mtod(m, char *), sizeof(*newp));
m->m_len = sizeof(*newp);
}
/*
* Do an in-place conversion of a new style
* socket address to the old style
*/
1996-02-14 01:04:21 +03:00
static void
new_to_old(m)
2000-03-30 17:53:32 +04:00
struct mbuf *m;
{
2000-03-30 17:53:32 +04:00
struct x25_sockaddr *oldp;
struct sockaddr_x25 *newp;
char *ocp, *ncp;
struct x25_sockaddr old;
oldp = &old;
1996-02-14 01:04:21 +03:00
newp = mtod(m, struct sockaddr_x25 *);
bzero((caddr_t) oldp, sizeof(*oldp));
oldp->xaddr_facilities = newp->x25_opts.op_flags & X25_REVERSE_CHARGE;
if (newp->x25_opts.op_psize == X25_PS128)
oldp->xaddr_facilities |= XS_HIPRIO; /* Datapac specific */
ocp = (char *) oldp->xaddr_addr;
ncp = newp->x25_addr;
while (*ncp) {
*ocp++ = *ncp++;
1996-02-14 01:04:21 +03:00
oldp->xaddr_len++;
}
1996-02-14 01:04:21 +03:00
bcopy(newp->x25_udata, (caddr_t) oldp->xaddr_proto, 4);
if (newp->x25_udlen > 4)
bcopy(newp->x25_udata + 4, (caddr_t) oldp->xaddr_userdata,
(unsigned) (newp->x25_udlen - 4));
1996-02-14 01:04:21 +03:00
bcopy((caddr_t) oldp, mtod(m, char *), sizeof(*oldp));
m->m_len = sizeof(*oldp);
}
1996-02-14 01:04:21 +03:00
int
pk_checksockaddr(m)
struct mbuf *m;
{
2000-03-30 17:53:32 +04:00
struct sockaddr_x25 *sa = mtod(m, struct sockaddr_x25 *);
char *cp;
1996-02-14 01:04:21 +03:00
if (m->m_len != sizeof(struct sockaddr_x25))
return (1);
1996-02-14 01:04:21 +03:00
if (sa->x25_family != AF_CCITT ||
sa->x25_udlen > sizeof(sa->x25_udata))
return (1);
1996-02-14 01:04:21 +03:00
for (cp = sa->x25_addr; *cp; cp++) {
if (*cp < '0' || *cp > '9' ||
1996-02-14 01:04:21 +03:00
cp >= &sa->x25_addr[sizeof(sa->x25_addr) - 1])
return (1);
}
return (0);
}
1996-02-14 01:04:21 +03:00
int
pk_send(m, v)
2000-03-30 17:53:32 +04:00
struct mbuf *m;
1996-02-14 01:04:21 +03:00
void *v;
{
1996-02-14 01:04:21 +03:00
struct pklcd *lcp = v;
int mqbit = 0, error = 0;
2000-03-30 17:53:32 +04:00
struct x25_packet *xp;
struct socket *so;
1996-02-14 01:04:21 +03:00
if (m->m_type == MT_OOBDATA) {
if (lcp->lcd_intrconf_pending)
error = ETOOMANYREFS;
1996-02-14 01:04:21 +03:00
if (m->m_pkthdr.len > 32)
error = EMSGSIZE;
M_PREPEND(m, PKHEADERLN, M_WAITOK);
if (m == 0 || error)
goto bad;
1996-02-14 01:04:21 +03:00
*(mtod(m, octet *)) = 0;
xp = mtod(m, struct x25_packet *);
X25SBITS(xp->bits, fmt_identifier, 1);
xp->packet_type = X25_INTERRUPT;
SET_LCN(xp, lcp->lcd_lcn);
sbinsertoob((so = lcp->lcd_so) ?
&so->so_snd : &lcp->lcd_sb, m);
goto send;
}
/*
* Application has elected (at call setup time) to prepend
* a control byte to each packet written indicating m-bit
* and q-bit status. Examine and then discard this byte.
*/
1996-02-14 01:04:21 +03:00
if (lcp->lcd_flags & X25_MQBIT) {
if (m->m_len < 1) {
m_freem(m);
return (EMSGSIZE);
}
1996-02-14 01:04:21 +03:00
mqbit = *(mtod(m, u_char *));
m->m_len--;
m->m_data++;
m->m_pkthdr.len--;
}
1996-02-14 01:04:21 +03:00
error = pk_fragment(lcp, m, mqbit & 0x80, mqbit & 0x40, 1);
send:
1996-02-14 01:04:21 +03:00
if (error == 0 && lcp->lcd_state == DATA_TRANSFER)
lcp->lcd_send(lcp); /* XXXXXXXXX fix pk_output!!! */
return (error);
bad:
if (m)
1996-02-14 01:04:21 +03:00
m_freem(m);
return (error);
}