Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com> |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 26 | */ |
| 27 | |
| 28 | #include "defs.h" |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 29 | #include <sys/socket.h> |
Dmitry V. Levin | deff569 | 2016-05-13 20:22:18 +0000 | [diff] [blame] | 30 | #if defined ALPHA || defined SH || defined SH64 |
| 31 | # include <linux/ioctl.h> |
| 32 | #endif |
Roland McGrath | 5687ff1 | 2004-07-12 07:13:06 +0000 | [diff] [blame] | 33 | #include <linux/sockios.h> |
Roland McGrath | 5687ff1 | 2004-07-12 07:13:06 +0000 | [diff] [blame] | 34 | #include <arpa/inet.h> |
Roland McGrath | 5687ff1 | 2004-07-12 07:13:06 +0000 | [diff] [blame] | 35 | #include <net/if.h> |
| 36 | |
Dmitry V. Levin | 0ed617b | 2014-04-25 23:30:54 +0000 | [diff] [blame] | 37 | #include "xlat/iffflags.h" |
Roland McGrath | e6a432d | 2005-02-02 20:25:17 +0000 | [diff] [blame] | 38 | |
Roland McGrath | e6a432d | 2005-02-02 20:25:17 +0000 | [diff] [blame] | 39 | static void |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 40 | print_ifreq_addr(struct tcb *tcp, const struct ifreq *ifr, const long addr) |
Roland McGrath | e6a432d | 2005-02-02 20:25:17 +0000 | [diff] [blame] | 41 | { |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 42 | tprintf("{"); |
| 43 | printxval(addrfams, ifr->ifr_addr.sa_family, "AF_???"); |
| 44 | tprints(", "); |
Roland McGrath | e6a432d | 2005-02-02 20:25:17 +0000 | [diff] [blame] | 45 | if (ifr->ifr_addr.sa_family == AF_INET) { |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 46 | const struct sockaddr_in *sinp = |
| 47 | (struct sockaddr_in *) &ifr->ifr_addr; |
Roland McGrath | e6a432d | 2005-02-02 20:25:17 +0000 | [diff] [blame] | 48 | tprintf("inet_addr(\"%s\")", inet_ntoa(sinp->sin_addr)); |
| 49 | } else |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 50 | printstr(tcp, addr + offsetof(struct ifreq, ifr_addr.sa_data), |
| 51 | sizeof(ifr->ifr_addr.sa_data)); |
| 52 | tprints("}"); |
Roland McGrath | e6a432d | 2005-02-02 20:25:17 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Dmitry V. Levin | 81e4515 | 2015-01-24 20:58:23 +0000 | [diff] [blame] | 55 | static void |
| 56 | print_ifname(const char *ifname) |
| 57 | { |
| 58 | print_quoted_string(ifname, IFNAMSIZ + 1, QUOTE_0_TERMINATED); |
| 59 | } |
| 60 | |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 61 | static void |
| 62 | print_ifreq(struct tcb *tcp, const unsigned int code, const long arg, |
| 63 | const struct ifreq *ifr) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 64 | { |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 65 | switch (code) { |
| 66 | case SIOCSIFADDR: |
| 67 | case SIOCGIFADDR: |
| 68 | tprints("ifr_addr="); |
| 69 | print_ifreq_addr(tcp, ifr, arg); |
| 70 | break; |
| 71 | case SIOCSIFDSTADDR: |
| 72 | case SIOCGIFDSTADDR: |
| 73 | tprints("ifr_dstaddr="); |
| 74 | print_ifreq_addr(tcp, ifr, arg); |
| 75 | break; |
| 76 | case SIOCSIFBRDADDR: |
| 77 | case SIOCGIFBRDADDR: |
| 78 | tprints("ifr_broadaddr="); |
| 79 | print_ifreq_addr(tcp, ifr, arg); |
| 80 | break; |
| 81 | case SIOCSIFNETMASK: |
| 82 | case SIOCGIFNETMASK: |
| 83 | tprints("ifr_netmask="); |
| 84 | print_ifreq_addr(tcp, ifr, arg); |
| 85 | break; |
| 86 | case SIOCSIFHWADDR: |
| 87 | case SIOCGIFHWADDR: { |
| 88 | /* XXX Are there other hardware addresses |
| 89 | than 6-byte MACs? */ |
| 90 | const unsigned char *bytes = |
| 91 | (unsigned char *) &ifr->ifr_hwaddr.sa_data; |
| 92 | tprintf("ifr_hwaddr=%02x:%02x:%02x:%02x:%02x:%02x", |
| 93 | bytes[0], bytes[1], bytes[2], |
| 94 | bytes[3], bytes[4], bytes[5]); |
| 95 | break; |
| 96 | } |
| 97 | case SIOCSIFFLAGS: |
| 98 | case SIOCGIFFLAGS: |
| 99 | tprints("ifr_flags="); |
| 100 | printflags(iffflags, ifr->ifr_flags, "IFF_???"); |
| 101 | break; |
| 102 | case SIOCSIFMETRIC: |
| 103 | case SIOCGIFMETRIC: |
| 104 | tprintf("ifr_metric=%d", ifr->ifr_metric); |
| 105 | break; |
| 106 | case SIOCSIFMTU: |
| 107 | case SIOCGIFMTU: |
| 108 | tprintf("ifr_mtu=%d", ifr->ifr_mtu); |
| 109 | break; |
| 110 | case SIOCSIFSLAVE: |
| 111 | case SIOCGIFSLAVE: |
| 112 | tprints("ifr_slave="); |
| 113 | print_ifname(ifr->ifr_slave); |
| 114 | break; |
| 115 | case SIOCSIFTXQLEN: |
| 116 | case SIOCGIFTXQLEN: |
| 117 | tprintf("ifr_qlen=%d", ifr->ifr_qlen); |
| 118 | break; |
| 119 | case SIOCSIFMAP: |
| 120 | case SIOCGIFMAP: |
| 121 | tprintf("ifr_map={mem_start=%#lx, " |
| 122 | "mem_end=%#lx, base_addr=%#x, " |
| 123 | "irq=%u, dma=%u, port=%u}", |
| 124 | ifr->ifr_map.mem_start, |
| 125 | ifr->ifr_map.mem_end, |
| 126 | (unsigned) ifr->ifr_map.base_addr, |
| 127 | (unsigned) ifr->ifr_map.irq, |
| 128 | (unsigned) ifr->ifr_map.dma, |
| 129 | (unsigned) ifr->ifr_map.port); |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | static unsigned int |
| 135 | print_ifc_len(int len) |
| 136 | { |
| 137 | const unsigned int n = (unsigned int) len / sizeof(struct ifreq); |
| 138 | |
| 139 | if (len < 0 || n * sizeof(struct ifreq) != (unsigned int) len) |
| 140 | tprintf("%d", len); |
| 141 | else |
| 142 | tprintf("%u * sizeof(struct ifreq)", n); |
| 143 | |
| 144 | return n; |
| 145 | } |
| 146 | |
| 147 | static int |
| 148 | decode_ifconf(struct tcb *tcp, const long addr) |
| 149 | { |
Roland McGrath | 5687ff1 | 2004-07-12 07:13:06 +0000 | [diff] [blame] | 150 | struct ifconf ifc; |
| 151 | |
| 152 | if (entering(tcp)) { |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 153 | tprints(", "); |
| 154 | if (umove_or_printaddr(tcp, addr, &ifc)) |
| 155 | return RVAL_DECODED | 1; |
| 156 | if (ifc.ifc_buf) { |
| 157 | tprints("{"); |
| 158 | print_ifc_len(ifc.ifc_len); |
Roland McGrath | 5687ff1 | 2004-07-12 07:13:06 +0000 | [diff] [blame] | 159 | } |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 160 | return 1; |
Roland McGrath | 5687ff1 | 2004-07-12 07:13:06 +0000 | [diff] [blame] | 161 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 162 | |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 163 | if (syserror(tcp) || umove(tcp, addr, &ifc) < 0) { |
| 164 | if (ifc.ifc_buf) |
| 165 | tprints("}"); |
| 166 | else |
| 167 | printaddr(addr); |
| 168 | return RVAL_DECODED | 1; |
| 169 | } |
| 170 | |
| 171 | if (!ifc.ifc_buf) { |
| 172 | tprints("{"); |
| 173 | print_ifc_len(ifc.ifc_len); |
| 174 | tprints(", NULL}"); |
| 175 | return RVAL_DECODED | 1; |
| 176 | } |
| 177 | |
| 178 | tprints(" => "); |
| 179 | const unsigned int nifra = print_ifc_len(ifc.ifc_len); |
| 180 | if (!nifra) { |
| 181 | tprints("}"); |
| 182 | return RVAL_DECODED | 1; |
| 183 | } |
| 184 | |
| 185 | struct ifreq ifra[nifra > max_strlen ? max_strlen : nifra]; |
| 186 | tprints(", "); |
| 187 | if (umove_or_printaddr(tcp, (unsigned long) ifc.ifc_buf, &ifra)) { |
| 188 | tprints("}"); |
| 189 | return RVAL_DECODED | 1; |
| 190 | } |
| 191 | |
| 192 | tprints("["); |
| 193 | unsigned int i; |
| 194 | for (i = 0; i < ARRAY_SIZE(ifra); ++i) { |
| 195 | if (i > 0) |
| 196 | tprints(", "); |
| 197 | tprints("{ifr_name="); |
| 198 | print_ifname(ifra[i].ifr_name); |
| 199 | tprints(", "); |
| 200 | if (verbose(tcp)) { |
| 201 | tprints("ifr_addr="); |
| 202 | print_ifreq_addr(tcp, &ifra[i], |
| 203 | addr + i * sizeof(ifra[0])); |
| 204 | } else |
| 205 | tprints("..."); |
| 206 | tprints("}"); |
| 207 | } |
| 208 | if (i < nifra) |
| 209 | tprints(", ..."); |
| 210 | tprints("]}"); |
| 211 | |
| 212 | return RVAL_DECODED | 1; |
| 213 | } |
| 214 | |
| 215 | int |
| 216 | sock_ioctl(struct tcb *tcp, const unsigned int code, const long arg) |
| 217 | { |
| 218 | struct ifreq ifr; |
| 219 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 220 | switch (code) { |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 221 | case SIOCGIFCONF: |
| 222 | return decode_ifconf(tcp, arg); |
| 223 | |
| 224 | #ifdef SIOCBRADDBR |
| 225 | case SIOCBRADDBR: |
| 226 | case SIOCBRDELBR: |
| 227 | tprints(", "); |
| 228 | printstr(tcp, arg, -1); |
| 229 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 230 | #endif |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 231 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 232 | #ifdef FIOSETOWN |
| 233 | case FIOSETOWN: |
| 234 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 235 | #ifdef SIOCSPGRP |
| 236 | case SIOCSPGRP: |
| 237 | #endif |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 238 | tprints(", "); |
| 239 | printnum_int(tcp, arg, "%d"); |
| 240 | break; |
| 241 | |
| 242 | #ifdef FIOGETOWN |
| 243 | case FIOGETOWN: |
| 244 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 245 | #ifdef SIOCGPGRP |
| 246 | case SIOCGPGRP: |
| 247 | #endif |
| 248 | #ifdef SIOCATMARK |
| 249 | case SIOCATMARK: |
| 250 | #endif |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 251 | if (entering(tcp)) |
| 252 | return 0; |
| 253 | tprints(", "); |
| 254 | printnum_int(tcp, arg, "%d"); |
| 255 | break; |
| 256 | |
| 257 | #ifdef SIOCBRADDIF |
| 258 | case SIOCBRADDIF: |
| 259 | #endif |
| 260 | #ifdef SIOCBRDELIF |
| 261 | case SIOCBRDELIF: |
| 262 | #endif |
| 263 | /* no arguments */ |
| 264 | break; |
| 265 | |
Dmitry V. Levin | 788c0d6 | 2014-10-31 19:36:01 +0000 | [diff] [blame] | 266 | case SIOCSIFNAME: |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 267 | case SIOCSIFADDR: |
| 268 | case SIOCSIFDSTADDR: |
| 269 | case SIOCSIFBRDADDR: |
| 270 | case SIOCSIFNETMASK: |
| 271 | case SIOCSIFFLAGS: |
| 272 | case SIOCSIFMETRIC: |
| 273 | case SIOCSIFMTU: |
| 274 | case SIOCSIFSLAVE: |
| 275 | case SIOCSIFHWADDR: |
| 276 | case SIOCSIFTXQLEN: |
| 277 | case SIOCSIFMAP: |
| 278 | tprints(", "); |
| 279 | if (umove_or_printaddr(tcp, arg, &ifr)) |
| 280 | break; |
| 281 | |
| 282 | tprints("{ifr_name="); |
| 283 | print_ifname(ifr.ifr_name); |
| 284 | tprints(", "); |
| 285 | if (code == SIOCSIFNAME) { |
| 286 | tprints("ifr_newname="); |
| 287 | print_ifname(ifr.ifr_newname); |
| 288 | } else { |
| 289 | print_ifreq(tcp, code, arg, &ifr); |
| 290 | } |
| 291 | tprints("}"); |
| 292 | break; |
| 293 | |
Roland McGrath | 5687ff1 | 2004-07-12 07:13:06 +0000 | [diff] [blame] | 294 | case SIOCGIFNAME: |
| 295 | case SIOCGIFINDEX: |
Roland McGrath | e6a432d | 2005-02-02 20:25:17 +0000 | [diff] [blame] | 296 | case SIOCGIFADDR: |
| 297 | case SIOCGIFDSTADDR: |
| 298 | case SIOCGIFBRDADDR: |
| 299 | case SIOCGIFNETMASK: |
| 300 | case SIOCGIFFLAGS: |
| 301 | case SIOCGIFMETRIC: |
| 302 | case SIOCGIFMTU: |
| 303 | case SIOCGIFSLAVE: |
| 304 | case SIOCGIFHWADDR: |
Dmitry V. Levin | 4028424 | 2007-03-21 13:52:14 +0000 | [diff] [blame] | 305 | case SIOCGIFTXQLEN: |
Dmitry V. Levin | ecdd0bb | 2007-03-21 13:57:50 +0000 | [diff] [blame] | 306 | case SIOCGIFMAP: |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 307 | if (entering(tcp)) { |
Dmitry V. Levin | 81e4515 | 2015-01-24 20:58:23 +0000 | [diff] [blame] | 308 | tprints(", "); |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 309 | if (umove_or_printaddr(tcp, arg, &ifr)) |
Roland McGrath | e6a432d | 2005-02-02 20:25:17 +0000 | [diff] [blame] | 310 | break; |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 311 | |
| 312 | if (SIOCGIFNAME == code) { |
| 313 | tprintf("{ifr_index=%d", ifr.ifr_ifindex); |
| 314 | } else { |
| 315 | tprints("{ifr_name="); |
| 316 | print_ifname(ifr.ifr_name); |
| 317 | } |
| 318 | return 1; |
| 319 | } else { |
| 320 | if (syserror(tcp)) { |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 321 | tprints("}"); |
Roland McGrath | e6a432d | 2005-02-02 20:25:17 +0000 | [diff] [blame] | 322 | break; |
Roland McGrath | e6a432d | 2005-02-02 20:25:17 +0000 | [diff] [blame] | 323 | } |
Dmitry V. Levin | 62e0596 | 2009-11-03 14:38:44 +0000 | [diff] [blame] | 324 | |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 325 | tprints(", "); |
| 326 | if (umove(tcp, arg, &ifr) < 0) { |
| 327 | tprints("???}"); |
| 328 | break; |
Dmitry V. Levin | 62e0596 | 2009-11-03 14:38:44 +0000 | [diff] [blame] | 329 | } |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 330 | |
| 331 | if (SIOCGIFNAME == code) { |
| 332 | tprints("ifr_name="); |
| 333 | print_ifname(ifr.ifr_name); |
| 334 | } else { |
| 335 | print_ifreq(tcp, code, arg, &ifr); |
Roland McGrath | 5687ff1 | 2004-07-12 07:13:06 +0000 | [diff] [blame] | 336 | } |
Denys Vlasenko | 60fe8c1 | 2011-09-01 10:00:28 +0200 | [diff] [blame] | 337 | tprints("}"); |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 338 | break; |
Roland McGrath | 5687ff1 | 2004-07-12 07:13:06 +0000 | [diff] [blame] | 339 | } |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 340 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 341 | default: |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 342 | return RVAL_DECODED; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 343 | } |
Dmitry V. Levin | b93c110 | 2015-07-04 14:01:08 +0300 | [diff] [blame] | 344 | |
| 345 | return RVAL_DECODED | 1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 346 | } |