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