NetBSD/sys/net/if_hippisubr.c

348 lines
8.6 KiB
C
Raw Normal View History

/* $NetBSD: if_hippisubr.c,v 1.37 2009/04/18 14:58:05 tsutsui Exp $ */
/*
* 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.
* 3. 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.
*/
2001-11-13 02:49:33 +03:00
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_hippisubr.c,v 1.37 2009/04/18 14:58:05 tsutsui Exp $");
2001-11-13 02:49:33 +03:00
1998-07-05 04:51:04 +04:00
#include "opt_inet.h"
#include "bpfilter.h"
#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 <sys/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>
#if NBPFILTER > 0
#include <net/bpf.h>
#endif
#include <net/if_hippi.h>
#include <netinet/in.h>
#if defined(INET) || defined(INET6)
#include <netinet/in_var.h>
#endif
#define senderr(e) { error = (e); goto bad;}
#ifndef llc_snap
#define llc_snap llc_un.type_snap
#endif
static int hippi_output(struct ifnet *, struct mbuf *,
const struct sockaddr *, struct rtentry *);
static void hippi_input(struct ifnet *, struct mbuf *);
/*
* 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.
*/
static int
hippi_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
struct rtentry *rt0)
{
uint16_t htype;
uint32_t ifield = 0;
int error = 0;
struct mbuf *m = m0;
struct rtentry *rt;
struct hippi_header *hh;
uint32_t *cci;
uint32_t d2_len;
2000-12-14 01:07:50 +03:00
ALTQ_DECL(struct altq_pktattr pktattr;)
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
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_second < 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);
switch (dst->sa_family) {
#ifdef INET
case AF_INET:
if (rt) {
2007-02-20 11:32:14 +03:00
const struct sockaddr_dl *sdl =
satocsdl(rt->rt_gateway);
if (sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0)
memcpy(&ifield, CLLADDR(sdl), 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) {
2007-02-20 11:32:14 +03:00
const struct sockaddr_dl *sdl =
satocsdl(rt->rt_gateway);
2002-06-13 09:09:52 +04:00
if (sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0)
memcpy(&ifield, CLLADDR(sdl), sizeof(ifield));
2002-06-13 09:09:52 +04:00
}
if (!ifield) /* XXX: bogus check, but helps us get going */
senderr(EHOSTUNREACH);
htype = htons(ETHERTYPE_IPV6);
break;
#endif
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;
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] =
l->llc_snap.org_code[2] = 0;
memcpy((void *) &l->llc_snap.ether_type, (void *) &htype,
sizeof(uint16_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, uint32_t *);
2001-07-18 20:43:09 +04:00
memset(cci, 0, sizeof(struct hippi_header) + 8);
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
if (d2_len % 8 != 0) {
static uint32_t buffer[2] = {0, 0};
m_copyback(m, m->m_pkthdr.len, 8 - d2_len % 8, (void *) buffer);
}
return ifq_enqueue(ifp, m ALTQ_COMMA ALTQ_DECL(&pktattr));
bad:
if (m)
m_freem(m);
return (error);
}
/*
* Process a received HIPPI packet;
* the packet is in the mbuf chain m with
* the HIPPI header.
*/
static void
hippi_input(struct ifnet *ifp, struct mbuf *m)
{
2000-03-30 13:45:33 +04:00
struct ifqueue *inq;
struct llc *l;
uint16_t htype;
struct hippi_header *hh;
int s;
if ((ifp->if_flags & IFF_UP) == 0) {
m_freem(m);
return;
}
/* XXX: need to check flags and drop if bogus! */
hh = mtod(m, struct hippi_header *);
ifp->if_ibytes += m->m_pkthdr.len;
if (hh->hi_le.le_dest_addr[0] & 1) {
if (memcmp(etherbroadcastaddr, 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++;
/* Skip past the HIPPI header. */
m_adj(m, sizeof(struct hippi_header));
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;
#endif
#ifdef INET6
case ETHERTYPE_IPV6:
schednetisr(NETISR_IPV6);
inq = &ip6intrq;
break;
#endif
default:
m_freem(m);
return;
}
s = splnet();
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
*/
#ifdef INET
void
hippi_ip_input(struct ifnet *ifp, struct mbuf *m)
{
2000-03-30 13:45:33 +04:00
struct ifqueue *inq;
int s;
schednetisr(NETISR_IP);
inq = &ipintrq;
s = splnet();
if (IF_QFULL(inq)) {
IF_DROP(inq);
m_freem(m);
} else
IF_ENQUEUE(inq, m);
splx(s);
}
#endif
/*
* Perform common duties while attaching to interface list
*/
void
hippi_ifattach(struct ifnet *ifp, void *lla)
{
ifp->if_type = IFT_HIPPI;
ifp->if_hdrlen = sizeof(struct hippi_header) + 8; /* add CCI */
2000-12-18 22:44:33 +03:00
ifp->if_dlt = DLT_HIPPI;
ifp->if_mtu = HIPPIMTU;
ifp->if_output = hippi_output;
ifp->if_input = hippi_input;
ifp->if_baudrate = IF_Mbps(800); /* XXX double-check */
*** Summary *** When a link-layer address changes (e.g., ifconfig ex0 link 02:de:ad:be:ef:02 active), send a gratuitous ARP and/or a Neighbor Advertisement to update the network-/link-layer address bindings on our LAN peers. Refuse a change of ethernet address to the address 00:00:00:00:00:00 or to any multicast/broadcast address. (Thanks matt@.) Reorder ifnet ioctl operations so that driver ioctls may inherit the functions of their "class"---ether_ioctl(), fddi_ioctl(), et cetera---and the class ioctls may inherit from the generic ioctl, ifioctl_common(), but both driver- and class-ioctls may override the generic behavior. Make network drivers share more code. Distinguish a "factory" link-layer address from others for the purposes of both protecting that address from deletion and computing EUI64. Return consistent, appropriate error codes from network drivers. Improve readability. KNF. *** Details *** In if_attach(), always initialize the interface ioctl routine, ifnet->if_ioctl, if the driver has not already initialized it. Delete if_ioctl == NULL tests everywhere else, because it cannot happen. In the ioctl routines of network interfaces, inherit common ioctl behaviors by calling either ifioctl_common() or whichever ioctl routine is appropriate for the class of interface---e.g., ether_ioctl() for ethernets. Stop (ab)using SIOCSIFADDR and start to use SIOCINITIFADDR. In the user->kernel interface, SIOCSIFADDR's argument was an ifreq, but on the protocol->ifnet interface, SIOCSIFADDR's argument was an ifaddr. That was confusing, and it would work against me as I make it possible for a network interface to overload most ioctls. On the protocol->ifnet interface, replace SIOCSIFADDR with SIOCINITIFADDR. In ifioctl(), return EPERM if userland tries to invoke SIOCINITIFADDR. In ifioctl(), give the interface the first shot at handling most interface ioctls, and give the protocol the second shot, instead of the other way around. Finally, let compatibility code (COMPAT_OSOCK) take a shot. Pull device initialization out of switch statements under SIOCINITIFADDR. For example, pull ..._init() out of any switch statement that looks like this: switch (...->sa_family) { case ...: ..._init(); ... break; ... default: ..._init(); ... break; } Rewrite many if-else clauses that handle all permutations of IFF_UP and IFF_RUNNING to use a switch statement, switch (x & (IFF_UP|IFF_RUNNING)) { case 0: ... break; case IFF_RUNNING: ... break; case IFF_UP: ... break; case IFF_UP|IFF_RUNNING: ... break; } unifdef lots of code containing #ifdef FreeBSD, #ifdef NetBSD, and #ifdef SIOCSIFMTU, especially in fwip(4) and in ndis(4). In ipw(4), remove an if_set_sadl() call that is out of place. In nfe(4), reuse the jumbo MTU logic in ether_ioctl(). Let ethernets register a callback for setting h/w state such as promiscuous mode and the multicast filter in accord with a change in the if_flags: ether_set_ifflags_cb() registers a callback that returns ENETRESET if the caller should reset the ethernet by calling if_init(), 0 on success, != 0 on failure. Pull common code from ex(4), gem(4), nfe(4), sip(4), tlp(4), vge(4) into ether_ioctl(), and register if_flags callbacks for those drivers. Return ENOTTY instead of EINVAL for inappropriate ioctls. In zyd(4), use ENXIO instead of ENOTTY to indicate that the device is not any longer attached. Add to if_set_sadl() a boolean 'factory' argument that indicates whether a link-layer address was assigned by the factory or some other source. In a comment, recommend using the factory address for generating an EUI64, and update in6_get_hw_ifid() to prefer a factory address to any other link-layer address. Add a routing message, RTM_LLINFO_UPD, that tells protocols to update the binding of network-layer addresses to link-layer addresses. Implement this message in IPv4 and IPv6 by sending a gratuitous ARP or a neighbor advertisement, respectively. Generate RTM_LLINFO_UPD messages on a change of an interface's link-layer address. In ether_ioctl(), do not let SIOCALIFADDR set a link-layer address that is broadcast/multicast or equal to 00:00:00:00:00:00. Make ether_ioctl() call ifioctl_common() to handle ioctls that it does not understand. In gif(4), initialize if_softc and use it, instead of assuming that the gif_softc and ifp overlap. Let ifioctl_common() handle SIOCGIFADDR. Sprinkle rtcache_invariants(), which checks on DIAGNOSTIC kernels that certain invariants on a struct route are satisfied. In agr(4), rewrite agr_ioctl_filter() to be a bit more explicit about the ioctls that we do not allow on an agr(4) member interface. bzero -> memset. Delete unnecessary casts to void *. Use sockaddr_in_init() and sockaddr_in6_init(). Compare pointers with NULL instead of "testing truth". Replace some instances of (type *)0 with NULL. Change some K&R prototypes to ANSI C, and join lines.
2008-11-07 03:20:01 +03:00
if_set_sadl(ifp, lla, 6, true);
#if NBPFILTER > 0
bpfattach(ifp, DLT_HIPPI, sizeof(struct hippi_header));
#endif
}