+gre_print(netdissect_options *ndo, const u_char *bp, u_int length)
+{
+ u_int len = length, vers;
+
+ ndo->ndo_protocol = "gre";
+ ND_TCHECK_2(bp);
+ if (len < 2)
+ goto trunc;
+ vers = GET_BE_U_2(bp) & GRE_VERS_MASK;
+ ND_PRINT("GREv%u",vers);
+
+ switch(vers) {
+ case 0:
+ gre_print_0(ndo, bp, len);
+ break;
+ case 1:
+ gre_print_1(ndo, bp, len);
+ break;
+ default:
+ ND_PRINT(" ERROR: unknown-version");
+ break;
+ }
+ return;
+
+trunc:
+ nd_print_trunc(ndo);
+}
+
+static void
+gre_print_0(netdissect_options *ndo, const u_char *bp, u_int length)
+{
+ u_int len = length;
+ uint16_t flags, prot;
+
+ /* 16 bits ND_TCHECKed in gre_print() */
+ flags = GET_BE_U_2(bp);
+ if (ndo->ndo_vflag)
+ ND_PRINT(", Flags [%s]",
+ bittok2str(gre_flag_values,"none",flags));
+
+ len -= 2;
+ bp += 2;
+
+ ND_TCHECK_2(bp);
+ if (len < 2)
+ goto trunc;
+ prot = GET_BE_U_2(bp);
+ len -= 2;
+ bp += 2;
+
+ if ((flags & GRE_CP) | (flags & GRE_RP)) {
+ ND_TCHECK_2(bp);
+ if (len < 2)
+ goto trunc;
+ if (ndo->ndo_vflag)
+ ND_PRINT(", sum 0x%x", GET_BE_U_2(bp));
+ bp += 2;
+ len -= 2;
+
+ ND_TCHECK_2(bp);
+ if (len < 2)
+ goto trunc;
+ ND_PRINT(", off 0x%x", GET_BE_U_2(bp));
+ bp += 2;
+ len -= 2;
+ }
+
+ if (flags & GRE_KP) {
+ ND_TCHECK_4(bp);
+ if (len < 4)
+ goto trunc;
+ ND_PRINT(", key=0x%x", GET_BE_U_4(bp));
+ bp += 4;
+ len -= 4;
+ }
+
+ if (flags & GRE_SP) {
+ ND_TCHECK_4(bp);
+ if (len < 4)
+ goto trunc;
+ ND_PRINT(", seq %u", GET_BE_U_4(bp));
+ bp += 4;
+ len -= 4;
+ }
+
+ if (flags & GRE_RP) {
+ for (;;) {
+ uint16_t af;
+ uint8_t sreoff;
+ uint8_t srelen;
+
+ ND_TCHECK_4(bp);
+ if (len < 4)
+ goto trunc;
+ af = GET_BE_U_2(bp);
+ sreoff = GET_U_1(bp + 2);
+ srelen = GET_U_1(bp + 3);
+ bp += 4;
+ len -= 4;
+
+ if (af == 0 && srelen == 0)
+ break;
+
+ if (!gre_sre_print(ndo, af, sreoff, srelen, bp, len))
+ goto trunc;
+
+ if (len < srelen)
+ goto trunc;
+ bp += srelen;
+ len -= srelen;
+ }
+ }
+
+ if (ndo->ndo_eflag)
+ ND_PRINT(", proto %s (0x%04x)",
+ tok2str(ethertype_values,"unknown",prot), prot);
+
+ ND_PRINT(", length %u",length);
+
+ if (ndo->ndo_vflag < 1)
+ ND_PRINT(": "); /* put in a colon as protocol demarc */
+ else
+ ND_PRINT("\n\t"); /* if verbose go multiline */
+
+ switch (prot) {
+ case ETHERTYPE_IP:
+ ip_print(ndo, bp, len);
+ break;
+ case ETHERTYPE_IPV6:
+ ip6_print(ndo, bp, len);
+ break;
+ case ETHERTYPE_MPLS:
+ mpls_print(ndo, bp, len);
+ break;
+ case ETHERTYPE_IPX:
+ ipx_print(ndo, bp, len);
+ break;
+ case ETHERTYPE_ATALK:
+ atalk_print(ndo, bp, len);
+ break;
+ case ETHERTYPE_GRE_ISO:
+ isoclns_print(ndo, bp, len);
+ break;
+ case ETHERTYPE_TEB:
+ ether_print(ndo, bp, len, ND_BYTES_AVAILABLE_AFTER(bp), NULL, NULL);
+ break;
+ default:
+ ND_PRINT("gre-proto-0x%x", prot);
+ }
+ return;
+
+trunc:
+ nd_print_trunc(ndo);
+}
+
+static void
+gre_print_1(netdissect_options *ndo, const u_char *bp, u_int length)