blob: 2c948ffea1b35af5847e634c2cf08499165908a8 [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>
24#include <netinet/icmp6.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050025#include <netinet/in.h>
26#include <netinet/ip.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050027#include <netinet/ip6.h>
junyulaic4e591a2018-11-26 22:36:10 +090028#include <netinet/ip_icmp.h>
29#include <netinet/tcp.h>
30#include <netinet/udp.h>
Daniel Drowna45056e2012-03-23 10:42:54 -050031
32#include "checksum.h"
33#include "clatd.h"
junyulaic4e591a2018-11-26 22:36:10 +090034#include "debug.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050035#include "logging.h"
36
Lorenzo Colittif68200a2013-02-01 10:48:29 +090037#if CLAT_DEBUG
38
Daniel Drowna45056e2012-03-23 10:42:54 -050039/* print ip header */
40void 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");
junyulaic4e591a2018-11-26 22:36:10 +090047 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));
Daniel Drowna45056e2012-03-23 10:42:54 -050052 printf("frag: ");
junyulaic4e591a2018-11-26 22:36:10 +090053 if (frag_flags & IP_RF) {
Daniel Drowna45056e2012-03-23 10:42:54 -050054 printf("(RF) ");
55 }
junyulaic4e591a2018-11-26 22:36:10 +090056 if (frag_flags & IP_DF) {
Daniel Drowna45056e2012-03-23 10:42:54 -050057 printf("DF ");
58 }
junyulaic4e591a2018-11-26 22:36:10 +090059 if (frag_flags & IP_MF) {
Daniel Drowna45056e2012-03-23 10:42:54 -050060 printf("MF ");
61 }
junyulaic4e591a2018-11-26 22:36:10 +090062 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));
Daniel Drowna45056e2012-03-23 10:42:54 -050066 inet_ntop(AF_INET, &header->saddr, addrstr, sizeof(addrstr));
junyulaic4e591a2018-11-26 22:36:10 +090067 printf("saddr = %s\n", addrstr);
Daniel Drowna45056e2012-03-23 10:42:54 -050068 inet_ntop(AF_INET, &header->daddr, addrstr, sizeof(addrstr));
junyulaic4e591a2018-11-26 22:36:10 +090069 printf("daddr = %s\n", addrstr);
Daniel Drowna45056e2012-03-23 10:42:54 -050070}
71
72/* print ip6 header */
73void dump_ip6(struct ip6_hdr *header) {
74 char addrstr[INET6_ADDRSTRLEN];
75
76 printf("ipv6\n");
junyulaic4e591a2018-11-26 22:36:10 +090077 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);
Daniel Drowna45056e2012-03-23 10:42:54 -050083
84 inet_ntop(AF_INET6, &header->ip6_src, addrstr, sizeof(addrstr));
junyulaic4e591a2018-11-26 22:36:10 +090085 printf("source = %s\n", addrstr);
Daniel Drowna45056e2012-03-23 10:42:54 -050086
87 inet_ntop(AF_INET6, &header->ip6_dst, addrstr, sizeof(addrstr));
junyulaic4e591a2018-11-26 22:36:10 +090088 printf("dest = %s\n", addrstr);
Daniel Drowna45056e2012-03-23 10:42:54 -050089}
90
91/* print icmp header */
92void dump_icmp(struct icmphdr *icmp) {
93 printf("ICMP\n");
94
junyulaic4e591a2018-11-26 22:36:10 +090095 printf("icmp.type = %x ", icmp->type);
96 if (icmp->type == ICMP_ECHOREPLY) {
Daniel Drowna45056e2012-03-23 10:42:54 -050097 printf("echo reply");
junyulaic4e591a2018-11-26 22:36:10 +090098 } else if (icmp->type == ICMP_ECHO) {
Daniel Drowna45056e2012-03-23 10:42:54 -050099 printf("echo request");
100 } else {
101 printf("other");
102 }
103 printf("\n");
junyulaic4e591a2018-11-26 22:36:10 +0900104 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));
Daniel Drowna45056e2012-03-23 10:42:54 -0500109 }
110}
111
112/* print icmp6 header */
113void dump_icmp6(struct icmp6_hdr *icmp6) {
114 printf("ICMP6\n");
junyulaic4e591a2018-11-26 22:36:10 +0900115 printf("type = %x", icmp6->icmp6_type);
116 if (icmp6->icmp6_type == ICMP6_ECHO_REQUEST) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500117 printf("(echo request)");
junyulaic4e591a2018-11-26 22:36:10 +0900118 } else if (icmp6->icmp6_type == ICMP6_ECHO_REPLY) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500119 printf("(echo reply)");
120 }
121 printf("\n");
junyulaic4e591a2018-11-26 22:36:10 +0900122 printf("code = %x\n", icmp6->icmp6_code);
Daniel Drowna45056e2012-03-23 10:42:54 -0500123
junyulaic4e591a2018-11-26 22:36:10 +0900124 printf("checksum = %x\n", icmp6->icmp6_cksum);
Daniel Drowna45056e2012-03-23 10:42:54 -0500125
junyulaic4e591a2018-11-26 22:36:10 +0900126 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);
Daniel Drowna45056e2012-03-23 10:42:54 -0500129 }
130}
131
132/* print udp header */
junyulaic4e591a2018-11-26 22:36:10 +0900133void dump_udp_generic(const struct udphdr *udp, uint32_t temp_checksum, const uint8_t *payload,
134 size_t payload_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500135 uint16_t my_checksum;
136
137 temp_checksum = ip_checksum_add(temp_checksum, udp, sizeof(struct udphdr));
138 temp_checksum = ip_checksum_add(temp_checksum, payload, payload_size);
junyulaic4e591a2018-11-26 22:36:10 +0900139 my_checksum = ip_checksum_finish(temp_checksum);
Daniel Drowna45056e2012-03-23 10:42:54 -0500140
141 printf("UDP\n");
junyulaic4e591a2018-11-26 22:36:10 +0900142 printf("source = %x\n", ntohs(udp->source));
143 printf("dest = %x\n", ntohs(udp->dest));
144 printf("len = %x\n", ntohs(udp->len));
145 printf("check = %x (mine %x)\n", udp->check, my_checksum);
Daniel Drowna45056e2012-03-23 10:42:54 -0500146}
147
148/* print ipv4/udp header */
junyulaic4e591a2018-11-26 22:36:10 +0900149void dump_udp(const struct udphdr *udp, const struct iphdr *ip, const uint8_t *payload,
150 size_t payload_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500151 uint32_t temp_checksum;
Lorenzo Colitti07f02652014-02-20 14:28:43 +0900152 temp_checksum = ipv4_pseudo_header_checksum(ip, sizeof(*udp) + payload_size);
Daniel Drowna45056e2012-03-23 10:42:54 -0500153 dump_udp_generic(udp, temp_checksum, payload, payload_size);
154}
155
156/* print ipv6/udp header */
junyulaic4e591a2018-11-26 22:36:10 +0900157void dump_udp6(const struct udphdr *udp, const struct ip6_hdr *ip6, const uint8_t *payload,
158 size_t payload_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500159 uint32_t temp_checksum;
Lorenzo Colitti07f02652014-02-20 14:28:43 +0900160 temp_checksum = ipv6_pseudo_header_checksum(ip6, sizeof(*udp) + payload_size, IPPROTO_UDP);
Daniel Drowna45056e2012-03-23 10:42:54 -0500161 dump_udp_generic(udp, temp_checksum, payload, payload_size);
162}
163
164/* print tcp header */
junyulaic4e591a2018-11-26 22:36:10 +0900165void dump_tcp_generic(const struct tcphdr *tcp, const uint8_t *options, size_t options_size,
166 uint32_t temp_checksum, const uint8_t *payload, size_t payload_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500167 uint16_t my_checksum;
168
169 temp_checksum = ip_checksum_add(temp_checksum, tcp, sizeof(struct tcphdr));
junyulaic4e591a2018-11-26 22:36:10 +0900170 if (options) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500171 temp_checksum = ip_checksum_add(temp_checksum, options, options_size);
172 }
173 temp_checksum = ip_checksum_add(temp_checksum, payload, payload_size);
junyulaic4e591a2018-11-26 22:36:10 +0900174 my_checksum = ip_checksum_finish(temp_checksum);
Daniel Drowna45056e2012-03-23 10:42:54 -0500175
176 printf("TCP\n");
junyulaic4e591a2018-11-26 22:36:10 +0900177 printf("source = %x\n", ntohs(tcp->source));
178 printf("dest = %x\n", ntohs(tcp->dest));
179 printf("seq = %x\n", ntohl(tcp->seq));
180 printf("ack = %x\n", ntohl(tcp->ack_seq));
181 printf("d_off = %x\n", tcp->doff);
182 printf("res1 = %x\n", tcp->res1);
Daniel Drowna45056e2012-03-23 10:42:54 -0500183#ifdef __BIONIC__
junyulaic4e591a2018-11-26 22:36:10 +0900184 printf("CWR = %x\n", tcp->cwr);
185 printf("ECE = %x\n", tcp->ece);
Daniel Drowna45056e2012-03-23 10:42:54 -0500186#else
junyulaic4e591a2018-11-26 22:36:10 +0900187 printf("CWR/ECE = %x\n", tcp->res2);
Daniel Drowna45056e2012-03-23 10:42:54 -0500188#endif
junyulaic4e591a2018-11-26 22:36:10 +0900189 printf("urg = %x ack = %x psh = %x rst = %x syn = %x fin = %x\n", tcp->urg, tcp->ack,
190 tcp->psh, tcp->rst, tcp->syn, tcp->fin);
191 printf("window = %x\n", ntohs(tcp->window));
192 printf("check = %x [mine %x]\n", tcp->check, my_checksum);
193 printf("urgent = %x\n", tcp->urg_ptr);
Daniel Drowna45056e2012-03-23 10:42:54 -0500194
junyulaic4e591a2018-11-26 22:36:10 +0900195 if (options) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500196 size_t i;
197
198 printf("options: ");
junyulaic4e591a2018-11-26 22:36:10 +0900199 for (i = 0; i < options_size; i++) {
200 printf("%x ", *(options + i));
Daniel Drowna45056e2012-03-23 10:42:54 -0500201 }
202 printf("\n");
203 }
204}
205
206/* print ipv4/tcp header */
junyulaic4e591a2018-11-26 22:36:10 +0900207void dump_tcp(const struct tcphdr *tcp, const struct iphdr *ip, const uint8_t *payload,
208 size_t payload_size, const uint8_t *options, size_t options_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500209 uint32_t temp_checksum;
210
Lorenzo Colitti07f02652014-02-20 14:28:43 +0900211 temp_checksum = ipv4_pseudo_header_checksum(ip, sizeof(*tcp) + options_size + payload_size);
Daniel Drowna45056e2012-03-23 10:42:54 -0500212 dump_tcp_generic(tcp, options, options_size, temp_checksum, payload, payload_size);
213}
214
215/* print ipv6/tcp header */
junyulaic4e591a2018-11-26 22:36:10 +0900216void dump_tcp6(const struct tcphdr *tcp, const struct ip6_hdr *ip6, const uint8_t *payload,
217 size_t payload_size, const uint8_t *options, size_t options_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500218 uint32_t temp_checksum;
219
junyulaic4e591a2018-11-26 22:36:10 +0900220 temp_checksum =
221 ipv6_pseudo_header_checksum(ip6, sizeof(*tcp) + options_size + payload_size, IPPROTO_TCP);
Daniel Drowna45056e2012-03-23 10:42:54 -0500222 dump_tcp_generic(tcp, options, options_size, temp_checksum, payload, payload_size);
223}
224
225/* generic hex dump */
Brian Carlstromfcac4102014-02-24 20:03:01 -0800226void logcat_hexdump(const char *info, const uint8_t *data, size_t len) {
junyulaic4e591a2018-11-26 22:36:10 +0900227 char output[PACKETLEN * 3 + 2];
Daniel Drowna45056e2012-03-23 10:42:54 -0500228 size_t i;
229
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900230 output[0] = '\0';
junyulaic4e591a2018-11-26 22:36:10 +0900231 for (i = 0; i < len && i < PACKETLEN; i++) {
232 snprintf(output + i * 3, 4, " %02x", data[i]);
Daniel Drowna45056e2012-03-23 10:42:54 -0500233 }
junyulaic4e591a2018-11-26 22:36:10 +0900234 output[len * 3 + 3] = '\0';
Daniel Drowna45056e2012-03-23 10:42:54 -0500235
junyulaic4e591a2018-11-26 22:36:10 +0900236 logmsg(ANDROID_LOG_WARN, "info %s len %d data%s", info, len, output);
Daniel Drowna45056e2012-03-23 10:42:54 -0500237}
Lorenzo Colittif68200a2013-02-01 10:48:29 +0900238
Lorenzo Colittif913fe42013-04-08 18:30:55 +0900239void dump_iovec(const struct iovec *iov, int iov_len) {
240 int i;
241 char *str;
242 for (i = 0; i < iov_len; i++) {
243 asprintf(&str, "iov[%d]: ", i);
244 logcat_hexdump(str, iov[i].iov_base, iov[i].iov_len);
245 free(str);
246 }
247}
Lorenzo Colittif68200a2013-02-01 10:48:29 +0900248#endif // CLAT_DEBUG