Home | History | Annotate | Line # | Download | only in net
if_vether.c revision 1.1
      1  1.1  roy /*	$NetBSD: if_vether.c,v 1.1 2020/09/27 13:31:04 roy 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  roy __KERNEL_RCSID(0, "$NetBSD: if_vether.c,v 1.1 2020/09/27 13:31:04 roy 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  roy #include <net/bpf.h>
     31  1.1  roy 
     32  1.1  roy void		vetherattach(int);
     33  1.1  roy static int	vether_ioctl(struct ifnet *, u_long, void *);
     34  1.1  roy static void	vether_start(struct ifnet *);
     35  1.1  roy static int	vether_clone_create(struct if_clone *, int);
     36  1.1  roy static int	vether_clone_destroy(struct ifnet *);
     37  1.1  roy 
     38  1.1  roy static void	vether_stop(struct ifnet *, int);
     39  1.1  roy static int	vether_init(struct ifnet *);
     40  1.1  roy 
     41  1.1  roy struct vether_softc {
     42  1.1  roy 	struct ethercom		sc_ec;
     43  1.1  roy };
     44  1.1  roy 
     45  1.1  roy struct if_clone	vether_cloner =
     46  1.1  roy     IF_CLONE_INITIALIZER("vether", vether_clone_create, vether_clone_destroy);
     47  1.1  roy 
     48  1.1  roy void
     49  1.1  roy vetherattach(int nvether)
     50  1.1  roy {
     51  1.1  roy 
     52  1.1  roy 	if_clone_attach(&vether_cloner);
     53  1.1  roy }
     54  1.1  roy 
     55  1.1  roy static int
     56  1.1  roy vether_clone_create(struct if_clone *ifc, int unit)
     57  1.1  roy {
     58  1.1  roy 	struct ifnet *ifp;
     59  1.1  roy 	struct vether_softc *sc;
     60  1.1  roy 	uint8_t enaddr[ETHER_ADDR_LEN] =
     61  1.1  roy 	    { 0xf2, 0x0b, 0xa4, 0xff, 0xff, 0xff };
     62  1.1  roy 
     63  1.1  roy 	sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
     64  1.1  roy 	ifp = &sc->sc_ec.ec_if;
     65  1.1  roy 	if_initname(ifp, ifc->ifc_name, unit);
     66  1.1  roy 	ifp->if_softc = sc;
     67  1.1  roy 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
     68  1.1  roy #ifdef NET_MPSAFE
     69  1.1  roy 	ifp->if_extflags = IFEF_MPSAFE;
     70  1.1  roy #endif
     71  1.1  roy 	ifp->if_ioctl = vether_ioctl;
     72  1.1  roy 	ifp->if_start = vether_start;
     73  1.1  roy 	ifp->if_stop  = vether_stop;
     74  1.1  roy 	ifp->if_init  = vether_init;
     75  1.1  roy 	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
     76  1.1  roy 	IFQ_SET_READY(&ifp->if_snd);
     77  1.1  roy 
     78  1.1  roy 	sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU | ETHERCAP_JUMBO_MTU;
     79  1.1  roy 
     80  1.1  roy 	/*
     81  1.1  roy 	 * In order to obtain unique initial Ethernet address on a host,
     82  1.1  roy 	 * do some randomisation.  It's not meant for anything but avoiding
     83  1.1  roy 	 * hard-coding an address.
     84  1.1  roy 	 */
     85  1.1  roy 	cprng_fast(&enaddr[3], 3);
     86  1.1  roy 
     87  1.1  roy 	/* Those steps are mandatory for an Ethernet driver. */
     88  1.1  roy 	if_initialize(ifp);
     89  1.1  roy 	ether_ifattach(ifp, enaddr);
     90  1.1  roy 	if_register(ifp);
     91  1.1  roy 
     92  1.1  roy 	return 0;
     93  1.1  roy }
     94  1.1  roy 
     95  1.1  roy static int
     96  1.1  roy vether_clone_destroy(struct ifnet *ifp)
     97  1.1  roy {
     98  1.1  roy 	struct vether_softc *sc = ifp->if_softc;
     99  1.1  roy 
    100  1.1  roy 	ether_ifdetach(ifp);
    101  1.1  roy 	if_detach(ifp);
    102  1.1  roy 	kmem_free(sc, sizeof(*sc));
    103  1.1  roy 	return 0;
    104  1.1  roy }
    105  1.1  roy 
    106  1.1  roy static int
    107  1.1  roy vether_init(struct ifnet *ifp)
    108  1.1  roy {
    109  1.1  roy 
    110  1.1  roy 	ifp->if_flags |= IFF_RUNNING;
    111  1.1  roy 	if_link_state_change(ifp, LINK_STATE_UP);
    112  1.1  roy 	vether_start(ifp);
    113  1.1  roy 	return 0;
    114  1.1  roy }
    115  1.1  roy 
    116  1.1  roy /*
    117  1.1  roy  * The bridge has magically already done all the work for us,
    118  1.1  roy  * and we only need to discard the packets.
    119  1.1  roy  */
    120  1.1  roy static void
    121  1.1  roy vether_start(struct ifnet *ifp)
    122  1.1  roy {
    123  1.1  roy 	struct mbuf *m;
    124  1.1  roy 
    125  1.1  roy 	for (;;) {
    126  1.1  roy 		IFQ_DEQUEUE(&ifp->if_snd, m);
    127  1.1  roy 		if (m == NULL)
    128  1.1  roy 			break;
    129  1.1  roy 		bpf_mtap(ifp, m, BPF_D_OUT);
    130  1.1  roy 		m_freem(m);
    131  1.1  roy 		if_statinc(ifp, if_opackets);
    132  1.1  roy 	}
    133  1.1  roy }
    134  1.1  roy 
    135  1.1  roy static void
    136  1.1  roy vether_stop(struct ifnet *ifp, __unused int disable)
    137  1.1  roy {
    138  1.1  roy 
    139  1.1  roy 	ifp->if_flags &= ~IFF_RUNNING;
    140  1.1  roy 	if_link_state_change(ifp, LINK_STATE_DOWN);
    141  1.1  roy }
    142  1.1  roy 
    143  1.1  roy static int
    144  1.1  roy vether_ioctl(struct ifnet *ifp, unsigned long cmd, void *data)
    145  1.1  roy {
    146  1.1  roy 	int error = 0;
    147  1.1  roy 
    148  1.1  roy 	switch (cmd) {
    149  1.1  roy 	case SIOCADDMULTI:
    150  1.1  roy 	case SIOCDELMULTI:
    151  1.1  roy 		break;
    152  1.1  roy 
    153  1.1  roy 	default:
    154  1.1  roy 		error = ether_ioctl(ifp, cmd, data);
    155  1.1  roy 	}
    156  1.1  roy 	return error;
    157  1.1  roy }
    158