xref: /openbsd-src/usr.sbin/tcpdump/print-etherip.c (revision e5157e49389faebcb42b7237d55fbf096d9c2523)
1 /*	$OpenBSD: print-etherip.c,v 1.7 2014/08/14 12:44:44 mpi 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/param.h>
34 #include <sys/time.h>
35 #include <sys/socket.h>
36 
37 #include <net/if.h>
38 #include <netinet/in.h>
39 #include <netinet/ip.h>
40 #include <netinet/ip_var.h>
41 #include <netinet/udp.h>
42 #include <netinet/udp_var.h>
43 #include <netinet/tcp.h>
44 #include <netinet/if_ether.h>
45 #include <netinet/ip_ether.h>
46 
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <stddef.h>
52 
53 #include "addrtoname.h"
54 #include "interface.h"
55 #include "extract.h"		    /* must come after interface.h */
56 
57 extern u_short extracted_ethertype;
58 
59 void
60 etherip_print(const u_char *bp, u_int caplen, u_int len, const u_char *bp2)
61 {
62 	const struct ip *ip = (const struct ip *)bp2;
63 	struct ether_header *eh;
64 	const u_char *pbuf = bp;
65 	u_int plen = caplen, hlen;
66 	u_int16_t etype;
67 
68 	if (plen < sizeof(struct etherip_header)) {
69 		printf("[|etherip]");
70 		return;
71 	}
72 
73 	printf("etherip %s > %s ver ", ipaddr_string(&ip->ip_src),
74 	    ipaddr_string(&ip->ip_dst));
75 
76 	switch ((*pbuf) & 0xf) {
77 	case 2:
78 		hlen = 1;
79 		printf("%d", 2);
80 		break;
81 	case 3:
82 		hlen = 2;
83 		printf("%d", 3);
84 		break;
85 	default:
86 		hlen = 0;
87 		printf("unknown");
88 		break;
89 	}
90 	printf(" len %d", len);
91 	if (hlen == 0)
92 		return;
93 
94 	printf(": ");
95 
96 	if (plen < hlen) {
97 		printf("[|etherip]");
98 		return;
99 	}
100 	pbuf += hlen;
101 	plen -= hlen;
102 	len -= hlen;
103 
104 	if (eflag)
105 		ether_print(pbuf, len);
106 	eh = (struct ether_header *)pbuf;
107 	if (plen < sizeof(struct ether_header)) {
108 		printf("[|ether]");
109 		return;
110 	}
111 	etype = EXTRACT_16BITS(pbuf + offsetof(struct ether_header, ether_type));
112 	pbuf += sizeof(struct ether_header);
113 	plen -= sizeof(struct ether_header);
114 	len -= sizeof(struct ether_header);
115 
116 	/* XXX LLC? */
117 	extracted_ethertype = 0;
118 	if (etype <= ETHERMTU) {
119 		if (llc_print(pbuf, len, plen, ESRC(eh), EDST(eh)) == 0) {
120 			if (!eflag)
121 				ether_print((u_char *)eh, len);
122 			if (extracted_ethertype) {
123 				printf("LLC %s",
124 				    etherproto_string(htons(extracted_ethertype)));
125 			}
126 			if (!xflag && !qflag)
127 				default_print(pbuf, plen);
128 		}
129 	} else if (ether_encap_print(etype, pbuf, len, plen) == 0) {
130 		if (!eflag)
131 			ether_print((u_char *)eh, len + sizeof(*eh));
132 		if (!xflag && !qflag)
133 			default_print(pbuf, plen);
134 	}
135 }
136