Ejemplo n.º 1
0
static void handle_llc_packet(const struct llc* llc, int dir) {

    struct ip* ip = (struct ip*)((void*)llc + sizeof(struct llc));

    /* Taken from tcpdump/print-llc.c */
    if(llc->ssap == LLCSAP_SNAP && llc->dsap == LLCSAP_SNAP
       && llc->llcui == LLC_UI) {
        u_int32_t orgcode;
        register u_short et;
        orgcode = EXTRACT_24BITS(&llc->llc_orgcode[0]);
        et = EXTRACT_16BITS(&llc->llc_ethertype[0]);
        switch(orgcode) {
          case OUI_ENCAP_ETHER:
          case OUI_CISCO_90:
            handle_ip_packet(ip, dir);
            break;
          case OUI_APPLETALK:
            if(et == ETHERTYPE_ATALK) {
              handle_ip_packet(ip, dir);
            }
            break;
          default:;
            /* Not a lot we can do */
        }
    }
}
Ejemplo n.º 2
0
static void handle_ppp_packet(unsigned char* args, const struct pcap_pkthdr* pkthdr, const unsigned char* packet)
{
	register u_int length = pkthdr->len;
	register u_int caplen = pkthdr->caplen;
	u_int proto;

	if (caplen < 2) 
        return;

	if(packet[0] == PPP_ADDRESS) {
		if (caplen < 4) 
            return;

		packet += 2;
		length -= 2;

		proto = EXTRACT_16BITS(packet);
		packet += 2;
		length -= 2;

        if(proto == PPP_IP || proto == ETHERTYPE_IP || proto == ETHERTYPE_IPV6) {
            handle_ip_packet((struct ip*)packet, -1);
        }
    }
}
Ejemplo n.º 3
0
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);
	
	//print_hex2(packet, len);
	EthernetFrame * frame = new EthernetFrame(packet, len);
	switch(frame->GetType())
	{
		case ARP_PACKET:
		{
			sr_arpcache_insert(&sr->cache, frame->GetSrcAddress(), flip_ip(get_int(frame->GetPayload()+12)));
			handle_arp_packet(sr, frame, interface);
			break;
		}
		case IP_PACKET:
		{
			//
			sr_arpcache_insert(&sr->cache, frame->GetSrcAddress(), flip_ip(get_int(frame->GetPayload()+12)));
			handle_ip_packet(sr, frame, interface);
		}
		break;
		default:
			cerr << "Not a packet" << endl;
	}
	delete frame;
}/* end sr_ForwardPacket */
Ejemplo n.º 4
0
 void sr_handlepacket(struct sr_instance* sr,
        uint8_t * packet/* lent */,
    unsigned int len,
        char* interface/* lent */) {
    /* REQUIRES */
    assert(sr);
    assert(packet);
    assert(interface);
    int res;
 

    /* Ethernet */
    int minlength = sizeof(sr_ethernet_hdr_t);
    if (len < minlength) {
        fprintf(stderr, "Failed to parse ETHERNET header, insufficient length\n");
        return;
    }

    uint16_t ethtype = ethertype(packet);

    /* IP */
    if (ethtype == ethertype_ip) { 

        minlength += sizeof(sr_ip_hdr_t);
        if (len < minlength) {
            fprintf(stderr, "Failed to parse IP header, insufficient length\n");
            return;
        } 
        res = handle_ip_packet(sr, packet, len );

        if (res == -1){
            fprintf(stderr, "bad handle_ip_packet\n");
            return;
        }
        
        /* end IP */
    } else if (ethtype == ethertype_arp) { 
        /* begin ARP */

        minlength += sizeof(sr_arp_hdr_t);

        if (len < minlength){
            fprintf(stderr, "Failed to parse ARP header, insufficient length\n");
        }
        res = handle_arp_packet(sr, packet, len );

        if (res == -1){
            fprintf(stderr, "bad handle_arp_packet\n");
            return;
        }
        /* end ARP */
    } else {
        fprintf(stderr, "Unrecognized Ethernet Type: %d\n", ethtype);
    }
    /* TODO: fill in code here */
    return;

}
Ejemplo n.º 5
0
static void handle_pflog_packet(unsigned char* args, const struct pcap_pkthdr* pkthdr, const unsigned char* packet)
{
	register u_int length = pkthdr->len;
	u_int hdrlen;
	const struct pfloghdr *hdr;
	
	hdr = (struct pfloghdr *)packet;
	hdrlen = BPF_WORDALIGN(hdr->length);
	length -= hdrlen;
	packet += hdrlen;
	handle_ip_packet((struct ip*)packet, -1);
}
Ejemplo n.º 6
0
static void handle_eth_packet(unsigned char* args, const struct pcap_pkthdr* pkthdr, const unsigned char* packet)
{
    struct ether_header *eptr;
    int ether_type;
    const unsigned char *payload;
    eptr = (struct ether_header*)packet;
    ether_type = ntohs(eptr->ether_type);
    payload = packet + sizeof(struct ether_header);

    tick(0);

    if(ether_type == ETHERTYPE_8021Q) {
	struct vlan_8021q_header* vptr;
	vptr = (struct vlan_8021q_header*)payload;
	ether_type = ntohs(vptr->ether_type);
        payload += sizeof(struct vlan_8021q_header);
    }

    if(ether_type == ETHERTYPE_IP || ether_type == ETHERTYPE_IPV6) {
        struct ip* iptr;
        int dir = -1;
        
        /*
         * Is a direction implied by the MAC addresses?
         */
        if(have_hw_addr && memcmp(eptr->ether_shost, if_hw_addr, 6) == 0 ) {
            /* packet leaving this i/f */
            dir = 1;
        }
        else if(have_hw_addr && memcmp(eptr->ether_dhost, if_hw_addr, 6) == 0 ) {
	    /* packet entering this i/f */
	    dir = 0;
	}
	else if (memcmp("\xFF\xFF\xFF\xFF\xFF\xFF", eptr->ether_dhost, 6) == 0) {
	  /* broadcast packet, count as incoming */
            dir = 0;
        }

        /* Distinguishing ip_hdr and ip6_hdr will be done later. */
        iptr = (struct ip*)(payload); /* alignment? */
        handle_ip_packet(iptr, dir);
    }
}
Ejemplo n.º 7
0
static void handle_cooked_packet(unsigned char *args, const struct pcap_pkthdr * thdr, const unsigned char * packet)
{
    struct sll_header *sptr;
    int dir = -1;
    sptr = (struct sll_header *) packet;

    switch (ntohs(sptr->sll_pkttype))
    {
    case LINUX_SLL_HOST:
        /*entering this interface*/
	dir = 0;
	break;
    case LINUX_SLL_OUTGOING:
	/*leaving this interface */
	dir=1;
	break;
    }
    handle_ip_packet((struct ip*)(packet+SLL_HDR_LEN), dir);
}
Ejemplo n.º 8
0
/*
 * Packets with a bonus radiotap header.
 * See http://www.gsp.com/cgi-bin/man.cgi?section=9&topic=ieee80211_radiotap
 */
static void handle_radiotap_packet(unsigned char* args, const struct pcap_pkthdr* pkthdr, const unsigned char* packet)
{
    /* 802.11 MAC header is = 34 bytes (not sure if that's universally true) */
    /* We could try harder to figure out hardware direction from the MAC header */
    handle_ip_packet((struct ip*)(packet + ((struct radiotap_header *)packet)->it_len + 34),-1);
}
Ejemplo n.º 9
0
static void handle_null_packet(unsigned char* args, const struct pcap_pkthdr* pkthdr, const unsigned char* packet)
{
    handle_ip_packet((struct ip*)(packet + 4), -1);
}
Ejemplo n.º 10
0
static void handle_raw_packet(unsigned char* args, const struct pcap_pkthdr* pkthdr, const unsigned char* packet)
{
    handle_ip_packet((struct ip*)packet, -1);
}