]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-bgp.c
ICMPv6: Add the IPv6-Only flag
[tcpdump] / print-bgp.c
index c8849cb4e8d18d4a2f377b2cdabb7c3e91a4cd9a..6f523585f0f0d3ad26f896e586d6cbee05dd7d57 100644 (file)
@@ -131,20 +131,20 @@ struct bgp_route_refresh {
 #define BGPTYPE_RCID_PATH               13    /* deprecated RFC1863 */
 #define BGPTYPE_MP_REACH_NLRI           14    /* RFC4760 */
 #define BGPTYPE_MP_UNREACH_NLRI         15    /* RFC4760 */
-#define BGPTYPE_EXTD_COMMUNITIES        16      /* RFC4360 */
-#define BGPTYPE_AS4_PATH                17      /* RFC6793 */
-#define BGPTYPE_AGGREGATOR4             18      /* RFC6793 */
-#define BGPTYPE_PMSI_TUNNEL             22      /* RFC6514 */
-#define BGPTYPE_TUNNEL_ENCAP            23      /* RFC5512 */
-#define BGPTYPE_TRAFFIC_ENG             24      /* RFC5543 */
-#define BGPTYPE_IPV6_EXTD_COMMUNITIES   25      /* RFC5701 */
-#define BGPTYPE_AIGP                    26      /* RFC7311 */
-#define BGPTYPE_PE_DISTINGUISHER_LABEL  27      /* RFC6514 */
-#define BGPTYPE_ENTROPY_LABEL           28      /* RFC6790 */
-#define BGPTYPE_LARGE_COMMUNITY         32      /* draft-ietf-idr-large-community-05 */
-#define BGPTYPE_ATTR_SET               128      /* RFC6368 */
-
-#define BGP_MP_NLRI_MINSIZE              3       /* End of RIB Marker detection */
+#define BGPTYPE_EXTD_COMMUNITIES        16    /* RFC4360 */
+#define BGPTYPE_AS4_PATH                17    /* RFC6793 */
+#define BGPTYPE_AGGREGATOR4             18    /* RFC6793 */
+#define BGPTYPE_PMSI_TUNNEL             22    /* RFC6514 */
+#define BGPTYPE_TUNNEL_ENCAP            23    /* RFC5512 */
+#define BGPTYPE_TRAFFIC_ENG             24    /* RFC5543 */
+#define BGPTYPE_IPV6_EXTD_COMMUNITIES   25    /* RFC5701 */
+#define BGPTYPE_AIGP                    26    /* RFC7311 */
+#define BGPTYPE_PE_DISTINGUISHER_LABEL  27    /* RFC6514 */
+#define BGPTYPE_ENTROPY_LABEL           28    /* RFC6790 */
+#define BGPTYPE_LARGE_COMMUNITY         32    /* draft-ietf-idr-large-community-05 */
+#define BGPTYPE_ATTR_SET               128    /* RFC6368 */
+
+#define BGP_MP_NLRI_MINSIZE              3    /* End of RIB Marker detection */
 
 static const struct tok bgp_attr_values[] = {
     { BGPTYPE_ORIGIN,           "Origin"},
@@ -410,7 +410,7 @@ static const struct tok bgp_safi_values[] = {
 #define BGP_COMMUNITY_NO_ADVERT              0xffffff02
 #define BGP_COMMUNITY_NO_EXPORT_SUBCONFED    0xffffff03
 
-/* Extended community type - draft-ietf-idr-bgp-ext-communities-05 */
+/* Extended community type - RFC 4360 */
 #define BGP_EXT_COM_RT_0        0x0002  /* Route Target,Format AS(2bytes):AN(4bytes) */
 #define BGP_EXT_COM_RT_1        0x0102  /* Route Target,Format IP address:AN(2bytes) */
 #define BGP_EXT_COM_RT_2        0x0202  /* Route Target,Format AN(4bytes):local(2bytes) */
@@ -468,12 +468,12 @@ static const struct tok bgp_extd_comm_subtype_values[] = {
     { BGP_EXT_COM_OSPF_RID,    "ospf-router-id"},
     { BGP_EXT_COM_OSPF_RID2,   "ospf-router-id"},
     { BGP_EXT_COM_L2INFO,      "layer2-info"},
-    { BGP_EXT_COM_EIGRP_GEN , "eigrp-general-route (flag, tag)" },
-    { BGP_EXT_COM_EIGRP_METRIC_AS_DELAY , "eigrp-route-metric (AS, delay)" },
-    { BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW , "eigrp-route-metric (reliability, nexthop, bandwidth)" },
-    { BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU , "eigrp-route-metric (load, MTU)" },
-    { BGP_EXT_COM_EIGRP_EXT_REMAS_REMID , "eigrp-external-route (remote-AS, remote-ID)" },
-    { BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC , "eigrp-external-route (remote-proto, remote-metric)" },
+    { BGP_EXT_COM_EIGRP_GEN,   "eigrp-general-route (flag, tag)" },
+    { BGP_EXT_COM_EIGRP_METRIC_AS_DELAY, "eigrp-route-metric (AS, delay)" },
+    { BGP_EXT_COM_EIGRP_METRIC_REL_NH_BW, "eigrp-route-metric (reliability, nexthop, bandwidth)" },
+    { BGP_EXT_COM_EIGRP_METRIC_LOAD_MTU, "eigrp-route-metric (load, MTU)" },
+    { BGP_EXT_COM_EIGRP_EXT_REMAS_REMID, "eigrp-external-route (remote-AS, remote-ID)" },
+    { BGP_EXT_COM_EIGRP_EXT_REMPROTO_REMMETRIC, "eigrp-external-route (remote-proto, remote-metric)" },
     { BGP_EXT_COM_SOURCE_AS, "source-AS" },
     { BGP_EXT_COM_VRF_RT_IMP, "vrf-route-import"},
     { BGP_EXT_COM_L2VPN_RT_0, "l2vpn-id"},
@@ -524,9 +524,9 @@ as_printf(netdissect_options *ndo,
           char *str, size_t size, u_int asnum)
 {
     if (!ndo->ndo_bflag || asnum <= 0xFFFF) {
-        snprintf(str, size, "%u", asnum);
+        nd_snprintf(str, size, "%u", asnum);
     } else {
-        snprintf(str, size, "%u.%u", asnum >> 16, asnum & 0xFFFF);
+        nd_snprintf(str, size, "%u.%u", asnum >> 16, asnum & 0xFFFF);
     }
     return str;
 }
@@ -555,7 +555,7 @@ decode_prefix4(netdissect_options *ndo,
     if (plen % 8) {
         ((u_char *)&addr)[plenbytes - 1] &= ((0xff00 >> (plen % 8)) & 0xff);
     }
-    snprintf(buf, buflen, "%s/%u", ipaddr_string(ndo, &addr), plen);
+    nd_snprintf(buf, buflen, "%s/%u", ipaddr_string(ndo, (const u_char *)&addr), plen);
     return 1 + plenbytes;
 
 trunc:
@@ -603,8 +603,8 @@ 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 */
-    snprintf(buf, buflen, "%s/%u, label:%u %s",
-             ipaddr_string(ndo, &addr),
+    nd_snprintf(buf, buflen, "%s/%u, label:%u %s",
+             ipaddr_string(ndo, (const u_char *)&addr),
              plen,
              EXTRACT_BE_U_3(pptr + 1)>>4,
              ((EXTRACT_U_1(pptr + 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
@@ -633,16 +633,16 @@ bgp_vpn_ip_print(netdissect_options *ndo,
     char *pos = addr;
 
     switch(addr_length) {
-    case (sizeof(struct in_addr) << 3): /* 32 */
-        ND_TCHECK_LEN(pptr, sizeof(struct in_addr));
-        snprintf(pos, sizeof(addr), "%s", ipaddr_string(ndo, pptr));
+    case (sizeof(nd_ipv4) << 3): /* 32 */
+        ND_TCHECK_LEN(pptr, sizeof(nd_ipv4));
+        nd_snprintf(pos, sizeof(addr), "%s", ipaddr_string(ndo, pptr));
         break;
-    case (sizeof(struct in6_addr) << 3): /* 128 */
-        ND_TCHECK_LEN(pptr, sizeof(struct in6_addr));
-        snprintf(pos, sizeof(addr), "%s", ip6addr_string(ndo, pptr));
+    case (sizeof(nd_ipv6) << 3): /* 128 */
+        ND_TCHECK_LEN(pptr, sizeof(nd_ipv6));
+        nd_snprintf(pos, sizeof(addr), "%s", ip6addr_string(ndo, pptr));
         break;
     default:
-        snprintf(pos, sizeof(addr), "bogus address length %u", addr_length);
+        nd_snprintf(pos, sizeof(addr), "bogus address length %u", addr_length);
         break;
     }
     pos += strlen(pos);
@@ -689,7 +689,7 @@ bgp_vpn_sg_print(netdissect_options *ndo,
     total_length += (addr_length >> 3) + 1;
     offset = strlen(buf);
     if (addr_length) {
-        snprintf(buf + offset, buflen - offset, ", Source %s",
+        nd_snprintf(buf + offset, buflen - offset, ", Source %s",
              bgp_vpn_ip_print(ndo, pptr, addr_length));
         pptr += (addr_length >> 3);
     }
@@ -704,7 +704,7 @@ bgp_vpn_sg_print(netdissect_options *ndo,
     total_length += (addr_length >> 3) + 1;
     offset = strlen(buf);
     if (addr_length) {
-        snprintf(buf + offset, buflen - offset, ", Group %s",
+        nd_snprintf(buf + offset, buflen - offset, ", Group %s",
              bgp_vpn_ip_print(ndo, pptr, addr_length));
         pptr += (addr_length >> 3);
     }
@@ -713,10 +713,8 @@ trunc:
     return (total_length);
 }
 
-/* RDs and RTs share the same semantics
- * we use bgp_vpn_rd_print for
- * printing route targets inside a NLRI */
-char *
+/* Print an RFC 4364 Route Distinguisher */
+const char *
 bgp_vpn_rd_print(netdissect_options *ndo,
                  const u_char *pptr)
 {
@@ -727,33 +725,33 @@ bgp_vpn_rd_print(netdissect_options *ndo,
     /* ok lets load the RD format */
     switch (EXTRACT_BE_U_2(pptr)) {
 
-        /* 2-byte-AS:number fmt*/
     case 0:
-        snprintf(pos, sizeof(rd) - (pos - rd), "%u:%u (= %u.%u.%u.%u)",
-                 EXTRACT_BE_U_2(pptr + 2),
-                 EXTRACT_BE_U_4(pptr + 4),
-                 EXTRACT_U_1(pptr + 4), EXTRACT_U_1(pptr + 5),
-                 EXTRACT_U_1(pptr + 6), EXTRACT_U_1(pptr + 7));
+        /* 2-byte-AS:number fmt */
+        nd_snprintf(pos, sizeof(rd) - (pos - rd), "%u:%u (= %u.%u.%u.%u)",
+                    EXTRACT_BE_U_2(pptr + 2),
+                    EXTRACT_BE_U_4(pptr + 4),
+                    EXTRACT_U_1(pptr + 4), EXTRACT_U_1(pptr + 5),
+                    EXTRACT_U_1(pptr + 6), EXTRACT_U_1(pptr + 7));
         break;
-        /* IP-address:AS fmt*/
 
     case 1:
-        snprintf(pos, sizeof(rd) - (pos - rd), "%u.%u.%u.%u:%u",
-                 EXTRACT_U_1(pptr + 2), EXTRACT_U_1(pptr + 3),
-                 EXTRACT_U_1(pptr + 4), EXTRACT_U_1(pptr + 5),
-                 EXTRACT_BE_U_2(pptr + 6));
+        /* IP-address:AS fmt */
+        nd_snprintf(pos, sizeof(rd) - (pos - rd), "%u.%u.%u.%u:%u",
+                    EXTRACT_U_1(pptr + 2), EXTRACT_U_1(pptr + 3),
+                    EXTRACT_U_1(pptr + 4), EXTRACT_U_1(pptr + 5),
+                    EXTRACT_BE_U_2(pptr + 6));
         break;
 
-        /* 4-byte-AS:number fmt*/
     case 2:
-        snprintf(pos, sizeof(rd) - (pos - rd), "%s:%u (%u.%u.%u.%u:%u)",
-                 as_printf(ndo, astostr, sizeof(astostr), EXTRACT_BE_U_4(pptr + 2)),
-                 EXTRACT_BE_U_2(pptr + 6), EXTRACT_U_1(pptr + 2),
-                 EXTRACT_U_1(pptr + 3), EXTRACT_U_1(pptr + 4),
-                 EXTRACT_U_1(pptr + 5), EXTRACT_BE_U_2(pptr + 6));
+        /* 4-byte-AS:number fmt */
+        nd_snprintf(pos, sizeof(rd) - (pos - rd), "%s:%u (%u.%u.%u.%u:%u)",
+                    as_printf(ndo, astostr, sizeof(astostr), EXTRACT_BE_U_4(pptr + 2)),
+                    EXTRACT_BE_U_2(pptr + 6), EXTRACT_U_1(pptr + 2),
+                    EXTRACT_U_1(pptr + 3), EXTRACT_U_1(pptr + 4),
+                    EXTRACT_U_1(pptr + 5), EXTRACT_BE_U_2(pptr + 6));
         break;
     default:
-        snprintf(pos, sizeof(rd) - (pos - rd), "unknown RD format");
+        nd_snprintf(pos, sizeof(rd) - (pos - rd), "unknown RD format");
         break;
     }
     pos += strlen(pos);
@@ -761,11 +759,101 @@ bgp_vpn_rd_print(netdissect_options *ndo,
     return (rd);
 }
 
+/*
+ * Print an RFC 4360 Extended Community.
+ */
+static void
+bgp_extended_community_print(netdissect_options *ndo,
+                             const u_char *pptr)
+{
+    union { /* copy buffer for bandwidth values */
+        float f;
+        uint32_t i;
+    } bw;
+
+    switch (EXTRACT_BE_U_2(pptr)) {
+
+    case BGP_EXT_COM_RT_0:
+    case BGP_EXT_COM_RO_0:
+    case BGP_EXT_COM_L2VPN_RT_0:
+        ND_PRINT("%u:%u (= %s)",
+                 EXTRACT_BE_U_2(pptr + 2),
+                 EXTRACT_BE_U_4(pptr + 4),
+                 ipaddr_string(ndo, pptr+4));
+        break;
+
+    case BGP_EXT_COM_RT_1:
+    case BGP_EXT_COM_RO_1:
+    case BGP_EXT_COM_L2VPN_RT_1:
+    case BGP_EXT_COM_VRF_RT_IMP:
+        ND_PRINT("%s:%u",
+                 ipaddr_string(ndo, pptr+2),
+                 EXTRACT_BE_U_2(pptr + 6));
+        break;
+
+    case BGP_EXT_COM_RT_2:
+        case BGP_EXT_COM_RO_2:
+            ND_PRINT("%s:%u",
+                     as_printf(ndo, astostr, sizeof(astostr),
+                     EXTRACT_BE_U_4(pptr + 2)), EXTRACT_BE_U_2(pptr + 6));
+            break;
+
+    case BGP_EXT_COM_LINKBAND:
+            bw.i = EXTRACT_BE_U_4(pptr + 2);
+            ND_PRINT("bandwidth: %.3f Mbps",
+                     bw.f*8/1000000);
+            break;
+
+    case BGP_EXT_COM_VPN_ORIGIN:
+    case BGP_EXT_COM_VPN_ORIGIN2:
+    case BGP_EXT_COM_VPN_ORIGIN3:
+    case BGP_EXT_COM_VPN_ORIGIN4:
+    case BGP_EXT_COM_OSPF_RID:
+    case BGP_EXT_COM_OSPF_RID2:
+        ND_PRINT("%s", ipaddr_string(ndo, pptr+2));
+        break;
+
+    case BGP_EXT_COM_OSPF_RTYPE:
+    case BGP_EXT_COM_OSPF_RTYPE2:
+        ND_PRINT("area:%s, router-type:%s, metric-type:%s%s",
+                 ipaddr_string(ndo, pptr+2),
+                 tok2str(bgp_extd_comm_ospf_rtype_values,
+                         "unknown (0x%02x)",
+                         EXTRACT_U_1((pptr + 6))),
+                 (EXTRACT_U_1(pptr + 7) &  BGP_OSPF_RTYPE_METRIC_TYPE) ? "E2" : "",
+                 ((EXTRACT_U_1(pptr + 6) == BGP_OSPF_RTYPE_EXT) || (EXTRACT_U_1(pptr + 6) == BGP_OSPF_RTYPE_NSSA)) ? "E1" : "");
+        break;
+
+    case BGP_EXT_COM_L2INFO:
+        ND_PRINT("%s Control Flags [0x%02x]:MTU %u",
+                 tok2str(l2vpn_encaps_values,
+                         "unknown encaps",
+                         EXTRACT_U_1((pptr + 2))),
+                 EXTRACT_U_1((pptr + 3)),
+                 EXTRACT_BE_U_2(pptr + 4));
+        break;
+
+    case BGP_EXT_COM_SOURCE_AS:
+        ND_PRINT("AS %u", EXTRACT_BE_U_2(pptr + 2));
+        break;
+
+    default:
+        ND_PRINT("%02x%02x%02x%02x%02x%02x",
+                 EXTRACT_U_1(pptr + 2),
+                 EXTRACT_U_1(pptr + 3),
+                 EXTRACT_U_1(pptr + 4),
+                 EXTRACT_U_1(pptr + 5),
+                 EXTRACT_U_1(pptr + 6),
+                 EXTRACT_U_1(pptr + 7));
+        break;
+    }
+}
+
+/* RFC 4684 */
 static int
 decode_rt_routing_info(netdissect_options *ndo,
-                       const u_char *pptr, char *buf, u_int buflen)
+                       const u_char *pptr)
 {
-    uint8_t route_target[8];
     u_int plen;
     char asbuf[sizeof(astostr)]; /* bgp_vpn_rd_print() overwrites astostr */
 
@@ -779,12 +867,14 @@ decode_rt_routing_info(netdissect_options *ndo,
      */
     if (0 == plen) {
         /* Without "origin AS", without "route target". */
-        snprintf(buf, buflen, "default route target");
+        ND_PRINT("\n\t      default route target");
         return 1;
     }
 
-    if (32 > plen)
+    if (32 > plen) {
+        ND_PRINT("\n\t      (illegal prefix length)");
         return -1;
+    }
 
     /* With at least "origin AS", possibly with "route target". */
     ND_TCHECK_4(pptr + 1);
@@ -792,26 +882,18 @@ decode_rt_routing_info(netdissect_options *ndo,
 
     plen -= 32; /* adjust prefix length */
 
-    if (64 < plen)
-        return -1;
-
-    /* From now on (plen + 7) / 8 evaluates to { 0, 1, 2, ..., 8 }
-     * and gives the number of octets in the variable-length "route
-     * target" field inside this NLRI "prefix". Look for it.
+    /* An extended community is 8 octets, so the remaining prefix
+     * length must be 64.
      */
-    memset(&route_target, 0, sizeof(route_target));
-    ND_TCHECK_LEN(pptr + 5, (plen + 7) / 8);
-    memcpy(&route_target, pptr + 5, (plen + 7) / 8);
-    /* Which specification says to do this? */
-    if (plen % 8) {
-        ((u_char *)&route_target)[(plen + 7) / 8 - 1] &=
-            ((0xff00 >> (plen % 8)) & 0xff);
+    if (64 != plen) {
+        ND_PRINT("\n\t    (illegal prefix length)");
+        return -1;
     }
-    snprintf(buf, buflen, "origin AS: %s, route target %s",
-             asbuf,
-             bgp_vpn_rd_print(ndo, (u_char *)&route_target));
+    ND_TCHECK_LEN(pptr + 5, 8);
+    ND_PRINT("\n\t      origin AS: %s, route target ", asbuf);
+    bgp_extended_community_print(ndo, pptr + 5);
 
-    return 5 + (plen + 7) / 8;
+    return 5 + 8;
 
 trunc:
     return -2;
@@ -843,12 +925,12 @@ decode_labeled_vpn_prefix4(netdissect_options *ndo,
             ((0xff00 >> (plen % 8)) & 0xff);
     }
     /* the label may get offsetted by 4 bits so lets shift it right */
-    snprintf(buf, buflen, "RD: %s, %s/%u, label:%u %s",
-             bgp_vpn_rd_print(ndo, pptr+4),
-             ipaddr_string(ndo, &addr),
-             plen,
-             EXTRACT_BE_U_3(pptr + 1)>>4,
-             ((EXTRACT_U_1(pptr + 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
+    nd_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,
+                EXTRACT_BE_U_3(pptr + 1)>>4,
+                ((EXTRACT_U_1(pptr + 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
 
     return 12 + (plen + 7) / 8;
 
@@ -888,15 +970,15 @@ decode_mdt_vpn_nlri(netdissect_options *ndo,
     pptr += 8;
 
     /* IPv4 address */
-    ND_TCHECK_LEN(pptr, sizeof(struct in_addr));
+    ND_TCHECK_LEN(pptr, sizeof(nd_ipv4));
     vpn_ip = pptr;
-    pptr += sizeof(struct in_addr);
+    pptr += sizeof(nd_ipv4);
 
     /* MDT Group Address */
-    ND_TCHECK_LEN(pptr, sizeof(struct in_addr));
+    ND_TCHECK_LEN(pptr, sizeof(nd_ipv4));
 
-    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));
+    nd_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;
 
@@ -937,7 +1019,7 @@ decode_multicast_vpn(netdissect_options *ndo,
     route_length = EXTRACT_U_1(pptr);
     pptr++;
 
-    snprintf(buf, buflen, "Route-Type: %s (%u), length: %u",
+    nd_snprintf(buf, buflen, "Route-Type: %s (%u), length: %u",
          tok2str(bgp_multicast_vpn_route_type_values,
                  "Unknown", route_type),
          route_type, route_length);
@@ -946,15 +1028,15 @@ decode_multicast_vpn(netdissect_options *ndo,
     case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI:
         ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN);
         offset = 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));
+        nd_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);
-        snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s",
+        nd_snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s",
         bgp_vpn_rd_print(ndo, pptr),
         as_printf(ndo, astostr, sizeof(astostr),
         EXTRACT_BE_U_4(pptr + BGP_VPN_RD_LEN)));
@@ -963,8 +1045,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);
-        snprintf(buf + offset, buflen - offset, ", RD: %s",
-                 bgp_vpn_rd_print(ndo, pptr));
+        nd_snprintf(buf + offset, buflen - offset, ", RD: %s",
+                    bgp_vpn_rd_print(ndo, pptr));
         pptr += BGP_VPN_RD_LEN;
 
         sg_length = bgp_vpn_sg_print(ndo, pptr, buf, buflen);
@@ -972,15 +1054,15 @@ decode_multicast_vpn(netdissect_options *ndo,
 
         ND_TCHECK_LEN(pptr, addr_length);
         offset = strlen(buf);
-        snprintf(buf + offset, buflen - offset, ", Originator %s",
-                 bgp_vpn_ip_print(ndo, pptr, addr_length << 3));
+        nd_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);
-        snprintf(buf + offset, buflen - offset, ", RD: %s",
-                 bgp_vpn_rd_print(ndo, pptr));
+        nd_snprintf(buf + offset, buflen - offset, ", RD: %s",
+                    bgp_vpn_rd_print(ndo, pptr));
         pptr += BGP_VPN_RD_LEN;
 
         bgp_vpn_sg_print(ndo, pptr, buf, buflen);
@@ -990,10 +1072,10 @@ decode_multicast_vpn(netdissect_options *ndo,
     case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN:
         ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN + 4);
         offset = strlen(buf);
-        snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s",
-                 bgp_vpn_rd_print(ndo, pptr),
-                 as_printf(ndo, astostr, sizeof(astostr),
-                 EXTRACT_BE_U_4(pptr + BGP_VPN_RD_LEN)));
+        nd_snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s",
+                    bgp_vpn_rd_print(ndo, pptr),
+                    as_printf(ndo, astostr, sizeof(astostr),
+                    EXTRACT_BE_U_4(pptr + BGP_VPN_RD_LEN)));
         pptr += BGP_VPN_RD_LEN + 4;
 
         bgp_vpn_sg_print(ndo, pptr, buf, buflen);
@@ -1052,9 +1134,9 @@ decode_labeled_vpn_l2(netdissect_options *ndo,
         /* assume AD-only with RD, BGPNH */
         ND_TCHECK_LEN(pptr, 12);
         buf[0] = '\0';
-        stringlen = snprintf(buf, buflen, "RD: %s, BGPNH: %s",
-                             bgp_vpn_rd_print(ndo, pptr),
-                             ipaddr_string(ndo, pptr+8));
+        stringlen = nd_snprintf(buf, buflen, "RD: %s, BGPNH: %s",
+                                bgp_vpn_rd_print(ndo, pptr),
+                                ipaddr_string(ndo, pptr+8));
         UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
         pptr += 12;
         tlen -= 12;
@@ -1065,11 +1147,11 @@ decode_labeled_vpn_l2(netdissect_options *ndo,
 
         ND_TCHECK_LEN(pptr, 15);
         buf[0] = '\0';
-        stringlen = snprintf(buf, buflen, "RD: %s, CE-ID: %u, Label-Block Offset: %u, Label Base %u",
-                             bgp_vpn_rd_print(ndo, pptr),
-                             EXTRACT_BE_U_2(pptr + 8),
-                             EXTRACT_BE_U_2(pptr + 10),
-                             EXTRACT_BE_U_3(pptr + 12)>>4); /* the label is offsetted by 4 bits so lets shift it right */
+        stringlen = nd_snprintf(buf, buflen, "RD: %s, CE-ID: %u, Label-Block Offset: %u, Label Base %u",
+                                bgp_vpn_rd_print(ndo, pptr),
+                                EXTRACT_BE_U_2(pptr + 8),
+                                EXTRACT_BE_U_2(pptr + 10),
+                                EXTRACT_BE_U_3(pptr + 12)>>4); /* the label is offsetted by 4 bits so lets shift it right */
         UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
         pptr += 15;
         tlen -= 15;
@@ -1078,7 +1160,7 @@ decode_labeled_vpn_l2(netdissect_options *ndo,
         while (tlen != 0) {
             if (tlen < 3) {
                 if (buflen != 0) {
-                    stringlen=snprintf(buf,buflen, "\n\t\tran past the end");
+                    stringlen=nd_snprintf(buf,buflen, "\n\t\tran past the end");
                     UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
                 }
                 return plen + 2;
@@ -1093,23 +1175,23 @@ decode_labeled_vpn_l2(netdissect_options *ndo,
             switch(tlv_type) {
             case 1:
                 if (buflen != 0) {
-                    stringlen=snprintf(buf,buflen, "\n\t\tcircuit status vector (%u) length: %u: 0x",
-                                       tlv_type,
-                                       tlv_len);
+                    stringlen=nd_snprintf(buf,buflen, "\n\t\tcircuit status vector (%u) length: %u: 0x",
+                                          tlv_type,
+                                          tlv_len);
                     UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
                 }
                 while (ttlv_len != 0) {
                     if (tlen < 1) {
                         if (buflen != 0) {
-                            stringlen=snprintf(buf,buflen, " (ran past the end)");
+                            stringlen=nd_snprintf(buf,buflen, " (ran past the end)");
                             UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
                         }
                         return plen + 2;
                     }
                     ND_TCHECK_1(pptr);
                     if (buflen != 0) {
-                        stringlen=snprintf(buf,buflen, "%02x",
-                                           EXTRACT_U_1(pptr));
+                        stringlen=nd_snprintf(buf,buflen, "%02x",
+                                              EXTRACT_U_1(pptr));
                         pptr++;
                         UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
                     }
@@ -1119,14 +1201,14 @@ decode_labeled_vpn_l2(netdissect_options *ndo,
                 break;
             default:
                 if (buflen != 0) {
-                    stringlen=snprintf(buf,buflen, "\n\t\tunknown TLV #%u, length: %u",
-                                       tlv_type,
-                                       tlv_len);
+                    stringlen=nd_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=snprintf(buf,buflen, " (ran past the end)");
+                        stringlen=nd_snprintf(buf,buflen, " (ran past the end)");
                         UPDATE_BUF_BUFLEN(buf, buflen, stringlen);
                     }
                     return plen + 2;
@@ -1169,7 +1251,7 @@ decode_prefix6(netdissect_options *ndo,
         addr.s6_addr[plenbytes - 1] &=
             ((0xff00 >> (plen % 8)) & 0xff);
     }
-    snprintf(buf, buflen, "%s/%u", ip6addr_string(ndo, &addr), plen);
+    nd_snprintf(buf, buflen, "%s/%u", ip6addr_string(ndo, (const u_char *)&addr), plen);
     return 1 + plenbytes;
 
 trunc:
@@ -1209,11 +1291,11 @@ decode_labeled_prefix6(netdissect_options *ndo,
             ((0xff00 >> (plen % 8)) & 0xff);
     }
     /* the label may get offsetted by 4 bits so lets shift it right */
-    snprintf(buf, buflen, "%s/%u, label:%u %s",
-             ip6addr_string(ndo, &addr),
-             plen,
-             EXTRACT_BE_U_3(pptr + 1)>>4,
-             ((EXTRACT_U_1(pptr + 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
+    nd_snprintf(buf, buflen, "%s/%u, label:%u %s",
+                ip6addr_string(ndo, (const u_char *)&addr),
+                plen,
+                EXTRACT_BE_U_3(pptr + 1)>>4,
+                ((EXTRACT_U_1(pptr + 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
 
     return 4 + plenbytes;
 
@@ -1250,12 +1332,12 @@ decode_labeled_vpn_prefix6(netdissect_options *ndo,
             ((0xff00 >> (plen % 8)) & 0xff);
     }
     /* the label may get offsetted by 4 bits so lets shift it right */
-    snprintf(buf, buflen, "RD: %s, %s/%u, label:%u %s",
-             bgp_vpn_rd_print(ndo, pptr+4),
-             ip6addr_string(ndo, &addr),
-             plen,
-             EXTRACT_BE_U_3(pptr + 1)>>4,
-             ((EXTRACT_U_1(pptr + 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
+    nd_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,
+                EXTRACT_BE_U_3(pptr + 1)>>4,
+                ((EXTRACT_U_1(pptr + 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
 
     return 12 + (plen + 7) / 8;
 
@@ -1283,9 +1365,9 @@ decode_clnp_prefix(netdissect_options *ndo,
         addr[(plen + 7) / 8 - 1] &=
             ((0xff00 >> (plen % 8)) & 0xff);
     }
-    snprintf(buf, buflen, "%s/%u",
-             isonsap_string(ndo, addr,(plen + 7) / 8),
-             plen);
+    nd_snprintf(buf, buflen, "%s/%u",
+                isonsap_string(ndo, addr,(plen + 7) / 8),
+                plen);
 
     return 1 + (plen + 7) / 8;
 
@@ -1318,12 +1400,12 @@ 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 */
-    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,
-             EXTRACT_BE_U_3(pptr + 1)>>4,
-             ((EXTRACT_U_1(pptr + 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
+    nd_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,
+                EXTRACT_BE_U_3(pptr + 1)>>4,
+                ((EXTRACT_U_1(pptr + 3) & 1) == 0) ? "(BOGUS: Bottom of Stack NOT set!)" : "(bottom)" );
 
     return 12 + (plen + 7) / 8;
 
@@ -1463,10 +1545,6 @@ bgp_attr_print(netdissect_options *ndo,
     u_int i;
     uint16_t af;
     uint8_t safi, snpa, nhlen;
-    union { /* copy buffer for bandwidth values */
-        float f;
-        uint32_t i;
-    } bw;
     int advance;
     u_int tlen;
     const u_char *tptr;
@@ -1644,6 +1722,8 @@ bgp_attr_print(netdissect_options *ndo,
         break;
     case BGPTYPE_MP_REACH_NLRI:
         ND_TCHECK_3(tptr);
+        if (tlen < 3)
+            goto trunc;
         af = EXTRACT_BE_U_2(tptr);
         safi = EXTRACT_U_1(tptr + 2);
 
@@ -1714,74 +1794,74 @@ bgp_attr_print(netdissect_options *ndo,
                 case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
                 case (AFNUM_INET<<8 | SAFNUM_MULTICAST_VPN):
                 case (AFNUM_INET<<8 | SAFNUM_MDT):
-                    if (tlen < sizeof(struct in_addr)) {
+                    if (tlen < sizeof(nd_ipv4)) {
                         ND_PRINT("invalid len");
                         tlen = 0;
                     } else {
-                        ND_TCHECK_LEN(tptr, sizeof(struct in_addr));
+                        ND_TCHECK_LEN(tptr, sizeof(nd_ipv4));
                         ND_PRINT("%s",ipaddr_string(ndo, tptr));
-                        tlen -= sizeof(struct in_addr);
-                        tptr += sizeof(struct in_addr);
+                        tlen -= sizeof(nd_ipv4);
+                        tptr += sizeof(nd_ipv4);
                     }
                     break;
                 case (AFNUM_INET<<8 | SAFNUM_VPNUNICAST):
                 case (AFNUM_INET<<8 | SAFNUM_VPNMULTICAST):
                 case (AFNUM_INET<<8 | SAFNUM_VPNUNIMULTICAST):
-                    if (tlen < sizeof(struct in_addr)+BGP_VPN_RD_LEN) {
+                    if (tlen < sizeof(nd_ipv4)+BGP_VPN_RD_LEN) {
                         ND_PRINT("invalid len");
                         tlen = 0;
                     } else {
                         ND_TCHECK_LEN(tptr,
-                                      sizeof(struct in_addr) + BGP_VPN_RD_LEN);
+                                      sizeof(nd_ipv4) + BGP_VPN_RD_LEN);
                         ND_PRINT("RD: %s, %s",
                                   bgp_vpn_rd_print(ndo, tptr),
                                   ipaddr_string(ndo, tptr+BGP_VPN_RD_LEN));
-                        tlen -= (sizeof(struct in_addr)+BGP_VPN_RD_LEN);
-                        tptr += (sizeof(struct in_addr)+BGP_VPN_RD_LEN);
+                        tlen -= (sizeof(nd_ipv4)+BGP_VPN_RD_LEN);
+                        tptr += (sizeof(nd_ipv4)+BGP_VPN_RD_LEN);
                     }
                     break;
                 case (AFNUM_INET6<<8 | SAFNUM_UNICAST):
                 case (AFNUM_INET6<<8 | SAFNUM_MULTICAST):
                 case (AFNUM_INET6<<8 | SAFNUM_UNIMULTICAST):
                 case (AFNUM_INET6<<8 | SAFNUM_LABUNICAST):
-                    if (tlen < sizeof(struct in6_addr)) {
+                    if (tlen < sizeof(nd_ipv6)) {
                         ND_PRINT("invalid len");
                         tlen = 0;
                     } else {
-                        ND_TCHECK_LEN(tptr, sizeof(struct in6_addr));
+                        ND_TCHECK_LEN(tptr, sizeof(nd_ipv6));
                         ND_PRINT("%s", ip6addr_string(ndo, tptr));
-                        tlen -= sizeof(struct in6_addr);
-                        tptr += sizeof(struct in6_addr);
+                        tlen -= sizeof(nd_ipv6);
+                        tptr += sizeof(nd_ipv6);
                     }
                     break;
                 case (AFNUM_INET6<<8 | SAFNUM_VPNUNICAST):
                 case (AFNUM_INET6<<8 | SAFNUM_VPNMULTICAST):
                 case (AFNUM_INET6<<8 | SAFNUM_VPNUNIMULTICAST):
-                    if (tlen < sizeof(struct in6_addr)+BGP_VPN_RD_LEN) {
+                    if (tlen < sizeof(nd_ipv6)+BGP_VPN_RD_LEN) {
                         ND_PRINT("invalid len");
                         tlen = 0;
                     } else {
                         ND_TCHECK_LEN(tptr,
-                                      sizeof(struct in6_addr) + BGP_VPN_RD_LEN);
+                                      sizeof(nd_ipv6) + BGP_VPN_RD_LEN);
                         ND_PRINT("RD: %s, %s",
                                   bgp_vpn_rd_print(ndo, tptr),
                                   ip6addr_string(ndo, tptr+BGP_VPN_RD_LEN));
-                        tlen -= (sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
-                        tptr += (sizeof(struct in6_addr)+BGP_VPN_RD_LEN);
+                        tlen -= (sizeof(nd_ipv6)+BGP_VPN_RD_LEN);
+                        tptr += (sizeof(nd_ipv6)+BGP_VPN_RD_LEN);
                     }
                     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):
-                    if (tlen < sizeof(struct in_addr)) {
+                    if (tlen < sizeof(nd_ipv4)) {
                         ND_PRINT("invalid len");
                         tlen = 0;
                     } else {
-                        ND_TCHECK_LEN(tptr, sizeof(struct in_addr));
+                        ND_TCHECK_LEN(tptr, sizeof(nd_ipv4));
                         ND_PRINT("%s", ipaddr_string(ndo, tptr));
-                        tlen -= (sizeof(struct in_addr));
-                        tptr += (sizeof(struct in_addr));
+                        tlen -= (sizeof(nd_ipv4));
+                        tptr += (sizeof(nd_ipv4));
                     }
                     break;
                 case (AFNUM_NSAP<<8 | SAFNUM_UNICAST):
@@ -1892,13 +1972,9 @@ bgp_attr_print(netdissect_options *ndo,
                     ND_PRINT("\n\t      %s", buf);
                 break;
             case (AFNUM_INET<<8 | SAFNUM_RT_ROUTING_INFO):
-                advance = decode_rt_routing_info(ndo, tptr, buf, sizeof(buf));
-                if (advance == -1)
-                    ND_PRINT("\n\t    (illegal prefix length)");
-                else if (advance == -2)
+                advance = decode_rt_routing_info(ndo, tptr);
+                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):
@@ -2205,71 +2281,11 @@ bgp_attr_print(netdissect_options *ndo,
                       extd_comm,
                       bittok2str(bgp_extd_comm_flag_values, "none", extd_comm));
 
-            ND_TCHECK_6(tptr + 2);
+            ND_TCHECK_8(tptr);
             if (tlen < 8)
                 goto trunc;
-            switch(extd_comm) {
-            case BGP_EXT_COM_RT_0:
-            case BGP_EXT_COM_RO_0:
-            case BGP_EXT_COM_L2VPN_RT_0:
-                ND_PRINT(": %u:%u (= %s)",
-                          EXTRACT_BE_U_2(tptr + 2),
-                          EXTRACT_BE_U_4(tptr + 4),
-                          ipaddr_string(ndo, tptr+4));
-                break;
-            case BGP_EXT_COM_RT_1:
-            case BGP_EXT_COM_RO_1:
-            case BGP_EXT_COM_L2VPN_RT_1:
-            case BGP_EXT_COM_VRF_RT_IMP:
-                ND_PRINT(": %s:%u",
-                          ipaddr_string(ndo, tptr+2),
-                          EXTRACT_BE_U_2(tptr + 6));
-                break;
-            case BGP_EXT_COM_RT_2:
-            case BGP_EXT_COM_RO_2:
-                ND_PRINT(": %s:%u",
-                          as_printf(ndo, astostr, sizeof(astostr),
-                          EXTRACT_BE_U_4(tptr + 2)), EXTRACT_BE_U_2(tptr + 6));
-                break;
-            case BGP_EXT_COM_LINKBAND:
-                bw.i = EXTRACT_BE_U_4(tptr + 2);
-                ND_PRINT(": bandwidth: %.3f Mbps",
-                          bw.f*8/1000000);
-                break;
-            case BGP_EXT_COM_VPN_ORIGIN:
-            case BGP_EXT_COM_VPN_ORIGIN2:
-            case BGP_EXT_COM_VPN_ORIGIN3:
-            case BGP_EXT_COM_VPN_ORIGIN4:
-            case BGP_EXT_COM_OSPF_RID:
-            case BGP_EXT_COM_OSPF_RID2:
-                ND_PRINT("%s", ipaddr_string(ndo, tptr+2));
-                break;
-            case BGP_EXT_COM_OSPF_RTYPE:
-            case BGP_EXT_COM_OSPF_RTYPE2:
-                ND_PRINT(": area:%s, router-type:%s, metric-type:%s%s",
-                          ipaddr_string(ndo, tptr+2),
-                          tok2str(bgp_extd_comm_ospf_rtype_values,
-                                  "unknown (0x%02x)",
-                                  EXTRACT_U_1((tptr + 6))),
-                          (EXTRACT_U_1(tptr + 7) &  BGP_OSPF_RTYPE_METRIC_TYPE) ? "E2" : "",
-                          ((EXTRACT_U_1(tptr + 6) == BGP_OSPF_RTYPE_EXT) || (EXTRACT_U_1(tptr + 6) == BGP_OSPF_RTYPE_NSSA)) ? "E1" : "");
-                break;
-            case BGP_EXT_COM_L2INFO:
-                ND_PRINT(": %s Control Flags [0x%02x]:MTU %u",
-                          tok2str(l2vpn_encaps_values,
-                                  "unknown encaps",
-                                  EXTRACT_U_1((tptr + 2))),
-                          EXTRACT_U_1((tptr + 3)),
-                          EXTRACT_BE_U_2(tptr + 4));
-                break;
-            case BGP_EXT_COM_SOURCE_AS:
-                ND_PRINT(": AS %u", EXTRACT_BE_U_2(tptr + 2));
-                break;
-            default:
-                ND_TCHECK_8(tptr);
-                print_unknown_data(ndo, tptr, "\n\t      ", 8);
-                break;
-            }
+            ND_PRINT(": ");
+            bgp_extended_community_print(ndo, tptr);
             tlen -= 8;
             tptr += 8;
         }
@@ -2280,6 +2296,8 @@ bgp_attr_print(netdissect_options *ndo,
         uint8_t tunnel_type, flags;
 
         ND_TCHECK_5(tptr);
+        if (tlen < 5)
+            goto trunc;
         flags = EXTRACT_U_1(tptr);
         tunnel_type = EXTRACT_U_1(tptr + 1);
         tlen = len;
@@ -2361,6 +2379,8 @@ bgp_attr_print(netdissect_options *ndo,
              * Check if we can read the TLV data.
              */
             ND_TCHECK_LEN(tptr + 3, length);
+            if (tlen < length)
+                goto trunc;
 
             switch (type) {
 
@@ -2395,16 +2415,22 @@ bgp_attr_print(netdissect_options *ndo,
             u_int aflags, alenlen, alen;
 
             ND_TCHECK_2(tptr);
-            if (len < 2)
-                goto trunc;
+            if (len < 2) {
+                ND_PRINT(" [path attr too short]");
+                tptr += len;
+                break;
+            }
             aflags = EXTRACT_U_1(tptr);
             atype = EXTRACT_U_1(tptr + 1);
             tptr += 2;
             len -= 2;
             alenlen = bgp_attr_lenlen(aflags, tptr);
             ND_TCHECK_LEN(tptr, alenlen);
-            if (len < alenlen)
-                goto trunc;
+            if (len < alenlen) {
+                ND_PRINT(" [path attr too short]");
+                tptr += len;
+                break;
+            }
             alen = bgp_attr_len(aflags, tptr);
             tptr += alenlen;
             len -= alenlen;
@@ -2423,7 +2449,13 @@ bgp_attr_print(netdissect_options *ndo,
                           aflags & 0x10 ? "E" : "");
                 if (aflags & 0xf)
                     ND_PRINT("+%x", aflags & 0xf);
-                ND_PRINT("]: ");
+                ND_PRINT("]");
+            }
+            ND_PRINT(": ");
+            if (len < alen) {
+                ND_PRINT(" [path attr too short]");
+                tptr += len;
+                break;
             }
             /* FIXME check for recursion */
             if (!bgp_attr_print(ndo, atype, tptr, alen))
@@ -2482,7 +2514,6 @@ bgp_capabilities_print(netdissect_options *ndo,
         ND_TCHECK_LEN(opt + i, BGP_CAP_HEADER_SIZE);
         cap_type=EXTRACT_U_1(opt + i);
         cap_len=EXTRACT_U_1(opt + i + 1);
-        tcap_len=cap_len;
         ND_PRINT("\n\t      %s (%u), length: %u",
                   tok2str(bgp_capcode_values, "Unknown", cap_type),
                   cap_type,
@@ -2490,6 +2521,11 @@ bgp_capabilities_print(netdissect_options *ndo,
         ND_TCHECK_LEN(opt + 2 + i, cap_len);
         switch (cap_type) {
         case BGP_CAPCODE_MP:
+            /* AFI (16 bits), Reserved (8 bits), SAFI (8 bits) */
+            if (cap_len < 4) {
+                ND_PRINT(" (too short, < 4)");
+                return;
+            }
             ND_PRINT("\n\t\tAFI %s (%u), SAFI %s (%u)",
                tok2str(af_values, "Unknown", EXTRACT_BE_U_2(opt + i + 2)),
                EXTRACT_BE_U_2(opt + i + 2),
@@ -2497,6 +2533,12 @@ bgp_capabilities_print(netdissect_options *ndo,
                EXTRACT_U_1(opt + i + 5));
             break;
         case BGP_CAPCODE_RESTART:
+            /* Restart Flags (4 bits), Restart Time in seconds (12 bits) */
+            if (cap_len < 2) {
+                ND_PRINT(" (too short, < 2)");
+                return;
+            }
+            tcap_len=cap_len;
             ND_PRINT("\n\t\tRestart Flags: [%s], Restart Time %us",
                       ((EXTRACT_U_1(opt + i + 2))&0x80) ? "R" : "none",
                       EXTRACT_BE_U_2(opt + i + 2)&0xfff);
@@ -2519,25 +2561,27 @@ bgp_capabilities_print(netdissect_options *ndo,
         case BGP_CAPCODE_RR_CISCO:
             break;
         case BGP_CAPCODE_AS_NEW:
-
             /*
              * Extract the 4 byte AS number encoded.
              */
-            if (cap_len == 4) {
+            if (cap_len < 4) {
+                ND_PRINT(" (too short, < 4)");
+                return;
+            }
             ND_PRINT("\n\t\t 4 Byte AS %s",
                       as_printf(ndo, astostr, sizeof(astostr),
                       EXTRACT_BE_U_4(opt + i + 2)));
-            }
             break;
         case BGP_CAPCODE_ADD_PATH:
-            cap_offset=2;
-            if (tcap_len == 0) {
+            if (cap_len == 0) {
                 ND_PRINT(" (bogus)"); /* length */
                 break;
             }
+            tcap_len=cap_len;
+            cap_offset=2;
             while (tcap_len != 0) {
                 if (tcap_len < 4) {
-                    ND_PRINT("\n\t\t(invalid)");
+                    nd_print_invalid(ndo);
                     break;
                 }
                 ND_PRINT("\n\t\tAFI %s (%u), SAFI %s (%u), Send/Receive: %s",
@@ -2567,7 +2611,7 @@ bgp_capabilities_print(netdissect_options *ndo,
     return;
 
 trunc:
-    ND_PRINT("[|BGP]");
+    nd_print_trunc(ndo);
 }
 
 static void
@@ -2592,7 +2636,7 @@ bgp_open_print(netdissect_options *ndo,
         as_printf(ndo, astostr, sizeof(astostr), EXTRACT_BE_U_2(bgp_open_header->bgpo_myas)));
     ND_PRINT("Holdtime %us, ",
         EXTRACT_BE_U_2(bgp_open_header->bgpo_holdtime));
-    ND_PRINT("ID %s", ipaddr_string(ndo, &bgp_open_header->bgpo_id));
+    ND_PRINT("ID %s", ipaddr_string(ndo, bgp_open_header->bgpo_id));
     optslen = EXTRACT_U_1(bgp_open_header->bgpo_optlen);
     ND_PRINT("\n\t  Optional parameters, length: %u", optslen);
 
@@ -2638,7 +2682,7 @@ bgp_open_print(netdissect_options *ndo,
     }
     return;
 trunc:
-    ND_PRINT("[|BGP]");
+    nd_print_trunc(ndo);
 }
 
 static void
@@ -2680,11 +2724,11 @@ bgp_update_print(netdissect_options *ndo,
         add_path = check_add_path(ndo, p, withdrawn_routes_len, 32);
         while (withdrawn_routes_len != 0) {
             if (add_path) {
-               if (withdrawn_routes_len < 4) {
-                   p += withdrawn_routes_len;
-                   length -= withdrawn_routes_len;
-                   break;
-               }
+                if (withdrawn_routes_len < 4) {
+                    p += withdrawn_routes_len;
+                    length -= withdrawn_routes_len;
+                    break;
+                }
                 path_id = EXTRACT_BE_U_4(p);
                 p += 4;
                 length -= 4;
@@ -2709,6 +2753,9 @@ bgp_update_print(netdissect_options *ndo,
             }
         }
     } else {
+        ND_TCHECK_LEN(p, withdrawn_routes_len);
+        if (length < withdrawn_routes_len)
+            goto trunc;
         p += withdrawn_routes_len;
         length -= withdrawn_routes_len;
     }
@@ -2732,10 +2779,14 @@ bgp_update_print(netdissect_options *ndo,
             u_int aflags, atype, alenlen, alen;
 
             ND_TCHECK_2(p);
-            if (len < 2)
-                goto trunc;
             if (length < 2)
                 goto trunc;
+            if (len < 2) {
+                ND_PRINT("\n\t  [path attrs too short]");
+                p += len;
+                length -= len;
+                break;
+            }
             aflags = EXTRACT_U_1(p);
             atype = EXTRACT_U_1(p + 1);
             p += 2;
@@ -2743,10 +2794,14 @@ bgp_update_print(netdissect_options *ndo,
             length -= 2;
             alenlen = bgp_attr_lenlen(aflags, p);
             ND_TCHECK_LEN(p, alenlen);
-            if (len < alenlen)
-                goto trunc;
             if (length < alenlen)
                 goto trunc;
+            if (len < alenlen) {
+                ND_PRINT("\n\t  [path attrs too short]");
+                p += len;
+                length -= len;
+                break;
+            }
             alen = bgp_attr_len(aflags, p);
             p += alenlen;
             len -= alenlen;
@@ -2767,8 +2822,12 @@ bgp_update_print(netdissect_options *ndo,
                     ND_PRINT("+%x", aflags & 0xf);
                 ND_PRINT("]: ");
             }
-            if (len < alen)
-                goto trunc;
+            if (len < alen) {
+                ND_PRINT(" [path attrs too short]");
+                p += len;
+                length -= len;
+                break;
+            }
             if (length < alen)
                 goto trunc;
             if (!bgp_attr_print(ndo, atype, p, alen))
@@ -2784,6 +2843,9 @@ bgp_update_print(netdissect_options *ndo,
         ND_PRINT("\n\t  Updated routes:");
         while (length != 0) {
             if (add_path) {
+                ND_TCHECK_4(p);
+                if (length < 4)
+                    goto trunc;
                 path_id = EXTRACT_BE_U_4(p);
                 p += 4;
                 length -= 4;
@@ -2808,7 +2870,7 @@ bgp_update_print(netdissect_options *ndo,
     }
     return;
 trunc:
-    ND_PRINT("[|BGP]");
+    nd_print_trunc(ndo);
 }
 
 static void
@@ -2909,7 +2971,7 @@ bgp_notification_print(netdissect_options *ndo,
             else {
                 ND_TCHECK_LEN(tptr + 1, shutdown_comm_length);
                 ND_PRINT(", Shutdown Communication (length: %u): \"", shutdown_comm_length);
-                (void)fn_printn(ndo, tptr+1, shutdown_comm_length, NULL);
+                (void)nd_printn(ndo, tptr+1, shutdown_comm_length, NULL);
                 ND_PRINT("\"");
                 remainder_offset += shutdown_comm_length + 1;
             }
@@ -2926,7 +2988,7 @@ bgp_notification_print(netdissect_options *ndo,
 
     return;
 trunc:
-    ND_PRINT("[|BGP]");
+    nd_print_trunc(ndo);
 }
 
 static void
@@ -2958,7 +3020,7 @@ bgp_route_refresh_print(netdissect_options *ndo,
 
     return;
 trunc:
-    ND_PRINT("[|BGP]");
+    nd_print_trunc(ndo);
 }
 
 static int
@@ -3001,16 +3063,16 @@ bgp_pdu_print(netdissect_options *ndo,
     }
     return 1;
 trunc:
-    ND_PRINT("[|BGP]");
+    nd_print_trunc(ndo);
     return 0;
 }
 
 void
 bgp_print(netdissect_options *ndo,
-          const u_char *dat, u_int length)
+          const u_char *dat, u_int length _U_)
 {
     const u_char *p;
-    const u_char *ep;
+    const u_char *ep = ndo->ndo_snapend;
     const u_char *start;
     const u_char marker[] = {
         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
@@ -3019,10 +3081,7 @@ bgp_print(netdissect_options *ndo,
     const struct bgp *bgp_header;
     uint16_t hlen;
 
-    ep = dat + length;
-    if (ndo->ndo_snapend < dat + length)
-        ep = ndo->ndo_snapend;
-
+    ndo->ndo_protocol = "bgp";
     ND_PRINT(": BGP");
 
     if (ndo->ndo_vflag < 1) /* lets be less chatty */
@@ -3050,12 +3109,12 @@ bgp_print(netdissect_options *ndo,
         bgp_header = (const struct bgp *)p;
 
         if (start != p)
-            ND_PRINT(" [|BGP]");
+            nd_print_trunc(ndo);
 
         hlen = EXTRACT_BE_U_2(bgp_header->bgp_len);
         if (hlen < BGP_SIZE) {
-            ND_PRINT("\n[|BGP Bogus header length %u < %u]", hlen,
-                      BGP_SIZE);
+            ND_PRINT("\nmessage length %u < %u", hlen, BGP_SIZE);
+            nd_print_invalid(ndo);
             break;
         }
 
@@ -3076,12 +3135,5 @@ bgp_print(netdissect_options *ndo,
     return;
 
 trunc:
-    ND_PRINT(" [|BGP]");
+    nd_print_trunc(ndo);
 }
-
-/*
- * Local Variables:
- * c-style: whitesmith
- * c-basic-offset: 4
- * End:
- */