Home | History | Annotate | Line # | Download | only in net
link_proto.c revision 1.9
      1 /*	$NetBSD: link_proto.c,v 1.9 2014/05/19 02:51:24 rmind 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.9 2014/05/19 02:51:24 rmind 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_attach(struct socket *, int);
     52 static void link_detach(struct socket *);
     53 static int link_usrreq(struct socket *, int, struct mbuf *, struct mbuf *,
     54     struct mbuf *, struct lwp *);
     55 static void link_init(void);
     56 
     57 /*
     58  * Definitions of protocols supported in the link-layer domain.
     59  */
     60 
     61 DOMAIN_DEFINE(linkdomain);	/* forward define and add to link set */
     62 
     63 static const struct pr_usrreqs link_usrreqs = {
     64 	.pr_attach	= link_attach,
     65 	.pr_detach	= link_detach,
     66 	.pr_generic	= link_usrreq,
     67 };
     68 
     69 const struct protosw linksw[] = {
     70 	{	.pr_type = SOCK_DGRAM,
     71 		.pr_domain = &linkdomain,
     72 		.pr_protocol = 0,	/* XXX */
     73 		.pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
     74 		.pr_input = NULL,
     75 		.pr_ctlinput = NULL,
     76 		.pr_ctloutput = NULL,
     77 		.pr_usrreqs = &link_usrreqs,
     78 		.pr_init = link_init,
     79 	},
     80 };
     81 
     82 struct domain linkdomain = {
     83 	.dom_family = AF_LINK,
     84 	.dom_name = "link",
     85 	.dom_externalize = NULL,
     86 	.dom_dispose = NULL,
     87 	.dom_protosw = linksw,
     88 	.dom_protoswNPROTOSW = &linksw[__arraycount(linksw)],
     89 	.dom_sockaddr_cmp = sockaddr_dl_cmp
     90 };
     91 
     92 static void
     93 link_init(void)
     94 {
     95 	return;
     96 }
     97 
     98 static int
     99 link_control(struct socket *so, unsigned long cmd, void *data,
    100     struct ifnet *ifp, struct lwp *l)
    101 {
    102 	int error, s;
    103 	bool isactive, mkactive;
    104 	struct if_laddrreq *iflr;
    105 	union {
    106 		struct sockaddr sa;
    107 		struct sockaddr_dl sdl;
    108 		struct sockaddr_storage ss;
    109 	} u;
    110 	struct ifaddr *ifa;
    111 	const struct sockaddr_dl *asdl, *nsdl;
    112 
    113 	switch (cmd) {
    114 	case SIOCALIFADDR:
    115 	case SIOCDLIFADDR:
    116 	case SIOCGLIFADDR:
    117 		iflr = data;
    118 
    119 		if (iflr->addr.ss_family != AF_LINK)
    120 			return EINVAL;
    121 
    122 		asdl = satocsdl(sstocsa(&iflr->addr));
    123 
    124 		if (asdl->sdl_alen != ifp->if_addrlen)
    125 			return EINVAL;
    126 
    127 		if (sockaddr_dl_init(&u.sdl, sizeof(u.ss), ifp->if_index,
    128 		    ifp->if_type, ifp->if_xname, strlen(ifp->if_xname),
    129 		    CLLADDR(asdl), asdl->sdl_alen) == NULL)
    130 			return EINVAL;
    131 
    132 		if ((iflr->flags & IFLR_PREFIX) == 0)
    133 			;
    134 		else if (iflr->prefixlen != NBBY * ifp->if_addrlen)
    135 			return EINVAL;	/* XXX match with prefix */
    136 
    137 		error = 0;
    138 
    139 		s = splnet();
    140 
    141 		IFADDR_FOREACH(ifa, ifp) {
    142 			if (sockaddr_cmp(&u.sa, ifa->ifa_addr) == 0)
    143 				break;
    144 		}
    145 
    146 		switch (cmd) {
    147 		case SIOCGLIFADDR:
    148 			if ((iflr->flags & IFLR_PREFIX) == 0) {
    149 				IFADDR_FOREACH(ifa, ifp) {
    150 					if (ifa->ifa_addr->sa_family == AF_LINK)
    151 						break;
    152 				}
    153 			}
    154 			if (ifa == NULL) {
    155 				error = EADDRNOTAVAIL;
    156 				break;
    157 			}
    158 
    159 			if (ifa == ifp->if_dl)
    160 				iflr->flags = IFLR_ACTIVE;
    161 			else
    162 				iflr->flags = 0;
    163 
    164 			if (ifa == ifp->if_hwdl)
    165 				iflr->flags |= IFLR_FACTORY;
    166 
    167 			sockaddr_copy(sstosa(&iflr->addr), sizeof(iflr->addr),
    168 			    ifa->ifa_addr);
    169 
    170 			break;
    171 		case SIOCDLIFADDR:
    172 			if (ifa == NULL)
    173 				error = EADDRNOTAVAIL;
    174 			else if (ifa == ifp->if_dl || ifa == ifp->if_hwdl)
    175 				error = EBUSY;
    176 			else {
    177 				/* TBD routing socket */
    178 				rt_newaddrmsg(RTM_DELETE, ifa, 0, NULL);
    179 				ifa_remove(ifp, ifa);
    180 			}
    181 			break;
    182 		case SIOCALIFADDR:
    183 			if (ifa != NULL)
    184 				;
    185 			else if ((ifa = if_dl_create(ifp, &nsdl)) == NULL) {
    186 				error = ENOMEM;
    187 				break;
    188 			} else {
    189 				sockaddr_copy(ifa->ifa_addr,
    190 				    ifa->ifa_addr->sa_len, &u.sa);
    191 				ifa_insert(ifp, ifa);
    192 				rt_newaddrmsg(RTM_ADD, ifa, 0, NULL);
    193 			}
    194 
    195 			mkactive = (iflr->flags & IFLR_ACTIVE) != 0;
    196 			isactive = (ifa == ifp->if_dl);
    197 
    198 			if (!isactive && mkactive) {
    199 				if_activate_sadl(ifp, ifa, nsdl);
    200 				rt_newaddrmsg(RTM_CHANGE, ifa, 0, NULL);
    201 				error = ENETRESET;
    202 			}
    203 			break;
    204 		}
    205 		splx(s);
    206 		if (error != ENETRESET)
    207 			return error;
    208 		else if ((ifp->if_flags & IFF_RUNNING) != 0)
    209 			return (*ifp->if_init)(ifp);
    210 		else
    211 			return 0;
    212 	default:
    213 		return ENOTTY;
    214 	}
    215 }
    216 
    217 static int
    218 link_attach(struct socket *so, int proto)
    219 {
    220 	sosetlock(so);
    221 	KASSERT(solocked(so));
    222 	return 0;
    223 }
    224 
    225 static void
    226 link_detach(struct socket *so)
    227 {
    228 	KASSERT(solocked(so));
    229 	sofree(so);
    230 }
    231 
    232 static int
    233 link_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
    234 	struct mbuf *control, struct lwp *l)
    235 {
    236 	KASSERT(req != PRU_ATTACH);
    237 	KASSERT(req != PRU_DETACH);
    238 
    239 	switch (req) {
    240 	case PRU_CONTROL:
    241 		return link_control(so, (unsigned long)m, nam,
    242 		    (struct ifnet *)control, l);
    243 	default:
    244 		return EOPNOTSUPP;
    245 	}
    246 }
    247 
    248 /* Compare the field at byte offsets [fieldstart, fieldend) in
    249  * two memory regions, [l, l + llen) and [r, r + llen).
    250  */
    251 static inline int
    252 submemcmp(const void *l, const void *r,
    253     const uint_fast8_t llen, const uint_fast8_t rlen,
    254     const uint_fast8_t fieldstart, const uint_fast8_t fieldend)
    255 {
    256 	uint_fast8_t cmpend, minlen;
    257 	const uint8_t *lb = l, *rb = r;
    258 	int rc;
    259 
    260 	minlen = MIN(llen, rlen);
    261 
    262 	/* The field is missing from one region.  The shorter region is the
    263 	 * lesser region.
    264 	 */
    265 	if (fieldstart >= minlen)
    266 		return llen - rlen;
    267 
    268 	/* Two empty, present fields are always equal. */
    269 	if (fieldstart > fieldend)
    270 		return 0;
    271 
    272 	cmpend = MIN(fieldend, minlen);
    273 
    274 	rc = memcmp(&lb[fieldstart], &rb[fieldstart], cmpend - fieldstart);
    275 
    276 	if (rc != 0)
    277 		return rc;
    278 	/* If one or both fields are truncated, then the shorter is the lesser
    279 	 * field.
    280 	 */
    281 	if (minlen < fieldend)
    282 		return llen - rlen;
    283 	/* Fields are full-length and equal.  The fields are equal. */
    284 	return 0;
    285 }
    286 
    287 uint8_t
    288 sockaddr_dl_measure(uint8_t namelen, uint8_t addrlen)
    289 {
    290 	return offsetof(struct sockaddr_dl, sdl_data[namelen + addrlen]);
    291 }
    292 
    293 struct sockaddr *
    294 sockaddr_dl_alloc(uint16_t ifindex, uint8_t type,
    295     const void *name, uint8_t namelen, const void *addr, uint8_t addrlen,
    296     int flags)
    297 {
    298 	struct sockaddr *sa;
    299 	socklen_t len;
    300 
    301 	len = sockaddr_dl_measure(namelen, addrlen);
    302 	sa = sockaddr_alloc(AF_LINK, len, flags);
    303 
    304 	if (sa == NULL)
    305 		return NULL;
    306 
    307 	if (sockaddr_dl_init(satosdl(sa), len, ifindex, type, name, namelen,
    308 	    addr, addrlen) == NULL) {
    309 		sockaddr_free(sa);
    310 		return NULL;
    311 	}
    312 
    313 	return sa;
    314 }
    315 
    316 struct sockaddr_dl *
    317 sockaddr_dl_init(struct sockaddr_dl *sdl, socklen_t socklen, uint16_t ifindex,
    318     uint8_t type, const void *name, uint8_t namelen, const void *addr,
    319     uint8_t addrlen)
    320 {
    321 	socklen_t len;
    322 
    323 	sdl->sdl_family = AF_LINK;
    324 	sdl->sdl_slen = 0;
    325 	len = sockaddr_dl_measure(namelen, addrlen);
    326 	if (len > socklen) {
    327 		sdl->sdl_len = socklen;
    328 #ifdef DIAGNOSTIC
    329 		printf("%s: too long: %" PRIu8 " > %" PRIu8 "\n", __func__, len,
    330 		    socklen);
    331 #endif
    332 		return NULL;
    333 	}
    334 	sdl->sdl_len = len;
    335 	sdl->sdl_index = ifindex;
    336 	sdl->sdl_type = type;
    337 	memset(&sdl->sdl_data[0], 0, namelen + addrlen);
    338 	if (name != NULL) {
    339 		memcpy(&sdl->sdl_data[0], name, namelen);
    340 		sdl->sdl_nlen = namelen;
    341 	} else
    342 		sdl->sdl_nlen = 0;
    343 	if (addr != NULL) {
    344 		memcpy(&sdl->sdl_data[sdl->sdl_nlen], addr, addrlen);
    345 		sdl->sdl_alen = addrlen;
    346 	} else
    347 		sdl->sdl_alen = 0;
    348 	return sdl;
    349 }
    350 
    351 static int
    352 sockaddr_dl_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
    353 {
    354 	int rc;
    355 	const uint_fast8_t indexofs = offsetof(struct sockaddr_dl, sdl_index);
    356 	const uint_fast8_t nlenofs = offsetof(struct sockaddr_dl, sdl_nlen);
    357 	uint_fast8_t dataofs = offsetof(struct sockaddr_dl, sdl_data[0]);
    358 	const struct sockaddr_dl *sdl1, *sdl2;
    359 
    360 	sdl1 = satocsdl(sa1);
    361 	sdl2 = satocsdl(sa2);
    362 
    363 	rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
    364 	    indexofs, nlenofs);
    365 
    366 	if (rc != 0)
    367 		return rc;
    368 
    369 	rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
    370 	    dataofs, dataofs + MIN(sdl1->sdl_nlen, sdl2->sdl_nlen));
    371 
    372 	if (rc != 0)
    373 		return rc;
    374 
    375 	if (sdl1->sdl_nlen != sdl2->sdl_nlen)
    376 		return sdl1->sdl_nlen - sdl2->sdl_nlen;
    377 
    378 	dataofs += sdl1->sdl_nlen;
    379 
    380 	rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
    381 	    dataofs, dataofs + MIN(sdl1->sdl_alen, sdl2->sdl_alen));
    382 
    383 	if (rc != 0)
    384 		return rc;
    385 
    386 	if (sdl1->sdl_alen != sdl2->sdl_alen)
    387 		return sdl1->sdl_alen - sdl2->sdl_alen;
    388 
    389 	dataofs += sdl1->sdl_alen;
    390 
    391 	rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
    392 	    dataofs, dataofs + MIN(sdl1->sdl_slen, sdl2->sdl_slen));
    393 
    394 	if (sdl1->sdl_slen != sdl2->sdl_slen)
    395 		return sdl1->sdl_slen - sdl2->sdl_slen;
    396 
    397 	return sdl1->sdl_len - sdl2->sdl_len;
    398 }
    399 
    400 struct sockaddr_dl *
    401 sockaddr_dl_setaddr(struct sockaddr_dl *sdl, socklen_t socklen,
    402     const void *addr, uint8_t addrlen)
    403 {
    404 	socklen_t len;
    405 
    406 	len = sockaddr_dl_measure(sdl->sdl_nlen, addrlen);
    407 	if (len > socklen) {
    408 #ifdef DIAGNOSTIC
    409 		printf("%s: too long: %" PRIu8 " > %" PRIu8 "\n", __func__, len,
    410 		    socklen);
    411 #endif
    412 		return NULL;
    413 	}
    414 	memcpy(&sdl->sdl_data[sdl->sdl_nlen], addr, addrlen);
    415 	sdl->sdl_alen = addrlen;
    416 	sdl->sdl_len = len;
    417 	return sdl;
    418 }
    419