blob: 28db13f070e79a8a2ac4195c84e85878d77adef2 [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
Lorenzo Colitti98de5952019-01-20 11:45:03 +090032#include "netutils/checksum.h"
33
Daniel Drowna45056e2012-03-23 10:42:54 -050034#include "clatd.h"
junyulaic4e591a2018-11-26 22:36:10 +090035#include "debug.h"
Daniel Drowna45056e2012-03-23 10:42:54 -050036#include "logging.h"
37
Lorenzo Colittif68200a2013-02-01 10:48:29 +090038#if CLAT_DEBUG
39
Daniel Drowna45056e2012-03-23 10:42:54 -050040/* print ip header */
41void dump_ip(struct iphdr *header) {
42 u_int16_t frag_flags;
43 char addrstr[INET6_ADDRSTRLEN];
44
45 frag_flags = ntohs(header->frag_off);
46
47 printf("IP packet\n");
junyulaic4e591a2018-11-26 22:36:10 +090048 printf("header_len = %x\n", header->ihl);
49 printf("version = %x\n", header->version);
50 printf("tos = %x\n", header->tos);
51 printf("tot_len = %x\n", ntohs(header->tot_len));
52 printf("id = %x\n", ntohs(header->id));
Daniel Drowna45056e2012-03-23 10:42:54 -050053 printf("frag: ");
junyulaic4e591a2018-11-26 22:36:10 +090054 if (frag_flags & IP_RF) {
Daniel Drowna45056e2012-03-23 10:42:54 -050055 printf("(RF) ");
56 }
junyulaic4e591a2018-11-26 22:36:10 +090057 if (frag_flags & IP_DF) {
Daniel Drowna45056e2012-03-23 10:42:54 -050058 printf("DF ");
59 }
junyulaic4e591a2018-11-26 22:36:10 +090060 if (frag_flags & IP_MF) {
Daniel Drowna45056e2012-03-23 10:42:54 -050061 printf("MF ");
62 }
junyulaic4e591a2018-11-26 22:36:10 +090063 printf("offset = %x\n", frag_flags & IP_OFFMASK);
64 printf("ttl = %x\n", header->ttl);
65 printf("protocol = %x\n", header->protocol);
66 printf("checksum = %x\n", ntohs(header->check));
Daniel Drowna45056e2012-03-23 10:42:54 -050067 inet_ntop(AF_INET, &header->saddr, addrstr, sizeof(addrstr));
junyulaic4e591a2018-11-26 22:36:10 +090068 printf("saddr = %s\n", addrstr);
Daniel Drowna45056e2012-03-23 10:42:54 -050069 inet_ntop(AF_INET, &header->daddr, addrstr, sizeof(addrstr));
junyulaic4e591a2018-11-26 22:36:10 +090070 printf("daddr = %s\n", addrstr);
Daniel Drowna45056e2012-03-23 10:42:54 -050071}
72
73/* print ip6 header */
74void dump_ip6(struct ip6_hdr *header) {
75 char addrstr[INET6_ADDRSTRLEN];
76
77 printf("ipv6\n");
junyulaic4e591a2018-11-26 22:36:10 +090078 printf("version = %x\n", header->ip6_vfc >> 4);
79 printf("traffic class = %x\n", header->ip6_flow >> 20);
80 printf("flow label = %x\n", ntohl(header->ip6_flow & 0x000fffff));
81 printf("payload len = %x\n", ntohs(header->ip6_plen));
82 printf("next header = %x\n", header->ip6_nxt);
83 printf("hop limit = %x\n", header->ip6_hlim);
Daniel Drowna45056e2012-03-23 10:42:54 -050084
85 inet_ntop(AF_INET6, &header->ip6_src, addrstr, sizeof(addrstr));
junyulaic4e591a2018-11-26 22:36:10 +090086 printf("source = %s\n", addrstr);
Daniel Drowna45056e2012-03-23 10:42:54 -050087
88 inet_ntop(AF_INET6, &header->ip6_dst, addrstr, sizeof(addrstr));
junyulaic4e591a2018-11-26 22:36:10 +090089 printf("dest = %s\n", addrstr);
Daniel Drowna45056e2012-03-23 10:42:54 -050090}
91
92/* print icmp header */
93void dump_icmp(struct icmphdr *icmp) {
94 printf("ICMP\n");
95
junyulaic4e591a2018-11-26 22:36:10 +090096 printf("icmp.type = %x ", icmp->type);
97 if (icmp->type == ICMP_ECHOREPLY) {
Daniel Drowna45056e2012-03-23 10:42:54 -050098 printf("echo reply");
junyulaic4e591a2018-11-26 22:36:10 +090099 } else if (icmp->type == ICMP_ECHO) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500100 printf("echo request");
101 } else {
102 printf("other");
103 }
104 printf("\n");
junyulaic4e591a2018-11-26 22:36:10 +0900105 printf("icmp.code = %x\n", icmp->code);
106 printf("icmp.checksum = %x\n", ntohs(icmp->checksum));
107 if (icmp->type == ICMP_ECHOREPLY || icmp->type == ICMP_ECHO) {
108 printf("icmp.un.echo.id = %x\n", ntohs(icmp->un.echo.id));
109 printf("icmp.un.echo.sequence = %x\n", ntohs(icmp->un.echo.sequence));
Daniel Drowna45056e2012-03-23 10:42:54 -0500110 }
111}
112
113/* print icmp6 header */
114void dump_icmp6(struct icmp6_hdr *icmp6) {
115 printf("ICMP6\n");
junyulaic4e591a2018-11-26 22:36:10 +0900116 printf("type = %x", icmp6->icmp6_type);
117 if (icmp6->icmp6_type == ICMP6_ECHO_REQUEST) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500118 printf("(echo request)");
junyulaic4e591a2018-11-26 22:36:10 +0900119 } else if (icmp6->icmp6_type == ICMP6_ECHO_REPLY) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500120 printf("(echo reply)");
121 }
122 printf("\n");
junyulaic4e591a2018-11-26 22:36:10 +0900123 printf("code = %x\n", icmp6->icmp6_code);
Daniel Drowna45056e2012-03-23 10:42:54 -0500124
junyulaic4e591a2018-11-26 22:36:10 +0900125 printf("checksum = %x\n", icmp6->icmp6_cksum);
Daniel Drowna45056e2012-03-23 10:42:54 -0500126
junyulaic4e591a2018-11-26 22:36:10 +0900127 if ((icmp6->icmp6_type == ICMP6_ECHO_REQUEST) || (icmp6->icmp6_type == ICMP6_ECHO_REPLY)) {
128 printf("icmp6_id = %x\n", icmp6->icmp6_id);
129 printf("icmp6_seq = %x\n", icmp6->icmp6_seq);
Daniel Drowna45056e2012-03-23 10:42:54 -0500130 }
131}
132
133/* print udp header */
junyulaic4e591a2018-11-26 22:36:10 +0900134void dump_udp_generic(const struct udphdr *udp, uint32_t temp_checksum, const uint8_t *payload,
135 size_t payload_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500136 uint16_t my_checksum;
137
138 temp_checksum = ip_checksum_add(temp_checksum, udp, sizeof(struct udphdr));
139 temp_checksum = ip_checksum_add(temp_checksum, payload, payload_size);
junyulaic4e591a2018-11-26 22:36:10 +0900140 my_checksum = ip_checksum_finish(temp_checksum);
Daniel Drowna45056e2012-03-23 10:42:54 -0500141
142 printf("UDP\n");
junyulaic4e591a2018-11-26 22:36:10 +0900143 printf("source = %x\n", ntohs(udp->source));
144 printf("dest = %x\n", ntohs(udp->dest));
145 printf("len = %x\n", ntohs(udp->len));
146 printf("check = %x (mine %x)\n", udp->check, my_checksum);
Daniel Drowna45056e2012-03-23 10:42:54 -0500147}
148
149/* print ipv4/udp header */
junyulaic4e591a2018-11-26 22:36:10 +0900150void dump_udp(const struct udphdr *udp, const struct iphdr *ip, const uint8_t *payload,
151 size_t payload_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500152 uint32_t temp_checksum;
Lorenzo Colitti07f02652014-02-20 14:28:43 +0900153 temp_checksum = ipv4_pseudo_header_checksum(ip, sizeof(*udp) + payload_size);
Daniel Drowna45056e2012-03-23 10:42:54 -0500154 dump_udp_generic(udp, temp_checksum, payload, payload_size);
155}
156
157/* print ipv6/udp header */
junyulaic4e591a2018-11-26 22:36:10 +0900158void dump_udp6(const struct udphdr *udp, const struct ip6_hdr *ip6, const uint8_t *payload,
159 size_t payload_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500160 uint32_t temp_checksum;
Lorenzo Colitti07f02652014-02-20 14:28:43 +0900161 temp_checksum = ipv6_pseudo_header_checksum(ip6, sizeof(*udp) + payload_size, IPPROTO_UDP);
Daniel Drowna45056e2012-03-23 10:42:54 -0500162 dump_udp_generic(udp, temp_checksum, payload, payload_size);
163}
164
165/* print tcp header */
junyulaic4e591a2018-11-26 22:36:10 +0900166void dump_tcp_generic(const struct tcphdr *tcp, const uint8_t *options, size_t options_size,
167 uint32_t temp_checksum, const uint8_t *payload, size_t payload_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500168 uint16_t my_checksum;
169
170 temp_checksum = ip_checksum_add(temp_checksum, tcp, sizeof(struct tcphdr));
junyulaic4e591a2018-11-26 22:36:10 +0900171 if (options) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500172 temp_checksum = ip_checksum_add(temp_checksum, options, options_size);
173 }
174 temp_checksum = ip_checksum_add(temp_checksum, payload, payload_size);
junyulaic4e591a2018-11-26 22:36:10 +0900175 my_checksum = ip_checksum_finish(temp_checksum);
Daniel Drowna45056e2012-03-23 10:42:54 -0500176
177 printf("TCP\n");
junyulaic4e591a2018-11-26 22:36:10 +0900178 printf("source = %x\n", ntohs(tcp->source));
179 printf("dest = %x\n", ntohs(tcp->dest));
180 printf("seq = %x\n", ntohl(tcp->seq));
181 printf("ack = %x\n", ntohl(tcp->ack_seq));
182 printf("d_off = %x\n", tcp->doff);
183 printf("res1 = %x\n", tcp->res1);
Daniel Drowna45056e2012-03-23 10:42:54 -0500184#ifdef __BIONIC__
junyulaic4e591a2018-11-26 22:36:10 +0900185 printf("CWR = %x\n", tcp->cwr);
186 printf("ECE = %x\n", tcp->ece);
Daniel Drowna45056e2012-03-23 10:42:54 -0500187#else
junyulaic4e591a2018-11-26 22:36:10 +0900188 printf("CWR/ECE = %x\n", tcp->res2);
Daniel Drowna45056e2012-03-23 10:42:54 -0500189#endif
junyulaic4e591a2018-11-26 22:36:10 +0900190 printf("urg = %x ack = %x psh = %x rst = %x syn = %x fin = %x\n", tcp->urg, tcp->ack,
191 tcp->psh, tcp->rst, tcp->syn, tcp->fin);
192 printf("window = %x\n", ntohs(tcp->window));
193 printf("check = %x [mine %x]\n", tcp->check, my_checksum);
194 printf("urgent = %x\n", tcp->urg_ptr);
Daniel Drowna45056e2012-03-23 10:42:54 -0500195
junyulaic4e591a2018-11-26 22:36:10 +0900196 if (options) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500197 size_t i;
198
199 printf("options: ");
junyulaic4e591a2018-11-26 22:36:10 +0900200 for (i = 0; i < options_size; i++) {
201 printf("%x ", *(options + i));
Daniel Drowna45056e2012-03-23 10:42:54 -0500202 }
203 printf("\n");
204 }
205}
206
207/* print ipv4/tcp header */
junyulaic4e591a2018-11-26 22:36:10 +0900208void dump_tcp(const struct tcphdr *tcp, const struct iphdr *ip, const uint8_t *payload,
209 size_t payload_size, const uint8_t *options, size_t options_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500210 uint32_t temp_checksum;
211
Lorenzo Colitti07f02652014-02-20 14:28:43 +0900212 temp_checksum = ipv4_pseudo_header_checksum(ip, sizeof(*tcp) + options_size + payload_size);
Daniel Drowna45056e2012-03-23 10:42:54 -0500213 dump_tcp_generic(tcp, options, options_size, temp_checksum, payload, payload_size);
214}
215
216/* print ipv6/tcp header */
junyulaic4e591a2018-11-26 22:36:10 +0900217void dump_tcp6(const struct tcphdr *tcp, const struct ip6_hdr *ip6, const uint8_t *payload,
218 size_t payload_size, const uint8_t *options, size_t options_size) {
Daniel Drowna45056e2012-03-23 10:42:54 -0500219 uint32_t temp_checksum;
220
junyulaic4e591a2018-11-26 22:36:10 +0900221 temp_checksum =
222 ipv6_pseudo_header_checksum(ip6, sizeof(*tcp) + options_size + payload_size, IPPROTO_TCP);
Daniel Drowna45056e2012-03-23 10:42:54 -0500223 dump_tcp_generic(tcp, options, options_size, temp_checksum, payload, payload_size);
224}
225
226/* generic hex dump */
Brian Carlstromfcac4102014-02-24 20:03:01 -0800227void logcat_hexdump(const char *info, const uint8_t *data, size_t len) {
junyulaic4e591a2018-11-26 22:36:10 +0900228 char output[PACKETLEN * 3 + 2];
Daniel Drowna45056e2012-03-23 10:42:54 -0500229 size_t i;
230
Lorenzo Colitti57d480d2014-02-09 10:35:38 +0900231 output[0] = '\0';
junyulaic4e591a2018-11-26 22:36:10 +0900232 for (i = 0; i < len && i < PACKETLEN; i++) {
233 snprintf(output + i * 3, 4, " %02x", data[i]);
Daniel Drowna45056e2012-03-23 10:42:54 -0500234 }
junyulaic4e591a2018-11-26 22:36:10 +0900235 output[len * 3 + 3] = '\0';
Daniel Drowna45056e2012-03-23 10:42:54 -0500236
junyulaic4e591a2018-11-26 22:36:10 +0900237 logmsg(ANDROID_LOG_WARN, "info %s len %d data%s", info, len, output);
Daniel Drowna45056e2012-03-23 10:42:54 -0500238}
Lorenzo Colittif68200a2013-02-01 10:48:29 +0900239
Lorenzo Colittif913fe42013-04-08 18:30:55 +0900240void dump_iovec(const struct iovec *iov, int iov_len) {
241 int i;
242 char *str;
243 for (i = 0; i < iov_len; i++) {
244 asprintf(&str, "iov[%d]: ", i);
245 logcat_hexdump(str, iov[i].iov_base, iov[i].iov_len);
246 free(str);
247 }
248}
Lorenzo Colittif68200a2013-02-01 10:48:29 +0900249#endif // CLAT_DEBUG