if_enc.c revision 1.66
1/*	$OpenBSD: if_enc.c,v 1.66 2017/01/20 00:51:56 mpi Exp $	*/
2
3/*
4 * Copyright (c) 2010 Reyk Floeter <reyk@vantronix.net>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include "enc.h"
20#include "bpfilter.h"
21
22#include <sys/param.h>
23#include <sys/systm.h>
24#include <sys/malloc.h>
25#include <sys/socket.h>
26#include <sys/sockio.h>
27#include <sys/mbuf.h>
28
29#include <net/if.h>
30#include <net/if_dl.h>
31#include <net/if_var.h>
32#include <net/if_enc.h>
33#include <net/if_types.h>
34#if NBPFILTER > 0
35#include <net/bpf.h>
36#endif
37
38struct ifnet			**enc_ifps;	/* rdomain-mapped enc ifs */
39u_int				  enc_max_id;
40struct ifnet			**enc_allifps;	/* unit-mapped enc ifs */
41u_int				  enc_max_unit;
42#define ENC_MAX_UNITS		  4096		/* XXX n per rdomain */
43
44void	 encattach(int);
45
46int	 enc_clone_create(struct if_clone *, int);
47int	 enc_clone_destroy(struct ifnet *);
48void	 enc_start(struct ifnet *);
49int	 enc_output(struct ifnet *, struct mbuf *, struct sockaddr *,
50	    struct rtentry *);
51int	 enc_ioctl(struct ifnet *, u_long, caddr_t);
52
53int	 enc_setif(struct ifnet *, u_int);
54void	 enc_unsetif(struct ifnet *);
55
56struct if_clone enc_cloner =
57    IF_CLONE_INITIALIZER("enc", enc_clone_create, enc_clone_destroy);
58
59void
60encattach(int count)
61{
62	/* Create enc0 by default */
63	(void)enc_clone_create(&enc_cloner, 0);
64
65	if_clone_attach(&enc_cloner);
66}
67
68int
69enc_clone_create(struct if_clone *ifc, int unit)
70{
71	struct enc_softc	*sc;
72	struct ifnet		*ifp;
73	struct ifnet		**new;
74	size_t			 newlen;
75	int			 error;
76
77	if (unit > ENC_MAX_UNITS)
78		return (EINVAL);
79
80	if ((sc = malloc(sizeof(struct enc_softc),
81	    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
82		return (ENOBUFS);
83
84	sc->sc_unit = unit;
85
86	ifp = &sc->sc_if;
87	ifp->if_softc = sc;
88	ifp->if_type = IFT_ENC;
89	ifp->if_start = enc_start;
90	ifp->if_output = enc_output;
91	ifp->if_ioctl = enc_ioctl;
92	ifp->if_hdrlen = ENC_HDRLEN;
93
94	snprintf(ifp->if_xname, sizeof(ifp->if_xname), "%s%d",
95	    ifc->ifc_name, unit);
96
97	if_attach(ifp);
98	if (unit == 0)
99		if_addgroup(ifp, ifc->ifc_name);
100	/*
101	 * enc(4) does not have a link-layer address but rtrequest()
102	 * wants an ifa for every route entry.  So let's setup a fake
103	 * and empty ifa of type AF_LINK for this purpose.
104	 */
105	if_alloc_sadl(ifp);
106	sc->sc_ifa.ifa_ifp = ifp;
107	sc->sc_ifa.ifa_addr = sdltosa(ifp->if_sadl);
108	sc->sc_ifa.ifa_netmask = NULL;
109
110#if NBPFILTER > 0
111	bpfattach(&ifp->if_bpf, ifp, DLT_ENC, ENC_HDRLEN);
112#endif
113
114	if ((error = enc_setif(ifp, 0)) != 0) {
115		if_detach(ifp);
116		free(sc, M_DEVBUF, 0);
117		return (error);
118	}
119
120	if (unit == 0 || unit > enc_max_unit) {
121		if ((new = mallocarray(unit + 1, sizeof(struct ifnet *),
122		    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
123			return (ENOBUFS);
124		newlen = sizeof(struct ifnet *) * (unit + 1);
125
126		if (enc_allifps != NULL) {
127			memcpy(new, enc_allifps,
128			    sizeof(struct ifnet *) * (enc_max_unit + 1));
129			free(enc_allifps, M_DEVBUF, 0);
130		}
131		enc_allifps = new;
132		enc_max_unit = unit;
133	}
134	enc_allifps[unit] = ifp;
135
136	return (0);
137}
138
139int
140enc_clone_destroy(struct ifnet *ifp)
141{
142	struct enc_softc	*sc = ifp->if_softc;
143	int			 s;
144
145	/* Protect users from removing enc0 */
146	if (sc->sc_unit == 0)
147		return (EPERM);
148
149	s = splnet();
150	enc_allifps[sc->sc_unit] = NULL;
151	enc_unsetif(ifp);
152	if_detach(ifp);
153	free(sc, M_DEVBUF, 0);
154	splx(s);
155
156	return (0);
157}
158
159void
160enc_start(struct ifnet *ifp)
161{
162	struct mbuf	*m;
163
164	for (;;) {
165		IFQ_DEQUEUE(&ifp->if_snd, m);
166		if (m == NULL)
167			break;
168		m_freem(m);
169	}
170}
171
172int
173enc_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
174    struct rtentry *rt)
175{
176	m_freem(m);	/* drop packet */
177	return (EAFNOSUPPORT);
178}
179
180int
181enc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
182{
183	struct ifreq	*ifr = (struct ifreq *)data;
184	int		 error;
185
186	switch (cmd) {
187	case SIOCSIFADDR:
188	case SIOCSIFDSTADDR:
189	case SIOCSIFFLAGS:
190		if (ifp->if_flags & IFF_UP)
191			ifp->if_flags |= IFF_RUNNING;
192		else
193			ifp->if_flags &= ~IFF_RUNNING;
194		break;
195	case SIOCSIFRDOMAIN:
196		if ((error = enc_setif(ifp, ifr->ifr_rdomainid)) != 0)
197			return (error);
198		/* FALLTHROUGH */
199	default:
200		return (ENOTTY);
201	}
202
203	return (0);
204}
205
206struct ifnet *
207enc_getif(u_int id, u_int unit)
208{
209	struct ifnet	*ifp;
210
211	/* Check if the caller wants to get a non-default enc interface */
212	if (unit > 0) {
213		if (unit > enc_max_unit)
214			return (NULL);
215		ifp = enc_allifps[unit];
216		if (ifp == NULL || ifp->if_rdomain != id)
217			return (NULL);
218		return (ifp);
219	}
220
221	/* Otherwise return the default enc interface for this rdomain */
222	if (enc_ifps == NULL)
223		return (NULL);
224	else if (id > RT_TABLEID_MAX)
225		return (NULL);
226	else if (id > enc_max_id)
227		return (NULL);
228	return (enc_ifps[id]);
229}
230
231struct ifaddr
232*enc_getifa(u_int id, u_int unit)
233{
234	struct ifnet		*ifp;
235	struct enc_softc	*sc;
236
237	ifp = enc_getif(id, unit);
238	if (ifp == NULL)
239		return (NULL);
240
241	sc = ifp->if_softc;
242	return (&sc->sc_ifa);
243}
244int
245enc_setif(struct ifnet *ifp, u_int id)
246{
247	struct ifnet	**new;
248	size_t		 newlen;
249
250	enc_unsetif(ifp);
251
252	/*
253	 * There can only be one default encif per rdomain -
254	 * Don't overwrite the existing enc iface that is stored
255	 * for this rdomain, so only the first enc interface that
256	 * was added for this rdomain becomes the default.
257	 */
258	if (enc_getif(id, 0) != NULL)
259		return (0);
260
261	if (id > RT_TABLEID_MAX)
262		return (EINVAL);
263
264	if (id == 0 || id > enc_max_id) {
265		if ((new = mallocarray(id + 1, sizeof(struct ifnet *),
266		    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
267			return (ENOBUFS);
268		newlen = sizeof(struct ifnet *) * (id + 1);
269
270		if (enc_ifps != NULL) {
271			memcpy(new, enc_ifps,
272			    sizeof(struct ifnet *) * (enc_max_id + 1));
273			free(enc_ifps, M_DEVBUF, 0);
274		}
275		enc_ifps = new;
276		enc_max_id = id;
277	}
278
279	enc_ifps[id] = ifp;
280
281	/* Indicate that this interface is the rdomain default */
282	ifp->if_link_state = LINK_STATE_UP;
283
284	return (0);
285}
286
287void
288enc_unsetif(struct ifnet *ifp)
289{
290	u_int			 id = ifp->if_rdomain, i;
291	struct ifnet		*oifp, *nifp;
292
293	if ((oifp = enc_getif(id, 0)) == NULL || oifp != ifp)
294		return;
295
296	/* Clear slot for this rdomain */
297	enc_ifps[id] = NULL;
298	ifp->if_link_state = LINK_STATE_UNKNOWN;
299
300	/*
301	 * Now find the next available encif to be the default interface
302	 * for this rdomain.
303	 */
304	for (i = 0; i < (enc_max_unit + 1); i++) {
305		nifp = enc_allifps[i];
306
307		if (nifp == NULL || nifp == ifp || nifp->if_rdomain != id)
308			continue;
309
310		enc_ifps[id] = nifp;
311		nifp->if_link_state = LINK_STATE_UP;
312		break;
313	}
314}
315