Home | History | Annotate | Line # | Download | only in net
link_proto.c revision 1.1
      1 /*	$NetBSD: link_proto.c,v 1.1 2007/07/19 20:48:52 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.1 2007/07/19 20:48:52 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 
     49 static int sockaddr_dl_cmp(const struct sockaddr *, const struct sockaddr *);
     50 
     51 /*
     52  * Definitions of protocols supported in the link-layer domain.
     53  */
     54 
     55 DOMAIN_DEFINE(linkdomain);	/* forward define and add to link set */
     56 
     57 POOL_INIT(sockaddr_dl_pool, sizeof(struct sockaddr_dl), 0, 0, 0,
     58     "sockaddr_dl_pool", NULL, IPL_NET);
     59 
     60 struct domain linkdomain = {
     61 	.dom_family = AF_LINK,
     62 	.dom_name = "link",
     63 	.dom_externalize = NULL,
     64 	.dom_dispose = NULL,
     65 	.dom_protosw = NULL,
     66 	.dom_protoswNPROTOSW = NULL,
     67 	.dom_sa_pool = &sockaddr_dl_pool,
     68 	.dom_sa_len = sizeof(struct sockaddr_dl),
     69 	.dom_sockaddr_cmp = sockaddr_dl_cmp
     70 };
     71 
     72 #define	satocsdl(__sa)	((const struct sockaddr_dl *)(__sa))
     73 
     74 /* Compare the field at byte offsets [fieldstart, fieldend) in
     75  * two memory regions, [l, l + llen) and [r, r + llen).
     76  */
     77 static inline int
     78 submemcmp(const void *l, const void *r,
     79     const uint_fast8_t llen, const uint_fast8_t rlen,
     80     const uint_fast8_t fieldstart, const uint_fast8_t fieldend)
     81 {
     82 	uint_fast8_t cmpend, minlen;
     83 	const uint8_t *lb = l, *rb = r;
     84 	int rc;
     85 
     86 	minlen = MIN(llen, rlen);
     87 
     88 	/* The field is missing from one region.  The shorter region is the
     89 	 * lesser region.
     90 	 */
     91 	if (fieldstart >= minlen)
     92 		return llen - rlen;
     93 
     94 	/* Two empty, present fields are always equal. */
     95 	if (fieldstart > fieldend)
     96 		return 0;
     97 
     98 	cmpend = MIN(fieldend, minlen);
     99 
    100 	rc = memcmp(&lb[fieldstart], &rb[fieldstart], cmpend - fieldstart);
    101 
    102 	if (rc != 0)
    103 		return rc;
    104 	/* If one or both fields are truncated, then the shorter is the lesser
    105 	 * field.
    106 	 */
    107 	if (minlen < fieldend)
    108 		return llen - rlen;
    109 	/* Fields are full-length and equal.  The fields are equal. */
    110 	return 0;
    111 }
    112 
    113 static int
    114 sockaddr_dl_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
    115 {
    116 	int rc;
    117 	const uint_fast8_t indexofs = offsetof(struct sockaddr_dl, sdl_index);
    118 	const uint_fast8_t nlenofs = offsetof(struct sockaddr_dl, sdl_nlen);
    119 	uint_fast8_t dataofs = offsetof(struct sockaddr_dl, sdl_data[0]);
    120 	const struct sockaddr_dl *sdl1, *sdl2;
    121 
    122 	sdl1 = satocsdl(sa1);
    123 	sdl2 = satocsdl(sa2);
    124 
    125 	rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
    126 	    indexofs, nlenofs);
    127 
    128 	if (rc != 0)
    129 		return rc;
    130 
    131 	rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
    132 	    dataofs, dataofs + MIN(sdl1->sdl_nlen, sdl2->sdl_nlen));
    133 
    134 	if (rc != 0)
    135 		return rc;
    136 
    137 	if (sdl1->sdl_nlen != sdl2->sdl_nlen)
    138 		return sdl1->sdl_nlen - sdl2->sdl_nlen;
    139 
    140 	dataofs += sdl1->sdl_nlen;
    141 
    142 	rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
    143 	    dataofs, dataofs + MIN(sdl1->sdl_alen, sdl2->sdl_alen));
    144 
    145 	if (rc != 0)
    146 		return rc;
    147 
    148 	if (sdl1->sdl_alen != sdl2->sdl_alen)
    149 		return sdl1->sdl_alen - sdl2->sdl_alen;
    150 
    151 	dataofs += sdl1->sdl_alen;
    152 
    153 	rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
    154 	    dataofs, dataofs + MIN(sdl1->sdl_slen, sdl2->sdl_slen));
    155 
    156 	if (sdl1->sdl_slen != sdl2->sdl_slen)
    157 		return sdl1->sdl_slen - sdl2->sdl_slen;
    158 
    159 	return sdl1->sdl_len - sdl2->sdl_len;
    160 }
    161