1*c97d4a25Sdlg /* $OpenBSD: print-etherip.c,v 1.10 2018/02/10 10:00:32 dlg Exp $ */
2284e04e3Sjason
3284e04e3Sjason /*
4284e04e3Sjason * Copyright (c) 2001 Jason L. Wright (jason@thought.net)
5284e04e3Sjason * All rights reserved.
6284e04e3Sjason *
7284e04e3Sjason * Redistribution and use in source and binary forms, with or without
8284e04e3Sjason * modification, are permitted provided that the following conditions
9284e04e3Sjason * are met:
10284e04e3Sjason * 1. Redistributions of source code must retain the above copyright
11284e04e3Sjason * notice, this list of conditions and the following disclaimer.
12284e04e3Sjason * 2. Redistributions in binary form must reproduce the above copyright
13284e04e3Sjason * notice, this list of conditions and the following disclaimer in the
14284e04e3Sjason * documentation and/or other materials provided with the distribution.
15284e04e3Sjason *
16284e04e3Sjason * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17284e04e3Sjason * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18284e04e3Sjason * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19284e04e3Sjason * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20284e04e3Sjason * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21284e04e3Sjason * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22284e04e3Sjason * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23284e04e3Sjason * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24284e04e3Sjason * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25284e04e3Sjason * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26284e04e3Sjason * POSSIBILITY OF SUCH DAMAGE.
27284e04e3Sjason */
28284e04e3Sjason
29284e04e3Sjason /*
30284e04e3Sjason * Format and print etherip packets
31284e04e3Sjason */
32284e04e3Sjason
33284e04e3Sjason #include <sys/time.h>
34284e04e3Sjason #include <sys/socket.h>
35284e04e3Sjason
36284e04e3Sjason #include <net/if.h>
37284e04e3Sjason #include <netinet/in.h>
38284e04e3Sjason #include <netinet/ip.h>
39284e04e3Sjason #include <netinet/ip_var.h>
40284e04e3Sjason #include <netinet/udp.h>
41284e04e3Sjason #include <netinet/udp_var.h>
42284e04e3Sjason #include <netinet/tcp.h>
43284e04e3Sjason #include <netinet/if_ether.h>
44284e04e3Sjason #include <netinet/ip_ether.h>
45284e04e3Sjason
46284e04e3Sjason #include <stdio.h>
47284e04e3Sjason #include <stdlib.h>
48284e04e3Sjason #include <string.h>
49284e04e3Sjason #include <unistd.h>
50284e04e3Sjason #include <stddef.h>
51284e04e3Sjason
52284e04e3Sjason #include "addrtoname.h"
53284e04e3Sjason #include "interface.h"
54284e04e3Sjason #include "extract.h" /* must come after interface.h */
55284e04e3Sjason
56284e04e3Sjason extern u_short extracted_ethertype;
57284e04e3Sjason
58284e04e3Sjason void
etherip_print(const u_char * bp,u_int caplen,u_int len)59*c97d4a25Sdlg etherip_print(const u_char *bp, u_int caplen, u_int len)
60284e04e3Sjason {
61284e04e3Sjason struct ether_header *eh;
62284e04e3Sjason const u_char *pbuf = bp;
63e3fcc785Smoritz u_int plen = caplen, hlen;
64284e04e3Sjason u_int16_t etype;
65284e04e3Sjason
66*c97d4a25Sdlg printf("etherip ");
67*c97d4a25Sdlg
68284e04e3Sjason if (plen < sizeof(struct etherip_header)) {
69284e04e3Sjason printf("[|etherip]");
70284e04e3Sjason return;
71284e04e3Sjason }
72284e04e3Sjason
7399350b59Sjca switch (*pbuf >> 4) {
74284e04e3Sjason case 2:
75284e04e3Sjason hlen = 1;
76284e04e3Sjason printf("%d", 2);
77284e04e3Sjason break;
78284e04e3Sjason case 3:
79284e04e3Sjason hlen = 2;
80284e04e3Sjason printf("%d", 3);
81284e04e3Sjason break;
82284e04e3Sjason default:
83284e04e3Sjason hlen = 0;
84284e04e3Sjason printf("unknown");
85284e04e3Sjason break;
86284e04e3Sjason }
87284e04e3Sjason printf(" len %d", len);
88284e04e3Sjason if (hlen == 0)
89284e04e3Sjason return;
90284e04e3Sjason
91284e04e3Sjason printf(": ");
92284e04e3Sjason
93e3fcc785Smoritz if (plen < hlen) {
94e3fcc785Smoritz printf("[|etherip]");
95e3fcc785Smoritz return;
96e3fcc785Smoritz }
97284e04e3Sjason pbuf += hlen;
98284e04e3Sjason plen -= hlen;
99e3fcc785Smoritz len -= hlen;
100284e04e3Sjason
101284e04e3Sjason if (eflag)
102e3fcc785Smoritz ether_print(pbuf, len);
103284e04e3Sjason eh = (struct ether_header *)pbuf;
104e3fcc785Smoritz if (plen < sizeof(struct ether_header)) {
105e3fcc785Smoritz printf("[|ether]");
106e3fcc785Smoritz return;
107e3fcc785Smoritz }
108284e04e3Sjason etype = EXTRACT_16BITS(pbuf + offsetof(struct ether_header, ether_type));
109284e04e3Sjason pbuf += sizeof(struct ether_header);
110284e04e3Sjason plen -= sizeof(struct ether_header);
111e3fcc785Smoritz len -= sizeof(struct ether_header);
112284e04e3Sjason
113284e04e3Sjason /* XXX LLC? */
114284e04e3Sjason extracted_ethertype = 0;
115284e04e3Sjason if (etype <= ETHERMTU) {
116e3fcc785Smoritz if (llc_print(pbuf, len, plen, ESRC(eh), EDST(eh)) == 0) {
117284e04e3Sjason if (!eflag)
118e3fcc785Smoritz ether_print((u_char *)eh, len);
119284e04e3Sjason if (extracted_ethertype) {
120284e04e3Sjason printf("LLC %s",
121284e04e3Sjason etherproto_string(htons(extracted_ethertype)));
122284e04e3Sjason }
123284e04e3Sjason if (!xflag && !qflag)
124284e04e3Sjason default_print(pbuf, plen);
125284e04e3Sjason }
126e3fcc785Smoritz } else if (ether_encap_print(etype, pbuf, len, plen) == 0) {
127284e04e3Sjason if (!eflag)
128e3fcc785Smoritz ether_print((u_char *)eh, len + sizeof(*eh));
129284e04e3Sjason if (!xflag && !qflag)
130284e04e3Sjason default_print(pbuf, plen);
131284e04e3Sjason }
132284e04e3Sjason }
133