-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests: udp: test for passing SO_MARK as cmsg
Before fix: | Case IPv6 rejection returned 0, expected 1 |FAIL - 1/4 cases failed With the fix: | OK Signed-off-by: Jakub Kicinski <kuba@kernel.org> Reviewed-by: David Ahern <dsahern@kernel.org> Reviewed-by: Willem de Bruijn <willemb@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
- Loading branch information
Jakub Kicinski
authored and
David S. Miller
committed
Nov 1, 2021
1 parent
42dcfd8
commit b0ced8f
Showing
4 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,4 @@ test_unix_oob | |
gro | ||
ioam6_parser | ||
toeplitz | ||
cmsg_so_mark |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
#include <errno.h> | ||
#include <netdb.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
#include <linux/types.h> | ||
#include <sys/socket.h> | ||
|
||
int main(int argc, const char **argv) | ||
{ | ||
char cbuf[CMSG_SPACE(sizeof(__u32))]; | ||
struct addrinfo hints, *ai; | ||
struct cmsghdr *cmsg; | ||
struct iovec iov[1]; | ||
struct msghdr msg; | ||
int mark; | ||
int err; | ||
int fd; | ||
|
||
if (argc != 4) { | ||
fprintf(stderr, "Usage: %s <dst_ip> <port> <mark>\n", argv[0]); | ||
return 1; | ||
} | ||
mark = atoi(argv[3]); | ||
|
||
memset(&hints, 0, sizeof(hints)); | ||
hints.ai_family = AF_UNSPEC; | ||
hints.ai_socktype = SOCK_DGRAM; | ||
|
||
ai = NULL; | ||
err = getaddrinfo(argv[1], argv[2], &hints, &ai); | ||
if (err) { | ||
fprintf(stderr, "Can't resolve address: %s\n", strerror(errno)); | ||
return 1; | ||
} | ||
|
||
fd = socket(ai->ai_family, SOCK_DGRAM, IPPROTO_UDP); | ||
if (fd < 0) { | ||
fprintf(stderr, "Can't open socket: %s\n", strerror(errno)); | ||
freeaddrinfo(ai); | ||
return 1; | ||
} | ||
|
||
iov[0].iov_base = "bla"; | ||
iov[0].iov_len = 4; | ||
|
||
msg.msg_name = ai->ai_addr; | ||
msg.msg_namelen = ai->ai_addrlen; | ||
msg.msg_iov = iov; | ||
msg.msg_iovlen = 1; | ||
msg.msg_control = cbuf; | ||
msg.msg_controllen = sizeof(cbuf); | ||
|
||
cmsg = CMSG_FIRSTHDR(&msg); | ||
cmsg->cmsg_level = SOL_SOCKET; | ||
cmsg->cmsg_type = SO_MARK; | ||
cmsg->cmsg_len = CMSG_LEN(sizeof(__u32)); | ||
*(__u32 *)CMSG_DATA(cmsg) = mark; | ||
|
||
err = sendmsg(fd, &msg, 0); | ||
|
||
close(fd); | ||
freeaddrinfo(ai); | ||
return err != 4; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#!/bin/bash | ||
# SPDX-License-Identifier: GPL-2.0 | ||
|
||
NS=ns | ||
IP4=172.16.0.1/24 | ||
TGT4=172.16.0.2 | ||
IP6=2001:db8:1::1/64 | ||
TGT6=2001:db8:1::2 | ||
MARK=1000 | ||
|
||
cleanup() | ||
{ | ||
ip netns del $NS | ||
} | ||
|
||
trap cleanup EXIT | ||
|
||
# Namespaces | ||
ip netns add $NS | ||
|
||
# Connectivity | ||
ip -netns $NS link add type dummy | ||
ip -netns $NS link set dev dummy0 up | ||
ip -netns $NS addr add $IP4 dev dummy0 | ||
ip -netns $NS addr add $IP6 dev dummy0 | ||
|
||
ip -netns $NS rule add fwmark $MARK lookup 300 | ||
ip -6 -netns $NS rule add fwmark $MARK lookup 300 | ||
ip -netns $NS route add prohibit any table 300 | ||
ip -6 -netns $NS route add prohibit any table 300 | ||
|
||
# Test | ||
BAD=0 | ||
TOTAL=0 | ||
|
||
check_result() { | ||
((TOTAL++)) | ||
if [ $1 -ne $2 ]; then | ||
echo " Case $3 returned $1, expected $2" | ||
((BAD++)) | ||
fi | ||
} | ||
|
||
ip netns exec $NS ./cmsg_so_mark $TGT4 1234 $((MARK + 1)) | ||
check_result $? 0 "IPv4 pass" | ||
ip netns exec $NS ./cmsg_so_mark $TGT6 1234 $((MARK + 1)) | ||
check_result $? 0 "IPv6 pass" | ||
|
||
ip netns exec $NS ./cmsg_so_mark $TGT4 1234 $MARK | ||
check_result $? 1 "IPv4 rejection" | ||
ip netns exec $NS ./cmsg_so_mark $TGT6 1234 $MARK | ||
check_result $? 1 "IPv6 rejection" | ||
|
||
# Summary | ||
if [ $BAD -ne 0 ]; then | ||
echo "FAIL - $BAD/$TOTAL cases failed" | ||
exit 1 | ||
else | ||
echo "OK" | ||
exit 0 | ||
fi |