link_proto.c revision 1.3 1 /* $NetBSD: link_proto.c,v 1.3 2007/08/30 02:17:35 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.3 2007/08/30 02:17:35 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 struct domain linkdomain = {
58 .dom_family = AF_LINK,
59 .dom_name = "link",
60 .dom_externalize = NULL,
61 .dom_dispose = NULL,
62 .dom_protosw = NULL,
63 .dom_protoswNPROTOSW = NULL,
64 .dom_sockaddr_cmp = sockaddr_dl_cmp
65 };
66
67 /* Compare the field at byte offsets [fieldstart, fieldend) in
68 * two memory regions, [l, l + llen) and [r, r + llen).
69 */
70 static inline int
71 submemcmp(const void *l, const void *r,
72 const uint_fast8_t llen, const uint_fast8_t rlen,
73 const uint_fast8_t fieldstart, const uint_fast8_t fieldend)
74 {
75 uint_fast8_t cmpend, minlen;
76 const uint8_t *lb = l, *rb = r;
77 int rc;
78
79 minlen = MIN(llen, rlen);
80
81 /* The field is missing from one region. The shorter region is the
82 * lesser region.
83 */
84 if (fieldstart >= minlen)
85 return llen - rlen;
86
87 /* Two empty, present fields are always equal. */
88 if (fieldstart > fieldend)
89 return 0;
90
91 cmpend = MIN(fieldend, minlen);
92
93 rc = memcmp(&lb[fieldstart], &rb[fieldstart], cmpend - fieldstart);
94
95 if (rc != 0)
96 return rc;
97 /* If one or both fields are truncated, then the shorter is the lesser
98 * field.
99 */
100 if (minlen < fieldend)
101 return llen - rlen;
102 /* Fields are full-length and equal. The fields are equal. */
103 return 0;
104 }
105
106 uint8_t
107 sockaddr_dl_measure(uint8_t namelen, uint8_t addrlen)
108 {
109 return offsetof(struct sockaddr_dl, sdl_data[namelen + addrlen]);
110 }
111
112 struct sockaddr *
113 sockaddr_dl_alloc(uint16_t ifindex, uint8_t type,
114 const void *name, uint8_t namelen, const void *addr, uint8_t addrlen,
115 int flags)
116 {
117 struct sockaddr *sa;
118 socklen_t len;
119
120 len = sockaddr_dl_measure(namelen, addrlen);
121 sa = sockaddr_alloc(AF_LINK, len, flags);
122
123 if (sa == NULL)
124 return NULL;
125
126 if (sockaddr_dl_init(satosdl(sa), len, ifindex, type, name, namelen,
127 addr, addrlen) == NULL) {
128 sockaddr_free(sa);
129 return NULL;
130 }
131
132 return sa;
133 }
134
135 struct sockaddr_dl *
136 sockaddr_dl_init(struct sockaddr_dl *sdl, socklen_t socklen, uint16_t ifindex,
137 uint8_t type, const void *name, uint8_t namelen, const void *addr,
138 uint8_t addrlen)
139 {
140 socklen_t len;
141
142 sdl->sdl_family = AF_LINK;
143 sdl->sdl_slen = 0;
144 len = sockaddr_dl_measure(namelen, addrlen);
145 if (len > socklen) {
146 sdl->sdl_len = socklen;
147 #ifdef DIAGNOSTIC
148 printf("%s: too long: %" PRIu8 " > %" PRIu8 "\n", __func__, len,
149 socklen);
150 #endif
151 return NULL;
152 }
153 sdl->sdl_len = len;
154 sdl->sdl_index = ifindex;
155 sdl->sdl_type = type;
156 memset(&sdl->sdl_data[0], 0, namelen + addrlen);
157 if (name != NULL) {
158 memcpy(&sdl->sdl_data[0], name, namelen);
159 sdl->sdl_nlen = namelen;
160 } else
161 sdl->sdl_nlen = 0;
162 if (addr != NULL) {
163 memcpy(&sdl->sdl_data[sdl->sdl_nlen], addr, addrlen);
164 sdl->sdl_alen = addrlen;
165 } else
166 sdl->sdl_alen = 0;
167 return sdl;
168 }
169
170 static int
171 sockaddr_dl_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
172 {
173 int rc;
174 const uint_fast8_t indexofs = offsetof(struct sockaddr_dl, sdl_index);
175 const uint_fast8_t nlenofs = offsetof(struct sockaddr_dl, sdl_nlen);
176 uint_fast8_t dataofs = offsetof(struct sockaddr_dl, sdl_data[0]);
177 const struct sockaddr_dl *sdl1, *sdl2;
178
179 sdl1 = satocsdl(sa1);
180 sdl2 = satocsdl(sa2);
181
182 rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
183 indexofs, nlenofs);
184
185 if (rc != 0)
186 return rc;
187
188 rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
189 dataofs, dataofs + MIN(sdl1->sdl_nlen, sdl2->sdl_nlen));
190
191 if (rc != 0)
192 return rc;
193
194 if (sdl1->sdl_nlen != sdl2->sdl_nlen)
195 return sdl1->sdl_nlen - sdl2->sdl_nlen;
196
197 dataofs += sdl1->sdl_nlen;
198
199 rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
200 dataofs, dataofs + MIN(sdl1->sdl_alen, sdl2->sdl_alen));
201
202 if (rc != 0)
203 return rc;
204
205 if (sdl1->sdl_alen != sdl2->sdl_alen)
206 return sdl1->sdl_alen - sdl2->sdl_alen;
207
208 dataofs += sdl1->sdl_alen;
209
210 rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
211 dataofs, dataofs + MIN(sdl1->sdl_slen, sdl2->sdl_slen));
212
213 if (sdl1->sdl_slen != sdl2->sdl_slen)
214 return sdl1->sdl_slen - sdl2->sdl_slen;
215
216 return sdl1->sdl_len - sdl2->sdl_len;
217 }
218
219 struct sockaddr_dl *
220 sockaddr_dl_setaddr(struct sockaddr_dl *sdl, socklen_t socklen,
221 const void *addr, uint8_t addrlen)
222 {
223 socklen_t len;
224
225 len = sockaddr_dl_measure(sdl->sdl_nlen, addrlen);
226 if (len > socklen) {
227 #ifdef DIAGNOSTIC
228 printf("%s: too long: %" PRIu8 " > %" PRIu8 "\n", __func__, len,
229 socklen);
230 #endif
231 return NULL;
232 }
233 memcpy(&sdl->sdl_data[sdl->sdl_nlen], addr, addrlen);
234 sdl->sdl_alen = addrlen;
235 sdl->sdl_len = len;
236 return sdl;
237 }
238