1 /* $OpenBSD: print-etherip.c,v 1.1 2001/02/05 15:18:47 jason 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 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by Jason L. Wright 18 * 4. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * Format and print etherip packets 36 */ 37 38 #ifndef lint 39 static const char rcsid[] = 40 "@(#) $Header: /home/cvs/src/usr.sbin/tcpdump/print-etherip.c,v 1.1 2001/02/05 15:18:47 jason Exp $"; 41 #endif 42 43 #include <sys/param.h> 44 #include <sys/time.h> 45 #include <sys/socket.h> 46 47 #include <net/if.h> 48 #include <netinet/in.h> 49 #include <netinet/in_systm.h> 50 #include <netinet/ip.h> 51 #include <netinet/ip_var.h> 52 #include <netinet/udp.h> 53 #include <netinet/udp_var.h> 54 #include <netinet/tcp.h> 55 #include <netinet/tcpip.h> 56 #include <netinet/if_ether.h> 57 #include <netinet/ip_ether.h> 58 59 #include <stdio.h> 60 #include <stdlib.h> 61 #include <string.h> 62 #include <unistd.h> 63 #include <stddef.h> 64 65 #include "addrtoname.h" 66 #include "interface.h" 67 #include "extract.h" /* must come after interface.h */ 68 69 extern u_short extracted_ethertype; 70 71 void 72 etherip_print(bp, len, bp2) 73 register const u_char *bp, *bp2; 74 register u_int len; 75 { 76 const struct ip *ip = (const struct ip *)bp2; 77 struct ether_header *eh; 78 const u_char *pbuf = bp; 79 u_int plen = len, hlen; 80 u_int16_t etype; 81 82 if (plen < sizeof(struct etherip_header)) { 83 printf("[|etherip]"); 84 return; 85 } 86 87 printf("etherip %s > %s ver ", ipaddr_string(&ip->ip_src), 88 ipaddr_string(&ip->ip_dst)); 89 90 switch ((*pbuf) & 0xf) { 91 case 2: 92 hlen = 1; 93 printf("%d", 2); 94 break; 95 case 3: 96 hlen = 2; 97 printf("%d", 3); 98 break; 99 default: 100 hlen = 0; 101 printf("unknown"); 102 break; 103 } 104 printf(" len %d", len); 105 if (hlen == 0) 106 return; 107 108 printf(": "); 109 110 pbuf += hlen; 111 plen -= hlen; 112 113 if (eflag) 114 ether_print(pbuf, plen); 115 eh = (struct ether_header *)pbuf; 116 etype = EXTRACT_16BITS(pbuf + offsetof(struct ether_header, ether_type)); 117 pbuf += sizeof(struct ether_header); 118 plen -= sizeof(struct ether_header); 119 120 /* XXX LLC? */ 121 extracted_ethertype = 0; 122 if (etype <= ETHERMTU) { 123 if (llc_print(pbuf, plen, plen, ESRC(eh), EDST(eh)) == 0) { 124 if (!eflag) 125 ether_print((u_char *)eh, plen); 126 if (extracted_ethertype) { 127 printf("LLC %s", 128 etherproto_string(htons(extracted_ethertype))); 129 } 130 if (!xflag && !qflag) 131 default_print(pbuf, plen); 132 } 133 } else if (ether_encap_print(etype, pbuf, plen, plen) == 0) { 134 if (!eflag) 135 ether_print((u_char *)eh, plen + sizeof(*eh)); 136 if (!xflag && !qflag) 137 default_print(pbuf, plen); 138 } 139 } 140