void liv3(u_int type,const u_char *p) { const u_char *mp; int i,j,k,r,ihl,flag; u_int id,fragm,len; u_char ttl,proto; u_long flow; struct filt_ipv4 *aux_ipv4; struct filt_ipv6 *aux_ipv6; int mask[8]={0,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe}; //increasing counter counter.lvl3++; switch(type) { // IPv4 case 0x800: if(!r_ipv4) return; printf("\n\nIPV4\n\n"); //increase counter counter.ipv4++; //start decoding ipv4 flag=0; for(aux_ipv4=filt_ipv4;aux_ipv4!=NULL;aux_ipv4=aux_ipv4->next) { i=aux_ipv4->scid; j=i/8; r=i%8; mp=p+12; for(k=0;k<j;k++) if(*(mp+k)!=aux_ipv4->sip[k]) break; if(k!=j||((*(mp+j))&mask[r])!=(aux_ipv4->sip[j]&mask[r])) { flag=1; continue; } i=aux_ipv4->dcid; j=i/8; r=i%8; mp=p+16; for(k=0;k<j;k++) if(*(mp+k)!=aux_ipv4->dip[k]) break; if(k!=j||((*(mp+j))&mask[r])!=(aux_ipv4->dip[j]&mask[r])) { flag=1; continue; } flag=0; break; } id=ntohs(*(u_int *)(p+4)); ttl=*(p+8); proto=*(p+9); //PROBABILMENTE a p + 14 + 9 vedo il protocollo livello 4 len=ntohs(*(u_int *)(p+2)); ihl=((*p)&0x0f)*4; fragm=ntohs(*(u_int *)(p+6)); if(p_ipv4) { colore(3); myprintf("IPv4 |"); //MODIFICATO myprintf print_ipv4(p+12); myprintf(" -> "); //MODIFICATO myprintf print_ipv4(p+16); myprintf(" Id:%d Ttl:%d Proto:%d Len:%d",id,ttl,proto,len); //MODIFICATO myprintf if(fragm&0x4000) myprintf(" DF"); //MODIFICATO myprintf myprintf(" Fragm:%d%c\n",fragm&0x1fff,(fragm&0x2000)?'M':'F'); //MODIFICATO myprintf } if(flag) { filt_kill=1; return; } liv4(proto,len-ihl,p+ihl, id); return; // IP v6 case 0x86dd: if(!r_ipv6) return; printf("\n\nIPV6\n\n"); //increase counter counter.ipv6++; //start decoding flag=0; for(aux_ipv6=filt_ipv6;aux_ipv6!=NULL;aux_ipv6=aux_ipv6->next) { i=aux_ipv6->scid; j=i/8; r=i%8; mp=p+8; for(k=0;k<j;k++) if(*(mp+k)!=aux_ipv6->sip[k]) break; if(k!=j||((*(mp+j))&mask[r])!=(aux_ipv6->sip[j]&mask[r])) { flag=1; continue; } i=aux_ipv6->dcid; j=i/8; r=i%8; mp=p+24; for(k=0;k<j;k++) if(*(mp+k)!=aux_ipv6->dip[k]) break; if(k!=j||((*(mp+j))&mask[r])!=(aux_ipv6->dip[j]&mask[r])) { flag=1; continue; } flag=0; break; } ttl=*(p+7); proto=*(p+6); flow=ntohl(*(u_long *)(p))&0x00ffffff; len=ntohs(*(u_int *)(p+4)); if(p_ipv6) { colore(3); myprintf("IPv6 |"); //MODIFICATO myprintf print_ipv6(p+8); myprintf(" -> "); //MODIFICATO myprintf print_ipv6(p+24); myprintf(" Proto:%d Hop:%d Len:%d Flow:%ld\n",proto,ttl,len,flow); //MODIFICATO myprintf } if(flag) { filt_kill=1; return; } liv4(proto,len-40,p+40, 0); return; case 0x0806: if(!p_arp) return; printf("\n\nARP\n\n"); //increase counter counter.arp++; //start decoding colore(3); myprintf("ARP |"); //MODIFICATO myprintf switch(htons(*(u_int *)(p+6))) { case 1: myprintf("Request "); break; case 2: myprintf("Reply "); break; case 3: myprintf("R_Request "); break; case 4: myprintf("R_Reply "); break; } myprintf(" "); //MODIFICATO myprintf print_liv2(p+8); myprintf(" -> "); //MODIFICATO myprintf print_liv2(p+18); myprintf(" "); //MODIFICATO myprintf print_ipv4(p+14); myprintf(" -> "); //MODIFICATO myprintf print_ipv4(p+24); myprintf("\n"); //MODIFICATO myprintf decoded=1; return; default: //increasing counter counter.unknown++; unknown=1; return; } }
/* for local debug */ void ofp_print_packet_buffer(const char *comment, uint8_t *p) { static int first = 1; FILE *f; struct ofp_ip *ip; uint16_t proto; char *g; /* * Filter "noise" */ #if 0 if (p[12] == 0x00 && p[13] == 0x27) return; if (p[12] == 0x01 && p[13] == 0x98) return; #endif if (first) { f = fopen(DEFAULT_DEBUG_TXT_FILE_NAME, "w"); fclose(f); first = 0; } f = fopen(DEFAULT_DEBUG_TXT_FILE_NAME, "a"); if (!f) return; static struct timeval tv0; struct timeval tv; gettimeofday(&tv, NULL); if (tv0.tv_sec == 0) tv0 = tv; int ms = (tv.tv_sec*1000+tv.tv_usec/1000) - (tv0.tv_sec*1000+tv0.tv_usec/1000); ofp_printf(f, "\n*************\n"); ofp_printf(f, "[%d] %s: %d.%03d\n", odp_cpu_id(), comment, ms/1000, ms%1000); ofp_printf(f, "%s ->%s\n ", ofp_print_mac(p+6), ofp_print_mac(p)); if (p[12] == 0x81 && p[13] == 0x00) { ofp_printf(f, "VLAN %d ", (p[14]<<8)|p[15]); p += 4; } if (p[12] == 0x88 && p[13] == 0x47) { uint8_t *label = p+14; int i; ofp_printf(f, "MPLS "); while (1) { ofp_printf(f, "[label=%d ttl=%d] ", label[0]*16*256 + label[1]*16 + label[2]/16, label[3]); if (label[2] & 1) break; label += 4; } if ((label[4] & 0xf0) == 0x40) { label[2] = 0x08; /* ipv4 */ label[3] = 0x00; } else { label[2] = 0x86; /* ipv6 */ label[3] = 0xdd; } label++; for (i = 0; i < 12; i++) *label-- = p[11 - i]; p = label+1; } if (p[12] == 0x08 && p[13] == 0x06) { print_arp(f, (char *)(p + L2_HEADER_NO_VLAN_SIZE)); } else if (p[12] == 0x86 && p[13] == 0xdd) { print_ipv6(f, (char *)(p + L2_HEADER_NO_VLAN_SIZE)); } else if (p[12] == 0x08 && p[13] == 0x00) { ip = (struct ofp_ip *)(p + L2_HEADER_NO_VLAN_SIZE); if (ip->ip_p == 47) { /* GRE */ g = ((char *)ip) + (ip->ip_hl << 2); g += print_gre(f, g, &proto); if (proto == 0x0800) print_ipv4(f, g); else if (proto == 0x86dd) print_ipv6(f, g); } else print_ipv4(f, (char *)(p + L2_HEADER_NO_VLAN_SIZE)); } else { ofp_printf(f, "UNKNOWN ETH PACKET TYPE 0x%02x%02x ", p[12], p[13]); } ofp_printf(f, "\n"); fclose(f); fflush(stdout); }