File: netlink_netfilter.c

package info (click to toggle)
strace 5.10-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 49,332 kB
  • sloc: ansic: 113,177; sh: 8,831; makefile: 3,108; awk: 364; perl: 267; sed: 9
file content (189 lines) | stat: -rw-r--r-- 5,435 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (c) 2017, 2018 Chen Jingpiao <chenjingpiao@gmail.com>
 * All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "tests.h"

#ifdef HAVE_LINUX_NETFILTER_NFNETLINK_H

# include <stdio.h>
# include <string.h>
# include <unistd.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# include <sys/socket.h>
# include "test_netlink.h"
# include <linux/netfilter/nfnetlink.h>
# ifdef HAVE_LINUX_NETFILTER_NF_TABLES_H
#  include <linux/netfilter/nf_tables.h>
# endif

# ifndef NFNETLINK_V0
#  define NFNETLINK_V0 0
# endif
# ifndef NFNL_SUBSYS_NFTABLES
#  define NFNL_SUBSYS_NFTABLES 10
# endif
# ifndef NFT_MSG_NEWTABLE
#  define NFT_MSG_NEWTABLE 0
# endif

static void
test_nlmsg_type(const int fd)
{
	long rc;
	struct nlmsghdr nlh = {
		.nlmsg_len = sizeof(nlh),
		.nlmsg_flags = NLM_F_REQUEST,
	};

# ifdef NFNL_MSG_BATCH_BEGIN
	nlh.nlmsg_type = NFNL_MSG_BATCH_BEGIN;
	rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
	printf("sendto(%d, {len=%u, type=NFNL_MSG_BATCH_BEGIN"
	       ", flags=NLM_F_REQUEST, seq=0, pid=0}"
	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
	       fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
# endif

	nlh.nlmsg_type = NFNL_SUBSYS_CTNETLINK << 8 | 0xff;
	rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
	printf("sendto(%d, {len=%u"
	       ", type=NFNL_SUBSYS_CTNETLINK<<8|0xff /* IPCTNL_MSG_CT_??? */"
	       ", flags=NLM_F_REQUEST, seq=0, pid=0}"
	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
	       fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));

	nlh.nlmsg_type = 0xffff;
	rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
	printf("sendto(%d, {len=%u, type=0xff /* NFNL_SUBSYS_??? */<<8|0xff"
	       ", flags=NLM_F_REQUEST, seq=0, pid=0}"
	       ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
	       fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
}

static void
test_nlmsg_done(const int fd)
{
	const int num = 0xabcdefad;
	void *const nlh0 = midtail_alloc(NLMSG_HDRLEN, sizeof(num));

	TEST_NETLINK(fd, nlh0, NLMSG_DONE, NLM_F_REQUEST,
		     sizeof(num), &num, sizeof(num),
		     printf("%d", num));
}

static void
test_nfgenmsg(const int fd)
{
	static const struct nlattr nla = {
		.nla_len = sizeof(nla),
		.nla_type = 0x0bcd
	};

	struct nfgenmsg msg = {
		.nfgen_family = AF_UNIX,
		.version = NFNETLINK_V0,
		.res_id = NFNL_SUBSYS_NFTABLES
	};
	char str_buf[NLMSG_ALIGN(sizeof(msg)) + 4];
	char nla_buf[NLMSG_ALIGN(sizeof(msg)) + sizeof(nla)];

	void *const nlh0 = midtail_alloc(NLMSG_HDRLEN,
					 MAX(sizeof(str_buf), sizeof(nla_buf)));

	TEST_NETLINK_OBJECT_EX_(fd, nlh0,
				NFNL_SUBSYS_NFTABLES << 8 | NFT_MSG_NEWTABLE,
				"NFNL_SUBSYS_NFTABLES<<8|NFT_MSG_NEWTABLE",
				NLM_F_REQUEST, "NLM_F_REQUEST",
				msg, print_quoted_hex,
				printf("{nfgen_family=AF_UNIX");
				printf(", version=NFNETLINK_V0");
				printf(", res_id=");
				if (htons(NFNL_SUBSYS_NFTABLES) == NFNL_SUBSYS_NFTABLES)
					printf("htons(NFNL_SUBSYS_NFTABLES)");
				else
					printf("NFNL_SUBSYS_NFTABLES");
				);

	msg.res_id = htons(NFNL_SUBSYS_NFTABLES);
	TEST_NETLINK_(fd, nlh0,
		      NFNL_SUBSYS_NFTABLES << 8 | NFT_MSG_NEWTABLE,
		      "NFNL_SUBSYS_NFTABLES<<8|NFT_MSG_NEWTABLE",
		      NLM_F_REQUEST, "NLM_F_REQUEST",
		      sizeof(msg), &msg, sizeof(msg),
		      printf("{nfgen_family=AF_UNIX");
		      printf(", version=NFNETLINK_V0");
		      printf(", res_id=htons(NFNL_SUBSYS_NFTABLES)"));

	msg.res_id = htons(0xabcd);
	TEST_NETLINK_(fd, nlh0,
		      NFNL_SUBSYS_NFTABLES << 8 | NFT_MSG_NEWTABLE,
		      "NFNL_SUBSYS_NFTABLES<<8|NFT_MSG_NEWTABLE",
		      NLM_F_REQUEST, "NLM_F_REQUEST",
		      sizeof(msg), &msg, sizeof(msg),
		      printf("{nfgen_family=AF_UNIX");
		      printf(", version=NFNETLINK_V0");
		      printf(", res_id=htons(%d)", 0xabcd));

# ifdef NFNL_MSG_BATCH_BEGIN
	msg.res_id = htons(NFNL_SUBSYS_NFTABLES);
	TEST_NETLINK(fd, nlh0,
		     NFNL_MSG_BATCH_BEGIN, NLM_F_REQUEST,
		     sizeof(msg), &msg, sizeof(msg),
		     printf("{nfgen_family=AF_UNIX");
		     printf(", version=NFNETLINK_V0");
		     printf(", res_id=htons(%d)", NFNL_SUBSYS_NFTABLES));

	msg.res_id = htons(0xabcd);
	memcpy(str_buf, &msg, sizeof(msg));
	memcpy(str_buf + NLMSG_ALIGN(sizeof(msg)), "1234", 4);

	TEST_NETLINK(fd, nlh0,
		     NFNL_MSG_BATCH_BEGIN, NLM_F_REQUEST,
		     sizeof(str_buf), str_buf, sizeof(str_buf),
		     printf("{nfgen_family=AF_UNIX");
		     printf(", version=NFNETLINK_V0");
		     printf(", res_id=htons(%d)"
			    ", \"\\x31\\x32\\x33\\x34\"", 0xabcd));
# endif /* NFNL_MSG_BATCH_BEGIN */

	msg.res_id = htons(NFNL_SUBSYS_NFTABLES);
	memcpy(nla_buf, &msg, sizeof(msg));
	memcpy(nla_buf + NLMSG_ALIGN(sizeof(msg)), &nla, sizeof(nla));

	TEST_NETLINK_(fd, nlh0,
		      NFNL_SUBSYS_NFTABLES << 8 | 0xff,
		      "NFNL_SUBSYS_NFTABLES<<8|0xff /* NFT_MSG_??? */",
		      NLM_F_REQUEST, "NLM_F_REQUEST",
		      sizeof(nla_buf), nla_buf, sizeof(nla_buf),
		      printf("{nfgen_family=AF_UNIX");
		      printf(", version=NFNETLINK_V0");
		      printf(", res_id=htons(NFNL_SUBSYS_NFTABLES)"
			     ", {nla_len=%d, nla_type=%#x}",
			     nla.nla_len, nla.nla_type));
}

int main(void)
{
	skip_if_unavailable("/proc/self/fd/");

	int fd = create_nl_socket(NETLINK_NETFILTER);

	test_nlmsg_type(fd);
	test_nlmsg_done(fd);
	test_nfgenmsg(fd);

	printf("+++ exited with 0 +++\n");

	return 0;
}

#else

SKIP_MAIN_UNDEFINED("HAVE_LINUX_NETFILTER_NFNETLINK_H")

#endif