summaryrefslogtreecommitdiffstats
path: root/lib/debug.c
blob: b2c5003d3b7d18b2a11b1694b3e9a00655c40110 (plain)
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
/* Copyright 2011 Jozsef Kadlecsik (kadlec@blackhole.kfki.hu)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <arpa/inet.h>					/* inet_ntop */
#include <libmnl/libmnl.h>				/* libmnl backend */

struct ipset_attrname {
	const char *name;
};

static const struct ipset_attrname cmdattr2name[] = {
	[IPSET_ATTR_PROTOCOL]	= { .name = "PROTOCOL" },
	[IPSET_ATTR_SETNAME]	= { .name = "SETNAME" },
	[IPSET_ATTR_TYPENAME]	= { .name = "TYPENAME" },
	[IPSET_ATTR_REVISION]	= { .name = "REVISION" },
	[IPSET_ATTR_FAMILY]	= { .name = "FAMILY" },
	[IPSET_ATTR_FLAGS]	= { .name = "FLAGS" },
	[IPSET_ATTR_DATA]	= { .name = "DATA" },
	[IPSET_ATTR_ADT]	= { .name = "ADT" },
	[IPSET_ATTR_LINENO]	= { .name = "LINENO" },
	[IPSET_ATTR_PROTOCOL_MIN] = { .name = "PROTO_MIN" },
};

static const struct ipset_attrname createattr2name[] = {
	[IPSET_ATTR_IP]		= { .name = "IP" },
	[IPSET_ATTR_IP_TO]	= { .name = "IP_TO" },
	[IPSET_ATTR_CIDR]	= { .name = "CIDR" },
	[IPSET_ATTR_PORT]	= { .name = "PORT" },
	[IPSET_ATTR_PORT_TO]	= { .name = "PORT_TO" },
	[IPSET_ATTR_TIMEOUT]	= { .name = "TIMEOUT" },
	[IPSET_ATTR_PROTO]	= { .name = "PROTO" },
	[IPSET_ATTR_CADT_FLAGS]	= { .name = "CADT_FLAGS" },
	[IPSET_ATTR_CADT_LINENO] = { .name = "CADT_LINENO" },
	[IPSET_ATTR_GC]		= { .name = "GC" },
	[IPSET_ATTR_HASHSIZE]	= { .name = "HASHSIZE" },
	[IPSET_ATTR_MAXELEM]	= { .name = "MAXELEM" },
	[IPSET_ATTR_MARKMASK]	= { .name = "MARKMASK" },
	[IPSET_ATTR_NETMASK]	= { .name = "NETMASK" },
	[IPSET_ATTR_PROBES]	= { .name = "PROBES" },
	[IPSET_ATTR_RESIZE]	= { .name = "RESIZE" },
	[IPSET_ATTR_SIZE]	= { .name = "SIZE" },
	[IPSET_ATTR_ELEMENTS]	= { .name = "ELEMENTS" },
	[IPSET_ATTR_REFERENCES]	= { .name = "REFERENCES" },
	[IPSET_ATTR_MEMSIZE]	= { .name = "MEMSIZE" },
};

static const struct ipset_attrname adtattr2name[] = {
	[IPSET_ATTR_IP]		= { .name = "IP" },
	[IPSET_ATTR_IP_TO]	= { .name = "IP_TO" },
	[IPSET_ATTR_CIDR]	= { .name = "CIDR" },
	[IPSET_ATTR_MARK]	= { .name = "MARK" },
	[IPSET_ATTR_PORT]	= { .name = "PORT" },
	[IPSET_ATTR_PORT_TO]	= { .name = "PORT_TO" },
	[IPSET_ATTR_TIMEOUT]	= { .name = "TIMEOUT" },
	[IPSET_ATTR_PROTO]	= { .name = "PROTO" },
	[IPSET_ATTR_CADT_FLAGS]	= { .name = "CADT_FLAGS" },
	[IPSET_ATTR_CADT_LINENO] = { .name = "CADT_LINENO" },
	[IPSET_ATTR_ETHER]	= { .name = "ETHER" },
	[IPSET_ATTR_NAME]	= { .name = "NAME" },
	[IPSET_ATTR_NAMEREF]	= { .name = "NAMEREF" },
	[IPSET_ATTR_IP2]	= { .name = "IP2" },
	[IPSET_ATTR_CIDR2]	= { .name = "CIDR2" },
	[IPSET_ATTR_IP2_TO]	= { .name = "IP2_TO" },
	[IPSET_ATTR_IFACE]	= { .name = "IFACE" },
	[IPSET_ATTR_COMMENT]	= { .name = "COMMENT" },
	[IPSET_ATTR_SKBMARK]	= { .name = "SKBMARK" },
	[IPSET_ATTR_SKBPRIO]	= { .name = "SKBPRIO" },
	[IPSET_ATTR_SKBQUEUE]	= { .name = "SKBQUEUE" },
};

static void
debug_cadt_attrs(int max, const struct ipset_attr_policy *policy,
		 const struct ipset_attrname attr2name[],
		 struct nlattr *nla[])
{
	uint32_t v;
	int i;

	fprintf(stderr, "\t\t%s attributes:\n",
		policy == create_attrs ? "CREATE" : "ADT");
	for (i = IPSET_ATTR_UNSPEC + 1; i <= max; i++) {
		if (!nla[i])
			continue;
		switch (policy[i].type) {
		case MNL_TYPE_U8:
			v = *(uint8_t *) mnl_attr_get_payload(nla[i]);
			fprintf(stderr, "\t\t%s: %u\n",
				attr2name[i].name, v);
			break;
		case MNL_TYPE_U16:
			v = *(uint16_t *) mnl_attr_get_payload(nla[i]);
			fprintf(stderr, "\t\t%s: %u\n",
				attr2name[i].name, ntohs(v));
			break;
		case MNL_TYPE_U32:
			v = *(uint32_t *) mnl_attr_get_payload(nla[i]);
			fprintf(stderr, "\t\t%s: %u\n",
				attr2name[i].name, ntohl(v));
			break;
		case MNL_TYPE_U64:
			fprintf(stderr, "\t\t%s: 0x%llx\n",
				attr2name[i].name, (long long int)
				be64toh(*(uint64_t *)
					mnl_attr_get_payload(nla[i])));
			break;
		case MNL_TYPE_NUL_STRING:
			fprintf(stderr, "\t\t%s: %s\n",
				attr2name[i].name,
				(const char *) mnl_attr_get_payload(nla[i]));
			break;
		case MNL_TYPE_NESTED: {
			struct nlattr *ipattr[IPSET_ATTR_IPADDR_MAX+1] = {};
			char addr[INET6_ADDRSTRLEN];
			void *d;

			if (mnl_attr_parse_nested(nla[i], ipaddr_attr_cb,
						  ipattr) < 0) {
				fprintf(stderr,
					"\t\tIPADDR: cannot validate "
					"and parse attributes\n");
				continue;
			}
			if (ipattr[IPSET_ATTR_IPADDR_IPV4]) {
				d = mnl_attr_get_payload(
					ipattr[IPSET_ATTR_IPADDR_IPV4]);

				inet_ntop(NFPROTO_IPV4, d, addr,
					  INET6_ADDRSTRLEN);
				fprintf(stderr, "\t\t%s: %s\n",
					attr2name[i].name, addr);
			} else if (ipattr[IPSET_ATTR_IPADDR_IPV6]) {
				d = mnl_attr_get_payload(
					ipattr[IPSET_ATTR_IPADDR_IPV6]);

				inet_ntop(NFPROTO_IPV6, d, addr,
					  INET6_ADDRSTRLEN);
				fprintf(stderr, "\t\t%s: %s\n",
					attr2name[i].name, addr);
			}
			break;
		}
		default:
			fprintf(stderr, "\t\t%s: unresolved!\n",
				attr2name[i].name);
		}
	}
}

static void
debug_cmd_attrs(int cmd, struct nlattr *nla[])
{
	struct nlattr *adt[IPSET_ATTR_ADT_MAX+1] = {};
	struct nlattr *cattr[IPSET_ATTR_CREATE_MAX+1] = {};
	uint32_t v;
	int i;

	fprintf(stderr, "\tCommand attributes:\n");
	for (i = IPSET_ATTR_UNSPEC + 1; i <= IPSET_ATTR_CMD_MAX; i++) {
		if (!nla[i])
			continue;
		switch (cmd_attrs[i].type) {
		case MNL_TYPE_U8:
			v = *(uint8_t *) mnl_attr_get_payload(nla[i]);
			fprintf(stderr, "\t%s: %u\n",
				cmdattr2name[i].name, v);
			break;
		case MNL_TYPE_U16:
			v = *(uint16_t *) mnl_attr_get_payload(nla[i]);
			fprintf(stderr, "\t%s: %u\n",
				cmdattr2name[i].name, ntohs(v));
			break;
		case MNL_TYPE_U32:
			v = *(uint32_t *) mnl_attr_get_payload(nla[i]);
			fprintf(stderr, "\t%s: %u\n",
				cmdattr2name[i].name, ntohl(v));
			break;
		case MNL_TYPE_NUL_STRING:
			fprintf(stderr, "\t%s: %s\n",
				cmdattr2name[i].name,
				(const char *) mnl_attr_get_payload(nla[i]));
			break;
		case MNL_TYPE_NESTED:
			if (i == IPSET_ATTR_DATA) {
				switch (cmd) {
				case IPSET_CMD_ADD:
				case IPSET_CMD_DEL:
				case IPSET_CMD_TEST:
					if (mnl_attr_parse_nested(nla[i],
							adt_attr_cb, adt) < 0) {
						fprintf(stderr,
							"\tADT: cannot validate "
							"and parse attributes\n");
						continue;
					}
					debug_cadt_attrs(IPSET_ATTR_ADT_MAX,
							 adt_attrs,
							 adtattr2name,
							 adt);
					break;
				default:
					if (mnl_attr_parse_nested(nla[i],
							create_attr_cb,
							cattr) < 0) {
						fprintf(stderr,
							"\tCREATE: cannot validate "
							"and parse attributes\n");
						continue;
					}
					debug_cadt_attrs(IPSET_ATTR_CREATE_MAX,
							 create_attrs,
							 createattr2name,
							 cattr);
				}
			} else {
				struct nlattr *tb;
				mnl_attr_for_each_nested(tb, nla[i]) {
					memset(adt, 0, sizeof(adt));
					if (mnl_attr_parse_nested(tb,
							adt_attr_cb, adt) < 0) {
						fprintf(stderr,
							"\tADT: cannot validate "
							"and parse attributes\n");
						continue;
					}
					debug_cadt_attrs(IPSET_ATTR_ADT_MAX,
							 adt_attrs,
							 adtattr2name,
							 adt);
				}
			}
			break;
		default:
			fprintf(stderr, "\t%s: unresolved!\n",
				cmdattr2name[i].name);
		}
	}
}

void
ipset_debug_msg(const char *dir, void *buffer, int len)
{
	const struct nlmsghdr *nlh = buffer;
	struct nlattr *nla[IPSET_ATTR_CMD_MAX+1] = {};
	int cmd, nfmsglen = MNL_ALIGN(sizeof(struct nfgenmsg));

	debug = 0;
	if (!mnl_nlmsg_ok(nlh, len)) {
		fprintf(stderr, "Broken message received!\n");
		if (len < (int)sizeof(struct nlmsghdr)) {
			fprintf(stderr, "len (%d) < sizeof(struct nlmsghdr) (%d)\n",
				len, (int)sizeof(struct nlmsghdr));
		} else if (nlh->nlmsg_len < sizeof(struct nlmsghdr)) {
			fprintf(stderr, "nlmsg_len (%u) < sizeof(struct nlmsghdr) (%d)\n",
				nlh->nlmsg_len, (int)sizeof(struct nlmsghdr));
		} else if ((int)nlh->nlmsg_len > len) {
			fprintf(stderr, "nlmsg_len (%u) > len (%d)\n",
				 nlh->nlmsg_len, len);
		}
	}
	while (mnl_nlmsg_ok(nlh, len)) {
		switch (nlh->nlmsg_type) {
		case NLMSG_NOOP:
		case NLMSG_DONE:
		case NLMSG_OVERRUN:
			fprintf(stderr, "Message header: %s msg %s\n"
					"\tlen %d\n"
					"\tseq  %u\n",
				dir,
				nlh->nlmsg_type == NLMSG_NOOP ? "NOOP" :
				nlh->nlmsg_type == NLMSG_DONE ? "DONE" :
				"OVERRUN",
				len, nlh->nlmsg_seq);
			goto next_msg;
		case NLMSG_ERROR: {
			const struct nlmsgerr *err = mnl_nlmsg_get_payload(nlh);
			fprintf(stderr, "Message header: %s msg ERROR\n"
					"\tlen %d\n"
					"\terrcode %d\n"
					"\tseq %u\n",
				dir, len, err->error, nlh->nlmsg_seq);
			goto next_msg;
			}
		default:
			;
		}
		cmd = ipset_get_nlmsg_type(nlh);
		fprintf(stderr, "Message header: %s cmd  %s (%d)\n"
				"\tlen %d\n"
				"\tflag %s\n"
				"\tseq %u\n",
			dir,
			cmd <= IPSET_CMD_NONE ? "NONE!" :
			cmd >= IPSET_CMD_MAX ? "MAX!" : cmd2name[cmd], cmd,
			len,
			!(nlh->nlmsg_flags & NLM_F_EXCL) ? "EXIST" : "none",
			nlh->nlmsg_seq);
		if (cmd <= IPSET_CMD_NONE || cmd >= IPSET_CMD_MAX)
			goto next_msg;
		memset(nla, 0, sizeof(nla));
		if (mnl_attr_parse(nlh, nfmsglen,
				cmd_attr_cb, nla) < MNL_CB_STOP) {
			fprintf(stderr, "\tcannot validate "
				"and parse attributes\n");
			goto next_msg;
		}
		debug_cmd_attrs(cmd, nla);
next_msg:
		nlh = mnl_nlmsg_next(nlh, &len);
	}
	debug = 1;
}