Commit 57ea56b0 authored by David S. Miller's avatar David S. Miller

Merge branch 'ping6-cmsg'

Jakub Kicinski says:

====================
net: ping6: support basic socket cmsgs

Add support for common SOL_SOCKET cmsgs in ICMPv6 sockets.
Extend the cmsg tests to cover more cmsgs and socket types.

SOL_IPV6 cmsgs to follow.
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 9557167b af6ca205
......@@ -59,8 +59,6 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
struct pingfakehdr pfh;
struct ipcm6_cookie ipc6;
pr_debug("ping_v6_sendmsg(sk=%p,sk->num=%u)\n", inet, inet->inet_num);
err = ping_common_sendmsg(AF_INET6, msg, len, &user_icmph,
sizeof(user_icmph));
if (err)
......@@ -99,6 +97,14 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
(oif && sk->sk_bound_dev_if && oif != sk->sk_bound_dev_if))
return -EINVAL;
ipcm6_init_sk(&ipc6, np);
ipc6.sockc.tsflags = sk->sk_tsflags;
ipc6.sockc.mark = sk->sk_mark;
err = sock_cmsg_send(sk, msg, &ipc6.sockc);
if (err)
return err;
/* TODO: use ip6_datagram_send_ctl to get options from cmsg */
memset(&fl6, 0, sizeof(fl6));
......@@ -107,14 +113,12 @@ static int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
fl6.saddr = np->saddr;
fl6.daddr = *daddr;
fl6.flowi6_oif = oif;
fl6.flowi6_mark = sk->sk_mark;
fl6.flowi6_mark = ipc6.sockc.mark;
fl6.flowi6_uid = sk->sk_uid;
fl6.fl6_icmp_type = user_icmph.icmp6_type;
fl6.fl6_icmp_code = user_icmph.icmp6_code;
security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6));
ipcm6_init_sk(&ipc6, np);
ipc6.sockc.mark = sk->sk_mark;
fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
dst = ip6_sk_dst_lookup_flow(sk, &fl6, daddr, false);
......
......@@ -35,4 +35,4 @@ test_unix_oob
gro
ioam6_parser
toeplitz
cmsg_so_mark
cmsg_sender
......@@ -30,6 +30,7 @@ TEST_PROGS += ioam6.sh
TEST_PROGS += gro.sh
TEST_PROGS += gre_gso.sh
TEST_PROGS += cmsg_so_mark.sh
TEST_PROGS += cmsg_time.sh
TEST_PROGS += srv6_end_dt46_l3vpn_test.sh
TEST_PROGS += srv6_end_dt4_l3vpn_test.sh
TEST_PROGS += srv6_end_dt6_l3vpn_test.sh
......@@ -52,7 +53,7 @@ TEST_GEN_FILES += gro
TEST_GEN_PROGS = reuseport_bpf reuseport_bpf_cpu reuseport_bpf_numa
TEST_GEN_PROGS += reuseport_dualstack reuseaddr_conflict tls
TEST_GEN_FILES += toeplitz
TEST_GEN_FILES += cmsg_so_mark
TEST_GEN_FILES += cmsg_sender
TEST_FILES := settings
......
// SPDX-License-Identifier: GPL-2.0-or-later
#include <errno.h>
#include <error.h>
#include <netdb.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <linux/errqueue.h>
#include <linux/icmp.h>
#include <linux/icmpv6.h>
#include <linux/net_tstamp.h>
#include <linux/types.h>
#include <linux/udp.h>
#include <sys/socket.h>
enum {
ERN_SUCCESS = 0,
/* Well defined errors, callers may depend on these */
ERN_SEND = 1,
/* Informational, can reorder */
ERN_HELP,
ERN_SEND_SHORT,
ERN_SOCK_CREATE,
ERN_RESOLVE,
ERN_CMSG_WR,
ERN_SOCKOPT,
ERN_GETTIME,
ERN_RECVERR,
ERN_CMSG_RD,
ERN_CMSG_RCV,
};
struct options {
bool silent_send;
const char *host;
const char *service;
struct {
unsigned int mark;
} sockopt;
struct {
unsigned int family;
unsigned int type;
unsigned int proto;
} sock;
struct {
bool ena;
unsigned int val;
} mark;
struct {
bool ena;
unsigned int delay;
} txtime;
struct {
bool ena;
} ts;
} opt = {
.sock = {
.family = AF_UNSPEC,
.type = SOCK_DGRAM,
.proto = IPPROTO_UDP,
},
};
static struct timespec time_start_real;
static struct timespec time_start_mono;
static void __attribute__((noreturn)) cs_usage(const char *bin)
{
printf("Usage: %s [opts] <dst host> <dst port / service>\n", bin);
printf("Options:\n"
"\t\t-s Silent send() failures\n"
"\t\t-4/-6 Force IPv4 / IPv6 only\n"
"\t\t-p prot Socket protocol\n"
"\t\t (u = UDP (default); i = ICMP; r = RAW)\n"
"\n"
"\t\t-m val Set SO_MARK with given value\n"
"\t\t-M val Set SO_MARK via setsockopt\n"
"\t\t-d val Set SO_TXTIME with given delay (usec)\n"
"\t\t-t Enable time stamp reporting\n"
"");
exit(ERN_HELP);
}
static void cs_parse_args(int argc, char *argv[])
{
char o;
while ((o = getopt(argc, argv, "46sp:m:M:d:t")) != -1) {
switch (o) {
case 's':
opt.silent_send = true;
break;
case '4':
opt.sock.family = AF_INET;
break;
case '6':
opt.sock.family = AF_INET6;
break;
case 'p':
if (*optarg == 'u' || *optarg == 'U') {
opt.sock.proto = IPPROTO_UDP;
} else if (*optarg == 'i' || *optarg == 'I') {
opt.sock.proto = IPPROTO_ICMP;
} else if (*optarg == 'r') {
opt.sock.type = SOCK_RAW;
} else {
printf("Error: unknown protocol: %s\n", optarg);
cs_usage(argv[0]);
}
break;
case 'm':
opt.mark.ena = true;
opt.mark.val = atoi(optarg);
break;
case 'M':
opt.sockopt.mark = atoi(optarg);
break;
case 'd':
opt.txtime.ena = true;
opt.txtime.delay = atoi(optarg);
break;
case 't':
opt.ts.ena = true;
break;
}
}
if (optind != argc - 2)
cs_usage(argv[0]);
opt.host = argv[optind];
opt.service = argv[optind + 1];
}
static void
cs_write_cmsg(int fd, struct msghdr *msg, char *cbuf, size_t cbuf_sz)
{
struct cmsghdr *cmsg;
size_t cmsg_len;
msg->msg_control = cbuf;
cmsg_len = 0;
if (opt.mark.ena) {
cmsg = (struct cmsghdr *)(cbuf + cmsg_len);
cmsg_len += CMSG_SPACE(sizeof(__u32));
if (cbuf_sz < cmsg_len)
error(ERN_CMSG_WR, EFAULT, "cmsg buffer too small");
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SO_MARK;
cmsg->cmsg_len = CMSG_LEN(sizeof(__u32));
*(__u32 *)CMSG_DATA(cmsg) = opt.mark.val;
}
if (opt.txtime.ena) {
struct sock_txtime so_txtime = {
.clockid = CLOCK_MONOTONIC,
};
__u64 txtime;
if (setsockopt(fd, SOL_SOCKET, SO_TXTIME,
&so_txtime, sizeof(so_txtime)))
error(ERN_SOCKOPT, errno, "setsockopt TXTIME");
txtime = time_start_mono.tv_sec * (1000ULL * 1000 * 1000) +
time_start_mono.tv_nsec +
opt.txtime.delay * 1000;
cmsg = (struct cmsghdr *)(cbuf + cmsg_len);
cmsg_len += CMSG_SPACE(sizeof(txtime));
if (cbuf_sz < cmsg_len)
error(ERN_CMSG_WR, EFAULT, "cmsg buffer too small");
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_TXTIME;
cmsg->cmsg_len = CMSG_LEN(sizeof(txtime));
memcpy(CMSG_DATA(cmsg), &txtime, sizeof(txtime));
}
if (opt.ts.ena) {
__u32 val = SOF_TIMESTAMPING_SOFTWARE |
SOF_TIMESTAMPING_OPT_TSONLY;
if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMPING,
&val, sizeof(val)))
error(ERN_SOCKOPT, errno, "setsockopt TIMESTAMPING");
cmsg = (struct cmsghdr *)(cbuf + cmsg_len);
cmsg_len += CMSG_SPACE(sizeof(__u32));
if (cbuf_sz < cmsg_len)
error(ERN_CMSG_WR, EFAULT, "cmsg buffer too small");
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SO_TIMESTAMPING;
cmsg->cmsg_len = CMSG_LEN(sizeof(__u32));
*(__u32 *)CMSG_DATA(cmsg) = SOF_TIMESTAMPING_TX_SCHED |
SOF_TIMESTAMPING_TX_SOFTWARE;
}
if (cmsg_len)
msg->msg_controllen = cmsg_len;
else
msg->msg_control = NULL;
}
static const char *cs_ts_info2str(unsigned int info)
{
static const char *names[] = {
[SCM_TSTAMP_SND] = "SND",
[SCM_TSTAMP_SCHED] = "SCHED",
[SCM_TSTAMP_ACK] = "ACK",
};
if (info < sizeof(names) / sizeof(names[0]))
return names[info];
return "unknown";
}
static void
cs_read_cmsg(int fd, struct msghdr *msg, char *cbuf, size_t cbuf_sz)
{
struct sock_extended_err *see;
struct scm_timestamping *ts;
struct cmsghdr *cmsg;
int i, err;
if (!opt.ts.ena)
return;
msg->msg_control = cbuf;
msg->msg_controllen = cbuf_sz;
while (true) {
ts = NULL;
see = NULL;
memset(cbuf, 0, cbuf_sz);
err = recvmsg(fd, msg, MSG_ERRQUEUE);
if (err < 0) {
if (errno == EAGAIN)
break;
error(ERN_RECVERR, errno, "recvmsg ERRQ");
}
for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
cmsg = CMSG_NXTHDR(msg, cmsg)) {
if (cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SO_TIMESTAMPING_OLD) {
if (cmsg->cmsg_len < sizeof(*ts))
error(ERN_CMSG_RD, EINVAL, "TS cmsg");
ts = (void *)CMSG_DATA(cmsg);
}
if ((cmsg->cmsg_level == SOL_IP &&
cmsg->cmsg_type == IP_RECVERR) ||
(cmsg->cmsg_level == SOL_IPV6 &&
cmsg->cmsg_type == IPV6_RECVERR)) {
if (cmsg->cmsg_len < sizeof(*see))
error(ERN_CMSG_RD, EINVAL, "sock_err cmsg");
see = (void *)CMSG_DATA(cmsg);
}
}
if (!ts)
error(ERN_CMSG_RCV, ENOENT, "TS cmsg not found");
if (!see)
error(ERN_CMSG_RCV, ENOENT, "sock_err cmsg not found");
for (i = 0; i < 3; i++) {
unsigned long long rel_time;
if (!ts->ts[i].tv_sec && !ts->ts[i].tv_nsec)
continue;
rel_time = (ts->ts[i].tv_sec - time_start_real.tv_sec) *
(1000ULL * 1000) +
(ts->ts[i].tv_nsec - time_start_real.tv_nsec) /
1000;
printf(" %5s ts%d %lluus\n",
cs_ts_info2str(see->ee_info),
i, rel_time);
}
}
}
int main(int argc, char *argv[])
{
char buf[] = "blablablabla";
struct addrinfo hints, *ai;
struct iovec iov[1];
struct msghdr msg;
char cbuf[1024];
int err;
int fd;
cs_parse_args(argc, argv);
memset(&hints, 0, sizeof(hints));
hints.ai_family = opt.sock.family;
ai = NULL;
err = getaddrinfo(opt.host, opt.service, &hints, &ai);
if (err) {
fprintf(stderr, "Can't resolve address [%s]:%s\n",
opt.host, opt.service);
return ERN_SOCK_CREATE;
}
if (ai->ai_family == AF_INET6 && opt.sock.proto == IPPROTO_ICMP)
opt.sock.proto = IPPROTO_ICMPV6;
fd = socket(ai->ai_family, opt.sock.type, opt.sock.proto);
if (fd < 0) {
fprintf(stderr, "Can't open socket: %s\n", strerror(errno));
freeaddrinfo(ai);
return ERN_RESOLVE;
}
if (opt.sock.proto == IPPROTO_ICMP) {
buf[0] = ICMP_ECHO;
buf[1] = 0;
} else if (opt.sock.proto == IPPROTO_ICMPV6) {
buf[0] = ICMPV6_ECHO_REQUEST;
buf[1] = 0;
} else if (opt.sock.type == SOCK_RAW) {
struct udphdr hdr = { 1, 2, htons(sizeof(buf)), 0 };
struct sockaddr_in6 *sin6 = (void *)ai->ai_addr;;
memcpy(buf, &hdr, sizeof(hdr));
sin6->sin6_port = htons(opt.sock.proto);
}
if (opt.sockopt.mark &&
setsockopt(fd, SOL_SOCKET, SO_MARK,
&opt.sockopt.mark, sizeof(opt.sockopt.mark)))
error(ERN_SOCKOPT, errno, "setsockopt SO_MARK");
if (clock_gettime(CLOCK_REALTIME, &time_start_real))
error(ERN_GETTIME, errno, "gettime REALTIME");
if (clock_gettime(CLOCK_MONOTONIC, &time_start_mono))
error(ERN_GETTIME, errno, "gettime MONOTINIC");
iov[0].iov_base = buf;
iov[0].iov_len = sizeof(buf);
memset(&msg, 0, sizeof(msg));
msg.msg_name = ai->ai_addr;
msg.msg_namelen = ai->ai_addrlen;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
cs_write_cmsg(fd, &msg, cbuf, sizeof(cbuf));
err = sendmsg(fd, &msg, 0);
if (err < 0) {
if (!opt.silent_send)
fprintf(stderr, "send failed: %s\n", strerror(errno));
err = ERN_SEND;
goto err_out;
} else if (err != sizeof(buf)) {
fprintf(stderr, "short send\n");
err = ERN_SEND_SHORT;
goto err_out;
} else {
err = ERN_SUCCESS;
}
/* Make sure all timestamps have time to loop back */
usleep(opt.txtime.delay);
cs_read_cmsg(fd, &msg, cbuf, sizeof(cbuf));
err_out:
close(fd);
freeaddrinfo(ai);
return err;
}
// 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;
}
......@@ -18,6 +18,8 @@ trap cleanup EXIT
# Namespaces
ip netns add $NS
ip netns exec $NS sysctl -w net.ipv4.ping_group_range='0 2147483647' > /dev/null
# Connectivity
ip -netns $NS link add type dummy
ip -netns $NS link set dev dummy0 up
......@@ -41,15 +43,29 @@ check_result() {
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"
for ovr in setsock cmsg both; do
for i in 4 6; do
[ $i == 4 ] && TGT=$TGT4 || TGT=$TGT6
for p in u i r; do
[ $p == "u" ] && prot=UDP
[ $p == "i" ] && prot=ICMP
[ $p == "r" ] && prot=RAW
[ $ovr == "setsock" ] && m="-M"
[ $ovr == "cmsg" ] && m="-m"
[ $ovr == "both" ] && m="-M $MARK -m"
ip netns exec $NS ./cmsg_sender -$i -p $p $m $((MARK + 1)) $TGT 1234
check_result $? 0 "$prot $ovr - pass"
[ $ovr == "diff" ] && m="-M $((MARK + 1)) -m"
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"
ip netns exec $NS ./cmsg_sender -$i -p $p $m $MARK -s $TGT 1234
check_result $? 1 "$prot $ovr - rejection"
done
done
done
# Summary
if [ $BAD -ne 0 ]; then
......
#!/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
cleanup()
{
ip netns del $NS
}
trap cleanup EXIT
# Namespaces
ip netns add $NS
ip netns exec $NS sysctl -w net.ipv4.ping_group_range='0 2147483647' > /dev/null
# 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
# Need FQ for TXTIME
ip netns exec $NS tc qdisc replace dev dummy0 root fq
# Test
BAD=0
TOTAL=0
check_result() {
((TOTAL++))
if [ $1 -ne 0 ]; then
echo " Case $4 returned $1, expected 0"
((BAD++))
elif [ "$2" != "$3" ]; then
echo " Case $4 returned '$2', expected '$3'"
((BAD++))
fi
}
for i in "-4 $TGT4" "-6 $TGT6"; do
for p in u i r; do
[ $p == "u" ] && prot=UDPv${i:1:2}
[ $p == "i" ] && prot=ICMPv${i:1:2}
[ $p == "r" ] && prot=RAWv${i:1:2}
ts=$(ip netns exec $NS ./cmsg_sender -p $p $i 1234)
check_result $? "$ts" "" "$prot - no options"
ts=$(ip netns exec $NS ./cmsg_sender -p $p $i 1234 -t | wc -l)
check_result $? "$ts" "2" "$prot - ts cnt"
ts=$(ip netns exec $NS ./cmsg_sender -p $p $i 1234 -t |
sed -n "s/.*SCHED ts0 [0-9].*/OK/p")
check_result $? "$ts" "OK" "$prot - ts0 SCHED"
ts=$(ip netns exec $NS ./cmsg_sender -p $p $i 1234 -t |
sed -n "s/.*SND ts0 [0-9].*/OK/p")
check_result $? "$ts" "OK" "$prot - ts0 SND"
ts=$(ip netns exec $NS ./cmsg_sender -p $p $i 1234 -t -d 1000 |
awk '/SND/ { if ($3 > 1000) print "OK"; }')
check_result $? "$ts" "OK" "$prot - TXTIME abs"
ts=$(ip netns exec $NS ./cmsg_sender -p $p $i 1234 -t -d 1000 |
awk '/SND/ {snd=$3}
/SCHED/ {sch=$3}
END { if (snd - sch > 500) print "OK"; }')
check_result $? "$ts" "OK" "$prot - TXTIME rel"
done
done
# Summary
if [ $BAD -ne 0 ]; then
echo "FAIL - $BAD/$TOTAL cases failed"
exit 1
else
echo "OK"
exit 0
fi
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment