1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
/*
* Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
* Copyright (c) 1996-2023 The strace developers.
* All rights reserved.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "defs.h"
#include DEF_MPERS_TYPE(struct_ifconf)
#include DEF_MPERS_TYPE(struct_ifreq)
#include <sys/socket.h>
#include <net/if.h>
typedef struct ifconf struct_ifconf;
typedef struct ifreq struct_ifreq;
#include MPERS_DEFS
#include <linux/ioctl.h>
#include <linux/sockios.h>
#include <arpa/inet.h>
#include "xlat/iffflags.h"
#define XLAT_MACROS_ONLY
#include "xlat/arp_hardware_types.h"
#undef XLAT_MACROS_ONLY
static void
print_ifr_hwaddr(const typeof_field(struct_ifreq, ifr_hwaddr) *const p)
{
tprint_struct_begin();
PRINT_FIELD_XVAL(*p, sa_family, arp_hardware_types, "ARPHRD_???");
tprint_struct_next();
PRINT_FIELD_HWADDR_SZ(*p, sa_data, sizeof(p->sa_data), p->sa_family);
tprint_struct_end();
}
static void
print_ifr_map(const typeof_field(struct_ifreq, ifr_map) *const p)
{
tprint_struct_begin();
PRINT_FIELD_X(*p, mem_start);
tprint_struct_next();
PRINT_FIELD_X(*p, mem_end);
tprint_struct_next();
PRINT_FIELD_X(*p, base_addr);
tprint_struct_next();
PRINT_FIELD_X(*p, irq);
tprint_struct_next();
PRINT_FIELD_X(*p, dma);
tprint_struct_next();
PRINT_FIELD_X(*p, port);
tprint_struct_end();
}
static void
print_ifreq(struct tcb *const tcp, const unsigned int code,
const kernel_ulong_t arg, const struct_ifreq *const ifr)
{
switch (code) {
case SIOCSIFADDR:
case SIOCGIFADDR:
PRINT_FIELD_SOCKADDR(*ifr, ifr_addr, tcp);
break;
case SIOCSIFDSTADDR:
case SIOCGIFDSTADDR:
PRINT_FIELD_SOCKADDR(*ifr, ifr_dstaddr, tcp);
break;
case SIOCSIFBRDADDR:
case SIOCGIFBRDADDR:
PRINT_FIELD_SOCKADDR(*ifr, ifr_broadaddr, tcp);
break;
case SIOCSIFNETMASK:
case SIOCGIFNETMASK:
PRINT_FIELD_SOCKADDR(*ifr, ifr_netmask, tcp);
break;
case SIOCADDMULTI:
case SIOCDELMULTI:
case SIOCGIFHWADDR:
case SIOCSIFHWADDR:
case SIOCSIFHWBROADCAST:
PRINT_FIELD_OBJ_PTR(*ifr, ifr_hwaddr, print_ifr_hwaddr);
break;
case SIOCSIFFLAGS:
case SIOCGIFFLAGS:
PRINT_FIELD_FLAGS(*ifr, ifr_flags, iffflags, "IFF_???");
break;
case SIOCGIFINDEX:
PRINT_FIELD_D(*ifr, ifr_ifindex);
break;
case SIOCSIFMETRIC:
case SIOCGIFMETRIC:
PRINT_FIELD_D(*ifr, ifr_metric);
break;
case SIOCSIFMTU:
case SIOCGIFMTU:
PRINT_FIELD_D(*ifr, ifr_mtu);
break;
case SIOCSIFSLAVE:
case SIOCGIFSLAVE:
PRINT_FIELD_CSTRING(*ifr, ifr_slave);
break;
case SIOCSIFNAME:
PRINT_FIELD_CSTRING(*ifr, ifr_newname);
break;
case SIOCGIFNAME:
PRINT_FIELD_CSTRING(*ifr, ifr_name);
break;
case SIOCSIFTXQLEN:
case SIOCGIFTXQLEN:
PRINT_FIELD_D(*ifr, ifr_qlen);
break;
case SIOCSIFMAP:
case SIOCGIFMAP:
PRINT_FIELD_OBJ_PTR(*ifr, ifr_map, print_ifr_map);
break;
}
}
static unsigned int
print_ifc_len(int len)
{
PRINT_VAL_D(len);
const unsigned int n = (unsigned int) len / sizeof(struct_ifreq);
if (len > 0 && n * sizeof(struct_ifreq) == (unsigned int) len) {
tprint_comment_begin();
PRINT_VAL_U(n);
tprints_string(" * sizeof(struct ifreq)");
tprint_comment_end();
}
return n;
}
static bool
print_ifconf_ifreq(struct tcb *tcp, void *elem_buf, size_t elem_size,
void *dummy)
{
struct_ifreq *ifr = elem_buf;
tprint_struct_begin();
PRINT_FIELD_CSTRING(*ifr, ifr_name);
tprint_struct_next();
PRINT_FIELD_SOCKADDR(*ifr, ifr_addr, tcp);
tprint_struct_end();
return true;
}
/*
* There are two different modes of operation:
*
* - Get buffer size. In this case, the callee sets ifc_buf to NULL,
* and the kernel returns the buffer size in ifc_len.
* - Get actual data. In this case, the callee specifies the buffer address
* in ifc_buf and its size in ifc_len. The kernel fills the buffer with
* the data, and its amount is returned in ifc_len.
*
* Note that, technically, the whole struct ifconf is overwritten,
* so ifc_buf could be different on exit, but current ioctl handler
* implementation does not touch it.
*/
static int
decode_ifconf(struct tcb *const tcp, const kernel_ulong_t addr)
{
struct_ifconf *entering_ifc = NULL;
struct_ifconf *ifc =
entering(tcp) ? malloc(sizeof(*ifc)) : alloca(sizeof(*ifc));
if (exiting(tcp)) {
entering_ifc = get_tcb_priv_data(tcp);
if (!entering_ifc) {
error_func_msg("where is my ifconf?");
return 0;
}
}
if (!ifc || umove(tcp, addr, ifc) < 0) {
if (entering(tcp)) {
free(ifc);
tprint_arg_next();
printaddr(addr);
} else {
/*
* We failed to fetch the structure on exiting syscall,
* print whatever was fetched on entering syscall.
*/
if (!entering_ifc->ifc_buf)
print_ifc_len(entering_ifc->ifc_len);
tprint_struct_next();
PRINT_FIELD_PTR(*entering_ifc, ifc_buf);
tprint_struct_end();
}
return RVAL_IOCTL_DECODED;
}
if (entering(tcp)) {
tprint_arg_next();
tprint_struct_begin();
tprints_field_name("ifc_len");
if (ifc->ifc_buf)
print_ifc_len(ifc->ifc_len);
set_tcb_priv_data(tcp, ifc, free);
return 0;
}
/* exiting */
if (entering_ifc->ifc_buf && (entering_ifc->ifc_len != ifc->ifc_len))
tprint_value_changed();
if (!entering_ifc->ifc_buf || (entering_ifc->ifc_len != ifc->ifc_len))
print_ifc_len(ifc->ifc_len);
if (!entering_ifc->ifc_buf || syserror(tcp)) {
tprint_struct_next();
PRINT_FIELD_PTR(*entering_ifc, ifc_buf);
if (entering_ifc->ifc_buf != ifc->ifc_buf) {
tprint_value_changed();
printaddr(ptr_to_kulong(ifc->ifc_buf));
}
} else {
struct_ifreq ifr;
tprint_struct_next();
tprints_field_name("ifc_buf");
print_array(tcp, ptr_to_kulong(ifc->ifc_buf),
ifc->ifc_len / sizeof(struct_ifreq),
&ifr, sizeof(ifr),
tfetch_mem, print_ifconf_ifreq, NULL);
}
tprint_struct_end();
return RVAL_IOCTL_DECODED;
}
MPERS_PRINTER_DECL(int, sock_ioctl,
struct tcb *tcp, const unsigned int code,
const kernel_ulong_t arg)
{
struct_ifreq ifr;
switch (code) {
case SIOCGIFCONF:
return decode_ifconf(tcp, arg);
case SIOCBRADDBR:
case SIOCBRDELBR:
tprint_arg_next();
printstr_ex(tcp, arg, sizeof(ifr.ifr_name), QUOTE_0_TERMINATED);
break;
case FIOGETOWN:
case SIOCATMARK:
case SIOCGIFENCAP:
case SIOCGPGRP:
#ifdef SIOCOUTQNSD
case SIOCOUTQNSD:
#endif
if (entering(tcp))
return 0;
ATTRIBUTE_FALLTHROUGH;
case FIOSETOWN:
case SIOCSIFENCAP:
case SIOCSPGRP:
tprint_arg_next();
printnum_int(tcp, arg, "%d");
break;
case SIOCBRADDIF:
case SIOCBRDELIF:
tprint_arg_next();
if (!umove_or_printaddr(tcp, arg, &ifr)) {
tprint_struct_begin();
PRINT_FIELD_IFINDEX(ifr, ifr_ifindex);
tprint_struct_end();
}
break;
case SIOCADDMULTI:
case SIOCDELMULTI:
case SIOCSIFADDR:
case SIOCSIFBRDADDR:
case SIOCSIFDSTADDR:
case SIOCSIFFLAGS:
case SIOCSIFHWADDR:
case SIOCSIFHWBROADCAST:
case SIOCSIFMAP:
case SIOCSIFMETRIC:
case SIOCSIFMTU:
case SIOCSIFNAME:
case SIOCSIFNETMASK:
case SIOCSIFSLAVE:
case SIOCSIFTXQLEN:
tprint_arg_next();
if (umove_or_printaddr(tcp, arg, &ifr))
break;
tprint_struct_begin();
PRINT_FIELD_CSTRING(ifr, ifr_name);
tprint_arg_next();
print_ifreq(tcp, code, arg, &ifr);
tprint_struct_end();
break;
case SIOCGIFADDR:
case SIOCGIFBRDADDR:
case SIOCGIFDSTADDR:
case SIOCGIFFLAGS:
case SIOCGIFHWADDR:
case SIOCGIFINDEX:
case SIOCGIFMAP:
case SIOCGIFMETRIC:
case SIOCGIFMTU:
case SIOCGIFNAME:
case SIOCGIFNETMASK:
case SIOCGIFSLAVE:
case SIOCGIFTXQLEN:
if (entering(tcp)) {
tprint_arg_next();
if (umove_or_printaddr(tcp, arg, &ifr))
break;
if (SIOCGIFNAME == code) {
tprint_struct_begin();
PRINT_FIELD_D(ifr, ifr_ifindex);
} else {
tprint_struct_begin();
PRINT_FIELD_CSTRING(ifr, ifr_name);
}
return 0;
} else {
if (!syserror(tcp) && !umove(tcp, arg, &ifr)) {
tprint_struct_next();
print_ifreq(tcp, code, arg, &ifr);
}
tprint_struct_end();
break;
}
case SIOCADDDLCI:
case SIOCADDRT:
case SIOCBONDCHANGEACTIVE:
case SIOCBONDENSLAVE:
case SIOCBONDINFOQUERY:
case SIOCBONDRELEASE:
case SIOCBONDSETHWADDR:
case SIOCBONDSLAVEINFOQUERY:
case SIOCDARP:
case SIOCDELDLCI:
case SIOCDELRT:
case SIOCDIFADDR:
case SIOCDRARP:
case SIOCETHTOOL:
case SIOCGARP:
#ifdef SIOCGHWTSTAMP
case SIOCGHWTSTAMP:
#endif
case SIOCGIFBR:
case SIOCGIFCOUNT:
case SIOCGIFMEM:
case SIOCGIFPFLAGS:
case SIOCGIFVLAN:
case SIOCGMIIPHY:
case SIOCGMIIREG:
case SIOCGRARP:
#ifdef SIOCGSKNS
case SIOCGSKNS:
#endif
#ifdef SIOCGSTAMP_OLD
case SIOCGSTAMP_OLD:
#endif
#ifdef SIOCGSTAMP_NEW
case SIOCGSTAMP_NEW:
#endif
#ifdef SIOCGSTAMPNS_OLD
case SIOCGSTAMPNS_OLD:
#endif
#ifdef SIOCGSTAMPNS_NEW
case SIOCGSTAMPNS_NEW:
#endif
case SIOCRTMSG:
case SIOCSARP:
#ifdef SIOCSHWTSTAMP
case SIOCSHWTSTAMP:
#endif
case SIOCSIFBR:
case SIOCSIFLINK:
case SIOCSIFMEM:
case SIOCSIFPFLAGS:
case SIOCSIFVLAN:
case SIOCSMIIREG:
case SIOCSRARP:
case SIOCWANDEV:
tprint_arg_next();
printaddr(arg);
break;
default:
return RVAL_DECODED;
}
return RVAL_IOCTL_DECODED;
}
|