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