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