xref: /freebsd/contrib/tcpdump/print-geonet.c (revision 0a7e5f1f02aad2ff5fff1c60f44c6975fd07e1d9)
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 #include <config.h>
21 
22 #include "netdissect-stdinc.h"
23 
24 #define ND_LONGJMP_FROM_TCHECK
25 #include "netdissect.h"
26 #include "extract.h"
27 #include "addrtoname.h"
28 
29 
30 /*
31    ETSI TS 102 636-5-1 V1.1.1 (2011-02)
32    Intelligent Transport Systems (ITS); Vehicular Communications; GeoNetworking;
33    Part 5: Transport Protocols; Sub-part 1: Basic Transport Protocol
34 
35    ETSI TS 102 636-4-1 V1.1.1 (2011-06)
36    Intelligent Transport Systems (ITS); Vehicular communications; GeoNetworking;
37    Part 4: Geographical addressing and forwarding for point-to-point and point-to-multipoint communications;
38    Sub-part 1: Media-Independent Functionality
39 */
40 
41 #define GEONET_ADDR_LEN 8
42 
43 static const struct tok msg_type_values[] = {
44 	{   0, "CAM" },
45 	{   1, "DENM" },
46 	{ 101, "TPEGM" },
47 	{ 102, "TSPDM" },
48 	{ 103, "VPM" },
49 	{ 104, "SRM" },
50 	{ 105, "SLAM" },
51 	{ 106, "ecoCAM" },
52 	{ 107, "ITM" },
53 	{ 150, "SA" },
54 	{   0, NULL }
55 };
56 
57 static void
print_btp_body(netdissect_options * ndo,const u_char * bp)58 print_btp_body(netdissect_options *ndo,
59 	       const u_char *bp)
60 {
61 	u_int msg_type;
62 
63 	/* Assuming ItsPduHeader */
64 	ND_PRINT("; ItsPduHeader v:%u", GET_U_1(bp));
65 
66 	msg_type = GET_U_1(bp + 1);
67 	ND_PRINT(" t:%u-%s", msg_type,
68 	         tok2str(msg_type_values, "unknown (%u)", msg_type));
69 }
70 
71 /* EN 302 636-5-1 V2.2.1 Section 7.2: BTP-A header */
72 static void
print_btp(netdissect_options * ndo,const u_char * bp)73 print_btp(netdissect_options *ndo,
74 	  const u_char *bp)
75 {
76 	ND_PRINT("; BTP Dst:%u", GET_BE_U_2(bp + 0));
77 	ND_PRINT(" Src:%u", GET_BE_U_2(bp + 2));
78 }
79 
80 static void
print_long_pos_vector(netdissect_options * ndo,const u_char * bp)81 print_long_pos_vector(netdissect_options *ndo,
82 		      const u_char *bp)
83 {
84 	ND_PRINT("GN_ADDR:%s ", GET_LINKADDR_STRING(bp, LINKADDR_OTHER, GEONET_ADDR_LEN));
85 	ND_PRINT("lat:%u ", GET_BE_U_4(bp + 12));
86 	ND_PRINT("lon:%u", GET_BE_U_4(bp + 16));
87 }
88 
89 
90 /*
91  * This is the top level routine of the printer.  'p' points
92  * to the geonet header of the packet.
93  */
94 void
geonet_print(netdissect_options * ndo,const u_char * bp,u_int length,const struct lladdr_info * src)95 geonet_print(netdissect_options *ndo, const u_char *bp, u_int length,
96 	     const struct lladdr_info *src)
97 {
98 	u_int version;
99 	u_int next_hdr;
100 	u_int hdr_type;
101 	u_int hdr_subtype;
102 	uint16_t payload_length;
103 	u_int hop_limit;
104 	const char *next_hdr_txt = "Unknown";
105 	const char *hdr_type_txt = "Unknown";
106 	int hdr_size = -1;
107 
108 	ndo->ndo_protocol = "geonet";
109 	ND_PRINT("GeoNet ");
110 	if (src != NULL)
111 		ND_PRINT("src:%s", (src->addr_string)(ndo, src->addr));
112 	ND_PRINT("; ");
113 
114 	/* Process Common Header */
115 	if (length < 36) {
116 		ND_PRINT(" (common header length %u < 36)", length);
117 		goto invalid;
118 	}
119 
120 	version = GET_U_1(bp) >> 4;
121 	next_hdr = GET_U_1(bp) & 0x0f;
122 	hdr_type = GET_U_1(bp + 1) >> 4;
123 	hdr_subtype = GET_U_1(bp + 1) & 0x0f;
124 	payload_length = GET_BE_U_2(bp + 4);
125 	hop_limit = GET_U_1(bp + 7);
126 
127 	switch (next_hdr) {
128 		case 0: next_hdr_txt = "Any"; break;
129 		case 1: next_hdr_txt = "BTP-A"; break;
130 		case 2: next_hdr_txt = "BTP-B"; break;
131 		case 3: next_hdr_txt = "IPv6"; break;
132 	}
133 
134 	switch (hdr_type) {
135 		case 0: hdr_type_txt = "Any"; break;
136 		case 1: hdr_type_txt = "Beacon"; break;
137 		case 2: hdr_type_txt = "GeoUnicast"; break;
138 		case 3: switch (hdr_subtype) {
139 				case 0: hdr_type_txt = "GeoAnycastCircle"; break;
140 				case 1: hdr_type_txt = "GeoAnycastRect"; break;
141 				case 2: hdr_type_txt = "GeoAnycastElipse"; break;
142 			}
143 			break;
144 		case 4: switch (hdr_subtype) {
145 				case 0: hdr_type_txt = "GeoBroadcastCircle"; break;
146 				case 1: hdr_type_txt = "GeoBroadcastRect"; break;
147 				case 2: hdr_type_txt = "GeoBroadcastElipse"; break;
148 			}
149 			break;
150 		case 5: switch (hdr_subtype) {
151 				case 0: hdr_type_txt = "TopoScopeBcast-SH"; break;
152 				case 1: hdr_type_txt = "TopoScopeBcast-MH"; break;
153 			}
154 			break;
155 		case 6: switch (hdr_subtype) {
156 				case 0: hdr_type_txt = "LocService-Request"; break;
157 				case 1: hdr_type_txt = "LocService-Reply"; break;
158 			}
159 			break;
160 	}
161 
162 	ND_PRINT("v:%u ", version);
163 	ND_PRINT("NH:%u-%s ", next_hdr, next_hdr_txt);
164 	ND_PRINT("HT:%u-%u-%s ", hdr_type, hdr_subtype, hdr_type_txt);
165 	ND_PRINT("HopLim:%u ", hop_limit);
166 	ND_PRINT("Payload:%u ", payload_length);
167 	print_long_pos_vector(ndo, bp + 8);
168 
169 	/* Skip Common Header */
170 	ND_TCHECK_LEN(bp, 36);
171 	length -= 36;
172 	bp += 36;
173 
174 	/* Process Extended Headers */
175 	switch (hdr_type) {
176 		case 0: /* Any */
177 			hdr_size = 0;
178 			break;
179 		case 1: /* Beacon */
180 			hdr_size = 0;
181 			break;
182 		case 2: /* GeoUnicast */
183 			break;
184 		case 3: switch (hdr_subtype) {
185 				case 0: /* GeoAnycastCircle */
186 					break;
187 				case 1: /* GeoAnycastRect */
188 					break;
189 				case 2: /* GeoAnycastElipse */
190 					break;
191 			}
192 			break;
193 		case 4: switch (hdr_subtype) {
194 				case 0: /* GeoBroadcastCircle */
195 					break;
196 				case 1: /* GeoBroadcastRect */
197 					break;
198 				case 2: /* GeoBroadcastElipse */
199 					break;
200 			}
201 			break;
202 		case 5: switch (hdr_subtype) {
203 				case 0: /* TopoScopeBcast-SH */
204 					hdr_size = 0;
205 					break;
206 				case 1: /* TopoScopeBcast-MH */
207 					hdr_size = 68 - 36;
208 					break;
209 			}
210 			break;
211 		case 6: switch (hdr_subtype) {
212 				case 0: /* LocService-Request */
213 					break;
214 				case 1: /* LocService-Reply */
215 					break;
216 			}
217 			break;
218 	}
219 
220 	/* Skip Extended headers */
221 	if (hdr_size >= 0) {
222 		if (length < (u_int)hdr_size) {
223 			ND_PRINT(" (header size %d > %u)", hdr_size, length);
224 			goto invalid;
225 		}
226 		ND_TCHECK_LEN(bp, hdr_size);
227 		length -= hdr_size;
228 		bp += hdr_size;
229 		switch (next_hdr) {
230 			case 0: /* Any */
231 				break;
232 			case 1:
233 			case 2: /* BTP A/B */
234 				if (length < 4) {
235 					ND_PRINT(" (BTP length %u < 4)", length);
236 					goto invalid;
237 				}
238 				print_btp(ndo, bp);
239 				length -= 4;
240 				bp += 4;
241 				if (length >= 2) {
242 					/*
243 					 * XXX - did print_btp_body()
244 					 * return if length < 2
245 					 * because this is optional,
246 					 * or was that just not
247 					 * reporting genuine errors?
248 					 */
249 					print_btp_body(ndo, bp);
250 				}
251 				break;
252 			case 3: /* IPv6 */
253 				break;
254 		}
255 	}
256 
257 	/* Print user data part */
258 	if (ndo->ndo_vflag)
259 		ND_DEFAULTPRINT(bp, length);
260 	return;
261 
262 invalid:
263 	nd_print_invalid(ndo);
264 	/* XXX - print the remaining data as hex? */
265 }
266