+/*
+ * The SID/Label Range TLV
+ * https://datatracker.ietf.org/doc/html/rfc8665#section-3.2
+ * and the SR Local Block TLV
+ * https://datatracker.ietf.org/doc/html/rfc8665#section-3.3
+ * have the same contents, so this function is used to
+ * print both.
+ */
+static int
+ospf_print_ri_lsa_sid_label_range_tlv(netdissect_options *ndo, const uint8_t *tptr,
+ u_int tlv_length)
+{
+ u_int subtlv_type, subtlv_length;
+
+ while (tlv_length >= 4) {
+
+ subtlv_type = GET_BE_U_2(tptr);
+ subtlv_length = GET_BE_U_2(tptr+2);
+ tptr+=4;
+ tlv_length-=4;
+
+ /* Infinite loop protection. */
+ if (subtlv_type == 0 || subtlv_length == 0) {
+ nd_print_invalid(ndo);
+ return -1;
+ }
+
+ ND_PRINT("\n\t %s subTLV (%u), length: %u, value: ",
+ tok2str(lsa_opaque_ri_sid_subtlv_values,"unknown",subtlv_type),
+ subtlv_type,
+ subtlv_length);
+
+ if (tlv_length < subtlv_length) {
+ ND_PRINT("\n\t Remaining TLV length %u < %u",
+ tlv_length, subtlv_length);
+ return -1;
+ }
+
+ switch (subtlv_type) {
+ case LS_OPAQUE_RI_SUBTLV_SID_LABEL:
+ if (subtlv_length == 3) {
+ ND_PRINT("\n\t\tLabel: %u", GET_BE_U_3(tptr));
+ } else if (subtlv_length == 4) {
+ ND_PRINT("\n\t\tSID: %u", GET_BE_U_4(tptr));
+ } else {
+ ND_PRINT("\n\t\tBogus subTLV length %u", subtlv_length);
+ }
+ break;
+
+ default:
+ if (ndo->ndo_vflag <= 1) {
+ if (!print_unknown_data(ndo, tptr, "\n\t\t", subtlv_length))
+ return -1;
+ }
+ }
+
+ /* in OSPF everything has to be 32-bit aligned, including subTLVs */
+ if (subtlv_length % 4) {
+ subtlv_length += (4 - (subtlv_length % 4));
+ if (tlv_length < subtlv_length) {
+ ND_PRINT("\n\t Remaining TLV length %u < %u",
+ tlv_length, subtlv_length);
+ return -1;
+ }
+ }
+ tptr+=subtlv_length;
+ tlv_length-=subtlv_length;
+ }
+ return 0;
+}
+
+static int
+ospf_print_ep_lsa_extd_prefix_tlv(netdissect_options *ndo, const uint8_t *tptr,
+ u_int tlv_length)
+{
+ u_int subtlv_type, subtlv_length;
+ uint8_t flags, mt_id, algo;
+
+ while (tlv_length >= 4) {
+ subtlv_type = GET_BE_U_2(tptr);
+ subtlv_length = GET_BE_U_2(tptr+2);
+ tptr+=4;
+ tlv_length-=4;
+
+ /* Infinite loop protection. */
+ if (subtlv_type == 0 || subtlv_length == 0) {
+ nd_print_invalid(ndo);
+ return -1;
+ }
+
+ ND_PRINT("\n\t\t%s subTLV (%u), length: %u, value: ",
+ tok2str(lsa_opaque_ep_extd_prefix_subtlv_values,"unknown",subtlv_type),
+ subtlv_type,
+ subtlv_length);
+
+ if (tlv_length < subtlv_length) {
+ ND_PRINT("\n\t Remaining TLV length %u < %u",
+ tlv_length, subtlv_length);
+ return -1;
+ }
+
+ switch (subtlv_type) {
+ case LS_OPAQUE_EP_SUBTLV_PREFIX_SID:
+ flags = GET_U_1(tptr);
+ mt_id = GET_U_1(tptr+2);
+ algo = GET_U_1(tptr+3);
+
+ if (subtlv_length == 7) {
+ ND_PRINT("\n\t\t Label: %u", GET_BE_U_3(tptr+4));
+ } else if (subtlv_length == 8) {
+ ND_PRINT("\n\t\t Index: %u", GET_BE_U_4(tptr+4));
+ } else {
+ ND_PRINT("\n\t\tBogus subTLV length %u", subtlv_length);
+ break;
+ }
+ ND_PRINT( ", MT-ID: %u, Algorithm: %s (%u), Flags [%s]",
+ mt_id, tok2str(lsa_opaque_ri_tlv_sr_algos, "Unknown", algo), algo,
+ bittok2str(ep_range_tlv_prefix_sid_subtlv_flag_values, "none", flags));
+ break;
+
+ default:
+ if (ndo->ndo_vflag <= 1) {
+ if (!print_unknown_data(ndo, tptr, "\n\t\t", subtlv_length))
+ return -1;
+ }
+ }
+
+ /* in OSPF everything has to be 32-bit aligned, including subTLVs */
+ if (subtlv_length % 4) {
+ subtlv_length += (4 - (subtlv_length % 4));
+ if (tlv_length < subtlv_length) {
+ ND_PRINT("\n\t Remaining TLV length %u < %u",
+ tlv_length, subtlv_length);
+ return -1;
+ }
+ }
+ tptr+=subtlv_length;
+ tlv_length-=subtlv_length;
+ }
+ return 0;
+}
+
+static int
+ospf_ep_lsa_print(netdissect_options *ndo, const uint8_t *tptr, u_int lsa_length)
+{
+ u_int tlv_type, tlv_length;
+ uint16_t range_size;
+ uint8_t af, prefix_length, route_type, flags;
+
+ while (lsa_length >= 4) {
+
+ tlv_type = GET_BE_U_2(tptr);
+ tlv_length = GET_BE_U_2(tptr+2);
+ tptr+=4;
+ lsa_length-=4;
+
+ /* Infinite loop protection. */
+ if (tlv_type == 0 || tlv_length == 0) {
+ nd_print_invalid(ndo);
+ return -1;
+ }
+
+ ND_PRINT("\n\t %s TLV (%u), length: %u, value: ",
+ tok2str(lsa_opaque_ep_tlv_values,"unknown",tlv_type),
+ tlv_type,
+ tlv_length);
+
+ if (tlv_length > lsa_length) {
+ ND_PRINT("\n\t Bogus length %u > %u",
+ tlv_length, lsa_length);
+ return -1;
+ }
+
+ switch (tlv_type) {
+ case LS_OPAQUE_EP_EXTD_PREFIX_TLV:
+ prefix_length = GET_U_1(tptr+1);
+ af = GET_U_1(tptr+2);
+ route_type = GET_U_1(tptr);
+ flags = GET_U_1(tptr+3);
+
+ if (af != 0) {
+ ND_PRINT("\n\t Bogus AF %u", af);
+ return -1;
+ }
+
+ if (prefix_length > 32) {
+ ND_PRINT("\n\t IPv4 prefix: bad bit length %u", prefix_length);
+ return -1;
+ }
+
+ ND_PRINT("\n\t IPv4 prefix: %15s/%u, Route Type: %s, Flags [%s]",
+ GET_IPADDR_STRING(tptr+4), prefix_length,
+ tok2str(lsa_opaque_ep_route_type_values, "Unknown", route_type),
+ bittok2str(ep_tlv_flag_values, "none", flags));
+
+ /* subTLVs present ? */
+ if (tlv_length > 12) {
+ if (ospf_print_ep_lsa_extd_prefix_tlv(ndo, tptr+8, tlv_length-8) == -1) {
+ return -1;
+ }
+ }
+ break;
+
+ case LS_OPAQUE_EP_EXTD_PREFIX_RANGE_TLV:
+ prefix_length = GET_U_1(tptr);
+ af = GET_U_1(tptr+1);
+ range_size = GET_BE_U_2(tptr+2);
+ flags = GET_U_1(tptr+4);
+
+ if (af != 0) {
+ ND_PRINT("\n\t Bogus AF %u", af);
+ return -1;
+ }
+
+ if (prefix_length > 32) {
+ ND_PRINT("\n\t IPv4 prefix: bad bit length %u", prefix_length);
+ return -1;
+ }
+
+ ND_PRINT("\n\t IPv4 prefix: %15s/%u, Range size: %u, Flags [%s]",
+ GET_IPADDR_STRING(tptr+8), prefix_length,
+ range_size,
+ bittok2str(ep_tlv_flag_values, "none", flags));
+
+ /* subTLVs present ? */
+ if (tlv_length > 12) {
+ if (ospf_print_ep_lsa_extd_prefix_tlv(ndo, tptr+12, tlv_length-12) == -1) {
+ return -1;
+ }
+ }
+ break;
+
+ default:
+ if (ndo->ndo_vflag <= 1) {
+ if (!print_unknown_data(ndo, tptr, "\n\t\t", tlv_length))
+ return -1;
+ }
+ }
+
+ /* in OSPF everything has to be 32-bit aligned, including TLVs */
+ if (tlv_length % 4) {
+ tlv_length += (4 - (tlv_length % 4));
+ if (tlv_length > lsa_length) {
+ ND_PRINT("\n\t Bogus padded length %u > %u", tlv_length,
+ lsa_length);
+ return -1;
+ }
+ }
+ tptr+=tlv_length;
+ lsa_length-=tlv_length;
+ }
+ return 0;
+}
+