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