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