#include <sys/errno.h>
#include <sys/types.h>
#include <sys/param.h>
#include <machine/spl.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/kauth.h>
#include <sys/filedesc.h>
#include <sys/fcntl.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/protosw.h>
#include <sys/malloc.h>
#include <sys/proc.h>
#include <sys/uio_internal.h>
#include <kern/locks.h>
#include <netinet/in.h>
#include <net/if.h>
#include <netat/sysglue.h>
#include <netat/appletalk.h>
#include <netat/at_var.h>
#include <netat/routing_tables.h>
#include <netat/at_pcb.h>
#include <netat/aurp.h>
#include <netat/debug.h>
#define M_RCVBUF (64 * 1024)
#define M_SNDBUF (64 * 1024)
extern lck_mtx_t * atalk_mutex;
static int ip_to_atalk(struct sockaddr_in *fp, register gbuf_t *p_mbuf);
static int aurp_bindrp(struct socket *so);
struct aurp_global_t aurp_global;
int
aurpd_start()
{
register int error;
register struct socket *so;
struct mbuf *m;
int maxbuf;
struct sockopt sopt;
if (suser(kauth_cred_get(), 0) != 0 )
return(EPERM);
bzero((char *)&aurp_global.tunnel, sizeof(aurp_global.tunnel));
ATLOCKINIT(aurp_global.glock);
ATEVENTINIT(aurp_global.event_anchor);
if (aurp_global.udp_port == 0)
aurp_global.udp_port = AURP_SOCKNUM;
error = socreate(AF_INET, &aurp_global.tunnel, SOCK_DGRAM,
IPPROTO_UDP);
if (error)
{ dPrintf(D_M_AURP, D_L_FATAL, ("AURP: Can't get socket (%d)\n",
error));
return(error);
}
so = aurp_global.tunnel;
if ((error = aurp_bindrp(so)) != 0)
{ dPrintf(D_M_AURP, D_L_FATAL,
("AURP: Can't bind to port %d (error %d)\n",
aurp_global.udp_port, error));
soclose(so);
return(error);
}
sblock(&so->so_rcv, M_WAIT);
sblock(&so->so_snd, M_WAIT);
m = m_get(M_WAIT, MT_SOOPTS);
if (m == NULL) {
error = ENOBUFS;
goto out;
} else {
maxbuf = M_RCVBUF;
sopt.sopt_val = CAST_USER_ADDR_T(&maxbuf);
sopt.sopt_valsize = sizeof(maxbuf);
sopt.sopt_level = SOL_SOCKET;
sopt.sopt_name = SO_RCVBUF;
sopt.sopt_dir = SOPT_SET;
sopt.sopt_p = NULL;
if ((error = sosetopt(so, &sopt)) != 0)
goto out;
}
m = m_get(M_WAIT, MT_SOOPTS);
if (m == NULL) {
error = ENOBUFS;
goto out;
} else {
maxbuf = M_SNDBUF;
sopt.sopt_val = CAST_USER_ADDR_T(&maxbuf);
sopt.sopt_valsize = sizeof(maxbuf);
sopt.sopt_level = SOL_SOCKET;
sopt.sopt_name = SO_SNDBUF;
sopt.sopt_dir = SOPT_SET;
sopt.sopt_p = NULL;
if ((error = sosetopt(so, &sopt)) != 0)
goto out;
}
so->so_upcall = aurp_wakeup;
so->so_upcallarg = (caddr_t)AE_UDPIP;
so->so_state |= SS_NBIO;
so->so_rcv.sb_flags |=(SB_SEL|SB_NOINTR);
so->so_snd.sb_flags |=(SB_SEL|SB_NOINTR);
out:
sbunlock(&so->so_snd, 0);
sbunlock(&so->so_rcv, 0);
return(error);
}
int
AURPgetmsg(err)
int *err;
{ register struct socket *so;
register int events;
so = aurp_global.tunnel;
*err = 0;
for (;;)
{ gbuf_t *from, *p_mbuf;
int flags = MSG_DONTWAIT;
uio_t auio;
char uio_buf[ UIO_SIZEOF(0) ];
ATDISABLE(s, aurp_global.glock);
events = aurp_global.event;
if (((*err == 0) || (*err == EWOULDBLOCK)) && events == 0)
{
lck_mtx_assert(atalk_mutex, LCK_MTX_ASSERT_OWNED);
*err = msleep(&aurp_global.event_anchor, atalk_mutex, PSOCK | PCATCH, "AURPgetmsg", 0);
events = aurp_global.event;
aurp_global.event = 0;
}
ATENABLE(s, aurp_global.glock);
if (((*err != EWOULDBLOCK) && (*err != 0)) || events & AE_SHUTDOWN)
{
dPrintf(D_M_AURP, D_L_SHUTDN_INFO,
("AURPgetmsg: AE_SHUTDOWN detected--starting shutdown sequence\n"));
aurp_global.shutdown = 1;
while (aurp_global.running)
;
aurp_global.tunnel = 0;
aurp_global.event = 0;
aurp_global.shutdown = 0;
soclose(so);
if (*err == 0)
*err = ESHUTDOWN;
dPrintf(D_M_AURP, D_L_SHUTDN_INFO,
("AURPgetmsg: shutdown completed\n"));
return -1;
}
auio = uio_createwithbuffer(0, 0, UIO_SYSSPACE, UIO_READ,
&uio_buf[0], sizeof(uio_buf));
for (;;)
{
#define A_LARGE_SIZE 700
flags = MSG_DONTWAIT;
uio_setresid(auio, A_LARGE_SIZE);
*err = soreceive(so, (struct sockaddr **)&from, auio, &p_mbuf, 0, &flags);
dPrintf(D_M_AURP, D_L_VERBOSE,
("AURPgetmsg: soreceive returned %d, aurp_global.event==0x%x\n", *err, events));
if (p_mbuf)
ip_to_atalk((struct sockaddr_in *)from, p_mbuf);
if (*err || (p_mbuf == NULL)) {
ATDISABLE(s, aurp_global.glock);
aurp_global.event &= ~AE_UDPIP;
ATENABLE(s, aurp_global.glock);
dPrintf(D_M_AURP, D_L_WARNING, ("AURPgetmsg: spurious soreceive, err==%d, p_mbuf==0x%x\n", *err, (unsigned int) p_mbuf));
break;
}
}
}
return -1;
}
void aurp_wakeup(__unused struct socket *so, register caddr_t p, __unused int state)
{
register int bit;
bit = (int) p;
ATDISABLE(s, aurp_global.glock);
aurp_global.event |= bit;
ATENABLE(s, aurp_global.glock);
dPrintf(D_M_AURP, D_L_STATE_CHG,
("aurp_wakeup: bit 0x%x, aurp_global.event now 0x%x\n",
bit, aurp_global.event));
wakeup(&aurp_global.event_anchor);
}
static int
aurp_bindrp(struct socket *so)
{
struct sockaddr_in sin;
struct proc *p = current_proc();
int error;
bzero(&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htons(aurp_global.src_addr);
sin.sin_port = htons(aurp_global.udp_port);
sin.sin_len = sizeof(struct sockaddr_in);
sblock(&so->so_rcv, M_WAIT);
sblock(&so->so_snd, M_WAIT);
so->so_state |= SS_PRIV;
error = (*so->so_proto->pr_usrreqs->pru_bind)(so, (struct sockaddr *) &sin, p);
sbunlock(&so->so_snd, 0);
sbunlock(&so->so_rcv, 0);
return (error);
}
int ip_to_atalk(register struct sockaddr_in *rem_addr, register gbuf_t *p_mbuf)
{
register aurp_domain_t *domain;
unsigned char node;
for (node=1; node <= dst_addr_cnt; node++) {
if (aurp_global.dst_addr[node] == *(long *)&rem_addr->sin_addr)
break;
}
if (node > dst_addr_cnt) {
dPrintf(D_M_AURP, D_L_WARNING,
("AURPrecv: invalid node, %d.%lx\n",
rem_addr->sin_port,
rem_addr->sin_addr.s_addr));
gbuf_freem(p_mbuf);
FREE(rem_addr, M_SONAME);
return -1;
}
domain = (aurp_domain_t *)gbuf_rptr(p_mbuf);
if ( (domain->dst_length != IP_LENGTH) ||
(domain->dst_authority != IP_AUTHORITY) ||
(domain->version != AUD_Version) ||
((domain->type != AUD_Atalk) && (domain->type != AUD_AURP)) ) {
dPrintf(D_M_AURP, D_L_WARNING,
("AURPrecv: invalid domain, %d.%lx\n",
rem_addr->sin_port,
rem_addr->sin_addr.s_addr));
gbuf_freem(p_mbuf);
FREE(rem_addr, M_SONAME);
return -1;
}
p_mbuf->m_pkthdr.len -= IP_DOMAINSIZE;
gbuf_rinc(p_mbuf,IP_DOMAINSIZE);
gbuf_set_type(p_mbuf, MSG_DATA);
at_insert(p_mbuf, domain->type, node);
FREE(rem_addr, M_SONAME);
return 0;
}
void
atalk_to_ip(register gbuf_t *m)
{ register aurp_domain_t *domain;
int error;
int flags = MSG_DONTWAIT;
struct sockaddr_in rem_addr;
m->m_type = MT_HEADER;
m->m_pkthdr.len = gbuf_msgsize(m);
m->m_pkthdr.rcvif = 0;
bzero((char *) &rem_addr, sizeof(rem_addr));
rem_addr.sin_family = PF_INET;
rem_addr.sin_port = aurp_global.udp_port;
rem_addr.sin_len = sizeof (struct sockaddr_in);
domain = (aurp_domain_t *)gbuf_rptr(m);
*(long *) &rem_addr.sin_addr = domain->dst_address;
ATDISABLE(s, aurp_global.glock);
aurp_global.running++;
ATENABLE(s, aurp_global.glock);
if (aurp_global.shutdown) {
gbuf_freem(m);
ATDISABLE(s, aurp_global.glock);
aurp_global.running--;
ATENABLE(s, aurp_global.glock);
dPrintf(D_M_AURP, D_L_SHUTDN_INFO,
("atalk_to_ip: detected aurp_global.shutdown state\n"));
return;
}
dPrintf(D_M_AURP, D_L_VERBOSE, ("atalk_to_ip: calling sosend\n"));
error = sosend(aurp_global.tunnel, (struct sockaddr *) &rem_addr, NULL, m, NULL, flags);
if (error)
{
dPrintf(D_M_AURP, D_L_ERROR, ("AURP: sosend error (%d)\n",
error));
}
ATDISABLE(s, aurp_global.glock);
aurp_global.running--;
ATENABLE(s, aurp_global.glock);
return;
}