]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-bgp.c
Remove more old-compiler compensation.
[tcpdump] / print-bgp.c
index a82dc8fe7b5e57fa93a734d27aa4760645b50e9d..301d24f88bad32081ade94c313f3d1b9935e6378 100644 (file)
@@ -216,6 +216,7 @@ static const struct tok bgp_opt_values[] = {
 #define BGP_CAPCODE_ORF                 3 /* RFC5291 */
 #define BGP_CAPCODE_MR                  4 /* RFC3107 */
 #define BGP_CAPCODE_EXT_NH              5 /* RFC5549 */
+#define BGP_CAPCODE_ML                  8 /* RFC8277 */
 #define BGP_CAPCODE_RESTART            64 /* RFC4724  */
 #define BGP_CAPCODE_AS_NEW             65 /* RFC6793 */
 #define BGP_CAPCODE_DYN_CAP            67 /* draft-ietf-idr-dynamic-cap */
@@ -230,6 +231,7 @@ static const struct tok bgp_capcode_values[] = {
     { BGP_CAPCODE_ORF,          "Cooperative Route Filtering"},
     { BGP_CAPCODE_MR,           "Multiple Routes to a Destination"},
     { BGP_CAPCODE_EXT_NH,       "Extended Next Hop Encoding"},
+    { BGP_CAPCODE_ML,           "Multiple Labels"},
     { BGP_CAPCODE_RESTART,      "Graceful Restart"},
     { BGP_CAPCODE_AS_NEW,       "32-Bit AS Number"},
     { BGP_CAPCODE_DYN_CAP,      "Dynamic Capability"},
@@ -524,9 +526,9 @@ as_printf(netdissect_options *ndo,
           char *str, size_t size, u_int asnum)
 {
     if (!ndo->ndo_bflag || asnum <= 0xFFFF) {
-        nd_snprintf(str, size, "%u", asnum);
+        snprintf(str, size, "%u", asnum);
     } else {
-        nd_snprintf(str, size, "%u.%u", asnum >> 16, asnum & 0xFFFF);
+        snprintf(str, size, "%u.%u", asnum >> 16, asnum & 0xFFFF);
     }
     return str;
 }
@@ -535,9 +537,9 @@ as_printf(netdissect_options *ndo,
 
 int
 decode_prefix4(netdissect_options *ndo,
-               const u_char *pptr, u_int itemlen, char *buf, u_int buflen)
+               const u_char *pptr, u_int itemlen, char *buf, size_t buflen)
 {
-    struct in_addr addr;
+    nd_ipv4 addr;
     u_int plen, plenbytes;
 
     ND_TCHECK_1(pptr);
@@ -555,7 +557,7 @@ decode_prefix4(netdissect_options *ndo,
     if (plen % 8) {
         ((u_char *)&addr)[plenbytes - 1] &= ((0xff00 >> (plen % 8)) & 0xff);
     }
-    nd_snprintf(buf, buflen, "%s/%u", ipaddr_string(ndo, (const u_char *)&addr), plen);
+    snprintf(buf, buflen, "%s/%u", ipaddr_string(ndo, (const u_char *)&addr), plen);
     return 1 + plenbytes;
 
 trunc:
@@ -567,9 +569,10 @@ badtlv:
 
 static int
 decode_labeled_prefix4(netdissect_options *ndo,
-                       const u_char *pptr, u_int itemlen, char *buf, u_int buflen)
+                       const u_char *pptr, u_int itemlen, char *buf,
+                       size_t buflen)
 {
-    struct in_addr addr;
+    nd_ipv4 addr;
     u_int plen, plenbytes;
 
     /* prefix length and label = 4 bytes */
@@ -603,7 +606,7 @@ decode_labeled_prefix4(netdissect_options *ndo,
         ((u_char *)&addr)[plenbytes - 1] &= ((0xff00 >> (plen % 8)) & 0xff);
     }
     /* the label may get offsetted by 4 bits so lets shift it right */
-    nd_snprintf(buf, buflen, "%s/%u, label:%u %s",
+    snprintf(buf, buflen, "%s/%u, label:%u %s",
              ipaddr_string(ndo, (const u_char *)&addr),
              plen,
              GET_BE_U_3(pptr + 1)>>4,
@@ -635,14 +638,14 @@ bgp_vpn_ip_print(netdissect_options *ndo,
     switch(addr_length) {
     case (sizeof(nd_ipv4) << 3): /* 32 */
         ND_TCHECK_LEN(pptr, sizeof(nd_ipv4));
-        nd_snprintf(pos, sizeof(addr), "%s", ipaddr_string(ndo, pptr));
+        snprintf(pos, sizeof(addr), "%s", ipaddr_string(ndo, pptr));
         break;
     case (sizeof(nd_ipv6) << 3): /* 128 */
         ND_TCHECK_LEN(pptr, sizeof(nd_ipv6));
-        nd_snprintf(pos, sizeof(addr), "%s", ip6addr_string(ndo, pptr));
+        snprintf(pos, sizeof(addr), "%s", ip6addr_string(ndo, pptr));
         break;
     default:
-        nd_snprintf(pos, sizeof(addr), "bogus address length %u", addr_length);
+        snprintf(pos, sizeof(addr), "bogus address length %u", addr_length);
         break;
     }
     pos += strlen(pos);
@@ -672,7 +675,7 @@ trunc:
  */
 static u_int
 bgp_vpn_sg_print(netdissect_options *ndo,
-                 const u_char *pptr, char *buf, u_int buflen)
+                 const u_char *pptr, char *buf, size_t buflen)
 {
     uint8_t addr_length;
     u_int total_length, offset;
@@ -687,9 +690,9 @@ bgp_vpn_sg_print(netdissect_options *ndo,
     /* Source address */
     ND_TCHECK_LEN(pptr, (addr_length >> 3));
     total_length += (addr_length >> 3) + 1;
-    offset = strlen(buf);
+    offset = (u_int)strlen(buf);
     if (addr_length) {
-        nd_snprintf(buf + offset, buflen - offset, ", Source %s",
+        snprintf(buf + offset, buflen - offset, ", Source %s",
              bgp_vpn_ip_print(ndo, pptr, addr_length));
         pptr += (addr_length >> 3);
     }
@@ -702,9 +705,9 @@ bgp_vpn_sg_print(netdissect_options *ndo,
     /* Group address */
     ND_TCHECK_LEN(pptr, (addr_length >> 3));
     total_length += (addr_length >> 3) + 1;
-    offset = strlen(buf);
+    offset = (u_int)strlen(buf);
     if (addr_length) {
-        nd_snprintf(buf + offset, buflen - offset, ", Group %s",
+        snprintf(buf + offset, buflen - offset, ", Group %s",
              bgp_vpn_ip_print(ndo, pptr, addr_length));
         pptr += (addr_length >> 3);
     }
@@ -727,7 +730,7 @@ bgp_vpn_rd_print(netdissect_options *ndo,
 
     case 0:
         /* 2-byte-AS:number fmt */
-        nd_snprintf(pos, sizeof(rd) - (pos - rd), "%u:%u (= %u.%u.%u.%u)",
+        snprintf(pos, sizeof(rd) - (pos - rd), "%u:%u (= %u.%u.%u.%u)",
                     GET_BE_U_2(pptr + 2),
                     GET_BE_U_4(pptr + 4),
                     GET_U_1(pptr + 4), GET_U_1(pptr + 5),
@@ -736,7 +739,7 @@ bgp_vpn_rd_print(netdissect_options *ndo,
 
     case 1:
         /* IP-address:AS fmt */
-        nd_snprintf(pos, sizeof(rd) - (pos - rd), "%u.%u.%u.%u:%u",
+        snprintf(pos, sizeof(rd) - (pos - rd), "%u.%u.%u.%u:%u",
                     GET_U_1(pptr + 2), GET_U_1(pptr + 3),
                     GET_U_1(pptr + 4), GET_U_1(pptr + 5),
                     GET_BE_U_2(pptr + 6));
@@ -744,14 +747,14 @@ bgp_vpn_rd_print(netdissect_options *ndo,
 
     case 2:
         /* 4-byte-AS:number fmt */
-        nd_snprintf(pos, sizeof(rd) - (pos - rd), "%s:%u (%u.%u.%u.%u:%u)",
+        snprintf(pos, sizeof(rd) - (pos - rd), "%s:%u (%u.%u.%u.%u:%u)",
                     as_printf(ndo, astostr, sizeof(astostr), GET_BE_U_4(pptr + 2)),
                     GET_BE_U_2(pptr + 6), GET_U_1(pptr + 2),
                     GET_U_1(pptr + 3), GET_U_1(pptr + 4),
                     GET_U_1(pptr + 5), GET_BE_U_2(pptr + 6));
         break;
     default:
-        nd_snprintf(pos, sizeof(rd) - (pos - rd), "unknown RD format");
+        snprintf(pos, sizeof(rd) - (pos - rd), "unknown RD format");
         break;
     }
     pos += strlen(pos);
@@ -901,9 +904,9 @@ trunc:
 
 static int
 decode_labeled_vpn_prefix4(netdissect_options *ndo,
-                           const u_char *pptr, char *buf, u_int buflen)
+                           const u_char *pptr, char *buf, size_t buflen)
 {
-    struct in_addr addr;
+    nd_ipv4 addr;
     u_int plen;
 
     ND_TCHECK_1(pptr);
@@ -925,7 +928,7 @@ decode_labeled_vpn_prefix4(netdissect_options *ndo,
             ((0xff00 >> (plen % 8)) & 0xff);
     }
     /* the label may get offsetted by 4 bits so lets shift it right */
-    nd_snprintf(buf, buflen, "RD: %s, %s/%u, label:%u %s",
+    snprintf(buf, buflen, "RD: %s, %s/%u, label:%u %s",
                 bgp_vpn_rd_print(ndo, pptr+4),
                 ipaddr_string(ndo, (const u_char *)&addr),
                 plen,
@@ -952,7 +955,7 @@ trunc:
 
 static int
 decode_mdt_vpn_nlri(netdissect_options *ndo,
-                    const u_char *pptr, char *buf, u_int buflen)
+                    const u_char *pptr, char *buf, size_t buflen)
 {
     const u_char *rd;
     const u_char *vpn_ip;
@@ -977,7 +980,7 @@ decode_mdt_vpn_nlri(netdissect_options *ndo,
     /* MDT Group Address */
     ND_TCHECK_LEN(pptr, sizeof(nd_ipv4));
 
-    nd_snprintf(buf, buflen, "RD: %s, VPN IP Address: %s, MC Group Address: %s",
+    snprintf(buf, buflen, "RD: %s, VPN IP Address: %s, MC Group Address: %s",
                 bgp_vpn_rd_print(ndo, rd), ipaddr_string(ndo, vpn_ip), ipaddr_string(ndo, pptr));
 
     return MDT_VPN_NLRI_LEN + 1;
@@ -1019,7 +1022,7 @@ decode_multicast_vpn(netdissect_options *ndo,
     route_length = GET_U_1(pptr);
     pptr++;
 
-    nd_snprintf(buf, buflen, "Route-Type: %s (%u), length: %u",
+    snprintf(buf, buflen, "Route-Type: %s (%u), length: %u",
          tok2str(bgp_multicast_vpn_route_type_values,
                  "Unknown", route_type),
          route_type, route_length);
@@ -1027,16 +1030,16 @@ decode_multicast_vpn(netdissect_options *ndo,
     switch(route_type) {
     case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI:
         ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN);
-        offset = strlen(buf);
-        nd_snprintf(buf + offset, buflen - offset, ", RD: %s, Originator %s",
+        offset = (u_int)strlen(buf);
+        snprintf(buf + offset, buflen - offset, ", RD: %s, Originator %s",
                     bgp_vpn_rd_print(ndo, pptr),
                     bgp_vpn_ip_print(ndo, pptr + BGP_VPN_RD_LEN,
                                      (route_length - BGP_VPN_RD_LEN) << 3));
         break;
     case BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI:
         ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN + 4);
-        offset = strlen(buf);
-        nd_snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s",
+        offset = (u_int)strlen(buf);
+        snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s",
         bgp_vpn_rd_print(ndo, pptr),
         as_printf(ndo, astostr, sizeof(astostr),
         GET_BE_U_4(pptr + BGP_VPN_RD_LEN)));
@@ -1044,8 +1047,8 @@ decode_multicast_vpn(netdissect_options *ndo,
 
     case BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI:
         ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN);
-        offset = strlen(buf);
-        nd_snprintf(buf + offset, buflen - offset, ", RD: %s",
+        offset = (u_int)strlen(buf);
+        snprintf(buf + offset, buflen - offset, ", RD: %s",
                     bgp_vpn_rd_print(ndo, pptr));
         pptr += BGP_VPN_RD_LEN;
 
@@ -1053,15 +1056,15 @@ decode_multicast_vpn(netdissect_options *ndo,
         addr_length =  route_length - sg_length;
 
         ND_TCHECK_LEN(pptr, addr_length);
-        offset = strlen(buf);
-        nd_snprintf(buf + offset, buflen - offset, ", Originator %s",
+        offset = (u_int)strlen(buf);
+        snprintf(buf + offset, buflen - offset, ", Originator %s",
                     bgp_vpn_ip_print(ndo, pptr, addr_length << 3));
         break;
 
     case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE:
         ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN);
-        offset = strlen(buf);
-        nd_snprintf(buf + offset, buflen - offset, ", RD: %s",
+        offset = (u_int)strlen(buf);
+        snprintf(buf + offset, buflen - offset, ", RD: %s",
                     bgp_vpn_rd_print(ndo, pptr));
         pptr += BGP_VPN_RD_LEN;
 
@@ -1071,8 +1074,8 @@ decode_multicast_vpn(netdissect_options *ndo,
     case BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN: /* fall through */
     case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN:
         ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN + 4);
-        offset = strlen(buf);
-        nd_snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s",
+        offset = (u_int)strlen(buf);
+        snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s",
                     bgp_vpn_rd_print(ndo, pptr),
                     as_printf(ndo, astostr, sizeof(astostr),
                     GET_BE_U_4(pptr + BGP_VPN_RD_LEN)));
@@ -1117,7 +1120,7 @@ trunc:
 
 static int
 decode_labeled_vpn_l2(netdissect_options *ndo,
-                      const u_char *pptr, char *buf, u_int buflen)
+                      const u_char *pptr, char *buf, size_t buflen)
 {
     u_int plen, tlen, tlv_type, tlv_len, ttlv_len;
     int stringlen;
@@ -1134,7 +1137,7 @@ decode_labeled_vpn_l2(netdissect_options *ndo,
         /* assume AD-only with RD, BGPNH */
         ND_TCHECK_LEN(pptr, 12);
         buf[0] = '\0';
-        stringlen = nd_snprintf(buf, buflen, "RD: %s, BGPNH: %s",
+        stringlen = snprintf(buf, buflen, "RD: %s, BGPNH: %s",
                                 bgp_vpn_rd_print(ndo, pptr),
                                 ipaddr_string(ndo, pptr+8));
         UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
@@ -1147,7 +1150,7 @@ decode_labeled_vpn_l2(netdissect_options *ndo,
 
         ND_TCHECK_LEN(pptr, 15);
         buf[0] = '\0';
-        stringlen = nd_snprintf(buf, buflen, "RD: %s, CE-ID: %u, Label-Block Offset: %u, Label Base %u",
+        stringlen = snprintf(buf, buflen, "RD: %s, CE-ID: %u, Label-Block Offset: %u, Label Base %u",
                                 bgp_vpn_rd_print(ndo, pptr),
                                 GET_BE_U_2(pptr + 8),
                                 GET_BE_U_2(pptr + 10),
@@ -1160,7 +1163,7 @@ decode_labeled_vpn_l2(netdissect_options *ndo,
         while (tlen != 0) {
             if (tlen < 3) {
                 if (buflen != 0) {
-                    stringlen=nd_snprintf(buf,buflen, "\n\t\tran past the end");
+                    stringlen=snprintf(buf,buflen, "\n\t\tran past the end");
                     UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
                 }
                 return plen + 2;
@@ -1175,7 +1178,7 @@ decode_labeled_vpn_l2(netdissect_options *ndo,
             switch(tlv_type) {
             case 1:
                 if (buflen != 0) {
-                    stringlen=nd_snprintf(buf,buflen, "\n\t\tcircuit status vector (%u) length: %u: 0x",
+                    stringlen=snprintf(buf,buflen, "\n\t\tcircuit status vector (%u) length: %u: 0x",
                                           tlv_type,
                                           tlv_len);
                     UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
@@ -1183,14 +1186,14 @@ decode_labeled_vpn_l2(netdissect_options *ndo,
                 while (ttlv_len != 0) {
                     if (tlen < 1) {
                         if (buflen != 0) {
-                            stringlen=nd_snprintf(buf,buflen, " (ran past the end)");
+                            stringlen=snprintf(buf,buflen, " (ran past the end)");
                             UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
                         }
                         return plen + 2;
                     }
                     ND_TCHECK_1(pptr);
                     if (buflen != 0) {
-                        stringlen=nd_snprintf(buf,buflen, "%02x",
+                        stringlen=snprintf(buf,buflen, "%02x",
                                               GET_U_1(pptr));
                         pptr++;
                         UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
@@ -1201,14 +1204,14 @@ decode_labeled_vpn_l2(netdissect_options *ndo,
                 break;
             default:
                 if (buflen != 0) {
-                    stringlen=nd_snprintf(buf,buflen, "\n\t\tunknown TLV #%u, length: %u",
+                    stringlen=snprintf(buf,buflen, "\n\t\tunknown TLV #%u, length: %u",
                                           tlv_type,
                                           tlv_len);
                     UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
                 }
                 if (tlen < ttlv_len) {
                     if (buflen != 0) {
-                        stringlen=nd_snprintf(buf,buflen, " (ran past the end)");
+                        stringlen=snprintf(buf,buflen, " (ran past the end)");
                         UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
                     }
                     return plen + 2;
@@ -1230,9 +1233,9 @@ trunc:
 
 int
 decode_prefix6(netdissect_options *ndo,
-               const u_char *pd, u_int itemlen, char *buf, u_int buflen)
+               const u_char *pd, u_int itemlen, char *buf, size_t buflen)
 {
-    struct in6_addr addr;
+    nd_ipv6 addr;
     u_int plen, plenbytes;
 
     ND_TCHECK_1(pd);
@@ -1248,10 +1251,10 @@ decode_prefix6(netdissect_options *ndo,
     ITEMCHECK(plenbytes);
     memcpy(&addr, pd + 1, plenbytes);
     if (plen % 8) {
-        addr.s6_addr[plenbytes - 1] &=
+        addr[plenbytes - 1] &=
             ((0xff00 >> (plen % 8)) & 0xff);
     }
-    nd_snprintf(buf, buflen, "%s/%u", ip6addr_string(ndo, (const u_char *)&addr), plen);
+    snprintf(buf, buflen, "%s/%u", ip6addr_string(ndo, (const u_char *)&addr), plen);
     return 1 + plenbytes;
 
 trunc:
@@ -1263,9 +1266,9 @@ badtlv:
 
 static int
 decode_labeled_prefix6(netdissect_options *ndo,
-               const u_char *pptr, u_int itemlen, char *buf, u_int buflen)
+               const u_char *pptr, u_int itemlen, char *buf, size_t buflen)
 {
-    struct in6_addr addr;
+    nd_ipv6 addr;
     u_int plen, plenbytes;
 
     /* prefix length and label = 4 bytes */
@@ -1287,11 +1290,11 @@ decode_labeled_prefix6(netdissect_options *ndo,
     ND_TCHECK_LEN(pptr + 4, plenbytes);
     memcpy(&addr, pptr + 4, plenbytes);
     if (plen % 8) {
-        addr.s6_addr[plenbytes - 1] &=
+        addr[plenbytes - 1] &=
             ((0xff00 >> (plen % 8)) & 0xff);
     }
     /* the label may get offsetted by 4 bits so lets shift it right */
-    nd_snprintf(buf, buflen, "%s/%u, label:%u %s",
+    snprintf(buf, buflen, "%s/%u, label:%u %s",
                 ip6addr_string(ndo, (const u_char *)&addr),
                 plen,
                 GET_BE_U_3(pptr + 1)>>4,
@@ -1308,9 +1311,9 @@ badtlv:
 
 static int
 decode_labeled_vpn_prefix6(netdissect_options *ndo,
-                           const u_char *pptr, char *buf, u_int buflen)
+                           const u_char *pptr, char *buf, size_t buflen)
 {
-    struct in6_addr addr;
+    nd_ipv6 addr;
     u_int plen;
 
     ND_TCHECK_1(pptr);
@@ -1328,11 +1331,11 @@ decode_labeled_vpn_prefix6(netdissect_options *ndo,
     ND_TCHECK_LEN(pptr + 12, (plen + 7) / 8);
     memcpy(&addr, pptr + 12, (plen + 7) / 8);
     if (plen % 8) {
-        addr.s6_addr[(plen + 7) / 8 - 1] &=
+        addr[(plen + 7) / 8 - 1] &=
             ((0xff00 >> (plen % 8)) & 0xff);
     }
     /* the label may get offsetted by 4 bits so lets shift it right */
-    nd_snprintf(buf, buflen, "RD: %s, %s/%u, label:%u %s",
+    snprintf(buf, buflen, "RD: %s, %s/%u, label:%u %s",
                 bgp_vpn_rd_print(ndo, pptr+4),
                 ip6addr_string(ndo, (const u_char *)&addr),
                 plen,
@@ -1347,7 +1350,7 @@ trunc:
 
 static int
 decode_clnp_prefix(netdissect_options *ndo,
-                   const u_char *pptr, char *buf, u_int buflen)
+                   const u_char *pptr, char *buf, size_t buflen)
 {
     uint8_t addr[19];
     u_int plen;
@@ -1365,7 +1368,7 @@ decode_clnp_prefix(netdissect_options *ndo,
         addr[(plen + 7) / 8 - 1] &=
             ((0xff00 >> (plen % 8)) & 0xff);
     }
-    nd_snprintf(buf, buflen, "%s/%u",
+    snprintf(buf, buflen, "%s/%u",
                 isonsap_string(ndo, addr,(plen + 7) / 8),
                 plen);
 
@@ -1377,7 +1380,7 @@ trunc:
 
 static int
 decode_labeled_vpn_clnp_prefix(netdissect_options *ndo,
-                               const u_char *pptr, char *buf, u_int buflen)
+                               const u_char *pptr, char *buf, size_t buflen)
 {
     uint8_t addr[19];
     u_int plen;
@@ -1400,7 +1403,7 @@ decode_labeled_vpn_clnp_prefix(netdissect_options *ndo,
         addr[(plen + 7) / 8 - 1] &= ((0xff00 >> (plen % 8)) & 0xff);
     }
     /* the label may get offsetted by 4 bits so lets shift it right */
-    nd_snprintf(buf, buflen, "RD: %s, %s/%u, label:%u %s",
+    snprintf(buf, buflen, "RD: %s, %s/%u, label:%u %s",
                 bgp_vpn_rd_print(ndo, pptr+4),
                 isonsap_string(ndo, addr,(plen + 7) / 8),
                 plen,
@@ -1538,6 +1541,236 @@ check_add_path(netdissect_options *ndo, const u_char *pptr, u_int length,
     return 0;
 }
 
+static int
+bgp_mp_af_print(netdissect_options *ndo,
+               const u_char *tptr, u_int tlen,
+               uint16_t *afp, uint8_t *safip)
+{
+       uint16_t af;
+       uint8_t safi;
+
+        af = GET_BE_U_2(tptr);
+       *afp = af;
+        safi = GET_U_1(tptr + 2);
+       *safip = safi;
+
+        ND_PRINT("\n\t    AFI: %s (%u), %sSAFI: %s (%u)",
+                  tok2str(af_values, "Unknown AFI", af),
+                  af,
+                  (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
+                  tok2str(bgp_safi_values, "Unknown SAFI", safi),
+                  safi);
+
+        switch(af<<8 | safi) {
+        case (AFNUM_INET<<8 | SAFNUM_UNICAST):
+        case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
+        case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
+        case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
+        case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
+        case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
+        case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
+        case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
+        case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN):
+        case (AFNUM_INET<<8 | SAFNUM_MDT):
+        case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
+        case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
+        case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
+        case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
+        case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
+        case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
+        case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
+        case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
+        case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
+        case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
+        case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
+        case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
+        case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
+        case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
+        case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
+        case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
+        case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
+            break;
+        default:
+            ND_TCHECK_LEN(tptr, tlen);
+            ND_PRINT("\n\t    no AFI %u / SAFI %u decoder", af, safi);
+            if (ndo->ndo_vflag <= 1)
+                print_unknown_data(ndo, tptr, "\n\t    ", tlen);
+            return -1;
+        }
+       return 0;
+trunc:
+       return -2;
+}
+
+static int
+bgp_nlri_print(netdissect_options *ndo, uint16_t af, uint8_t safi,
+              const u_char *tptr, u_int len,
+              char *buf, size_t buflen,
+              int add_path4, int add_path6)
+{
+       int advance;
+       u_int path_id = 0;
+
+       switch (af<<8 | safi) {
+            case (AFNUM_INET<<8 | SAFNUM_UNICAST):
+            case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
+            case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
+                if (add_path4) {
+                    path_id = GET_BE_U_4(tptr);
+                    tptr += 4;
+                }
+                advance = decode_prefix4(ndo, tptr, len, buf, buflen);
+                if (advance == -1)
+                    ND_PRINT("\n\t    (illegal prefix length)");
+                else if (advance == -2)
+                    goto trunc;
+                else if (advance == -3)
+                    break; /* bytes left, but not enough */
+                else
+                    ND_PRINT("\n\t      %s", buf);
+                if (add_path4) {
+                    ND_PRINT("   Path Id: %u", path_id);
+                   advance += 4;
+                }
+                break;
+            case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
+                advance = decode_labeled_prefix4(ndo, tptr, len, buf, buflen);
+                if (advance == -1)
+                    ND_PRINT("\n\t    (illegal prefix length)");
+                else if (advance == -2)
+                    goto trunc;
+                else if (advance == -3)
+                    break; /* bytes left, but not enough */
+                else
+                    ND_PRINT("\n\t      %s", buf);
+                break;
+            case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
+            case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
+            case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
+                advance = decode_labeled_vpn_prefix4(ndo, tptr, buf, buflen);
+                if (advance == -1)
+                    ND_PRINT("\n\t    (illegal prefix length)");
+                else if (advance == -2)
+                    goto trunc;
+                else
+                    ND_PRINT("\n\t      %s", buf);
+                break;
+            case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
+                advance = decode_rt_routing_info(ndo, tptr);
+                if (advance == -2)
+                    goto trunc;
+                break;
+            case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN): /* fall through */
+            case (AFNUM_INET6<<8 | SAFNUM_MULTICAST_VPN):
+                advance = decode_multicast_vpn(ndo, tptr, buf, buflen);
+                if (advance == -1)
+                    ND_PRINT("\n\t    (illegal prefix length)");
+                else if (advance == -2)
+                    goto trunc;
+                else
+                    ND_PRINT("\n\t      %s", buf);
+                break;
+
+            case (AFNUM_INET<<8 | SAFNUM_MDT):
+                advance = decode_mdt_vpn_nlri(ndo, tptr, buf, buflen);
+                if (advance == -1)
+                    ND_PRINT("\n\t    (illegal prefix length)");
+                else if (advance == -2)
+                    goto trunc;
+                else
+                    ND_PRINT("\n\t      %s", buf);
+                break;
+            case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
+            case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
+            case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
+                if (add_path6) {
+                    path_id = GET_BE_U_4(tptr);
+                    tptr += 4;
+                }
+                advance = decode_prefix6(ndo, tptr, len, buf, buflen);
+                if (advance == -1)
+                    ND_PRINT("\n\t    (illegal prefix length)");
+                else if (advance == -2)
+                    goto trunc;
+                else if (advance == -3)
+                    break; /* bytes left, but not enough */
+                else
+                    ND_PRINT("\n\t      %s", buf);
+                if (add_path6) {
+                    ND_PRINT("   Path Id: %u", path_id);
+                   advance += 4;
+                }
+                break;
+            case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
+                advance = decode_labeled_prefix6(ndo, tptr, len, buf, buflen);
+                if (advance == -1)
+                    ND_PRINT("\n\t    (illegal prefix length)");
+                else if (advance == -2)
+                    goto trunc;
+                else if (advance == -3)
+                    break; /* bytes left, but not enough */
+                else
+                    ND_PRINT("\n\t      %s", buf);
+                break;
+            case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
+            case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
+            case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
+                advance = decode_labeled_vpn_prefix6(ndo, tptr, buf, buflen);
+                if (advance == -1)
+                    ND_PRINT("\n\t    (illegal prefix length)");
+                else if (advance == -2)
+                    goto trunc;
+                else
+                    ND_PRINT("\n\t      %s", buf);
+                break;
+            case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
+            case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
+            case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
+            case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
+                advance = decode_labeled_vpn_l2(ndo, tptr, buf, buflen);
+                if (advance == -1)
+                    ND_PRINT("\n\t    (illegal length)");
+                else if (advance == -2)
+                    goto trunc;
+                else
+                    ND_PRINT("\n\t      %s", buf);
+                break;
+            case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
+            case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
+            case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
+                advance = decode_clnp_prefix(ndo, tptr, buf, buflen);
+                if (advance == -1)
+                    ND_PRINT("\n\t    (illegal prefix length)");
+                else if (advance == -2)
+                    goto trunc;
+                else
+                    ND_PRINT("\n\t      %s", buf);
+                break;
+            case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
+            case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
+            case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
+                advance = decode_labeled_vpn_clnp_prefix(ndo, tptr, buf, buflen);
+                if (advance == -1)
+                    ND_PRINT("\n\t    (illegal prefix length)");
+                else if (advance == -2)
+                    goto trunc;
+                else
+                    ND_PRINT("\n\t      %s", buf);
+                break;
+            default:
+               /*
+                * This should not happen, we should have been protected
+                * by bgp_mp_af_print()'s return value.
+                */
+                ND_PRINT("\n\t    ERROR: no AFI %u / SAFI %u decoder", af, safi);
+                advance = -4;
+                break;
+       }
+       return advance;
+trunc: /* we rely on the caller to recognize -2 return value */
+       return -2;
+}
+
 static int
 bgp_attr_print(netdissect_options *ndo,
                uint8_t atype, const u_char *pptr, u_int len)
@@ -1551,7 +1784,7 @@ bgp_attr_print(netdissect_options *ndo,
     char buf[MAXHOSTNAMELEN + 100];
     u_int as_size;
     int add_path4, add_path6;
-    u_int path_id = 0;
+    int ret;
 
     tptr = pptr;
     tlen = len;
@@ -1724,53 +1957,11 @@ bgp_attr_print(netdissect_options *ndo,
         ND_TCHECK_3(tptr);
         if (tlen < 3)
             goto trunc;
-        af = GET_BE_U_2(tptr);
-        safi = GET_U_1(tptr + 2);
-
-        ND_PRINT("\n\t    AFI: %s (%u), %sSAFI: %s (%u)",
-                  tok2str(af_values, "Unknown AFI", af),
-                  af,
-                  (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
-                  tok2str(bgp_safi_values, "Unknown SAFI", safi),
-                  safi);
-
-        switch(af<<8 | safi) {
-        case (AFNUM_INET<<8 | SAFNUM_UNICAST):
-        case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
-        case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
-        case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
-        case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
-        case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
-        case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
-        case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
-        case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN):
-        case (AFNUM_INET<<8 | SAFNUM_MDT):
-        case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
-        case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
-        case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
-        case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
-        case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
-        case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
-        case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
-        case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
-        case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
-        case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
-        case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
-        case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
-        case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
-        case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
-        case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
-        case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
-        case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
-            break;
-        default:
-            ND_TCHECK_LEN(tptr, tlen);
-            ND_PRINT("\n\t    no AFI %u / SAFI %u decoder", af, safi);
-            if (ndo->ndo_vflag <= 1)
-                print_unknown_data(ndo, tptr, "\n\t    ", tlen);
-            goto done;
-            break;
-        }
+       ret = bgp_mp_af_print(ndo, tptr, tlen, &af, &safi);
+       if (ret == -2)
+           goto trunc;
+       if (ret < 0)
+           break;
 
         tptr +=3;
 
@@ -1895,10 +2086,11 @@ bgp_attr_print(netdissect_options *ndo,
                     }
                     break;
                 default:
-                    ND_TCHECK_LEN(tptr, tlen);
-                    ND_PRINT("no AFI %u/SAFI %u decoder", af, safi);
-                    if (ndo->ndo_vflag <= 1)
-                        print_unknown_data(ndo, tptr, "\n\t    ", tlen);
+                   /*
+                    * bgp_mp_af_print() should have saved us from
+                    * an unsupported AFI/SAFI.
+                    */
+                    ND_PRINT("ERROR: no AFI %u/SAFI %u nexthop decoder", af, safi);
                     tptr += tlen;
                     tlen = 0;
                     goto done;
@@ -1924,338 +2116,41 @@ bgp_attr_print(netdissect_options *ndo,
             ND_PRINT(", no SNPA");
         }
 
-        add_path4 = check_add_path(ndo, tptr, (len-(tptr - pptr)), 32);
-        add_path6 = check_add_path(ndo, tptr, (len-(tptr - pptr)), 128);
+        add_path4 = check_add_path(ndo, tptr, (len-ND_BYTES_BETWEEN(tptr, pptr)), 32);
+        add_path6 = check_add_path(ndo, tptr, (len-ND_BYTES_BETWEEN(tptr, pptr)), 128);
 
         while (tptr < pptr + len) {
-            switch (af<<8 | safi) {
-            case (AFNUM_INET<<8 | SAFNUM_UNICAST):
-            case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
-            case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
-                if (add_path4) {
-                    path_id = GET_BE_U_4(tptr);
-                    tptr += 4;
-                }
-                advance = decode_prefix4(ndo, tptr, len, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal prefix length)");
-                else if (advance == -2)
-                    goto trunc;
-                else if (advance == -3)
-                    break; /* bytes left, but not enough */
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                if (add_path4) {
-                    ND_PRINT("   Path Id: %u", path_id);
-                }
-                break;
-            case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
-                advance = decode_labeled_prefix4(ndo, tptr, len, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal prefix length)");
-                else if (advance == -2)
-                    goto trunc;
-                else if (advance == -3)
-                    break; /* bytes left, but not enough */
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                break;
-            case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
-            case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
-            case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
-                advance = decode_labeled_vpn_prefix4(ndo, tptr, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal prefix length)");
-                else if (advance == -2)
-                    goto trunc;
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                break;
-            case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
-                advance = decode_rt_routing_info(ndo, tptr);
-                if (advance == -2)
-                    goto trunc;
-                break;
-            case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN): /* fall through */
-            case (AFNUM_INET6<<8 | SAFNUM_MULTICAST_VPN):
-                advance = decode_multicast_vpn(ndo, tptr, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal prefix length)");
-                else if (advance == -2)
-                    goto trunc;
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                break;
-
-            case (AFNUM_INET<<8 | SAFNUM_MDT):
-                advance = decode_mdt_vpn_nlri(ndo, tptr, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal prefix length)");
-                else if (advance == -2)
-                    goto trunc;
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                break;
-            case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
-            case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
-            case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
-                if (add_path6) {
-                    path_id = GET_BE_U_4(tptr);
-                    tptr += 4;
-                }
-                advance = decode_prefix6(ndo, tptr, len, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal prefix length)");
-                else if (advance == -2)
-                    goto trunc;
-                else if (advance == -3)
-                    break; /* bytes left, but not enough */
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                if (add_path6) {
-                    ND_PRINT("   Path Id: %u", path_id);
-                }
-                break;
-            case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
-                advance = decode_labeled_prefix6(ndo, tptr, len, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal prefix length)");
-                else if (advance == -2)
-                    goto trunc;
-                else if (advance == -3)
-                    break; /* bytes left, but not enough */
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                break;
-            case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
-            case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
-            case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
-                advance = decode_labeled_vpn_prefix6(ndo, tptr, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal prefix length)");
-                else if (advance == -2)
-                    goto trunc;
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                break;
-            case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
-            case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
-            case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
-            case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
-                advance = decode_labeled_vpn_l2(ndo, tptr, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal length)");
-                else if (advance == -2)
-                    goto trunc;
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                break;
-            case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
-            case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
-            case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
-                advance = decode_clnp_prefix(ndo, tptr, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal prefix length)");
-                else if (advance == -2)
-                    goto trunc;
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                break;
-            case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
-            case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
-            case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
-                advance = decode_labeled_vpn_clnp_prefix(ndo, tptr, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal prefix length)");
-                else if (advance == -2)
-                    goto trunc;
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                break;
-            default:
-                ND_TCHECK_LEN(tptr, tlen);
-                ND_PRINT("\n\t    no AFI %u / SAFI %u decoder", af, safi);
-                if (ndo->ndo_vflag <= 1)
-                    print_unknown_data(ndo, tptr, "\n\t    ", tlen);
-                advance = 0;
-                tptr = pptr + len;
-                break;
-            }
+            advance = bgp_nlri_print(ndo, af, safi, tptr, len, buf, sizeof(buf),
+                    add_path4, add_path6);
+            if (advance == -2)
+                goto trunc;
             if (advance < 0)
                 break;
             tptr += advance;
         }
-    done:
         break;
 
     case BGPTYPE_MP_UNREACH_NLRI:
         ND_TCHECK_LEN(tptr, BGP_MP_NLRI_MINSIZE);
-        af = GET_BE_U_2(tptr);
-        safi = GET_U_1(tptr + 2);
-
-        ND_PRINT("\n\t    AFI: %s (%u), %sSAFI: %s (%u)",
-                  tok2str(af_values, "Unknown AFI", af),
-                  af,
-                  (safi>128) ? "vendor specific " : "", /* 128 is meanwhile wellknown */
-                  tok2str(bgp_safi_values, "Unknown SAFI", safi),
-                  safi);
+       ret = bgp_mp_af_print(ndo, tptr, tlen, &af, &safi);
+       if (ret == -2)
+           goto trunc;
+       if (ret < 0)
+           break;
 
         if (len == BGP_MP_NLRI_MINSIZE)
             ND_PRINT("\n\t      End-of-Rib Marker (empty NLRI)");
 
         tptr += 3;
 
-        add_path4 = check_add_path(ndo, tptr, (len-(tptr - pptr)), 32);
-        add_path6 = check_add_path(ndo, tptr, (len-(tptr - pptr)), 128);
+        add_path4 = check_add_path(ndo, tptr, (len-ND_BYTES_BETWEEN(tptr, pptr)), 32);
+        add_path6 = check_add_path(ndo, tptr, (len-ND_BYTES_BETWEEN(tptr, pptr)), 128);
 
         while (tptr < pptr + len) {
-            switch (af<<8 | safi) {
-            case (AFNUM_INET<<8 | SAFNUM_UNICAST):
-            case (AFNUM_INET<<8 | SAFNUM_MULTICAST):
-            case (AFNUM_INET<<8 | SAFNUM_UNIMULTICAST):
-                if (add_path4) {
-                    path_id = GET_BE_U_4(tptr);
-                    tptr += 4;
-                }
-                advance = decode_prefix4(ndo, tptr, len, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal prefix length)");
-                else if (advance == -2)
-                    goto trunc;
-                else if (advance == -3)
-                    break; /* bytes left, but not enough */
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                if (add_path4) {
-                    ND_PRINT("   Path Id: %u", path_id);
-                }
-                break;
-            case (AFNUM_INET<<8 | SAFNUM_LABUNICAST):
-                advance = decode_labeled_prefix4(ndo, tptr, len, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal prefix length)");
-                else if (advance == -2)
-                    goto trunc;
-                else if (advance == -3)
-                    break; /* bytes left, but not enough */
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                break;
-            case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
-            case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
-            case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
-                advance = decode_labeled_vpn_prefix4(ndo, tptr, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal prefix length)");
-                else if (advance == -2)
-                    goto trunc;
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                break;
-            case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
-            case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
-            case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
-                if (add_path6) {
-                    path_id = GET_BE_U_4(tptr);
-                    tptr += 4;
-                }
-                advance = decode_prefix6(ndo, tptr, len, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal prefix length)");
-                else if (advance == -2)
-                    goto trunc;
-                else if (advance == -3)
-                    break; /* bytes left, but not enough */
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                if (add_path6) {
-                    ND_PRINT("   Path Id: %u", path_id);
-                }
-                break;
-            case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
-                advance = decode_labeled_prefix6(ndo, tptr, len, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal prefix length)");
-                else if (advance == -2)
-                    goto trunc;
-                else if (advance == -3)
-                    break; /* bytes left, but not enough */
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                break;
-            case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
-            case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
-            case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
-                advance = decode_labeled_vpn_prefix6(ndo, tptr, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal prefix length)");
-                else if (advance == -2)
-                    goto trunc;
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                break;
-            case (AFNUM_VPLS<<8 | SAFNUM_VPLS):
-            case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNICAST):
-            case (AFNUM_L2VPN<<8 | SAFNUM_VPNMULTICAST):
-            case (AFNUM_L2VPN<<8 | SAFNUM_VPNUNIMULTICAST):
-                advance = decode_labeled_vpn_l2(ndo, tptr, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal length)");
-                else if (advance == -2)
-                    goto trunc;
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                break;
-            case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
-            case (AFNUM_NSAP<<8 | SAFNUM_MULTICAST):
-            case (AFNUM_NSAP<<8 | SAFNUM_UNIMULTICAST):
-                advance = decode_clnp_prefix(ndo, tptr, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal prefix length)");
-                else if (advance == -2)
-                    goto trunc;
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                break;
-            case (AFNUM_NSAP<<8 | SAFNUM_VPNUNICAST):
-            case (AFNUM_NSAP<<8 | SAFNUM_VPNMULTICAST):
-            case (AFNUM_NSAP<<8 | SAFNUM_VPNUNIMULTICAST):
-                advance = decode_labeled_vpn_clnp_prefix(ndo, tptr, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal prefix length)");
-                else if (advance == -2)
-                    goto trunc;
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                break;
-            case (AFNUM_INET<<8 | SAFNUM_MDT):
-                advance = decode_mdt_vpn_nlri(ndo, tptr, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal prefix length)");
-                else if (advance == -2)
-                    goto trunc;
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                break;
-            case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN): /* fall through */
-            case (AFNUM_INET6<<8 | SAFNUM_MULTICAST_VPN):
-                advance = decode_multicast_vpn(ndo, tptr, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal prefix length)");
-                else if (advance == -2)
-                    goto trunc;
-                else
-                    ND_PRINT("\n\t      %s", buf);
-                break;
-            default:
-                ND_TCHECK_LEN(tptr - 3, tlen);
-                ND_PRINT("no AFI %u / SAFI %u decoder", af, safi);
-                if (ndo->ndo_vflag <= 1)
-                    print_unknown_data(ndo, tptr-3, "\n\t    ", tlen);
-                advance = 0;
-                tptr = pptr + len;
-                break;
-            }
+            advance = bgp_nlri_print(ndo, af, safi, tptr, len, buf, sizeof(buf),
+                    add_path4, add_path6);
+            if (advance == -2)
+                goto trunc;
             if (advance < 0)
                 break;
             tptr += advance;
@@ -2493,6 +2388,7 @@ bgp_attr_print(netdissect_options *ndo,
             print_unknown_data(ndo, pptr, "\n\t    ", len);
         break;
     }
+done:
     if (ndo->ndo_vflag > 1 && len) { /* omit zero length attributes*/
         ND_TCHECK_LEN(pptr, len);
         print_unknown_data(ndo, pptr, "\n\t    ", len);
@@ -2532,6 +2428,22 @@ bgp_capabilities_print(netdissect_options *ndo,
                tok2str(bgp_safi_values, "Unknown", GET_U_1(opt + i + 5)),
                GET_U_1(opt + i + 5));
             break;
+        case BGP_CAPCODE_ML:
+            cap_offset = 2;
+            tcap_len = cap_len;
+            while (tcap_len >= 4) {
+                ND_PRINT( "\n\t\tAFI %s (%u), SAFI %s (%u), Count: %u",
+                       tok2str(af_values, "Unknown",
+                                  GET_BE_U_2(opt + i + cap_offset)),
+                       GET_BE_U_2(opt + i + cap_offset),
+                       tok2str(bgp_safi_values, "Unknown",
+                                  GET_U_1(opt + i + cap_offset + 2)),
+                       GET_U_1(opt + i + cap_offset + 2),
+                       GET_U_1(opt + i + cap_offset + 3));
+                tcap_len -= 4;
+                cap_offset += 4;
+            }
+            break;
         case BGP_CAPCODE_RESTART:
             /* Restart Flags (4 bits), Restart Time in seconds (12 bits) */
             if (cap_len < 2) {