Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / net / link_proto.c
blob95c2b89ae597848fa58ae808ef01572d328b2a95
1 /* $NetBSD: link_proto.c,v 1.4 2008/05/13 18:09:22 dyoung Exp $ */
3 /*-
4 * Copyright (c) 1982, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
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.
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.
31 * @(#)uipc_proto.c 8.2 (Berkeley) 2/14/95
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: link_proto.c,v 1.4 2008/05/13 18:09:22 dyoung Exp $");
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>
45 #include <net/if.h>
46 #include <net/if_dl.h>
47 #include <net/raw_cb.h>
48 #include <net/route.h>
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);
56 * Definitions of protocols supported in the link-layer domain.
59 DOMAIN_DEFINE(linkdomain); /* forward define and add to link set */
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,
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
84 static void
85 link_init(void)
87 return;
90 static int
91 link_control(struct socket *so, unsigned long cmd, void *data,
92 struct ifnet *ifp, struct lwp *l)
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;
105 switch (cmd) {
106 case SIOCALIFADDR:
107 case SIOCDLIFADDR:
108 case SIOCGLIFADDR:
109 iflr = data;
111 if (iflr->addr.ss_family != AF_LINK)
112 return EINVAL;
114 asdl = satocsdl(sstocsa(&iflr->addr));
116 if (asdl->sdl_alen != ifp->if_addrlen)
117 return EINVAL;
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;
124 if ((iflr->flags & IFLR_PREFIX) == 0)
126 else if (iflr->prefixlen != NBBY * ifp->if_addrlen)
127 return EINVAL; /* XXX match with prefix */
129 error = 0;
131 s = splnet();
133 IFADDR_FOREACH(ifa, ifp) {
134 if (sockaddr_cmp(&u.sa, ifa->ifa_addr) == 0)
135 break;
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;
146 if (ifa == NULL) {
147 error = EADDRNOTAVAIL;
148 break;
151 if (ifa == ifp->if_dl)
152 iflr->flags = IFLR_ACTIVE;
153 else
154 iflr->flags = 0;
156 if (ifa == ifp->if_hwdl)
157 iflr->flags |= IFLR_FACTORY;
159 sockaddr_copy(sstosa(&iflr->addr), sizeof(iflr->addr),
160 ifa->ifa_addr);
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);
173 break;
174 case SIOCALIFADDR:
175 if (ifa != NULL)
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);
187 mkactive = (iflr->flags & IFLR_ACTIVE) != 0;
188 isactive = (ifa == ifp->if_dl);
190 if (!isactive && mkactive) {
191 if_activate_sadl(ifp, ifa, nsdl);
192 error = ENETRESET;
194 break;
196 splx(s);
197 if (error != ENETRESET)
198 return error;
199 else if ((ifp->if_flags & IFF_RUNNING) != 0)
200 return (*ifp->if_init)(ifp);
201 else
202 return 0;
203 default:
204 return ENOTTY;
208 static int
209 link_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
210 struct mbuf *control, struct lwp *l)
212 switch (req) {
213 case PRU_ATTACH:
214 sosetlock(so);
215 return 0;
216 case PRU_DETACH:
217 sofree(so);
218 return 0;
219 case PRU_CONTROL:
220 return link_control(so, (unsigned long)m, nam,
221 (struct ifnet *)control, l);
222 default:
223 return EOPNOTSUPP;
227 /* Compare the field at byte offsets [fieldstart, fieldend) in
228 * two memory regions, [l, l + llen) and [r, r + llen).
230 static inline int
231 submemcmp(const void *l, const void *r,
232 const uint_fast8_t llen, const uint_fast8_t rlen,
233 const uint_fast8_t fieldstart, const uint_fast8_t fieldend)
235 uint_fast8_t cmpend, minlen;
236 const uint8_t *lb = l, *rb = r;
237 int rc;
239 minlen = MIN(llen, rlen);
241 /* The field is missing from one region. The shorter region is the
242 * lesser region.
244 if (fieldstart >= minlen)
245 return llen - rlen;
247 /* Two empty, present fields are always equal. */
248 if (fieldstart > fieldend)
249 return 0;
251 cmpend = MIN(fieldend, minlen);
253 rc = memcmp(&lb[fieldstart], &rb[fieldstart], cmpend - fieldstart);
255 if (rc != 0)
256 return rc;
257 /* If one or both fields are truncated, then the shorter is the lesser
258 * field.
260 if (minlen < fieldend)
261 return llen - rlen;
262 /* Fields are full-length and equal. The fields are equal. */
263 return 0;
266 uint8_t
267 sockaddr_dl_measure(uint8_t namelen, uint8_t addrlen)
269 return offsetof(struct sockaddr_dl, sdl_data[namelen + addrlen]);
272 struct sockaddr *
273 sockaddr_dl_alloc(uint16_t ifindex, uint8_t type,
274 const void *name, uint8_t namelen, const void *addr, uint8_t addrlen,
275 int flags)
277 struct sockaddr *sa;
278 socklen_t len;
280 len = sockaddr_dl_measure(namelen, addrlen);
281 sa = sockaddr_alloc(AF_LINK, len, flags);
283 if (sa == NULL)
284 return NULL;
286 if (sockaddr_dl_init(satosdl(sa), len, ifindex, type, name, namelen,
287 addr, addrlen) == NULL) {
288 sockaddr_free(sa);
289 return NULL;
292 return sa;
295 struct sockaddr_dl *
296 sockaddr_dl_init(struct sockaddr_dl *sdl, socklen_t socklen, uint16_t ifindex,
297 uint8_t type, const void *name, uint8_t namelen, const void *addr,
298 uint8_t addrlen)
300 socklen_t len;
302 sdl->sdl_family = AF_LINK;
303 sdl->sdl_slen = 0;
304 len = sockaddr_dl_measure(namelen, addrlen);
305 if (len > socklen) {
306 sdl->sdl_len = socklen;
307 #ifdef DIAGNOSTIC
308 printf("%s: too long: %" PRIu8 " > %" PRIu8 "\n", __func__, len,
309 socklen);
310 #endif
311 return NULL;
313 sdl->sdl_len = len;
314 sdl->sdl_index = ifindex;
315 sdl->sdl_type = type;
316 memset(&sdl->sdl_data[0], 0, namelen + addrlen);
317 if (name != NULL) {
318 memcpy(&sdl->sdl_data[0], name, namelen);
319 sdl->sdl_nlen = namelen;
320 } else
321 sdl->sdl_nlen = 0;
322 if (addr != NULL) {
323 memcpy(&sdl->sdl_data[sdl->sdl_nlen], addr, addrlen);
324 sdl->sdl_alen = addrlen;
325 } else
326 sdl->sdl_alen = 0;
327 return sdl;
330 static int
331 sockaddr_dl_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2)
333 int rc;
334 const uint_fast8_t indexofs = offsetof(struct sockaddr_dl, sdl_index);
335 const uint_fast8_t nlenofs = offsetof(struct sockaddr_dl, sdl_nlen);
336 uint_fast8_t dataofs = offsetof(struct sockaddr_dl, sdl_data[0]);
337 const struct sockaddr_dl *sdl1, *sdl2;
339 sdl1 = satocsdl(sa1);
340 sdl2 = satocsdl(sa2);
342 rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
343 indexofs, nlenofs);
345 if (rc != 0)
346 return rc;
348 rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
349 dataofs, dataofs + MIN(sdl1->sdl_nlen, sdl2->sdl_nlen));
351 if (rc != 0)
352 return rc;
354 if (sdl1->sdl_nlen != sdl2->sdl_nlen)
355 return sdl1->sdl_nlen - sdl2->sdl_nlen;
357 dataofs += sdl1->sdl_nlen;
359 rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
360 dataofs, dataofs + MIN(sdl1->sdl_alen, sdl2->sdl_alen));
362 if (rc != 0)
363 return rc;
365 if (sdl1->sdl_alen != sdl2->sdl_alen)
366 return sdl1->sdl_alen - sdl2->sdl_alen;
368 dataofs += sdl1->sdl_alen;
370 rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len,
371 dataofs, dataofs + MIN(sdl1->sdl_slen, sdl2->sdl_slen));
373 if (sdl1->sdl_slen != sdl2->sdl_slen)
374 return sdl1->sdl_slen - sdl2->sdl_slen;
376 return sdl1->sdl_len - sdl2->sdl_len;
379 struct sockaddr_dl *
380 sockaddr_dl_setaddr(struct sockaddr_dl *sdl, socklen_t socklen,
381 const void *addr, uint8_t addrlen)
383 socklen_t len;
385 len = sockaddr_dl_measure(sdl->sdl_nlen, addrlen);
386 if (len > socklen) {
387 #ifdef DIAGNOSTIC
388 printf("%s: too long: %" PRIu8 " > %" PRIu8 "\n", __func__, len,
389 socklen);
390 #endif
391 return NULL;
393 memcpy(&sdl->sdl_data[sdl->sdl_nlen], addr, addrlen);
394 sdl->sdl_alen = addrlen;
395 sdl->sdl_len = len;
396 return sdl;