+static void
+gre_print_1(netdissect_options *ndo, const u_char *bp, u_int length)
+{
+ u_int len = length;
+ uint16_t flags, prot;
+
+ flags = EXTRACT_16BITS(bp);
+ len -= 2;
+ bp += 2;
+
+ if (ndo->ndo_vflag)
+ ND_PRINT((ndo, ", Flags [%s]",
+ bittok2str(gre_flag_values,"none",flags)));
+
+ if (len < 2)
+ goto trunc;
+ prot = EXTRACT_16BITS(bp);
+ len -= 2;
+ bp += 2;
+
+
+ if (flags & GRE_KP) {
+ uint32_t k;
+
+ if (len < 4)
+ goto trunc;
+ k = EXTRACT_32BITS(bp);
+ ND_PRINT((ndo, ", call %d", k & 0xffff));
+ len -= 4;
+ bp += 4;
+ }
+
+ if (flags & GRE_SP) {
+ if (len < 4)
+ goto trunc;
+ ND_PRINT((ndo, ", seq %u", EXTRACT_32BITS(bp)));
+ bp += 4;
+ len -= 4;
+ }
+
+ if (flags & GRE_AP) {
+ if (len < 4)
+ goto trunc;
+ ND_PRINT((ndo, ", ack %u", EXTRACT_32BITS(bp)));
+ bp += 4;
+ len -= 4;
+ }
+
+ if ((flags & GRE_SP) == 0)
+ ND_PRINT((ndo, ", no-payload"));
+
+ if (ndo->ndo_eflag)
+ ND_PRINT((ndo, ", proto %s (0x%04x)",
+ tok2str(ethertype_values,"unknown",prot),
+ prot));
+
+ ND_PRINT((ndo, ", length %u",length));
+
+ if ((flags & GRE_SP) == 0)
+ return;
+
+ if (ndo->ndo_vflag < 1)
+ ND_PRINT((ndo, ": ")); /* put in a colon as protocol demarc */
+ else
+ ND_PRINT((ndo, "\n\t")); /* if verbose go multiline */
+
+ switch (prot) {
+ case ETHERTYPE_PPP:
+ ppp_print(ndo, bp, len);
+ break;
+ default:
+ ND_PRINT((ndo, "gre-proto-0x%x", prot));
+ break;
+ }
+ return;
+
+trunc:
+ ND_PRINT((ndo, "%s", tstr));
+}
+
+static void
+gre_sre_print(netdissect_options *ndo, uint16_t af, uint8_t sreoff,
+ uint8_t srelen, const u_char *bp, u_int len)
+{
+ switch (af) {
+ case GRESRE_IP:
+ ND_PRINT((ndo, ", (rtaf=ip"));
+ gre_sre_ip_print(ndo, sreoff, srelen, bp, len);
+ ND_PRINT((ndo, ")"));
+ break;
+ case GRESRE_ASN:
+ ND_PRINT((ndo, ", (rtaf=asn"));
+ gre_sre_asn_print(ndo, sreoff, srelen, bp, len);
+ ND_PRINT((ndo, ")"));
+ break;
+ default:
+ ND_PRINT((ndo, ", (rtaf=0x%x)", af));
+ }
+}
+
+static void
+gre_sre_ip_print(netdissect_options *ndo, uint8_t sreoff, uint8_t srelen,
+ const u_char *bp, u_int len)
+{
+ const u_char *up = bp;
+ char buf[INET_ADDRSTRLEN];
+
+ if (sreoff & 3) {
+ ND_PRINT((ndo, ", badoffset=%u", sreoff));
+ return;
+ }
+ if (srelen & 3) {
+ ND_PRINT((ndo, ", badlength=%u", srelen));
+ return;
+ }
+ if (sreoff >= srelen) {
+ ND_PRINT((ndo, ", badoff/len=%u/%u", sreoff, srelen));
+ return;
+ }
+
+ for (;;) {
+ if (len < 4 || srelen == 0)
+ return;
+
+ addrtostr(bp, buf, sizeof(buf));
+ ND_PRINT((ndo, " %s%s",
+ ((bp - up) == sreoff) ? "*" : "", buf));
+
+ bp += 4;
+ len -= 4;
+ srelen -= 4;
+ }
+}
+
+static void
+gre_sre_asn_print(netdissect_options *ndo, uint8_t sreoff, uint8_t srelen,
+ const u_char *bp, u_int len)
+{
+ const u_char *up = bp;
+
+ if (sreoff & 1) {
+ ND_PRINT((ndo, ", badoffset=%u", sreoff));
+ return;
+ }
+ if (srelen & 1) {
+ ND_PRINT((ndo, ", badlength=%u", srelen));
+ return;
+ }
+ if (sreoff >= srelen) {
+ ND_PRINT((ndo, ", badoff/len=%u/%u", sreoff, srelen));
+ return;
+ }
+
+ for (;;) {
+ if (len < 2 || srelen == 0)
+ return;
+
+ ND_PRINT((ndo, " %s%x",
+ ((bp - up) == sreoff) ? "*" : "",
+ EXTRACT_16BITS(bp)));
+
+ bp += 2;
+ len -= 2;
+ srelen -= 2;
+ }