aboutgitcodelistschat:MatrixIRC
path: root/cooker/calls/net.c
blob: df97aabffa3fa4b492b7ab318cad39b9d9d3a51a (plain) (blame)
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
// SPDX-License-Identifier: GPL-3.0-or-later

/* seitan - Syscall Expressive Interpreter, Transformer and Notifier
 *
 * cooker/calls/net.c - Description of known networking system calls
 *
 * Copyright 2023 Red Hat GmbH
 * Author: Stefano Brivio <sbrivio@redhat.com>
 */

/*
fd = socket(family, type stream/dgram/..., protocol)
fd = connect(fd, addr, addrlen)
fd = accept(fd, addr, addrlen)
n  = sendto(fd, buf, len, flags, dst addr, addrlen)
n  = recvfrom(fd, buf, len, flags, src addr, addrlen)
n  = sendmsg(fd, msg, flags)
n  = recvmsg(fd, msg, flags)
e  = shutdown(fd, rd/wr/rdwr)
e  = bind(fd, addr, addrlen)
e  = listen(fd, backlog)
e  = getsockname(fd, bound addr, addrlen)
e  = getpeername(fd, peer addr, addrlen)
e  = socketpair(family, type stream/dgram/..., sockets[2])
e  = setsockopt(fd, level, optname, *optval, optlen)
e  = getsockopt(fd, level, optname, *optval, *optlen)
n  = recvmmsg(fd, *msgvec, vlen, flags, *timeout)
n  = sendmmsg(fd, *msgvec, vlen, flags)
*/

#include <asm-generic/unistd.h>
#include <sys/syscall.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <linux/un.h>
#include <linux/netlink.h>

#include "../cooker.h"
#include "../calls.h"

static struct num af[] = {
	{ "unix",	AF_UNIX },
	{ "ipv4",	AF_INET },
	{ "ipv6",	AF_INET6 },
	{ "netlink",	AF_NETLINK },
	{ "packet",	AF_PACKET },
	{ "vsock",	AF_VSOCK },
	{ 0 },
};

static struct num socket_types[] = {
	{ "stream",	SOCK_STREAM },
	{ "dgram",	SOCK_DGRAM },
	{ "seq",	SOCK_SEQPACKET },
	{ "raw",	SOCK_RAW },
	{ "packet",	SOCK_PACKET },
	{ 0 },
};

static struct num socket_flags[] = {
	{ "nonblock",	SOCK_NONBLOCK },
	{ "cloexec",	SOCK_CLOEXEC },
	{ 0 },
};

static struct num protocols[] = {
	{ "ip",		IPPROTO_IP },
	{ "icmp",	IPPROTO_ICMP },
	{ "igmp",	IPPROTO_IGMP },
	{ "tcp",	IPPROTO_TCP },
	{ "udp",	IPPROTO_UDP },
	{ "ipv6",	IPPROTO_IPV6 },
	{ "gre",	IPPROTO_GRE },
	{ "esp",	IPPROTO_ESP },
	{ "ah",		IPPROTO_AH },
	{ "sctp",	IPPROTO_SCTP },
	{ "udplite",	IPPROTO_UDPLITE },
	{ "mpls",	IPPROTO_MPLS },
	{ "raw",	IPPROTO_RAW },
	{ "mptcp",	IPPROTO_MPTCP },
	{ 0 },
};

static struct arg socket_args[] = {
	{ 0, "family",		INT,		0, { .d_num = af } },
	{ 1, "type",		INTMASK,	0, { .d_num = socket_types } },
	{ 1, "flags",		INTFLAGS,	0, { .d_num = socket_flags } },
	{ 2, "protocol",	INT,		0, { .d_num = protocols } },
	{ 0 },
};

static struct field connect_addr_unix[] = {
	{
		"path",		STRING,
		offsetof(struct sockaddr_un, sun_path),
		UNIX_PATH_MAX,	{ 0 }
	},
	{ 0 },
};

static struct field connect_addr_ipv4[] = {
	{
		"port",		PORT,
		offsetof(struct sockaddr_in, sin_port),
		0,		{ 0 }
	},
	{
		"addr",		IPV4,
		offsetof(struct sockaddr_in, sin_addr),
		0,		{ 0 }
	},
	{ 0 },
};

static struct field connect_addr_ipv6[] = {
	{
		"port",		PORT,
		offsetof(struct sockaddr_in6, sin6_port),
		0,		{ 0 }
	},
	{
		"addr",		IPV6,
		offsetof(struct sockaddr_in6, sin6_addr),
		0,		{ 0 }
	},
	{ 0 },
};

static struct field connect_addr_nl[] = {
	{
		"pid",		PID,
		offsetof(struct sockaddr_nl, nl_pid),
		0,		{ 0 }
	},
	{
		"groups",	U32,
		offsetof(struct sockaddr_nl, nl_groups),
		0,		{ 0 }
	},
	{ 0 },
};

static struct field connect_family = {
	"family",	INT,
	offsetof(struct sockaddr, sa_family),
	0,		{ .d_num = af }
};

static struct select_num connect_addr_select_family[] = {
	{
		AF_UNIX,
		{ 1, NULL, STRUCT, 0, { .d_struct = connect_addr_unix } }
	},
	{
		AF_INET,
		{ 1, NULL, STRUCT, 0, { .d_struct = connect_addr_ipv4 } }
	},
	{
		AF_INET6,
		{ 1, NULL, STRUCT, 0, { .d_struct = connect_addr_ipv6 } }
	},
	{
		AF_NETLINK,
		{ 1, NULL, STRUCT, 0, { .d_struct = connect_addr_nl } }
	},
	{ 0 },
};

static struct select connect_addr_select = {
	&connect_family, { .d_num = connect_addr_select_family }
};

static struct arg connect_args[] = {
	{
		0, "fd",	INT,	0,
		{ 0 },
	},
	{
		0, "path",	FDPATH,	0,
		{ 0 },
	},
	{
		1, "addr",	SELECT,	sizeof(struct sockaddr_storage),
		{ .d_select = &connect_addr_select },
	},
	{
		2, "addrlen",	LONG,	0,
		{ 0 },
	},
};

struct call syscalls_net[] = {
	{ __NR_connect,		"connect",		connect_args },
	{ __NR_socket,		"socket",		socket_args },
	{ 0 },
};