rtutil.c revision 1.3 1 1.3 christos /* $NetBSD: rtutil.c,v 1.3 2014/11/12 03:34:08 christos Exp $ */
2 1.1 christos /* $OpenBSD: show.c,v 1.1 2006/05/27 19:16:37 claudio Exp $ */
3 1.1 christos
4 1.1 christos /*
5 1.1 christos * Copyright (c) 1983, 1988, 1993
6 1.1 christos * The Regents of the University of California. All rights reserved.
7 1.1 christos *
8 1.1 christos * Redistribution and use in source and binary forms, with or without
9 1.1 christos * modification, are permitted provided that the following conditions
10 1.1 christos * are met:
11 1.1 christos * 1. Redistributions of source code must retain the above copyright
12 1.1 christos * notice, this list of conditions and the following disclaimer.
13 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 christos * notice, this list of conditions and the following disclaimer in the
15 1.1 christos * documentation and/or other materials provided with the distribution.
16 1.1 christos * 3. Neither the name of the University nor the names of its contributors
17 1.1 christos * may be used to endorse or promote products derived from this software
18 1.1 christos * without specific prior written permission.
19 1.1 christos *
20 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1 christos * SUCH DAMAGE.
31 1.1 christos */
32 1.1 christos
33 1.1 christos #include <sys/param.h>
34 1.1 christos #include <sys/protosw.h>
35 1.1 christos #include <sys/socket.h>
36 1.1 christos #include <sys/mbuf.h>
37 1.1 christos #include <sys/sysctl.h>
38 1.1 christos
39 1.1 christos #include <net/if.h>
40 1.1 christos #include <net/if_dl.h>
41 1.1 christos #include <net/if_types.h>
42 1.1 christos #include <net/pfvar.h>
43 1.1 christos #include <net/pfkeyv2.h>
44 1.1 christos #include <net/route.h>
45 1.1 christos #include <netinet/in.h>
46 1.1 christos #include <netinet/if_ether.h>
47 1.1 christos #include <netatalk/at.h>
48 1.1 christos #include <netmpls/mpls.h>
49 1.1 christos #include <arpa/inet.h>
50 1.1 christos
51 1.1 christos #include <err.h>
52 1.1 christos #include <errno.h>
53 1.1 christos #include <netdb.h>
54 1.1 christos #include <stdio.h>
55 1.1 christos #include <stddef.h>
56 1.1 christos #include <stdlib.h>
57 1.1 christos #include <string.h>
58 1.1 christos #include <unistd.h>
59 1.1 christos
60 1.1 christos #include "prog_ops.h"
61 1.1 christos #include "rtutil.h"
62 1.1 christos
63 1.1 christos
64 1.1 christos #define PLEN (LONG_BIT / 4 + 2)
65 1.1 christos #define PFKEYV2_CHUNK sizeof(u_int64_t)
66 1.1 christos static char *link_print(const struct sockaddr *);
67 1.1 christos
68 1.1 christos /*
69 1.1 christos * Definitions for showing gateway flags.
70 1.1 christos */
71 1.1 christos struct bits {
72 1.1 christos int b_mask;
73 1.1 christos char b_val;
74 1.1 christos };
75 1.1 christos static const struct bits bits[] = {
76 1.1 christos { RTF_UP, 'U' },
77 1.1 christos { RTF_GATEWAY, 'G' },
78 1.1 christos { RTF_HOST, 'H' },
79 1.1 christos { RTF_REJECT, 'R' },
80 1.1 christos { RTF_BLACKHOLE, 'B' },
81 1.1 christos { RTF_DYNAMIC, 'D' },
82 1.1 christos { RTF_MODIFIED, 'M' },
83 1.1 christos { RTF_DONE, 'd' }, /* Completed -- for routing messages only */
84 1.1 christos { RTF_MASK, 'm' }, /* Mask Present -- for routing messages only */
85 1.1 christos { RTF_CLONING, 'C' },
86 1.1 christos { RTF_XRESOLVE, 'X' },
87 1.1 christos { RTF_LLINFO, 'L' },
88 1.1 christos { RTF_STATIC, 'S' },
89 1.1 christos { RTF_PROTO1, '1' },
90 1.1 christos { RTF_PROTO2, '2' },
91 1.1 christos /* { RTF_PROTO3, '3' }, */
92 1.1 christos { RTF_CLONED, 'c' },
93 1.1 christos /* { RTF_JUMBO, 'J' }, */
94 1.1 christos { RTF_ANNOUNCE, 'p' },
95 1.1 christos { 0, 0 }
96 1.1 christos };
97 1.1 christos
98 1.1 christos #ifndef SMALL
99 1.1 christos static void p_tag(const struct sockaddr *sa);
100 1.1 christos #endif
101 1.1 christos static void p_rtentry(struct rt_msghdr *, int, int);
102 1.1 christos
103 1.1 christos /*
104 1.1 christos * Print routing tables.
105 1.1 christos */
106 1.1 christos void
107 1.1 christos p_rttables(int paf, int flags, int pflags, int interesting)
108 1.1 christos {
109 1.1 christos struct rt_msghdr *rtm;
110 1.1 christos char *buf = NULL, *next, *lim = NULL;
111 1.1 christos size_t needed;
112 1.1 christos int mib[6];
113 1.1 christos struct sockaddr *sa;
114 1.1 christos
115 1.1 christos mib[0] = CTL_NET;
116 1.1 christos mib[1] = PF_ROUTE;
117 1.1 christos mib[2] = 0;
118 1.1 christos mib[3] = paf;
119 1.1 christos mib[4] = NET_RT_DUMP;
120 1.1 christos mib[5] = 0;
121 1.1 christos if (prog_sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
122 1.1 christos err(1, "route-sysctl-estimate");
123 1.1 christos if (needed > 0) {
124 1.1 christos if ((buf = malloc(needed)) == 0)
125 1.1 christos err(1, NULL);
126 1.1 christos if (prog_sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
127 1.1 christos err(1, "sysctl of routing table");
128 1.1 christos lim = buf + needed;
129 1.1 christos }
130 1.1 christos
131 1.1 christos printf("Routing tables\n");
132 1.1 christos
133 1.1 christos if (buf) {
134 1.1 christos for (next = buf; next < lim; next += rtm->rtm_msglen) {
135 1.1 christos rtm = (struct rt_msghdr *)next;
136 1.1 christos sa = (struct sockaddr *)(rtm + 1);
137 1.1 christos if ((rtm->rtm_flags & pflags) != pflags)
138 1.1 christos continue;
139 1.1 christos if (paf != AF_UNSPEC && sa->sa_family != paf)
140 1.1 christos continue;
141 1.1 christos p_rtentry(rtm, flags, interesting);
142 1.1 christos }
143 1.1 christos free(buf);
144 1.1 christos buf = NULL;
145 1.1 christos }
146 1.1 christos
147 1.1 christos if (paf != 0 && paf != PF_KEY)
148 1.1 christos return;
149 1.1 christos
150 1.1 christos #if 0 /* XXX-elad */
151 1.1 christos mib[0] = CTL_NET;
152 1.1 christos mib[1] = PF_KEY;
153 1.1 christos mib[2] = PF_KEY_V2;
154 1.1 christos mib[3] = NET_KEY_SPD_DUMP;
155 1.1 christos mib[4] = mib[5] = 0;
156 1.1 christos
157 1.1 christos if (prog_sysctl(mib, 4, NULL, &needed, NULL, 0) == -1) {
158 1.1 christos if (errno == ENOPROTOOPT)
159 1.1 christos return;
160 1.1 christos err(1, "spd-sysctl-estimate");
161 1.1 christos }
162 1.1 christos if (needed > 0) {
163 1.1 christos if ((buf = malloc(needed)) == 0)
164 1.1 christos err(1, NULL);
165 1.1 christos if (prog_sysctl(mib, 4, buf, &needed, NULL, 0) == -1)
166 1.1 christos err(1,"sysctl of spd");
167 1.1 christos lim = buf + needed;
168 1.1 christos }
169 1.1 christos
170 1.1 christos if (buf) {
171 1.1 christos printf("\nEncap:\n");
172 1.1 christos
173 1.1 christos for (next = buf; next < lim; next += msg->sadb_msg_len *
174 1.1 christos PFKEYV2_CHUNK) {
175 1.1 christos msg = (struct sadb_msg *)next;
176 1.1 christos if (msg->sadb_msg_len == 0)
177 1.1 christos break;
178 1.1 christos p_pfkentry(msg);
179 1.1 christos }
180 1.1 christos free(buf);
181 1.1 christos buf = NULL;
182 1.1 christos }
183 1.1 christos #endif /* 0 */
184 1.1 christos }
185 1.1 christos
186 1.1 christos /*
187 1.1 christos * column widths; each followed by one space
188 1.1 christos * width of destination/gateway column
189 1.2 christos * strlen("fe80::aaaa:bbbb:cccc:dddd@gif0") == 30, strlen("/128") == 4 = 34
190 1.2 christos * strlen("aaaa:bbbb:cccc:dddd:eeee:ffff:gggg:hhhh") == 39
191 1.1 christos */
192 1.1 christos #ifndef INET6
193 1.1 christos #define WID_DST(af) 18 /* width of destination column */
194 1.1 christos #define WID_GW(af) 18 /* width of gateway column */
195 1.1 christos #else
196 1.2 christos #define WID_DST(af) ((af) == AF_INET6 ? ((flags & RT_NFLAG) ? 39 : 18) : 18)
197 1.1 christos #define WID_GW(af) ((af) == AF_INET6 ? ((flags & RT_NFLAG) ? 30 : 18) : 18)
198 1.1 christos #endif
199 1.1 christos
200 1.1 christos /*
201 1.1 christos * Print header for routing table columns.
202 1.1 christos */
203 1.1 christos void
204 1.1 christos p_rthdr(int paf, int flags)
205 1.1 christos {
206 1.1 christos #ifndef SMALL
207 1.1 christos if (flags & RT_AFLAG)
208 1.1 christos printf("%-*.*s ", PLEN, PLEN, "Address");
209 1.1 christos if (paf == PF_KEY) {
210 1.1 christos printf("%-18s %-5s %-18s %-5s %-5s %-22s\n",
211 1.1 christos "Source", "Port", "Destination",
212 1.1 christos "Port", "Proto", "SA(Address/Proto/Type/Direction)");
213 1.1 christos return;
214 1.1 christos }
215 1.1 christos if (flags & RT_TFLAG) {
216 1.1 christos printf("%-*.*s %-*.*s %-6.6s %6.6s %8.8s %6.6s %7.7s"
217 1.1 christos " %s\n", WID_DST(paf), WID_DST(paf), "Destination",
218 1.1 christos WID_GW(paf), WID_GW(paf), "Gateway",
219 1.1 christos "Flags", "Refs", "Use", "Mtu", "Tag", "Interface");
220 1.1 christos return;
221 1.1 christos }
222 1.1 christos #endif
223 1.1 christos #ifndef SMALL
224 1.1 christos printf("%-*.*s %-*.*s %-6.6s %6.6s %8.8s %6.6s %s\n",
225 1.1 christos WID_DST(paf), WID_DST(paf), "Destination",
226 1.1 christos WID_GW(paf), WID_GW(paf), "Gateway",
227 1.1 christos "Flags", "Refs", "Use", "Mtu", "Interface");
228 1.1 christos #else
229 1.1 christos printf("%-*.*s %-*.*s %-6.6s\n",
230 1.1 christos WID_DST(paf), WID_DST(paf), "Destination",
231 1.1 christos WID_GW(paf), WID_GW(paf), "Gateway",
232 1.1 christos "Flags");
233 1.1 christos #endif
234 1.1 christos }
235 1.1 christos
236 1.1 christos static void
237 1.1 christos get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
238 1.1 christos {
239 1.1 christos int i;
240 1.1 christos
241 1.1 christos for (i = 0; i < RTAX_MAX; i++) {
242 1.1 christos if (addrs & (1 << i)) {
243 1.1 christos rti_info[i] = sa;
244 1.1 christos sa = (struct sockaddr *)((char *)(sa) +
245 1.1 christos RT_ROUNDUP(sa->sa_len));
246 1.1 christos } else
247 1.1 christos rti_info[i] = NULL;
248 1.1 christos }
249 1.1 christos }
250 1.1 christos
251 1.1 christos /*
252 1.1 christos * Print a routing table entry.
253 1.1 christos */
254 1.1 christos static void
255 1.1 christos p_rtentry(struct rt_msghdr *rtm, int flags, int interesting)
256 1.1 christos {
257 1.1 christos static int old_af = -1;
258 1.1 christos struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
259 1.1 christos struct sockaddr *mask, *rti_info[RTAX_MAX];
260 1.1 christos #ifndef SMALL
261 1.1 christos char ifbuf[IF_NAMESIZE];
262 1.1 christos #endif
263 1.1 christos
264 1.3 christos if ((flags & RT_LFLAG) && (rtm->rtm_flags & RTF_LLINFO))
265 1.3 christos return;
266 1.3 christos
267 1.1 christos if (old_af != sa->sa_family) {
268 1.1 christos old_af = sa->sa_family;
269 1.1 christos p_family(sa->sa_family);
270 1.1 christos p_rthdr(sa->sa_family, flags);
271 1.1 christos }
272 1.1 christos get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
273 1.1 christos
274 1.1 christos mask = rti_info[RTAX_NETMASK];
275 1.1 christos if ((sa = rti_info[RTAX_DST]) == NULL)
276 1.1 christos return;
277 1.1 christos
278 1.1 christos p_sockaddr(sa, mask, rtm->rtm_flags, WID_DST(sa->sa_family), flags);
279 1.1 christos p_sockaddr(rti_info[RTAX_GATEWAY], NULL, RTF_HOST,
280 1.1 christos WID_GW(sa->sa_family), flags);
281 1.1 christos p_flags(rtm->rtm_flags & interesting);
282 1.1 christos #if 0 /* XXX-elad */
283 1.1 christos printf("%6d %8"PRId64" ", (int)rtm->rtm_rmx.rmx_refcnt,
284 1.1 christos rtm->rtm_rmx.rmx_pksent);
285 1.1 christos #else
286 1.1 christos printf("%6s %8s ", "-", "-");
287 1.1 christos #endif
288 1.1 christos #ifndef SMALL
289 1.1 christos if (rtm->rtm_rmx.rmx_mtu)
290 1.1 christos printf("%6"PRId64, rtm->rtm_rmx.rmx_mtu);
291 1.1 christos else
292 1.1 christos printf("%6s", "-");
293 1.1 christos putchar((rtm->rtm_rmx.rmx_locks & RTV_MTU) ? 'L' : ' ');
294 1.1 christos if (flags & RT_TFLAG)
295 1.1 christos p_tag(rti_info[RTAX_TAG]);
296 1.1 christos printf(" %.16s", if_indextoname(rtm->rtm_index, ifbuf));
297 1.1 christos putchar('\n');
298 1.1 christos if (flags & RT_VFLAG)
299 1.1 christos p_rtrmx(&rtm->rtm_rmx);
300 1.1 christos #endif
301 1.1 christos }
302 1.1 christos
303 1.1 christos /*
304 1.1 christos * Print address family header before a section of the routing table.
305 1.1 christos */
306 1.1 christos void
307 1.1 christos p_family(int paf)
308 1.1 christos {
309 1.1 christos const char *afname;
310 1.1 christos
311 1.1 christos switch (paf) {
312 1.1 christos case AF_INET:
313 1.1 christos afname = "Internet";
314 1.1 christos break;
315 1.1 christos #ifdef INET6
316 1.1 christos case AF_INET6:
317 1.1 christos afname = "Internet6";
318 1.1 christos break;
319 1.1 christos #endif
320 1.1 christos case PF_KEY:
321 1.1 christos afname = "Encap";
322 1.1 christos break;
323 1.1 christos case AF_APPLETALK:
324 1.1 christos afname = "AppleTalk";
325 1.1 christos break;
326 1.1 christos #ifndef SMALL
327 1.1 christos case AF_MPLS:
328 1.1 christos afname = "MPLS";
329 1.1 christos break;
330 1.1 christos #endif
331 1.1 christos default:
332 1.1 christos afname = NULL;
333 1.1 christos break;
334 1.1 christos }
335 1.1 christos if (afname)
336 1.1 christos printf("\n%s:\n", afname);
337 1.1 christos else
338 1.1 christos printf("\nProtocol Family %d:\n", paf);
339 1.1 christos }
340 1.1 christos
341 1.1 christos void
342 1.1 christos p_sockaddr(const struct sockaddr *sa, const struct sockaddr *mask, int rflags,
343 1.1 christos int width, int flags)
344 1.1 christos {
345 1.1 christos char *cp;
346 1.1 christos
347 1.1 christos switch (sa->sa_family) {
348 1.1 christos #ifdef INET6
349 1.1 christos case AF_INET6:
350 1.1 christos {
351 1.1 christos struct sockaddr_in6 sa6 = *(const struct sockaddr_in6 *)sa;
352 1.1 christos
353 1.1 christos inet6_getscopeid(&sa6, INET6_IS_ADDR_LINKLOCAL|
354 1.1 christos INET6_IS_ADDR_MC_LINKLOCAL);
355 1.1 christos if (rflags & RTF_HOST)
356 1.1 christos cp = routename((const struct sockaddr *)&sa6, flags);
357 1.1 christos else
358 1.1 christos cp = netname((const struct sockaddr *)&sa6, mask, flags);
359 1.1 christos break;
360 1.1 christos }
361 1.1 christos #endif
362 1.1 christos default:
363 1.1 christos if ((rflags & RTF_HOST) || mask == NULL)
364 1.1 christos cp = routename(sa, flags);
365 1.1 christos else
366 1.1 christos cp = netname(sa, mask, flags);
367 1.1 christos break;
368 1.1 christos }
369 1.1 christos if (width < 0)
370 1.1 christos printf("%s", cp);
371 1.1 christos else {
372 1.1 christos if (flags & RT_NFLAG)
373 1.1 christos printf("%-*s ", width, cp);
374 1.1 christos else
375 1.1 christos printf("%-*.*s ", width, width, cp);
376 1.1 christos }
377 1.1 christos }
378 1.1 christos
379 1.1 christos void
380 1.1 christos p_flags(int f)
381 1.1 christos {
382 1.1 christos char name[33], *flags;
383 1.1 christos const struct bits *p = bits;
384 1.1 christos
385 1.1 christos for (flags = name; p->b_mask && flags < &name[sizeof(name) - 2]; p++)
386 1.1 christos if (p->b_mask & f)
387 1.1 christos *flags++ = p->b_val;
388 1.1 christos *flags = '\0';
389 1.1 christos printf("%-6.6s ", name);
390 1.1 christos }
391 1.1 christos
392 1.1 christos #ifndef SMALL
393 1.1 christos void
394 1.1 christos p_rtrmx(const struct rt_metrics *rmx)
395 1.1 christos {
396 1.1 christos printf("\texpire %10"PRId64"%c recvpipe %10"PRIu64"%c "
397 1.1 christos "sendpipe %10"PRIu64"%c\n",
398 1.1 christos (int64_t)rmx->rmx_expire,
399 1.1 christos (rmx->rmx_locks & RTV_EXPIRE) ? 'L' : ' ', rmx->rmx_recvpipe,
400 1.1 christos (rmx->rmx_locks & RTV_RPIPE) ? 'L' : ' ', rmx->rmx_sendpipe,
401 1.1 christos (rmx->rmx_locks & RTV_SPIPE) ? 'L' : ' ');
402 1.1 christos printf("\tssthresh %10"PRIu64"%c rtt %10"PRIu64"%c "
403 1.1 christos "rttvar %10"PRIu64"%c\n", rmx->rmx_ssthresh,
404 1.1 christos (rmx->rmx_locks & RTV_SSTHRESH) ? 'L' : ' ',
405 1.1 christos rmx->rmx_rtt, (rmx->rmx_locks & RTV_RTT) ? 'L' : ' ',
406 1.1 christos rmx->rmx_rttvar, (rmx->rmx_locks & RTV_RTTVAR) ? 'L' : ' ');
407 1.1 christos printf("\thopcount %10"PRIu64"%c\n",
408 1.1 christos rmx->rmx_hopcount, (rmx->rmx_locks & RTV_HOPCOUNT) ? 'L' : ' ');
409 1.1 christos }
410 1.1 christos
411 1.1 christos static void
412 1.1 christos p_tag(const struct sockaddr *sa)
413 1.1 christos {
414 1.1 christos char *line;
415 1.1 christos
416 1.1 christos if (sa == NULL || sa->sa_family != AF_MPLS) {
417 1.1 christos printf("%7s", "-");
418 1.1 christos return;
419 1.1 christos }
420 1.1 christos line = mpls_ntoa(sa);
421 1.1 christos if (strlen(line) < 7)
422 1.1 christos printf("%7s", line);
423 1.1 christos else
424 1.1 christos printf("%s", line);
425 1.1 christos }
426 1.1 christos #endif
427 1.1 christos
428 1.1 christos static char line[MAXHOSTNAMELEN];
429 1.1 christos static char domain[MAXHOSTNAMELEN];
430 1.1 christos
431 1.1 christos char *
432 1.1 christos routename(const struct sockaddr *sa, int flags)
433 1.1 christos {
434 1.1 christos char *cp = NULL;
435 1.1 christos static int first = 1;
436 1.1 christos
437 1.1 christos if (first) {
438 1.1 christos first = 0;
439 1.1 christos if (gethostname(domain, sizeof(domain)) == 0 &&
440 1.1 christos (cp = strchr(domain, '.')))
441 1.1 christos (void)strlcpy(domain, cp + 1, sizeof(domain));
442 1.1 christos else
443 1.1 christos domain[0] = '\0';
444 1.1 christos cp = NULL;
445 1.1 christos }
446 1.1 christos
447 1.1 christos if (sa->sa_len == 0) {
448 1.1 christos (void)strlcpy(line, "default", sizeof(line));
449 1.1 christos return (line);
450 1.1 christos }
451 1.1 christos
452 1.1 christos switch (sa->sa_family) {
453 1.1 christos case AF_INET:
454 1.1 christos return routename4(
455 1.1 christos ((const struct sockaddr_in *)sa)->sin_addr.s_addr,
456 1.1 christos flags);
457 1.1 christos #ifdef INET6
458 1.1 christos case AF_INET6:
459 1.1 christos {
460 1.1 christos struct sockaddr_in6 sin6;
461 1.1 christos
462 1.1 christos memset(&sin6, 0, sizeof(sin6));
463 1.1 christos memcpy(&sin6, sa, sa->sa_len);
464 1.1 christos sin6.sin6_len = sizeof(struct sockaddr_in6);
465 1.1 christos sin6.sin6_family = AF_INET6;
466 1.1 christos if (sa->sa_len == sizeof(struct sockaddr_in6))
467 1.1 christos inet6_getscopeid(&sin6, INET6_IS_ADDR_LINKLOCAL|
468 1.1 christos INET6_IS_ADDR_MC_LINKLOCAL);
469 1.1 christos return routename6(&sin6, flags);
470 1.1 christos }
471 1.1 christos #endif
472 1.1 christos case AF_LINK:
473 1.1 christos return link_print(sa);
474 1.1 christos
475 1.1 christos #ifndef SMALL
476 1.1 christos case AF_MPLS:
477 1.1 christos return mpls_ntoa(sa);
478 1.1 christos
479 1.1 christos case AF_APPLETALK:
480 1.1 christos (void)snprintf(line, sizeof(line), "atalk %d.%d",
481 1.1 christos ((const struct sockaddr_at *)sa)->sat_addr.s_net,
482 1.1 christos ((const struct sockaddr_at *)sa)->sat_addr.s_node);
483 1.1 christos break;
484 1.1 christos #endif
485 1.1 christos
486 1.1 christos #if 0 /* XXX-elad */
487 1.1 christos case AF_UNSPEC:
488 1.1 christos if (sa->sa_len == sizeof(struct sockaddr_rtlabel)) {
489 1.1 christos static char name[RTLABEL_LEN];
490 1.1 christos struct sockaddr_rtlabel *sr;
491 1.1 christos
492 1.1 christos sr = (struct sockaddr_rtlabel *)sa;
493 1.1 christos strlcpy(name, sr->sr_label, sizeof(name));
494 1.1 christos return (name);
495 1.1 christos }
496 1.1 christos /* FALLTHROUGH */
497 1.1 christos #endif
498 1.1 christos default:
499 1.1 christos (void)snprintf(line, sizeof(line), "(%d) %s",
500 1.1 christos sa->sa_family, any_ntoa(sa));
501 1.1 christos break;
502 1.1 christos }
503 1.1 christos return (line);
504 1.1 christos }
505 1.1 christos
506 1.1 christos char *
507 1.1 christos routename4(in_addr_t in, int flags)
508 1.1 christos {
509 1.1 christos const char *cp = NULL;
510 1.1 christos struct in_addr ina;
511 1.1 christos struct hostent *hp;
512 1.1 christos
513 1.1 christos if (in == INADDR_ANY)
514 1.1 christos cp = "default";
515 1.1 christos if (!cp && (flags & RT_NFLAG) == 0) {
516 1.1 christos if ((hp = gethostbyaddr((char *)&in,
517 1.1 christos sizeof(in), AF_INET)) != NULL) {
518 1.1 christos char *p;
519 1.1 christos if ((p = strchr(hp->h_name, '.')) &&
520 1.1 christos !strcmp(p + 1, domain))
521 1.1 christos *p = '\0';
522 1.1 christos cp = hp->h_name;
523 1.1 christos }
524 1.1 christos }
525 1.1 christos ina.s_addr = in;
526 1.1 christos strlcpy(line, cp ? cp : inet_ntoa(ina), sizeof(line));
527 1.1 christos
528 1.1 christos return (line);
529 1.1 christos }
530 1.1 christos
531 1.1 christos #ifdef INET6
532 1.1 christos char *
533 1.1 christos routename6(const struct sockaddr_in6 *sin6, int flags)
534 1.1 christos {
535 1.1 christos int niflags = 0;
536 1.1 christos
537 1.1 christos if ((flags & RT_NFLAG))
538 1.1 christos niflags |= NI_NUMERICHOST;
539 1.1 christos else
540 1.1 christos niflags |= NI_NOFQDN;
541 1.1 christos
542 1.1 christos if (getnameinfo((const struct sockaddr *)sin6, sin6->sin6_len,
543 1.1 christos line, sizeof(line), NULL, 0, niflags) != 0)
544 1.1 christos strncpy(line, "invalid", sizeof(line));
545 1.1 christos
546 1.1 christos return (line);
547 1.1 christos }
548 1.1 christos #endif
549 1.1 christos
550 1.1 christos /*
551 1.1 christos * Return the name of the network whose address is given.
552 1.1 christos * The address is assumed to be that of a net or subnet, not a host.
553 1.1 christos */
554 1.1 christos char *
555 1.1 christos netname4(in_addr_t in, in_addr_t mask, int flags)
556 1.1 christos {
557 1.1 christos const char *cp = NULL;
558 1.1 christos struct netent *np = NULL;
559 1.1 christos int mbits;
560 1.1 christos
561 1.1 christos in = ntohl(in);
562 1.1 christos mask = ntohl(mask);
563 1.1 christos if (!(flags & RT_NFLAG) && in != INADDR_ANY) {
564 1.1 christos if ((np = getnetbyaddr(in, AF_INET)) != NULL)
565 1.1 christos cp = np->n_name;
566 1.1 christos }
567 1.1 christos mbits = mask ? 33 - ffs(mask) : 0;
568 1.1 christos if (in == INADDR_ANY && !mbits)
569 1.1 christos cp = "default";
570 1.1 christos if (cp)
571 1.1 christos strlcpy(line, cp, sizeof(line));
572 1.1 christos #define C(x) ((x) & 0xff)
573 1.1 christos else if (mbits < 9)
574 1.1 christos snprintf(line, sizeof(line), "%u/%d", C(in >> 24), mbits);
575 1.1 christos else if (mbits < 17)
576 1.1 christos snprintf(line, sizeof(line), "%u.%u/%d",
577 1.1 christos C(in >> 24) , C(in >> 16), mbits);
578 1.1 christos else if (mbits < 25)
579 1.1 christos snprintf(line, sizeof(line), "%u.%u.%u/%d",
580 1.1 christos C(in >> 24), C(in >> 16), C(in >> 8), mbits);
581 1.1 christos else
582 1.1 christos snprintf(line, sizeof(line), "%u.%u.%u.%u/%d", C(in >> 24),
583 1.1 christos C(in >> 16), C(in >> 8), C(in), mbits);
584 1.1 christos #undef C
585 1.1 christos return (line);
586 1.1 christos }
587 1.1 christos
588 1.1 christos #ifdef INET6
589 1.1 christos char *
590 1.1 christos netname6(const struct sockaddr_in6 *sa6, const struct sockaddr_in6 *mask, int flags)
591 1.1 christos {
592 1.1 christos struct sockaddr_in6 sin6;
593 1.1 christos const u_char *p;
594 1.1 christos int masklen, final = 0, illegal = 0;
595 1.1 christos int i, lim, flag, error;
596 1.1 christos char hbuf[NI_MAXHOST];
597 1.1 christos
598 1.1 christos sin6 = *sa6;
599 1.1 christos
600 1.1 christos flag = 0;
601 1.1 christos masklen = 0;
602 1.1 christos if (mask) {
603 1.1 christos lim = mask->sin6_len - offsetof(struct sockaddr_in6, sin6_addr);
604 1.1 christos if (lim < 0)
605 1.1 christos lim = 0;
606 1.1 christos else if (lim > (int)sizeof(struct in6_addr))
607 1.1 christos lim = sizeof(struct in6_addr);
608 1.1 christos for (p = (const u_char *)&mask->sin6_addr, i = 0; i < lim; p++) {
609 1.1 christos if (final && *p) {
610 1.1 christos illegal++;
611 1.1 christos sin6.sin6_addr.s6_addr[i++] = 0x00;
612 1.1 christos continue;
613 1.1 christos }
614 1.1 christos
615 1.1 christos switch (*p & 0xff) {
616 1.1 christos case 0xff:
617 1.1 christos masklen += 8;
618 1.1 christos break;
619 1.1 christos case 0xfe:
620 1.1 christos masklen += 7;
621 1.1 christos final++;
622 1.1 christos break;
623 1.1 christos case 0xfc:
624 1.1 christos masklen += 6;
625 1.1 christos final++;
626 1.1 christos break;
627 1.1 christos case 0xf8:
628 1.1 christos masklen += 5;
629 1.1 christos final++;
630 1.1 christos break;
631 1.1 christos case 0xf0:
632 1.1 christos masklen += 4;
633 1.1 christos final++;
634 1.1 christos break;
635 1.1 christos case 0xe0:
636 1.1 christos masklen += 3;
637 1.1 christos final++;
638 1.1 christos break;
639 1.1 christos case 0xc0:
640 1.1 christos masklen += 2;
641 1.1 christos final++;
642 1.1 christos break;
643 1.1 christos case 0x80:
644 1.1 christos masklen += 1;
645 1.1 christos final++;
646 1.1 christos break;
647 1.1 christos case 0x00:
648 1.1 christos final++;
649 1.1 christos break;
650 1.1 christos default:
651 1.1 christos final++;
652 1.1 christos illegal++;
653 1.1 christos break;
654 1.1 christos }
655 1.1 christos
656 1.1 christos if (!illegal)
657 1.1 christos sin6.sin6_addr.s6_addr[i++] &= *p;
658 1.1 christos else
659 1.1 christos sin6.sin6_addr.s6_addr[i++] = 0x00;
660 1.1 christos }
661 1.1 christos while (i < (int)sizeof(struct in6_addr))
662 1.1 christos sin6.sin6_addr.s6_addr[i++] = 0x00;
663 1.1 christos } else
664 1.1 christos masklen = 128;
665 1.1 christos
666 1.1 christos if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr)) {
667 1.1 christos snprintf(line, sizeof(line), "default");
668 1.1 christos return (line);
669 1.1 christos }
670 1.1 christos
671 1.1 christos if (illegal)
672 1.1 christos warnx("illegal prefixlen");
673 1.1 christos
674 1.1 christos if (flags & RT_NFLAG)
675 1.1 christos flag |= NI_NUMERICHOST;
676 1.1 christos error = getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
677 1.1 christos hbuf, sizeof(hbuf), NULL, 0, flag);
678 1.1 christos if (error)
679 1.1 christos snprintf(hbuf, sizeof(hbuf), "invalid");
680 1.1 christos
681 1.1 christos snprintf(line, sizeof(line), "%s/%d", hbuf, masklen);
682 1.1 christos return (line);
683 1.1 christos }
684 1.1 christos #endif
685 1.1 christos
686 1.1 christos /*
687 1.1 christos * Return the name of the network whose address is given.
688 1.1 christos * The address is assumed to be that of a net or subnet, not a host.
689 1.1 christos */
690 1.1 christos char *
691 1.1 christos netname(const struct sockaddr *sa, const struct sockaddr *mask, int flags)
692 1.1 christos {
693 1.1 christos switch (sa->sa_family) {
694 1.1 christos
695 1.1 christos case AF_INET:
696 1.1 christos return netname4(((const struct sockaddr_in *)sa)->sin_addr.s_addr,
697 1.1 christos ((const struct sockaddr_in *)mask)->sin_addr.s_addr, flags);
698 1.1 christos #ifdef INET6
699 1.1 christos case AF_INET6:
700 1.1 christos return netname6((const struct sockaddr_in6 *)sa,
701 1.1 christos (const struct sockaddr_in6 *)mask, flags);
702 1.1 christos #endif
703 1.1 christos case AF_LINK:
704 1.1 christos return link_print(sa);
705 1.1 christos default:
706 1.1 christos snprintf(line, sizeof(line), "af %d: %s",
707 1.1 christos sa->sa_family, any_ntoa(sa));
708 1.1 christos break;
709 1.1 christos }
710 1.1 christos return (line);
711 1.1 christos }
712 1.1 christos
713 1.1 christos static const char hexlist[] = "0123456789abcdef";
714 1.1 christos
715 1.1 christos char *
716 1.1 christos any_ntoa(const struct sockaddr *sa)
717 1.1 christos {
718 1.1 christos static char obuf[240];
719 1.1 christos const char *in = sa->sa_data;
720 1.1 christos char *out = obuf;
721 1.1 christos int len = sa->sa_len - offsetof(struct sockaddr, sa_data);
722 1.1 christos
723 1.1 christos *out++ = 'Q';
724 1.1 christos do {
725 1.1 christos *out++ = hexlist[(*in >> 4) & 15];
726 1.1 christos *out++ = hexlist[(*in++) & 15];
727 1.1 christos *out++ = '.';
728 1.1 christos } while (--len > 0 && (out + 3) < &obuf[sizeof(obuf) - 1]);
729 1.1 christos out[-1] = '\0';
730 1.1 christos return (obuf);
731 1.1 christos }
732 1.1 christos
733 1.1 christos static char *
734 1.1 christos link_print(const struct sockaddr *sa)
735 1.1 christos {
736 1.1 christos const struct sockaddr_dl *sdl = (const struct sockaddr_dl *)sa;
737 1.1 christos const u_char *lla = (const u_char *)sdl->sdl_data + sdl->sdl_nlen;
738 1.1 christos
739 1.1 christos if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 &&
740 1.1 christos sdl->sdl_slen == 0) {
741 1.1 christos (void)snprintf(line, sizeof(line), "link#%d", sdl->sdl_index);
742 1.1 christos return (line);
743 1.1 christos }
744 1.1 christos switch (sdl->sdl_type) {
745 1.1 christos case IFT_ETHER:
746 1.1 christos case IFT_CARP:
747 1.1 christos return ether_ntoa((const struct ether_addr *)lla);
748 1.1 christos default:
749 1.1 christos return link_ntoa(sdl);
750 1.1 christos }
751 1.1 christos }
752 1.1 christos
753 1.1 christos #ifndef SMALL
754 1.1 christos char *
755 1.1 christos mpls_ntoa(const struct sockaddr *sa)
756 1.1 christos {
757 1.1 christos static char obuf[16];
758 1.1 christos size_t olen;
759 1.1 christos const union mpls_shim *pms;
760 1.1 christos union mpls_shim ms;
761 1.1 christos int psize = sizeof(struct sockaddr_mpls);
762 1.1 christos
763 1.1 christos pms = &((const struct sockaddr_mpls*)sa)->smpls_addr;
764 1.1 christos ms.s_addr = ntohl(pms->s_addr);
765 1.1 christos
766 1.1 christos snprintf(obuf, sizeof(obuf), "%u", ms.shim.label);
767 1.1 christos
768 1.1 christos while(psize < sa->sa_len) {
769 1.1 christos pms++;
770 1.1 christos ms.s_addr = ntohl(pms->s_addr);
771 1.1 christos olen = strlen(obuf);
772 1.1 christos snprintf(obuf + olen, sizeof(obuf) - olen, ",%u",
773 1.1 christos ms.shim.label);
774 1.1 christos psize+=sizeof(ms);
775 1.1 christos }
776 1.1 christos return obuf;
777 1.1 christos }
778 1.1 christos #endif
779 1.1 christos
780 1.1 christos void
781 1.1 christos p_addr(const struct sockaddr *sa, const struct sockaddr *mask, int rflags, int flags)
782 1.1 christos {
783 1.1 christos p_sockaddr(sa, mask, rflags, WID_DST(sa->sa_family), flags);
784 1.1 christos }
785 1.1 christos
786 1.1 christos void
787 1.1 christos p_gwaddr(const struct sockaddr *sa, int gwaf, int flags)
788 1.1 christos {
789 1.1 christos p_sockaddr(sa, 0, RTF_HOST, WID_GW(gwaf), flags);
790 1.1 christos }
791