]> The Tcpdump Group git mirrors - tcpdump/blob - print-geonet.c
Clean up whitespaces/indentation
[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 version = EXTRACT_U_1(bp);
68 msg_type = EXTRACT_U_1(bp + 1);
69 msg_type_str = tok2str(msg_type_values, "unknown (%u)", msg_type);
70
71 ND_PRINT("; ItsPduHeader v:%u t:%u-%s", version, msg_type, msg_type_str);
72 }
73
74 static void
75 print_btp(netdissect_options *ndo,
76 const u_char *bp)
77 {
78 uint16_t dest = EXTRACT_BE_U_2(bp + 0);
79 uint16_t src = EXTRACT_BE_U_2(bp + 2);
80 ND_PRINT("; BTP Dst:%u Src:%u", dest, src);
81 }
82
83 static int
84 print_long_pos_vector(netdissect_options *ndo,
85 const u_char *bp)
86 {
87 uint32_t lat, lon;
88
89 if (!ND_TTEST_LEN(bp, GEONET_ADDR_LEN))
90 return (-1);
91 ND_PRINT("GN_ADDR:%s ", linkaddr_string (ndo, bp, 0, GEONET_ADDR_LEN));
92
93 if (!ND_TTEST_8(bp + 12))
94 return (-1);
95 lat = EXTRACT_BE_U_4(bp + 12);
96 ND_PRINT("lat:%u ", lat);
97 lon = EXTRACT_BE_U_4(bp + 16);
98 ND_PRINT("lon:%u", lon);
99 return (0);
100 }
101
102
103 /*
104 * This is the top level routine of the printer. 'p' points
105 * to the geonet header of the packet.
106 */
107 void
108 geonet_print(netdissect_options *ndo, const u_char *bp, u_int length,
109 const struct lladdr_info *src)
110 {
111 u_int version;
112 u_int next_hdr;
113 u_int hdr_type;
114 u_int hdr_subtype;
115 uint16_t payload_length;
116 u_int hop_limit;
117 const char *next_hdr_txt = "Unknown";
118 const char *hdr_type_txt = "Unknown";
119 int hdr_size = -1;
120
121 ndo->ndo_protocol = "geonet";
122 ND_PRINT("GeoNet ");
123 if (src != NULL)
124 ND_PRINT("src:%s", (src->addr_string)(ndo, src->addr));
125 ND_PRINT("; ");
126
127 /* Process Common Header */
128 if (length < 36)
129 goto invalid;
130
131 ND_TCHECK_8(bp);
132 version = EXTRACT_U_1(bp) >> 4;
133 next_hdr = EXTRACT_U_1(bp) & 0x0f;
134 hdr_type = EXTRACT_U_1(bp + 1) >> 4;
135 hdr_subtype = EXTRACT_U_1(bp + 1) & 0x0f;
136 payload_length = EXTRACT_BE_U_2(bp + 4);
137 hop_limit = EXTRACT_U_1(bp + 7);
138
139 switch (next_hdr) {
140 case 0: next_hdr_txt = "Any"; break;
141 case 1: next_hdr_txt = "BTP-A"; break;
142 case 2: next_hdr_txt = "BTP-B"; break;
143 case 3: next_hdr_txt = "IPv6"; break;
144 }
145
146 switch (hdr_type) {
147 case 0: hdr_type_txt = "Any"; break;
148 case 1: hdr_type_txt = "Beacon"; break;
149 case 2: hdr_type_txt = "GeoUnicast"; break;
150 case 3: switch (hdr_subtype) {
151 case 0: hdr_type_txt = "GeoAnycastCircle"; break;
152 case 1: hdr_type_txt = "GeoAnycastRect"; break;
153 case 2: hdr_type_txt = "GeoAnycastElipse"; break;
154 }
155 break;
156 case 4: switch (hdr_subtype) {
157 case 0: hdr_type_txt = "GeoBroadcastCircle"; break;
158 case 1: hdr_type_txt = "GeoBroadcastRect"; break;
159 case 2: hdr_type_txt = "GeoBroadcastElipse"; break;
160 }
161 break;
162 case 5: switch (hdr_subtype) {
163 case 0: hdr_type_txt = "TopoScopeBcast-SH"; break;
164 case 1: hdr_type_txt = "TopoScopeBcast-MH"; break;
165 }
166 break;
167 case 6: switch (hdr_subtype) {
168 case 0: hdr_type_txt = "LocService-Request"; break;
169 case 1: hdr_type_txt = "LocService-Reply"; break;
170 }
171 break;
172 }
173
174 ND_PRINT("v:%u ", version);
175 ND_PRINT("NH:%u-%s ", next_hdr, next_hdr_txt);
176 ND_PRINT("HT:%u-%u-%s ", hdr_type, hdr_subtype, hdr_type_txt);
177 ND_PRINT("HopLim:%u ", hop_limit);
178 ND_PRINT("Payload:%u ", payload_length);
179 if (print_long_pos_vector(ndo, bp + 8) == -1)
180 goto trunc;
181
182 /* Skip Common Header */
183 length -= 36;
184 bp += 36;
185
186 /* Process Extended Headers */
187 switch (hdr_type) {
188 case 0: /* Any */
189 hdr_size = 0;
190 break;
191 case 1: /* Beacon */
192 hdr_size = 0;
193 break;
194 case 2: /* GeoUnicast */
195 break;
196 case 3: switch (hdr_subtype) {
197 case 0: /* GeoAnycastCircle */
198 break;
199 case 1: /* GeoAnycastRect */
200 break;
201 case 2: /* GeoAnycastElipse */
202 break;
203 }
204 break;
205 case 4: switch (hdr_subtype) {
206 case 0: /* GeoBroadcastCircle */
207 break;
208 case 1: /* GeoBroadcastRect */
209 break;
210 case 2: /* GeoBroadcastElipse */
211 break;
212 }
213 break;
214 case 5: switch (hdr_subtype) {
215 case 0: /* TopoScopeBcast-SH */
216 hdr_size = 0;
217 break;
218 case 1: /* TopoScopeBcast-MH */
219 hdr_size = 68 - 36;
220 break;
221 }
222 break;
223 case 6: switch (hdr_subtype) {
224 case 0: /* LocService-Request */
225 break;
226 case 1: /* LocService-Reply */
227 break;
228 }
229 break;
230 }
231
232 /* Skip Extended headers */
233 if (hdr_size >= 0) {
234 if (length < (u_int)hdr_size)
235 goto invalid;
236 ND_TCHECK_LEN(bp, hdr_size);
237 length -= hdr_size;
238 bp += hdr_size;
239 switch (next_hdr) {
240 case 0: /* Any */
241 break;
242 case 1:
243 case 2: /* BTP A/B */
244 if (length < 4)
245 goto invalid;
246 ND_TCHECK_4(bp);
247 print_btp(ndo, bp);
248 length -= 4;
249 bp += 4;
250 if (length >= 2) {
251 /*
252 * XXX - did print_btp_body()
253 * return if length < 2
254 * because this is optional,
255 * or was that just not
256 * reporting genuine errors?
257 */
258 ND_TCHECK_2(bp);
259 print_btp_body(ndo, bp);
260 }
261 break;
262 case 3: /* IPv6 */
263 break;
264 }
265 }
266
267 /* Print user data part */
268 if (ndo->ndo_vflag)
269 ND_DEFAULTPRINT(bp, length);
270 return;
271
272 invalid:
273 ND_PRINT(" Malformed (small) ");
274 /* XXX - print the remaining data as hex? */
275 return;
276
277 trunc:
278 nd_print_trunc(ndo);
279 }