]> The Tcpdump Group git mirrors - tcpdump/blob - print-juniper.c
-change the BPF_ defines to JUNIPER_BPF defines as they are private
[tcpdump] / print-juniper.c
1 /*
2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that: (1) source code
4 * distributions retain the above copyright notice and this paragraph
5 * in its entirety, and (2) distributions including binary code include
6 * the above copyright notice and this paragraph in its entirety in
7 * the documentation or other materials provided with the distribution.
8 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND
9 * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT
10 * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
11 * FOR A PARTICULAR PURPOSE.
12 *
13 * Original code by Hannes Gredler (hannes@juniper.net)
14 */
15
16 #ifndef lint
17 static const char rcsid[] _U_ =
18 "@(#) $Header: /tcpdump/master/tcpdump/print-juniper.c,v 1.3 2004-10-28 09:36:16 hannes Exp $ (LBL)";
19 #endif
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <tcpdump-stdinc.h>
26
27 #include <pcap.h>
28 #include <stdio.h>
29
30 #include "interface.h"
31 #include "extract.h"
32 #include "ppp.h"
33
34 #define JUNIPER_BPF_OUT 0 /* Outgoing packet */
35 #define JUNIPER_BPF_IN 1 /* Incoming packet */
36 #define JUNIPER_BPF_PKT_IN 0x1 /* Incoming packet */
37 #define JUNIPER_BPF_NO_L2 0x2 /* L2 header stripped */
38
39 #define ATM2_PKT_TYPE_MASK 0x70
40 #define ATM2_GAP_COUNT_MASK 0x3F
41
42 int ip_heuristic_guess(register const u_char *, u_int);
43 int juniper_ppp_heuristic_guess(register const u_char *, u_int);
44 static int juniper_parse_header (const u_char *, u_int8_t *, u_int);
45
46 /*
47 * ATM1 PIC cookie format
48 *
49 * +-----+-------------------------+-------------------------------+
50 * |fmtid| vc index | channel ID |
51 * +-----+-------------------------+-------------------------------+
52 */
53
54 u_int
55 juniper_atm1_print(const struct pcap_pkthdr *h, register const u_char *p)
56 {
57 register u_int length = h->len;
58 register u_int caplen = h->caplen;
59 u_int16_t extracted_ethertype;
60 u_int8_t direction;
61 u_int32_t cookie1;
62
63 if(juniper_parse_header(p, &direction,length) == 0)
64 return 0;
65
66 p+=4;
67 length-=4;
68 caplen-=4;
69
70 cookie1=EXTRACT_32BITS(p);
71
72 if (eflag) {
73 /* FIXME decode channel-id, vc-index, fmt-id
74 for once lets just hexdump the cookie */
75
76 printf("ATM1 cookie 0x%08x, ", EXTRACT_32BITS(p));
77 }
78
79 p+=4;
80 length-=4;
81 caplen-=4;
82
83 if (cookie1 & 0x80000000) { /* OAM cell ? */
84 oam_print(p,length);
85 }
86
87 if (EXTRACT_24BITS(p) == 0xfefe03 || /* NLPID encaps ? */
88 EXTRACT_24BITS(p) == 0xaaaa03) { /* SNAP encaps ? */
89
90 if (llc_print(p, length, caplen, NULL, NULL,
91 &extracted_ethertype) != 0)
92 return 8;
93 }
94
95 if (p[0] == 0x03) { /* Cisco style NLPID encaps ? */
96 isoclns_print(p + 1, length - 1, caplen - 1);
97 /* FIXME check if frame was recognized */
98 return 8;
99 }
100
101 if(ip_heuristic_guess(p, length) != 0) /* last try - vcmux encaps ? */
102 return 0;
103
104 return (8);
105 }
106
107 /*
108 * ATM2 PIC cookie format
109 *
110 * +-------------------------------+---------+---+-----+-----------+
111 * | channel ID | reserv |AAL| CCRQ| gap cnt |
112 * +-------------------------------+---------+---+-----+-----------+
113 */
114
115 u_int
116 juniper_atm2_print(const struct pcap_pkthdr *h, register const u_char *p)
117 {
118 register u_int length = h->len;
119 register u_int caplen = h->caplen;
120 u_int16_t extracted_ethertype;
121 u_int8_t direction;
122 u_int32_t cookie1,cookie2;
123
124 if(juniper_parse_header(p, &direction,length) == 0)
125 return 0;
126
127 p+=4;
128 length-=4;
129 caplen-=4;
130
131 cookie1=EXTRACT_32BITS(p);
132 cookie2=EXTRACT_32BITS(p+4);
133
134 if (eflag) {
135 /* FIXME decode channel, fmt-id, ccrq, aal, gap cnt
136 for once lets just hexdump the cookie */
137
138 printf("ATM2 cookie 0x%08x%08x, ",
139 EXTRACT_32BITS(p),
140 EXTRACT_32BITS(p+4));
141 }
142
143 p+=8;
144 length-=8;
145 caplen-=8;
146
147 if (cookie2 & ATM2_PKT_TYPE_MASK) { /* OAM cell ? */
148 oam_print(p,length);
149 return 12;
150 }
151
152 if (EXTRACT_24BITS(p) == 0xfefe03 || /* NLPID encaps ? */
153 EXTRACT_24BITS(p) == 0xaaaa03) { /* SNAP encaps ? */
154
155 if (llc_print(p, length, caplen, NULL, NULL,
156 &extracted_ethertype) != 0)
157 return 12;
158 }
159
160 if (direction != JUNIPER_BPF_PKT_IN && /* ether-over-1483 encaps ? */
161 (cookie1 & ATM2_GAP_COUNT_MASK)) {
162 ether_print(p, length, caplen);
163 return 12;
164 }
165
166 if (p[0] == 0x03) { /* Cisco style NLPID encaps ? */
167 isoclns_print(p + 1, length - 1, caplen - 1);
168 /* FIXME check if frame was recognized */
169 return 12;
170 }
171
172 if(juniper_ppp_heuristic_guess(p, length) != 0) /* PPPoA vcmux encaps ? */
173 return 12;
174
175 if(ip_heuristic_guess(p, length) != 0) /* last try - vcmux encaps ? */
176 return 12;
177
178 return (12);
179 }
180
181
182 /* try to guess, based on all PPP protos that are supported in
183 * a juniper router if the payload data is encapsulated using PPP */
184 int
185 juniper_ppp_heuristic_guess(register const u_char *p, u_int length) {
186
187 switch(EXTRACT_16BITS(p)) {
188 case PPP_IP :
189 case PPP_OSI :
190 case PPP_MPLS_UCAST :
191 case PPP_MPLS_MCAST :
192 case PPP_IPCP :
193 case PPP_OSICP :
194 case PPP_MPLSCP :
195 case PPP_LCP :
196 case PPP_PAP :
197 case PPP_CHAP :
198 case PPP_ML :
199 #ifdef INET6
200 case PPP_IPV6 :
201 case PPP_IPV6CP :
202 #endif
203 ppp_print(p, length);
204 break;
205
206 default:
207 return 0; /* did not find a ppp header */
208 break;
209 }
210 return 1; /* we printed a ppp packet */
211 }
212
213 int
214 ip_heuristic_guess(register const u_char *p, u_int length) {
215
216 switch(p[0]) {
217 case 0x45:
218 case 0x46:
219 case 0x47:
220 case 0x48:
221 case 0x49:
222 case 0x4a:
223 case 0x4b:
224 case 0x4c:
225 case 0x4d:
226 case 0x4e:
227 case 0x4f:
228 ip_print(p, length);
229 break;
230 #ifdef INET6
231 case 0x60:
232 case 0x61:
233 case 0x62:
234 case 0x63:
235 case 0x64:
236 case 0x65:
237 case 0x66:
238 case 0x67:
239 case 0x68:
240 case 0x69:
241 case 0x6a:
242 case 0x6b:
243 case 0x6c:
244 case 0x6d:
245 case 0x6e:
246 case 0x6f:
247 ip6_print(p, length);
248 break;
249 #endif
250 default:
251 return 0; /* did not find a ip header */
252 break;
253 }
254 return 1; /* we printed an v4/v6 packet */
255 }
256
257 static int
258 juniper_parse_header (const u_char *p, u_int8_t *direction, u_int length) {
259
260 *direction = p[3]&JUNIPER_BPF_PKT_IN;
261
262 if (EXTRACT_24BITS(p) != 0x4d4743) /* magic number found ? */
263 return -1;
264
265 if (*direction == JUNIPER_BPF_PKT_IN) {
266 if (eflag)
267 printf("%3s ", "In");
268 }
269 else {
270 if (eflag)
271 printf("%3s ", "Out");
272 }
273
274 if ((p[3] & JUNIPER_BPF_NO_L2 ) == JUNIPER_BPF_NO_L2 ) {
275 if (eflag)
276 printf("no-L2-hdr, ");
277
278 /* there is no link-layer present -
279 * perform the v4/v6 heuristics
280 * to figure out what it is
281 */
282 if(ip_heuristic_guess(p+8,length-8) == 0)
283 printf("no IP-hdr found!");
284
285 return 0; /* stop parsing the output further */
286
287 }
288 return 1; /* everything went ok so far. continue parsing */
289 }