1 /* $NetBSD: link_proto.c,v 1.4 2008/05/13 18:09:22 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.4 2008/05/13 18:09:22 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 #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 sockaddr_copy(sstosa(&iflr->addr), sizeof(iflr->addr), 157 ifa->ifa_addr); 158 159 break; 160 case SIOCDLIFADDR: 161 if (ifa == NULL) 162 error = EADDRNOTAVAIL; 163 else if (ifa == ifp->if_dl) 164 error = EBUSY; 165 else { 166 /* TBD routing socket */ 167 rt_newaddrmsg(RTM_DELETE, ifa, 0, NULL); 168 ifa_remove(ifp, ifa); 169 } 170 break; 171 case SIOCALIFADDR: 172 if (ifa != NULL) 173 ; 174 else if ((ifa = if_dl_create(ifp, &nsdl)) == NULL) { 175 error = ENOMEM; 176 break; 177 } else { 178 sockaddr_copy(ifa->ifa_addr, 179 ifa->ifa_addr->sa_len, &u.sa); 180 ifa_insert(ifp, ifa); 181 rt_newaddrmsg(RTM_ADD, ifa, 0, NULL); 182 } 183 184 mkactive = (iflr->flags & IFLR_ACTIVE) != 0; 185 isactive = (ifa == ifp->if_dl); 186 187 if (!isactive && mkactive) { 188 if_activate_sadl(ifp, ifa, nsdl); 189 error = ENETRESET; 190 } 191 break; 192 } 193 splx(s); 194 if (error != ENETRESET) 195 return error; 196 else if ((ifp->if_flags & IFF_RUNNING) != 0) 197 return (*ifp->if_init)(ifp); 198 else 199 return 0; 200 default: 201 return ENOTTY; 202 } 203 } 204 205 static int 206 link_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam, 207 struct mbuf *control, struct lwp *l) 208 { 209 switch (req) { 210 case PRU_ATTACH: 211 sosetlock(so); 212 return 0; 213 case PRU_DETACH: 214 sofree(so); 215 return 0; 216 case PRU_CONTROL: 217 return link_control(so, (unsigned long)m, nam, 218 (struct ifnet *)control, l); 219 default: 220 return EOPNOTSUPP; 221 } 222 } 223 224 /* Compare the field at byte offsets [fieldstart, fieldend) in 225 * two memory regions, [l, l + llen) and [r, r + llen). 226 */ 227 static inline int 228 submemcmp(const void *l, const void *r, 229 const uint_fast8_t llen, const uint_fast8_t rlen, 230 const uint_fast8_t fieldstart, const uint_fast8_t fieldend) 231 { 232 uint_fast8_t cmpend, minlen; 233 const uint8_t *lb = l, *rb = r; 234 int rc; 235 236 minlen = MIN(llen, rlen); 237 238 /* The field is missing from one region. The shorter region is the 239 * lesser region. 240 */ 241 if (fieldstart >= minlen) 242 return llen - rlen; 243 244 /* Two empty, present fields are always equal. */ 245 if (fieldstart > fieldend) 246 return 0; 247 248 cmpend = MIN(fieldend, minlen); 249 250 rc = memcmp(&lb[fieldstart], &rb[fieldstart], cmpend - fieldstart); 251 252 if (rc != 0) 253 return rc; 254 /* If one or both fields are truncated, then the shorter is the lesser 255 * field. 256 */ 257 if (minlen < fieldend) 258 return llen - rlen; 259 /* Fields are full-length and equal. The fields are equal. */ 260 return 0; 261 } 262 263 uint8_t 264 sockaddr_dl_measure(uint8_t namelen, uint8_t addrlen) 265 { 266 return offsetof(struct sockaddr_dl, sdl_data[namelen + addrlen]); 267 } 268 269 struct sockaddr * 270 sockaddr_dl_alloc(uint16_t ifindex, uint8_t type, 271 const void *name, uint8_t namelen, const void *addr, uint8_t addrlen, 272 int flags) 273 { 274 struct sockaddr *sa; 275 socklen_t len; 276 277 len = sockaddr_dl_measure(namelen, addrlen); 278 sa = sockaddr_alloc(AF_LINK, len, flags); 279 280 if (sa == NULL) 281 return NULL; 282 283 if (sockaddr_dl_init(satosdl(sa), len, ifindex, type, name, namelen, 284 addr, addrlen) == NULL) { 285 sockaddr_free(sa); 286 return NULL; 287 } 288 289 return sa; 290 } 291 292 struct sockaddr_dl * 293 sockaddr_dl_init(struct sockaddr_dl *sdl, socklen_t socklen, uint16_t ifindex, 294 uint8_t type, const void *name, uint8_t namelen, const void *addr, 295 uint8_t addrlen) 296 { 297 socklen_t len; 298 299 sdl->sdl_family = AF_LINK; 300 sdl->sdl_slen = 0; 301 len = sockaddr_dl_measure(namelen, addrlen); 302 if (len > socklen) { 303 sdl->sdl_len = socklen; 304 #ifdef DIAGNOSTIC 305 printf("%s: too long: %" PRIu8 " > %" PRIu8 "\n", __func__, len, 306 socklen); 307 #endif 308 return NULL; 309 } 310 sdl->sdl_len = len; 311 sdl->sdl_index = ifindex; 312 sdl->sdl_type = type; 313 memset(&sdl->sdl_data[0], 0, namelen + addrlen); 314 if (name != NULL) { 315 memcpy(&sdl->sdl_data[0], name, namelen); 316 sdl->sdl_nlen = namelen; 317 } else 318 sdl->sdl_nlen = 0; 319 if (addr != NULL) { 320 memcpy(&sdl->sdl_data[sdl->sdl_nlen], addr, addrlen); 321 sdl->sdl_alen = addrlen; 322 } else 323 sdl->sdl_alen = 0; 324 return sdl; 325 } 326 327 static int 328 sockaddr_dl_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2) 329 { 330 int rc; 331 const uint_fast8_t indexofs = offsetof(struct sockaddr_dl, sdl_index); 332 const uint_fast8_t nlenofs = offsetof(struct sockaddr_dl, sdl_nlen); 333 uint_fast8_t dataofs = offsetof(struct sockaddr_dl, sdl_data[0]); 334 const struct sockaddr_dl *sdl1, *sdl2; 335 336 sdl1 = satocsdl(sa1); 337 sdl2 = satocsdl(sa2); 338 339 rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len, 340 indexofs, nlenofs); 341 342 if (rc != 0) 343 return rc; 344 345 rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len, 346 dataofs, dataofs + MIN(sdl1->sdl_nlen, sdl2->sdl_nlen)); 347 348 if (rc != 0) 349 return rc; 350 351 if (sdl1->sdl_nlen != sdl2->sdl_nlen) 352 return sdl1->sdl_nlen - sdl2->sdl_nlen; 353 354 dataofs += sdl1->sdl_nlen; 355 356 rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len, 357 dataofs, dataofs + MIN(sdl1->sdl_alen, sdl2->sdl_alen)); 358 359 if (rc != 0) 360 return rc; 361 362 if (sdl1->sdl_alen != sdl2->sdl_alen) 363 return sdl1->sdl_alen - sdl2->sdl_alen; 364 365 dataofs += sdl1->sdl_alen; 366 367 rc = submemcmp(sdl1, sdl2, sdl1->sdl_len, sdl2->sdl_len, 368 dataofs, dataofs + MIN(sdl1->sdl_slen, sdl2->sdl_slen)); 369 370 if (sdl1->sdl_slen != sdl2->sdl_slen) 371 return sdl1->sdl_slen - sdl2->sdl_slen; 372 373 return sdl1->sdl_len - sdl2->sdl_len; 374 } 375 376 struct sockaddr_dl * 377 sockaddr_dl_setaddr(struct sockaddr_dl *sdl, socklen_t socklen, 378 const void *addr, uint8_t addrlen) 379 { 380 socklen_t len; 381 382 len = sockaddr_dl_measure(sdl->sdl_nlen, addrlen); 383 if (len > socklen) { 384 #ifdef DIAGNOSTIC 385 printf("%s: too long: %" PRIu8 " > %" PRIu8 "\n", __func__, len, 386 socklen); 387 #endif 388 return NULL; 389 } 390 memcpy(&sdl->sdl_data[sdl->sdl_nlen], addr, addrlen); 391 sdl->sdl_alen = addrlen; 392 sdl->sdl_len = len; 393 return sdl; 394 } 395