Example #1
0
void
bridge_cfg(const char *delim)
{
	struct ifbropreq ifbp;
	u_int16_t pri;
	u_int8_t ht, fd, ma, hc, proto;
	u_int8_t lladdr[ETHER_ADDR_LEN];
	u_int16_t bprio;

	strlcpy(ifbp.ifbop_name, name, sizeof(ifbp.ifbop_name));
	if (ioctl(s, SIOCBRDGGPARAM, (caddr_t)&ifbp))
		err(1, "%s", name);
	printf("%s", delim);
	pri = ifbp.ifbop_priority;
	ht = ifbp.ifbop_hellotime;
	fd = ifbp.ifbop_fwddelay;
	ma = ifbp.ifbop_maxage;
	hc = ifbp.ifbop_holdcount;
	proto = ifbp.ifbop_protocol;

	printf("priority %u hellotime %u fwddelay %u maxage %u "
	    "holdcnt %u proto %s\n", pri, ht, fd, ma, hc, stpproto[proto]);

	if (aflag)
		return;

	PV2ID(ifbp.ifbop_desg_bridge, bprio, lladdr);
	printf("\tdesignated: id %s priority %u\n",
	    ether_ntoa((struct ether_addr *)lladdr), bprio);

	if (ifbp.ifbop_root_bridge == ifbp.ifbop_desg_bridge)
		return;

	PV2ID(ifbp.ifbop_root_bridge, bprio, lladdr);
	printf("\troot: id %s priority %u ifcost %u port %u\n",
	    ether_ntoa((struct ether_addr *)lladdr), bprio,
	    ifbp.ifbop_root_path_cost, ifbp.ifbop_root_port & 0xfff);
}
Example #2
0
/*Look if the source mac address is a broadcast addr or is all zeros*/
int watch_eth_broadcast(char* buffer, uint16_t vlan_id, struct ether_header* eptr, struct ip6_hdr* ipptr)
{
	struct ether_addr* eth_addr = (struct ether_addr*) eptr->ether_shost;
	struct ether_addr* test = malloc(sizeof(struct ether_addr));
	char str_ip[IP6_STR_SIZE];
	int broad =0;


	bzero(test,6);
	if (MEMCMP(eth_addr, test,6) ==0)
		broad=1;
	else
	{
		memset(test,255,6);
		if(MEMCMP(eth_addr, test,6)==0)
			broad= 1; 
		else
		{
			char* test2= "33:33:0:0:0:1";
			if(strcmp(ether_ntoa(eth_addr), test2)==0)
				broad=1;
		}
	}

	if(broad)
	{
		ipv6_ntoa(str_ip, ipptr->ip6_src);
		snprintf (buffer, NOTIFY_BUFFER_SIZE,  "VLAN%d: ethernet broadcast %s %s",vlan_id,ether_ntoa(eth_addr), str_ip);
		free(test);
		notify(1, buffer, "ethernet broadcast", eth_addr, str_ip, NULL);
		return 1;
	} else {
	    free(test);
	    return 0;	
	}
	
}
Example #3
0
void
bridge_addrs(const char *delim, int d)
{
	char dstaddr[NI_MAXHOST];
	char dstport[NI_MAXSERV];
	const int niflag = NI_NUMERICHOST|NI_DGRAM;
	struct ifbaconf ifbac;
	struct ifbareq *ifba;
	char *inbuf = NULL, buf[sizeof(ifba->ifba_ifsname) + 1], *inb;
	struct sockaddr *sa;
	int i, len = 8192;

	/* ifconfig will call us with the argv of the command */
	if (strcmp(delim, "addr") == 0)
		delim = "";

	while (1) {
		ifbac.ifbac_len = len;
		inb = realloc(inbuf, len);
		if (inb == NULL)
			err(1, "malloc");
		ifbac.ifbac_buf = inbuf = inb;
		strlcpy(ifbac.ifbac_name, name, sizeof(ifbac.ifbac_name));
		if (ioctl(s, SIOCBRDGRTS, &ifbac) < 0) {
			if (errno == ENETDOWN)
				return;
			err(1, "%s", name);
		}
		if (ifbac.ifbac_len + sizeof(*ifba) < len)
			break;
		len *= 2;
	}

	for (i = 0; i < ifbac.ifbac_len / sizeof(*ifba); i++) {
		ifba = ifbac.ifbac_req + i;
		strlcpy(buf, ifba->ifba_ifsname, sizeof(buf));
		printf("%s%s %s %u ", delim, ether_ntoa(&ifba->ifba_dst),
		    buf, ifba->ifba_age);
		sa = (struct sockaddr *)&ifba->ifba_dstsa;
		printb("flags", ifba->ifba_flags, IFBAFBITS);
		if (sa->sa_family != AF_UNSPEC &&
		    getnameinfo(sa, sa->sa_len,
		    dstaddr, sizeof(dstaddr),
		    dstport, sizeof(dstport), niflag) == 0)
			printf(" tunnel %s:%s", dstaddr, dstport);
		printf("\n");
	}
	free(inbuf);
}
Example #4
0
static int get_dest_addr(const char *hostid, struct ether_addr *eaddr)
{
	struct ether_addr *eap;

	eap = ether_aton(hostid);
	if (eap) {
		*eaddr = *eap;
		if (debug)
			fprintf(stderr, "The target station address is %s.\n",
					ether_ntoa(eaddr));
	} else if (ether_hostton(hostid, eaddr) == 0) {
		if (debug)
			fprintf(stderr, "Station address for hostname %s is %s.\n",
					hostid, ether_ntoa(eaddr));
	} else {
		(void)fprintf(stderr,
					  "ether-wake: The Magic Packet host address must be "
					  "specified as\n"
					  "  - a station address, 00:11:22:33:44:55, or\n"
					  "  - a hostname with a known 'ethers' entry.\n");
		return -1;
	}
	return 0;
}
Example #5
0
File: rarpd.c Project: afett/rarpd
/*
   This may (and will) block.
   Use local /etc/ethers and /etc/hosts
   to avoid NIS and DNS queries.
 */
int resolve(struct ether_addr *addr, struct in_addr *in_addr)
{
	char hostname[4096];
	memset(hostname, 0, sizeof(hostname));
	/*
	   This is not really safe, at the time of writing glibc uses
	   a 1024 byte buffer for the answer internally ...  We could
	   parse /etc/ethers by ourselves but then we would loose
	   nsswitch support. Alas RARP support without NIS is no fun :-(
	*/
	int ret = ether_ntohost(hostname, addr);
	if (ret != 0) {
		XLOG_DEBUG("failed to lookup %s", ether_ntoa(addr));
		return -1;
	}

	XLOG_DEBUG("lookup for %s returned %s", ether_ntoa(addr), hostname);

	struct addrinfo hints;
	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_INET;
	hints.ai_flags = AI_ADDRCONFIG;

	struct addrinfo *ai = NULL;
	ret = getaddrinfo(hostname, NULL, &hints, &ai);
	if (ret != 0) {
		XLOG_DEBUG("could not resolve '%s': %s\n", hostname,
			ret == EAI_SYSTEM ? strerror(errno) : gai_strerror(ret));
		return -1;
	}

	struct sockaddr_in *sa = (struct sockaddr_in *)(ai->ai_addr);
	memcpy(in_addr, &(sa->sin_addr), sizeof(struct in_addr));

	return 0;
}
Example #6
0
/*
 * Answer the RARP request in 'pkt', on the interface 'ii'.  'pkt' has
 * already been checked for validity.  The reply is overlaid on the request.
 */
void
rarp_process(struct if_info *ii, u_char *pkt)
{
	char    ename[HOST_NAME_MAX+1];
	u_int32_t  target_ipaddr;
	struct ether_header *ep;
	struct ether_addr *ea;
	struct hostent *hp;
	struct	in_addr in;
	struct if_addr *ia;

	ep = (struct ether_header *) pkt;
	ea = (struct ether_addr *) &ep->ether_shost;

	debug("%s", ether_ntoa(ea));
	if (ether_ntohost(ename, ea) != 0) {
		debug("ether_ntohost failed");
		return;
	}
	if ((hp = gethostbyname(ename)) == 0) {
		debug("gethostbyname (%s) failed", ename);
		return;
	}

	/* Choose correct address from list. */
	if (hp->h_addrtype != AF_INET) {
		error(FATAL, "cannot handle non IP addresses");
		/* NOTREACHED */
	}
	for (target_ipaddr = 0, ia = ii->ii_addrs; ia; ia = ia->ia_next) {
		target_ipaddr = choose_ipaddr((u_int32_t **) hp->h_addr_list,
		    ia->ia_ipaddr & ia->ia_netmask, ia->ia_netmask);
		if (target_ipaddr)
			break;
	}

	if (target_ipaddr == 0) {
		for (ia = ii->ii_addrs; ia; ia = ia->ia_next) {
			in.s_addr = ia->ia_ipaddr & ia->ia_netmask;
			error(NONFATAL, "cannot find %s on net %s",
			    ename, inet_ntoa(in));
		}
		return;
	}
	if (tflag == 0 || rarp_bootable(htonl(target_ipaddr)))
		rarp_reply(ii, ia, ep, target_ipaddr, hp);
	debug("reply sent");
}
static int net_clone_mac_detect(struct mbuf * m, struct ether_header *eh)
{
	char *p;
    if (net_clone_stat==1)
    {
        if (net_clone_if!=m->m_pkthdr.rcvif ) return 0;
		//if (eh->ether_type != htons(ETHERTYPE_ARP)) return 0;
		p=(char *)&eh->ether_shost[0];
        memcpy(net_clone_mac_addr, p, 6);
        diag_printf("CLN MAC: %s\n" , ether_ntoa(p));
        net_clone_stat++;
        mon_snd_cmd(13);
    }

	return 0;
}
Example #8
0
int addrouteInfo(routeInfo *rtinfo)
{
	int node_found=0;
	if(!rtinfo)
	{
		return 0;
	}
	else
	{
		routeNode *newnode = (routeNode *)malloc(sizeof(routeNode));
		memset(newnode,0,sizeof(routeNode));
		memcpy(&newnode->rtinfo,rtinfo,sizeof(routeInfo));
		newnode->next = NULL;
		if(routeList==NULL)
			routeList = newnode;
		else
		{
			routeNode *tmp = routeList; //tmp=1234, routelist=1234
			while(tmp!=NULL)
			{
				if(strcmp(tmp->rtinfo.dest_ipaddr,rtinfo->dest_ipaddr)==0)
				{
					node_found=1;
					break;
				}
				tmp=tmp->next;
			}
			if(node_found)
			{
				struct ether_addr addr;
				memset(&addr,0,sizeof(addr));
				memcpy(&tmp->rtinfo.dest_mac,&rtinfo->dest_mac,sizeof(hwaddr));
	       		memcpy(addr.ether_addr_octet,tmp->rtinfo.dest_mac.sll_addr, IF_HADDR);
				printf("ARP : ARP CACHE UPDATED FOR IP : %s -- %s\n",tmp->rtinfo.dest_ipaddr,ether_ntoa(&addr));
				//printf("ARP : TOUR SOCKFD FOR IP %S : %d\n",tmp->rtinfo.dest_ipaddr,tmp->rtinfo.tour_sockfd);
			}
			else 
			{
				tmp=routeList;
				newnode->next = tmp;
				routeList = newnode;
			}

		}
	}
	return 1;
}
Example #9
0
/*
 * Map an ethernet address to a hostname. Use either /etc/ethers or NIS/YP.
 */
int
ether_ntohost(char *hostname, const struct ether_addr *e)
{
	FILE *fp;
	char buf[BUFSIZ + 2];
	struct ether_addr local_ether;
	char local_host[MAXHOSTNAMELEN];
#ifdef YP
	char *result;
	int resultlen;
	char *ether_a;
	char *yp_domain;
#endif

	if ((fp = fopen(_PATH_ETHERS, "r")) == NULL)
		return (1);
	while (fgets(buf,BUFSIZ,fp)) {
		if (buf[0] == '#')
			continue;
#ifdef YP
		if (buf[0] == '+') {
			if (yp_get_default_domain(&yp_domain))
				continue;
			ether_a = ether_ntoa(e);
			if (yp_match(yp_domain, "ethers.byaddr", ether_a,
			    strlen(ether_a), &result, &resultlen)) {
				continue;
			}
			strncpy(buf, result, resultlen);
			buf[resultlen] = '\0';
			free(result);
		}
#endif
		if (!ether_line(buf, &local_ether, local_host)) {
			if (!bcmp((char *)&local_ether.octet[0],
			    (char *)&e->octet[0], 6)) {
				/* We have a match. */
				strcpy(hostname, local_host);
				fclose(fp);
				return(0);
			}
		}
	}
	fclose(fp);
	return (1);
}
Example #10
0
int
bridge_addrs(int s, char *brdg, char *hdr_delim, char *body_delim)
{
	struct ifbaconf ifbac;
	struct ifbareq *ifba;
	char buf[sizeof(ifba->ifba_ifsname) + 1], *inbuf = NULL, *inb;
	u_int i, len = 8192;

	while (1) {
		ifbac.ifbac_len = len;
		inb = realloc(inbuf, len);
		if (inb == NULL) {
			free(inbuf);
			printf("%% bridge_addrs: malloc: %s\n",
			    strerror(errno));
			return(0);
		}
		ifbac.ifbac_buf = inbuf = inb;
		strlcpy(ifbac.ifbac_name, brdg, sizeof(ifbac.ifbac_name));
		if (ioctl(s, SIOCBRDGRTS, &ifbac) < 0) {
			if (errno != ENETDOWN)
				printf("%% bridge_addrs: SIOCBRDGRTS: %s\n",
				    strerror(errno));
			free(inbuf);
			return(0);
		}
		if (ifbac.ifbac_len + sizeof(*ifba) < len)
			break;
		len *= 2;
	}
	if (ifbac.ifbac_len / sizeof(*ifba)) {
		printf("%sLearned addresses:\n", hdr_delim);
		printf("%saddress           member age\n", body_delim);
	}

	for (i = 0; i < ifbac.ifbac_len / sizeof(*ifba); i++) {
		ifba = ifbac.ifbac_req + i;
		strlcpy(buf, ifba->ifba_ifsname, sizeof(buf));
		printf("%s%s %-6s %u ", body_delim, ether_ntoa(&ifba->ifba_dst),
		    buf, ifba->ifba_age);
		bprintf(stdout, ifba->ifba_flags, IFBAFBITS);
		printf("\n");
	}
	free(inbuf);
	return (0);
}
Example #11
0
static int net_clone_mac_detect(struct mbuf * m)
{
	struct ether_arp *ea;
	ea = mtod(m, struct ether_arp *);
	char *p=(char *)&ea->arp_sha[0];

    if (net_clone_stat==1)
    {
        if (net_clone_if==m->m_pkthdr.rcvif)
        {
            memcpy(net_clone_mac_addr,p, 6);
            diag_printf("target if=%x EA: %s\n" , m->m_pkthdr.rcvif, ether_ntoa(p));
            net_clone_stat++;
            mon_snd_cmd(13);
        }
    }
}
Example #12
0
/*
 * Build a reverse ARP packet and sent it out on the interface.
 * 'ep' points to a valid ARPOP_REVREQUEST.  The ARPOP_REVREPLY is built
 * on top of the request, then written to the network.
 *
 * RFC 903 defines the ether_arp fields as follows.  The following comments
 * are taken (more or less) straight from this document.
 *
 * ARPOP_REVREQUEST
 *
 * arp_sha is the hardware address of the sender of the packet.
 * arp_spa is undefined.
 * arp_tha is the 'target' hardware address.
 *   In the case where the sender wishes to determine his own
 *   protocol address, this, like arp_sha, will be the hardware
 *   address of the sender.
 * arp_tpa is undefined.
 *
 * ARPOP_REVREPLY
 *
 * arp_sha is the hardware address of the responder (the sender of the
 *   reply packet).
 * arp_spa is the protocol address of the responder (see the note below).
 * arp_tha is the hardware address of the target, and should be the same as
 *   that which was given in the request.
 * arp_tpa is the protocol address of the target, that is, the desired address.
 *
 * Note that the requirement that arp_spa be filled in with the responder's
 * protocol is purely for convenience.  For instance, if a system were to use
 * both ARP and RARP, then the inclusion of the valid protocol-hardware
 * address pair (arp_spa, arp_sha) may eliminate the need for a subsequent
 * ARP request.
 */
void
rarp_reply(struct if_info *ii, struct if_addr *ia, struct ether_header *ep,
    u_int32_t ipaddr, struct hostent *hp)
{
	struct ether_arp *ap = (struct ether_arp *) (ep + 1);
	int len, n;

	/*
	 * Poke the kernel arp tables with the ethernet/ip address
	 * combination given.  When processing a reply, we must
	 * do this so that the booting host (i.e. the guy running
	 * rarpd), won't try to ARP for the hardware address of the
	 * guy being booted (he cannot answer the ARP).
	 */
	if (arptab_set((u_char *)&ap->arp_sha, ipaddr) > 0)
		syslog(LOG_ERR, "couldn't update arp table");

	/* Build the rarp reply by modifying the rarp request in place. */
	ep->ether_type = htons(ETHERTYPE_REVARP);
	ap->ea_hdr.ar_hrd = htons(ARPHRD_ETHER);
	ap->ea_hdr.ar_pro = htons(ETHERTYPE_IP);
	ap->arp_op = htons(ARPOP_REVREPLY);

	memcpy((char *) &ep->ether_dhost, (char *) &ap->arp_sha, 6);
	memcpy((char *) &ep->ether_shost, (char *) ii->ii_eaddr, 6);
	memcpy((char *) &ap->arp_sha, (char *) ii->ii_eaddr, 6);

	memcpy((char *) ap->arp_tpa, (char *) &ipaddr, 4);
	/* Target hardware is unchanged. */
	memcpy((char *) ap->arp_spa, (char *) &ia->ia_ipaddr, 4);

	if (lflag) {
		struct ether_addr ea;

		memcpy(&ea.ether_addr_octet, &ap->arp_sha, 6);
		syslog(LOG_INFO, "%s asked; %s replied", hp->h_name,
		    ether_ntoa(&ea));
	}

	len = sizeof(*ep) + sizeof(*ap);
	n = write(ii->ii_fd, (char *) ep, len);
	if (n != len)
		error(NONFATAL, "write: only %d of %d bytes written", n, len);
}
Example #13
0
char *
get_hwdaddr(char *ifname)
{
	int i, found = 0;
	char *val = NULL;
	struct ifaddrs *ifap, *ifa;
	struct ether_addr *ea;
	struct sockaddr_dl *sdl = NULL;

	if (getifaddrs(&ifap) != 0) {
		printf("%% get_hwdaddr: getifaddrs: %s\n", strerror(errno));
		return(NULL);
	}

	for (ifa = ifap; ifa; ifa = ifa->ifa_next)
		if (ifa->ifa_addr->sa_family == AF_LINK &&
		    (strcmp(ifname, ifa->ifa_name) == 0)) {
			sdl = (struct sockaddr_dl *)ifa->ifa_addr;
			found++;
			break;
		}

	if (found && sdl && sdl->sdl_alen)
		switch(sdl->sdl_type) {
		case IFT_ETHER:
		case IFT_IEEE80211:
			ea = (struct ether_addr *)LLADDR(sdl);
			val = ether_ntoa(ea);
			for (found = 0, i = 0; i < ETHER_ADDR_LEN; i++)
				if (ea->ether_addr_octet[i] == 0)
					found++;
			if (found == ETHER_ADDR_LEN)
				val = NULL;
			break;
		default:
			val = NULL;
			break;
		}

	freeifaddrs(ifap);

	return(val);
}
Example #14
0
/*Look if the source ip address is a broadcast addr*/
int watch_ip_broadcast(char* buffer, uint16_t vlan_id, struct ether_header* eptr, struct ip6_hdr* ipptr)
{
	struct ether_addr* eth_addr = (struct ether_addr*) eptr->ether_shost;
	struct in6_addr* ip_addr = &ipptr->ip6_src;
	char str_ip[IP6_STR_SIZE];

	ipv6_ntoa(str_ip, *ip_addr);

	if (IN6_IS_ADDR_MULTICAST(ip_addr))
	{

		snprintf (buffer, NOTIFY_BUFFER_SIZE, "VLAN%d: ip multicast %s %s",vlan_id,ether_ntoa(eth_addr),str_ip);
		notify(1, buffer, "ip multicast", eth_addr, str_ip, NULL);
		return 1;

	}
	else
		return 0;
}
Example #15
0
static void
mpe_status(int s)
{
	struct if_laddrreq iflr;
	struct sockaddr_mpls *smpls;
	struct sockaddr_dl *sdl;
	
	(void)memset(&iflr, 0, sizeof(iflr));
	(void)memcpy(iflr.iflr_name, name, IFNAMSIZ);	
	
	smpls = (struct sockaddr_mpls *)&iflr.addr;
	smpls->smpls_len = sizeof(*smpls);
	smpls->smpls_family = AF_MPLS;
	
	sdl = (struct sockaddr_dl *)&iflr.dstaddr;
	sdl->sdl_len = sizeof(*sdl);
	sdl->sdl_family = AF_LINK;	
	
	if (ioctl(s, SIOCGLIFPHYADDR, (caddr_t)&iflr) < 0)  
		return;
/*
 * Following code-section is reused from implementation 
 * of in af_link.c defined link_status procedure, part of
 * ifconfig(8) implementation. 
 */		
	(void)printf("\tlink ");
		
	if (sdl != NULL && sdl->sdl_alen > 0) {
		if ((sdl->sdl_type == IFT_ETHER 
			|| sdl->sdl_type == IFT_L2VLAN 
			|| sdl->sdl_type == IFT_BRIDGE) 
			&& sdl->sdl_alen == ETHER_ADDR_LEN) {
			
			(void)printf("%s ",
				ether_ntoa((struct ether_addr *)LLADDR(sdl)));
		} else {
			int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;
			
			(void)printf("%s ", link_ntoa(sdl) + n);
		}
	}	
}
Example #16
0
static void
link_status(int s __unused, const struct ifaddrs *ifa)
{
	/* XXX no const 'cuz LLADDR is defined wrong */
	struct sockaddr_dl *sdl = (struct sockaddr_dl *) ifa->ifa_addr;

	if (sdl != NULL && sdl->sdl_alen > 0) {
		if ((sdl->sdl_type == IFT_ETHER ||
		    sdl->sdl_type == IFT_L2VLAN ||
		    sdl->sdl_type == IFT_BRIDGE) &&
		    sdl->sdl_alen == ETHER_ADDR_LEN)
			printf("\tether %s\n",
			    ether_ntoa((struct ether_addr *)LLADDR(sdl)));
		else {
			int n = sdl->sdl_nlen > 0 ? sdl->sdl_nlen + 1 : 0;

			printf("\tlladdr %s\n", link_ntoa(sdl) + n);
		}
	}
}
Example #17
0
static void
extract_mac_range(struct mac_range *r)
{
	D("extract MAC range from %s", r->name);
	bcopy(ether_aton(r->name), &r->start, 6);
	bcopy(ether_aton(r->name), &r->end, 6);
#if 0
	bcopy(targ->src_mac, eh->ether_shost, 6);
	p = index(targ->g->src_mac, '-');
	if (p)
		targ->src_mac_range = atoi(p+1);

	bcopy(ether_aton(targ->g->dst_mac), targ->dst_mac, 6);
	bcopy(targ->dst_mac, eh->ether_dhost, 6);
	p = index(targ->g->dst_mac, '-');
	if (p)
		targ->dst_mac_range = atoi(p+1);
#endif
	D("%s starts at %s", r->name, ether_ntoa(&r->start));
}
Example #18
0
int
bridge_confaddrs(int s, char *brdg, char *delim, FILE *output)
{
	struct ifbaconf ifbac;
	struct ifbareq *ifba;
	char buf[sizeof(ifba->ifba_ifsname) + 1], *inbuf = NULL, *inb;
	u_int i, len = 8192;

	while (1) {
		ifbac.ifbac_len = len;
		inb = realloc(inbuf, len);
		if (inb == NULL) {
			free(inbuf);
			printf("%% bridge_confaddrs: malloc: %s\n",
				strerror(errno));
			return(0);
		}
		ifbac.ifbac_buf = inbuf = inb;
		strlcpy(ifbac.ifbac_name, brdg, sizeof(ifbac.ifbac_name));
		if (ioctl(s, SIOCBRDGRTS, &ifbac) < 0) {
			if (errno != ENETDOWN)
				printf("%% bridge_confaddrs: SIOCBRDGRTS: %s\n",
				    strerror(errno));
			free(inbuf);
			return(0);
		}
		if (ifbac.ifbac_len + sizeof(*ifba) < len)
			break;
		len *= 2;
	}

	for (i = 0; i < ifbac.ifbac_len / sizeof(*ifba); i++) {
		ifba = ifbac.ifbac_req + i;
		strlcpy(buf, ifba->ifba_ifsname, sizeof(buf));
		if (ifba->ifba_flags & IFBAF_STATIC)
			fprintf(output, "%s%s %s\n", delim,
			    ether_ntoa(&ifba->ifba_dst), buf);
	}
	free(inbuf);
	return (0);
}
Example #19
0
char* SockAddr_toString(SockAddr self) {
    static char buf[512];
    switch (self->addr->sa_family) {
        case AF_INET6: {
            struct sockaddr_in6* sin6 = (struct sockaddr_in6*)self->addr;
            char ntopbuf[INET6_ADDRSTRLEN];
            inet_ntop(AF_INET6, &(sin6->sin6_addr), ntopbuf, INET6_ADDRSTRLEN);
            sprintf(buf, "SockAddr IPv6 %s port=%d", ntopbuf, be16toh(sin6->sin6_port));
        } break;
#ifdef __linux__
        case AF_PACKET: {
            struct sockaddr_ll* sll = (struct sockaddr_ll*)self->addr;
            sprintf(buf, "SockAddr Ethernet %s", ether_ntoa((struct ether_addr*)sll->sll_addr));
        } break;
#endif
        default:
        sprintf(buf, "SockAddr family=%x", self->addr->sa_family);
        break;
    }
    return buf;
}
Example #20
0
/* Obsluga nadchodzacych odpowiedzi */
void
io_handle (int a)
{
  char buf[1024];
  struct in_addr ipadd;
  struct ethhdr *ethdp;
  struct arphdr *arphdp;

  recv (sd, buf, sizeof (buf), 0);

  ethdp = (struct ethhdr *) buf;
  arphdp = (struct arphdr *) (buf + sizeof (struct ethhdr));

  if (ntohs (arphdp->ar_op) == ARPOP_REPLY)
    {
      memcpy ((char *) &ipadd, (char *) arphdp + sizeof (struct arphdr) +
	      ETH_ALEN, sizeof (ipadd));
      printf ("%s => %s\n", inet_ntoa (ipadd),
	      ether_ntoa ((struct ether_addr *) &ethdp->h_source));
    }
}
Example #21
0
u_int16_t handle_ethernet
(u_char *args,const struct pcap_pkthdr* pkthdr,const u_char*
 packet)
{

    u_int caplen = pkthdr->caplen;
    struct ether_header *eptr;
    u_int16_t ether_type;

    if (caplen < ETHER_HDRLEN)
    {
        fprintf(stdout,"Packet length less than ethernet header length\n");
        return -1;
    }

    eptr = (struct ether_header *) packet;
    ether_type = ntohs(eptr->ether_type);
    char* source_mac = ether_ntoa((struct ether_addr*)eptr->ether_shost);
    process_MAC(mac_array,source_mac);
    return ether_type;
}
Example #22
0
void
bridge_addrs(const char *delim, int d)
{
	struct ifbaconf ifbac;
	struct ifbareq *ifba;
	char *inbuf = NULL, buf[sizeof(ifba->ifba_ifsname) + 1], *inb;
	int i, len = 8192;

	/* ifconfig will call us with the argv of the command */
	if (strcmp(delim, "addr") == 0)
		delim = "";

	while (1) {
		ifbac.ifbac_len = len;
		inb = realloc(inbuf, len);
		if (inb == NULL)
			err(1, "malloc");
		ifbac.ifbac_buf = inbuf = inb;
		strlcpy(ifbac.ifbac_name, name, sizeof(ifbac.ifbac_name));
		if (ioctl(s, SIOCBRDGRTS, &ifbac) < 0) {
			if (errno == ENETDOWN)
				return;
			err(1, "%s", name);
		}
		if (ifbac.ifbac_len + sizeof(*ifba) < len)
			break;
		len *= 2;
	}

	for (i = 0; i < ifbac.ifbac_len / sizeof(*ifba); i++) {
		ifba = ifbac.ifbac_req + i;
		strlcpy(buf, ifba->ifba_ifsname, sizeof(buf));
		printf("%s%s %s %u ", delim, ether_ntoa(&ifba->ifba_dst),
		    buf, ifba->ifba_age);
		printb("flags", ifba->ifba_flags, IFBAFBITS);
		printf("\n");
	}
	free(inbuf);
}
Example #23
0
static int wlc_maclist(wlc_param param, void *data, void *value)
{
	unsigned int *var = ((unsigned int *) data);
	unsigned int ioc = *var;
	int limit = (sizeof(wlbuf) - 4) / sizeof(struct ether_addr);
	struct maclist *list = (struct maclist *) wlbuf;
	char *str = (char *) value;
	char astr[30], *p;
	struct ether_addr *addr;
	int isset = 0;
	int ret;

	if ((param & PARAM_MODE) == GET) {
		list->count = limit;
		ret = wl_ioctl(interface, (ioc >> 16) & 0xffff, wlbuf, sizeof(wlbuf));
		
		if (!ret) 
			while (list->count)
				str += sprintf(str, "%s%s", ((((char *) value) == str) ? "" : " "), ether_ntoa(&list->ea[list->count-- - 1]));
		
		return ret;
	} else {
Example #24
0
static int
wpa_driver_bsd_del_key(struct wpa_driver_bsd_data *drv, int key_idx,
		       const unsigned char *addr)
{
	struct ieee80211req_del_key wk;

	memset(&wk, 0, sizeof(wk));
	if (addr != NULL &&
	    bcmp(addr, "\xff\xff\xff\xff\xff\xff", IEEE80211_ADDR_LEN) != 0) {
		struct ether_addr ea;

		memcpy(&ea, addr, IEEE80211_ADDR_LEN);
		wpa_printf(MSG_DEBUG, "%s: addr=%s keyidx=%d",
			__func__, ether_ntoa(&ea), key_idx);
		memcpy(wk.idk_macaddr, addr, IEEE80211_ADDR_LEN);
		wk.idk_keyix = (uint8_t) IEEE80211_KEYIX_NONE;
	} else {
		wpa_printf(MSG_DEBUG, "%s: keyidx=%d", __func__, key_idx);
		wk.idk_keyix = key_idx;
	}
	return set80211var(drv, IEEE80211_IOC_DELKEY, &wk, sizeof(wk));
}
Example #25
0
int
main(int argc, char **argv)
{
	/*
	 * The hostname buffer must be at least as large as the line buffer
	 * to avoid buffer overflows when ether_line(3SOCKET) is called.
	 * We simply use the same size for both buffers to be safe.
	 */
	char line[MAXHOSTNAMELEN + 256], *lf, hostname[sizeof (line)];
	struct ether_addr e;
	FILE *in;

	if (argc > 1) {
		in = fopen(argv[1], "r");
		if (in == NULL) {
			fprintf(stderr,
			    "%s: can't open %s\n", argv[0], argv[1]);
			return (1);
		}
	} else {
		in = stdin;
	}
	while (fgets(line, sizeof (line), in) != NULL) {
		lf = strchr(line, '\n');
		if (lf != NULL)
			*lf = '\0';
		if ((line[0] == '#') || (line[0] == '\0'))
			continue;
		if (ether_line(line, &e, hostname) == 0) {
			(void) fprintf(stdout, "%s\t%s\n", ether_ntoa(&e),
			    hostname);
		} else {
			(void) fprintf(stderr,
			    "%s: ignoring line: %s\n", argv[0], line);
		}
	}
	return (0);
}
Example #26
0
void
table_print(struct ipfw_ioc_table * tbl)
{
	int i;
        if (tbl->type == 0)
                errx(EX_USAGE, "table %d is not in use", tbl->id);

        printf("table %d", tbl->id);
        if (tbl->type == 1)
                printf(" type ip");
        else if (tbl->type == 2)
                printf(" type mac");

        printf(" count %d", tbl->count);
	if (strlen(tbl->name) > 0)
		printf(" name %s", tbl->name);

	printf("\n");

        if (tbl->type == 1) {
                struct ipfw_ioc_table_ip_entry *ip_ent;
                ip_ent = tbl->ip_ent;
                for (i = 0; i < tbl->count; i++) {
                        printf("%s", inet_ntoa(*(struct in_addr *)&ip_ent->addr));
                        printf("/%d ", ip_ent->masklen);
                        printf("\n");
                        ip_ent++;
                }
        } else if (tbl->type == 2) {
                struct ipfw_ioc_table_mac_entry *mac_ent;
                mac_ent = tbl->mac_ent;
                for (i = 0; i < tbl->count; i++) {
                        printf("%s", ether_ntoa(&mac_ent->addr));
                        printf("\n");
                        mac_ent++;
                }
        }
}
Example #27
0
static void rarp_reply(ethernet_t *ep, char *hostname, ipaddr_t ip_addr,
						ether_addr_t eth_addr)
{
    rarp46_t rarp46;

    /* Construct a RARP reply packet and send it. */
    rarp46.a46_dstaddr= eth_addr;
    rarp46.a46_hdr= HTONS(RARP_ETHERNET);
    rarp46.a46_pro= HTONS(ETH_IP_PROTO);
    rarp46.a46_hln= 6;
    rarp46.a46_pln= 4;
    rarp46.a46_op= HTONS(RARP_REPLY);
    rarp46.a46_sha= ep->eth_addr;
    memcpy(rarp46.a46_spa, &ep->ip_addr, sizeof(ipaddr_t));
    rarp46.a46_tha= eth_addr;
    memcpy(rarp46.a46_tpa, &ip_addr, sizeof(ipaddr_t));

    if (debug >= 1) {
	printf("%s: Replying %s (%s) to %s\n",
	    ethdev(ep->n), inet_ntoa(ip_addr), hostname, ether_ntoa(&eth_addr));
    }
    (void) write(ep->eth_fd, &rarp46, sizeof(rarp46));
}
Example #28
0
const char *
log_sockaddr(struct sockaddr *sa)
{
	static char	buf[NI_MAXHOST];

	if (sa == NULL)
		return "(none)";

	if (sa->sa_family == AF_UNIX) {
		if (strlen(((struct sockaddr_un *)sa)->sun_path))
			return ((struct sockaddr_un *)sa)->sun_path;
		else
			return "(local user)";

	} else if (sa->sa_family == AF_LINK) {
		return ether_ntoa((struct ether_addr *)LLADDR((struct sockaddr_dl *)sa));
	}

	if (getnameinfo(sa, sa->sa_len, buf, sizeof(buf), NULL, 0,
	    NI_NUMERICHOST))
		return "(unknown)";
	else
		return buf;
}
Example #29
0
void
tap_init(void)
{
	channel_t *chan;
	struct ifreq ifr;
	int fd, s;
	char pidfile[PATH_MAX];

	fd = open(interface_name, O_RDWR);
	if (fd == -1) {
		log_err("Could not open \"%s\": %m", interface_name);
		exit(EXIT_FAILURE);
	}

	memset(&ifr, 0, sizeof(ifr));
	if (ioctl(fd, TAPGIFNAME, &ifr) == -1) {
		log_err("Could not get interface name: %m");
		exit(EXIT_FAILURE);
	}

	s = socket(AF_INET, SOCK_DGRAM, 0);
	if (s == -1) {
		log_err("Could not open PF_LINK socket: %m");
		exit(EXIT_FAILURE);
	}

	ifr.ifr_addr.sa_family = AF_LINK;
	ifr.ifr_addr.sa_len = ETHER_ADDR_LEN;
	b2eaddr(ifr.ifr_addr.sa_data, &local_bdaddr);

	if (ioctl(s, SIOCSIFLLADDR, &ifr) == -1) {
		log_err("Could not set %s physical address: %m", ifr.ifr_name);
		exit(EXIT_FAILURE);
	}

	if (ioctl(s, SIOCGIFFLAGS, &ifr) == -1) {
		log_err("Could not get interface flags: %m");
		exit(EXIT_FAILURE);
	}

	if ((ifr.ifr_flags & IFF_UP) == 0) {
		ifr.ifr_flags |= IFF_UP;

		if (ioctl(s, SIOCSIFFLAGS, &ifr) == -1) {
			log_err("Could not set IFF_UP: %m");
			exit(EXIT_FAILURE);
		}
	}

	close(s);

	log_info("Using interface %s with addr %s", ifr.ifr_name,
		ether_ntoa((struct ether_addr *)&ifr.ifr_addr.sa_data));

	chan = channel_alloc();
	if (chan == NULL)
		exit(EXIT_FAILURE);

	chan->send = tap_send;
	chan->recv = tap_recv;
	chan->mru = ETHER_HDR_LEN + ETHER_MAX_LEN;
	memcpy(chan->raddr, ifr.ifr_addr.sa_data, ETHER_ADDR_LEN);
	memcpy(chan->laddr, ifr.ifr_addr.sa_data, ETHER_ADDR_LEN);
	chan->state = CHANNEL_OPEN;
	if (!channel_open(chan, fd))
		exit(EXIT_FAILURE);

	snprintf(pidfile, sizeof(pidfile), "%s/%s.pid",
		_PATH_VARRUN, ifr.ifr_name);
	chan->pfh = pidfile_open(pidfile, 0600, NULL);
	if (chan->pfh == NULL)
		log_err("can't create pidfile");
	else if (pidfile_write(chan->pfh) < 0) {
		log_err("can't write pidfile");
		pidfile_remove(chan->pfh);
		chan->pfh = NULL;
	}
}
Example #30
0
static bool recv_pack(unsigned char *buf, int len, struct sockaddr_ll *FROM)
{
	struct arphdr *ah = (struct arphdr *) buf;
	unsigned char *p = (unsigned char *) (ah + 1);
	struct in_addr src_ip, dst_ip;
	/* moves below assume in_addr is 4 bytes big, ensure that */
	struct BUG_in_addr_must_be_4 {
		char BUG_in_addr_must_be_4[
			sizeof(struct in_addr) == 4 ? 1 : -1
		];
		char BUG_s_addr_must_be_4[
			sizeof(src_ip.s_addr) == 4 ? 1 : -1
		];
	};

	/* Filter out wild packets */
	if (FROM->sll_pkttype != PACKET_HOST
	 && FROM->sll_pkttype != PACKET_BROADCAST
	 && FROM->sll_pkttype != PACKET_MULTICAST)
		return false;

	/* Only these types are recognised */
	if (ah->ar_op != htons(ARPOP_REQUEST) && ah->ar_op != htons(ARPOP_REPLY))
		return false;

	/* ARPHRD check and this darned FDDI hack here :-( */
	if (ah->ar_hrd != htons(FROM->sll_hatype)
	 && (FROM->sll_hatype != ARPHRD_FDDI || ah->ar_hrd != htons(ARPHRD_ETHER)))
		return false;

	/* Protocol must be IP. */
	if (ah->ar_pro != htons(ETH_P_IP)
	 || (ah->ar_pln != 4)
	 || (ah->ar_hln != me.sll_halen)
	 || (len < (int)(sizeof(*ah) + 2 * (4 + ah->ar_hln))))
		return false;

	move_from_unaligned32(src_ip.s_addr, p + ah->ar_hln);
	move_from_unaligned32(dst_ip.s_addr, p + ah->ar_hln + 4 + ah->ar_hln);

	if (dst.s_addr != src_ip.s_addr)
		return false;
	if (!(option_mask32 & DAD)) {
		if ((src.s_addr != dst_ip.s_addr)
			|| (memcmp(p + ah->ar_hln + 4, &me.sll_addr, ah->ar_hln)))
			return false;
	} else {
		/* DAD packet was:
		   src_ip = 0 (or some src)
		   src_hw = ME
		   dst_ip = tested address
		   dst_hw = <unspec>

		   We fail, if receive request/reply with:
		   src_ip = tested_address
		   src_hw != ME
		   if src_ip in request was not zero, check
		   also that it matches to dst_ip, otherwise
		   dst_ip/dst_hw do not matter.
		 */
		if ((memcmp(p, &me.sll_addr, me.sll_halen) == 0)
		 || (src.s_addr && src.s_addr != dst_ip.s_addr))
			return false;
	}
	if (!(option_mask32 & QUIET)) {
		int s_printed = 0;

		printf("%scast re%s from %s [%s]",
			FROM->sll_pkttype == PACKET_HOST ? "Uni" : "Broad",
			ah->ar_op == htons(ARPOP_REPLY) ? "ply" : "quest",
			inet_ntoa(src_ip),
			ether_ntoa((struct ether_addr *) p));
		if (dst_ip.s_addr != src.s_addr) {
			printf("for %s ", inet_ntoa(dst_ip));
			s_printed = 1;
		}
		if (memcmp(p + ah->ar_hln + 4, me.sll_addr, ah->ar_hln)) {
			if (!s_printed)
				printf("for ");
			printf("[%s]",
				ether_ntoa((struct ether_addr *) p + ah->ar_hln + 4));
		}

		if (last) {
			unsigned diff = MONOTONIC_US() - last;
			printf(" %u.%03ums\n", diff / 1000, diff % 1000);
		} else {
			printf(" UNSOLICITED?\n");
		}
		fflush(stdout);
	}
	received++;
	if (FROM->sll_pkttype != PACKET_HOST)
		brd_recv++;
	if (ah->ar_op == htons(ARPOP_REQUEST))
		req_recv++;
	if (option_mask32 & QUIT_ON_REPLY)
		finish();
	if (!(option_mask32 & BCAST_ONLY)) {
		memcpy(he.sll_addr, p, me.sll_halen);
		option_mask32 |= UNICASTING;
	}
	return true;
}