static void process_pkt(void* data) { struct pkt_buff pkt; struct ioq_header *ioq = data; unsigned int size = ntohs(ioq->byte_length); unsigned short dest = ntohs(ioq->src_port) ^ 2; //print_pkt(data, size + sizeof(struct ioq_header)); log("%hd -> %hd (%d)\n", ntohs(ioq->src_port), dest, size); // log("ioq_hdr: dst=%hx words=%hu src=%hu bytes=%hu\n", ntohs(ioq->dst_port), ntohs(ioq->word_length), ntohs(ioq->src_port), size); pkt_fill(&pkt, data, size + sizeof(struct ioq_header)); struct pkt_buff reply; struct ioq_header *dioq; pkt_push_all(&pkt); while (pkt_alloc(&reply, pkt.len) == 0) { log("Failed to alloc\n"); } memcpy32(reply.data+ sizeof(struct ioq_header), pkt.data + sizeof(struct ioq_header), reply.len - sizeof(struct ioq_header)); nf_pktin_free(data); dioq = pkt_pull_type(&reply, struct ioq_header); fill_ioq(dioq, dest, size); nf_pktout_send(reply.data, reply.data + reply.total_size); }
int main(void) { if (nf_tid() != 0) { while (1) {} } nf_lock(LOCK_INIT); nf_pktout_init(); nf_pktin_init(); nf_unlock(LOCK_INIT); t_addr *pkt = nf_pktout_alloc(SIZE); fill_ioq((struct ioq_header *)pkt, 0, SIZE-sizeof(struct ioq_header)); nf_pktout_send(pkt, pkt + SIZE); nf_pktout_send(pkt, pkt + SIZE); nf_pktout_send(pkt, pkt + SIZE); nf_pktout_send(pkt, pkt + SIZE); while (1) {} return 0; }
int process_arp(struct net_iface *iface, struct ioq_header *ioq, struct ether_header *eth, struct pkt_buff *pkt) { unsigned short int my_hrd; unsigned short int my_pro; unsigned short int my_op; struct ether_arp *etharp; //struct pkt_buff reply; t_addr *reply; struct ether_header *reth; struct ether_arp *rarp; my_hrd = 6; // set to mac(6) my_pro = 4; // set to ipv4(4) my_op = ARPOP_REPLY; // set to reply etharp = pkt_pull(pkt, sizeof(struct ether_arp)); //if(ntohs(etharp->ea_hdr.ar_hrd) != ARPHRD_ETHER && // ntohs(etharp->ea_hdr.ar_pro) != ETHERTYPE_IP && // etharp->ea_hdr.ar_hln != 6 && // etharp->ea_hdr.ar_pln != 4 && // ( // ntohs(etharp->ea_hdr.ar_op) != ARPOP_REPLY || // ntohs(etharp->ea_hdr.ar_op) != ARPOP_REQUEST // ) // ) //{ //} //if(memcmp(htonl(etharp->arp_tpa), iface->ip, 4) == 0) //{ //if(etharp->ea_hdr.ar_op == ARPOP_REQUEST) //{ reply = nf_pktout_alloc(PKT_SIZE); // setup the ioq_header fill_ioq((struct ioq_header*) reply, 2, PKT_SIZE); // setup the ethernet header reth = (struct ether_header*) (reply + sizeof(struct ioq_header)); // setup the ethernet arp rarp = (struct ether_arp*) (reply + sizeof(struct ioq_header) + sizeof(struct ether_header)); // start putting things into the packet // ethernet memcpy(reth->ether_shost, iface->mac, ETH_ALEN); memcpy(reth->ether_dhost, eth->ether_shost, ETH_ALEN); reth->ether_type = ETHERTYPE_ARP; // arp header rarp->ea_hdr.ar_hrd = htons(ARPHRD_ETHER); rarp->ea_hdr.ar_pro = htons(ETHERTYPE_IP); rarp->ea_hdr.ar_hln = 6; rarp->ea_hdr.ar_pln = 4; rarp->ea_hdr.ar_op = htons(ARPOP_REPLY); // arp ethernet // source memcpy(rarp->arp_sha, iface->mac, ETH_ALEN); memcpy(rarp->arp_spa, iface->ip, 4); // target memcpy(rarp->arp_tha, etharp->arp_sha, ETH_ALEN); memcpy(rarp->arp_tpa, etharp->arp_spa, 4); // send it nf_pktout_send(reply, reply + PKT_SIZE); //nf_pktout_send(reply, reply + PKT_SIZE); //nf_pktout_send(reply, reply + PKT_SIZE); //nf_pktout_send(reply, reply + PKT_SIZE); //} //} }
int main(void) { t_addr *pkt; struct net_iface iface; struct ether_header *reth; struct ether_arp *rarp; unsigned char dest_mac[6]; unsigned char dest_ip[4]; //u_int16_t ethtype; //#ifndef DEBUG // t_addr* next_packet; // int i=nf_tid(); // // if(i== 0) // { // nf_lock(LOCK_INIT); // should get it on the first attempt // nf_pktout_init(); // nf_pktin_init(); // } // else // { // nf_stall_a_bit(); // nf_lock(LOCK_INIT); // should not get it // } // nf_unlock(LOCK_INIT); // //#endif //test_this(); // iface is not shared, it's on the stack arp_init(&iface.arp); iface.mac[0] = 0x00; iface.mac[1] = 0x43; iface.mac[2] = 0x32; iface.mac[3] = 0x46; iface.mac[4] = 0x4e; iface.mac[5] = 0x00; iface.ip[0] = 192; iface.ip[1] = 168; iface.ip[2] = 0; iface.ip[3] = 100; //dest_mac[0] = 0x70; //dest_mac[1] = 0x56; //dest_mac[2] = 0x81; //dest_mac[3] = 0xa9; //dest_mac[4] = 0x2c; //dest_mac[5] = 0x39; dest_mac[0] = 0xff; dest_mac[1] = 0xff; dest_mac[2] = 0xff; dest_mac[3] = 0xff; dest_mac[4] = 0xff; dest_mac[5] = 0xff; dest_ip[0] = 192; dest_ip[1] = 168; dest_ip[2] = 0; dest_ip[3] = 185; //ethtype = ETHERTYPE_ARP; //only run this program on thread 0 if (nf_tid() != 0) { while (1) {} } // initialize nf_pktout_init(); nf_pktin_init(); // allocate an output buffer pkt = nf_pktout_alloc(PKT_SIZE); // setup the ioq_header fill_ioq((struct ioq_header*) pkt, 2, PKT_SIZE); // setup the ethernet header //struct ether_header *reth = (struct ether_header*) (pkt + sizeof(struct ioq_header)); reth = (struct ether_header*) (pkt + sizeof(struct ioq_header)); // setup the ethernet arp //struct ether_arp *rarp = (struct ether_arp*) (pkt + sizeof(struct ioq_header) + sizeof(struct ether_header)); rarp = (struct ether_arp*) (pkt + sizeof(struct ioq_header) + sizeof(struct ether_header)); // start putting things into the packet // ethernet memcpy(reth->ether_shost, &iface.mac, ETH_ALEN); memcpy(reth->ether_dhost, &dest_mac, ETH_ALEN); //memcpy(reth->ether_type, htons(ethtype), sizeof(u_int16_t)); //memcpy(reth->ether_type, ðtype, 2); //memcpy(reth->ether_type, &mytype, 2); reth->ether_type = ETHERTYPE_ARP; // arp header rarp->ea_hdr.ar_hrd = htons(ARPHRD_ETHER); rarp->ea_hdr.ar_pro = htons(ETHERTYPE_IP); //rarp->ea_hdr.ar_pro = htons(0x0800); rarp->ea_hdr.ar_hln = 6; rarp->ea_hdr.ar_pln = 4; rarp->ea_hdr.ar_op = htons(ARPOP_REQUEST); // arp ethernet // source memcpy(rarp->arp_sha, &iface.mac, ETH_ALEN); memcpy(rarp->arp_spa, &iface.ip, 4); // target memcpy(rarp->arp_tha, dest_mac, ETH_ALEN); memcpy(rarp->arp_tpa, dest_ip, 4); // send it nf_pktout_send(pkt, pkt + PKT_SIZE); nf_pktout_send(pkt, pkt + PKT_SIZE); nf_pktout_send(pkt, pkt + PKT_SIZE); nf_pktout_send(pkt, pkt + PKT_SIZE); //free //nf_pktin_free(pkt); // start in on replying //while(1) //{ // pkt = nf_pktin_pop(); // test for next_packet // if(!nf_pktin_is_valid(pkt)) // continue; // process_eth(&iface, pkt); // nf_pktin_free(pkt); //} //memcpy(reply.data, pkt->head, pkt->len); //struct ioq_header *reply_ioq = pkt_pull(&reply, sizeof(struct ioq_header)); //fill_ioq(reply_ioq, ioq->src_port, reply.len); //struct ether_header *reply_ether = pkt_pull(&reply, sizeof(struct ether_header)); //struct ether_arp *reply_arp = pkt_pull(&reply, sizeof(struct ether_arp)); // copy source ip and mac to target //memcpy(reply_arp->arp_tha, reply_arp->arp_sha, ETH_ALEN); //memcpy(reply_arp->arp_tpa, reply_arp->arp_spa, 4); // copy this interface's mac and ip to source //memcpy(reply_arp->arp_sha, iface->mac, ETH_ALEN); //memcpy(reply_arp->arp_spa, iface->ip, 4); //reply_arp->ea_hdr.ar_op = htons(ARPOP_REPLY); //memcpy(reply_ether->ether_shost, iface->mac, ETH_ALEN); //memcpy(reply_ether->ether_dhost, reply_arp->arp_tha, ETH_ALEN); //pkt_push_all(&reply); //send_pkt(reply.data, reply.len); return 0; }
int process_arp(struct net_iface *iface, struct ioq_header *ioq, struct ether_header *eth, struct ether_arp *etharp, t_addr *pkt) { unsigned short int my_hrd; unsigned short int my_pro; unsigned short int my_op; t_addr *reply; struct ether_header *reth; struct ether_arp *rarp; my_hrd = 6; // set to mac(6) my_pro = 4; // set to ipv4(4) my_op = ARPOP_REPLY; // set to reply // if this isn't an ARP reply or request we don't care if(ntohs(etharp->ea_hdr.ar_hrd) != ARPHRD_ETHER || ntohs(etharp->ea_hdr.ar_pro) != ETHERTYPE_IP || etharp->ea_hdr.ar_hln != my_hrd || etharp->ea_hdr.ar_pln != my_pro || ( ntohs(etharp->ea_hdr.ar_op) != ARPOP_REPLY && ntohs(etharp->ea_hdr.ar_op) != ARPOP_REQUEST ) ) { return 1; } if(memcmp(htonl(etharp->arp_tpa), iface->ip, 4) == 0) { if(ntohs(etharp->ea_hdr.ar_op) == ARPOP_REQUEST) { // allocate reply size reply = nf_pktout_alloc(PKT_SIZE); // setup the ioq_header fill_ioq((struct ioq_header*) reply, 2, PKT_SIZE); // setup the ethernet header reth = (struct ether_header*) (reply + sizeof(struct ioq_header)); // setup the ethernet arp rarp = (struct ether_arp*) (reply + sizeof(struct ioq_header) + sizeof(struct ether_header)); // start putting things into the packet // ethernet memcpy(reth->ether_shost, iface->mac, ETH_ALEN); memcpy(reth->ether_dhost, eth->ether_shost, ETH_ALEN); reth->ether_type = ETHERTYPE_ARP; // arp header rarp->ea_hdr.ar_hrd = htons(ARPHRD_ETHER); rarp->ea_hdr.ar_pro = htons(ETHERTYPE_IP); rarp->ea_hdr.ar_hln = 6; rarp->ea_hdr.ar_pln = 4; rarp->ea_hdr.ar_op = htons(ARPOP_REPLY); // arp ethernet // source memcpy(rarp->arp_sha, iface->mac, ETH_ALEN); memcpy(rarp->arp_spa, iface->ip, 4); // target memcpy(rarp->arp_tha, etharp->arp_sha, ETH_ALEN); memcpy(rarp->arp_tpa, etharp->arp_spa, 4); // send it nf_pktout_send(reply, reply + PKT_SIZE); //nf_pktout_send(reply, reply + PKT_SIZE); //nf_pktout_send(reply, reply + PKT_SIZE); //nf_pktout_send(reply, reply + PKT_SIZE); } } }
int main(void) { t_addr *pkt; struct net_iface iface; struct ether_header *reth; struct ether_arp *rarp; unsigned char dest_mac[6]; unsigned char dest_ip[4]; // iface is not shared, it's on the stack arp_init(&iface.arp); iface.mac[0] = 0x00; iface.mac[1] = 0x43; iface.mac[2] = 0x32; iface.mac[3] = 0x46; iface.mac[4] = 0x4e; iface.mac[5] = 0x00; iface.ip[0] = 192; iface.ip[1] = 168; iface.ip[2] = 0; iface.ip[3] = 100; dest_mac[0] = 0xff; dest_mac[1] = 0xff; dest_mac[2] = 0xff; dest_mac[3] = 0xff; dest_mac[4] = 0xff; dest_mac[5] = 0xff; dest_ip[0] = 192; dest_ip[1] = 168; dest_ip[2] = 0; dest_ip[3] = 185; //only run this program on thread 0 if (nf_tid() != 0) { while (1) {} } // initialize nf_pktout_init(); nf_pktin_init(); // This is to just send an ARP request start uncommenting to play // allocate an output buffer //t_addr *pkt = nf_pktout_alloc(PKT_SIZE); pkt = nf_pktout_alloc(PKT_SIZE); // setup the ioq_header fill_ioq((struct ioq_header*) pkt, 2, PKT_SIZE); // setup the ethernet header //struct ether_header *reth = (struct ether_header*) (pkt + sizeof(struct ioq_header)); reth = (struct ether_header*) (pkt + sizeof(struct ioq_header)); // setup the ethernet arp //struct ether_arp *rarp = (struct ether_arp*) (pkt + sizeof(struct ioq_header) + sizeof(struct ether_header)); rarp = (struct ether_arp*) (pkt + sizeof(struct ioq_header) + sizeof(struct ether_header)); // start putting things into the packet // ethernet memcpy(reth->ether_shost, &iface.mac, ETH_ALEN); memcpy(reth->ether_dhost, &dest_mac, ETH_ALEN); //memcpy(reth->ether_type, htons(ethtype), sizeof(u_int16_t)); //memcpy(reth->ether_type, ðtype, 2); //memcpy(reth->ether_type, &mytype, 2); reth->ether_type = ETHERTYPE_ARP; // arp header rarp->ea_hdr.ar_hrd = htons(ARPHRD_ETHER); rarp->ea_hdr.ar_pro = htons(ETHERTYPE_IP); //rarp->ea_hdr.ar_pro = htons(0x0800); rarp->ea_hdr.ar_hln = 6; rarp->ea_hdr.ar_pln = 4; rarp->ea_hdr.ar_op = htons(ARPOP_REQUEST); // arp ethernet // source memcpy(rarp->arp_sha, &iface.mac, ETH_ALEN); memcpy(rarp->arp_spa, &iface.ip, 4); // target memcpy(rarp->arp_tha, dest_mac, ETH_ALEN); memcpy(rarp->arp_tpa, dest_ip, 4); // send it nf_pktout_send(pkt, pkt + PKT_SIZE); nf_pktout_send(pkt, pkt + PKT_SIZE); nf_pktout_send(pkt, pkt + PKT_SIZE); nf_pktout_send(pkt, pkt + PKT_SIZE); //end uncomment // start in on replying while(1) { pkt = nf_pktin_pop(); // test for next_packet if(!nf_pktin_is_valid(pkt)) continue; process_eth(&iface, pkt); nf_pktin_free(pkt); } return 0; }
int main() { int mytid = nf_tid(); if(mytid != 0) { nf_stall_a_bit(); nf_lock(LOCK_INIT); // should not get it } else { nf_lock(LOCK_INIT); // should get it on the first attempt nf_pktout_init(); nf_pktin_init(); } nf_unlock(LOCK_INIT); //only run this program on thread 0 //if (nf_tid() != 0) //{ // while (1) {} //} //// initialize //nf_pktout_init(); //nf_pktin_init(); // allocate an output buffer t_addr *pkt = nf_pktout_alloc(PKT_SIZE); // setup the ioq_header fill_ioq((struct ioq_header*)pkt, 2, sizeof(struct netfpga_to_driver)); // get a pointer to the payload struct struct netfpga_to_driver* n2d = (struct netfpga_to_driver*) (pkt + sizeof(struct ioq_header)); // initialize the message memset(n2d->str, 0, STR_SIZE); memcpy(n2d->str, mystr, strlen(mystr)); //n2d->str[strlen(mystr)+1] = nf_tid() + 0x30; //thread id in ascii n2d->str[strlen(mystr)+1] = mytid + 0x30; //thread id in ascii // send it nf_pktout_send(pkt, pkt + PKT_SIZE); //int i = 0; //int mytid; //struct netfpga_to_driver* n2d; //t_addr *pkt; //while(i <= 1) //{ // mytid = nf_tid(); // // if(mytid != 0) // { // nf_stall_a_bit(); // nf_lock(LOCK_INIT); // should not get it // } // else // { // nf_lock(LOCK_INIT); // should get it on the first attempt // nf_pktout_init(); // nf_pktin_init(); // } // nf_unlock(LOCK_INIT); // // allocate an output buffer // pkt = nf_pktout_alloc(PKT_SIZE); // // setup the ioq_header // fill_ioq((struct ioq_header*)pkt, 2, sizeof(struct netfpga_to_driver)); // // get a pointer to the payload struct // n2d = // (struct netfpga_to_driver*) (pkt + sizeof(struct ioq_header)); // // initialize the message // memset(n2d->str, 0, STR_SIZE); // memcpy(n2d->str, mystr, strlen(mystr)); // //n2d->str[strlen(mystr)+1] = nf_tid() + 0x30; //thread id in ascii // n2d->str[strlen(mystr)+1] = mytid + 0x30; //thread id in ascii // // send it // nf_pktout_send(pkt, pkt + PKT_SIZE); // i = i + 1; //} //#else // int mytid = nf_tid(); // // if(mytid != 0) // { // nf_stall_a_bit(); // nf_lock(LOCK_INIT); // should not get it // } // else // { // nf_lock(LOCK_INIT); // should get it on the first attempt // nf_pktout_init(); // nf_pktin_init(); // } // nf_unlock(LOCK_INIT); // t_addr *pkt = nf_pktout_alloc(PKT_SIZE); // // // setup the ioq_header // fill_ioq((struct ioq_header*)pkt, 2, sizeof(struct netfpga_to_driver)); // // // get a pointer to the payload struct // struct netfpga_to_driver* n2d = // (struct netfpga_to_driver*) (pkt + sizeof(struct ioq_header)); // // // initialize the message // memset(n2d->str, 0, STR_SIZE); // memcpy(n2d->str, mystr, strlen(mystr)); // //n2d->str[strlen(mystr)+1] = nf_tid() + 0x30; //thread id in ascii // n2d->str[strlen(mystr)+1] = mytid + 0x30; //thread id in ascii // nf_pktout_send(pkt, pkt + PKT_SIZE); //#endif return 0; }