]> The Tcpdump Group git mirrors - tcpdump/blob - print-juniper.c
- add a brief ML-PPP printer
[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.2 2004-10-20 16:14: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 BPF_OUT 0 /* Outgoing packet */
35 #define BPF_IN 1 /* Incoming packet */
36 #define BPF_PKT_IN 0x1 /* Incoming packet */
37 #define 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 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 != 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(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 int
183 ppp_heuristic_guess(register const u_char *p, u_int length) {
184
185 switch(EXTRACT_16BITS(p)) {
186 case PPP_IP :
187 case PPP_OSI :
188 case PPP_MPLS_UCAST :
189 case PPP_MPLS_MCAST :
190 case PPP_IPCP :
191 case PPP_OSICP :
192 case PPP_MPLSCP :
193 case PPP_LCP :
194 case PPP_PAP :
195 case PPP_CHAP :
196 case PPP_ML :
197 #ifdef INET6
198 case PPP_IPV6 :
199 case PPP_IPV6CP :
200 #endif
201 ppp_print(p, length);
202 break;
203
204 default:
205 return 0; /* did not find a ppp header */
206 break;
207 }
208 return 1; /* we printed a ppp packet */
209 }
210
211 int
212 ip_heuristic_guess(register const u_char *p, u_int length) {
213
214 switch(p[0]) {
215 case 0x45:
216 case 0x46:
217 case 0x47:
218 case 0x48:
219 case 0x49:
220 case 0x4a:
221 case 0x4b:
222 case 0x4c:
223 case 0x4d:
224 case 0x4e:
225 case 0x4f:
226 ip_print(p, length);
227 break;
228 #ifdef INET6
229 case 0x60:
230 case 0x61:
231 case 0x62:
232 case 0x63:
233 case 0x64:
234 case 0x65:
235 case 0x66:
236 case 0x67:
237 case 0x68:
238 case 0x69:
239 case 0x6a:
240 case 0x6b:
241 case 0x6c:
242 case 0x6d:
243 case 0x6e:
244 case 0x6f:
245 ip6_print(p, length);
246 break;
247 #endif
248 default:
249 return 0; /* did not find a ip header */
250 break;
251 }
252 return 1; /* we printed an v4/v6 packet */
253 }
254
255 static int
256 juniper_parse_header (const u_char *p, u_int8_t *direction, u_int length) {
257
258 *direction = p[3]&BPF_PKT_IN;
259
260 if (EXTRACT_24BITS(p) != 0x4d4743) /* magic number found ? */
261 return -1;
262
263 if (*direction == BPF_PKT_IN) {
264 if (eflag)
265 printf("%3s ", "In");
266 }
267 else {
268 if (eflag)
269 printf("%3s ", "Out");
270 }
271
272 if ((p[3] & BPF_NO_L2 ) == BPF_NO_L2 ) {
273 if (eflag)
274 printf("no-L2-hdr, ");
275
276 /* there is no link-layer present -
277 * perform the v4/v6 heuristics
278 * to figure out what it is
279 */
280 if(ip_heuristic_guess(p+8,length-8) == 0)
281 printf("no IP-hdr found!");
282
283 return 0; /* stop parsing the output further */
284
285 }
286 return 1; /* everything went ok so far. continue parsing */
287 }