Home | History | Annotate | Line # | Download | only in net
if_vether.c revision 1.1.26.1
      1  1.1.26.1  perseant /*	$NetBSD: if_vether.c,v 1.1.26.1 2025/08/02 05:57:47 perseant Exp $	*/
      2       1.1       roy /* $OpenBSD: if_vether.c,v 1.27 2016/04/13 11:41:15 mpi Exp $ */
      3       1.1       roy 
      4       1.1       roy /*
      5       1.1       roy  * Copyright (c) 2009 Theo de Raadt
      6       1.1       roy  * Copyright (c) 2020 Roy Marples
      7       1.1       roy  *
      8       1.1       roy  * Permission to use, copy, modify, and distribute this software for any
      9       1.1       roy  * purpose with or without fee is hereby granted, provided that the above
     10       1.1       roy  * copyright notice and this permission notice appear in all copies.
     11       1.1       roy  *
     12       1.1       roy  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     13       1.1       roy  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     14       1.1       roy  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     15       1.1       roy  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     16       1.1       roy  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     17       1.1       roy  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     18       1.1       roy  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     19       1.1       roy  */
     20       1.1       roy 
     21       1.1       roy #include <sys/cdefs.h>
     22  1.1.26.1  perseant __KERNEL_RCSID(0, "$NetBSD: if_vether.c,v 1.1.26.1 2025/08/02 05:57:47 perseant Exp $");
     23       1.1       roy 
     24       1.1       roy #include <sys/cprng.h>
     25       1.1       roy #include <sys/kmem.h>
     26       1.1       roy #include <sys/mbuf.h>
     27       1.1       roy 
     28       1.1       roy #include <net/if.h>
     29       1.1       roy #include <net/if_ether.h>
     30  1.1.26.1  perseant #include <net/if_media.h>
     31       1.1       roy #include <net/bpf.h>
     32       1.1       roy 
     33       1.1       roy void		vetherattach(int);
     34       1.1       roy static int	vether_ioctl(struct ifnet *, u_long, void *);
     35  1.1.26.1  perseant static int	vether_mediachange(struct ifnet *);
     36  1.1.26.1  perseant static void	vether_mediastatus(struct ifnet *, struct ifmediareq *);
     37       1.1       roy static void	vether_start(struct ifnet *);
     38       1.1       roy static int	vether_clone_create(struct if_clone *, int);
     39       1.1       roy static int	vether_clone_destroy(struct ifnet *);
     40       1.1       roy 
     41       1.1       roy static void	vether_stop(struct ifnet *, int);
     42       1.1       roy static int	vether_init(struct ifnet *);
     43       1.1       roy 
     44       1.1       roy struct vether_softc {
     45       1.1       roy 	struct ethercom		sc_ec;
     46  1.1.26.1  perseant 	struct ifmedia		sc_im;
     47       1.1       roy };
     48       1.1       roy 
     49       1.1       roy struct if_clone	vether_cloner =
     50       1.1       roy     IF_CLONE_INITIALIZER("vether", vether_clone_create, vether_clone_destroy);
     51       1.1       roy 
     52       1.1       roy void
     53       1.1       roy vetherattach(int nvether)
     54       1.1       roy {
     55       1.1       roy 
     56       1.1       roy 	if_clone_attach(&vether_cloner);
     57       1.1       roy }
     58       1.1       roy 
     59       1.1       roy static int
     60       1.1       roy vether_clone_create(struct if_clone *ifc, int unit)
     61       1.1       roy {
     62       1.1       roy 	struct ifnet *ifp;
     63       1.1       roy 	struct vether_softc *sc;
     64       1.1       roy 	uint8_t enaddr[ETHER_ADDR_LEN] =
     65       1.1       roy 	    { 0xf2, 0x0b, 0xa4, 0xff, 0xff, 0xff };
     66       1.1       roy 
     67       1.1       roy 	sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
     68  1.1.26.1  perseant 
     69  1.1.26.1  perseant 	sc->sc_ec.ec_ifmedia = &sc->sc_im;
     70  1.1.26.1  perseant 	ifmedia_init(&sc->sc_im, 0, vether_mediachange, vether_mediastatus);
     71  1.1.26.1  perseant 	ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_AUTO, 0, NULL);
     72  1.1.26.1  perseant 	ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_NONE, 0, NULL);
     73  1.1.26.1  perseant 	ifmedia_set(&sc->sc_im, IFM_ETHER|IFM_AUTO);
     74  1.1.26.1  perseant 
     75       1.1       roy 	ifp = &sc->sc_ec.ec_if;
     76       1.1       roy 	if_initname(ifp, ifc->ifc_name, unit);
     77       1.1       roy 	ifp->if_softc = sc;
     78       1.1       roy 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
     79       1.1       roy #ifdef NET_MPSAFE
     80       1.1       roy 	ifp->if_extflags = IFEF_MPSAFE;
     81       1.1       roy #endif
     82       1.1       roy 	ifp->if_ioctl = vether_ioctl;
     83       1.1       roy 	ifp->if_start = vether_start;
     84       1.1       roy 	ifp->if_stop  = vether_stop;
     85       1.1       roy 	ifp->if_init  = vether_init;
     86       1.1       roy 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
     87       1.1       roy 	IFQ_SET_READY(&ifp->if_snd);
     88       1.1       roy 
     89       1.1       roy 	sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU | ETHERCAP_JUMBO_MTU;
     90       1.1       roy 
     91       1.1       roy 	/*
     92       1.1       roy 	 * In order to obtain unique initial Ethernet address on a host,
     93       1.1       roy 	 * do some randomisation.  It's not meant for anything but avoiding
     94       1.1       roy 	 * hard-coding an address.
     95       1.1       roy 	 */
     96       1.1       roy 	cprng_fast(&enaddr[3], 3);
     97       1.1       roy 
     98       1.1       roy 	/* Those steps are mandatory for an Ethernet driver. */
     99       1.1       roy 	if_initialize(ifp);
    100       1.1       roy 	ether_ifattach(ifp, enaddr);
    101       1.1       roy 	if_register(ifp);
    102       1.1       roy 
    103  1.1.26.1  perseant 	/* Notify our link state */
    104  1.1.26.1  perseant 	vether_mediachange(ifp);
    105  1.1.26.1  perseant 
    106       1.1       roy 	return 0;
    107       1.1       roy }
    108       1.1       roy 
    109       1.1       roy static int
    110       1.1       roy vether_clone_destroy(struct ifnet *ifp)
    111       1.1       roy {
    112       1.1       roy 	struct vether_softc *sc = ifp->if_softc;
    113       1.1       roy 
    114       1.1       roy 	ether_ifdetach(ifp);
    115       1.1       roy 	if_detach(ifp);
    116       1.1       roy 	kmem_free(sc, sizeof(*sc));
    117       1.1       roy 	return 0;
    118       1.1       roy }
    119       1.1       roy 
    120       1.1       roy static int
    121       1.1       roy vether_init(struct ifnet *ifp)
    122       1.1       roy {
    123       1.1       roy 
    124       1.1       roy 	ifp->if_flags |= IFF_RUNNING;
    125       1.1       roy 	vether_start(ifp);
    126       1.1       roy 	return 0;
    127       1.1       roy }
    128       1.1       roy 
    129  1.1.26.1  perseant static int
    130  1.1.26.1  perseant vether_mediachange(struct ifnet *ifp)
    131  1.1.26.1  perseant {
    132  1.1.26.1  perseant 	struct vether_softc *sc = ifp->if_softc;
    133  1.1.26.1  perseant 	int link_state;
    134  1.1.26.1  perseant 
    135  1.1.26.1  perseant 	if (IFM_SUBTYPE(sc->sc_im.ifm_cur->ifm_media) == IFM_NONE)
    136  1.1.26.1  perseant 		link_state = LINK_STATE_DOWN;
    137  1.1.26.1  perseant 	else
    138  1.1.26.1  perseant 		link_state = LINK_STATE_UP;
    139  1.1.26.1  perseant 
    140  1.1.26.1  perseant 	if_link_state_change(ifp, link_state);
    141  1.1.26.1  perseant 	return 0;
    142  1.1.26.1  perseant }
    143  1.1.26.1  perseant 
    144  1.1.26.1  perseant static void
    145  1.1.26.1  perseant vether_mediastatus(struct ifnet *ifp, struct ifmediareq *imr)
    146  1.1.26.1  perseant {
    147  1.1.26.1  perseant 	struct vether_softc *sc = ifp->if_softc;
    148  1.1.26.1  perseant 
    149  1.1.26.1  perseant 	imr->ifm_active = sc->sc_im.ifm_cur->ifm_media;
    150  1.1.26.1  perseant 
    151  1.1.26.1  perseant 	imr->ifm_status = IFM_AVALID;
    152  1.1.26.1  perseant 	if (IFM_SUBTYPE(imr->ifm_active) != IFM_NONE)
    153  1.1.26.1  perseant 		imr->ifm_status |= IFM_ACTIVE;
    154  1.1.26.1  perseant }
    155  1.1.26.1  perseant 
    156       1.1       roy /*
    157       1.1       roy  * The bridge has magically already done all the work for us,
    158       1.1       roy  * and we only need to discard the packets.
    159       1.1       roy  */
    160       1.1       roy static void
    161       1.1       roy vether_start(struct ifnet *ifp)
    162       1.1       roy {
    163       1.1       roy 	struct mbuf *m;
    164       1.1       roy 
    165       1.1       roy 	for (;;) {
    166       1.1       roy 		IFQ_DEQUEUE(&ifp->if_snd, m);
    167       1.1       roy 		if (m == NULL)
    168       1.1       roy 			break;
    169       1.1       roy 		bpf_mtap(ifp, m, BPF_D_OUT);
    170       1.1       roy 		m_freem(m);
    171       1.1       roy 		if_statinc(ifp, if_opackets);
    172       1.1       roy 	}
    173       1.1       roy }
    174       1.1       roy 
    175       1.1       roy static void
    176       1.1       roy vether_stop(struct ifnet *ifp, __unused int disable)
    177       1.1       roy {
    178       1.1       roy 
    179       1.1       roy 	ifp->if_flags &= ~IFF_RUNNING;
    180       1.1       roy }
    181       1.1       roy 
    182       1.1       roy static int
    183       1.1       roy vether_ioctl(struct ifnet *ifp, unsigned long cmd, void *data)
    184       1.1       roy {
    185       1.1       roy 	int error = 0;
    186       1.1       roy 
    187       1.1       roy 	switch (cmd) {
    188       1.1       roy 	case SIOCADDMULTI:
    189       1.1       roy 	case SIOCDELMULTI:
    190       1.1       roy 		break;
    191       1.1       roy 
    192       1.1       roy 	default:
    193       1.1       roy 		error = ether_ioctl(ifp, cmd, data);
    194       1.1       roy 	}
    195       1.1       roy 	return error;
    196       1.1       roy }
    197