if_enc.c revision 1.60
1/*	$OpenBSD: if_enc.c,v 1.60 2015/03/14 03:38:51 jsg 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_var.h>
31#include <net/if_enc.h>
32#include <net/if_types.h>
33#if NBPFILTER > 0
34#include <net/bpf.h>
35#endif
36
37struct ifnet			**enc_ifps;	/* rdomain-mapped enc ifs */
38u_int				  enc_max_id;
39struct ifnet			**enc_allifps;	/* unit-mapped enc ifs */
40u_int				  enc_max_unit;
41#define ENC_MAX_UNITS		  4096		/* XXX n per rdomain */
42
43void	 encattach(int);
44
45int	 enc_clone_create(struct if_clone *, int);
46int	 enc_clone_destroy(struct ifnet *);
47void	 enc_start(struct ifnet *);
48int	 enc_output(struct ifnet *, struct mbuf *, struct sockaddr *,
49	    struct rtentry *);
50int	 enc_ioctl(struct ifnet *, u_long, caddr_t);
51
52int	 enc_setif(struct ifnet *, u_int);
53void	 enc_unsetif(struct ifnet *);
54
55struct if_clone enc_cloner =
56    IF_CLONE_INITIALIZER("enc", enc_clone_create, enc_clone_destroy);
57
58void
59encattach(int count)
60{
61	/* Create enc0 by default */
62	(void)enc_clone_create(&enc_cloner, 0);
63
64	if_clone_attach(&enc_cloner);
65}
66
67int
68enc_clone_create(struct if_clone *ifc, int unit)
69{
70	struct enc_softc	*sc;
71	struct ifnet		*ifp;
72	struct ifnet		**new;
73	size_t			 newlen;
74	int			 error;
75
76	if (unit > ENC_MAX_UNITS)
77		return (EINVAL);
78
79	if ((sc = malloc(sizeof(struct enc_softc),
80	    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
81		return (ENOBUFS);
82
83	sc->sc_unit = unit;
84
85	ifp = &sc->sc_if;
86	ifp->if_softc = sc;
87	ifp->if_type = IFT_ENC;
88	ifp->if_start = enc_start;
89	ifp->if_output = enc_output;
90	ifp->if_ioctl = enc_ioctl;
91	ifp->if_hdrlen = ENC_HDRLEN;
92
93	snprintf(ifp->if_xname, sizeof(ifp->if_xname), "%s%d",
94	    ifc->ifc_name, unit);
95
96	if_attach(ifp);
97	if (unit == 0)
98		if_addgroup(ifp, ifc->ifc_name);
99	/*
100	 * enc(4) does not have a link-layer address but rtrequest1()
101	 * wants an ifa for every route entry.  So let's setup a fake
102	 * and empty ifa of type AF_LINK for this purpose.
103	 */
104	if_alloc_sadl(ifp);
105	sc->sc_ifa.ifa_ifp = ifp;
106	sc->sc_ifa.ifa_rtrequest = link_rtrequest;
107	sc->sc_ifa.ifa_addr = (struct sockaddr *)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		IF_DROP(&ifp->if_snd);
166		IF_DEQUEUE(&ifp->if_snd, m);
167		if (m == NULL)
168			break;
169		m_freem(m);
170	}
171}
172
173int
174enc_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
175    struct rtentry *rt)
176{
177	m_freem(m);	/* drop packet */
178	return (0);
179}
180
181int
182enc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
183{
184	struct ifreq	*ifr = (struct ifreq *)data;
185	int		 error;
186
187	switch (cmd) {
188	case SIOCAIFADDR:
189	case SIOCSIFADDR:
190	case SIOCSIFDSTADDR:
191	case SIOCSIFFLAGS:
192		if (ifp->if_flags & IFF_UP)
193			ifp->if_flags |= IFF_RUNNING;
194		else
195			ifp->if_flags &= ~IFF_RUNNING;
196		break;
197	case SIOCSIFRDOMAIN:
198		if ((error = enc_setif(ifp, ifr->ifr_rdomainid)) != 0)
199			return (error);
200		/* FALLTHROUGH */
201	default:
202		return (ENOTTY);
203	}
204
205	return (0);
206}
207
208struct ifnet *
209enc_getif(u_int id, u_int unit)
210{
211	struct ifnet	*ifp;
212
213	/* Check if the caller wants to get a non-default enc interface */
214	if (unit > 0) {
215		if (unit > enc_max_unit)
216			return (NULL);
217		ifp = enc_allifps[unit];
218		if (ifp == NULL || ifp->if_rdomain != id)
219			return (NULL);
220		return (ifp);
221	}
222
223	/* Otherwise return the default enc interface for this rdomain */
224	if (enc_ifps == NULL)
225		return (NULL);
226	else if (id > RT_TABLEID_MAX)
227		return (NULL);
228	else if (id > enc_max_id)
229		return (NULL);
230	return (enc_ifps[id]);
231}
232
233struct ifaddr
234*enc_getifa(u_int id, u_int unit)
235{
236	struct ifnet		*ifp;
237	struct enc_softc	*sc;
238
239	ifp = enc_getif(id, unit);
240	if (ifp == NULL)
241		return (NULL);
242
243	sc = ifp->if_softc;
244	return (&sc->sc_ifa);
245}
246int
247enc_setif(struct ifnet *ifp, u_int id)
248{
249	struct ifnet	**new;
250	size_t		 newlen;
251
252	enc_unsetif(ifp);
253
254	/*
255	 * There can only be one default encif per rdomain -
256	 * Don't overwrite the existing enc iface that is stored
257	 * for this rdomain, so only the first enc interface that
258	 * was added for this rdomain becomes the default.
259	 */
260	if (enc_getif(id, 0) != NULL)
261		return (0);
262
263	if (id > RT_TABLEID_MAX)
264		return (EINVAL);
265
266	if (id == 0 || id > enc_max_id) {
267		if ((new = mallocarray(id + 1, sizeof(struct ifnet *),
268		    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
269			return (ENOBUFS);
270		newlen = sizeof(struct ifnet *) * (id + 1);
271
272		if (enc_ifps != NULL) {
273			memcpy(new, enc_ifps,
274			    sizeof(struct ifnet *) * (enc_max_id + 1));
275			free(enc_ifps, M_DEVBUF, 0);
276		}
277		enc_ifps = new;
278		enc_max_id = id;
279	}
280
281	enc_ifps[id] = ifp;
282
283	/* Indicate that this interface is the rdomain default */
284	ifp->if_link_state = LINK_STATE_UP;
285
286	return (0);
287}
288
289void
290enc_unsetif(struct ifnet *ifp)
291{
292	u_int			 id = ifp->if_rdomain, i;
293	struct ifnet		*oifp, *nifp;
294
295	if ((oifp = enc_getif(id, 0)) == NULL || oifp != ifp)
296		return;
297
298	/* Clear slot for this rdomain */
299	enc_ifps[id] = NULL;
300	ifp->if_link_state = LINK_STATE_UNKNOWN;
301
302	/*
303	 * Now find the next available encif to be the default interface
304	 * for this rdomain.
305	 */
306	for (i = 0; i < (enc_max_unit + 1); i++) {
307		nifp = enc_allifps[i];
308
309		if (nifp == NULL || nifp == ifp || nifp->if_rdomain != id)
310			continue;
311
312		enc_ifps[id] = nifp;
313		nifp->if_link_state = LINK_STATE_UP;
314		break;
315	}
316}
317