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> |
Lorenzo Colitti | d908418 | 2013-03-22 00:42:21 +0900 | [diff] [blame] | 20 | #include <stdlib.h> |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 21 | |
| 22 | #include <arpa/inet.h> |
| 23 | #include <netinet/in.h> |
| 24 | #include <netinet/ip.h> |
| 25 | #include <netinet/ip_icmp.h> |
| 26 | #include <netinet/udp.h> |
| 27 | #include <netinet/tcp.h> |
| 28 | #include <netinet/ip6.h> |
| 29 | #include <netinet/icmp6.h> |
| 30 | #include <linux/icmp.h> |
| 31 | |
Lorenzo Colitti | f68200a | 2013-02-01 10:48:29 +0900 | [diff] [blame] | 32 | #include "debug.h" |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 33 | #include "checksum.h" |
| 34 | #include "clatd.h" |
| 35 | #include "logging.h" |
| 36 | |
Lorenzo Colitti | f68200a | 2013-02-01 10:48:29 +0900 | [diff] [blame] | 37 | #if CLAT_DEBUG |
| 38 | |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 39 | /* print ip header */ |
| 40 | void dump_ip(struct iphdr *header) { |
| 41 | u_int16_t frag_flags; |
| 42 | char addrstr[INET6_ADDRSTRLEN]; |
| 43 | |
| 44 | frag_flags = ntohs(header->frag_off); |
| 45 | |
| 46 | printf("IP packet\n"); |
| 47 | printf("header_len = %x\n",header->ihl); |
| 48 | printf("version = %x\n",header->version); |
| 49 | printf("tos = %x\n",header->tos); |
| 50 | printf("tot_len = %x\n",ntohs(header->tot_len)); |
| 51 | printf("id = %x\n",ntohs(header->id)); |
| 52 | printf("frag: "); |
| 53 | if(frag_flags & IP_RF) { |
| 54 | printf("(RF) "); |
| 55 | } |
| 56 | if(frag_flags & IP_DF) { |
| 57 | printf("DF "); |
| 58 | } |
| 59 | if(frag_flags & IP_MF) { |
| 60 | printf("MF "); |
| 61 | } |
| 62 | printf("offset = %x\n",frag_flags & IP_OFFMASK); |
| 63 | printf("ttl = %x\n",header->ttl); |
| 64 | printf("protocol = %x\n",header->protocol); |
| 65 | printf("checksum = %x\n",ntohs(header->check)); |
| 66 | inet_ntop(AF_INET, &header->saddr, addrstr, sizeof(addrstr)); |
| 67 | printf("saddr = %s\n",addrstr); |
| 68 | inet_ntop(AF_INET, &header->daddr, addrstr, sizeof(addrstr)); |
| 69 | printf("daddr = %s\n",addrstr); |
| 70 | } |
| 71 | |
| 72 | /* print ip6 header */ |
| 73 | void dump_ip6(struct ip6_hdr *header) { |
| 74 | char addrstr[INET6_ADDRSTRLEN]; |
| 75 | |
| 76 | printf("ipv6\n"); |
| 77 | printf("version = %x\n",header->ip6_vfc >> 4); |
| 78 | printf("traffic class = %x\n",header->ip6_flow >> 20); |
| 79 | printf("flow label = %x\n",ntohl(header->ip6_flow & 0x000fffff)); |
| 80 | printf("payload len = %x\n",ntohs(header->ip6_plen)); |
| 81 | printf("next header = %x\n",header->ip6_nxt); |
| 82 | printf("hop limit = %x\n",header->ip6_hlim); |
| 83 | |
| 84 | inet_ntop(AF_INET6, &header->ip6_src, addrstr, sizeof(addrstr)); |
| 85 | printf("source = %s\n",addrstr); |
| 86 | |
| 87 | inet_ntop(AF_INET6, &header->ip6_dst, addrstr, sizeof(addrstr)); |
| 88 | printf("dest = %s\n",addrstr); |
| 89 | } |
| 90 | |
| 91 | /* print icmp header */ |
| 92 | void dump_icmp(struct icmphdr *icmp) { |
| 93 | printf("ICMP\n"); |
| 94 | |
| 95 | printf("icmp.type = %x ",icmp->type); |
| 96 | if(icmp->type == ICMP_ECHOREPLY) { |
| 97 | printf("echo reply"); |
| 98 | } else if(icmp->type == ICMP_ECHO) { |
| 99 | printf("echo request"); |
| 100 | } else { |
| 101 | printf("other"); |
| 102 | } |
| 103 | printf("\n"); |
| 104 | printf("icmp.code = %x\n",icmp->code); |
| 105 | printf("icmp.checksum = %x\n",ntohs(icmp->checksum)); |
| 106 | if(icmp->type == ICMP_ECHOREPLY || icmp->type == ICMP_ECHO) { |
| 107 | printf("icmp.un.echo.id = %x\n",ntohs(icmp->un.echo.id)); |
| 108 | printf("icmp.un.echo.sequence = %x\n",ntohs(icmp->un.echo.sequence)); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /* print icmp6 header */ |
| 113 | void dump_icmp6(struct icmp6_hdr *icmp6) { |
| 114 | printf("ICMP6\n"); |
| 115 | printf("type = %x",icmp6->icmp6_type); |
| 116 | if(icmp6->icmp6_type == ICMP6_ECHO_REQUEST) { |
| 117 | printf("(echo request)"); |
| 118 | } else if(icmp6->icmp6_type == ICMP6_ECHO_REPLY) { |
| 119 | printf("(echo reply)"); |
| 120 | } |
| 121 | printf("\n"); |
| 122 | printf("code = %x\n",icmp6->icmp6_code); |
| 123 | |
| 124 | printf("checksum = %x\n",icmp6->icmp6_cksum); |
| 125 | |
| 126 | if((icmp6->icmp6_type == ICMP6_ECHO_REQUEST) || (icmp6->icmp6_type == ICMP6_ECHO_REPLY)) { |
| 127 | printf("icmp6_id = %x\n",icmp6->icmp6_id); |
| 128 | printf("icmp6_seq = %x\n",icmp6->icmp6_seq); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* print udp header */ |
| 133 | void dump_udp_generic(const struct udphdr *udp, uint32_t temp_checksum, const char *payload, size_t payload_size) { |
| 134 | uint16_t my_checksum; |
| 135 | |
| 136 | temp_checksum = ip_checksum_add(temp_checksum, udp, sizeof(struct udphdr)); |
| 137 | temp_checksum = ip_checksum_add(temp_checksum, payload, payload_size); |
| 138 | my_checksum = ip_checksum_finish(temp_checksum); |
| 139 | |
| 140 | printf("UDP\n"); |
| 141 | printf("source = %x\n",ntohs(udp->source)); |
| 142 | printf("dest = %x\n",ntohs(udp->dest)); |
| 143 | printf("len = %x\n",ntohs(udp->len)); |
| 144 | printf("check = %x (mine %x)\n",udp->check,my_checksum); |
| 145 | } |
| 146 | |
| 147 | /* print ipv4/udp header */ |
| 148 | void dump_udp(const struct udphdr *udp, const struct iphdr *ip, const char *payload, size_t payload_size) { |
| 149 | uint32_t temp_checksum; |
Lorenzo Colitti | 07f0265 | 2014-02-20 14:28:43 +0900 | [diff] [blame] | 150 | temp_checksum = ipv4_pseudo_header_checksum(ip, sizeof(*udp) + payload_size); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 151 | dump_udp_generic(udp, temp_checksum, payload, payload_size); |
| 152 | } |
| 153 | |
| 154 | /* print ipv6/udp header */ |
| 155 | void dump_udp6(const struct udphdr *udp, const struct ip6_hdr *ip6, const char *payload, size_t payload_size) { |
| 156 | uint32_t temp_checksum; |
Lorenzo Colitti | 07f0265 | 2014-02-20 14:28:43 +0900 | [diff] [blame] | 157 | temp_checksum = ipv6_pseudo_header_checksum(ip6, sizeof(*udp) + payload_size, IPPROTO_UDP); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 158 | dump_udp_generic(udp, temp_checksum, payload, payload_size); |
| 159 | } |
| 160 | |
| 161 | /* print tcp header */ |
| 162 | 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) { |
| 163 | uint16_t my_checksum; |
| 164 | |
| 165 | temp_checksum = ip_checksum_add(temp_checksum, tcp, sizeof(struct tcphdr)); |
| 166 | if(options) { |
| 167 | temp_checksum = ip_checksum_add(temp_checksum, options, options_size); |
| 168 | } |
| 169 | temp_checksum = ip_checksum_add(temp_checksum, payload, payload_size); |
| 170 | my_checksum = ip_checksum_finish(temp_checksum); |
| 171 | |
| 172 | printf("TCP\n"); |
| 173 | printf("source = %x\n",ntohs(tcp->source)); |
| 174 | printf("dest = %x\n",ntohs(tcp->dest)); |
| 175 | printf("seq = %x\n",ntohl(tcp->seq)); |
| 176 | printf("ack = %x\n",ntohl(tcp->ack_seq)); |
| 177 | printf("d_off = %x\n",tcp->doff); |
| 178 | printf("res1 = %x\n",tcp->res1); |
| 179 | #ifdef __BIONIC__ |
| 180 | printf("CWR = %x\n",tcp->cwr); |
| 181 | printf("ECE = %x\n",tcp->ece); |
| 182 | #else |
| 183 | printf("CWR/ECE = %x\n",tcp->res2); |
| 184 | #endif |
| 185 | printf("urg = %x ack = %x psh = %x rst = %x syn = %x fin = %x\n", |
| 186 | tcp->urg, tcp->ack, tcp->psh, tcp->rst, tcp->syn, tcp->fin); |
| 187 | printf("window = %x\n",ntohs(tcp->window)); |
| 188 | printf("check = %x [mine %x]\n",tcp->check,my_checksum); |
| 189 | printf("urgent = %x\n",tcp->urg_ptr); |
| 190 | |
| 191 | if(options) { |
| 192 | size_t i; |
| 193 | |
| 194 | printf("options: "); |
| 195 | for(i=0; i<options_size; i++) { |
| 196 | printf("%x ",*(options+i)); |
| 197 | } |
| 198 | printf("\n"); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /* print ipv4/tcp header */ |
| 203 | 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) { |
| 204 | uint32_t temp_checksum; |
| 205 | |
Lorenzo Colitti | 07f0265 | 2014-02-20 14:28:43 +0900 | [diff] [blame] | 206 | temp_checksum = ipv4_pseudo_header_checksum(ip, sizeof(*tcp) + options_size + payload_size); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 207 | dump_tcp_generic(tcp, options, options_size, temp_checksum, payload, payload_size); |
| 208 | } |
| 209 | |
| 210 | /* print ipv6/tcp header */ |
| 211 | 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) { |
| 212 | uint32_t temp_checksum; |
| 213 | |
Lorenzo Colitti | 07f0265 | 2014-02-20 14:28:43 +0900 | [diff] [blame] | 214 | temp_checksum = ipv6_pseudo_header_checksum(ip6, sizeof(*tcp) + options_size + payload_size, IPPROTO_TCP); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 215 | dump_tcp_generic(tcp, options, options_size, temp_checksum, payload, payload_size); |
| 216 | } |
| 217 | |
| 218 | /* generic hex dump */ |
| 219 | void logcat_hexdump(const char *info, const char *data, size_t len) { |
Lorenzo Colitti | f913fe4 | 2013-04-08 18:30:55 +0900 | [diff] [blame] | 220 | char output[PACKETLEN*3+2]; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 221 | size_t i; |
| 222 | |
Lorenzo Colitti | 57d480d | 2014-02-09 10:35:38 +0900 | [diff] [blame^] | 223 | output[0] = '\0'; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 224 | for(i = 0; i < len && i < PACKETLEN; i++) { |
Lorenzo Colitti | f913fe4 | 2013-04-08 18:30:55 +0900 | [diff] [blame] | 225 | snprintf(output + i*3, 4, " %02x", (uint8_t)data[i]); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 226 | } |
Lorenzo Colitti | f913fe4 | 2013-04-08 18:30:55 +0900 | [diff] [blame] | 227 | output[len*3+3] = '\0'; |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 228 | |
Lorenzo Colitti | f913fe4 | 2013-04-08 18:30:55 +0900 | [diff] [blame] | 229 | logmsg(ANDROID_LOG_WARN,"info %s len %d data%s", info, len, output); |
Daniel Drown | a45056e | 2012-03-23 10:42:54 -0500 | [diff] [blame] | 230 | } |
Lorenzo Colitti | f68200a | 2013-02-01 10:48:29 +0900 | [diff] [blame] | 231 | |
Lorenzo Colitti | f913fe4 | 2013-04-08 18:30:55 +0900 | [diff] [blame] | 232 | void dump_iovec(const struct iovec *iov, int iov_len) { |
| 233 | int i; |
| 234 | char *str; |
| 235 | for (i = 0; i < iov_len; i++) { |
| 236 | asprintf(&str, "iov[%d]: ", i); |
| 237 | logcat_hexdump(str, iov[i].iov_base, iov[i].iov_len); |
| 238 | free(str); |
| 239 | } |
| 240 | } |
Lorenzo Colitti | f68200a | 2013-02-01 10:48:29 +0900 | [diff] [blame] | 241 | #endif // CLAT_DEBUG |