print-egp.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * Copyright (c) 1991, 1992, 1993, 1994, 1995, 1996
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms are permitted
  6. * provided that the above copyright notice and this paragraph are
  7. * duplicated in all such forms and that any documentation,
  8. * advertising materials, and other materials related to such
  9. * distribution and use acknowledge that the software was developed
  10. * by the University of California, Lawrence Berkeley Laboratory,
  11. * Berkeley, CA. The name of the University may not be used to
  12. * endorse or promote products derived from this software without
  13. * specific prior written permission.
  14. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * Initial contribution from Jeff Honig (jch@MITCHELL.CIT.CORNELL.EDU).
  19. */
  20. /* \summary: Exterior Gateway Protocol (EGP) printer */
  21. #ifdef HAVE_CONFIG_H
  22. #include "config.h"
  23. #endif
  24. #include <netdissect-stdinc.h>
  25. #include "netdissect.h"
  26. #include "addrtoname.h"
  27. #include "extract.h"
  28. struct egp_packet {
  29. uint8_t egp_version;
  30. #define EGP_VERSION 2
  31. uint8_t egp_type;
  32. #define EGPT_ACQUIRE 3
  33. #define EGPT_REACH 5
  34. #define EGPT_POLL 2
  35. #define EGPT_UPDATE 1
  36. #define EGPT_ERROR 8
  37. uint8_t egp_code;
  38. #define EGPC_REQUEST 0
  39. #define EGPC_CONFIRM 1
  40. #define EGPC_REFUSE 2
  41. #define EGPC_CEASE 3
  42. #define EGPC_CEASEACK 4
  43. #define EGPC_HELLO 0
  44. #define EGPC_HEARDU 1
  45. uint8_t egp_status;
  46. #define EGPS_UNSPEC 0
  47. #define EGPS_ACTIVE 1
  48. #define EGPS_PASSIVE 2
  49. #define EGPS_NORES 3
  50. #define EGPS_ADMIN 4
  51. #define EGPS_GODOWN 5
  52. #define EGPS_PARAM 6
  53. #define EGPS_PROTO 7
  54. #define EGPS_INDET 0
  55. #define EGPS_UP 1
  56. #define EGPS_DOWN 2
  57. #define EGPS_UNSOL 0x80
  58. uint16_t egp_checksum;
  59. uint16_t egp_as;
  60. uint16_t egp_sequence;
  61. union {
  62. uint16_t egpu_hello;
  63. uint8_t egpu_gws[2];
  64. uint16_t egpu_reason;
  65. #define EGPR_UNSPEC 0
  66. #define EGPR_BADHEAD 1
  67. #define EGPR_BADDATA 2
  68. #define EGPR_NOREACH 3
  69. #define EGPR_XSPOLL 4
  70. #define EGPR_NORESP 5
  71. #define EGPR_UVERSION 6
  72. } egp_handg;
  73. #define egp_hello egp_handg.egpu_hello
  74. #define egp_intgw egp_handg.egpu_gws[0]
  75. #define egp_extgw egp_handg.egpu_gws[1]
  76. #define egp_reason egp_handg.egpu_reason
  77. union {
  78. uint16_t egpu_poll;
  79. uint32_t egpu_sourcenet;
  80. } egp_pands;
  81. #define egp_poll egp_pands.egpu_poll
  82. #define egp_sourcenet egp_pands.egpu_sourcenet
  83. };
  84. static const char *egp_acquire_codes[] = {
  85. "request",
  86. "confirm",
  87. "refuse",
  88. "cease",
  89. "cease_ack"
  90. };
  91. static const char *egp_acquire_status[] = {
  92. "unspecified",
  93. "active_mode",
  94. "passive_mode",
  95. "insufficient_resources",
  96. "administratively_prohibited",
  97. "going_down",
  98. "parameter_violation",
  99. "protocol_violation"
  100. };
  101. static const char *egp_reach_codes[] = {
  102. "hello",
  103. "i-h-u"
  104. };
  105. static const char *egp_status_updown[] = {
  106. "indeterminate",
  107. "up",
  108. "down"
  109. };
  110. static const char *egp_reasons[] = {
  111. "unspecified",
  112. "bad_EGP_header_format",
  113. "bad_EGP_data_field_format",
  114. "reachability_info_unavailable",
  115. "excessive_polling_rate",
  116. "no_response",
  117. "unsupported_version"
  118. };
  119. static void
  120. egpnrprint(netdissect_options *ndo,
  121. register const struct egp_packet *egp, u_int length)
  122. {
  123. register const uint8_t *cp;
  124. uint32_t addr;
  125. register uint32_t net;
  126. register u_int netlen;
  127. int gateways, distances, networks;
  128. int t_gateways;
  129. const char *comma;
  130. addr = egp->egp_sourcenet;
  131. if (IN_CLASSA(addr)) {
  132. net = addr & IN_CLASSA_NET;
  133. netlen = 1;
  134. } else if (IN_CLASSB(addr)) {
  135. net = addr & IN_CLASSB_NET;
  136. netlen = 2;
  137. } else if (IN_CLASSC(addr)) {
  138. net = addr & IN_CLASSC_NET;
  139. netlen = 3;
  140. } else {
  141. net = 0;
  142. netlen = 0;
  143. }
  144. cp = (const uint8_t *)(egp + 1);
  145. length -= sizeof(*egp);
  146. t_gateways = egp->egp_intgw + egp->egp_extgw;
  147. for (gateways = 0; gateways < t_gateways; ++gateways) {
  148. /* Pickup host part of gateway address */
  149. addr = 0;
  150. if (length < 4 - netlen)
  151. goto trunc;
  152. ND_TCHECK2(cp[0], 4 - netlen);
  153. switch (netlen) {
  154. case 1:
  155. addr = *cp++;
  156. /* fall through */
  157. case 2:
  158. addr = (addr << 8) | *cp++;
  159. /* fall through */
  160. case 3:
  161. addr = (addr << 8) | *cp++;
  162. }
  163. addr |= net;
  164. length -= 4 - netlen;
  165. if (length < 1)
  166. goto trunc;
  167. ND_TCHECK2(cp[0], 1);
  168. distances = *cp++;
  169. length--;
  170. ND_PRINT((ndo, " %s %s ",
  171. gateways < (int)egp->egp_intgw ? "int" : "ext",
  172. ipaddr_string(ndo, &addr)));
  173. comma = "";
  174. ND_PRINT((ndo, "("));
  175. while (--distances >= 0) {
  176. if (length < 2)
  177. goto trunc;
  178. ND_TCHECK2(cp[0], 2);
  179. ND_PRINT((ndo, "%sd%d:", comma, (int)*cp++));
  180. comma = ", ";
  181. networks = *cp++;
  182. length -= 2;
  183. while (--networks >= 0) {
  184. /* Pickup network number */
  185. if (length < 1)
  186. goto trunc;
  187. ND_TCHECK2(cp[0], 1);
  188. addr = (uint32_t)*cp++ << 24;
  189. length--;
  190. if (IN_CLASSB(addr)) {
  191. if (length < 1)
  192. goto trunc;
  193. ND_TCHECK2(cp[0], 1);
  194. addr |= (uint32_t)*cp++ << 16;
  195. length--;
  196. } else if (!IN_CLASSA(addr)) {
  197. if (length < 2)
  198. goto trunc;
  199. ND_TCHECK2(cp[0], 2);
  200. addr |= (uint32_t)*cp++ << 16;
  201. addr |= (uint32_t)*cp++ << 8;
  202. length -= 2;
  203. }
  204. ND_PRINT((ndo, " %s", ipaddr_string(ndo, &addr)));
  205. }
  206. }
  207. ND_PRINT((ndo, ")"));
  208. }
  209. return;
  210. trunc:
  211. ND_PRINT((ndo, "[|]"));
  212. }
  213. void
  214. egp_print(netdissect_options *ndo,
  215. register const uint8_t *bp, register u_int length)
  216. {
  217. register const struct egp_packet *egp;
  218. register int status;
  219. register int code;
  220. register int type;
  221. egp = (const struct egp_packet *)bp;
  222. if (length < sizeof(*egp) || !ND_TTEST(*egp)) {
  223. ND_PRINT((ndo, "[|egp]"));
  224. return;
  225. }
  226. if (!ndo->ndo_vflag) {
  227. ND_PRINT((ndo, "EGPv%u, AS %u, seq %u, length %u",
  228. egp->egp_version,
  229. EXTRACT_16BITS(&egp->egp_as),
  230. EXTRACT_16BITS(&egp->egp_sequence),
  231. length));
  232. return;
  233. } else
  234. ND_PRINT((ndo, "EGPv%u, length %u",
  235. egp->egp_version,
  236. length));
  237. if (egp->egp_version != EGP_VERSION) {
  238. ND_PRINT((ndo, "[version %d]", egp->egp_version));
  239. return;
  240. }
  241. type = egp->egp_type;
  242. code = egp->egp_code;
  243. status = egp->egp_status;
  244. switch (type) {
  245. case EGPT_ACQUIRE:
  246. ND_PRINT((ndo, " acquire"));
  247. switch (code) {
  248. case EGPC_REQUEST:
  249. case EGPC_CONFIRM:
  250. ND_PRINT((ndo, " %s", egp_acquire_codes[code]));
  251. switch (status) {
  252. case EGPS_UNSPEC:
  253. case EGPS_ACTIVE:
  254. case EGPS_PASSIVE:
  255. ND_PRINT((ndo, " %s", egp_acquire_status[status]));
  256. break;
  257. default:
  258. ND_PRINT((ndo, " [status %d]", status));
  259. break;
  260. }
  261. ND_PRINT((ndo, " hello:%d poll:%d",
  262. EXTRACT_16BITS(&egp->egp_hello),
  263. EXTRACT_16BITS(&egp->egp_poll)));
  264. break;
  265. case EGPC_REFUSE:
  266. case EGPC_CEASE:
  267. case EGPC_CEASEACK:
  268. ND_PRINT((ndo, " %s", egp_acquire_codes[code]));
  269. switch (status ) {
  270. case EGPS_UNSPEC:
  271. case EGPS_NORES:
  272. case EGPS_ADMIN:
  273. case EGPS_GODOWN:
  274. case EGPS_PARAM:
  275. case EGPS_PROTO:
  276. ND_PRINT((ndo, " %s", egp_acquire_status[status]));
  277. break;
  278. default:
  279. ND_PRINT((ndo, "[status %d]", status));
  280. break;
  281. }
  282. break;
  283. default:
  284. ND_PRINT((ndo, "[code %d]", code));
  285. break;
  286. }
  287. break;
  288. case EGPT_REACH:
  289. switch (code) {
  290. case EGPC_HELLO:
  291. case EGPC_HEARDU:
  292. ND_PRINT((ndo, " %s", egp_reach_codes[code]));
  293. if (status <= EGPS_DOWN)
  294. ND_PRINT((ndo, " state:%s", egp_status_updown[status]));
  295. else
  296. ND_PRINT((ndo, " [status %d]", status));
  297. break;
  298. default:
  299. ND_PRINT((ndo, "[reach code %d]", code));
  300. break;
  301. }
  302. break;
  303. case EGPT_POLL:
  304. ND_PRINT((ndo, " poll"));
  305. if (egp->egp_status <= EGPS_DOWN)
  306. ND_PRINT((ndo, " state:%s", egp_status_updown[status]));
  307. else
  308. ND_PRINT((ndo, " [status %d]", status));
  309. ND_PRINT((ndo, " net:%s", ipaddr_string(ndo, &egp->egp_sourcenet)));
  310. break;
  311. case EGPT_UPDATE:
  312. ND_PRINT((ndo, " update"));
  313. if (status & EGPS_UNSOL) {
  314. status &= ~EGPS_UNSOL;
  315. ND_PRINT((ndo, " unsolicited"));
  316. }
  317. if (status <= EGPS_DOWN)
  318. ND_PRINT((ndo, " state:%s", egp_status_updown[status]));
  319. else
  320. ND_PRINT((ndo, " [status %d]", status));
  321. ND_PRINT((ndo, " %s int %d ext %d",
  322. ipaddr_string(ndo, &egp->egp_sourcenet),
  323. egp->egp_intgw,
  324. egp->egp_extgw));
  325. if (ndo->ndo_vflag)
  326. egpnrprint(ndo, egp, length);
  327. break;
  328. case EGPT_ERROR:
  329. ND_PRINT((ndo, " error"));
  330. if (status <= EGPS_DOWN)
  331. ND_PRINT((ndo, " state:%s", egp_status_updown[status]));
  332. else
  333. ND_PRINT((ndo, " [status %d]", status));
  334. if (EXTRACT_16BITS(&egp->egp_reason) <= EGPR_UVERSION)
  335. ND_PRINT((ndo, " %s", egp_reasons[EXTRACT_16BITS(&egp->egp_reason)]));
  336. else
  337. ND_PRINT((ndo, " [reason %d]", EXTRACT_16BITS(&egp->egp_reason)));
  338. break;
  339. default:
  340. ND_PRINT((ndo, "[type %d]", type));
  341. break;
  342. }
  343. }