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