Home | History | Annotate | Download | only in tests-m32
      1 /*
      2  * Copyright (c) 2017 JingPiao Chen <chenjingpiao (at) 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 <inttypes.h>
     33 #include "test_nlattr.h"
     34 #include <linux/if.h>
     35 #include <linux/if_arp.h>
     36 #ifdef HAVE_LINUX_IF_LINK_H
     37 # include <linux/if_link.h>
     38 #endif
     39 #include <linux/rtnetlink.h>
     40 
     41 #define IFLA_BRPORT_PRIORITY 2
     42 #define IFLA_BRPORT_MESSAGE_AGE_TIMER 21
     43 
     44 const unsigned int hdrlen = sizeof(struct ifinfomsg);
     45 
     46 static void
     47 init_ifinfomsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
     48 {
     49 	SET_STRUCT(struct nlmsghdr, nlh,
     50 		.nlmsg_len = msg_len,
     51 		.nlmsg_type = RTM_GETLINK,
     52 		.nlmsg_flags = NLM_F_DUMP
     53 	);
     54 
     55 	struct ifinfomsg *const msg = NLMSG_DATA(nlh);
     56 	SET_STRUCT(struct ifinfomsg, msg,
     57 		.ifi_family = AF_UNIX,
     58 		.ifi_type = ARPHRD_LOOPBACK,
     59 		.ifi_index = ifindex_lo(),
     60 		.ifi_flags = IFF_UP,
     61 	);
     62 
     63 	struct nlattr *const nla = NLMSG_ATTR(nlh, sizeof(*msg));
     64 	SET_STRUCT(struct nlattr, nla,
     65 		.nla_len = msg_len - NLMSG_SPACE(hdrlen),
     66 		.nla_type = IFLA_PROTINFO
     67 	);
     68 }
     69 
     70 static void
     71 print_ifinfomsg(const unsigned int msg_len)
     72 {
     73 	printf("{len=%u, type=RTM_GETLINK, flags=NLM_F_DUMP"
     74 	       ", seq=0, pid=0}, {ifi_family=AF_UNIX"
     75 	       ", ifi_type=ARPHRD_LOOPBACK"
     76 	       ", ifi_index=" IFINDEX_LO_STR
     77 	       ", ifi_flags=IFF_UP, ifi_change=0}"
     78 	       ", {{nla_len=%u, nla_type=IFLA_PROTINFO}",
     79 	       msg_len, msg_len - NLMSG_SPACE(hdrlen));
     80 }
     81 
     82 int
     83 main(void)
     84 {
     85 	skip_if_unavailable("/proc/self/fd/");
     86 
     87 	const int fd = create_nl_socket(NETLINK_ROUTE);
     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 	const uint16_t u16 = 0xabcd;
     94 	TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
     95 				  init_ifinfomsg, print_ifinfomsg,
     96 				  IFLA_BRPORT_PRIORITY, pattern, u16,
     97 				  printf("%u", u16));
     98 
     99 	const uint64_t u64 = 0xabcdedeeefeafeab;
    100 	TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
    101 				  init_ifinfomsg, print_ifinfomsg,
    102 				  IFLA_BRPORT_MESSAGE_AGE_TIMER, pattern, u64,
    103 				  printf("%" PRIu64, u64));
    104 
    105 #ifdef HAVE_STRUCT_IFLA_BRIDGE_ID
    106 	static const struct ifla_bridge_id id = {
    107 		.prio = { 0xab, 0xcd },
    108 		.addr = { 0xab, 0xcd, 0xef, 0xac, 0xbc, 0xcd }
    109 	};
    110 	TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
    111 				  init_ifinfomsg, print_ifinfomsg,
    112 				  IFLA_BRPORT_ROOT_ID, pattern, id,
    113 				  printf("{prio=[%u, %u]"
    114 					 ", addr=%02x:%02x:%02x:%02x:%02x:%02x}",
    115 					 id.prio[0], id.prio[1],
    116 					 id.addr[0], id.addr[1], id.addr[2],
    117 					 id.addr[3], id.addr[4], id.addr[5]));
    118 #endif
    119 
    120 	puts("+++ exited with 0 +++");
    121 	return 0;
    122 }
    123