1 /* $OpenBSD: print-ip6.c,v 1.5 2004/02/04 08:35:12 otto 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 hlen = sizeof(struct ip6_hdr); 104 105 len = ntohs(ip6->ip6_plen); 106 if (length < len + hlen) 107 (void)printf("truncated-ip6 - %d bytes missing!", 108 len + hlen - length); 109 110 cp = (const u_char *)ip6; 111 nh = ip6->ip6_nxt; 112 while (cp < snapend) { 113 cp += hlen; 114 115 if (cp == (u_char *)(ip6 + 1) 116 && nh != IPPROTO_TCP && nh != IPPROTO_UDP) { 117 (void)printf("%s > %s: ", ip6addr_string(&ip6->ip6_src), 118 ip6addr_string(&ip6->ip6_dst)); 119 } 120 121 switch (nh) { 122 case IPPROTO_HOPOPTS: 123 hlen = hbhopt_print(cp); 124 nh = *cp; 125 break; 126 case IPPROTO_DSTOPTS: 127 hlen = dstopt_print(cp); 128 nh = *cp; 129 break; 130 case IPPROTO_FRAGMENT: 131 hlen = frag6_print(cp, (const u_char *)ip6); 132 if (snapend <= cp + hlen) 133 goto end; 134 nh = *cp; 135 break; 136 case IPPROTO_ROUTING: 137 hlen = rt6_print(cp, (const u_char *)ip6); 138 nh = *cp; 139 break; 140 case IPPROTO_TCP: 141 tcp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp), 142 (const u_char *)ip6); 143 goto end; 144 case IPPROTO_UDP: 145 udp_print(cp, len + sizeof(struct ip6_hdr) - (cp - bp), 146 (const u_char *)ip6); 147 goto end; 148 case IPPROTO_ICMPV6: 149 icmp6_print(cp, (const u_char *)ip6); 150 goto end; 151 case IPPROTO_PIM: 152 (void)printf("PIM"); 153 pim_print(cp, len); 154 goto end; 155 #ifndef IPPROTO_OSPF 156 #define IPPROTO_OSPF 89 157 #endif 158 case IPPROTO_OSPF: 159 ospf6_print(cp, len); 160 goto end; 161 case IPPROTO_IPV6: 162 ip6_print(cp, len); 163 goto end; 164 #ifndef IPPROTO_IPV4 165 #define IPPROTO_IPV4 4 166 #endif 167 case IPPROTO_IPV4: 168 ip_print(cp, len); 169 goto end; 170 case IPPROTO_NONE: 171 (void)printf("no next header"); 172 goto end; 173 174 default: 175 (void)printf("ip-proto-%d %d", ip6->ip6_nxt, len); 176 goto end; 177 } 178 if (hlen == 0) 179 break; 180 } 181 182 end: 183 184 flow = ntohl(ip6->ip6_flow); 185 #if 0 186 /* rfc1883 */ 187 if (flow & 0x0f000000) 188 (void)printf(" [pri 0x%x]", (flow & 0x0f000000) >> 24); 189 if (flow & 0x00ffffff) 190 (void)printf(" [flowlabel 0x%x]", flow & 0x00ffffff); 191 #else 192 /* RFC 2460 */ 193 if (flow & 0x0ff00000) 194 (void)printf(" [class 0x%x]", (flow & 0x0ff00000) >> 20); 195 if (flow & 0x000fffff) 196 (void)printf(" [flowlabel 0x%x]", flow & 0x000fffff); 197 #endif 198 199 if (ip6->ip6_hlim <= 1) 200 (void)printf(" [hlim %d]", (int)ip6->ip6_hlim); 201 202 if (vflag) { 203 printf(" ("); 204 (void)printf("len %d", len); 205 if (ip6->ip6_hlim > 1) 206 (void)printf(", hlim %d", (int)ip6->ip6_hlim); 207 printf(")"); 208 } 209 } 210 211 #endif /* INET6 */ 212