Home | History | Annotate | Line # | Download | only in net
link_proto.c revision 1.14
      1 /*	$NetBSD: link_proto.c,v 1.14 2014/07/07 17:13:56 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.14 2014/07/07 17:13:56 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 	KASSERT(solocked(so));
    246 
    247 	return EOPNOTSUPP;
    248 }
    249 
    250 static int
    251 link_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
    252 	struct mbuf *control, struct lwp *l)
    253 {
    254 	KASSERT(req != PRU_ATTACH);
    255 	KASSERT(req != PRU_DETACH);
    256 	KASSERT(req != PRU_CONTROL);
    257 	KASSERT(req != PRU_SENSE);
    258 
    259 	return EOPNOTSUPP;
    260 }
    261 
    262 /* Compare the field at byte offsets [fieldstart, fieldend) in
    263  * two memory regions, [l, l + llen) and [r, r + llen).
    264  */
    265 static inline int
    266 submemcmp(const void *l, const void *r,
    267     const uint_fast8_t llen, const uint_fast8_t rlen,
    268     const uint_fast8_t fieldstart, const uint_fast8_t fieldend)
    269 {
    270 	uint_fast8_t cmpend, minlen;
    271 	const uint8_t *lb = l, *rb = r;
    272 	int rc;
    273 
    274 	minlen = MIN(llen, rlen);
    275 
    276 	/* The field is missing from one region.  The shorter region is the
    277 	 * lesser region.
    278 	 */
    279 	if (fieldstart >= minlen)
    280 		return llen - rlen;
    281 
    282 	/* Two empty, present fields are always equal. */
    283 	if (fieldstart > fieldend)
    284 		return 0;
    285 
    286 	cmpend = MIN(fieldend, minlen);
    287 
    288 	rc = memcmp(&lb[fieldstart], &rb[fieldstart], cmpend - fieldstart);
    289 
    290 	if (rc != 0)
    291 		return rc;
    292 	/* If one or both fields are truncated, then the shorter is the lesser
    293 	 * field.
    294 	 */
    295 	if (minlen < fieldend)
    296 		return llen - rlen;
    297 	/* Fields are full-length and equal.  The fields are equal. */
    298 	return 0;
    299 }
    300 
    301 uint8_t
    302 sockaddr_dl_measure(uint8_t namelen, uint8_t addrlen)
    303 {
    304 	return offsetof(struct sockaddr_dl, sdl_data[namelen + addrlen]);
    305 }
    306 
    307 struct sockaddr *
    308 sockaddr_dl_alloc(uint16_t ifindex, uint8_t type,
    309     const void *name, uint8_t namelen, const void *addr, uint8_t addrlen,
    310     int flags)
    311 {
    312 	struct sockaddr *sa;
    313 	socklen_t len;
    314 
    315 	len = sockaddr_dl_measure(namelen, addrlen);
    316 	sa = sockaddr_alloc(AF_LINK, len, flags);
    317 
    318 	if (sa == NULL)
    319 		return NULL;
    320 
    321 	if (sockaddr_dl_init(satosdl(sa), len, ifindex, type, name, namelen,
    322 	    addr, addrlen) == NULL) {
    323 		sockaddr_free(sa);
    324 		return NULL;
    325 	}
    326 
    327 	return sa;
    328 }
    329 
    330 struct sockaddr_dl *
    331 sockaddr_dl_init(struct sockaddr_dl *sdl, socklen_t socklen, uint16_t ifindex,
    332     uint8_t type, const void *name, uint8_t namelen, const void *addr,
    333     uint8_t addrlen)
    334 {
    335 	socklen_t len;
    336 
    337 	sdl->sdl_family = AF_LINK;
    338 	sdl->sdl_slen = 0;
    339 	len = sockaddr_dl_measure(namelen, addrlen);
    340 	if (len > socklen) {
    341 		sdl->sdl_len = socklen;
    342 #ifdef DIAGNOSTIC
    343 		printf("%s: too long: %" PRIu8 " > %" PRIu8 "\n", __func__, len,
    344 		    socklen);
    345 #endif
    346 		return NULL;
    347 	}
    348 	sdl->sdl_len = len;
    349 	sdl->sdl_index = ifindex;
    350 	sdl->sdl_type = type;
    351 	memset(&sdl->sdl_data[0], 0, namelen + addrlen);
    352 	if (name != NULL) {
    353 		memcpy(&sdl->sdl_data[0], name, namelen);
    354 		sdl->sdl_nlen = namelen;
    355 	} else
    356 		sdl->sdl_nlen = 0;
    357 	if (addr != NULL) {
    358 		memcpy(&sdl->sdl_data[sdl->sdl_nlen], addr, addrlen);
    359 		sdl->sdl_alen = addrlen;
    360 	} else
    361 		sdl->sdl_alen = 0;
    362 	return sdl;
    363 }
    364 
    365 static int
    366 sockaddr_dl_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
    367 {
    368 	int rc;
    369 	const uint_fast8_t indexofs = offsetof(struct sockaddr_dl, sdl_index);
    370 	const uint_fast8_t nlenofs = offsetof(struct sockaddr_dl, sdl_nlen);
    371 	uint_fast8_t dataofs = offsetof(struct sockaddr_dl, sdl_data[0]);
    372 	const struct sockaddr_dl *sdl1, *sdl2;
    373 
    374 	sdl1 = satocsdl(sa1);
    375 	sdl2 = satocsdl(sa2);
    376 
    377 	rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
    378 	    indexofs, nlenofs);
    379 
    380 	if (rc != 0)
    381 		return rc;
    382 
    383 	rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
    384 	    dataofs, dataofs + MIN(sdl1->sdl_nlen, sdl2->sdl_nlen));
    385 
    386 	if (rc != 0)
    387 		return rc;
    388 
    389 	if (sdl1->sdl_nlen != sdl2->sdl_nlen)
    390 		return sdl1->sdl_nlen - sdl2->sdl_nlen;
    391 
    392 	dataofs += sdl1->sdl_nlen;
    393 
    394 	rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
    395 	    dataofs, dataofs + MIN(sdl1->sdl_alen, sdl2->sdl_alen));
    396 
    397 	if (rc != 0)
    398 		return rc;
    399 
    400 	if (sdl1->sdl_alen != sdl2->sdl_alen)
    401 		return sdl1->sdl_alen - sdl2->sdl_alen;
    402 
    403 	dataofs += sdl1->sdl_alen;
    404 
    405 	rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
    406 	    dataofs, dataofs + MIN(sdl1->sdl_slen, sdl2->sdl_slen));
    407 
    408 	if (sdl1->sdl_slen != sdl2->sdl_slen)
    409 		return sdl1->sdl_slen - sdl2->sdl_slen;
    410 
    411 	return sdl1->sdl_len - sdl2->sdl_len;
    412 }
    413 
    414 struct sockaddr_dl *
    415 sockaddr_dl_setaddr(struct sockaddr_dl *sdl, socklen_t socklen,
    416     const void *addr, uint8_t addrlen)
    417 {
    418 	socklen_t len;
    419 
    420 	len = sockaddr_dl_measure(sdl->sdl_nlen, addrlen);
    421 	if (len > socklen) {
    422 #ifdef DIAGNOSTIC
    423 		printf("%s: too long: %" PRIu8 " > %" PRIu8 "\n", __func__, len,
    424 		    socklen);
    425 #endif
    426 		return NULL;
    427 	}
    428 	memcpy(&sdl->sdl_data[sdl->sdl_nlen], addr, addrlen);
    429 	sdl->sdl_alen = addrlen;
    430 	sdl->sdl_len = len;
    431 	return sdl;
    432 }
    433