xref: /openbsd/usr.sbin/tcpdump/print-atm.c (revision 426b943d)
1 /*	$OpenBSD: print-atm.c,v 1.15 2021/12/01 18:28:45 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 1994, 1995, 1996, 1997
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that: (1) source code distributions
9  * retain the above copyright notice and this paragraph in its entirety, (2)
10  * distributions including binary code include the above copyright notice and
11  * this paragraph in its entirety in the documentation or other materials
12  * provided with the distribution, and (3) all advertising materials mentioning
13  * features or use of this software display the following acknowledgement:
14  * ``This product includes software developed by the University of California,
15  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16  * the University nor the names of its contributors may be used to endorse
17  * or promote products derived from this software without specific prior
18  * written permission.
19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22  */
23 
24 #include <sys/time.h>
25 #include <sys/socket.h>
26 
27 #include <net/if.h>
28 
29 #include <netinet/in.h>
30 #include <netinet/if_ether.h>
31 #include <netinet/ip.h>
32 #include <netinet/ip_var.h>
33 #include <netinet/udp.h>
34 #include <netinet/udp_var.h>
35 #include <netinet/tcp.h>
36 
37 #include <stdio.h>
38 #include <pcap.h>
39 
40 #include "interface.h"
41 #include "addrtoname.h"
42 #include "ethertype.h"
43 
44 /*
45  * This is the top level routine of the printer.  'p' is the points
46  * to the LLC/SNAP header of the packet, 'tvp' is the timestamp,
47  * 'length' is the length of the packet off the wire, and 'caplen'
48  * is the number of bytes actually captured.
49  */
50 void
atm_if_print(u_char * user,const struct pcap_pkthdr * h,const u_char * p)51 atm_if_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
52 {
53 	u_int caplen = h->caplen;
54 	u_int length = h->len;
55 	u_short ethertype;
56 
57 	ts_print(&h->ts);
58 
59 	if (caplen < 8) {
60 		printf("[|atm]");
61 		goto out;
62 	}
63 	if (p[0] != 0xaa || p[1] != 0xaa || p[2] != 0x03) {
64 		/*XXX assume 802.6 MAC header from fore driver */
65 #define MIN_ATM_8026_HDRLEN (20 + 8)
66 		if (caplen < MIN_ATM_8026_HDRLEN) {
67 			printf("[|atm]");
68 			goto out;
69 		}
70 		if (eflag)
71 			printf("%04x%04x %04x%04x ",
72 			       p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3],
73 			       p[4] << 24 | p[5] << 16 | p[6] << 8 | p[7],
74 			       p[8] << 24 | p[9] << 16 | p[10] << 8 | p[11],
75 			       p[12] << 24 | p[13] << 16 | p[14] << 8 | p[15]);
76 		p += 20;
77 		length -= 20;
78 		caplen -= 20;
79 	}
80 	ethertype = p[6] << 8 | p[7];
81 	if (eflag)
82 		printf("%02x %02x %02x %02x-%02x-%02x %04x: ",
83 		       p[0], p[1], p[2], /* dsap/ssap/ctrl */
84 		       p[3], p[4], p[5], /* manufacturer's code */
85 		       ethertype);
86 
87 	/*
88 	 * Some printers want to get back at the ethernet addresses,
89 	 * and/or check that they're not walking off the end of the packet.
90 	 * Rather than pass them all the way down, we set these globals.
91 	 */
92 	packetp = p;
93 	snapend = p + caplen;
94 
95 	length -= 8;
96 	caplen -= 8;
97 	p += 8;
98 
99 	switch (ethertype) {
100 
101 	case ETHERTYPE_IP:
102 		ip_print(p, length);
103 		break;
104 
105 	case ETHERTYPE_IPV6:
106 		ip6_print(p, length);
107 		break;
108 
109 		/*XXX this probably isn't right */
110 	case ETHERTYPE_ARP:
111 	case ETHERTYPE_REVARP:
112 		arp_print(p, length, caplen);
113 		break;
114 #ifdef notyet
115 	case ETHERTYPE_DN:
116 		decnet_print(p, length, caplen);
117 		break;
118 
119 	case ETHERTYPE_ATALK:
120 		if (vflag)
121 			printf("et1 ");
122 		atalk_print(p, length);
123 		break;
124 
125 	case ETHERTYPE_AARP:
126 		aarp_print(p, length);
127 		break;
128 
129 	case ETHERTYPE_LAT:
130 	case ETHERTYPE_MOPRC:
131 	case ETHERTYPE_MOPDL:
132 		/* default_print for now */
133 #endif
134 	default:
135 		/* ether_type not known, print raw packet */
136 		if (!eflag)
137 			printf("%02x %02x %02x %02x-%02x-%02x %04x: ",
138 			       p[0], p[1], p[2], /* dsap/ssap/ctrl */
139 			       p[3], p[4], p[5], /* manufacturer's code */
140 			       ethertype);
141 		if (!xflag && !qflag)
142 			default_print(p, caplen);
143 	}
144 	if (xflag)
145 		default_print(p, caplen);
146  out:
147 	putchar('\n');
148 }
149