link_proto.c revision 1.8 1 /* $NetBSD: link_proto.c,v 1.8 2014/05/18 14:46:16 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.8 2014/05/18 14:46:16 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_usrreq(struct socket *, int, struct mbuf *, struct mbuf *,
52 struct mbuf *, struct lwp *);
53 static void link_init(void);
54
55 /*
56 * Definitions of protocols supported in the link-layer domain.
57 */
58
59 DOMAIN_DEFINE(linkdomain); /* forward define and add to link set */
60
61 static const struct pr_usrreqs link_usrreqs = {
62 .pr_generic = link_usrreq,
63 };
64
65 const struct protosw linksw[] = {
66 { .pr_type = SOCK_DGRAM,
67 .pr_domain = &linkdomain,
68 .pr_protocol = 0, /* XXX */
69 .pr_flags = PR_ATOMIC|PR_ADDR|PR_PURGEIF,
70 .pr_input = NULL,
71 .pr_ctlinput = NULL,
72 .pr_ctloutput = NULL,
73 .pr_usrreqs = &link_usrreqs,
74 .pr_init = link_init,
75 },
76 };
77
78 struct domain linkdomain = {
79 .dom_family = AF_LINK,
80 .dom_name = "link",
81 .dom_externalize = NULL,
82 .dom_dispose = NULL,
83 .dom_protosw = linksw,
84 .dom_protoswNPROTOSW = &linksw[__arraycount(linksw)],
85 .dom_sockaddr_cmp = sockaddr_dl_cmp
86 };
87
88 static void
89 link_init(void)
90 {
91 return;
92 }
93
94 static int
95 link_control(struct socket *so, unsigned long cmd, void *data,
96 struct ifnet *ifp, struct lwp *l)
97 {
98 int error, s;
99 bool isactive, mkactive;
100 struct if_laddrreq *iflr;
101 union {
102 struct sockaddr sa;
103 struct sockaddr_dl sdl;
104 struct sockaddr_storage ss;
105 } u;
106 struct ifaddr *ifa;
107 const struct sockaddr_dl *asdl, *nsdl;
108
109 switch (cmd) {
110 case SIOCALIFADDR:
111 case SIOCDLIFADDR:
112 case SIOCGLIFADDR:
113 iflr = data;
114
115 if (iflr->addr.ss_family != AF_LINK)
116 return EINVAL;
117
118 asdl = satocsdl(sstocsa(&iflr->addr));
119
120 if (asdl->sdl_alen != ifp->if_addrlen)
121 return EINVAL;
122
123 if (sockaddr_dl_init(&u.sdl, sizeof(u.ss), ifp->if_index,
124 ifp->if_type, ifp->if_xname, strlen(ifp->if_xname),
125 CLLADDR(asdl), asdl->sdl_alen) == NULL)
126 return EINVAL;
127
128 if ((iflr->flags & IFLR_PREFIX) == 0)
129 ;
130 else if (iflr->prefixlen != NBBY * ifp->if_addrlen)
131 return EINVAL; /* XXX match with prefix */
132
133 error = 0;
134
135 s = splnet();
136
137 IFADDR_FOREACH(ifa, ifp) {
138 if (sockaddr_cmp(&u.sa, ifa->ifa_addr) == 0)
139 break;
140 }
141
142 switch (cmd) {
143 case SIOCGLIFADDR:
144 if ((iflr->flags & IFLR_PREFIX) == 0) {
145 IFADDR_FOREACH(ifa, ifp) {
146 if (ifa->ifa_addr->sa_family == AF_LINK)
147 break;
148 }
149 }
150 if (ifa == NULL) {
151 error = EADDRNOTAVAIL;
152 break;
153 }
154
155 if (ifa == ifp->if_dl)
156 iflr->flags = IFLR_ACTIVE;
157 else
158 iflr->flags = 0;
159
160 if (ifa == ifp->if_hwdl)
161 iflr->flags |= IFLR_FACTORY;
162
163 sockaddr_copy(sstosa(&iflr->addr), sizeof(iflr->addr),
164 ifa->ifa_addr);
165
166 break;
167 case SIOCDLIFADDR:
168 if (ifa == NULL)
169 error = EADDRNOTAVAIL;
170 else if (ifa == ifp->if_dl || ifa == ifp->if_hwdl)
171 error = EBUSY;
172 else {
173 /* TBD routing socket */
174 rt_newaddrmsg(RTM_DELETE, ifa, 0, NULL);
175 ifa_remove(ifp, ifa);
176 }
177 break;
178 case SIOCALIFADDR:
179 if (ifa != NULL)
180 ;
181 else if ((ifa = if_dl_create(ifp, &nsdl)) == NULL) {
182 error = ENOMEM;
183 break;
184 } else {
185 sockaddr_copy(ifa->ifa_addr,
186 ifa->ifa_addr->sa_len, &u.sa);
187 ifa_insert(ifp, ifa);
188 rt_newaddrmsg(RTM_ADD, ifa, 0, NULL);
189 }
190
191 mkactive = (iflr->flags & IFLR_ACTIVE) != 0;
192 isactive = (ifa == ifp->if_dl);
193
194 if (!isactive && mkactive) {
195 if_activate_sadl(ifp, ifa, nsdl);
196 rt_newaddrmsg(RTM_CHANGE, ifa, 0, NULL);
197 error = ENETRESET;
198 }
199 break;
200 }
201 splx(s);
202 if (error != ENETRESET)
203 return error;
204 else if ((ifp->if_flags & IFF_RUNNING) != 0)
205 return (*ifp->if_init)(ifp);
206 else
207 return 0;
208 default:
209 return ENOTTY;
210 }
211 }
212
213 static int
214 link_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
215 struct mbuf *control, struct lwp *l)
216 {
217 switch (req) {
218 case PRU_ATTACH:
219 sosetlock(so);
220 return 0;
221 case PRU_DETACH:
222 sofree(so);
223 return 0;
224 case PRU_CONTROL:
225 return link_control(so, (unsigned long)m, nam,
226 (struct ifnet *)control, l);
227 default:
228 return EOPNOTSUPP;
229 }
230 }
231
232 /* Compare the field at byte offsets [fieldstart, fieldend) in
233 * two memory regions, [l, l + llen) and [r, r + llen).
234 */
235 static inline int
236 submemcmp(const void *l, const void *r,
237 const uint_fast8_t llen, const uint_fast8_t rlen,
238 const uint_fast8_t fieldstart, const uint_fast8_t fieldend)
239 {
240 uint_fast8_t cmpend, minlen;
241 const uint8_t *lb = l, *rb = r;
242 int rc;
243
244 minlen = MIN(llen, rlen);
245
246 /* The field is missing from one region. The shorter region is the
247 * lesser region.
248 */
249 if (fieldstart >= minlen)
250 return llen - rlen;
251
252 /* Two empty, present fields are always equal. */
253 if (fieldstart > fieldend)
254 return 0;
255
256 cmpend = MIN(fieldend, minlen);
257
258 rc = memcmp(&lb[fieldstart], &rb[fieldstart], cmpend - fieldstart);
259
260 if (rc != 0)
261 return rc;
262 /* If one or both fields are truncated, then the shorter is the lesser
263 * field.
264 */
265 if (minlen < fieldend)
266 return llen - rlen;
267 /* Fields are full-length and equal. The fields are equal. */
268 return 0;
269 }
270
271 uint8_t
272 sockaddr_dl_measure(uint8_t namelen, uint8_t addrlen)
273 {
274 return offsetof(struct sockaddr_dl, sdl_data[namelen + addrlen]);
275 }
276
277 struct sockaddr *
278 sockaddr_dl_alloc(uint16_t ifindex, uint8_t type,
279 const void *name, uint8_t namelen, const void *addr, uint8_t addrlen,
280 int flags)
281 {
282 struct sockaddr *sa;
283 socklen_t len;
284
285 len = sockaddr_dl_measure(namelen, addrlen);
286 sa = sockaddr_alloc(AF_LINK, len, flags);
287
288 if (sa == NULL)
289 return NULL;
290
291 if (sockaddr_dl_init(satosdl(sa), len, ifindex, type, name, namelen,
292 addr, addrlen) == NULL) {
293 sockaddr_free(sa);
294 return NULL;
295 }
296
297 return sa;
298 }
299
300 struct sockaddr_dl *
301 sockaddr_dl_init(struct sockaddr_dl *sdl, socklen_t socklen, uint16_t ifindex,
302 uint8_t type, const void *name, uint8_t namelen, const void *addr,
303 uint8_t addrlen)
304 {
305 socklen_t len;
306
307 sdl->sdl_family = AF_LINK;
308 sdl->sdl_slen = 0;
309 len = sockaddr_dl_measure(namelen, addrlen);
310 if (len > socklen) {
311 sdl->sdl_len = socklen;
312 #ifdef DIAGNOSTIC
313 printf("%s: too long: %" PRIu8 " > %" PRIu8 "\n", __func__, len,
314 socklen);
315 #endif
316 return NULL;
317 }
318 sdl->sdl_len = len;
319 sdl->sdl_index = ifindex;
320 sdl->sdl_type = type;
321 memset(&sdl->sdl_data[0], 0, namelen + addrlen);
322 if (name != NULL) {
323 memcpy(&sdl->sdl_data[0], name, namelen);
324 sdl->sdl_nlen = namelen;
325 } else
326 sdl->sdl_nlen = 0;
327 if (addr != NULL) {
328 memcpy(&sdl->sdl_data[sdl->sdl_nlen], addr, addrlen);
329 sdl->sdl_alen = addrlen;
330 } else
331 sdl->sdl_alen = 0;
332 return sdl;
333 }
334
335 static int
336 sockaddr_dl_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
337 {
338 int rc;
339 const uint_fast8_t indexofs = offsetof(struct sockaddr_dl, sdl_index);
340 const uint_fast8_t nlenofs = offsetof(struct sockaddr_dl, sdl_nlen);
341 uint_fast8_t dataofs = offsetof(struct sockaddr_dl, sdl_data[0]);
342 const struct sockaddr_dl *sdl1, *sdl2;
343
344 sdl1 = satocsdl(sa1);
345 sdl2 = satocsdl(sa2);
346
347 rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
348 indexofs, nlenofs);
349
350 if (rc != 0)
351 return rc;
352
353 rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
354 dataofs, dataofs + MIN(sdl1->sdl_nlen, sdl2->sdl_nlen));
355
356 if (rc != 0)
357 return rc;
358
359 if (sdl1->sdl_nlen != sdl2->sdl_nlen)
360 return sdl1->sdl_nlen - sdl2->sdl_nlen;
361
362 dataofs += sdl1->sdl_nlen;
363
364 rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
365 dataofs, dataofs + MIN(sdl1->sdl_alen, sdl2->sdl_alen));
366
367 if (rc != 0)
368 return rc;
369
370 if (sdl1->sdl_alen != sdl2->sdl_alen)
371 return sdl1->sdl_alen - sdl2->sdl_alen;
372
373 dataofs += sdl1->sdl_alen;
374
375 rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
376 dataofs, dataofs + MIN(sdl1->sdl_slen, sdl2->sdl_slen));
377
378 if (sdl1->sdl_slen != sdl2->sdl_slen)
379 return sdl1->sdl_slen - sdl2->sdl_slen;
380
381 return sdl1->sdl_len - sdl2->sdl_len;
382 }
383
384 struct sockaddr_dl *
385 sockaddr_dl_setaddr(struct sockaddr_dl *sdl, socklen_t socklen,
386 const void *addr, uint8_t addrlen)
387 {
388 socklen_t len;
389
390 len = sockaddr_dl_measure(sdl->sdl_nlen, addrlen);
391 if (len > socklen) {
392 #ifdef DIAGNOSTIC
393 printf("%s: too long: %" PRIu8 " > %" PRIu8 "\n", __func__, len,
394 socklen);
395 #endif
396 return NULL;
397 }
398 memcpy(&sdl->sdl_data[sdl->sdl_nlen], addr, addrlen);
399 sdl->sdl_alen = addrlen;
400 sdl->sdl_len = len;
401 return sdl;
402 }
403