Exemplo n.º 1
0
int Arp::transmit(Packet_ptr pckt){
  
  assert(pckt->size());
  
  /** Get destination IP from IP header   */
  IP4::ip_header* iphdr = (IP4::ip_header*)(pckt->buffer() 
                                            + sizeof(Ethernet::header));
  IP4::addr sip = iphdr->saddr;
  IP4::addr dip = pckt->next_hop();

  debug2("<ARP -> physical> Transmitting %i bytes to %s \n",
        pckt->size(),dip.str().c_str());
  
  Ethernet::addr mac;
  
  if (iphdr->daddr == IP4::INADDR_BCAST)
  {
    // when broadcasting our source IP should be either
    // our own IP or 0.0.0.0
    static const IP4::addr INADDR_NONE  {{0}};
    if (sip != ip_ && sip != INADDR_NONE)
    {
      debug2("<ARP> Dropping outbound broadcast packet due to "
             "invalid source IP %s\n",  sip.str().c_str());
      return -1;
    }
    mac = Ethernet::addr::BROADCAST_FRAME;
  }
  else
  {
    if (sip != ip_)
    {
      debug2("<ARP -> physical> Not bound to source IP %s. My IP is %s. DROP!\n",
            sip.str().c_str(), ip_.str().c_str());
      return -1;
    }
    
    // If we don't have a cached IP, get mac from next-hop (Håreks c001 hack)
    if (!is_valid_cached(dip))  
        return arp_resolver_(pckt);
    
    // Get mac from cache
    mac = cache_[dip].mac_;
  }
  
  /** Attach next-hop mac and ethertype to ethernet header  */  
  Ethernet::header* ethhdr = (Ethernet::header*)pckt->buffer();    
  ethhdr->src = mac_;
  ethhdr->dest.major = mac.major;
  ethhdr->dest.minor = mac.minor;
  ethhdr->type = Ethernet::ETH_IP4;
  
  debug2("<ARP -> physical> Sending packet to %s \n",mac.str().c_str());
  return linklayer_out_(pckt);
}
Exemplo n.º 2
0
  void IP4::bottom(Packet_ptr pckt) {
    debug2("<IP4 handler> got the data.\n");

    auto data = pckt->buffer();
    ip_header* hdr = &reinterpret_cast<full_header*>(data)->ip_hdr;

    debug2("\t Source IP: %s Dest.IP: %s\n",
           hdr->saddr.str().c_str(), hdr->daddr.str().c_str());

    switch(hdr->protocol){
    case IP4_ICMP:
      debug2("\t Type: ICMP\n");
      icmp_handler_(pckt);
      break;
    case IP4_UDP:
      debug2("\t Type: UDP\n");
      udp_handler_(pckt);
      break;
    case IP4_TCP:
      tcp_handler_(pckt);
      debug2("\t Type: TCP\n");
      break;
    default:
      debug("\t Type: UNKNOWN %i\n", hdr->protocol);
      break;
    }
  }
Exemplo n.º 3
0
  void Ethernet::receive(Packet_ptr pckt) {
    Expects(pckt->size() > 0);

    header* eth = reinterpret_cast<header*>(pckt->buffer());

    /** Do we pass on ethernet headers? As for now, yes.
        data += sizeof(header);
        len -= sizeof(header);
    */
    debug2("<Ethernet IN> %s => %s , Eth.type: 0x%x ",
           eth->src.str().c_str(), eth->dest.str().c_str(), eth->type);

    // Stat increment packets received
    packets_rx_++;

    bool dropped = false;

    switch(eth->type) {
    case ETH_IP4:
      debug2("IPv4 packet\n");
      ip4_upstream_(std::move(pckt));
      break;

    case ETH_IP6:
      debug2("IPv6 packet\n");
      ip6_upstream_(std::move(pckt));
      break;

    case ETH_ARP:
      debug2("ARP packet\n");
      arp_upstream_(std::move(pckt));
      break;

    case ETH_WOL:
      dropped = true;
      debug2("Wake-on-LAN packet\n");
      break;

    case ETH_VLAN:
      dropped = true;
      debug("VLAN tagged frame (not yet supported)");
      break;

    default:
      dropped = true;
      // This might be 802.3 LLC traffic
      if (net::ntohs(eth->type) > 1500) {
        debug2("<Ethernet> UNKNOWN ethertype 0x%x\n", ntohs(eth->type));
      }else {
        debug2("IEEE802.3 Length field: 0x%x\n", ntohs(eth->type));
      }
      break;
    }

    if(dropped)
      packets_dropped_++;
  }
Exemplo n.º 4
0
int Arp::bottom(Packet_ptr pckt)
{
  debug2("<ARP handler> got %i bytes of data \n", pckt->size());

  header* hdr = (header*) pckt->buffer();
  //debug2("\t OPCODE: 0x%x \n",hdr->opcode);
  //debug2("Chaching IP %s for %s \n", hdr->sipaddr.str().c_str() , hdr->shwaddr.str().c_str())
  debug2("Have valid cache? %s \n",is_valid_cached(hdr->sipaddr) ? "YES":"NO");
  cache(hdr->sipaddr, hdr->shwaddr);
  
  switch(hdr->opcode){
    
  case H_request:
    debug2("\t ARP REQUEST: ");
    debug2("%s is looking for %s \n",
          hdr->sipaddr.str().c_str(),hdr->dipaddr.str().c_str());
    
    if (hdr->dipaddr == ip_)
      arp_respond(hdr);    
    else
    {
      debug2("\t NO MATCH for My IP (%s). DROP!\n",
          ip().str().c_str());
    }
    break;
    
  case H_reply:
    {
      debug2("\t ARP REPLY: %s belongs to %s\n",
	     hdr->sipaddr.str().c_str(), hdr->shwaddr.str().c_str());
      auto waiting = waiting_packets_.find(hdr->sipaddr);
      if (waiting != waiting_packets_.end()) {
	debug ("Had a packet waiting for this IP. Sending\n");
	transmit(waiting->second);
	waiting_packets_.erase(waiting);
      }
    }
    
    break;
    
  default:
    debug2("\t UNKNOWN OPCODE \n");
    break;
  }
  
  // Free the buffer (We're leaf node for this one's path)
  // @todo Freeing here corrupts the outgoing frame. Why?
  //free(data);
  
  return 0 + 0 * pckt->size(); // yep, it's what you think it is (and what's that?!)
};
Exemplo n.º 5
0
int Ethernet::bottom(Packet_ptr pckt)
{
  assert(pckt->size() > 0);
  
  header* eth = (header*) pckt->buffer();  
  
  /** Do we pass on ethernet headers? As for now, yes.
    data += sizeof(header);
    len -= sizeof(header);
  */    
  debug2("<Ethernet IN> %s => %s , Eth.type: 0x%x ",
         eth->src.str().c_str(),
         eth->dest.str().c_str(),eth->type); 


  switch(eth->type){ 

  case ETH_IP4:
    debug2("IPv4 packet \n");
    return ip4_handler_(pckt);

  case ETH_IP6:
    debug2("IPv6 packet \n");
    return ip6_handler_(pckt);
    
  case ETH_ARP:
    debug2("ARP packet \n");
    return arp_handler_(pckt);
    
  case ETH_WOL:
    debug2("Wake-on-LAN packet \n");
    break;

  case ETH_VLAN:
    debug("VLAN tagged frame (not yet supported)");
    
  default:

    // This might be 802.3 LLC traffic
    if (net::ntohs(eth->type) > 1500){
      debug("<Ethernet> UNKNOWN ethertype 0x%x\n",ntohs(eth->type));
    }else{
      debug2("IEEE802.3 Length field: 0x%x\n",ntohs(eth->type));
    }

    break;
    
  }
  
  return -1;
}
Exemplo n.º 6
0
  void Arp::bottom(Packet_ptr pckt) {
    debug2("<ARP handler> got %i bytes of data\n", pckt->size());

    header* hdr = reinterpret_cast<header*>(pckt->buffer());

    debug2("Have valid cache? %s\n", is_valid_cached(hdr->sipaddr) ? "YES" : "NO");
    cache(hdr->sipaddr, hdr->shwaddr);

    switch(hdr->opcode) {

    case H_request: {
      // Stat increment requests received
      requests_rx_++;

      debug2("\t ARP REQUEST: ");
      debug2("%s is looking for %s\n",
             hdr->sipaddr.str().c_str(),
             hdr->dipaddr.str().c_str());

      if (hdr->dipaddr == inet_.ip_addr()) {
        arp_respond(hdr);
      } else {
        debug2("\t NO MATCH for My IP (%s). DROP!\n",
               inet_.ip_addr().str().c_str());
      }
      break;
    }

    case H_reply: {
      // Stat increment replies received
      replies_rx_++;

      debug2("\t ARP REPLY: %s belongs to %s (waiting: %u)\n",
             hdr->sipaddr.str().c_str(), hdr->shwaddr.str().c_str(), waiting_packets_.size());

      auto waiting = waiting_packets_.find(hdr->sipaddr);

      if (waiting != waiting_packets_.end()) {
        debug("Had a packet waiting for this IP. Sending\n");
        transmit(std::move(waiting->second));
        waiting_packets_.erase(waiting);
      }
      break;
    }

    default:
      debug2("\t UNKNOWN OPCODE\n");
      break;
    } //< switch(hdr->opcode)
  }
Exemplo n.º 7
0
int Ethernet::transmit(Packet_ptr pckt){
  header* hdr = (header*)pckt->buffer();

  // Verify ethernet header
  assert(hdr->dest.major != 0 || hdr->dest.minor !=0);
  assert(hdr->type != 0);
  
  // Add source address
  hdr->src.major = _mac.major;
  hdr->src.minor = _mac.minor;

  debug2("<Ethernet OUT> Transmitting %i b, from %s -> %s. Type: %i \n",
         pckt->size(),hdr->src.str().c_str(), hdr->dest.str().c_str(),hdr->type);
  
  return physical_out_(pckt);
}
Exemplo n.º 8
0
int ICMP::bottom(Packet_ptr pckt){

  if (pckt->size() < sizeof(full_header)) //Drop if not a full header.
    return -1;
  
  full_header* full_hdr = (full_header*)pckt->buffer();
  icmp_header* hdr = &full_hdr->icmp_hdr;
  
  switch(hdr->type)
  {
  case (ICMP_ECHO):
    debug("<ICMP> PING from %s \n",full_hdr->ip_hdr.saddr.str().c_str());
    ping_reply(full_hdr);
    break;
  case (ICMP_ECHO_REPLY):
    debug("<ICMP> PING Reply from %s \n",full_hdr->ip_hdr.saddr.str().c_str());
    break;
  }
  
  return 0;
}
Exemplo n.º 9
0
  void ICMPv4::bottom(Packet_ptr pckt) {
    if (pckt->size() < sizeof(full_header)) // Drop if not a full header
      return;

    full_header* full_hdr = reinterpret_cast<full_header*>(pckt->buffer());
    icmp_header* hdr = &full_hdr->icmp_hdr;

#ifdef DEBUG
    auto ip_address = full_hdr->ip_hdr.saddr.str().c_str();
#endif

    switch(hdr->type) {
    case (ICMP_ECHO):
      debug("<ICMP> PING from %s\n", ip_address);
      ping_reply(full_hdr, pckt->size());
      break;
    case (ICMP_ECHO_REPLY):
      debug("<ICMP> PING Reply from %s\n", ip_address);
      break;
    }
  }
Exemplo n.º 10
0
  void Arp::transmit(Packet_ptr pckt) {
    assert(pckt->size());

    /** Get destination IP from IP header */
    IP4::ip_header* iphdr = reinterpret_cast<IP4::ip_header*>(pckt->buffer()
                                                              + sizeof(Ethernet::header));
    IP4::addr sip = iphdr->saddr;
    IP4::addr dip = pckt->next_hop();

    debug2("<ARP -> physical> Transmitting %i bytes to %s\n",
           pckt->size(), dip.str().c_str());

    Ethernet::addr dest_mac;

    if (iphdr->daddr == IP4::INADDR_BCAST) {
      // When broadcasting our source IP should be either
      // our own IP or 0.0.0.0

      if (sip != inet_.ip_addr() && sip != IP4::INADDR_ANY) {
        debug2("<ARP> Dropping outbound broadcast packet due to "
               "invalid source IP %s\n",  sip.str().c_str());
        return;
      }
      // mui importante
      dest_mac = Ethernet::BROADCAST_FRAME;

    } else {
      if (sip != inet_.ip_addr()) {
        debug2("<ARP -> physical> Not bound to source IP %s. My IP is %s. DROP!\n",
               sip.str().c_str(), inet_.ip_addr().str().c_str());
        return;
      }

      // If we don't have a cached IP, perform address resolution
      if (!is_valid_cached(dip)) {
        arp_resolver_(std::move(pckt));
        return;
      }

      // Get MAC from cache
      dest_mac = cache_[dip].mac_;
    }

    /** Attach next-hop mac and ethertype to ethernet header */
    auto* ethhdr = reinterpret_cast<Ethernet::header*>(pckt->buffer());
    ethhdr->src  = mac_;
    ethhdr->dest = dest_mac;
    ethhdr->type = Ethernet::ETH_IP4;

    /** Update chain as well */
    auto* next = pckt->tail();
    while(next) {
      auto* headur = reinterpret_cast<Ethernet::header*>(next->buffer());
      headur->src  = mac_;
      headur->dest = dest_mac;
      headur->type = Ethernet::ETH_IP4;
      next = next->tail();
    }

    debug2("<ARP -> physical> Sending packet to %s\n", mac_.str().c_str());
    linklayer_out_(std::move(pckt));
  }