link_proto.c revision 1.2 1 /* $NetBSD: link_proto.c,v 1.2 2007/08/07 04:06:20 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.2 2007/08/07 04:06:20 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 /* Compare the field at byte offsets [fieldstart, fieldend) in
73 * two memory regions, [l, l + llen) and [r, r + llen).
74 */
75 static inline int
76 submemcmp(const void *l, const void *r,
77 const uint_fast8_t llen, const uint_fast8_t rlen,
78 const uint_fast8_t fieldstart, const uint_fast8_t fieldend)
79 {
80 uint_fast8_t cmpend, minlen;
81 const uint8_t *lb = l, *rb = r;
82 int rc;
83
84 minlen = MIN(llen, rlen);
85
86 /* The field is missing from one region. The shorter region is the
87 * lesser region.
88 */
89 if (fieldstart >= minlen)
90 return llen - rlen;
91
92 /* Two empty, present fields are always equal. */
93 if (fieldstart > fieldend)
94 return 0;
95
96 cmpend = MIN(fieldend, minlen);
97
98 rc = memcmp(&lb[fieldstart], &rb[fieldstart], cmpend - fieldstart);
99
100 if (rc != 0)
101 return rc;
102 /* If one or both fields are truncated, then the shorter is the lesser
103 * field.
104 */
105 if (minlen < fieldend)
106 return llen - rlen;
107 /* Fields are full-length and equal. The fields are equal. */
108 return 0;
109 }
110
111 uint8_t
112 sockaddr_dl_measure(uint8_t namelen, uint8_t addrlen)
113 {
114 return offsetof(struct sockaddr_dl, sdl_data[namelen + addrlen]);
115 }
116
117 void
118 sockaddr_dl_init(struct sockaddr_dl *sdl, uint16_t ifindex, uint8_t type,
119 const void *name, uint8_t namelen, const void *addr, uint8_t addrlen)
120 {
121 sdl->sdl_family = AF_LINK;
122 sdl->sdl_slen = 0;
123 sdl->sdl_len = sockaddr_dl_measure(namelen, addrlen);
124 KASSERT(sdl->sdl_len <= sizeof(*sdl));
125 sdl->sdl_index = ifindex;
126 sdl->sdl_type = type;
127 memset(&sdl->sdl_data[0], 0, sizeof(sdl->sdl_data));
128 if (name != NULL)
129 memcpy(&sdl->sdl_data[0], name, namelen);
130 sdl->sdl_nlen = namelen;
131 if (addr != NULL)
132 memcpy(&sdl->sdl_data[sdl->sdl_nlen], addr, addrlen);
133 sdl->sdl_alen = addrlen;
134 }
135
136 static int
137 sockaddr_dl_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
138 {
139 int rc;
140 const uint_fast8_t indexofs = offsetof(struct sockaddr_dl, sdl_index);
141 const uint_fast8_t nlenofs = offsetof(struct sockaddr_dl, sdl_nlen);
142 uint_fast8_t dataofs = offsetof(struct sockaddr_dl, sdl_data[0]);
143 const struct sockaddr_dl *sdl1, *sdl2;
144
145 sdl1 = satocsdl(sa1);
146 sdl2 = satocsdl(sa2);
147
148 rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
149 indexofs, nlenofs);
150
151 if (rc != 0)
152 return rc;
153
154 rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
155 dataofs, dataofs + MIN(sdl1->sdl_nlen, sdl2->sdl_nlen));
156
157 if (rc != 0)
158 return rc;
159
160 if (sdl1->sdl_nlen != sdl2->sdl_nlen)
161 return sdl1->sdl_nlen - sdl2->sdl_nlen;
162
163 dataofs += sdl1->sdl_nlen;
164
165 rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
166 dataofs, dataofs + MIN(sdl1->sdl_alen, sdl2->sdl_alen));
167
168 if (rc != 0)
169 return rc;
170
171 if (sdl1->sdl_alen != sdl2->sdl_alen)
172 return sdl1->sdl_alen - sdl2->sdl_alen;
173
174 dataofs += sdl1->sdl_alen;
175
176 rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
177 dataofs, dataofs + MIN(sdl1->sdl_slen, sdl2->sdl_slen));
178
179 if (sdl1->sdl_slen != sdl2->sdl_slen)
180 return sdl1->sdl_slen - sdl2->sdl_slen;
181
182 return sdl1->sdl_len - sdl2->sdl_len;
183 }
184
185 struct sockaddr_dl *
186 sockaddr_dl_setaddr(struct sockaddr_dl *sdl, const void *addr, uint8_t addrlen)
187 {
188 uint_fast8_t endofs;
189
190 endofs =
191 offsetof(struct sockaddr_dl, sdl_data[sdl->sdl_nlen + addrlen]);
192
193 if (endofs > sizeof(struct sockaddr_dl)) {
194 printf("%s: too long: %" PRIu8 " bytes\n", __func__, addrlen);
195 sdl->sdl_alen = 0;
196 return NULL;
197 }
198 memcpy(&sdl->sdl_data[sdl->sdl_nlen], addr, addrlen);
199 sdl->sdl_alen = addrlen;
200 return sdl;
201 }
202