/* ipv4_handle_packet() - handle an incoming IPv4 packet by decapsulating it, optionally verifying its header checksum, and passing it up to the next protocol handler. */ s32 ipv4_rx(net_address_t *src, net_address_t *dest, net_packet_t *packet) { ipv4_hdr_t *hdr = (ipv4_hdr_t *) net_packet_get_start(packet); net_address_t ipv4_src, ipv4_dest; s32 ret; UNUSED(dest); /* It's usually not necessary to verify the IPv4 header checksum on received packets, as the hardware will already have verified the checksum of the whole (e.g. Ethernet) frame. */ #if(IPV4_VERIFY_CHECKSUM) if(net_cksum(hdr, (hdr->version_hdr_len & 0xf) << 2) != 0x0000) return ECKSUM; /* Drop packet */ #endif ret = net_packet_consume(packet, sizeof(ipv4_hdr_t)); if(ret != SUCCESS) return ret; ipv4_make_addr(hdr->src, IPV4_PORT_NONE, &ipv4_src); ipv4_make_addr(hdr->dest, IPV4_PORT_NONE, &ipv4_dest); net_packet_set_proto(packet, ipv4_get_proto(hdr->protocol)); /* Add the packet's source hardware address and source protocol address to the ARP cache */ if(hdr->src != IPV4_ADDR_NONE) arp_cache_add(net_packet_get_interface(packet), src, &ipv4_src); return net_protocol_rx(&ipv4_src, &ipv4_dest, packet); }
void sr_handlepacket(struct sr_instance* sr, uint8_t* packet/* lent */, unsigned int len, char* interface/* lent */) { /* REQUIRES */ assert(sr); assert(packet); assert(interface); printf("*** -> Received packet of length %d \n",len); if( len < ETHER_HDR_LEN ) { printf("Bogus packet length\n"); return; } struct sr_if *iface; struct frame_t *incoming = create_frame_t(sr, packet, len, interface); struct frame_t *outgoing = NULL; /* First, deal with ARP cache timeouts */ arp_cache_flush(&sr_arp_cache); arp_queue_flush(sr, &sr_arp_queue); /* Do we only need to cache ARP replies, or src MAC/IP on regular IP packets too, etc? */ /* Also, do we need to worry about fragmentation? */ /* Then actually handle the packet */ /* Start by determining protocol */ if (incoming->ip_header){ /* sanity checks */ if ( incoming->ip_header->ip_v != 4 ){ printf("IP packet not IPv4\n"); return; } compute_ip_checksum(incoming); /* Check the checksum */ if( incoming->ip_header->ip_sum != 0 ) { fprintf(stderr, "IP checksum incorrect, packet was dropped\n"); return; } //set checksum back to what it was compute_ip_checksum(incoming); /* Are we the destination? */ if (iface = if_dst_check(sr, incoming->to_ip)){ //we could change this to just take incoming and then get to_ip /* Is this an ICMP packet? */ if (incoming->icmp_header){ printf("received ICMP datagram\n"); compute_icmp_checksum(incoming); if(incoming->icmp_header->icmp_type == ECHO_REQUEST && incoming->icmp_header->icmp_sum == 0){ outgoing = generate_icmp_echo(incoming); printf("received ICMP echo request\n"); } else printf("Dropped packet--we don't deal with that code, or invalid checksum\n"); } else{ outgoing = generate_icmp_error(incoming, DEST_UNREACH, PORT_UNREACH); printf("A packet for me! Flattering, but wrong.\n"); } } else { /* Has it timed out? */ if (incoming->ip_header->ip_ttl <= 0){ int err = 0; //make sure it's not an ICMP error packet alrady if (incoming->ip_header->ip_p == IPPROTO_ICMP){ incoming->icmp_header = ((void *) incoming->ip_header + incoming->ip_hl); uint8_t code = incoming->icmp_header->icmp_type; //don't send ICMP error messages about ICMP error messages if (code == DEST_UNREACH || code == TIME_EXCEEDED || code == 12 || code == 31) //12 and 31 indicate bad IP header and datagram conversion error, respectively err = 1; } if (!err){ outgoing = generate_icmp_error(incoming, TIME_EXCEEDED, TIME_INTRANSIT); printf("Slowpoke. TTL exceeded.\n"); } } else { /* update and forward packet; if necessary, add it to queue */ struct arpc_entry *incache; uint32_t arpc_ip; outgoing = update_ip_hdr(sr, incoming, &arpc_ip); incache = arp_cache_lookup(sr_arp_cache.first, arpc_ip); if (!incache){ struct arpq_entry *entry = arp_queue_lookup(sr_arp_queue.first, outgoing->to_ip); struct arpq_entry *temp_entry = NULL; if ( entry ){ //if we've already sent an ARP request about this IP if( time(NULL) - 1 > entry->arpq_last_req ) { if(entry->arpq_num_reqs >= ARP_MAX_REQ) { printf("Too many ARP requests\n"); arpq_packets_icmpsend(sr, &entry->arpq_packets); destroy_arpq_entry(entry); return; } else if (entry->arpq_packets.first) { struct frame_t *arp_req; struct queued_packet *old_packet = entry->arpq_packets.first; entry->arpq_last_req = time(NULL); entry->arpq_num_reqs++; } } assert( (entry->arpq_packets).first ); if (!arpq_add_packet(entry, outgoing, len, incoming->from_MAC, incoming->iface)) printf("ARP queue packet add failed\n"); else printf("added packet to queue\n"); } /* else, there are no outstanding ARP requests for this particular IP */ else { printf("outgoing ip is %d\n", ntohl(outgoing->to_ip)); temp_entry = arpq_add_entry(&sr_arp_queue, outgoing->iface, outgoing, outgoing->to_ip, outgoing->ip_len, incoming->from_MAC, incoming->iface); } free(outgoing->frame); /* make ARP request */ outgoing = arp_create(sr, outgoing, outgoing->iface, ARP_REQUEST); //send datagram will now point to an ARP packet, not to the IP datagram if (temp_entry){ temp_entry->arpq_next_hop = outgoing->to_ip; } printf("sending ARP request\n"); } else{ printf("got the MAC, can actually send this packet\n"); memcpy(outgoing->to_MAC, incache->arpc_mac, ETHER_ADDR_LEN); encapsulate(outgoing); } } } } else if ( incoming->arp_header ) { printf("received ARP packet\n"); struct sr_arphdr *arp_header = incoming->arp_header; uint8_t in_cache = 0; struct arpc_entry *arpc_ent = arp_cache_lookup(sr_arp_cache.first, arp_header->ar_sip); printf("checking the cache\n"); if( arpc_ent ) { arp_cache_update(arpc_ent, arp_header->ar_sha); printf("updated cache\n"); in_cache = 1; } struct sr_if *target_if = if_dst_check(sr, arp_header->ar_tip); printf("checking the target\n"); if( target_if ) { printf("It's for us\n"); if( !in_cache ) { if( arp_cache_add(&sr_arp_cache, arp_header->ar_sha, arp_header->ar_sip) ) { printf("added to cache\n"); printf("ip is %d\n", ntohl(arp_header->ar_sip)); struct arpq_entry *new_ent; if( new_ent = arpq_next_hop_lookup(sr_arp_queue.first, arp_header->ar_sip) ) arpq_entry_clear(sr, &sr_arp_queue, new_ent, arp_header->ar_sha); } else perror("ARP request not added to cache"); } if( ntohs(arp_header->ar_op) == ARP_REQUEST ){ outgoing = arp_create(sr, incoming, incoming->iface, ARP_REPLY); printf("created ARP reply\n"); assert(outgoing); } } } else perror("Unknown protocol"); //send datagram, if appropriate if (outgoing != NULL){ sr_send_packet(sr, (uint8_t *)outgoing->frame, outgoing->len, outgoing->iface->name); printf("sent packet of length %d on iface %s\n", outgoing->len, outgoing->iface->name); } if (outgoing != NULL) destroy_frame_t(outgoing); }/* end sr_handlepacket */