]> The Tcpdump Group git mirrors - tcpdump/blob - print-ospf6.c
gre: add support for MikroTik Ethernet-over-IP hack.
[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_Nt 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 #define LSA_PREFIX_OPT_N 0x20
107
108 /* sla_tosmetric breakdown */
109 #define SLA_MASK_TOS 0x7f000000
110 #define SLA_MASK_METRIC 0x00ffffff
111 #define SLA_SHIFT_TOS 24
112
113 /* asla_metric */
114 #define ASLA_FLAG_FWDADDR 0x02000000
115 #define ASLA_FLAG_ROUTETAG 0x01000000
116 #define ASLA_MASK_METRIC 0x00ffffff
117
118 /* RFC6506 Section 4.1 */
119 #define OSPF6_AT_HDRLEN 16U
120 #define OSPF6_AUTH_TYPE_HMAC 0x0001
121
122 typedef nd_uint32_t rtrid_t;
123
124 /* link state advertisement header */
125 struct lsa6_hdr {
126 nd_uint16_t ls_age;
127 nd_uint16_t ls_type;
128 rtrid_t ls_stateid;
129 rtrid_t ls_router;
130 nd_uint32_t ls_seq;
131 nd_uint16_t ls_chksum;
132 nd_uint16_t ls_length;
133 };
134
135 /* Length of an IPv6 address, in bytes. */
136 #define IPV6_ADDR_LEN_BYTES (128/8)
137
138 struct lsa6_prefix {
139 nd_uint8_t lsa_p_len;
140 nd_uint8_t lsa_p_opt;
141 nd_uint16_t lsa_p_metric;
142 nd_byte lsa_p_prefix[IPV6_ADDR_LEN_BYTES]; /* maximum length */
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 nd_uint8_t flg;
155 nd_uint32_t opt;
156 } rla_flgandopt;
157 #define rla_flags rla_flgandopt.flg
158 #define rla_options rla_flgandopt.opt
159 struct rlalink6 {
160 nd_uint8_t link_type;
161 nd_byte link_zero;
162 nd_uint16_t link_metric;
163 nd_uint32_t link_ifid;
164 nd_uint32_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 nd_uint32_t nla_options;
172 rtrid_t nla_router[1]; /* may repeat */
173 } un_nla;
174
175 /* Inter Area Prefix LSA */
176 struct {
177 nd_uint32_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 nd_uint32_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 nd_ipv4 sla_mask;
192 nd_uint32_t sla_tosmetric[1]; /* may repeat */
193 } un_sla;
194
195 /* Multicast group membership */
196 struct mcla {
197 nd_uint32_t mcla_vtype;
198 nd_ipv4 mcla_vid;
199 } un_mcla[1];
200 #endif
201
202 /* Type 7 LSA */
203
204 /* Link LSA */
205 struct llsa {
206 union {
207 nd_uint8_t pri;
208 nd_uint32_t opt;
209 } llsa_priandopt;
210 #define llsa_priority llsa_priandopt.pri
211 #define llsa_options llsa_priandopt.opt
212 nd_ipv6 llsa_lladdr;
213 nd_uint32_t llsa_nprefix;
214 struct lsa6_prefix llsa_prefix[1];
215 } un_llsa;
216
217 /* Intra-Area-Prefix */
218 struct {
219 nd_uint16_t intra_ap_nprefix;
220 nd_uint16_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 nd_uint8_t ospf6_version;
233 nd_uint8_t ospf6_type;
234 nd_uint16_t ospf6_len;
235 rtrid_t ospf6_routerid;
236 rtrid_t ospf6_areaid;
237 nd_uint16_t ospf6_chksum;
238 nd_uint8_t ospf6_instanceid;
239 nd_uint8_t ospf6_rsvd;
240 };
241
242 /*
243 * The OSPF6 header length is 16 bytes, regardless of how your compiler
244 * might choose to pad the above structure.
245 */
246 #define OSPF6HDR_LEN 16
247
248 /* Hello packet */
249 struct hello6 {
250 nd_uint32_t hello_ifid;
251 union {
252 nd_uint8_t pri;
253 nd_uint32_t opt;
254 } hello_priandopt;
255 #define hello_priority hello_priandopt.pri
256 #define hello_options hello_priandopt.opt
257 nd_uint16_t hello_helloint;
258 nd_uint16_t hello_deadint;
259 rtrid_t hello_dr;
260 rtrid_t hello_bdr;
261 rtrid_t hello_neighbor[1]; /* may repeat */
262 };
263
264 /* Database Description packet */
265 struct dd6 {
266 nd_uint32_t db_options;
267 nd_uint16_t db_mtu;
268 nd_uint8_t db_mbz;
269 nd_uint8_t db_flags;
270 nd_uint32_t db_seq;
271 struct lsa6_hdr db_lshdr[1]; /* may repeat */
272 };
273
274 /* Link State Request */
275 struct lsr6 {
276 nd_uint16_t ls_mbz;
277 nd_uint16_t ls_type;
278 rtrid_t ls_stateid;
279 rtrid_t ls_router;
280 };
281
282 /* Link State Update */
283 struct lsu6 {
284 nd_uint32_t lsu_count;
285 struct lsa6 lsu_lsa[1]; /* may repeat */
286 };
287
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, "Deprecated" },
307 { RLA_FLAG_Nt, "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 { LSA_PREFIX_OPT_N, "N-bit" },
367 { 0, NULL }
368 };
369
370 static const struct tok ospf6_auth_type_str[] = {
371 { OSPF6_AUTH_TYPE_HMAC, "HMAC" },
372 { 0, NULL }
373 };
374
375 static void
376 ospf6_print_ls_type(netdissect_options *ndo,
377 u_int ls_type, const rtrid_t *ls_stateid)
378 {
379 ND_PRINT("\n\t %s LSA (%u), %s Scope%s, LSA-ID %s",
380 tok2str(ospf6_lsa_values, "Unknown", ls_type & LS_TYPE_MASK),
381 ls_type & LS_TYPE_MASK,
382 tok2str(ospf6_ls_scope_values, "Unknown", ls_type & LS_SCOPE_MASK),
383 ls_type &0x8000 ? ", transitive" : "", /* U-bit */
384 GET_IPADDR_STRING(ls_stateid));
385 }
386
387 static int
388 ospf6_print_lshdr(netdissect_options *ndo,
389 const struct lsa6_hdr *lshp, const u_char *dataend)
390 {
391 u_int ls_length;
392
393 if ((const u_char *)(lshp + 1) > dataend)
394 goto trunc;
395
396 ls_length = GET_BE_U_2(lshp->ls_length);
397 if (ls_length < sizeof(struct lsa_hdr)) {
398 ND_PRINT("\n\t Bogus length %u < header (%zu)", ls_length,
399 sizeof(struct lsa_hdr));
400 goto trunc;
401 }
402
403 ND_PRINT("\n\t Advertising Router %s, seq 0x%08x, age %us, length %zu",
404 GET_IPADDR_STRING(lshp->ls_router),
405 GET_BE_U_4(lshp->ls_seq),
406 GET_BE_U_2(lshp->ls_age),
407 ls_length-sizeof(struct lsa6_hdr));
408
409 ospf6_print_ls_type(ndo, GET_BE_U_2(lshp->ls_type),
410 &lshp->ls_stateid);
411
412 return (0);
413 trunc:
414 return (1);
415 }
416
417 static int
418 ospf6_print_lsaprefix(netdissect_options *ndo,
419 const uint8_t *tptr, u_int lsa_length)
420 {
421 const struct lsa6_prefix *lsapp = (const struct lsa6_prefix *)tptr;
422 u_int wordlen;
423 nd_ipv6 prefix;
424
425 if (lsa_length < sizeof (*lsapp) - IPV6_ADDR_LEN_BYTES)
426 goto trunc;
427 lsa_length -= sizeof (*lsapp) - IPV6_ADDR_LEN_BYTES;
428 ND_TCHECK_LEN(lsapp, sizeof(*lsapp) - IPV6_ADDR_LEN_BYTES);
429 wordlen = (GET_U_1(lsapp->lsa_p_len) + 31) / 32;
430 if (wordlen * 4 > sizeof(nd_ipv6)) {
431 ND_PRINT(" bogus prefixlen /%u", GET_U_1(lsapp->lsa_p_len));
432 goto trunc;
433 }
434 if (lsa_length < wordlen * 4)
435 goto trunc;
436 lsa_length -= wordlen * 4;
437 memset(prefix, 0, sizeof(prefix));
438 GET_CPY_BYTES(prefix, lsapp->lsa_p_prefix, wordlen * 4);
439 ND_PRINT("\n\t\t%s/%u", ip6addr_string(ndo, prefix), /* local buffer, not packet data; don't use GET_IP6ADDR_STRING() */
440 GET_U_1(lsapp->lsa_p_len));
441 if (GET_U_1(lsapp->lsa_p_opt)) {
442 ND_PRINT(", Options [%s]",
443 bittok2str(ospf6_lsa_prefix_option_values,
444 "none", GET_U_1(lsapp->lsa_p_opt)));
445 }
446 ND_PRINT(", metric %u", GET_BE_U_2(lsapp->lsa_p_metric));
447 return sizeof(*lsapp) - IPV6_ADDR_LEN_BYTES + wordlen * 4;
448
449 trunc:
450 return -1;
451 }
452
453
454 /*
455 * Print a single link state advertisement. If truncated return 1, else 0.
456 */
457 static int
458 ospf6_print_lsa(netdissect_options *ndo,
459 const struct lsa6 *lsap, const u_char *dataend)
460 {
461 const struct rlalink6 *rlp;
462 #if 0
463 const struct tos_metric *tosp;
464 #endif
465 const rtrid_t *ap;
466 #if 0
467 const struct aslametric *almp;
468 const struct mcla *mcp;
469 #endif
470 const struct llsa *llsap;
471 const struct lsa6_prefix *lsapp;
472 #if 0
473 const uint32_t *lp;
474 #endif
475 u_int prefixes;
476 int bytelen;
477 u_int length, lsa_length;
478 uint32_t flags32;
479 const uint8_t *tptr;
480
481 if (ospf6_print_lshdr(ndo, &lsap->ls_hdr, dataend))
482 return (1);
483 length = GET_BE_U_2(lsap->ls_hdr.ls_length);
484
485 /*
486 * The LSA length includes the length of the header;
487 * it must have a value that's at least that length.
488 * If it does, find the length of what follows the
489 * header.
490 */
491 if (length < sizeof(struct lsa6_hdr) || (const u_char *)lsap + length > dataend)
492 return (1);
493 lsa_length = length - sizeof(struct lsa6_hdr);
494 tptr = (const uint8_t *)lsap+sizeof(struct lsa6_hdr);
495
496 switch (GET_BE_U_2(lsap->ls_hdr.ls_type)) {
497 case LS_TYPE_ROUTER | LS_SCOPE_AREA:
498 if (lsa_length < sizeof (lsap->lsa_un.un_rla.rla_options))
499 return (1);
500 lsa_length -= sizeof (lsap->lsa_un.un_rla.rla_options);
501 ND_PRINT("\n\t Options [%s]",
502 bittok2str(ospf6_option_values, "none",
503 GET_BE_U_4(lsap->lsa_un.un_rla.rla_options)));
504 ND_PRINT(", RLA-Flags [%s]",
505 bittok2str(ospf6_rla_flag_values, "none",
506 GET_U_1(lsap->lsa_un.un_rla.rla_flags)));
507
508 rlp = lsap->lsa_un.un_rla.rla_link;
509 while (lsa_length != 0) {
510 if (lsa_length < sizeof (*rlp))
511 return (1);
512 lsa_length -= sizeof (*rlp);
513 ND_TCHECK_SIZE(rlp);
514 switch (GET_U_1(rlp->link_type)) {
515
516 case RLA_TYPE_VIRTUAL:
517 ND_PRINT("\n\t Virtual Link: Neighbor Router-ID %s"
518 "\n\t Neighbor Interface-ID %s, Interface %s",
519 GET_IPADDR_STRING(rlp->link_nrtid),
520 GET_IPADDR_STRING(rlp->link_nifid),
521 GET_IPADDR_STRING(rlp->link_ifid));
522 break;
523
524 case RLA_TYPE_ROUTER:
525 ND_PRINT("\n\t Neighbor Router-ID %s"
526 "\n\t Neighbor Interface-ID %s, Interface %s",
527 GET_IPADDR_STRING(rlp->link_nrtid),
528 GET_IPADDR_STRING(rlp->link_nifid),
529 GET_IPADDR_STRING(rlp->link_ifid));
530 break;
531
532 case RLA_TYPE_TRANSIT:
533 ND_PRINT("\n\t Neighbor Network-ID %s"
534 "\n\t Neighbor Interface-ID %s, Interface %s",
535 GET_IPADDR_STRING(rlp->link_nrtid),
536 GET_IPADDR_STRING(rlp->link_nifid),
537 GET_IPADDR_STRING(rlp->link_ifid));
538 break;
539
540 default:
541 ND_PRINT("\n\t Unknown Router Links Type 0x%02x",
542 GET_U_1(rlp->link_type));
543 return (0);
544 }
545 ND_PRINT(", metric %u", GET_BE_U_2(rlp->link_metric));
546 rlp++;
547 }
548 break;
549
550 case LS_TYPE_NETWORK | LS_SCOPE_AREA:
551 if (lsa_length < sizeof (lsap->lsa_un.un_nla.nla_options))
552 return (1);
553 lsa_length -= sizeof (lsap->lsa_un.un_nla.nla_options);
554 ND_PRINT("\n\t Options [%s]",
555 bittok2str(ospf6_option_values, "none",
556 GET_BE_U_4(lsap->lsa_un.un_nla.nla_options)));
557
558 ND_PRINT("\n\t Connected Routers:");
559 ap = lsap->lsa_un.un_nla.nla_router;
560 while (lsa_length != 0) {
561 if (lsa_length < sizeof (*ap))
562 return (1);
563 lsa_length -= sizeof (*ap);
564 ND_PRINT("\n\t\t%s", GET_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 ND_PRINT(", metric %u",
574 GET_BE_U_4(lsap->lsa_un.un_inter_ap.inter_ap_metric) & SLA_MASK_METRIC);
575
576 tptr = (const uint8_t *)lsap->lsa_un.un_inter_ap.inter_ap_prefix;
577 while (lsa_length != 0) {
578 bytelen = ospf6_print_lsaprefix(ndo, tptr, lsa_length);
579 if (bytelen < 0)
580 goto trunc;
581 /*
582 * ospf6_print_lsaprefix() will return -1 if
583 * the length is too high, so this will not
584 * underflow.
585 */
586 lsa_length -= bytelen;
587 tptr += bytelen;
588 }
589 break;
590
591 case LS_TYPE_ASE | LS_SCOPE_AS:
592 if (lsa_length < sizeof (lsap->lsa_un.un_asla.asla_metric))
593 return (1);
594 lsa_length -= sizeof (lsap->lsa_un.un_asla.asla_metric);
595 flags32 = GET_BE_U_4(lsap->lsa_un.un_asla.asla_metric);
596 ND_PRINT("\n\t Flags [%s]",
597 bittok2str(ospf6_asla_flag_values, "none", flags32));
598 ND_PRINT(" metric %u",
599 GET_BE_U_4(lsap->lsa_un.un_asla.asla_metric) &
600 ASLA_MASK_METRIC);
601
602 tptr = (const uint8_t *)lsap->lsa_un.un_asla.asla_prefix;
603 lsapp = (const struct lsa6_prefix *)tptr;
604 bytelen = ospf6_print_lsaprefix(ndo, tptr, lsa_length);
605 if (bytelen < 0)
606 goto trunc;
607 /*
608 * ospf6_print_lsaprefix() will return -1 if
609 * the length is too high, so this will not
610 * underflow.
611 */
612 lsa_length -= bytelen;
613 tptr += bytelen;
614
615 if ((flags32 & ASLA_FLAG_FWDADDR) != 0) {
616 if (lsa_length < sizeof (nd_ipv6))
617 return (1);
618 lsa_length -= sizeof (nd_ipv6);
619 ND_PRINT(" forward %s",
620 GET_IP6ADDR_STRING(tptr));
621 tptr += sizeof(nd_ipv6);
622 }
623
624 if ((flags32 & ASLA_FLAG_ROUTETAG) != 0) {
625 if (lsa_length < sizeof (uint32_t))
626 return (1);
627 lsa_length -= sizeof (uint32_t);
628 ND_PRINT(" tag %s",
629 GET_IPADDR_STRING(tptr));
630 tptr += sizeof(uint32_t);
631 }
632
633 if (GET_U_1(lsapp->lsa_p_metric)) {
634 if (lsa_length < sizeof (uint32_t))
635 return (1);
636 lsa_length -= sizeof (uint32_t);
637 ND_PRINT(" RefLSID: %s",
638 GET_IPADDR_STRING(tptr));
639 tptr += sizeof(uint32_t);
640 }
641 break;
642
643 case LS_TYPE_LINK:
644 /* Link LSA */
645 llsap = &lsap->lsa_un.un_llsa;
646 if (lsa_length < sizeof (llsap->llsa_priandopt))
647 return (1);
648 lsa_length -= sizeof (llsap->llsa_priandopt);
649 ND_TCHECK_SIZE(&llsap->llsa_priandopt);
650 ND_PRINT("\n\t Options [%s]",
651 bittok2str(ospf6_option_values, "none",
652 GET_BE_U_4(llsap->llsa_options)));
653
654 if (lsa_length < sizeof (llsap->llsa_lladdr) + sizeof (llsap->llsa_nprefix))
655 return (1);
656 lsa_length -= sizeof (llsap->llsa_lladdr) + sizeof (llsap->llsa_nprefix);
657 prefixes = GET_BE_U_4(llsap->llsa_nprefix);
658 ND_PRINT("\n\t Priority %u, Link-local address %s, Prefixes %u:",
659 GET_U_1(llsap->llsa_priority),
660 GET_IP6ADDR_STRING(llsap->llsa_lladdr),
661 prefixes);
662
663 tptr = (const uint8_t *)llsap->llsa_prefix;
664 while (prefixes > 0) {
665 bytelen = ospf6_print_lsaprefix(ndo, tptr, lsa_length);
666 if (bytelen < 0)
667 goto trunc;
668 prefixes--;
669 /*
670 * ospf6_print_lsaprefix() will return -1 if
671 * the length is too high, so this will not
672 * underflow.
673 */
674 lsa_length -= bytelen;
675 tptr += bytelen;
676 }
677 break;
678
679 case LS_TYPE_INTRA_AP | LS_SCOPE_AREA:
680 /* Intra-Area-Prefix LSA */
681 if (lsa_length < sizeof (lsap->lsa_un.un_intra_ap.intra_ap_rtid))
682 return (1);
683 lsa_length -= sizeof (lsap->lsa_un.un_intra_ap.intra_ap_rtid);
684 ND_TCHECK_4(lsap->lsa_un.un_intra_ap.intra_ap_rtid);
685 ospf6_print_ls_type(ndo,
686 GET_BE_U_2(lsap->lsa_un.un_intra_ap.intra_ap_lstype),
687 &lsap->lsa_un.un_intra_ap.intra_ap_lsid);
688
689 if (lsa_length < sizeof (lsap->lsa_un.un_intra_ap.intra_ap_nprefix))
690 return (1);
691 lsa_length -= sizeof (lsap->lsa_un.un_intra_ap.intra_ap_nprefix);
692 prefixes = GET_BE_U_2(lsap->lsa_un.un_intra_ap.intra_ap_nprefix);
693 ND_PRINT("\n\t Prefixes %u:", prefixes);
694
695 tptr = (const uint8_t *)lsap->lsa_un.un_intra_ap.intra_ap_prefix;
696 while (prefixes > 0) {
697 bytelen = ospf6_print_lsaprefix(ndo, tptr, lsa_length);
698 if (bytelen < 0)
699 goto trunc;
700 prefixes--;
701 /*
702 * ospf6_print_lsaprefix() will return -1 if
703 * the length is too high, so this will not
704 * underflow.
705 */
706 lsa_length -= bytelen;
707 tptr += bytelen;
708 }
709 break;
710
711 case LS_TYPE_GRACE | LS_SCOPE_LINKLOCAL:
712 if (ospf_grace_lsa_print(ndo, tptr, lsa_length) == -1) {
713 return 1;
714 }
715 break;
716
717 case LS_TYPE_INTRA_ATE | LS_SCOPE_LINKLOCAL:
718 if (ospf_te_lsa_print(ndo, tptr, lsa_length) == -1) {
719 return 1;
720 }
721 break;
722
723 default:
724 if(!print_unknown_data(ndo,tptr,
725 "\n\t ",
726 lsa_length)) {
727 return (1);
728 }
729 break;
730 }
731
732 return (0);
733 trunc:
734 return (1);
735 }
736
737 static int
738 ospf6_decode_v3(netdissect_options *ndo,
739 const struct ospf6hdr *op,
740 const u_char *dataend)
741 {
742 const rtrid_t *ap;
743 const struct lsr6 *lsrp;
744 const struct lsa6_hdr *lshp;
745 const struct lsa6 *lsap;
746 uint32_t i;
747
748 switch (GET_U_1(op->ospf6_type)) {
749
750 case OSPF_TYPE_HELLO: {
751 const struct hello6 *hellop = (const struct hello6 *)((const uint8_t *)op + OSPF6HDR_LEN);
752
753 ND_PRINT("\n\tOptions [%s]",
754 bittok2str(ospf6_option_values, "none",
755 GET_BE_U_4(hellop->hello_options)));
756
757 ND_PRINT("\n\t Hello Timer %us, Dead Timer %us, Interface-ID %s, Priority %u",
758 GET_BE_U_2(hellop->hello_helloint),
759 GET_BE_U_2(hellop->hello_deadint),
760 GET_IPADDR_STRING(hellop->hello_ifid),
761 GET_U_1(hellop->hello_priority));
762
763 if (GET_BE_U_4(hellop->hello_dr) != 0)
764 ND_PRINT("\n\t Designated Router %s",
765 GET_IPADDR_STRING(hellop->hello_dr));
766 if (GET_BE_U_4(hellop->hello_bdr) != 0)
767 ND_PRINT(", Backup Designated Router %s",
768 GET_IPADDR_STRING(hellop->hello_bdr));
769 if (ndo->ndo_vflag > 1) {
770 ND_PRINT("\n\t Neighbor List:");
771 ap = hellop->hello_neighbor;
772 while ((const u_char *)ap < dataend) {
773 ND_PRINT("\n\t %s", GET_IPADDR_STRING(ap));
774 ++ap;
775 }
776 }
777 break; /* HELLO */
778 }
779
780 case OSPF_TYPE_DD: {
781 const struct dd6 *ddp = (const struct dd6 *)((const uint8_t *)op + OSPF6HDR_LEN);
782
783 ND_PRINT("\n\tOptions [%s]",
784 bittok2str(ospf6_option_values, "none",
785 GET_BE_U_4(ddp->db_options)));
786 ND_PRINT(", DD Flags [%s]",
787 bittok2str(ospf6_dd_flag_values,"none",GET_U_1(ddp->db_flags)));
788
789 ND_PRINT(", MTU %u, DD-Sequence 0x%08x",
790 GET_BE_U_2(ddp->db_mtu),
791 GET_BE_U_4(ddp->db_seq));
792 if (ndo->ndo_vflag > 1) {
793 /* Print all the LS adv's */
794 lshp = ddp->db_lshdr;
795 while ((const u_char *)lshp < dataend) {
796 if (ospf6_print_lshdr(ndo, lshp++, dataend))
797 goto trunc;
798 }
799 }
800 break;
801 }
802
803 case OSPF_TYPE_LS_REQ:
804 if (ndo->ndo_vflag > 1) {
805 lsrp = (const struct lsr6 *)((const uint8_t *)op + OSPF6HDR_LEN);
806 while ((const u_char *)lsrp < dataend) {
807 ND_TCHECK_SIZE(lsrp);
808 ND_PRINT("\n\t Advertising Router %s",
809 GET_IPADDR_STRING(lsrp->ls_router));
810 ospf6_print_ls_type(ndo,
811 GET_BE_U_2(lsrp->ls_type),
812 &lsrp->ls_stateid);
813 ++lsrp;
814 }
815 }
816 break;
817
818 case OSPF_TYPE_LS_UPDATE:
819 if (ndo->ndo_vflag > 1) {
820 const struct lsu6 *lsup = (const struct lsu6 *)((const uint8_t *)op + OSPF6HDR_LEN);
821
822 i = GET_BE_U_4(lsup->lsu_count);
823 lsap = lsup->lsu_lsa;
824 while ((const u_char *)lsap < dataend && i--) {
825 if (ospf6_print_lsa(ndo, lsap, dataend))
826 goto trunc;
827 lsap = (const struct lsa6 *)((const u_char *)lsap +
828 GET_BE_U_2(lsap->ls_hdr.ls_length));
829 }
830 }
831 break;
832
833 case OSPF_TYPE_LS_ACK:
834 if (ndo->ndo_vflag > 1) {
835 lshp = (const struct lsa6_hdr *)((const uint8_t *)op + OSPF6HDR_LEN);
836 while ((const u_char *)lshp < dataend) {
837 if (ospf6_print_lshdr(ndo, lshp++, dataend))
838 goto trunc;
839 }
840 }
841 break;
842
843 default:
844 break;
845 }
846 return (0);
847 trunc:
848 return (1);
849 }
850
851 /* RFC5613 Section 2.2 (w/o the TLVs) */
852 static int
853 ospf6_print_lls(netdissect_options *ndo,
854 const u_char *cp, const u_int len)
855 {
856 uint16_t llsdatalen;
857
858 if (len == 0)
859 return 0;
860 if (len < OSPF_LLS_HDRLEN)
861 goto trunc;
862 /* Checksum */
863 ND_PRINT("\n\tLLS Checksum 0x%04x", GET_BE_U_2(cp));
864 cp += 2;
865 /* LLS Data Length */
866 llsdatalen = GET_BE_U_2(cp);
867 ND_PRINT(", Data Length %u", llsdatalen);
868 if (llsdatalen < OSPF_LLS_HDRLEN || llsdatalen > len)
869 goto trunc;
870 cp += 2;
871 /* LLS TLVs */
872 ND_TCHECK_LEN(cp, llsdatalen - OSPF_LLS_HDRLEN);
873 /* FIXME: code in print-ospf.c can be reused to decode the TLVs */
874
875 return llsdatalen;
876 trunc:
877 return -1;
878 }
879
880 /* RFC6506 Section 4.1 */
881 static int
882 ospf6_decode_at(netdissect_options *ndo,
883 const u_char *cp, const u_int len)
884 {
885 uint16_t authdatalen;
886
887 if (len == 0)
888 return 0;
889 if (len < OSPF6_AT_HDRLEN)
890 goto trunc;
891 /* Authentication Type */
892 ND_PRINT("\n\tAuthentication Type %s",
893 tok2str(ospf6_auth_type_str, "unknown (0x%04x)", GET_BE_U_2(cp)));
894 cp += 2;
895 /* Auth Data Len */
896 authdatalen = GET_BE_U_2(cp);
897 ND_PRINT(", Length %u", authdatalen);
898 if (authdatalen < OSPF6_AT_HDRLEN || authdatalen > len)
899 goto trunc;
900 cp += 2;
901 /* Reserved */
902 cp += 2;
903 /* Security Association ID */
904 ND_PRINT(", SAID %u", GET_BE_U_2(cp));
905 cp += 2;
906 /* Cryptographic Sequence Number (High-Order 32 Bits) */
907 ND_PRINT(", CSN 0x%08x", GET_BE_U_4(cp));
908 cp += 4;
909 /* Cryptographic Sequence Number (Low-Order 32 Bits) */
910 ND_PRINT(":%08x", GET_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 = GET_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 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 if (GET_BE_U_4(ddp->db_options) & OSPF6_OPTION_L)
944 lls_dd = 1;
945 }
946 if ((lls_hello || lls_dd) && (llslen = ospf6_print_lls(ndo, cp, len)) < 0)
947 goto trunc;
948 return ospf6_decode_at(ndo, cp + llslen, len - llslen);
949
950 trunc:
951 return 1;
952 }
953
954 void
955 ospf6_print(netdissect_options *ndo,
956 const u_char *bp, u_int length)
957 {
958 const struct ospf6hdr *op;
959 const u_char *dataend;
960 const char *cp;
961 uint16_t datalen;
962
963 ndo->ndo_protocol = "ospf3";
964 op = (const struct ospf6hdr *)bp;
965
966 /* If the type is valid translate it, or just print the type */
967 /* value. If it's not valid, say so and return */
968 cp = tok2str(ospf6_type_values, "unknown packet type (%u)",
969 GET_U_1(op->ospf6_type));
970 ND_PRINT("OSPFv%u, %s, length %u", GET_U_1(op->ospf6_version), cp,
971 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 datalen = GET_BE_U_2(op->ospf6_len);
982 if (datalen > length) {
983 ND_PRINT(" [len %u]", datalen);
984 return;
985 }
986 dataend = bp + datalen;
987
988 ND_PRINT("\n\tRouter-ID %s", GET_IPADDR_STRING(op->ospf6_routerid));
989
990 if (GET_BE_U_4(op->ospf6_areaid) != 0)
991 ND_PRINT(", Area %s", GET_IPADDR_STRING(op->ospf6_areaid));
992 else
993 ND_PRINT(", Backbone Area");
994 if (GET_U_1(op->ospf6_instanceid))
995 ND_PRINT(", Instance %u", GET_U_1(op->ospf6_instanceid));
996
997 /* Do rest according to version. */
998 switch (GET_U_1(op->ospf6_version)) {
999
1000 case 3:
1001 /* ospf version 3 */
1002 if (ospf6_decode_v3(ndo, op, dataend) ||
1003 ospf6_decode_v3_trailer(ndo, op, dataend, length - datalen))
1004 goto trunc;
1005 break;
1006 } /* end switch on version */
1007
1008 return;
1009 trunc:
1010 nd_print_trunc(ndo);
1011 }