summaryrefslogtreecommitdiff
path: root/sys/net/if_vether.c
blob: 520bf4af16068024a1bf0ce6be7c38bf47c7b08e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*	$NetBSD: if_vether.c,v 1.1 2020/09/27 13:31:04 roy Exp $	*/
/* $OpenBSD: if_vether.c,v 1.27 2016/04/13 11:41:15 mpi Exp $ */

/*
 * Copyright (c) 2009 Theo de Raadt
 * Copyright (c) 2020 Roy Marples
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_vether.c,v 1.1 2020/09/27 13:31:04 roy Exp $");

#include <sys/cprng.h>
#include <sys/kmem.h>
#include <sys/mbuf.h>

#include <net/if.h>
#include <net/if_ether.h>
#include <net/bpf.h>

void		vetherattach(int);
static int	vether_ioctl(struct ifnet *, u_long, void *);
static void	vether_start(struct ifnet *);
static int	vether_clone_create(struct if_clone *, int);
static int	vether_clone_destroy(struct ifnet *);

static void	vether_stop(struct ifnet *, int);
static int	vether_init(struct ifnet *);

struct vether_softc {
	struct ethercom		sc_ec;
};

struct if_clone	vether_cloner =
    IF_CLONE_INITIALIZER("vether", vether_clone_create, vether_clone_destroy);

void
vetherattach(int nvether)
{

	if_clone_attach(&vether_cloner);
}

static int
vether_clone_create(struct if_clone *ifc, int unit)
{
	struct ifnet *ifp;
	struct vether_softc *sc;
	uint8_t enaddr[ETHER_ADDR_LEN] =
	    { 0xf2, 0x0b, 0xa4, 0xff, 0xff, 0xff };

	sc = kmem_zalloc(sizeof(*sc), KM_SLEEP);
	ifp = &sc->sc_ec.ec_if;
	if_initname(ifp, ifc->ifc_name, unit);
	ifp->if_softc = sc;
	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
#ifdef NET_MPSAFE
	ifp->if_extflags = IFEF_MPSAFE;
#endif
	ifp->if_ioctl = vether_ioctl;
	ifp->if_start = vether_start;
	ifp->if_stop  = vether_stop;
	ifp->if_init  = vether_init;
	IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
	IFQ_SET_READY(&ifp->if_snd);

	sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU | ETHERCAP_JUMBO_MTU;

	/*
	 * In order to obtain unique initial Ethernet address on a host,
	 * do some randomisation.  It's not meant for anything but avoiding
	 * hard-coding an address.
	 */
	cprng_fast(&enaddr[3], 3);

	/* Those steps are mandatory for an Ethernet driver. */
	if_initialize(ifp);
	ether_ifattach(ifp, enaddr);
	if_register(ifp);

	return 0;
}

static int
vether_clone_destroy(struct ifnet *ifp)
{
	struct vether_softc *sc = ifp->if_softc;

	ether_ifdetach(ifp);
	if_detach(ifp);
	kmem_free(sc, sizeof(*sc));
	return 0;
}

static int
vether_init(struct ifnet *ifp)
{

	ifp->if_flags |= IFF_RUNNING;
	if_link_state_change(ifp, LINK_STATE_UP);
	vether_start(ifp);
	return 0;
}

/*
 * The bridge has magically already done all the work for us,
 * and we only need to discard the packets.
 */
static void
vether_start(struct ifnet *ifp)
{
	struct mbuf *m;

	for (;;) {
		IFQ_DEQUEUE(&ifp->if_snd, m);
		if (m == NULL)
			break;
		bpf_mtap(ifp, m, BPF_D_OUT);
		m_freem(m);
		if_statinc(ifp, if_opackets);
	}
}

static void
vether_stop(struct ifnet *ifp, __unused int disable)
{

	ifp->if_flags &= ~IFF_RUNNING;
	if_link_state_change(ifp, LINK_STATE_DOWN);
}

static int
vether_ioctl(struct ifnet *ifp, unsigned long cmd, void *data)
{
	int error = 0;

	switch (cmd) {
	case SIOCADDMULTI:
	case SIOCDELMULTI:
		break;

	default:
		error = ether_ioctl(ifp, cmd, data);
	}
	return error;
}