示例#1
0
int main( void )
{
    HWADDR hwaddr;

    if( gethwaddr( IFNAME, hwaddr ) == -1 )
    {
        fprintf( stderr, "Failed to get MAC address of %s\n", IFNAME );

        return 1;
    }

    printf("MAC address of %s = %02x:%02x:%02x:%02x:%02x:%02x\n",
           IFNAME, hwaddr[ 0 ], hwaddr[ 1 ], hwaddr[ 2 ],
           hwaddr[ 3 ], hwaddr[ 4 ], hwaddr[ 5 ]);

    return 0;
}
void config_BSP(void)
{
#if defined(CONFIG_UCBOOTSTRAP)
	unsigned char *p;

	printk("Arcturus Networks Inc. uC5471DSP uCbootstrap system calls\n");
	printk("   Serial number: [%s]\n", getserialnum());

	p = gethwaddr(0);
	if (p == NULL) {
		memcpy (fec_hwaddr, "\0x00\0xde\0xad\0xbe\0xef\0x27", 6);
		p = fec_hwaddr;
		printk ("Warning: HWADDR0 not set in uCbootloader. Using default.\n");
	} else {
		memcpy (fec_hwaddr, p, 6);
	}
	printk("uCbootstrap eth addr 0 %02x:%02x:%02x:%02x:%02x:%02x\n",
		p[0], p[1], p[2], p[3], p[4], p[5]);

#endif
}
示例#3
0
signed openchannel (struct channel * channel)

{

#if defined (__linux__)

	struct ifreq ifreq;
	struct sockaddr_ll sockaddr_ll =
	{
		PF_PACKET,
		0x0000,
		0x0000,
		ARPHRD_ETHER,
		PACKET_HOST,
		ETHER_ADDR_LEN,
		{
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00,
			0x00
		}
	};

/*
 *      raw packets require root privileges on linux; one does not have to be
 *      root when this program is installed setuid using 'chown root:root' and
 *      'chmod 4555';
 */

	if (geteuid ())
	{
		error (1, EPERM, ERROR_NOTROOT);
	}

	memset (&ifreq, 0, sizeof (ifreq));
	sockaddr_ll.sll_protocol = htons (channel->type);
	if ((channel->fd = socket (sockaddr_ll.sll_family, SOCK_RAW, sockaddr_ll.sll_protocol)) == -1)
	{
		error (1, errno, "%s", channel->ifname);
	}
	memcpy (ifreq.ifr_name, channel->ifname, sizeof (ifreq.ifr_name));
	if (ioctl (channel->fd, SIOCGIFINDEX, &ifreq) == -1)
	{
		error (1, errno, "%s", ifreq.ifr_name);
	}
	channel->ifindex = sockaddr_ll.sll_ifindex = ifreq.ifr_ifindex;
	if (ioctl (channel->fd, SIOCGIFHWADDR, &ifreq) == -1)
	{
		error (1, errno, "%s", ifreq.ifr_name);
	}
	memcpy (sockaddr_ll.sll_addr, ifreq.ifr_ifru.ifru_hwaddr.sa_data, sizeof (sockaddr_ll.sll_addr));
	if (bind (channel->fd, (struct sockaddr *) (&sockaddr_ll), sizeof (sockaddr_ll)) == -1)
	{
		error (1, errno, "%s", ifreq.ifr_name);
	}
	memcpy (channel->host, sockaddr_ll.sll_addr, sizeof (channel->host));
	if (ioctl (channel->fd, SIOCGIFFLAGS, &ifreq) == -1)
	{
		error (1, errno, "%s", ifreq.ifr_name);
	}
	channel->ifstate = ifreq.ifr_flags;
	_setbits (ifreq.ifr_flags, (IFF_UP | IFF_BROADCAST | IFF_MULTICAST));
	_clrbits (ifreq.ifr_flags, (IFF_ALLMULTI | IFF_PROMISC));
	if (ioctl (channel->fd, SIOCSIFFLAGS, &ifreq) == -1)
	{
		error (1, errno, "%s", ifreq.ifr_name);
	}

#else

	struct bpf_program bpf_program;
	static struct bpf_insn bpf_insn [] =
	{
		{
			BPF_LD + BPF_H + BPF_ABS,
			0,
			0,
			12
		},
		{
			BPF_JMP + BPF_JEQ + BPF_K,
			0,
			18,
			0
		},
		{
			BPF_LD + BPF_B + BPF_ABS,
			0,
			0,
			0
		},
		{
			BPF_JMP + BPF_JEQ + BPF_K,
			0,
			10,
			0
		},
		{
			BPF_LD + BPF_B + BPF_ABS,
			0,
			0,
			1
		},
		{
			BPF_JMP + BPF_JEQ + BPF_K,
			0,
			8,
			0
		},
		{
			BPF_LD + BPF_B + BPF_ABS,
			0,
			0,
			2
		},
		{
			BPF_JMP + BPF_JEQ + BPF_K,
			0,
			6,
			0
		},
		{
			BPF_LD + BPF_B + BPF_ABS,
			0,
			0,
			3
		},
		{
			BPF_JMP + BPF_JEQ + BPF_K,
			0,
			4,
			0
		},
		{
			BPF_LD + BPF_B + BPF_ABS,
			0,
			0,
			4
		},
		{
			BPF_JMP + BPF_JEQ + BPF_K,
			0,
			2,
			0
		},
		{
			BPF_LD + BPF_B + BPF_ABS,
			0,
			0,
			5
		},
		{
			BPF_JMP + BPF_JEQ + BPF_K,
			4,
			0,
			0
		},
		{
			BPF_LD + BPF_W + BPF_ABS,
			0,
			0,
			0
		},
		{
			BPF_JMP + BPF_JEQ + BPF_K,
			0,
			4,
			0xFFFFFFFF
		},
		{
			BPF_LD + BPF_H + BPF_ABS,
			0,
			0,
			4
		},
		{
			BPF_JMP + BPF_JEQ + BPF_K,
			0,
			2,
			0xFFFF
		},
		{
			BPF_LD + BPF_W + BPF_LEN,
			0,
			0,
			0
		},
		{
			BPF_RET + BPF_A,
			0,
			0,
			0
		},
		{
			BPF_RET + BPF_K,
			0,
			0,
			0
		}
	};

#if defined (__APPLE__) || defined (__OpenBSD__) || defined (__NetBSD__)

	struct ifreq ifreq;
	struct timeval timeval;
	struct bpf * bpf;
	char filename [sizeof (CHANNEL_BPFDEVICE) + 1];
	unsigned count;
	unsigned state;
	int stat_errno = 0;
	int open_errno = 0;
	for (count = 0; count < 100; count++)
	{
		struct stat st;
		snprintf (filename, sizeof (filename), CHANNEL_BPFDEVICE, count);
		if (stat(filename, &st) == -1)
		{
			stat_errno = errno;
			continue;
		}
		if ((channel->fd = open (filename, O_RDWR)) != -1)
		{
			break;
		}
		else
		{
			open_errno = errno;
		}
	}
	if (channel->fd == -1)
	{
		if (open_errno)
		{
			error (1, open_errno, "Could not open bpf device");
		}
		else
		{
			error (1, stat_errno, "No bpf device found");
		}
	}
	memcpy (ifreq.ifr_name, channel->ifname, sizeof (ifreq.ifr_name));
	if (ioctl (channel->fd, BIOCSETIF, &ifreq) == -1)
	{
		error (1, errno, "%s", ifreq.ifr_name);
	}
	channel->bpf = bpf = malloc (sizeof (* bpf));
	if (ioctl (channel->fd, BIOCGBLEN, &bpf->bpf_length) == -1)
	{
		error (1, errno, "Can't determine buffer length: %s", ifreq.ifr_name);
	}
	bpf->bpf_bp = bpf->bpf_buffer = malloc (bpf->bpf_length);
	if (bpf->bpf_buffer == NULL)
	{
		error (1, errno, "Can't allocate receive buffer");
	}

#if defined (__APPLE__) || defined (__NetBSD__)

	state = 0;
	if (ioctl (channel->fd, BIOCSSEESENT, &state) == -1)
	{
		error (1, errno, "Can't hide outgoing frames: %s", ifreq.ifr_name);
	}

#elif defined (__OpenBSD__)

	state = BPF_DIRECTION_OUT;
	if (ioctl (channel->fd, BIOCSDIRFILT, &state) == -1)
	{
		error (0, errno, "Can't hide outgoing frames");
	}

#else
#error "Abandon all hope"
#endif

	if (channel->capture > 1000)
	{
		timeval.tv_sec = channel->capture / 1000;
		timeval.tv_usec = 0;
	}
	else
	{

#if defined (__MAC_10_6)

/*
 *	accommodate known bug in BPF on MAC OS X 10.6; shorter times cause socket read
 *	operations to block indefinitely if no frames are waiting because tv_usec gets
 *      clobbered;
 */

		timeval.tv_sec = 1;
		timeval.tv_usec = 0;

#else

		timeval.tv_sec = 0;
		timeval.tv_usec = channel->capture * 1000;

#endif

	}
	if (ioctl (channel->fd, BIOCSRTIMEOUT, &timeval) == -1)
	{
		error (1, errno, "Can't set channel timeout: %s", ifreq.ifr_name);
	}
	state = 1;
	if (ioctl (channel->fd, BIOCIMMEDIATE, &state) == -1)
	{
		error (1, errno, "Can't set immediate mode: %s", ifreq.ifr_name);
	}

#if 1

	state = 1;
	if (ioctl (channel->fd, BIOCSHDRCMPLT, &state) == -1)
	{
		error (1, errno, "Can't set header complete mode: %s", ifreq.ifr_name);
	}

#endif

#if 1

	gethwaddr (channel->host, channel->ifname);

#else

	if (ioctl (channel->fd, SIOCGIFADDR, &ifreq) > 0)
	{
		error (1, errno, "%s", ifreq.ifr_name);
	}
	memcpy (channel->host, LLADDR (ifreq.ifr_ifru.ifru_addr), sizeof (channel->host));

#endif

	bpf_program.bf_len = sizeof (bpf_insn) / sizeof (struct bpf_insn);
	bpf_program.bf_insns = bpf_insn;
	if (channel->type == ETH_P_802_2)
	{
		bpf_insn [1].code = BPF_JMP + BPF_JGT + BPF_K;
		bpf_insn [1].jt = 18;
		bpf_insn [1].jf = 0;
		bpf_insn [1].k = ETHERMTU;
	}
	else
	{
		bpf_insn [1].code = BPF_JMP + BPF_JEQ + BPF_K;
		bpf_insn [1].jt = 0;
		bpf_insn [1].jf = 18;
		bpf_insn [1].k = channel->type;
	}
	bpf_insn [3].k = channel->host [0];
	bpf_insn [5].k = channel->host [1];
	bpf_insn [7].k = channel->host [2];
	bpf_insn [9].k = channel->host [3];
	bpf_insn [11].k = channel->host [4];
	bpf_insn [13].k = channel->host [5];
	if (ioctl (channel->fd, BIOCSETF, &bpf_program) == -1)
	{
		error (1, errno, "Can't store filter: %s", channel->ifname);
	}

#elif defined (WINPCAP) || defined (LIBPCAP)

	channel->ifname = getifname (channel->ifindex);
	gethwaddr (channel->host, channel->ifname);
	channel->socket = pcap_open_live (channel->ifname, 65536, 0, channel->capture, channel->errbuf);
	snprintf ((char *)(channel->ifname), strlen (channel->ifname), "nic%d", channel->ifindex);
	if (!channel->socket)
	{
		error (1, errno, "Can't open interface: %s", channel->ifname);
	}
	bpf_program.bf_len = sizeof (bpf_insn)/sizeof (struct bpf_insn);
	bpf_program.bf_insns = bpf_insn;
	if (channel->type == ETH_P_802_2)
	{
		bpf_insn [1].code = BPF_JMP + BPF_JGT + BPF_K;
		bpf_insn [1].jt = 18;
		bpf_insn [1].jf = 0;
		bpf_insn [1].k = ETHERMTU;
	}
	else
	{
		bpf_insn [1].code = BPF_JMP + BPF_JEQ + BPF_K;
		bpf_insn [1].jt = 0;
		bpf_insn [1].jf = 18;
		bpf_insn [1].k = channel->type;
	}
	bpf_insn [3].k = channel->host [0];
	bpf_insn [5].k = channel->host [1];
	bpf_insn [7].k = channel->host [2];
	bpf_insn [9].k = channel->host [3];
	bpf_insn [11].k = channel->host [4];
	bpf_insn [13].k = channel->host [5];
	if (pcap_setfilter (channel->socket, &bpf_program) < 0)
	{
		error (1, errno, "Can't store filter: %s", channel->ifname);
	}
	if (pcap_setmintocopy (channel->socket, ETHER_MIN_LEN) < 0)
	{
		error (1, errno, "Can't set pcap mintocopy: %s", channel->ifname);
	}

#else
#error "Unknown Environment"
#endif
#endif

	return (0);
}
示例#4
0
int main(){
	int sock,size,flags;
	char buf[1024];
	struct sockaddr_ll addr;
	struct ifreq ifr;
	unsigned char hwaddr[6];
	PACKET pk_start;
	if((sock=socket(PF_PACKET,SOCK_RAW,htons(ETH_P_PAE)))<0){
		printf("create raw socket error\n");
		return -1;
	}
	/*flags=fcntl(sock,F_GETFL,0);
	printf("%d\n",flags);
	if(fcntl(sock,F_SETFL,flags|O_NONBLOCK)<0)
	{
		printf("set nonblock error\n");
		return -1;
	}*/
	strcpy(ifr.ifr_name,"eth0");
	if(ioctl(sock,SIOCGIFINDEX,&ifr)<0)
	{
		printf("get ifindex error\n");
		return -1;
	}
	printf("%d\n",ifr.ifr_ifindex);
	bzero(&addr,sizeof(struct sockaddr_ll));
	addr.sll_family=PF_PACKET;
	addr.sll_protocol=htons(ETH_P_PAE);
	addr.sll_ifindex=ifr.ifr_ifindex;
	if(bind(sock,(struct sockaddr *)&addr,sizeof(struct sockaddr_ll))<0)
	{
		printf("bind error\n");
		return -1;
	}
	if(gethwaddr(hwaddr)<0)
	{
		printf("get hwaddr error\n");
		return -1;
	}
	printf("mac %02x:%02x:%02x:%02x:%02x:%02x\n",hwaddr[0],hwaddr[1],hwaddr[2],hwaddr[3],hwaddr[4],hwaddr[5]);
	get_start_packet(&pk_start,hwaddr);
	if(sendto(sock,&pk_start,sizeof(pk_start),0,(struct sockaddr *)&addr,sizeof(struct sockaddr_ll))<0)
	{
		printf("send packet error");
		return -1;
	}
	PACKET pkt_recv;
	printf("recv message\n");
	//block until receive message
	size=recvfrom(sock,(void *)&pkt_recv,sizeof(pkt_recv),0,NULL,NULL);
	int type = packet_type(&pkt_recv);
	int id = pkt_recv.body.test.id;
	printf("id is %d\n",id);
	if(type!=1)
	{
		printf("warning:identity packet type is 1\n");
		return -1;
	}
	PACKET pkt_id;
	get_identity_packet(&pkt_id,id,hwaddr);
	if(sendto(sock,&pkt_id,sizeof(pk_start),0,(struct sockaddr *)&addr,sizeof(struct sockaddr_ll))<0)
	{
		printf("send packet error");
		return -1;
	}
	bzero(&pkt_recv,sizeof(pkt_recv));
	size=recvfrom(sock,(void *)&pkt_recv,sizeof(pkt_recv),0,NULL,NULL);
	printf("%d\n",size);
	return 0;
}
示例#5
0
void pcap_enum (flag_t flags)

{
	char report [PCAP_ERRBUF_SIZE];
	char string [ETHER_ADDR_LEN * 3];
	byte number [ETHER_ADDR_LEN];
	pcap_if_t * device;
	pcap_if_t * devices = (pcap_if_t *)(0);
	unsigned index;
	if (pcap_findalldevs (&devices, report) == -1)
	{
		error (1, 0, "Can't enumerate interfaces");
	}
	if (!devices)
	{
		error (1, 0, "No interfaces available");
	}
	if (_anyset (flags, PCAP_DEVICES))
	{
		for (device = devices, index = 1; device; device = device->next, index++)
		{
			gethwaddr (number, device->name);
			hexdecode (number, sizeof (number), string, sizeof (string));
			printf ("%2d %s %s", index, string, device->name);
			if (device->description)
			{
				printf ("\t(%s)", device->description);
			}
			printf ("\n");
		}
	}
	if (_anyset (flags, PCAP_NICS))
	{
		for (device = devices, index = 1; device; device = device->next, index++)
		{

#if defined (WIN32)

			printf ("ETH%d=%d", index, index);

#else

			printf ("ETH%d=%s", index, device->name);

#endif

			if (device->description)
			{
				printf ("\t# %s", device->description);
			}
			printf ("\n");
		}
		printf ("\n");
	}
	if (_anyset (flags, PCAP_MACS))
	{
		for (device = devices, index = 1; device; device = device->next, index++)
		{
			gethwaddr (number, device->name);
			hexdecode (number, sizeof (number), string, sizeof (string));
			printf ("NIC%d=%s", index, string);
			if (device->description)
			{
				printf ("\t# %s", device->description);
			}
			printf ("\n");
		}
		printf ("\n");
	}
	pcap_freealldevs (devices);
	return;
}