static void arp_process_func(void** state) { // Timer setting timer_init("IntelCore(TM) i5-4670 CPU @ 3.40GHz"); // Nic initialization void* malloc_pool = malloc(POOL_SIZE); init_memory_pool(POOL_SIZE, malloc_pool, 0); __nics[0] = malloc(sizeof(NIC)); __nic_count++; __nics[0]->mac = 0x74d4358f66cb; __nics[0]->pool_size = POOL_SIZE; __nics[0]->pool = malloc_pool; __nics[0]->output_buffer = fifo_create(8, malloc_pool); __nics[0]->config = map_create(8, NULL, NULL, __nics[0]->pool); // Arp request Packet* packet = nic_alloc(__nics[0], sizeof(arp_request_packet)); memcpy(packet->buffer + packet->start, arp_request_packet, sizeof(arp_request_packet)); Ether* ether = (Ether*)(packet->buffer + packet->start); ARP* arp = (ARP*)ether->payload; uint32_t addr = endian32(arp->tpa); nic_ip_add(__nics[0], addr); assert_true(arp_process(packet)); packet = fifo_pop(__nics[0]->output_buffer); assert_memory_equal(packet->buffer + packet->start, arp_reply_packet, 42); nic_free(packet); packet = NULL; // Arp response packet = nic_alloc(__nics[0], sizeof(arp_reply_packet)); memcpy(packet->buffer + packet->start, arp_reply_packet, sizeof(arp_reply_packet)); ether = (Ether*)(packet->buffer + packet->start); arp = (ARP*)ether->payload; addr = endian32(arp->tpa); nic_ip_add(__nics[0], addr); uint8_t comp_mac[6] = { 0xcb, 0x66, 0x8f, 0x35, 0xd4, 0x74 }; uint32_t sip = endian32(arp->spa); Map* arp_table = nic_config_get(packet->nic, "net.arp.arptable"); assert_true(arp_process(packet)); ARPEntity* entity = map_get(arp_table, (void*)(uintptr_t)sip); assert_memory_equal((uint8_t*)&entity->mac, comp_mac, 6); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; }
/*! \brief process an arp packet \arg1 [IN] arp packet \arg2 [IN] packet len */ RESULT process_arp_pkt( BYTE* pkt, SHORT len ) { RESULT _res_ = ERR_STATE; _res_ = arp_process( pkt + sizeof( eth_frame ), len ); if( _res_ == ARP_PKT_SEND_RESPONSE ) { /* Switch dest with src eth */ memcpy( pkt, pkt + 6, 6 ); memcpy( pkt + 6, __MAC, 6 ); _res_ = snd_packet( pkt, len ); } //yh_free( pkt, len ); return _res_; }
void net_handle(u8 *buf, u32 len) { ETH_HDR *p = (ETH_HDR *)buf; switch (HON(p->type)) { case PROTO_ARP: arp_process(buf,len); break; case PROTO_IP: ip_process(buf,len); break; default: break; } }
static void arp_get_mac_func(void** state) { //timer setting timer_init("IntelCore(TM) i5-4670 CPU @ 3.40GHz"); // Nic initialization void* malloc_pool = malloc(POOL_SIZE); init_memory_pool(POOL_SIZE, malloc_pool, 0); __nics[0] = malloc(sizeof(NIC)); __nic_count++; __nics[0]->mac = 0x74d4358f66cb; __nics[0]->pool_size = POOL_SIZE; __nics[0]->pool = malloc_pool; __nics[0]->output_buffer = fifo_create(8, malloc_pool); __nics[0]->config = map_create(8, NULL, NULL, __nics[0]->pool); Packet* packet = nic_alloc(__nics[0], sizeof(arp_reply_packet)); memcpy(packet->buffer + packet->start, arp_reply_packet, sizeof(arp_reply_packet)); Ether* ether = (Ether*)(packet->buffer + packet->start); ARP* arp = (ARP*)ether->payload; uint32_t addr = endian32(arp->tpa); nic_ip_add(__nics[0], addr); if(!arp_process(packet)) return; uint32_t spa = 0xc0a80a6f; uint32_t dpa = 0xc0a80a90; uint64_t comp_tha = 0x74d4358f66cb; uint64_t d_mac = arp_get_mac(__nics[0], dpa, spa); assert_memory_equal((uint8_t*)&d_mac, (uint8_t*)&comp_tha, 6); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; }
static void parp_redo(struct sk_buff *skb) { arp_process(skb); }
void process_packet(u_char *user, const struct pcap_pkthdr *h,const u_char *bytes) { arp_process(bytes,h->caplen); }
void process(NetworkInterface* ni) { Packet* packet = ni_input(ni); if(!packet) return; if(arp_process(packet)) return; Ether* ether = (Ether*)(packet->buffer + packet->start); /* if(endian16(ether->type) == ETHER_TYPE_ARP) { // ARP response ARP* arp = (ARP*)ether->payload; if(endian16(arp->operation) == 1 && endian32(arp->tpa) == address) { ether->dmac = ether->smac; ether->smac = endian48(ni->mac); arp->operation = endian16(2); arp->tha = arp->sha; arp->tpa = arp->spa; arp->sha = ether->smac; arp->spa = endian32(address); printf("- Ethernet Header -\n"); printf("Dst Mac : %012lx Src Mac : %012lx\n Ether Type : %04hx\n", endian48(ether->dmac), endian48(ether->smac), ether->type); ni_output(ni, packet); packet = NULL; } }*/ if(endian16(ether->type) == ETHER_TYPE_IPv4) { IP* ip = (IP*)ether->payload; if(ip->protocol == IP_PROTOCOL_ICMP && endian32(ip->destination) == address){ // Echo reply ICMP* icmp = (ICMP*)ip->body; // Performance check // perf(); icmp->type = 0; icmp->checksum = 0; icmp->checksum = endian16(checksum(icmp, packet->end - packet->start - ETHER_LEN - IP_LEN)); ip->destination = ip->source; ip->source = endian32(address); ip->ttl = endian8(64); ip->checksum = 0; ip->checksum = endian16(checksum(ip, ip->ihl * 4)); ether->dmac = ether->smac; ether->smac = endian48(ni->mac); ni_output(ni, packet); packet = NULL; } /* } else if(ip->protocol == IP_PROTOCOL_TCP) { TCP* tcp = (TCP*)ip->body; printf("source=%u, destination=%u, sequence=%u, acknowledgement=%u\n", endian16(tcp->source), endian16(tcp->destination), endian32(tcp->sequence), endian32(tcp->acknowledgement)); printf("offset=%d, ns=%d, cwr=%d, ece=%d, urg=%d, ack=%d, psh=%d, rst=%d, syn=%d, fin=%d\n", tcp->offset, tcp->ns, tcp->cwr, tcp->ece, tcp->urg, tcp->ack, tcp->psh, tcp->rst, tcp->syn, tcp->fin); printf("window=%d, checksum=%x, urgent=%d\n", endian16(tcp->window), endian16(tcp->checksum), endian16(tcp->urgent)); */ } if(packet) ni_free(packet); }
void process(NetworkInterface* ni) { Packet* packet = ni_input(ni); if(!packet) return; if(arp_process(packet)) return; Ether* ether = (Ether*)(packet->buffer + packet->start); if(endian16(ether->type) == ETHER_TYPE_IPv4) { IP* ip = (IP*)ether->payload; if(ip->protocol == IP_PROTOCOL_UDP) { UDP* udp = (UDP*)ip->body; if(endian16(udp->destination) == SETKEY_PORT_NUM) { int orig_len = endian16(ip->length); Parameter* parameter = (Parameter*)udp->body; // ARP Request if(arp_request(ni, parameter->dst_ip) == true); //printf("ARP Request for destination IP\n"); int result = parse(parameter); memcpy(&(udp->body), &result, sizeof(result)); #ifdef _DEBUG_ for(int i = 0; i < 64 /* Packet Minimum Size */ - ETHER_LEN /* 14 */ - IP_LEN /* 20 */ - UDP_LEN /* 8 */ - sizeof(result); i++) udp->body[i + 4] = i; #endif uint16_t t = udp->destination; udp->destination = udp->source; udp->source = t; udp->checksum = 0; udp->length = endian16(64 - ETHER_LEN - IP_LEN); uint32_t t2 = ip->destination; ip->destination = ip->source; ip->source = t2; ip->ttl = 0x40; ip->length = endian16(64 - ETHER_LEN); ip->checksum = 0; ip->checksum = endian16(checksum(ip, ip->ihl * 4)); uint64_t t3 = ether->dmac; ether->dmac = ether->smac; ether->smac = t3; packet->end += (endian16(ip->length) - orig_len); ni_output(ni, packet); packet = NULL; } } if(ip->protocol == IP_PROTOCOL_ESP){ int orig_len = endian16(ip->length); #ifdef _DEBUG_ printf("Before Decryption - Packet : \n"); for(int i = 1; i < 1 + endian16(ip->length); i++) { printf("%02x ", ether->payload[i - 1]); // Packet - IP Header if( i % 16 == 0 ) printf("\n"); } printf("\n"); #endif if(decrypt(ip) >= 0) { packet->end += (endian16(ip->length) - orig_len); // ether->dmac = endian48(arp_get_mac(ni, endian32(ip->destination))); // ether->smac = endian48(ni->mac); #ifdef _DEBUG_ printf("- Ethernet Header After Decryption-\n"); printf("Dst Mac : %012lx Src Mac : %012lx\n Ether Type : %x\n", endian48(ether->dmac), endian48(ether->smac), ether->type); #endif if(ip->protocol == IP_PROTOCOL_ICMP){ // Echo reply ICMP* icmp = (ICMP*)ip->body; icmp->type = 0; icmp->checksum = 0; icmp->checksum = endian16(checksum(icmp, packet->end - packet->start - ETHER_LEN - IP_LEN)); uint32_t temp = ip->destination; ip->destination = ip->source; ip->source = temp; ip->ttl = endian8(64); ip->checksum = 0; ip->checksum = endian16(checksum(ip, ip->ihl * 4)); // ether->dmac = ether->smac; // ether->smac = endian48(ni->mac); // ni_output(ni, packet); // packet = NULL; } // ni_output(ni1, packet); // packet = NULL; } orig_len = endian16(ip->length); #ifdef _DEBUG_ printf("Before Encryption - Packet : \n"); for(int i = 1; i < 1 + endian16(ip->length); i++) { printf("%02x ", ether->payload[i - 1]); // Packet - IP Header if( i % 16 == 0 ) printf("\n"); } printf("\n"); #endif if(encrypt(ip) >= 0) { packet->end += (endian16(ip->length) - orig_len); ether->dmac = endian48(arp_get_mac(ni, endian32(ip->destination))); ether->smac = endian48(ni->mac); #ifdef _DEBUG_ printf("- Ethernet Header After Encryption-\n"); printf("Dst Mac : %012lx Src Mac : %012lx\n Ether Type : %x\n", endian48(ether->dmac), endian48(ether->smac), ether->type); #endif ni_output(ni, packet); packet = NULL; } } } if(packet) ni_free(packet); }
int main(void) { unsigned int num_bytes ; WDTCTL = WDTPW + WDTHOLD ; Init_CLK() ; Init_PSPI() ; P9DIR |= IrDA_OUT; P9OUT &=~IrDA_OUT; MAIN_POWER_ON ; /* for(;;){ for(i=0;i<40;i++) { P9OUT |= IrDA_OUT; __delay_cycles(20000); P9OUT &=~IrDA_OUT; __delay_cycles(20000); } __delay_cycles(5000000); } */ /* for(;;){ for(j=0;j<40;j++){ for(i=0;i<50;i++) { P9OUT &=~IrDA_OUT; __delay_cycles(270); P9OUT |= IrDA_OUT; __delay_cycles(270); } __delay_cycles(20000); } __delay_cycles(5000000); } */ myNode.node.ip = IP4_ADDR(103,1,168,192) ; myNode.node.mask = IP4_ADDR(0,255,255,255) ; myNode.node.gate = IP4_ADDR(1,1,168,192) ; myNode.node.mac[0] = 0x00 ; myNode.node.mac[1] = 0x60 ; myNode.node.mac[2] = 0x97 ; myNode.node.mac[3] = 0x48 ; myNode.node.mac[4] = 0xF4 ; myNode.node.mac[5] = 0xAB ; encInit() ; nicSetMacAddress() ; encPhyWrite(0x14,0x0470) ; arptab_init() ; ENC_SLEEP() ;// TEST ENC_WAKEUP();// TEST while(1) { num_bytes = encPacketReceive(&rxdnetbuf); if(num_bytes > 0) { if(rxdnetbuf.etherframe.protocal==ARP_PACKET) // 收到一个AR请求包 { if(rxdnetbuf.arpframe.operation==0x0100) arp_answer() ; // ARP request,处理arp数据包 else if(rxdnetbuf.arpframe.operation==0x0200) arp_process() ; // ARP answer } else if( (rxdnetbuf.etherframe.protocal == IP_PACKET) // 收到一个IP包 && ((rxdnetbuf.ipframe.verandihl&0xf0) == 0x40) // IP V4 && (rxdnetbuf.ipframe.destip[0] == myNode.nodebytes.ipbytes[0]) && (rxdnetbuf.ipframe.destip[1] == myNode.nodebytes.ipbytes[1]) && (rxdnetbuf.ipframe.destip[2] == myNode.nodebytes.ipbytes[2]) && (rxdnetbuf.ipframe.destip[3] == myNode.nodebytes.ipbytes[3])) { arp_ip_mac() ; if(rxdnetbuf.ipframe.protocal == 1) // ICMP { if(rxdnetbuf.icmpframe.type == 8) // ECHO ping_answer() ; } else if(rxdnetbuf.ipframe.protocal == 17) // UDP { // net_udp_rcv() ; _NOP(); // Clear_UPD_Buf() ; } /* else if(rxdnetbuf.ipframe.protocal == 6) // TCP tcp_rcve(&rxdnetbuf) ; updatearptab() ; */ } } } }
static void parp_redo(struct sk_buff *skb) { arp_process(dev_net(skb->dev), NULL, skb); }