xref: /freebsd/usr.bin/netstat/nhops.c (revision 95968ea7ec8f1cd3c1d03cc56c704cb2392d0d25)
1a6663252SAlexander V. Chernikov /*-
2a6663252SAlexander V. Chernikov  * SPDX-License-Identifier: BSD-3-Clause
3a6663252SAlexander V. Chernikov  *
4a6663252SAlexander V. Chernikov  * Copyright (c) 1983, 1988, 1993
5a6663252SAlexander V. Chernikov  *	The Regents of the University of California.  All rights reserved.
6a6663252SAlexander V. Chernikov  *
7a6663252SAlexander V. Chernikov  * Redistribution and use in source and binary forms, with or without
8a6663252SAlexander V. Chernikov  * modification, are permitted provided that the following conditions
9a6663252SAlexander V. Chernikov  * are met:
10a6663252SAlexander V. Chernikov  * 1. Redistributions of source code must retain the above copyright
11a6663252SAlexander V. Chernikov  *    notice, this list of conditions and the following disclaimer.
12a6663252SAlexander V. Chernikov  * 2. Redistributions in binary form must reproduce the above copyright
13a6663252SAlexander V. Chernikov  *    notice, this list of conditions and the following disclaimer in the
14a6663252SAlexander V. Chernikov  *    documentation and/or other materials provided with the distribution.
15a6663252SAlexander V. Chernikov  * 3. Neither the name of the University nor the names of its contributors
16a6663252SAlexander V. Chernikov  *    may be used to endorse or promote products derived from this software
17a6663252SAlexander V. Chernikov  *    without specific prior written permission.
18a6663252SAlexander V. Chernikov  *
19a6663252SAlexander V. Chernikov  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20a6663252SAlexander V. Chernikov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21a6663252SAlexander V. Chernikov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22a6663252SAlexander V. Chernikov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23a6663252SAlexander V. Chernikov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24a6663252SAlexander V. Chernikov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25a6663252SAlexander V. Chernikov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26a6663252SAlexander V. Chernikov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27a6663252SAlexander V. Chernikov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28a6663252SAlexander V. Chernikov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29a6663252SAlexander V. Chernikov  * SUCH DAMAGE.
30a6663252SAlexander V. Chernikov  */
31a6663252SAlexander V. Chernikov 
32a6663252SAlexander V. Chernikov #include <sys/param.h>
33a6663252SAlexander V. Chernikov #include <sys/protosw.h>
34a6663252SAlexander V. Chernikov #include <sys/socket.h>
35a6663252SAlexander V. Chernikov #include <sys/socketvar.h>
36a6663252SAlexander V. Chernikov #include <sys/sysctl.h>
37a6663252SAlexander V. Chernikov #include <sys/time.h>
38a6663252SAlexander V. Chernikov 
39a6663252SAlexander V. Chernikov #include <net/ethernet.h>
40a6663252SAlexander V. Chernikov #include <net/if.h>
41a6663252SAlexander V. Chernikov #include <net/if_dl.h>
42a6663252SAlexander V. Chernikov #include <net/if_types.h>
43a6663252SAlexander V. Chernikov #include <net/route.h>
44a6663252SAlexander V. Chernikov #include <net/route/nhop.h>
45a6663252SAlexander V. Chernikov 
46a6663252SAlexander V. Chernikov #include <netinet/in.h>
47a6663252SAlexander V. Chernikov #include <netgraph/ng_socket.h>
48a6663252SAlexander V. Chernikov 
49a6663252SAlexander V. Chernikov #include <arpa/inet.h>
50a6663252SAlexander V. Chernikov #include <ifaddrs.h>
51a6663252SAlexander V. Chernikov #include <libutil.h>
52a6663252SAlexander V. Chernikov #include <netdb.h>
53a6663252SAlexander V. Chernikov #include <stdbool.h>
54a6663252SAlexander V. Chernikov #include <stdint.h>
55a6663252SAlexander V. Chernikov #include <stdio.h>
56a6663252SAlexander V. Chernikov #include <stdlib.h>
57a6663252SAlexander V. Chernikov #include <stdbool.h>
58a6663252SAlexander V. Chernikov #include <string.h>
59a6663252SAlexander V. Chernikov #include <sysexits.h>
60a6663252SAlexander V. Chernikov #include <unistd.h>
61a6663252SAlexander V. Chernikov #include <libxo/xo.h>
62a6663252SAlexander V. Chernikov #include "netstat.h"
63a6663252SAlexander V. Chernikov #include "common.h"
64a6663252SAlexander V. Chernikov 
65a6663252SAlexander V. Chernikov /* column widths; each followed by one space */
66ae9c0ba8SLexi Winter #define WID_IF_DEFAULT		(Wflag ? IFNAMSIZ : 12)	/* width of netif column */
67a6663252SAlexander V. Chernikov #ifndef INET6
68a6663252SAlexander V. Chernikov #define	WID_DST_DEFAULT(af) 	18	/* width of destination column */
69a6663252SAlexander V. Chernikov #define	WID_GW_DEFAULT(af)	18	/* width of gateway column */
70a6663252SAlexander V. Chernikov #else
71a6663252SAlexander V. Chernikov #define	WID_DST_DEFAULT(af) \
72a6663252SAlexander V. Chernikov 	((af) == AF_INET6 ? (numeric_addr ? 33: 18) : 18)
73a6663252SAlexander V. Chernikov #define	WID_GW_DEFAULT(af) \
74a6663252SAlexander V. Chernikov 	((af) == AF_INET6 ? (numeric_addr ? 29 : 18) : 18)
75a6663252SAlexander V. Chernikov #endif /*INET6*/
76a6663252SAlexander V. Chernikov static int wid_dst;
77a6663252SAlexander V. Chernikov static int wid_gw;
78a6663252SAlexander V. Chernikov static int wid_flags;
79a6663252SAlexander V. Chernikov static int wid_pksent;
80a6663252SAlexander V. Chernikov static int wid_mtu;
81a6663252SAlexander V. Chernikov static int wid_if;
82a6663252SAlexander V. Chernikov static int wid_nhidx;
83a6663252SAlexander V. Chernikov static int wid_nhtype;
84a6663252SAlexander V. Chernikov static int wid_refcnt;
85a6663252SAlexander V. Chernikov static int wid_prepend;
86a6663252SAlexander V. Chernikov 
87a6663252SAlexander V. Chernikov static struct bits nh_bits[] = {
88a6663252SAlexander V. Chernikov 	{ NHF_REJECT,	'R', "reject" },
89a6663252SAlexander V. Chernikov 	{ NHF_BLACKHOLE,'B', "blackhole" },
90a6663252SAlexander V. Chernikov 	{ NHF_REDIRECT,	'r', "redirect" },
91a6663252SAlexander V. Chernikov 	{ NHF_GATEWAY,	'G', "gateway" },
92a6663252SAlexander V. Chernikov 	{ NHF_DEFAULT,	'd', "default" },
93a6663252SAlexander V. Chernikov 	{ NHF_BROADCAST,'b', "broadcast" },
94a6663252SAlexander V. Chernikov 	{ 0 , 0, NULL }
95a6663252SAlexander V. Chernikov };
96a6663252SAlexander V. Chernikov 
97a6663252SAlexander V. Chernikov static char *nh_types[] = {
98a6663252SAlexander V. Chernikov 	"empty", /* 0 */
99a6663252SAlexander V. Chernikov 	"v4/resolve", /* 1 */
100a6663252SAlexander V. Chernikov 	"v4/gw",
101a6663252SAlexander V. Chernikov 	"v6/resolve",
102a6663252SAlexander V. Chernikov 	"v6/gw"
103a6663252SAlexander V. Chernikov };
104a6663252SAlexander V. Chernikov 
105a6663252SAlexander V. Chernikov struct nhop_entry {
106a6663252SAlexander V. Chernikov 	char	gw[64];
107a6663252SAlexander V. Chernikov 	char	ifname[IFNAMSIZ];
108a6663252SAlexander V. Chernikov };
109a6663252SAlexander V. Chernikov 
110a6663252SAlexander V. Chernikov struct nhop_map {
111a6663252SAlexander V. Chernikov 	struct nhop_entry	*ptr;
112a6663252SAlexander V. Chernikov 	size_t			size;
113a6663252SAlexander V. Chernikov };
114a6663252SAlexander V. Chernikov static struct nhop_map global_nhop_map;
115a6663252SAlexander V. Chernikov 
116a6663252SAlexander V. Chernikov static struct nhop_entry *nhop_get(struct nhop_map *map, uint32_t idx);
117a6663252SAlexander V. Chernikov 
118a6663252SAlexander V. Chernikov 
119a6663252SAlexander V. Chernikov static struct ifmap_entry *ifmap;
120a6663252SAlexander V. Chernikov static size_t ifmap_size;
121a6663252SAlexander V. Chernikov 
122a6663252SAlexander V. Chernikov static void
print_sockaddr_buf(char * buf,size_t bufsize,const struct sockaddr * sa)123a6663252SAlexander V. Chernikov print_sockaddr_buf(char *buf, size_t bufsize, const struct sockaddr *sa)
124a6663252SAlexander V. Chernikov {
125a6663252SAlexander V. Chernikov 
126a6663252SAlexander V. Chernikov 	switch (sa->sa_family) {
127a6663252SAlexander V. Chernikov 	case AF_INET:
128a6663252SAlexander V. Chernikov 		inet_ntop(AF_INET, &((struct sockaddr_in *)sa)->sin_addr,
129a6663252SAlexander V. Chernikov 		    buf, bufsize);
130a6663252SAlexander V. Chernikov 		break;
131a6663252SAlexander V. Chernikov 	case AF_INET6:
132a6663252SAlexander V. Chernikov 		inet_ntop(AF_INET6, &((struct sockaddr_in6 *)sa)->sin6_addr,
133a6663252SAlexander V. Chernikov 		    buf, bufsize);
134a6663252SAlexander V. Chernikov 		break;
135a6663252SAlexander V. Chernikov 	default:
136a6663252SAlexander V. Chernikov 		snprintf(buf, bufsize, "unknown:%d", sa->sa_family);
137a6663252SAlexander V. Chernikov 		break;
138a6663252SAlexander V. Chernikov 	}
139a6663252SAlexander V. Chernikov }
140a6663252SAlexander V. Chernikov 
141a6663252SAlexander V. Chernikov static int
print_addr(const char * name,const char * addr,int width)142a6663252SAlexander V. Chernikov print_addr(const char *name, const char *addr, int width)
143a6663252SAlexander V. Chernikov {
144a6663252SAlexander V. Chernikov 	char buf[128];
145a6663252SAlexander V. Chernikov 	int protrusion;
146a6663252SAlexander V. Chernikov 
147a6663252SAlexander V. Chernikov 	if (width < 0) {
148a6663252SAlexander V. Chernikov 		snprintf(buf, sizeof(buf), "{:%s/%%s} ", name);
149a6663252SAlexander V. Chernikov 		xo_emit(buf, addr);
150a6663252SAlexander V. Chernikov 		protrusion = 0;
151a6663252SAlexander V. Chernikov 	} else {
152a6663252SAlexander V. Chernikov 		if (Wflag != 0 || numeric_addr) {
153a6663252SAlexander V. Chernikov 			snprintf(buf, sizeof(buf), "{[:%d}{:%s/%%s}{]:} ",
154a6663252SAlexander V. Chernikov 			    -width, name);
155a6663252SAlexander V. Chernikov 			xo_emit(buf, addr);
156a6663252SAlexander V. Chernikov 			protrusion = strlen(addr) - width;
157a6663252SAlexander V. Chernikov 			if (protrusion < 0)
158a6663252SAlexander V. Chernikov 				protrusion = 0;
159a6663252SAlexander V. Chernikov 		} else {
160a6663252SAlexander V. Chernikov 			snprintf(buf, sizeof(buf), "{[:%d}{:%s/%%-.*s}{]:} ",
161a6663252SAlexander V. Chernikov 			    -width, name);
162a6663252SAlexander V. Chernikov 			xo_emit(buf, width, addr);
163a6663252SAlexander V. Chernikov 			protrusion = 0;
164a6663252SAlexander V. Chernikov 		}
165a6663252SAlexander V. Chernikov 	}
166a6663252SAlexander V. Chernikov 	return (protrusion);
167a6663252SAlexander V. Chernikov }
168a6663252SAlexander V. Chernikov 
169a6663252SAlexander V. Chernikov 
170a6663252SAlexander V. Chernikov static void
print_nhop_header(int af1 __unused)171a6663252SAlexander V. Chernikov print_nhop_header(int af1 __unused)
172a6663252SAlexander V. Chernikov {
173a6663252SAlexander V. Chernikov 
174a6663252SAlexander V. Chernikov 	if (Wflag) {
175a6663252SAlexander V. Chernikov 		xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} "
176a6663252SAlexander V. Chernikov 		    "{T:/%*.*s} {T:/%-*.*s} {T:/%*.*s} {T:/%*.*s} {T:/%*.*s} {T:/%*s}\n",
177a6663252SAlexander V. Chernikov 			wid_nhidx,	wid_nhidx,	"Idx",
178a6663252SAlexander V. Chernikov 			wid_nhtype,	wid_nhtype,	"Type",
179a6663252SAlexander V. Chernikov 			wid_dst,	wid_dst,	"IFA",
180a6663252SAlexander V. Chernikov 			wid_gw,		wid_gw,		"Gateway",
181a6663252SAlexander V. Chernikov 			wid_flags,	wid_flags,	"Flags",
182a6663252SAlexander V. Chernikov 			wid_pksent,	wid_pksent,	"Use",
183a6663252SAlexander V. Chernikov 			wid_mtu,	wid_mtu,	"Mtu",
184a6663252SAlexander V. Chernikov 			wid_if,		wid_if,		"Netif",
185a6663252SAlexander V. Chernikov 			wid_if,		wid_if,		"Addrif",
186a6663252SAlexander V. Chernikov 			wid_refcnt,	wid_refcnt,	"Refcnt",
187a6663252SAlexander V. Chernikov 			wid_prepend,			"Prepend");
188a6663252SAlexander V. Chernikov 	} else {
189a6663252SAlexander V. Chernikov 		xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%*.*s} "
190a6663252SAlexander V. Chernikov 		    " {T:/%*s}\n",
191a6663252SAlexander V. Chernikov 			wid_nhidx,	wid_nhidx,	"Idx",
192a6663252SAlexander V. Chernikov 			wid_dst,	wid_dst,	"IFA",
193a6663252SAlexander V. Chernikov 			wid_gw,		wid_gw,		"Gateway",
194a6663252SAlexander V. Chernikov 			wid_flags,	wid_flags,	"Flags",
195a6663252SAlexander V. Chernikov 			wid_if,		wid_if,		"Netif",
196a6663252SAlexander V. Chernikov 			wid_prepend,			"Refcnt");
197a6663252SAlexander V. Chernikov 	}
198a6663252SAlexander V. Chernikov }
199a6663252SAlexander V. Chernikov 
200fedeb08bSAlexander V. Chernikov void
nhop_map_update(struct nhop_map * map,uint32_t idx,char * gw,char * ifname)201a6663252SAlexander V. Chernikov nhop_map_update(struct nhop_map *map, uint32_t idx, char *gw, char *ifname)
202a6663252SAlexander V. Chernikov {
203a6663252SAlexander V. Chernikov 	if (idx >= map->size) {
204a6663252SAlexander V. Chernikov 		uint32_t new_size;
205a6663252SAlexander V. Chernikov 		size_t sz;
206a6663252SAlexander V. Chernikov 		if (map->size  == 0)
207a6663252SAlexander V. Chernikov 			new_size = 32;
208a6663252SAlexander V. Chernikov 		else
209a6663252SAlexander V. Chernikov 			new_size = map->size * 2;
210a6663252SAlexander V. Chernikov 		if (new_size <= idx)
211f6b6d216SXin LI 			new_size = roundup2(idx + 1, 32);
212a6663252SAlexander V. Chernikov 
213a6663252SAlexander V. Chernikov 		sz = new_size * (sizeof(struct nhop_entry));
214a6663252SAlexander V. Chernikov 		if ((map->ptr = realloc(map->ptr, sz)) == NULL)
215*95968ea7SYan-Hao Wang 			xo_errx(EX_OSERR, "realloc(%zu) failed", sz);
216a6663252SAlexander V. Chernikov 
217a6663252SAlexander V. Chernikov 		memset(&map->ptr[map->size], 0, (new_size - map->size) * sizeof(struct nhop_entry));
218a6663252SAlexander V. Chernikov 		map->size = new_size;
219a6663252SAlexander V. Chernikov 	}
220a6663252SAlexander V. Chernikov 
221a6663252SAlexander V. Chernikov 	strlcpy(map->ptr[idx].ifname, ifname, sizeof(map->ptr[idx].ifname));
222a6663252SAlexander V. Chernikov 	strlcpy(map->ptr[idx].gw, gw, sizeof(map->ptr[idx].gw));
223a6663252SAlexander V. Chernikov }
224a6663252SAlexander V. Chernikov 
225a6663252SAlexander V. Chernikov static struct nhop_entry *
nhop_get(struct nhop_map * map,uint32_t idx)226a6663252SAlexander V. Chernikov nhop_get(struct nhop_map *map, uint32_t idx)
227a6663252SAlexander V. Chernikov {
228a6663252SAlexander V. Chernikov 
229a6663252SAlexander V. Chernikov 	if (idx >= map->size)
230a6663252SAlexander V. Chernikov 		return (NULL);
231a6663252SAlexander V. Chernikov 	if (*map->ptr[idx].ifname == '\0')
232a6663252SAlexander V. Chernikov 		return (NULL);
233a6663252SAlexander V. Chernikov 	return &map->ptr[idx];
234a6663252SAlexander V. Chernikov }
235a6663252SAlexander V. Chernikov 
236a6663252SAlexander V. Chernikov static void
print_nhop_entry_sysctl(const char * name,struct rt_msghdr * rtm,struct nhop_external * nh)237a6663252SAlexander V. Chernikov print_nhop_entry_sysctl(const char *name, struct rt_msghdr *rtm, struct nhop_external *nh)
238a6663252SAlexander V. Chernikov {
239a6663252SAlexander V. Chernikov 	char buffer[128];
240a6663252SAlexander V. Chernikov 	char iface_name[128];
241a6663252SAlexander V. Chernikov 	int protrusion;
242a6663252SAlexander V. Chernikov 	char gw_addr[64];
243a6663252SAlexander V. Chernikov 	struct nhop_addrs *na;
244a6663252SAlexander V. Chernikov 	struct sockaddr *sa_gw, *sa_ifa;
245a6663252SAlexander V. Chernikov 
246a6663252SAlexander V. Chernikov 	xo_open_instance(name);
247a6663252SAlexander V. Chernikov 
248a6663252SAlexander V. Chernikov 	snprintf(buffer, sizeof(buffer), "{[:-%d}{:index/%%lu}{]:} ", wid_nhidx);
249a6663252SAlexander V. Chernikov 	//xo_emit("{t:index/%-lu} ", wid_nhidx, nh->nh_idx);
250a6663252SAlexander V. Chernikov 	xo_emit(buffer, nh->nh_idx);
251a6663252SAlexander V. Chernikov 
252a6663252SAlexander V. Chernikov 	if (Wflag) {
253a6663252SAlexander V. Chernikov 		char *cp = nh_types[nh->nh_type];
254a6663252SAlexander V. Chernikov 		xo_emit("{t:type_str/%*s} ", wid_nhtype, cp);
255a6663252SAlexander V. Chernikov 	}
256a6663252SAlexander V. Chernikov 	memset(iface_name, 0, sizeof(iface_name));
257a6663252SAlexander V. Chernikov 	if (nh->ifindex < (uint32_t)ifmap_size) {
258a6663252SAlexander V. Chernikov 		strlcpy(iface_name, ifmap[nh->ifindex].ifname,
259a6663252SAlexander V. Chernikov 		    sizeof(iface_name));
260a6663252SAlexander V. Chernikov 		if (*iface_name == '\0')
261a6663252SAlexander V. Chernikov 			strlcpy(iface_name, "---", sizeof(iface_name));
262a6663252SAlexander V. Chernikov 	}
263a6663252SAlexander V. Chernikov 
264a6663252SAlexander V. Chernikov 	na = (struct nhop_addrs *)((char *)nh + nh->nh_len);
265a6663252SAlexander V. Chernikov 	//inet_ntop(nh->nh_family, &nh->nh_src, src_addr, sizeof(src_addr));
266a6663252SAlexander V. Chernikov 	//protrusion = p_addr("ifa", src_addr, wid_dst);
267a6663252SAlexander V. Chernikov 	sa_gw = (struct sockaddr *)((char *)na + na->gw_sa_off);
268a6663252SAlexander V. Chernikov 	sa_ifa = (struct sockaddr *)((char *)na + na->src_sa_off);
269a6663252SAlexander V. Chernikov 	protrusion = p_sockaddr("ifa", sa_ifa, NULL, RTF_HOST, wid_dst);
270a6663252SAlexander V. Chernikov 
271a6663252SAlexander V. Chernikov 	if (nh->nh_flags & NHF_GATEWAY) {
272a6663252SAlexander V. Chernikov 		const char *cp;
273a6663252SAlexander V. Chernikov 		cp = fmt_sockaddr(sa_gw, NULL, RTF_HOST);
274a6663252SAlexander V. Chernikov 		strlcpy(gw_addr, cp, sizeof(gw_addr));
275a6663252SAlexander V. Chernikov 	} else
276a6663252SAlexander V. Chernikov 		snprintf(gw_addr, sizeof(gw_addr), "%s/resolve", iface_name);
277a6663252SAlexander V. Chernikov 	protrusion = print_addr("gateway", gw_addr, wid_dst - protrusion);
278a6663252SAlexander V. Chernikov 
279a6663252SAlexander V. Chernikov 	nhop_map_update(&global_nhop_map, nh->nh_idx, gw_addr, iface_name);
280a6663252SAlexander V. Chernikov 
281a6663252SAlexander V. Chernikov 	snprintf(buffer, sizeof(buffer), "{[:-%d}{:flags/%%s}{]:} ",
282a6663252SAlexander V. Chernikov 	    wid_flags - protrusion);
283a6663252SAlexander V. Chernikov 
284a6663252SAlexander V. Chernikov 	//p_nhflags(nh->nh_flags, buffer);
285a6663252SAlexander V. Chernikov 	print_flags_generic(rtm->rtm_flags, rt_bits, buffer, "rt_flags_pretty");
286a6663252SAlexander V. Chernikov 
287a6663252SAlexander V. Chernikov 	if (Wflag) {
288a6663252SAlexander V. Chernikov 		xo_emit("{t:use/%*lu} ", wid_pksent, nh->nh_pksent);
289a6663252SAlexander V. Chernikov 		xo_emit("{t:mtu/%*lu} ", wid_mtu, nh->nh_mtu);
290a6663252SAlexander V. Chernikov 	}
291a6663252SAlexander V. Chernikov 	//printf("IDX: %d IFACE: %s FAMILY: %d TYPE: %d FLAGS: %X GW \n");
292a6663252SAlexander V. Chernikov 
293a6663252SAlexander V. Chernikov 	if (Wflag)
294a6663252SAlexander V. Chernikov 		xo_emit("{t:interface-name/%*s}", wid_if, iface_name);
295a6663252SAlexander V. Chernikov 	else
296a6663252SAlexander V. Chernikov 		xo_emit("{t:interface-name/%*.*s}", wid_if, wid_if, iface_name);
297a6663252SAlexander V. Chernikov 
298a6663252SAlexander V. Chernikov 	memset(iface_name, 0, sizeof(iface_name));
299a6663252SAlexander V. Chernikov 	if (nh->aifindex < (uint32_t)ifmap_size && nh->ifindex != nh->aifindex) {
300a6663252SAlexander V. Chernikov 		strlcpy(iface_name, ifmap[nh->aifindex].ifname,
301a6663252SAlexander V. Chernikov 		    sizeof(iface_name));
302a6663252SAlexander V. Chernikov 		if (*iface_name == '\0')
303a6663252SAlexander V. Chernikov 			strlcpy(iface_name, "---", sizeof(iface_name));
304a6663252SAlexander V. Chernikov 	}
305a6663252SAlexander V. Chernikov 	if (Wflag)
306a6663252SAlexander V. Chernikov 		xo_emit("{t:address-interface-name/%*s}", wid_if, iface_name);
307a6663252SAlexander V. Chernikov 
308a6663252SAlexander V. Chernikov 	xo_emit("{t:refcount/%*lu} ", wid_refcnt, nh->nh_refcount);
309a6663252SAlexander V. Chernikov 	if (Wflag && nh->prepend_len) {
310731bfa9fSAlexander V. Chernikov 		int max_bytes = MIN(nh->prepend_len, sizeof(buffer) / 2 - 1);
311731bfa9fSAlexander V. Chernikov 		for (int i = 0; i < max_bytes; i++)
312731bfa9fSAlexander V. Chernikov 			snprintf(&buffer[i * 2], 3, "%02X", nh->nh_prepend[i]);
313731bfa9fSAlexander V. Chernikov 		xo_emit(" {:nhop-prepend/%*s}", wid_prepend, buffer);
314a6663252SAlexander V. Chernikov 	}
315a6663252SAlexander V. Chernikov 
316a6663252SAlexander V. Chernikov 	xo_emit("\n");
317a6663252SAlexander V. Chernikov 	xo_close_instance(name);
318a6663252SAlexander V. Chernikov }
319a6663252SAlexander V. Chernikov 
320a6663252SAlexander V. Chernikov static int
cmp_nh_idx(const void * _a,const void * _b)321a6663252SAlexander V. Chernikov cmp_nh_idx(const void *_a, const void *_b)
322a6663252SAlexander V. Chernikov {
323a6663252SAlexander V. Chernikov 	const struct nhops_map *a, *b;
324a6663252SAlexander V. Chernikov 
325a6663252SAlexander V. Chernikov 	a = _a;
326a6663252SAlexander V. Chernikov 	b = _b;
327a6663252SAlexander V. Chernikov 
328a6663252SAlexander V. Chernikov 	if (a->idx > b->idx)
329a6663252SAlexander V. Chernikov 		return (1);
330a6663252SAlexander V. Chernikov 	else if (a->idx < b->idx)
331a6663252SAlexander V. Chernikov 		return (-1);
332a6663252SAlexander V. Chernikov 	return (0);
333a6663252SAlexander V. Chernikov }
334a6663252SAlexander V. Chernikov 
335fedeb08bSAlexander V. Chernikov void
dump_nhops_sysctl(int fibnum,int af,struct nhops_dump * nd)336fedeb08bSAlexander V. Chernikov dump_nhops_sysctl(int fibnum, int af, struct nhops_dump *nd)
337a6663252SAlexander V. Chernikov {
338a6663252SAlexander V. Chernikov 	size_t needed;
339a6663252SAlexander V. Chernikov 	int mib[7];
340a6663252SAlexander V. Chernikov 	char *buf, *next, *lim;
341a6663252SAlexander V. Chernikov 	struct rt_msghdr *rtm;
342a6663252SAlexander V. Chernikov 	struct nhop_external *nh;
343a6663252SAlexander V. Chernikov 	struct nhops_map *nh_map;
344a6663252SAlexander V. Chernikov 	size_t nh_count, nh_size;
345a6663252SAlexander V. Chernikov 
346a6663252SAlexander V. Chernikov 	mib[0] = CTL_NET;
347a6663252SAlexander V. Chernikov 	mib[1] = PF_ROUTE;
348a6663252SAlexander V. Chernikov 	mib[2] = 0;
349a6663252SAlexander V. Chernikov 	mib[3] = af;
350a6663252SAlexander V. Chernikov 	mib[4] = NET_RT_NHOP;
351a6663252SAlexander V. Chernikov 	mib[5] = 0;
352a6663252SAlexander V. Chernikov 	mib[6] = fibnum;
353a6663252SAlexander V. Chernikov 	if (sysctl(mib, nitems(mib), NULL, &needed, NULL, 0) < 0)
354*95968ea7SYan-Hao Wang 		xo_err(EX_OSERR, "sysctl: net.route.0.%d.nhdump.%d estimate", af,
355a6663252SAlexander V. Chernikov 		    fibnum);
356a6663252SAlexander V. Chernikov 	if ((buf = malloc(needed)) == NULL)
357*95968ea7SYan-Hao Wang 		xo_errx(EX_OSERR, "malloc(%lu)", (unsigned long)needed);
358a6663252SAlexander V. Chernikov 	if (sysctl(mib, nitems(mib), buf, &needed, NULL, 0) < 0)
359*95968ea7SYan-Hao Wang 		xo_err(EX_OSERR, "sysctl: net.route.0.%d.nhdump.%d", af, fibnum);
360a6663252SAlexander V. Chernikov 	lim  = buf + needed;
361a6663252SAlexander V. Chernikov 
362a6663252SAlexander V. Chernikov 	/*
363a6663252SAlexander V. Chernikov 	 * nexhops are received unsorted. Collect everything first, sort and then display
364a6663252SAlexander V. Chernikov 	 * sorted.
365a6663252SAlexander V. Chernikov 	 */
366a6663252SAlexander V. Chernikov 	nh_count = 0;
367a6663252SAlexander V. Chernikov 	nh_size = 16;
368a6663252SAlexander V. Chernikov 	nh_map = calloc(nh_size, sizeof(struct nhops_map));
369a6663252SAlexander V. Chernikov 	for (next = buf; next < lim; next += rtm->rtm_msglen) {
370a6663252SAlexander V. Chernikov 		rtm = (struct rt_msghdr *)next;
371a6663252SAlexander V. Chernikov 		if (rtm->rtm_version != RTM_VERSION)
372a6663252SAlexander V. Chernikov 			continue;
373a6663252SAlexander V. Chernikov 
374a6663252SAlexander V. Chernikov 		if (nh_count >= nh_size) {
375a6663252SAlexander V. Chernikov 			nh_size *= 2;
376a6663252SAlexander V. Chernikov 			nh_map = realloc(nh_map, nh_size * sizeof(struct nhops_map));
377a6663252SAlexander V. Chernikov 		}
378a6663252SAlexander V. Chernikov 
379a6663252SAlexander V. Chernikov 		nh = (struct nhop_external *)(rtm + 1);
380a6663252SAlexander V. Chernikov 		nh_map[nh_count].idx = nh->nh_idx;
381a6663252SAlexander V. Chernikov 		nh_map[nh_count].rtm = rtm;
382a6663252SAlexander V. Chernikov 		nh_count++;
383a6663252SAlexander V. Chernikov 	}
384a6663252SAlexander V. Chernikov 
385fedeb08bSAlexander V. Chernikov 	if (nh_count > 0)
386a6663252SAlexander V. Chernikov 		qsort(nh_map, nh_count, sizeof(struct nhops_map), cmp_nh_idx);
387fedeb08bSAlexander V. Chernikov 	nd->nh_buf = buf;
388fedeb08bSAlexander V. Chernikov 	nd->nh_count = nh_count;
389fedeb08bSAlexander V. Chernikov 	nd->nh_map = nh_map;
390fedeb08bSAlexander V. Chernikov }
391fedeb08bSAlexander V. Chernikov 
392fedeb08bSAlexander V. Chernikov static void
print_nhops_sysctl(int fibnum,int af)393fedeb08bSAlexander V. Chernikov print_nhops_sysctl(int fibnum, int af)
394fedeb08bSAlexander V. Chernikov {
395fedeb08bSAlexander V. Chernikov 	struct nhops_dump nd;
396fedeb08bSAlexander V. Chernikov 	struct nhop_external *nh;
397fedeb08bSAlexander V. Chernikov 	int fam;
398fedeb08bSAlexander V. Chernikov 	struct rt_msghdr *rtm;
399fedeb08bSAlexander V. Chernikov 
400fedeb08bSAlexander V. Chernikov 	dump_nhops_sysctl(fibnum, af, &nd);
401fedeb08bSAlexander V. Chernikov 
402fedeb08bSAlexander V. Chernikov 	xo_open_container("nhop-table");
403fedeb08bSAlexander V. Chernikov 	xo_open_list("rt-family");
404fedeb08bSAlexander V. Chernikov 	if (nd.nh_count > 0) {
405fedeb08bSAlexander V. Chernikov 		nh = (struct nhop_external *)(nd.nh_map[0].rtm + 1);
406a6663252SAlexander V. Chernikov 		fam = nh->nh_family;
407a6663252SAlexander V. Chernikov 
408a6663252SAlexander V. Chernikov 		wid_dst = WID_GW_DEFAULT(fam);
409a6663252SAlexander V. Chernikov 		wid_gw = WID_GW_DEFAULT(fam);
410a6663252SAlexander V. Chernikov 		wid_nhidx = 5;
411a6663252SAlexander V. Chernikov 		wid_nhtype = 12;
412a6663252SAlexander V. Chernikov 		wid_refcnt = 6;
413a6663252SAlexander V. Chernikov 		wid_flags = 6;
414a6663252SAlexander V. Chernikov 		wid_pksent = 8;
415a6663252SAlexander V. Chernikov 		wid_mtu = 6;
416d33b87e8SLexi Winter 		wid_if = WID_IF_DEFAULT;
417a6663252SAlexander V. Chernikov 		xo_open_instance("rt-family");
418a6663252SAlexander V. Chernikov 		pr_family(fam);
419a6663252SAlexander V. Chernikov 		xo_open_list("nh-entry");
420a6663252SAlexander V. Chernikov 
421a6663252SAlexander V. Chernikov 		print_nhop_header(fam);
422a6663252SAlexander V. Chernikov 
423fedeb08bSAlexander V. Chernikov 		for (size_t i = 0; i < nd.nh_count; i++) {
424fedeb08bSAlexander V. Chernikov 			rtm = nd.nh_map[i].rtm;
425a6663252SAlexander V. Chernikov 			nh = (struct nhop_external *)(rtm + 1);
426a6663252SAlexander V. Chernikov 			print_nhop_entry_sysctl("nh-entry", rtm, nh);
427a6663252SAlexander V. Chernikov 		}
428a6663252SAlexander V. Chernikov 
429a6663252SAlexander V. Chernikov 		xo_close_list("nh-entry");
430a6663252SAlexander V. Chernikov 		xo_close_instance("rt-family");
431a6663252SAlexander V. Chernikov 	}
432a6663252SAlexander V. Chernikov 	xo_close_list("rt-family");
433a6663252SAlexander V. Chernikov 	xo_close_container("nhop-table");
434fedeb08bSAlexander V. Chernikov 	free(nd.nh_buf);
435a6663252SAlexander V. Chernikov }
436a6663252SAlexander V. Chernikov 
437a6663252SAlexander V. Chernikov static void
p_nhflags(int f,const char * format)438a6663252SAlexander V. Chernikov p_nhflags(int f, const char *format)
439a6663252SAlexander V. Chernikov {
440a6663252SAlexander V. Chernikov 	struct bits *p;
441a6663252SAlexander V. Chernikov 	char *pretty_name = "nh_flags_pretty";
442a6663252SAlexander V. Chernikov 
443a6663252SAlexander V. Chernikov 	xo_emit(format, fmt_flags(nh_bits, f));
444a6663252SAlexander V. Chernikov 
445a6663252SAlexander V. Chernikov 	xo_open_list(pretty_name);
446a6663252SAlexander V. Chernikov 	for (p = nh_bits; p->b_mask; p++)
447a6663252SAlexander V. Chernikov 		if (p->b_mask & f)
448a6663252SAlexander V. Chernikov 			xo_emit("{le:nh_flags_pretty/%s}", p->b_name);
449a6663252SAlexander V. Chernikov 	xo_close_list(pretty_name);
450a6663252SAlexander V. Chernikov }
451a6663252SAlexander V. Chernikov 
452a6663252SAlexander V. Chernikov void
nhops_print(int fibnum,int af)453a6663252SAlexander V. Chernikov nhops_print(int fibnum, int af)
454a6663252SAlexander V. Chernikov {
455a6663252SAlexander V. Chernikov 	size_t intsize;
456a6663252SAlexander V. Chernikov 	int numfibs;
457a6663252SAlexander V. Chernikov 
458a6663252SAlexander V. Chernikov 	intsize = sizeof(int);
459a6663252SAlexander V. Chernikov 	if (fibnum == -1 &&
460a6663252SAlexander V. Chernikov 	    sysctlbyname("net.my_fibnum", &fibnum, &intsize, NULL, 0) == -1)
461a6663252SAlexander V. Chernikov 		fibnum = 0;
462a6663252SAlexander V. Chernikov 	if (sysctlbyname("net.fibs", &numfibs, &intsize, NULL, 0) == -1)
463a6663252SAlexander V. Chernikov 		numfibs = 1;
464a6663252SAlexander V. Chernikov 	if (fibnum < 0 || fibnum > numfibs - 1)
465*95968ea7SYan-Hao Wang 		xo_errx(EX_USAGE, "%d: invalid fib", fibnum);
466a6663252SAlexander V. Chernikov 
467a6663252SAlexander V. Chernikov 	ifmap = prepare_ifmap(&ifmap_size);
468a6663252SAlexander V. Chernikov 
469a6663252SAlexander V. Chernikov 	xo_open_container("route-nhop-information");
470a6663252SAlexander V. Chernikov 	xo_emit("{T:Nexthop data}");
471a6663252SAlexander V. Chernikov 	if (fibnum)
472a6663252SAlexander V. Chernikov 		xo_emit(" ({L:fib}: {:fib/%d})", fibnum);
473a6663252SAlexander V. Chernikov 	xo_emit("\n");
474a6663252SAlexander V. Chernikov 	print_nhops_sysctl(fibnum, af);
475a6663252SAlexander V. Chernikov 	xo_close_container("route-nhop-information");
476a6663252SAlexander V. Chernikov }
477a6663252SAlexander V. Chernikov 
478