1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright 2019 Conrad Meyer <cem@FreeBSD.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "opt_inet.h"
#include "opt_inet6.h"
#include <sys/ctype.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <sys/syslog.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/rmlock.h>
#include <ddb/ddb.h>
#include <ddb/db_lex.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/if_dl.h>
#include <net/route.h>
#include <net/route/nhop.h>
#include <net/route/route_ctl.h>
#include <net/route/route_var.h>
/*
* Unfortunately, RTF_ values are expressed as raw masks rather than powers of
* 2, so we cannot use them as nice C99 initializer indices below.
*/
static const char * const rtf_flag_strings[] = {
"UP",
"GATEWAY",
"HOST",
"REJECT",
"DYNAMIC",
"MODIFIED",
"DONE",
"UNUSED_0x80",
"UNUSED_0x100",
"XRESOLVE",
"LLDATA",
"STATIC",
"BLACKHOLE",
"UNUSED_0x2000",
"PROTO2",
"PROTO1",
"UNUSED_0x10000",
"UNUSED_0x20000",
"PROTO3",
"FIXEDMTU",
"PINNED",
"LOCAL",
"BROADCAST",
"MULTICAST",
/* Big gap. */
[28] = "STICKY",
[30] = "RNH_LOCKED",
[31] = "GWFLAG_COMPAT",
};
static const char * __pure
rt_flag_name(unsigned idx)
{
if (idx >= nitems(rtf_flag_strings))
return ("INVALID_FLAG");
if (rtf_flag_strings[idx] == NULL)
return ("UNKNOWN");
return (rtf_flag_strings[idx]);
}
static void
rt_dumpaddr_ddb(const char *name, const struct sockaddr *sa)
{
char buf[INET6_ADDRSTRLEN], *res;
res = NULL;
if (sa == NULL)
res = "NULL";
else if (sa->sa_family == AF_INET) {
res = inet_ntop(AF_INET,
&((const struct sockaddr_in *)sa)->sin_addr,
buf, sizeof(buf));
} else if (sa->sa_family == AF_INET6) {
res = inet_ntop(AF_INET6,
&((const struct sockaddr_in6 *)sa)->sin6_addr,
buf, sizeof(buf));
} else if (sa->sa_family == AF_LINK) {
res = "on link";
}
if (res != NULL) {
db_printf("%s <%s> ", name, res);
return;
}
db_printf("%s <af:%d> ", name, sa->sa_family);
}
static int
rt_dumpentry_ddb(struct radix_node *rn, void *arg __unused)
{
struct sockaddr_storage ss;
struct rtentry *rt;
struct nhop_object *nh;
int flags, idx;
/* If RNTORT is important, put it in a header. */
rt = (void *)rn;
nh = (struct nhop_object *)rt->rt_nhop;
rt_dumpaddr_ddb("dst", rt_key(rt));
rt_dumpaddr_ddb("gateway", &rt->rt_nhop->gw_sa);
rt_dumpaddr_ddb("netmask", rtsock_fix_netmask(rt_key(rt), rt_mask(rt),
&ss));
if ((nh->nh_ifp->if_flags & IFF_DYING) == 0) {
rt_dumpaddr_ddb("ifp", nh->nh_ifp->if_addr->ifa_addr);
rt_dumpaddr_ddb("ifa", nh->nh_ifa->ifa_addr);
}
db_printf("flags ");
flags = rt->rte_flags | nhop_get_rtflags(nh);
if (flags == 0)
db_printf("none");
while ((idx = ffs(flags)) > 0) {
idx--;
db_printf("%s", rt_flag_name(idx));
flags &= ~(1ul << idx);
if (flags != 0)
db_printf(",");
}
db_printf("\n");
return (0);
}
DB_SHOW_COMMAND(routetable, db_show_routetable_cmd)
{
struct rib_head *rnh;
int error, i, lim;
if (have_addr)
i = lim = addr;
else {
i = 1;
lim = AF_MAX;
}
for (; i <= lim; i++) {
rnh = rt_tables_get_rnh(0, i);
if (rnh == NULL) {
if (have_addr) {
db_printf("%s: AF %d not supported?\n",
__func__, i);
break;
}
continue;
}
if (!have_addr && i > 1)
db_printf("\n");
db_printf("Route table for AF %d%s%s%s:\n", i,
(i == AF_INET || i == AF_INET6) ? " (" : "",
(i == AF_INET) ? "INET" : (i == AF_INET6) ? "INET6" : "",
(i == AF_INET || i == AF_INET6) ? ")" : "");
error = rnh->rnh_walktree(&rnh->head, rt_dumpentry_ddb, NULL);
if (error != 0)
db_printf("%s: walktree(%d): %d\n", __func__, i,
error);
}
}
DB_SHOW_COMMAND_FLAGS(route, db_show_route_cmd, CS_OWN)
{
char abuf[INET6_ADDRSTRLEN], *buf, *end;
struct rib_head *rh;
struct radix_node *rn;
void *dst_addrp;
struct rtentry *rt;
union {
struct sockaddr_in dest_sin;
struct sockaddr_in6 dest_sin6;
} u;
int af;
buf = db_get_line();
/* Remove whitespaces from both ends */
end = buf + strlen(buf) - 1;
for (; (end >= buf) && (*end=='\n' || isspace(*end)); end--)
*end = '\0';
while (isspace(*buf))
buf++;
/* Determine AF */
if (strchr(buf, ':') != NULL) {
af = AF_INET6;
u.dest_sin6.sin6_family = af;
u.dest_sin6.sin6_len = sizeof(struct sockaddr_in6);
dst_addrp = &u.dest_sin6.sin6_addr;
} else {
af = AF_INET;
u.dest_sin.sin_family = af;
u.dest_sin.sin_len = sizeof(struct sockaddr_in);
dst_addrp = &u.dest_sin.sin_addr;
}
if (inet_pton(af, buf, dst_addrp) != 1)
goto usage;
if (inet_ntop(af, dst_addrp, abuf, sizeof(abuf)) != NULL)
db_printf("Looking up route to destination '%s'\n", abuf);
rt = NULL;
CURVNET_SET(vnet0);
rh = rt_tables_get_rnh(RT_DEFAULT_FIB, af);
rn = rh->rnh_matchaddr(&u, &rh->head);
if (rn && ((rn->rn_flags & RNF_ROOT) == 0))
rt = (struct rtentry *)rn;
CURVNET_RESTORE();
if (rt == NULL) {
db_printf("Could not get route for that server.\n");
return;
}
rt_dumpentry_ddb((void *)rt, NULL);
return;
usage:
db_printf("Usage: 'show route <address>'\n"
" Currently accepts only IPv4 and IPv6 addresses\n");
db_skip_to_eol();
}
|