xref: /netbsd-src/sys/net/link_proto.c (revision 5bbd2a12505d72a8177929a37b5cee489d0a1cfd)
1 /*	$NetBSD: link_proto.c,v 1.7 2011/10/07 16:34:31 dyoung Exp $	*/
2 
3 /*-
4  * Copyright (c) 1982, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)uipc_proto.c	8.2 (Berkeley) 2/14/95
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: link_proto.c,v 1.7 2011/10/07 16:34:31 dyoung Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/socket.h>
39 #include <sys/protosw.h>
40 #include <sys/domain.h>
41 #include <sys/mbuf.h>
42 #include <sys/un.h>
43 #include <sys/socketvar.h>
44 
45 #include <net/if.h>
46 #include <net/if_dl.h>
47 #include <net/raw_cb.h>
48 #include <net/route.h>
49 
50 static int sockaddr_dl_cmp(const struct sockaddr *, const struct sockaddr *);
51 static int link_usrreq(struct socket *, int, struct mbuf *, struct mbuf *,
52     struct mbuf *, struct lwp *);
53 static void link_init(void);
54 
55 /*
56  * Definitions of protocols supported in the link-layer domain.
57  */
58 
59 DOMAIN_DEFINE(linkdomain);	/* forward define and add to link set */
60 
61 const struct protosw linksw[] = {
62 	{	.pr_type = SOCK_DGRAM,
63 		.pr_domain = &linkdomain,
64 		.pr_protocol = 0,	/* XXX */
65 		.pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
66 		.pr_input = NULL,
67 		.pr_ctlinput = NULL,
68 		.pr_ctloutput = NULL,
69 		.pr_usrreq = link_usrreq,
70 		.pr_init = link_init,
71 	},
72 };
73 
74 struct domain linkdomain = {
75 	.dom_family = AF_LINK,
76 	.dom_name = "link",
77 	.dom_externalize = NULL,
78 	.dom_dispose = NULL,
79 	.dom_protosw = linksw,
80 	.dom_protoswNPROTOSW = &linksw[__arraycount(linksw)],
81 	.dom_sockaddr_cmp = sockaddr_dl_cmp
82 };
83 
84 static void
85 link_init(void)
86 {
87 	return;
88 }
89 
90 static int
91 link_control(struct socket *so, unsigned long cmd, void *data,
92     struct ifnet *ifp, struct lwp *l)
93 {
94 	int error, s;
95 	bool isactive, mkactive;
96 	struct if_laddrreq *iflr;
97 	union {
98 		struct sockaddr sa;
99 		struct sockaddr_dl sdl;
100 		struct sockaddr_storage ss;
101 	} u;
102 	struct ifaddr *ifa;
103 	const struct sockaddr_dl *asdl, *nsdl;
104 
105 	switch (cmd) {
106 	case SIOCALIFADDR:
107 	case SIOCDLIFADDR:
108 	case SIOCGLIFADDR:
109 		iflr = data;
110 
111 		if (iflr->addr.ss_family != AF_LINK)
112 			return EINVAL;
113 
114 		asdl = satocsdl(sstocsa(&iflr->addr));
115 
116 		if (asdl->sdl_alen != ifp->if_addrlen)
117 			return EINVAL;
118 
119 		if (sockaddr_dl_init(&u.sdl, sizeof(u.ss), ifp->if_index,
120 		    ifp->if_type, ifp->if_xname, strlen(ifp->if_xname),
121 		    CLLADDR(asdl), asdl->sdl_alen) == NULL)
122 			return EINVAL;
123 
124 		if ((iflr->flags & IFLR_PREFIX) == 0)
125 			;
126 		else if (iflr->prefixlen != NBBY * ifp->if_addrlen)
127 			return EINVAL;	/* XXX match with prefix */
128 
129 		error = 0;
130 
131 		s = splnet();
132 
133 		IFADDR_FOREACH(ifa, ifp) {
134 			if (sockaddr_cmp(&u.sa, ifa->ifa_addr) == 0)
135 				break;
136 		}
137 
138 		switch (cmd) {
139 		case SIOCGLIFADDR:
140 			if ((iflr->flags & IFLR_PREFIX) == 0) {
141 				IFADDR_FOREACH(ifa, ifp) {
142 					if (ifa->ifa_addr->sa_family == AF_LINK)
143 						break;
144 				}
145 			}
146 			if (ifa == NULL) {
147 				error = EADDRNOTAVAIL;
148 				break;
149 			}
150 
151 			if (ifa == ifp->if_dl)
152 				iflr->flags = IFLR_ACTIVE;
153 			else
154 				iflr->flags = 0;
155 
156 			if (ifa == ifp->if_hwdl)
157 				iflr->flags |= IFLR_FACTORY;
158 
159 			sockaddr_copy(sstosa(&iflr->addr), sizeof(iflr->addr),
160 			    ifa->ifa_addr);
161 
162 			break;
163 		case SIOCDLIFADDR:
164 			if (ifa == NULL)
165 				error = EADDRNOTAVAIL;
166 			else if (ifa == ifp->if_dl || ifa == ifp->if_hwdl)
167 				error = EBUSY;
168 			else {
169 				/* TBD routing socket */
170 				rt_newaddrmsg(RTM_DELETE, ifa, 0, NULL);
171 				ifa_remove(ifp, ifa);
172 			}
173 			break;
174 		case SIOCALIFADDR:
175 			if (ifa != NULL)
176 				;
177 			else if ((ifa = if_dl_create(ifp, &nsdl)) == NULL) {
178 				error = ENOMEM;
179 				break;
180 			} else {
181 				sockaddr_copy(ifa->ifa_addr,
182 				    ifa->ifa_addr->sa_len, &u.sa);
183 				ifa_insert(ifp, ifa);
184 				rt_newaddrmsg(RTM_ADD, ifa, 0, NULL);
185 			}
186 
187 			mkactive = (iflr->flags & IFLR_ACTIVE) != 0;
188 			isactive = (ifa == ifp->if_dl);
189 
190 			if (!isactive && mkactive) {
191 				if_activate_sadl(ifp, ifa, nsdl);
192 				rt_newaddrmsg(RTM_CHANGE, ifa, 0, NULL);
193 				error = ENETRESET;
194 			}
195 			break;
196 		}
197 		splx(s);
198 		if (error != ENETRESET)
199 			return error;
200 		else if ((ifp->if_flags & IFF_RUNNING) != 0)
201 			return (*ifp->if_init)(ifp);
202 		else
203 			return 0;
204 	default:
205 		return ENOTTY;
206 	}
207 }
208 
209 static int
210 link_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
211 	struct mbuf *control, struct lwp *l)
212 {
213 	switch (req) {
214 	case PRU_ATTACH:
215 		sosetlock(so);
216 		return 0;
217 	case PRU_DETACH:
218 		sofree(so);
219 		return 0;
220 	case PRU_CONTROL:
221 		return link_control(so, (unsigned long)m, nam,
222 		    (struct ifnet *)control, l);
223 	default:
224 		return EOPNOTSUPP;
225 	}
226 }
227 
228 /* Compare the field at byte offsets [fieldstart, fieldend) in
229  * two memory regions, [l, l + llen) and [r, r + llen).
230  */
231 static inline int
232 submemcmp(const void *l, const void *r,
233     const uint_fast8_t llen, const uint_fast8_t rlen,
234     const uint_fast8_t fieldstart, const uint_fast8_t fieldend)
235 {
236 	uint_fast8_t cmpend, minlen;
237 	const uint8_t *lb = l, *rb = r;
238 	int rc;
239 
240 	minlen = MIN(llen, rlen);
241 
242 	/* The field is missing from one region.  The shorter region is the
243 	 * lesser region.
244 	 */
245 	if (fieldstart >= minlen)
246 		return llen - rlen;
247 
248 	/* Two empty, present fields are always equal. */
249 	if (fieldstart > fieldend)
250 		return 0;
251 
252 	cmpend = MIN(fieldend, minlen);
253 
254 	rc = memcmp(&lb[fieldstart], &rb[fieldstart], cmpend - fieldstart);
255 
256 	if (rc != 0)
257 		return rc;
258 	/* If one or both fields are truncated, then the shorter is the lesser
259 	 * field.
260 	 */
261 	if (minlen < fieldend)
262 		return llen - rlen;
263 	/* Fields are full-length and equal.  The fields are equal. */
264 	return 0;
265 }
266 
267 uint8_t
268 sockaddr_dl_measure(uint8_t namelen, uint8_t addrlen)
269 {
270 	return offsetof(struct sockaddr_dl, sdl_data[namelen + addrlen]);
271 }
272 
273 struct sockaddr *
274 sockaddr_dl_alloc(uint16_t ifindex, uint8_t type,
275     const void *name, uint8_t namelen, const void *addr, uint8_t addrlen,
276     int flags)
277 {
278 	struct sockaddr *sa;
279 	socklen_t len;
280 
281 	len = sockaddr_dl_measure(namelen, addrlen);
282 	sa = sockaddr_alloc(AF_LINK, len, flags);
283 
284 	if (sa == NULL)
285 		return NULL;
286 
287 	if (sockaddr_dl_init(satosdl(sa), len, ifindex, type, name, namelen,
288 	    addr, addrlen) == NULL) {
289 		sockaddr_free(sa);
290 		return NULL;
291 	}
292 
293 	return sa;
294 }
295 
296 struct sockaddr_dl *
297 sockaddr_dl_init(struct sockaddr_dl *sdl, socklen_t socklen, uint16_t ifindex,
298     uint8_t type, const void *name, uint8_t namelen, const void *addr,
299     uint8_t addrlen)
300 {
301 	socklen_t len;
302 
303 	sdl->sdl_family = AF_LINK;
304 	sdl->sdl_slen = 0;
305 	len = sockaddr_dl_measure(namelen, addrlen);
306 	if (len > socklen) {
307 		sdl->sdl_len = socklen;
308 #ifdef DIAGNOSTIC
309 		printf("%s: too long: %" PRIu8 " > %" PRIu8 "\n", __func__, len,
310 		    socklen);
311 #endif
312 		return NULL;
313 	}
314 	sdl->sdl_len = len;
315 	sdl->sdl_index = ifindex;
316 	sdl->sdl_type = type;
317 	memset(&sdl->sdl_data[0], 0, namelen + addrlen);
318 	if (name != NULL) {
319 		memcpy(&sdl->sdl_data[0], name, namelen);
320 		sdl->sdl_nlen = namelen;
321 	} else
322 		sdl->sdl_nlen = 0;
323 	if (addr != NULL) {
324 		memcpy(&sdl->sdl_data[sdl->sdl_nlen], addr, addrlen);
325 		sdl->sdl_alen = addrlen;
326 	} else
327 		sdl->sdl_alen = 0;
328 	return sdl;
329 }
330 
331 static int
332 sockaddr_dl_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
333 {
334 	int rc;
335 	const uint_fast8_t indexofs = offsetof(struct sockaddr_dl, sdl_index);
336 	const uint_fast8_t nlenofs = offsetof(struct sockaddr_dl, sdl_nlen);
337 	uint_fast8_t dataofs = offsetof(struct sockaddr_dl, sdl_data[0]);
338 	const struct sockaddr_dl *sdl1, *sdl2;
339 
340 	sdl1 = satocsdl(sa1);
341 	sdl2 = satocsdl(sa2);
342 
343 	rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
344 	    indexofs, nlenofs);
345 
346 	if (rc != 0)
347 		return rc;
348 
349 	rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
350 	    dataofs, dataofs + MIN(sdl1->sdl_nlen, sdl2->sdl_nlen));
351 
352 	if (rc != 0)
353 		return rc;
354 
355 	if (sdl1->sdl_nlen != sdl2->sdl_nlen)
356 		return sdl1->sdl_nlen - sdl2->sdl_nlen;
357 
358 	dataofs += sdl1->sdl_nlen;
359 
360 	rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
361 	    dataofs, dataofs + MIN(sdl1->sdl_alen, sdl2->sdl_alen));
362 
363 	if (rc != 0)
364 		return rc;
365 
366 	if (sdl1->sdl_alen != sdl2->sdl_alen)
367 		return sdl1->sdl_alen - sdl2->sdl_alen;
368 
369 	dataofs += sdl1->sdl_alen;
370 
371 	rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
372 	    dataofs, dataofs + MIN(sdl1->sdl_slen, sdl2->sdl_slen));
373 
374 	if (sdl1->sdl_slen != sdl2->sdl_slen)
375 		return sdl1->sdl_slen - sdl2->sdl_slen;
376 
377 	return sdl1->sdl_len - sdl2->sdl_len;
378 }
379 
380 struct sockaddr_dl *
381 sockaddr_dl_setaddr(struct sockaddr_dl *sdl, socklen_t socklen,
382     const void *addr, uint8_t addrlen)
383 {
384 	socklen_t len;
385 
386 	len = sockaddr_dl_measure(sdl->sdl_nlen, addrlen);
387 	if (len > socklen) {
388 #ifdef DIAGNOSTIC
389 		printf("%s: too long: %" PRIu8 " > %" PRIu8 "\n", __func__, len,
390 		    socklen);
391 #endif
392 		return NULL;
393 	}
394 	memcpy(&sdl->sdl_data[sdl->sdl_nlen], addr, addrlen);
395 	sdl->sdl_alen = addrlen;
396 	sdl->sdl_len = len;
397 	return sdl;
398 }
399