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