print-geonet.c 6.7 KB

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