X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/e5568f75972dfc723778653c11cb6b4dc825716a..0c530ab8987f0ae6a1a3d9284f40182b88852816:/bsd/netat/ddp_nbp.c diff --git a/bsd/netat/ddp_nbp.c b/bsd/netat/ddp_nbp.c index 1bfec9e17..8b51c6128 100644 --- a/bsd/netat/ddp_nbp.c +++ b/bsd/netat/ddp_nbp.c @@ -76,7 +76,6 @@ extern at_ifaddr_t *ifID_home; TAILQ_HEAD(name_registry, _nve_) name_registry; -atlock_t nve_lock; /* statics */ static int errno; @@ -104,6 +103,39 @@ extern void nbp_add_multicast( at_nvestr_t *, at_ifaddr_t *); static long nbp_id_count = 0; +/* + * Copy src to string dst of size siz. At most siz-1 characters + * will be copied. Always NUL terminates (unless siz == 0). + * Returns strlen(src); if retval >= siz, truncation occurred. + */ +__private_extern__ size_t +strlcpy(char *dst, const char *src, size_t siz) +{ + char *d = dst; + const char *s = src; + size_t n = siz; + + /* Copy as many bytes as will fit */ + if (n != 0 && --n != 0) { + do { + if ((*d++ = *s++) == 0) + break; + } while (--n != 0); + } + + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) { + if (siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; + } + + return(s - src - 1); /* count does not include NUL */ +} + + + void sethzonehash(elapp) at_ifaddr_t *elapp; { @@ -117,14 +149,12 @@ void nbp_shutdown() /* delete all NVE's and release buffers */ register nve_entry_t *nve_entry, *nve_next; - ATDISABLE(nve_lock_pri,NVE_LOCK); - for ((nve_entry = TAILQ_FIRST(&name_registry)); nve_entry; nve_entry = nve_next) { - nve_next = TAILQ_NEXT(nve_entry, nve_link); + for ((nve_entry = TAILQ_FIRST(&name_registry)); nve_entry; nve_entry = nve_next) { + nve_next = TAILQ_NEXT(nve_entry, nve_link); /* NB: nbp_delete_entry calls TAILQ_REMOVE */ nbp_delete_entry(nve_entry); } - ATENABLE(nve_lock_pri,NVE_LOCK); if (lzones) { gbuf_freem(lzones); @@ -207,12 +237,9 @@ void nbp_input(m, ifID) #ifdef NBP_DEBUG { char zone[35],object[35],type[35]; - strncpy(zone,nbp_req.nve.zone.str, nbp_req.nve.zone.len); - strncpy(object,nbp_req.nve.object.str, nbp_req.nve.object.len); - strncpy(type,nbp_req.nve.type.str, nbp_req.nve.type.len); - object[nbp_req.nve.object.len] = '\0'; - zone[nbp_req.nve.zone.len] = '\0'; - type[nbp_req.nve.type.len] = '\0'; + strlcpy(zone,nbp_req.nve.zone.str, sizeof(zone)); + strlcpy(object,nbp_req.nve.object.str, sizeof(object)); + strlcpy(type,nbp_req.nve.type.str, sizeof(type)); if (ifID != ifID_home) dPrintf(D_M_NBP_LOW,D_L_USR2, ("nbp_LKUP for:%s:%s@%s", object, type, zone)); @@ -226,7 +253,7 @@ void nbp_input(m, ifID) /* true if home zone == destination zone */ register int zno, i; register gbuf_t *m2; - register error_found =0; + register int error_found =0; register at_ifaddr_t *ifIDorig; if (!ROUTING_MODE) /* for routers only! */ @@ -403,7 +430,7 @@ void nbp_input(m, ifID) ("nbp_input: BRREQ: src changed to %d.%d.%d\n", ifID->ifThisNode.s_net, ifID->ifThisNode.s_node, ourSkt)); - nbp->tuple[0].enu_addr.net = ifID->ifThisNode.s_net; + nbp->tuple[0].enu_addr.net = htons(ifID->ifThisNode.s_net); nbp->tuple[0].enu_addr.node = ifID->ifThisNode.s_node; nbp->tuple[0].enu_addr.socket = ourSkt; ddp->src_socket = NBP_SOCKET; @@ -499,7 +526,7 @@ static int nbp_validate_n_hash (nbp_req, wild_ok, checkLocal) #ifdef COMMENTED_OUT { int net,node,skt; - net = tuple->enu_addr.net; + net = ntohs(tuple->enu_addr.net); node = tuple->enu_addr.node; skt = tuple->enu_addr.socket; dPrintf(D_M_NBP_LOW,D_L_USR4, @@ -523,12 +550,9 @@ static int nbp_validate_n_hash (nbp_req, wild_ok, checkLocal) #ifdef NBP_DEBUG { char xzone[35],xobject[35],xtype[35]; - strncpy(xzone,zone->str, zone->len); - strncpy(xobject,object->str, object->len); - strncpy(xtype,type->str, type->len); - xobject[object->len] = '\0'; - xzone[zone->len] = '\0'; - xtype[type->len] = '\0'; + strlcpy(xzone,zone->str, sizeof(xzone)); + strlcpy(xobject,object->str, sizeof(xobject)); + strlcpy(xtype,type->str, sizeof(xtype)); dPrintf(D_M_NBP_LOW, D_L_USR4, ("nbp_validate: looking for %s:%s@%s\n", xobject, xtype, xzone)); @@ -541,8 +565,7 @@ static int nbp_validate_n_hash (nbp_req, wild_ok, checkLocal) if (checkLocal && !isZoneLocal(zone)) { char str[35]; - strncpy(str,zone->str,zone->len); - str[zone->len] = '\0'; + strlcpy(str,zone->str,sizeof(str)); dPrintf(D_M_NBP_LOW,D_L_WARNING, ("nbp_val_n_hash bad zone: %s\n", str)); errno = EINVAL; @@ -626,12 +649,9 @@ static int nbp_validate_n_hash (nbp_req, wild_ok, checkLocal) #ifdef NBP_DEBUG { char zone[35],object[35],type[35]; - strncpy(zone,nbp_req->nve.zone.str, nbp_req->nve.zone.len); - strncpy(object,nbp_req->nve.object.str, nbp_req->nve.object.len); - strncpy(type,nbp_req->nve.type.str, nbp_req->nve.type.len); - object[nbp_req->nve.object.len] = '\0'; - zone[nbp_req->nve.zone.len] = '\0'; - type[nbp_req->nve.type.len] = '\0'; + strlcpy(zone,nbp_req.nve.zone.str, sizeof(zone)); + strlcpy(object,nbp_req.nve.object.str, sizeof(object)); + strlcpy(type,nbp_req.nve.type.str, sizeof(type)); dPrintf(D_M_NBP_LOW,D_L_USR4, ("nbp_validate: after hash: %s:%s@%s\n", object, type, zone)); @@ -703,18 +723,14 @@ static nve_entry_t *nbp_search_nve (nbp_req, ifID) #ifdef NBP_DEBUG { char zone[35],object[35],type[35]; - strncpy(zone,nbp_req->nve.zone.str, nbp_req->nve.zone.len); - strncpy(object,nbp_req->nve.object.str, nbp_req->nve.object.len); - strncpy(type,nbp_req->nve.type.str, nbp_req->nve.type.len); - object[nbp_req->nve.object.len] = '\0'; - zone[nbp_req->nve.zone.len] = '\0'; - type[nbp_req->nve.type.len] = '\0'; + strlcpy(zone,nbp_req.nve.zone.str, sizeof(zone)); + strlcpy(object,nbp_req.nve.object.str, sizeof(object)); + strlcpy(type,nbp_req.nve.type.str, sizeof(type)); dPrintf(D_M_NBP_LOW, D_L_USR4, ("nbp_search: looking for %s:%s@%s resp:0x%x\n",object,type,zone, (u_int) nbp_req->response)); } #endif /* NBP_DEBUG */ - ATDISABLE(nve_lock_pri,NVE_LOCK); TAILQ_FOREACH(nve_entry, &name_registry, nve_link) { if ((nbp_req->nve.zone_hash) && ((nbp_req->nve.zone_hash != @@ -786,12 +802,9 @@ static nve_entry_t *nbp_search_nve (nbp_req, ifID) { char zone[35],object[35],type[35]; - strncpy(zone,nbp_req->nve.zone.str, nbp_req->nve.zone.len); - strncpy(object,nbp_req->nve.object.str, nbp_req->nve.object.len); - strncpy(type,nbp_req->nve.type.str, nbp_req->nve.type.len); - object[nbp_req->nve.object.len] = '\0'; - zone[nbp_req->nve.zone.len] = '\0'; - type[nbp_req->nve.type.len] = '\0'; + strlcpy(zone,nbp_req.nve.zone.str, sizeof(zone)); + strlcpy(object,nbp_req.nve.object.str, sizeof(object)); + strlcpy(type,nbp_req.nve.type.str, sizeof(type)); dPrintf(D_M_NBP_LOW, D_L_USR2, ("nbp_search: found %s:%s@%s net:%d\n", object, type, zone, (int)nve_entry->address.net)); @@ -800,15 +813,11 @@ static nve_entry_t *nbp_search_nve (nbp_req, ifID) if (nbp_req->func != NULL) { if ((*(nbp_req->func))(nbp_req, nve_entry) != 0) { /* errno expected to be set by func */ - ATENABLE(nve_lock_pri,NVE_LOCK); return (NULL); } - } else { - ATENABLE(nve_lock_pri,NVE_LOCK); + } else return (nve_entry); - } } - ATENABLE(nve_lock_pri,NVE_LOCK); errno = 0; return (NULL); @@ -848,7 +857,9 @@ register nve_entry_t *nve_entry; * tuple we want to write. Write it! */ tuple = (at_nbptuple_t *)gbuf_wptr(nbp_req->response); - tuple->enu_addr = nve_entry->address; + tuple->enu_addr.net = htons(nve_entry->address.net); + tuple->enu_addr.node = nve_entry->address.node; + tuple->enu_addr.socket = nve_entry->address.socket; tuple->enu_enum = nve_entry->enumerator; /* tuple is in the compressed (no "filler") format */ @@ -969,7 +980,7 @@ register nbp_req_t *nbp_req; case NBP_LKUP : ddp->dst_socket = nbp_req->nve.address.socket; ddp->dst_node = nbp_req->nve.address.node; - NET_ASSIGN(ddp->dst_net, nbp_req->nve.address.net); + NET_ASSIGN_NOSWAP(ddp->dst_net, nbp_req->nve.address.net); nbp->control = NBP_LKUP_REPLY; break; } @@ -1022,8 +1033,7 @@ void nbp_add_multicast(zone, ifID) { char str[35]; - strncpy(str,zone->str,zone->len); - str[zone->len] = '\0'; + strlcpy(str,zone->str,sizeof(str)); dPrintf(D_M_NBP_LOW, D_L_USR3, ("nbp_add_multi getting mc for %s\n", str)); } @@ -1047,10 +1057,8 @@ getNbpTableSize() register nve_entry_t *nve; register int i=0; - ATDISABLE(nve_lock_pri,NVE_LOCK); for (nve = TAILQ_FIRST(&name_registry); nve; nve = TAILQ_NEXT(nve, nve_link), i++) i++; - ATENABLE(nve_lock_pri,NVE_LOCK); return(i); } @@ -1073,7 +1081,6 @@ getNbpTable(p, s, c) else nve = TAILQ_FIRST(&name_registry); - ATDISABLE(nve_lock_pri,NVE_LOCK); for ( ; nve && c ; nve = TAILQ_NEXT(nve, nve_link), p++,i++) { if (i>= s) { p->nbpe_object = nve->object; @@ -1081,7 +1088,6 @@ getNbpTable(p, s, c) c--; } } - ATENABLE(nve_lock_pri,NVE_LOCK); if (nve) { next = nve; nextNo = i; @@ -1115,8 +1121,7 @@ int setLocalZones(newzones, size) while (bytesread < size) { /* for each new zone */ { char str[35]; - strncpy(str,pnew->str,pnew->len); - str[pnew->len] = '\0'; + strlcpy(str,pnew->str,sizeof(str)); } m = lzones; pnve = (at_nvestr_t*)gbuf_rptr(m); @@ -1147,7 +1152,7 @@ int setLocalZones(newzones, size) gbuf_wset(gbuf_cont(m),0); pnve = (at_nvestr_t*)gbuf_rptr(gbuf_cont(m)); } - strncpy(pnve->str,pnew->str,pnew->len); + strlcpy(pnve->str,pnew->str,sizeof(pnve->str)); pnve->len = pnew->len; lzonecnt++; } @@ -1170,8 +1175,7 @@ showLocalZones1() if (!(pnve = getLocalZone(i))) { break; } - strncpy(str,pnve->str,pnve->len); - str[pnve->len] = '\0'; + strlcpy(str,pnve->str,sizeof(str)); } } @@ -1323,7 +1327,6 @@ nve_entry_t *nbp_find_nve(nve) { register nve_entry_t *nve_entry; - ATDISABLE(nve_lock_pri,NVE_LOCK); TAILQ_FOREACH(nve_entry, &name_registry, nve_link) { if (nve->zone_hash && ((nve->zone_hash != nve_entry->zone_hash) && @@ -1343,10 +1346,8 @@ nve_entry_t *nbp_find_nve(nve) continue; /* Found a match! */ - ATENABLE(nve_lock_pri,NVE_LOCK); return (nve_entry); } - ATENABLE(nve_lock_pri,NVE_LOCK); return (NULL); } /* nbp_find_nve */ @@ -1357,22 +1358,19 @@ static int nbp_enum_gen (nve_entry) register int new_enum = 0; register nve_entry_t *ne; - ATDISABLE(nve_lock_pri,NVE_LOCK); re_do: TAILQ_FOREACH(ne, &name_registry, nve_link) { if ((*(int *)&ne->address == *(int *)&nve_entry->address) && (ne->enumerator == new_enum)) { - if (new_enum == 255) { - ATENABLE(nve_lock_pri,NVE_LOCK); + if (new_enum == 255) return(EADDRNOTAVAIL); - } else { + else { new_enum++; goto re_do; } } } - ATENABLE(nve_lock_pri,NVE_LOCK); nve_entry->enumerator = new_enum; return (0); } @@ -1423,22 +1421,17 @@ int nbp_new_nve_entry(nve_entry, ifID) new_entry->zone_hash = nbp_strhash(&new_entry->zone); } new_entry->tag = tag; - new_entry->pid = current_proc()->p_pid; + new_entry->pid = proc_selfpid(); - ATDISABLE(nve_lock_pri,NVE_LOCK); TAILQ_INSERT_TAIL(&name_registry, new_entry, nve_link); - ATENABLE(nve_lock_pri,NVE_LOCK); at_state.flags |= AT_ST_NBP_CHANGED; #ifdef NBP_DEBUG { char zone[35],object[35],type[35]; - strncpy(zone,new_entry->zone.str, new_entry->zone.len); - strncpy(object,new_entry->object.str, new_entry->object.len); - strncpy(type,new_entry->type.str, new_entry->type.len); - object[new_entry->object.len] = '\0'; - zone[new_entry->zone.len] = '\0'; - type[new_entry->type.len] = '\0'; + strlcpy(zone,nbp_req.nve.zone.str, sizeof(zone)); + strlcpy(object,nbp_req.nve.object.str, sizeof(object)); + strlcpy(type,nbp_req.nve.type.str, sizeof(type)); dPrintf(D_M_NBP_LOW, D_L_USR4, ("nbp_insert: adding %s:%s@%s addr:%d.%d ", object, type, zone,