Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2011 Daniel Drown |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | * dump.c - print various headers for debugging |
| 17 | */ |
| 18 | #include <stdio.h> |
| 19 | #include <stdint.h> |
| 20 | |
| 21 | #include <arpa/inet.h> |
| 22 | #include <netinet/in.h> |
| 23 | #include <netinet/ip.h> |
| 24 | #include <netinet/ip_icmp.h> |
| 25 | #include <netinet/udp.h> |
| 26 | #include <netinet/tcp.h> |
| 27 | #include <netinet/ip6.h> |
| 28 | #include <netinet/icmp6.h> |
| 29 | #include <linux/icmp.h> |
| 30 | |
| 31 | #include "checksum.h" |
| 32 | #include "clatd.h" |
| 33 | #include "logging.h" |
| 34 | |
| 35 | /* print ip header */ |
| 36 | void dump_ip(struct iphdr *header) { |
| 37 | u_int16_t frag_flags; |
| 38 | char addrstr[INET6_ADDRSTRLEN]; |
| 39 | |
| 40 | frag_flags = ntohs(header->frag_off); |
| 41 | |
| 42 | printf("IP packet\n"); |
| 43 | printf("header_len = %x\n",header->ihl); |
| 44 | printf("version = %x\n",header->version); |
| 45 | printf("tos = %x\n",header->tos); |
| 46 | printf("tot_len = %x\n",ntohs(header->tot_len)); |
| 47 | printf("id = %x\n",ntohs(header->id)); |
| 48 | printf("frag: "); |
| 49 | if(frag_flags & IP_RF) { |
| 50 | printf("(RF) "); |
| 51 | } |
| 52 | if(frag_flags & IP_DF) { |
| 53 | printf("DF "); |
| 54 | } |
| 55 | if(frag_flags & IP_MF) { |
| 56 | printf("MF "); |
| 57 | } |
| 58 | printf("offset = %x\n",frag_flags & IP_OFFMASK); |
| 59 | printf("ttl = %x\n",header->ttl); |
| 60 | printf("protocol = %x\n",header->protocol); |
| 61 | printf("checksum = %x\n",ntohs(header->check)); |
| 62 | inet_ntop(AF_INET, &header->saddr, addrstr, sizeof(addrstr)); |
| 63 | printf("saddr = %s\n",addrstr); |
| 64 | inet_ntop(AF_INET, &header->daddr, addrstr, sizeof(addrstr)); |
| 65 | printf("daddr = %s\n",addrstr); |
| 66 | } |
| 67 | |
| 68 | /* print ip6 header */ |
| 69 | void dump_ip6(struct ip6_hdr *header) { |
| 70 | char addrstr[INET6_ADDRSTRLEN]; |
| 71 | |
| 72 | printf("ipv6\n"); |
| 73 | printf("version = %x\n",header->ip6_vfc >> 4); |
| 74 | printf("traffic class = %x\n",header->ip6_flow >> 20); |
| 75 | printf("flow label = %x\n",ntohl(header->ip6_flow & 0x000fffff)); |
| 76 | printf("payload len = %x\n",ntohs(header->ip6_plen)); |
| 77 | printf("next header = %x\n",header->ip6_nxt); |
| 78 | printf("hop limit = %x\n",header->ip6_hlim); |
| 79 | |
| 80 | inet_ntop(AF_INET6, &header->ip6_src, addrstr, sizeof(addrstr)); |
| 81 | printf("source = %s\n",addrstr); |
| 82 | |
| 83 | inet_ntop(AF_INET6, &header->ip6_dst, addrstr, sizeof(addrstr)); |
| 84 | printf("dest = %s\n",addrstr); |
| 85 | } |
| 86 | |
| 87 | /* print icmp header */ |
| 88 | void dump_icmp(struct icmphdr *icmp) { |
| 89 | printf("ICMP\n"); |
| 90 | |
| 91 | printf("icmp.type = %x ",icmp->type); |
| 92 | if(icmp->type == ICMP_ECHOREPLY) { |
| 93 | printf("echo reply"); |
| 94 | } else if(icmp->type == ICMP_ECHO) { |
| 95 | printf("echo request"); |
| 96 | } else { |
| 97 | printf("other"); |
| 98 | } |
| 99 | printf("\n"); |
| 100 | printf("icmp.code = %x\n",icmp->code); |
| 101 | printf("icmp.checksum = %x\n",ntohs(icmp->checksum)); |
| 102 | if(icmp->type == ICMP_ECHOREPLY || icmp->type == ICMP_ECHO) { |
| 103 | printf("icmp.un.echo.id = %x\n",ntohs(icmp->un.echo.id)); |
| 104 | printf("icmp.un.echo.sequence = %x\n",ntohs(icmp->un.echo.sequence)); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /* print icmp6 header */ |
| 109 | void dump_icmp6(struct icmp6_hdr *icmp6) { |
| 110 | printf("ICMP6\n"); |
| 111 | printf("type = %x",icmp6->icmp6_type); |
| 112 | if(icmp6->icmp6_type == ICMP6_ECHO_REQUEST) { |
| 113 | printf("(echo request)"); |
| 114 | } else if(icmp6->icmp6_type == ICMP6_ECHO_REPLY) { |
| 115 | printf("(echo reply)"); |
| 116 | } |
| 117 | printf("\n"); |
| 118 | printf("code = %x\n",icmp6->icmp6_code); |
| 119 | |
| 120 | printf("checksum = %x\n",icmp6->icmp6_cksum); |
| 121 | |
| 122 | if((icmp6->icmp6_type == ICMP6_ECHO_REQUEST) || (icmp6->icmp6_type == ICMP6_ECHO_REPLY)) { |
| 123 | printf("icmp6_id = %x\n",icmp6->icmp6_id); |
| 124 | printf("icmp6_seq = %x\n",icmp6->icmp6_seq); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /* print udp header */ |
| 129 | void dump_udp_generic(const struct udphdr *udp, uint32_t temp_checksum, const char *payload, size_t payload_size) { |
| 130 | uint16_t my_checksum; |
| 131 | |
| 132 | temp_checksum = ip_checksum_add(temp_checksum, udp, sizeof(struct udphdr)); |
| 133 | temp_checksum = ip_checksum_add(temp_checksum, payload, payload_size); |
| 134 | my_checksum = ip_checksum_finish(temp_checksum); |
| 135 | |
| 136 | printf("UDP\n"); |
| 137 | printf("source = %x\n",ntohs(udp->source)); |
| 138 | printf("dest = %x\n",ntohs(udp->dest)); |
| 139 | printf("len = %x\n",ntohs(udp->len)); |
| 140 | printf("check = %x (mine %x)\n",udp->check,my_checksum); |
| 141 | } |
| 142 | |
| 143 | /* print ipv4/udp header */ |
| 144 | void dump_udp(const struct udphdr *udp, const struct iphdr *ip, const char *payload, size_t payload_size) { |
| 145 | uint32_t temp_checksum; |
| 146 | temp_checksum = ipv4_pseudo_header_checksum(0, ip); |
| 147 | dump_udp_generic(udp, temp_checksum, payload, payload_size); |
| 148 | } |
| 149 | |
| 150 | /* print ipv6/udp header */ |
| 151 | void dump_udp6(const struct udphdr *udp, const struct ip6_hdr *ip6, const char *payload, size_t payload_size) { |
| 152 | uint32_t temp_checksum; |
| 153 | temp_checksum = ipv6_pseudo_header_checksum(0, ip6); |
| 154 | dump_udp_generic(udp, temp_checksum, payload, payload_size); |
| 155 | } |
| 156 | |
| 157 | /* print tcp header */ |
| 158 | void dump_tcp_generic(const struct tcphdr *tcp, const char *options, size_t options_size, uint32_t temp_checksum, const char *payload, size_t payload_size) { |
| 159 | uint16_t my_checksum; |
| 160 | |
| 161 | temp_checksum = ip_checksum_add(temp_checksum, tcp, sizeof(struct tcphdr)); |
| 162 | if(options) { |
| 163 | temp_checksum = ip_checksum_add(temp_checksum, options, options_size); |
| 164 | } |
| 165 | temp_checksum = ip_checksum_add(temp_checksum, payload, payload_size); |
| 166 | my_checksum = ip_checksum_finish(temp_checksum); |
| 167 | |
| 168 | printf("TCP\n"); |
| 169 | printf("source = %x\n",ntohs(tcp->source)); |
| 170 | printf("dest = %x\n",ntohs(tcp->dest)); |
| 171 | printf("seq = %x\n",ntohl(tcp->seq)); |
| 172 | printf("ack = %x\n",ntohl(tcp->ack_seq)); |
| 173 | printf("d_off = %x\n",tcp->doff); |
| 174 | printf("res1 = %x\n",tcp->res1); |
| 175 | #ifdef __BIONIC__ |
| 176 | printf("CWR = %x\n",tcp->cwr); |
| 177 | printf("ECE = %x\n",tcp->ece); |
| 178 | #else |
| 179 | printf("CWR/ECE = %x\n",tcp->res2); |
| 180 | #endif |
| 181 | printf("urg = %x ack = %x psh = %x rst = %x syn = %x fin = %x\n", |
| 182 | tcp->urg, tcp->ack, tcp->psh, tcp->rst, tcp->syn, tcp->fin); |
| 183 | printf("window = %x\n",ntohs(tcp->window)); |
| 184 | printf("check = %x [mine %x]\n",tcp->check,my_checksum); |
| 185 | printf("urgent = %x\n",tcp->urg_ptr); |
| 186 | |
| 187 | if(options) { |
| 188 | size_t i; |
| 189 | |
| 190 | printf("options: "); |
| 191 | for(i=0; i<options_size; i++) { |
| 192 | printf("%x ",*(options+i)); |
| 193 | } |
| 194 | printf("\n"); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /* print ipv4/tcp header */ |
| 199 | void dump_tcp(const struct tcphdr *tcp, const struct iphdr *ip, const char *payload, size_t payload_size, const char *options, size_t options_size) { |
| 200 | uint32_t temp_checksum; |
| 201 | |
| 202 | temp_checksum = ipv4_pseudo_header_checksum(0, ip); |
| 203 | dump_tcp_generic(tcp, options, options_size, temp_checksum, payload, payload_size); |
| 204 | } |
| 205 | |
| 206 | /* print ipv6/tcp header */ |
| 207 | void dump_tcp6(const struct tcphdr *tcp, const struct ip6_hdr *ip6, const char *payload, size_t payload_size, const char *options, size_t options_size) { |
| 208 | uint32_t temp_checksum; |
| 209 | |
| 210 | temp_checksum = ipv6_pseudo_header_checksum(0, ip6); |
| 211 | dump_tcp_generic(tcp, options, options_size, temp_checksum, payload, payload_size); |
| 212 | } |
| 213 | |
| 214 | /* generic hex dump */ |
| 215 | void logcat_hexdump(const char *info, const char *data, size_t len) { |
| 216 | char output[PACKETLEN*2+1]; |
| 217 | size_t i; |
| 218 | |
| 219 | for(i = 0; i < len && i < PACKETLEN; i++) { |
| 220 | snprintf(output + i*2, 3, "%02x", (uint8_t)data[i]); |
| 221 | } |
| 222 | output[len*2+2] = '\0'; |
| 223 | |
| 224 | logmsg(ANDROID_LOG_WARN,"info %s len %d data %s", info, len, output); |
| 225 | } |