aboutgitcodelistschat:MatrixIRC
path: root/seitan-cooker/calls/net.c
blob: c0949ccfb759729d65361adb427d6ea5c043ae0f (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
// 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 arg_num af[] = {
	{ "unix",	AF_UNIX },
	{ "ipv4",	AF_INET },
	{ "ipv6",	AF_INET6 },
	{ "netlink",	AF_NETLINK },
	{ "packet",	AF_PACKET },
	{ "vsock",	AF_VSOCK },
	{ 0 },
};

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

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

static struct arg_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",		ARG_INT,	0, { .d_num = af } },
	{ 1, "type",		ARG_INTMASK,	0, { .d_num = socket_types } },
	{ 1, "flags",		ARG_INTFLAGS,	0, { .d_num = socket_flags } },
	{ 2, "protocol",	ARG_INT,	0, { .d_num = protocols } },
	{ 0 },
};

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

static struct arg_struct connect_addr_ipv4[] = {
	{ "port",	ARG_PORT,
		offsetof(struct sockaddr_in, sin_port),
		0,			{ 0 }
	},
	{ "addr",	ARG_IPV4,
		offsetof(struct sockaddr_in, sin_addr),
		0,			{ 0 }
	},
	{ 0 },
};

static struct arg_struct connect_addr_ipv6[] = {
	{ "port",	ARG_PORT,
		offsetof(struct sockaddr_in6, sin6_port),
		0,			{ 0 }
	},
	{ "addr",	ARG_IPV6,
		offsetof(struct sockaddr_in6, sin6_addr),
		0,			{ 0 }
	},
	{ 0 },
};

static struct arg_struct connect_addr_nl[] = {
	{ "pid",	ARG_PID,
		offsetof(struct sockaddr_nl, nl_pid),
		0,			{ 0 }
	},
	{ "groups",	ARG_U32,
		offsetof(struct sockaddr_in6, sin6_addr),
		0,			{ 0 }
	},
	{ 0 },
};

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

static struct arg_select_num connect_addr_select_family[] = {
	{ AF_UNIX,	ARG_STRUCT,	{ .d_struct = connect_addr_unix } },
	{ AF_INET,	ARG_STRUCT,	{ .d_struct = connect_addr_ipv4 } },
	{ AF_INET6,	ARG_STRUCT,	{ .d_struct = connect_addr_ipv6 } },
	{ AF_NETLINK,	ARG_STRUCT,	{ .d_struct = connect_addr_nl } },
	{ 0 },
};

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

static struct arg connect_args[] = {
	{ 0, "fd",	ARG_INT,	0,
		{ 0 },
	},
	{ 0, "path",	ARG_FDPATH,	0,
		{ 0 },
	},
	{ 1, "addr",	ARG_SELECT,	sizeof(struct sockaddr_storage),
		{ .d_select = &connect_addr_select },
	},
	{ 2, "addrlen",	ARG_LONG,	0,
		{ 0 },
	},
};

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