/* 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; }