Repository navigation
Expand file tree
/
Copy pathrtinfo.c
More file actions
461 lines (393 loc) · 12.3 KB
/
Copy pathrtinfo.c
File metadata and controls
461 lines (393 loc) · 12.3 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
* Mock net.route sysctl(2) subtree implementation using RMIB. This code
* serves as a temporary bridge to allow libc to switch from the original,
* native MINIX3 getifaddrs(3) to the NetBSD getifaddrs(3). As such, it
* implements only a small subset of NetBSD's full net.route functionality,
* although also more than needed only to imitate the MINIX3 getifaddrs(3).
*/
#include "inet.h"
#include "generic/type.h"
#include "generic/buf.h"
#include "generic/event.h"
#include "generic/ip_int.h"
#include "osdep_eth.h"
#include "generic/eth_int.h"
#include <netinet/in.h>
#include <net/if_dl.h>
#include <net/if_types.h>
#include <net/route.h>
#include <sys/sysctl.h>
#include <minix/rmib.h>
#include <assert.h>
/* Max. number of bytes for a full sockaddr_dl structure, including data. */
#define SDL_BUFSIZE (sizeof(struct sockaddr_dl) + 32)
static const char padbuf[RT_ROUNDUP(0)] = { 0 };
/*
* Copy out a vector of data to the sysctl(2) caller. TODO: decide what to do
* with this. We could implement this as a vectored-safecopy operation in
* RMIB. We could also copy everything into a single buffer first. The
* current implementation is probably the worst among the possibilities.
*/
static ssize_t
rmib_vcopyout(struct rmib_oldp * oldp, size_t off, const iovec_t * iov,
unsigned int iovcnt)
{
unsigned int i;
ssize_t r, len;
len = 0;
for (i = 0; i < iovcnt; i++) {
if ((r = rmib_copyout(oldp, off + len,
(const void *)iov[i].iov_addr, iov[i].iov_size)) < 0)
return r;
len += r;
}
return len;
}
/*
* Compute the length for, and possibly copy out, an interface information or
* interface address record with an associated set of zero or more routing
* table addresses. The addresses are padded as necessary. Store the full
* record length and the address bitmap before copying out the entire record.
*/
static ssize_t
copyout_rta(void * hdr, size_t size, u_short * msglen, int * addrs,
void * rta_map[RTAX_MAX], size_t rta_len[RTAX_MAX],
struct rmib_oldp * oldp, ssize_t off)
{
iovec_t iov[1 + RTAX_MAX * 2];
size_t len, total, padlen;
unsigned int i, iovcnt;
int mask;
iovcnt = 0;
iov[iovcnt].iov_addr = (vir_bytes)hdr;
iov[iovcnt++].iov_size = size;
total = size;
mask = 0;
/*
* Any addresses in the given map should be stored in the numbering
* order of the map.
*/
for (i = 0; i < RTAX_MAX; i++) {
if (rta_map[i] == NULL)
continue;
assert(iovcnt < __arraycount(iov));
iov[iovcnt].iov_addr = (vir_bytes)rta_map[i];
iov[iovcnt++].iov_size = len = rta_len[i];
padlen = RT_ROUNDUP(len) - len;
if (padlen > 0) {
assert(iovcnt < __arraycount(iov));
iov[iovcnt].iov_addr = (vir_bytes)padbuf;
iov[iovcnt++].iov_size = padlen;
}
total += len + padlen;
mask |= (1 << i);
}
/* If only the length was requested, return it now. */
if (oldp == NULL)
return total;
/*
* Casting 'hdr' would violate C99 strict aliasing rules, so store the
* computed header values through direct pointers. Bah.
*/
*msglen = total;
*addrs = mask;
return rmib_vcopyout(oldp, off, iov, iovcnt);
}
/*
* Given an INET ip-layer datalink type, return a matching BSD interface type.
*/
static unsigned char
ipdl_to_ift(int ip_dl_type)
{
switch (ip_dl_type) {
case IPDL_ETH:
return IFT_ETHER;
case IPDL_PSIP:
return IFT_LOOP;
default:
return IFT_OTHER;
}
}
/*
* Compute the length for, and possibly generate, a sockaddr_dl structure for
* the given interface. The complication here is that the structure contains
* various field packed together dynamically, making it variable sized.
*/
static size_t
make_sdl(const ip_port_t * ip_port, int ndx, char * buf, size_t max)
{
const eth_port_t *eth_port;
struct sockaddr_dl sdl;
static char namebuf[8];
const void *addrptr;
size_t hdrlen, namelen, addrlen, padlen, len;
/* Normally the interface name would be pregenerated somewhere. */
snprintf(namebuf, sizeof(namebuf), "ip%u", ip_port->ip_port);
namelen = strlen(namebuf);
addrlen = 0;
if (ip_port->ip_dl_type == IPDL_ETH) {
eth_port = ð_port_table[ip_port->ip_dl.dl_eth.de_port];
if (eth_port->etp_flags & EPF_GOT_ADDR) {
addrptr = ð_port->etp_ethaddr;
addrlen = sizeof(eth_port->etp_ethaddr);
}
}
/*
* Compute the unpadded and padded length of the structure. We pad the
* structure ourselves here, even though the caller will otherwise pad
* it later, because it is easy to do so and saves on a vector element.
*/
hdrlen = offsetof(struct sockaddr_dl, sdl_data);
len = hdrlen + namelen + addrlen;
padlen = RT_ROUNDUP(len) - len;
assert(len + padlen <= max);
/* If we are asked not to generate the actual data, stop here. */
if (buf == NULL)
return len + padlen;
/*
* Fill the sockaddr_dl structure header. The C99 strict aliasing
* rules prevent us from filling 'buf' through a pointer structure
* directly.
*/
memset(&sdl, 0, hdrlen);
sdl.sdl_len = len;
sdl.sdl_family = AF_LINK;
sdl.sdl_index = ndx;
sdl.sdl_type = ipdl_to_ift(ip_port->ip_dl_type);
sdl.sdl_nlen = namelen;
sdl.sdl_alen = addrlen;
sdl.sdl_slen = 0;
/*
* Generate the full sockaddr_dl structure in the given buffer. These
* memory sizes are typically small, so the extra memory copies are not
* too expensive. The advantage of generating a single sockaddr_dl
* structure buffer is that we can use copyout_rta() on it.
*/
memcpy(buf, &sdl, hdrlen);
if (namelen > 0)
memcpy(&buf[hdrlen], namebuf, namelen);
if (addrlen > 0)
memcpy(&buf[hdrlen + namelen], addrptr, addrlen);
if (padlen > 0)
memset(&buf[len], 0, padlen);
return len + padlen;
}
/*
* Compute the length for, and possibly generate, an interface information
* record for the given interface.
*/
static ssize_t
gen_ifm(const ip_port_t * ip_port, int ndx, int is_up, struct rmib_oldp * oldp,
ssize_t off)
{
struct if_msghdr ifm;
char buf[SDL_BUFSIZE];
void *rta_map[RTAX_MAX];
size_t rta_len[RTAX_MAX], size;
if (oldp != NULL) {
memset(&ifm, 0, sizeof(ifm));
ifm.ifm_version = RTM_VERSION;
ifm.ifm_type = RTM_IFINFO;
ifm.ifm_flags = (is_up) ? (IFF_UP | IFF_RUNNING) : 0;
ifm.ifm_index = ndx;
ifm.ifm_data.ifi_type = ipdl_to_ift(ip_port->ip_dl_type);
if (ifm.ifm_data.ifi_type == IFT_LOOP)
ifm.ifm_flags |= IFF_LOOPBACK;
/* TODO: other ifm_flags, other ifm_data fields, etc. */
}
/*
* Note that we add padding even in this case, to ensure that the
* following structures are properly aligned as well.
*/
size = make_sdl(ip_port, ndx, (oldp != NULL) ? buf : NULL,
sizeof(buf));
memset(rta_map, 0, sizeof(rta_map));
rta_map[RTAX_IFP] = buf;
rta_len[RTAX_IFP] = size;
return copyout_rta(&ifm, sizeof(ifm), &ifm.ifm_msglen, &ifm.ifm_addrs,
rta_map, rta_len, oldp, off);
}
/*
* Compute the length for, and possibly generate, an AF_LINK-family interface
* address record.
*/
static ssize_t
gen_ifam_dl(const ip_port_t * ip_port, int ndx, int is_up,
struct rmib_oldp * oldp, ssize_t off)
{
struct ifa_msghdr ifam;
char buf[SDL_BUFSIZE];
void *rta_map[RTAX_MAX];
size_t rta_len[RTAX_MAX], size;
if (oldp != NULL) {
memset(&ifam, 0, sizeof(ifam));
ifam.ifam_version = RTM_VERSION;
ifam.ifam_type = RTM_NEWADDR;
ifam.ifam_index = ndx;
ifam.ifam_metric = 0; /* unknown and irrelevant */
}
size = make_sdl(ip_port, ndx, (oldp != NULL) ? buf : NULL,
sizeof(buf));
/*
* We do not generate a netmask. NetBSD seems to generate a netmask
* with all-one bits for the number of bytes equal to the name length,
* for reasons unknown to me. If we did the same, we would end up with
* a conflict on the static 'namebuf' buffer.
*/
memset(rta_map, 0, sizeof(rta_map));
rta_map[RTAX_IFA] = buf;
rta_len[RTAX_IFA] = size;
return copyout_rta(&ifam, sizeof(ifam), &ifam.ifam_msglen,
&ifam.ifam_addrs, rta_map, rta_len, oldp, off);
}
/*
* Compute the length for, and possibly generate, an AF_INET-family interface
* address record.
*/
static ssize_t
gen_ifam_inet(const ip_port_t * ip_port, int ndx, int is_u
D771
p,
struct rmib_oldp * oldp, ssize_t off)
{
struct ifa_msghdr ifam;
struct sockaddr_in ipaddr, netmask;
void *rta_map[RTAX_MAX];
size_t rta_len[RTAX_MAX];
if (oldp != NULL) {
memset(&ifam, 0, sizeof(ifam));
ifam.ifam_msglen = sizeof(ifam);
ifam.ifam_version = RTM_VERSION;
ifam.ifam_type = RTM_NEWADDR;
ifam.ifam_addrs = 0;
ifam.ifam_index = ndx;
ifam.ifam_metric = 0; /* unknown and irrelevant */
}
memset(rta_map, 0, sizeof(rta_map));
if (ip_port->ip_flags & IPF_IPADDRSET) {
if (oldp != NULL) {
memset(&ipaddr, 0, sizeof(ipaddr));
ipaddr.sin_family = AF_INET;
ipaddr.sin_len = sizeof(ipaddr);
ipaddr.sin_addr.s_addr = ip_port->ip_ipaddr;
}
rta_map[RTAX_IFA] = &ipaddr;
rta_len[RTAX_IFA] = sizeof(ipaddr);
}
if (ip_port->ip_flags & IPF_NETMASKSET) {
/*
* TODO: BSD goes through the trouble of compressing the
* netmask for some reason. We need to figure out if
* compression is actually required by any part of userland.
*/
if (oldp != NULL) {
memset(&netmask, 0, sizeof(netmask));
netmask.sin_family = AF_INET;
netmask.sin_len = sizeof(netmask);
netmask.sin_addr.s_addr = ip_port->ip_subnetmask;
}
rta_map[RTAX_NETMASK] = &netmask;
rta_len[RTAX_NETMASK] = sizeof(netmask);
}
return copyout_rta(&ifam, sizeof(ifam), &ifam.ifam_msglen,
&ifam.ifam_addrs, rta_map, rta_len, oldp, off);
}
/*
* Compute the size needed for, and optionally copy out, the interface and
* address information for the given interface.
*/
static ssize_t
do_one_if(const ip_port_t * ip_port, int ndx, struct rmib_oldp * oldp,
ssize_t off, int filter)
{
ssize_t r, len;
int is_up;
/*
* If the interface is not configured, we mark it as down and do not
* provide IP address information.
*/
is_up = (ip_port->ip_flags & IPF_IPADDRSET);
len = 0;
/* There is always a full interface information record. */
if ((r = gen_ifm(ip_port, ndx, is_up, oldp, off)) < 0)
return r;
len += r;
/* If not filtered, there is a datalink address record. */
if (filter == 0 || filter == AF_LINK) {
if ((r = gen_ifam_dl(ip_port, ndx, is_up, oldp,
off + len)) < 0)
return r;
len += r;
}
/* If configured and not filtered, there is an IPv4 address record. */
if (is_up && (filter == 0 || filter == AF_INET)) {
if ((r = gen_ifam_inet(ip_port, ndx, is_up, oldp,
off + len)) < 0)
return r;
len += r;
}
/*
* Whether or not anything was copied out, upon success we return the
* full length of the data.
*/
return len;
}
/*
* Remote MIB implementation of CTL_NET PF_ROUTE 0. This function handles all
* queries on the "net.route.rtable" sysctl(2) node.
*/
static ssize_t
net_route_rtable(struct rmib_call * call, struct rmib_node * node __unused,
struct rmib_oldp * oldp, struct rmib_newp * newp __unused)
{
const ip_port_t *ip_port;
ssize_t r, off;
int i, filter, ndx;
if (call->call_namelen != 3)
return EINVAL;
/* We only support listing interfaces for now. */
if (call->call_name[1] != NET_RT_IFLIST)
return EOPNOTSUPP;
filter = call->call_name[0];
ndx = call->call_name[2];
off = 0;
for (i = 0, ip_port = ip_port_table; i < ip_conf_nr; i++, ip_port++) {
if (!(ip_port->ip_flags & IPF_CONFIGURED))
continue;
/*
* If information about a specific interface index is requested
* then skip all other entries. Interface indices must be
* nonzero, so we shift the numbers by one. We can avoid going
* through the loop altogether here, but getifaddrs(3) does not
* query specific interfaces anyway.
*/
if (ndx != 0 && ndx != ip_port->ip_port + 1)
continue;
/* Avoid generating results that are never copied out. */
if (oldp != NULL && !rmib_inrange(oldp, off))
oldp = NULL;
if ((r = do_one_if(ip_port, ip_port->ip_port + 1, oldp, off,
filter)) < 0)
return r;
off += r;
}
return off;
}
/* The CTL_NET PF_ROUTE subtree. */
static struct rmib_node net_route_table[] = {
[0] = RMIB_FUNC(RMIB_RO | CTLTYPE_NODE, 0, net_route_rtable,
"rtable", "Routing table information")
};
/* The CTL_NET PF_ROUTE node. */
static struct rmib_node net_route_node =
RMIB_NODE(RMIB_RO, net_route_table, "route", "PF_ROUTE information");
/*
* Register the net.route RMIB subtree with the MIB service. Since inet does
* not support clean shutdowns, there is no matching cleanup function.
*/
void
rtinfo_init(void)
{
const int mib[] = { CTL_NET, PF_ROUTE };
int r;
if ((r = rmib_register(mib, __arraycount(mib), &net_route_node)) != OK)
panic("unable to register remote MIB tree: %d", r);
}