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