Changeset View
Changeset View
Standalone View
Standalone View
usr.bin/netstat/nhgrp.c
- This file was copied from usr.bin/netstat/nhops.c.
| Show First 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | |||||
| static int wid_gw; | static int wid_gw; | ||||
| static int wid_flags; | static int wid_flags; | ||||
| static int wid_pksent; | static int wid_pksent; | ||||
| static int wid_mtu; | static int wid_mtu; | ||||
| static int wid_if; | static int wid_if; | ||||
| static int wid_nhidx; | static int wid_nhidx; | ||||
| static int wid_nhtype; | static int wid_nhtype; | ||||
| static int wid_refcnt; | static int wid_refcnt; | ||||
| static int wid_prepend; | |||||
| static struct bits nh_bits[] = { | |||||
| { NHF_REJECT, 'R', "reject" }, | |||||
| { NHF_BLACKHOLE,'B', "blackhole" }, | |||||
| { NHF_REDIRECT, 'r', "redirect" }, | |||||
| { NHF_GATEWAY, 'G', "gateway" }, | |||||
| { NHF_DEFAULT, 'd', "default" }, | |||||
| { NHF_BROADCAST,'b', "broadcast" }, | |||||
| { 0 , 0, NULL } | |||||
| }; | |||||
| static char *nh_types[] = { | |||||
| "empty", /* 0 */ | |||||
| "v4/resolve", /* 1 */ | |||||
| "v4/gw", | |||||
| "v6/resolve", | |||||
| "v6/gw" | |||||
| }; | |||||
| struct nhop_entry { | struct nhop_entry { | ||||
| char gw[64]; | char gw[64]; | ||||
| char ifname[IFNAMSIZ]; | char ifname[IFNAMSIZ]; | ||||
| }; | }; | ||||
| struct nhop_map { | struct nhop_map { | ||||
| struct nhop_entry *ptr; | struct nhop_entry *ptr; | ||||
| size_t size; | size_t size; | ||||
| }; | }; | ||||
| static struct nhop_map global_nhop_map; | static struct nhop_map global_nhop_map; | ||||
| static void nhop_map_update(struct nhop_map *map, uint32_t idx, | |||||
| char *gw, char *ifname); | |||||
| static struct nhop_entry *nhop_get(struct nhop_map *map, uint32_t idx); | |||||
| static struct ifmap_entry *ifmap; | static struct ifmap_entry *ifmap; | ||||
| static size_t ifmap_size; | static size_t ifmap_size; | ||||
| static void | static struct nhop_entry * | ||||
| print_sockaddr_buf(char *buf, size_t bufsize, const struct sockaddr *sa) | nhop_get(struct nhop_map *map, uint32_t idx) | ||||
| { | { | ||||
| switch (sa->sa_family) { | if (idx >= map->size) | ||||
| case AF_INET: | return (NULL); | ||||
| inet_ntop(AF_INET, &((struct sockaddr_in *)sa)->sin_addr, | if (*map->ptr[idx].ifname == '\0') | ||||
| buf, bufsize); | return (NULL); | ||||
| break; | return &map->ptr[idx]; | ||||
| case AF_INET6: | |||||
| inet_ntop(AF_INET6, &((struct sockaddr_in6 *)sa)->sin6_addr, | |||||
| buf, bufsize); | |||||
| break; | |||||
| default: | |||||
| snprintf(buf, bufsize, "unknown:%d", sa->sa_family); | |||||
| break; | |||||
| } | } | ||||
| } | |||||
| static int | |||||
| print_addr(const char *name, const char *addr, int width) | |||||
| { | |||||
| char buf[128]; | |||||
| int protrusion; | |||||
| if (width < 0) { | |||||
| snprintf(buf, sizeof(buf), "{:%s/%%s} ", name); | |||||
| xo_emit(buf, addr); | |||||
| protrusion = 0; | |||||
| } else { | |||||
| if (Wflag != 0 || numeric_addr) { | |||||
| snprintf(buf, sizeof(buf), "{[:%d}{:%s/%%s}{]:} ", | |||||
| -width, name); | |||||
| xo_emit(buf, addr); | |||||
| protrusion = strlen(addr) - width; | |||||
| if (protrusion < 0) | |||||
| protrusion = 0; | |||||
| } else { | |||||
| snprintf(buf, sizeof(buf), "{[:%d}{:%s/%%-.*s}{]:} ", | |||||
| -width, name); | |||||
| xo_emit(buf, width, addr); | |||||
| protrusion = 0; | |||||
| } | |||||
| } | |||||
| return (protrusion); | |||||
| } | |||||
| static void | static void | ||||
| print_nhop_header(int af1 __unused) | print_nhgroup_header(int af1 __unused) | ||||
| { | { | ||||
| if (Wflag) { | |||||
| xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} " | xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s}" | ||||
| "{T:/%*.*s} {T:/%-*.*s} {T:/%*.*s} {T:/%*.*s} {T:/%*.*s} {T:/%*s}\n", | " {T:/%-*.*s} {T:/%*s}\n", | ||||
| wid_nhidx, wid_nhidx, "Idx", | wid_nhidx, wid_nhidx, "MpIdx", | ||||
| wid_nhtype, wid_nhtype, "Type", | wid_nhidx, wid_nhidx, "NHIdx", | ||||
| wid_dst, wid_dst, "IFA", | wid_nhidx, wid_nhidx, "Weight", | ||||
| wid_nhidx, wid_nhidx, "Slots", | |||||
| wid_gw, wid_gw, "Gateway", | wid_gw, wid_gw, "Gateway", | ||||
| wid_flags, wid_flags, "Flags", | |||||
| wid_pksent, wid_pksent, "Use", | |||||
| wid_mtu, wid_mtu, "Mtu", | |||||
| wid_if, wid_if, "Netif", | wid_if, wid_if, "Netif", | ||||
| wid_if, wid_if, "Addrif", | wid_nhidx, "Refcnt"); | ||||
| wid_refcnt, wid_refcnt, "Refcnt", | |||||
| wid_prepend, "Prepend"); | |||||
| } else { | |||||
| xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} " | |||||
| " {T:/%*s}\n", | |||||
| wid_nhidx, wid_nhidx, "Idx", | |||||
| wid_dst, wid_dst, "IFA", | |||||
| wid_gw, wid_gw, "Gateway", | |||||
| wid_flags, wid_flags, "Flags", | |||||
| wid_if, wid_if, "Netif", | |||||
| wid_prepend, "Refcnt"); | |||||
| } | } | ||||
| } | |||||
| static void | static void | ||||
| nhop_map_update(struct nhop_map *map, uint32_t idx, char *gw, char *ifname) | print_nhgroup_entry_sysctl(const char *name, struct rt_msghdr *rtm, | ||||
| struct mpath_external *mpe) | |||||
| { | { | ||||
| if (idx >= map->size) { | char buffer[128]; | ||||
| uint32_t new_size; | struct nhop_entry *ne; | ||||
| size_t sz; | |||||
| if (map->size == 0) | |||||
| new_size = 32; | |||||
| else | |||||
| new_size = map->size * 2; | |||||
| if (new_size <= idx) | |||||
| new_size = roundup(idx + 1, 32); | |||||
| sz = new_size * (sizeof(struct nhop_entry)); | xo_open_instance(name); | ||||
| if ((map->ptr = realloc(map->ptr, sz)) == NULL) | |||||
| errx(2, "realloc(%zu) failed", sz); | |||||
| memset(&map->ptr[map->size], 0, (new_size - map->size) * sizeof(struct nhop_entry)); | snprintf(buffer, sizeof(buffer), "{[:-%d}{:mp_index/%%lu}{]:} ", wid_nhidx); | ||||
| map->size = new_size; | xo_emit(buffer, mpe->mp_idx); | ||||
| } | |||||
| strlcpy(map->ptr[idx].ifname, ifname, sizeof(map->ptr[idx].ifname)); | xo_emit("{t:dummy-1/%*.*s}", wid_nhidx, wid_nhidx, "----"); | ||||
| strlcpy(map->ptr[idx].gw, gw, sizeof(map->ptr[idx].gw)); | xo_emit("{t:dummy-2/%*.*s}", wid_nhidx, wid_nhidx, "----"); | ||||
| } | xo_emit("{t:dummy-2/%*.*s}", wid_nhidx, wid_nhidx, "----"); | ||||
| xo_emit("{t:dummy-3/%*.*s}", wid_gw, wid_gw, "----"); | |||||
| xo_emit("{t:dummy-4/%*.*s}", wid_if, wid_if, "----"); | |||||
| xo_emit("{t:mp-refcnt/%*lu}", wid_nhidx, mpe->mp_refcount); | |||||
| xo_emit("\n"); | |||||
| static struct nhop_entry * | struct mpath_nhop_external *ext; | ||||
| nhop_get(struct nhop_map *map, uint32_t idx) | ext = (struct mpath_nhop_external *)(mpe + 1); | ||||
| { | |||||
| if (idx >= map->size) | uint32_t *fwd_c = calloc(sizeof(uint32_t), global_nhop_map.size); | ||||
| return (NULL); | uint32_t *pidx; | ||||
| if (*map->ptr[idx].ifname == '\0') | pidx = (uint32_t *)&ext[mpe->mp_nh_count]; | ||||
| return (NULL); | for (uint32_t i = 0; i < mpe->mp_group_size; i++) { | ||||
| return &map->ptr[idx]; | fwd_c[pidx[i]]++; | ||||
| } | } | ||||
| static void | xo_open_list("nhop_weights"); | ||||
| print_nhop_entry_sysctl(const char *name, struct rt_msghdr *rtm, struct nhop_external *nh) | for (uint32_t i = 0; i < mpe->mp_nh_count; i++) { | ||||
| { | xo_open_instance("nhop-weight"); | ||||
| char buffer[128]; | snprintf(buffer, sizeof(buffer), "{[:-%d}{:d-0/%%s}{]:} ", wid_nhidx); | ||||
| char iface_name[128]; | xo_emit(buffer, ""); | ||||
| int protrusion; | // nh index | ||||
| char gw_addr[64]; | xo_emit("{t:nh-index/%*lu} ", wid_nhidx, ext[i].nh_idx); | ||||
| struct nhop_addrs *na; | xo_emit("{t:nh-weight/%*lu} ", wid_nhidx, ext[i].nh_weight); | ||||
| struct sockaddr *sa_gw, *sa_ifa; | xo_emit("{t:nh-slots/%*lu} ", wid_nhidx, fwd_c[ext[i].nh_idx]); | ||||
| ne = nhop_get(&global_nhop_map, ext[i].nh_idx); | |||||
| if (ne != NULL) { | |||||
| xo_emit("{t:nh-gw/%*.*s}", wid_gw, wid_gw, ne->gw); | |||||
| xo_emit("{t:nh-interface/%*.*s}", wid_if, wid_if, ne->ifname); | |||||
| } | |||||
| xo_emit("\n"); | |||||
| xo_close_instance("nhop-weight"); | |||||
| } | |||||
| xo_close_list("nhop_weights"); | |||||
| xo_open_instance(name); | #if 0 | ||||
| xo_emit("{t:dummy-2/%*.*s}", wid_nhidx, wid_nhidx, ""); | |||||
| xo_emit("{t:dummy-1/%*.*s}", wid_nhidx, wid_nhidx, "----"); | |||||
| xo_emit("\n"); | |||||
| snprintf(buffer, sizeof(buffer), "{[:-%d}{:index/%%lu}{]:} ", wid_nhidx); | uint32_t *pidx; | ||||
| //xo_emit("{t:index/%-lu} ", wid_nhidx, nh->nh_idx); | pidx = (uint32_t *)&ext[mpe->mp_nh_count]; | ||||
| xo_emit(buffer, nh->nh_idx); | xo_open_list("fwd-nhops"); | ||||
| for (uint32_t i = 0; i < mpe->mp_group_size; i++) { | |||||
| xo_open_instance("fwd-nhop"); | |||||
| snprintf(buffer, sizeof(buffer), "{[:-%d}{:d-0/%%s}{]:} ", wid_nhidx); | |||||
| xo_emit(buffer, ""); | |||||
| // nh index | |||||
olivier: I have a build error here:
```
--- all_subdir_usr.bin/netstat ---
/usr/src/usr. | |||||
Done Inline ActionsShould be fixed now. melifaro: Should be fixed now. | |||||
| xo_emit("{t:nh-index/%*lu} ", wid_nhidx, pidx[i]); | |||||
| ne = nhop_get(&global_nhop_map, pidx[i]); | |||||
| if (ne != NULL) { | |||||
| xo_emit("{t:dummy-2/%*.*s}", wid_nhidx, wid_nhidx, ""); | |||||
| xo_emit("{t:nh-gw/%*.*s}", wid_gw, wid_gw, ne->gw); | |||||
| xo_emit("{t:nh-interface/%*.*s}", wid_if, wid_if, ne->ifname); | |||||
| } | |||||
| xo_emit("\n"); | |||||
| xo_close_instance("fwd-nhop"); | |||||
| } | |||||
| xo_close_list("fwd-nhops"); | |||||
| #endif | |||||
| #if 0 | |||||
| if (Wflag) { | if (Wflag) { | ||||
| char *cp = nh_types[nh->nh_type]; | char *cp = nh_types[nh->nh_type]; | ||||
| xo_emit("{t:type_str/%*s} ", wid_nhtype, cp); | xo_emit("{t:type_str/%*s} ", wid_nhtype, cp); | ||||
| } | } | ||||
| memset(iface_name, 0, sizeof(iface_name)); | memset(iface_name, 0, sizeof(iface_name)); | ||||
| if (nh->ifindex < (uint32_t)ifmap_size) { | if (nh->ifindex < (uint32_t)ifmap_size) { | ||||
| strlcpy(iface_name, ifmap[nh->ifindex].ifname, | strlcpy(iface_name, ifmap[nh->ifindex].ifname, | ||||
| sizeof(iface_name)); | sizeof(iface_name)); | ||||
| if (*iface_name == '\0') | if (*iface_name == '\0') | ||||
| strlcpy(iface_name, "---", sizeof(iface_name)); | strlcpy(iface_name, "---", sizeof(iface_name)); | ||||
| } | } | ||||
| na = (struct nhop_addrs *)((char *)nh + nh->nh_len); | |||||
| //inet_ntop(nh->nh_family, &nh->nh_src, src_addr, sizeof(src_addr)); | //inet_ntop(nh->nh_family, &nh->nh_src, src_addr, sizeof(src_addr)); | ||||
| //protrusion = p_addr("ifa", src_addr, wid_dst); | //protrusion = p_addr("ifa", src_addr, wid_dst); | ||||
| sa_gw = (struct sockaddr *)((char *)na + na->gw_sa_off); | sa_gw = (struct sockaddr *)(nh + 1); | ||||
| sa_ifa = (struct sockaddr *)((char *)na + na->src_sa_off); | sa_ifa = (struct sockaddr *)((char *)sa_gw + sa_gw->sa_len); | ||||
| protrusion = p_sockaddr("ifa", sa_ifa, NULL, RTF_HOST, wid_dst); | protrusion = p_sockaddr("ifa", sa_ifa, NULL, RTF_HOST, wid_dst); | ||||
| if (nh->nh_flags & NHF_GATEWAY) { | if (nh->nh_flags & NHF_GATEWAY) { | ||||
| const char *cp; | const char *cp; | ||||
| cp = fmt_sockaddr(sa_gw, NULL, RTF_HOST); | cp = fmt_sockaddr(sa_gw, NULL, RTF_HOST); | ||||
| strlcpy(gw_addr, cp, sizeof(gw_addr)); | strlcpy(gw_addr, cp, sizeof(gw_addr)); | ||||
| } else | } else | ||||
| snprintf(gw_addr, sizeof(gw_addr), "%s/resolve", iface_name); | snprintf(gw_addr, sizeof(gw_addr), "%s/resolve", iface_name); | ||||
| protrusion = print_addr("gateway", gw_addr, wid_dst - protrusion); | protrusion = p_addr("gateway", gw_addr, wid_dst - protrusion); | ||||
| nhop_map_update(&global_nhop_map, nh->nh_idx, gw_addr, iface_name); | |||||
| snprintf(buffer, sizeof(buffer), "{[:-%d}{:flags/%%s}{]:} ", | snprintf(buffer, sizeof(buffer), "{[:-%d}{:flags/%%s}{]:} ", | ||||
| wid_flags - protrusion); | wid_flags - protrusion); | ||||
| //p_nhflags(nh->nh_flags, buffer); | //p_nhflags(nh->nh_flags, buffer); | ||||
| print_flags_generic(rtm->rtm_flags, rt_bits, buffer, "rt_flags_pretty"); | p_flags(rtm->rtm_flags, buffer); | ||||
| if (Wflag) { | if (Wflag) { | ||||
| xo_emit("{t:use/%*lu} ", wid_pksent, nh->nh_pksent); | xo_emit("{t:use/%*lu} ", wid_pksent, nh->nh_pksent); | ||||
| xo_emit("{t:mtu/%*lu} ", wid_mtu, nh->nh_mtu); | xo_emit("{t:mtu/%*lu} ", wid_mtu, nh->nh_mtu); | ||||
| } | } | ||||
| //printf("IDX: %d IFACE: %s FAMILY: %d TYPE: %d FLAGS: %X GW \n"); | //printf("IDX: %d IFACE: %s FAMILY: %d TYPE: %d FLAGS: %X GW \n"); | ||||
| if (Wflag) | if (Wflag) | ||||
| Show All 11 Lines | #if 0 | ||||
| if (Wflag) | if (Wflag) | ||||
| xo_emit("{t:address-interface-name/%*s}", wid_if, iface_name); | xo_emit("{t:address-interface-name/%*s}", wid_if, iface_name); | ||||
| xo_emit("{t:refcount/%*lu} ", wid_refcnt, nh->nh_refcount); | xo_emit("{t:refcount/%*lu} ", wid_refcnt, nh->nh_refcount); | ||||
| if (Wflag && nh->prepend_len) { | if (Wflag && nh->prepend_len) { | ||||
| char *prepend_hex = "AABBCCDDEE"; | char *prepend_hex = "AABBCCDDEE"; | ||||
| xo_emit(" {:nhop-prepend/%*s}", wid_prepend, prepend_hex); | xo_emit(" {:nhop-prepend/%*s}", wid_prepend, prepend_hex); | ||||
| } | } | ||||
| #endif | |||||
| xo_emit("\n"); | //xo_emit("\n"); | ||||
| xo_close_instance(name); | xo_close_instance(name); | ||||
| } | } | ||||
| struct nhops_map { | |||||
| uint32_t idx; | |||||
| struct rt_msghdr *rtm; | |||||
| }; | |||||
| static int | |||||
| cmp_nh_idx(const void *_a, const void *_b) | |||||
| { | |||||
| const struct nhops_map *a, *b; | |||||
| a = _a; | |||||
| b = _b; | |||||
| if (a->idx > b->idx) | |||||
| return (1); | |||||
| else if (a->idx < b->idx) | |||||
| return (-1); | |||||
| return (0); | |||||
| } | |||||
| static void | static void | ||||
| print_nhops_sysctl(int fibnum, int af) | print_nhgrp_sysctl(int fibnum, int af) | ||||
| { | { | ||||
| size_t needed; | size_t needed; | ||||
| int mib[7]; | int mib[7]; | ||||
| char *buf, *next, *lim; | char *buf, *next, *lim; | ||||
| struct rt_msghdr *rtm; | struct rt_msghdr *rtm; | ||||
| struct nhop_external *nh; | struct mpath_external *mp; | ||||
| int fam; | int fam = AF_UNSPEC; | ||||
| struct nhops_map *nh_map; | int need_table_close = false; | ||||
| size_t nh_count, nh_size; | |||||
| mib[0] = CTL_NET; | mib[0] = CTL_NET; | ||||
| mib[1] = PF_ROUTE; | mib[1] = PF_ROUTE; | ||||
| mib[2] = 0; | mib[2] = 0; | ||||
| mib[3] = af; | mib[3] = af; | ||||
| mib[4] = NET_RT_NHOP; | mib[4] = NET_RT_NHGROUPS; | ||||
| mib[5] = 0; | mib[5] = 0; | ||||
| mib[6] = fibnum; | mib[6] = fibnum; | ||||
| if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0) | if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0) | ||||
| err(EX_OSERR, "sysctl: net.route.0.%d.nhdump.%d estimate", af, | err(EX_OSERR, "sysctl: net.route.0.%d.nhgrpdump.%d estimate", | ||||
| fibnum); | af, fibnum); | ||||
| if ((buf = malloc(needed)) == NULL) | if ((buf = malloc(needed)) == NULL) | ||||
| errx(2, "malloc(%lu)", (unsigned long)needed); | errx(2, "malloc(%lu)", (unsigned long)needed); | ||||
| if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0) | if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0) | ||||
| err(1, "sysctl: net.route.0.%d.nhdump.%d", af, fibnum); | err(1, "sysctl: net.route.0.%d.nhgrpdump.%d", af, fibnum); | ||||
| printf("BUF: %zu\n", needed); | |||||
| lim = buf + needed; | lim = buf + needed; | ||||
| xo_open_container("nhop-table"); | xo_open_container("nhgrp-table"); | ||||
| xo_open_list("rt-family"); | xo_open_list("rt-family"); | ||||
| /* | |||||
| * nexhops are received unsorted. Collect everything first, sort and then display | |||||
| * sorted. | |||||
| */ | |||||
| nh_count = 0; | |||||
| nh_size = 16; | |||||
| nh_map = calloc(nh_size, sizeof(struct nhops_map)); | |||||
| for (next = buf; next < lim; next += rtm->rtm_msglen) { | for (next = buf; next < lim; next += rtm->rtm_msglen) { | ||||
| rtm = (struct rt_msghdr *)next; | rtm = (struct rt_msghdr *)next; | ||||
| if (rtm->rtm_version != RTM_VERSION) | if (rtm->rtm_version != RTM_VERSION) | ||||
| continue; | continue; | ||||
| if (nh_count >= nh_size) { | mp = (struct mpath_external *)(rtm + 1); | ||||
| nh_size *= 2; | /* | ||||
| nh_map = realloc(nh_map, nh_size * sizeof(struct nhops_map)); | * Peek inside header to determine AF | ||||
| */ | |||||
| /* Only print family first time. */ | |||||
| if (fam != af) { | |||||
| if (need_table_close) { | |||||
| xo_close_list("nhgrp-entry"); | |||||
| xo_close_instance("rt-family"); | |||||
| } | } | ||||
| need_table_close = true; | |||||
| nh = (struct nhop_external *)(rtm + 1); | fam = af; | ||||
| nh_map[nh_count].idx = nh->nh_idx; | |||||
| nh_map[nh_count].rtm = rtm; | |||||
| nh_count++; | |||||
| } | |||||
| if (nh_count > 0) { | |||||
| qsort(nh_map, nh_count, sizeof(struct nhops_map), cmp_nh_idx); | |||||
| nh = (struct nhop_external *)(nh_map[0].rtm + 1); | |||||
| fam = nh->nh_family; | |||||
| wid_dst = WID_GW_DEFAULT(fam); | wid_dst = WID_GW_DEFAULT(fam); | ||||
| wid_gw = WID_GW_DEFAULT(fam); | wid_gw = WID_GW_DEFAULT(fam); | ||||
| wid_nhidx = 5; | wid_nhidx = 5; | ||||
| wid_nhtype = 12; | wid_nhtype = 12; | ||||
| wid_refcnt = 6; | wid_refcnt = 6; | ||||
| wid_flags = 6; | wid_flags = 6; | ||||
| wid_pksent = 8; | wid_pksent = 8; | ||||
| wid_mtu = 6; | wid_mtu = 6; | ||||
| wid_if = WID_IF_DEFAULT(fam); | wid_if = WID_IF_DEFAULT(fam); | ||||
| xo_open_instance("rt-family"); | xo_open_instance("rt-family"); | ||||
| pr_family(fam); | pr_family(fam); | ||||
| xo_open_list("nh-entry"); | xo_open_list("nhgrp-entry"); | ||||
| print_nhop_header(fam); | print_nhgroup_header(fam); | ||||
| for (size_t i = 0; i < nh_count; i++) { | |||||
| rtm = nh_map[i].rtm; | |||||
| nh = (struct nhop_external *)(rtm + 1); | |||||
| print_nhop_entry_sysctl("nh-entry", rtm, nh); | |||||
| } | } | ||||
| print_nhgroup_entry_sysctl("nhgrp-entry", rtm, mp); | |||||
| xo_close_list("nh-entry"); | } | ||||
| if (need_table_close) { | |||||
| xo_close_list("nhgrp-entry"); | |||||
| xo_close_instance("rt-family"); | xo_close_instance("rt-family"); | ||||
| } | } | ||||
| xo_close_list("rt-family"); | xo_close_list("rt-family"); | ||||
| xo_close_container("nhop-table"); | xo_close_container("nhgrp-table"); | ||||
| free(buf); | free(buf); | ||||
| } | } | ||||
| static void | static void | ||||
| p_nhflags(int f, const char *format) | update_global_map(struct nhop_external *nh) | ||||
| { | { | ||||
| struct bits *p; | char iface_name[128]; | ||||
| char *pretty_name = "nh_flags_pretty"; | char gw_addr[64]; | ||||
| struct nhop_addrs *na; | |||||
| struct sockaddr *sa_gw; | |||||
| xo_emit(format, fmt_flags(nh_bits, f)); | na = (struct nhop_addrs *)((char *)nh + nh->nh_len); | ||||
| sa_gw = (struct sockaddr *)((char *)na + na->gw_sa_off); | |||||
| xo_open_list(pretty_name); | memset(iface_name, 0, sizeof(iface_name)); | ||||
| for (p = nh_bits; p->b_mask; p++) | if (nh->ifindex < (uint32_t)ifmap_size) { | ||||
| if (p->b_mask & f) | strlcpy(iface_name, ifmap[nh->ifindex].ifname, | ||||
| xo_emit("{le:nh_flags_pretty/%s}", p->b_name); | sizeof(iface_name)); | ||||
| xo_close_list(pretty_name); | if (*iface_name == '\0') | ||||
| strlcpy(iface_name, "---", sizeof(iface_name)); | |||||
| } | } | ||||
| if (nh->nh_flags & NHF_GATEWAY) { | |||||
| const char *cp; | |||||
| cp = fmt_sockaddr(sa_gw, NULL, RTF_HOST); | |||||
| strlcpy(gw_addr, cp, sizeof(gw_addr)); | |||||
| } else | |||||
| snprintf(gw_addr, sizeof(gw_addr), "%s/resolve", iface_name); | |||||
| nhop_map_update(&global_nhop_map, nh->nh_idx, gw_addr, iface_name); | |||||
| } | |||||
| static void | |||||
| prepare_nh_map(int fibnum, int af) | |||||
| { | |||||
| struct nhops_dump nd; | |||||
| struct nhop_external *nh; | |||||
| struct rt_msghdr *rtm; | |||||
| dump_nhops_sysctl(fibnum, af, &nd); | |||||
| for (size_t i = 0; i < nd.nh_count; i++) { | |||||
| rtm = nd.nh_map[i].rtm; | |||||
| nh = (struct nhop_external *)(rtm + 1); | |||||
| update_global_map(nh); | |||||
| } | |||||
| free(nd.nh_buf); | |||||
| } | |||||
| void | void | ||||
| nhops_print(int fibnum, int af) | nhgrp_print(int fibnum, int af) | ||||
| { | { | ||||
| size_t intsize; | size_t intsize; | ||||
| int numfibs; | int numfibs; | ||||
| intsize = sizeof(int); | intsize = sizeof(int); | ||||
| if (fibnum == -1 && | if (fibnum == -1 && | ||||
| sysctlbyname("net.my_fibnum", &fibnum, &intsize, NULL, 0) == -1) | sysctlbyname("net.my_fibnum", &fibnum, &intsize, NULL, 0) == -1) | ||||
| fibnum = 0; | fibnum = 0; | ||||
| if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1) | if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1) | ||||
| numfibs = 1; | numfibs = 1; | ||||
| if (fibnum < 0 || fibnum > numfibs - 1) | if (fibnum < 0 || fibnum > numfibs - 1) | ||||
| errx(EX_USAGE, "%d: invalid fib", fibnum); | errx(EX_USAGE, "%d: invalid fib", fibnum); | ||||
| ifmap = prepare_ifmap(&ifmap_size); | ifmap = prepare_ifmap(&ifmap_size); | ||||
| prepare_nh_map(fibnum, af); | |||||
| xo_open_container("route-nhop-information"); | xo_open_container("route-nhgrp-information"); | ||||
| xo_emit("{T:Nexthop data}"); | xo_emit("{T:Nexthop groups data}"); | ||||
| if (fibnum) | if (fibnum) | ||||
| xo_emit(" ({L:fib}: {:fib/%d})", fibnum); | xo_emit(" ({L:fib}: {:fib/%d})", fibnum); | ||||
| xo_emit("\n"); | xo_emit("\n"); | ||||
| print_nhops_sysctl(fibnum, af); | print_nhgrp_sysctl(fibnum, af); | ||||
| xo_close_container("route-nhop-information"); | xo_close_container("route-nhgrp-information"); | ||||
| } | } | ||||
I have a build error here:
--- all_subdir_usr.bin/netstat --- /usr/src/usr.bin/netstat/nhgrp.c:193:11: error: use of undeclared identifier 'NET_RT_NHGROUPS' mib[4] = NET_RT_NHGROUPS;