]> The Tcpdump Group git mirrors - tcpdump/blob - print-geonet.c
Revert partially the commit 21b1273
[tcpdump] / print-geonet.c
1 /*
2 * Copyright (c) 2013 The TCPDUMP project
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that: (1) source code
6 * distributions retain the above copyright notice and this paragraph
7 * in its entirety, and (2) distributions including binary code include
8 * the above copyright notice and this paragraph in its entirety in
9 * the documentation or other materials provided with the distribution.
10 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
11 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
12 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13 * FOR A PARTICULAR PURPOSE.
14 *
15 * Original code by Ola Martin Lykkja (ola.lykkja@q-free.com)
16 */
17
18 /* \summary: ISO CALM FAST and ETSI GeoNetworking printer */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include "netdissect-stdinc.h"
25
26 #include "netdissect.h"
27 #include "extract.h"
28 #include "addrtoname.h"
29
30
31 /*
32 ETSI TS 102 636-5-1 V1.1.1 (2011-02)
33 Intelligent Transport Systems (ITS); Vehicular Communications; GeoNetworking;
34 Part 5: Transport Protocols; Sub-part 1: Basic Transport Protocol
35
36 ETSI TS 102 636-4-1 V1.1.1 (2011-06)
37 Intelligent Transport Systems (ITS); Vehicular communications; GeoNetworking;
38 Part 4: Geographical addressing and forwarding for point-to-point and point-to-multipoint communications;
39 Sub-part 1: Media-Independent Functionality
40 */
41
42 #define GEONET_ADDR_LEN 8
43
44 static const struct tok msg_type_values[] = {
45 { 0, "CAM" },
46 { 1, "DENM" },
47 { 101, "TPEGM" },
48 { 102, "TSPDM" },
49 { 103, "VPM" },
50 { 104, "SRM" },
51 { 105, "SLAM" },
52 { 106, "ecoCAM" },
53 { 107, "ITM" },
54 { 150, "SA" },
55 { 0, NULL }
56 };
57
58 static void
59 print_btp_body(netdissect_options *ndo,
60 const u_char *bp)
61 {
62 u_int version;
63 u_int msg_type;
64 const char *msg_type_str;
65
66 /* Assuming ItsDpuHeader */
67 /* 2 bytes ND_TCHECKed in geonet_print() */
68 version = GET_U_1(bp);
69 msg_type = GET_U_1(bp + 1);
70 msg_type_str = tok2str(msg_type_values, "unknown (%u)", msg_type);
71
72 ND_PRINT("; ItsPduHeader v:%u t:%u-%s", version, msg_type, msg_type_str);
73 }
74
75 static void
76 print_btp(netdissect_options *ndo,
77 const u_char *bp)
78 {
79 /* 4 bytes ND_TCHECKed in geonet_print() */
80 uint16_t dest = GET_BE_U_2(bp + 0);
81 uint16_t src = GET_BE_U_2(bp + 2);
82 ND_PRINT("; BTP Dst:%u Src:%u", dest, src);
83 }
84
85 static int
86 print_long_pos_vector(netdissect_options *ndo,
87 const u_char *bp)
88 {
89 uint32_t lat, lon;
90
91 ND_PRINT("GN_ADDR:%s ", GET_LINKADDR_STRING(bp, LINKADDR_OTHER, GEONET_ADDR_LEN));
92
93 lat = GET_BE_U_4(bp + 12);
94 ND_PRINT("lat:%u ", lat);
95 lon = GET_BE_U_4(bp + 16);
96 ND_PRINT("lon:%u", lon);
97 return (0);
98 }
99
100
101 /*
102 * This is the top level routine of the printer. 'p' points
103 * to the geonet header of the packet.
104 */
105 void
106 geonet_print(netdissect_options *ndo, const u_char *bp, u_int length,
107 const struct lladdr_info *src)
108 {
109 u_int version;
110 u_int next_hdr;
111 u_int hdr_type;
112 u_int hdr_subtype;
113 uint16_t payload_length;
114 u_int hop_limit;
115 const char *next_hdr_txt = "Unknown";
116 const char *hdr_type_txt = "Unknown";
117 int hdr_size = -1;
118
119 ndo->ndo_protocol = "geonet";
120 ND_PRINT("GeoNet ");
121 if (src != NULL)
122 ND_PRINT("src:%s", (src->addr_string)(ndo, src->addr));
123 ND_PRINT("; ");
124
125 /* Process Common Header */
126 if (length < 36)
127 goto invalid;
128
129 ND_TCHECK_8(bp);
130 version = GET_U_1(bp) >> 4;
131 next_hdr = GET_U_1(bp) & 0x0f;
132 hdr_type = GET_U_1(bp + 1) >> 4;
133 hdr_subtype = GET_U_1(bp + 1) & 0x0f;
134 payload_length = GET_BE_U_2(bp + 4);
135 hop_limit = GET_U_1(bp + 7);
136
137 switch (next_hdr) {
138 case 0: next_hdr_txt = "Any"; break;
139 case 1: next_hdr_txt = "BTP-A"; break;
140 case 2: next_hdr_txt = "BTP-B"; break;
141 case 3: next_hdr_txt = "IPv6"; break;
142 }
143
144 switch (hdr_type) {
145 case 0: hdr_type_txt = "Any"; break;
146 case 1: hdr_type_txt = "Beacon"; break;
147 case 2: hdr_type_txt = "GeoUnicast"; break;
148 case 3: switch (hdr_subtype) {
149 case 0: hdr_type_txt = "GeoAnycastCircle"; break;
150 case 1: hdr_type_txt = "GeoAnycastRect"; break;
151 case 2: hdr_type_txt = "GeoAnycastElipse"; break;
152 }
153 break;
154 case 4: switch (hdr_subtype) {
155 case 0: hdr_type_txt = "GeoBroadcastCircle"; break;
156 case 1: hdr_type_txt = "GeoBroadcastRect"; break;
157 case 2: hdr_type_txt = "GeoBroadcastElipse"; break;
158 }
159 break;
160 case 5: switch (hdr_subtype) {
161 case 0: hdr_type_txt = "TopoScopeBcast-SH"; break;
162 case 1: hdr_type_txt = "TopoScopeBcast-MH"; break;
163 }
164 break;
165 case 6: switch (hdr_subtype) {
166 case 0: hdr_type_txt = "LocService-Request"; break;
167 case 1: hdr_type_txt = "LocService-Reply"; break;
168 }
169 break;
170 }
171
172 ND_PRINT("v:%u ", version);
173 ND_PRINT("NH:%u-%s ", next_hdr, next_hdr_txt);
174 ND_PRINT("HT:%u-%u-%s ", hdr_type, hdr_subtype, hdr_type_txt);
175 ND_PRINT("HopLim:%u ", hop_limit);
176 ND_PRINT("Payload:%u ", payload_length);
177 if (print_long_pos_vector(ndo, bp + 8) == -1)
178 goto trunc;
179
180 /* Skip Common Header */
181 length -= 36;
182 bp += 36;
183
184 /* Process Extended Headers */
185 switch (hdr_type) {
186 case 0: /* Any */
187 hdr_size = 0;
188 break;
189 case 1: /* Beacon */
190 hdr_size = 0;
191 break;
192 case 2: /* GeoUnicast */
193 break;
194 case 3: switch (hdr_subtype) {
195 case 0: /* GeoAnycastCircle */
196 break;
197 case 1: /* GeoAnycastRect */
198 break;
199 case 2: /* GeoAnycastElipse */
200 break;
201 }
202 break;
203 case 4: switch (hdr_subtype) {
204 case 0: /* GeoBroadcastCircle */
205 break;
206 case 1: /* GeoBroadcastRect */
207 break;
208 case 2: /* GeoBroadcastElipse */
209 break;
210 }
211 break;
212 case 5: switch (hdr_subtype) {
213 case 0: /* TopoScopeBcast-SH */
214 hdr_size = 0;
215 break;
216 case 1: /* TopoScopeBcast-MH */
217 hdr_size = 68 - 36;
218 break;
219 }
220 break;
221 case 6: switch (hdr_subtype) {
222 case 0: /* LocService-Request */
223 break;
224 case 1: /* LocService-Reply */
225 break;
226 }
227 break;
228 }
229
230 /* Skip Extended headers */
231 if (hdr_size >= 0) {
232 if (length < (u_int)hdr_size)
233 goto invalid;
234 ND_TCHECK_LEN(bp, hdr_size);
235 length -= hdr_size;
236 bp += hdr_size;
237 switch (next_hdr) {
238 case 0: /* Any */
239 break;
240 case 1:
241 case 2: /* BTP A/B */
242 if (length < 4)
243 goto invalid;
244 ND_TCHECK_4(bp);
245 print_btp(ndo, bp);
246 length -= 4;
247 bp += 4;
248 if (length >= 2) {
249 /*
250 * XXX - did print_btp_body()
251 * return if length < 2
252 * because this is optional,
253 * or was that just not
254 * reporting genuine errors?
255 */
256 ND_TCHECK_2(bp);
257 print_btp_body(ndo, bp);
258 }
259 break;
260 case 3: /* IPv6 */
261 break;
262 }
263 }
264
265 /* Print user data part */
266 if (ndo->ndo_vflag)
267 ND_DEFAULTPRINT(bp, length);
268 return;
269
270 invalid:
271 ND_PRINT(" Malformed (small) ");
272 /* XXX - print the remaining data as hex? */
273 return;
274
275 trunc:
276 nd_print_trunc(ndo);
277 }