Debian Codegraph @

ifconfig/flags.c

raw
1 /* flags.c -- network interface flag handling
2 Copyright (C) 2001-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /* Written by Marcus Brinkmann. */
20
21 #include <config.h>
22
23 #include <stdio.h>
24
25 #include <string.h>
26
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <net/if.h>
30 #include <stdlib.h>
31 #include "ifconfig.h"
32 #include "xalloc.h"
33
34 /* Conversion table for interface flag names.
35 The mask must be a power of 2. */
36 struct if_flag
37 {
38 const char *name;
39 int mask;
40 int rev;
41 } if_flags[] = {
42 /* Available on all systems which derive the network interface from
43 BSD. Verified for GNU, Linux 2.4, FreeBSD, Solaris 2.7, HP-UX
44 10.20 and OSF 4.0g. */
45
46 #ifdef IFF_UP /* Interface is up. */
47 {"UP", IFF_UP, 0},
48 #endif
49 #ifdef IFF_BROADCAST /* Broadcast address is valid. */
50 {"BROADCAST", IFF_BROADCAST, 0},
51 #endif
52 #ifdef IFF_DEBUG /* Debugging is turned on. */
53 {"DEBUG", IFF_DEBUG, 0},
54 #endif
55 #ifdef IFF_LOOPBACK /* Is a loopback net. */
56 {"LOOPBACK", IFF_LOOPBACK, 0},
57 #endif
58 #ifdef IFF_POINTOPOINT /* Interface is a point-to-point link. */
59 {"POINTOPOINT", IFF_POINTOPOINT, 0},
60 #endif
61 #ifdef IFF_RUNNING /* Resources allocated. */
62 {"RUNNING", IFF_RUNNING, 0},
63 #endif
64 #ifdef IFF_NOARP /* No address resolution protocol. */
65 {"NOARP", IFF_NOARP, 0},
66 {"ARP", IFF_NOARP, 1},
67 #endif
68 /* Keep IFF_PPROMISC prior to IFF_PROMISC
69 * for precedence in parsing.
70 */
71 #ifdef IFF_PPROMISC /* User accessible promiscuous mode. */
72 {"PROMISC", IFF_PPROMISC, 0},
73 #endif
74 #ifdef IFF_PROMISC /* Receive all packets. */
75 {"PROMISC", IFF_PROMISC, 0},
76 #endif
77 #ifdef IFF_ALLMULTI /* Receive all multicast packets. */
78 {"ALLMULTI", IFF_ALLMULTI, 0},
79 #endif
80 #ifdef IFF_MULTICAST /* Supports multicast. */
81 {"MULTICAST", IFF_MULTICAST, 0},
82 #endif
83 /* Usually available on all systems which derive the network
84 interface from BSD (see above), but with exceptions noted. */
85 #ifdef IFF_NOTRAILERS /* Avoid use of trailers. */
86 /* Obsoleted on FreeBSD systems. */
87 {"NOTRAILERS", IFF_NOTRAILERS, 0},
88 {"TRAILERS", IFF_NOTRAILERS, 1},
89 #endif
90 /* Available on GNU and Linux systems. */
91 #ifdef IFF_MASTER /* Master of a load balancer. */
92 {"MASTER", IFF_MASTER, 0},
93 #endif
94 #ifdef IFF_SLAVE /* Slave of a load balancer. */
95 {"SLAVE", IFF_SLAVE, 0},
96 #endif
97 #ifdef IFF_PORTSEL /* Can set media type. */
98 {"PORTSEL", IFF_PORTSEL, 0},
99 #endif
100 #ifdef IFF_AUTOMEDIA /* Auto media select is active. */
101 {"AUTOMEDIA", IFF_AUTOMEDIA, 0},
102 #endif
103 /* Available on Linux 2.4 systems (not glibc <= 2.2.1). */
104 #ifdef IFF_DYNAMIC /* Dialup service with hanging addresses. */
105 {"DYNAMIC", IFF_DYNAMIC, 0},
106 #endif
107 /* Available on FreeBSD and OSF 4.0g systems. */
108 #ifdef IFF_OACTIVE /* Transmission is in progress. */
109 {"OACTIVE", IFF_OACTIVE, 0},
110 #endif
111 #ifdef IFF_SIMPLEX /* Can't hear own transmissions. */
112 {"SIMPLEX", IFF_SIMPLEX, 0},
113 #endif
114 /* Available on FreeBSD systems. */
115 #ifdef IFF_LINK0 /* Per link layer defined bit. */
116 {"LINK0", IFF_LINK0, 0},
117 #endif
118 #ifdef IFF_LINK1 /* Per link layer defined bit. */
119 {"LINK1", IFF_LINK1, 0},
120 #endif
121 #if defined IFF_LINK2 && defined IFF_ALTPHYS
122 # if IFF_LINK2 == IFF_ALTPHYS
123 /* IFF_ALTPHYS == IFF_LINK2 on FreeBSD. This entry is used as a
124 fallback for if_flagtoname conversion, if no relevant EXPECT_
125 macro is specified to figure out which one is meant. */
126 {"LINK2/ALTPHYS", IFF_LINK2, 0},
127 # endif
128 #endif
129 #ifdef IFF_LINK2 /* Per link layer defined bit. */
130 {"LINK2", IFF_LINK2, 0},
131 #endif
132 #ifdef IFF_ALTPHYS /* Use alternate physical connection. */
133 {"ALTPHYS", IFF_ALTPHYS, 0},
134 #endif
135 /* Available on Solaris 2.7 systems. */
136 #ifdef IFF_INTELLIGENT /* Protocol code on board. */
137 {"INTELLIGENT", IFF_INTELLIGENT, 0},
138 #endif
139 #ifdef IFF_MULTI_BCAST /* Multicast using broadcast address. */
140 {"MULTI_BCAST", IFF_MULTI_BCAST, 0},
141 #endif
142 #ifdef IFF_UNNUMBERED /* Address is not unique. */
143 {"UNNUMBERED", IFF_UNNUMBERED, 0},
144 #endif
145 #ifdef IFF_DHCPRUNNING /* Interface is under control of DHCP. */
146 {"DHCPRUNNING", IFF_DHCPRUNNING, 0},
147 #endif
148 #ifdef IFF_PRIVATE /* Do not advertise. */
149 {"PRIVATE", IFF_PRIVATE, 0},
150 #endif
151 /* Available on HP-UX 10.20 systems. */
152 #ifdef IFF_NOTRAILERS /* Avoid use of trailers. */
153 {"NOTRAILERS", IFF_NOTRAILERS, 0},
154 {"TRAILERS", IFF_NOTRAILERS, 1},
155 #endif
156 #ifdef IFF_LOCALSUBNETS /* Subnets of this net are local. */
157 {"LOCALSUBNETS", IFF_LOCALSUBNETS, 0},
158 #endif
159 #ifdef IFF_CKO /* Interface supports header checksum. */
160 {"CKO", IFF_CKO, 0},
161 #endif
162 #ifdef IFF_NOACC /* No data access on outbound. */
163 {"NOACC", IFF_NOACC, 0},
164 {"ACC", IFF_NOACC, 1},
165 #endif
166 #ifdef IFF_NOSR8025 /* No source route 802.5. */
167 {"NOSR8025", IFF_NOSR8025, 0},
168 {"SR8025", IFF_NOSR8025, 1},
169 #endif
170 #ifdef IFF_CKO_ETC /* Interface supports trailer checksum. */
171 {"CKO_ETC", IFF_CKO_ETC, 0},
172 #endif
173 #ifdef IFF_AR_SR8025 /* All routes broadcast for ARP 8025. */
174 {"AR_SR8025", IFF_AR_SR8025, 0},
175 #endif
176 #ifdef IFF_ALT_SR8025 /* Alternating no rif, rif for ARP on. */
177 {"ALT_SR8025", IFF_ALT_SR8025, 0},
178 #endif
179 /* Defined on OSF 4.0g systems. */
180 #ifdef IFF_PFCOPYALL /* PFILT gets packets to this host. */
181 {"PFCOPYALL", IFF_PFCOPYALL, 0},
182 #endif
183 #ifdef IFF_UIOMOVE /* DART. */
184 {"UIOMOVE", IFF_UIOMOVE, 0},
185 #endif
186 #ifdef IFF_PKTOK /* DART. */
187 {"PKTOK", IFF_PKTOK, 0},
188 #endif
189 #ifdef IFF_SOCKBUF /* DART. */
190 {"SOCKBUF", IFF_SOCKBUF, 0},
191 #endif
192 #ifdef IFF_VAR_MTU /* Interface supports variable MTUs. */
193 {"VAR_MTU", IFF_VAR_MTU, 0},
194 #endif
195 #ifdef IFF_NOCHECKSUM /* No checksums needed (reliable media). */
196 {"NOCHECKSUM", IFF_NOCHECKSUM, 0},
197 {"CHECKSUM", IFF_NOCHECKSUM, 1},
198 #endif
199 #ifdef IFF_MULTINET /* Multiple networks on interface. */
200 {"MULTINET", IFF_MULTINET, 0},
201 #endif
202 #ifdef IFF_VMIFNET /* Used to identify a virtual MAC address. */
203 {"VMIFNET", IFF_VMIFNET, 0},
204 #endif
205 #if defined IFF_D1 && defined IFF_SNAP
206 # if IFF_D1 == IFF_SNAP
207 /* IFF_SNAP == IFF_D1 on OSF 4.0g systems. This entry is used as a
208 fallback for if_flagtoname conversion, if no relevant EXPECT_
209 macro is specified to figure out which one is meant. */
210 {"D1/SNAP", IFF_D2, 0},
211 # endif
212 #endif
213 #ifdef IFF_D2 /* Flag is specific to device. */
214 {"D2", IFF_D2, 0},
215 #endif
216 #ifdef IFF_SNAP /* Ethernet driver outputs SNAP header. */
217 {"SNAP", IFF_SNAP, 0},
218 #endif
219 #ifdef IFF_CANTCONFIG
220 {"CANTCONFIG", IFF_CANTCONFIG, 0},
221 #endif
222 #ifdef IFF_MONITOR
223 {"MONITOR", IFF_MONITOR, 0},
224 #endif
225 #ifdef IFF_STATICARP
226 {"STATICARP", IFF_STATICARP, 0},
227 #endif
228 {NULL, 0, 0}
229 };
230
231 static int
232 cmpname (const void *a, const void *b)
233 {
234 return strcmp (*(const char **) a, *(const char **) b);
235 }
236
237 char *
238 if_list_flags (const char *prefix)
239 {
240 #define FLAGS_COMMENT "\nPrepend 'no' to negate the effect."
241 size_t len = 0;
242 struct if_flag *fp;
243 char **fnames;
244 size_t i, fcount;
245 char *str, *p;
246
247 for (fp = if_flags, len = 0, fcount = 0; fp->name; fp++)
248 if (!fp->rev)
249 {
250 fcount++;
251 len += strlen (fp->name) + 1;
252 }
253
254 fcount = sizeof (if_flags) / sizeof (if_flags[0]) - 1;
255 fnames = xmalloc (fcount * sizeof (fnames[0]) + len);
256 p = (char *) (fnames + fcount);
257
258 for (fp = if_flags, i = 0; fp->name; fp++)
259 if (!fp->rev)
260 {
261 const char *q;
262
263 if (fp->mask & IU_IFF_CANTCHANGE)
264 continue;
265
266 fnames[i++] = p;
267 q = fp->name;
268 if (strncmp (q, "NO", 2) == 0)
269 q += 2;
270 for (; *q; q++)
271 *p++ = tolower (*q);
272 *p++ = 0;
273 }
274 fcount = i;
275 qsort (fnames, fcount, sizeof (fnames[0]), cmpname);
276
277 len += strlen (", ") * fcount; /* Delimiters */
278
279 if (prefix)
280 len += strlen (prefix);
281
282 len += strlen (FLAGS_COMMENT);
283
284 str = xmalloc (len + 1);
285 p = str;
286 if (prefix)
287 {
288 strcpy (p, prefix);
289 p += strlen (prefix);
290 }
291
292 for (i = 0; i < fcount; i++)
293 {
294 /* Omit repeated, or alternate names, like "link2/altphys". */
295 if (i && strncmp (fnames[i - 1], fnames[i],
296 strlen (fnames[i - 1])) == 0)
297 continue;
298 strcpy (p, fnames[i]);
299 p += strlen (fnames[i]);
300 if (i + 1 < fcount)
301 {
302 *p++ = ',';
303 *p++ = ' ';
304 }
305 }
306 strcpy (p, FLAGS_COMMENT);
307 p += strlen (FLAGS_COMMENT);
308 #undef FLAGS_COMMENT
309 *p = 0;
310 free (fnames);
311 return str;
312 }
313
314 /* Return the name corresponding to the interface flag FLAG.
315 If FLAG is unknown, return NULL.
316 AVOID contains a ':' surrounded and separated list of flag names
317 that should be avoided if alternative names with the same flag value
318 exists. The first unavoided match is returned, or the first avoided
319 match if no better is available. */
320 const char *
321 if_flagtoname (int flag, const char *avoid)
322 {
323 struct if_flag *fp;
324 const char *first_match = NULL;
325 char *start;
326
327 for (fp = if_flags;; fp++)
328 {
329 if (!fp->name)
330 return NULL;
331 if (flag == fp->mask && !fp->rev)
332 break;
333 }
334
335 first_match = fp->name;
336
337 /* We now have found the first match. Look for a better one. */
338 if (avoid)
339 do
340 {
341 start = strstr (avoid, fp->name);
342 if (!start || *(start - 1) != ':'
343 || *(start + strlen (fp->name)) != ':')
344 break;
345 fp++;
346 }
347 while (fp->name);
348
349 if (fp->name)
350 return fp->name;
351 else
352 return first_match;
353 }
354
355 int
356 if_nametoflag (const char *name, size_t len, int *prev)
357 {
358 struct if_flag *fp;
359 int rev = 0;
360
361 if (len > 1 && name[0] == '-')
362 {
363 name++;
364 len--;
365 rev = 1;
366 }
367 else if (len > 2 && strncasecmp (name, "NO", 2) == 0)
368 {
369 name += 2;
370 len -= 2;
371 rev = 1;
372 }
373
374 for (fp = if_flags; fp->name; fp++)
375 {
376 if (strncasecmp (fp->name, name, len) == 0)
377 {
378 *prev = fp->rev ^ rev;
379 return fp->mask;
380 }
381 }
382 return 0;
383 }
384
385 int
386 if_nameztoflag (const char *name, int *prev)
387 {
388 return if_nametoflag (name, strlen (name), prev);
389 }
390
391 struct if_flag_char
392 {
393 int mask;
394 int ch;
395 };
396
397 /* Interface flag bits and the corresponding letters for short output.
398 Notice that the entries must be ordered alphabetically, by the letter name.
399 There are two lamentable exceptions:
400
401 1. The 'd' is misplaced.
402 2. The 'P' letter is ambiguous. Depending on its position in the output
403 line it may stand for IFF_PROMISC or IFF_POINTOPOINT.
404
405 That's the way netstat does it.
406 */
407 static struct if_flag_char flag_char_tab[] = {
408 #ifdef IFF_ALLMULTI
409 {IFF_ALLMULTI, 'A'},
410 #endif
411 #ifdef IFF_BROADCAST
412 {IFF_BROADCAST, 'B'},
413 #endif
414 #ifdef IFF_DEBUG
415 {IFF_DEBUG, 'D'},
416 #endif
417 #ifdef IFF_LOOPBACK
418 {IFF_LOOPBACK, 'L'},
419 #endif
420 #ifdef IFF_MULTICAST
421 {IFF_MULTICAST, 'M'},
422 #endif
423 #ifdef HAVE_DYNAMIC
424 {IFF_DYNAMIC, 'd'},
425 #endif
426 #ifdef IFF_PROMISC
427 {IFF_PROMISC, 'P'},
428 #endif
429 #ifdef IFF_NOTRAILERS
430 {IFF_NOTRAILERS, 'N'},
431 #endif
432 #ifdef IFF_NOARP
433 {IFF_NOARP, 'O'},
434 #endif
435 #ifdef IFF_POINTOPOINT
436 {IFF_POINTOPOINT, 'P'},
437 #endif
438 #ifdef IFF_SLAVE
439 {IFF_SLAVE, 's'}, /* Linux */
440 #endif
441 #ifdef IFF_STATICARP
442 {IFF_STATICARP, 's'}, /* FreeBSD */
443 #endif
444 #ifdef IFF_MASTER
445 {IFF_MASTER, 'm'}, /* Linux */
446 #endif
447 #ifdef IFF_MONITOR
448 {IFF_MONITOR, 'm'}, /* FreeBSD */
449 #endif
450 #ifdef IFF_SIMPLEX
451 {IFF_SIMPLEX, 'S'},
452 #endif
453 #ifdef IFF_RUNNING
454 {IFF_RUNNING, 'R'},
455 #endif
456 #ifdef IFF_UP
457 {IFF_UP, 'U'},
458 #endif
459 {0, 0}
460 };
461
462 void
463 if_format_flags (int flags, char *buf, size_t size)
464 {
465 struct if_flag_char *fp;
466 size--;
467 for (fp = flag_char_tab; size && fp->mask; fp++)
468 if (fp->mask & flags)
469 {
470 *buf++ = fp->ch;
471 size--;
472 }
473 *buf = 0;
474 }
475
476
477 /* Print the flags in FLAGS, using AVOID as in if_flagtoname, and
478 SEPARATOR between individual flags. Returns the number of
479 characters printed. */
480 int
481 print_if_flags (int flags, const char *avoid, char separator)
482 {
483 int f = 1;
484 const char *name;
485 int first = 1;
486 int length = 0;
487
488 while (flags && f)
489 {
490 if (f & flags)
491 {
492 name = if_flagtoname (f, avoid);
493 if (name)
494 {
495 if (!first)
496 {
497 putchar (separator);
498 length++;
499 }
500 length += printf ("%s", name);
501 flags &= ~f;
502 first = 0;
503 }
504 }
505 f = f << 1;
506 }
507 if (flags)
508 {
509 if (!first)
510 {
511 putchar (separator);
512 length++;
513 }
514 length += printf ("%#x", flags);
515 }
516 return length;
517 }