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