xref: /netbsd-src/external/bsd/tcpdump/dist/print-someip.c (revision c41df9f6167ea7cd2f761f0a97783c8267cb8847)
1*d881c474Schristos /*
2*d881c474Schristos  * Redistribution and use in source and binary forms, with or without
3*d881c474Schristos  * modification, are permitted provided that: (1) source code
4*d881c474Schristos  * distributions retain the above copyright notice and this paragraph
5*d881c474Schristos  * in its entirety, and (2) distributions including binary code include
6*d881c474Schristos  * the above copyright notice and this paragraph in its entirety in
7*d881c474Schristos  * the documentation or other materials provided with the distribution.
8*d881c474Schristos  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
9*d881c474Schristos  * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
10*d881c474Schristos  * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
11*d881c474Schristos  * FOR A PARTICULAR PURPOSE.
12*d881c474Schristos  *
13*d881c474Schristos  * Original code by Francesco Fondelli (francesco dot fondelli, gmail dot com)
14*d881c474Schristos  */
15*d881c474Schristos 
16*d881c474Schristos /* \summary: Autosar SOME/IP Protocol printer */
17*d881c474Schristos 
18*d881c474Schristos #include <config.h>
19*d881c474Schristos 
20*d881c474Schristos #include "netdissect-stdinc.h"
21*d881c474Schristos #include "netdissect.h"
22*d881c474Schristos #include "extract.h"
23*d881c474Schristos 
24*d881c474Schristos /*
25*d881c474Schristos  * SOMEIP Header (R19-11)
26*d881c474Schristos  *
27*d881c474Schristos  *     0                   1                   2                   3
28*d881c474Schristos  *     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
29*d881c474Schristos  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30*d881c474Schristos  *    |               Message ID (Service ID/Method ID)               |
31*d881c474Schristos  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32*d881c474Schristos  *    |                           Length                              |
33*d881c474Schristos  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34*d881c474Schristos  *    |               Request ID (Client ID/Session ID)               |
35*d881c474Schristos  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36*d881c474Schristos  *    | Protocol Ver  | Interface Ver | Message Type  |  Return Code  |
37*d881c474Schristos  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38*d881c474Schristos  *    |                            Payload                            |
39*d881c474Schristos  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40*d881c474Schristos  */
41*d881c474Schristos 
42*d881c474Schristos static const struct tok message_type_values[] = {
43*d881c474Schristos     { 0x00, "REQUEST" },
44*d881c474Schristos     { 0x01, "REQUEST_NO_RETURN" },
45*d881c474Schristos     { 0x02, "NOTIFICATION" },
46*d881c474Schristos     { 0x80, "RESPONSE" },
47*d881c474Schristos     { 0x81, "ERROR" },
48*d881c474Schristos     { 0x20, "TP_REQUEST" },
49*d881c474Schristos     { 0x21, "TP_REQUEST_NO_RETURN" },
50*d881c474Schristos     { 0x22, "TP_NOTIFICATION" },
51*d881c474Schristos     { 0xa0, "TP_RESPONSE" },
52*d881c474Schristos     { 0xa1, "TP_ERROR" },
53*d881c474Schristos     { 0, NULL }
54*d881c474Schristos };
55*d881c474Schristos 
56*d881c474Schristos static const struct tok return_code_values[] = {
57*d881c474Schristos     { 0x00, "E_OK" },
58*d881c474Schristos     { 0x01, "E_NOT_OK" },
59*d881c474Schristos     { 0x02, "E_UNKNOWN_SERVICE" },
60*d881c474Schristos     { 0x03, "E_UNKNOWN_METHOD" },
61*d881c474Schristos     { 0x04, "E_NOT_READY" },
62*d881c474Schristos     { 0x05, "E_NOT_REACHABLE" },
63*d881c474Schristos     { 0x06, "E_TIMEOUT" },
64*d881c474Schristos     { 0x07, "E_WRONG_PROTOCOL_VERSION" },
65*d881c474Schristos     { 0x08, "E_WRONG_INTERFACE_VERSION" },
66*d881c474Schristos     { 0x09, "E_MALFORMED_MESSAGE" },
67*d881c474Schristos     { 0x0a, "E_WRONG_MESSAGE_TYPE" },
68*d881c474Schristos     { 0x0b, "E_E2E_REPEATED" },
69*d881c474Schristos     { 0x0c, "E_E2E_WRONG_SEQUENCE" },
70*d881c474Schristos     { 0x0d, "E_E2E" },
71*d881c474Schristos     { 0x0e, "E_E2E_NOT_AVAILABLE" },
72*d881c474Schristos     { 0x0f, "E_E2E_NO_NEW_DATA" },
73*d881c474Schristos     { 0, NULL }
74*d881c474Schristos };
75*d881c474Schristos 
76*d881c474Schristos void
77*d881c474Schristos someip_print(netdissect_options *ndo, const u_char *bp, const u_int len)
78*d881c474Schristos {
79*d881c474Schristos     uint32_t message_id;
80*d881c474Schristos     uint16_t service_id;
81*d881c474Schristos     uint16_t method_or_event_id;
82*d881c474Schristos     uint8_t event_flag;
83*d881c474Schristos     uint32_t message_len;
84*d881c474Schristos     uint32_t request_id;
85*d881c474Schristos     uint16_t client_id;
86*d881c474Schristos     uint16_t session_id;
87*d881c474Schristos     uint8_t protocol_version;
88*d881c474Schristos     uint8_t interface_version;
89*d881c474Schristos     uint8_t message_type;
90*d881c474Schristos     uint8_t return_code;
91*d881c474Schristos 
92*d881c474Schristos     ndo->ndo_protocol = "someip";
93*d881c474Schristos     nd_print_protocol_caps(ndo);
94*d881c474Schristos 
95*d881c474Schristos     if (len < 16) {
96*d881c474Schristos         goto invalid;
97*d881c474Schristos     }
98*d881c474Schristos 
99*d881c474Schristos     message_id = GET_BE_U_4(bp);
100*d881c474Schristos     service_id = message_id >> 16;
101*d881c474Schristos     event_flag = (message_id & 0x00008000) >> 15;
102*d881c474Schristos     method_or_event_id = message_id & 0x00007FFF;
103*d881c474Schristos     bp += 4;
104*d881c474Schristos     ND_PRINT(", service %u, %s %u",
105*d881c474Schristos              service_id, event_flag ? "event" : "method", method_or_event_id);
106*d881c474Schristos 
107*d881c474Schristos     message_len = GET_BE_U_4(bp);
108*d881c474Schristos     bp += 4;
109*d881c474Schristos     ND_PRINT(", len %u", message_len);
110*d881c474Schristos 
111*d881c474Schristos     request_id = GET_BE_U_4(bp);
112*d881c474Schristos     client_id = request_id >> 16;
113*d881c474Schristos     session_id = request_id & 0x0000FFFF;
114*d881c474Schristos     bp += 4;
115*d881c474Schristos     ND_PRINT(", client %u, session %u", client_id, session_id);
116*d881c474Schristos 
117*d881c474Schristos     protocol_version = GET_U_1(bp);
118*d881c474Schristos     bp += 1;
119*d881c474Schristos     ND_PRINT(", pver %u", protocol_version);
120*d881c474Schristos 
121*d881c474Schristos     interface_version = GET_U_1(bp);
122*d881c474Schristos     bp += 1;
123*d881c474Schristos     ND_PRINT(", iver %u", interface_version);
124*d881c474Schristos 
125*d881c474Schristos     message_type = GET_U_1(bp);
126*d881c474Schristos     bp += 1;
127*d881c474Schristos     ND_PRINT(", msgtype %s",
128*d881c474Schristos              tok2str(message_type_values, "Unknown", message_type));
129*d881c474Schristos 
130*d881c474Schristos     return_code = GET_U_1(bp);
131*d881c474Schristos     bp += 1;
132*d881c474Schristos     ND_PRINT(", retcode %s\n",
133*d881c474Schristos 	     tok2str(return_code_values, "Unknown", return_code));
134*d881c474Schristos 
135*d881c474Schristos     return;
136*d881c474Schristos 
137*d881c474Schristos invalid:
138*d881c474Schristos     nd_print_invalid(ndo);
139*d881c474Schristos }
140