Home | History | Annotate | Download | only in strace
      1 /*
      2  * Copyright (c) 2016 Fabien Siron <fabien.siron (at) epita.fr>
      3  * Copyright (c) 2017 JingPiao Chen <chenjingpiao (at) gmail.com>
      4  * Copyright (c) 2016-2018 The strace developers.
      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 "defs.h"
     31 
     32 #include "netlink_route.h"
     33 #include "nlattr.h"
     34 #include "print_fields.h"
     35 
     36 #include "netlink.h"
     37 #include <linux/rtnetlink.h>
     38 #ifdef HAVE_LINUX_FIB_RULES_H
     39 # include <linux/fib_rules.h>
     40 #endif
     41 
     42 #include "xlat/fib_rule_actions.h"
     43 #include "xlat/fib_rule_flags.h"
     44 #include "xlat/rtnl_rule_attrs.h"
     45 
     46 static bool
     47 decode_rule_addr(struct tcb *const tcp,
     48 		 const kernel_ulong_t addr,
     49 		 const unsigned int len,
     50 		 const void *const opaque_data)
     51 {
     52 	const struct rtmsg *const rtmsg = opaque_data;
     53 
     54 	decode_inet_addr(tcp, addr, len, rtmsg->rtm_family, NULL);
     55 
     56 	return true;
     57 }
     58 
     59 static bool
     60 decode_fib_rule_uid_range(struct tcb *const tcp,
     61 			  const kernel_ulong_t addr,
     62 			  const unsigned int len,
     63 			  const void *const opaque_data)
     64 {
     65 #ifdef HAVE_STRUCT_FIB_RULE_UID_RANGE
     66 	struct fib_rule_uid_range range;
     67 
     68 	if (len < sizeof(range))
     69 		return false;
     70 	else if (!umove_or_printaddr(tcp, addr, &range)) {
     71 		PRINT_FIELD_U("{", range, start);
     72 		PRINT_FIELD_U(", ", range, end);
     73 		tprints("}");
     74 	}
     75 
     76 	return true;
     77 #else
     78 	return false;
     79 #endif
     80 }
     81 
     82 static const nla_decoder_t fib_rule_hdr_nla_decoders[] = {
     83 	[FRA_DST]			= decode_rule_addr,
     84 	[FRA_SRC]			= decode_rule_addr,
     85 	[FRA_IIFNAME]			= decode_nla_str,
     86 	[FRA_GOTO]			= decode_nla_u32,
     87 	[FRA_PRIORITY]			= decode_nla_u32,
     88 	[FRA_FWMARK]			= decode_nla_u32,
     89 	[FRA_FLOW]			= decode_nla_u32,
     90 	[FRA_TUN_ID]			= decode_nla_be64,
     91 	[FRA_SUPPRESS_IFGROUP]		= decode_nla_u32,
     92 	[FRA_SUPPRESS_PREFIXLEN]	= decode_nla_u32,
     93 	[FRA_TABLE]			= decode_nla_rt_class,
     94 	[FRA_FWMASK]			= decode_nla_u32,
     95 	[FRA_OIFNAME]			= decode_nla_str,
     96 	[FRA_PAD]			= NULL,
     97 	[FRA_L3MDEV]			= decode_nla_u8,
     98 	[FRA_UID_RANGE]			= decode_fib_rule_uid_range
     99 };
    100 
    101 DECL_NETLINK_ROUTE_DECODER(decode_fib_rule_hdr)
    102 {
    103 	/*
    104 	 * struct rtmsg and struct fib_rule_hdr are essentially
    105 	 * the same structure, use struct rtmsg but treat it as
    106 	 * struct fib_rule_hdr.
    107 	 */
    108 	struct rtmsg msg = { .rtm_family = family };
    109 	size_t offset = sizeof(msg.rtm_family);
    110 	bool decode_nla = false;
    111 
    112 	tprints("{family=");
    113 	printxval(addrfams, msg.rtm_family, "AF_???");
    114 
    115 	tprints(", ");
    116 	if (len >= sizeof(msg)) {
    117 		if (!umoven_or_printaddr(tcp, addr + offset,
    118 					 sizeof(msg) - offset,
    119 					 (char *) &msg + offset)) {
    120 			tprintf("dst_len=%u, src_len=%u",
    121 				msg.rtm_dst_len, msg.rtm_src_len);
    122 			tprints(", tos=");
    123 			printflags(ip_type_of_services, msg.rtm_tos,
    124 				   "IPTOS_TOS_???");
    125 			tprints(", table=");
    126 			printxval(routing_table_ids, msg.rtm_table, NULL);
    127 			tprints(", action=");
    128 			printxval(fib_rule_actions, msg.rtm_type, "FR_ACT_???");
    129 			tprints(", flags=");
    130 			printflags(fib_rule_flags, msg.rtm_flags,
    131 				   "FIB_RULE_???");
    132 			decode_nla = true;
    133 		}
    134 	} else
    135 		tprints("...");
    136 	tprints("}");
    137 
    138 	offset = NLMSG_ALIGN(sizeof(msg));
    139 	if (decode_nla && len > offset) {
    140 		tprints(", ");
    141 		decode_nlattr(tcp, addr + offset, len - offset,
    142 			      rtnl_rule_attrs, "FRA_???",
    143 			      fib_rule_hdr_nla_decoders,
    144 			      ARRAY_SIZE(fib_rule_hdr_nla_decoders), &msg);
    145 	}
    146 }
    147