1 | /* |
---|
2 | * Copyright (c) 1983, 1993 |
---|
3 | * The Regents of the University of California. All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions |
---|
7 | * are met: |
---|
8 | * 1. Redistributions of source code must retain the above copyright |
---|
9 | * notice, this list of conditions and the following disclaimer. |
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer in the |
---|
12 | * documentation and/or other materials provided with the distribution. |
---|
13 | * 4. Neither the name of the University nor the names of its contributors |
---|
14 | * may be used to endorse or promote products derived from this software |
---|
15 | * without specific prior written permission. |
---|
16 | * |
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
27 | * SUCH DAMAGE. |
---|
28 | */ |
---|
29 | |
---|
30 | #ifndef lint |
---|
31 | static const char copyright[] = |
---|
32 | "@(#) Copyright (c) 1983, 1993\n\ |
---|
33 | The Regents of the University of California. All rights reserved.\n"; |
---|
34 | #endif /* not lint */ |
---|
35 | |
---|
36 | #ifndef lint |
---|
37 | #if 0 |
---|
38 | static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94"; |
---|
39 | #endif |
---|
40 | static const char rcsid[] = |
---|
41 | "$FreeBSD$"; |
---|
42 | #endif /* not lint */ |
---|
43 | |
---|
44 | #ifdef __rtems__ |
---|
45 | #define __need_getopt_newlib |
---|
46 | #define option getopt_option |
---|
47 | #include <getopt.h> |
---|
48 | #undef option |
---|
49 | #include <machine/rtems-bsd-program.h> |
---|
50 | #include <machine/rtems-bsd-commands.h> |
---|
51 | #endif /* __rtems__ */ |
---|
52 | #include <rtems/bsd/sys/param.h> |
---|
53 | #include <sys/ioctl.h> |
---|
54 | #include <sys/socket.h> |
---|
55 | #include <rtems/bsd/sys/time.h> |
---|
56 | #include <sys/module.h> |
---|
57 | #include <sys/linker.h> |
---|
58 | |
---|
59 | #include <net/ethernet.h> |
---|
60 | #include <net/if.h> |
---|
61 | #include <net/if_var.h> |
---|
62 | #include <net/if_dl.h> |
---|
63 | #include <net/if_types.h> |
---|
64 | #include <net/route.h> |
---|
65 | |
---|
66 | /* IP */ |
---|
67 | #include <netinet/in.h> |
---|
68 | #include <netinet/in_var.h> |
---|
69 | #include <arpa/inet.h> |
---|
70 | #include <netdb.h> |
---|
71 | |
---|
72 | #include <ifaddrs.h> |
---|
73 | #include <ctype.h> |
---|
74 | #include <err.h> |
---|
75 | #include <errno.h> |
---|
76 | #include <fcntl.h> |
---|
77 | #include <jail.h> |
---|
78 | |
---|
79 | #include <stdio.h> |
---|
80 | #include <stdlib.h> |
---|
81 | #include <string.h> |
---|
82 | #include <unistd.h> |
---|
83 | |
---|
84 | #include "ifconfig.h" |
---|
85 | |
---|
86 | /* |
---|
87 | * Since "struct ifreq" is composed of various union members, callers |
---|
88 | * should pay special attention to interprete the value. |
---|
89 | * (.e.g. little/big endian difference in the structure.) |
---|
90 | */ |
---|
91 | struct ifreq ifr; |
---|
92 | |
---|
93 | char name[IFNAMSIZ]; |
---|
94 | char *descr = NULL; |
---|
95 | size_t descrlen = 64; |
---|
96 | int setaddr; |
---|
97 | int setmask; |
---|
98 | int doalias; |
---|
99 | int clearaddr; |
---|
100 | int newaddr = 1; |
---|
101 | int verbose; |
---|
102 | int noload; |
---|
103 | |
---|
104 | int supmedia = 0; |
---|
105 | int printkeys = 0; /* Print keying material for interfaces. */ |
---|
106 | |
---|
107 | static int ifconfig(int argc, char *const *argv, int iscreate, |
---|
108 | const struct afswtch *afp); |
---|
109 | static void status(const struct afswtch *afp, const struct sockaddr_dl *sdl, |
---|
110 | struct ifaddrs *ifa); |
---|
111 | static void tunnel_status(int s); |
---|
112 | static void usage(void); |
---|
113 | |
---|
114 | static struct afswtch *af_getbyname(const char *name); |
---|
115 | static struct afswtch *af_getbyfamily(int af); |
---|
116 | static void af_other_status(int); |
---|
117 | |
---|
118 | static struct option *opts = NULL; |
---|
119 | |
---|
120 | void |
---|
121 | opt_register(struct option *p) |
---|
122 | { |
---|
123 | p->next = opts; |
---|
124 | opts = p; |
---|
125 | } |
---|
126 | |
---|
127 | static void |
---|
128 | usage(void) |
---|
129 | { |
---|
130 | char options[1024]; |
---|
131 | struct option *p; |
---|
132 | |
---|
133 | /* XXX not right but close enough for now */ |
---|
134 | options[0] = '\0'; |
---|
135 | for (p = opts; p != NULL; p = p->next) { |
---|
136 | strlcat(options, p->opt_usage, sizeof(options)); |
---|
137 | strlcat(options, " ", sizeof(options)); |
---|
138 | } |
---|
139 | |
---|
140 | fprintf(stderr, |
---|
141 | "usage: ifconfig %sinterface address_family [address [dest_address]]\n" |
---|
142 | " [parameters]\n" |
---|
143 | " ifconfig interface create\n" |
---|
144 | " ifconfig -a %s[-d] [-m] [-u] [-v] [address_family]\n" |
---|
145 | " ifconfig -l [-d] [-u] [address_family]\n" |
---|
146 | " ifconfig %s[-d] [-m] [-u] [-v]\n", |
---|
147 | options, options, options); |
---|
148 | exit(1); |
---|
149 | } |
---|
150 | |
---|
151 | #ifdef __rtems__ |
---|
152 | static void ifconfig_ctor(void); |
---|
153 | static int main(int argc, char *argv[]); |
---|
154 | |
---|
155 | int rtems_bsd_command_ifconfig(int argc, char *argv[]) |
---|
156 | { |
---|
157 | int exit_code; |
---|
158 | |
---|
159 | rtems_bsd_program_lock(); |
---|
160 | |
---|
161 | ifconfig_ctor(); |
---|
162 | |
---|
163 | atalk_ctor(); |
---|
164 | bridge_ctor(); |
---|
165 | carp_ctor(); |
---|
166 | clone_ctor(); |
---|
167 | gif_ctor(); |
---|
168 | gre_ctor(); |
---|
169 | group_ctor(); |
---|
170 | ifmedia_ctor(); |
---|
171 | inet_ctor(); |
---|
172 | inet6_ctor(); |
---|
173 | lagg_ctor(); |
---|
174 | link_ctor(); |
---|
175 | mac_ctor(); |
---|
176 | nd6_ctor(); |
---|
177 | pfsync_ctor(); |
---|
178 | vlan_ctor(); |
---|
179 | |
---|
180 | exit_code = rtems_bsd_program_call_main("ifconfig", main, argc, argv); |
---|
181 | |
---|
182 | rtems_bsd_program_unlock(); |
---|
183 | |
---|
184 | return exit_code; |
---|
185 | } |
---|
186 | #endif /* __rtems__ */ |
---|
187 | int |
---|
188 | main(int argc, char *argv[]) |
---|
189 | { |
---|
190 | int c, all, namesonly, downonly, uponly; |
---|
191 | const struct afswtch *afp = NULL; |
---|
192 | int ifindex; |
---|
193 | struct ifaddrs *ifap, *ifa; |
---|
194 | struct ifreq paifr; |
---|
195 | const struct sockaddr_dl *sdl; |
---|
196 | char options[1024], *cp; |
---|
197 | const char *ifname; |
---|
198 | struct option *p; |
---|
199 | size_t iflen; |
---|
200 | #ifdef __rtems__ |
---|
201 | struct getopt_data getopt_data; |
---|
202 | memset(&getopt_data, 0, sizeof(getopt_data)); |
---|
203 | #define optind getopt_data.optind |
---|
204 | #define optarg getopt_data.optarg |
---|
205 | #define opterr getopt_data.opterr |
---|
206 | #define optopt getopt_data.optopt |
---|
207 | #define getopt(argc, argv, opt) getopt_r(argc, argv, opt, &getopt_data) |
---|
208 | #endif /* __rtems__ */ |
---|
209 | |
---|
210 | all = downonly = uponly = namesonly = noload = verbose = 0; |
---|
211 | |
---|
212 | /* Parse leading line options */ |
---|
213 | strlcpy(options, "adklmnuv", sizeof(options)); |
---|
214 | for (p = opts; p != NULL; p = p->next) |
---|
215 | strlcat(options, p->opt, sizeof(options)); |
---|
216 | while ((c = getopt(argc, argv, options)) != -1) { |
---|
217 | switch (c) { |
---|
218 | case 'a': /* scan all interfaces */ |
---|
219 | all++; |
---|
220 | break; |
---|
221 | case 'd': /* restrict scan to "down" interfaces */ |
---|
222 | downonly++; |
---|
223 | break; |
---|
224 | case 'k': |
---|
225 | printkeys++; |
---|
226 | break; |
---|
227 | case 'l': /* scan interface names only */ |
---|
228 | namesonly++; |
---|
229 | break; |
---|
230 | case 'm': /* show media choices in status */ |
---|
231 | supmedia = 1; |
---|
232 | break; |
---|
233 | case 'n': /* suppress module loading */ |
---|
234 | noload++; |
---|
235 | break; |
---|
236 | case 'u': /* restrict scan to "up" interfaces */ |
---|
237 | uponly++; |
---|
238 | break; |
---|
239 | case 'v': |
---|
240 | verbose++; |
---|
241 | break; |
---|
242 | default: |
---|
243 | for (p = opts; p != NULL; p = p->next) |
---|
244 | if (p->opt[0] == c) { |
---|
245 | p->cb(optarg); |
---|
246 | break; |
---|
247 | } |
---|
248 | if (p == NULL) |
---|
249 | usage(); |
---|
250 | break; |
---|
251 | } |
---|
252 | } |
---|
253 | argc -= optind; |
---|
254 | argv += optind; |
---|
255 | |
---|
256 | /* -l cannot be used with -a or -m */ |
---|
257 | if (namesonly && (all || supmedia)) |
---|
258 | usage(); |
---|
259 | |
---|
260 | /* nonsense.. */ |
---|
261 | if (uponly && downonly) |
---|
262 | usage(); |
---|
263 | |
---|
264 | /* no arguments is equivalent to '-a' */ |
---|
265 | if (!namesonly && argc < 1) |
---|
266 | all = 1; |
---|
267 | |
---|
268 | /* -a and -l allow an address family arg to limit the output */ |
---|
269 | if (all || namesonly) { |
---|
270 | if (argc > 1) |
---|
271 | usage(); |
---|
272 | |
---|
273 | ifname = NULL; |
---|
274 | ifindex = 0; |
---|
275 | if (argc == 1) { |
---|
276 | afp = af_getbyname(*argv); |
---|
277 | if (afp == NULL) |
---|
278 | usage(); |
---|
279 | if (afp->af_name != NULL) |
---|
280 | argc--, argv++; |
---|
281 | /* leave with afp non-zero */ |
---|
282 | } |
---|
283 | } else { |
---|
284 | /* not listing, need an argument */ |
---|
285 | if (argc < 1) |
---|
286 | usage(); |
---|
287 | |
---|
288 | ifname = *argv; |
---|
289 | argc--, argv++; |
---|
290 | |
---|
291 | /* check and maybe load support for this interface */ |
---|
292 | ifmaybeload(ifname); |
---|
293 | |
---|
294 | ifindex = if_nametoindex(ifname); |
---|
295 | if (ifindex == 0) { |
---|
296 | /* |
---|
297 | * NOTE: We must special-case the `create' command |
---|
298 | * right here as we would otherwise fail when trying |
---|
299 | * to find the interface. |
---|
300 | */ |
---|
301 | if (argc > 0 && (strcmp(argv[0], "create") == 0 || |
---|
302 | strcmp(argv[0], "plumb") == 0)) { |
---|
303 | iflen = strlcpy(name, ifname, sizeof(name)); |
---|
304 | if (iflen >= sizeof(name)) |
---|
305 | errx(1, "%s: cloning name too long", |
---|
306 | ifname); |
---|
307 | ifconfig(argc, argv, 1, NULL); |
---|
308 | exit(0); |
---|
309 | } |
---|
310 | /* |
---|
311 | * NOTE: We have to special-case the `-vnet' command |
---|
312 | * right here as we would otherwise fail when trying |
---|
313 | * to find the interface as it lives in another vnet. |
---|
314 | */ |
---|
315 | if (argc > 0 && (strcmp(argv[0], "-vnet") == 0)) { |
---|
316 | iflen = strlcpy(name, ifname, sizeof(name)); |
---|
317 | if (iflen >= sizeof(name)) |
---|
318 | errx(1, "%s: interface name too long", |
---|
319 | ifname); |
---|
320 | ifconfig(argc, argv, 0, NULL); |
---|
321 | exit(0); |
---|
322 | } |
---|
323 | errx(1, "interface %s does not exist", ifname); |
---|
324 | } |
---|
325 | } |
---|
326 | |
---|
327 | /* Check for address family */ |
---|
328 | if (argc > 0) { |
---|
329 | afp = af_getbyname(*argv); |
---|
330 | if (afp != NULL) |
---|
331 | argc--, argv++; |
---|
332 | } |
---|
333 | |
---|
334 | if (getifaddrs(&ifap) != 0) |
---|
335 | err(EXIT_FAILURE, "getifaddrs"); |
---|
336 | cp = NULL; |
---|
337 | ifindex = 0; |
---|
338 | for (ifa = ifap; ifa; ifa = ifa->ifa_next) { |
---|
339 | memset(&paifr, 0, sizeof(paifr)); |
---|
340 | strncpy(paifr.ifr_name, ifa->ifa_name, sizeof(paifr.ifr_name)); |
---|
341 | if (sizeof(paifr.ifr_addr) >= ifa->ifa_addr->sa_len) { |
---|
342 | memcpy(&paifr.ifr_addr, ifa->ifa_addr, |
---|
343 | ifa->ifa_addr->sa_len); |
---|
344 | } |
---|
345 | |
---|
346 | if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0) |
---|
347 | continue; |
---|
348 | if (ifa->ifa_addr->sa_family == AF_LINK) |
---|
349 | sdl = (const struct sockaddr_dl *) ifa->ifa_addr; |
---|
350 | else |
---|
351 | sdl = NULL; |
---|
352 | if (cp != NULL && strcmp(cp, ifa->ifa_name) == 0) |
---|
353 | continue; |
---|
354 | iflen = strlcpy(name, ifa->ifa_name, sizeof(name)); |
---|
355 | if (iflen >= sizeof(name)) { |
---|
356 | warnx("%s: interface name too long, skipping", |
---|
357 | ifa->ifa_name); |
---|
358 | continue; |
---|
359 | } |
---|
360 | cp = ifa->ifa_name; |
---|
361 | |
---|
362 | if (downonly && (ifa->ifa_flags & IFF_UP) != 0) |
---|
363 | continue; |
---|
364 | if (uponly && (ifa->ifa_flags & IFF_UP) == 0) |
---|
365 | continue; |
---|
366 | ifindex++; |
---|
367 | /* |
---|
368 | * Are we just listing the interfaces? |
---|
369 | */ |
---|
370 | if (namesonly) { |
---|
371 | if (ifindex > 1) |
---|
372 | printf(" "); |
---|
373 | fputs(name, stdout); |
---|
374 | continue; |
---|
375 | } |
---|
376 | |
---|
377 | if (argc > 0) |
---|
378 | ifconfig(argc, argv, 0, afp); |
---|
379 | else |
---|
380 | status(afp, sdl, ifa); |
---|
381 | } |
---|
382 | if (namesonly) |
---|
383 | printf("\n"); |
---|
384 | freeifaddrs(ifap); |
---|
385 | |
---|
386 | exit(0); |
---|
387 | } |
---|
388 | |
---|
389 | static struct afswtch *afs = NULL; |
---|
390 | |
---|
391 | void |
---|
392 | af_register(struct afswtch *p) |
---|
393 | { |
---|
394 | p->af_next = afs; |
---|
395 | afs = p; |
---|
396 | } |
---|
397 | |
---|
398 | static struct afswtch * |
---|
399 | af_getbyname(const char *name) |
---|
400 | { |
---|
401 | struct afswtch *afp; |
---|
402 | |
---|
403 | for (afp = afs; afp != NULL; afp = afp->af_next) |
---|
404 | if (strcmp(afp->af_name, name) == 0) |
---|
405 | return afp; |
---|
406 | return NULL; |
---|
407 | } |
---|
408 | |
---|
409 | static struct afswtch * |
---|
410 | af_getbyfamily(int af) |
---|
411 | { |
---|
412 | struct afswtch *afp; |
---|
413 | |
---|
414 | for (afp = afs; afp != NULL; afp = afp->af_next) |
---|
415 | if (afp->af_af == af) |
---|
416 | return afp; |
---|
417 | return NULL; |
---|
418 | } |
---|
419 | |
---|
420 | static void |
---|
421 | af_other_status(int s) |
---|
422 | { |
---|
423 | struct afswtch *afp; |
---|
424 | uint8_t afmask[howmany(AF_MAX, NBBY)]; |
---|
425 | |
---|
426 | memset(afmask, 0, sizeof(afmask)); |
---|
427 | for (afp = afs; afp != NULL; afp = afp->af_next) { |
---|
428 | if (afp->af_other_status == NULL) |
---|
429 | continue; |
---|
430 | if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af)) |
---|
431 | continue; |
---|
432 | afp->af_other_status(s); |
---|
433 | setbit(afmask, afp->af_af); |
---|
434 | } |
---|
435 | } |
---|
436 | |
---|
437 | static void |
---|
438 | af_all_tunnel_status(int s) |
---|
439 | { |
---|
440 | struct afswtch *afp; |
---|
441 | uint8_t afmask[howmany(AF_MAX, NBBY)]; |
---|
442 | |
---|
443 | memset(afmask, 0, sizeof(afmask)); |
---|
444 | for (afp = afs; afp != NULL; afp = afp->af_next) { |
---|
445 | if (afp->af_status_tunnel == NULL) |
---|
446 | continue; |
---|
447 | if (afp->af_af != AF_UNSPEC && isset(afmask, afp->af_af)) |
---|
448 | continue; |
---|
449 | afp->af_status_tunnel(s); |
---|
450 | setbit(afmask, afp->af_af); |
---|
451 | } |
---|
452 | } |
---|
453 | |
---|
454 | static struct cmd *cmds = NULL; |
---|
455 | |
---|
456 | void |
---|
457 | cmd_register(struct cmd *p) |
---|
458 | { |
---|
459 | p->c_next = cmds; |
---|
460 | cmds = p; |
---|
461 | } |
---|
462 | |
---|
463 | static const struct cmd * |
---|
464 | cmd_lookup(const char *name, int iscreate) |
---|
465 | { |
---|
466 | #define N(a) (sizeof(a)/sizeof(a[0])) |
---|
467 | const struct cmd *p; |
---|
468 | |
---|
469 | for (p = cmds; p != NULL; p = p->c_next) |
---|
470 | if (strcmp(name, p->c_name) == 0) { |
---|
471 | if (iscreate) { |
---|
472 | if (p->c_iscloneop) |
---|
473 | return p; |
---|
474 | } else { |
---|
475 | if (!p->c_iscloneop) |
---|
476 | return p; |
---|
477 | } |
---|
478 | } |
---|
479 | return NULL; |
---|
480 | #undef N |
---|
481 | } |
---|
482 | |
---|
483 | struct callback { |
---|
484 | callback_func *cb_func; |
---|
485 | void *cb_arg; |
---|
486 | struct callback *cb_next; |
---|
487 | }; |
---|
488 | static struct callback *callbacks = NULL; |
---|
489 | |
---|
490 | void |
---|
491 | callback_register(callback_func *func, void *arg) |
---|
492 | { |
---|
493 | struct callback *cb; |
---|
494 | |
---|
495 | cb = malloc(sizeof(struct callback)); |
---|
496 | if (cb == NULL) |
---|
497 | errx(1, "unable to allocate memory for callback"); |
---|
498 | cb->cb_func = func; |
---|
499 | cb->cb_arg = arg; |
---|
500 | cb->cb_next = callbacks; |
---|
501 | callbacks = cb; |
---|
502 | } |
---|
503 | |
---|
504 | /* specially-handled commands */ |
---|
505 | static void setifaddr(const char *, int, int, const struct afswtch *); |
---|
506 | static const struct cmd setifaddr_cmd = DEF_CMD("ifaddr", 0, setifaddr); |
---|
507 | |
---|
508 | static void setifdstaddr(const char *, int, int, const struct afswtch *); |
---|
509 | static const struct cmd setifdstaddr_cmd = |
---|
510 | DEF_CMD("ifdstaddr", 0, setifdstaddr); |
---|
511 | |
---|
512 | static int |
---|
513 | ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp) |
---|
514 | { |
---|
515 | const struct afswtch *afp, *nafp; |
---|
516 | const struct cmd *p; |
---|
517 | struct callback *cb; |
---|
518 | int s; |
---|
519 | |
---|
520 | strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name); |
---|
521 | afp = uafp != NULL ? uafp : af_getbyname("inet"); |
---|
522 | top: |
---|
523 | ifr.ifr_addr.sa_family = |
---|
524 | afp->af_af == AF_LINK || afp->af_af == AF_UNSPEC ? |
---|
525 | AF_LOCAL : afp->af_af; |
---|
526 | |
---|
527 | if ((s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0)) < 0 && |
---|
528 | (uafp != NULL || errno != EPROTONOSUPPORT || |
---|
529 | (s = socket(AF_LOCAL, SOCK_DGRAM, 0)) < 0)) |
---|
530 | err(1, "socket(family %u,SOCK_DGRAM", ifr.ifr_addr.sa_family); |
---|
531 | |
---|
532 | while (argc > 0) { |
---|
533 | p = cmd_lookup(*argv, iscreate); |
---|
534 | if (iscreate && p == NULL) { |
---|
535 | /* |
---|
536 | * Push the clone create callback so the new |
---|
537 | * device is created and can be used for any |
---|
538 | * remaining arguments. |
---|
539 | */ |
---|
540 | cb = callbacks; |
---|
541 | if (cb == NULL) |
---|
542 | errx(1, "internal error, no callback"); |
---|
543 | callbacks = cb->cb_next; |
---|
544 | cb->cb_func(s, cb->cb_arg); |
---|
545 | iscreate = 0; |
---|
546 | /* |
---|
547 | * Handle any address family spec that |
---|
548 | * immediately follows and potentially |
---|
549 | * recreate the socket. |
---|
550 | */ |
---|
551 | nafp = af_getbyname(*argv); |
---|
552 | if (nafp != NULL) { |
---|
553 | argc--, argv++; |
---|
554 | if (nafp != afp) { |
---|
555 | close(s); |
---|
556 | afp = nafp; |
---|
557 | goto top; |
---|
558 | } |
---|
559 | } |
---|
560 | /* |
---|
561 | * Look for a normal parameter. |
---|
562 | */ |
---|
563 | continue; |
---|
564 | } |
---|
565 | if (p == NULL) { |
---|
566 | /* |
---|
567 | * Not a recognized command, choose between setting |
---|
568 | * the interface address and the dst address. |
---|
569 | */ |
---|
570 | p = (setaddr ? &setifdstaddr_cmd : &setifaddr_cmd); |
---|
571 | } |
---|
572 | if (p->c_u.c_func || p->c_u.c_func2) { |
---|
573 | if (p->c_parameter == NEXTARG) { |
---|
574 | if (argv[1] == NULL) |
---|
575 | errx(1, "'%s' requires argument", |
---|
576 | p->c_name); |
---|
577 | p->c_u.c_func(argv[1], 0, s, afp); |
---|
578 | argc--, argv++; |
---|
579 | } else if (p->c_parameter == OPTARG) { |
---|
580 | p->c_u.c_func(argv[1], 0, s, afp); |
---|
581 | if (argv[1] != NULL) |
---|
582 | argc--, argv++; |
---|
583 | } else if (p->c_parameter == NEXTARG2) { |
---|
584 | if (argc < 3) |
---|
585 | errx(1, "'%s' requires 2 arguments", |
---|
586 | p->c_name); |
---|
587 | p->c_u.c_func2(argv[1], argv[2], s, afp); |
---|
588 | argc -= 2, argv += 2; |
---|
589 | } else |
---|
590 | p->c_u.c_func(*argv, p->c_parameter, s, afp); |
---|
591 | } |
---|
592 | argc--, argv++; |
---|
593 | } |
---|
594 | |
---|
595 | /* |
---|
596 | * Do any post argument processing required by the address family. |
---|
597 | */ |
---|
598 | if (afp->af_postproc != NULL) |
---|
599 | afp->af_postproc(s, afp); |
---|
600 | /* |
---|
601 | * Do deferred callbacks registered while processing |
---|
602 | * command-line arguments. |
---|
603 | */ |
---|
604 | for (cb = callbacks; cb != NULL; cb = cb->cb_next) |
---|
605 | cb->cb_func(s, cb->cb_arg); |
---|
606 | /* |
---|
607 | * Do deferred operations. |
---|
608 | */ |
---|
609 | if (clearaddr) { |
---|
610 | if (afp->af_ridreq == NULL || afp->af_difaddr == 0) { |
---|
611 | warnx("interface %s cannot change %s addresses!", |
---|
612 | name, afp->af_name); |
---|
613 | clearaddr = 0; |
---|
614 | } |
---|
615 | } |
---|
616 | if (clearaddr) { |
---|
617 | int ret; |
---|
618 | strncpy(afp->af_ridreq, name, sizeof ifr.ifr_name); |
---|
619 | ret = ioctl(s, afp->af_difaddr, afp->af_ridreq); |
---|
620 | if (ret < 0) { |
---|
621 | if (errno == EADDRNOTAVAIL && (doalias >= 0)) { |
---|
622 | /* means no previous address for interface */ |
---|
623 | } else |
---|
624 | Perror("ioctl (SIOCDIFADDR)"); |
---|
625 | } |
---|
626 | } |
---|
627 | if (newaddr) { |
---|
628 | if (afp->af_addreq == NULL || afp->af_aifaddr == 0) { |
---|
629 | warnx("interface %s cannot change %s addresses!", |
---|
630 | name, afp->af_name); |
---|
631 | newaddr = 0; |
---|
632 | } |
---|
633 | } |
---|
634 | if (newaddr && (setaddr || setmask)) { |
---|
635 | strncpy(afp->af_addreq, name, sizeof ifr.ifr_name); |
---|
636 | if (ioctl(s, afp->af_aifaddr, afp->af_addreq) < 0) |
---|
637 | Perror("ioctl (SIOCAIFADDR)"); |
---|
638 | } |
---|
639 | |
---|
640 | close(s); |
---|
641 | return(0); |
---|
642 | } |
---|
643 | |
---|
644 | /*ARGSUSED*/ |
---|
645 | static void |
---|
646 | setifaddr(const char *addr, int param, int s, const struct afswtch *afp) |
---|
647 | { |
---|
648 | if (afp->af_getaddr == NULL) |
---|
649 | return; |
---|
650 | /* |
---|
651 | * Delay the ioctl to set the interface addr until flags are all set. |
---|
652 | * The address interpretation may depend on the flags, |
---|
653 | * and the flags may change when the address is set. |
---|
654 | */ |
---|
655 | setaddr++; |
---|
656 | if (doalias == 0 && afp->af_af != AF_LINK) |
---|
657 | clearaddr = 1; |
---|
658 | afp->af_getaddr(addr, (doalias >= 0 ? ADDR : RIDADDR)); |
---|
659 | } |
---|
660 | |
---|
661 | static void |
---|
662 | settunnel(const char *src, const char *dst, int s, const struct afswtch *afp) |
---|
663 | { |
---|
664 | struct addrinfo *srcres, *dstres; |
---|
665 | int ecode; |
---|
666 | |
---|
667 | if (afp->af_settunnel == NULL) { |
---|
668 | warn("address family %s does not support tunnel setup", |
---|
669 | afp->af_name); |
---|
670 | return; |
---|
671 | } |
---|
672 | |
---|
673 | if ((ecode = getaddrinfo(src, NULL, NULL, &srcres)) != 0) |
---|
674 | errx(1, "error in parsing address string: %s", |
---|
675 | gai_strerror(ecode)); |
---|
676 | |
---|
677 | if ((ecode = getaddrinfo(dst, NULL, NULL, &dstres)) != 0) |
---|
678 | errx(1, "error in parsing address string: %s", |
---|
679 | gai_strerror(ecode)); |
---|
680 | |
---|
681 | if (srcres->ai_addr->sa_family != dstres->ai_addr->sa_family) |
---|
682 | errx(1, |
---|
683 | "source and destination address families do not match"); |
---|
684 | |
---|
685 | afp->af_settunnel(s, srcres, dstres); |
---|
686 | |
---|
687 | freeaddrinfo(srcres); |
---|
688 | freeaddrinfo(dstres); |
---|
689 | } |
---|
690 | |
---|
691 | /* ARGSUSED */ |
---|
692 | static void |
---|
693 | deletetunnel(const char *vname, int param, int s, const struct afswtch *afp) |
---|
694 | { |
---|
695 | |
---|
696 | if (ioctl(s, SIOCDIFPHYADDR, &ifr) < 0) |
---|
697 | err(1, "SIOCDIFPHYADDR"); |
---|
698 | } |
---|
699 | |
---|
700 | #ifndef __rtems__ |
---|
701 | static void |
---|
702 | setifvnet(const char *jname, int dummy __unused, int s, |
---|
703 | const struct afswtch *afp) |
---|
704 | { |
---|
705 | struct ifreq my_ifr; |
---|
706 | |
---|
707 | memcpy(&my_ifr, &ifr, sizeof(my_ifr)); |
---|
708 | my_ifr.ifr_jid = jail_getid(jname); |
---|
709 | if (my_ifr.ifr_jid < 0) |
---|
710 | errx(1, "%s", jail_errmsg); |
---|
711 | if (ioctl(s, SIOCSIFVNET, &my_ifr) < 0) |
---|
712 | err(1, "SIOCSIFVNET"); |
---|
713 | } |
---|
714 | |
---|
715 | static void |
---|
716 | setifrvnet(const char *jname, int dummy __unused, int s, |
---|
717 | const struct afswtch *afp) |
---|
718 | { |
---|
719 | struct ifreq my_ifr; |
---|
720 | |
---|
721 | memcpy(&my_ifr, &ifr, sizeof(my_ifr)); |
---|
722 | my_ifr.ifr_jid = jail_getid(jname); |
---|
723 | if (my_ifr.ifr_jid < 0) |
---|
724 | errx(1, "%s", jail_errmsg); |
---|
725 | if (ioctl(s, SIOCSIFRVNET, &my_ifr) < 0) |
---|
726 | err(1, "SIOCSIFRVNET(%d, %s)", my_ifr.ifr_jid, my_ifr.ifr_name); |
---|
727 | } |
---|
728 | #endif /* __rtems__ */ |
---|
729 | |
---|
730 | static void |
---|
731 | setifnetmask(const char *addr, int dummy __unused, int s, |
---|
732 | const struct afswtch *afp) |
---|
733 | { |
---|
734 | if (afp->af_getaddr != NULL) { |
---|
735 | setmask++; |
---|
736 | afp->af_getaddr(addr, MASK); |
---|
737 | } |
---|
738 | } |
---|
739 | |
---|
740 | static void |
---|
741 | setifbroadaddr(const char *addr, int dummy __unused, int s, |
---|
742 | const struct afswtch *afp) |
---|
743 | { |
---|
744 | if (afp->af_getaddr != NULL) |
---|
745 | afp->af_getaddr(addr, DSTADDR); |
---|
746 | } |
---|
747 | |
---|
748 | static void |
---|
749 | setifipdst(const char *addr, int dummy __unused, int s, |
---|
750 | const struct afswtch *afp) |
---|
751 | { |
---|
752 | const struct afswtch *inet; |
---|
753 | |
---|
754 | inet = af_getbyname("inet"); |
---|
755 | if (inet == NULL) |
---|
756 | return; |
---|
757 | inet->af_getaddr(addr, DSTADDR); |
---|
758 | clearaddr = 0; |
---|
759 | newaddr = 0; |
---|
760 | } |
---|
761 | |
---|
762 | static void |
---|
763 | notealias(const char *addr, int param, int s, const struct afswtch *afp) |
---|
764 | { |
---|
765 | #define rqtosa(x) (&(((struct ifreq *)(afp->x))->ifr_addr)) |
---|
766 | if (setaddr && doalias == 0 && param < 0) |
---|
767 | if (afp->af_addreq != NULL && afp->af_ridreq != NULL) |
---|
768 | bcopy((caddr_t)rqtosa(af_addreq), |
---|
769 | (caddr_t)rqtosa(af_ridreq), |
---|
770 | rqtosa(af_addreq)->sa_len); |
---|
771 | doalias = param; |
---|
772 | if (param < 0) { |
---|
773 | clearaddr = 1; |
---|
774 | newaddr = 0; |
---|
775 | } else |
---|
776 | clearaddr = 0; |
---|
777 | #undef rqtosa |
---|
778 | } |
---|
779 | |
---|
780 | /*ARGSUSED*/ |
---|
781 | static void |
---|
782 | setifdstaddr(const char *addr, int param __unused, int s, |
---|
783 | const struct afswtch *afp) |
---|
784 | { |
---|
785 | if (afp->af_getaddr != NULL) |
---|
786 | afp->af_getaddr(addr, DSTADDR); |
---|
787 | } |
---|
788 | |
---|
789 | /* |
---|
790 | * Note: doing an SIOCIGIFFLAGS scribbles on the union portion |
---|
791 | * of the ifreq structure, which may confuse other parts of ifconfig. |
---|
792 | * Make a private copy so we can avoid that. |
---|
793 | */ |
---|
794 | static void |
---|
795 | setifflags(const char *vname, int value, int s, const struct afswtch *afp) |
---|
796 | { |
---|
797 | struct ifreq my_ifr; |
---|
798 | int flags; |
---|
799 | |
---|
800 | memset(&my_ifr, 0, sizeof(my_ifr)); |
---|
801 | (void) strlcpy(my_ifr.ifr_name, name, sizeof(my_ifr.ifr_name)); |
---|
802 | |
---|
803 | if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&my_ifr) < 0) { |
---|
804 | Perror("ioctl (SIOCGIFFLAGS)"); |
---|
805 | exit(1); |
---|
806 | } |
---|
807 | flags = (my_ifr.ifr_flags & 0xffff) | (my_ifr.ifr_flagshigh << 16); |
---|
808 | |
---|
809 | if (value < 0) { |
---|
810 | value = -value; |
---|
811 | flags &= ~value; |
---|
812 | } else |
---|
813 | flags |= value; |
---|
814 | my_ifr.ifr_flags = flags & 0xffff; |
---|
815 | my_ifr.ifr_flagshigh = flags >> 16; |
---|
816 | if (ioctl(s, SIOCSIFFLAGS, (caddr_t)&my_ifr) < 0) |
---|
817 | Perror(vname); |
---|
818 | } |
---|
819 | |
---|
820 | void |
---|
821 | setifcap(const char *vname, int value, int s, const struct afswtch *afp) |
---|
822 | { |
---|
823 | int flags; |
---|
824 | |
---|
825 | if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) < 0) { |
---|
826 | Perror("ioctl (SIOCGIFCAP)"); |
---|
827 | exit(1); |
---|
828 | } |
---|
829 | flags = ifr.ifr_curcap; |
---|
830 | if (value < 0) { |
---|
831 | value = -value; |
---|
832 | flags &= ~value; |
---|
833 | } else |
---|
834 | flags |= value; |
---|
835 | flags &= ifr.ifr_reqcap; |
---|
836 | ifr.ifr_reqcap = flags; |
---|
837 | if (ioctl(s, SIOCSIFCAP, (caddr_t)&ifr) < 0) |
---|
838 | Perror(vname); |
---|
839 | } |
---|
840 | |
---|
841 | static void |
---|
842 | setifmetric(const char *val, int dummy __unused, int s, |
---|
843 | const struct afswtch *afp) |
---|
844 | { |
---|
845 | strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); |
---|
846 | ifr.ifr_metric = atoi(val); |
---|
847 | if (ioctl(s, SIOCSIFMETRIC, (caddr_t)&ifr) < 0) |
---|
848 | warn("ioctl (set metric)"); |
---|
849 | } |
---|
850 | |
---|
851 | static void |
---|
852 | setifmtu(const char *val, int dummy __unused, int s, |
---|
853 | const struct afswtch *afp) |
---|
854 | { |
---|
855 | strncpy(ifr.ifr_name, name, sizeof (ifr.ifr_name)); |
---|
856 | ifr.ifr_mtu = atoi(val); |
---|
857 | if (ioctl(s, SIOCSIFMTU, (caddr_t)&ifr) < 0) |
---|
858 | warn("ioctl (set mtu)"); |
---|
859 | } |
---|
860 | |
---|
861 | static void |
---|
862 | setifname(const char *val, int dummy __unused, int s, |
---|
863 | const struct afswtch *afp) |
---|
864 | { |
---|
865 | char *newname; |
---|
866 | |
---|
867 | newname = strdup(val); |
---|
868 | if (newname == NULL) { |
---|
869 | warn("no memory to set ifname"); |
---|
870 | return; |
---|
871 | } |
---|
872 | ifr.ifr_data = newname; |
---|
873 | if (ioctl(s, SIOCSIFNAME, (caddr_t)&ifr) < 0) { |
---|
874 | warn("ioctl (set name)"); |
---|
875 | free(newname); |
---|
876 | return; |
---|
877 | } |
---|
878 | strlcpy(name, newname, sizeof(name)); |
---|
879 | free(newname); |
---|
880 | } |
---|
881 | |
---|
882 | /* ARGSUSED */ |
---|
883 | static void |
---|
884 | setifdescr(const char *val, int dummy __unused, int s, |
---|
885 | const struct afswtch *afp) |
---|
886 | { |
---|
887 | char *newdescr; |
---|
888 | |
---|
889 | ifr.ifr_buffer.length = strlen(val) + 1; |
---|
890 | if (ifr.ifr_buffer.length == 1) { |
---|
891 | ifr.ifr_buffer.buffer = newdescr = NULL; |
---|
892 | ifr.ifr_buffer.length = 0; |
---|
893 | } else { |
---|
894 | newdescr = strdup(val); |
---|
895 | ifr.ifr_buffer.buffer = newdescr; |
---|
896 | if (newdescr == NULL) { |
---|
897 | warn("no memory to set ifdescr"); |
---|
898 | return; |
---|
899 | } |
---|
900 | } |
---|
901 | |
---|
902 | if (ioctl(s, SIOCSIFDESCR, (caddr_t)&ifr) < 0) |
---|
903 | warn("ioctl (set descr)"); |
---|
904 | |
---|
905 | free(newdescr); |
---|
906 | } |
---|
907 | |
---|
908 | /* ARGSUSED */ |
---|
909 | static void |
---|
910 | unsetifdescr(const char *val, int value, int s, const struct afswtch *afp) |
---|
911 | { |
---|
912 | |
---|
913 | setifdescr("", 0, s, 0); |
---|
914 | } |
---|
915 | |
---|
916 | #define IFFBITS \ |
---|
917 | "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \ |
---|
918 | "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \ |
---|
919 | "\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP" |
---|
920 | |
---|
921 | #define IFCAPBITS \ |
---|
922 | "\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \ |
---|
923 | "\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" \ |
---|
924 | "\21VLAN_HWFILTER\23VLAN_HWTSO\24LINKSTATE" |
---|
925 | |
---|
926 | /* |
---|
927 | * Print the status of the interface. If an address family was |
---|
928 | * specified, show only it; otherwise, show them all. |
---|
929 | */ |
---|
930 | static void |
---|
931 | status(const struct afswtch *afp, const struct sockaddr_dl *sdl, |
---|
932 | struct ifaddrs *ifa) |
---|
933 | { |
---|
934 | struct ifaddrs *ift; |
---|
935 | int allfamilies, s; |
---|
936 | struct ifstat ifs; |
---|
937 | |
---|
938 | if (afp == NULL) { |
---|
939 | allfamilies = 1; |
---|
940 | ifr.ifr_addr.sa_family = AF_LOCAL; |
---|
941 | } else { |
---|
942 | allfamilies = 0; |
---|
943 | ifr.ifr_addr.sa_family = |
---|
944 | afp->af_af == AF_LINK ? AF_LOCAL : afp->af_af; |
---|
945 | } |
---|
946 | strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); |
---|
947 | |
---|
948 | s = socket(ifr.ifr_addr.sa_family, SOCK_DGRAM, 0); |
---|
949 | if (s < 0) |
---|
950 | err(1, "socket(family %u,SOCK_DGRAM)", ifr.ifr_addr.sa_family); |
---|
951 | |
---|
952 | printf("%s: ", name); |
---|
953 | printb("flags", ifa->ifa_flags, IFFBITS); |
---|
954 | if (ioctl(s, SIOCGIFMETRIC, &ifr) != -1) |
---|
955 | printf(" metric %d", ifr.ifr_metric); |
---|
956 | if (ioctl(s, SIOCGIFMTU, &ifr) != -1) |
---|
957 | printf(" mtu %d", ifr.ifr_mtu); |
---|
958 | putchar('\n'); |
---|
959 | |
---|
960 | for (;;) { |
---|
961 | if ((descr = reallocf(descr, descrlen)) != NULL) { |
---|
962 | ifr.ifr_buffer.buffer = descr; |
---|
963 | ifr.ifr_buffer.length = descrlen; |
---|
964 | if (ioctl(s, SIOCGIFDESCR, &ifr) == 0) { |
---|
965 | if (ifr.ifr_buffer.buffer == descr) { |
---|
966 | if (strlen(descr) > 0) |
---|
967 | printf("\tdescription: %s\n", |
---|
968 | descr); |
---|
969 | } else if (ifr.ifr_buffer.length > descrlen) { |
---|
970 | descrlen = ifr.ifr_buffer.length; |
---|
971 | continue; |
---|
972 | } |
---|
973 | } |
---|
974 | } else |
---|
975 | warn("unable to allocate memory for interface" |
---|
976 | "description"); |
---|
977 | break; |
---|
978 | } |
---|
979 | |
---|
980 | if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) { |
---|
981 | if (ifr.ifr_curcap != 0) { |
---|
982 | printb("\toptions", ifr.ifr_curcap, IFCAPBITS); |
---|
983 | putchar('\n'); |
---|
984 | } |
---|
985 | if (supmedia && ifr.ifr_reqcap != 0) { |
---|
986 | printb("\tcapabilities", ifr.ifr_reqcap, IFCAPBITS); |
---|
987 | putchar('\n'); |
---|
988 | } |
---|
989 | } |
---|
990 | |
---|
991 | tunnel_status(s); |
---|
992 | |
---|
993 | for (ift = ifa; ift != NULL; ift = ift->ifa_next) { |
---|
994 | if (ift->ifa_addr == NULL) |
---|
995 | continue; |
---|
996 | if (strcmp(ifa->ifa_name, ift->ifa_name) != 0) |
---|
997 | continue; |
---|
998 | if (allfamilies) { |
---|
999 | const struct afswtch *p; |
---|
1000 | p = af_getbyfamily(ift->ifa_addr->sa_family); |
---|
1001 | if (p != NULL && p->af_status != NULL) |
---|
1002 | p->af_status(s, ift); |
---|
1003 | } else if (afp->af_af == ift->ifa_addr->sa_family) |
---|
1004 | afp->af_status(s, ift); |
---|
1005 | } |
---|
1006 | #if 0 |
---|
1007 | if (allfamilies || afp->af_af == AF_LINK) { |
---|
1008 | const struct afswtch *lafp; |
---|
1009 | |
---|
1010 | /* |
---|
1011 | * Hack; the link level address is received separately |
---|
1012 | * from the routing information so any address is not |
---|
1013 | * handled above. Cobble together an entry and invoke |
---|
1014 | * the status method specially. |
---|
1015 | */ |
---|
1016 | lafp = af_getbyname("lladdr"); |
---|
1017 | if (lafp != NULL) { |
---|
1018 | info.rti_info[RTAX_IFA] = (struct sockaddr *)sdl; |
---|
1019 | lafp->af_status(s, &info); |
---|
1020 | } |
---|
1021 | } |
---|
1022 | #endif |
---|
1023 | if (allfamilies) |
---|
1024 | af_other_status(s); |
---|
1025 | else if (afp->af_other_status != NULL) |
---|
1026 | afp->af_other_status(s); |
---|
1027 | |
---|
1028 | strncpy(ifs.ifs_name, name, sizeof ifs.ifs_name); |
---|
1029 | if (ioctl(s, SIOCGIFSTATUS, &ifs) == 0) |
---|
1030 | printf("%s", ifs.ascii); |
---|
1031 | |
---|
1032 | close(s); |
---|
1033 | return; |
---|
1034 | } |
---|
1035 | |
---|
1036 | static void |
---|
1037 | tunnel_status(int s) |
---|
1038 | { |
---|
1039 | af_all_tunnel_status(s); |
---|
1040 | } |
---|
1041 | |
---|
1042 | void |
---|
1043 | Perror(const char *cmd) |
---|
1044 | { |
---|
1045 | switch (errno) { |
---|
1046 | |
---|
1047 | case ENXIO: |
---|
1048 | errx(1, "%s: no such interface", cmd); |
---|
1049 | break; |
---|
1050 | |
---|
1051 | case EPERM: |
---|
1052 | errx(1, "%s: permission denied", cmd); |
---|
1053 | break; |
---|
1054 | |
---|
1055 | default: |
---|
1056 | err(1, "%s", cmd); |
---|
1057 | } |
---|
1058 | } |
---|
1059 | |
---|
1060 | /* |
---|
1061 | * Print a value a la the %b format of the kernel's printf |
---|
1062 | */ |
---|
1063 | void |
---|
1064 | printb(const char *s, unsigned v, const char *bits) |
---|
1065 | { |
---|
1066 | int i, any = 0; |
---|
1067 | char c; |
---|
1068 | |
---|
1069 | if (bits && *bits == 8) |
---|
1070 | printf("%s=%o", s, v); |
---|
1071 | else |
---|
1072 | printf("%s=%x", s, v); |
---|
1073 | bits++; |
---|
1074 | if (bits) { |
---|
1075 | putchar('<'); |
---|
1076 | while ((i = *bits++) != '\0') { |
---|
1077 | if (v & (1 << (i-1))) { |
---|
1078 | if (any) |
---|
1079 | putchar(','); |
---|
1080 | any = 1; |
---|
1081 | for (; (c = *bits) > 32; bits++) |
---|
1082 | putchar(c); |
---|
1083 | } else |
---|
1084 | for (; *bits > 32; bits++) |
---|
1085 | ; |
---|
1086 | } |
---|
1087 | putchar('>'); |
---|
1088 | } |
---|
1089 | } |
---|
1090 | |
---|
1091 | void |
---|
1092 | ifmaybeload(const char *name) |
---|
1093 | { |
---|
1094 | #ifndef __rtems__ |
---|
1095 | #define MOD_PREFIX_LEN 3 /* "if_" */ |
---|
1096 | struct module_stat mstat; |
---|
1097 | int fileid, modid; |
---|
1098 | char ifkind[IFNAMSIZ + MOD_PREFIX_LEN], ifname[IFNAMSIZ], *dp; |
---|
1099 | const char *cp; |
---|
1100 | |
---|
1101 | /* loading suppressed by the user */ |
---|
1102 | if (noload) |
---|
1103 | return; |
---|
1104 | |
---|
1105 | /* trim the interface number off the end */ |
---|
1106 | strlcpy(ifname, name, sizeof(ifname)); |
---|
1107 | for (dp = ifname; *dp != 0; dp++) |
---|
1108 | if (isdigit(*dp)) { |
---|
1109 | *dp = 0; |
---|
1110 | break; |
---|
1111 | } |
---|
1112 | |
---|
1113 | /* turn interface and unit into module name */ |
---|
1114 | strcpy(ifkind, "if_"); |
---|
1115 | strlcpy(ifkind + MOD_PREFIX_LEN, ifname, |
---|
1116 | sizeof(ifkind) - MOD_PREFIX_LEN); |
---|
1117 | |
---|
1118 | /* scan files in kernel */ |
---|
1119 | mstat.version = sizeof(struct module_stat); |
---|
1120 | for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) { |
---|
1121 | /* scan modules in file */ |
---|
1122 | for (modid = kldfirstmod(fileid); modid > 0; |
---|
1123 | modid = modfnext(modid)) { |
---|
1124 | if (modstat(modid, &mstat) < 0) |
---|
1125 | continue; |
---|
1126 | /* strip bus name if present */ |
---|
1127 | if ((cp = strchr(mstat.name, '/')) != NULL) { |
---|
1128 | cp++; |
---|
1129 | } else { |
---|
1130 | cp = mstat.name; |
---|
1131 | } |
---|
1132 | /* already loaded? */ |
---|
1133 | if (strncmp(ifname, cp, strlen(ifname) + 1) == 0 || |
---|
1134 | strncmp(ifkind, cp, strlen(ifkind) + 1) == 0) |
---|
1135 | return; |
---|
1136 | } |
---|
1137 | } |
---|
1138 | |
---|
1139 | /* not present, we should try to load it */ |
---|
1140 | kldload(ifkind); |
---|
1141 | #endif |
---|
1142 | } |
---|
1143 | |
---|
1144 | static struct cmd basic_cmds[] = { |
---|
1145 | DEF_CMD("up", IFF_UP, setifflags), |
---|
1146 | DEF_CMD("down", -IFF_UP, setifflags), |
---|
1147 | DEF_CMD("arp", -IFF_NOARP, setifflags), |
---|
1148 | DEF_CMD("-arp", IFF_NOARP, setifflags), |
---|
1149 | DEF_CMD("debug", IFF_DEBUG, setifflags), |
---|
1150 | DEF_CMD("-debug", -IFF_DEBUG, setifflags), |
---|
1151 | DEF_CMD_ARG("description", setifdescr), |
---|
1152 | DEF_CMD_ARG("descr", setifdescr), |
---|
1153 | DEF_CMD("-description", 0, unsetifdescr), |
---|
1154 | DEF_CMD("-descr", 0, unsetifdescr), |
---|
1155 | DEF_CMD("promisc", IFF_PPROMISC, setifflags), |
---|
1156 | DEF_CMD("-promisc", -IFF_PPROMISC, setifflags), |
---|
1157 | DEF_CMD("add", IFF_UP, notealias), |
---|
1158 | DEF_CMD("alias", IFF_UP, notealias), |
---|
1159 | DEF_CMD("-alias", -IFF_UP, notealias), |
---|
1160 | DEF_CMD("delete", -IFF_UP, notealias), |
---|
1161 | DEF_CMD("remove", -IFF_UP, notealias), |
---|
1162 | #ifdef notdef |
---|
1163 | #define EN_SWABIPS 0x1000 |
---|
1164 | DEF_CMD("swabips", EN_SWABIPS, setifflags), |
---|
1165 | DEF_CMD("-swabips", -EN_SWABIPS, setifflags), |
---|
1166 | #endif |
---|
1167 | DEF_CMD_ARG("netmask", setifnetmask), |
---|
1168 | DEF_CMD_ARG("metric", setifmetric), |
---|
1169 | DEF_CMD_ARG("broadcast", setifbroadaddr), |
---|
1170 | DEF_CMD_ARG("ipdst", setifipdst), |
---|
1171 | DEF_CMD_ARG2("tunnel", settunnel), |
---|
1172 | DEF_CMD("-tunnel", 0, deletetunnel), |
---|
1173 | DEF_CMD("deletetunnel", 0, deletetunnel), |
---|
1174 | #ifndef __rtems__ |
---|
1175 | DEF_CMD_ARG("vnet", setifvnet), |
---|
1176 | DEF_CMD_ARG("-vnet", setifrvnet), |
---|
1177 | #endif /* __rtems__ */ |
---|
1178 | DEF_CMD("link0", IFF_LINK0, setifflags), |
---|
1179 | DEF_CMD("-link0", -IFF_LINK0, setifflags), |
---|
1180 | DEF_CMD("link1", IFF_LINK1, setifflags), |
---|
1181 | DEF_CMD("-link1", -IFF_LINK1, setifflags), |
---|
1182 | DEF_CMD("link2", IFF_LINK2, setifflags), |
---|
1183 | DEF_CMD("-link2", -IFF_LINK2, setifflags), |
---|
1184 | DEF_CMD("monitor", IFF_MONITOR, setifflags), |
---|
1185 | DEF_CMD("-monitor", -IFF_MONITOR, setifflags), |
---|
1186 | DEF_CMD("staticarp", IFF_STATICARP, setifflags), |
---|
1187 | DEF_CMD("-staticarp", -IFF_STATICARP, setifflags), |
---|
1188 | DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap), |
---|
1189 | DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap), |
---|
1190 | DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap), |
---|
1191 | DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap), |
---|
1192 | DEF_CMD("netcons", IFCAP_NETCONS, setifcap), |
---|
1193 | DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap), |
---|
1194 | DEF_CMD("polling", IFCAP_POLLING, setifcap), |
---|
1195 | DEF_CMD("-polling", -IFCAP_POLLING, setifcap), |
---|
1196 | DEF_CMD("tso", IFCAP_TSO, setifcap), |
---|
1197 | DEF_CMD("-tso", -IFCAP_TSO, setifcap), |
---|
1198 | DEF_CMD("lro", IFCAP_LRO, setifcap), |
---|
1199 | DEF_CMD("-lro", -IFCAP_LRO, setifcap), |
---|
1200 | DEF_CMD("wol", IFCAP_WOL, setifcap), |
---|
1201 | DEF_CMD("-wol", -IFCAP_WOL, setifcap), |
---|
1202 | DEF_CMD("wol_ucast", IFCAP_WOL_UCAST, setifcap), |
---|
1203 | DEF_CMD("-wol_ucast", -IFCAP_WOL_UCAST, setifcap), |
---|
1204 | DEF_CMD("wol_mcast", IFCAP_WOL_MCAST, setifcap), |
---|
1205 | DEF_CMD("-wol_mcast", -IFCAP_WOL_MCAST, setifcap), |
---|
1206 | DEF_CMD("wol_magic", IFCAP_WOL_MAGIC, setifcap), |
---|
1207 | DEF_CMD("-wol_magic", -IFCAP_WOL_MAGIC, setifcap), |
---|
1208 | DEF_CMD("normal", -IFF_LINK0, setifflags), |
---|
1209 | DEF_CMD("compress", IFF_LINK0, setifflags), |
---|
1210 | DEF_CMD("noicmp", IFF_LINK1, setifflags), |
---|
1211 | DEF_CMD_ARG("mtu", setifmtu), |
---|
1212 | DEF_CMD_ARG("name", setifname), |
---|
1213 | }; |
---|
1214 | |
---|
1215 | static __constructor void |
---|
1216 | ifconfig_ctor(void) |
---|
1217 | { |
---|
1218 | #ifdef __rtems__ |
---|
1219 | memset(&ifr, 0, sizeof(ifr)); |
---|
1220 | memset(&name, 0, sizeof(name)); |
---|
1221 | descr = NULL; |
---|
1222 | descrlen = 64; |
---|
1223 | setaddr = 0; |
---|
1224 | setmask = 0; |
---|
1225 | doalias = 0; |
---|
1226 | clearaddr = 0; |
---|
1227 | newaddr = 1; |
---|
1228 | verbose = 0; |
---|
1229 | noload = 0; |
---|
1230 | supmedia = 0; |
---|
1231 | printkeys = 0; |
---|
1232 | opts = NULL; |
---|
1233 | afs = NULL; |
---|
1234 | callbacks = NULL; |
---|
1235 | cmds = NULL; |
---|
1236 | #endif /* __rtems__ */ |
---|
1237 | #define N(a) (sizeof(a) / sizeof(a[0])) |
---|
1238 | size_t i; |
---|
1239 | |
---|
1240 | for (i = 0; i < N(basic_cmds); i++) |
---|
1241 | cmd_register(&basic_cmds[i]); |
---|
1242 | #undef N |
---|
1243 | } |
---|