示例#1
0
static void send_image( image *img )
{
    Huint   s;
    Hubyte  target[6];
    Huint   header[3];

    printf( "Image Width: %u\n", img->width );
    printf( "Image Height: %u\n", img->height );
    printf( "Image Depth: %u\n", img->depth );

    target[0] = 0x00;
    target[1] = 0xE0;
    target[2] = 0x18;
    target[3] = 0xE4;
    target[4] = 0xB2;
    target[5] = 0x75;
    
    header[0] = htonl( img->width );
    header[1] = htonl( img->height );
    header[2] = htonl( img->depth );
    eth_send( target, (Hubyte*)header, sizeof(Huint)*3 );

    s = sizeof(Hubyte)*img->width*img->height*img->depth;
    eth_send( target, img->data, s );
}
示例#2
0
文件: main.c 项目: jsdario/rysca
int main() {

	bold("This sends and empty payload, length should be 64\n");
	printf("Opening eth0\n");

	eth_iface_t * iface = eth_open( "eth0" );

	printf("Sending empty payload (1 byte) to 00:11:22:33:44:55\n");
	mac_addr_t dst;
	mac_str_addr("00:11:22:33:44:55", dst);
	uint8_t byte = 1;

	
	printf("With type IPV4_OVER_ETH_TYPE\n");
	
	int bytes_sent = eth_send( iface, dst, 0x0800, &byte, 1 );

	printf(BOLDCYAN "bytes_sent = %d\n" RESET, bytes_sent); 



	bold("Now we try to send 128 bits, for exmple: \n");
	printf(" Now we try 128 bytes to 00:11:22:33:44:55\n");

	int i;
	unsigned char word[128];
	for( i=0; i < 128; i++ ) word[i] = i;
		
	bytes_sent = eth_send( iface, dst, 0x0800, word, 128 );

	printf(BOLDCYAN "bytes_sent = %d\n" RESET, bytes_sent); 

	return 0;
}
示例#3
0
文件: ip.c 项目: gfiorav/rysca
/* devuelve lo mismo que eth_send (bytes enviados) o -1 si ha habido error*/
int ipv4_send( ipv4_address_t dst, uint8_t type, unsigned char * payload, int payload_len  ) {

	int bytes_sent = 0;
	int arp_flag = 0;   

        mac_addr_t mac_addr; //mac destino 
   
   	arp_flag = arp_resolve (iface, dst , mac_addr_t ); //resolvemos ip

	if( arp_flag == -1 ) {

	//si error salimos
		printf("Cannot resolve ip addr into mac addr");
		return arp_flag; //-1
	}    

	bytes_sent eth_send ( eth_iface_aux, 
	 			 dst,
				 IPV4_OVER_ETH_TYPE,
				 payload,
 				 payload_len );
	

	return bytes_sent;
}
示例#4
0
文件: ip.c 项目: RoXeon/TCPIPv6
size_t ip_send(session_t *session, const uint8_t dst_ip[], uint8_t protocol,
               const uint8_t data[], size_t data_len)
{
    // We can send maximum of IP_DATA_MAX_LEN bytes of data.
    if(data_len > IP_DATA_MAX_LEN)
        return 0;

    // Prepare the packet
    ip_packet_t packet;
    packet.flow = 0;
    packet.version = 0x60;
    packet.payload_length = netb_s(data_len);
    packet.next_header = protocol;
    packet.hop_limit = 255;
    memcpy(packet.src_ip, session->src_ip, IP_ADDR_LEN);
    memcpy(packet.dst_ip, dst_ip, IP_ADDR_LEN);
    memcpy(packet.data, data, data_len);


    uint8_t dst_hw_addr[ETH_ADDR_LEN];
    if(ip_to_hw(session, dst_ip, dst_hw_addr) != 0)
        return 0;

    const size_t packet_len = IP_HEADER_LEN + data_len;
    const size_t sent = eth_send(session, dst_hw_addr, packet.buffer,
                                 packet_len);

    return sent == packet_len ? data_len : 0;
}
示例#5
0
static int Active_SendEth (
    const DAQ_PktHdr_t* h, int rev, const uint8_t* buf, uint32_t len)
{
    ssize_t sent = eth_send(s_link, buf, len);
    s_injects++;
    return ( (uint32_t) sent != len );
}
int l2_packet_send(struct l2_packet_data *l2, u8 *buf, size_t len)
{
#ifdef CONFIG_WINPCAP
	return pcap_sendpacket(l2->pcap, buf, len);
#else
	return eth_send(l2->eth, buf, len);
#endif
}
示例#7
0
文件: pack.c 项目: hemiao3000/code
int eth_pack(unsigned int length, unsigned short type)
{
	eth_h *eth = (void *)send_buf;

	memcpy(eth->dest, heth, 6);
	memcpy(eth->source, eeth, 6);
	eth->proto = htons(type);

	return eth_send((void *)send_buf, length + sizeof(eth_h));
}
示例#8
0
文件: pack.c 项目: heihei1252/develop
int eth_pack(u32 length, u16 type)
{
	eth_t *eth = (void *)send_buf;

	memcpy((void *)eth->dest,(void *)heth, 6);
	memcpy((void *)eth->source,(void *)eeth, 6);
	eth->proto = htons(type);

	return eth_send((void *)send_buf, length + sizeof(eth_t));
}
示例#9
0
//------------------------------------------------------------------------
// This allocates memory for the entire outgoing message,
// including eth and ip headers, then builds an outgoing
// ARP response message
// See "TCP/IP Illustrated, Volume 1" Sect 4.4
//------------------------------------------------------------------------
void arp_send(UCHAR * hwaddr, ULONG ipaddr, UCHAR msg_type)
{
	UCHAR xdata * outbuf;
	ARP_HEADER xdata * arp;
         
   
   // Allocate memory for entire outgoing message including
   // eth header. Always 42 bytes
   outbuf = (UCHAR xdata *)malloc(42);
   if (outbuf == NULL)
   {
      if (debug) serial_send("ARP: Oops, out of memory\r");
      return;
   }      
     
   // Allow 14 bytes for the ethernet header
   arp = (ARP_HEADER xdata *)(outbuf + 14);
 	
	arp->hardware_type = DIX_ETHERNET; 
   arp->protocol_type = IP_PACKET;
   arp->hwaddr_len = 6;
	arp->ipaddr_len = 4;               
   arp->message_type = (UINT)msg_type;
   
   // My hardware address and IP addresses 
   memcpy(arp->source_hwaddr, my_hwaddr, 6);
   arp->source_ipaddr = my_ipaddr;

   // Destination hwaddr and dest IP addr
   if (msg_type == ARP_REQUEST) memset(arp->dest_hwaddr, 0, 6);
   else memcpy(arp->dest_hwaddr, hwaddr, 6);
   
   arp->dest_ipaddr = ipaddr;
      
   // If request then the message is a brodcast, if a response then
   // send to specified hwaddr
   // ARP payload size is always 28 bytes
	if (msg_type == ARP_REQUEST) eth_send(outbuf, broadcast_hwaddr, ARP_PACKET, 28);
   else eth_send(outbuf, hwaddr, ARP_PACKET, 28);
}
示例#10
0
文件: main.c 项目: hemiao3000/code
int main(void)
{
	eth_init();

	char buf[20] = {0x11, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x10, 0x12};

	while(1){
		eth_send(buf, 12);
		delay();
	}	
	
	return 0;
}
示例#11
0
void ORILIB_EthWriter_i (
      IN  void * sampleBuf,
      IN  ORILIB_EthReader_t_State * const inOutRStateBuf,
      IN  ORILIB_EthWriter_t_State * const inOutWStateBuf,
      CF  ORILIB_EthWriter_t_Conf * conf
      ) {

	Uint32 idx = 0;

	// VITA
	if (conf->vitaOrDebug)
	{
		Uint16 ip_len, udp_len, vita_len;

		// fill in the packet template and samples
		_mem4cpy(tx_packet + idx, hdrs, sizeof(hdrs) >> 2);
		idx += sizeof(hdrs);
		_mem4cpy(tx_packet + idx, sampleBuf, conf->nSamplesPerSymbol);
		idx += conf->nSamplesPerSymbol << 2;
		_mem4cpy(tx_packet + idx, trailer, sizeof(trailer) >> 2);
		idx += sizeof(trailer);

		// fill in dynamic header fields
		
		// vita seqnum
		*((Uint8*)(tx_packet + VITA_SEQNUM_IDX)) = inOutWStateBuf->vita_seqnum++ & 0x0F;

		// src mac
		memcpy(tx_packet + ETH_DEST_IDX , &inOutRStateBuf->srcMAC, sizeof(inOutRStateBuf->srcMAC));

		// src udp port
		*((Uint16*)(tx_packet + UDP_DEST_IDX)) = inOutRStateBuf->srcUDPPort;

		// ip length 
		ip_len = IP_HDR_LEN + UDP_HDR_LEN + (conf->nSamplesPerSymbol << 2)	+ VITA_HDR_LEN + VITA_TRL_LEN;
		memcpy(tx_packet + IP_LEN_IDX, &ip_len, sizeof(ip_len));

		// udp length
		udp_len = ip_len - IP_HDR_LEN;
		memcpy(tx_packet + UDP_LEN_IDX, &udp_len, sizeof(udp_len));

		// vita length (words)
		vita_len = VITA_HDR_WRD + VITA_TRL_WRD + conf->nSamplesPerSymbol;
		memcpy(tx_packet + VITA_LEN_IDX, &vita_len, sizeof(vita_len));
		
		// TODO IP checksum
		// TODO UDP checksum

		eth_send(tx_packet, idx);
	}
int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,
		   const u8 *buf, size_t len)
{
	int ret;
	struct l2_ethhdr *eth;

	if (l2 == NULL)
		return -1;

	if (l2->l2_hdr) {
#ifdef CONFIG_WINPCAP
		ret = pcap_sendpacket(l2->pcap, buf, len);
#else /* CONFIG_WINPCAP */
		ret = eth_send(l2->eth, buf, len);
#endif /* CONFIG_WINPCAP */
	} else {
		size_t mlen = sizeof(*eth) + len;
		eth = os_malloc(mlen);
		if (eth == NULL)
			return -1;

		os_memcpy(eth->h_dest, dst_addr, ETH_ALEN);
		os_memcpy(eth->h_source, l2->own_addr, ETH_ALEN);
		eth->h_proto = htons(proto);
		os_memcpy(eth + 1, buf, len);

#ifdef CONFIG_WINPCAP
		ret = pcap_sendpacket(l2->pcap, (u8 *) eth, mlen);
#else /* CONFIG_WINPCAP */
		ret = eth_send(l2->eth, (u8 *) eth, mlen);
#endif /* CONFIG_WINPCAP */

		os_free(eth);
	}

	return ret;
}
示例#13
0
static void
fragroute_process(const struct pcap_pkthdr *hdr, void *buf, size_t len, void *arg)
{
    struct pktq pktq;
    struct pkt *pkt, *next;
    
    if ((pkt = pkt_new()) == NULL) {
        warn("pkt_new");
        return;
    }
    if (ETH_HDR_LEN + len > PKT_BUF_LEN) {
        warn("dropping oversized packet");
        return;
    }
    memcpy(pkt->pkt_data + ETH_HDR_LEN, buf, len);
    pkt->pkt_end = pkt->pkt_data + ETH_HDR_LEN + len;

    pkt_decorate(pkt);
    
    if (pkt->pkt_ip == NULL) {
        warn("dropping non-IP packet");
        return;
    }
    eth_pack_hdr(pkt->pkt_eth, ctx.dmac.addr_eth,
        ctx.smac.addr_eth, ETH_TYPE_IP);
    
    pkt->pkt_ip->ip_src = ctx.src.addr_ip;
    ip_checksum(pkt->pkt_ip, len);

    /* Forward this packet along as is. */
    if(ctx.dfile && 
       eth_send(ctx.eth, pkt->pkt_data, pkt->pkt_end - pkt->pkt_data) < 0)
        warn("eth_send");

    TAILQ_INIT(&pktq);
    TAILQ_INSERT_TAIL(&pktq, pkt, pkt_next);
    
    mod_apply(&pktq);

    for (pkt = TAILQ_FIRST(&pktq); pkt != TAILQ_END(&pktq); pkt = next) {
        next = TAILQ_NEXT(pkt, pkt_next);
        _resend_outgoing(pkt);
    }

}
示例#14
0
void retrans(struct my_pkthdr *h, u_char *pack ) {
  struct eth_hdr *ethhdr;
  struct ip_hdr *iphdr;
  struct tcp_hdr *tcphdr;
  struct addr srcad, srcha;
  char sip[32],smac[32];
  int n;

  ethhdr = (struct eth_hdr *)pack;
  iphdr = (struct ip_hdr *)(pack+ETH_HDR_LEN);
  tcphdr= (struct tcp_hdr *)(pack+ETH_HDR_LEN+TCP_HDR_LEN);
  addr_pack(&srcha,ADDR_TYPE_ETH,ETH_ADDR_BITS,&(ethhdr->eth_src),ETH_ADDR_LEN);
  addr_pack(&srcad,ADDR_TYPE_IP,IP_ADDR_BITS,&(iphdr->ip_src),IP_ADDR_LEN);
  if((strcmp(addr_ntoa(&srcha),ahw)==0)){
	// Replace source address with my address and destination address
	memcpy( &ethhdr->eth_src, &reat_mac.addr_eth, ETH_ADDR_LEN);
	memcpy( &iphdr->ip_src, &reat_ip.addr_ip, IP_ADDR_LEN);

	// Replace destination address with other client
	if ( addr_cmp( &srcad, &aad ) == 0 ) {
		memcpy( &ethhdr->eth_dst, &revi_mac.addr_eth, ETH_ADDR_LEN);
		memcpy( &iphdr->ip_dst, &revi_ip.addr_ip, IP_ADDR_LEN);
	}else{
		memcpy( &ethhdr->eth_dst, &reat_mac.addr_eth, ETH_ADDR_LEN);
		memcpy( &iphdr->ip_dst, &reat_ip.addr_ip, IP_ADDR_LEN);
	}
/*	if(modify_tcp_header(&tcphdr,tcphdr->th_sport,tcphdr->th_dport,next_seq,next_ack,tcphdr->th_flags,tcphdr->th_win,tcphdr->th_sum,tcphdr->th_urp,2)<0){
//		return;
	}
	if(modify_tcp_header(&tcphdr,tcphdr->th_sport,tcphdr->th_dport,next_seq,next_ack,tcphdr->th_flags,tcphdr->th_win,tcphdr->th_sum,tcphdr->th_urp,3)<0){
//		return;
	}*/
	ip_checksum((void *)iphdr, ntohs(iphdr->ip_len));
	n = eth_send(e,pack,h->len);
	//n=ip_send(e,pack,h->len);
	if ( n != h->len ) { 
		fprintf(stderr,"Partial packet transmission %d/%d\n",n,h->len);
	} else {
		fprintf(stdout, "Packet Transmission Successfull %d %d\n", n, h->len);
	}
   }
}
示例#15
0
/***********************************************************
 * send_frame_C()
 * what: sends an ethernet frame
 * params:
 *    socket - socket descriptor. 
 *    length - length of data to send
 *    data_p - data pointer
 * returns:
 *    error code 
 **********************************************************/
int send_frame_C(short int length, const unsigned char * data_p) {
  

  // buffer to send:
  unsigned char send_buffer[ETH_FRAME_LEN];
  
  // prepare send_buffer with DEST and SRC addresses 
  memcpy((void*)send_buffer, (void*)neuflow_mac, ETH_ALEN);
#ifdef _LINUX_  
  memcpy((void*)(send_buffer+ETH_ALEN), (void*)host_mac, ETH_ALEN);
#else
  unsigned char enet_src[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  memcpy((void*)(send_buffer+ETH_ALEN), (void*)enet_src, ETH_ALEN);
#endif
  // copy length to send_buffer
  unsigned char* length_str_reversed = (unsigned char*)&length;
  send_buffer[ETH_ALEN*2] = length_str_reversed[1];
  send_buffer[ETH_ALEN*2+1] = length_str_reversed[0];

  // copy user data to send_buffer
  memcpy((void*)(send_buffer+ETH_HLEN), (void*)data_p, length);

#ifdef _LINUX_
  // send packet
  int sent = sendto(socketw, send_buffer, length+ETH_HLEN, 0,
                    (struct sockaddr*)&socket_address, socklen);
  if (sent == -1) {
    perror("sendto():");
    exit(1);
  }
  /* printf("sending: %d bytes\n", length); */
/*   if (length == 64) */
/*     printf("%s\n", send_buffer); */
#else
  eth_send(socketw, send_buffer, length+ETH_HLEN);
  /* printf("sending: %d bytes\n", length); */
/*   if (length == 64) */
/*     printf("%s\n", send_buffer); */
#endif
  return 0;
}
示例#16
0
文件: arpd.c 项目: MagicalTux/farpd
static void
arpd_send(eth_t *eth, int op,
    struct addr *sha, struct addr *spa,
    struct addr *tha, struct addr *tpa)
{
	u_char pkt[ETH_HDR_LEN + ARP_HDR_LEN + ARP_ETHIP_LEN];

	eth_pack_hdr(pkt, tha->addr_eth, sha->addr_eth, ETH_TYPE_ARP);
	arp_pack_hdr_ethip(pkt + ETH_HDR_LEN, op, sha->addr_eth,
	    spa->addr_ip, tha->addr_eth, tpa->addr_ip);
	
	if (op == ARP_OP_REQUEST) {
		syslog(LOG_DEBUG, "%s: who-has %s tell %s", __func__,
		    addr_ntoa(tpa), addr_ntoa(spa));
	} else if (op == ARP_OP_REPLY) {
		syslog(LOG_INFO, "arp reply %s is-at %s",
		    addr_ntoa(spa), addr_ntoa(sha));
	}
	if (eth_send(eth, pkt, sizeof(pkt)) != sizeof(pkt))
		syslog(LOG_ERR, "couldn't send packet: %m");
}
示例#17
0
文件: daq_ipq.c 项目: remfalc/vyt-daq
static int ipq_daq_inject (
    void* handle, const DAQ_PktHdr_t* hdr, const uint8_t* buf, uint32_t len,
    int reverse)
{
    IpqImpl* impl = (IpqImpl*)handle;
    ssize_t sent = 0;

    if ( impl->link )
        sent = eth_send(impl->link, buf, len);

    else if ( impl->net )
        sent = ip_send(impl->net, buf, len);

    if ( (uint32_t)sent != len )
    {
        DPE(impl->error, "%s: failed to send",
            __FUNCTION__);
        return DAQ_ERROR;
    }
    impl->stats.packets_injected++;
    return DAQ_SUCCESS;
}
示例#18
0
static void
_resend_outgoing(struct pkt *pkt)
{
    if (timerisset(&pkt->pkt_ts)) {
        timeout_set(&pkt->pkt_ev, _timed_outgoing, pkt);
        timeout_add(&pkt->pkt_ev, &pkt->pkt_ts);
    } else {
        eth_pack_hdr(pkt->pkt_eth, ctx.dmac.addr_eth,
            ctx.smac.addr_eth, ETH_TYPE_IP);
        if(ctx.dfile) {
            struct pcap_pkthdr pkthdr;
            gettimeofday(&pkthdr.ts, NULL);
            pkthdr.caplen = pkthdr.len = pkt->pkt_end - pkt->pkt_data;
            pcap_dump((u_char*)ctx.dfile, &pkthdr, pkt->pkt_data);
            pcap_dump_flush(ctx.dfile);
        }
        else if (eth_send(ctx.eth, pkt->pkt_data,
            pkt->pkt_end - pkt->pkt_data) < 0)
            warn("eth_send");

        pkt_free(pkt);
    }
}
示例#19
0
/**
 * returns number of bytes sent on success or -1 on error
 * Note: it is theoretically possible to get a return code >0 and < len
 * which for most people would be considered an error (the packet wasn't fully sent)
 * so you may want to test for recode != len too.
 *
 * Most socket API's have two interesting errors: ENOBUFS & EAGAIN.  ENOBUFS
 * is usually due to the kernel buffers being full.  EAGAIN happens when you
 * try to send traffic faster then the PHY allows.
 */
int
sendpacket(sendpacket_t *sp, const u_char *data, size_t len, struct pcap_pkthdr *pkthdr)
{
    int retcode = 0, val;
    static u_char buffer[10000]; /* 10K bytes, enough for jumbo frames + pkthdr
                                  * larger than page size so made static to
                                  * prevent page misses on stack
                                  */
    static const size_t buffer_payload_size = sizeof(buffer) + sizeof(struct pcap_pkthdr);

    assert(sp);
    assert(data);

    if (len <= 0)
        return -1;

TRY_SEND_AGAIN:
    sp->attempt ++;

    switch (sp->handle_type) {
        case SP_TYPE_KHIAL:

            memcpy(buffer, pkthdr, sizeof(struct pcap_pkthdr));
            memcpy(buffer + sizeof(struct pcap_pkthdr), data, min(len, buffer_payload_size));

            /* tell the kernel module which direction the traffic is going */
            if (sp->cache_dir == TCPR_DIR_C2S) {  /* aka PRIMARY */
                val = KHIAL_DIRECTION_RX;
                if (ioctl(sp->handle.fd, KHIAL_SET_DIRECTION, (void *)&val) < 0) {
                    sendpacket_seterr(sp, "Error setting direction on %s: %s (%d)",
                            sp->device, strerror(errno), errno);
                    return -1;
                }
            } else if (sp->cache_dir == TCPR_DIR_S2C) {
                val = KHIAL_DIRECTION_TX;
                if (ioctl(sp->handle.fd, KHIAL_SET_DIRECTION, (void *)&val) < 0) {
                    sendpacket_seterr(sp, "Error setting direction on %s: %s (%d)",
                            sp->device, strerror(errno), errno);
                    return -1;
                }
            }

            /* write the pkthdr + packet data all at once */
            retcode = write(sp->handle.fd, (void *)buffer, sizeof(struct pcap_pkthdr) + len);
            retcode -= sizeof(struct pcap_pkthdr); /* only record packet bytes we sent, not pcap data too */
                    
            if (retcode < 0 && !sp->abort) {
                switch(errno) {
                    case EAGAIN:
                        sp->retry_eagain ++;
                        goto TRY_SEND_AGAIN;
                        break;
                    case ENOBUFS:
                        sp->retry_enobufs ++;
                        goto TRY_SEND_AGAIN;
                        break;
                    default:
                        sendpacket_seterr(sp, "Error with %s [" COUNTER_SPEC "]: %s (errno = %d)",
                                "khial", sp->sent + sp->failed + 1, strerror(errno), errno);
                }
                break;
            }

            break;

        case SP_TYPE_TUNTAP:
            retcode = write(sp->handle.fd, (void *)data, len);
            break;

            /* Linux PF_PACKET and TX_RING */
        case SP_TYPE_PF_PACKET:
        case SP_TYPE_TX_RING:
#if defined HAVE_PF_PACKET
#ifdef HAVE_TX_RING
            retcode = (int)txring_put(sp->tx_ring, data, len);
#else
            retcode = (int)send(sp->handle.fd, (void *)data, len, 0);
#endif

            /* out of buffers, or hit max PHY speed, silently retry
             * as long as we're not told to abort
             */
            if (retcode < 0 && !sp->abort) {
                switch (errno) {
                    case EAGAIN:
                        sp->retry_eagain ++;
                        goto TRY_SEND_AGAIN;
                        break;
                    case ENOBUFS:
                        sp->retry_enobufs ++;
                        goto TRY_SEND_AGAIN;
                        break;

                    default:
                        sendpacket_seterr(sp, "Error with %s [" COUNTER_SPEC "]: %s (errno = %d)", 
                                INJECT_METHOD, sp->sent + sp->failed + 1, strerror(errno), errno);
                }
            }

#endif /* HAVE_PF_PACKET */

            break;


        /* BPF */
        case SP_TYPE_BPF:
#if defined HAVE_BPF
            retcode = write(sp->handle.fd, (void *)data, len);

            /* out of buffers, or hit max PHY speed, silently retry */
            if (retcode < 0 && !sp->abort) {
                switch (errno) {
                    case EAGAIN:
                        sp->retry_eagain ++;
                        goto TRY_SEND_AGAIN;
                        break;

                    case ENOBUFS:
                        sp->retry_enobufs ++;
                        goto TRY_SEND_AGAIN;
                        break;

                    default:
                        sendpacket_seterr(sp, "Error with %s [" COUNTER_SPEC "]: %s (errno = %d)", 
                                INJECT_METHOD, sp->sent + sp->failed + 1, strerror(errno), errno);
                }
            }
#endif
            break;

        /* Libdnet */
        case SP_TYPE_LIBDNET:

#if defined HAVE_LIBDNET
            retcode = eth_send(sp->handle.ldnet, (void*)data, (size_t)len);

            /* out of buffers, or hit max PHY speed, silently retry */
            if (retcode < 0 && !sp->abort) {
                switch (errno) {
                    case EAGAIN:
                        sp->retry_eagain ++;
                        goto TRY_SEND_AGAIN;
                        break;

                    case ENOBUFS:
                        sp->retry_enobufs ++;
                        goto TRY_SEND_AGAIN;
                        break;

                    default:
                        sendpacket_seterr(sp, "Error with %s [" COUNTER_SPEC "]: %s (errno = %d)", 
                                INJECT_METHOD, sp->sent + sp->failed + 1, strerror(errno), errno);
                }
            }
#endif
            break;

        case SP_TYPE_LIBPCAP:
#if (defined HAVE_PCAP_INJECT || defined HAVE_PCAP_SENDPACKET)
#if defined HAVE_PCAP_INJECT
            /* 
             * pcap methods don't seem to support ENOBUFS, so we just straight fail
             * is there a better way???
             */
            retcode = pcap_inject(sp->handle.pcap, (void*)data, len);
#elif defined HAVE_PCAP_SENDPACKET
            retcode = pcap_sendpacket(sp->handle.pcap, data, (int)len);
#endif

            /* out of buffers, or hit max PHY speed, silently retry */
            if (retcode < 0 && !sp->abort) {
                switch (errno) {
                    case EAGAIN:
                        sp->retry_eagain ++;
                        goto TRY_SEND_AGAIN;
                        break;

                    case ENOBUFS:
                        sp->retry_enobufs ++;
                        goto TRY_SEND_AGAIN;
                        break;

                    default:
                        sendpacket_seterr(sp, "Error with %s [" COUNTER_SPEC "]: %s (errno = %d)", 
                                INJECT_METHOD, sp->sent + sp->failed + 1, pcap_geterr(sp->handle.pcap), errno);
                }
            }
#if defined HAVE_PCAP_SENDPACKET
            /* 
             * pcap_sendpacket returns 0 on success, not the packet length! 
             * hence, we have to fix retcode to be more standard on success
             */
            if (retcode == 0)
                retcode = len;
#endif /* HAVE_PCAP_SENDPACKET */

#endif /* HAVE_PCAP_INJECT || HAVE_PCAP_SENDPACKET */

            break;

        case SP_TYPE_QUICK_TX:
#ifdef HAVE_QUICK_TX
            retcode = quick_tx_send_packet(sp->qtx_dev, data, len);
            if (retcode < 0)
                sendpacket_seterr(sp, "Quick TX send failure");
#endif
            break;

        case SP_TYPE_NETMAP:
#ifdef HAVE_NETMAP
            retcode = sendpacket_send_netmap(sp, data, len);

            if (retcode == -1) {
                sendpacket_seterr(sp, "interface hung!!");
            } else if (retcode == -2) {
                /* this indicates that a retry was requested - this is not a failure */
                retcode = 0;
                goto TRY_SEND_AGAIN;
            }
#endif /* HAVE_NETMAP */
            break;

        default:
            errx(-1, "Unsupported sp->handle_type = %d", sp->handle_type);
    } /* end case */

    if (retcode < 0) {
        sp->failed ++;
    } else if (retcode != (int)len) {
        sendpacket_seterr(sp, "Only able to write %d bytes out of %u bytes total",
                retcode, len);
        sp->trunc_packets ++;
    } else {
        sp->bytes_sent += len;
        sp->sent ++;
    }
    return retcode;
}
示例#20
0
int dev_write_net(void *cookie, void *buf, int len)
{
	/* XXX verify that cookie points to a valid net device??? */

	return eth_send(buf, len);
}
示例#21
0
void retrans(struct my_pkthdr *h, u_char *pack ) {
  struct eth_hdr *ethhdr;
  struct ip_hdr *iphdr;
  struct addr srcad, srcha;
  char sip[32],smac[32];
  int n;

  ethhdr = (struct eth_hdr *)pack;
  iphdr = (struct ip_hdr *)(pack + ETH_HDR_LEN);

/*
DELETE ME WHEN FINISHED
struct addr ad;
struct addr mad, mha;        		// my ip, mac
struct addr vad, vha, vprt;        	// victim ip, mac
struct addr aad, aha, aprt;        	// attacker ip, mac
struct addr revi_ip, revi_mac;		// replay victim ip, mac
struct addr reat_ip, reat_mac;		// replay attacker ip, mac

char mip[32], mhw[32];       		// my ip, mac
char vip[32], vhw[32], vpt[32];       	// victim ip, mac
char aip[32], ahw[32], apt[32];       	// attacker ip, mac
char rvip[32], rvmc[32];		// replay victim ip, mac
char ratip[32], ratmac[32];		// replay attacker ip, mac*/


  // Get source addresses from packet (mac and ip)
  addr_pack(&srcha,ADDR_TYPE_ETH,ETH_ADDR_BITS,&(ethhdr->eth_src),ETH_ADDR_LEN);
  addr_pack(&srcad,ADDR_TYPE_IP,IP_ADDR_BITS,&(iphdr->ip_src),IP_ADDR_LEN);
  if((strcmp(addr_ntoa(&srcha),vhw)==0)){
	// Replace source address with my address and destination address
	memcpy( &ethhdr->eth_src, &revi_mac.addr_eth, ETH_ADDR_LEN);
	memcpy( &iphdr->ip_src, &revi_ip.addr_ip, IP_ADDR_LEN);

	// Replace destination address with other client
	if ( addr_cmp( &srcad, &vad ) == 0 ) {
		memcpy( &ethhdr->eth_dst, &reat_mac.addr_eth, ETH_ADDR_LEN);
		memcpy( &iphdr->ip_dst, &reat_ip.addr_ip, IP_ADDR_LEN);
	}else{
		memcpy( &ethhdr->eth_dst, &revi_mac.addr_eth, ETH_ADDR_LEN);
		memcpy( &iphdr->ip_dst, &revi_ip.addr_ip, IP_ADDR_LEN);
	}

	// Compute both ip and tcp checksums
	ip_checksum((void *)iphdr, ntohs(iphdr->ip_len));
		// Send packet
		n = eth_send(e,pack,h->len);
	if ( n != h->len ) { 
		fprintf(stderr,"Partial packet transmission %d/%d\n",n,h->len);
	} else {
		fprintf(stdout, "Packet Transmission Successfull %d %d\n", n, h->len);
	}
  }

  if((strcmp(addr_ntoa(&srcha),ahw)==0)){
	// Replace source address with my address and destination address
	memcpy( &ethhdr->eth_src, &reat_mac.addr_eth, ETH_ADDR_LEN);
	memcpy( &iphdr->ip_src, &reat_ip.addr_ip, IP_ADDR_LEN);

	// Replace destination address with other client
	if ( addr_cmp( &srcad, &aad ) == 0 ) {
		memcpy( &ethhdr->eth_dst, &revi_mac.addr_eth, ETH_ADDR_LEN);
		memcpy( &iphdr->ip_dst, &revi_ip.addr_ip, IP_ADDR_LEN);
	}else{
		memcpy( &ethhdr->eth_dst, &reat_mac.addr_eth, ETH_ADDR_LEN);
		memcpy( &iphdr->ip_dst, &reat_ip.addr_ip, IP_ADDR_LEN);
	}

	// Compute both ip and tcp checksums
	ip_checksum((void *)iphdr, ntohs(iphdr->ip_len));
		// Send packet
		n = eth_send(e,pack,h->len);
	if ( n != h->len ) { 
		fprintf(stderr,"Partial packet transmission %d/%d\n",n,h->len);
	} else {
		fprintf(stdout, "Packet Transmission Successfull %d %d\n", n, h->len);
	}
   }
}
示例#22
0
文件: ntapy.c 项目: ChrisRx/ntapy
void packet_retrans(char *user, struct pcap_pkthdr *pkthdr, u_char *pkt)
{
    eth_send(eth_retrans, pkt, pkthdr->caplen);
    return;
}
示例#23
0
文件: eth.c 项目: 0xBADCA7/lk
static int eth_rx_worker(void *arg)
{
    for (;;) {
#if 0
        status_t event_err = event_wait_timeout(&eth.rx_event, 1000);
        if (event_err == ERR_TIMED_OUT) {
            /* periodically poll the phys status register */
            /* XXX specific to DP83848 */
            uint32_t val;

            /* Read PHY_MISR */
            /* seems to take about 30 usecs */
            HAL_ETH_ReadPHYRegister(&eth.EthHandle, PHY_MISR, &val);

            /* Check whether the link interrupt has occurred or not */
            if (val & PHY_LINK_INTERRUPT) {
                /* Read PHY_SR*/
                HAL_ETH_ReadPHYRegister(&eth.EthHandle, PHY_SR, &val);

                /* Check whether the link is up or down*/
                if (val & PHY_LINK_STATUS) {
                    printf("eth: link up\n");
                    //netif_set_link_up(link_arg->netif);
                } else {
                    printf("eth: link down\n");
                    //netif_set_link_down(link_arg->netif);
                }
            }
        } else {
#else
        status_t event_err = event_wait(&eth.rx_event);
        if (event_err >= NO_ERROR) {
#endif
            // XXX probably race with the event here
            while (HAL_ETH_GetReceivedFrame_IT(&eth.EthHandle) == HAL_OK) {
                LTRACEF("got packet len %u, buffer %p, seg count %u\n", eth.EthHandle.RxFrameInfos.length,
                        (void *)eth.EthHandle.RxFrameInfos.buffer,
                        eth.EthHandle.RxFrameInfos.SegCount);

#if WITH_LIB_MINIP
                /* allocate a pktbuf header, point it at our rx buffer, and pass up the stack */
                pktbuf_t *p = pktbuf_alloc_empty();
                if (p) {
                    pktbuf_add_buffer(p, (void *)eth.EthHandle.RxFrameInfos.buffer, eth.EthHandle.RxFrameInfos.length,
                                      0, 0, NULL, NULL);
                    p->dlen = eth.EthHandle.RxFrameInfos.length;

                    minip_rx_driver_callback(p);

                    pktbuf_free(p, true);
                }
#endif

                /* Release descriptors to DMA */
                /* Point to first descriptor */
                __IO ETH_DMADescTypeDef *dmarxdesc;

                dmarxdesc = eth.EthHandle.RxFrameInfos.FSRxDesc;
                /* Set Own bit in Rx descriptors: gives the buffers back to DMA */
                for (uint i=0; i< eth.EthHandle.RxFrameInfos.SegCount; i++) {
                    dmarxdesc->Status |= ETH_DMARXDESC_OWN;
                    dmarxdesc = (ETH_DMADescTypeDef *)(dmarxdesc->Buffer2NextDescAddr);
                }

                /* Clear Segment_Count */
                eth.EthHandle.RxFrameInfos.SegCount =0;

                /* When Rx Buffer unavailable flag is set: clear it and resume reception */
                if ((eth.EthHandle.Instance->DMASR & ETH_DMASR_RBUS) != (uint32_t)RESET) {
                    /* Clear RBUS ETHERNET DMA flag */
                    eth.EthHandle.Instance->DMASR = ETH_DMASR_RBUS;
                    /* Resume DMA reception */
                    eth.EthHandle.Instance->DMARPDR = 0;
                }
            }
        }
    }

    return 0;
}

#if WITH_LIB_MINIP

status_t stm32_eth_send_minip_pkt(pktbuf_t *p)
{
    LTRACEF("p %p, dlen %zu, eof %u\n", p, p->dlen, p->flags & PKTBUF_FLAG_EOF);

    DEBUG_ASSERT(p && p->dlen);

    if (!(p->flags & PKTBUF_FLAG_EOF)) {
        /* can't handle multi part packets yet */
        PANIC_UNIMPLEMENTED;

        return ERR_NOT_IMPLEMENTED;
    }

    status_t err = eth_send(p->data, p->dlen);

    pktbuf_free(p, true);

    return err;
}
示例#24
0
//------------------------------------------------------------------------------
int HWInit(bd_t * bd)
{
	int btemp;
	unsigned int duplex, speed;
	unsigned long tmp1, mst = 250;

	// Enable MAC interface
	CLKPWR->clkpwr_macclk_ctrl = (CLKPWR_MACCTRL_HRCCLK_EN |
		CLKPWR_MACCTRL_MMIOCLK_EN | CLKPWR_MACCTRL_DMACLK_EN |
#ifdef USE_PHY_RMII
		CLKPWR_MACCTRL_USE_RMII_PINS);
#else
		CLKPWR_MACCTRL_USE_MII_PINS);
#endif

	// Set RMII management clock rate. This clock should be slower
	// than 12.5MHz (for NXP PHYs only). For a divider of 28, the
	// clock rate when HCLK is 150MHz will be 5.4MHz
	ENETMAC->mcfg = MCFG_CLOCK_SELECT(MCFG_CLOCK_HOST_DIV_28);

	// Reset all MAC logic
	ENETMAC->mac1 = (MAC1_SOFT_RESET | MAC1_SIMULATION_RESET |
		MAC1_RESET_MCS_TX | MAC1_RESET_TX | MAC1_RESET_MCS_RX |
		MAC1_RESET_RX);
	ENETMAC->command = (COMMAND_REG_RESET | COMMAND_TXRESET |
		COMMAND_RXRESET);
	msDelay(10);

	// Initial MAC initialization
	ENETMAC->mac1 = MAC1_PASS_ALL_RX_FRAMES;
	ENETMAC->mac2 = (MAC2_PAD_CRC_ENABLE | MAC2_CRC_ENABLE);
	ENETMAC->maxf = ENET_MAXF_SIZE;

	// Maximum number of retries, 0x37 collision window, gap */
	ENETMAC->clrt = (CLRT_LOAD_RETRY_MAX(0xF) |
		CLRT_LOAD_COLLISION_WINDOW(0x37));
	ENETMAC->ipgr = IPGR_LOAD_PART2(0x12);

#ifdef USE_PHY_RMII
	// RMII setup
	ENETMAC->command = (COMMAND_RMII | COMMAND_PASSRUNTFRAME);
	ENETMAC->supp = SUPP_RESET_RMII;
	msDelay(10);
#else
	// MII setup
	ENETMAC->command = COMMAND_PASSRUNTFRAME;
#endif
	// Enable rate auto-negotiation for the link
	if (RMII_Write(PHY_BMCR,
		(PHY_BMCR_100MB | PHY_BMCR_AUTON)) == 0)
	{
		return 0;
	}
        
	mst = 1000;
        btemp = 1;
        while(mst > 0)
        {
                /* Wait for Link status to set UP or Timeout */
                if(phy_get_link_status() == 0) {
                        mst = 0;
                        btemp = 0;
                        printf("ENET:auto-negotiation complete#$\n");
                }
                else {
                        mst--;
                        msDelay(1);
                }
        }
        if(btemp) {
                printf("ENET:auto-negotiation failed#$\n");
                return 0;
        }

        /* Read PHY Status Register to determine Ethernet Configuration */
        tmp1 = 0;
        RMII_Read (DP83848_PHY_STATUS,&tmp1);
        duplex = (tmp1 & 0x0004) >> 2;
        speed = (tmp1 & 0x0002) >> 1;

	// Configure Full/Half Duplex mode
	if (duplex == 1)
	{
		// 10MBase full duplex is supported
		ENETMAC->mac2 |= MAC2_FULL_DUPLEX;
		ENETMAC->command |= COMMAND_FULLDUPLEX;
		ENETMAC->ipgt = IPGT_LOAD(0x15);
		printf("ENET:FULL DUPLEX\n");
	}
	else
	{
		ENETMAC->ipgt = IPGT_LOAD(0x12);
		printf("ENET:HALF DUPLEX\n");
	}

	// Configure 100MBit/10MBit mode
	if (speed == 0)
	{
		// 100MBase mode
		ENETMAC->supp = SUPP_SPEED;
		printf("ENET:100MBase\n");
	}
	else
	{
		// 10MBase mode
		ENETMAC->supp = 0;
		printf("ENET:10Base\n");
	}

	// Save station address
	ENETMAC->sa [2] = (unsigned long) (bd->bi_enetaddr[0] | (bd->bi_enetaddr[1] << 8));
	ENETMAC->sa [1] = (unsigned long) (bd->bi_enetaddr[2] | (bd->bi_enetaddr[3] << 8));
	ENETMAC->sa [0] = (unsigned long) (bd->bi_enetaddr[4] | (bd->bi_enetaddr[5] << 8));

	// Setup TX and RX descriptors
	txrx_setup();

	// Enable broadcast and matching address packets
	ENETMAC->rxfliterctrl = (RXFLTRW_ACCEPTUBROADCAST |
		RXFLTRW_ACCEPTPERFECT);

	// Clear and enable interrupts
	ENETMAC->intclear = 0xFFFF;
	ENETMAC->intenable = 0;

	// Enable receive and transmit mode of MAC ethernet core
	ENETMAC->command |= (COMMAND_RXENABLE | COMMAND_TXENABLE);
	ENETMAC->mac1 |= MAC1_RECV_ENABLE;

	// Perform a 'dummy' send of the first ethernet frame with a size of 0
	// to 'prime' the MAC. The first packet after a reset seems to wait
	// until at least 2 packets are ready to go.
	tmp1 = 0;
	eth_send(&tmp1, 4);

	return 1;
}