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