1 /* $OpenBSD: print-ip6.c,v 1.11 2009/10/27 23:59:55 deraadt 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 #ifdef INET6 25 26 #include <sys/param.h> 27 #include <sys/time.h> 28 #include <sys/types.h> 29 #include <sys/socket.h> 30 31 #include <netinet/in.h> 32 #include <netinet/in_systm.h> 33 #include <netinet/ip.h> 34 #include <netinet/ip_var.h> 35 #include <netinet/udp.h> 36 #include <netinet/udp_var.h> 37 #include <netinet/tcp.h> 38 39 #include <inttypes.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <unistd.h> 44 45 #include "interface.h" 46 #include "addrtoname.h" 47 48 #include <netinet/ip6.h> 49 50 /* 51 * print an IP6 datagram. 52 */ 53 void 54 ip6_print(register const u_char *bp, register int length) 55 { 56 register const struct ip6_hdr *ip6; 57 register int hlen; 58 register int len; 59 register const u_char *cp; 60 int nh; 61 u_int flow; 62 63 ip6 = (const struct ip6_hdr *)bp; 64 65 /* 66 * The IP header is not word aligned, so copy into abuf. 67 * This will never happen with BPF. It does happen with 68 * raw packet dumps from -r. 69 */ 70 if ((intptr_t)ip6 & (sizeof(long)-1)) { 71 static u_char *abuf = NULL; 72 static int didwarn = 0; 73 int clen = snapend - bp; 74 if (clen > snaplen) 75 clen = snaplen; 76 77 if (abuf == NULL) { 78 abuf = (u_char *)malloc(snaplen); 79 if (abuf == NULL) 80 error("ip6_print: malloc"); 81 } 82 memmove((char *)abuf, (char *)ip6, min(length, clen)); 83 snapend = abuf + clen; 84 packetp = abuf; 85 ip6 = (struct ip6_hdr *)abuf; 86 /* We really want libpcap to give us aligned packets */ 87 if (!didwarn) { 88 warning("compensating for unaligned libpcap packets"); 89 ++didwarn; 90 } 91 } 92 93 if ((u_char *)(ip6 + 1) > snapend) { 94 printf("[|ip6]"); 95 return; 96 } 97 if (length < sizeof (struct ip6_hdr)) { 98 (void)printf("truncated-ip6 %d", length); 99 return; 100 } 101 if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { 102 (void)printf("bad-ip6-version %u", ip6->ip6_vfc >> 4); 103 return; 104 } 105 hlen = sizeof(struct ip6_hdr); 106 107 len = ntohs(ip6->ip6_plen); 108 if (length < len + hlen) 109 (void)printf("truncated-ip6 - %d bytes missing!", 110 len + hlen - length); 111 112 cp = (const u_char *)ip6; 113 nh = ip6->ip6_nxt; 114 while (cp + hlen < snapend) { 115 cp += hlen; 116 117 if (cp == (u_char *)(ip6 + 1) && 118 nh != IPPROTO_TCP && nh != IPPROTO_UDP && 119 nh != IPPROTO_ESP && nh != IPPROTO_AH) { 120 (void)printf("%s > %s: ", ip6addr_string(&ip6->ip6_src), 121 ip6addr_string(&ip6->ip6_dst)); 122 } 123 124 switch (nh) { 125 case IPPROTO_HOPOPTS: 126 hlen = hbhopt_print(cp); 127 nh = *cp; 128 break; 129 case IPPROTO_DSTOPTS: 130 hlen = dstopt_print(cp); 131 nh = *cp; 132 break; 133 case IPPROTO_FRAGMENT: 134 hlen = frag6_print(cp, (const u_char *)ip6); 135 if (snapend <= cp + hlen) 136 goto end; 137 nh = *cp; 138 break; 139 case IPPROTO_ROUTING: 140 hlen = rt6_print(cp, (const u_char *)ip6); 141 nh = *cp; 142 break; 143 case IPPROTO_TCP: 144 tcp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp), 145 (const u_char *)ip6); 146 goto end; 147 case IPPROTO_UDP: 148 udp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp), 149 (const u_char *)ip6); 150 goto end; 151 case IPPROTO_ESP: 152 esp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp), 153 (const u_char *)ip6); 154 goto end; 155 case IPPROTO_AH: 156 ah_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp), 157 (const u_char *)ip6); 158 goto end; 159 case IPPROTO_ICMPV6: 160 icmp6_print(cp, (const u_char *)ip6); 161 goto end; 162 case IPPROTO_PIM: 163 (void)printf("PIM"); 164 pim_print(cp, len); 165 goto end; 166 #ifndef IPPROTO_OSPF 167 #define IPPROTO_OSPF 89 168 #endif 169 case IPPROTO_OSPF: 170 ospf6_print(cp, len); 171 goto end; 172 case IPPROTO_IPV6: 173 ip6_print(cp, len); 174 goto end; 175 #ifndef IPPROTO_IPV4 176 #define IPPROTO_IPV4 4 177 #endif 178 case IPPROTO_IPV4: 179 ip_print(cp, len); 180 goto end; 181 case IPPROTO_NONE: 182 (void)printf("no next header"); 183 goto end; 184 185 #ifndef IPPROTO_CARP 186 #define IPPROTO_CARP 112 187 #endif 188 case IPPROTO_CARP: 189 if (packettype == PT_VRRP) 190 vrrp_print(cp, len, ip6->ip6_hlim); 191 else 192 carp_print(cp, len, ip6->ip6_hlim); 193 break; 194 195 default: 196 (void)printf("ip-proto-%d %d", ip6->ip6_nxt, len); 197 goto end; 198 } 199 if (hlen == 0) 200 break; 201 } 202 203 end: 204 205 flow = ntohl(ip6->ip6_flow); 206 #if 0 207 /* rfc1883 */ 208 if (flow & 0x0f000000) 209 (void)printf(" [pri 0x%x]", (flow & 0x0f000000) >> 24); 210 if (flow & 0x00ffffff) 211 (void)printf(" [flowlabel 0x%x]", flow & 0x00ffffff); 212 #else 213 /* RFC 2460 */ 214 if (flow & 0x0ff00000) 215 (void)printf(" [class 0x%x]", (flow & 0x0ff00000) >> 20); 216 if (flow & 0x000fffff) 217 (void)printf(" [flowlabel 0x%x]", flow & 0x000fffff); 218 #endif 219 220 if (ip6->ip6_hlim <= 1) 221 (void)printf(" [hlim %d]", (int)ip6->ip6_hlim); 222 223 if (vflag) { 224 printf(" ("); 225 (void)printf("len %d", len); 226 if (ip6->ip6_hlim > 1) 227 (void)printf(", hlim %d", (int)ip6->ip6_hlim); 228 printf(")"); 229 } 230 } 231 232 #endif /* INET6 */ 233