xref: /netbsd-src/sbin/route/show.c (revision 0a22125dcab0c46b7f65f1852546ed9a86021c93)
1 /*	$NetBSD: show.c,v 1.51 2020/08/29 19:28:32 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "from: @(#)route.c	8.3 (Berkeley) 3/9/94";
36 #else
37 __RCSID("$NetBSD: show.c,v 1.51 2020/08/29 19:28:32 christos Exp $");
38 #endif
39 #endif /* not lint */
40 
41 #include <sys/param.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 
45 #include <arpa/inet.h>
46 #include <net/if.h>
47 #include <net/if_dl.h>
48 #include <net/if_types.h>
49 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netmpls/mpls.h>
52 
53 #include <sys/sysctl.h>
54 
55 #include <netdb.h>
56 #include <stdbool.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61 #include <err.h>
62 
63 #include "keywords.h"
64 #include "rtutil.h"
65 #include "extern.h"
66 #include "prog_ops.h"
67 
68 void
parse_show_opts(int argc,char * const * argv,int * afp,int * flagsp,const char ** afnamep,bool nolink)69 parse_show_opts(int argc, char * const *argv, int *afp, int *flagsp,
70     const char **afnamep, bool nolink)
71 {
72 	const char *afname = "unspec";
73 	int af, flags;
74 
75 	flags = 0;
76 	af = AF_UNSPEC;
77 	for (; argc >= 2; argc--) {
78 		if (*argv[argc - 1] != '-')
79 			goto bad;
80 		switch (keyword(argv[argc - 1] + 1)) {
81 		case K_HOST:
82 			flags |= RTF_HOST;
83 			break;
84 		case K_INET:
85 			af = AF_INET;
86 			afname = argv[argc - 1] + 1;
87 			break;
88 #ifdef INET6
89 		case K_INET6:
90 			af = AF_INET6;
91 			afname = argv[argc - 1] + 1;
92 			break;
93 #endif
94 #ifndef SMALL
95 		case K_ATALK:
96 			af = AF_APPLETALK;
97 			afname = argv[argc - 1] + 1;
98 			break;
99 		case K_MPLS:
100 			af = AF_MPLS;
101 			afname = argv[argc - 1] + 1;
102 			break;
103 #endif /* SMALL */
104 		case K_LINK:
105 			if (nolink)
106 				goto bad;
107 			af = AF_LINK;
108 			afname = argv[argc - 1] + 1;
109 			break;
110 		default:
111 			goto bad;
112 		}
113 	}
114 	switch (argc) {
115 	case 1:
116 	case 0:
117 		break;
118 	default:
119 	bad:
120 		usage(argv[argc - 1]);
121 	}
122 	if (afnamep != NULL)
123 		*afnamep = afname;
124 	*afp = af;
125 	*flagsp = flags;
126 }
127 
128 /*
129  * Print routing tables.
130  */
131 void
show(int argc,char * const * argv,int flags)132 show(int argc, char *const *argv, int flags)
133 {
134 	int af, rflags;
135 	static int interesting = RTF_ANNOUNCE | RTF_BLACKHOLE | RTF_BROADCAST |
136 	    RTF_CONNECTED | RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST | RTF_LLDATA |
137 	    RTF_LOCAL | RTF_MODIFIED | RTF_PROTO1 | RTF_PROTO2 | RTF_REJECT |
138 	    RTF_STATIC | RTF_UP;
139 
140 	parse_show_opts(argc, argv, &af, &rflags, NULL, true);
141 	p_rttables(af, flags, rflags, interesting);
142 }
143