1998-01-13 05:26:06 +03:00
|
|
|
/* $NetBSD: ipkdb_if.c,v 1.7 1998/01/13 02:26:06 thorpej Exp $ */
|
1997-06-26 11:21:44 +04:00
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 1993-1996 Wolfgang Solfrank.
|
|
|
|
* Copyright (C) 1993-1996 TooLs GmbH.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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 TooLs GmbH.
|
|
|
|
* 4. The name of TooLs GmbH may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``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 TOOLS GMBH 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.
|
|
|
|
*/
|
|
|
|
#include "ether.h"
|
|
|
|
#include "bpfilter.h"
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/device.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
1997-04-19 05:52:15 +04:00
|
|
|
#include <net/if_dl.h>
|
1997-03-15 21:09:08 +03:00
|
|
|
#include <net/if_ether.h>
|
1996-09-30 20:16:45 +04:00
|
|
|
|
|
|
|
#include <netinet/in.h>
|
1997-03-15 21:09:08 +03:00
|
|
|
#include <netinet/if_inarp.h>
|
1996-09-30 20:16:45 +04:00
|
|
|
|
1996-10-16 23:32:08 +04:00
|
|
|
#include <ipkdb/ipkdb.h>
|
|
|
|
#include <machine/ipkdb.h>
|
1996-09-30 20:16:45 +04:00
|
|
|
|
1997-04-19 05:52:15 +04:00
|
|
|
int ipkdb_probe __P((struct device *, struct cfdata *, void *));
|
1996-10-16 23:32:08 +04:00
|
|
|
void ipkdb_attach __P((struct device *, struct device *, void *));
|
1996-09-30 20:16:45 +04:00
|
|
|
|
1996-10-16 23:32:08 +04:00
|
|
|
static void ipkdbrcpy __P((struct ipkdb_if * , void *, void *, int));
|
|
|
|
static void ipkdbwcpy __P((struct ipkdb_if * , void *, void *, int));
|
|
|
|
static int ipkdbread __P((struct ipkdb_if *));
|
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
/* For the config system the device doesn't exist */
|
|
|
|
int
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdb_probe(parent, match, aux)
|
1996-09-30 20:16:45 +04:00
|
|
|
struct device *parent;
|
1997-04-19 05:52:15 +04:00
|
|
|
struct cfdata *match;
|
|
|
|
void *aux;
|
1996-09-30 20:16:45 +04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdb_attach(parent, self, aux)
|
1996-09-30 20:16:45 +04:00
|
|
|
struct device *parent, *self;
|
|
|
|
void *aux;
|
|
|
|
{
|
1996-10-16 23:32:08 +04:00
|
|
|
panic("ipkdb_attach");
|
1996-09-30 20:16:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbrcpy(kip, vsp, vdp, l)
|
|
|
|
struct ipkdb_if *kip;
|
|
|
|
void *vsp;
|
|
|
|
void *vdp;
|
1996-09-30 20:16:45 +04:00
|
|
|
int l;
|
|
|
|
{
|
|
|
|
int l1;
|
1996-10-16 23:32:08 +04:00
|
|
|
char *sp = vsp, *dp = vdp;
|
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
/* bounce source pointer */
|
|
|
|
while (sp >= kip->gotbuf + sizeof kip->gotbuf)
|
|
|
|
sp -= sizeof kip->gotbuf;
|
|
|
|
l1 = kip->gotbuf + sizeof kip->gotbuf - sp;
|
|
|
|
if (l >= l1) {
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(sp, dp, l1);
|
1996-09-30 20:16:45 +04:00
|
|
|
l -= l1;
|
|
|
|
dp += l1;
|
|
|
|
sp = kip->gotbuf;
|
|
|
|
}
|
|
|
|
if (l)
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(sp, dp, l);
|
1996-09-30 20:16:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbwcpy(kip, vsp, vdp, l)
|
|
|
|
struct ipkdb_if *kip;
|
|
|
|
void *vsp;
|
|
|
|
void *vdp;
|
1996-09-30 20:16:45 +04:00
|
|
|
int l;
|
|
|
|
{
|
|
|
|
int l1;
|
1996-10-16 23:32:08 +04:00
|
|
|
char *sp = vsp, *dp = vdp;
|
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
/* bounce destination pointer */
|
|
|
|
while (dp >= kip->gotbuf + sizeof kip->gotbuf)
|
|
|
|
dp -= sizeof kip->gotbuf;
|
|
|
|
l1 = kip->gotbuf + sizeof kip->gotbuf - dp;
|
|
|
|
if (l >= l1) {
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(sp, dp, l1);
|
1996-09-30 20:16:45 +04:00
|
|
|
l -= l1;
|
|
|
|
sp += l1;
|
|
|
|
dp = kip->gotbuf;
|
|
|
|
}
|
|
|
|
if (l)
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(sp, dp, l);
|
1996-09-30 20:16:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbread(kip)
|
|
|
|
struct ipkdb_if *kip;
|
1996-09-30 20:16:45 +04:00
|
|
|
{
|
1997-03-15 21:09:08 +03:00
|
|
|
struct ifnet *ifp = &kip->arp->ec_if;
|
1996-09-30 20:16:45 +04:00
|
|
|
struct ether_header *eh;
|
|
|
|
struct mbuf *m, **mp, *head = 0;
|
|
|
|
int l, len;
|
|
|
|
char *buf = kip->got;
|
1996-10-16 23:32:08 +04:00
|
|
|
|
|
|
|
ipkdbrcpy(kip, buf, &len, sizeof(int));
|
1996-09-30 20:16:45 +04:00
|
|
|
buf += sizeof(int);
|
|
|
|
kip->got += len + sizeof(int);
|
|
|
|
if (kip->got >= kip->gotbuf + sizeof kip->gotbuf)
|
|
|
|
kip->got -= sizeof kip->gotbuf;
|
|
|
|
if ((kip->gotlen -= len + sizeof(int)) < 0)
|
|
|
|
goto bad;
|
1996-10-16 23:32:08 +04:00
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
/* Allocate a header mbuf */
|
|
|
|
MGETHDR(m, M_DONTWAIT, MT_DATA);
|
|
|
|
if (m == 0)
|
|
|
|
goto bad;
|
|
|
|
m->m_pkthdr.rcvif = ifp;
|
|
|
|
m->m_pkthdr.len = len;
|
|
|
|
l = MHLEN;
|
|
|
|
mp = &head;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pull packet out of buf.
|
|
|
|
*/
|
|
|
|
while (len > 0) {
|
|
|
|
if (head) {
|
|
|
|
MGET(m, M_DONTWAIT, MT_DATA);
|
1997-04-28 21:03:58 +04:00
|
|
|
if (m == 0) {
|
|
|
|
m_freem(head);
|
1996-09-30 20:16:45 +04:00
|
|
|
goto bad;
|
1997-04-28 21:03:58 +04:00
|
|
|
}
|
1996-09-30 20:16:45 +04:00
|
|
|
l = MLEN;
|
|
|
|
}
|
|
|
|
if (len >= MINCLSIZE) {
|
|
|
|
MCLGET(m, M_DONTWAIT);
|
1997-04-28 21:03:58 +04:00
|
|
|
if ((m->m_flags & M_EXT) == 0) {
|
|
|
|
m_free(m);
|
|
|
|
m_freem(head);
|
1997-04-24 12:14:04 +04:00
|
|
|
goto bad;
|
1997-04-28 21:03:58 +04:00
|
|
|
}
|
1997-04-24 12:14:04 +04:00
|
|
|
l = MCLBYTES;
|
1996-09-30 20:16:45 +04:00
|
|
|
}
|
|
|
|
m->m_len = l = min(len, l);
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbrcpy(kip, buf, mtod(m, caddr_t), l);
|
1996-09-30 20:16:45 +04:00
|
|
|
buf += l;
|
|
|
|
len -= l;
|
|
|
|
*mp = m;
|
|
|
|
mp = &m->m_next;
|
|
|
|
}
|
|
|
|
ifp->if_ipackets++;
|
|
|
|
eh = mtod(head, struct ether_header *);
|
|
|
|
|
|
|
|
#if NBPFILTER > 0
|
|
|
|
/*
|
|
|
|
* Check if there's a bp filter listening on this interface.
|
|
|
|
* If so, hand off the raw packet to bpf.
|
|
|
|
*/
|
1997-03-15 21:09:08 +03:00
|
|
|
if (kip->arp->ec_if.if_bpf)
|
|
|
|
bpf_mtap(kip->arp->ec_if.if_bpf, head);
|
1996-10-16 23:32:08 +04:00
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
/*
|
|
|
|
* Note that the interface cannot be in promiscuous mode if
|
|
|
|
* there are no bpf listeners. And if we are in promiscuous
|
|
|
|
* mode, we have to check if this packet is really ours.
|
|
|
|
*/
|
|
|
|
if ((ifp->if_flags&IFF_PROMISC)
|
1996-10-16 23:32:08 +04:00
|
|
|
&& ipkdbcmp(eh->ether_dhost,
|
1997-04-19 05:52:15 +04:00
|
|
|
LLADDR(ifp->if_sadl),
|
1996-10-16 23:32:08 +04:00
|
|
|
sizeof eh->ether_dhost)
|
1996-09-30 20:16:45 +04:00
|
|
|
!= 0
|
|
|
|
&& !(eh->ether_dhost[0] & 1)) { /* !mcast && !bcast */
|
|
|
|
m_freem(head);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
1996-10-16 23:32:08 +04:00
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
/*
|
|
|
|
* Fix up data start offset in mbuf to point past ether header
|
|
|
|
*/
|
|
|
|
m_adj(head, sizeof(struct ether_header));
|
1996-10-16 23:32:08 +04:00
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
ether_input(ifp, eh, head);
|
|
|
|
return 1;
|
1997-04-28 21:03:58 +04:00
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
bad:
|
|
|
|
ifp->if_ierrors++;
|
|
|
|
/* flush buffer */
|
|
|
|
kip->got = kip->gotbuf;
|
|
|
|
kip->gotlen = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* How to handle packets arriving during debugging:
|
|
|
|
* 0 - drop 'em immediately
|
|
|
|
* 1 - deliver 'em just as they would without debugging
|
|
|
|
*/
|
|
|
|
#ifdef __notyet__ /* results in mp_map overflows XXX */
|
1996-10-16 23:32:08 +04:00
|
|
|
char ipkdbget = 1;
|
1996-09-30 20:16:45 +04:00
|
|
|
#else
|
1996-10-16 23:32:08 +04:00
|
|
|
char ipkdbget = 0;
|
1996-09-30 20:16:45 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Interface driver interrupt handler calls here
|
1996-10-16 23:32:08 +04:00
|
|
|
* to get packets buffered by IPKDB.
|
1996-09-30 20:16:45 +04:00
|
|
|
*/
|
|
|
|
void
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbrint(kip, ifp)
|
|
|
|
struct ipkdb_if *kip;
|
1996-09-30 20:16:45 +04:00
|
|
|
struct ifnet *ifp;
|
|
|
|
{
|
1997-03-15 21:09:08 +03:00
|
|
|
if (kip && kip->arp && ifp == &kip->arp->ec_if)
|
1996-09-30 20:16:45 +04:00
|
|
|
while (kip->gotlen > 0)
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbread(kip);
|
1996-09-30 20:16:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-10-16 23:32:08 +04:00
|
|
|
* IPKDB hands out a packet that it doesn't want
|
1996-09-30 20:16:45 +04:00
|
|
|
*/
|
|
|
|
void
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbgotpkt(kip, cp, len)
|
|
|
|
struct ipkdb_if *kip;
|
1996-09-30 20:16:45 +04:00
|
|
|
char *cp;
|
|
|
|
int len;
|
|
|
|
{
|
|
|
|
char *buf;
|
1996-10-16 23:32:08 +04:00
|
|
|
|
|
|
|
if (!kip->arp || !ipkdbget)
|
1996-09-30 20:16:45 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (kip->gotlen + sizeof(int) + len > sizeof kip->gotbuf)
|
|
|
|
return;
|
1996-10-16 23:32:08 +04:00
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
buf = kip->got + kip->gotlen;
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbwcpy(kip, &len, buf, sizeof(int));
|
1996-09-30 20:16:45 +04:00
|
|
|
buf += sizeof(int);
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbwcpy(kip, cp, buf, len);
|
1996-09-30 20:16:45 +04:00
|
|
|
kip->gotlen += sizeof(int) + roundup(len, sizeof(int));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-10-16 23:32:08 +04:00
|
|
|
* IPKDB wants to know the IP address of its interface
|
1996-09-30 20:16:45 +04:00
|
|
|
*/
|
|
|
|
void
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbinet(kip)
|
|
|
|
struct ipkdb_if *kip;
|
1996-09-30 20:16:45 +04:00
|
|
|
{
|
|
|
|
struct ifaddr *ap;
|
1996-10-16 23:32:08 +04:00
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
if (kip->arp) {
|
1997-03-15 21:09:08 +03:00
|
|
|
for (ap = kip->arp->ec_if.if_addrlist.tqh_first; ap; ap = ap->ifa_list.tqe_next) {
|
1996-09-30 20:16:45 +04:00
|
|
|
if (ap->ifa_addr->sa_family == AF_INET) {
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbcopy(&((struct sockaddr_in *)ap->ifa_addr)->sin_addr,
|
1996-09-30 20:16:45 +04:00
|
|
|
kip->myinetaddr, sizeof kip->myinetaddr);
|
1996-10-16 23:32:08 +04:00
|
|
|
kip->flags |= IPKDB_MYIP;
|
1996-09-30 20:16:45 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-10-16 23:32:08 +04:00
|
|
|
* Initialize IPKDB Interface handling
|
1996-09-30 20:16:45 +04:00
|
|
|
*/
|
|
|
|
int
|
1996-10-16 23:32:08 +04:00
|
|
|
ipkdbifinit(kip, unit)
|
|
|
|
struct ipkdb_if *kip;
|
1996-09-30 20:16:45 +04:00
|
|
|
int unit;
|
|
|
|
{
|
|
|
|
struct cfdata *cfp, *pcfp;
|
|
|
|
extern struct cfdata cfdata[];
|
|
|
|
short *pp;
|
|
|
|
u_char *cp;
|
1996-10-16 23:32:08 +04:00
|
|
|
|
1996-09-30 20:16:45 +04:00
|
|
|
/* flush buffer */
|
|
|
|
kip->got = kip->gotbuf;
|
|
|
|
/* defaults: */
|
|
|
|
kip->mtu = ETHERMTU;
|
|
|
|
for (cp = kip->hisenetaddr; cp < kip->hisenetaddr + sizeof kip->hisenetaddr;)
|
|
|
|
*cp++ = -1;
|
|
|
|
for (cp = kip->hisinetaddr; cp < kip->hisinetaddr + sizeof kip->hisinetaddr;)
|
|
|
|
*cp++ = -1;
|
|
|
|
/* search for interface */
|
|
|
|
for (cfp = cfdata; cfp->cf_driver; cfp++) {
|
1996-10-16 23:32:08 +04:00
|
|
|
if (strcmp(cfp->cf_driver->cd_name, "ipkdb")
|
1996-09-30 20:16:45 +04:00
|
|
|
|| cfp->cf_unit != unit)
|
|
|
|
continue;
|
|
|
|
kip->cfp = cfp;
|
|
|
|
for (pp = cfp->cf_parents; *pp >= 0; pp++) {
|
|
|
|
pcfp = cfdata + *pp;
|
|
|
|
if (pcfp->cf_attach->ca_match((struct device *)0,
|
|
|
|
pcfp,
|
|
|
|
kip)
|
|
|
|
>= 0) {
|
1996-10-16 23:32:08 +04:00
|
|
|
printf("IPKDB on %s at address %x\n",
|
|
|
|
kip->name, kip->port);
|
|
|
|
if (cfp->cf_loc[0]) /* disable interface from system */
|
1996-09-30 20:16:45 +04:00
|
|
|
pcfp->cf_fstate = FSTATE_FOUND;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|