Skip to content

Commit

Permalink
tests/inet_socket: cover the MPTCP protocol
Browse files Browse the repository at this point in the history
As of kernel 6.5 MPTCP should work as a drop-in replacement for TCP
w.r.t. SELinux, so test it in addition to TCP using the same tests.

This requires modifying the inet_socket test to test each protocol
separately, using the same subdirectory symlink trick as in the
[fs_]filesystem tests. After that, an mptcp subtest can be added easily
by extending the programs to support it and adding a new subdirectory
symlink for it.

Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
  • Loading branch information
WOnder93 committed Jun 19, 2023
1 parent 17fecc4 commit 4dcea27
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 266 deletions.
10 changes: 8 additions & 2 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ SUBDIRS:= domain_trans entrypoint execshare exectrace execute_no_trans \
task_setnice task_setscheduler task_getscheduler task_getsid \
task_getpgid task_setpgid file ioctl capable_file capable_net \
capable_sys dyntrans dyntrace bounds nnp_nosuid mmap unix_socket \
inet_socket overlay checkreqprot mqueue mac_admin atsecure \
infiniband_endport infiniband_pkey
inet_socket/tcp inet_socket/udp overlay checkreqprot mqueue \
mac_admin atsecure infiniband_endport infiniband_pkey

ifeq ($(shell grep -q cap_userns $(POLDEV)/include/support/all_perms.spt && echo true),true)
ifneq ($(shell ./kvercmp $$(uname -r) 4.7),-1)
Expand Down Expand Up @@ -147,6 +147,12 @@ SUBDIRS += secretmem
endif
endif

# MPTCP is supported since kernel 5.6, but only works with SELinux
# since 6.5
ifneq ($(shell ./kvercmp $$(uname -r) 6.5),-1)
SUBDIRS += inet_socket/mptcp
endif

ifeq ($(DISTRO),RHEL4)
SUBDIRS:=$(filter-out bounds dyntrace dyntrans inet_socket mmap nnp_nosuid overlay unix_socket, $(SUBDIRS))
endif
Expand Down
22 changes: 16 additions & 6 deletions tests/inet_socket/bind.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
#include <unistd.h>
#include <stdio.h>

#ifndef IPPROTO_MPTCP
#define IPPROTO_MPTCP 262
#endif

void usage(char *progname)
{
fprintf(stderr, "usage: %s [stream|dgram] port\n", progname);
fprintf(stderr, "usage: %s protocol port\n", progname);
exit(1);
}

Expand All @@ -23,24 +27,30 @@ main(int argc, char **argv)
int result;
struct sockaddr_in sin;
socklen_t sinlen;
int type;
int type, protocol;
unsigned short port;

if (argc != 3)
usage(argv[0]);

if (!strcmp(argv[1], "stream"))
if (!strcmp(argv[1], "tcp")) {
type = SOCK_STREAM;
protocol = IPPROTO_TCP;
} else if (!strcmp(argv[1], "mptcp")) {
type = SOCK_STREAM;
else if (!strcmp(argv[1], "dgram"))
protocol = IPPROTO_MPTCP;
} else if (!strcmp(argv[1], "udp")) {
type = SOCK_DGRAM;
else
protocol = IPPROTO_UDP;
} else {
usage(argv[0]);
}

port = atoi(argv[2]);
if (!port)
usage(argv[0]);

sock = socket(AF_INET, type, 0);
sock = socket(AF_INET, type, protocol);
if (sock < 0) {
perror("socket");
exit(1);
Expand Down
33 changes: 21 additions & 12 deletions tests/inet_socket/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,28 @@
#include <stdbool.h>
#include <selinux/selinux.h>

#ifndef IPPROTO_MPTCP
#define IPPROTO_MPTCP 262
#endif

void usage(char *progname)
{
fprintf(stderr,
"usage: %s [-e expected_msg] [stream|dgram] addr port\n"
"usage: %s [-e expected_msg] protocol addr port\n"
"\nWhere:\n\t"
"-e Optional expected message from server e.g. \"nopeer\".\n\t"
" If not present the client context will be used as a\n\t"
" comparison with the servers reply.\n\t"
"stream Use TCP protocol or:\n\t"
"dgram use UDP protocol.\n\t"
"addr IPv4 or IPv6 address (e.g. 127.0.0.1 or ::1)\n\t"
"port Port for accessing server.\n", progname);
"-e Optional expected message from server e.g. \"nopeer\".\n\t"
" If not present the client context will be used as a\n\t"
" comparison with the servers reply.\n\t"
"protocol Protocol to use (tcp, udp, or mptcp)\n\t"
"addr IPv4 or IPv6 address (e.g. 127.0.0.1 or ::1)\n\t"
"port Port for accessing server.\n", progname);
exit(1);
}

int main(int argc, char **argv)
{
char byte, label[256], *expected = NULL;
int sock, result, opt;
int sock, result, sockprotocol, opt;
struct addrinfo hints, *serverinfo;
struct timeval tm;

Expand All @@ -53,12 +56,18 @@ int main(int argc, char **argv)

memset(&hints, 0, sizeof(struct addrinfo));

if (!strcmp(argv[optind], "stream")) {
if (!strcmp(argv[optind], "tcp")) {
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
sockprotocol = IPPROTO_TCP;
} else if (!strcmp(argv[optind], "mptcp")) {
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
} else if (!strcmp(argv[optind], "dgram")) {
sockprotocol = IPPROTO_MPTCP;
} else if (!strcmp(argv[optind], "udp")) {
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
sockprotocol = IPPROTO_UDP;
} else {
usage(argv[0]);
}
Expand All @@ -71,7 +80,7 @@ int main(int argc, char **argv)
}

sock = socket(serverinfo->ai_family, serverinfo->ai_socktype,
serverinfo->ai_protocol);
sockprotocol);
if (sock < 0) {
perror("socket");
exit(3);
Expand Down
27 changes: 22 additions & 5 deletions tests/inet_socket/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
#include <errno.h>
#include <poll.h>

#ifndef IPPROTO_MPTCP
#define IPPROTO_MPTCP 262
#endif

void usage(char *progname)
{
fprintf(stderr,
"usage: %s port\n", progname);
fprintf(stderr, "usage: %s protocol port\n", progname);
exit(1);
}

Expand All @@ -27,16 +30,30 @@ main(int argc, char **argv)
int result;
struct sockaddr_in sin;
socklen_t sinlen;
int type, protocol;
unsigned short port;

if (argc != 2)
if (argc != 3)
usage(argv[0]);

if (!strcmp(argv[1], "tcp")) {
type = SOCK_STREAM;
protocol = IPPROTO_TCP;
} else if (!strcmp(argv[1], "mptcp")) {
type = SOCK_STREAM;
protocol = IPPROTO_MPTCP;
} else if (!strcmp(argv[1], "udp")) {
type = SOCK_DGRAM;
protocol = IPPROTO_UDP;
} else {
usage(argv[0]);
}

port = atoi(argv[1]);
port = atoi(argv[2]);
if (!port)
usage(argv[0]);

ssock = socket(AF_INET, SOCK_STREAM, 0);
ssock = socket(AF_INET, type, protocol);
if (ssock < 0) {
perror("socket");
exit(1);
Expand Down
1 change: 1 addition & 0 deletions tests/inet_socket/mptcp
33 changes: 21 additions & 12 deletions tests/inet_socket/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,27 @@
#define SCM_SECURITY 0x03
#endif

#ifndef IPPROTO_MPTCP
#define IPPROTO_MPTCP 262
#endif

void usage(char *progname)
{
fprintf(stderr,
"usage: %s [-f file] [-n] [stream|dgram] port\n"
"usage: %s [-f file] [-n] protocol port\n"
"\nWhere:\n\t"
"-f Write a line to the file when listening starts.\n\t"
"-n No peer context will be available therefore send\n\t"
" \"nopeer\" message to client, otherwise the peer context\n\t"
" will be retrieved and sent to client.\n\t"
"stream Use TCP protocol or:\n\t"
"dgram use UDP protocol.\n\t"
"port Listening port\n", progname);
"-f Write a line to the file when listening starts.\n\t"
"-n No peer context will be available therefore send\n\t"
" \"nopeer\" message to client, otherwise the peer context\n\t"
" will be retrieved and sent to client.\n\t"
"protocol Protocol to use (tcp, udp, or mptcp)\n\t"
"port Listening port\n", progname);
exit(1);
}

int main(int argc, char **argv)
{
int sock, result, opt, on = 1;
int sock, result, opt, sockprotocol, on = 1;
socklen_t sinlen;
struct sockaddr_storage sin;
struct addrinfo hints, *res;
Expand Down Expand Up @@ -63,12 +66,18 @@ int main(int argc, char **argv)
hints.ai_flags = AI_PASSIVE;
hints.ai_family = AF_INET6;

if (!strcmp(argv[optind], "stream")) {
if (!strcmp(argv[optind], "tcp")) {
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
sockprotocol = IPPROTO_TCP;
} else if (!strcmp(argv[optind], "mptcp")) {
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
} else if (!strcmp(argv[optind], "dgram")) {
sockprotocol = IPPROTO_MPTCP;
} else if (!strcmp(argv[optind], "udp")) {
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
sockprotocol = IPPROTO_UDP;
} else {
usage(argv[0]);
}
Expand All @@ -79,7 +88,7 @@ int main(int argc, char **argv)
exit(1);
}

sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
sock = socket(res->ai_family, res->ai_socktype, sockprotocol);
if (sock < 0) {
perror("socket");
exit(1);
Expand Down
1 change: 1 addition & 0 deletions tests/inet_socket/tcp
Loading

0 comments on commit 4dcea27

Please sign in to comment.