]> granicus.if.org Git - strace/commitdiff
Implement IPPROTO_IP control messages decoding
authorDmitry V. Levin <ldv@altlinux.org>
Sat, 21 Nov 2015 00:03:54 +0000 (03:03 +0300)
committerDmitry V. Levin <ldv@altlinux.org>
Sun, 22 Nov 2015 02:56:45 +0000 (02:56 +0000)
* net.c: Include "xlat/ip_cmsg_types.h".
(print_cmsg_ip_pktinfo, print_cmsg_ip_ttl, print_cmsg_ip_tos,
print_cmsg_ip_opts, print_cmsg_ip_recverr, print_cmsg_ip_checksum,
print_cmsg_ip_origdstaddr): New functions.
(print_cmsg_type_data): Add generic SOL_IP level decoding.
Use these functions for decoding of IP_PKTINFO, IP_TTL, IP_TOS,
IP_RECVOPTS, IP_RETOPTS, IP_RECVERR, IP_ORIGDSTADDR, IP_CHECKSUM,
and SCM_SECURITY type messages.
* xlat/ip_cmsg_types.in: New file.
* xlat/sockipoptions.in: Move IP_RETOPTS before IP_RECVRETOPTS.
* tests/inet-cmsg.c: New file.
* tests/inet-cmsg.test: New test.
* tests/Makefile.am (check_PROGRAMS): Add inet-cmsg.
(TESTS): Add inet-cmsg.test.
* tests/.gitignore: Add inet-cmsg.

Suggested-by: Orion Poplawski <orion@cora.nwra.com>
net.c
tests/.gitignore
tests/Makefile.am
tests/inet-cmsg.c [new file with mode: 0644]
tests/inet-cmsg.test [new file with mode: 0755]
xlat/ip_cmsg_types.in [new file with mode: 0644]
xlat/sockipoptions.in

diff --git a/net.c b/net.c
index 3dd1368f61ec55432d55094154c9455ad145b92b..85c46ee7340863f52a5a0bf3fd36097c3f3df679 100644 (file)
--- a/net.c
+++ b/net.c
@@ -302,6 +302,7 @@ printsock(struct tcb *tcp, long addr, int addrlen)
 }
 
 #include "xlat/scmvals.h"
+#include "xlat/ip_cmsg_types.h"
 
 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
 struct cmsghdr32 {
@@ -365,6 +366,111 @@ print_scm_security(struct tcb *tcp, const void *cmsg_data,
        print_quoted_string(cmsg_data, data_len, 0);
 }
 
+static void
+print_cmsg_ip_pktinfo(struct tcb *tcp, const void *cmsg_data,
+                     const size_t data_len)
+{
+       const struct in_pktinfo *info = cmsg_data;
+
+       if (sizeof(*info) > data_len)
+               return;
+
+       tprints(", {ipi_ifindex=");
+       print_ifindex(info->ipi_ifindex);
+       tprintf(", ipi_spec_dst=inet_addr(\"%s\"), ipi_addr=inet_addr(\"%s\")}",
+               inet_ntoa(info->ipi_spec_dst), inet_ntoa(info->ipi_addr));
+}
+
+static void
+print_cmsg_ip_ttl(struct tcb *tcp, const void *cmsg_data,
+                 const size_t data_len)
+{
+       const unsigned int *ttl = cmsg_data;
+
+       if (sizeof(*ttl) > data_len)
+               return;
+
+       tprintf(", {ttl=%u}", *ttl);
+}
+
+static void
+print_cmsg_ip_tos(struct tcb *tcp, const void *cmsg_data,
+                 const size_t data_len)
+{
+       const uint8_t *tos = cmsg_data;
+
+       if (sizeof(*tos) > data_len)
+               return;
+
+       tprintf(", {tos=%x}", *tos);
+}
+
+static void
+print_cmsg_ip_checksum(struct tcb *tcp, const void *cmsg_data,
+                      const size_t data_len)
+{
+       const uint32_t *csum = cmsg_data;
+
+       if (sizeof(*csum) > data_len)
+               return;
+
+       tprintf(", {csum=%u}", *csum);
+}
+
+static void
+print_cmsg_ip_opts(struct tcb *tcp, const void *cmsg_data,
+                  const size_t data_len)
+{
+       const char *opts = cmsg_data;
+       size_t i;
+
+       if (!data_len)
+               return;
+
+       tprints(", {opts=0x");
+       for (i = 0; i < data_len; ++i)
+               tprintf("%02x", opts[i]);
+       tprints("}");
+}
+
+static void
+print_cmsg_ip_recverr(struct tcb *tcp, const void *cmsg_data,
+                     const size_t data_len)
+{
+       const struct {
+               uint32_t ee_errno;
+               uint8_t  ee_origin;
+               uint8_t  ee_type;
+               uint8_t  ee_code;
+               uint8_t  ee_pad;
+               uint32_t ee_info;
+               uint32_t ee_data;
+               struct sockaddr_in offender;
+       } *err = cmsg_data;
+
+       if (sizeof(*err) > data_len)
+               return;
+
+       tprintf(", {ee_errno=%u, ee_origin=%u, ee_type=%u, ee_code=%u"
+               ", ee_info=%u, ee_data=%u, offender=",
+               err->ee_errno, err->ee_origin, err->ee_type,
+               err->ee_code, err->ee_info, err->ee_data);
+       print_sockaddr(tcp, (const void *) &err->offender,
+               sizeof(err->offender));
+       tprints("}");
+}
+
+static void
+print_cmsg_ip_origdstaddr(struct tcb *tcp, const void *cmsg_data,
+                         const size_t data_len)
+{
+       if (sizeof(struct sockaddr_in) > data_len)
+               return;
+
+       tprints(", ");
+       print_sockaddr(tcp, cmsg_data, data_len);
+}
+
 static void
 print_cmsg_type_data(struct tcb *tcp, const int cmsg_level, const int cmsg_type,
                     const void *cmsg_data, const size_t data_len)
@@ -384,6 +490,36 @@ print_cmsg_type_data(struct tcb *tcp, const int cmsg_level, const int cmsg_type,
                        break;
                }
                break;
+       case SOL_IP:
+               printxval(ip_cmsg_types, cmsg_type, "IP_???");
+               switch (cmsg_type) {
+               case IP_PKTINFO:
+                       print_cmsg_ip_pktinfo(tcp, cmsg_data, data_len);
+                       break;
+               case IP_TTL:
+                       print_cmsg_ip_ttl(tcp, cmsg_data, data_len);
+                       break;
+               case IP_TOS:
+                       print_cmsg_ip_tos(tcp, cmsg_data, data_len);
+                       break;
+               case IP_RECVOPTS:
+               case IP_RETOPTS:
+                       print_cmsg_ip_opts(tcp, cmsg_data, data_len);
+                       break;
+               case IP_RECVERR:
+                       print_cmsg_ip_recverr(tcp, cmsg_data, data_len);
+                       break;
+               case IP_ORIGDSTADDR:
+                       print_cmsg_ip_origdstaddr(tcp, cmsg_data, data_len);
+                       break;
+               case IP_CHECKSUM:
+                       print_cmsg_ip_checksum(tcp, cmsg_data, data_len);
+                       break;
+               case SCM_SECURITY:
+                       print_scm_security(tcp, cmsg_data, data_len);
+                       break;
+               }
+               break;
        default:
                tprintf("%u", cmsg_type);
        }
index 8542f53f3432f79b9e6aaa1b272dec89c7292755..097d1c1e01a4a0941fe022d49c72a5629dceb6f1 100644 (file)
@@ -14,6 +14,7 @@ getdents
 getdents64
 getrandom
 inet-accept-connect-send-recv
+inet-cmsg
 ioctl
 ip_mreq
 ipc_msg
index 86a9a507a04d8c45bbb6760e875c1e08efa53f8c..2db10c4e95cd26c6426b8caefa746f4117bf9cb1 100644 (file)
@@ -27,6 +27,7 @@ check_PROGRAMS = \
        getdents64 \
        getrandom \
        inet-accept-connect-send-recv \
+       inet-cmsg \
        ioctl \
        ip_mreq \
        ipc_msg \
@@ -125,6 +126,7 @@ TESTS = \
        getdents.test \
        getdents64.test \
        getrandom.test \
+       inet-cmsg.test \
        ioctl.test \
        ip_mreq.test \
        ipc_msg.test \
diff --git a/tests/inet-cmsg.c b/tests/inet-cmsg.c
new file mode 100644 (file)
index 0000000..bfc8075
--- /dev/null
@@ -0,0 +1,192 @@
+/*
+ * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+static void
+print_pktinfo(const struct cmsghdr *c)
+{
+       printf("IP_PKTINFO, {ipi_ifindex=if_nametoindex(\"lo\")"
+              ", ipi_spec_dst=inet_addr(\"127.0.0.1\")"
+              ", ipi_addr=inet_addr(\"127.0.0.1\")}");
+}
+
+static void
+print_ttl(const struct cmsghdr *c)
+{
+       const unsigned int *ttl = (const unsigned int *) CMSG_DATA(c);
+
+       printf("IP_TTL, {ttl=%u}", *ttl);
+}
+
+static void
+print_tos(const struct cmsghdr *c)
+{
+       const uint8_t *tos = (const uint8_t *) CMSG_DATA(c);
+
+       printf("IP_TOS, {tos=%x}", *tos);
+}
+
+static void
+print_opts(const char *name, const struct cmsghdr *c)
+{
+       const unsigned char *opts = (const unsigned char *) CMSG_DATA(c);
+       const size_t len = c->cmsg_len - CMSG_ALIGN(sizeof(*c));
+
+       printf("%s", name);
+       if (len) {
+               printf(", {opts=0x");
+               size_t i;
+               for (i = 0; i < len; ++i)
+                       printf("%02x", opts[i]);
+               printf("}");
+       }
+}
+
+static void
+print_origdstaddr(const struct cmsghdr *c)
+{
+       const struct sockaddr_in *sin =
+               (const struct sockaddr_in *) CMSG_DATA(c);
+
+       printf("IP_ORIGDSTADDR, {sa_family=AF_INET, sin_port=htons(%u)"
+              ", sin_addr=inet_addr(\"127.0.0.1\")}", ntohs(sin->sin_port));
+}
+
+int
+main(void)
+{
+       int i;
+       while ((i = open("/dev/null", O_RDWR)) < 3)
+               assert(i >= 0);
+       assert(!close(0));
+       assert(!close(3));
+
+       if (socket(PF_INET, SOCK_DGRAM, 0)) {
+               perror("socket");
+               return 77;
+       }
+       struct sockaddr_in addr = {
+               .sin_family = AF_INET,
+               .sin_addr.s_addr = htonl(INADDR_LOOPBACK)
+       };
+       socklen_t len = sizeof(addr);
+       assert(!bind(0, (struct sockaddr *) &addr, len));
+       assert(!getsockname(0, (struct sockaddr *) &addr, &len));
+
+       assert(socket(PF_INET, SOCK_DGRAM, 0) == 3);
+       assert(!connect(3, (struct sockaddr *) &addr, len));
+
+       const int opt_1 = htonl(0x01000000);
+#define SETSOCKOPT(fd, name) assert(!setsockopt(fd, IPPROTO_IP, (name), &opt_1, sizeof(opt_1)))
+       SETSOCKOPT(3, IP_OPTIONS);
+       SETSOCKOPT(0, IP_PKTINFO);
+       SETSOCKOPT(0, IP_RECVTTL);
+       SETSOCKOPT(0, IP_RECVTOS);
+       SETSOCKOPT(0, IP_RECVOPTS);
+       SETSOCKOPT(0, IP_RETOPTS);
+#ifdef IP_RECVORIGDSTADDR
+       SETSOCKOPT(0, IP_RECVORIGDSTADDR);
+#endif
+
+       static const char data[] = "data";
+       const size_t size = sizeof(data) - 1;
+       assert(send(3, data, size, 0) == (int) size);
+       assert(!close(3));
+
+       char buf[size];
+       struct iovec iov = {
+               .iov_base = buf,
+               .iov_len = sizeof(buf)
+       };
+       struct cmsghdr control[16];
+       struct msghdr mh = {
+               .msg_name = &addr,
+               .msg_namelen = len,
+               .msg_iov = &iov,
+               .msg_iovlen = 1,
+               .msg_control = control,
+               .msg_controllen = sizeof(control)
+       };
+
+       assert(recvmsg(0, &mh, 0) == (int) size);
+       assert(!close(0));
+
+       printf("recvmsg(0, {msg_name(%u)={sa_family=AF_INET, sin_port=htons(%u)"
+              ", sin_addr=inet_addr(\"127.0.0.1\")}, msg_iov(1)=[{\"%s\", %zu}]"
+              ", msg_controllen=%zu, [",
+              (unsigned) mh.msg_namelen, ntohs(addr.sin_port),
+              data, size, mh.msg_controllen);
+
+       struct cmsghdr *c;
+       for (c = CMSG_FIRSTHDR(&mh); c; c = CMSG_NXTHDR(&mh, c)) {
+               if (IPPROTO_IP != c->cmsg_level)
+                       continue;
+               if (c != control)
+                       printf(", ");
+               printf("{cmsg_len=%zu, cmsg_level=SOL_IP, cmsg_type=",
+                      c->cmsg_len);
+               switch (c->cmsg_type) {
+                       case IP_PKTINFO:
+                               print_pktinfo(c);
+                               break;
+                       case IP_TTL:
+                               print_ttl(c);
+                               break;
+                       case IP_TOS:
+                               print_tos(c);
+                               break;
+                       case IP_RECVOPTS:
+                               print_opts("IP_RECVOPTS", c);
+                               break;
+                       case IP_RETOPTS:
+                               print_opts("IP_RETOPTS", c);
+                               break;
+#ifdef IP_ORIGDSTADDR
+                       case IP_ORIGDSTADDR:
+                               print_origdstaddr(c);
+                               break;
+#endif
+                       default:
+                               printf("%d", c->cmsg_type);
+                               break;
+               }
+               printf("}");
+       }
+       printf("], msg_flags=0}, 0) = %zu\n", size);
+       puts("+++ exited with 0 +++");
+
+       return 0;
+}
diff --git a/tests/inet-cmsg.test b/tests/inet-cmsg.test
new file mode 100755 (executable)
index 0000000..be9f845
--- /dev/null
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+# Check IPPROTO_IP control messages decoding.
+
+. "${srcdir=.}/init.sh"
+
+run_prog > /dev/null
+
+OUT="$LOG.out"
+run_strace -erecvmsg $args > "$OUT"
+
+match_diff "$OUT" "$LOG"
+rm -f "$OUT"
+
+exit 0
diff --git a/xlat/ip_cmsg_types.in b/xlat/ip_cmsg_types.in
new file mode 100644 (file)
index 0000000..1bc81f4
--- /dev/null
@@ -0,0 +1,9 @@
+IP_TOS         1
+IP_TTL         2
+IP_RECVOPTS    6
+IP_RETOPTS     7
+IP_PKTINFO     8
+IP_RECVERR     11
+IP_ORIGDSTADDR 20
+IP_CHECKSUM    23
+SCM_SECURITY
index b88b346b009736987336df22ecd4e4aa4a72cdff..aa785322d486e088ea3ae54e26f66c8109d3904e 100644 (file)
@@ -5,9 +5,9 @@ IP_OPTIONS
 IP_ROUTER_ALERT
 IP_RECVOPTIONS
 IP_RECVOPTS
+IP_RETOPTS
 IP_RECVRETOPTS
 IP_RECVDSTADDR
-IP_RETOPTS
 IP_PKTINFO
 IP_PKTOPTIONS
 IP_MTU_DISCOVER