xref: /openbsd-src/usr.sbin/tcpdump/print-etherip.c (revision ae3cb403620ab940fbaabb3055fac045a63d56b7)
1 /*	$OpenBSD: print-etherip.c,v 1.9 2017/03/08 10:05:30 jca Exp $	*/
2 
3 /*
4  * Copyright (c) 2001 Jason L. Wright (jason@thought.net)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * Format and print etherip packets
31  */
32 
33 #include <sys/time.h>
34 #include <sys/socket.h>
35 
36 #include <net/if.h>
37 #include <netinet/in.h>
38 #include <netinet/ip.h>
39 #include <netinet/ip_var.h>
40 #include <netinet/udp.h>
41 #include <netinet/udp_var.h>
42 #include <netinet/tcp.h>
43 #include <netinet/if_ether.h>
44 #include <netinet/ip_ether.h>
45 
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <stddef.h>
51 
52 #include "addrtoname.h"
53 #include "interface.h"
54 #include "extract.h"		    /* must come after interface.h */
55 
56 extern u_short extracted_ethertype;
57 
58 void
59 etherip_print(const u_char *bp, u_int caplen, u_int len, const u_char *bp2)
60 {
61 	const struct ip *ip = (const struct ip *)bp2;
62 	struct ether_header *eh;
63 	const u_char *pbuf = bp;
64 	u_int plen = caplen, hlen;
65 	u_int16_t etype;
66 
67 	if (plen < sizeof(struct etherip_header)) {
68 		printf("[|etherip]");
69 		return;
70 	}
71 
72 	printf("etherip %s > %s ver ", ipaddr_string(&ip->ip_src),
73 	    ipaddr_string(&ip->ip_dst));
74 
75 	switch (*pbuf >> 4) {
76 	case 2:
77 		hlen = 1;
78 		printf("%d", 2);
79 		break;
80 	case 3:
81 		hlen = 2;
82 		printf("%d", 3);
83 		break;
84 	default:
85 		hlen = 0;
86 		printf("unknown");
87 		break;
88 	}
89 	printf(" len %d", len);
90 	if (hlen == 0)
91 		return;
92 
93 	printf(": ");
94 
95 	if (plen < hlen) {
96 		printf("[|etherip]");
97 		return;
98 	}
99 	pbuf += hlen;
100 	plen -= hlen;
101 	len -= hlen;
102 
103 	if (eflag)
104 		ether_print(pbuf, len);
105 	eh = (struct ether_header *)pbuf;
106 	if (plen < sizeof(struct ether_header)) {
107 		printf("[|ether]");
108 		return;
109 	}
110 	etype = EXTRACT_16BITS(pbuf + offsetof(struct ether_header, ether_type));
111 	pbuf += sizeof(struct ether_header);
112 	plen -= sizeof(struct ether_header);
113 	len -= sizeof(struct ether_header);
114 
115 	/* XXX LLC? */
116 	extracted_ethertype = 0;
117 	if (etype <= ETHERMTU) {
118 		if (llc_print(pbuf, len, plen, ESRC(eh), EDST(eh)) == 0) {
119 			if (!eflag)
120 				ether_print((u_char *)eh, len);
121 			if (extracted_ethertype) {
122 				printf("LLC %s",
123 				    etherproto_string(htons(extracted_ethertype)));
124 			}
125 			if (!xflag && !qflag)
126 				default_print(pbuf, plen);
127 		}
128 	} else if (ether_encap_print(etype, pbuf, len, plen) == 0) {
129 		if (!eflag)
130 			ether_print((u_char *)eh, len + sizeof(*eh));
131 		if (!xflag && !qflag)
132 			default_print(pbuf, plen);
133 	}
134 }
135