1 /* Copyright (c) 2013, The TCPDUMP project 2 * All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * 7 * 1. Redistributions of source code must retain the above copyright notice, this 8 * list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright notice, 10 * this list of conditions and the following disclaimer in the documentation 11 * and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 17 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 20 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 25 #include <sys/cdefs.h> 26 #ifndef lint 27 __RCSID("$NetBSD: print-m3ua.c,v 1.8 2024/09/02 16:15:32 christos Exp $"); 28 #endif 29 30 /* \summary: Message Transfer Part 3 (MTP3) User Adaptation Layer (M3UA) printer */ 31 32 /* RFC 4666 */ 33 34 #include <config.h> 35 36 #include "netdissect-stdinc.h" 37 38 #define ND_LONGJMP_FROM_TCHECK 39 #include "netdissect.h" 40 #include "extract.h" 41 42 43 #define M3UA_REL_1_0 1 44 45 struct m3ua_common_header { 46 nd_uint8_t v; 47 nd_uint8_t reserved; 48 nd_uint8_t msg_class; 49 nd_uint8_t msg_type; 50 nd_uint32_t len; 51 }; 52 53 struct m3ua_param_header { 54 nd_uint16_t tag; 55 nd_uint16_t len; 56 }; 57 58 /* message classes */ 59 #define M3UA_MSGC_MGMT 0 60 #define M3UA_MSGC_TRANSFER 1 61 #define M3UA_MSGC_SSNM 2 62 #define M3UA_MSGC_ASPSM 3 63 #define M3UA_MSGC_ASPTM 4 64 /* reserved values */ 65 #define M3UA_MSGC_RKM 9 66 67 static const struct tok MessageClasses[] = { 68 { M3UA_MSGC_MGMT, "Management" }, 69 { M3UA_MSGC_TRANSFER, "Transfer" }, 70 { M3UA_MSGC_SSNM, "SS7" }, 71 { M3UA_MSGC_ASPSM, "ASP" }, 72 { M3UA_MSGC_ASPTM, "ASP" }, 73 { M3UA_MSGC_RKM, "Routing Key Management"}, 74 { 0, NULL } 75 }; 76 77 /* management messages */ 78 #define M3UA_MGMT_ERROR 0 79 #define M3UA_MGMT_NOTIFY 1 80 81 static const struct tok MgmtMessages[] = { 82 { M3UA_MGMT_ERROR, "Error" }, 83 { M3UA_MGMT_NOTIFY, "Notify" }, 84 { 0, NULL } 85 }; 86 87 /* transfer messages */ 88 #define M3UA_TRANSFER_DATA 1 89 90 static const struct tok TransferMessages[] = { 91 { M3UA_TRANSFER_DATA, "Data" }, 92 { 0, NULL } 93 }; 94 95 /* SS7 Signaling Network Management messages */ 96 #define M3UA_SSNM_DUNA 1 97 #define M3UA_SSNM_DAVA 2 98 #define M3UA_SSNM_DAUD 3 99 #define M3UA_SSNM_SCON 4 100 #define M3UA_SSNM_DUPU 5 101 #define M3UA_SSNM_DRST 6 102 103 static const struct tok SS7Messages[] = { 104 { M3UA_SSNM_DUNA, "Destination Unavailable" }, 105 { M3UA_SSNM_DAVA, "Destination Available" }, 106 { M3UA_SSNM_DAUD, "Destination State Audit" }, 107 { M3UA_SSNM_SCON, "Signalling Congestion" }, 108 { M3UA_SSNM_DUPU, "Destination User Part Unavailable" }, 109 { M3UA_SSNM_DRST, "Destination Restricted" }, 110 { 0, NULL } 111 }; 112 113 /* ASP State Maintenance messages */ 114 #define M3UA_ASP_UP 1 115 #define M3UA_ASP_DN 2 116 #define M3UA_ASP_BEAT 3 117 #define M3UA_ASP_UP_ACK 4 118 #define M3UA_ASP_DN_ACK 5 119 #define M3UA_ASP_BEAT_ACK 6 120 121 static const struct tok ASPStateMessages[] = { 122 { M3UA_ASP_UP, "Up" }, 123 { M3UA_ASP_DN, "Down" }, 124 { M3UA_ASP_BEAT, "Heartbeat" }, 125 { M3UA_ASP_UP_ACK, "Up Acknowledgement" }, 126 { M3UA_ASP_DN_ACK, "Down Acknowledgement" }, 127 { M3UA_ASP_BEAT_ACK, "Heartbeat Acknowledgement" }, 128 { 0, NULL } 129 }; 130 131 /* ASP Traffic Maintenance messages */ 132 #define M3UA_ASP_AC 1 133 #define M3UA_ASP_IA 2 134 #define M3UA_ASP_AC_ACK 3 135 #define M3UA_ASP_IA_ACK 4 136 137 static const struct tok ASPTrafficMessages[] = { 138 { M3UA_ASP_AC, "Active" }, 139 { M3UA_ASP_IA, "Inactive" }, 140 { M3UA_ASP_AC_ACK, "Active Acknowledgement" }, 141 { M3UA_ASP_IA_ACK, "Inactive Acknowledgement" }, 142 { 0, NULL } 143 }; 144 145 /* Routing Key Management messages */ 146 #define M3UA_RKM_REQ 1 147 #define M3UA_RKM_RSP 2 148 #define M3UA_RKM_DEREQ 3 149 #define M3UA_RKM_DERSP 4 150 151 static const struct tok RoutingKeyMgmtMessages[] = { 152 { M3UA_RKM_REQ, "Registration Request" }, 153 { M3UA_RKM_RSP, "Registration Response" }, 154 { M3UA_RKM_DEREQ, "Deregistration Request" }, 155 { M3UA_RKM_DERSP, "Deregistration Response" }, 156 { 0, NULL } 157 }; 158 159 static const struct uint_tokary m3ua_msgc2tokary[] = { 160 { M3UA_MSGC_MGMT, MgmtMessages }, 161 { M3UA_MSGC_TRANSFER, TransferMessages }, 162 { M3UA_MSGC_SSNM, SS7Messages }, 163 { M3UA_MSGC_ASPSM, ASPStateMessages }, 164 { M3UA_MSGC_ASPTM, ASPTrafficMessages }, 165 { M3UA_MSGC_RKM, RoutingKeyMgmtMessages }, 166 /* uint2tokary() does not use array termination. */ 167 }; 168 169 /* M3UA Parameters */ 170 #define M3UA_PARAM_INFO 0x0004 171 #define M3UA_PARAM_ROUTING_CTX 0x0006 172 #define M3UA_PARAM_DIAGNOSTIC 0x0007 173 #define M3UA_PARAM_HB_DATA 0x0009 174 #define M3UA_PARAM_TRAFFIC_MODE_TYPE 0x000b 175 #define M3UA_PARAM_ERROR_CODE 0x000c 176 #define M3UA_PARAM_STATUS 0x000d 177 #define M3UA_PARAM_ASP_ID 0x0011 178 #define M3UA_PARAM_AFFECTED_POINT_CODE 0x0012 179 #define M3UA_PARAM_CORR_ID 0x0013 180 181 #define M3UA_PARAM_NETWORK_APPEARANCE 0x0200 182 #define M3UA_PARAM_USER 0x0204 183 #define M3UA_PARAM_CONGESTION_INDICATION 0x0205 184 #define M3UA_PARAM_CONCERNED_DST 0x0206 185 #define M3UA_PARAM_ROUTING_KEY 0x0207 186 #define M3UA_PARAM_REG_RESULT 0x0208 187 #define M3UA_PARAM_DEREG_RESULT 0x0209 188 #define M3UA_PARAM_LOCAL_ROUTING_KEY_ID 0x020a 189 #define M3UA_PARAM_DST_POINT_CODE 0x020b 190 #define M3UA_PARAM_SI 0x020c 191 #define M3UA_PARAM_ORIGIN_POINT_CODE_LIST 0x020e 192 #define M3UA_PARAM_PROTO_DATA 0x0210 193 #define M3UA_PARAM_REG_STATUS 0x0212 194 #define M3UA_PARAM_DEREG_STATUS 0x0213 195 196 static const struct tok ParamName[] = { 197 { M3UA_PARAM_INFO, "INFO String" }, 198 { M3UA_PARAM_ROUTING_CTX, "Routing Context" }, 199 { M3UA_PARAM_DIAGNOSTIC, "Diagnostic Info" }, 200 { M3UA_PARAM_HB_DATA, "Heartbeat Data" }, 201 { M3UA_PARAM_TRAFFIC_MODE_TYPE, "Traffic Mode Type" }, 202 { M3UA_PARAM_ERROR_CODE, "Error Code" }, 203 { M3UA_PARAM_STATUS, "Status" }, 204 { M3UA_PARAM_ASP_ID, "ASP Identifier" }, 205 { M3UA_PARAM_AFFECTED_POINT_CODE, "Affected Point Code" }, 206 { M3UA_PARAM_CORR_ID, "Correlation ID" }, 207 { M3UA_PARAM_NETWORK_APPEARANCE, "Network Appearance" }, 208 { M3UA_PARAM_USER, "User/Cause" }, 209 { M3UA_PARAM_CONGESTION_INDICATION, "Congestion Indications" }, 210 { M3UA_PARAM_CONCERNED_DST, "Concerned Destination" }, 211 { M3UA_PARAM_ROUTING_KEY, "Routing Key" }, 212 { M3UA_PARAM_REG_RESULT, "Registration Result" }, 213 { M3UA_PARAM_DEREG_RESULT, "Deregistration Result" }, 214 { M3UA_PARAM_LOCAL_ROUTING_KEY_ID, "Local Routing Key Identifier" }, 215 { M3UA_PARAM_DST_POINT_CODE, "Destination Point Code" }, 216 { M3UA_PARAM_SI, "Service Indicators" }, 217 { M3UA_PARAM_ORIGIN_POINT_CODE_LIST, "Originating Point Code List" }, 218 { M3UA_PARAM_PROTO_DATA, "Protocol Data" }, 219 { M3UA_PARAM_REG_STATUS, "Registration Status" }, 220 { M3UA_PARAM_DEREG_STATUS, "Deregistration Status" }, 221 { 0, NULL } 222 }; 223 224 static void 225 tag_value_print(netdissect_options *ndo, 226 const u_char *buf, const uint16_t tag, const uint16_t size) 227 { 228 switch (tag) { 229 case M3UA_PARAM_NETWORK_APPEARANCE: 230 case M3UA_PARAM_ROUTING_CTX: 231 case M3UA_PARAM_CORR_ID: 232 /* buf and size don't include the header */ 233 if (size < 4) 234 goto invalid; 235 ND_PRINT("0x%08x", GET_BE_U_4(buf)); 236 break; 237 /* ... */ 238 default: 239 ND_PRINT("(length %zu)", size + sizeof(struct m3ua_param_header)); 240 } 241 ND_TCHECK_LEN(buf, size); 242 return; 243 244 invalid: 245 nd_print_invalid(ndo); 246 ND_TCHECK_LEN(buf, size); 247 } 248 249 /* 250 * 0 1 2 3 251 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 252 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 253 * | Parameter Tag | Parameter Length | 254 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 255 * \ \ 256 * / Parameter Value / 257 * \ \ 258 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 259 */ 260 static void 261 m3ua_tags_print(netdissect_options *ndo, 262 const u_char *buf, const u_int size) 263 { 264 const u_char *p = buf; 265 int align; 266 uint16_t hdr_tag; 267 uint16_t hdr_len; 268 269 while (p < buf + size) { 270 if (p + sizeof(struct m3ua_param_header) > buf + size) 271 goto invalid; 272 /* Parameter Tag */ 273 hdr_tag = GET_BE_U_2(p); 274 ND_PRINT("\n\t\t\t%s: ", tok2str(ParamName, "Unknown Parameter (0x%04x)", hdr_tag)); 275 /* Parameter Length */ 276 hdr_len = GET_BE_U_2(p + 2); 277 if (hdr_len < sizeof(struct m3ua_param_header)) 278 goto invalid; 279 /* Parameter Value */ 280 align = (p + hdr_len - buf) % 4; 281 align = align ? 4 - align : 0; 282 ND_TCHECK_LEN(p, hdr_len + align); 283 tag_value_print(ndo, p, hdr_tag, hdr_len - sizeof(struct m3ua_param_header)); 284 p += hdr_len + align; 285 } 286 return; 287 288 invalid: 289 nd_print_invalid(ndo); 290 ND_TCHECK_LEN(buf, size); 291 } 292 293 /* 294 * 0 1 2 3 295 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 296 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 297 * | Version | Reserved | Message Class | Message Type | 298 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 299 * | Message Length | 300 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 301 * \ \ 302 * / / 303 */ 304 UNALIGNED_OK 305 void 306 m3ua_print(netdissect_options *ndo, 307 const u_char *buf, const u_int size) 308 { 309 const struct m3ua_common_header *hdr = (const struct m3ua_common_header *) buf; 310 const struct tok *dict; 311 uint8_t msg_class; 312 313 ndo->ndo_protocol = "m3ua"; 314 /* size includes the header */ 315 if (size < sizeof(struct m3ua_common_header)) 316 goto invalid; 317 ND_TCHECK_SIZE(hdr); 318 if (GET_U_1(hdr->v) != M3UA_REL_1_0) 319 return; 320 321 msg_class = GET_U_1(hdr->msg_class); 322 dict = uint2tokary(m3ua_msgc2tokary, msg_class); 323 324 ND_PRINT("\n\t\t%s", tok2str(MessageClasses, "Unknown message class %i", msg_class)); 325 if (dict != NULL) 326 ND_PRINT(" %s Message", 327 tok2str(dict, "Unknown (0x%02x)", GET_U_1(hdr->msg_type))); 328 329 if (size != GET_BE_U_4(hdr->len)) 330 ND_PRINT("\n\t\t\t@@@@@@ Corrupted length %u of message @@@@@@", 331 GET_BE_U_4(hdr->len)); 332 else 333 m3ua_tags_print(ndo, buf + sizeof(struct m3ua_common_header), 334 GET_BE_U_4(hdr->len) - sizeof(struct m3ua_common_header)); 335 return; 336 337 invalid: 338 nd_print_invalid(ndo); 339 ND_TCHECK_LEN(buf, size); 340 } 341