]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-juniper.c
From Manu Pathak: add support for LMP Service Discovery extensions
[tcpdump] / print-juniper.c
index df5ca92265017b3b77a676078b5444a9feed10c6..14baa6231e58c706416b6f6371d68605a7772f77 100644 (file)
@@ -15,7 +15,7 @@
 
 #ifndef lint
 static const char rcsid[] _U_ =
-    "@(#) $Header: /tcpdump/master/tcpdump/print-juniper.c,v 1.2 2004-10-20 16:14:16 hannes Exp $ (LBL)";
+    "@(#) $Header: /tcpdump/master/tcpdump/print-juniper.c,v 1.16 2005-05-17 09:37:30 hannes Exp $ (LBL)";
 #endif
 
 #ifdef HAVE_CONFIG_H
@@ -30,18 +30,224 @@ static const char rcsid[] _U_ =
 #include "interface.h"
 #include "extract.h"
 #include "ppp.h"
-
-#define BPF_OUT           0       /* Outgoing packet */
-#define BPF_IN            1       /* Incoming packet */
-#define BPF_PKT_IN        0x1     /* Incoming packet */
-#define BPF_NO_L2         0x2     /* L2 header stripped */
+#include "llc.h"
+#include "nlpid.h"
+#include "ethertype.h"
+
+#define JUNIPER_BPF_OUT           0       /* Outgoing packet */
+#define JUNIPER_BPF_IN            1       /* Incoming packet */
+#define JUNIPER_BPF_PKT_IN        0x1     /* Incoming packet */
+#define JUNIPER_BPF_NO_L2         0x2     /* L2 header stripped */
+#define JUNIPER_MGC_NUMBER        0x4d4743 /* = "MGC" */
+
+#define JUNIPER_LSQ_L3_PROTO_SHIFT     4
+#define JUNIPER_LSQ_L3_PROTO_MASK     (0x17 << JUNIPER_LSQ_L3_PROTO_SHIFT)
+#define JUNIPER_LSQ_L3_PROTO_IPV4     (0 << JUNIPER_LSQ_L3_PROTO_SHIFT)   /* must be 0! */
+#define JUNIPER_LSQ_L3_PROTO_IPV6     (1 << JUNIPER_LSQ_L3_PROTO_SHIFT)
+#define JUNIPER_LSQ_L3_PROTO_MPLS     (2 << JUNIPER_LSQ_L3_PROTO_SHIFT)
+#define JUNIPER_LSQ_L3_PROTO_ISO      (3 << JUNIPER_LSQ_L3_PROTO_SHIFT)
+
+static struct tok juniper_direction_values[] = {
+    { JUNIPER_BPF_IN,  "In"},
+    { JUNIPER_BPF_OUT, "Out"},
+    { 0, NULL}
+};
+
+struct juniper_cookie_table_t {
+    u_int32_t pictype;         /* pic type */
+    u_int8_t  cookie_len;       /* cookie len */
+    const char *s;             /* pic name */
+};
+
+static struct juniper_cookie_table_t juniper_cookie_table[] = {
+    { DLT_JUNIPER_ATM1,  4, "ATM1"},
+    { DLT_JUNIPER_ATM2,  8, "ATM2"},
+    { DLT_JUNIPER_MLPPP, 2, "MLPPP"},
+    { DLT_JUNIPER_MLFR,  2, "MLFR"},
+    { DLT_JUNIPER_MFR,   4, "MFR"},
+    { DLT_JUNIPER_PPPOE, 0, "PPPoE"},
+    { DLT_JUNIPER_PPPOE_ATM, 0, "PPPoE ATM"},
+};
+
+struct juniper_l2info_t {
+    u_int32_t length;
+    u_int32_t caplen;
+    u_int32_t pictype;
+    u_int8_t direction;
+    u_int8_t header_len;
+    u_int8_t cookie_len;
+    u_int8_t cookie_type;
+    u_int8_t cookie[8];
+    u_int8_t bundle;
+    u_int16_t proto;
+};
+
+#define LS_COOKIE_ID            0x54
+#define AS_COOKIE_ID            0x47
+#define LS_MLFR_COOKIE_LEN     4
+#define ML_MLFR_COOKIE_LEN     2
+#define LS_MFR_COOKIE_LEN      6
+#define ATM1_COOKIE_LEN         4
+#define ATM2_COOKIE_LEN         8
 
 #define ATM2_PKT_TYPE_MASK  0x70
 #define ATM2_GAP_COUNT_MASK 0x3F
 
 int ip_heuristic_guess(register const u_char *, u_int);
-int ppp_heuristic_guess(register const u_char *, u_int);
-static int juniper_parse_header (const u_char *, u_int8_t *, u_int);
+int juniper_ppp_heuristic_guess(register const u_char *, u_int);
+static int juniper_parse_header (const u_char *, const struct pcap_pkthdr *, struct juniper_l2info_t *);
+
+u_int
+juniper_pppoe_print(const struct pcap_pkthdr *h, register const u_char *p)
+{
+        struct juniper_l2info_t l2info;
+
+        l2info.pictype = DLT_JUNIPER_PPPOE;
+        if(juniper_parse_header(p, h, &l2info) == 0)
+            return l2info.header_len;
+
+        p+=l2info.header_len;
+        /* this DLT contains nothing but raw ethernet frames */
+        ether_print(p, l2info.length, l2info.caplen);
+        return l2info.header_len;
+}
+
+u_int
+juniper_pppoe_atm_print(const struct pcap_pkthdr *h, register const u_char *p)
+{
+        struct juniper_l2info_t l2info;
+       u_int16_t extracted_ethertype;
+
+        l2info.pictype = DLT_JUNIPER_PPPOE_ATM;
+        if(juniper_parse_header(p, h, &l2info) == 0)
+            return l2info.header_len;
+
+        p+=l2info.header_len;
+
+        extracted_ethertype = EXTRACT_16BITS(p);
+        /* this DLT contains nothing but raw PPPoE frames,
+         * prepended with a type field*/
+        if (ether_encap_print(extracted_ethertype,
+                              p+ETHERTYPE_LEN,
+                              l2info.length-ETHERTYPE_LEN,
+                              l2info.caplen-ETHERTYPE_LEN,
+                              &extracted_ethertype) == 0)
+            /* ether_type not known, probably it wasn't one */
+            printf("unknown ethertype 0x%04x", extracted_ethertype);
+        
+        return l2info.header_len;
+}
+
+u_int
+juniper_mlppp_print(const struct pcap_pkthdr *h, register const u_char *p)
+{
+        struct juniper_l2info_t l2info;
+
+        l2info.pictype = DLT_JUNIPER_MLPPP;
+        if(juniper_parse_header(p, h, &l2info) == 0)
+            return l2info.header_len;
+
+        /* suppress Bundle-ID if frame was captured on a child-link
+         * best indicator if the cookie looks like a proto */
+        if (eflag &&
+            EXTRACT_16BITS(&l2info.cookie) != PPP_OSI &&
+            EXTRACT_16BITS(&l2info.cookie) !=  (PPP_ADDRESS << 8 | PPP_CONTROL))
+            printf("Bundle-ID %u: ",l2info.bundle);
+
+        p+=l2info.header_len;
+
+        /* first try the LSQ protos */
+        switch(l2info.proto) {
+        case JUNIPER_LSQ_L3_PROTO_IPV4:
+            ip_print(gndo, p, l2info.length);
+            return l2info.header_len;
+        case JUNIPER_LSQ_L3_PROTO_IPV6:
+            ip6_print(p,l2info.length);
+            return l2info.header_len;
+        case JUNIPER_LSQ_L3_PROTO_MPLS:
+            mpls_print(p,l2info.length);
+            return l2info.header_len;
+        case JUNIPER_LSQ_L3_PROTO_ISO:
+            isoclns_print(p,l2info.length,l2info.caplen);
+            return l2info.header_len;
+        default:
+            break;
+        }
+
+        /* zero length cookie ? */
+        switch (EXTRACT_16BITS(&l2info.cookie)) {
+        case PPP_OSI:
+            ppp_print(p-2,l2info.length+2);
+            break;
+        case (PPP_ADDRESS << 8 | PPP_CONTROL): /* fall through */
+        default:
+            ppp_print(p,l2info.length);
+            break;
+        }
+
+        return l2info.header_len;
+}
+
+
+u_int
+juniper_mfr_print(const struct pcap_pkthdr *h, register const u_char *p)
+{
+        struct juniper_l2info_t l2info;
+
+        l2info.pictype = DLT_JUNIPER_MFR;
+        if(juniper_parse_header(p, h, &l2info) == 0)
+            return l2info.header_len;
+        
+        p+=l2info.header_len;
+        /* suppress Bundle-ID if frame was captured on a child-link */
+        if (eflag && EXTRACT_32BITS(l2info.cookie) != 1) printf("Bundle-ID %u, ",l2info.bundle);
+        switch (l2info.proto) {
+        case (LLCSAP_ISONS<<8 | LLCSAP_ISONS):
+            isoclns_print(p+1, l2info.length-1, l2info.caplen-1);
+            break;
+        case (LLC_UI<<8 | NLPID_Q933):
+        case (LLC_UI<<8 | NLPID_IP):
+        case (LLC_UI<<8 | NLPID_IP6):
+            /* pass IP{4,6} to the OSI layer for proper link-layer printing */
+            isoclns_print(p-1, l2info.length+1, l2info.caplen+1); 
+            break;
+        default:
+            printf("unknown protocol 0x%04x, length %u",l2info.proto, l2info.length);
+        }
+
+        return l2info.header_len;
+}
+
+u_int
+juniper_mlfr_print(const struct pcap_pkthdr *h, register const u_char *p)
+{
+        struct juniper_l2info_t l2info;
+
+        l2info.pictype = DLT_JUNIPER_MLFR;
+        if(juniper_parse_header(p, h, &l2info) == 0)
+            return l2info.header_len;
+
+        p+=l2info.header_len;
+
+        /* suppress Bundle-ID if frame was captured on a child-link */
+        if (eflag && EXTRACT_32BITS(l2info.cookie) != 1) printf("Bundle-ID %u, ",l2info.bundle);
+        switch (l2info.proto) {
+        case (LLC_UI):
+        case (LLC_UI<<8):
+            isoclns_print(p, l2info.length, l2info.caplen);
+            break;
+        case (LLC_UI<<8 | NLPID_Q933):
+        case (LLC_UI<<8 | NLPID_IP):
+        case (LLC_UI<<8 | NLPID_IP6):
+            /* pass IP{4,6} to the OSI layer for proper link-layer printing */
+            isoclns_print(p-1, l2info.length+1, l2info.caplen+1);
+            break;
+        default:
+            printf("unknown protocol 0x%04x, length %u",l2info.proto, l2info.length);
+        }
+
+        return l2info.header_len;
+}
 
 /*
  *     ATM1 PIC cookie format
@@ -54,54 +260,39 @@ static int juniper_parse_header (const u_char *, u_int8_t *, u_int);
 u_int
 juniper_atm1_print(const struct pcap_pkthdr *h, register const u_char *p)
 {
-       register u_int length = h->len;
-       register u_int caplen = h->caplen;
         u_int16_t extracted_ethertype;
-        u_int8_t direction;
-        u_int32_t cookie1;
-
-        if(juniper_parse_header(p, &direction,length) == 0)
-            return 0;
 
-        p+=4;
-        length-=4;
-        caplen-=4;
+        struct juniper_l2info_t l2info;
 
-        cookie1=EXTRACT_32BITS(p);
+        l2info.pictype = DLT_JUNIPER_ATM1;
+        if(juniper_parse_header(p, h, &l2info) == 0)
+            return l2info.header_len;
 
-        if (eflag) {
-            /* FIXME decode channel-id, vc-index, fmt-id
-               for once lets just hexdump the cookie */
+        p+=l2info.header_len;
 
-            printf("ATM1 cookie 0x%08x, ", EXTRACT_32BITS(p));
-        }
-
-        p+=4;
-        length-=4;
-        caplen-=4;
-
-        if (cookie1 & 0x80000000) { /* OAM cell ? */
-            oam_print(p,length);
+        if (l2info.cookie[0] == 0x80) { /* OAM cell ? */
+            oam_print(p,l2info.length);
+            return l2info.header_len;
         }
 
         if (EXTRACT_24BITS(p) == 0xfefe03 || /* NLPID encaps ? */
             EXTRACT_24BITS(p) == 0xaaaa03) { /* SNAP encaps ? */
 
-            if (llc_print(p, length, caplen, NULL, NULL,
+            if (llc_print(p, l2info.length, l2info.caplen, NULL, NULL,
                           &extracted_ethertype) != 0)
-                return 8;
+                return l2info.header_len;
         }
 
         if (p[0] == 0x03) { /* Cisco style NLPID encaps ? */
-            isoclns_print(p + 1, length - 1, caplen - 1);
+            isoclns_print(p + 1, l2info.length - 1, l2info.caplen - 1);
             /* FIXME check if frame was recognized */
-            return 8;
+            return l2info.header_len;
         }
 
-        if(ip_heuristic_guess(p, length) != 0) /* last try - vcmux encaps ? */
-            return 0;
+        if(ip_heuristic_guess(p, l2info.length) != 0) /* last try - vcmux encaps ? */
+            return l2info.header_len;
 
-       return (8);
+       return l2info.header_len;
 }
 
 /*
@@ -115,72 +306,55 @@ juniper_atm1_print(const struct pcap_pkthdr *h, register const u_char *p)
 u_int
 juniper_atm2_print(const struct pcap_pkthdr *h, register const u_char *p)
 {
-       register u_int length = h->len;
-       register u_int caplen = h->caplen;
         u_int16_t extracted_ethertype;
-        u_int8_t direction;
-        u_int32_t cookie1,cookie2;
 
-        if(juniper_parse_header(p, &direction,length) == 0)
-            return 0;
+        struct juniper_l2info_t l2info;
 
-        p+=4;
-        length-=4;
-        caplen-=4;
+        l2info.pictype = DLT_JUNIPER_ATM2;
+        if(juniper_parse_header(p, h, &l2info) == 0)
+            return l2info.header_len;
 
-        cookie1=EXTRACT_32BITS(p);
-        cookie2=EXTRACT_32BITS(p+4);
+        p+=l2info.header_len;
 
-        if (eflag) {
-            /* FIXME decode channel, fmt-id, ccrq, aal, gap cnt
-               for once lets just hexdump the cookie */
-
-            printf("ATM2 cookie 0x%08x%08x, ",
-                   EXTRACT_32BITS(p),
-                   EXTRACT_32BITS(p+4));
-        }
-
-        p+=8;
-        length-=8;
-        caplen-=8;
-
-        if (cookie2 & ATM2_PKT_TYPE_MASK) { /* OAM cell ? */
-            oam_print(p,length);
-            return 12;
+        if (l2info.cookie[7] & ATM2_PKT_TYPE_MASK) { /* OAM cell ? */
+            oam_print(p,l2info.length);
+            return l2info.header_len;
         }
 
         if (EXTRACT_24BITS(p) == 0xfefe03 || /* NLPID encaps ? */
             EXTRACT_24BITS(p) == 0xaaaa03) { /* SNAP encaps ? */
 
-            if (llc_print(p, length, caplen, NULL, NULL,
+            if (llc_print(p, l2info.length, l2info.caplen, NULL, NULL,
                           &extracted_ethertype) != 0)
-                return 12;
+                return l2info.header_len;
         }
 
-        if (direction != BPF_PKT_IN && /* ether-over-1483 encaps ? */
-            (cookie1 & ATM2_GAP_COUNT_MASK)) {
-            ether_print(p, length, caplen);
-            return 12;
+        if (l2info.direction != JUNIPER_BPF_PKT_IN && /* ether-over-1483 encaps ? */
+            (EXTRACT_32BITS(l2info.cookie) & ATM2_GAP_COUNT_MASK)) {
+            ether_print(p, l2info.length, l2info.caplen);
+            return l2info.header_len;
         }
 
         if (p[0] == 0x03) { /* Cisco style NLPID encaps ? */
-            isoclns_print(p + 1, length - 1, caplen - 1);
+            isoclns_print(p + 1, l2info.length - 1, l2info.caplen - 1);
             /* FIXME check if frame was recognized */
-            return 12;
+            return l2info.header_len;
         }
 
-        if(ppp_heuristic_guess(p, length) != 0) /* PPPoA vcmux encaps ? */
-            return 12;
+        if(juniper_ppp_heuristic_guess(p, l2info.length) != 0) /* PPPoA vcmux encaps ? */
+            return l2info.header_len;
 
-        if(ip_heuristic_guess(p, length) != 0) /* last try - vcmux encaps ? */
-            return 12;
+        if(ip_heuristic_guess(p, l2info.length) != 0) /* last try - vcmux encaps ? */
+            return l2info.header_len;
 
-       return (12);
+       return l2info.header_len;
 }
 
 
+/* try to guess, based on all PPP protos that are supported in
+ * a juniper router if the payload data is encapsulated using PPP */
 int
-ppp_heuristic_guess(register const u_char *p, u_int length) {
+juniper_ppp_heuristic_guess(register const u_char *p, u_int length) {
 
     switch(EXTRACT_16BITS(p)) {
     case PPP_IP :
@@ -223,8 +397,8 @@ ip_heuristic_guess(register const u_char *p, u_int length) {
     case 0x4d:
     case 0x4e:
     case 0x4f:
-        ip_print(p, length);
-        break;
+           ip_print(gndo, p, length);
+           break;
 #ifdef INET6
     case 0x60:
     case 0x61:
@@ -253,23 +427,29 @@ ip_heuristic_guess(register const u_char *p, u_int length) {
 }
 
 static int
-juniper_parse_header (const u_char *p, u_int8_t *direction, u_int length) {
+juniper_parse_header (const u_char *p, const struct pcap_pkthdr *h, struct juniper_l2info_t *l2info) {
 
-    *direction = p[3]&BPF_PKT_IN;
-    
-    if (EXTRACT_24BITS(p) != 0x4d4743) /* magic number found ? */
-        return -1;
+    struct juniper_cookie_table_t *lp = juniper_cookie_table;
+    u_int idx;
+
+    l2info->header_len = 0;
+    l2info->cookie_len = 0;
+    l2info->proto = 0;
+
+
+    l2info->length = h->len;
+    l2info->caplen = h->caplen;
+    l2info->direction = p[3]&JUNIPER_BPF_PKT_IN;
     
-    if (*direction == BPF_PKT_IN) {
-        if (eflag)
-            printf("%3s ", "In");
-    }
-    else {
-        if (eflag)
-            printf("%3s ", "Out");
-    }
+    if (EXTRACT_24BITS(p) != JUNIPER_MGC_NUMBER) /* magic number found ? */
+        return 0;
+    else
+        l2info->header_len = 4;
 
-    if ((p[3] & BPF_NO_L2 ) == BPF_NO_L2 ) {            
+    if (eflag) /* print direction */
+        printf("%3s ",tok2str(juniper_direction_values,"---",l2info->direction));
+
+    if ((p[3] & JUNIPER_BPF_NO_L2 ) == JUNIPER_BPF_NO_L2 ) {            
         if (eflag)
             printf("no-L2-hdr, ");
 
@@ -277,11 +457,123 @@ juniper_parse_header (const u_char *p, u_int8_t *direction, u_int length) {
          * perform the v4/v6 heuristics
          * to figure out what it is
          */
-        if(ip_heuristic_guess(p+8,length-8) == 0)
+        if(ip_heuristic_guess(p+8,l2info->length-8) == 0)
             printf("no IP-hdr found!");
 
+        l2info->header_len+=4;
         return 0; /* stop parsing the output further */
         
     }
+
+    p+=l2info->header_len;
+    l2info->length -= l2info->header_len;
+    l2info->caplen -= l2info->header_len;
+
+    /* search through the cookie table and copy values matching for our PIC type */
+    while (lp->s != NULL) {
+        if (lp->pictype == l2info->pictype) {
+
+            l2info->cookie_len = lp->cookie_len;
+            l2info->header_len += lp->cookie_len;
+
+            switch (p[0]) {
+            case LS_COOKIE_ID:
+                l2info->cookie_type = LS_COOKIE_ID;
+                l2info->cookie_len += 2;
+                l2info->header_len += 2;
+                break;
+            case AS_COOKIE_ID:
+                l2info->cookie_type = AS_COOKIE_ID;
+                l2info->cookie_len += 6;
+                l2info->header_len += 6;
+                break;
+            
+            default:
+                l2info->bundle = l2info->cookie[0];
+                break;
+            }
+
+            if (eflag)
+                printf("%s-PIC, cookie-len %u",
+                       lp->s,
+                       l2info->cookie_len);
+
+            if (l2info->cookie_len > 0) {
+                if (eflag)
+                    printf(", cookie 0x");
+                for (idx = 0; idx < l2info->cookie_len; idx++) {
+                    l2info->cookie[idx] = p[idx]; /* copy cookie data */
+                    if (eflag) printf("%02x",p[idx]);
+                }
+            }
+
+            if (eflag) printf(": "); /* print demarc b/w L2/L3*/
+            
+
+            l2info->proto = EXTRACT_16BITS(p+l2info->cookie_len); 
+            break;
+        }
+        ++lp;
+    }
+    p+=l2info->cookie_len;
+
+    /* DLT_ specific parsing */
+    switch(l2info->pictype) {
+    case DLT_JUNIPER_MLPPP:
+        switch (l2info->cookie_type) {
+        case LS_COOKIE_ID:
+            l2info->bundle = l2info->cookie[1];
+            break;
+        case AS_COOKIE_ID:
+            l2info->bundle = (EXTRACT_16BITS(&l2info->cookie[6])>>3)&0xfff;
+            l2info->proto = (l2info->cookie[5])&JUNIPER_LSQ_L3_PROTO_MASK;            
+            break;
+        default:
+            l2info->bundle = l2info->cookie[0];
+            break;
+        }
+        break;
+    case DLT_JUNIPER_MLFR: /* fall through */
+    case DLT_JUNIPER_MFR:
+        switch (l2info->cookie_type) {
+        case LS_COOKIE_ID:
+            l2info->bundle = l2info->cookie[1];
+            break;
+        case AS_COOKIE_ID:
+            l2info->bundle = (EXTRACT_16BITS(&l2info->cookie[6])>>3)&0xfff;
+            break;
+        default:
+            l2info->bundle = l2info->cookie[0];
+            break;
+        }
+        l2info->proto = EXTRACT_16BITS(p);        
+        l2info->header_len += 2;
+        l2info->length -= 2;
+        l2info->caplen -= 2;
+        break;
+    case DLT_JUNIPER_ATM2:
+        /* ATM cell relay control word present ? */
+        if (l2info->cookie[7] & ATM2_PKT_TYPE_MASK && *p & 0x08) {
+            l2info->header_len += 4;
+            if (eflag)
+                printf("control-word 0x%08x ",EXTRACT_32BITS(p));
+        }
+        break;
+    case DLT_JUNIPER_ATM1:
+    default:
+        break;
+    }
+    
+    if (eflag > 1)
+        printf("hlen %u, proto 0x%04x, ",l2info->header_len,l2info->proto);
+
     return 1; /* everything went ok so far. continue parsing */
 }
+
+
+/*
+ * Local Variables:
+ * c-style: whitesmith
+ * c-basic-offset: 4
+ * End:
+ */