1 /* $OpenBSD: print-ip6.c,v 1.10 2007/10/04 18:38:29 canacar 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 int clen = snapend - bp; 79 if (clen > snaplen) 80 clen = snaplen; 81 82 if (abuf == NULL) { 83 abuf = (u_char *)malloc(snaplen); 84 if (abuf == NULL) 85 error("ip6_print: malloc"); 86 } 87 memmove((char *)abuf, (char *)ip6, min(length, clen)); 88 snapend = abuf + clen; 89 packetp = abuf; 90 ip6 = (struct ip6_hdr *)abuf; 91 /* We really want libpcap to give us aligned packets */ 92 if (!didwarn) { 93 warning("compensating for unaligned libpcap packets"); 94 ++didwarn; 95 } 96 } 97 98 if ((u_char *)(ip6 + 1) > snapend) { 99 printf("[|ip6]"); 100 return; 101 } 102 if (length < sizeof (struct ip6_hdr)) { 103 (void)printf("truncated-ip6 %d", length); 104 return; 105 } 106 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 107 (void)printf("bad-ip6-version %u", ip6->ip6_vfc >> 4); 108 return; 109 } 110 hlen = sizeof(struct ip6_hdr); 111 112 len = ntohs(ip6->ip6_plen); 113 if (length < len + hlen) 114 (void)printf("truncated-ip6 - %d bytes missing!", 115 len + hlen - length); 116 117 cp = (const u_char *)ip6; 118 nh = ip6->ip6_nxt; 119 while (cp + hlen < snapend) { 120 cp += hlen; 121 122 if (cp == (u_char *)(ip6 + 1) && 123 nh != IPPROTO_TCP && nh != IPPROTO_UDP && 124 nh != IPPROTO_ESP && nh != IPPROTO_AH) { 125 (void)printf("%s > %s: ", ip6addr_string(&ip6->ip6_src), 126 ip6addr_string(&ip6->ip6_dst)); 127 } 128 129 switch (nh) { 130 case IPPROTO_HOPOPTS: 131 hlen = hbhopt_print(cp); 132 nh = *cp; 133 break; 134 case IPPROTO_DSTOPTS: 135 hlen = dstopt_print(cp); 136 nh = *cp; 137 break; 138 case IPPROTO_FRAGMENT: 139 hlen = frag6_print(cp, (const u_char *)ip6); 140 if (snapend <= cp + hlen) 141 goto end; 142 nh = *cp; 143 break; 144 case IPPROTO_ROUTING: 145 hlen = rt6_print(cp, (const u_char *)ip6); 146 nh = *cp; 147 break; 148 case IPPROTO_TCP: 149 tcp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp), 150 (const u_char *)ip6); 151 goto end; 152 case IPPROTO_UDP: 153 udp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp), 154 (const u_char *)ip6); 155 goto end; 156 case IPPROTO_ESP: 157 esp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp), 158 (const u_char *)ip6); 159 goto end; 160 case IPPROTO_AH: 161 ah_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp), 162 (const u_char *)ip6); 163 goto end; 164 case IPPROTO_ICMPV6: 165 icmp6_print(cp, (const u_char *)ip6); 166 goto end; 167 case IPPROTO_PIM: 168 (void)printf("PIM"); 169 pim_print(cp, len); 170 goto end; 171 #ifndef IPPROTO_OSPF 172 #define IPPROTO_OSPF 89 173 #endif 174 case IPPROTO_OSPF: 175 ospf6_print(cp, len); 176 goto end; 177 case IPPROTO_IPV6: 178 ip6_print(cp, len); 179 goto end; 180 #ifndef IPPROTO_IPV4 181 #define IPPROTO_IPV4 4 182 #endif 183 case IPPROTO_IPV4: 184 ip_print(cp, len); 185 goto end; 186 case IPPROTO_NONE: 187 (void)printf("no next header"); 188 goto end; 189 190 #ifndef IPPROTO_CARP 191 #define IPPROTO_CARP 112 192 #endif 193 case IPPROTO_CARP: 194 if (packettype == PT_VRRP) 195 vrrp_print(cp, len, ip6->ip6_hlim); 196 else 197 carp_print(cp, len, ip6->ip6_hlim); 198 break; 199 200 default: 201 (void)printf("ip-proto-%d %d", ip6->ip6_nxt, len); 202 goto end; 203 } 204 if (hlen == 0) 205 break; 206 } 207 208 end: 209 210 flow = ntohl(ip6->ip6_flow); 211 #if 0 212 /* rfc1883 */ 213 if (flow & 0x0f000000) 214 (void)printf(" [pri 0x%x]", (flow & 0x0f000000) >> 24); 215 if (flow & 0x00ffffff) 216 (void)printf(" [flowlabel 0x%x]", flow & 0x00ffffff); 217 #else 218 /* RFC 2460 */ 219 if (flow & 0x0ff00000) 220 (void)printf(" [class 0x%x]", (flow & 0x0ff00000) >> 20); 221 if (flow & 0x000fffff) 222 (void)printf(" [flowlabel 0x%x]", flow & 0x000fffff); 223 #endif 224 225 if (ip6->ip6_hlim <= 1) 226 (void)printf(" [hlim %d]", (int)ip6->ip6_hlim); 227 228 if (vflag) { 229 printf(" ("); 230 (void)printf("len %d", len); 231 if (ip6->ip6_hlim > 1) 232 (void)printf(", hlim %d", (int)ip6->ip6_hlim); 233 printf(")"); 234 } 235 } 236 237 #endif /* INET6 */ 238