]> The Tcpdump Group git mirrors - tcpdump/blob - print-ospf6.c
Use nd_ipv4 rather than struct in_addr
[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
288 static const struct tok ospf6_option_values[] = {
289 { OSPF6_OPTION_V6, "V6" },
290 { OSPF6_OPTION_E, "External" },
291 { OSPF6_OPTION_MC, "Deprecated" },
292 { OSPF6_OPTION_N, "NSSA" },
293 { OSPF6_OPTION_R, "Router" },
294 { OSPF6_OPTION_DC, "Demand Circuit" },
295 { OSPF6_OPTION_AF, "AFs Support" },
296 { OSPF6_OPTION_L, "LLS" },
297 { OSPF6_OPTION_AT, "Authentication Trailer" },
298 { 0, NULL }
299 };
300
301 static const struct tok ospf6_rla_flag_values[] = {
302 { RLA_FLAG_B, "ABR" },
303 { RLA_FLAG_E, "External" },
304 { RLA_FLAG_V, "Virtual-Link Endpoint" },
305 { RLA_FLAG_W, "Wildcard Receiver" },
306 { RLA_FLAG_N, "NSSA Translator" },
307 { 0, NULL }
308 };
309
310 static const struct tok ospf6_asla_flag_values[] = {
311 { ASLA_FLAG_EXTERNAL, "External Type 2" },
312 { ASLA_FLAG_FWDADDR, "Forwarding" },
313 { ASLA_FLAG_ROUTETAG, "Tag" },
314 { 0, NULL }
315 };
316
317 static const struct tok ospf6_type_values[] = {
318 { OSPF_TYPE_HELLO, "Hello" },
319 { OSPF_TYPE_DD, "Database Description" },
320 { OSPF_TYPE_LS_REQ, "LS-Request" },
321 { OSPF_TYPE_LS_UPDATE, "LS-Update" },
322 { OSPF_TYPE_LS_ACK, "LS-Ack" },
323 { 0, NULL }
324 };
325
326 static const struct tok ospf6_lsa_values[] = {
327 { LS_TYPE_ROUTER, "Router" },
328 { LS_TYPE_NETWORK, "Network" },
329 { LS_TYPE_INTER_AP, "Inter-Area Prefix" },
330 { LS_TYPE_INTER_AR, "Inter-Area Router" },
331 { LS_TYPE_ASE, "External" },
332 { LS_TYPE_GROUP, "Deprecated" },
333 { LS_TYPE_NSSA, "NSSA" },
334 { LS_TYPE_LINK, "Link" },
335 { LS_TYPE_INTRA_AP, "Intra-Area Prefix" },
336 { LS_TYPE_INTRA_ATE, "Intra-Area TE" },
337 { LS_TYPE_GRACE, "Grace" },
338 { LS_TYPE_RI, "Router Information" },
339 { LS_TYPE_INTER_ASTE, "Inter-AS-TE" },
340 { LS_TYPE_L1VPN, "Layer 1 VPN" },
341 { 0, NULL }
342 };
343
344 static const struct tok ospf6_ls_scope_values[] = {
345 { LS_SCOPE_LINKLOCAL, "Link Local" },
346 { LS_SCOPE_AREA, "Area Local" },
347 { LS_SCOPE_AS, "Domain Wide" },
348 { 0, NULL }
349 };
350
351 static const struct tok ospf6_dd_flag_values[] = {
352 { OSPF6_DB_INIT, "Init" },
353 { OSPF6_DB_MORE, "More" },
354 { OSPF6_DB_MASTER, "Master" },
355 { OSPF6_DB_M6, "IPv6 MTU" },
356 { 0, NULL }
357 };
358
359 static const struct tok ospf6_lsa_prefix_option_values[] = {
360 { LSA_PREFIX_OPT_NU, "No Unicast" },
361 { LSA_PREFIX_OPT_LA, "Local address" },
362 { LSA_PREFIX_OPT_MC, "Deprecated" },
363 { LSA_PREFIX_OPT_P, "Propagate" },
364 { LSA_PREFIX_OPT_DN, "Down" },
365 { 0, NULL }
366 };
367
368 static const struct tok ospf6_auth_type_str[] = {
369 { OSPF6_AUTH_TYPE_HMAC, "HMAC" },
370 { 0, NULL }
371 };
372
373 static void
374 ospf6_print_ls_type(netdissect_options *ndo,
375 u_int ls_type, const rtrid_t *ls_stateid)
376 {
377 ND_PRINT("\n\t %s LSA (%u), %s Scope%s, LSA-ID %s",
378 tok2str(ospf6_lsa_values, "Unknown", ls_type & LS_TYPE_MASK),
379 ls_type & LS_TYPE_MASK,
380 tok2str(ospf6_ls_scope_values, "Unknown", ls_type & LS_SCOPE_MASK),
381 ls_type &0x8000 ? ", transitive" : "", /* U-bit */
382 ipaddr_string(ndo, (const u_char *)ls_stateid));
383 }
384
385 static int
386 ospf6_print_lshdr(netdissect_options *ndo,
387 const struct lsa6_hdr *lshp, const u_char *dataend)
388 {
389 if ((const u_char *)(lshp + 1) > dataend)
390 goto trunc;
391 ND_TCHECK_4(lshp->ls_router);
392
393 ND_PRINT("\n\t Advertising Router %s, seq 0x%08x, age %us, length %u",
394 ipaddr_string(ndo, lshp->ls_router),
395 GET_BE_U_4(lshp->ls_seq),
396 GET_BE_U_2(lshp->ls_age),
397 GET_BE_U_2(lshp->ls_length)-(u_int)sizeof(struct lsa6_hdr));
398
399 ospf6_print_ls_type(ndo, GET_BE_U_2(lshp->ls_type),
400 &lshp->ls_stateid);
401
402 return (0);
403 trunc:
404 return (1);
405 }
406
407 static int
408 ospf6_print_lsaprefix(netdissect_options *ndo,
409 const uint8_t *tptr, u_int lsa_length)
410 {
411 const struct lsa6_prefix *lsapp = (const struct lsa6_prefix *)tptr;
412 u_int wordlen;
413 nd_ipv6 prefix;
414
415 if (lsa_length < sizeof (*lsapp) - IPV6_ADDR_LEN_BYTES)
416 goto trunc;
417 lsa_length -= sizeof (*lsapp) - IPV6_ADDR_LEN_BYTES;
418 ND_TCHECK_LEN(lsapp, sizeof(*lsapp) - IPV6_ADDR_LEN_BYTES);
419 wordlen = (GET_U_1(lsapp->lsa_p_len) + 31) / 32;
420 if (wordlen * 4 > sizeof(nd_ipv6)) {
421 ND_PRINT(" bogus prefixlen /%u", GET_U_1(lsapp->lsa_p_len));
422 goto trunc;
423 }
424 if (lsa_length < wordlen * 4)
425 goto trunc;
426 lsa_length -= wordlen * 4;
427 ND_TCHECK_LEN(lsapp->lsa_p_prefix, wordlen * 4);
428 memset(prefix, 0, sizeof(prefix));
429 memcpy(prefix, lsapp->lsa_p_prefix, wordlen * 4);
430 ND_PRINT("\n\t\t%s/%u", ip6addr_string(ndo, prefix),
431 GET_U_1(lsapp->lsa_p_len));
432 if (GET_U_1(lsapp->lsa_p_opt)) {
433 ND_PRINT(", Options [%s]",
434 bittok2str(ospf6_lsa_prefix_option_values,
435 "none", GET_U_1(lsapp->lsa_p_opt)));
436 }
437 ND_PRINT(", metric %u", GET_BE_U_2(lsapp->lsa_p_metric));
438 return sizeof(*lsapp) - IPV6_ADDR_LEN_BYTES + wordlen * 4;
439
440 trunc:
441 return -1;
442 }
443
444
445 /*
446 * Print a single link state advertisement. If truncated return 1, else 0.
447 */
448 static int
449 ospf6_print_lsa(netdissect_options *ndo,
450 const struct lsa6 *lsap, const u_char *dataend)
451 {
452 const struct rlalink6 *rlp;
453 #if 0
454 const struct tos_metric *tosp;
455 #endif
456 const rtrid_t *ap;
457 #if 0
458 const struct aslametric *almp;
459 const struct mcla *mcp;
460 #endif
461 const struct llsa *llsap;
462 const struct lsa6_prefix *lsapp;
463 #if 0
464 const uint32_t *lp;
465 #endif
466 u_int prefixes;
467 int bytelen;
468 u_int length, lsa_length;
469 uint32_t flags32;
470 const uint8_t *tptr;
471
472 if (ospf6_print_lshdr(ndo, &lsap->ls_hdr, dataend))
473 return (1);
474 ND_TCHECK_2(lsap->ls_hdr.ls_length);
475 length = GET_BE_U_2(lsap->ls_hdr.ls_length);
476
477 /*
478 * The LSA length includes the length of the header;
479 * it must have a value that's at least that length.
480 * If it does, find the length of what follows the
481 * header.
482 */
483 if (length < sizeof(struct lsa6_hdr) || (const u_char *)lsap + length > dataend)
484 return (1);
485 lsa_length = length - sizeof(struct lsa6_hdr);
486 tptr = (const uint8_t *)lsap+sizeof(struct lsa6_hdr);
487
488 switch (GET_BE_U_2(lsap->ls_hdr.ls_type)) {
489 case LS_TYPE_ROUTER | LS_SCOPE_AREA:
490 if (lsa_length < sizeof (lsap->lsa_un.un_rla.rla_options))
491 return (1);
492 lsa_length -= sizeof (lsap->lsa_un.un_rla.rla_options);
493 ND_TCHECK_4(lsap->lsa_un.un_rla.rla_options);
494 ND_PRINT("\n\t Options [%s]",
495 bittok2str(ospf6_option_values, "none",
496 GET_BE_U_4(lsap->lsa_un.un_rla.rla_options)));
497 ND_PRINT(", RLA-Flags [%s]",
498 bittok2str(ospf6_rla_flag_values, "none",
499 GET_U_1(lsap->lsa_un.un_rla.rla_flags)));
500
501 rlp = lsap->lsa_un.un_rla.rla_link;
502 while (lsa_length != 0) {
503 if (lsa_length < sizeof (*rlp))
504 return (1);
505 lsa_length -= sizeof (*rlp);
506 ND_TCHECK_SIZE(rlp);
507 switch (GET_U_1(rlp->link_type)) {
508
509 case RLA_TYPE_VIRTUAL:
510 ND_PRINT("\n\t Virtual Link: Neighbor Router-ID %s"
511 "\n\t Neighbor Interface-ID %s, Interface %s",
512 ipaddr_string(ndo, rlp->link_nrtid),
513 ipaddr_string(ndo, rlp->link_nifid),
514 ipaddr_string(ndo, rlp->link_ifid));
515 break;
516
517 case RLA_TYPE_ROUTER:
518 ND_PRINT("\n\t Neighbor Router-ID %s"
519 "\n\t Neighbor Interface-ID %s, Interface %s",
520 ipaddr_string(ndo, rlp->link_nrtid),
521 ipaddr_string(ndo, rlp->link_nifid),
522 ipaddr_string(ndo, rlp->link_ifid));
523 break;
524
525 case RLA_TYPE_TRANSIT:
526 ND_PRINT("\n\t Neighbor Network-ID %s"
527 "\n\t Neighbor Interface-ID %s, Interface %s",
528 ipaddr_string(ndo, rlp->link_nrtid),
529 ipaddr_string(ndo, rlp->link_nifid),
530 ipaddr_string(ndo, rlp->link_ifid));
531 break;
532
533 default:
534 ND_PRINT("\n\t Unknown Router Links Type 0x%02x",
535 GET_U_1(rlp->link_type));
536 return (0);
537 }
538 ND_PRINT(", metric %u", GET_BE_U_2(rlp->link_metric));
539 rlp++;
540 }
541 break;
542
543 case LS_TYPE_NETWORK | LS_SCOPE_AREA:
544 if (lsa_length < sizeof (lsap->lsa_un.un_nla.nla_options))
545 return (1);
546 lsa_length -= sizeof (lsap->lsa_un.un_nla.nla_options);
547 ND_TCHECK_4(lsap->lsa_un.un_nla.nla_options);
548 ND_PRINT("\n\t Options [%s]",
549 bittok2str(ospf6_option_values, "none",
550 GET_BE_U_4(lsap->lsa_un.un_nla.nla_options)));
551
552 ND_PRINT("\n\t Connected Routers:");
553 ap = lsap->lsa_un.un_nla.nla_router;
554 while (lsa_length != 0) {
555 if (lsa_length < sizeof (*ap))
556 return (1);
557 lsa_length -= sizeof (*ap);
558 ND_TCHECK_SIZE(ap);
559 ND_PRINT("\n\t\t%s", ipaddr_string(ndo, *ap));
560 ++ap;
561 }
562 break;
563
564 case LS_TYPE_INTER_AP | LS_SCOPE_AREA:
565 if (lsa_length < sizeof (lsap->lsa_un.un_inter_ap.inter_ap_metric))
566 return (1);
567 lsa_length -= sizeof (lsap->lsa_un.un_inter_ap.inter_ap_metric);
568 ND_TCHECK_4(lsap->lsa_un.un_inter_ap.inter_ap_metric);
569 ND_PRINT(", metric %u",
570 GET_BE_U_4(lsap->lsa_un.un_inter_ap.inter_ap_metric) & SLA_MASK_METRIC);
571
572 tptr = (const uint8_t *)lsap->lsa_un.un_inter_ap.inter_ap_prefix;
573 while (lsa_length != 0) {
574 bytelen = ospf6_print_lsaprefix(ndo, tptr, lsa_length);
575 if (bytelen < 0)
576 goto trunc;
577 lsa_length -= bytelen;
578 tptr += bytelen;
579 }
580 break;
581
582 case LS_TYPE_ASE | LS_SCOPE_AS:
583 if (lsa_length < sizeof (lsap->lsa_un.un_asla.asla_metric))
584 return (1);
585 lsa_length -= sizeof (lsap->lsa_un.un_asla.asla_metric);
586 ND_TCHECK_4(lsap->lsa_un.un_asla.asla_metric);
587 flags32 = GET_BE_U_4(lsap->lsa_un.un_asla.asla_metric);
588 ND_PRINT("\n\t Flags [%s]",
589 bittok2str(ospf6_asla_flag_values, "none", flags32));
590 ND_PRINT(" metric %u",
591 GET_BE_U_4(lsap->lsa_un.un_asla.asla_metric) &
592 ASLA_MASK_METRIC);
593
594 tptr = (const uint8_t *)lsap->lsa_un.un_asla.asla_prefix;
595 lsapp = (const struct lsa6_prefix *)tptr;
596 bytelen = ospf6_print_lsaprefix(ndo, tptr, lsa_length);
597 if (bytelen < 0)
598 goto trunc;
599 lsa_length -= bytelen;
600 tptr += bytelen;
601
602 if ((flags32 & ASLA_FLAG_FWDADDR) != 0) {
603 if (lsa_length < sizeof (nd_ipv6))
604 return (1);
605 lsa_length -= sizeof (nd_ipv6);
606 ND_TCHECK_16(tptr);
607 ND_PRINT(" forward %s",
608 ip6addr_string(ndo, tptr));
609 tptr += sizeof(nd_ipv6);
610 }
611
612 if ((flags32 & ASLA_FLAG_ROUTETAG) != 0) {
613 if (lsa_length < sizeof (uint32_t))
614 return (1);
615 lsa_length -= sizeof (uint32_t);
616 ND_TCHECK_4(tptr);
617 ND_PRINT(" tag %s",
618 ipaddr_string(ndo, tptr));
619 tptr += sizeof(uint32_t);
620 }
621
622 if (GET_U_1(lsapp->lsa_p_metric)) {
623 if (lsa_length < sizeof (uint32_t))
624 return (1);
625 lsa_length -= sizeof (uint32_t);
626 ND_TCHECK_4(tptr);
627 ND_PRINT(" RefLSID: %s",
628 ipaddr_string(ndo, tptr));
629 tptr += sizeof(uint32_t);
630 }
631 break;
632
633 case LS_TYPE_LINK:
634 /* Link LSA */
635 llsap = &lsap->lsa_un.un_llsa;
636 if (lsa_length < sizeof (llsap->llsa_priandopt))
637 return (1);
638 lsa_length -= sizeof (llsap->llsa_priandopt);
639 ND_TCHECK_SIZE(&llsap->llsa_priandopt);
640 ND_PRINT("\n\t Options [%s]",
641 bittok2str(ospf6_option_values, "none",
642 GET_BE_U_4(llsap->llsa_options)));
643
644 if (lsa_length < sizeof (llsap->llsa_lladdr) + sizeof (llsap->llsa_nprefix))
645 return (1);
646 lsa_length -= sizeof (llsap->llsa_lladdr) + sizeof (llsap->llsa_nprefix);
647 ND_TCHECK_4(llsap->llsa_nprefix);
648 prefixes = GET_BE_U_4(llsap->llsa_nprefix);
649 ND_PRINT("\n\t Priority %u, Link-local address %s, Prefixes %u:",
650 GET_U_1(llsap->llsa_priority),
651 ip6addr_string(ndo, llsap->llsa_lladdr),
652 prefixes);
653
654 tptr = (const uint8_t *)llsap->llsa_prefix;
655 while (prefixes > 0) {
656 bytelen = ospf6_print_lsaprefix(ndo, tptr, lsa_length);
657 if (bytelen < 0)
658 goto trunc;
659 prefixes--;
660 lsa_length -= bytelen;
661 tptr += bytelen;
662 }
663 break;
664
665 case LS_TYPE_INTRA_AP | LS_SCOPE_AREA:
666 /* Intra-Area-Prefix LSA */
667 if (lsa_length < sizeof (lsap->lsa_un.un_intra_ap.intra_ap_rtid))
668 return (1);
669 lsa_length -= sizeof (lsap->lsa_un.un_intra_ap.intra_ap_rtid);
670 ND_TCHECK_4(lsap->lsa_un.un_intra_ap.intra_ap_rtid);
671 ospf6_print_ls_type(ndo,
672 GET_BE_U_2(lsap->lsa_un.un_intra_ap.intra_ap_lstype),
673 &lsap->lsa_un.un_intra_ap.intra_ap_lsid);
674
675 if (lsa_length < sizeof (lsap->lsa_un.un_intra_ap.intra_ap_nprefix))
676 return (1);
677 lsa_length -= sizeof (lsap->lsa_un.un_intra_ap.intra_ap_nprefix);
678 ND_TCHECK_2(lsap->lsa_un.un_intra_ap.intra_ap_nprefix);
679 prefixes = GET_BE_U_2(lsap->lsa_un.un_intra_ap.intra_ap_nprefix);
680 ND_PRINT("\n\t Prefixes %u:", prefixes);
681
682 tptr = (const uint8_t *)lsap->lsa_un.un_intra_ap.intra_ap_prefix;
683 while (prefixes > 0) {
684 bytelen = ospf6_print_lsaprefix(ndo, tptr, lsa_length);
685 if (bytelen < 0)
686 goto trunc;
687 prefixes--;
688 lsa_length -= bytelen;
689 tptr += bytelen;
690 }
691 break;
692
693 case LS_TYPE_GRACE | LS_SCOPE_LINKLOCAL:
694 if (ospf_grace_lsa_print(ndo, tptr, lsa_length) == -1) {
695 return 1;
696 }
697 break;
698
699 case LS_TYPE_INTRA_ATE | LS_SCOPE_LINKLOCAL:
700 if (ospf_te_lsa_print(ndo, tptr, lsa_length) == -1) {
701 return 1;
702 }
703 break;
704
705 default:
706 if(!print_unknown_data(ndo,tptr,
707 "\n\t ",
708 lsa_length)) {
709 return (1);
710 }
711 break;
712 }
713
714 return (0);
715 trunc:
716 return (1);
717 }
718
719 static int
720 ospf6_decode_v3(netdissect_options *ndo,
721 const struct ospf6hdr *op,
722 const u_char *dataend)
723 {
724 const rtrid_t *ap;
725 const struct lsr6 *lsrp;
726 const struct lsa6_hdr *lshp;
727 const struct lsa6 *lsap;
728 int i;
729
730 switch (GET_U_1(op->ospf6_type)) {
731
732 case OSPF_TYPE_HELLO: {
733 const struct hello6 *hellop = (const struct hello6 *)((const uint8_t *)op + OSPF6HDR_LEN);
734
735 ND_TCHECK_4(hellop->hello_options);
736 ND_PRINT("\n\tOptions [%s]",
737 bittok2str(ospf6_option_values, "none",
738 GET_BE_U_4(hellop->hello_options)));
739
740 ND_TCHECK_2(hellop->hello_deadint);
741 ND_PRINT("\n\t Hello Timer %us, Dead Timer %us, Interface-ID %s, Priority %u",
742 GET_BE_U_2(hellop->hello_helloint),
743 GET_BE_U_2(hellop->hello_deadint),
744 ipaddr_string(ndo, hellop->hello_ifid),
745 GET_U_1(hellop->hello_priority));
746
747 ND_TCHECK_4(hellop->hello_dr);
748 if (GET_BE_U_4(hellop->hello_dr) != 0)
749 ND_PRINT("\n\t Designated Router %s",
750 ipaddr_string(ndo, hellop->hello_dr));
751 ND_TCHECK_4(hellop->hello_bdr);
752 if (GET_BE_U_4(hellop->hello_bdr) != 0)
753 ND_PRINT(", Backup Designated Router %s",
754 ipaddr_string(ndo, hellop->hello_bdr));
755 if (ndo->ndo_vflag > 1) {
756 ND_PRINT("\n\t Neighbor List:");
757 ap = hellop->hello_neighbor;
758 while ((const u_char *)ap < dataend) {
759 ND_TCHECK_SIZE(ap);
760 ND_PRINT("\n\t %s", ipaddr_string(ndo, *ap));
761 ++ap;
762 }
763 }
764 break; /* HELLO */
765 }
766
767 case OSPF_TYPE_DD: {
768 const struct dd6 *ddp = (const struct dd6 *)((const uint8_t *)op + OSPF6HDR_LEN);
769
770 ND_TCHECK_4(ddp->db_options);
771 ND_PRINT("\n\tOptions [%s]",
772 bittok2str(ospf6_option_values, "none",
773 GET_BE_U_4(ddp->db_options)));
774 ND_TCHECK_1(ddp->db_flags);
775 ND_PRINT(", DD Flags [%s]",
776 bittok2str(ospf6_dd_flag_values,"none",GET_U_1(ddp->db_flags)));
777
778 ND_TCHECK_4(ddp->db_seq);
779 ND_PRINT(", MTU %u, DD-Sequence 0x%08x",
780 GET_BE_U_2(ddp->db_mtu),
781 GET_BE_U_4(ddp->db_seq));
782 if (ndo->ndo_vflag > 1) {
783 /* Print all the LS adv's */
784 lshp = ddp->db_lshdr;
785 while ((const u_char *)lshp < dataend) {
786 if (ospf6_print_lshdr(ndo, lshp++, dataend))
787 goto trunc;
788 }
789 }
790 break;
791 }
792
793 case OSPF_TYPE_LS_REQ:
794 if (ndo->ndo_vflag > 1) {
795 lsrp = (const struct lsr6 *)((const uint8_t *)op + OSPF6HDR_LEN);
796 while ((const u_char *)lsrp < dataend) {
797 ND_TCHECK_SIZE(lsrp);
798 ND_PRINT("\n\t Advertising Router %s",
799 ipaddr_string(ndo, lsrp->ls_router));
800 ospf6_print_ls_type(ndo,
801 GET_BE_U_2(lsrp->ls_type),
802 &lsrp->ls_stateid);
803 ++lsrp;
804 }
805 }
806 break;
807
808 case OSPF_TYPE_LS_UPDATE:
809 if (ndo->ndo_vflag > 1) {
810 const struct lsu6 *lsup = (const struct lsu6 *)((const uint8_t *)op + OSPF6HDR_LEN);
811
812 ND_TCHECK_4(lsup->lsu_count);
813 i = GET_BE_U_4(lsup->lsu_count);
814 lsap = lsup->lsu_lsa;
815 while ((const u_char *)lsap < dataend && i--) {
816 if (ospf6_print_lsa(ndo, lsap, dataend))
817 goto trunc;
818 lsap = (const struct lsa6 *)((const u_char *)lsap +
819 GET_BE_U_2(lsap->ls_hdr.ls_length));
820 }
821 }
822 break;
823
824 case OSPF_TYPE_LS_ACK:
825 if (ndo->ndo_vflag > 1) {
826 lshp = (const struct lsa6_hdr *)((const uint8_t *)op + OSPF6HDR_LEN);
827 while ((const u_char *)lshp < dataend) {
828 if (ospf6_print_lshdr(ndo, lshp++, dataend))
829 goto trunc;
830 }
831 }
832 break;
833
834 default:
835 break;
836 }
837 return (0);
838 trunc:
839 return (1);
840 }
841
842 /* RFC5613 Section 2.2 (w/o the TLVs) */
843 static int
844 ospf6_print_lls(netdissect_options *ndo,
845 const u_char *cp, const u_int len)
846 {
847 uint16_t llsdatalen;
848
849 if (len == 0)
850 return 0;
851 if (len < OSPF_LLS_HDRLEN)
852 goto trunc;
853 /* Checksum */
854 ND_TCHECK_2(cp);
855 ND_PRINT("\n\tLLS Checksum 0x%04x", GET_BE_U_2(cp));
856 cp += 2;
857 /* LLS Data Length */
858 ND_TCHECK_2(cp);
859 llsdatalen = GET_BE_U_2(cp);
860 ND_PRINT(", Data Length %u", llsdatalen);
861 if (llsdatalen < OSPF_LLS_HDRLEN || llsdatalen > len)
862 goto trunc;
863 cp += 2;
864 /* LLS TLVs */
865 ND_TCHECK_LEN(cp, llsdatalen - OSPF_LLS_HDRLEN);
866 /* FIXME: code in print-ospf.c can be reused to decode the TLVs */
867
868 return llsdatalen;
869 trunc:
870 return -1;
871 }
872
873 /* RFC6506 Section 4.1 */
874 static int
875 ospf6_decode_at(netdissect_options *ndo,
876 const u_char *cp, const u_int len)
877 {
878 uint16_t authdatalen;
879
880 if (len == 0)
881 return 0;
882 if (len < OSPF6_AT_HDRLEN)
883 goto trunc;
884 /* Authentication Type */
885 ND_TCHECK_2(cp);
886 ND_PRINT("\n\tAuthentication Type %s",
887 tok2str(ospf6_auth_type_str, "unknown (0x%04x)", GET_BE_U_2(cp)));
888 cp += 2;
889 /* Auth Data Len */
890 ND_TCHECK_2(cp);
891 authdatalen = GET_BE_U_2(cp);
892 ND_PRINT(", Length %u", authdatalen);
893 if (authdatalen < OSPF6_AT_HDRLEN || authdatalen > len)
894 goto trunc;
895 cp += 2;
896 /* Reserved */
897 ND_TCHECK_2(cp);
898 cp += 2;
899 /* Security Association ID */
900 ND_TCHECK_2(cp);
901 ND_PRINT(", SAID %u", GET_BE_U_2(cp));
902 cp += 2;
903 /* Cryptographic Sequence Number (High-Order 32 Bits) */
904 ND_TCHECK_4(cp);
905 ND_PRINT(", CSN 0x%08x", GET_BE_U_4(cp));
906 cp += 4;
907 /* Cryptographic Sequence Number (Low-Order 32 Bits) */
908 ND_TCHECK_4(cp);
909 ND_PRINT(":%08x", GET_BE_U_4(cp));
910 cp += 4;
911 /* Authentication Data */
912 ND_TCHECK_LEN(cp, authdatalen - OSPF6_AT_HDRLEN);
913 if (ndo->ndo_vflag > 1)
914 print_unknown_data(ndo,cp, "\n\tAuthentication Data ", authdatalen - OSPF6_AT_HDRLEN);
915 return 0;
916
917 trunc:
918 return 1;
919 }
920
921 /* The trailing data may include LLS and/or AT data (in this specific order).
922 * LLS data may be present only in Hello and DBDesc packets with the L-bit set.
923 * AT data may be present in Hello and DBDesc packets with the AT-bit set or in
924 * any other packet type, thus decode the AT data regardless of the AT-bit.
925 */
926 static int
927 ospf6_decode_v3_trailer(netdissect_options *ndo,
928 const struct ospf6hdr *op, const u_char *cp, const unsigned len)
929 {
930 uint8_t type;
931 int llslen = 0;
932 int lls_hello = 0;
933 int lls_dd = 0;
934
935 type = GET_U_1(op->ospf6_type);
936 if (type == OSPF_TYPE_HELLO) {
937 const struct hello6 *hellop = (const struct hello6 *)((const uint8_t *)op + OSPF6HDR_LEN);
938 ND_TCHECK_4(hellop->hello_options);
939 if (GET_BE_U_4(hellop->hello_options) & OSPF6_OPTION_L)
940 lls_hello = 1;
941 } else if (type == OSPF_TYPE_DD) {
942 const struct dd6 *ddp = (const struct dd6 *)((const uint8_t *)op + OSPF6HDR_LEN);
943 ND_TCHECK_4(ddp->db_options);
944 if (GET_BE_U_4(ddp->db_options) & OSPF6_OPTION_L)
945 lls_dd = 1;
946 }
947 if ((lls_hello || lls_dd) && (llslen = ospf6_print_lls(ndo, cp, len)) < 0)
948 goto trunc;
949 return ospf6_decode_at(ndo, cp + llslen, len - llslen);
950
951 trunc:
952 return 1;
953 }
954
955 void
956 ospf6_print(netdissect_options *ndo,
957 const u_char *bp, u_int length)
958 {
959 const struct ospf6hdr *op;
960 const u_char *dataend;
961 const char *cp;
962 uint16_t datalen;
963
964 ndo->ndo_protocol = "ospf3";
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)",
971 GET_U_1(op->ospf6_type));
972 ND_PRINT("OSPFv%u, %s, length %u", GET_U_1(op->ospf6_version), cp,
973 length);
974 if (*cp == 'u') {
975 return;
976 }
977
978 if(!ndo->ndo_vflag) { /* non verbose - so lets bail out here */
979 return;
980 }
981
982 /* OSPFv3 data always comes first and optional trailing data may follow. */
983 ND_TCHECK_2(op->ospf6_len);
984 datalen = GET_BE_U_2(op->ospf6_len);
985 if (datalen > length) {
986 ND_PRINT(" [len %u]", datalen);
987 return;
988 }
989 dataend = bp + datalen;
990
991 ND_TCHECK_4(op->ospf6_routerid);
992 ND_PRINT("\n\tRouter-ID %s", ipaddr_string(ndo, op->ospf6_routerid));
993
994 ND_TCHECK_4(op->ospf6_areaid);
995 if (GET_BE_U_4(op->ospf6_areaid) != 0)
996 ND_PRINT(", Area %s", ipaddr_string(ndo, op->ospf6_areaid));
997 else
998 ND_PRINT(", Backbone Area");
999 ND_TCHECK_1(op->ospf6_instanceid);
1000 if (GET_U_1(op->ospf6_instanceid))
1001 ND_PRINT(", Instance %u", GET_U_1(op->ospf6_instanceid));
1002
1003 /* Do rest according to version. */
1004 switch (GET_U_1(op->ospf6_version)) {
1005
1006 case 3:
1007 /* ospf version 3 */
1008 if (ospf6_decode_v3(ndo, op, dataend) ||
1009 ospf6_decode_v3_trailer(ndo, op, dataend, length - datalen))
1010 goto trunc;
1011 break;
1012 } /* end switch on version */
1013
1014 return;
1015 trunc:
1016 nd_print_trunc(ndo);
1017 }