1 /* $OpenBSD: print-etherip.c,v 1.5 2007/10/07 16:41:05 deraadt 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 "@(#) $Id: print-etherip.c,v 1.5 2007/10/07 16:41:05 deraadt 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(const u_char *bp, u_int caplen, u_int len, const u_char *bp2) 68 { 69 const struct ip *ip = (const struct ip *)bp2; 70 struct ether_header *eh; 71 const u_char *pbuf = bp; 72 u_int plen = caplen, hlen; 73 u_int16_t etype; 74 75 if (plen < sizeof(struct etherip_header)) { 76 printf("[|etherip]"); 77 return; 78 } 79 80 printf("etherip %s > %s ver ", ipaddr_string(&ip->ip_src), 81 ipaddr_string(&ip->ip_dst)); 82 83 switch ((*pbuf) & 0xf) { 84 case 2: 85 hlen = 1; 86 printf("%d", 2); 87 break; 88 case 3: 89 hlen = 2; 90 printf("%d", 3); 91 break; 92 default: 93 hlen = 0; 94 printf("unknown"); 95 break; 96 } 97 printf(" len %d", len); 98 if (hlen == 0) 99 return; 100 101 printf(": "); 102 103 if (plen < hlen) { 104 printf("[|etherip]"); 105 return; 106 } 107 pbuf += hlen; 108 plen -= hlen; 109 len -= hlen; 110 111 if (eflag) 112 ether_print(pbuf, len); 113 eh = (struct ether_header *)pbuf; 114 if (plen < sizeof(struct ether_header)) { 115 printf("[|ether]"); 116 return; 117 } 118 etype = EXTRACT_16BITS(pbuf + offsetof(struct ether_header, ether_type)); 119 pbuf += sizeof(struct ether_header); 120 plen -= sizeof(struct ether_header); 121 len -= sizeof(struct ether_header); 122 123 /* XXX LLC? */ 124 extracted_ethertype = 0; 125 if (etype <= ETHERMTU) { 126 if (llc_print(pbuf, len, plen, ESRC(eh), EDST(eh)) == 0) { 127 if (!eflag) 128 ether_print((u_char *)eh, len); 129 if (extracted_ethertype) { 130 printf("LLC %s", 131 etherproto_string(htons(extracted_ethertype))); 132 } 133 if (!xflag && !qflag) 134 default_print(pbuf, plen); 135 } 136 } else if (ether_encap_print(etype, pbuf, len, plen) == 0) { 137 if (!eflag) 138 ether_print((u_char *)eh, len + sizeof(*eh)); 139 if (!xflag && !qflag) 140 default_print(pbuf, plen); 141 } 142 } 143