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