Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1 | /* |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2 | * stolen from net-tools-1.59 and stripped down for busybox by |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 3 | * Erik Andersen <andersen@codepoet.org> |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 4 | * |
| 5 | * Heavily modified by Manuel Novoa III Mar 12, 2001 |
| 6 | * |
| 7 | * Pruned unused code using KEEP_UNUSED define. |
| 8 | * Added print_bytes_scaled function to reduce code size. |
| 9 | * Added some (potentially) missing defines. |
| 10 | * Improved display support for -a and for a named interface. |
| 11 | * |
| 12 | * ----------------------------------------------------------- |
| 13 | * |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 14 | * ifconfig This file contains an implementation of the command |
| 15 | * that either displays or sets the characteristics of |
| 16 | * one or more of the system's networking interfaces. |
| 17 | * |
Eric Andersen | 6fea732 | 2004-08-26 21:45:21 +0000 | [diff] [blame] | 18 | * Version: $Id: interface.c,v 1.25 2004/08/26 21:45:21 andersen Exp $ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 19 | * |
| 20 | * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org> |
| 21 | * and others. Copyright 1993 MicroWalt Corporation |
| 22 | * |
| 23 | * This program is free software; you can redistribute it |
| 24 | * and/or modify it under the terms of the GNU General |
| 25 | * Public License as published by the Free Software |
| 26 | * Foundation; either version 2 of the License, or (at |
| 27 | * your option) any later version. |
| 28 | * |
| 29 | * Patched to support 'add' and 'del' keywords for INET(4) addresses |
| 30 | * by Mrs. Brisby <mrs.brisby@nimh.org> |
| 31 | * |
| 32 | * {1.34} - 19980630 - Arnaldo Carvalho de Melo <acme@conectiva.com.br> |
| 33 | * - gettext instead of catgets for i18n |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 34 | * 10/1998 - Andi Kleen. Use interface list primitives. |
| 35 | * 20001008 - Bernd Eckenfels, Patch from RH for setting mtu |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 36 | * (default AF was wrong) |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 37 | */ |
| 38 | |
| 39 | /* #define KEEP_UNUSED */ |
| 40 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 41 | /* |
| 42 | * |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 43 | * Protocol Families. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 44 | * |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 45 | */ |
| 46 | #define HAVE_AFINET 1 |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 47 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 48 | /* |
| 49 | * |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 50 | * Device Hardware types. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 51 | * |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 52 | */ |
| 53 | #define HAVE_HWETHER 1 |
| 54 | #define HAVE_HWPPP 1 |
| 55 | #undef HAVE_HWSLIP |
| 56 | |
| 57 | |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 58 | #include "inet_common.h" |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 59 | #include <stdio.h> |
| 60 | #include <errno.h> |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 61 | #include <stdlib.h> |
| 62 | #include <string.h> |
| 63 | #include <unistd.h> |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 64 | #include <fcntl.h> |
| 65 | #include <ctype.h> |
| 66 | #include <sys/ioctl.h> |
Eric Andersen | 85e5e72 | 2003-07-22 08:56:55 +0000 | [diff] [blame] | 67 | #include <sys/types.h> |
Eric Andersen | cd8c436 | 2001-11-10 11:22:46 +0000 | [diff] [blame] | 68 | #include <net/if.h> |
| 69 | #include <net/if_arp.h> |
Bernhard Reutner-Fischer | fa939aa | 2006-04-05 16:21:37 +0000 | [diff] [blame] | 70 | #include "busybox.h" |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 71 | |
Glenn L McGrath | b54a748 | 2003-08-29 11:34:08 +0000 | [diff] [blame] | 72 | #ifdef CONFIG_FEATURE_IPV6 |
| 73 | # define HAVE_AFINET6 1 |
| 74 | #else |
| 75 | # undef HAVE_AFINET6 |
| 76 | #endif |
| 77 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 78 | #define _PATH_PROCNET_DEV "/proc/net/dev" |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 79 | #define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6" |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 80 | #define new(p) ((p) = xcalloc(1,sizeof(*(p)))) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 81 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 82 | #ifdef HAVE_HWSLIP |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 83 | #include <net/if_slip.h> |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 84 | #endif |
| 85 | |
| 86 | #if HAVE_AFINET6 |
| 87 | |
| 88 | #ifndef _LINUX_IN6_H |
| 89 | /* |
| 90 | * This is in linux/include/net/ipv6.h. |
| 91 | */ |
| 92 | |
| 93 | struct in6_ifreq { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 94 | struct in6_addr ifr6_addr; |
Glenn L McGrath | b54a748 | 2003-08-29 11:34:08 +0000 | [diff] [blame] | 95 | uint32_t ifr6_prefixlen; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 96 | unsigned int ifr6_ifindex; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | #endif |
| 100 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 101 | #endif /* HAVE_AFINET6 */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 102 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 103 | /* Defines for glibc2.0 users. */ |
| 104 | #ifndef SIOCSIFTXQLEN |
| 105 | #define SIOCSIFTXQLEN 0x8943 |
| 106 | #define SIOCGIFTXQLEN 0x8942 |
| 107 | #endif |
| 108 | |
| 109 | /* ifr_qlen is ifru_ivalue, but it isn't present in 2.0 kernel headers */ |
| 110 | #ifndef ifr_qlen |
| 111 | #define ifr_qlen ifr_ifru.ifru_mtu |
| 112 | #endif |
| 113 | |
| 114 | #ifndef HAVE_TXQUEUELEN |
| 115 | #define HAVE_TXQUEUELEN 1 |
| 116 | #endif |
| 117 | |
| 118 | #ifndef IFF_DYNAMIC |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 119 | #define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 120 | #endif |
| 121 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 122 | /* This structure defines protocol families and their handlers. */ |
| 123 | struct aftype { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 124 | const char *name; |
| 125 | const char *title; |
| 126 | int af; |
| 127 | int alen; |
| 128 | char *(*print) (unsigned char *); |
| 129 | char *(*sprint) (struct sockaddr *, int numeric); |
| 130 | int (*input) (int type, char *bufp, struct sockaddr *); |
| 131 | void (*herror) (char *text); |
| 132 | int (*rprint) (int options); |
| 133 | int (*rinput) (int typ, int ext, char **argv); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 134 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 135 | /* may modify src */ |
| 136 | int (*getmask) (char *src, struct sockaddr * mask, char *name); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 137 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 138 | int fd; |
| 139 | char *flag_file; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 142 | #ifdef KEEP_UNUSED |
| 143 | |
| 144 | static int flag_unx; |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 145 | static int flag_inet; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 146 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 147 | static struct aftrans_t { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 148 | char *alias; |
| 149 | char *name; |
| 150 | int *flag; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 151 | } aftrans[] = { |
| 152 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 153 | { |
| 154 | "ip", "inet", &flag_inet}, |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 155 | #ifdef HAVE_AFINET6 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 156 | { |
| 157 | "ip6", "inet6", &flag_inet6}, |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 158 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 159 | { |
| 160 | "inet", "inet", &flag_inet}, |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 161 | #ifdef HAVE_AFINET6 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 162 | { |
| 163 | "inet6", "inet6", &flag_inet6}, |
Eric Andersen | 8b113f9 | 2001-06-01 21:47:15 +0000 | [diff] [blame] | 164 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 165 | { |
| 166 | "unix", "unix", &flag_unx}, { |
| 167 | "tcpip", "inet", &flag_inet}, |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 168 | { |
| 169 | 0, 0, 0} |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 172 | static char afname[256] = ""; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 173 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 174 | |
| 175 | #if HAVE_AFUNIX |
| 176 | |
| 177 | /* Display a UNIX domain address. */ |
| 178 | static char *UNIX_print(unsigned char *ptr) |
| 179 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 180 | return (ptr); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | |
| 184 | /* Display a UNIX domain address. */ |
| 185 | static char *UNIX_sprint(struct sockaddr *sap, int numeric) |
| 186 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 187 | static char buf[64]; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 188 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 189 | if (sap->sa_family == 0xFFFF || sap->sa_family == 0) |
| 190 | return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf)); |
| 191 | return (UNIX_print(sap->sa_data)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 195 | static struct aftype unix_aftype = { |
| 196 | "unix", "UNIX Domain", AF_UNIX, 0, |
| 197 | UNIX_print, UNIX_sprint, NULL, NULL, |
| 198 | NULL, NULL, NULL, |
| 199 | -1, |
| 200 | "/proc/net/unix" |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 201 | }; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 202 | #endif /* HAVE_AFUNIX */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 203 | |
| 204 | #if HAVE_AFINET |
| 205 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 206 | #ifdef KEEP_UNUSED |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 207 | static void INET_reserror(char *text) |
| 208 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 209 | herror(text); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 212 | /* Display an Internet socket address. */ |
| 213 | static char *INET_print(unsigned char *ptr) |
| 214 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 215 | return (inet_ntoa((*(struct in_addr *) ptr))); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 216 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 217 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 218 | |
| 219 | /* Display an Internet socket address. */ |
| 220 | static char *INET_sprint(struct sockaddr *sap, int numeric) |
| 221 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 222 | static char buff[128]; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 223 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 224 | if (sap->sa_family == 0xFFFF || sap->sa_family == 0) |
| 225 | return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 226 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 227 | if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap, |
| 228 | numeric, 0xffffff00) != 0) |
| 229 | return (NULL); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 230 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 231 | return (buff); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 234 | #ifdef KEEP_UNUSED |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 235 | static char *INET_sprintmask(struct sockaddr *sap, int numeric, |
| 236 | unsigned int netmask) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 237 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 238 | static char buff[128]; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 239 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 240 | if (sap->sa_family == 0xFFFF || sap->sa_family == 0) |
| 241 | return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff)); |
| 242 | if (INET_rresolve(buff, sizeof(buff), (struct sockaddr_in *) sap, |
| 243 | numeric, netmask) != 0) |
| 244 | return (NULL); |
| 245 | return (buff); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 248 | static int INET_getsock(char *bufp, struct sockaddr *sap) |
| 249 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 250 | char *sp = bufp, *bp; |
| 251 | unsigned int i; |
| 252 | unsigned val; |
| 253 | struct sockaddr_in *sin; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 254 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 255 | sin = (struct sockaddr_in *) sap; |
| 256 | sin->sin_family = AF_INET; |
| 257 | sin->sin_port = 0; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 258 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 259 | val = 0; |
| 260 | bp = (char *) &val; |
| 261 | for (i = 0; i < sizeof(sin->sin_addr.s_addr); i++) { |
| 262 | *sp = toupper(*sp); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 263 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 264 | if ((*sp >= 'A') && (*sp <= 'F')) |
| 265 | bp[i] |= (int) (*sp - 'A') + 10; |
| 266 | else if ((*sp >= '0') && (*sp <= '9')) |
| 267 | bp[i] |= (int) (*sp - '0'); |
| 268 | else |
| 269 | return (-1); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 270 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 271 | bp[i] <<= 4; |
| 272 | sp++; |
| 273 | *sp = toupper(*sp); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 274 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 275 | if ((*sp >= 'A') && (*sp <= 'F')) |
| 276 | bp[i] |= (int) (*sp - 'A') + 10; |
| 277 | else if ((*sp >= '0') && (*sp <= '9')) |
| 278 | bp[i] |= (int) (*sp - '0'); |
| 279 | else |
| 280 | return (-1); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 281 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 282 | sp++; |
| 283 | } |
| 284 | sin->sin_addr.s_addr = htonl(val); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 285 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 286 | return (sp - bufp); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | static int INET_input(int type, char *bufp, struct sockaddr *sap) |
| 290 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 291 | switch (type) { |
| 292 | case 1: |
| 293 | return (INET_getsock(bufp, sap)); |
| 294 | case 256: |
| 295 | return (INET_resolve(bufp, (struct sockaddr_in *) sap, 1)); |
| 296 | default: |
| 297 | return (INET_resolve(bufp, (struct sockaddr_in *) sap, 0)); |
| 298 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | static int INET_getnetmask(char *adr, struct sockaddr *m, char *name) |
| 302 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 303 | struct sockaddr_in *mask = (struct sockaddr_in *) m; |
| 304 | char *slash, *end; |
| 305 | int prefix; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 306 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 307 | if ((slash = strchr(adr, '/')) == NULL) |
| 308 | return 0; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 309 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 310 | *slash++ = '\0'; |
| 311 | prefix = strtoul(slash, &end, 0); |
| 312 | if (*end != '\0') |
| 313 | return -1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 314 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 315 | if (name) { |
| 316 | sprintf(name, "/%d", prefix); |
| 317 | } |
| 318 | mask->sin_family = AF_INET; |
| 319 | mask->sin_addr.s_addr = htonl(~(0xffffffffU >> prefix)); |
| 320 | return 1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 321 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 322 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 323 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 324 | static struct aftype inet_aftype = { |
| 325 | "inet", "DARPA Internet", AF_INET, sizeof(unsigned long), |
| 326 | NULL /* UNUSED INET_print */ , INET_sprint, |
| 327 | NULL /* UNUSED INET_input */ , NULL /* UNUSED INET_reserror */ , |
| 328 | NULL /*INET_rprint */ , NULL /*INET_rinput */ , |
| 329 | NULL /* UNUSED INET_getnetmask */ , |
| 330 | -1, |
| 331 | NULL |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 332 | }; |
| 333 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 334 | #endif /* HAVE_AFINET */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 335 | |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 336 | #if HAVE_AFINET6 |
| 337 | |
| 338 | #ifdef KEEP_UNUSED |
| 339 | static void INET6_reserror(char *text) |
| 340 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 341 | herror(text); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /* Display an Internet socket address. */ |
| 345 | static char *INET6_print(unsigned char *ptr) |
| 346 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 347 | static char name[80]; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 348 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 349 | inet_ntop(AF_INET6, (struct in6_addr *) ptr, name, 80); |
| 350 | return name; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 351 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 352 | #endif /* KEEP_UNUSED */ |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 353 | |
| 354 | /* Display an Internet socket address. */ |
| 355 | /* dirty! struct sockaddr usually doesn't suffer for inet6 addresses, fst. */ |
| 356 | static char *INET6_sprint(struct sockaddr *sap, int numeric) |
| 357 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 358 | static char buff[128]; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 359 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 360 | if (sap->sa_family == 0xFFFF || sap->sa_family == 0) |
| 361 | return safe_strncpy(buff, _("[NONE SET]"), sizeof(buff)); |
| 362 | if (INET6_rresolve |
| 363 | (buff, sizeof(buff), (struct sockaddr_in6 *) sap, numeric) != 0) |
| 364 | return safe_strncpy(buff, _("[UNKNOWN]"), sizeof(buff)); |
| 365 | return (buff); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | #ifdef KEEP_UNUSED |
| 369 | static int INET6_getsock(char *bufp, struct sockaddr *sap) |
| 370 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 371 | struct sockaddr_in6 *sin6; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 372 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 373 | sin6 = (struct sockaddr_in6 *) sap; |
| 374 | sin6->sin6_family = AF_INET6; |
| 375 | sin6->sin6_port = 0; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 376 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 377 | if (inet_pton(AF_INET6, bufp, sin6->sin6_addr.s6_addr) <= 0) |
| 378 | return (-1); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 379 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 380 | return 16; /* ?;) */ |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | static int INET6_input(int type, char *bufp, struct sockaddr *sap) |
| 384 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 385 | switch (type) { |
| 386 | case 1: |
| 387 | return (INET6_getsock(bufp, sap)); |
| 388 | default: |
| 389 | return (INET6_resolve(bufp, (struct sockaddr_in6 *) sap)); |
| 390 | } |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 391 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 392 | #endif /* KEEP_UNUSED */ |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 393 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 394 | static struct aftype inet6_aftype = { |
| 395 | "inet6", "IPv6", AF_INET6, sizeof(struct in6_addr), |
| 396 | NULL /* UNUSED INET6_print */ , INET6_sprint, |
| 397 | NULL /* UNUSED INET6_input */ , NULL /* UNUSED INET6_reserror */ , |
| 398 | NULL /*INET6_rprint */ , NULL /*INET6_rinput */ , |
| 399 | NULL /* UNUSED INET6_getnetmask */ , |
| 400 | -1, |
| 401 | NULL |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 402 | }; |
| 403 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 404 | #endif /* HAVE_AFINET6 */ |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 405 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 406 | /* Display an UNSPEC address. */ |
| 407 | static char *UNSPEC_print(unsigned char *ptr) |
| 408 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 409 | static char buff[sizeof(struct sockaddr) * 3 + 1]; |
| 410 | char *pos; |
| 411 | unsigned int i; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 412 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 413 | pos = buff; |
| 414 | for (i = 0; i < sizeof(struct sockaddr); i++) { |
| 415 | /* careful -- not every libc's sprintf returns # bytes written */ |
| 416 | sprintf(pos, "%02X-", (*ptr++ & 0377)); |
| 417 | pos += 3; |
| 418 | } |
| 419 | /* Erase trailing "-". Works as long as sizeof(struct sockaddr) != 0 */ |
| 420 | *--pos = '\0'; |
| 421 | return (buff); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /* Display an UNSPEC socket address. */ |
| 425 | static char *UNSPEC_sprint(struct sockaddr *sap, int numeric) |
| 426 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 427 | static char buf[64]; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 428 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 429 | if (sap->sa_family == 0xFFFF || sap->sa_family == 0) |
| 430 | return safe_strncpy(buf, _("[NONE SET]"), sizeof(buf)); |
Eric Andersen | 0cb6f35 | 2006-01-30 22:30:41 +0000 | [diff] [blame] | 431 | return (UNSPEC_print((unsigned char *)sap->sa_data)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 434 | static struct aftype unspec_aftype = { |
| 435 | "unspec", "UNSPEC", AF_UNSPEC, 0, |
| 436 | UNSPEC_print, UNSPEC_sprint, NULL, NULL, |
| 437 | NULL, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 438 | }; |
| 439 | |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 440 | static struct aftype * const aftypes[] = { |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 441 | #if HAVE_AFUNIX |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 442 | &unix_aftype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 443 | #endif |
| 444 | #if HAVE_AFINET |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 445 | &inet_aftype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 446 | #endif |
| 447 | #if HAVE_AFINET6 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 448 | &inet6_aftype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 449 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 450 | &unspec_aftype, |
| 451 | NULL |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 452 | }; |
| 453 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 454 | #ifdef KEEP_UNUSED |
| 455 | static short sVafinit = 0; |
| 456 | |
| 457 | static void afinit() |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 458 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 459 | unspec_aftype.title = _("UNSPEC"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 460 | #if HAVE_AFUNIX |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 461 | unix_aftype.title = _("UNIX Domain"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 462 | #endif |
| 463 | #if HAVE_AFINET |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 464 | inet_aftype.title = _("DARPA Internet"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 465 | #endif |
| 466 | #if HAVE_AFINET6 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 467 | inet6_aftype.title = _("IPv6"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 468 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 469 | sVafinit = 1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 472 | static int aftrans_opt(const char *arg) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 473 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 474 | struct aftrans_t *paft; |
| 475 | char *tmp1, *tmp2; |
| 476 | char buf[256]; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 477 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 478 | safe_strncpy(buf, arg, sizeof(buf)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 479 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 480 | tmp1 = buf; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 481 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 482 | while (tmp1) { |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 483 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 484 | tmp2 = strchr(tmp1, ','); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 485 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 486 | if (tmp2) |
| 487 | *(tmp2++) = '\0'; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 488 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 489 | paft = aftrans; |
| 490 | for (paft = aftrans; paft->alias; paft++) { |
| 491 | if (strcmp(tmp1, paft->alias)) |
| 492 | continue; |
| 493 | if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 494 | bb_error_msg(_("Too many address family arguments.")); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 495 | return (0); |
| 496 | } |
| 497 | if (paft->flag) |
| 498 | (*paft->flag)++; |
| 499 | if (afname[0]) |
| 500 | strcat(afname, ","); |
| 501 | strcat(afname, paft->name); |
| 502 | break; |
| 503 | } |
| 504 | if (!paft->alias) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 505 | bb_error_msg(_("Unknown address family `%s'."), tmp1); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 506 | return (1); |
| 507 | } |
| 508 | tmp1 = tmp2; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 509 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 510 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 511 | return (0); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | /* set the default AF list from the program name or a constant value */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 515 | static void aftrans_def(char *tool, char *argv0, char *dflt) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 516 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 517 | char *tmp; |
| 518 | char *buf; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 519 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 520 | strcpy(afname, dflt); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 521 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 522 | if (!(tmp = strrchr(argv0, '/'))) |
| 523 | tmp = argv0; /* no slash?! */ |
| 524 | else |
| 525 | tmp++; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 526 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 527 | if (!(buf = strdup(tmp))) |
| 528 | return; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 529 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 530 | if (strlen(tool) >= strlen(tmp)) { |
| 531 | free(buf); |
| 532 | return; |
| 533 | } |
| 534 | tmp = buf + (strlen(tmp) - strlen(tool)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 535 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 536 | if (strcmp(tmp, tool) != 0) { |
| 537 | free(buf); |
| 538 | return; |
| 539 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 540 | *tmp = '\0'; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 541 | if ((tmp = strchr(buf, '_'))) |
| 542 | *tmp = '\0'; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 543 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 544 | afname[0] = '\0'; |
| 545 | if (aftrans_opt(buf)) |
| 546 | strcpy(afname, buf); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 547 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 548 | free(buf); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 551 | /* Check our protocol family table for this family. */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 552 | static struct aftype *get_aftype(const char *name) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 553 | { |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 554 | struct aftype * const *afp; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 555 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 556 | #ifdef KEEP_UNUSED |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 557 | if (!sVafinit) |
| 558 | afinit(); |
| 559 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 560 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 561 | afp = aftypes; |
| 562 | while (*afp != NULL) { |
| 563 | if (!strcmp((*afp)->name, name)) |
| 564 | return (*afp); |
| 565 | afp++; |
| 566 | } |
| 567 | if (strchr(name, ',')) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 568 | bb_error_msg(_("Please don't supply more than one address family.")); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 569 | return (NULL); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 570 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 571 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 572 | |
| 573 | /* Check our protocol family table for this family. */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 574 | static struct aftype *get_afntype(int af) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 575 | { |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 576 | struct aftype * const *afp; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 577 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 578 | #ifdef KEEP_UNUSED |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 579 | if (!sVafinit) |
| 580 | afinit(); |
| 581 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 582 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 583 | afp = aftypes; |
| 584 | while (*afp != NULL) { |
| 585 | if ((*afp)->af == af) |
| 586 | return (*afp); |
| 587 | afp++; |
| 588 | } |
| 589 | return (NULL); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | /* Check our protocol family table for this family and return its socket */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 593 | static int get_socket_for_af(int af) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 594 | { |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 595 | struct aftype * const *afp; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 596 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 597 | #ifdef KEEP_UNUSED |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 598 | if (!sVafinit) |
| 599 | afinit(); |
| 600 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 601 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 602 | afp = aftypes; |
| 603 | while (*afp != NULL) { |
| 604 | if ((*afp)->af == af) |
| 605 | return (*afp)->fd; |
| 606 | afp++; |
| 607 | } |
| 608 | return -1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 611 | #ifdef KEEP_UNUSED |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 612 | /* type: 0=all, 1=getroute */ |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 613 | static void print_aflist(int type) |
| 614 | { |
| 615 | int count = 0; |
| 616 | char *txt; |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 617 | struct aftype * const *afp; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 618 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 619 | #ifdef KEEP_UNUSED |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 620 | if (!sVafinit) |
| 621 | afinit(); |
| 622 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 623 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 624 | afp = aftypes; |
| 625 | while (*afp != NULL) { |
| 626 | if ((type == 1 && ((*afp)->rprint == NULL)) || ((*afp)->af == 0)) { |
| 627 | afp++; |
| 628 | continue; |
| 629 | } |
| 630 | if ((count % 3) == 0) |
| 631 | fprintf(stderr, count ? "\n " : " "); |
| 632 | txt = (*afp)->name; |
| 633 | if (!txt) |
| 634 | txt = ".."; |
| 635 | fprintf(stderr, "%s (%s) ", txt, _((*afp)->title)); |
| 636 | count++; |
| 637 | afp++; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 638 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 639 | fprintf(stderr, "\n"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 640 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 641 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 642 | |
| 643 | struct user_net_device_stats { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 644 | unsigned long long rx_packets; /* total packets received */ |
| 645 | unsigned long long tx_packets; /* total packets transmitted */ |
| 646 | unsigned long long rx_bytes; /* total bytes received */ |
| 647 | unsigned long long tx_bytes; /* total bytes transmitted */ |
| 648 | unsigned long rx_errors; /* bad packets received */ |
| 649 | unsigned long tx_errors; /* packet transmit problems */ |
| 650 | unsigned long rx_dropped; /* no space in linux buffers */ |
| 651 | unsigned long tx_dropped; /* no space available in linux */ |
| 652 | unsigned long rx_multicast; /* multicast packets received */ |
| 653 | unsigned long rx_compressed; |
| 654 | unsigned long tx_compressed; |
| 655 | unsigned long collisions; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 656 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 657 | /* detailed rx_errors: */ |
| 658 | unsigned long rx_length_errors; |
| 659 | unsigned long rx_over_errors; /* receiver ring buff overflow */ |
| 660 | unsigned long rx_crc_errors; /* recved pkt with crc error */ |
| 661 | unsigned long rx_frame_errors; /* recv'd frame alignment error */ |
| 662 | unsigned long rx_fifo_errors; /* recv'r fifo overrun */ |
| 663 | unsigned long rx_missed_errors; /* receiver missed packet */ |
| 664 | /* detailed tx_errors */ |
| 665 | unsigned long tx_aborted_errors; |
| 666 | unsigned long tx_carrier_errors; |
| 667 | unsigned long tx_fifo_errors; |
| 668 | unsigned long tx_heartbeat_errors; |
| 669 | unsigned long tx_window_errors; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 670 | }; |
| 671 | |
| 672 | struct interface { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 673 | struct interface *next, *prev; |
| 674 | char name[IFNAMSIZ]; /* interface name */ |
| 675 | short type; /* if type */ |
| 676 | short flags; /* various flags */ |
| 677 | int metric; /* routing metric */ |
| 678 | int mtu; /* MTU value */ |
| 679 | int tx_queue_len; /* transmit queue length */ |
| 680 | struct ifmap map; /* hardware setup */ |
| 681 | struct sockaddr addr; /* IP address */ |
| 682 | struct sockaddr dstaddr; /* P-P IP address */ |
| 683 | struct sockaddr broadaddr; /* IP broadcast address */ |
| 684 | struct sockaddr netmask; /* IP network mask */ |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 685 | int has_ip; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 686 | char hwaddr[32]; /* HW address */ |
| 687 | int statistics_valid; |
| 688 | struct user_net_device_stats stats; /* statistics */ |
| 689 | int keepalive; /* keepalive value for SLIP */ |
| 690 | int outfill; /* outfill value for SLIP */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 691 | }; |
| 692 | |
| 693 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 694 | int interface_opt_a = 0; /* show all interfaces */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 695 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 696 | #ifdef KEEP_UNUSED |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 697 | static int opt_i = 0; /* show the statistics */ |
| 698 | static int opt_v = 0; /* debugging output flag */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 699 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 700 | static int addr_family = 0; /* currently selected AF */ |
| 701 | #endif /* KEEP_UNUSED */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 702 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 703 | static struct interface *int_list, *int_last; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 704 | static int skfd = -1; /* generic raw socket desc. */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 705 | |
| 706 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 707 | static int sockets_open(int family) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 708 | { |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 709 | struct aftype * const *aft; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 710 | int sfd = -1; |
| 711 | static int force = -1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 712 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 713 | if (force < 0) { |
| 714 | force = 0; |
Bernhard Reutner-Fischer | e2e56c7 | 2006-05-19 11:54:02 +0000 | [diff] [blame^] | 715 | if (get_linux_version_code() < KERNEL_VERSION(2,1,0)) |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 716 | force = 1; |
| 717 | if (access("/proc/net", R_OK)) |
| 718 | force = 1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 719 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 720 | for (aft = aftypes; *aft; aft++) { |
| 721 | struct aftype *af = *aft; |
| 722 | int type = SOCK_DGRAM; |
| 723 | |
| 724 | if (af->af == AF_UNSPEC) |
| 725 | continue; |
| 726 | if (family && family != af->af) |
| 727 | continue; |
| 728 | if (af->fd != -1) { |
| 729 | sfd = af->fd; |
| 730 | continue; |
| 731 | } |
| 732 | /* Check some /proc file first to not stress kmod */ |
| 733 | if (!family && !force && af->flag_file) { |
| 734 | if (access(af->flag_file, R_OK)) |
| 735 | continue; |
| 736 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 737 | af->fd = socket(af->af, type, 0); |
| 738 | if (af->fd >= 0) |
| 739 | sfd = af->fd; |
| 740 | } |
Glenn L McGrath | 642f289 | 2002-11-28 10:20:45 +0000 | [diff] [blame] | 741 | if (sfd < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 742 | bb_error_msg(_("No usable address families found.")); |
Glenn L McGrath | 642f289 | 2002-11-28 10:20:45 +0000 | [diff] [blame] | 743 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 744 | return sfd; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Rob Landley | 9398304 | 2005-05-03 21:30:26 +0000 | [diff] [blame] | 747 | #ifdef CONFIG_FEATURE_CLEAN_UP |
| 748 | static void sockets_close(void) |
| 749 | { |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 750 | struct aftype * const *aft; |
Rob Landley | 9398304 | 2005-05-03 21:30:26 +0000 | [diff] [blame] | 751 | for (aft = aftypes; *aft != NULL; aft++) { |
| 752 | struct aftype *af = *aft; |
| 753 | if( af->fd != -1 ) { |
| 754 | close(af->fd); |
| 755 | af->fd = -1; |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | #endif |
| 760 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 761 | /* like strcmp(), but knows about numbers */ |
Glenn L McGrath | 2e99d43 | 2004-07-23 01:49:46 +0000 | [diff] [blame] | 762 | static int nstrcmp(const char *a, const char *b) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 763 | { |
Glenn L McGrath | 2e99d43 | 2004-07-23 01:49:46 +0000 | [diff] [blame] | 764 | const char *a_ptr = a; |
| 765 | const char *b_ptr = b; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 766 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 767 | while (*a == *b) { |
Glenn L McGrath | 2e99d43 | 2004-07-23 01:49:46 +0000 | [diff] [blame] | 768 | if (*a == '\0') { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 769 | return 0; |
Glenn L McGrath | 2e99d43 | 2004-07-23 01:49:46 +0000 | [diff] [blame] | 770 | } |
| 771 | if (!isdigit(*a) && isdigit(*(a+1))) { |
| 772 | a_ptr = a+1; |
| 773 | b_ptr = b+1; |
| 774 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 775 | a++; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 776 | b++; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 777 | } |
Glenn L McGrath | 2e99d43 | 2004-07-23 01:49:46 +0000 | [diff] [blame] | 778 | |
| 779 | if (isdigit(*a) && isdigit(*b)) { |
| 780 | return atoi(a_ptr) > atoi(b_ptr) ? 1 : -1; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 781 | } |
| 782 | return *a - *b; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | static struct interface *add_interface(char *name) |
| 786 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 787 | struct interface *ife, **nextp, *new; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 788 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 789 | for (ife = int_last; ife; ife = ife->prev) { |
| 790 | int n = nstrcmp(ife->name, name); |
| 791 | |
| 792 | if (n == 0) |
| 793 | return ife; |
| 794 | if (n < 0) |
| 795 | break; |
| 796 | } |
| 797 | new(new); |
| 798 | safe_strncpy(new->name, name, IFNAMSIZ); |
| 799 | nextp = ife ? &ife->next : &int_list; |
| 800 | new->prev = ife; |
| 801 | new->next = *nextp; |
| 802 | if (new->next) |
| 803 | new->next->prev = new; |
| 804 | else |
| 805 | int_last = new; |
| 806 | *nextp = new; |
| 807 | return new; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | |
| 811 | static int if_readconf(void) |
| 812 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 813 | int numreqs = 30; |
| 814 | struct ifconf ifc; |
| 815 | struct ifreq *ifr; |
| 816 | int n, err = -1; |
| 817 | int skfd2; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 818 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 819 | /* SIOCGIFCONF currently seems to only work properly on AF_INET sockets |
| 820 | (as of 2.1.128) */ |
| 821 | skfd2 = get_socket_for_af(AF_INET); |
| 822 | if (skfd2 < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 823 | bb_perror_msg(("warning: no inet socket available")); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 824 | /* Try to soldier on with whatever socket we can get hold of. */ |
| 825 | skfd2 = sockets_open(0); |
| 826 | if (skfd2 < 0) |
| 827 | return -1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 828 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 829 | |
| 830 | ifc.ifc_buf = NULL; |
| 831 | for (;;) { |
| 832 | ifc.ifc_len = sizeof(struct ifreq) * numreqs; |
| 833 | ifc.ifc_buf = xrealloc(ifc.ifc_buf, ifc.ifc_len); |
| 834 | |
| 835 | if (ioctl(skfd2, SIOCGIFCONF, &ifc) < 0) { |
| 836 | perror("SIOCGIFCONF"); |
| 837 | goto out; |
| 838 | } |
| 839 | if (ifc.ifc_len == sizeof(struct ifreq) * numreqs) { |
| 840 | /* assume it overflowed and try again */ |
| 841 | numreqs += 10; |
| 842 | continue; |
| 843 | } |
| 844 | break; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 845 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 846 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 847 | ifr = ifc.ifc_req; |
| 848 | for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) { |
| 849 | add_interface(ifr->ifr_name); |
| 850 | ifr++; |
| 851 | } |
| 852 | err = 0; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 853 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 854 | out: |
| 855 | free(ifc.ifc_buf); |
| 856 | return err; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 859 | static char *get_name(char *name, char *p) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 860 | { |
Eric Andersen | 9940e08 | 2004-08-12 16:52:00 +0000 | [diff] [blame] | 861 | /* Extract <name>[:<alias>] from nul-terminated p where p matches |
| 862 | <name>[:<alias>]: after leading whitespace. |
| 863 | If match is not made, set name empty and return unchanged p */ |
| 864 | int namestart=0, nameend=0, aliasend; |
| 865 | while (isspace(p[namestart])) |
| 866 | namestart++; |
| 867 | nameend=namestart; |
| 868 | while (p[nameend] && p[nameend]!=':' && !isspace(p[nameend])) |
| 869 | nameend++; |
| 870 | if (p[nameend]==':') { |
| 871 | aliasend=nameend+1; |
| 872 | while (p[aliasend] && isdigit(p[aliasend])) |
| 873 | aliasend++; |
| 874 | if (p[aliasend]==':') { |
| 875 | nameend=aliasend; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 876 | } |
Eric Andersen | 9940e08 | 2004-08-12 16:52:00 +0000 | [diff] [blame] | 877 | if ((nameend-namestart)<IFNAMSIZ) { |
| 878 | memcpy(name,&p[namestart],nameend-namestart); |
| 879 | name[nameend-namestart]='\0'; |
| 880 | p=&p[nameend]; |
| 881 | } else { |
| 882 | /* Interface name too large */ |
| 883 | name[0]='\0'; |
| 884 | } |
| 885 | } else { |
| 886 | /* first ':' not found - return empty */ |
| 887 | name[0]='\0'; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 888 | } |
Eric Andersen | 6fea732 | 2004-08-26 21:45:21 +0000 | [diff] [blame] | 889 | return p + 1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 892 | /* If scanf supports size qualifiers for %n conversions, then we can |
| 893 | * use a modified fmt that simply stores the position in the fields |
| 894 | * having no associated fields in the proc string. Of course, we need |
| 895 | * to zero them again when we're done. But that is smaller than the |
| 896 | * old approach of multiple scanf occurrences with large numbers of |
| 897 | * args. */ |
| 898 | |
Bernhard Reutner-Fischer | a901b40 | 2006-01-19 14:51:17 +0000 | [diff] [blame] | 899 | /* static const char * const ss_fmt[] = { */ |
Bernhard Reutner-Fischer | d409c3a | 2006-03-29 22:34:47 +0000 | [diff] [blame] | 900 | /* "%lln%llu%lu%lu%lu%lu%ln%ln%lln%llu%lu%lu%lu%lu%lu", */ |
| 901 | /* "%llu%llu%lu%lu%lu%lu%ln%ln%llu%llu%lu%lu%lu%lu%lu", */ |
| 902 | /* "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu" */ |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 903 | /* }; */ |
| 904 | |
| 905 | /* Lie about the size of the int pointed to for %n. */ |
| 906 | #if INT_MAX == LONG_MAX |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 907 | static const char * const ss_fmt[] = { |
Bernhard Reutner-Fischer | d409c3a | 2006-03-29 22:34:47 +0000 | [diff] [blame] | 908 | "%n%llu%u%u%u%u%n%n%n%llu%u%u%u%u%u", |
| 909 | "%llu%llu%u%u%u%u%n%n%llu%llu%u%u%u%u%u", |
| 910 | "%llu%llu%u%u%u%u%u%u%llu%llu%u%u%u%u%u%u" |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 911 | }; |
| 912 | #else |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 913 | static const char * const ss_fmt[] = { |
Bernhard Reutner-Fischer | d409c3a | 2006-03-29 22:34:47 +0000 | [diff] [blame] | 914 | "%n%llu%lu%lu%lu%lu%n%n%n%llu%lu%lu%lu%lu%lu", |
| 915 | "%llu%llu%lu%lu%lu%lu%n%n%llu%llu%lu%lu%lu%lu%lu", |
| 916 | "%llu%llu%lu%lu%lu%lu%lu%lu%llu%llu%lu%lu%lu%lu%lu%lu" |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 917 | }; |
| 918 | |
| 919 | #endif |
| 920 | |
| 921 | static void get_dev_fields(char *bp, struct interface *ife, int procnetdev_vsn) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 922 | { |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 923 | memset(&ife->stats, 0, sizeof(struct user_net_device_stats)); |
| 924 | |
| 925 | sscanf(bp, ss_fmt[procnetdev_vsn], |
| 926 | &ife->stats.rx_bytes, /* missing for 0 */ |
| 927 | &ife->stats.rx_packets, |
| 928 | &ife->stats.rx_errors, |
| 929 | &ife->stats.rx_dropped, |
| 930 | &ife->stats.rx_fifo_errors, |
| 931 | &ife->stats.rx_frame_errors, |
| 932 | &ife->stats.rx_compressed, /* missing for <= 1 */ |
| 933 | &ife->stats.rx_multicast, /* missing for <= 1 */ |
| 934 | &ife->stats.tx_bytes, /* missing for 0 */ |
| 935 | &ife->stats.tx_packets, |
| 936 | &ife->stats.tx_errors, |
| 937 | &ife->stats.tx_dropped, |
| 938 | &ife->stats.tx_fifo_errors, |
| 939 | &ife->stats.collisions, |
| 940 | &ife->stats.tx_carrier_errors, |
| 941 | &ife->stats.tx_compressed /* missing for <= 1 */ |
| 942 | ); |
| 943 | |
| 944 | if (procnetdev_vsn <= 1) { |
| 945 | if (procnetdev_vsn == 0) { |
| 946 | ife->stats.rx_bytes = 0; |
| 947 | ife->stats.tx_bytes = 0; |
| 948 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 949 | ife->stats.rx_multicast = 0; |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 950 | ife->stats.rx_compressed = 0; |
| 951 | ife->stats.tx_compressed = 0; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 952 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 955 | static inline int procnetdev_version(char *buf) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 956 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 957 | if (strstr(buf, "compressed")) |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 958 | return 2; |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 959 | if (strstr(buf, "bytes")) |
| 960 | return 1; |
| 961 | return 0; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | static int if_readlist_proc(char *target) |
| 965 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 966 | static int proc_read; |
| 967 | FILE *fh; |
| 968 | char buf[512]; |
| 969 | struct interface *ife; |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 970 | int err, procnetdev_vsn; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 971 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 972 | if (proc_read) |
| 973 | return 0; |
| 974 | if (!target) |
| 975 | proc_read = 1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 976 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 977 | fh = fopen(_PATH_PROCNET_DEV, "r"); |
| 978 | if (!fh) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 979 | bb_perror_msg(_("Warning: cannot open %s. Limited output."), _PATH_PROCNET_DEV); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 980 | return if_readconf(); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 981 | } |
| 982 | fgets(buf, sizeof buf, fh); /* eat line */ |
| 983 | fgets(buf, sizeof buf, fh); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 984 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 985 | procnetdev_vsn = procnetdev_version(buf); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 986 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 987 | err = 0; |
| 988 | while (fgets(buf, sizeof buf, fh)) { |
Eric Andersen | f96675b | 2003-07-28 06:37:04 +0000 | [diff] [blame] | 989 | char *s, name[128]; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 990 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 991 | s = get_name(name, buf); |
| 992 | ife = add_interface(name); |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 993 | get_dev_fields(s, ife, procnetdev_vsn); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 994 | ife->statistics_valid = 1; |
| 995 | if (target && !strcmp(target, name)) |
| 996 | break; |
| 997 | } |
| 998 | if (ferror(fh)) { |
| 999 | perror(_PATH_PROCNET_DEV); |
| 1000 | err = -1; |
| 1001 | proc_read = 0; |
| 1002 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1003 | fclose(fh); |
| 1004 | return err; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1007 | static int if_readlist(void) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1008 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1009 | int err = if_readlist_proc(NULL); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1010 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1011 | if (!err) |
| 1012 | err = if_readconf(); |
| 1013 | return err; |
| 1014 | } |
| 1015 | |
| 1016 | static int for_all_interfaces(int (*doit) (struct interface *, void *), |
| 1017 | void *cookie) |
| 1018 | { |
| 1019 | struct interface *ife; |
| 1020 | |
| 1021 | if (!int_list && (if_readlist() < 0)) |
| 1022 | return -1; |
| 1023 | for (ife = int_list; ife; ife = ife->next) { |
| 1024 | int err = doit(ife, cookie); |
| 1025 | |
| 1026 | if (err) |
| 1027 | return err; |
| 1028 | } |
| 1029 | return 0; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1032 | /* Fetch the interface configuration from the kernel. */ |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1033 | static int if_fetch(struct interface *ife) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1034 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1035 | struct ifreq ifr; |
| 1036 | int fd; |
| 1037 | char *ifname = ife->name; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1038 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1039 | strcpy(ifr.ifr_name, ifname); |
| 1040 | if (ioctl(skfd, SIOCGIFFLAGS, &ifr) < 0) |
| 1041 | return (-1); |
| 1042 | ife->flags = ifr.ifr_flags; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1043 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1044 | strcpy(ifr.ifr_name, ifname); |
| 1045 | if (ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0) |
| 1046 | memset(ife->hwaddr, 0, 32); |
| 1047 | else |
| 1048 | memcpy(ife->hwaddr, ifr.ifr_hwaddr.sa_data, 8); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1049 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1050 | ife->type = ifr.ifr_hwaddr.sa_family; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1051 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1052 | strcpy(ifr.ifr_name, ifname); |
| 1053 | if (ioctl(skfd, SIOCGIFMETRIC, &ifr) < 0) |
| 1054 | ife->metric = 0; |
| 1055 | else |
| 1056 | ife->metric = ifr.ifr_metric; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1057 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1058 | strcpy(ifr.ifr_name, ifname); |
| 1059 | if (ioctl(skfd, SIOCGIFMTU, &ifr) < 0) |
| 1060 | ife->mtu = 0; |
| 1061 | else |
| 1062 | ife->mtu = ifr.ifr_mtu; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1063 | |
| 1064 | #ifdef HAVE_HWSLIP |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1065 | if (ife->type == ARPHRD_SLIP || ife->type == ARPHRD_CSLIP || |
| 1066 | ife->type == ARPHRD_SLIP6 || ife->type == ARPHRD_CSLIP6 || |
| 1067 | ife->type == ARPHRD_ADAPT) { |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1068 | #ifdef SIOCGOUTFILL |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1069 | strcpy(ifr.ifr_name, ifname); |
| 1070 | if (ioctl(skfd, SIOCGOUTFILL, &ifr) < 0) |
| 1071 | ife->outfill = 0; |
| 1072 | else |
| 1073 | ife->outfill = (unsigned int) ifr.ifr_data; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1074 | #endif |
| 1075 | #ifdef SIOCGKEEPALIVE |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1076 | strcpy(ifr.ifr_name, ifname); |
| 1077 | if (ioctl(skfd, SIOCGKEEPALIVE, &ifr) < 0) |
| 1078 | ife->keepalive = 0; |
| 1079 | else |
| 1080 | ife->keepalive = (unsigned int) ifr.ifr_data; |
| 1081 | #endif |
| 1082 | } |
| 1083 | #endif |
| 1084 | |
Rob Landley | 9398304 | 2005-05-03 21:30:26 +0000 | [diff] [blame] | 1085 | #ifdef SIOCGIFMAP |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1086 | strcpy(ifr.ifr_name, ifname); |
Rob Landley | 9398304 | 2005-05-03 21:30:26 +0000 | [diff] [blame] | 1087 | if (ioctl(skfd, SIOCGIFMAP, &ifr) == 0) |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1088 | ife->map = ifr.ifr_map; |
Rob Landley | 9398304 | 2005-05-03 21:30:26 +0000 | [diff] [blame] | 1089 | else |
| 1090 | #endif |
| 1091 | memset(&ife->map, 0, sizeof(struct ifmap)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1092 | |
| 1093 | #ifdef HAVE_TXQUEUELEN |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1094 | strcpy(ifr.ifr_name, ifname); |
| 1095 | if (ioctl(skfd, SIOCGIFTXQLEN, &ifr) < 0) |
| 1096 | ife->tx_queue_len = -1; /* unknown value */ |
| 1097 | else |
| 1098 | ife->tx_queue_len = ifr.ifr_qlen; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1099 | #else |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1100 | ife->tx_queue_len = -1; /* unknown value */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1101 | #endif |
| 1102 | |
| 1103 | #if HAVE_AFINET |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1104 | /* IPv4 address? */ |
| 1105 | fd = get_socket_for_af(AF_INET); |
| 1106 | if (fd >= 0) { |
| 1107 | strcpy(ifr.ifr_name, ifname); |
| 1108 | ifr.ifr_addr.sa_family = AF_INET; |
| 1109 | if (ioctl(fd, SIOCGIFADDR, &ifr) == 0) { |
| 1110 | ife->has_ip = 1; |
| 1111 | ife->addr = ifr.ifr_addr; |
| 1112 | strcpy(ifr.ifr_name, ifname); |
| 1113 | if (ioctl(fd, SIOCGIFDSTADDR, &ifr) < 0) |
| 1114 | memset(&ife->dstaddr, 0, sizeof(struct sockaddr)); |
| 1115 | else |
| 1116 | ife->dstaddr = ifr.ifr_dstaddr; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1117 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1118 | strcpy(ifr.ifr_name, ifname); |
| 1119 | if (ioctl(fd, SIOCGIFBRDADDR, &ifr) < 0) |
| 1120 | memset(&ife->broadaddr, 0, sizeof(struct sockaddr)); |
| 1121 | else |
| 1122 | ife->broadaddr = ifr.ifr_broadaddr; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1123 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1124 | strcpy(ifr.ifr_name, ifname); |
| 1125 | if (ioctl(fd, SIOCGIFNETMASK, &ifr) < 0) |
| 1126 | memset(&ife->netmask, 0, sizeof(struct sockaddr)); |
| 1127 | else |
| 1128 | ife->netmask = ifr.ifr_netmask; |
| 1129 | } else |
| 1130 | memset(&ife->addr, 0, sizeof(struct sockaddr)); |
| 1131 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1132 | #endif |
| 1133 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1134 | return 0; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
| 1137 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1138 | static int do_if_fetch(struct interface *ife) |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1139 | { |
| 1140 | if (if_fetch(ife) < 0) { |
| 1141 | char *errmsg; |
| 1142 | |
| 1143 | if (errno == ENODEV) { |
| 1144 | /* Give better error message for this case. */ |
| 1145 | errmsg = _("Device not found"); |
| 1146 | } else { |
| 1147 | errmsg = strerror(errno); |
| 1148 | } |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 1149 | bb_error_msg(_("%s: error fetching interface information: %s"), |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1150 | ife->name, errmsg); |
| 1151 | return -1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1152 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1153 | return 0; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | /* This structure defines hardware protocols and their handlers. */ |
| 1157 | struct hwtype { |
Bernhard Reutner-Fischer | a901b40 | 2006-01-19 14:51:17 +0000 | [diff] [blame] | 1158 | const char * const name; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1159 | const char *title; |
| 1160 | int type; |
| 1161 | int alen; |
| 1162 | char *(*print) (unsigned char *); |
| 1163 | int (*input) (char *, struct sockaddr *); |
| 1164 | int (*activate) (int fd); |
| 1165 | int suppress_null_addr; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1166 | }; |
| 1167 | |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 1168 | static const struct hwtype unspec_hwtype = { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1169 | "unspec", "UNSPEC", -1, 0, |
| 1170 | UNSPEC_print, NULL, NULL |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1171 | }; |
| 1172 | |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 1173 | static const struct hwtype loop_hwtype = { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1174 | "loop", "Local Loopback", ARPHRD_LOOPBACK, 0, |
| 1175 | NULL, NULL, NULL |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1176 | }; |
| 1177 | |
| 1178 | #if HAVE_HWETHER |
| 1179 | #include <net/if_arp.h> |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 1180 | |
Mike Frysinger | 3b05b80 | 2005-03-04 01:10:56 +0000 | [diff] [blame] | 1181 | #if (__GLIBC__ >=2 && __GLIBC_MINOR >= 1) || defined(_NEWLIB_VERSION) |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 1182 | #include <net/ethernet.h> |
| 1183 | #else |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1184 | #include <linux/if_ether.h> |
Eric Andersen | ab4e19a | 2003-01-14 08:54:08 +0000 | [diff] [blame] | 1185 | #endif |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1186 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1187 | /* Display an Ethernet address in readable format. */ |
| 1188 | static char *pr_ether(unsigned char *ptr) |
| 1189 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1190 | static char buff[64]; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1191 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1192 | snprintf(buff, sizeof(buff), "%02X:%02X:%02X:%02X:%02X:%02X", |
| 1193 | (ptr[0] & 0377), (ptr[1] & 0377), (ptr[2] & 0377), |
| 1194 | (ptr[3] & 0377), (ptr[4] & 0377), (ptr[5] & 0377) |
| 1195 | ); |
| 1196 | return (buff); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1199 | #ifdef KEEP_UNUSED |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1200 | /* Input an Ethernet address and convert to binary. */ |
| 1201 | static int in_ether(char *bufp, struct sockaddr *sap) |
| 1202 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1203 | unsigned char *ptr; |
| 1204 | char c, *orig; |
| 1205 | int i; |
| 1206 | unsigned val; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1207 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1208 | sap->sa_family = ether_hwtype.type; |
| 1209 | ptr = sap->sa_data; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1210 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1211 | i = 0; |
| 1212 | orig = bufp; |
| 1213 | while ((*bufp != '\0') && (i < ETH_ALEN)) { |
| 1214 | val = 0; |
| 1215 | c = *bufp++; |
| 1216 | if (isdigit(c)) |
| 1217 | val = c - '0'; |
| 1218 | else if (c >= 'a' && c <= 'f') |
| 1219 | val = c - 'a' + 10; |
| 1220 | else if (c >= 'A' && c <= 'F') |
| 1221 | val = c - 'A' + 10; |
| 1222 | else { |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1223 | #ifdef DEBUG |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1224 | bb_error_msg(_("in_ether(%s): invalid ether address!\n"), orig); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1225 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1226 | errno = EINVAL; |
| 1227 | return (-1); |
| 1228 | } |
| 1229 | val <<= 4; |
| 1230 | c = *bufp; |
| 1231 | if (isdigit(c)) |
| 1232 | val |= c - '0'; |
| 1233 | else if (c >= 'a' && c <= 'f') |
| 1234 | val |= c - 'a' + 10; |
| 1235 | else if (c >= 'A' && c <= 'F') |
| 1236 | val |= c - 'A' + 10; |
| 1237 | else if (c == ':' || c == 0) |
| 1238 | val >>= 4; |
| 1239 | else { |
| 1240 | #ifdef DEBUG |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1241 | bb_error_msg(_("in_ether(%s): invalid ether address!"), orig); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1242 | #endif |
| 1243 | errno = EINVAL; |
| 1244 | return (-1); |
| 1245 | } |
| 1246 | if (c != 0) |
| 1247 | bufp++; |
| 1248 | *ptr++ = (unsigned char) (val & 0377); |
| 1249 | i++; |
| 1250 | |
| 1251 | /* We might get a semicolon here - not required. */ |
| 1252 | if (*bufp == ':') { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1253 | #ifdef DEBUG |
Glenn L McGrath | 642f289 | 2002-11-28 10:20:45 +0000 | [diff] [blame] | 1254 | if (i == ETH_ALEN) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1255 | bb_error_msg(_("in_ether(%s): trailing : ignored!"), orig); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1256 | } |
Glenn L McGrath | 642f289 | 2002-11-28 10:20:45 +0000 | [diff] [blame] | 1257 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1258 | bufp++; |
| 1259 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1260 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1261 | |
Glenn L McGrath | 642f289 | 2002-11-28 10:20:45 +0000 | [diff] [blame] | 1262 | #ifdef DEBUG |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1263 | /* That's it. Any trailing junk? */ |
| 1264 | if ((i == ETH_ALEN) && (*bufp != '\0')) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1265 | bb_error_msg(_("in_ether(%s): trailing junk!"), orig); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1266 | errno = EINVAL; |
| 1267 | return (-1); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1268 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1269 | bb_error_msg("in_ether(%s): %s", orig, pr_ether(sap->sa_data)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1270 | #endif |
| 1271 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1272 | return (0); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1273 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1274 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1275 | |
| 1276 | |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 1277 | static const struct hwtype ether_hwtype = { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1278 | "ether", "Ethernet", ARPHRD_ETHER, ETH_ALEN, |
| 1279 | pr_ether, NULL /* UNUSED in_ether */ , NULL |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1280 | }; |
| 1281 | |
| 1282 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1283 | #endif /* HAVE_HWETHER */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1284 | |
| 1285 | |
| 1286 | #if HAVE_HWPPP |
| 1287 | |
| 1288 | #include <net/if_arp.h> |
| 1289 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1290 | #ifdef KEEP_UNUSED |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1291 | /* Start the PPP encapsulation on the file descriptor. */ |
| 1292 | static int do_ppp(int fd) |
| 1293 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1294 | bb_error_msg(_("You cannot start PPP with this program.")); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1295 | return -1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1296 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1297 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1298 | |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 1299 | static const struct hwtype ppp_hwtype = { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1300 | "ppp", "Point-Point Protocol", ARPHRD_PPP, 0, |
| 1301 | NULL, NULL, NULL /* UNUSED do_ppp */ , 0 |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1302 | }; |
| 1303 | |
| 1304 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1305 | #endif /* HAVE_PPP */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1306 | |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 1307 | static const struct hwtype * const hwtypes[] = { |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1308 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1309 | &loop_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1310 | |
| 1311 | #if HAVE_HWSLIP |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1312 | &slip_hwtype, |
| 1313 | &cslip_hwtype, |
| 1314 | &slip6_hwtype, |
| 1315 | &cslip6_hwtype, |
| 1316 | &adaptive_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1317 | #endif |
| 1318 | #if HAVE_HWSTRIP |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1319 | &strip_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1320 | #endif |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1321 | #if HAVE_HWETHER |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1322 | ðer_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1323 | #endif |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1324 | #if HAVE_HWTUNNEL |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1325 | &tunnel_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1326 | #endif |
| 1327 | #if HAVE_HWPPP |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1328 | &ppp_hwtype, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1329 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1330 | &unspec_hwtype, |
| 1331 | NULL |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1332 | }; |
| 1333 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1334 | #ifdef KEEP_UNUSED |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1335 | static short sVhwinit = 0; |
| 1336 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1337 | static void hwinit() |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1338 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1339 | loop_hwtype.title = _("Local Loopback"); |
| 1340 | unspec_hwtype.title = _("UNSPEC"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1341 | #if HAVE_HWSLIP |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1342 | slip_hwtype.title = _("Serial Line IP"); |
| 1343 | cslip_hwtype.title = _("VJ Serial Line IP"); |
| 1344 | slip6_hwtype.title = _("6-bit Serial Line IP"); |
| 1345 | cslip6_hwtype.title = _("VJ 6-bit Serial Line IP"); |
| 1346 | adaptive_hwtype.title = _("Adaptive Serial Line IP"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1347 | #endif |
| 1348 | #if HAVE_HWETHER |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1349 | ether_hwtype.title = _("Ethernet"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1350 | #endif |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1351 | #if HAVE_HWTUNNEL |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1352 | tunnel_hwtype.title = _("IPIP Tunnel"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1353 | #endif |
| 1354 | #if HAVE_HWPPP |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1355 | ppp_hwtype.title = _("Point-to-Point Protocol"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1356 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1357 | sVhwinit = 1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1358 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1359 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1360 | |
| 1361 | #ifdef IFF_PORTSEL |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 1362 | #if 0 |
| 1363 | static const char * const if_port_text[][4] = { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1364 | /* Keep in step with <linux/netdevice.h> */ |
| 1365 | {"unknown", NULL, NULL, NULL}, |
| 1366 | {"10base2", "bnc", "coax", NULL}, |
| 1367 | {"10baseT", "utp", "tpe", NULL}, |
| 1368 | {"AUI", "thick", "db15", NULL}, |
| 1369 | {"100baseT", NULL, NULL, NULL}, |
| 1370 | {"100baseTX", NULL, NULL, NULL}, |
| 1371 | {"100baseFX", NULL, NULL, NULL}, |
| 1372 | {NULL, NULL, NULL, NULL}, |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1373 | }; |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 1374 | #else |
| 1375 | static const char * const if_port_text[] = { |
| 1376 | /* Keep in step with <linux/netdevice.h> */ |
| 1377 | "unknown", |
| 1378 | "10base2", |
| 1379 | "10baseT", |
| 1380 | "AUI", |
| 1381 | "100baseT", |
| 1382 | "100baseTX", |
| 1383 | "100baseFX", |
| 1384 | NULL |
| 1385 | }; |
| 1386 | #endif |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1387 | #endif |
| 1388 | |
| 1389 | /* Check our hardware type table for this type. */ |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 1390 | static const struct hwtype *get_hwntype(int type) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1391 | { |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 1392 | const struct hwtype * const *hwp; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1393 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1394 | #ifdef KEEP_UNUSED |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1395 | if (!sVhwinit) |
| 1396 | hwinit(); |
| 1397 | #endif /* KEEP_UNUSED */ |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1398 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1399 | hwp = hwtypes; |
| 1400 | while (*hwp != NULL) { |
| 1401 | if ((*hwp)->type == type) |
| 1402 | return (*hwp); |
| 1403 | hwp++; |
| 1404 | } |
| 1405 | return (NULL); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | /* return 1 if address is all zeros */ |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 1409 | static int hw_null_address(const struct hwtype *hw, void *ap) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1410 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1411 | unsigned int i; |
| 1412 | unsigned char *address = (unsigned char *) ap; |
| 1413 | |
| 1414 | for (i = 0; i < hw->alen; i++) |
| 1415 | if (address[i]) |
| 1416 | return 0; |
| 1417 | return 1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 1420 | static const char TRext[] = "\0\0\0Ki\0Mi\0Gi\0Ti"; |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1421 | |
| 1422 | static void print_bytes_scaled(unsigned long long ull, const char *end) |
| 1423 | { |
| 1424 | unsigned long long int_part; |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1425 | const char *ext; |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 1426 | unsigned int frac_part; |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1427 | int i; |
| 1428 | |
| 1429 | frac_part = 0; |
| 1430 | ext = TRext; |
| 1431 | int_part = ull; |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 1432 | i = 4; |
| 1433 | do { |
| 1434 | #if 0 |
| 1435 | /* This does correct rounding and is a little larger. But it |
| 1436 | * uses KiB as the smallest displayed unit. */ |
| 1437 | if ((int_part < (1024*1024 - 51)) || !--i) { |
| 1438 | i = 0; |
| 1439 | int_part += 51; /* 1024*.05 = 51.2 */ |
| 1440 | frac_part = ((((unsigned int) int_part) & (1024-1)) * 10) / 1024; |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1441 | } |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 1442 | int_part /= 1024; |
| 1443 | ext += 3; /* KiB, MiB, GiB, TiB */ |
| 1444 | #else |
| 1445 | if (int_part >= 1024) { |
| 1446 | frac_part = ((((unsigned int) int_part) & (1024-1)) * 10) / 1024; |
| 1447 | int_part /= 1024; |
| 1448 | ext += 3; /* KiB, MiB, GiB, TiB */ |
| 1449 | } |
| 1450 | --i; |
| 1451 | #endif |
| 1452 | } while (i); |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1453 | |
Bernhard Reutner-Fischer | d409c3a | 2006-03-29 22:34:47 +0000 | [diff] [blame] | 1454 | printf("X bytes:%llu (%llu.%u %sB)%s", ull, int_part, frac_part, ext, end); |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 1457 | static const char * const ife_print_flags_strs[] = { |
| 1458 | "UP ", |
| 1459 | "BROADCAST ", |
| 1460 | "DEBUG ", |
| 1461 | "LOOPBACK ", |
| 1462 | "POINTOPOINT ", |
| 1463 | "NOTRAILERS ", |
| 1464 | "RUNNING ", |
| 1465 | "NOARP ", |
| 1466 | "PROMISC ", |
| 1467 | "ALLMULTI ", |
| 1468 | "SLAVE ", |
| 1469 | "MASTER ", |
| 1470 | "MULTICAST ", |
| 1471 | #ifdef HAVE_DYNAMIC |
| 1472 | "DYNAMIC " |
| 1473 | #endif |
| 1474 | }; |
| 1475 | |
| 1476 | static const unsigned short ife_print_flags_mask[] = { |
| 1477 | IFF_UP, |
| 1478 | IFF_BROADCAST, |
| 1479 | IFF_DEBUG, |
| 1480 | IFF_LOOPBACK, |
| 1481 | IFF_POINTOPOINT, |
| 1482 | IFF_NOTRAILERS, |
| 1483 | IFF_RUNNING, |
| 1484 | IFF_NOARP, |
| 1485 | IFF_PROMISC, |
| 1486 | IFF_ALLMULTI, |
| 1487 | IFF_SLAVE, |
| 1488 | IFF_MASTER, |
| 1489 | IFF_MULTICAST, |
| 1490 | #ifdef HAVE_DYNAMIC |
| 1491 | IFF_DYNAMIC |
| 1492 | #endif |
| 1493 | 0 |
| 1494 | }; |
| 1495 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1496 | static void ife_print(struct interface *ptr) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1497 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1498 | struct aftype *ap; |
"Vladimir N. Oleynik" | 0fa9ded | 2005-10-20 09:48:28 +0000 | [diff] [blame] | 1499 | const struct hwtype *hw; |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1500 | int hf; |
| 1501 | int can_compress = 0; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1502 | |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1503 | #if HAVE_AFINET6 |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1504 | FILE *f; |
| 1505 | char addr6[40], devname[20]; |
| 1506 | struct sockaddr_in6 sap; |
| 1507 | int plen, scope, dad_status, if_idx; |
| 1508 | char addr6p[8][5]; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1509 | #endif |
| 1510 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1511 | ap = get_afntype(ptr->addr.sa_family); |
| 1512 | if (ap == NULL) |
| 1513 | ap = get_afntype(0); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1514 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1515 | hf = ptr->type; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1516 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1517 | if (hf == ARPHRD_CSLIP || hf == ARPHRD_CSLIP6) |
| 1518 | can_compress = 1; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1519 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1520 | hw = get_hwntype(hf); |
| 1521 | if (hw == NULL) |
| 1522 | hw = get_hwntype(-1); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1523 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1524 | printf(_("%-9.9s Link encap:%s "), ptr->name, _(hw->title)); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1525 | /* For some hardware types (eg Ash, ATM) we don't print the |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1526 | hardware address if it's null. */ |
| 1527 | if (hw->print != NULL && (!(hw_null_address(hw, ptr->hwaddr) && |
| 1528 | hw->suppress_null_addr))) |
Eric Andersen | 0cb6f35 | 2006-01-30 22:30:41 +0000 | [diff] [blame] | 1529 | printf(_("HWaddr %s "), hw->print((unsigned char *)ptr->hwaddr)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1530 | #ifdef IFF_PORTSEL |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1531 | if (ptr->flags & IFF_PORTSEL) { |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 1532 | printf(_("Media:%s"), if_port_text[ptr->map.port] /* [0] */); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1533 | if (ptr->flags & IFF_AUTOMEDIA) |
| 1534 | printf(_("(auto)")); |
| 1535 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1536 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1537 | printf("\n"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1538 | |
| 1539 | #if HAVE_AFINET |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1540 | if (ptr->has_ip) { |
| 1541 | printf(_(" %s addr:%s "), ap->name, |
| 1542 | ap->sprint(&ptr->addr, 1)); |
| 1543 | if (ptr->flags & IFF_POINTOPOINT) { |
| 1544 | printf(_(" P-t-P:%s "), ap->sprint(&ptr->dstaddr, 1)); |
| 1545 | } |
| 1546 | if (ptr->flags & IFF_BROADCAST) { |
| 1547 | printf(_(" Bcast:%s "), ap->sprint(&ptr->broadaddr, 1)); |
| 1548 | } |
| 1549 | printf(_(" Mask:%s\n"), ap->sprint(&ptr->netmask, 1)); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1550 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1551 | #endif |
| 1552 | |
| 1553 | #if HAVE_AFINET6 |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1554 | |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 1555 | #define IPV6_ADDR_ANY 0x0000U |
| 1556 | |
| 1557 | #define IPV6_ADDR_UNICAST 0x0001U |
| 1558 | #define IPV6_ADDR_MULTICAST 0x0002U |
| 1559 | #define IPV6_ADDR_ANYCAST 0x0004U |
| 1560 | |
| 1561 | #define IPV6_ADDR_LOOPBACK 0x0010U |
| 1562 | #define IPV6_ADDR_LINKLOCAL 0x0020U |
| 1563 | #define IPV6_ADDR_SITELOCAL 0x0040U |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1564 | |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 1565 | #define IPV6_ADDR_COMPATv4 0x0080U |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1566 | |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 1567 | #define IPV6_ADDR_SCOPE_MASK 0x00f0U |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1568 | |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 1569 | #define IPV6_ADDR_MAPPED 0x1000U |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1570 | #define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */ |
| 1571 | |
| 1572 | if ((f = fopen(_PATH_PROCNET_IFINET6, "r")) != NULL) { |
| 1573 | while (fscanf |
| 1574 | (f, "%4s%4s%4s%4s%4s%4s%4s%4s %02x %02x %02x %02x %20s\n", |
| 1575 | addr6p[0], addr6p[1], addr6p[2], addr6p[3], addr6p[4], |
| 1576 | addr6p[5], addr6p[6], addr6p[7], &if_idx, &plen, &scope, |
| 1577 | &dad_status, devname) != EOF) { |
| 1578 | if (!strcmp(devname, ptr->name)) { |
| 1579 | sprintf(addr6, "%s:%s:%s:%s:%s:%s:%s:%s", |
| 1580 | addr6p[0], addr6p[1], addr6p[2], addr6p[3], |
| 1581 | addr6p[4], addr6p[5], addr6p[6], addr6p[7]); |
| 1582 | inet_pton(AF_INET6, addr6, |
| 1583 | (struct sockaddr *) &sap.sin6_addr); |
| 1584 | sap.sin6_family = AF_INET6; |
| 1585 | printf(_(" inet6 addr: %s/%d"), |
| 1586 | inet6_aftype.sprint((struct sockaddr *) &sap, 1), |
| 1587 | plen); |
| 1588 | printf(_(" Scope:")); |
| 1589 | switch (scope & IPV6_ADDR_SCOPE_MASK) { |
| 1590 | case 0: |
| 1591 | printf(_("Global")); |
| 1592 | break; |
| 1593 | case IPV6_ADDR_LINKLOCAL: |
| 1594 | printf(_("Link")); |
| 1595 | break; |
| 1596 | case IPV6_ADDR_SITELOCAL: |
| 1597 | printf(_("Site")); |
| 1598 | break; |
| 1599 | case IPV6_ADDR_COMPATv4: |
| 1600 | printf(_("Compat")); |
| 1601 | break; |
| 1602 | case IPV6_ADDR_LOOPBACK: |
| 1603 | printf(_("Host")); |
| 1604 | break; |
| 1605 | default: |
| 1606 | printf(_("Unknown")); |
| 1607 | } |
| 1608 | printf("\n"); |
| 1609 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1610 | } |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1611 | fclose(f); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1612 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1613 | #endif |
| 1614 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1615 | printf(" "); |
| 1616 | /* DONT FORGET TO ADD THE FLAGS IN ife_print_short, too */ |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 1617 | |
| 1618 | if (ptr->flags == 0) { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1619 | printf(_("[NO FLAGS] ")); |
Manuel Novoa III | b1d5b74 | 2003-08-02 00:04:18 +0000 | [diff] [blame] | 1620 | } else { |
| 1621 | int i = 0; |
| 1622 | do { |
| 1623 | if (ptr->flags & ife_print_flags_mask[i]) { |
| 1624 | printf(_(ife_print_flags_strs[i])); |
| 1625 | } |
| 1626 | } while (ife_print_flags_mask[++i]); |
| 1627 | } |
| 1628 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1629 | /* DONT FORGET TO ADD THE FLAGS IN ife_print_short */ |
| 1630 | printf(_(" MTU:%d Metric:%d"), ptr->mtu, ptr->metric ? ptr->metric : 1); |
| 1631 | #ifdef SIOCSKEEPALIVE |
| 1632 | if (ptr->outfill || ptr->keepalive) |
| 1633 | printf(_(" Outfill:%d Keepalive:%d"), ptr->outfill, ptr->keepalive); |
| 1634 | #endif |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1635 | printf("\n"); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1636 | |
| 1637 | /* If needed, display the interface statistics. */ |
| 1638 | |
| 1639 | if (ptr->statistics_valid) { |
| 1640 | /* XXX: statistics are currently only printed for the primary address, |
| 1641 | * not for the aliases, although strictly speaking they're shared |
| 1642 | * by all addresses. |
| 1643 | */ |
| 1644 | printf(" "); |
| 1645 | |
Bernhard Reutner-Fischer | d409c3a | 2006-03-29 22:34:47 +0000 | [diff] [blame] | 1646 | printf("RX packets:%llu errors:%lu dropped:%lu overruns:%lu frame:%lu\n", |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1647 | ptr->stats.rx_packets, ptr->stats.rx_errors, |
| 1648 | ptr->stats.rx_dropped, ptr->stats.rx_fifo_errors, |
| 1649 | ptr->stats.rx_frame_errors); |
| 1650 | if (can_compress) |
Bernhard Reutner-Fischer | d409c3a | 2006-03-29 22:34:47 +0000 | [diff] [blame] | 1651 | printf(" compressed:%lu\n", |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1652 | ptr->stats.rx_compressed); |
| 1653 | printf(" "); |
Bernhard Reutner-Fischer | d409c3a | 2006-03-29 22:34:47 +0000 | [diff] [blame] | 1654 | printf("TX packets:%llu errors:%lu dropped:%lu overruns:%lu carrier:%lu\n", |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1655 | ptr->stats.tx_packets, ptr->stats.tx_errors, |
| 1656 | ptr->stats.tx_dropped, ptr->stats.tx_fifo_errors, |
| 1657 | ptr->stats.tx_carrier_errors); |
Bernhard Reutner-Fischer | 214744d | 2006-03-30 13:38:19 +0000 | [diff] [blame] | 1658 | printf(" collisions:%lu ", ptr->stats.collisions); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1659 | if (can_compress) |
Bernhard Reutner-Fischer | d409c3a | 2006-03-29 22:34:47 +0000 | [diff] [blame] | 1660 | printf("compressed:%lu ", ptr->stats.tx_compressed); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1661 | if (ptr->tx_queue_len != -1) |
Bernhard Reutner-Fischer | d409c3a | 2006-03-29 22:34:47 +0000 | [diff] [blame] | 1662 | printf("txqueuelen:%d ", ptr->tx_queue_len); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1663 | printf("\n R"); |
| 1664 | print_bytes_scaled(ptr->stats.rx_bytes, " T"); |
| 1665 | print_bytes_scaled(ptr->stats.tx_bytes, "\n"); |
| 1666 | |
| 1667 | } |
| 1668 | |
| 1669 | if ((ptr->map.irq || ptr->map.mem_start || ptr->map.dma || |
| 1670 | ptr->map.base_addr)) { |
| 1671 | printf(" "); |
| 1672 | if (ptr->map.irq) |
| 1673 | printf(_("Interrupt:%d "), ptr->map.irq); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1674 | if (ptr->map.base_addr >= 0x100) /* Only print devices using it for |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1675 | I/O maps */ |
| 1676 | printf(_("Base address:0x%lx "), |
| 1677 | (unsigned long) ptr->map.base_addr); |
| 1678 | if (ptr->map.mem_start) { |
| 1679 | printf(_("Memory:%lx-%lx "), ptr->map.mem_start, |
| 1680 | ptr->map.mem_end); |
| 1681 | } |
| 1682 | if (ptr->map.dma) |
| 1683 | printf(_("DMA chan:%x "), ptr->map.dma); |
| 1684 | printf("\n"); |
| 1685 | } |
| 1686 | printf("\n"); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1690 | static int do_if_print(struct interface *ife, void *cookie) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1691 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1692 | int *opt_a = (int *) cookie; |
| 1693 | int res; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1694 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1695 | res = do_if_fetch(ife); |
| 1696 | if (res >= 0) { |
| 1697 | if ((ife->flags & IFF_UP) || *opt_a) |
| 1698 | ife_print(ife); |
| 1699 | } |
| 1700 | return res; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1703 | static struct interface *lookup_interface(char *name) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1704 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1705 | struct interface *ife = NULL; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1706 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1707 | if (if_readlist_proc(name) < 0) |
| 1708 | return NULL; |
| 1709 | ife = add_interface(name); |
| 1710 | return ife; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | /* for ipv4 add/del modes */ |
| 1714 | static int if_print(char *ifname) |
| 1715 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1716 | int res; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1717 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1718 | if (!ifname) { |
| 1719 | res = for_all_interfaces(do_if_print, &interface_opt_a); |
| 1720 | } else { |
| 1721 | struct interface *ife; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1722 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1723 | ife = lookup_interface(ifname); |
| 1724 | res = do_if_fetch(ife); |
| 1725 | if (res >= 0) |
| 1726 | ife_print(ife); |
| 1727 | } |
| 1728 | return res; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Mike Frysinger | b5547fb | 2006-05-11 02:35:55 +0000 | [diff] [blame] | 1731 | int display_interfaces(char *ifname); |
Manuel Novoa III | 68ea1d0 | 2001-03-12 09:57:59 +0000 | [diff] [blame] | 1732 | int display_interfaces(char *ifname) |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1733 | { |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1734 | int status; |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1735 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1736 | /* Create a channel to the NET kernel. */ |
| 1737 | if ((skfd = sockets_open(0)) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1738 | bb_perror_msg_and_die("socket"); |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1739 | } |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1740 | |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1741 | /* Do we have to show the current setup? */ |
| 1742 | status = if_print(ifname); |
Rob Landley | 9398304 | 2005-05-03 21:30:26 +0000 | [diff] [blame] | 1743 | #ifdef CONFIG_FEATURE_CLEAN_UP |
| 1744 | sockets_close(); |
| 1745 | #endif |
Glenn L McGrath | 1ed9dd9 | 2002-08-22 19:35:36 +0000 | [diff] [blame] | 1746 | exit(status < 0); |
Eric Andersen | f15d4da | 2001-03-06 00:48:59 +0000 | [diff] [blame] | 1747 | } |