1 /* $OpenBSD: print-ip6.c,v 1.7 2006/09/19 14:25:04 naddy Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that: (1) source code distributions 9 * retain the above copyright notice and this paragraph in its entirety, (2) 10 * distributions including binary code include the above copyright notice and 11 * this paragraph in its entirety in the documentation or other materials 12 * provided with the distribution, and (3) all advertising materials mentioning 13 * features or use of this software display the following acknowledgement: 14 * ``This product includes software developed by the University of California, 15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 16 * the University nor the names of its contributors may be used to endorse 17 * or promote products derived from this software without specific prior 18 * written permission. 19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 22 */ 23 24 #ifndef lint 25 static const char rcsid[] = 26 "@(#) /master/usr.sbin/tcpdump/tcpdump/print-ip.c,v 2.1 1995/02/03 18:14:45 polk Exp (LBL)"; 27 #endif 28 29 #ifdef INET6 30 31 #include <sys/param.h> 32 #include <sys/time.h> 33 #include <sys/types.h> 34 #include <sys/socket.h> 35 36 #include <netinet/in.h> 37 #include <netinet/in_systm.h> 38 #include <netinet/ip.h> 39 #include <netinet/ip_var.h> 40 #include <netinet/udp.h> 41 #include <netinet/udp_var.h> 42 #include <netinet/tcp.h> 43 44 #include <inttypes.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <unistd.h> 49 50 #include "interface.h" 51 #include "addrtoname.h" 52 53 #include <netinet/ip6.h> 54 55 /* 56 * print an IP6 datagram. 57 */ 58 void 59 ip6_print(register const u_char *bp, register int length) 60 { 61 register const struct ip6_hdr *ip6; 62 register int hlen; 63 register int len; 64 register const u_char *cp; 65 int nh; 66 u_int flow; 67 68 ip6 = (const struct ip6_hdr *)bp; 69 70 /* 71 * The IP header is not word aligned, so copy into abuf. 72 * This will never happen with BPF. It does happen with 73 * raw packet dumps from -r. 74 */ 75 if ((intptr_t)ip6 & (sizeof(long)-1)) { 76 static u_char *abuf = NULL; 77 static int didwarn = 0; 78 79 if (abuf == NULL) { 80 abuf = (u_char *)malloc(snaplen); 81 if (abuf == NULL) 82 error("ip6_print: malloc"); 83 } 84 memcpy((char *)abuf, (char *)ip6, min(length, snaplen)); 85 snapend += abuf - (u_char *)ip6; 86 packetp = abuf; 87 ip6 = (struct ip6_hdr *)abuf; 88 /* We really want libpcap to give us aligned packets */ 89 if (!didwarn) { 90 warning("compensating for unaligned libpcap packets"); 91 ++didwarn; 92 } 93 } 94 95 if ((u_char *)(ip6 + 1) > snapend) { 96 printf("[|ip6]"); 97 return; 98 } 99 if (length < sizeof (struct ip6_hdr)) { 100 (void)printf("truncated-ip6 %d", length); 101 return; 102 } 103 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 104 (void)printf("bad-ip6-version %u", ip6->ip6_vfc >> 4); 105 return; 106 } 107 hlen = sizeof(struct ip6_hdr); 108 109 len = ntohs(ip6->ip6_plen); 110 if (length < len + hlen) 111 (void)printf("truncated-ip6 - %d bytes missing!", 112 len + hlen - length); 113 114 cp = (const u_char *)ip6; 115 nh = ip6->ip6_nxt; 116 while (cp < snapend) { 117 cp += hlen; 118 119 if (cp == (u_char *)(ip6 + 1) && 120 nh != IPPROTO_TCP && nh != IPPROTO_UDP && 121 nh != IPPROTO_ESP && nh != IPPROTO_AH) { 122 (void)printf("%s > %s: ", ip6addr_string(&ip6->ip6_src), 123 ip6addr_string(&ip6->ip6_dst)); 124 } 125 126 switch (nh) { 127 case IPPROTO_HOPOPTS: 128 hlen = hbhopt_print(cp); 129 nh = *cp; 130 break; 131 case IPPROTO_DSTOPTS: 132 hlen = dstopt_print(cp); 133 nh = *cp; 134 break; 135 case IPPROTO_FRAGMENT: 136 hlen = frag6_print(cp, (const u_char *)ip6); 137 if (snapend <= cp + hlen) 138 goto end; 139 nh = *cp; 140 break; 141 case IPPROTO_ROUTING: 142 hlen = rt6_print(cp, (const u_char *)ip6); 143 nh = *cp; 144 break; 145 case IPPROTO_TCP: 146 tcp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp), 147 (const u_char *)ip6); 148 goto end; 149 case IPPROTO_UDP: 150 udp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp), 151 (const u_char *)ip6); 152 goto end; 153 case IPPROTO_ESP: 154 esp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp), 155 (const u_char *)ip6); 156 goto end; 157 case IPPROTO_AH: 158 ah_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp), 159 (const u_char *)ip6); 160 goto end; 161 case IPPROTO_ICMPV6: 162 icmp6_print(cp, (const u_char *)ip6); 163 goto end; 164 case IPPROTO_PIM: 165 (void)printf("PIM"); 166 pim_print(cp, len); 167 goto end; 168 #ifndef IPPROTO_OSPF 169 #define IPPROTO_OSPF 89 170 #endif 171 case IPPROTO_OSPF: 172 ospf6_print(cp, len); 173 goto end; 174 case IPPROTO_IPV6: 175 ip6_print(cp, len); 176 goto end; 177 #ifndef IPPROTO_IPV4 178 #define IPPROTO_IPV4 4 179 #endif 180 case IPPROTO_IPV4: 181 ip_print(cp, len); 182 goto end; 183 case IPPROTO_NONE: 184 (void)printf("no next header"); 185 goto end; 186 187 default: 188 (void)printf("ip-proto-%d %d", ip6->ip6_nxt, len); 189 goto end; 190 } 191 if (hlen == 0) 192 break; 193 } 194 195 end: 196 197 flow = ntohl(ip6->ip6_flow); 198 #if 0 199 /* rfc1883 */ 200 if (flow & 0x0f000000) 201 (void)printf(" [pri 0x%x]", (flow & 0x0f000000) >> 24); 202 if (flow & 0x00ffffff) 203 (void)printf(" [flowlabel 0x%x]", flow & 0x00ffffff); 204 #else 205 /* RFC 2460 */ 206 if (flow & 0x0ff00000) 207 (void)printf(" [class 0x%x]", (flow & 0x0ff00000) >> 20); 208 if (flow & 0x000fffff) 209 (void)printf(" [flowlabel 0x%x]", flow & 0x000fffff); 210 #endif 211 212 if (ip6->ip6_hlim <= 1) 213 (void)printf(" [hlim %d]", (int)ip6->ip6_hlim); 214 215 if (vflag) { 216 printf(" ("); 217 (void)printf("len %d", len); 218 if (ip6->ip6_hlim > 1) 219 (void)printf(", hlim %d", (int)ip6->ip6_hlim); 220 printf(")"); 221 } 222 } 223 224 #endif /* INET6 */ 225