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