print-ospf6.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. /*
  2. * Copyright (c) 1992, 1993, 1994, 1995, 1996, 1997
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that: (1) source code distributions
  7. * retain the above copyright notice and this paragraph in its entirety, (2)
  8. * distributions including binary code include the above copyright notice and
  9. * this paragraph in its entirety in the documentation or other materials
  10. * provided with the distribution, and (3) all advertising materials mentioning
  11. * features or use of this software display the following acknowledgement:
  12. * ``This product includes software developed by the University of California,
  13. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14. * the University nor the names of its contributors may be used to endorse
  15. * or promote products derived from this software without specific prior
  16. * written permission.
  17. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. *
  21. * OSPF support contributed by Jeffrey Honig (jch@mitchell.cit.cornell.edu)
  22. */
  23. /* \summary: IPv6 Open Shortest Path First (OSPFv3) printer */
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include <netdissect-stdinc.h>
  28. #include <string.h>
  29. #include "netdissect.h"
  30. #include "addrtoname.h"
  31. #include "extract.h"
  32. #include "ospf.h"
  33. #define OSPF_TYPE_HELLO 1 /* Hello */
  34. #define OSPF_TYPE_DD 2 /* Database Description */
  35. #define OSPF_TYPE_LS_REQ 3 /* Link State Request */
  36. #define OSPF_TYPE_LS_UPDATE 4 /* Link State Update */
  37. #define OSPF_TYPE_LS_ACK 5 /* Link State Ack */
  38. /* Options *_options */
  39. #define OSPF6_OPTION_V6 0x01 /* V6 bit: A bit for peeping tom */
  40. #define OSPF6_OPTION_E 0x02 /* E bit: External routes advertised */
  41. #define OSPF6_OPTION_MC 0x04 /* MC bit: Multicast capable */
  42. #define OSPF6_OPTION_N 0x08 /* N bit: For type-7 LSA */
  43. #define OSPF6_OPTION_R 0x10 /* R bit: Router bit */
  44. #define OSPF6_OPTION_DC 0x20 /* DC bit: Demand circuits */
  45. /* The field is actually 24-bit (RFC5340 Section A.2). */
  46. #define OSPF6_OPTION_AF 0x0100 /* AF bit: Multiple address families */
  47. #define OSPF6_OPTION_L 0x0200 /* L bit: Link-local signaling (LLS) */
  48. #define OSPF6_OPTION_AT 0x0400 /* AT bit: Authentication trailer */
  49. /* db_flags */
  50. #define OSPF6_DB_INIT 0x04 /* */
  51. #define OSPF6_DB_MORE 0x02
  52. #define OSPF6_DB_MASTER 0x01
  53. #define OSPF6_DB_M6 0x10 /* IPv6 MTU */
  54. /* ls_type */
  55. #define LS_TYPE_ROUTER 1 /* router link */
  56. #define LS_TYPE_NETWORK 2 /* network link */
  57. #define LS_TYPE_INTER_AP 3 /* Inter-Area-Prefix */
  58. #define LS_TYPE_INTER_AR 4 /* Inter-Area-Router */
  59. #define LS_TYPE_ASE 5 /* ASE */
  60. #define LS_TYPE_GROUP 6 /* Group membership */
  61. #define LS_TYPE_NSSA 7 /* NSSA */
  62. #define LS_TYPE_LINK 8 /* Link LSA */
  63. #define LS_TYPE_INTRA_AP 9 /* Intra-Area-Prefix */
  64. #define LS_TYPE_INTRA_ATE 10 /* Intra-Area-TE */
  65. #define LS_TYPE_GRACE 11 /* Grace LSA */
  66. #define LS_TYPE_RI 12 /* Router information */
  67. #define LS_TYPE_INTER_ASTE 13 /* Inter-AS-TE */
  68. #define LS_TYPE_L1VPN 14 /* L1VPN */
  69. #define LS_TYPE_MASK 0x1fff
  70. #define LS_SCOPE_LINKLOCAL 0x0000
  71. #define LS_SCOPE_AREA 0x2000
  72. #define LS_SCOPE_AS 0x4000
  73. #define LS_SCOPE_MASK 0x6000
  74. #define LS_SCOPE_U 0x8000
  75. /* rla_link.link_type */
  76. #define RLA_TYPE_ROUTER 1 /* point-to-point to another router */
  77. #define RLA_TYPE_TRANSIT 2 /* connection to transit network */
  78. #define RLA_TYPE_VIRTUAL 4 /* virtual link */
  79. /* rla_flags */
  80. #define RLA_FLAG_B 0x01
  81. #define RLA_FLAG_E 0x02
  82. #define RLA_FLAG_V 0x04
  83. #define RLA_FLAG_W 0x08
  84. #define RLA_FLAG_N 0x10
  85. /* lsa_prefix options */
  86. #define LSA_PREFIX_OPT_NU 0x01
  87. #define LSA_PREFIX_OPT_LA 0x02
  88. #define LSA_PREFIX_OPT_MC 0x04
  89. #define LSA_PREFIX_OPT_P 0x08
  90. #define LSA_PREFIX_OPT_DN 0x10
  91. /* sla_tosmetric breakdown */
  92. #define SLA_MASK_TOS 0x7f000000
  93. #define SLA_MASK_METRIC 0x00ffffff
  94. #define SLA_SHIFT_TOS 24
  95. /* asla_metric */
  96. #define ASLA_FLAG_FWDADDR 0x02000000
  97. #define ASLA_FLAG_ROUTETAG 0x01000000
  98. #define ASLA_MASK_METRIC 0x00ffffff
  99. /* RFC6506 Section 4.1 */
  100. #define OSPF6_AT_HDRLEN 16U
  101. #define OSPF6_AUTH_TYPE_HMAC 0x0001
  102. typedef uint32_t rtrid_t;
  103. /* link state advertisement header */
  104. struct lsa6_hdr {
  105. uint16_t ls_age;
  106. uint16_t ls_type;
  107. rtrid_t ls_stateid;
  108. rtrid_t ls_router;
  109. uint32_t ls_seq;
  110. uint16_t ls_chksum;
  111. uint16_t ls_length;
  112. };
  113. /* Length of an IPv6 address, in bytes. */
  114. #define IPV6_ADDR_LEN_BYTES (128/8)
  115. struct lsa6_prefix {
  116. uint8_t lsa_p_len;
  117. uint8_t lsa_p_opt;
  118. uint16_t lsa_p_metric;
  119. uint8_t lsa_p_prefix[IPV6_ADDR_LEN_BYTES]; /* maximum length */
  120. };
  121. /* link state advertisement */
  122. struct lsa6 {
  123. struct lsa6_hdr ls_hdr;
  124. /* Link state types */
  125. union {
  126. /* Router links advertisements */
  127. struct {
  128. union {
  129. uint8_t flg;
  130. uint32_t opt;
  131. } rla_flgandopt;
  132. #define rla_flags rla_flgandopt.flg
  133. #define rla_options rla_flgandopt.opt
  134. struct rlalink6 {
  135. uint8_t link_type;
  136. uint8_t link_zero[1];
  137. uint16_t link_metric;
  138. uint32_t link_ifid;
  139. uint32_t link_nifid;
  140. rtrid_t link_nrtid;
  141. } rla_link[1]; /* may repeat */
  142. } un_rla;
  143. /* Network links advertisements */
  144. struct {
  145. uint32_t nla_options;
  146. rtrid_t nla_router[1]; /* may repeat */
  147. } un_nla;
  148. /* Inter Area Prefix LSA */
  149. struct {
  150. uint32_t inter_ap_metric;
  151. struct lsa6_prefix inter_ap_prefix[1];
  152. } un_inter_ap;
  153. /* AS external links advertisements */
  154. struct {
  155. uint32_t asla_metric;
  156. struct lsa6_prefix asla_prefix[1];
  157. /* some optional fields follow */
  158. } un_asla;
  159. #if 0
  160. /* Summary links advertisements */
  161. struct {
  162. struct in_addr sla_mask;
  163. uint32_t sla_tosmetric[1]; /* may repeat */
  164. } un_sla;
  165. /* Multicast group membership */
  166. struct mcla {
  167. uint32_t mcla_vtype;
  168. struct in_addr mcla_vid;
  169. } un_mcla[1];
  170. #endif
  171. /* Type 7 LSA */
  172. /* Link LSA */
  173. struct llsa {
  174. union {
  175. uint8_t pri;
  176. uint32_t opt;
  177. } llsa_priandopt;
  178. #define llsa_priority llsa_priandopt.pri
  179. #define llsa_options llsa_priandopt.opt
  180. struct in6_addr llsa_lladdr;
  181. uint32_t llsa_nprefix;
  182. struct lsa6_prefix llsa_prefix[1];
  183. } un_llsa;
  184. /* Intra-Area-Prefix */
  185. struct {
  186. uint16_t intra_ap_nprefix;
  187. uint16_t intra_ap_lstype;
  188. rtrid_t intra_ap_lsid;
  189. rtrid_t intra_ap_rtid;
  190. struct lsa6_prefix intra_ap_prefix[1];
  191. } un_intra_ap;
  192. } lsa_un;
  193. };
  194. /*
  195. * the main header
  196. */
  197. struct ospf6hdr {
  198. uint8_t ospf6_version;
  199. uint8_t ospf6_type;
  200. uint16_t ospf6_len;
  201. rtrid_t ospf6_routerid;
  202. rtrid_t ospf6_areaid;
  203. uint16_t ospf6_chksum;
  204. uint8_t ospf6_instanceid;
  205. uint8_t ospf6_rsvd;
  206. };
  207. /*
  208. * The OSPF6 header length is 16 bytes, regardless of how your compiler
  209. * might choose to pad the above structure.
  210. */
  211. #define OSPF6HDR_LEN 16
  212. /* Hello packet */
  213. struct hello6 {
  214. uint32_t hello_ifid;
  215. union {
  216. uint8_t pri;
  217. uint32_t opt;
  218. } hello_priandopt;
  219. #define hello_priority hello_priandopt.pri
  220. #define hello_options hello_priandopt.opt
  221. uint16_t hello_helloint;
  222. uint16_t hello_deadint;
  223. rtrid_t hello_dr;
  224. rtrid_t hello_bdr;
  225. rtrid_t hello_neighbor[1]; /* may repeat */
  226. };
  227. /* Database Description packet */
  228. struct dd6 {
  229. uint32_t db_options;
  230. uint16_t db_mtu;
  231. uint8_t db_mbz;
  232. uint8_t db_flags;
  233. uint32_t db_seq;
  234. struct lsa6_hdr db_lshdr[1]; /* may repeat */
  235. };
  236. /* Link State Request */
  237. struct lsr6 {
  238. uint16_t ls_mbz;
  239. uint16_t ls_type;
  240. rtrid_t ls_stateid;
  241. rtrid_t ls_router;
  242. };
  243. /* Link State Update */
  244. struct lsu6 {
  245. uint32_t lsu_count;
  246. struct lsa6 lsu_lsa[1]; /* may repeat */
  247. };
  248. static const char tstr[] = " [|ospf3]";
  249. static const struct tok ospf6_option_values[] = {
  250. { OSPF6_OPTION_V6, "V6" },
  251. { OSPF6_OPTION_E, "External" },
  252. { OSPF6_OPTION_MC, "Deprecated" },
  253. { OSPF6_OPTION_N, "NSSA" },
  254. { OSPF6_OPTION_R, "Router" },
  255. { OSPF6_OPTION_DC, "Demand Circuit" },
  256. { OSPF6_OPTION_AF, "AFs Support" },
  257. { OSPF6_OPTION_L, "LLS" },
  258. { OSPF6_OPTION_AT, "Authentication Trailer" },
  259. { 0, NULL }
  260. };
  261. static const struct tok ospf6_rla_flag_values[] = {
  262. { RLA_FLAG_B, "ABR" },
  263. { RLA_FLAG_E, "External" },
  264. { RLA_FLAG_V, "Virtual-Link Endpoint" },
  265. { RLA_FLAG_W, "Wildcard Receiver" },
  266. { RLA_FLAG_N, "NSSA Translator" },
  267. { 0, NULL }
  268. };
  269. static const struct tok ospf6_asla_flag_values[] = {
  270. { ASLA_FLAG_EXTERNAL, "External Type 2" },
  271. { ASLA_FLAG_FWDADDR, "Forwarding" },
  272. { ASLA_FLAG_ROUTETAG, "Tag" },
  273. { 0, NULL }
  274. };
  275. static const struct tok ospf6_type_values[] = {
  276. { OSPF_TYPE_HELLO, "Hello" },
  277. { OSPF_TYPE_DD, "Database Description" },
  278. { OSPF_TYPE_LS_REQ, "LS-Request" },
  279. { OSPF_TYPE_LS_UPDATE, "LS-Update" },
  280. { OSPF_TYPE_LS_ACK, "LS-Ack" },
  281. { 0, NULL }
  282. };
  283. static const struct tok ospf6_lsa_values[] = {
  284. { LS_TYPE_ROUTER, "Router" },
  285. { LS_TYPE_NETWORK, "Network" },
  286. { LS_TYPE_INTER_AP, "Inter-Area Prefix" },
  287. { LS_TYPE_INTER_AR, "Inter-Area Router" },
  288. { LS_TYPE_ASE, "External" },
  289. { LS_TYPE_GROUP, "Deprecated" },
  290. { LS_TYPE_NSSA, "NSSA" },
  291. { LS_TYPE_LINK, "Link" },
  292. { LS_TYPE_INTRA_AP, "Intra-Area Prefix" },
  293. { LS_TYPE_INTRA_ATE, "Intra-Area TE" },
  294. { LS_TYPE_GRACE, "Grace" },
  295. { LS_TYPE_RI, "Router Information" },
  296. { LS_TYPE_INTER_ASTE, "Inter-AS-TE" },
  297. { LS_TYPE_L1VPN, "Layer 1 VPN" },
  298. { 0, NULL }
  299. };
  300. static const struct tok ospf6_ls_scope_values[] = {
  301. { LS_SCOPE_LINKLOCAL, "Link Local" },
  302. { LS_SCOPE_AREA, "Area Local" },
  303. { LS_SCOPE_AS, "Domain Wide" },
  304. { 0, NULL }
  305. };
  306. static const struct tok ospf6_dd_flag_values[] = {
  307. { OSPF6_DB_INIT, "Init" },
  308. { OSPF6_DB_MORE, "More" },
  309. { OSPF6_DB_MASTER, "Master" },
  310. { OSPF6_DB_M6, "IPv6 MTU" },
  311. { 0, NULL }
  312. };
  313. static const struct tok ospf6_lsa_prefix_option_values[] = {
  314. { LSA_PREFIX_OPT_NU, "No Unicast" },
  315. { LSA_PREFIX_OPT_LA, "Local address" },
  316. { LSA_PREFIX_OPT_MC, "Deprecated" },
  317. { LSA_PREFIX_OPT_P, "Propagate" },
  318. { LSA_PREFIX_OPT_DN, "Down" },
  319. { 0, NULL }
  320. };
  321. static const struct tok ospf6_auth_type_str[] = {
  322. { OSPF6_AUTH_TYPE_HMAC, "HMAC" },
  323. { 0, NULL }
  324. };
  325. static void
  326. ospf6_print_ls_type(netdissect_options *ndo,
  327. register u_int ls_type, register const rtrid_t *ls_stateid)
  328. {
  329. ND_PRINT((ndo, "\n\t %s LSA (%d), %s Scope%s, LSA-ID %s",
  330. tok2str(ospf6_lsa_values, "Unknown", ls_type & LS_TYPE_MASK),
  331. ls_type & LS_TYPE_MASK,
  332. tok2str(ospf6_ls_scope_values, "Unknown", ls_type & LS_SCOPE_MASK),
  333. ls_type &0x8000 ? ", transitive" : "", /* U-bit */
  334. ipaddr_string(ndo, ls_stateid)));
  335. }
  336. static int
  337. ospf6_print_lshdr(netdissect_options *ndo,
  338. register const struct lsa6_hdr *lshp, const u_char *dataend)
  339. {
  340. if ((const u_char *)(lshp + 1) > dataend)
  341. goto trunc;
  342. ND_TCHECK(lshp->ls_type);
  343. ND_TCHECK(lshp->ls_seq);
  344. ND_PRINT((ndo, "\n\t Advertising Router %s, seq 0x%08x, age %us, length %u",
  345. ipaddr_string(ndo, &lshp->ls_router),
  346. EXTRACT_32BITS(&lshp->ls_seq),
  347. EXTRACT_16BITS(&lshp->ls_age),
  348. EXTRACT_16BITS(&lshp->ls_length)-(u_int)sizeof(struct lsa6_hdr)));
  349. ospf6_print_ls_type(ndo, EXTRACT_16BITS(&lshp->ls_type), &lshp->ls_stateid);
  350. return (0);
  351. trunc:
  352. return (1);
  353. }
  354. static int
  355. ospf6_print_lsaprefix(netdissect_options *ndo,
  356. const uint8_t *tptr, u_int lsa_length)
  357. {
  358. const struct lsa6_prefix *lsapp = (const struct lsa6_prefix *)tptr;
  359. u_int wordlen;
  360. struct in6_addr prefix;
  361. if (lsa_length < sizeof (*lsapp) - IPV6_ADDR_LEN_BYTES)
  362. goto trunc;
  363. lsa_length -= sizeof (*lsapp) - IPV6_ADDR_LEN_BYTES;
  364. ND_TCHECK2(*lsapp, sizeof (*lsapp) - IPV6_ADDR_LEN_BYTES);
  365. wordlen = (lsapp->lsa_p_len + 31) / 32;
  366. if (wordlen * 4 > sizeof(struct in6_addr)) {
  367. ND_PRINT((ndo, " bogus prefixlen /%d", lsapp->lsa_p_len));
  368. goto trunc;
  369. }
  370. if (lsa_length < wordlen * 4)
  371. goto trunc;
  372. lsa_length -= wordlen * 4;
  373. ND_TCHECK2(lsapp->lsa_p_prefix, wordlen * 4);
  374. memset(&prefix, 0, sizeof(prefix));
  375. memcpy(&prefix, lsapp->lsa_p_prefix, wordlen * 4);
  376. ND_PRINT((ndo, "\n\t\t%s/%d", ip6addr_string(ndo, &prefix),
  377. lsapp->lsa_p_len));
  378. if (lsapp->lsa_p_opt) {
  379. ND_PRINT((ndo, ", Options [%s]",
  380. bittok2str(ospf6_lsa_prefix_option_values,
  381. "none", lsapp->lsa_p_opt)));
  382. }
  383. ND_PRINT((ndo, ", metric %u", EXTRACT_16BITS(&lsapp->lsa_p_metric)));
  384. return sizeof(*lsapp) - IPV6_ADDR_LEN_BYTES + wordlen * 4;
  385. trunc:
  386. return -1;
  387. }
  388. /*
  389. * Print a single link state advertisement. If truncated return 1, else 0.
  390. */
  391. static int
  392. ospf6_print_lsa(netdissect_options *ndo,
  393. register const struct lsa6 *lsap, const u_char *dataend)
  394. {
  395. register const struct rlalink6 *rlp;
  396. #if 0
  397. register const struct tos_metric *tosp;
  398. #endif
  399. register const rtrid_t *ap;
  400. #if 0
  401. register const struct aslametric *almp;
  402. register const struct mcla *mcp;
  403. #endif
  404. register const struct llsa *llsap;
  405. register const struct lsa6_prefix *lsapp;
  406. #if 0
  407. register const uint32_t *lp;
  408. #endif
  409. register u_int prefixes;
  410. register int bytelen;
  411. register u_int length, lsa_length;
  412. uint32_t flags32;
  413. const uint8_t *tptr;
  414. if (ospf6_print_lshdr(ndo, &lsap->ls_hdr, dataend))
  415. return (1);
  416. ND_TCHECK(lsap->ls_hdr.ls_length);
  417. length = EXTRACT_16BITS(&lsap->ls_hdr.ls_length);
  418. /*
  419. * The LSA length includes the length of the header;
  420. * it must have a value that's at least that length.
  421. * If it does, find the length of what follows the
  422. * header.
  423. */
  424. if (length < sizeof(struct lsa6_hdr) || (const u_char *)lsap + length > dataend)
  425. return (1);
  426. lsa_length = length - sizeof(struct lsa6_hdr);
  427. tptr = (const uint8_t *)lsap+sizeof(struct lsa6_hdr);
  428. switch (EXTRACT_16BITS(&lsap->ls_hdr.ls_type)) {
  429. case LS_TYPE_ROUTER | LS_SCOPE_AREA:
  430. if (lsa_length < sizeof (lsap->lsa_un.un_rla.rla_options))
  431. return (1);
  432. lsa_length -= sizeof (lsap->lsa_un.un_rla.rla_options);
  433. ND_TCHECK(lsap->lsa_un.un_rla.rla_options);
  434. ND_PRINT((ndo, "\n\t Options [%s]",
  435. bittok2str(ospf6_option_values, "none",
  436. EXTRACT_32BITS(&lsap->lsa_un.un_rla.rla_options))));
  437. ND_PRINT((ndo, ", RLA-Flags [%s]",
  438. bittok2str(ospf6_rla_flag_values, "none",
  439. lsap->lsa_un.un_rla.rla_flags)));
  440. rlp = lsap->lsa_un.un_rla.rla_link;
  441. while (lsa_length != 0) {
  442. if (lsa_length < sizeof (*rlp))
  443. return (1);
  444. lsa_length -= sizeof (*rlp);
  445. ND_TCHECK(*rlp);
  446. switch (rlp->link_type) {
  447. case RLA_TYPE_VIRTUAL:
  448. ND_PRINT((ndo, "\n\t Virtual Link: Neighbor Router-ID %s"
  449. "\n\t Neighbor Interface-ID %s, Interface %s",
  450. ipaddr_string(ndo, &rlp->link_nrtid),
  451. ipaddr_string(ndo, &rlp->link_nifid),
  452. ipaddr_string(ndo, &rlp->link_ifid)));
  453. break;
  454. case RLA_TYPE_ROUTER:
  455. ND_PRINT((ndo, "\n\t Neighbor Router-ID %s"
  456. "\n\t Neighbor Interface-ID %s, Interface %s",
  457. ipaddr_string(ndo, &rlp->link_nrtid),
  458. ipaddr_string(ndo, &rlp->link_nifid),
  459. ipaddr_string(ndo, &rlp->link_ifid)));
  460. break;
  461. case RLA_TYPE_TRANSIT:
  462. ND_PRINT((ndo, "\n\t Neighbor Network-ID %s"
  463. "\n\t Neighbor Interface-ID %s, Interface %s",
  464. ipaddr_string(ndo, &rlp->link_nrtid),
  465. ipaddr_string(ndo, &rlp->link_nifid),
  466. ipaddr_string(ndo, &rlp->link_ifid)));
  467. break;
  468. default:
  469. ND_PRINT((ndo, "\n\t Unknown Router Links Type 0x%02x",
  470. rlp->link_type));
  471. return (0);
  472. }
  473. ND_PRINT((ndo, ", metric %d", EXTRACT_16BITS(&rlp->link_metric)));
  474. rlp++;
  475. }
  476. break;
  477. case LS_TYPE_NETWORK | LS_SCOPE_AREA:
  478. if (lsa_length < sizeof (lsap->lsa_un.un_nla.nla_options))
  479. return (1);
  480. lsa_length -= sizeof (lsap->lsa_un.un_nla.nla_options);
  481. ND_TCHECK(lsap->lsa_un.un_nla.nla_options);
  482. ND_PRINT((ndo, "\n\t Options [%s]",
  483. bittok2str(ospf6_option_values, "none",
  484. EXTRACT_32BITS(&lsap->lsa_un.un_nla.nla_options))));
  485. ND_PRINT((ndo, "\n\t Connected Routers:"));
  486. ap = lsap->lsa_un.un_nla.nla_router;
  487. while (lsa_length != 0) {
  488. if (lsa_length < sizeof (*ap))
  489. return (1);
  490. lsa_length -= sizeof (*ap);
  491. ND_TCHECK(*ap);
  492. ND_PRINT((ndo, "\n\t\t%s", ipaddr_string(ndo, ap)));
  493. ++ap;
  494. }
  495. break;
  496. case LS_TYPE_INTER_AP | LS_SCOPE_AREA:
  497. if (lsa_length < sizeof (lsap->lsa_un.un_inter_ap.inter_ap_metric))
  498. return (1);
  499. lsa_length -= sizeof (lsap->lsa_un.un_inter_ap.inter_ap_metric);
  500. ND_TCHECK(lsap->lsa_un.un_inter_ap.inter_ap_metric);
  501. ND_PRINT((ndo, ", metric %u",
  502. EXTRACT_32BITS(&lsap->lsa_un.un_inter_ap.inter_ap_metric) & SLA_MASK_METRIC));
  503. tptr = (const uint8_t *)lsap->lsa_un.un_inter_ap.inter_ap_prefix;
  504. while (lsa_length != 0) {
  505. bytelen = ospf6_print_lsaprefix(ndo, tptr, lsa_length);
  506. if (bytelen < 0)
  507. goto trunc;
  508. lsa_length -= bytelen;
  509. tptr += bytelen;
  510. }
  511. break;
  512. case LS_TYPE_ASE | LS_SCOPE_AS:
  513. if (lsa_length < sizeof (lsap->lsa_un.un_asla.asla_metric))
  514. return (1);
  515. lsa_length -= sizeof (lsap->lsa_un.un_asla.asla_metric);
  516. ND_TCHECK(lsap->lsa_un.un_asla.asla_metric);
  517. flags32 = EXTRACT_32BITS(&lsap->lsa_un.un_asla.asla_metric);
  518. ND_PRINT((ndo, "\n\t Flags [%s]",
  519. bittok2str(ospf6_asla_flag_values, "none", flags32)));
  520. ND_PRINT((ndo, " metric %u",
  521. EXTRACT_32BITS(&lsap->lsa_un.un_asla.asla_metric) &
  522. ASLA_MASK_METRIC));
  523. tptr = (const uint8_t *)lsap->lsa_un.un_asla.asla_prefix;
  524. lsapp = (const struct lsa6_prefix *)tptr;
  525. bytelen = ospf6_print_lsaprefix(ndo, tptr, lsa_length);
  526. if (bytelen < 0)
  527. goto trunc;
  528. lsa_length -= bytelen;
  529. tptr += bytelen;
  530. if ((flags32 & ASLA_FLAG_FWDADDR) != 0) {
  531. const struct in6_addr *fwdaddr6;
  532. fwdaddr6 = (const struct in6_addr *)tptr;
  533. if (lsa_length < sizeof (*fwdaddr6))
  534. return (1);
  535. lsa_length -= sizeof (*fwdaddr6);
  536. ND_TCHECK(*fwdaddr6);
  537. ND_PRINT((ndo, " forward %s",
  538. ip6addr_string(ndo, fwdaddr6)));
  539. tptr += sizeof(*fwdaddr6);
  540. }
  541. if ((flags32 & ASLA_FLAG_ROUTETAG) != 0) {
  542. if (lsa_length < sizeof (uint32_t))
  543. return (1);
  544. lsa_length -= sizeof (uint32_t);
  545. ND_TCHECK(*(const uint32_t *)tptr);
  546. ND_PRINT((ndo, " tag %s",
  547. ipaddr_string(ndo, (const uint32_t *)tptr)));
  548. tptr += sizeof(uint32_t);
  549. }
  550. if (lsapp->lsa_p_metric) {
  551. if (lsa_length < sizeof (uint32_t))
  552. return (1);
  553. lsa_length -= sizeof (uint32_t);
  554. ND_TCHECK(*(const uint32_t *)tptr);
  555. ND_PRINT((ndo, " RefLSID: %s",
  556. ipaddr_string(ndo, (const uint32_t *)tptr)));
  557. tptr += sizeof(uint32_t);
  558. }
  559. break;
  560. case LS_TYPE_LINK:
  561. /* Link LSA */
  562. llsap = &lsap->lsa_un.un_llsa;
  563. if (lsa_length < sizeof (llsap->llsa_priandopt))
  564. return (1);
  565. lsa_length -= sizeof (llsap->llsa_priandopt);
  566. ND_TCHECK(llsap->llsa_priandopt);
  567. ND_PRINT((ndo, "\n\t Options [%s]",
  568. bittok2str(ospf6_option_values, "none",
  569. EXTRACT_32BITS(&llsap->llsa_options))));
  570. if (lsa_length < sizeof (llsap->llsa_lladdr) + sizeof (llsap->llsa_nprefix))
  571. return (1);
  572. lsa_length -= sizeof (llsap->llsa_lladdr) + sizeof (llsap->llsa_nprefix);
  573. ND_TCHECK(llsap->llsa_nprefix);
  574. prefixes = EXTRACT_32BITS(&llsap->llsa_nprefix);
  575. ND_PRINT((ndo, "\n\t Priority %d, Link-local address %s, Prefixes %d:",
  576. llsap->llsa_priority,
  577. ip6addr_string(ndo, &llsap->llsa_lladdr),
  578. prefixes));
  579. tptr = (const uint8_t *)llsap->llsa_prefix;
  580. while (prefixes > 0) {
  581. bytelen = ospf6_print_lsaprefix(ndo, tptr, lsa_length);
  582. if (bytelen < 0)
  583. goto trunc;
  584. prefixes--;
  585. lsa_length -= bytelen;
  586. tptr += bytelen;
  587. }
  588. break;
  589. case LS_TYPE_INTRA_AP | LS_SCOPE_AREA:
  590. /* Intra-Area-Prefix LSA */
  591. if (lsa_length < sizeof (lsap->lsa_un.un_intra_ap.intra_ap_rtid))
  592. return (1);
  593. lsa_length -= sizeof (lsap->lsa_un.un_intra_ap.intra_ap_rtid);
  594. ND_TCHECK(lsap->lsa_un.un_intra_ap.intra_ap_rtid);
  595. ospf6_print_ls_type(ndo,
  596. EXTRACT_16BITS(&lsap->lsa_un.un_intra_ap.intra_ap_lstype),
  597. &lsap->lsa_un.un_intra_ap.intra_ap_lsid);
  598. if (lsa_length < sizeof (lsap->lsa_un.un_intra_ap.intra_ap_nprefix))
  599. return (1);
  600. lsa_length -= sizeof (lsap->lsa_un.un_intra_ap.intra_ap_nprefix);
  601. ND_TCHECK(lsap->lsa_un.un_intra_ap.intra_ap_nprefix);
  602. prefixes = EXTRACT_16BITS(&lsap->lsa_un.un_intra_ap.intra_ap_nprefix);
  603. ND_PRINT((ndo, "\n\t Prefixes %d:", prefixes));
  604. tptr = (const uint8_t *)lsap->lsa_un.un_intra_ap.intra_ap_prefix;
  605. while (prefixes > 0) {
  606. bytelen = ospf6_print_lsaprefix(ndo, tptr, lsa_length);
  607. if (bytelen < 0)
  608. goto trunc;
  609. prefixes--;
  610. lsa_length -= bytelen;
  611. tptr += bytelen;
  612. }
  613. break;
  614. case LS_TYPE_GRACE | LS_SCOPE_LINKLOCAL:
  615. if (ospf_print_grace_lsa(ndo, tptr, lsa_length) == -1) {
  616. return 1;
  617. }
  618. break;
  619. case LS_TYPE_INTRA_ATE | LS_SCOPE_LINKLOCAL:
  620. if (ospf_print_te_lsa(ndo, tptr, lsa_length) == -1) {
  621. return 1;
  622. }
  623. break;
  624. default:
  625. if(!print_unknown_data(ndo,tptr,
  626. "\n\t ",
  627. lsa_length)) {
  628. return (1);
  629. }
  630. break;
  631. }
  632. return (0);
  633. trunc:
  634. return (1);
  635. }
  636. static int
  637. ospf6_decode_v3(netdissect_options *ndo,
  638. register const struct ospf6hdr *op,
  639. register const u_char *dataend)
  640. {
  641. register const rtrid_t *ap;
  642. register const struct lsr6 *lsrp;
  643. register const struct lsa6_hdr *lshp;
  644. register const struct lsa6 *lsap;
  645. register int i;
  646. switch (op->ospf6_type) {
  647. case OSPF_TYPE_HELLO: {
  648. register const struct hello6 *hellop = (const struct hello6 *)((const uint8_t *)op + OSPF6HDR_LEN);
  649. ND_TCHECK_32BITS(&hellop->hello_options);
  650. ND_PRINT((ndo, "\n\tOptions [%s]",
  651. bittok2str(ospf6_option_values, "none",
  652. EXTRACT_32BITS(&hellop->hello_options))));
  653. ND_TCHECK(hellop->hello_deadint);
  654. ND_PRINT((ndo, "\n\t Hello Timer %us, Dead Timer %us, Interface-ID %s, Priority %u",
  655. EXTRACT_16BITS(&hellop->hello_helloint),
  656. EXTRACT_16BITS(&hellop->hello_deadint),
  657. ipaddr_string(ndo, &hellop->hello_ifid),
  658. hellop->hello_priority));
  659. ND_TCHECK(hellop->hello_dr);
  660. if (EXTRACT_32BITS(&hellop->hello_dr) != 0)
  661. ND_PRINT((ndo, "\n\t Designated Router %s",
  662. ipaddr_string(ndo, &hellop->hello_dr)));
  663. ND_TCHECK(hellop->hello_bdr);
  664. if (EXTRACT_32BITS(&hellop->hello_bdr) != 0)
  665. ND_PRINT((ndo, ", Backup Designated Router %s",
  666. ipaddr_string(ndo, &hellop->hello_bdr)));
  667. if (ndo->ndo_vflag > 1) {
  668. ND_PRINT((ndo, "\n\t Neighbor List:"));
  669. ap = hellop->hello_neighbor;
  670. while ((const u_char *)ap < dataend) {
  671. ND_TCHECK(*ap);
  672. ND_PRINT((ndo, "\n\t %s", ipaddr_string(ndo, ap)));
  673. ++ap;
  674. }
  675. }
  676. break; /* HELLO */
  677. }
  678. case OSPF_TYPE_DD: {
  679. register const struct dd6 *ddp = (const struct dd6 *)((const uint8_t *)op + OSPF6HDR_LEN);
  680. ND_TCHECK(ddp->db_options);
  681. ND_PRINT((ndo, "\n\tOptions [%s]",
  682. bittok2str(ospf6_option_values, "none",
  683. EXTRACT_32BITS(&ddp->db_options))));
  684. ND_TCHECK(ddp->db_flags);
  685. ND_PRINT((ndo, ", DD Flags [%s]",
  686. bittok2str(ospf6_dd_flag_values,"none",ddp->db_flags)));
  687. ND_TCHECK(ddp->db_seq);
  688. ND_PRINT((ndo, ", MTU %u, DD-Sequence 0x%08x",
  689. EXTRACT_16BITS(&ddp->db_mtu),
  690. EXTRACT_32BITS(&ddp->db_seq)));
  691. if (ndo->ndo_vflag > 1) {
  692. /* Print all the LS adv's */
  693. lshp = ddp->db_lshdr;
  694. while ((const u_char *)lshp < dataend) {
  695. if (ospf6_print_lshdr(ndo, lshp++, dataend))
  696. goto trunc;
  697. }
  698. }
  699. break;
  700. }
  701. case OSPF_TYPE_LS_REQ:
  702. if (ndo->ndo_vflag > 1) {
  703. lsrp = (const struct lsr6 *)((const uint8_t *)op + OSPF6HDR_LEN);
  704. while ((const u_char *)lsrp < dataend) {
  705. ND_TCHECK(*lsrp);
  706. ND_PRINT((ndo, "\n\t Advertising Router %s",
  707. ipaddr_string(ndo, &lsrp->ls_router)));
  708. ospf6_print_ls_type(ndo, EXTRACT_16BITS(&lsrp->ls_type),
  709. &lsrp->ls_stateid);
  710. ++lsrp;
  711. }
  712. }
  713. break;
  714. case OSPF_TYPE_LS_UPDATE:
  715. if (ndo->ndo_vflag > 1) {
  716. register const struct lsu6 *lsup = (const struct lsu6 *)((const uint8_t *)op + OSPF6HDR_LEN);
  717. ND_TCHECK(lsup->lsu_count);
  718. i = EXTRACT_32BITS(&lsup->lsu_count);
  719. lsap = lsup->lsu_lsa;
  720. while ((const u_char *)lsap < dataend && i--) {
  721. if (ospf6_print_lsa(ndo, lsap, dataend))
  722. goto trunc;
  723. lsap = (const struct lsa6 *)((const u_char *)lsap +
  724. EXTRACT_16BITS(&lsap->ls_hdr.ls_length));
  725. }
  726. }
  727. break;
  728. case OSPF_TYPE_LS_ACK:
  729. if (ndo->ndo_vflag > 1) {
  730. lshp = (const struct lsa6_hdr *)((const uint8_t *)op + OSPF6HDR_LEN);
  731. while ((const u_char *)lshp < dataend) {
  732. if (ospf6_print_lshdr(ndo, lshp++, dataend))
  733. goto trunc;
  734. }
  735. }
  736. break;
  737. default:
  738. break;
  739. }
  740. return (0);
  741. trunc:
  742. return (1);
  743. }
  744. /* RFC5613 Section 2.2 (w/o the TLVs) */
  745. static int
  746. ospf6_print_lls(netdissect_options *ndo,
  747. const u_char *cp, const u_int len)
  748. {
  749. uint16_t llsdatalen;
  750. if (len == 0)
  751. return 0;
  752. if (len < OSPF_LLS_HDRLEN)
  753. goto trunc;
  754. /* Checksum */
  755. ND_TCHECK2(*cp, 2);
  756. ND_PRINT((ndo, "\n\tLLS Checksum 0x%04x", EXTRACT_16BITS(cp)));
  757. cp += 2;
  758. /* LLS Data Length */
  759. ND_TCHECK2(*cp, 2);
  760. llsdatalen = EXTRACT_16BITS(cp);
  761. ND_PRINT((ndo, ", Data Length %u", llsdatalen));
  762. if (llsdatalen < OSPF_LLS_HDRLEN || llsdatalen > len)
  763. goto trunc;
  764. cp += 2;
  765. /* LLS TLVs */
  766. ND_TCHECK2(*cp, llsdatalen - OSPF_LLS_HDRLEN);
  767. /* FIXME: code in print-ospf.c can be reused to decode the TLVs */
  768. return llsdatalen;
  769. trunc:
  770. return -1;
  771. }
  772. /* RFC6506 Section 4.1 */
  773. static int
  774. ospf6_decode_at(netdissect_options *ndo,
  775. const u_char *cp, const u_int len)
  776. {
  777. uint16_t authdatalen;
  778. if (len == 0)
  779. return 0;
  780. if (len < OSPF6_AT_HDRLEN)
  781. goto trunc;
  782. /* Authentication Type */
  783. ND_TCHECK2(*cp, 2);
  784. ND_PRINT((ndo, "\n\tAuthentication Type %s", tok2str(ospf6_auth_type_str, "unknown (0x%04x)", EXTRACT_16BITS(cp))));
  785. cp += 2;
  786. /* Auth Data Len */
  787. ND_TCHECK2(*cp, 2);
  788. authdatalen = EXTRACT_16BITS(cp);
  789. ND_PRINT((ndo, ", Length %u", authdatalen));
  790. if (authdatalen < OSPF6_AT_HDRLEN || authdatalen > len)
  791. goto trunc;
  792. cp += 2;
  793. /* Reserved */
  794. ND_TCHECK2(*cp, 2);
  795. cp += 2;
  796. /* Security Association ID */
  797. ND_TCHECK2(*cp, 2);
  798. ND_PRINT((ndo, ", SAID %u", EXTRACT_16BITS(cp)));
  799. cp += 2;
  800. /* Cryptographic Sequence Number (High-Order 32 Bits) */
  801. ND_TCHECK2(*cp, 4);
  802. ND_PRINT((ndo, ", CSN 0x%08x", EXTRACT_32BITS(cp)));
  803. cp += 4;
  804. /* Cryptographic Sequence Number (Low-Order 32 Bits) */
  805. ND_TCHECK2(*cp, 4);
  806. ND_PRINT((ndo, ":%08x", EXTRACT_32BITS(cp)));
  807. cp += 4;
  808. /* Authentication Data */
  809. ND_TCHECK2(*cp, authdatalen - OSPF6_AT_HDRLEN);
  810. if (ndo->ndo_vflag > 1)
  811. print_unknown_data(ndo,cp, "\n\tAuthentication Data ", authdatalen - OSPF6_AT_HDRLEN);
  812. return 0;
  813. trunc:
  814. return 1;
  815. }
  816. /* The trailing data may include LLS and/or AT data (in this specific order).
  817. * LLS data may be present only in Hello and DBDesc packets with the L-bit set.
  818. * AT data may be present in Hello and DBDesc packets with the AT-bit set or in
  819. * any other packet type, thus decode the AT data regardless of the AT-bit.
  820. */
  821. static int
  822. ospf6_decode_v3_trailer(netdissect_options *ndo,
  823. const struct ospf6hdr *op, const u_char *cp, const unsigned len)
  824. {
  825. int llslen = 0;
  826. int lls_hello = 0;
  827. int lls_dd = 0;
  828. if (op->ospf6_type == OSPF_TYPE_HELLO) {
  829. const struct hello6 *hellop = (const struct hello6 *)((const uint8_t *)op + OSPF6HDR_LEN);
  830. ND_TCHECK(hellop->hello_options);
  831. if (EXTRACT_32BITS(&hellop->hello_options) & OSPF6_OPTION_L)
  832. lls_hello = 1;
  833. } else if (op->ospf6_type == OSPF_TYPE_DD) {
  834. const struct dd6 *ddp = (const struct dd6 *)((const uint8_t *)op + OSPF6HDR_LEN);
  835. ND_TCHECK(ddp->db_options);
  836. if (EXTRACT_32BITS(&ddp->db_options) & OSPF6_OPTION_L)
  837. lls_dd = 1;
  838. }
  839. if ((lls_hello || lls_dd) && (llslen = ospf6_print_lls(ndo, cp, len)) < 0)
  840. goto trunc;
  841. return ospf6_decode_at(ndo, cp + llslen, len - llslen);
  842. trunc:
  843. return 1;
  844. }
  845. void
  846. ospf6_print(netdissect_options *ndo,
  847. register const u_char *bp, register u_int length)
  848. {
  849. register const struct ospf6hdr *op;
  850. register const u_char *dataend;
  851. register const char *cp;
  852. uint16_t datalen;
  853. op = (const struct ospf6hdr *)bp;
  854. /* If the type is valid translate it, or just print the type */
  855. /* value. If it's not valid, say so and return */
  856. ND_TCHECK(op->ospf6_type);
  857. cp = tok2str(ospf6_type_values, "unknown packet type (%u)", op->ospf6_type);
  858. ND_PRINT((ndo, "OSPFv%u, %s, length %d", op->ospf6_version, cp, length));
  859. if (*cp == 'u') {
  860. return;
  861. }
  862. if(!ndo->ndo_vflag) { /* non verbose - so lets bail out here */
  863. return;
  864. }
  865. /* OSPFv3 data always comes first and optional trailing data may follow. */
  866. ND_TCHECK(op->ospf6_len);
  867. datalen = EXTRACT_16BITS(&op->ospf6_len);
  868. if (datalen > length) {
  869. ND_PRINT((ndo, " [len %d]", datalen));
  870. return;
  871. }
  872. dataend = bp + datalen;
  873. ND_TCHECK(op->ospf6_routerid);
  874. ND_PRINT((ndo, "\n\tRouter-ID %s", ipaddr_string(ndo, &op->ospf6_routerid)));
  875. ND_TCHECK(op->ospf6_areaid);
  876. if (EXTRACT_32BITS(&op->ospf6_areaid) != 0)
  877. ND_PRINT((ndo, ", Area %s", ipaddr_string(ndo, &op->ospf6_areaid)));
  878. else
  879. ND_PRINT((ndo, ", Backbone Area"));
  880. ND_TCHECK(op->ospf6_instanceid);
  881. if (op->ospf6_instanceid)
  882. ND_PRINT((ndo, ", Instance %u", op->ospf6_instanceid));
  883. /* Do rest according to version. */
  884. switch (op->ospf6_version) {
  885. case 3:
  886. /* ospf version 3 */
  887. if (ospf6_decode_v3(ndo, op, dataend) ||
  888. ospf6_decode_v3_trailer(ndo, op, dataend, length - datalen))
  889. goto trunc;
  890. break;
  891. } /* end switch on version */
  892. return;
  893. trunc:
  894. ND_PRINT((ndo, "%s", tstr));
  895. }