Home | History | Annotate | Download | only in tests-mx32
      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 #ifdef HAVE_LINUX_FIB_RULES_H
     32 
     33 # include <stdio.h>
     34 # include <inttypes.h>
     35 # include "test_nlattr.h"
     36 # include <linux/fib_rules.h>
     37 # include <linux/ip.h>
     38 # include <linux/rtnetlink.h>
     39 
     40 #define FRA_TUN_ID 12
     41 #define FRA_TABLE 15
     42 #define FRA_UID_RANGE 20
     43 
     44 static void
     45 init_rtmsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
     46 {
     47 	SET_STRUCT(struct nlmsghdr, nlh,
     48 		.nlmsg_len = msg_len,
     49 		.nlmsg_type = RTM_GETRULE,
     50 		.nlmsg_flags = NLM_F_DUMP
     51 	);
     52 
     53 	struct rtmsg *const msg = NLMSG_DATA(nlh);
     54 	SET_STRUCT(struct rtmsg, msg,
     55 		.rtm_family = AF_UNIX,
     56 		.rtm_tos = IPTOS_LOWDELAY,
     57 		.rtm_table = RT_TABLE_UNSPEC,
     58 		.rtm_type = FR_ACT_TO_TBL,
     59 		.rtm_flags = FIB_RULE_INVERT
     60 	);
     61 }
     62 
     63 static void
     64 print_rtmsg(const unsigned int msg_len)
     65 {
     66 	printf("{len=%u, type=RTM_GETRULE, flags=NLM_F_DUMP"
     67 	       ", seq=0, pid=0}, {family=AF_UNIX"
     68 	       ", dst_len=0, src_len=0"
     69 	       ", tos=IPTOS_LOWDELAY"
     70 	       ", table=RT_TABLE_UNSPEC"
     71 	       ", action=FR_ACT_TO_TBL"
     72 	       ", flags=FIB_RULE_INVERT}",
     73 	       msg_len);
     74 }
     75 
     76 int
     77 main(void)
     78 {
     79 	skip_if_unavailable("/proc/self/fd/");
     80 
     81 	const int fd = create_nl_socket(NETLINK_ROUTE);
     82 	const unsigned int hdrlen = sizeof(struct rtmsg);
     83 	void *nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
     84 
     85 	static char pattern[4096];
     86 	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
     87 
     88 	const unsigned int nla_type = 0xffff & NLA_TYPE_MASK;
     89 	char nla_type_str[256];
     90 	sprintf(nla_type_str, "%#x /* FRA_??? */", nla_type);
     91 	TEST_NLATTR_(fd, nlh0, hdrlen,
     92 		     init_rtmsg, print_rtmsg,
     93 		     nla_type, nla_type_str,
     94 		     4, pattern, 4,
     95 		     print_quoted_hex(pattern, 4));
     96 
     97 	TEST_NLATTR(fd, nlh0, hdrlen,
     98 		    init_rtmsg, print_rtmsg,
     99 		    FRA_DST, 4, pattern, 4,
    100 		    print_quoted_hex(pattern, 4));
    101 
    102 	const uint32_t table_id = RT_TABLE_DEFAULT;
    103 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
    104 			   init_rtmsg, print_rtmsg,
    105 			   FRA_TABLE, pattern, table_id,
    106 			   printf("RT_TABLE_DEFAULT"));
    107 
    108 #ifdef HAVE_STRUCT_FIB_RULE_UID_RANGE
    109 	static const struct fib_rule_uid_range range = {
    110 		.start = 0xabcdedad,
    111 		.end = 0xbcdeadba
    112 	};
    113 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
    114 			   init_rtmsg, print_rtmsg,
    115 			   FRA_UID_RANGE, pattern, range,
    116 			   PRINT_FIELD_U("{", range, start);
    117 			   PRINT_FIELD_U(", ", range, end);
    118 			   printf("}"));
    119 #endif
    120 #if defined HAVE_BE64TOH || defined be64toh
    121 	const uint64_t tun_id = 0xabcdcdbeedabadef;
    122 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
    123 			   init_rtmsg, print_rtmsg,
    124 			   FRA_TUN_ID, pattern, tun_id,
    125 			   printf("htobe64(%" PRIu64 ")", be64toh(tun_id)));
    126 #endif
    127 
    128 	puts("+++ exited with 0 +++");
    129 	return 0;
    130 }
    131 
    132 #else
    133 
    134 SKIP_MAIN_UNDEFINED("HAVE_LINUX_FIB_RULES_H")
    135 
    136 #endif
    137