NetBSD/sys/net/if_ecosubr.c

881 lines
22 KiB
C
Raw Normal View History

/* $NetBSD: if_ecosubr.c,v 1.33 2009/11/20 02:14:56 christos Exp $ */
/*-
* Copyright (c) 2001 Ben Harris
* 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. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
2005-02-27 01:45:09 +03:00
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
*/
/*
* 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.
*
* @(#)if_ethersubr.c 8.2 (Berkeley) 4/4/96
*/
2001-11-13 02:49:33 +03:00
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_ecosubr.c,v 1.33 2009/11/20 02:14:56 christos Exp $");
2001-11-13 02:49:33 +03:00
#include "bpfilter.h"
#include "opt_inet.h"
#include "opt_pfil_hooks.h"
#include <sys/param.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/syslog.h>
#include <sys/systm.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_eco.h>
#include <net/if_types.h>
#include <net/netisr.h>
#include <net/route.h>
#if NBPFILTER > 0
#include <net/bpf.h>
#endif
#ifdef INET
#include <net/ethertypes.h>
#include <net/if_arp.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#endif
struct eco_retryparms {
int erp_delay;
int erp_count;
};
/* Default broadcast address */
static const uint8_t eco_broadcastaddr[] = { 0xff, 0xff };
2009-01-07 23:56:40 +03:00
static int eco_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
struct rtentry *);
static void eco_input(struct ifnet *, struct mbuf *);
static void eco_start(struct ifnet *);
static int eco_ioctl(struct ifnet *, u_long, void *);
static int eco_interestingp(struct ifnet *ifp, struct mbuf *m);
static struct mbuf *eco_immediate(struct ifnet *ifp, struct mbuf *m);
static struct mbuf *eco_ack(struct ifnet *ifp, struct mbuf *m);
static void eco_defer(struct ifnet *, struct mbuf *, int);
static void eco_retry_free(struct eco_retry *er);
static void eco_retry(void *);
void
eco_ifattach(struct ifnet *ifp, const uint8_t *lla)
{
struct ecocom *ec = (void *)ifp;
ifp->if_type = IFT_ECONET;
ifp->if_addrlen = ECO_ADDR_LEN;
ifp->if_hdrlen = ECO_HDR_LEN;
ifp->if_dlt = DLT_ECONET;
ifp->if_mtu = ECO_MTU;
ifp->if_output = eco_output;
ifp->if_input = eco_input;
ifp->if_start = eco_start;
ifp->if_ioctl = eco_ioctl;
/* ifp->if_baudrate...; */
2009-01-07 23:56:40 +03:00
if_set_sadl(ifp, lla, ECO_ADDR_LEN, FALSE);
2006-02-12 13:32:46 +03:00
ifp->if_broadcastaddr = eco_broadcastaddr;
LIST_INIT(&ec->ec_retries);
#if NBPFILTER > 0
bpfattach(ifp, ifp->if_dlt, ECO_HDR_LEN);
#endif
}
#define senderr(e) do { \
error = (e); \
goto bad; \
} while (/*CONSTCOND*/0)
int
*** 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
eco_init(struct ifnet *ifp)
{
struct ecocom *ec = (struct ecocom *)ifp;
if ((ifp->if_flags & IFF_RUNNING) == 0)
ec->ec_state = ECO_UNKNOWN;
return 0;
}
void
eco_stop(struct ifnet *ifp, int disable)
{
struct ecocom *ec = (struct ecocom *)ifp;
while (!LIST_EMPTY(&ec->ec_retries))
eco_retry_free(LIST_FIRST(&ec->ec_retries));
}
static int
2009-01-07 23:56:40 +03:00
eco_output(struct ifnet *ifp, struct mbuf *m0, const struct sockaddr *dst,
struct rtentry *rt0)
{
struct eco_header ehdr, *eh;
int error;
struct mbuf *m = m0, *mcopy = NULL;
struct rtentry *rt;
int hdrcmplt;
2006-02-12 13:32:46 +03:00
int retry_delay, retry_count;
struct m_tag *mtag;
struct eco_retryparms *erp;
#ifdef INET
struct mbuf *m1;
struct arphdr *ah;
2009-01-07 23:56:40 +03:00
void *tha;
struct eco_arp *ecah;
#endif
ALTQ_DECL(struct altq_pktattr pktattr;)
if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
senderr(ENETDOWN);
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 ||
time_second < rt->rt_rmx.rmx_expire)
senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
}
/*
* If the queueing discipline needs packet classification,
* do it before prepending link headers.
*/
IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family, &pktattr);
hdrcmplt = 0;
2006-02-12 13:32:46 +03:00
retry_delay = hz / 16;
retry_count = 16;
switch (dst->sa_family) {
#ifdef INET
case AF_INET:
if (m->m_flags & M_BCAST)
memcpy(ehdr.eco_dhost, eco_broadcastaddr,
ECO_ADDR_LEN);
else if (!arpresolve(ifp, rt, m, dst, ehdr.eco_dhost))
return (0); /* if not yet resolved */
/* If broadcasting on a simplex interface, loopback a copy */
if ((m->m_flags & M_BCAST) && (ifp->if_flags & IFF_SIMPLEX))
mcopy = m_copy(m, 0, (int)M_COPYALL);
ehdr.eco_port = ECO_PORT_IP;
ehdr.eco_control = ECO_CTL_IP;
break;
case AF_ARP:
ah = mtod(m, struct arphdr *);
if (ntohs(ah->ar_pro) != ETHERTYPE_IP)
return EAFNOSUPPORT;
ehdr.eco_port = ECO_PORT_IP;
switch (ntohs(ah->ar_op)) {
case ARPOP_REQUEST:
ehdr.eco_control = ECO_CTL_ARP_REQUEST;
break;
case ARPOP_REPLY:
ehdr.eco_control = ECO_CTL_ARP_REPLY;
break;
default:
return EOPNOTSUPP;
}
if (m->m_flags & M_BCAST)
memcpy(ehdr.eco_dhost, eco_broadcastaddr,
ECO_ADDR_LEN);
2009-01-07 23:56:40 +03:00
else {
tha = ar_tha(ah);
if (tha == NULL)
return 0;
2009-01-07 23:56:40 +03:00
memcpy(ehdr.eco_dhost, tha, ECO_ADDR_LEN);
}
MGETHDR(m1, M_DONTWAIT, MT_DATA);
if (m1 == NULL)
senderr(ENOBUFS);
M_MOVE_PKTHDR(m1, m);
m1->m_len = sizeof(*ecah);
m1->m_pkthdr.len = m1->m_len;
MH_ALIGN(m1, m1->m_len);
ecah = mtod(m1, struct eco_arp *);
memset(ecah, 0, m1->m_len);
memcpy(ecah->ecar_spa, ar_spa(ah), ah->ar_pln);
memcpy(ecah->ecar_tpa, ar_tpa(ah), ah->ar_pln);
m_freem(m);
m = m1;
break;
#endif
case pseudo_AF_HDRCMPLT:
hdrcmplt = 1;
/* FALLTHROUGH */
case AF_UNSPEC:
2009-01-07 23:56:40 +03:00
ehdr = *(struct eco_header const *)dst->sa_data;
break;
default:
log(LOG_ERR, "%s: can't handle af%d\n", ifp->if_xname,
dst->sa_family);
senderr(EAFNOSUPPORT);
}
if (mcopy)
(void) looutput(ifp, mcopy, dst, rt);
/*
* Add local net header. If no space in first mbuf,
* allocate another.
*/
M_PREPEND(m, sizeof (struct eco_header), M_DONTWAIT);
if (m == 0)
senderr(ENOBUFS);
eh = mtod(m, struct eco_header *);
*eh = ehdr;
if (!hdrcmplt)
memcpy(eh->eco_shost, CLLADDR(ifp->if_sadl),
ECO_ADDR_LEN);
if ((m->m_flags & M_BCAST) == 0) {
/* Attach retry info to packet. */
mtag = m_tag_get(PACKET_TAG_ECO_RETRYPARMS,
sizeof(struct eco_retryparms), M_NOWAIT);
if (mtag == NULL)
senderr(ENOBUFS);
erp = (struct eco_retryparms *)(mtag + 1);
2006-02-12 13:32:46 +03:00
erp->erp_delay = retry_delay;
erp->erp_count = retry_count;
}
#ifdef PFIL_HOOKS
if ((error = pfil_run_hooks(&ifp->if_pfil, &m, ifp, PFIL_OUT)) != 0)
return (error);
if (m == NULL)
return (0);
#endif
2005-02-27 01:45:09 +03:00
2006-02-12 13:32:46 +03:00
return ifq_enqueue(ifp, m ALTQ_COMMA ALTQ_DECL(&pktattr));
bad:
if (m)
m_freem(m);
return error;
}
/*
* Given a scout, decide if we want the rest of the packet.
*/
static int
eco_interestingp(struct ifnet *ifp, struct mbuf *m)
{
struct eco_header *eh;
eh = mtod(m, struct eco_header *);
switch (eh->eco_port) {
#ifdef INET
case ECO_PORT_IP:
return 1;
#endif
}
return 0;
}
static void
eco_input(struct ifnet *ifp, struct mbuf *m)
{
struct ifqueue *inq;
struct eco_header ehdr, *eh;
int s, i;
#ifdef INET
struct arphdr *ah;
struct eco_arp *ecah;
struct mbuf *m1;
2009-01-07 23:56:40 +03:00
void *tha;
#endif
#ifdef PFIL_HOOKS
if (pfil_run_hooks(&ifp->if_pfil, &m, ifp, PFIL_IN) != 0)
return;
if (m == NULL)
return;
#endif
/* Copy the mbuf header and trim it off. */
/* XXX use m_split? */
eh = &ehdr;
m_copydata(m, 0, ECO_HDR_LEN, (void *)eh);
m_adj(m, ECO_HDR_LEN);
switch (eh->eco_port) {
#ifdef INET
case ECO_PORT_IP:
switch (eh->eco_control) {
case ECO_CTL_IP:
schednetisr(NETISR_IP);
inq = &ipintrq;
break;
case ECO_CTL_ARP_REQUEST:
case ECO_CTL_ARP_REPLY:
/*
* ARP over Econet is strange, because Econet only
* supports 8 bytes of data in a broadcast packet.
* To cope with this, only the source and destination
* IP addresses are actually contained in the packet
* and we have to infer the rest and build a fake ARP
* packet to pass upwards.
*/
if (m->m_pkthdr.len != sizeof(struct eco_arp))
goto drop;
if (m->m_len < sizeof(struct eco_arp)) {
m = m_pullup(m, sizeof(struct eco_arp));
if (m == NULL) goto drop;
}
ecah = mtod(m, struct eco_arp *);
/* This code derived from arprequest() */
MGETHDR(m1, M_DONTWAIT, MT_DATA);
if (m1 == NULL)
goto drop;
M_MOVE_PKTHDR(m1, m);
m1->m_len = sizeof(*ah) + 2*sizeof(struct in_addr) +
2*ifp->if_data.ifi_addrlen;
m1->m_pkthdr.len = m1->m_len;
MH_ALIGN(m1, m1->m_len);
ah = mtod(m1, struct arphdr *);
2009-03-18 19:00:08 +03:00
memset((void *)ah, 0, m1->m_len);
ah->ar_pro = htons(ETHERTYPE_IP);
ah->ar_hln = ifp->if_data.ifi_addrlen;
ah->ar_pln = sizeof(struct in_addr);
if (eh->eco_control == ECO_CTL_ARP_REQUEST)
ah->ar_op = htons(ARPOP_REQUEST);
else
ah->ar_op = htons(ARPOP_REPLY);
2009-01-07 23:56:40 +03:00
tha = ar_tha(ah);
KASSERT(tha != NULL);
memcpy(ar_sha(ah), eh->eco_shost, ah->ar_hln);
2009-01-07 23:56:40 +03:00
memcpy(tha, eh->eco_dhost, ah->ar_hln);
memcpy(ar_spa(ah), ecah->ecar_spa, ah->ar_pln);
memcpy(ar_tpa(ah), ecah->ecar_tpa, ah->ar_pln);
m_freem(m);
m = m1;
schednetisr(NETISR_ARP);
inq = &arpintrq;
break;
case ECO_CTL_IPBCAST_REQUEST:
{
struct sockaddr_storage dst_store;
struct sockaddr *dst = (struct sockaddr *)&dst_store;
/* Queue? */
memcpy(eh->eco_dhost, eh->eco_shost, ECO_ADDR_LEN);
eh->eco_control = ECO_CTL_IPBCAST_REPLY;
/* dst->sa_len??? */
dst->sa_family = AF_UNSPEC;
memcpy(dst->sa_data, eh, ECO_HDR_LEN);
ifp->if_output(ifp, m, dst, NULL);
return;
}
default:
printf("%s: unknown IP stn %s ctl 0x%02x len %d:",
ifp->if_xname, eco_sprintf(eh->eco_shost),
eh->eco_control, m->m_pkthdr.len);
if (m->m_len == 0) {
m = m_pullup(m, 1);
if (m == 0) {
printf("\n");
goto drop;
}
}
for (i = 0; i < m->m_len; i++)
printf(" %02x", mtod(m, uint8_t *)[i]);
printf("\n");
goto drop;
}
break;
#endif
default:
printf("%s: unknown port stn %s port 0x%02x ctl 0x%02x\n",
ifp->if_xname, eco_sprintf(eh->eco_shost),
eh->eco_port, eh->eco_control);
drop:
m_freem(m);
return;
}
s = splnet();
if (IF_QFULL(inq)) {
IF_DROP(inq);
m_freem(m);
} else
IF_ENQUEUE(inq, m);
splx(s);
}
static void
eco_start(struct ifnet *ifp)
{
struct ecocom *ec = (void *)ifp;
struct mbuf *m;
struct eco_header *eh;
if (ec->ec_state != ECO_IDLE) return;
IFQ_DEQUEUE(&ifp->if_snd, m);
if (m == NULL) return;
if (ec->ec_claimwire(ifp) == 0) {
eh = mtod(m, struct eco_header *);
if (eh->eco_port == ECO_PORT_IMMEDIATE) {
ec->ec_txframe(ifp, m);
ec->ec_state = ECO_IMMED_SENT;
} else if (eh->eco_dhost[0] == 255) {
ec->ec_txframe(ifp, m);
ec->ec_state = ECO_DONE;
} else {
ec->ec_packet = m;
m = m_copym(m, 0, ECO_HDR_LEN, M_DONTWAIT);
if (m == NULL) {
m_freem(ec->ec_packet);
ec->ec_packet = NULL;
return;
}
ec->ec_txframe(ifp, m);
ec->ec_state = ECO_SCOUT_SENT;
}
ifp->if_flags |= IFF_OACTIVE;
} else {
log(LOG_ERR, "%s: line jammed\n", ifp->if_xname);
m_freem(m);
}
}
static int
eco_ioctl(struct ifnet *ifp, u_long cmd, void *data)
{
struct ifaddr *ifa = (struct ifaddr *)data;
int error;
switch (cmd) {
*** 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
case SIOCINITIFADDR:
ifp->if_flags |= IFF_UP;
*** 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 ((ifp->if_flags & IFF_RUNNING) == 0 &&
(error = (*ifp->if_init)(ifp)) != 0)
return error;
switch (ifa->ifa_addr->sa_family) {
#ifdef INET
case AF_INET:
arp_ifinit(ifp, ifa);
break;
#endif
default:
break;
}
return 0;
case SIOCSIFMTU:
2009-01-07 23:56:40 +03:00
if ((error = ifioctl_common(ifp, cmd, data)) != ENETRESET)
return error;
else if (ifp->if_flags & IFF_UP)
return (*ifp->if_init)(ifp);
else
return 0;
break;
case SIOCSIFFLAGS:
*** 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 ((error = ifioctl_common(ifp, cmd, data)) != 0)
return error;
switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
case IFF_RUNNING:
/*
* If interface is marked down and it is running,
* then stop and disable it.
*/
(*ifp->if_stop)(ifp, 1);
return 0;
case IFF_UP:
/*
* If interface is marked up and it is stopped, then
* start it.
*/
return (*ifp->if_init)(ifp);
case IFF_UP|IFF_RUNNING:
/*
* Reset the interface to pick up changes in any other
* flags that affect the hardware state.
*/
return (*ifp->if_init)(ifp);
case 0:
return 0;
}
break;
default:
*** 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
return ifioctl_common(ifp, cmd, data);
}
return 0;
}
/*
* Handle a raw Econet frame off the interface. The interface may be
* flag-filling for a response.
*
* May be called from IPL_NET or IPL_SOFTNET.
*/
struct mbuf *
eco_inputframe(struct ifnet *ifp, struct mbuf *m)
{
struct ecocom *ec = (void *)ifp;
struct eco_header *eh, *eh0;
struct mbuf *m0;
struct mbuf *reply;
int len;
eh = mtod(m, struct eco_header *);
switch (ec->ec_state) {
case ECO_IDLE: /* Start of a packet (bcast, immed, scout) */
if (m->m_pkthdr.len < ECO_HDR_LEN) {
log(LOG_NOTICE, "%s: undersize scout\n",
ifp->if_xname);
goto drop;
}
if (memcmp(eh->eco_dhost, eco_broadcastaddr,
ECO_ADDR_LEN) == 0) {
/* Broadcast */
eco_input(ifp, m);
} else if (memcmp(eh->eco_dhost, CLLADDR(ifp->if_sadl),
ECO_ADDR_LEN) == 0) {
/* Unicast for us */
if (eh->eco_port == ECO_PORT_IMMEDIATE)
return eco_immediate(ifp, m);
else {
if (eco_interestingp(ifp, m)) {
reply = eco_ack(ifp, m);
if (reply == NULL) {
m_freem(m);
return NULL;
}
ec->ec_state = ECO_SCOUT_RCVD;
ec->ec_scout = m;
return reply;
} else {
m_freem(m);
return NULL;
}
}
} else
/* Not for us. Throw it away. */
m_freem(m);
break;
case ECO_SCOUT_RCVD: /* Packet data */
KASSERT(ec->ec_scout != NULL);
m0 = ec->ec_scout;
eh0 = mtod(m0, struct eco_header *);
if (m->m_pkthdr.len < ECO_SHDR_LEN ||
memcmp(eh->eco_shost, eh0->eco_shost, ECO_ADDR_LEN) != 0 ||
memcmp(eh->eco_dhost, eh0->eco_dhost, ECO_ADDR_LEN) != 0) {
log(LOG_NOTICE, "%s: garbled data packet header\n",
ifp->if_xname);
goto drop;
}
reply = eco_ack(ifp, m);
/*
* Chop off the small header from this frame, and put
* the scout (which holds the control byte and port)
* in its place.
*/
ec->ec_scout = NULL;
m_adj(m, ECO_SHDR_LEN);
len = m0->m_pkthdr.len + m->m_pkthdr.len;
m_cat(m0, m);
m0->m_pkthdr.len = len;
ec->ec_state = ECO_DONE;
eco_input(ifp, m0);
return reply;
case ECO_SCOUT_SENT: /* Scout ack */
KASSERT(ec->ec_packet != NULL);
m0 = ec->ec_packet;
eh0 = mtod(m0, struct eco_header *);
if (m->m_pkthdr.len != ECO_SHDR_LEN ||
memcmp(eh->eco_shost, eh0->eco_dhost, ECO_ADDR_LEN) != 0 ||
memcmp(eh->eco_dhost, eh0->eco_shost, ECO_ADDR_LEN) != 0) {
log(LOG_NOTICE, "%s: garbled scout ack\n",
ifp->if_xname);
goto drop;
}
m_freem(m);
/* Chop out the control and port bytes. */
m0 = m_copym(ec->ec_packet, 0, ECO_SHDR_LEN, M_DONTWAIT);
if (m0 == NULL) {
m_freem(ec->ec_packet);
return NULL;
}
m = ec->ec_packet;
ec->ec_packet = m_copypacket(m, M_DONTWAIT);
if (ec->ec_packet == NULL) {
m_freem(m0);
m_freem(m);
return NULL;
}
m_adj(m, ECO_HDR_LEN);
len = m0->m_pkthdr.len + m->m_pkthdr.len;
m_cat(m0, m); /* Doesn't update packet header */
m0->m_pkthdr.len = len;
ec->ec_state = ECO_DATA_SENT;
return m0;
case ECO_DATA_SENT: /* Data ack */
KASSERT(ec->ec_packet != NULL);
m0 = ec->ec_packet;
eh0 = mtod(m0, struct eco_header *);
if (m->m_pkthdr.len != ECO_SHDR_LEN ||
memcmp(eh->eco_shost, eh0->eco_dhost, ECO_ADDR_LEN) != 0 ||
memcmp(eh->eco_dhost, eh0->eco_shost, ECO_ADDR_LEN) != 0) {
log(LOG_NOTICE, "%s: garbled data ack\n",
ifp->if_xname);
goto drop;
}
m_freem(m);
m_freem(ec->ec_packet);
ec->ec_packet = NULL;
ec->ec_state = ECO_DONE;
return NULL;
default:
drop:
m_freem(m);
break;
}
return NULL;
}
/*
* Handle an immediate operation, and return the reply, or NULL not to reply.
* Frees the incoming mbuf.
*/
static struct mbuf *
eco_immediate(struct ifnet *ifp, struct mbuf *m)
{
struct eco_header *eh, *reh;
struct mbuf *n;
static const uint8_t machinepeek_data[] = { 42, 0, 0, 1 };
eh = mtod(m, struct eco_header *);
switch (eh->eco_control) {
case ECO_CTL_MACHINEPEEK:
MGETHDR(n, M_DONTWAIT, MT_DATA);
if (n == NULL)
goto bad;
n->m_len = n->m_pkthdr.len = ECO_SHDR_LEN + 4;
reh = mtod(n, struct eco_header *);
memcpy(reh->eco_dhost, eh->eco_shost,
ECO_ADDR_LEN);
memcpy(reh->eco_shost, CLLADDR(ifp->if_sadl),
ECO_ADDR_LEN);
2009-01-07 23:56:40 +03:00
memcpy(mtod(n, char *) + ECO_SHDR_LEN, machinepeek_data,
sizeof(machinepeek_data));
m_freem(m);
return n;
default:
bad:
m_freem(m);
return NULL;
}
}
/*
* Generate (and return) an acknowledgement for a frame. Doesn't free the
* original frame, since it's probably needed elsewhere.
*/
static struct mbuf *
eco_ack(struct ifnet *ifp, struct mbuf *m)
{
struct eco_header *eh, *reh;
struct mbuf *n;
eh = mtod(m, struct eco_header *);
MGETHDR(n, M_DONTWAIT, MT_DATA);
if (n == NULL)
return NULL;
n->m_len = n->m_pkthdr.len = ECO_SHDR_LEN;
reh = mtod(n, struct eco_header *);
memcpy(reh->eco_dhost, eh->eco_shost, ECO_ADDR_LEN);
memcpy(reh->eco_shost, CLLADDR(ifp->if_sadl), ECO_ADDR_LEN);
return n;
}
void
eco_inputidle(struct ifnet *ifp)
{
struct ecocom *ec = (void *)ifp;
struct mbuf *m;
struct m_tag *mtag;
struct eco_retryparms *erp;
switch (ec->ec_state) {
case ECO_SCOUT_SENT:
case ECO_DATA_SENT:
case ECO_IMMED_SENT:
/* Outgoing packet failed. Check if we should retry. */
m = ec->ec_packet;
ec->ec_packet = NULL;
mtag = m_tag_find(m, PACKET_TAG_ECO_RETRYPARMS, NULL);
if (mtag == NULL)
m_freem(m);
else {
erp = (struct eco_retryparms *)(mtag + 1);
if (--erp->erp_count > 0)
eco_defer(ifp, m, erp->erp_delay);
else
printf("%s: pkt failed\n", ifp->if_xname);
}
break;
case ECO_SCOUT_RCVD:
m_freem(ec->ec_scout);
ec->ec_scout = NULL;
break;
default:
break;
}
ec->ec_state = ECO_IDLE;
ifp->if_start(ifp);
}
/*
* Convert Econet address to printable (loggable) representation.
*/
char *
eco_sprintf(const uint8_t *ea)
{
static char buf[8];
if (ea[1] == 0)
2004-04-21 22:40:37 +04:00
snprintf(buf, sizeof(buf), "%d", ea[0]);
else
2004-04-21 22:40:37 +04:00
snprintf(buf, sizeof(buf), "%d.%d", ea[1], ea[0]);
return buf;
}
2005-02-27 01:45:09 +03:00
/*
* Econet retry handling.
*/
static void
2006-02-12 13:32:46 +03:00
eco_defer(struct ifnet *ifp, struct mbuf *m, int retry_delay)
{
struct ecocom *ec = (struct ecocom *)ifp;
struct eco_retry *er;
int s;
2008-12-17 23:51:31 +03:00
er = malloc(sizeof(*er), M_TEMP, M_NOWAIT);
if (er == NULL) {
m_freem(m);
return;
}
callout_init(&er->er_callout, 0);
er->er_packet = m;
er->er_ifp = ifp;
s = splnet();
LIST_INSERT_HEAD(&ec->ec_retries, er, er_link);
splx(s);
2006-02-12 13:32:46 +03:00
callout_reset(&er->er_callout, retry_delay, eco_retry, er);
}
static void
eco_retry_free(struct eco_retry *er)
{
int s;
callout_stop(&er->er_callout);
m_freem(er->er_packet);
s = splnet();
LIST_REMOVE(er, er_link);
splx(s);
callout_destroy(&er->er_callout);
2008-12-17 23:51:31 +03:00
free(er, M_TEMP);
}
static void
eco_retry(void *arg)
{
struct eco_retry *er = arg;
struct mbuf *m;
struct ifnet *ifp;
ifp = er->er_ifp;
m = er->er_packet;
LIST_REMOVE(er, er_link);
(void)ifq_enqueue(ifp, m ALTQ_COMMA ALTQ_DECL(NULL));
2008-12-17 23:51:31 +03:00
free(er, M_TEMP);
}