176404edcSAsim Jamshed #include <stdio.h>
276404edcSAsim Jamshed #include <unistd.h>
376404edcSAsim Jamshed #include <string.h>
476404edcSAsim Jamshed #include <stdint.h>
576404edcSAsim Jamshed #include <stdarg.h>
676404edcSAsim Jamshed #include "debug.h"
776404edcSAsim Jamshed #include "tcp_in.h"
876404edcSAsim Jamshed #include "logger.h"
976404edcSAsim Jamshed #include "ip_in.h"
1076404edcSAsim Jamshed
1176404edcSAsim Jamshed /*----------------------------------------------------------------------------*/
flush_log_data(mtcp_manager_t mtcp)1276404edcSAsim Jamshed void flush_log_data(mtcp_manager_t mtcp)
1376404edcSAsim Jamshed {
1476404edcSAsim Jamshed int ret = 0;
1576404edcSAsim Jamshed if (mtcp->w_buffer) {
1676404edcSAsim Jamshed EnqueueJobBuffer(mtcp->logger, mtcp->w_buffer);
1776404edcSAsim Jamshed ret = write(mtcp->sp_fd, "A", 1);
1876404edcSAsim Jamshed if (ret != 1) {
1976404edcSAsim Jamshed TRACE_INFO("Failed to flush logs in the buffer.\n");
2076404edcSAsim Jamshed perror("write() for pipe");
2176404edcSAsim Jamshed }
2276404edcSAsim Jamshed }
2376404edcSAsim Jamshed }
2476404edcSAsim Jamshed /*----------------------------------------------------------------------------*/
2576404edcSAsim Jamshed void
thread_printf(mtcp_manager_t mtcp,FILE * f_idx,const char * _Format,...)2676404edcSAsim Jamshed thread_printf(mtcp_manager_t mtcp, FILE* f_idx, const char* _Format, ...)
2776404edcSAsim Jamshed {
2876404edcSAsim Jamshed va_list argptr;
2976404edcSAsim Jamshed va_start(argptr, _Format);
3076404edcSAsim Jamshed
3176404edcSAsim Jamshed #define PRINT_LIMIT 4096
3276404edcSAsim Jamshed int len;
3376404edcSAsim Jamshed log_buff *wbuf;
3476404edcSAsim Jamshed
3576404edcSAsim Jamshed assert(f_idx != NULL);
3676404edcSAsim Jamshed
3776404edcSAsim Jamshed pthread_mutex_lock(&mtcp->logger->mutex);
3876404edcSAsim Jamshed wbuf = mtcp->w_buffer;
3976404edcSAsim Jamshed if (wbuf && (wbuf->buff_len + PRINT_LIMIT > LOG_BUFF_SIZE)) {
4076404edcSAsim Jamshed flush_log_data(mtcp);
4176404edcSAsim Jamshed wbuf = NULL;
4276404edcSAsim Jamshed }
4376404edcSAsim Jamshed
4476404edcSAsim Jamshed if (!wbuf) {
45*cafe7743SAsim Jamshed do { // out of free buffers!!
4676404edcSAsim Jamshed wbuf = DequeueFreeBuffer(mtcp->logger);
4776404edcSAsim Jamshed assert(wbuf);
48*cafe7743SAsim Jamshed } while (!wbuf);
4976404edcSAsim Jamshed wbuf->buff_len = 0;
5076404edcSAsim Jamshed wbuf->tid = mtcp->ctx->cpu;
5176404edcSAsim Jamshed wbuf->fid = f_idx;
5276404edcSAsim Jamshed mtcp->w_buffer = wbuf;
5376404edcSAsim Jamshed }
5476404edcSAsim Jamshed
5576404edcSAsim Jamshed len = vsnprintf(wbuf->buff + wbuf->buff_len, PRINT_LIMIT, _Format, argptr);
5676404edcSAsim Jamshed wbuf->buff_len += len;
5776404edcSAsim Jamshed pthread_mutex_unlock(&mtcp->logger->mutex);
5876404edcSAsim Jamshed
5976404edcSAsim Jamshed va_end(argptr);
6076404edcSAsim Jamshed
6176404edcSAsim Jamshed }
6276404edcSAsim Jamshed /*----------------------------------------------------------------------------*/
6376404edcSAsim Jamshed void
DumpPacket(mtcp_manager_t mtcp,char * buf,int len,char * step,int ifindex)6476404edcSAsim Jamshed DumpPacket(mtcp_manager_t mtcp, char *buf, int len, char *step, int ifindex)
6576404edcSAsim Jamshed {
6676404edcSAsim Jamshed struct ethhdr *ethh;
6776404edcSAsim Jamshed struct iphdr *iph;
6876404edcSAsim Jamshed struct udphdr *udph;
6976404edcSAsim Jamshed struct tcphdr *tcph;
7076404edcSAsim Jamshed uint8_t *t;
7176404edcSAsim Jamshed
7276404edcSAsim Jamshed if (ifindex >= 0)
7376404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "%s %d %u", step, ifindex, mtcp->cur_ts);
7476404edcSAsim Jamshed else
7576404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "%s ? %u", step, mtcp->cur_ts);
7676404edcSAsim Jamshed
7776404edcSAsim Jamshed ethh = (struct ethhdr *)buf;
78*cafe7743SAsim Jamshed if (ntohs(ethh->h_proto) != ETH_P_IP) {
7976404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "%02X:%02X:%02X:%02X:%02X:%02X -> %02X:%02X:%02X:%02X:%02X:%02X ",
8076404edcSAsim Jamshed ethh->h_source[0],
8176404edcSAsim Jamshed ethh->h_source[1],
8276404edcSAsim Jamshed ethh->h_source[2],
8376404edcSAsim Jamshed ethh->h_source[3],
8476404edcSAsim Jamshed ethh->h_source[4],
8576404edcSAsim Jamshed ethh->h_source[5],
8676404edcSAsim Jamshed ethh->h_dest[0],
8776404edcSAsim Jamshed ethh->h_dest[1],
8876404edcSAsim Jamshed ethh->h_dest[2],
8976404edcSAsim Jamshed ethh->h_dest[3],
9076404edcSAsim Jamshed ethh->h_dest[4],
9176404edcSAsim Jamshed ethh->h_dest[5]);
9276404edcSAsim Jamshed
9376404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "protocol %04hx ", ntohs(ethh->h_proto));
94*cafe7743SAsim Jamshed goto done;
95*cafe7743SAsim Jamshed }
9676404edcSAsim Jamshed
9776404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, " ");
9876404edcSAsim Jamshed
9976404edcSAsim Jamshed iph = (struct iphdr *)(ethh + 1);
10076404edcSAsim Jamshed udph = (struct udphdr *)((uint32_t *)iph + iph->ihl);
10176404edcSAsim Jamshed tcph = (struct tcphdr *)((uint32_t *)iph + iph->ihl);
10276404edcSAsim Jamshed
10376404edcSAsim Jamshed t = (uint8_t *)&iph->saddr;
10476404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "%u.%u.%u.%u", t[0], t[1], t[2], t[3]);
10576404edcSAsim Jamshed if (iph->protocol == IPPROTO_TCP || iph->protocol == IPPROTO_UDP)
10676404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "(%d)", ntohs(udph->source));
10776404edcSAsim Jamshed
10876404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, " -> ");
10976404edcSAsim Jamshed
11076404edcSAsim Jamshed t = (uint8_t *)&iph->daddr;
11176404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "%u.%u.%u.%u", t[0], t[1], t[2], t[3]);
11276404edcSAsim Jamshed if (iph->protocol == IPPROTO_TCP || iph->protocol == IPPROTO_UDP)
11376404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "(%d)", ntohs(udph->dest));
11476404edcSAsim Jamshed
11576404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, " IP_ID=%d", ntohs(iph->id));
11676404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, " TTL=%d ", iph->ttl);
11776404edcSAsim Jamshed
11876404edcSAsim Jamshed if (ip_fast_csum(iph, iph->ihl)) {
11976404edcSAsim Jamshed __sum16 org_csum, correct_csum;
12076404edcSAsim Jamshed
12176404edcSAsim Jamshed org_csum = iph->check;
12276404edcSAsim Jamshed iph->check = 0;
12376404edcSAsim Jamshed correct_csum = ip_fast_csum(iph, iph->ihl);
12476404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "(bad checksum %04x should be %04x) ",
12576404edcSAsim Jamshed ntohs(org_csum), ntohs(correct_csum));
12676404edcSAsim Jamshed iph->check = org_csum;
12776404edcSAsim Jamshed }
12876404edcSAsim Jamshed
12976404edcSAsim Jamshed switch (iph->protocol) {
13076404edcSAsim Jamshed case IPPROTO_TCP:
13176404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "TCP ");
13276404edcSAsim Jamshed
13376404edcSAsim Jamshed if (tcph->syn)
13476404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "S ");
13576404edcSAsim Jamshed if (tcph->fin)
13676404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "F ");
13776404edcSAsim Jamshed if (tcph->ack)
13876404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "A ");
13976404edcSAsim Jamshed if (tcph->rst)
14076404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "R ");
14176404edcSAsim Jamshed
14276404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "seq %u ", ntohl(tcph->seq));
14376404edcSAsim Jamshed if (tcph->ack)
14476404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "ack %u ", ntohl(tcph->ack_seq));
14576404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "WDW=%u ", ntohs(tcph->window));
14676404edcSAsim Jamshed break;
14776404edcSAsim Jamshed case IPPROTO_UDP:
14876404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "UDP ");
14976404edcSAsim Jamshed break;
15076404edcSAsim Jamshed default:
15176404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "protocol %d ", iph->protocol);
15276404edcSAsim Jamshed goto done;
15376404edcSAsim Jamshed }
15476404edcSAsim Jamshed done:
15576404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "len=%d\n", len);
15676404edcSAsim Jamshed }
15776404edcSAsim Jamshed /*----------------------------------------------------------------------------*/
15876404edcSAsim Jamshed void
DumpIPPacket(mtcp_manager_t mtcp,const struct iphdr * iph,int len)15976404edcSAsim Jamshed DumpIPPacket(mtcp_manager_t mtcp, const struct iphdr *iph, int len)
16076404edcSAsim Jamshed {
16176404edcSAsim Jamshed struct udphdr *udph;
16276404edcSAsim Jamshed struct tcphdr *tcph;
16376404edcSAsim Jamshed uint8_t *t;
16476404edcSAsim Jamshed
16576404edcSAsim Jamshed udph = (struct udphdr *)((uint32_t *)iph + iph->ihl);
16676404edcSAsim Jamshed tcph = (struct tcphdr *)((uint32_t *)iph + iph->ihl);
16776404edcSAsim Jamshed
16876404edcSAsim Jamshed t = (uint8_t *)&iph->saddr;
16976404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "%u.%u.%u.%u", t[0], t[1], t[2], t[3]);
17076404edcSAsim Jamshed if (iph->protocol == IPPROTO_TCP || iph->protocol == IPPROTO_UDP)
17176404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "(%d)", ntohs(udph->source));
17276404edcSAsim Jamshed
17376404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, " -> ");
17476404edcSAsim Jamshed
17576404edcSAsim Jamshed t = (uint8_t *)&iph->daddr;
17676404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "%u.%u.%u.%u", t[0], t[1], t[2], t[3]);
17776404edcSAsim Jamshed if (iph->protocol == IPPROTO_TCP || iph->protocol == IPPROTO_UDP)
17876404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "(%d)", ntohs(udph->dest));
17976404edcSAsim Jamshed
18076404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, " IP_ID=%d", ntohs(iph->id));
18176404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, " TTL=%d ", iph->ttl);
18276404edcSAsim Jamshed
18376404edcSAsim Jamshed if (ip_fast_csum(iph, iph->ihl)) {
18476404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "(bad checksum) ");
18576404edcSAsim Jamshed }
18676404edcSAsim Jamshed
18776404edcSAsim Jamshed switch (iph->protocol) {
18876404edcSAsim Jamshed case IPPROTO_TCP:
18976404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "TCP ");
19076404edcSAsim Jamshed
19176404edcSAsim Jamshed if (tcph->syn)
19276404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "S ");
19376404edcSAsim Jamshed if (tcph->fin)
19476404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "F ");
19576404edcSAsim Jamshed if (tcph->ack)
19676404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "A ");
19776404edcSAsim Jamshed if (tcph->rst)
19876404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "R ");
19976404edcSAsim Jamshed
20076404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "seq %u ", ntohl(tcph->seq));
20176404edcSAsim Jamshed if (tcph->ack)
20276404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "ack %u ", ntohl(tcph->ack_seq));
20376404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "WDW=%u ", ntohs(tcph->window));
20476404edcSAsim Jamshed break;
20576404edcSAsim Jamshed case IPPROTO_UDP:
20676404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "UDP ");
20776404edcSAsim Jamshed break;
20876404edcSAsim Jamshed default:
20976404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "protocol %d ", iph->protocol);
21076404edcSAsim Jamshed goto done;
21176404edcSAsim Jamshed }
21276404edcSAsim Jamshed done:
21376404edcSAsim Jamshed thread_printf(mtcp, mtcp->log_fp, "len=%d\n", len);
21476404edcSAsim Jamshed }
21576404edcSAsim Jamshed /*----------------------------------------------------------------------------*/
21676404edcSAsim Jamshed void
DumpIPPacketToFile(FILE * fout,const struct iphdr * iph,int len)21776404edcSAsim Jamshed DumpIPPacketToFile(FILE *fout, const struct iphdr *iph, int len)
21876404edcSAsim Jamshed {
21976404edcSAsim Jamshed struct udphdr *udph;
22076404edcSAsim Jamshed struct tcphdr *tcph;
22176404edcSAsim Jamshed uint8_t *t;
22276404edcSAsim Jamshed
22376404edcSAsim Jamshed udph = (struct udphdr *)((uint32_t *)iph + iph->ihl);
22476404edcSAsim Jamshed tcph = (struct tcphdr *)((uint32_t *)iph + iph->ihl);
22576404edcSAsim Jamshed
22676404edcSAsim Jamshed t = (uint8_t *)&iph->saddr;
22776404edcSAsim Jamshed fprintf(fout, "%u.%u.%u.%u", t[0], t[1], t[2], t[3]);
22876404edcSAsim Jamshed if (iph->protocol == IPPROTO_TCP || iph->protocol == IPPROTO_UDP)
22976404edcSAsim Jamshed fprintf(fout, "(%d)", ntohs(udph->source));
23076404edcSAsim Jamshed
23176404edcSAsim Jamshed fprintf(fout, " -> ");
23276404edcSAsim Jamshed
23376404edcSAsim Jamshed t = (uint8_t *)&iph->daddr;
23476404edcSAsim Jamshed fprintf(fout, "%u.%u.%u.%u", t[0], t[1], t[2], t[3]);
23576404edcSAsim Jamshed if (iph->protocol == IPPROTO_TCP || iph->protocol == IPPROTO_UDP)
23676404edcSAsim Jamshed fprintf(fout, "(%d)", ntohs(udph->dest));
23776404edcSAsim Jamshed
23876404edcSAsim Jamshed fprintf(fout, " IP_ID=%d", ntohs(iph->id));
23976404edcSAsim Jamshed fprintf(fout, " TTL=%d ", iph->ttl);
24076404edcSAsim Jamshed
24176404edcSAsim Jamshed if (ip_fast_csum(iph, iph->ihl)) {
24276404edcSAsim Jamshed fprintf(fout, "(bad checksum) ");
24376404edcSAsim Jamshed }
24476404edcSAsim Jamshed
24576404edcSAsim Jamshed switch (iph->protocol) {
24676404edcSAsim Jamshed case IPPROTO_TCP:
24776404edcSAsim Jamshed fprintf(fout, "TCP ");
24876404edcSAsim Jamshed
24976404edcSAsim Jamshed if (tcph->syn)
25076404edcSAsim Jamshed fprintf(fout, "S ");
25176404edcSAsim Jamshed if (tcph->fin)
25276404edcSAsim Jamshed fprintf(fout, "F ");
25376404edcSAsim Jamshed if (tcph->ack)
25476404edcSAsim Jamshed fprintf(fout, "A ");
25576404edcSAsim Jamshed if (tcph->rst)
25676404edcSAsim Jamshed fprintf(fout, "R ");
25776404edcSAsim Jamshed
25876404edcSAsim Jamshed fprintf(fout, "seq %u ", ntohl(tcph->seq));
25976404edcSAsim Jamshed if (tcph->ack)
26076404edcSAsim Jamshed fprintf(fout, "ack %u ", ntohl(tcph->ack_seq));
26176404edcSAsim Jamshed fprintf(fout, "WDW=%u ", ntohs(tcph->window));
26276404edcSAsim Jamshed break;
26376404edcSAsim Jamshed case IPPROTO_UDP:
26476404edcSAsim Jamshed fprintf(fout, "UDP ");
26576404edcSAsim Jamshed break;
26676404edcSAsim Jamshed default:
26776404edcSAsim Jamshed fprintf(fout, "protocol %d ", iph->protocol);
26876404edcSAsim Jamshed goto done;
26976404edcSAsim Jamshed }
27076404edcSAsim Jamshed done:
27176404edcSAsim Jamshed fprintf(fout, "len=%d\n", len);
27276404edcSAsim Jamshed }
273