Esempio n. 1
0
void send_gratuitous_arp(const int ifIndex, const char *devname, const char *ipaddress, const char *src_mac_address, const char *dst_mac_address){
  int rawfd;
  struct ether_arp arpbody;
  struct sockaddr_ll ll_from, ll_to;
  struct ether_addr src_mac_addr;
  struct ether_addr dst_mac_addr;

  ether_aton_r(src_mac_address, &src_mac_addr);
  ether_aton_r(dst_mac_address, &dst_mac_addr);

  arpbody.arp_hrd = htons(ARPHRD_ETHER);
  arpbody.arp_pro = htons(ETH_P_IP);
  arpbody.arp_hln = 6;
  arpbody.arp_pln = 4;
  arpbody.arp_op  = htons(ARPOP_REQUEST);
  memcpy(&(arpbody.arp_sha[0]), src_mac_addr.ether_addr_octet, ETH_ALEN); //arpbody.arp_sha
  memcpy(&(arpbody.arp_tha[0]), dst_mac_addr.ether_addr_octet, ETH_ALEN); //arpbody.arp_tha
  *((int *)&(arpbody.arp_spa[0])) = inet_addr(ipaddress); //arpbody.arp_spa
  *((int *)&(arpbody.arp_tpa[0])) = inet_addr(ipaddress); //arpbody.arp_tpa

  
  ll_from.sll_family = AF_PACKET;
  ll_from.sll_protocol = htons(ETH_P_ARP);
  ll_from.sll_ifindex = ifIndex;
  ll_from.sll_hatype = 0; //ARPHRD_ETHER;
  ll_from.sll_pkttype = 0; //PACKET_BROADCAST;
  ll_from.sll_halen = ETH_ALEN;
  //ll_from.sll_addr = NULL;
  memcpy(ll_from.sll_addr, src_mac_addr.ether_addr_octet, sizeof(src_mac_addr.ether_addr_octet));
  
  ll_to.sll_family = AF_PACKET;
  ll_to.sll_protocol = htons(ETH_P_ARP);
  ll_to.sll_ifindex = ifIndex;
  ll_to.sll_hatype = 0; //ARPHRD_ETHER;
  ll_to.sll_pkttype = 0; //PACKET_BROADCAST;
  ll_to.sll_halen = ETH_ALEN;
  //ll_to.sll_addr = NULL;
  memcpy(ll_to.sll_addr, dst_mac_addr.ether_addr_octet, sizeof(dst_mac_addr.ether_addr_octet));
  
  if ((rawfd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ARP))) < 0) {
	perror("raw socket creatin");
	exit(1);
  } 
  
  if (bind(rawfd, (struct sockaddr *)&ll_from, sizeof(ll_from)) < 0) {
	perror("bind");
	exit(1);
  }
  
  int size=0;
  if ( (size = sendto(rawfd, (void *)&arpbody, sizeof(struct ether_arp), 0, (struct sockaddr *)&ll_to, sizeof(ll_to))) <= 0) {
	perror("write");
	exit(1);
  }
}
Esempio n. 2
0
static int FAST_FUNC read_staticlease(const char *const_line, void *arg)
{
	char *line;
	char *mac_string;
	char *ip_string;
	struct ether_addr mac_bytes; /* it's "struct { uint8_t mac[6]; }" */
	uint32_t nip;

	/* Read mac */
	line = (char *) const_line;
	mac_string = strtok_r(line, " \t", &line);
	if (!mac_string || !ether_aton_r(mac_string, &mac_bytes))
		return 0;

	/* Read ip */
	ip_string = strtok_r(NULL, " \t", &line);
	if (!ip_string || !udhcp_str2nip(ip_string, &nip))
		return 0;

	add_static_lease(arg, (uint8_t*) &mac_bytes, nip);

	log_static_leases(arg);

	return 1;
}
Esempio n. 3
0
void cmd_del(int argc, char *argv[])
{
    int                 i;
    struct in_addr      ip_addr;
    struct ether_addr   mac_addr;


    if (argc != 4)
        help();

    if (inet_aton(argv[3], &ip_addr)) {
        cmd.args.del.addr_type = RTCFG_ADDR_IP;
        cmd.args.del.ip_addr   = ip_addr.s_addr;
    } else if (ether_aton_r(argv[3], &mac_addr) != NULL) {
        cmd.args.del.addr_type = RTCFG_ADDR_MAC;
        memcpy(cmd.args.del.mac_addr, mac_addr.ether_addr_octet,
               sizeof(mac_addr.ether_addr_octet));
    } else {
        fprintf(stderr, "invalid IP or physical address: %s\n", argv[3]);
        exit(1);
    }

    i = ioctl(f, RTCFG_IOC_DEL, &cmd);
    if (i < 0) {
        perror("ioctl");
        exit(1);
    }
    exit(0);
}
Esempio n. 4
0
struct ether_addr *
ether_aton(const char *a)
{
	static struct ether_addr e;

	return (ether_aton_r(a, &e));
}
Esempio n. 5
0
 explicit mac_address(std::string const &str) : addr_{{0}}
 {
     if (!ether_aton_r(str.c_str(), &addr_))
         throw std::invalid_argument(
             "invalid_argument: could not convert to internal type: " + str
         );
 }
Esempio n. 6
0
/*
 * The helper functions make Andrew Brown's interface really
 * shine.  It makes possible to create value on the fly whether
 * the sysctl value is read or written.
 *
 * As shown as an example in the man page, the first step is to
 * create a copy of the node to have sysctl_lookup work on it.
 *
 * Here, we have more work to do than just a copy, since we have
 * to create the string.  The first step is to collect the actual
 * value of the node, which is a convenient pointer to the softc
 * of the interface.  From there we create the string and use it
 * as the value, but only for the *copy* of the node.
 *
 * Then we let sysctl_lookup do the magic, which consists in
 * setting oldp and newp as required by the operation.  When the
 * value is read, that means that the string will be copied to
 * the user, and when it is written, the new value will be copied
 * over in the addr array.
 *
 * If newp is NULL, the user was reading the value, so we don't
 * have anything else to do.  If a new value was written, we
 * have to check it.
 *
 * If it is incorrect, we can return an error and leave 'node' as
 * it is:  since it is a copy of the actual node, the change will
 * be forgotten.
 *
 * Upon a correct input, we commit the change to the ifnet
 * structure of our interface.
 */
static int
tap_sysctl_handler(SYSCTLFN_ARGS)
{
	struct sysctlnode node;
	struct tap_softc *sc;
	struct ifnet *ifp;
	int error;
	size_t len;
	char addr[3 * ETHER_ADDR_LEN];
	uint8_t enaddr[ETHER_ADDR_LEN];

	node = *rnode;
	sc = node.sysctl_data;
	ifp = &sc->sc_ec.ec_if;
	(void)ether_snprintf(addr, sizeof(addr), CLLADDR(ifp->if_sadl));
	node.sysctl_data = addr;
	error = sysctl_lookup(SYSCTLFN_CALL(&node));
	if (error || newp == NULL)
		return (error);

	len = strlen(addr);
	if (len < 11 || len > 17)
		return (EINVAL);

	/* Commit change */
	if (ether_aton_r(enaddr, sizeof(enaddr), addr) != 0)
		return (EINVAL);
	if_set_sadl(ifp, enaddr, ETHER_ADDR_LEN, false);
	return (error);
}
Esempio n. 7
0
struct ether_addr *
ether_aton (const char *asc)
{
  static struct ether_addr result;

  return ether_aton_r (asc, &result);
}
Esempio n. 8
0
std::string textToEthernet(const std::string &textEthernetAddress) {
  ether_addr binaryEthernetAddress;
  ether_aton_r(textEthernetAddress.c_str(), &binaryEthernetAddress);
  #ifdef __FreeBSD__
    return std::string((char*)binaryEthernetAddress.octet, ETHER_ADDR_LEN);
  #endif
  #ifdef __linux__
    return std::string((char*)binaryEthernetAddress.ether_addr_octet,
                       ETHER_ADDR_LEN);
  #endif
}
Esempio n. 9
0
/*
 * This function accepts either a full MAC address using : or - as seperators.
 * Or a router hostname. The hostname will be searched for via MNDP broadcast packets.
 */
int query_mndp_or_mac(char *address, unsigned char *dstmac, int verbose) {
	char *p = address;
	int colons = 0;
	int dashs = 0;

	while (*p++) {
		if (*p == ':') {
			colons++;
		}
		else if (*p == '-') {
			dashs++;
		}
	}

	/* 
	* Windows users often enter macs with dash instead
	* of colon.
	*/
	if (colons == 0 && dashs == 5) {
		p = address;
		while (*p++) {
			if (*p == '-') {
				*p = ':';
			}
		}
		colons = dashs;
	}

	if (colons != 5) {
		/* 
		 * Not a valid mac-address.
		 * Search for Router by identity name, using MNDP
		 */
		if (verbose) {
			fprintf(stderr, _("Searching for '%s'..."), address);
		}
		if (!query_mndp(address, dstmac)) {
			if (verbose) {
				fprintf(stderr, _("not found\n"));
			}
			return 0;
		}

		/* Router found, display mac and continue */
		if (verbose) {
			fprintf(stderr, _("found\n"));
		}
	} else {
		/* Convert mac address string to ether_addr struct */
		ether_aton_r(address, (struct ether_addr *)dstmac);
	}

	return 1;
}
Esempio n. 10
0
bool parse_mac(const char *str, cfg_mac_t *mac)
{
	memset(&mac->buffer, 0, sizeof(struct ether_addr));

	mac->mac = ether_aton_r(str, &mac->buffer);
	if (mac->mac)
		return TRUE;

	AVB_LOGF_ERROR("Failed to parse addr: %s", str);
	return FALSE;
}
Esempio n. 11
0
static void
test_ether_aton_r_bad(void)
{
	struct ether_addr e, *ep;

	testnum++;
	ep = ether_aton_r(ether_aton_bad_string, &e);
	if (ep == &e)
		NOTOK("returned success");
	if (ep != NULL)
		NOTOK("returned different pointer");
	OK();
}
Esempio n. 12
0
bool openavbAdpOpenSocket(const char* ifname, U16 vlanID, U8 vlanPCP)
{
	AVB_TRACE_ENTRY(AVB_TRACE_ADP);

	hdr_info_t hdr;

#ifndef UBUNTU
	// This is the normal case for most of our supported platforms
	rxSock = openavbRawsockOpen(ifname, TRUE, FALSE, ETHERTYPE_8021Q, ADP_FRAME_LEN, ADP_NUM_BUFFERS);
#else
	rxSock = openavbRawsockOpen(ifname, TRUE, FALSE, ETHERTYPE_AVTP, ADP_FRAME_LEN, ADP_NUM_BUFFERS);
#endif
	txSock = openavbRawsockOpen(ifname, FALSE, TRUE, ETHERTYPE_AVTP, ADP_FRAME_LEN, ADP_NUM_BUFFERS);

	if (txSock && rxSock
		&& openavbRawsockGetAddr(txSock, ADDR_PTR(&intfAddr))
		&& ether_aton_r(ADP_PROTOCOL_ADDR, &adpAddr)
		&& openavbRawsockRxMulticast(rxSock, TRUE, ADDR_PTR(&adpAddr)))
	{
		if (!openavbRawsockRxAVTPSubtype(rxSock, OPENAVB_ADP_AVTP_SUBTYPE | 0x80)) {
			AVB_LOG_DEBUG("RX AVTP Subtype not supported");
		}

		memset(&hdr, 0, sizeof(hdr_info_t));
		hdr.shost = ADDR_PTR(&intfAddr);
		hdr.dhost = ADDR_PTR(&adpAddr);
		hdr.ethertype = ETHERTYPE_AVTP;
		if (vlanID != 0 || vlanPCP != 0) {
			hdr.vlan = TRUE;
			hdr.vlan_pcp = vlanPCP;
			hdr.vlan_vid = vlanID;
			AVB_LOGF_DEBUG("VLAN pcp=%d vid=%d", hdr.vlan_pcp, hdr.vlan_vid);
		}
		if (!openavbRawsockTxSetHdr(txSock, &hdr)) {
			AVB_LOG_ERROR("TX socket Header Failure");
			openavbAdpCloseSocket();
			AVB_TRACE_EXIT(AVB_TRACE_ADP);
			return false;
		}

		AVB_TRACE_EXIT(AVB_TRACE_ADP);
		return true;
	}

	AVB_LOG_ERROR("Invalid socket");
	openavbAdpCloseSocket();

	AVB_TRACE_EXIT(AVB_TRACE_ADP);
	return false;
}
Esempio n. 13
0
 sll_endpoint(std::string const& macaddr, std::string const& device_name) : data_{}
 {
     std::memset(&data_, sizeof data_, 0);
     data_.sll.sll_family = AF_PACKET;
     data_.sll.sll_halen = IFHWADDRLEN;
     data_.sll.sll_protocol = protocol_type::proto().protocol();
     data_.sll.sll_ifindex = ::if_nametoindex(device_name.c_str());
     namespace system = boost::system;
     if ( data_.sll.sll_ifindex == 0 )
         boost::asio::detail::throw_error(
            system::error_code(system::errc::no_such_device, system::system_category())
         );
     ether_aton_r(macaddr.c_str(), reinterpret_cast<struct ether_addr *>(&data_.sll.sll_addr));
 }
Esempio n. 14
0
/* Convert the host ID string to a MAC address.
 * The string may be a:
 *    Host name
 *    IP address string
 *    MAC address string
 */
static void get_dest_addr(const char *hostid, struct ether_addr *eaddr)
{
	struct ether_addr *eap;

	eap = ether_aton_r(hostid, eaddr);
	if (eap) {
		bb_debug_msg("The target station address is %s\n\n", ether_ntoa(eap));
#if !defined(__UCLIBC__) || UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 30)
	} else if (ether_hostton(hostid, eaddr) == 0) {
		bb_debug_msg("Station address for hostname %s is %s\n\n", hostid, ether_ntoa(eaddr));
#endif
	} else {
		bb_show_usage();
	}
}
Esempio n. 15
0
static void
test_ether_aton_r(void)
{
	struct ether_addr e, *ep;

	testnum++;
	ep = ether_aton_r(ether_aton_string, &e);
	if (ep == NULL)
		NOTOK("returned NULL");
	if (ep != &e)
		NOTOK("returned different pointer");
	if (bcmp(&e, &ether_aton_addr, ETHER_ADDR_LEN) != 0)
		NOTOK("bad address");
	OK();
}
void
init_picotcp() {
	struct ether_addr mac;
	struct pico_device *dev = NULL;
	struct pico_ip4 addr;
	struct pico_ip4 netm;

	ether_aton_r(config.if_mac, &mac);

	pico_stack_init();

	dev = pico_netmap_create(config.if_name, "eth_if", (uint8_t *) &mac);

	pico_string_to_ipv4(config.if_addr,  &addr.addr);
	pico_string_to_ipv4("255.255.255.0", &netm.addr);
	pico_ipv4_link_add(dev, addr, netm);
}
Esempio n. 17
0
static void handle_requests(int fd)
{
    struct message_buffer buf;
    struct request *request = &buf.u.request;
    struct ether_addr mac;
    int n, status;
    char *data;

    while ((n = read(fd, buf.chars, sizeof(buf.chars) - 1)) > 0) {
        buf.chars[n] = 0;
        if (parse_request(buf.chars, request)) {
            WARNING("invalid request");
            send_response(fd, 400, NULL, &buf);
            continue;
        }
        switch (request->method) {
        case RSUM:
        case SUSP:
            if (ether_aton_r(request->uri, &mac) == NULL) {
                WARNING("URI not a MAC address: %s", request->uri);
                send_response(fd, 404, NULL, &buf);
                break;
            }
            if (request->method == RSUM)
                status = resume(&mac);
            else // req.method == SUSP 
                status = suspend(&mac, &buf);
            send_response(fd, status, NULL, &buf);
            break; 
        case GETA:
            data = serialize_agent_list();
            if (data == NULL) {
                WARNING("serialize_agent_list() failed");
                break;
            }
            send_response(fd, 200, data, &buf);
            free(data);
            break;
        default:
            send_response(fd, 501, NULL, &buf);
            break;
        }
    }
    if (n == -1)
        WARNING("read() failed: %m");
}
Esempio n. 18
0
void cmd_add(int argc, char *argv[])
{
    int                 i;
    unsigned int        ioctl_code;
    struct in_addr      ip_addr;
    struct ether_addr   mac_addr;


    if (argc < 4)
        help();

    if (inet_aton(argv[3], &ip_addr)) {
        ioctl_code = RTCFG_IOC_ADD_IP;
        cmd.args.add.ip_addr = ip_addr.s_addr;
    } else if (ether_aton_r(argv[3], &mac_addr) != NULL) {
        ioctl_code = RTCFG_IOC_ADD_MAC;
        memcpy(cmd.args.add.mac_addr, mac_addr.ether_addr_octet,
               sizeof(mac_addr.ether_addr_octet));
    } else {
        fprintf(stderr, "Invalid IP or physical address: %s\n", argv[3]);
        exit(1);
    }

    cmd.args.add.stage1_file.size      = 0;
    cmd.args.add.stage1_file.frag_size = 0;
    cmd.args.add.stage2_file.size      = 0;
    cmd.args.add.stage2_file.frag_size = 0;

    for (i = 4; i < argc; i++) {
        if (strcmp(argv[i], "-stage1") == 0) {
            fprintf(stderr, "Configuration files are not supported yet.\n");
            exit(1);
        } else if (strcmp(argv[i], "-stage2") == 0) {
            fprintf(stderr, "Configuration files are not supported yet.\n");
            exit(1);
        } else
            help();
    }

    i = ioctl(f, ioctl_code, &cmd);
    if (i < 0) {
        perror("ioctl");
        exit(1);
    }
    exit(0);
}
Esempio n. 19
0
/* Convert the host ID string to a MAC address.
 * The string may be a:
 *    Host name
 *    IP address string
 *    MAC address string
 */
static void get_dest_addr(const char *hostid, struct ether_addr *eaddr)
{
	struct ether_addr *eap;

	eap = ether_aton_r(hostid, eaddr);
	if (eap) {
		bb_debug_msg("The target station address is %s\n\n", ether_ntoa(eap));
#if !defined(__UCLIBC_MAJOR__) \
 || __UCLIBC_MAJOR__ > 0 \
 || __UCLIBC_MINOR__ > 9 \
 || (__UCLIBC_MINOR__ == 9 && __UCLIBC_SUBLEVEL__ >= 30)
	} else if (ether_hostton(hostid, eaddr) == 0) {
		bb_debug_msg("Station address for hostname %s is %s\n\n", hostid, ether_ntoa(eaddr));
#endif
	} else {
		bb_show_usage();
	}
}
Esempio n. 20
0
static void nameif_parse_selector(ethtable_t *ch, char *selector)
{
	struct ether_addr *lmac;
#if ENABLE_FEATURE_NAMEIF_EXTENDED
	int found_selector = 0;

	while (*selector) {
		char *next;
#endif
		selector = skip_whitespace(selector);
#if ENABLE_FEATURE_NAMEIF_EXTENDED
		ch->phy_address = -1;
		if (*selector == '\0')
			break;
		/* Search for the end .... */
		next = skip_non_whitespace(selector);
		if (*next)
			*next++ = '\0';
		/* Check for selectors, mac= is assumed */
		if (strncmp(selector, "bus=", 4) == 0) {
			ch->bus_info = xstrdup(selector + 4);
			found_selector++;
		} else if (strncmp(selector, "driver=", 7) == 0) {
			ch->driver = xstrdup(selector + 7);
			found_selector++;
		} else if (strncmp(selector, "phyaddr=", 8) == 0) {
			ch->phy_address = xatoi_positive(selector + 8);
			found_selector++;
		} else {
#endif
			lmac = xmalloc(ETH_ALEN);
			ch->mac = ether_aton_r(selector + (strncmp(selector, "mac=", 4) != 0 ? 0 : 4), lmac);
			if (ch->mac == NULL)
				bb_error_msg_and_die("can't parse %s", selector);
#if  ENABLE_FEATURE_NAMEIF_EXTENDED
			found_selector++;
		};
		selector = next;
	}
	if (found_selector == 0)
		bb_error_msg_and_die("no selectors found for %s", ch->ifname);
#endif
}
Esempio n. 21
0
void route_add(int argc, char *argv[])
{
    struct ether_addr   dev_addr;
    int                 ret;


    if (argc == 6) {
        /*** add host route ***/
        if ((ether_aton_r(argv[3], &dev_addr) == NULL) ||
            (strcmp(argv[4], "dev") != 0))
            help();

        cmd.args.addhost.ip_addr = addr.s_addr;
        memcpy(cmd.args.addhost.dev_addr, dev_addr.ether_addr_octet,
               sizeof(dev_addr.ether_addr_octet));
        strncpy(cmd.head.if_name, argv[5], IFNAMSIZ);

        ret = ioctl(f, IOC_RT_HOST_ROUTE_ADD, &cmd);
    } else if (argc == 7) {
        /*** add network route ***/
        if ((strcmp(argv[3], "netmask") != 0) || (strcmp(argv[5], "gw") != 0))
            help();

        cmd.args.addnet.net_addr = addr.s_addr;
        if (!inet_aton(argv[4], &addr))
            help();
        cmd.args.addnet.net_mask = addr.s_addr;
        if (!inet_aton(argv[6], &addr))
            help();
        cmd.args.addnet.gw_addr = addr.s_addr;

        ret = ioctl(f, IOC_RT_NET_ROUTE_ADD, &cmd);
    } else
        help();

    if (ret < 0) {
        perror("ioctl");
        exit(1);
    }

    exit(0);
}
Esempio n. 22
0
int main(int argc, char **argv)
{
	int ch;
	int debug = 0, promisc = 0;
	int timeout = 100; 
	bpf_u_int32 localnet=0, netmask=0;
	unsigned int error = 0;
	char *interface = NULL;
	char *proto = ETHER_TYPE_TEST;
	char in_string[MAXPROG];
	char tmp[ETHER_ADDR_LEN];
	char addr[ETHER_ADDR_LEN];
	char *user_addr = NULL;
	pcap_t *capture;
	struct bpf_program program;
	struct pcap_pkthdr *header;
	unsigned char *packet = NULL;

	while ((ch = getopt(argc, argv, "a:e:i:t:pd")) != -1) {
		switch (ch) {
		case 'a':
			user_addr = optarg;
			break;
		case 'e':
			proto = optarg;
			break;
		case 'i':
			interface = optarg;
			break;
		case 'p':
			promisc = 1;
			break;
		case 't':
			timeout = atoi(optarg);
			break;
		case 'd':
			debug = 1;
			break;
		case '?':
		default:
			usage("invalid arguments");
		}
	}
	argc -= optind;
	argv += optind;

	if (interface == NULL)
		usage("You must specify an interface");

	if (user_addr != NULL)
		ether_aton_r(user_addr, (struct ether_addr *)&tmp);

	if ((capture = pcap_open_live(interface, SNAPLEN, promisc, timeout, 
				      &errbuf[0])) == NULL)
		usage(errbuf);

	snprintf(&in_string[0], MAXPROG, "ether proto %s\n", proto);

	if (pcap_lookupnet(interface, &localnet, &netmask, errbuf) < 0)
		usage(errbuf);

	if (pcap_compile(capture, &program, in_string, 1, netmask) < 0)
		usage(errbuf);

	if (pcap_setfilter(capture, &program) < 0)
		usage(errbuf);

	if (pcap_setdirection(capture, PCAP_D_IN) < 0)
		usage(errbuf);

	while (1) {
		error = pcap_next_ex(capture, &header, 
				     (const unsigned char **)&packet);
		if (error == 0)
			continue;
		if (error == -1)
			usage("packet read error");
		if (error == -2)
			usage("savefile?  invalid!");

		if (debug) {
			printf ("got packet of %d length\n", header->len);
			printf ("header %s\n", 
				ether_ntoa((const struct ether_addr*)
					   &packet[0]));
			printf ("header %s\n", 
				ether_ntoa((const struct ether_addr*)
					   &packet[ETHER_ADDR_LEN]));
		}
		
		/*
		 * If the user did not supply an address then we simply
		 * reverse the source and destination addresses.
		 */
		if (user_addr == NULL) {
			bcopy(packet, &tmp, ETHER_ADDR_LEN);
			bcopy(&packet[ETHER_ADDR_LEN], packet, ETHER_ADDR_LEN);
			bcopy(&tmp, &packet[ETHER_ADDR_LEN], ETHER_ADDR_LEN);
		} else {
			bcopy(&tmp, packet, ETHER_ADDR_LEN);
		}
		if (pcap_inject(capture, packet, header->len) < 0)
			if (debug)
				pcap_perror(capture, "pcap_inject");
	}
}
Esempio n. 23
0
static gboolean
verify (NMSetting *setting, GSList *all_settings, GError **error)
{
	NMSettingWirelessPrivate *priv = NM_SETTING_WIRELESS_GET_PRIVATE (setting);
	const char *valid_modes[] = { NM_SETTING_WIRELESS_MODE_INFRA, NM_SETTING_WIRELESS_MODE_ADHOC, NM_SETTING_WIRELESS_MODE_AP, NULL };
	const char *valid_bands[] = { "a", "bg", NULL };
	GSList *iter;

	if (!priv->ssid) {
		g_set_error_literal (error,
		                     NM_SETTING_WIRELESS_ERROR,
		                     NM_SETTING_WIRELESS_ERROR_MISSING_PROPERTY,
		                     _("property is missing"));
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID);
		return FALSE;
	}

	if (!priv->ssid->len || priv->ssid->len > 32) {
		g_set_error_literal (error,
		                     NM_SETTING_WIRELESS_ERROR,
		                     NM_SETTING_WIRELESS_ERROR_INVALID_PROPERTY,
		                     _("SSID length is out of range <1-32> bytes"));
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SSID);
		return FALSE;
	}

	if (priv->mode && !_nm_utils_string_in_list (priv->mode, valid_modes)) {
		g_set_error (error,
		             NM_SETTING_WIRELESS_ERROR,
		             NM_SETTING_WIRELESS_ERROR_INVALID_PROPERTY,
		             _("'%s' is not a valid Wi-Fi mode"),
		             priv->mode);
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_MODE);
		return FALSE;
	}

	if (priv->band && !_nm_utils_string_in_list (priv->band, valid_bands)) {
		g_set_error (error,
		             NM_SETTING_WIRELESS_ERROR,
		             NM_SETTING_WIRELESS_ERROR_INVALID_PROPERTY,
		             _("'%s' is not a valid band"),
		             priv->band);
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_BAND);
		return FALSE;
	}

	if (priv->channel && !priv->band) {
		g_set_error (error,
		             NM_SETTING_WIRELESS_ERROR,
		             NM_SETTING_WIRELESS_ERROR_CHANNEL_REQUIRES_BAND,
		             _("requires setting '%s' property"),
		             NM_SETTING_WIRELESS_BAND);
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_CHANNEL);
		return FALSE;
	}

	if (priv->channel) {
		if (!nm_utils_wifi_is_channel_valid (priv->channel, priv->band)) {
			g_set_error (error,
			             NM_SETTING_WIRELESS_ERROR,
			             NM_SETTING_WIRELESS_ERROR_INVALID_PROPERTY,
			             _("'%d' is not a valid channel"),
			             priv->channel);
			g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_CHANNEL);
			return FALSE;
		}
	}

	if (priv->bssid && priv->bssid->len != ETH_ALEN) {
		g_set_error_literal (error,
		                     NM_SETTING_WIRELESS_ERROR,
		                     NM_SETTING_WIRELESS_ERROR_INVALID_PROPERTY,
		                     _("property is invalid"));
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_BSSID);
		return FALSE;
	}

	if (priv->device_mac_address && priv->device_mac_address->len != ETH_ALEN) {
		g_set_error_literal (error,
		                     NM_SETTING_WIRELESS_ERROR,
		                     NM_SETTING_WIRELESS_ERROR_INVALID_PROPERTY,
		                     _("property is invalid"));
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_MAC_ADDRESS);
		return FALSE;
	}

	if (priv->cloned_mac_address && priv->cloned_mac_address->len != ETH_ALEN) {
		g_set_error_literal (error,
		                     NM_SETTING_WIRELESS_ERROR,
		                     NM_SETTING_WIRELESS_ERROR_INVALID_PROPERTY,
		                     _("property is invalid"));
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_CLONED_MAC_ADDRESS);
		return FALSE;
	}

	for (iter = priv->mac_address_blacklist; iter; iter = iter->next) {
		struct ether_addr addr;

		if (!ether_aton_r (iter->data, &addr)) {
			g_set_error (error,
			             NM_SETTING_WIRELESS_ERROR,
			             NM_SETTING_WIRELESS_ERROR_INVALID_PROPERTY,
			             _("'%s' is not a valid MAC address"),
			             (const char *) iter->data);
			g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_MAC_ADDRESS_BLACKLIST);
			return FALSE;
		}
	}

	for (iter = priv->seen_bssids; iter; iter = iter->next) {
		struct ether_addr addr;

		if (!ether_aton_r (iter->data, &addr)) {
			g_set_error (error,
			             NM_SETTING_WIRELESS_ERROR,
			             NM_SETTING_WIRELESS_ERROR_INVALID_PROPERTY,
			             _("'%s' is not a valid MAC address"),
			             (const char *) iter->data);
			g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_WIRELESS_SEEN_BSSIDS);
			return FALSE;
		}
	}

	return TRUE;
}
Esempio n. 24
0
void do_up(int argc, char *argv[])
{
    int                 ret;
    int                 i;
    struct in_addr      addr;
    __u32               ip_mask;
    struct ether_addr   hw_addr;


    if ((argc > 3) && (inet_aton(argv[3], &addr))) {
        i = 4;
        cmd.args.up.ip_addr = addr.s_addr;
        if (addr.s_addr == 0xFFFFFFFF) {
            fprintf(stderr, "Invalid IP address!\n");
            exit(1);
        }
    } else {
        i = 3;
        /* don't change ip settings */
        cmd.args.up.ip_addr = 0xFFFFFFFF;
    }

    /* set default netmask */
    if (ntohl(cmd.args.up.ip_addr) <= 0x7FFFFFFF)       /* 127.255.255.255  */
        ip_mask = 0x000000FF;                           /* 255.0.0.0        */
    else if (ntohl(cmd.args.up.ip_addr) <= 0xBFFFFFFF)  /* 191.255.255.255  */
        ip_mask = 0x0000FFFF;                           /* 255.255.0.0      */
    else
        ip_mask = 0x00FFFFFF;                           /* 255.255.255.0    */

    /* default: don't change flags, don't set dev_addr */
    cmd.args.up.set_dev_flags   = 0;
    cmd.args.up.clear_dev_flags = 0;
    cmd.args.up.dev_addr_type   = 0xFFFF;

    /* parse optional parameters */
    for ( ; i < argc; i++) {
        if (strcmp(argv[i], "netmask") == 0) {
            if ((++i >= argc) || (cmd.args.up.ip_addr == 0) ||
                (!inet_aton(argv[i], &addr)))
                help();
            ip_mask = addr.s_addr;
        } else if (strcmp(argv[i], "hw ether") == 0) {
            if ((++i >= argc) || (ether_aton_r(argv[i], &hw_addr) == NULL))
                help();
            memcpy(cmd.args.up.dev_addr, hw_addr.ether_addr_octet,
                   sizeof(hw_addr.ether_addr_octet));
            cmd.args.up.dev_addr_type = ARPHRD_ETHER;
        } else if (strcmp(argv[i], "promisc") == 0) {
            cmd.args.up.set_dev_flags   |= IFF_PROMISC;
            cmd.args.up.clear_dev_flags &= ~IFF_PROMISC;
        } else if (strcmp(argv[i], "-promisc") == 0) {
            cmd.args.up.set_dev_flags   &= ~IFF_PROMISC;
            cmd.args.up.clear_dev_flags |= IFF_PROMISC;
        } else
            help();
    }

    cmd.args.up.broadcast_ip = cmd.args.up.ip_addr | (~ip_mask);

    ret = ioctl(f, IOC_RT_IFUP, &cmd);
    if (ret < 0) {
        perror("ioctl");
        exit(1);
    }
    exit(0);
}
Esempio n. 25
0
struct ether_addr *MF_EtherProto::ether_aton(const char *asc) {
	//MF_Log::mf_log(MF_DEBUG, "MF_EtherProto:ether_aton");
    static struct ether_addr result;
    return ether_aton_r(asc, &result);
}
static bool
parse_arguments( int argc, char *argv[], command_options *options ) {
  assert( argv != NULL );
  assert( options != NULL );

  if ( argc <= 1 || argc >= 11 ) {
    return false;
  }

  memset( options, 0, sizeof( command_options ) );
  options->type = MESSAGE_TYPE_MAX;
  options->port = 0;
  options->aging_time = -1;

  bool ret = true;
  int c = -1;
  while ( ( c = getopt_long( argc, argv, short_options, long_options, NULL ) ) != -1 ) {
    switch ( c ) {
      case 'a':
        options->type = ADD_INSTANCE_REQUEST;
        break;

      case 's':
        options->type = SET_INSTANCE_REQUEST;
        break;

      case 'd':
        options->type = DEL_INSTANCE_REQUEST;
        break;

      case 'g':
        if ( options->type != LIST_INSTANCES_REQUEST) {
          options->type = SHOW_GLOBAL_REQUEST;
        }
        options->set_bitmap |= SHOW_GLOBAL;
        break;

      case 'l':
        options->type = LIST_INSTANCES_REQUEST;
        break;

      case 'f':
        options->type = SHOW_FDB_REQUEST;
        break;

      case 'e':
        options->type = ADD_FDB_ENTRY_REQUEST;
        break;

      case 'b':
        options->type = DEL_FDB_ENTRY_REQUEST;
        break;

      case 'w':
        options->type = INACTIVATE_INSTANCE_REQUEST;
        break;

      case 'o':
        options->type = ACTIVATE_INSTANCE_REQUEST;
        break;

      case 'n':
        if ( optarg != NULL ) {
          char *endp = NULL;
          unsigned long int vni = strtoul( optarg, &endp, 0 );
          if ( *endp == '\0' && vni <= 0x00ffffff ) {
            options->vni = ( uint32_t ) vni;
            options->set_bitmap |= SET_VNI;
          }
          else {
            printf( "Invalid VNI value ( %s ).\n", optarg );
            ret &= false;
          }
        }
        else {
          printf( "A VNI must be specified.\n" );
          ret &= false;
        }
        break;

      case 'i':
      {
        if ( optarg != NULL ) {
          int retval = inet_pton( AF_INET, optarg, &options->ip_addr );
          if ( retval != 1 ) {
            printf( "Invalid IP address value ( %s ).\n", optarg );
            ret &= false;
          }
          else {
            options->set_bitmap |= SET_IP_ADDR;
          }
        }
        else {
          printf( "An IP address must be specified.\n" );
          ret &= false;
        }
      }
      break;

      case 'p':
        if ( optarg != NULL ) {
          char *endp = NULL;
          unsigned long int port = strtoul( optarg, &endp, 0 );
          if ( *endp == '\0' && port <= UINT16_MAX ) {
            options->port = ( uint16_t ) port;
            options->set_bitmap |= SET_UDP_PORT;
          }
          else {
            printf( "Invalid UDP port value ( %s ).\n", optarg );
            ret &= false;
          }
        }
        else {
          printf( "A UDP port must be specified.\n" );
          ret &= false;
        }
        break;

      case 'm':
        if ( optarg != NULL ) {
          struct ether_addr *addr = ether_aton_r( optarg, &options->eth_addr );
          if ( addr == NULL ) {
            printf( "Invalid MAC address ( %s ).\n", optarg );
            ret &= false;
          }
          options->set_bitmap |= SET_MAC_ADDR;
        }
        else {
          printf( "A UDP port must be specified.\n" );
          ret &= false;
        }
        break;

      case 't':
        if ( optarg != NULL ) {
          char *endp = NULL;
          long long int aging_time = strtoll( optarg, &endp, 0 );
          if ( *endp == '\0' && aging_time >= 0 && aging_time <= VXLAN_MAX_AGING_TIME ) {
            options->aging_time = ( time_t ) aging_time;
            options->set_bitmap |= SET_AGING_TIME;
          }
          else {
            printf( "Invalid aging time value ( %s ).\n", optarg );
            ret &= false;
          }
        }
        else {
          printf( "An aging time value must be specified.\n" );
          ret &= false;
        }
        break;

      case 'q':
        options->set_bitmap |= DISABLE_HEADER;
        break;

      case 'h':
        usage();
        exit( EXIT_SUCCESS );
        break;

      default:
        ret &= false;
        break;
    }
  }

  switch ( options->type ) {
    case ADD_INSTANCE_REQUEST:
    {
      uint16_t mask = SET_VNI;
      if ( ( options->set_bitmap & mask ) != mask ) {
        ret &= false;
      }
      mask = SET_VNI | SET_IP_ADDR | SET_UDP_PORT | SET_AGING_TIME;
      if ( ( options->set_bitmap & ~mask ) != 0 ) {
        ret &= false;
      }
    }
    break;

    case SET_INSTANCE_REQUEST:
    {
      uint16_t mask = SET_VNI;
      if ( ( options->set_bitmap & mask ) != mask ) {
        ret &= false;
      }
      mask = SET_IP_ADDR | SET_UDP_PORT | SET_AGING_TIME;
      if ( ( options->set_bitmap & mask ) == 0 ) {
        ret &= false;
      }
      mask = SET_VNI | SET_IP_ADDR | SET_UDP_PORT | SET_AGING_TIME;
      if ( ( options->set_bitmap & ~mask ) != 0 ) {
        ret &= false;
      }
    }
    break;

    case INACTIVATE_INSTANCE_REQUEST:
    {
      if ( options->set_bitmap != SET_VNI ) {
        ret &= false;
      }
    }
    break;

    case ACTIVATE_INSTANCE_REQUEST:
    {
      if ( options->set_bitmap != SET_VNI ) {
        ret &= false;
      }
    }
    break;

    case DEL_INSTANCE_REQUEST:
    {
      if ( options->set_bitmap != SET_VNI ) {
        ret &= false;
      }
    }
    break;

    case SHOW_GLOBAL_REQUEST:
    {
      uint16_t mask = SHOW_GLOBAL | DISABLE_HEADER;
      if ( ( options->set_bitmap & ~mask ) != 0 ) {
        ret &= false;
      }
    }
    break;

    case LIST_INSTANCES_REQUEST:
    {
      uint16_t mask = SET_VNI;
      if ( ( options-> set_bitmap & mask ) == 0 )  {
        options->vni = 0xffffffff;
      }
      mask = SET_VNI | SHOW_GLOBAL | DISABLE_HEADER;
      if ( ( options->set_bitmap & ~mask ) != 0 ) {
        ret &= false;
      }
    }
    break;

    case SHOW_FDB_REQUEST:
    {
      if ( options->set_bitmap != SET_VNI ) {
        ret &= false;
      }
    }
    break;

    case ADD_FDB_ENTRY_REQUEST:
    {
      uint16_t mask = SET_VNI | SET_IP_ADDR | SET_MAC_ADDR;
      if ( ( options->set_bitmap & mask ) != mask ) {
        ret &= false;
      }
      mask = SET_VNI | SET_IP_ADDR | SET_MAC_ADDR | SET_AGING_TIME;
      if ( ( options->set_bitmap & ~mask ) != 0 ) {
        ret &= false;
      }
    }
    break;

    case DEL_FDB_ENTRY_REQUEST:
    {
      uint16_t mask = SET_VNI;
      if ( ( options->set_bitmap & mask ) != mask ) {
        ret &= false;
      }
      mask = SET_VNI | SET_MAC_ADDR;
      if ( ( options->set_bitmap & ~mask ) != 0 ) {
        ret &= false;
      }
    }
    break;

    default:
    {
      ret &= false;
    }
    break;
  }

  return ret;
}
Esempio n. 27
0
void cmd_add(int argc, char *argv[])
{
    int               i;
    struct in_addr    ip_addr;
    struct ether_addr mac_addr;
    const char        *stage1_filename = NULL;
    const char        *stage2_filename = NULL;
    int               file;
    size_t            buf_size;
    void              *new_buf;


    if (argc < 4)
        help();

    if (inet_aton(argv[3], &ip_addr)) {
        cmd.args.add.addr_type = RTCFG_ADDR_IP;
        cmd.args.add.ip_addr   = ip_addr.s_addr;
    } else if (ether_aton_r(argv[3], &mac_addr) != NULL) {
        cmd.args.add.addr_type = RTCFG_ADDR_MAC;
        memcpy(cmd.args.add.mac_addr, mac_addr.ether_addr_octet,
               sizeof(mac_addr.ether_addr_octet));
    } else {
        fprintf(stderr, "invalid IP or physical address: %s\n", argv[3]);
        exit(1);
    }

    cmd.args.add.stage1_data     = NULL;
    cmd.args.add.stage1_size     = 0;
    cmd.args.add.stage2_filename = NULL;
    cmd.args.add.timeout         = 0;   /* infinite */

    for (i = 4; i < argc; i++) {
        if (strcmp(argv[i], "-hw") == 0) {
            if ((++i >= argc) || (ether_aton_r(argv[i], &mac_addr) == NULL))
                help();
            cmd.args.add.addr_type = RTCFG_ADDR_IP | FLAG_ASSIGN_ADDR_BY_MAC;
            memcpy(cmd.args.add.mac_addr, mac_addr.ether_addr_octet,
                   sizeof(mac_addr.ether_addr_octet));
        } else if (strcmp(argv[i], "-stage1") == 0) {
            if (++i >= argc)
                help();
            stage1_filename = argv[i];
        } else if (strcmp(argv[i], "-stage2") == 0) {
            if (++i >= argc)
                help();
            stage2_filename = argv[i];
        } else if (strcmp(argv[i], "-t") == 0)
            cmd.args.add.timeout = getintopt(argc, ++i, argv, 0);
        else
            help();
    }

    if (stage1_filename != NULL) {
        if (strcmp(stage1_filename, "-") == 0)
            file = 0; /* stdin */
        else {
            file = open(stage1_filename, O_RDONLY);
            if (file < 0) {
                perror("open stage 1 file");
                exit(1);
            }
        }

        buf_size = 0;
        do {
            buf_size += 4096;

            new_buf = realloc(cmd.args.add.stage1_data, buf_size);
            if (new_buf == NULL) {
                fprintf(stderr, "insufficient memory\n");
                if (cmd.args.add.stage1_data != NULL)
                    free(cmd.args.add.stage1_data);
                exit(1);
            }
            cmd.args.add.stage1_data = new_buf;

            i = read(file, cmd.args.add.stage1_data+cmd.args.add.stage1_size,
                     4096);
            if (i < 0) {
                perror("read stage 1 file");
                free(cmd.args.add.stage1_data);
                exit(1);
            }
            cmd.args.add.stage1_size += i;
        } while (i == 4096);

        close(file);
    }

    if (stage2_filename != NULL) {
        cmd.args.add.stage2_filename = malloc(PATH_MAX);
        if (cmd.args.add.stage2_filename == NULL) {
            fprintf(stderr, "insufficient memory\n");
            if (cmd.args.add.stage1_data != NULL)
                free(cmd.args.add.stage1_data);
            exit(1);
        }

        if (realpath(stage2_filename,
                     (char *)cmd.args.add.stage2_filename) == NULL) {
            perror("resolve stage 2 file");
            free((void *)cmd.args.add.stage2_filename);
            if (cmd.args.add.stage1_data != NULL)
                free(cmd.args.add.stage1_data);
            exit(1);
        }
    }

    i = ioctl(f, RTCFG_IOC_ADD, &cmd);

    if (cmd.args.add.stage1_data != NULL)
        free(cmd.args.add.stage1_data);
    if (cmd.args.add.stage2_filename != NULL)
        free((void *)cmd.args.add.stage2_filename);

    if (i < 0) {
        switch (errno) {
            case ESTAGE1SIZE:
                fprintf(stderr, "stage 1 file too big\n");
                break;

            case EEXIST:
                fprintf(stderr, "client entry already exists\n");
                break;

            default:
                perror("ioctl (add)");
        }
        exit(1);
    }
    exit(0);
}
Esempio n. 28
0
struct ether_addr* ether_aton(const char* x) {
    static struct ether_addr a;
    return ether_aton_r(x, &a);
}
Esempio n. 29
0
static gboolean
verify (NMSetting *setting, GSList *all_settings, GError **error)
{
	NMSettingWiredPrivate *priv = NM_SETTING_WIRED_GET_PRIVATE (setting);
	const char *valid_ports[] = { "tp", "aui", "bnc", "mii", NULL };
	const char *valid_duplex[] = { "half", "full", NULL };
	const char *valid_nettype[] = { "qeth", "lcs", "ctc", NULL };
	GHashTableIter iter;
	GSList* mac_blacklist_iter;
	const char *key, *value;

	if (priv->port && !_nm_utils_string_in_list (priv->port, valid_ports)) {
		g_set_error (error,
		             NM_SETTING_WIRED_ERROR,
		             NM_SETTING_WIRED_ERROR_INVALID_PROPERTY,
		             _("'%s' is not a valid Ethernet port value"),
		             priv->port);
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRED_SETTING_NAME, NM_SETTING_WIRED_PORT);
		return FALSE;
	}

	if (priv->duplex && !_nm_utils_string_in_list (priv->duplex, valid_duplex)) {
		g_set_error (error,
		             NM_SETTING_WIRED_ERROR,
		             NM_SETTING_WIRED_ERROR_INVALID_PROPERTY,
		             _("'%s' is not a valid duplex value"),
		             priv->duplex);
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRED_SETTING_NAME, NM_SETTING_WIRED_DUPLEX);
		return FALSE;
	}

	if (priv->device_mac_address && priv->device_mac_address->len != ETH_ALEN) {
		g_set_error_literal (error,
		                     NM_SETTING_WIRED_ERROR,
		                     NM_SETTING_WIRED_ERROR_INVALID_PROPERTY,
		                     _("is not a valid MAC address"));
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRED_SETTING_NAME, NM_SETTING_WIRED_MAC_ADDRESS);
		return FALSE;
	}

	for (mac_blacklist_iter = priv->mac_address_blacklist; mac_blacklist_iter;
	     mac_blacklist_iter = mac_blacklist_iter->next) {
		struct ether_addr addr;

		if (!ether_aton_r (mac_blacklist_iter->data, &addr)) {
			g_set_error (error,
			             NM_SETTING_WIRED_ERROR,
			             NM_SETTING_WIRED_ERROR_INVALID_PROPERTY,
			             _("'%s' is not a valid MAC address"),
			             (const char *) mac_blacklist_iter->data);
			g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRED_SETTING_NAME, NM_SETTING_WIRED_MAC_ADDRESS_BLACKLIST);
			return FALSE;
		}
	}

	if (   priv->s390_subchannels
	    && !(priv->s390_subchannels->len == 3 || priv->s390_subchannels->len == 2)) {
		g_set_error_literal (error,
		                     NM_SETTING_WIRED_ERROR,
		                     NM_SETTING_WIRED_ERROR_INVALID_PROPERTY,
		                     _("property is invalid"));
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRED_SETTING_NAME, NM_SETTING_WIRED_S390_SUBCHANNELS);
		return FALSE;
	}

	if (priv->s390_nettype && !_nm_utils_string_in_list (priv->s390_nettype, valid_nettype)) {
		g_set_error_literal (error,
		                     NM_SETTING_WIRED_ERROR,
		                     NM_SETTING_WIRED_ERROR_INVALID_PROPERTY,
		                     _("property is invalid"));
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRED_SETTING_NAME, NM_SETTING_WIRED_S390_NETTYPE);
		return FALSE;
	}

	g_hash_table_iter_init (&iter, priv->s390_options);
	while (g_hash_table_iter_next (&iter, (gpointer) &key, (gpointer) &value)) {
		if (   !_nm_utils_string_in_list (key, valid_s390_opts)
		    || !strlen (value)
		    || (strlen (value) > 200)) {
			g_set_error (error,
			             NM_SETTING_WIRED_ERROR,
			             NM_SETTING_WIRED_ERROR_INVALID_PROPERTY,
			             _("invalid '%s' or its value '%s'"),
			             key, value);
			g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRED_SETTING_NAME, NM_SETTING_WIRED_S390_OPTIONS);
			return FALSE;
		}
	}

	if (priv->cloned_mac_address && priv->cloned_mac_address->len != ETH_ALEN) {
		g_set_error_literal (error,
		                     NM_SETTING_WIRED_ERROR,
		                     NM_SETTING_WIRED_ERROR_INVALID_PROPERTY,
		                     _("is not a valid MAC address"));
		g_prefix_error (error, "%s.%s: ", NM_SETTING_WIRED_SETTING_NAME, NM_SETTING_WIRED_CLONED_MAC_ADDRESS);
		return FALSE;
	}

	return TRUE;
}
Esempio n. 30
0
int main(int argc, char **argv)
{
	unsigned char opt_flags[256] = {};
	unsigned int kill_after_s = 0;
	const char *optstr = "i:a:p:s:d:m:T:P:SNh";
	int min_port = 0, max_port = 0;
	struct iptnl_info tnl = {};
	struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
	struct vip vip = {};
	char filename[256];
	int opt;
	int i;

	tnl.family = AF_UNSPEC;
	vip.protocol = IPPROTO_TCP;

	for (i = 0; i < strlen(optstr); i++)
		if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z')
			opt_flags[(unsigned char)optstr[i]] = 1;

	while ((opt = getopt(argc, argv, optstr)) != -1) {
		unsigned short family;
		unsigned int *v6;

		switch (opt) {
		case 'i':
			ifindex = atoi(optarg);
			break;
		case 'a':
			vip.family = parse_ipstr(optarg, vip.daddr.v6);
			if (vip.family == AF_UNSPEC)
				return 1;
			break;
		case 'p':
			if (parse_ports(optarg, &min_port, &max_port))
				return 1;
			break;
		case 'P':
			vip.protocol = atoi(optarg);
			break;
		case 's':
		case 'd':
			if (opt == 's')
				v6 = tnl.saddr.v6;
			else
				v6 = tnl.daddr.v6;

			family = parse_ipstr(optarg, v6);
			if (family == AF_UNSPEC)
				return 1;
			if (tnl.family == AF_UNSPEC) {
				tnl.family = family;
			} else if (tnl.family != family) {
				fprintf(stderr,
					"The IP version of the src and dst addresses used in the IP encapsulation does not match\n");
				return 1;
			}
			break;
		case 'm':
			if (!ether_aton_r(optarg,
					  (struct ether_addr *)tnl.dmac)) {
				fprintf(stderr, "Invalid mac address:%s\n",
					optarg);
				return 1;
			}
			break;
		case 'T':
			kill_after_s = atoi(optarg);
			break;
		case 'S':
			xdp_flags |= XDP_FLAGS_SKB_MODE;
			break;
		case 'N':
			xdp_flags |= XDP_FLAGS_DRV_MODE;
			break;
		default:
			usage(argv[0]);
			return 1;
		}
		opt_flags[opt] = 0;
	}

	for (i = 0; i < strlen(optstr); i++) {
		if (opt_flags[(unsigned int)optstr[i]]) {
			fprintf(stderr, "Missing argument -%c\n", optstr[i]);
			usage(argv[0]);
			return 1;
		}
	}

	if (setrlimit(RLIMIT_MEMLOCK, &r)) {
		perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)");
		return 1;
	}

	snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);

	if (load_bpf_file(filename)) {
		printf("%s", bpf_log_buf);
		return 1;
	}

	if (!prog_fd[0]) {
		printf("load_bpf_file: %s\n", strerror(errno));
		return 1;
	}

	signal(SIGINT, int_exit);
	signal(SIGTERM, int_exit);

	while (min_port <= max_port) {
		vip.dport = htons(min_port++);
		if (bpf_map_update_elem(map_fd[1], &vip, &tnl, BPF_NOEXIST)) {
			perror("bpf_map_update_elem(&vip2tnl)");
			return 1;
		}
	}

	if (bpf_set_link_xdp_fd(ifindex, prog_fd[0], xdp_flags) < 0) {
		printf("link set xdp fd failed\n");
		return 1;
	}

	poll_stats(kill_after_s);

	bpf_set_link_xdp_fd(ifindex, -1, xdp_flags);

	return 0;
}