]> granicus.if.org Git - strace/blob - tests/netlink_inet_diag.c
Robustify netlink response parsers
[strace] / tests / netlink_inet_diag.c
1 /*
2  * This file is part of inet-yy strace test.
3  *
4  * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "tests.h"
31 #include <assert.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <netinet/in.h>
36 #include <linux/netlink.h>
37 #include <linux/sock_diag.h>
38 #include <linux/inet_diag.h>
39
40 static void
41 send_query(const int fd, const int family, const int proto)
42 {
43         struct sockaddr_nl nladdr = {
44                 .nl_family = AF_NETLINK
45         };
46         struct {
47                 struct nlmsghdr nlh;
48                 struct inet_diag_req_v2 idr;
49         } req = {
50                 .nlh = {
51                         .nlmsg_len = sizeof(req),
52                         .nlmsg_type = SOCK_DIAG_BY_FAMILY,
53                         .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
54                 },
55                 .idr = {
56                         .sdiag_family = family,
57                         .sdiag_protocol = proto,
58                         .idiag_states = -1
59                 }
60         };
61         struct iovec iov = {
62                 .iov_base = &req,
63                 .iov_len = sizeof(req)
64         };
65         struct msghdr msg = {
66                 .msg_name = (void *) &nladdr,
67                 .msg_namelen = sizeof(nladdr),
68                 .msg_iov = &iov,
69                 .msg_iovlen = 1
70         };
71
72         if (sendmsg(fd, &msg, 0) <= 0)
73                 perror_msg_and_skip("sendmsg");
74 }
75
76 static void
77 check_responses(const int fd)
78 {
79         static char buf[8192];
80         struct sockaddr_nl nladdr = {
81                 .nl_family = AF_NETLINK
82         };
83         struct iovec iov = {
84                 .iov_base = buf,
85                 .iov_len = sizeof(buf)
86         };
87         struct msghdr msg = {
88                 .msg_name = (void *) &nladdr,
89                 .msg_namelen = sizeof(nladdr),
90                 .msg_iov = &iov,
91                 .msg_iovlen = 1
92         };
93
94         ssize_t ret = recvmsg(fd, &msg, 0);
95         if (ret <= 0)
96                 perror_msg_and_skip("recvmsg");
97
98         struct nlmsghdr *h = (struct nlmsghdr *) buf;
99         if (!NLMSG_OK(h, ret))
100                 error_msg_and_skip("!NLMSG_OK");
101         if (h->nlmsg_type == NLMSG_ERROR) {
102                 const struct nlmsgerr *err = NLMSG_DATA(h);
103                 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))
104                         error_msg_and_skip("NLMSG_ERROR");
105                 errno = -err->error;
106                 perror_msg_and_skip("NLMSG_ERROR");
107         }
108         if (h->nlmsg_type != SOCK_DIAG_BY_FAMILY)
109                 error_msg_and_skip("unexpected nlmsg_type %u",
110                                    (unsigned) h->nlmsg_type);
111
112         const struct inet_diag_msg *diag = NLMSG_DATA(h);
113         if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*diag)))
114                 error_msg_and_skip("short response");
115 }
116
117 int main(void)
118 {
119         struct sockaddr_in addr;
120         socklen_t len = sizeof(addr);
121
122         memset(&addr, 0, sizeof(addr));
123         addr.sin_family = AF_INET;
124         addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
125
126         close(0);
127         close(1);
128
129         if (socket(PF_INET, SOCK_STREAM, 0))
130                 perror_msg_and_skip("socket PF_INET");
131         if (bind(0, (struct sockaddr *) &addr, len))
132                 perror_msg_and_skip("bind");
133         if (listen(0, 5))
134                 perror_msg_and_skip("listen");
135         if (socket(AF_NETLINK, SOCK_RAW, NETLINK_INET_DIAG) != 1)
136                 perror_msg_and_skip("socket AF_NETLINK");
137
138         send_query(1, AF_INET, IPPROTO_TCP);
139         check_responses(1);
140         return 0;
141 }