2005-02-27 01:45:09 +03:00
|
|
|
/* $NetBSD: if_hippisubr.c,v 1.18 2005/02/26 22:45:09 perry Exp $ */
|
1998-05-14 04:04:57 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1982, 1989, 1993
|
|
|
|
* The Regents of the University of California. 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.
|
2003-08-07 20:26:28 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1998-05-14 04:04:57 +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.
|
|
|
|
*/
|
|
|
|
|
2001-11-13 02:49:33 +03:00
|
|
|
#include <sys/cdefs.h>
|
2005-02-27 01:45:09 +03:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: if_hippisubr.c,v 1.18 2005/02/26 22:45:09 perry Exp $");
|
2001-11-13 02:49:33 +03:00
|
|
|
|
1998-07-05 04:51:04 +04:00
|
|
|
#include "opt_inet.h"
|
|
|
|
|
2000-12-12 21:00:22 +03:00
|
|
|
#include "bpfilter.h"
|
|
|
|
|
1998-05-14 04:04:57 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mbuf.h>
|
|
|
|
#include <sys/protosw.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/syslog.h>
|
|
|
|
|
|
|
|
#include <machine/cpu.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/netisr.h>
|
|
|
|
#include <net/route.h>
|
|
|
|
#include <net/if_llc.h>
|
|
|
|
#include <net/if_dl.h>
|
|
|
|
#include <net/if_types.h>
|
|
|
|
|
2000-12-12 21:00:22 +03:00
|
|
|
#if NBPFILTER > 0
|
|
|
|
#include <net/bpf.h>
|
|
|
|
#endif
|
|
|
|
|
1998-05-14 04:04:57 +04:00
|
|
|
#include <net/if_hippi.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
2000-10-02 07:52:10 +04:00
|
|
|
#if defined(INET) || defined(INET6)
|
1998-05-14 04:04:57 +04:00
|
|
|
#include <netinet/in_var.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define senderr(e) { error = (e); goto bad;}
|
|
|
|
|
|
|
|
#define SIN(x) ((struct sockaddr_in *)x)
|
|
|
|
#define SDL(x) ((struct sockaddr_dl *)x)
|
|
|
|
|
|
|
|
#ifndef llc_snap
|
|
|
|
#define llc_snap llc_un.type_snap
|
|
|
|
#endif
|
|
|
|
|
1999-05-19 03:52:51 +04:00
|
|
|
static int hippi_output __P((struct ifnet *, struct mbuf *,
|
2005-02-27 01:45:09 +03:00
|
|
|
struct sockaddr *, struct rtentry *));
|
1999-05-19 03:52:51 +04:00
|
|
|
static void hippi_input __P((struct ifnet *, struct mbuf *));
|
1998-05-14 04:04:57 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* HIPPI output routine.
|
|
|
|
* Encapsulate a packet of type family for the local net.
|
|
|
|
* I don't know anything about the mapping of AppleTalk or OSI
|
|
|
|
* protocols to HIPPI, so I don't include any code for them.
|
|
|
|
*/
|
|
|
|
|
1999-05-19 03:52:51 +04:00
|
|
|
static int
|
1998-05-14 04:04:57 +04:00
|
|
|
hippi_output(ifp, m0, dst, rt0)
|
|
|
|
struct ifnet *ifp;
|
|
|
|
struct mbuf *m0;
|
|
|
|
struct sockaddr *dst;
|
|
|
|
struct rtentry *rt0;
|
|
|
|
{
|
|
|
|
u_int16_t htype;
|
|
|
|
u_int32_t ifield = 0;
|
2000-12-14 01:07:50 +03:00
|
|
|
int s, len, error = 0;
|
1998-05-14 04:04:57 +04:00
|
|
|
struct mbuf *m = m0;
|
|
|
|
struct rtentry *rt;
|
|
|
|
struct hippi_header *hh;
|
|
|
|
u_int32_t *cci;
|
|
|
|
u_int32_t d2_len;
|
2000-12-14 01:07:50 +03:00
|
|
|
ALTQ_DECL(struct altq_pktattr pktattr;)
|
1998-05-14 04:04:57 +04:00
|
|
|
|
|
|
|
if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
|
|
|
|
senderr(ENETDOWN);
|
|
|
|
|
|
|
|
/* HIPPI doesn't really do broadcast or multicast right now */
|
|
|
|
if (m->m_flags & (M_BCAST | M_MCAST))
|
|
|
|
senderr(EOPNOTSUPP); /* XXX: some other error? */
|
|
|
|
|
|
|
|
if ((rt = rt0) != NULL) {
|
|
|
|
if ((rt->rt_flags & RTF_UP) == 0) {
|
|
|
|
if ((rt0 = rt = rtalloc1(dst, 1)) != NULL) {
|
|
|
|
rt->rt_refcnt--;
|
|
|
|
if (rt->rt_ifp != ifp)
|
|
|
|
return (*rt->rt_ifp->if_output)
|
|
|
|
(ifp, m0, dst, rt);
|
2005-02-27 01:45:09 +03:00
|
|
|
} else
|
1998-05-14 04:04:57 +04:00
|
|
|
senderr(EHOSTUNREACH);
|
|
|
|
}
|
|
|
|
if ((rt->rt_flags & RTF_GATEWAY) && dst->sa_family != AF_NS) {
|
|
|
|
if (rt->rt_gwroute == 0)
|
|
|
|
goto lookup;
|
|
|
|
if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
|
|
|
|
rtfree(rt); rt = rt0;
|
|
|
|
lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
|
|
|
|
if ((rt = rt->rt_gwroute) == 0)
|
|
|
|
senderr(EHOSTUNREACH);
|
|
|
|
/* the "G" test below also prevents rt == rt0 */
|
|
|
|
if ((rt->rt_flags & RTF_GATEWAY) ||
|
|
|
|
(rt->rt_ifp != ifp)) {
|
|
|
|
rt->rt_refcnt--;
|
|
|
|
rt0->rt_gwroute = 0;
|
|
|
|
senderr(EHOSTUNREACH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rt->rt_flags & RTF_REJECT)
|
|
|
|
if (rt->rt_rmx.rmx_expire == 0 || /* XXX: no ARP */
|
|
|
|
time.tv_sec < rt->rt_rmx.rmx_expire)
|
|
|
|
senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
|
|
|
|
}
|
2000-12-14 01:07:50 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the queueing discipline needs packet classification,
|
|
|
|
* do it before prepending link headers.
|
|
|
|
*/
|
|
|
|
IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
|
|
|
|
|
1998-05-14 04:04:57 +04:00
|
|
|
switch (dst->sa_family) {
|
|
|
|
#ifdef INET
|
|
|
|
case AF_INET:
|
|
|
|
if (rt) {
|
2005-02-27 01:45:09 +03:00
|
|
|
struct sockaddr_dl *sdl =
|
1998-05-14 04:04:57 +04:00
|
|
|
(struct sockaddr_dl *) SDL(rt->rt_gateway);
|
|
|
|
if (sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0)
|
|
|
|
bcopy(LLADDR(sdl), &ifield, sizeof(ifield));
|
|
|
|
}
|
|
|
|
if (!ifield) /* XXX: bogus check, but helps us get going */
|
|
|
|
senderr(EHOSTUNREACH);
|
|
|
|
htype = htons(ETHERTYPE_IP);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2002-06-13 09:09:52 +04:00
|
|
|
#ifdef INET6
|
|
|
|
case AF_INET6:
|
|
|
|
if (rt) {
|
2005-02-27 01:45:09 +03:00
|
|
|
struct sockaddr_dl *sdl =
|
2002-06-13 09:09:52 +04:00
|
|
|
(struct sockaddr_dl *) SDL(rt->rt_gateway);
|
|
|
|
if (sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0)
|
|
|
|
bcopy(LLADDR(sdl), &ifield, sizeof(ifield));
|
|
|
|
}
|
|
|
|
if (!ifield) /* XXX: bogus check, but helps us get going */
|
|
|
|
senderr(EHOSTUNREACH);
|
|
|
|
htype = htons(ETHERTYPE_IPV6);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
1998-05-14 04:04:57 +04:00
|
|
|
default:
|
|
|
|
printf("%s: can't handle af%d\n", ifp->if_xname,
|
|
|
|
dst->sa_family);
|
|
|
|
senderr(EAFNOSUPPORT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (htype != 0) {
|
2000-03-30 13:45:33 +04:00
|
|
|
struct llc *l;
|
1998-05-14 04:04:57 +04:00
|
|
|
M_PREPEND(m, sizeof (struct llc), M_DONTWAIT);
|
|
|
|
if (m == 0)
|
|
|
|
senderr(ENOBUFS);
|
|
|
|
l = mtod(m, struct llc *);
|
|
|
|
l->llc_control = LLC_UI;
|
|
|
|
l->llc_dsap = l->llc_ssap = LLC_SNAP_LSAP;
|
2005-02-27 01:45:09 +03:00
|
|
|
l->llc_snap.org_code[0] = l->llc_snap.org_code[1] =
|
1998-05-14 04:04:57 +04:00
|
|
|
l->llc_snap.org_code[2] = 0;
|
|
|
|
bcopy((caddr_t) &htype, (caddr_t) &l->llc_snap.ether_type,
|
|
|
|
sizeof(u_int16_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
d2_len = m->m_pkthdr.len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add local net header. If no space in first mbuf,
|
|
|
|
* allocate another.
|
|
|
|
*/
|
|
|
|
|
|
|
|
M_PREPEND(m, sizeof (struct hippi_header) + 8, M_DONTWAIT);
|
|
|
|
if (m == 0)
|
|
|
|
senderr(ENOBUFS);
|
|
|
|
cci = mtod(m, u_int32_t *);
|
2001-07-18 20:43:09 +04:00
|
|
|
memset(cci, 0, sizeof(struct hippi_header) + 8);
|
1998-05-14 04:04:57 +04:00
|
|
|
cci[0] = 0;
|
|
|
|
cci[1] = ifield;
|
|
|
|
hh = (struct hippi_header *) &cci[2];
|
|
|
|
hh->hi_fp.fp_ulp = HIPPI_ULP_802;
|
|
|
|
hh->hi_fp.fp_flags = HIPPI_FP_D1_PRESENT;
|
|
|
|
hh->hi_fp.fp_offsets = htons(sizeof(struct hippi_le));
|
|
|
|
hh->hi_fp.fp_d2_len = htonl(d2_len);
|
|
|
|
|
|
|
|
/* Pad out the D2 area to end on a quadword (64-bit) boundry. */
|
2005-02-27 01:45:09 +03:00
|
|
|
|
1998-05-14 04:04:57 +04:00
|
|
|
if (d2_len % 8 != 0) {
|
|
|
|
static u_int32_t buffer[2] = {0, 0};
|
|
|
|
m_copyback(m, m->m_pkthdr.len, 8 - d2_len % 8, (caddr_t) buffer);
|
|
|
|
}
|
|
|
|
|
2000-12-14 01:07:50 +03:00
|
|
|
len = m->m_pkthdr.len;
|
2001-04-14 03:29:55 +04:00
|
|
|
s = splnet();
|
1998-05-14 04:04:57 +04:00
|
|
|
/*
|
|
|
|
* Queue message on interface, and start output if interface
|
|
|
|
* not yet active.
|
|
|
|
*/
|
2000-12-14 01:07:50 +03:00
|
|
|
IFQ_ENQUEUE(&ifp->if_snd, m, &pktattr, error);
|
|
|
|
if (error) {
|
|
|
|
/* mbuf is already free */
|
1998-05-14 04:04:57 +04:00
|
|
|
splx(s);
|
2000-12-14 01:07:50 +03:00
|
|
|
return (error);
|
1998-05-14 04:04:57 +04:00
|
|
|
}
|
2000-12-14 01:07:50 +03:00
|
|
|
ifp->if_obytes += len;
|
1998-05-14 04:04:57 +04:00
|
|
|
if ((ifp->if_flags & IFF_OACTIVE) == 0)
|
|
|
|
(*ifp->if_start)(ifp);
|
|
|
|
splx(s);
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
bad:
|
|
|
|
if (m)
|
|
|
|
m_freem(m);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process a received HIPPI packet;
|
1999-05-19 03:52:51 +04:00
|
|
|
* the packet is in the mbuf chain m with
|
|
|
|
* the HIPPI header.
|
1998-05-14 04:04:57 +04:00
|
|
|
*/
|
|
|
|
|
1999-05-19 03:52:51 +04:00
|
|
|
static void
|
|
|
|
hippi_input(ifp, m)
|
1998-05-14 04:04:57 +04:00
|
|
|
struct ifnet *ifp;
|
|
|
|
struct mbuf *m;
|
|
|
|
{
|
2000-03-30 13:45:33 +04:00
|
|
|
struct ifqueue *inq;
|
|
|
|
struct llc *l;
|
1998-05-14 04:04:57 +04:00
|
|
|
u_int16_t htype;
|
1999-05-19 03:52:51 +04:00
|
|
|
struct hippi_header *hh;
|
1998-05-14 04:04:57 +04:00
|
|
|
int s;
|
|
|
|
|
|
|
|
if ((ifp->if_flags & IFF_UP) == 0) {
|
|
|
|
m_freem(m);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX: need to check flags and drop if bogus! */
|
|
|
|
|
1999-05-19 03:52:51 +04:00
|
|
|
hh = mtod(m, struct hippi_header *);
|
|
|
|
|
|
|
|
ifp->if_ibytes += m->m_pkthdr.len;
|
1998-05-14 04:04:57 +04:00
|
|
|
if (hh->hi_le.le_dest_addr[0] & 1) {
|
2005-02-27 01:45:09 +03:00
|
|
|
if (bcmp((caddr_t)etherbroadcastaddr,
|
1998-05-14 04:04:57 +04:00
|
|
|
(caddr_t)hh->hi_le.le_dest_addr,
|
|
|
|
sizeof(etherbroadcastaddr)) == 0)
|
|
|
|
m->m_flags |= M_BCAST;
|
|
|
|
else
|
|
|
|
m->m_flags |= M_MCAST;
|
|
|
|
}
|
|
|
|
if (m->m_flags & (M_BCAST|M_MCAST))
|
|
|
|
ifp->if_imcasts++;
|
|
|
|
|
1999-05-19 03:52:51 +04:00
|
|
|
/* Skip past the HIPPI header. */
|
|
|
|
m_adj(m, sizeof(struct hippi_header));
|
|
|
|
|
1998-05-14 04:04:57 +04:00
|
|
|
l = mtod(m, struct llc *);
|
|
|
|
if (l->llc_dsap != LLC_SNAP_LSAP) {
|
|
|
|
m_freem(m);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
htype = ntohs(l->llc_snap.ether_type);
|
|
|
|
m_adj(m, 8);
|
|
|
|
switch (htype) {
|
|
|
|
#ifdef INET
|
|
|
|
case ETHERTYPE_IP:
|
|
|
|
schednetisr(NETISR_IP);
|
|
|
|
inq = &ipintrq;
|
|
|
|
break;
|
2000-10-02 07:52:10 +04:00
|
|
|
#endif
|
|
|
|
#ifdef INET6
|
|
|
|
case ETHERTYPE_IPV6:
|
|
|
|
schednetisr(NETISR_IPV6);
|
|
|
|
inq = &ip6intrq;
|
|
|
|
break;
|
1998-05-14 04:04:57 +04:00
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
m_freem(m);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-04-14 03:29:55 +04:00
|
|
|
s = splnet();
|
1998-05-14 04:04:57 +04:00
|
|
|
if (IF_QFULL(inq)) {
|
|
|
|
IF_DROP(inq);
|
|
|
|
m_freem(m);
|
|
|
|
} else
|
|
|
|
IF_ENQUEUE(inq, m);
|
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle packet from HIPPI that has no MAC header
|
|
|
|
*/
|
|
|
|
|
2000-10-02 07:52:10 +04:00
|
|
|
#ifdef INET
|
1998-05-14 04:04:57 +04:00
|
|
|
void
|
|
|
|
hippi_ip_input(ifp, m)
|
|
|
|
struct ifnet *ifp;
|
|
|
|
struct mbuf *m;
|
|
|
|
{
|
2000-03-30 13:45:33 +04:00
|
|
|
struct ifqueue *inq;
|
1998-05-14 04:04:57 +04:00
|
|
|
int s;
|
|
|
|
|
|
|
|
schednetisr(NETISR_IP);
|
|
|
|
inq = &ipintrq;
|
|
|
|
|
2001-04-14 03:29:55 +04:00
|
|
|
s = splnet();
|
1998-05-14 04:04:57 +04:00
|
|
|
if (IF_QFULL(inq)) {
|
|
|
|
IF_DROP(inq);
|
|
|
|
m_freem(m);
|
|
|
|
} else
|
|
|
|
IF_ENQUEUE(inq, m);
|
|
|
|
splx(s);
|
|
|
|
}
|
2000-10-02 07:52:10 +04:00
|
|
|
#endif
|
1998-05-14 04:04:57 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform common duties while attaching to interface list
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
hippi_ifattach(ifp, lla)
|
2000-03-30 13:45:33 +04:00
|
|
|
struct ifnet *ifp;
|
1998-05-14 04:04:57 +04:00
|
|
|
caddr_t lla;
|
|
|
|
{
|
|
|
|
|
|
|
|
ifp->if_type = IFT_HIPPI;
|
|
|
|
ifp->if_addrlen = 6; /* regular 802.3 MAC address */
|
|
|
|
ifp->if_hdrlen = sizeof(struct hippi_header) + 8; /* add CCI */
|
2000-12-18 22:44:33 +03:00
|
|
|
ifp->if_dlt = DLT_HIPPI;
|
1998-05-14 04:04:57 +04:00
|
|
|
ifp->if_mtu = HIPPIMTU;
|
|
|
|
ifp->if_output = hippi_output;
|
1999-05-19 03:52:51 +04:00
|
|
|
ifp->if_input = hippi_input;
|
2000-03-06 23:51:27 +03:00
|
|
|
ifp->if_baudrate = IF_Mbps(800); /* XXX double-check */
|
2001-01-17 03:30:49 +03:00
|
|
|
|
|
|
|
if_alloc_sadl(ifp);
|
|
|
|
memcpy(LLADDR(ifp->if_sadl), lla, ifp->if_addrlen);
|
2000-12-12 21:00:22 +03:00
|
|
|
|
|
|
|
#if NBPFILTER > 0
|
|
|
|
bpfattach(ifp, DLT_HIPPI, sizeof(struct hippi_header));
|
|
|
|
#endif
|
1998-05-14 04:04:57 +04:00
|
|
|
}
|