enum nhrp_route_type nhrp_route_address(struct interface *in_ifp, union sockunion *addr, struct prefix *p, struct nhrp_peer **peer) { struct interface *ifp = in_ifp; struct nhrp_interface *nifp; struct nhrp_cache *c; union sockunion via[4]; uint32_t network_id = 0; afi_t afi = family2afi(sockunion_family(addr)); int i; if (ifp) { nifp = ifp->info; network_id = nifp->afi[afi].network_id; c = nhrp_cache_get(ifp, addr, 0); if (c && c->cur.type == NHRP_CACHE_LOCAL) { if (p) memset(p, 0, sizeof(*p)); return NHRP_ROUTE_LOCAL; } } for (i = 0; i < 4; i++) { if (!nhrp_route_get_nexthop(addr, p, &via[i], &ifp)) return NHRP_ROUTE_BLACKHOLE; if (ifp) { /* Departing from nbma network? */ nifp = ifp->info; if (network_id && network_id != nifp->afi[afi].network_id) return NHRP_ROUTE_OFF_NBMA; } if (sockunion_family(&via[i]) == AF_UNSPEC) break; /* Resolve via node, but return the prefix of first match */ addr = &via[i]; p = NULL; } if (ifp) { c = nhrp_cache_get(ifp, addr, 0); if (c && c->cur.type >= NHRP_CACHE_DYNAMIC) { if (p) memset(p, 0, sizeof(*p)); if (c->cur.type == NHRP_CACHE_LOCAL) return NHRP_ROUTE_LOCAL; if (peer) *peer = nhrp_peer_ref(c->cur.peer); return NHRP_ROUTE_NBMA_NEXTHOP; } } return NHRP_ROUTE_BLACKHOLE; }
static void nhrp_interface_update_address(struct interface *ifp, afi_t afi, int force) { const int family = afi2family(afi); struct nhrp_interface *nifp = ifp->info; struct nhrp_afi_data *if_ad = &nifp->afi[afi]; struct nhrp_cache *nc; struct connected *c, *best; struct listnode *cnode; union sockunion addr; char buf[PREFIX_STRLEN]; /* Select new best match preferring primary address */ best = NULL; for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) { if (PREFIX_FAMILY(c->address) != family) continue; if (best == NULL) { best = c; continue; } if ((best->flags & ZEBRA_IFA_SECONDARY) && !(c->flags & ZEBRA_IFA_SECONDARY)) { best = c; continue; } if (!(best->flags & ZEBRA_IFA_SECONDARY) && (c->flags & ZEBRA_IFA_SECONDARY)) continue; if (best->address->prefixlen > c->address->prefixlen) { best = c; continue; } if (best->address->prefixlen < c->address->prefixlen) continue; } /* On NHRP interfaces a host prefix is required */ if (best && if_ad->configured && best->address->prefixlen != 8 * prefix_blen(best->address)) { zlog_notice("%s: %s is not a host prefix", ifp->name, prefix2str(best->address, buf, sizeof buf)); best = NULL; } /* Update address if it changed */ if (best) prefix2sockunion(best->address, &addr); else memset(&addr, 0, sizeof(addr)); if (!force && sockunion_same(&if_ad->addr, &addr)) return; if (sockunion_family(&if_ad->addr) != AF_UNSPEC) { nc = nhrp_cache_get(ifp, &if_ad->addr, 0); if (nc) nhrp_cache_update_binding(nc, NHRP_CACHE_LOCAL, -1, NULL, 0, NULL); } debugf(NHRP_DEBUG_KERNEL, "%s: IPv%d address changed to %s", ifp->name, afi == AFI_IP ? 4 : 6, best ? prefix2str(best->address, buf, sizeof buf) : "(none)"); if_ad->addr = addr; if (if_ad->configured && sockunion_family(&if_ad->addr) != AF_UNSPEC) { nc = nhrp_cache_get(ifp, &addr, 1); if (nc) nhrp_cache_update_binding(nc, NHRP_CACHE_LOCAL, 0, NULL, 0, NULL); } notifier_call(&nifp->notifier_list, NOTIFY_INTERFACE_ADDRESS_CHANGED); }