]> The Tcpdump Group git mirrors - tcpdump/blob - print-geonet.c
Merge pull request #500 from atsampson/master
[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 int version;
63 int msg_type;
64 const char *msg_type_str;
65
66 /* Assuming ItsDpuHeader */
67 version = bp[0];
68 msg_type = bp[1];
69 msg_type_str = tok2str(msg_type_values, "unknown (%u)", msg_type);
70
71 ND_PRINT((ndo, "; ItsPduHeader v:%d t:%d-%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_16BITS(bp+0);
79 uint16_t src = EXTRACT_16BITS(bp+2);
80 ND_PRINT((ndo, "; 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 ND_PRINT((ndo, "GN_ADDR:%s ", linkaddr_string (ndo, bp, 0, GEONET_ADDR_LEN)));
90
91 if (!ND_TTEST2(*(bp+12), 8))
92 return (-1);
93 lat = EXTRACT_32BITS(bp+12);
94 ND_PRINT((ndo, "lat:%d ", lat));
95 lon = EXTRACT_32BITS(bp+16);
96 ND_PRINT((ndo, "lon:%d", 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 *eth, const u_char *bp, u_int length)
107 {
108 int version;
109 int next_hdr;
110 int hdr_type;
111 int hdr_subtype;
112 uint16_t payload_length;
113 int hop_limit;
114 const char *next_hdr_txt = "Unknown";
115 const char *hdr_type_txt = "Unknown";
116 int hdr_size = -1;
117
118 ND_PRINT((ndo, "GeoNet src:%s; ", etheraddr_string(ndo, eth+6)));
119
120 /* Process Common Header */
121 if (length < 36)
122 goto invalid;
123
124 ND_TCHECK2(*bp, 7);
125 version = bp[0] >> 4;
126 next_hdr = bp[0] & 0x0f;
127 hdr_type = bp[1] >> 4;
128 hdr_subtype = bp[1] & 0x0f;
129 payload_length = EXTRACT_16BITS(bp+4);
130 hop_limit = bp[7];
131
132 switch (next_hdr) {
133 case 0: next_hdr_txt = "Any"; break;
134 case 1: next_hdr_txt = "BTP-A"; break;
135 case 2: next_hdr_txt = "BTP-B"; break;
136 case 3: next_hdr_txt = "IPv6"; break;
137 }
138
139 switch (hdr_type) {
140 case 0: hdr_type_txt = "Any"; break;
141 case 1: hdr_type_txt = "Beacon"; break;
142 case 2: hdr_type_txt = "GeoUnicast"; break;
143 case 3: switch (hdr_subtype) {
144 case 0: hdr_type_txt = "GeoAnycastCircle"; break;
145 case 1: hdr_type_txt = "GeoAnycastRect"; break;
146 case 2: hdr_type_txt = "GeoAnycastElipse"; break;
147 }
148 break;
149 case 4: switch (hdr_subtype) {
150 case 0: hdr_type_txt = "GeoBroadcastCircle"; break;
151 case 1: hdr_type_txt = "GeoBroadcastRect"; break;
152 case 2: hdr_type_txt = "GeoBroadcastElipse"; break;
153 }
154 break;
155 case 5: switch (hdr_subtype) {
156 case 0: hdr_type_txt = "TopoScopeBcast-SH"; break;
157 case 1: hdr_type_txt = "TopoScopeBcast-MH"; break;
158 }
159 break;
160 case 6: switch (hdr_subtype) {
161 case 0: hdr_type_txt = "LocService-Request"; break;
162 case 1: hdr_type_txt = "LocService-Reply"; break;
163 }
164 break;
165 }
166
167 ND_PRINT((ndo, "v:%d ", version));
168 ND_PRINT((ndo, "NH:%d-%s ", next_hdr, next_hdr_txt));
169 ND_PRINT((ndo, "HT:%d-%d-%s ", hdr_type, hdr_subtype, hdr_type_txt));
170 ND_PRINT((ndo, "HopLim:%d ", hop_limit));
171 ND_PRINT((ndo, "Payload:%d ", payload_length));
172 if (print_long_pos_vector(ndo, bp + 8) == -1)
173 goto trunc;
174
175 /* Skip Common Header */
176 length -= 36;
177 bp += 36;
178
179 /* Process Extended Headers */
180 switch (hdr_type) {
181 case 0: /* Any */
182 hdr_size = 0;
183 break;
184 case 1: /* Beacon */
185 hdr_size = 0;
186 break;
187 case 2: /* GeoUnicast */
188 break;
189 case 3: switch (hdr_subtype) {
190 case 0: /* GeoAnycastCircle */
191 break;
192 case 1: /* GeoAnycastRect */
193 break;
194 case 2: /* GeoAnycastElipse */
195 break;
196 }
197 break;
198 case 4: switch (hdr_subtype) {
199 case 0: /* GeoBroadcastCircle */
200 break;
201 case 1: /* GeoBroadcastRect */
202 break;
203 case 2: /* GeoBroadcastElipse */
204 break;
205 }
206 break;
207 case 5: switch (hdr_subtype) {
208 case 0: /* TopoScopeBcast-SH */
209 hdr_size = 0;
210 break;
211 case 1: /* TopoScopeBcast-MH */
212 hdr_size = 68 - 36;
213 break;
214 }
215 break;
216 case 6: switch (hdr_subtype) {
217 case 0: /* LocService-Request */
218 break;
219 case 1: /* LocService-Reply */
220 break;
221 }
222 break;
223 }
224
225 /* Skip Extended headers */
226 if (hdr_size >= 0) {
227 if (length < (u_int)hdr_size)
228 goto invalid;
229 ND_TCHECK2(*bp, hdr_size);
230 length -= hdr_size;
231 bp += hdr_size;
232 switch (next_hdr) {
233 case 0: /* Any */
234 break;
235 case 1:
236 case 2: /* BTP A/B */
237 if (length < 4)
238 goto invalid;
239 ND_TCHECK2(*bp, 4);
240 print_btp(ndo, bp);
241 length -= 4;
242 bp += 4;
243 if (length >= 2) {
244 /*
245 * XXX - did print_btp_body()
246 * return if length < 2
247 * because this is optional,
248 * or was that just not
249 * reporting genuine errors?
250 */
251 ND_TCHECK2(*bp, 2);
252 print_btp_body(ndo, bp);
253 }
254 break;
255 case 3: /* IPv6 */
256 break;
257 }
258 }
259
260 /* Print user data part */
261 if (ndo->ndo_vflag)
262 ND_DEFAULTPRINT(bp, length);
263 return;
264
265 invalid:
266 ND_PRINT((ndo, " Malformed (small) "));
267 /* XXX - print the remaining data as hex? */
268 return;
269
270 trunc:
271 ND_PRINT((ndo, "[|geonet]"));
272 }
273
274
275 /*
276 * Local Variables:
277 * c-style: whitesmith
278 * c-basic-offset: 8
279 * End:
280 */