• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
3  * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
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 "defs.h"
30 #include <sys/socket.h>
31 #include <linux/netlink.h>
32 #include "xlat/netlink_flags.h"
33 #include "xlat/netlink_types.h"
34 
35 /*
36  * Fetch a struct nlmsghdr from the given address.
37  */
38 static bool
fetch_nlmsghdr(struct tcb * const tcp,struct nlmsghdr * const nlmsghdr,const kernel_ulong_t addr,const kernel_ulong_t len)39 fetch_nlmsghdr(struct tcb *const tcp, struct nlmsghdr *const nlmsghdr,
40 	       const kernel_ulong_t addr, const kernel_ulong_t len)
41 {
42 	if (len < sizeof(struct nlmsghdr)) {
43 		printstrn(tcp, addr, len);
44 		return false;
45 	}
46 
47 	if (umove_or_printaddr(tcp, addr, nlmsghdr))
48 		return false;
49 
50 	return true;
51 }
52 
53 static void
print_nlmsghdr(struct tcb * tcp,const struct nlmsghdr * const nlmsghdr)54 print_nlmsghdr(struct tcb *tcp, const struct nlmsghdr *const nlmsghdr)
55 {
56 	/* print the whole structure regardless of its nlmsg_len */
57 
58 	tprintf("{len=%u, type=", nlmsghdr->nlmsg_len);
59 
60 	printxval(netlink_types, nlmsghdr->nlmsg_type, "NLMSG_???");
61 
62 	tprints(", flags=");
63 	printflags(netlink_flags, nlmsghdr->nlmsg_flags, "NLM_F_???");
64 
65 	tprintf(", seq=%u, pid=%u}", nlmsghdr->nlmsg_seq,
66 		nlmsghdr->nlmsg_pid);
67 }
68 
69 static void
decode_nlmsghdr_with_payload(struct tcb * const tcp,const struct nlmsghdr * const nlmsghdr,const kernel_ulong_t addr,const kernel_ulong_t len)70 decode_nlmsghdr_with_payload(struct tcb *const tcp,
71 			     const struct nlmsghdr *const nlmsghdr,
72 			     const kernel_ulong_t addr,
73 			     const kernel_ulong_t len)
74 {
75 	tprints("{");
76 
77 	print_nlmsghdr(tcp, nlmsghdr);
78 
79 	unsigned int nlmsg_len =
80 		nlmsghdr->nlmsg_len > len ? len : nlmsghdr->nlmsg_len;
81 	if (nlmsg_len > sizeof(struct nlmsghdr)) {
82 		tprints(", ");
83 
84 		printstrn(tcp, addr + sizeof(struct nlmsghdr),
85 			  nlmsg_len - sizeof(struct nlmsghdr));
86 	}
87 
88 	tprints("}");
89 }
90 
91 void
decode_netlink(struct tcb * const tcp,kernel_ulong_t addr,kernel_ulong_t len)92 decode_netlink(struct tcb *const tcp, kernel_ulong_t addr, kernel_ulong_t len)
93 {
94 	struct nlmsghdr nlmsghdr;
95 	bool print_array = false;
96 	unsigned int elt;
97 
98 	for (elt = 0; fetch_nlmsghdr(tcp, &nlmsghdr, addr, len); elt++) {
99 		if (abbrev(tcp) && elt == max_strlen) {
100 			tprints("...");
101 			break;
102 		}
103 
104 		unsigned int nlmsg_len = NLMSG_ALIGN(nlmsghdr.nlmsg_len);
105 		kernel_ulong_t next_addr = 0;
106 		kernel_ulong_t next_len = 0;
107 
108 		if (nlmsghdr.nlmsg_len >= sizeof(struct nlmsghdr)) {
109 			next_len = (len >= nlmsg_len) ? len - nlmsg_len : 0;
110 
111 			if (next_len && addr + nlmsg_len > addr)
112 				next_addr = addr + nlmsg_len;
113 		}
114 
115 		if (!print_array && next_addr) {
116 			tprints("[");
117 			print_array = true;
118 		}
119 
120 		decode_nlmsghdr_with_payload(tcp, &nlmsghdr, addr, len);
121 
122 		if (!next_addr)
123 			break;
124 
125 		tprints(", ");
126 		addr = next_addr;
127 		len = next_len;
128 	}
129 
130 	if (print_array) {
131 		tprints("]");
132 	}
133 }
134