]> granicus.if.org Git - strace/blob - sock.c
Enhance capget and capset syscalls decoding
[strace] / sock.c
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.
26  *
27  *      $Id$
28  */
29
30 #include "defs.h"
31
32 #include <sys/socket.h>
33 #include <linux/sockios.h>
34 #include <arpa/inet.h>
35
36 #if defined(ALPHA) || defined(SH) || defined(SH64)
37 # ifdef HAVE_SYS_IOCTL_H
38 #  include <sys/ioctl.h>
39 # elif defined(HAVE_IOCTLS_H)
40 #  include <ioctls.h>
41 # endif
42 #endif
43 #include <net/if.h>
44
45 static const struct xlat iffflags[] = {
46         { IFF_UP,               "IFF_UP"                },
47         { IFF_BROADCAST,        "IFF_BROADCAST"         },
48         { IFF_DEBUG,            "IFF_DEBUG"             },
49         { IFF_LOOPBACK,         "IFF_LOOPBACK"          },
50         { IFF_POINTOPOINT,      "IFF_POINTOPOINT"       },
51         { IFF_NOTRAILERS,       "IFF_NOTRAILERS"        },
52         { IFF_RUNNING,          "IFF_RUNNING"           },
53         { IFF_NOARP,            "IFF_NOARP"             },
54         { IFF_PROMISC,          "IFF_PROMISC"           },
55         { IFF_ALLMULTI,         "IFF_ALLMULTI"          },
56         { IFF_MASTER,           "IFF_MASTER"            },
57         { IFF_SLAVE,            "IFF_SLAVE"             },
58         { IFF_MULTICAST,        "IFF_MULTICAST"         },
59         { IFF_PORTSEL,          "IFF_PORTSEL"           },
60         { IFF_AUTOMEDIA,        "IFF_AUTOMEDIA"         },
61         { 0,                    NULL                    }
62 };
63
64 static void
65 print_addr(struct tcb *tcp, long addr, struct ifreq *ifr)
66 {
67         if (ifr->ifr_addr.sa_family == AF_INET) {
68                 struct sockaddr_in *sinp;
69                 sinp = (struct sockaddr_in *) &ifr->ifr_addr;
70                 tprintf("inet_addr(\"%s\")", inet_ntoa(sinp->sin_addr));
71         } else
72                 printstr(tcp, addr, sizeof(ifr->ifr_addr.sa_data));
73 }
74
75 int
76 sock_ioctl(struct tcb *tcp, long code, long arg)
77 {
78         struct ifreq ifr;
79         struct ifconf ifc;
80         const char *str = NULL;
81         unsigned char *bytes;
82
83         if (entering(tcp)) {
84                 if (code == SIOCGIFCONF) {
85                         if (umove(tcp, tcp->u_arg[2], &ifc) >= 0
86                             && ifc.ifc_buf == NULL)
87                                 tprintf(", {%d -> ", ifc.ifc_len);
88                         else
89                                 tprints(", {");
90                 }
91                 return 0;
92         }
93
94         switch (code) {
95 #ifdef SIOCSHIWAT
96         case SIOCSHIWAT:
97 #endif
98 #ifdef SIOCGHIWAT
99         case SIOCGHIWAT:
100 #endif
101 #ifdef SIOCSLOWAT
102         case SIOCSLOWAT:
103 #endif
104 #ifdef SIOCGLOWAT
105         case SIOCGLOWAT:
106 #endif
107 #ifdef FIOSETOWN
108         case FIOSETOWN:
109 #endif
110 #ifdef FIOGETOWN
111         case FIOGETOWN:
112 #endif
113 #ifdef SIOCSPGRP
114         case SIOCSPGRP:
115 #endif
116 #ifdef SIOCGPGRP
117         case SIOCGPGRP:
118 #endif
119 #ifdef SIOCATMARK
120         case SIOCATMARK:
121 #endif
122                 printnum(tcp, arg, ", %#d");
123                 return 1;
124         case SIOCGIFNAME:
125         case SIOCSIFNAME:
126         case SIOCGIFINDEX:
127         case SIOCGIFADDR:
128         case SIOCSIFADDR:
129         case SIOCGIFDSTADDR:
130         case SIOCSIFDSTADDR:
131         case SIOCGIFBRDADDR:
132         case SIOCSIFBRDADDR:
133         case SIOCGIFNETMASK:
134         case SIOCSIFNETMASK:
135         case SIOCGIFFLAGS:
136         case SIOCSIFFLAGS:
137         case SIOCGIFMETRIC:
138         case SIOCSIFMETRIC:
139         case SIOCGIFMTU:
140         case SIOCSIFMTU:
141         case SIOCGIFSLAVE:
142         case SIOCSIFSLAVE:
143         case SIOCGIFHWADDR:
144         case SIOCSIFHWADDR:
145         case SIOCGIFTXQLEN:
146         case SIOCSIFTXQLEN:
147         case SIOCGIFMAP:
148         case SIOCSIFMAP:
149                 if (umove(tcp, tcp->u_arg[2], &ifr) < 0)
150                         tprintf(", %#lx", tcp->u_arg[2]);
151                 else if (syserror(tcp)) {
152                         if (code == SIOCGIFNAME || code == SIOCSIFNAME)
153                                 tprintf(", {ifr_index=%d, ifr_name=???}", ifr.ifr_ifindex);
154                         else
155                                 tprintf(", {ifr_name=\"%s\", ???}", ifr.ifr_name);
156                 } else if (code == SIOCGIFNAME || code == SIOCSIFNAME)
157                         tprintf(", {ifr_index=%d, ifr_name=\"%s\"}",
158                                 ifr.ifr_ifindex, ifr.ifr_name);
159                 else {
160                         tprintf(", {ifr_name=\"%s\", ", ifr.ifr_name);
161                         switch (code) {
162                         case SIOCGIFINDEX:
163                                 tprintf("ifr_index=%d", ifr.ifr_ifindex);
164                                 break;
165                         case SIOCGIFADDR:
166                         case SIOCSIFADDR:
167                                 str = "ifr_addr";
168                         case SIOCGIFDSTADDR:
169                         case SIOCSIFDSTADDR:
170                                 if (!str)
171                                         str = "ifr_dstaddr";
172                         case SIOCGIFBRDADDR:
173                         case SIOCSIFBRDADDR:
174                                 if (!str)
175                                         str = "ifr_broadaddr";
176                         case SIOCGIFNETMASK:
177                         case SIOCSIFNETMASK:
178                                 if (!str)
179                                         str = "ifr_netmask";
180                                 tprintf("%s={", str);
181                                 printxval(addrfams,
182                                           ifr.ifr_addr.sa_family,
183                                           "AF_???");
184                                 tprints(", ");
185                                 print_addr(tcp, ((long) tcp->u_arg[2]
186                                                  + offsetof(struct ifreq,
187                                                              ifr_addr.sa_data)),
188                                            &ifr);
189                                 tprints("}");
190                                 break;
191                         case SIOCGIFHWADDR:
192                         case SIOCSIFHWADDR:
193                                 /* XXX Are there other hardware addresses
194                                    than 6-byte MACs?  */
195                                 bytes = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
196                                 tprintf("ifr_hwaddr=%02x:%02x:%02x:%02x:%02x:%02x",
197                                         bytes[0], bytes[1], bytes[2],
198                                         bytes[3], bytes[4], bytes[5]);
199                                 break;
200                         case SIOCGIFFLAGS:
201                         case SIOCSIFFLAGS:
202                                 tprints("ifr_flags=");
203                                 printflags(iffflags, ifr.ifr_flags, "IFF_???");
204                                 break;
205                         case SIOCGIFMETRIC:
206                         case SIOCSIFMETRIC:
207                                 tprintf("ifr_metric=%d", ifr.ifr_metric);
208                                 break;
209                         case SIOCGIFMTU:
210                         case SIOCSIFMTU:
211                                 tprintf("ifr_mtu=%d", ifr.ifr_mtu);
212                                 break;
213                         case SIOCGIFSLAVE:
214                         case SIOCSIFSLAVE:
215                                 tprintf("ifr_slave=\"%s\"", ifr.ifr_slave);
216                                 break;
217                         case SIOCGIFTXQLEN:
218                         case SIOCSIFTXQLEN:
219                                 tprintf("ifr_qlen=%d", ifr.ifr_qlen);
220                                 break;
221                         case SIOCGIFMAP:
222                         case SIOCSIFMAP:
223                                 tprintf("ifr_map={mem_start=%#lx, "
224                                         "mem_end=%#lx, base_addr=%#x, "
225                                         "irq=%u, dma=%u, port=%u}",
226                                         ifr.ifr_map.mem_start,
227                                         ifr.ifr_map.mem_end,
228                                         (unsigned) ifr.ifr_map.base_addr,
229                                         (unsigned) ifr.ifr_map.irq,
230                                         (unsigned) ifr.ifr_map.dma,
231                                         (unsigned) ifr.ifr_map.port);
232                                 break;
233                         }
234                         tprints("}");
235                 }
236                 return 1;
237         case SIOCGIFCONF:
238                 if (umove(tcp, tcp->u_arg[2], &ifc) < 0) {
239                         tprints("???}");
240                         return 1;
241                 }
242                 tprintf("%d, ", ifc.ifc_len);
243                 if (syserror(tcp)) {
244                         tprintf("%lx", (unsigned long) ifc.ifc_buf);
245                 } else if (ifc.ifc_buf == NULL) {
246                         tprints("NULL");
247                 } else {
248                         int i;
249                         unsigned nifra = ifc.ifc_len / sizeof(struct ifreq);
250                         struct ifreq ifra[nifra];
251
252                         if (umoven(tcp, (unsigned long) ifc.ifc_buf,
253                                 sizeof(ifra), (char *) ifra) < 0) {
254                                 tprintf("%lx}", (unsigned long) ifc.ifc_buf);
255                                 return 1;
256                         }
257                         tprints("{");
258                         for (i = 0; i < nifra; ++i ) {
259                                 if (i > 0)
260                                         tprints(", ");
261                                 tprintf("{\"%s\", {",
262                                         ifra[i].ifr_name);
263                                 if (verbose(tcp)) {
264                                         printxval(addrfams,
265                                                   ifra[i].ifr_addr.sa_family,
266                                                   "AF_???");
267                                         tprints(", ");
268                                         print_addr(tcp, ((long) tcp->u_arg[2]
269                                                          + offsetof(struct ifreq,
270                                                                      ifr_addr.sa_data)
271                                                          + ((char *) &ifra[i]
272                                                             - (char *) &ifra[0])),
273                                                    &ifra[i]);
274                                 } else
275                                         tprints("...");
276                                 tprints("}}");
277                         }
278                         tprints("}");
279                 }
280                 tprints("}");
281                 return 1;
282         default:
283                 return 0;
284         }
285 }
286
287 int
288 sys_socketcall(struct tcb *tcp)
289 {
290         return printargs(tcp);
291 }