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) 2017-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 #include "netlink.h" 32 #include "netlink_sock_diag.h" 33 #include "nlattr.h" 34 #include "print_fields.h" 35 36 #include <linux/sock_diag.h> 37 #include <linux/unix_diag.h> 38 39 #include "xlat/unix_diag_attrs.h" 40 #include "xlat/unix_diag_show.h" 41 42 DECL_NETLINK_DIAG_DECODER(decode_unix_diag_req) 43 { 44 struct unix_diag_req req = { .sdiag_family = family }; 45 const size_t offset = sizeof(req.sdiag_family); 46 47 PRINT_FIELD_XVAL("{", req, sdiag_family, addrfams, "AF_???"); 48 tprints(", "); 49 if (len >= sizeof(req)) { 50 if (!umoven_or_printaddr(tcp, addr + offset, 51 sizeof(req) - offset, 52 (char *) &req + offset)) { 53 PRINT_FIELD_U("", req, sdiag_protocol); 54 PRINT_FIELD_FLAGS(", ", req, udiag_states, 55 tcp_state_flags, "1<<TCP_???"); 56 PRINT_FIELD_U(", ", req, udiag_ino); 57 PRINT_FIELD_FLAGS(", ", req, udiag_show, 58 unix_diag_show, "UDIAG_SHOW_???"); 59 PRINT_FIELD_COOKIE(", ", req, udiag_cookie); 60 } 61 } else 62 tprints("..."); 63 tprints("}"); 64 } 65 66 static bool 67 decode_unix_diag_vfs(struct tcb *const tcp, 68 const kernel_ulong_t addr, 69 const unsigned int len, 70 const void *const opaque_data) 71 { 72 struct unix_diag_vfs uv; 73 74 if (len < sizeof(uv)) 75 return false; 76 if (umove_or_printaddr(tcp, addr, &uv)) 77 return true; 78 79 PRINT_FIELD_DEV("{", uv, udiag_vfs_dev); 80 PRINT_FIELD_U(", ", uv, udiag_vfs_ino); 81 tprints("}"); 82 83 return true; 84 } 85 86 static bool 87 print_inode(struct tcb *const tcp, 88 void *const elem_buf, 89 const size_t elem_size, 90 void *const opaque_data) 91 { 92 tprintf("%" PRIu32, *(uint32_t *) elem_buf); 93 94 return true; 95 } 96 97 static bool 98 decode_unix_diag_inode(struct tcb *const tcp, 99 const kernel_ulong_t addr, 100 const unsigned int len, 101 const void *const opaque_data) 102 { 103 uint32_t inode; 104 const size_t nmemb = len / sizeof(inode); 105 106 if (!nmemb) 107 return false; 108 109 print_array(tcp, addr, nmemb, &inode, sizeof(inode), 110 tfetch_mem, print_inode, 0); 111 112 return true; 113 } 114 115 static bool 116 decode_unix_diag_rqlen(struct tcb *const tcp, 117 const kernel_ulong_t addr, 118 const unsigned int len, 119 const void *const opaque_data) 120 { 121 struct unix_diag_rqlen rql; 122 123 if (len < sizeof(rql)) 124 return false; 125 if (umove_or_printaddr(tcp, addr, &rql)) 126 return true; 127 128 PRINT_FIELD_U("{", rql, udiag_rqueue); 129 PRINT_FIELD_U(", ", rql, udiag_wqueue); 130 tprints("}"); 131 132 return true; 133 } 134 135 static const nla_decoder_t unix_diag_msg_nla_decoders[] = { 136 [UNIX_DIAG_NAME] = decode_nla_str, 137 [UNIX_DIAG_VFS] = decode_unix_diag_vfs, 138 [UNIX_DIAG_PEER] = decode_nla_u32, 139 [UNIX_DIAG_ICONS] = decode_unix_diag_inode, 140 [UNIX_DIAG_RQLEN] = decode_unix_diag_rqlen, 141 [UNIX_DIAG_MEMINFO] = decode_nla_meminfo, 142 [UNIX_DIAG_SHUTDOWN] = decode_nla_u8 143 }; 144 145 DECL_NETLINK_DIAG_DECODER(decode_unix_diag_msg) 146 { 147 struct unix_diag_msg msg = { .udiag_family = family }; 148 size_t offset = sizeof(msg.udiag_family); 149 bool decode_nla = false; 150 151 PRINT_FIELD_XVAL("{", msg, udiag_family, addrfams, "AF_???"); 152 tprints(", "); 153 if (len >= sizeof(msg)) { 154 if (!umoven_or_printaddr(tcp, addr + offset, 155 sizeof(msg) - offset, 156 (char *) &msg + offset)) { 157 PRINT_FIELD_XVAL("", msg, udiag_type, 158 socktypes, "SOCK_???"); 159 PRINT_FIELD_XVAL(", ", msg, udiag_state, 160 tcp_states, "TCP_???"); 161 PRINT_FIELD_U(", ", msg, udiag_ino); 162 PRINT_FIELD_COOKIE(", ", msg, udiag_cookie); 163 decode_nla = true; 164 } 165 } else 166 tprints("..."); 167 tprints("}"); 168 169 offset = NLMSG_ALIGN(sizeof(msg)); 170 if (decode_nla && len > offset) { 171 tprints(", "); 172 decode_nlattr(tcp, addr + offset, len - offset, 173 unix_diag_attrs, "UNIX_DIAG_???", 174 unix_diag_msg_nla_decoders, 175 ARRAY_SIZE(unix_diag_msg_nla_decoders), NULL); 176 } 177 } 178