1 /*
2 * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
3 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
4 * Copyright (c) 2016-2022 The strace developers.
5 * All rights reserved.
6 *
7 * SPDX-License-Identifier: LGPL-2.1-or-later
8 */
9
10 #ifndef STRACE_NLATTR_H
11 # define STRACE_NLATTR_H
12
13 # include "xlat.h"
14
15 struct decode_nla_xlat_opts {
16 const struct xlat *xlat;
17 const char *dflt;
18 enum xlat_style style;
19 const char *fn_str;
20 uint64_t (*process_fn)(uint64_t val);
21 size_t size;
22 };
23
24 /*
25 * Used for IFLA_LINKINFO decoding. Since there are no other indicators
26 * regarding the nature of data except for previously provided string
27 * in an IFLA_LINKINFO_KIND attribute, we have to store it in order to pass
28 * between calls as an opaque data.
29 */
30 struct ifla_linkinfo_ctx {
31 char kind[64];
32 char slave_kind[64];
33 };
34
35 typedef bool (*nla_decoder_t)(struct tcb *, kernel_ulong_t addr,
36 unsigned int len, const void *opaque_data);
37
38 /**
39 * The case of non-NULL decoders and zero size is handled in a special way:
40 * the zeroth decoder is always called with nla_type being passed as opaque
41 * data.
42 */
43 extern void
44 decode_nlattr(struct tcb *,
45 kernel_ulong_t addr,
46 unsigned int len,
47 const struct xlat *,
48 const char *dflt,
49 const nla_decoder_t *decoders,
50 unsigned int size,
51 const void *opaque_data);
52
53 # define DECL_NLA(name) \
54 extern bool \
55 decode_nla_ ## name(struct tcb *, kernel_ulong_t addr, \
56 unsigned int len, const void *) \
57 /* End of DECL_NLA definition. */
58
59 DECL_NLA(x8);
60 DECL_NLA(x16);
61 DECL_NLA(x32);
62 DECL_NLA(x64);
63 DECL_NLA(xint);
64 DECL_NLA(u8);
65 DECL_NLA(u16);
66 DECL_NLA(u32);
67 DECL_NLA(u64);
68 DECL_NLA(s8);
69 DECL_NLA(s16);
70 DECL_NLA(s32);
71 DECL_NLA(s64);
72 DECL_NLA(be16);
73 DECL_NLA(be64);
74 DECL_NLA(xval);
75 DECL_NLA(flags);
76 DECL_NLA(str);
77 DECL_NLA(strn);
78 DECL_NLA(fd);
79 DECL_NLA(uid);
80 DECL_NLA(gid);
81 DECL_NLA(clock_t);
82 DECL_NLA(ifindex);
83 DECL_NLA(ifla_af_spec);
84 DECL_NLA(ether_proto);
85 DECL_NLA(ip_proto);
86 DECL_NLA(in_addr);
87 DECL_NLA(in6_addr);
88 DECL_NLA(lwt_encap_type);
89 DECL_NLA(meminfo);
90 DECL_NLA(rt_class);
91 DECL_NLA(rt_proto);
92 DECL_NLA(rtnl_link_stats64);
93 DECL_NLA(tc_stats);
94
95 # define NLA_HWADDR_FAMILY_OFFSET 1024
96
97 /**
98 * Print hardware (low-level, L2) address.
99 * @param opaque_data Interpreted as integer value, not pointer
100 */
101 DECL_NLA(hwaddr);
102
103 /** Non-standard decoder that accepts ARPHRD_* value as an argument */
104 static inline bool
105 decode_nla_hwaddr_family(struct tcb *const tcp,
106 const kernel_ulong_t addr,
107 const unsigned int len,
108 const unsigned int arphrd)
109 {
110 return decode_nla_hwaddr(tcp, addr, len, (void *) (uintptr_t) (
111 arphrd > NLA_HWADDR_FAMILY_OFFSET ? 0
112 : NLA_HWADDR_FAMILY_OFFSET | arphrd));
113 }
114
115 /** decode_nla_hwaddr wrapper that ignores opaque_data */
116 static inline bool
117 decode_nla_hwaddr_nofamily(struct tcb *const tcp,
118 const kernel_ulong_t addr,
119 const unsigned int len,
120 const void *const opaque_data)
121 {
122 return decode_nla_hwaddr(tcp, addr, len, NULL);
123 }
124
125 /* Common handling for AF_SPEC-type decoders */
126
127 struct af_spec_decoder_desc {
128 uint8_t af;
129 const struct xlat *xlat;
130 const char *dflt;
131 const nla_decoder_t *table;
132 size_t size;
133 };
134
135 /**
136 * Non-standard decoder for handling AF_SPEC-type netlink attributes.
137 *
138 * @param af Address family (AF_*), as passed to decoder in opaque_data
139 * parameter through zero-sized decoder table decode_nlattr
140 * hack.
141 * @param descs List of supported decoders.
142 * @param desc_cnt Number of items in descs.
143 */
144 extern void decode_nla_af_spec(struct tcb *const tcp,
145 const kernel_ulong_t addr,
146 const unsigned int len,
147 uint8_t af,
148 const struct af_spec_decoder_desc *descs,
149 size_t desc_cnt);
150
151 #endif /* !STRACE_NLATTR_H */