/* Send ARP request if the request in our cache is not sent more than 5 times */ void handle_arpreq (struct sr_arpreq * req, struct sr_instance *sr) { struct sr_arpcache *sr_cache = &sr->cache; time_t curr_time; time(&curr_time); double one_sec = 1.0; if (difftime(curr_time, req->sent) > one_sec){ struct sr_packet *packet = req->packets; /* If packet is sent more than equal or more than 5 times, send ICMP host unreachable message */ if ((req->times_sent) >= 5) { printf("Packet sent more than 5 times\n"); while (packet) { uint8_t *buf = packet->buf; char *interface = packet->iface; int packet_len = sizeof(sr_ethernet_hdr_t) + sizeof(sr_ip_hdr_t) + sizeof(sr_icmp_t3_hdr_t); uint8_t *new_packet = malloc(packet_len); /* Get Ethernet header */ sr_ethernet_hdr_t* eth_hdr = get_eth_hdr(buf); /* Get IP header */ sr_ip_hdr_t * ip_hdr = get_ip_hdr(buf); /* Create ethernet header */ create_ethernet_header (eth_hdr, new_packet, eth_hdr->ether_dhost, eth_hdr->ether_shost, htons(ethertype_ip)); /* Create IP header */ create_ip_header (ip_hdr, new_packet, sr_get_interface(sr, interface)->ip, ip_hdr->ip_src); /* Create ICMP Header */ create_icmp_type3_header (ip_hdr, new_packet, dest_host_unreachable_type, dest_host_unreachable_code); /* Look up routing table for rt entry that is mapped to the source of received packet */ struct sr_rt *src_lpm = sr_routing_lpm(sr, ip_hdr->ip_src); /* Send ICMP host unreachable message */ send_icmp_type3_msg (new_packet, src_lpm, sr_cache, sr, interface, packet_len); free(new_packet); packet = packet->next; } sr_arpreq_destroy(sr_cache, req); } else { /* Send out arp request */ struct sr_if *target_iface = sr_get_interface(sr, packet->iface); int packet_len = sizeof(sr_ethernet_hdr_t) + sizeof(sr_arp_hdr_t); uint8_t *new_packet = malloc(packet_len); /* Createn ethernet header */ sr_ethernet_hdr_t *new_eth_hdr = (sr_ethernet_hdr_t *) new_packet; memset(new_eth_hdr->ether_dhost, 255, sizeof(uint8_t)*ETHER_ADDR_LEN); memcpy(new_eth_hdr->ether_shost, target_iface->addr, sizeof(uint8_t)*ETHER_ADDR_LEN); new_eth_hdr->ether_type = htons(ethertype_arp); /* Create ARP header */ sr_arp_hdr_t *new_arp_hdr = (sr_arp_hdr_t *)(new_packet + sizeof(sr_ethernet_hdr_t)); new_arp_hdr->ar_hrd = htons(arp_hrd_ethernet); new_arp_hdr->ar_pro = htons(ethertype_ip); new_arp_hdr->ar_hln = ETHER_ADDR_LEN; new_arp_hdr->ar_pln = sizeof(uint32_t); new_arp_hdr->ar_op = htons(arp_op_request); memcpy(new_arp_hdr->ar_sha, target_iface->addr, sizeof(unsigned char)*ETHER_ADDR_LEN); new_arp_hdr->ar_sip = target_iface->ip; memset(new_arp_hdr->ar_tha, 255, sizeof(unsigned char)*ETHER_ADDR_LEN); new_arp_hdr->ar_tip = req->ip; sr_send_packet(sr, new_packet, packet_len, target_iface->name); free(new_packet); } req->sent = curr_time; req->times_sent = req->times_sent + 1; } }
/* handle/generate ARP packet */ void sr_handle_arppacket(struct sr_instance* sr, uint8_t * packet/* lent */, unsigned int len, char* interface/* lent */) { assert(sr); assert(packet); assert(interface); /* Get ethernet header */ sr_ethernet_hdr_t *eth_hdr = (sr_ethernet_hdr_t *)get_eth_hdr(packet); if (eth_hdr == NULL) { printf("ethernet header NULL!!!\n"); return; } /* Get arp header */ sr_arp_hdr_t *arp_hdr = (sr_arp_hdr_t *)get_arp_hdr(packet); if (arp_hdr == NULL) { printf("arp header NULL!!!\n"); return; } /* Check the arp packet minimum length */ if (!check_min_length(len, ARP_PACKET_LEN)) { fprintf(stderr, "arp packet length is not enough:(\n"); return; } /* check the opcode to see if it is request or reply */ unsigned short ar_op = ntohs(arp_hdr->ar_op); /* Get the connected interface in the router */ struct sr_if *sr_con_if = sr_get_interface(sr, interface); /* Get the detination interface in the router */ /* If the connected interface exists, because arp has to be the connected interface */ if (sr_con_if) { /* ********** ARP request ********** */ /* Construct an arp reply and send it back */ if (ar_op == arp_op_request) { /*fprintf(stderr, "********** ARP REQUEST **********\n"); ar_op = 1 */ /* Set the back-packet length */ int packet_len = ARP_PACKET_LEN; uint8_t *arp_reply_hdr = (uint8_t *)malloc(packet_len); /* Create ethernet header */ create_ethernet_hdr(eth_hdr, (sr_ethernet_hdr_t *)arp_reply_hdr, sr_con_if); /* Create arp header */ create_back_arp_hdr(arp_hdr, (sr_arp_hdr_t *)((unsigned char *)arp_reply_hdr+ETHER_PACKET_LEN), sr_con_if); /* Send APR reply */ sr_send_packet(sr, /*(sr_ethernet_hdr_t *)*/arp_reply_hdr, packet_len, sr_con_if->name); free(arp_reply_hdr); return; } /* ********** ARP reply ********** */ /* Cache it, go thru my request queue and send outstanding packets */ else if (ar_op == arp_op_reply) { /*fprintf(stderr, "********** ARP REPLY **********\n"); ar_op = 2 */ /* cache first, and send all the packets in the queue with ip->mac mapping!!! */ handle_arpreply(arp_hdr, sr); return; } /* ********** Otherwise, error! ********** */ else { fprintf(stderr, "Invalid arp type!!!\n"); return; } } else { fprintf(stderr, "Router doesnt have this interface, drop it!\n"); return; } return; }
/* Handle IP packet */ void sr_handle_ippacket(struct sr_instance* sr, uint8_t * packet/* lent */, unsigned int len, char* interface/* lent */) { assert(sr); assert(packet); assert(interface); /* Get ethernet header */ sr_ethernet_hdr_t *eth_hdr = get_eth_hdr(packet); if (eth_hdr == NULL) { printf("ethernet header NULL!!!\n"); return; } /* Get ip header */ sr_ip_hdr_t *ip_hdr = get_ip_hdr(packet); if (ip_hdr == NULL) { printf("ip header NULL!!!\n"); return; } /* Before doing ttl decrement, check checksum */ uint16_t old_ip_sum = ip_hdr->ip_sum; ip_hdr->ip_sum = 0; if (!verify_checksum(ip_hdr, sizeof(sr_ip_hdr_t), old_ip_sum)) { fprintf(stderr, "CHECKSUM FAILED!!\n"); return; } ip_hdr->ip_sum = old_ip_sum; /* Get the arp cache */ struct sr_arpcache *sr_arp_cache = &sr->cache; /* Get the destination interface on the router */ struct sr_if *sr_iface = sr_get_router_if(sr, ip_hdr->ip_dst); /* Get the connected interface on the router */ struct sr_if *sr_con_if = sr_get_interface(sr, interface); /* Check the time exceeded condition, if ttl==0, we need to form icmp 11 and send back */ if (ip_hdr->ip_ttl <= 1) { /* time exceeded message and icmp type 11 */ printf("TTL time exceeded\n"); int packet_len = ICMP_T3_PACKET_LEN; uint8_t *icmp_t3_hdr = (uint8_t *)malloc(packet_len); create_ethernet_hdr(eth_hdr, (sr_ethernet_hdr_t *)icmp_t3_hdr, sr_con_if); /* Create ip header */ create_echo_ip_hdr(ip_hdr, (sr_ip_hdr_t *)((char *)icmp_t3_hdr+ETHER_PACKET_LEN), sr_con_if); /* Send icmp type 11 time exceeded */ /* icmp_t3 type=11, code=0 */ create_icmp_t3_hdr(ip_hdr, (sr_icmp_t3_hdr_t *)((char *)icmp_t3_hdr+IP_PACKET_LEN), 11, 0); /* Send icmp type 11 packet */ struct sr_arpentry *arp_entry = sr_arpcache_lookup(sr_arp_cache, ip_hdr->ip_src); if (arp_entry != NULL) { sr_send_packet(sr, icmp_t3_hdr, packet_len, sr_con_if->name); free(icmp_t3_hdr); } else { struct sr_arpreq *arp_req = sr_arpcache_queuereq(sr_arp_cache, ip_hdr->ip_src, icmp_t3_hdr, packet_len, sr_con_if->name); handle_arpreq(arp_req, sr); } return; } /* Get the protocol from IP */ uint8_t ip_p = ip_hdr->ip_p; /* If the packet is sent to self, meaning the ip is sent to the router */ if (sr_iface) { /* Check the protocol if it is icmp */ if (ip_p == ip_protocol_icmp) { /* Get the icmp header */ sr_icmp_hdr_t *icmp_hdr = get_icmp_hdr(packet); /* Check if it is ICMP echo request */ /* icmp_echo_req = 8 */ if (icmp_hdr->icmp_type == 8) { /* Do LPM on the routing table */ /* Check the routing table and see if the incoming ip matches the routing table ip, and find LPM router entry */ struct sr_rt *longest_pref_match = sr_lpm(sr, ip_hdr->ip_src); if (longest_pref_match) { /* check ARP cache */ struct sr_arpentry *arp_entry = sr_arpcache_lookup(&sr->cache, longest_pref_match->gw.s_addr); struct sr_if *out_iface = sr_get_interface(sr, longest_pref_match->interface); /* If hit, meaning the arp mapping has been cached */ if (arp_entry != NULL) { /* We need to send the icmp echo reply */ /* Modify ethernet header */ memcpy(eth_hdr->ether_dhost, eth_hdr->ether_shost, ETHER_ADDR_LEN); memcpy(eth_hdr->ether_shost, out_iface->addr, ETHER_ADDR_LEN); /* Modify ip header */ ip_hdr->ip_off = htons(0b0100000000000000); /* fragment offset field */ ip_hdr->ip_ttl = 100; /* time to live */ uint32_t temp = ip_hdr->ip_src; ip_hdr->ip_src = ip_hdr->ip_dst; /* source address */ ip_hdr->ip_dst = temp; /* dest address */ ip_hdr->ip_sum = 0; ip_hdr->ip_sum = cksum(ip_hdr, sizeof(sr_ip_hdr_t)); /* checksum */ /* Modify icmp header */ unsigned int icmp_whole_size = len - IP_PACKET_LEN; icmp_hdr->icmp_type = 0; icmp_hdr->icmp_code = 0; icmp_hdr->icmp_sum = 0; icmp_hdr->icmp_sum = cksum(icmp_hdr, icmp_whole_size); /* Send icmp echo reply */ sr_send_packet(sr, packet, len, out_iface->name); return; } /* Else no hit, we cache it to the queue and send arp request */ else { /* Add reply to the ARP queue */ /* We need to send the icmp echo reply */ /* Modify ethernet header */ memcpy(eth_hdr->ether_dhost, eth_hdr->ether_shost, ETHER_ADDR_LEN); memcpy(eth_hdr->ether_shost, sr_con_if->addr, ETHER_ADDR_LEN); /* Modify ip header */ ip_hdr->ip_off = htons(0b0100000000000000); /* fragment offset field */ ip_hdr->ip_ttl = 100; /* time to live */ uint32_t temp = ip_hdr->ip_src; ip_hdr->ip_src = ip_hdr->ip_dst; /* source address */ ip_hdr->ip_dst = temp; /* dest address */ ip_hdr->ip_sum = 0; ip_hdr->ip_sum = cksum(ip_hdr, sizeof(sr_ip_hdr_t)); /* checksum */ /* Modify icmp header */ unsigned int icmp_whole_size = len - IP_PACKET_LEN; icmp_hdr->icmp_type = 0; icmp_hdr->icmp_code = 0; icmp_hdr->icmp_sum = 0; icmp_hdr->icmp_sum = cksum(icmp_hdr, icmp_whole_size); struct sr_arpreq *arp_req = sr_arpcache_queuereq(sr_arp_cache, ip_hdr->ip_dst, packet, len, out_iface->name); /* Send ARP request, which is a broadcast */ handle_arpreq(arp_req, sr); return; } } else { fprintf(stderr, "Longest prefix doesnt match!!\n"); return; } } else { fprintf(stderr, "Not an ICMP request!\n"); return; } } /* Else it is TCP/UDP request */ else { fprintf(stderr, "*** -> Received TCP/UDP!\n"); /* Do LPM on the routing table */ /* Check the routing table and see if the incoming ip matches the routing table ip, and find LPM router entry */ struct sr_rt *longest_pref_match = sr_lpm(sr, ip_hdr->ip_src); if (longest_pref_match) { /* check ARP cache */ struct sr_arpentry *arp_entry = sr_arpcache_lookup(&sr->cache, longest_pref_match->gw.s_addr); struct sr_if *out_iface = sr_get_interface(sr, longest_pref_match->interface); /* Send ICMP port unreachable */ if (arp_entry != NULL) { int packet_len = ICMP_T3_PACKET_LEN; uint8_t *icmp_t3_hdr = (uint8_t *)malloc(packet_len); /* Create ethernet header */ create_ethernet_hdr(eth_hdr, (sr_ethernet_hdr_t *)icmp_t3_hdr, sr_iface); /*memcpy(((sr_ethernet_hdr_t *)icmp_t3_hdr)->ether_dhost, eth_hdr->ether_shost, ETHER_ADDR_LEN); memcpy(((sr_ethernet_hdr_t *)icmp_t3_hdr)->ether_shost, eth_hdr->ether_dhost, ETHER_ADDR_LEN);*/ /* Create ip header */ create_echo_ip_hdr(ip_hdr, (sr_ip_hdr_t *)((char *)icmp_t3_hdr+ETHER_PACKET_LEN), sr_iface); /*sr_ip_hdr_t *icmp_t3_hdr_ip = (sr_ip_hdr_t *)((char *)icmp_t3_hdr+ETHER_PACKET_LEN); icmp_t3_hdr_ip->ip_src = ip_hdr->ip_dst; icmp_t3_hdr_ip->ip_sum = 0; icmp_t3_hdr_ip->ip_sum = cksum(icmp_t3_hdr_ip, sizeof(sr_ip_hdr_t));*/ /* Should update source address to be interface address */ /* Send icmp type 3 port unreachable */ /* Create icmp port unreachable packet */ /* icmp_t3 type=3, code=3 */ create_icmp_t3_hdr(ip_hdr, (sr_icmp_t3_hdr_t *)((char *)icmp_t3_hdr+IP_PACKET_LEN), 3, 3); /* Send icmp type 3 packet */ sr_send_packet(sr, icmp_t3_hdr, packet_len, out_iface->name); free(icmp_t3_hdr); return; } else { int packet_len = ICMP_T3_PACKET_LEN; uint8_t *icmp_t3_hdr = (uint8_t *)malloc(packet_len); /* Create ethernet header */ create_ethernet_hdr(eth_hdr, (sr_ethernet_hdr_t *)icmp_t3_hdr, sr_iface); /*memcpy(((sr_ethernet_hdr_t *)icmp_t3_hdr)->ether_dhost, eth_hdr->ether_shost, ETHER_ADDR_LEN); memcpy(((sr_ethernet_hdr_t *)icmp_t3_hdr)->ether_shost, eth_hdr->ether_dhost, ETHER_ADDR_LEN);*/ /* Create ip header */ create_echo_ip_hdr(ip_hdr, (sr_ip_hdr_t *)((char *)icmp_t3_hdr+ETHER_PACKET_LEN), sr_iface); /*sr_ip_hdr_t *icmp_t3_hdr_ip = (sr_ip_hdr_t *)((char *)icmp_t3_hdr+ETHER_PACKET_LEN); icmp_t3_hdr_ip->ip_src = ip_hdr->ip_dst; icmp_t3_hdr_ip->ip_sum = 0; icmp_t3_hdr_ip->ip_sum = cksum(icmp_t3_hdr_ip, sizeof(sr_ip_hdr_t));*/ /* Send icmp type 3 port unreachable */ /* Create icmp port unreachable packet */ /* icmp_t3 type=3, code=3 */ create_icmp_t3_hdr(ip_hdr, (sr_icmp_t3_hdr_t *)((char *)icmp_t3_hdr+IP_PACKET_LEN), 3, 3); struct sr_arpreq *arp_req = sr_arpcache_queuereq(sr_arp_cache, ip_hdr->ip_src, icmp_t3_hdr, packet_len, out_iface->name); /* Send ARP request, which is a broadcast */ handle_arpreq(arp_req, sr); return; } } else { fprintf(stderr, "Longest prefix doesnt match!!\n"); return; } } } /* Else Check the routing table, perfomr LPM */ else { /* Sanity-check the packet */ /* minimum length */ if (!check_min_length(len, IP_PACKET_LEN)) { fprintf(stderr, "The packet length is not enough:(\n"); return; } /* Do LPM on the routing table */ /* Check the routing table and see if the incoming ip matches the routing table ip, and find LPM router entry */ struct sr_rt *longest_pref_match = sr_lpm(sr, ip_hdr->ip_dst); if (longest_pref_match) { /* check ARP cache */ struct sr_if *out_iface = sr_get_interface(sr, longest_pref_match->interface); struct sr_arpentry *arp_entry = sr_arpcache_lookup(&sr->cache, longest_pref_match->gw.s_addr); /* ip_hdr->ip_dst */ /* If hit, meaning the arp_entry is found */ if (arp_entry) { /*fprintf(stderr, "************ found the lpm router entry ***********\n");*/ /* Send frame to next hop */ /* update the eth_hdr source and destination ethernet address */ /* use next_hop_ip->mac mapping in the entry to send the packet */ ip_hdr->ip_ttl--; /* recompute the packet checksum over the modified header */ ip_hdr->ip_sum = 0; uint16_t new_ip_sum = cksum(ip_hdr, sizeof(sr_ip_hdr_t)); ip_hdr->ip_sum = new_ip_sum; memcpy(eth_hdr->ether_shost, out_iface->addr, ETHER_ADDR_LEN); memcpy(eth_hdr->ether_dhost, arp_entry->mac, ETHER_ADDR_LEN); sr_send_packet(sr, packet, len, out_iface->name); print_hdr_ip((uint8_t*)ip_hdr); /* free the entry */ free(arp_entry); return; } else/* No Hit */ { /* send an ARP request for the next-hop IP */ /* add the packet to the queue of packets waiting on this ARP request */ /* Add request to ARP queue*/ ip_hdr->ip_ttl--; /* recompute the packet checksum over the modified header */ ip_hdr->ip_sum = 0; uint16_t new_ip_sum = cksum(ip_hdr, sizeof(sr_ip_hdr_t)); ip_hdr->ip_sum = new_ip_sum; struct sr_arpreq *arp_req = sr_arpcache_queuereq(sr_arp_cache, ip_hdr->ip_dst, packet, len, out_iface->name); /* send ARP request, this is a broadcast */ handle_arpreq(arp_req, sr); return; } } else /* if not matched */ { /* Send ICMP net unreachable */ printf("--------------- Net Unreachable ---------------\n"); /* Do LPM on the routing table */ /* Check the routing table and see if the incoming ip matches the routing table ip, and find LPM router entry */ struct sr_rt *longest_pref_match = sr_lpm(sr, ip_hdr->ip_src); if (longest_pref_match) { /* check ARP cache */ struct sr_arpentry *arp_entry = sr_arpcache_lookup(&sr->cache, longest_pref_match->gw.s_addr); struct sr_if *out_iface = sr_get_interface(sr, longest_pref_match->interface); if (arp_entry) { int packet_len = ICMP_T3_PACKET_LEN; uint8_t *icmp_t3_hdr = (uint8_t *)malloc(packet_len); /* Create ethernet header */ create_ethernet_hdr(eth_hdr, (sr_ethernet_hdr_t *)icmp_t3_hdr, out_iface); /* Create ip header */ create_echo_ip_hdr(ip_hdr, (sr_ip_hdr_t *)((char *)icmp_t3_hdr+ETHER_PACKET_LEN), out_iface); /* Create icmp net unreachable */ /* icmp_t3 type=3, code=0 */ create_icmp_t3_hdr(ip_hdr, (sr_icmp_t3_hdr_t *)((char *)icmp_t3_hdr+IP_PACKET_LEN), 3, 0); /* Send icmp type 3 packet */ sr_send_packet(sr, icmp_t3_hdr, packet_len, out_iface->name); free(icmp_t3_hdr); return; } else { int packet_len = ICMP_T3_PACKET_LEN; uint8_t *icmp_t3_hdr = (uint8_t *)malloc(packet_len); /* Create ethernet header */ create_ethernet_hdr(eth_hdr, (sr_ethernet_hdr_t *)icmp_t3_hdr, out_iface); /* Create ip header */ create_echo_ip_hdr(ip_hdr, (sr_ip_hdr_t *)((char *)icmp_t3_hdr+ETHER_PACKET_LEN), out_iface); /* ((sr_ip_hdr_t *)((char *)icmp_t3_hdr+ETHER_PACKET_LEN))->ip_ttl += 1; */ /* Send icmp type 3 net unreachable */ /* Create icmp net unreachable packet */ /* icmp_t3 type=3, code=0 */ create_icmp_t3_hdr(ip_hdr, (sr_icmp_t3_hdr_t *)((char *)icmp_t3_hdr+IP_PACKET_LEN), 3, 0); struct sr_arpreq *arp_req = sr_arpcache_queuereq(sr_arp_cache, ip_hdr->ip_src, icmp_t3_hdr, packet_len, out_iface->name); /* Send ARP request, which is a broadcast */ handle_arpreq(arp_req, sr); return; } } else { fprintf(stderr, "Longest prefix doesnt match!!\n"); return; } } } return; }
void *sr_nat_timeout(void *sr_ptr) { /* Periodic Timout handling */ struct sr_instance *sr = (struct sr_instance *)sr_ptr; struct sr_nat *nat = sr->nat; while (1) { sleep(1.0); pthread_mutex_lock(&(nat->lock)); /* handle periodic tasks here */ time_t curtime = time(NULL); struct sr_tcp_unsolicited_packet *my_pkt = nat->unsolicited_packet; /* if my_pkt is NULL, finish! */ if (my_pkt == NULL) { pthread_mutex_unlock(&(nat->lock)); } else { /* get the next packet */ struct sr_tcp_unsolicited_packet *next_pkt = my_pkt->next; /* if next pkt is NULL, only check my_pkt */ if (next_pkt == NULL) { time_t pkt_time = my_pkt->time_updated; /* if the time difference is bigger than 6 seconds */ if (difftime(curtime, pkt_time) >= 6) { /* get all the headers */ uint8_t *packet = my_pkt->buf; sr_ethernet_hdr_t *eth_hdr = get_eth_hdr(packet); sr_ip_hdr_t *ip_hdr = get_ip_hdr(packet); /* create a new icmp t3 port unreachable */ int packet_len = ICMP_T3_PACKET_LEN; uint8_t *icmp_t3_hdr = (uint8_t *)malloc(packet_len); /* create ethernet header */ sr_ethernet_hdr_t *new_eth_hdr = (sr_ethernet_hdr_t *)icmp_t3_hdr; memcpy(new_eth_hdr->ether_dhost, eth_hdr->ether_shost, ETHER_ADDR_LEN); memcpy(new_eth_hdr->ether_shost, eth_hdr->ether_dhost, ETHER_ADDR_LEN); /* create ip header */ sr_ip_hdr_t *new_ip_hdr = (sr_ip_hdr_t *)((char *)icmp_t3_hdr + ETHER_PACKET_LEN); new_ip_hdr->ip_hl = ip_hdr->ip_hl; /* header length */ new_ip_hdr->ip_v = ip_hdr->ip_v; /* header version */ new_ip_hdr->ip_tos = ip_hdr->ip_tos; /* type of service */ new_ip_hdr->ip_len = htons(56); /* total length */ new_ip_hdr->ip_id = 0; /* identification */ new_ip_hdr->ip_off = htons(0b0100000000000000); /* fragment offset field */ new_ip_hdr->ip_ttl = 64; /* time to live */ new_ip_hdr->ip_p = ip_protocol_icmp; /* protocol */ new_ip_hdr->ip_src = ip_hdr->ip_dst; /* source address */ new_ip_hdr->ip_dst = ip_hdr->ip_src; /* dest address */ new_ip_hdr->ip_sum = 0; new_ip_hdr->ip_sum = cksum(new_ip_hdr, sizeof(sr_ip_hdr_t));; /* checksum */ /* create icmp t3 header */ sr_icmp_t3_hdr_t *new_icmp_t3_hdr = (sr_icmp_t3_hdr_t *)((char *)icmp_t3_hdr + IP_PACKET_LEN); new_icmp_t3_hdr->icmp_type = htons(3); new_icmp_t3_hdr->icmp_code = htons(3); new_icmp_t3_hdr->unused = 0; new_icmp_t3_hdr->next_mtu = 0; memcpy(new_icmp_t3_hdr->data, new_ip_hdr, ICMP_DATA_SIZE); new_icmp_t3_hdr->icmp_sum = 0; new_icmp_t3_hdr->icmp_sum = cksum(new_icmp_t3_hdr, sizeof(sr_icmp_t3_hdr_t)); struct sr_if *out_iface = sr_get_router_if(sr, ip_hdr->ip_dst); sr_send_packet(sr, icmp_t3_hdr, packet_len, out_iface->name); /* set unsolicited_packet to be NULL */ nat->unsolicited_packet = NULL; } pthread_mutex_unlock(&(nat->lock)); } /* otherwise we need to loop through the packets */ else { struct sr_tcp_unsolicited_packet *prev_pkt = my_pkt; while (next_pkt != NULL) { time_t pkt_time = my_pkt->time_updated; /* if the time difference is bigger than 6 seconds */ if (difftime(curtime, pkt_time) >= 6) { /* get all the headers */ uint8_t *packet = my_pkt->buf; sr_ethernet_hdr_t *eth_hdr = get_eth_hdr(packet); sr_ip_hdr_t *ip_hdr = get_ip_hdr(packet); /* create a new icmp t3 port unreachable */ int packet_len = ICMP_T3_PACKET_LEN; uint8_t *icmp_t3_hdr = (uint8_t *)malloc(packet_len); /* create ethernet header */ sr_ethernet_hdr_t *new_eth_hdr = (sr_ethernet_hdr_t *)icmp_t3_hdr; memcpy(new_eth_hdr->ether_dhost, eth_hdr->ether_shost, ETHER_ADDR_LEN); memcpy(new_eth_hdr->ether_shost, eth_hdr->ether_dhost, ETHER_ADDR_LEN); /* create ip header */ sr_ip_hdr_t *new_ip_hdr = (sr_ip_hdr_t *)((char *)icmp_t3_hdr + ETHER_PACKET_LEN); new_ip_hdr->ip_hl = ip_hdr->ip_hl; /* header length */ new_ip_hdr->ip_v = ip_hdr->ip_v; /* header version */ new_ip_hdr->ip_tos = ip_hdr->ip_tos; /* type of service */ new_ip_hdr->ip_len = htons(56); /* total length */ new_ip_hdr->ip_id = 0; /* identification */ new_ip_hdr->ip_off = htons(0b0100000000000000); /* fragment offset field */ new_ip_hdr->ip_ttl = 64; /* time to live */ new_ip_hdr->ip_p = ip_protocol_icmp; /* protocol */ new_ip_hdr->ip_src = ip_hdr->ip_dst; /* source address */ new_ip_hdr->ip_dst = ip_hdr->ip_src; /* dest address */ new_ip_hdr->ip_sum = 0; new_ip_hdr->ip_sum = cksum(new_ip_hdr, sizeof(sr_ip_hdr_t));; /* checksum */ /* create icmp t3 header */ sr_icmp_t3_hdr_t *new_icmp_t3_hdr = (sr_icmp_t3_hdr_t *)((char *)icmp_t3_hdr + IP_PACKET_LEN); new_icmp_t3_hdr->icmp_type = htons(3); new_icmp_t3_hdr->icmp_code = htons(3); new_icmp_t3_hdr->unused = 0; new_icmp_t3_hdr->next_mtu = 0; memcpy(new_icmp_t3_hdr->data, new_ip_hdr, ICMP_DATA_SIZE); new_icmp_t3_hdr->icmp_sum = 0; new_icmp_t3_hdr->icmp_sum = cksum(new_icmp_t3_hdr, sizeof(sr_icmp_t3_hdr_t)); struct sr_if *out_iface = sr_get_router_if(sr, ip_hdr->ip_dst); sr_send_packet(sr, icmp_t3_hdr, packet_len, out_iface->name); /* unlist the my_pkt */ if (my_pkt == nat->unsolicited_packet) { nat->unsolicited_packet = next_pkt; prev_pkt = next_pkt; my_pkt = next_pkt; next_pkt = next_pkt->next; } else { prev_pkt->next = next_pkt; my_pkt = next_pkt; next_pkt = next_pkt->next; } } prev_pkt = my_pkt; my_pkt = next_pkt; next_pkt = next_pkt->next; } time_t pkt_time = my_pkt->time_updated; /* if the time difference is bigger than 6 seconds */ if (difftime(curtime, pkt_time) >= 6) { /* get all the headers */ uint8_t *packet = my_pkt->buf; sr_ethernet_hdr_t *eth_hdr = get_eth_hdr(packet); sr_ip_hdr_t *ip_hdr = get_ip_hdr(packet); /* create a new icmp t3 port unreachable */ int packet_len = ICMP_T3_PACKET_LEN; uint8_t *icmp_t3_hdr = (uint8_t *)malloc(packet_len); /* create ethernet header */ sr_ethernet_hdr_t *new_eth_hdr = (sr_ethernet_hdr_t *)icmp_t3_hdr; memcpy(new_eth_hdr->ether_dhost, eth_hdr->ether_shost, ETHER_ADDR_LEN); memcpy(new_eth_hdr->ether_shost, eth_hdr->ether_dhost, ETHER_ADDR_LEN); /* create ip header */ sr_ip_hdr_t *new_ip_hdr = (sr_ip_hdr_t *)((char *)icmp_t3_hdr + ETHER_PACKET_LEN); new_ip_hdr->ip_hl = ip_hdr->ip_hl; /* header length */ new_ip_hdr->ip_v = ip_hdr->ip_v; /* header version */ new_ip_hdr->ip_tos = ip_hdr->ip_tos; /* type of service */ new_ip_hdr->ip_len = htons(56); /* total length */ new_ip_hdr->ip_id = 0; /* identification */ new_ip_hdr->ip_off = htons(0b0100000000000000); /* fragment offset field */ new_ip_hdr->ip_ttl = 64; /* time to live */ new_ip_hdr->ip_p = ip_protocol_icmp; /* protocol */ new_ip_hdr->ip_src = ip_hdr->ip_dst; /* source address */ new_ip_hdr->ip_dst = ip_hdr->ip_src; /* dest address */ new_ip_hdr->ip_sum = 0; new_ip_hdr->ip_sum = cksum(new_ip_hdr, sizeof(sr_ip_hdr_t));; /* checksum */ /* create icmp t3 header */ sr_icmp_t3_hdr_t *new_icmp_t3_hdr = (sr_icmp_t3_hdr_t *)((char *)icmp_t3_hdr + IP_PACKET_LEN); new_icmp_t3_hdr->icmp_type = htons(3); new_icmp_t3_hdr->icmp_code = htons(3); new_icmp_t3_hdr->unused = 0; new_icmp_t3_hdr->next_mtu = 0; memcpy(new_icmp_t3_hdr->data, new_ip_hdr, ICMP_DATA_SIZE); new_icmp_t3_hdr->icmp_sum = 0; new_icmp_t3_hdr->icmp_sum = cksum(new_icmp_t3_hdr, sizeof(sr_icmp_t3_hdr_t)); struct sr_if *out_iface = sr_get_router_if(sr, ip_hdr->ip_dst); sr_send_packet(sr, icmp_t3_hdr, packet_len, out_iface->name); /* set the last packet to be NULL */ prev_pkt->next = NULL; } pthread_mutex_unlock(&(nat->lock)); } } } return NULL; }