xref: /openbsd/sys/net/if_enc.c (revision 771fbea0)
1 /*	$OpenBSD: if_enc.c,v 1.78 2020/12/28 14:28:50 kn 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 "bpfilter.h"
20 
21 #include <sys/param.h>
22 #include <sys/systm.h>
23 #include <sys/malloc.h>
24 #include <sys/socket.h>
25 #include <sys/sockio.h>
26 #include <sys/mbuf.h>
27 
28 #include <net/if.h>
29 #include <net/if_dl.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 
37 struct ifnet			**enc_ifps;	/* rdomain-mapped enc ifs */
38 u_int				  enc_max_rdomain;
39 struct ifnet			**enc_allifps;	/* unit-mapped enc ifs */
40 u_int				  enc_max_unit;
41 #define ENC_MAX_UNITS		  4096		/* XXX n per rdomain */
42 
43 void	 encattach(int);
44 
45 int	 enc_clone_create(struct if_clone *, int);
46 int	 enc_clone_destroy(struct ifnet *);
47 int	 enc_output(struct ifnet *, struct mbuf *, struct sockaddr *,
48 	    struct rtentry *);
49 int	 enc_ioctl(struct ifnet *, u_long, caddr_t);
50 
51 int	 enc_setif(struct ifnet *, u_int);
52 void	 enc_unsetif(struct ifnet *);
53 
54 struct if_clone enc_cloner =
55     IF_CLONE_INITIALIZER("enc", enc_clone_create, enc_clone_destroy);
56 
57 void
58 encattach(int count)
59 {
60 	/* Create enc0 by default */
61 	(void)enc_clone_create(&enc_cloner, 0);
62 
63 	if_clone_attach(&enc_cloner);
64 }
65 
66 int
67 enc_clone_create(struct if_clone *ifc, int unit)
68 {
69 	struct enc_softc	*sc;
70 	struct ifnet		*ifp;
71 	struct ifnet		**new;
72 	size_t			 oldlen;
73 	int			 error;
74 
75 	if (unit > ENC_MAX_UNITS)
76 		return (EINVAL);
77 
78 	if ((sc = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
79 		return (ENOBUFS);
80 
81 	sc->sc_unit = unit;
82 
83 	ifp = &sc->sc_if;
84 	ifp->if_softc = sc;
85 	ifp->if_type = IFT_ENC;
86 	ifp->if_xflags = IFXF_CLONED;
87 	ifp->if_output = enc_output;
88 	ifp->if_ioctl = enc_ioctl;
89 	ifp->if_hdrlen = ENC_HDRLEN;
90 
91 	snprintf(ifp->if_xname, sizeof(ifp->if_xname), "%s%d",
92 	    ifc->ifc_name, unit);
93 
94 	if_attach(ifp);
95 	if (unit == 0)
96 		if_addgroup(ifp, ifc->ifc_name);
97 	/*
98 	 * enc(4) does not have a link-layer address but rtrequest()
99 	 * wants an ifa for every route entry.  So let's setup a fake
100 	 * and empty ifa of type AF_LINK for this purpose.
101 	 */
102 	if_alloc_sadl(ifp);
103 	sc->sc_ifa.ifa_ifp = ifp;
104 	sc->sc_ifa.ifa_addr = sdltosa(ifp->if_sadl);
105 	sc->sc_ifa.ifa_netmask = NULL;
106 
107 #if NBPFILTER > 0
108 	bpfattach(&ifp->if_bpf, ifp, DLT_ENC, ENC_HDRLEN);
109 #endif
110 	NET_LOCK();
111 	error = enc_setif(ifp, 0);
112 	if (error != 0) {
113 		NET_UNLOCK();
114 		if_detach(ifp);
115 		free(sc, M_DEVBUF, sizeof(*sc));
116 		return (error);
117 	}
118 
119 	if (enc_allifps == NULL || unit > enc_max_unit) {
120 		if ((new = mallocarray(unit + 1, sizeof(struct ifnet *),
121 		    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL) {
122 			NET_UNLOCK();
123 			return (ENOBUFS);
124 		}
125 
126 		if (enc_allifps != NULL) {
127 			oldlen = sizeof(struct ifnet *) * (enc_max_unit + 1);
128 			memcpy(new, enc_allifps, oldlen);
129 			free(enc_allifps, M_DEVBUF, oldlen);
130 		}
131 		enc_allifps = new;
132 		enc_max_unit = unit;
133 	}
134 	enc_allifps[unit] = ifp;
135 	NET_UNLOCK();
136 
137 	return (0);
138 }
139 
140 int
141 enc_clone_destroy(struct ifnet *ifp)
142 {
143 	struct enc_softc	*sc = ifp->if_softc;
144 
145 	/* Protect users from removing enc0 */
146 	if (sc->sc_unit == 0)
147 		return (EPERM);
148 
149 	NET_LOCK();
150 	enc_allifps[sc->sc_unit] = NULL;
151 	enc_unsetif(ifp);
152 	NET_UNLOCK();
153 
154 	if_detach(ifp);
155 	free(sc, M_DEVBUF, sizeof(*sc));
156 
157 	return (0);
158 }
159 
160 int
161 enc_output(struct ifnet *ifp, struct mbuf *m, struct sockaddr *sa,
162     struct rtentry *rt)
163 {
164 	m_freem(m);	/* drop packet */
165 	return (EAFNOSUPPORT);
166 }
167 
168 int
169 enc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
170 {
171 	struct ifreq	*ifr = (struct ifreq *)data;
172 	int		 error;
173 
174 	switch (cmd) {
175 	case SIOCSIFADDR:
176 	case SIOCSIFDSTADDR:
177 	case SIOCSIFFLAGS:
178 		if (ifp->if_flags & IFF_UP)
179 			ifp->if_flags |= IFF_RUNNING;
180 		else
181 			ifp->if_flags &= ~IFF_RUNNING;
182 		break;
183 	case SIOCSIFRDOMAIN:
184 		if ((error = enc_setif(ifp, ifr->ifr_rdomainid)) != 0)
185 			return (error);
186 		/* FALLTHROUGH */
187 	default:
188 		return (ENOTTY);
189 	}
190 
191 	return (0);
192 }
193 
194 struct ifnet *
195 enc_getif(u_int rdomain, u_int unit)
196 {
197 	struct ifnet	*ifp;
198 
199 	NET_ASSERT_LOCKED();
200 
201 	/* Check if the caller wants to get a non-default enc interface */
202 	if (unit > 0) {
203 		if (unit > enc_max_unit)
204 			return (NULL);
205 		ifp = enc_allifps[unit];
206 		if (ifp == NULL || ifp->if_rdomain != rdomain)
207 			return (NULL);
208 		return (ifp);
209 	}
210 
211 	/* Otherwise return the default enc interface for this rdomain */
212 	if (enc_ifps == NULL)
213 		return (NULL);
214 	else if (rdomain > RT_TABLEID_MAX)
215 		return (NULL);
216 	else if (rdomain > enc_max_rdomain)
217 		return (NULL);
218 	return (enc_ifps[rdomain]);
219 }
220 
221 struct ifaddr *
222 enc_getifa(u_int rdomain, u_int unit)
223 {
224 	struct ifnet		*ifp;
225 	struct enc_softc	*sc;
226 
227 	ifp = enc_getif(rdomain, unit);
228 	if (ifp == NULL)
229 		return (NULL);
230 
231 	sc = ifp->if_softc;
232 	return (&sc->sc_ifa);
233 }
234 int
235 enc_setif(struct ifnet *ifp, u_int rdomain)
236 {
237 	struct ifnet	**new;
238 	size_t		 oldlen;
239 
240 	NET_ASSERT_LOCKED();
241 
242 	enc_unsetif(ifp);
243 
244 	/*
245 	 * There can only be one default encif per rdomain -
246 	 * Don't overwrite the existing enc iface that is stored
247 	 * for this rdomain, so only the first enc interface that
248 	 * was added for this rdomain becomes the default.
249 	 */
250 	if (enc_getif(rdomain, 0) != NULL)
251 		return (0);
252 
253 	if (rdomain > RT_TABLEID_MAX)
254 		return (EINVAL);
255 
256 	if (enc_ifps == NULL || rdomain > enc_max_rdomain) {
257 		if ((new = mallocarray(rdomain + 1, sizeof(struct ifnet *),
258 		    M_DEVBUF, M_NOWAIT|M_ZERO)) == NULL)
259 			return (ENOBUFS);
260 
261 		if (enc_ifps != NULL) {
262 			oldlen = sizeof(struct ifnet *) * (enc_max_rdomain + 1);
263 			memcpy(new, enc_ifps, oldlen);
264 			free(enc_ifps, M_DEVBUF, oldlen);
265 		}
266 		enc_ifps = new;
267 		enc_max_rdomain = rdomain;
268 	}
269 
270 	enc_ifps[rdomain] = ifp;
271 
272 	/* Indicate that this interface is the rdomain default */
273 	ifp->if_link_state = LINK_STATE_UP;
274 
275 	return (0);
276 }
277 
278 void
279 enc_unsetif(struct ifnet *ifp)
280 {
281 	u_int			 rdomain = ifp->if_rdomain, i;
282 	struct ifnet		*oifp, *nifp;
283 
284 	if ((oifp = enc_getif(rdomain, 0)) == NULL || oifp != ifp)
285 		return;
286 
287 	/* Clear slot for this rdomain */
288 	enc_ifps[rdomain] = NULL;
289 	ifp->if_link_state = LINK_STATE_UNKNOWN;
290 
291 	/*
292 	 * Now find the next available encif to be the default interface
293 	 * for this rdomain.
294 	 */
295 	for (i = 0; i < (enc_max_unit + 1); i++) {
296 		nifp = enc_allifps[i];
297 
298 		if (nifp == NULL || nifp == ifp || nifp->if_rdomain != rdomain)
299 			continue;
300 
301 		enc_ifps[rdomain] = nifp;
302 		nifp->if_link_state = LINK_STATE_UP;
303 		break;
304 	}
305 }
306