1 /* 2 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com> 3 * Copyright (c) 2017 The strace developers. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "tests.h" 30 31 #include <stdio.h> 32 #include <arpa/inet.h> 33 #include "test_nlattr.h" 34 #ifdef HAVE_LINUX_IF_ADDR_H 35 # include <linux/if_addr.h> 36 #endif 37 #include <linux/rtnetlink.h> 38 39 #define IFA_FLAGS 8 40 41 #define SET_IFA_FAMILY(af) \ 42 do { \ 43 ifa_family = af; \ 44 ifa_family_str = #af; \ 45 } \ 46 while (0) 47 48 uint8_t ifa_family; 49 const char *ifa_family_str; 50 51 static void 52 init_ifaddrmsg(struct nlmsghdr *const nlh, const unsigned int msg_len) 53 { 54 SET_STRUCT(struct nlmsghdr, nlh, 55 .nlmsg_len = msg_len, 56 .nlmsg_type = RTM_GETADDR, 57 .nlmsg_flags = NLM_F_DUMP 58 ); 59 60 struct ifaddrmsg *const msg = NLMSG_DATA(nlh); 61 SET_STRUCT(struct ifaddrmsg, msg, 62 .ifa_family = ifa_family, 63 .ifa_flags = IFA_F_SECONDARY, 64 .ifa_scope = RT_SCOPE_UNIVERSE, 65 .ifa_index = ifindex_lo() 66 ); 67 } 68 69 static void 70 print_ifaddrmsg(const unsigned int msg_len) 71 { 72 printf("{len=%u, type=RTM_GETADDR, flags=NLM_F_DUMP" 73 ", seq=0, pid=0}, {ifa_family=%s" 74 ", ifa_prefixlen=0" 75 ", ifa_flags=IFA_F_SECONDARY" 76 ", ifa_scope=RT_SCOPE_UNIVERSE" 77 ", ifa_index=" IFINDEX_LO_STR "}", 78 msg_len, ifa_family_str); 79 } 80 81 int 82 main(void) 83 { 84 skip_if_unavailable("/proc/self/fd/"); 85 86 const int fd = create_nl_socket(NETLINK_ROUTE); 87 const unsigned int hdrlen = sizeof(struct ifaddrmsg); 88 void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen)); 89 90 static char pattern[4096]; 91 fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1); 92 93 SET_IFA_FAMILY(AF_UNSPEC); 94 const unsigned int nla_type = 0xffff & NLA_TYPE_MASK; 95 char nla_type_str[256]; 96 sprintf(nla_type_str, "%#x /* IFA_??? */", nla_type); 97 TEST_NLATTR_(fd, nlh0, hdrlen, 98 init_ifaddrmsg, print_ifaddrmsg, 99 nla_type, nla_type_str, 100 4, pattern, 4, 101 print_quoted_hex(pattern, 4)); 102 103 TEST_NLATTR(fd, nlh0, hdrlen, 104 init_ifaddrmsg, print_ifaddrmsg, 105 IFA_ADDRESS, 4, pattern, 4, 106 print_quoted_hex(pattern, 4)); 107 108 SET_IFA_FAMILY(AF_INET); 109 static const char address4[] = "12.34.56.78"; 110 struct in_addr a4; 111 112 if (!inet_pton(AF_INET, address4, &a4)) 113 perror_msg_and_skip("inet_pton"); 114 115 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, 116 init_ifaddrmsg, print_ifaddrmsg, 117 IFA_ADDRESS, pattern, a4, 118 printf("%s", address4)); 119 120 SET_IFA_FAMILY(AF_INET6); 121 static const char address6[] = "12:34:56:78:90:ab:cd:ef"; 122 struct in6_addr a6; 123 124 if (!inet_pton(AF_INET6, address6, &a6)) 125 perror_msg_and_skip("inet_pton"); 126 127 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, 128 init_ifaddrmsg, print_ifaddrmsg, 129 IFA_ADDRESS, pattern, a6, 130 printf("%s", address6)); 131 132 static const struct ifa_cacheinfo ci = { 133 .ifa_prefered = 0xabcdefac, 134 .ifa_valid = 0xbcdadbca, 135 .cstamp = 0xcdabedba, 136 .tstamp = 0xdebabdac 137 }; 138 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, 139 init_ifaddrmsg, print_ifaddrmsg, 140 IFA_CACHEINFO, pattern, ci, 141 PRINT_FIELD_U("{", ci, ifa_prefered); 142 PRINT_FIELD_U(", ", ci, ifa_valid); 143 PRINT_FIELD_U(", ", ci, cstamp); 144 PRINT_FIELD_U(", ", ci, tstamp); 145 printf("}")); 146 147 const uint32_t ifa_flags = IFA_F_SECONDARY | IFA_F_PERMANENT; 148 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen, 149 init_ifaddrmsg, print_ifaddrmsg, 150 IFA_FLAGS, pattern, ifa_flags, 151 printf("IFA_F_SECONDARY|IFA_F_PERMANENT")); 152 153 puts("+++ exited with 0 +++"); 154 return 0; 155 } 156