xref: /openbsd/usr.sbin/tcpdump/print-etherip.c (revision c97d4a25)
1 /*	$OpenBSD: print-etherip.c,v 1.10 2018/02/10 10:00:32 dlg 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
etherip_print(const u_char * bp,u_int caplen,u_int len)59 etherip_print(const u_char *bp, u_int caplen, u_int len)
60 {
61 	struct ether_header *eh;
62 	const u_char *pbuf = bp;
63 	u_int plen = caplen, hlen;
64 	u_int16_t etype;
65 
66 	printf("etherip ");
67 
68 	if (plen < sizeof(struct etherip_header)) {
69 		printf("[|etherip]");
70 		return;
71 	}
72 
73 	switch (*pbuf >> 4) {
74 	case 2:
75 		hlen = 1;
76 		printf("%d", 2);
77 		break;
78 	case 3:
79 		hlen = 2;
80 		printf("%d", 3);
81 		break;
82 	default:
83 		hlen = 0;
84 		printf("unknown");
85 		break;
86 	}
87 	printf(" len %d", len);
88 	if (hlen == 0)
89 		return;
90 
91 	printf(": ");
92 
93 	if (plen < hlen) {
94 		printf("[|etherip]");
95 		return;
96 	}
97 	pbuf += hlen;
98 	plen -= hlen;
99 	len -= hlen;
100 
101 	if (eflag)
102 		ether_print(pbuf, len);
103 	eh = (struct ether_header *)pbuf;
104 	if (plen < sizeof(struct ether_header)) {
105 		printf("[|ether]");
106 		return;
107 	}
108 	etype = EXTRACT_16BITS(pbuf + offsetof(struct ether_header, ether_type));
109 	pbuf += sizeof(struct ether_header);
110 	plen -= sizeof(struct ether_header);
111 	len -= sizeof(struct ether_header);
112 
113 	/* XXX LLC? */
114 	extracted_ethertype = 0;
115 	if (etype <= ETHERMTU) {
116 		if (llc_print(pbuf, len, plen, ESRC(eh), EDST(eh)) == 0) {
117 			if (!eflag)
118 				ether_print((u_char *)eh, len);
119 			if (extracted_ethertype) {
120 				printf("LLC %s",
121 				    etherproto_string(htons(extracted_ethertype)));
122 			}
123 			if (!xflag && !qflag)
124 				default_print(pbuf, plen);
125 		}
126 	} else if (ether_encap_print(etype, pbuf, len, plen) == 0) {
127 		if (!eflag)
128 			ether_print((u_char *)eh, len + sizeof(*eh));
129 		if (!xflag && !qflag)
130 			default_print(pbuf, plen);
131 	}
132 }
133