blob: 7332d6c652786dc1ec0bf9141b6cd1c63ae7e1f3 [file] [log] [blame]
Daniel Drowna45056e2012-03-23 10:42:54 -05001/*
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 */
Daniel Drowna45056e2012-03-23 10:42:54 -050018#include <stdint.h>
junyulaic4e591a2018-11-26 22:36:10 +090019#include <stdio.h>
Lorenzo Colittid9084182013-03-22 00:42:21 +090020#include <stdlib.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050021
22#include <arpa/inet.h>
junyulaic4e591a2018-11-26 22:36:10 +090023#include <linux/icmp.h>
Rocco Yuedf338692020-10-19 19:59:38 +080024#include <linux/if_tun.h>
junyulaic4e591a2018-11-26 22:36:10 +090025#include <netinet/icmp6.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050026#include <netinet/in.h>
27#include <netinet/ip.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050028#include <netinet/ip6.h>
junyulaic4e591a2018-11-26 22:36:10 +090029#include <netinet/ip_icmp.h>
30#include <netinet/tcp.h>
31#include <netinet/udp.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050032
Lorenzo Colitti98de5952019-01-20 11:45:03 +090033#include "netutils/checksum.h"
34
Daniel Drowna45056e2012-03-23 10:42:54 -050035#include "clatd.h"
junyulaic4e591a2018-11-26 22:36:10 +090036#include "debug.h"
Rocco Yuedf338692020-10-19 19:59:38 +080037#include "dump.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050038#include "logging.h"
39
Lorenzo Colittif68200a2013-02-01 10:48:29 +090040#if CLAT_DEBUG
41
Daniel Drowna45056e2012-03-23 10:42:54 -050042/* print ip header */
43void dump_ip(struct iphdr *header) {
44 u_int16_t frag_flags;
45 char addrstr[INET6_ADDRSTRLEN];
46
47 frag_flags = ntohs(header->frag_off);
48
49 printf("IP packet\n");
junyulaic4e591a2018-11-26 22:36:10 +090050 printf("header_len = %x\n", header->ihl);
51 printf("version = %x\n", header->version);
52 printf("tos = %x\n", header->tos);
53 printf("tot_len = %x\n", ntohs(header->tot_len));
54 printf("id = %x\n", ntohs(header->id));
Daniel Drowna45056e2012-03-23 10:42:54 -050055 printf("frag: ");
junyulaic4e591a2018-11-26 22:36:10 +090056 if (frag_flags & IP_RF) {
Daniel Drowna45056e2012-03-23 10:42:54 -050057 printf("(RF) ");
58 }
junyulaic4e591a2018-11-26 22:36:10 +090059 if (frag_flags & IP_DF) {
Daniel Drowna45056e2012-03-23 10:42:54 -050060 printf("DF ");
61 }
junyulaic4e591a2018-11-26 22:36:10 +090062 if (frag_flags & IP_MF) {
Daniel Drowna45056e2012-03-23 10:42:54 -050063 printf("MF ");
64 }
junyulaic4e591a2018-11-26 22:36:10 +090065 printf("offset = %x\n", frag_flags & IP_OFFMASK);
66 printf("ttl = %x\n", header->ttl);
67 printf("protocol = %x\n", header->protocol);
68 printf("checksum = %x\n", ntohs(header->check));
Daniel Drowna45056e2012-03-23 10:42:54 -050069 inet_ntop(AF_INET, &header->saddr, addrstr, sizeof(addrstr));
junyulaic4e591a2018-11-26 22:36:10 +090070 printf("saddr = %s\n", addrstr);
Daniel Drowna45056e2012-03-23 10:42:54 -050071 inet_ntop(AF_INET, &header->daddr, addrstr, sizeof(addrstr));
junyulaic4e591a2018-11-26 22:36:10 +090072 printf("daddr = %s\n", addrstr);
Daniel Drowna45056e2012-03-23 10:42:54 -050073}
74
75/* print ip6 header */
76void dump_ip6(struct ip6_hdr *header) {
77 char addrstr[INET6_ADDRSTRLEN];
78
79 printf("ipv6\n");
junyulaic4e591a2018-11-26 22:36:10 +090080 printf("version = %x\n", header->ip6_vfc >> 4);
81 printf("traffic class = %x\n", header->ip6_flow >> 20);
82 printf("flow label = %x\n", ntohl(header->ip6_flow & 0x000fffff));
83 printf("payload len = %x\n", ntohs(header->ip6_plen));
84 printf("next header = %x\n", header->ip6_nxt);
85 printf("hop limit = %x\n", header->ip6_hlim);
Daniel Drowna45056e2012-03-23 10:42:54 -050086
87 inet_ntop(AF_INET6, &header->ip6_src, addrstr, sizeof(addrstr));
junyulaic4e591a2018-11-26 22:36:10 +090088 printf("source = %s\n", addrstr);
Daniel Drowna45056e2012-03-23 10:42:54 -050089
90 inet_ntop(AF_INET6, &header->ip6_dst, addrstr, sizeof(addrstr));
junyulaic4e591a2018-11-26 22:36:10 +090091 printf("dest = %s\n", addrstr);
Daniel Drowna45056e2012-03-23 10:42:54 -050092}
93
94/* print icmp header */
95void dump_icmp(struct icmphdr *icmp) {
96 printf("ICMP\n");
97
junyulaic4e591a2018-11-26 22:36:10 +090098 printf("icmp.type = %x ", icmp->type);
99 if (icmp->type == ICMP_ECHOREPLY) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500100 printf("echo reply");
junyulaic4e591a2018-11-26 22:36:10 +0900101 } else if (icmp->type == ICMP_ECHO) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500102 printf("echo request");
103 } else {
104 printf("other");
105 }
106 printf("\n");
junyulaic4e591a2018-11-26 22:36:10 +0900107 printf("icmp.code = %x\n", icmp->code);
108 printf("icmp.checksum = %x\n", ntohs(icmp->checksum));
109 if (icmp->type == ICMP_ECHOREPLY || icmp->type == ICMP_ECHO) {
110 printf("icmp.un.echo.id = %x\n", ntohs(icmp->un.echo.id));
111 printf("icmp.un.echo.sequence = %x\n", ntohs(icmp->un.echo.sequence));
Daniel Drowna45056e2012-03-23 10:42:54 -0500112 }
113}
114
115/* print icmp6 header */
116void dump_icmp6(struct icmp6_hdr *icmp6) {
117 printf("ICMP6\n");
junyulaic4e591a2018-11-26 22:36:10 +0900118 printf("type = %x", icmp6->icmp6_type);
119 if (icmp6->icmp6_type == ICMP6_ECHO_REQUEST) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500120 printf("(echo request)");
junyulaic4e591a2018-11-26 22:36:10 +0900121 } else if (icmp6->icmp6_type == ICMP6_ECHO_REPLY) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500122 printf("(echo reply)");
123 }
124 printf("\n");
junyulaic4e591a2018-11-26 22:36:10 +0900125 printf("code = %x\n", icmp6->icmp6_code);
Daniel Drowna45056e2012-03-23 10:42:54 -0500126
junyulaic4e591a2018-11-26 22:36:10 +0900127 printf("checksum = %x\n", icmp6->icmp6_cksum);
Daniel Drowna45056e2012-03-23 10:42:54 -0500128
junyulaic4e591a2018-11-26 22:36:10 +0900129 if ((icmp6->icmp6_type == ICMP6_ECHO_REQUEST) || (icmp6->icmp6_type == ICMP6_ECHO_REPLY)) {
130 printf("icmp6_id = %x\n", icmp6->icmp6_id);
131 printf("icmp6_seq = %x\n", icmp6->icmp6_seq);
Daniel Drowna45056e2012-03-23 10:42:54 -0500132 }
133}
134
135/* print udp header */
junyulaic4e591a2018-11-26 22:36:10 +0900136void dump_udp_generic(const struct udphdr *udp, uint32_t temp_checksum, const uint8_t *payload,
137 size_t payload_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500138 uint16_t my_checksum;
139
140 temp_checksum = ip_checksum_add(temp_checksum, udp, sizeof(struct udphdr));
141 temp_checksum = ip_checksum_add(temp_checksum, payload, payload_size);
junyulaic4e591a2018-11-26 22:36:10 +0900142 my_checksum = ip_checksum_finish(temp_checksum);
Daniel Drowna45056e2012-03-23 10:42:54 -0500143
144 printf("UDP\n");
junyulaic4e591a2018-11-26 22:36:10 +0900145 printf("source = %x\n", ntohs(udp->source));
146 printf("dest = %x\n", ntohs(udp->dest));
147 printf("len = %x\n", ntohs(udp->len));
148 printf("check = %x (mine %x)\n", udp->check, my_checksum);
Daniel Drowna45056e2012-03-23 10:42:54 -0500149}
150
151/* print ipv4/udp header */
junyulaic4e591a2018-11-26 22:36:10 +0900152void dump_udp(const struct udphdr *udp, const struct iphdr *ip, const uint8_t *payload,
153 size_t payload_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500154 uint32_t temp_checksum;
Lorenzo Colitti07f02652014-02-20 14:28:43 +0900155 temp_checksum = ipv4_pseudo_header_checksum(ip, sizeof(*udp) + payload_size);
Daniel Drowna45056e2012-03-23 10:42:54 -0500156 dump_udp_generic(udp, temp_checksum, payload, payload_size);
157}
158
159/* print ipv6/udp header */
junyulaic4e591a2018-11-26 22:36:10 +0900160void dump_udp6(const struct udphdr *udp, const struct ip6_hdr *ip6, const uint8_t *payload,
161 size_t payload_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500162 uint32_t temp_checksum;
Lorenzo Colitti07f02652014-02-20 14:28:43 +0900163 temp_checksum = ipv6_pseudo_header_checksum(ip6, sizeof(*udp) + payload_size, IPPROTO_UDP);
Daniel Drowna45056e2012-03-23 10:42:54 -0500164 dump_udp_generic(udp, temp_checksum, payload, payload_size);
165}
166
167/* print tcp header */
junyulaic4e591a2018-11-26 22:36:10 +0900168void dump_tcp_generic(const struct tcphdr *tcp, const uint8_t *options, size_t options_size,
169 uint32_t temp_checksum, const uint8_t *payload, size_t payload_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500170 uint16_t my_checksum;
171
172 temp_checksum = ip_checksum_add(temp_checksum, tcp, sizeof(struct tcphdr));
junyulaic4e591a2018-11-26 22:36:10 +0900173 if (options) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500174 temp_checksum = ip_checksum_add(temp_checksum, options, options_size);
175 }
176 temp_checksum = ip_checksum_add(temp_checksum, payload, payload_size);
junyulaic4e591a2018-11-26 22:36:10 +0900177 my_checksum = ip_checksum_finish(temp_checksum);
Daniel Drowna45056e2012-03-23 10:42:54 -0500178
179 printf("TCP\n");
junyulaic4e591a2018-11-26 22:36:10 +0900180 printf("source = %x\n", ntohs(tcp->source));
181 printf("dest = %x\n", ntohs(tcp->dest));
182 printf("seq = %x\n", ntohl(tcp->seq));
183 printf("ack = %x\n", ntohl(tcp->ack_seq));
184 printf("d_off = %x\n", tcp->doff);
185 printf("res1 = %x\n", tcp->res1);
Daniel Drowna45056e2012-03-23 10:42:54 -0500186#ifdef __BIONIC__
junyulaic4e591a2018-11-26 22:36:10 +0900187 printf("CWR = %x\n", tcp->cwr);
188 printf("ECE = %x\n", tcp->ece);
Daniel Drowna45056e2012-03-23 10:42:54 -0500189#else
junyulaic4e591a2018-11-26 22:36:10 +0900190 printf("CWR/ECE = %x\n", tcp->res2);
Daniel Drowna45056e2012-03-23 10:42:54 -0500191#endif
junyulaic4e591a2018-11-26 22:36:10 +0900192 printf("urg = %x ack = %x psh = %x rst = %x syn = %x fin = %x\n", tcp->urg, tcp->ack,
193 tcp->psh, tcp->rst, tcp->syn, tcp->fin);
194 printf("window = %x\n", ntohs(tcp->window));
195 printf("check = %x [mine %x]\n", tcp->check, my_checksum);
196 printf("urgent = %x\n", tcp->urg_ptr);
Daniel Drowna45056e2012-03-23 10:42:54 -0500197
junyulaic4e591a2018-11-26 22:36:10 +0900198 if (options) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500199 size_t i;
200
201 printf("options: ");
junyulaic4e591a2018-11-26 22:36:10 +0900202 for (i = 0; i < options_size; i++) {
203 printf("%x ", *(options + i));
Daniel Drowna45056e2012-03-23 10:42:54 -0500204 }
205 printf("\n");
206 }
207}
208
209/* print ipv4/tcp header */
junyulaic4e591a2018-11-26 22:36:10 +0900210void dump_tcp(const struct tcphdr *tcp, const struct iphdr *ip, const uint8_t *payload,
211 size_t payload_size, const uint8_t *options, size_t options_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500212 uint32_t temp_checksum;
213
Lorenzo Colitti07f02652014-02-20 14:28:43 +0900214 temp_checksum = ipv4_pseudo_header_checksum(ip, sizeof(*tcp) + options_size + payload_size);
Daniel Drowna45056e2012-03-23 10:42:54 -0500215 dump_tcp_generic(tcp, options, options_size, temp_checksum, payload, payload_size);
216}
217
218/* print ipv6/tcp header */
junyulaic4e591a2018-11-26 22:36:10 +0900219void dump_tcp6(const struct tcphdr *tcp, const struct ip6_hdr *ip6, const uint8_t *payload,
220 size_t payload_size, const uint8_t *options, size_t options_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500221 uint32_t temp_checksum;
222
junyulaic4e591a2018-11-26 22:36:10 +0900223 temp_checksum =
224 ipv6_pseudo_header_checksum(ip6, sizeof(*tcp) + options_size + payload_size, IPPROTO_TCP);
Daniel Drowna45056e2012-03-23 10:42:54 -0500225 dump_tcp_generic(tcp, options, options_size, temp_checksum, payload, payload_size);
226}
227
228/* generic hex dump */
Brian Carlstromfcac4102014-02-24 20:03:01 -0800229void logcat_hexdump(const char *info, const uint8_t *data, size_t len) {
junyulaic4e591a2018-11-26 22:36:10 +0900230 char output[PACKETLEN * 3 + 2];
Daniel Drowna45056e2012-03-23 10:42:54 -0500231 size_t i;
232
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900233 output[0] = '\0';
junyulaic4e591a2018-11-26 22:36:10 +0900234 for (i = 0; i < len && i < PACKETLEN; i++) {
235 snprintf(output + i * 3, 4, " %02x", data[i]);
Daniel Drowna45056e2012-03-23 10:42:54 -0500236 }
junyulaic4e591a2018-11-26 22:36:10 +0900237 output[len * 3 + 3] = '\0';
Daniel Drowna45056e2012-03-23 10:42:54 -0500238
junyulaic4e591a2018-11-26 22:36:10 +0900239 logmsg(ANDROID_LOG_WARN, "info %s len %d data%s", info, len, output);
Daniel Drowna45056e2012-03-23 10:42:54 -0500240}
Lorenzo Colittif68200a2013-02-01 10:48:29 +0900241
Lorenzo Colittif913fe42013-04-08 18:30:55 +0900242void dump_iovec(const struct iovec *iov, int iov_len) {
243 int i;
244 char *str;
245 for (i = 0; i < iov_len; i++) {
246 asprintf(&str, "iov[%d]: ", i);
247 logcat_hexdump(str, iov[i].iov_base, iov[i].iov_len);
248 free(str);
249 }
250}
Lorenzo Colittif68200a2013-02-01 10:48:29 +0900251#endif // CLAT_DEBUG