]> CyberLeo.Net >> Repos - FreeBSD/releng/9.1.git/blob - contrib/tcpdump/print-ether.c
MFC r238935,238960:
[FreeBSD/releng/9.1.git] / contrib / tcpdump / print-ether.c
1 /*
2  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 2000
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that: (1) source code distributions
7  * retain the above copyright notice and this paragraph in its entirety, (2)
8  * distributions including binary code include the above copyright notice and
9  * this paragraph in its entirety in the documentation or other materials
10  * provided with the distribution, and (3) all advertising materials mentioning
11  * features or use of this software display the following acknowledgement:
12  * ``This product includes software developed by the University of California,
13  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14  * the University nor the names of its contributors may be used to endorse
15  * or promote products derived from this software without specific prior
16  * written permission.
17  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20  *
21  * $FreeBSD$
22  */
23 #ifndef lint
24 static const char rcsid[] _U_ =
25     "@(#) $Header: /tcpdump/master/tcpdump/print-ether.c,v 1.106 2008-02-06 10:47:53 guy Exp $ (LBL)";
26 #endif
27
28 #define NETDISSECT_REWORKED
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <tcpdump-stdinc.h>
34
35 #include <stdio.h>
36 #include <pcap.h>
37
38 #include "interface.h"
39 #include "extract.h"
40 #include "addrtoname.h"
41 #include "ethertype.h"
42
43 #include "ether.h"
44
45 const struct tok ethertype_values[] = { 
46     { ETHERTYPE_IP,             "IPv4" },
47     { ETHERTYPE_MPLS,           "MPLS unicast" },
48     { ETHERTYPE_MPLS_MULTI,     "MPLS multicast" },
49     { ETHERTYPE_IPV6,           "IPv6" },
50     { ETHERTYPE_8021Q,          "802.1Q" },
51     { ETHERTYPE_8021Q9100,      "802.1Q-9100" },
52     { ETHERTYPE_8021QinQ,       "802.1Q-QinQ" },
53     { ETHERTYPE_8021Q9200,      "802.1Q-9200" },
54     { ETHERTYPE_VMAN,           "VMAN" },
55     { ETHERTYPE_PUP,            "PUP" },
56     { ETHERTYPE_ARP,            "ARP"},
57     { ETHERTYPE_REVARP,         "Reverse ARP"},
58     { ETHERTYPE_NS,             "NS" },
59     { ETHERTYPE_SPRITE,         "Sprite" },
60     { ETHERTYPE_TRAIL,          "Trail" },
61     { ETHERTYPE_MOPDL,          "MOP DL" },
62     { ETHERTYPE_MOPRC,          "MOP RC" },
63     { ETHERTYPE_DN,             "DN" },
64     { ETHERTYPE_LAT,            "LAT" },
65     { ETHERTYPE_SCA,            "SCA" },
66     { ETHERTYPE_TEB,            "TEB" },
67     { ETHERTYPE_LANBRIDGE,      "Lanbridge" },
68     { ETHERTYPE_DECDNS,         "DEC DNS" },
69     { ETHERTYPE_DECDTS,         "DEC DTS" },
70     { ETHERTYPE_VEXP,           "VEXP" },
71     { ETHERTYPE_VPROD,          "VPROD" },
72     { ETHERTYPE_ATALK,          "Appletalk" },
73     { ETHERTYPE_AARP,           "Appletalk ARP" },
74     { ETHERTYPE_IPX,            "IPX" },
75     { ETHERTYPE_PPP,            "PPP" },
76     { ETHERTYPE_MPCP,           "MPCP" },
77     { ETHERTYPE_SLOW,           "Slow Protocols" },
78     { ETHERTYPE_PPPOED,         "PPPoE D" },
79     { ETHERTYPE_PPPOES,         "PPPoE S" },
80     { ETHERTYPE_EAPOL,          "EAPOL" },
81     { ETHERTYPE_RRCP,           "RRCP" },
82     { ETHERTYPE_JUMBO,          "Jumbo" },
83     { ETHERTYPE_LOOPBACK,       "Loopback" },
84     { ETHERTYPE_ISO,            "OSI" },
85     { ETHERTYPE_GRE_ISO,        "GRE-OSI" },
86     { ETHERTYPE_CFM_OLD,        "CFM (old)" },
87     { ETHERTYPE_CFM,            "CFM" },
88     { ETHERTYPE_LLDP,           "LLDP" },
89     { 0, NULL}
90 };
91
92 static inline void
93 ether_hdr_print(netdissect_options *ndo,
94                 const u_char *bp, u_int length)
95 {
96         register const struct ether_header *ep;
97         u_int16_t ether_type;
98
99         ep = (const struct ether_header *)bp;
100
101         (void)ND_PRINT((ndo, "%s > %s",
102                      etheraddr_string(ESRC(ep)),
103                      etheraddr_string(EDST(ep))));
104
105         ether_type = EXTRACT_16BITS(&ep->ether_type);
106         if (!ndo->ndo_qflag) {
107                 if (ether_type <= ETHERMTU)
108                           (void)ND_PRINT((ndo, ", 802.3"));
109                 else 
110                           (void)ND_PRINT((ndo, ", ethertype %s (0x%04x)",
111                                        tok2str(ethertype_values,"Unknown", ether_type),
112                                        ether_type));
113         } else {
114                 if (ether_type <= ETHERMTU)
115                           (void)ND_PRINT((ndo, ", 802.3"));
116                 else 
117                           (void)ND_PRINT((ndo, ", %s", tok2str(ethertype_values,"Unknown Ethertype (0x%04x)", ether_type)));
118         }
119
120         (void)ND_PRINT((ndo, ", length %u: ", length));
121 }
122
123 /*
124  * Print an Ethernet frame.
125  * This might be encapsulated within another frame; we might be passed
126  * a pointer to a function that can print header information for that
127  * frame's protocol, and an argument to pass to that function.
128  */
129 void
130 ether_print(netdissect_options *ndo,
131             const u_char *p, u_int length, u_int caplen,
132             void (*print_encap_header)(netdissect_options *ndo, const u_char *), const u_char *encap_header_arg)
133 {
134         struct ether_header *ep;
135         u_int orig_length;
136         u_short ether_type;
137         u_short extracted_ether_type;
138
139         if (caplen < ETHER_HDRLEN || length < ETHER_HDRLEN) {
140                 ND_PRINT((ndo, "[|ether]"));
141                 return;
142         }
143
144         if (ndo->ndo_eflag) {
145                 if (print_encap_header != NULL)
146                         (*print_encap_header)(ndo, encap_header_arg);
147                 ether_hdr_print(ndo, p, length);
148         }
149         orig_length = length;
150
151         length -= ETHER_HDRLEN;
152         caplen -= ETHER_HDRLEN;
153         ep = (struct ether_header *)p;
154         p += ETHER_HDRLEN;
155
156         ether_type = EXTRACT_16BITS(&ep->ether_type);
157
158 recurse:
159         /*
160          * Is it (gag) an 802.3 encapsulation?
161          */
162         if (ether_type <= ETHERMTU) {
163                 /* Try to print the LLC-layer header & higher layers */
164                 if (llc_print(p, length, caplen, ESRC(ep), EDST(ep),
165                     &extracted_ether_type) == 0) {
166                         /* ether_type not known, print raw packet */
167                         if (!ndo->ndo_eflag) {
168                                 if (print_encap_header != NULL)
169                                         (*print_encap_header)(ndo, encap_header_arg);
170                                 ether_hdr_print(ndo, (u_char *)ep, orig_length);
171                         }
172
173                         if (!ndo->ndo_suppress_default_print)
174                                 ndo->ndo_default_print(ndo, p, caplen);
175                 }
176         } else if (ether_type == ETHERTYPE_8021Q  ||
177                 ether_type == ETHERTYPE_8021Q9100 ||
178                 ether_type == ETHERTYPE_8021Q9200 ||
179                 ether_type == ETHERTYPE_8021QinQ) {
180                 /*
181                  * Print VLAN information, and then go back and process
182                  * the enclosed type field.
183                  */
184                 if (caplen < 4 || length < 4) {
185                         ND_PRINT((ndo, "[|vlan]"));
186                         return;
187                 }
188                 if (ndo->ndo_eflag) {
189                         u_int16_t tag = EXTRACT_16BITS(p);
190
191                         ND_PRINT((ndo, "vlan %u, p %u%s, ",
192                             tag & 0xfff,
193                             tag >> 13,
194                             (tag & 0x1000) ? ", CFI" : ""));
195                 }
196
197                 ether_type = EXTRACT_16BITS(p + 2);
198                 if (ndo->ndo_eflag && ether_type > ETHERMTU)
199                         ND_PRINT((ndo, "ethertype %s, ", tok2str(ethertype_values,"0x%04x", ether_type)));
200                 p += 4;
201                 length -= 4;
202                 caplen -= 4;
203                 goto recurse;
204         } else if (ether_type == ETHERTYPE_JUMBO) {
205                 /*
206                  * Alteon jumbo frames.
207                  * See
208                  *
209                  *      http://tools.ietf.org/html/draft-ietf-isis-ext-eth-01
210                  *
211                  * which indicates that, following the type field,
212                  * there's an LLC header and payload.
213                  */
214                 /* Try to print the LLC-layer header & higher layers */
215                 if (llc_print(p, length, caplen, ESRC(ep), EDST(ep),
216                     &extracted_ether_type) == 0) {
217                         /* ether_type not known, print raw packet */
218                         if (!ndo->ndo_eflag) {
219                                 if (print_encap_header != NULL)
220                                         (*print_encap_header)(ndo, encap_header_arg);
221                                 ether_hdr_print(ndo, (u_char *)ep, orig_length);
222                         }
223
224                         if (!ndo->ndo_suppress_default_print)
225                                 ndo->ndo_default_print(ndo, p, caplen);
226                 }
227         } else {
228                 if (ethertype_print(ndo, ether_type, p, length, caplen) == 0) {
229                         /* ether_type not known, print raw packet */
230                         if (!ndo->ndo_eflag) {
231                                 if (print_encap_header != NULL)
232                                         (*print_encap_header)(ndo, encap_header_arg);
233                                 ether_hdr_print(ndo, (u_char *)ep, orig_length);
234                         }
235
236                         if (!ndo->ndo_suppress_default_print)
237                                 ndo->ndo_default_print(ndo, p, caplen);
238                 }
239         }
240 }
241
242 /*
243  * This is the top level routine of the printer.  'p' points
244  * to the ether header of the packet, 'h->ts' is the timestamp,
245  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
246  * is the number of bytes actually captured.
247  */
248 u_int
249 ether_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
250                const u_char *p)
251 {
252         ether_print(ndo, p, h->len, h->caplen, NULL, NULL);
253
254         return (ETHER_HDRLEN);
255 }
256
257 /*
258  * This is the top level routine of the printer.  'p' points
259  * to the ether header of the packet, 'h->ts' is the timestamp,
260  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
261  * is the number of bytes actually captured.
262  *
263  * This is for DLT_NETANALYZER, which has a 4-byte pseudo-header
264  * before the Ethernet header.
265  */
266 u_int
267 netanalyzer_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
268                      const u_char *p)
269 {
270         /*
271          * Fail if we don't have enough data for the Hilscher pseudo-header.
272          */
273         if (h->len < 4 || h->caplen < 4) {
274                 printf("[|netanalyzer]");
275                 return (h->caplen);
276         }
277
278         /* Skip the pseudo-header. */
279         ether_print(ndo, p + 4, h->len - 4, h->caplen - 4, NULL, NULL);
280
281         return (4 + ETHER_HDRLEN);
282 }
283
284 /*
285  * This is the top level routine of the printer.  'p' points
286  * to the ether header of the packet, 'h->ts' is the timestamp,
287  * 'h->len' is the length of the packet off the wire, and 'h->caplen'
288  * is the number of bytes actually captured.
289  *
290  * This is for DLT_NETANALYZER_TRANSPARENT, which has a 4-byte
291  * pseudo-header, a 7-byte Ethernet preamble, and a 1-byte Ethernet SOF
292  * before the Ethernet header.
293  */
294 u_int
295 netanalyzer_transparent_if_print(netdissect_options *ndo,
296                                  const struct pcap_pkthdr *h,
297                                  const u_char *p)
298 {
299         /*
300          * Fail if we don't have enough data for the Hilscher pseudo-header,
301          * preamble, and SOF.
302          */
303         if (h->len < 12 || h->caplen < 12) {
304                 printf("[|netanalyzer-transparent]");
305                 return (h->caplen);
306         }
307
308         /* Skip the pseudo-header, preamble, and SOF. */
309         ether_print(ndo, p + 12, h->len - 12, h->caplen - 12, NULL, NULL);
310
311         return (12 + ETHER_HDRLEN);
312 }
313
314 /*
315  * Prints the packet payload, given an Ethernet type code for the payload's
316  * protocol.
317  *
318  * Returns non-zero if it can do so, zero if the ethertype is unknown.
319  */
320
321 int
322 ethertype_print(netdissect_options *ndo,
323                 u_short ether_type, const u_char *p,
324                 u_int length, u_int caplen)
325 {
326         switch (ether_type) {
327
328         case ETHERTYPE_IP:
329                 ip_print(ndo, p, length);
330                 return (1);
331
332 #ifdef INET6
333         case ETHERTYPE_IPV6:
334                 ip6_print(ndo, p, length);
335                 return (1);
336 #endif /*INET6*/
337
338         case ETHERTYPE_ARP:
339         case ETHERTYPE_REVARP:
340                 arp_print(ndo, p, length, caplen);
341                 return (1);
342
343         case ETHERTYPE_DN:
344                 decnet_print(/*ndo,*/p, length, caplen);
345                 return (1);
346
347         case ETHERTYPE_ATALK:
348                 if (ndo->ndo_vflag)
349                         fputs("et1 ", stdout);
350                 atalk_print(/*ndo,*/p, length);
351                 return (1);
352
353         case ETHERTYPE_AARP:
354                 aarp_print(/*ndo,*/p, length);
355                 return (1);
356
357         case ETHERTYPE_IPX:
358                 ND_PRINT((ndo, "(NOV-ETHII) "));
359                 ipx_print(/*ndo,*/p, length);
360                 return (1);
361
362         case ETHERTYPE_ISO:
363                 isoclns_print(/*ndo,*/p+1, length-1, length-1);
364                 return(1);
365
366         case ETHERTYPE_PPPOED:
367         case ETHERTYPE_PPPOES:
368         case ETHERTYPE_PPPOED2:
369         case ETHERTYPE_PPPOES2:
370                 pppoe_print(/*ndo,*/p, length);
371                 return (1);
372
373         case ETHERTYPE_EAPOL:
374                 eap_print(ndo, p, length);
375                 return (1);
376
377         case ETHERTYPE_RRCP:
378                 rrcp_print(ndo, p - 14 , length + 14);
379                 return (1);
380
381         case ETHERTYPE_PPP:
382                 if (length) {
383                         printf(": ");
384                         ppp_print(/*ndo,*/p, length);
385                 }
386                 return (1);
387
388         case ETHERTYPE_MPCP:
389                 mpcp_print(/*ndo,*/p, length);
390                 return (1);
391
392         case ETHERTYPE_SLOW:
393                 slow_print(/*ndo,*/p, length);
394                 return (1);
395
396         case ETHERTYPE_CFM:
397         case ETHERTYPE_CFM_OLD:
398                 cfm_print(/*ndo,*/p, length);
399                 return (1);
400
401         case ETHERTYPE_LLDP:
402                 lldp_print(/*ndo,*/p, length);
403                 return (1);
404
405         case ETHERTYPE_LOOPBACK:
406                 return (1);
407
408         case ETHERTYPE_MPLS:
409         case ETHERTYPE_MPLS_MULTI:
410                 mpls_print(/*ndo,*/p, length);
411                 return (1);
412
413         case ETHERTYPE_LAT:
414         case ETHERTYPE_SCA:
415         case ETHERTYPE_MOPRC:
416         case ETHERTYPE_MOPDL:
417                 /* default_print for now */
418         default:
419                 return (0);
420         }
421 }
422
423
424 /*
425  * Local Variables:
426  * c-style: whitesmith
427  * c-basic-offset: 8
428  * End:
429  */
430