Ejemplo n.º 1
0
Archivo: arp.c Proyecto: Xeslaro/mp
int main(int c, char *z[])
{
	errn1(packet_socket = socket(AF_PACKET, SOCK_RAW, rev2(ETH_P_ALL)), "open socket error");
	struct sockaddr_ll	sa_ll_b, sa_ll_s;
	sa_ll_b.sll_family = sa_ll_s.sll_family = AF_PACKET;
	sa_ll_b.sll_protocol = rev2(ETH_P_ALL), sa_ll_b.sll_ifindex = get_ifindex(packet_socket, net_if);
	sa_ll_s.sll_halen = 0x06, sa_ll_s.sll_ifindex = get_ifindex(packet_socket, net_if), strcpy(sa_ll_s.sll_addr, "\xff\xff\xff\xff\xff\xff");
	ok0(bind(packet_socket, (struct sockaddr*)&sa_ll_b, sizeof(struct sockaddr_ll)), "bind error");
	char	msg[1500+14];
	int	len = mk_arp_request_packet(msg, "\xcc\xcc\xcc\xcc\xcc\xcc", conv_ip("1.1.1.1"), conv_ip(z[1]));
	semi	a;
	a.socket = packet_socket, a.len = len, a.msg = msg, a.sa_ll = &sa_ll_s;
	pthread_t	thread_info;
	ok0(sem_init(&sem_info, 0, 0), "sem_init error");
	ok0(pthread_create(&thread_info, NULL, send_eth_packet, (void*)&a), "pthread_create error");
	f = set_promiscuous(packet_socket, net_if);
	signal(SIGALRM, alarm_handler);
	alarm(2);
	while (1) {
		ok0(sem_post(&sem_info), "sem_post error");
		errn1(recvfrom(packet_socket, msg, 1514, 0, NULL, NULL), "recvfrom error");
		if (*((short*)(msg+12)) == rev2(0x0806) && *((int*)(msg+28)) == conv_ip(z[1])) {
			int	i;
			for (i=0;i<6;i++)
				printf("%.2x%c", msg[22+i]&0xff, (i+1==6)?'\n':':');
			if (f)
				leave_promiscuous(packet_socket, net_if);
			return 0;
		}
	}
}
Ejemplo n.º 2
0
static int nft_fib6_flowi_init(struct flowi6 *fl6, const struct nft_fib *priv,
			       const struct nft_pktinfo *pkt,
			       const struct net_device *dev)
{
	const struct ipv6hdr *iph = ipv6_hdr(pkt->skb);
	int lookup_flags = 0;

	if (priv->flags & NFTA_FIB_F_DADDR) {
		fl6->daddr = iph->daddr;
		fl6->saddr = iph->saddr;
	} else {
		fl6->daddr = iph->saddr;
		fl6->saddr = iph->daddr;
	}

	if (ipv6_addr_type(&fl6->daddr) & IPV6_ADDR_LINKLOCAL) {
		lookup_flags |= RT6_LOOKUP_F_IFACE;
		fl6->flowi6_oif = get_ifindex(dev ? dev : pkt->skb->dev);
	}

	if (ipv6_addr_type(&fl6->saddr) & IPV6_ADDR_UNICAST)
		lookup_flags |= RT6_LOOKUP_F_HAS_SADDR;

	if (priv->flags & NFTA_FIB_F_MARK)
		fl6->flowi6_mark = pkt->skb->mark;

	fl6->flowlabel = (*(__be32 *)iph) & IPV6_FLOWINFO_MASK;

	return lookup_flags;
}
Ejemplo n.º 3
0
int raw_send(char *net_i, uint16_t ether_type, uint8_t *ptr, int len)
{
    int fd;
    int ifindex = 0;
    uint8_t *buf = NULL;
    char *device = NULL;
    struct sockaddr_ll sll;
    uint16_t n_ethtype;
    char dmac[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
    char smac[6];

    raw_print("line: %d.\n", __LINE__);
    if (net_i) {
        device = net_i;
        if (get_macaddr(device, smac)) {
            memset(smac, 0x88, 6);
        }
    } else {
        device = "lo";
        memset(smac, 0x88, 6);
    }
    if ((ifindex = get_ifindex(device)) <= 0) {
        return -1;
    }
    n_ethtype = htons(ether_type);
    if ((fd = socket(AF_PACKET, SOCK_RAW, n_ethtype)) < 0) {
        printf("create socket failed.\n");
        return fd;
    }
    raw_print("line: %d.\n", __LINE__);

    bzero(&sll, sizeof(sll));
    sll.sll_family = AF_PACKET;
    sll.sll_halen = 6;
    //memcpy(sll.sll_addr, dmac, 6);
    sll.sll_ifindex = ifindex;
    sll.sll_protocol = n_ethtype;

    bind(fd, (struct sockaddr *)&sll, sizeof(sll));
    if (strlen((char *)ptr) == 0) {
        ptr = (uint8_t *)"xuchunxiao";
        len = strlen((char *)ptr);
    }
    buf = malloc(len + 14);
    memcpy(buf, dmac, 6);
    memcpy(buf + 6, smac, 6);
    memcpy(buf + 12, &n_ethtype, 2);
    memcpy(buf + 14, ptr, len);
    raw_print("line: %d.\n", __LINE__);
    if (sendto(fd, buf, len + 14, 0, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
        perror("sendto error.\n");
        return -1;
    }
    raw_print("line: %d.\n", __LINE__);
    free(buf);
    close(fd);
    return 0;
}
Ejemplo n.º 4
0
int main(int argc, char **argv)
{
	const char *options="c:i:";
	int opt_status=0;
	int pppoe_count=1;
	char *NIC_name;
	while((opt_status=getopt(argc,argv,options))!=-1)
		switch (opt_status) {
			case 'c':
				pppoe_count=atoi(optarg);
				break;
			case 'i':
				NIC_name=optarg;
				break;
			default:
				break;
		}
	//how many pppoe client to fork is determined!
	
	struct Connection_info infos[pppoe_count];
	memset(infos,0,sizeof(infos));
	//shuttles for pppoe infomations
	
	srand((unsigned int)getpid()); //ready for rand
	set_promisc(NIC_name); //set the NIC to be used promisc mode
	
	
	
	int count1;
	for(count1=0;count1<pppoe_count;++count1) {
		infos[count1].ifindex=get_ifindex(NIC_name); //get the ifindex of NIC
		infos[count1].my_mac[0]=0x11;
		infos[count1].my_mac[1]=0x22;
		infos[count1].my_mac[2]=0x33;
		int loop_count;
		for(loop_count=3;loop_count<6;++loop_count) {
		infos[count1].my_mac[loop_count]=(int)(rand()/(float)RAND_MAX*255);
		}
		//src mac of package done!
		infos[count1].discovery_sock=socket(AF_PACKET,SOCK_RAW,htons(ETHER_TYPE_DISCOVERY));
		if(infos[count1].discovery_sock<0) {
			printf("error in creating discovery socket\n%s\n",strerror(errno));
			exit(1);
		}
		//discovery socket's ready!
		sendPADI(&infos[count1]);
		recv_PADO(&infos[count1]);
		sendPADR(&infos[count1]);
		recv_PADS(&infos[count1]);
		if(fork()==0) {
			session(&infos[count1]);
		}
		printf("client %i is forked!\n",count1+1);
		sleep(1);
	}
	return 0;
}
Ejemplo n.º 5
0
int raw_send_all(char *net_i, uint8_t *ptr, int len)
{
    int fd;
    int ifindex = 0;
    char *device = NULL;
    struct sockaddr_ll sll;

    raw_print("line: %d.\n", __LINE__);
    condition_if_false_ret(ptr != NULL, -1);
    if (net_i) {
        device = net_i;
    } else {
        device = "lo";
    }
    if ((ifindex = get_ifindex(device)) <= 0) {
        return -1;
    }
    if ((fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
        printf("create socket failed.\n");
        return fd;
    }
    raw_print("line: %d.\n", __LINE__);

    bzero(&sll, sizeof(sll));
    sll.sll_family = AF_PACKET;
    sll.sll_halen = 6;
    //memcpy(sll.sll_addr, dmac, 6);
    sll.sll_ifindex = ifindex;
    sll.sll_protocol = htons(ETH_P_ALL);

    bind(fd, (struct sockaddr *)&sll, sizeof(sll));
    if (strlen((char *)ptr) == 0) {
        ptr = (uint8_t *)"xuchunxiao";
        len = strlen((char *)ptr);
    }
    raw_print("line: %d.\n", __LINE__);
    if (sendto(fd, ptr, len, 0, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
        perror("sendto error.\n");
        return -1;
    }
    raw_print("line: %d.\n", __LINE__);
    close(fd);
    return 0;
}
Ejemplo n.º 6
0
Archivo: uacctd.c Proyecto: hrey/pmacct
/* Cache name to ifindex mapping */
unsigned int cache_ifindex(char *device, unsigned long now)
{
  struct ifname_cache *ifc, **top;
  unsigned int ifindex;

  top = &hash_heads[hash_ifname(device)];
  while ( (ifc = *top) != NULL) {
    if (strncmp(device, ifc->name, IFNAMSIZ)) {
      top = &ifc->next;
      continue;
    }

    /* prune old entry to deal with hotplug */
    if ((long)(now - ifc->tstamp) > IFCACHE_LIFETIME) {
      *top = ifc->next;
      free(ifc);
      break;
    }

    return ifc->index;
  }

  ifindex = get_ifindex(device);
  if (ifindex) {
    ifc = malloc(sizeof(struct ifname_cache));
    if (ifc) {
      ifc->index = ifindex;
      strncpy(ifc->name, device, IFNAMSIZ);
      ifc->tstamp = now;
      ifc->next = *top;
      *top = ifc;
    }
  }

  return ifindex;
}
Ejemplo n.º 7
0
/*
 * parse a conn section
 */
static void load_conn(starter_conn_t *conn, kw_list_t *kw, starter_config_t *cfg)
{
	char *conn_name = (conn->name == NULL)? "%default":conn->name;

	for ( ; kw; kw = kw->next)
	{
		bool assigned = FALSE;

		kw_token_t token = kw->entry->token;

		if (token >= KW_LEFT_FIRST && token <= KW_LEFT_LAST)
		{
			kw_end(conn, &conn->left, token - KW_LEFT_FIRST + KW_END_FIRST
				,  kw, conn_name, cfg);
			continue;
		}
		else if (token >= KW_RIGHT_FIRST && token <= KW_RIGHT_LAST)
		{
			kw_end(conn, &conn->right, token - KW_RIGHT_FIRST + KW_END_FIRST
				 , kw, conn_name, cfg);
			continue;
		}

		if (token == KW_AUTO)
		{
			token = KW_CONN_SETUP;
		}
		else if (token == KW_ALSO)
		{
			if (cfg->parse_also)
			{
				also_t *also = malloc_thing(also_t);

				also->name = clone_str(kw->value);
				also->next = conn->also;
				conn->also = also;

				DBG(DBG_CONTROL,
					DBG_log("  also=%s", kw->value)
				)
			}
			continue;
		}

		if (token < KW_CONN_FIRST || token > KW_CONN_LAST)
		{
			plog("# unsupported keyword '%s' in conn '%s'"
				, kw->entry->name, conn_name);
			cfg->err++;
			continue;
		}

		if (!assign_arg(token, KW_CONN_FIRST, kw, (char *)conn, &assigned))
		{
			plog("  bad argument value in conn '%s'", conn_name);
			cfg->err++;
			continue;
		}

		if (assigned)
			continue;

		switch (token)
		{
		case KW_TYPE:
			conn->policy &= ~(POLICY_TUNNEL | POLICY_SHUNT_MASK);
			if (streq(kw->value, "tunnel"))
			{
				conn->policy |= POLICY_TUNNEL;
			}
			else if (streq(kw->value, "beet"))
			{
				conn->policy |= POLICY_BEET;
			}
			else if (streq(kw->value, "transport_proxy"))
			{
				conn->policy |= POLICY_PROXY;
			}
			else if (streq(kw->value, "passthrough") || streq(kw->value, "pass"))
			{
				conn->policy |= POLICY_SHUNT_PASS;
			}
			else if (streq(kw->value, "drop"))
			{
				conn->policy |= POLICY_SHUNT_DROP;
			}
			else if (streq(kw->value, "reject"))
			{
				conn->policy |= POLICY_SHUNT_REJECT;
			}
			else if (strcmp(kw->value, "transport") != 0)
			{
				plog("# bad policy value: %s=%s", kw->entry->name, kw->value);
				cfg->err++;
			}
			break;
		case KW_PFS:
			KW_POLICY_FLAG("yes", "no", POLICY_PFS)
			break;
		case KW_COMPRESS:
			KW_POLICY_FLAG("yes", "no", POLICY_COMPRESS)
			break;
		case KW_PMTUDISC:
			if (streq(kw->value, "no"))
			{
				conn->xfrm_flags |= XFRM_STATE_NOPMTUDISC;
			}
			break;
		case KW_IPSECDEV:
			conn->dev = get_ifindex(kw->value);
			if (conn->dev < 0) {
				cfg->err++;
			}
			break;
		case KW_ECN:
			if (streq(kw->value, "no"))
			{
				conn->xfrm_flags |= XFRM_STATE_NOECN;
			}
			break;
		case KW_AUTH:
			KW_POLICY_FLAG("ah", "esp", POLICY_AUTHENTICATE)
			break;
		case KW_AUTHBY:
			conn->policy &= ~(POLICY_ID_AUTH_MASK | POLICY_ENCRYPT);

			if (!(streq(kw->value, "never") || streq(kw->value, "eap")))
			{
				char *value = kw->value;
				char *second = strchr(kw->value, '|');

				if (second != NULL)
				{
					*second = '\0';
				}

				/* also handles the cases secret|rsasig and rsasig|secret */
				for (;;)
				{
					if (streq(value, "rsa")   || streq(value, "rsasig")   ||
						streq(value, "ecdsa") || streq(value, "ecdsasig") ||
						streq(value, "pubkey"))
					{
						conn->policy |= POLICY_PUBKEY | POLICY_ENCRYPT;
					}
					else if (streq(value, "secret") || streq(value, "psk"))
					{
						conn->policy |= POLICY_PSK | POLICY_ENCRYPT;
					}
					else if (streq(value, "xauthrsasig"))
					{
						conn->policy |= POLICY_XAUTH_RSASIG | POLICY_ENCRYPT;
					}
					else if (streq(value, "xauthpsk"))
					{
						conn->policy |= POLICY_XAUTH_PSK | POLICY_ENCRYPT;
					}
					else
					{
						plog("# bad policy value: %s=%s", kw->entry->name, kw->value);
						cfg->err++;
						break;
					}
					if (second == NULL)
					{
						break;
					}
					value = second;
					second = NULL; /* traverse the loop no more than twice */
				}
			}
			break;
		case KW_EAP:
		{
			char *sep;

			/* check for vendor-type format */
			sep = strchr(kw->value, '-');
			if (sep)
			{
				*(sep++) = '\0';
				conn->eap_type = atoi(kw->value);
				conn->eap_vendor = atoi(sep);
				if (conn->eap_type == 0 || conn->eap_vendor == 0)
				{
					plog("# invalid EAP type: %s=%s", kw->entry->name, kw->value);
					cfg->err++;
				}
				break;
			}
			if (streq(kw->value, "aka"))
			{
				conn->eap_type = 23;
			}
			else if (streq(kw->value, "sim"))
			{
				conn->eap_type = 18;
			}
			else if (streq(kw->value, "md5"))
			{
				conn->eap_type = 4;
			}
			else if (streq(kw->value, "gtc"))
			{
				conn->eap_type = 6;
			}
			else if (streq(kw->value, "mschapv2"))
			{
				conn->eap_type = 26;
			}
			else if (streq(kw->value, "radius"))
			{	/* pseudo-type */
				conn->eap_type = 253;
			}
			else
			{
				conn->eap_type = atoi(kw->value);
				if (conn->eap_type == 0)
				{
					plog("# unknown EAP type: %s=%s", kw->entry->name, kw->value);
					cfg->err++;
				}
			}
			break;
		}
		case KW_KEYINGTRIES:
			if (streq(kw->value, "%forever"))
			{
				conn->sa_keying_tries = 0;
			}
			else
			{
				char *endptr;

				conn->sa_keying_tries = strtoul(kw->value, &endptr, 10);
				if (*endptr != '\0')
				{
					plog("# bad integer value: %s=%s", kw->entry->name, kw->value);
		    		cfg->err++;
				}
			}
			break;
		case KW_REKEY:
			KW_POLICY_FLAG("no", "yes", POLICY_DONT_REKEY)
			break;
		case KW_REAUTH:
			KW_POLICY_FLAG("no", "yes", POLICY_DONT_REAUTH)
			break;
		case KW_MOBIKE:
			KW_POLICY_FLAG("yes", "no", POLICY_MOBIKE)
			break;
		case KW_FORCEENCAPS:
			KW_POLICY_FLAG("yes", "no", POLICY_FORCE_ENCAP)
			break;
		case KW_MODECONFIG:
			KW_POLICY_FLAG("push", "pull", POLICY_MODECFG_PUSH)
			break;
		case KW_XAUTH:
			KW_POLICY_FLAG("server", "client", POLICY_XAUTH_SERVER)
			break;
		default:
			break;
		}
	}
Ejemplo n.º 8
0
void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs,
		   const struct nft_pktinfo *pkt)
{
	const struct nft_fib *priv = nft_expr_priv(expr);
	int noff = skb_network_offset(pkt->skb);
	u32 *dest = &regs->data[priv->dreg];
	struct iphdr *iph, _iph;
	struct fib_result res;
	struct flowi4 fl4 = {
		.flowi4_scope = RT_SCOPE_UNIVERSE,
		.flowi4_iif = LOOPBACK_IFINDEX,
	};
	const struct net_device *oif;
	struct net_device *found;
#ifdef CONFIG_IP_ROUTE_MULTIPATH
	int i;
#endif

	/*
	 * Do not set flowi4_oif, it restricts results (for example, asking
	 * for oif 3 will get RTN_UNICAST result even if the daddr exits
	 * on another interface.
	 *
	 * Search results for the desired outinterface instead.
	 */
	if (priv->flags & NFTA_FIB_F_OIF)
		oif = nft_out(pkt);
	else if (priv->flags & NFTA_FIB_F_IIF)
		oif = nft_in(pkt);
	else
		oif = NULL;

	if (nft_hook(pkt) == NF_INET_PRE_ROUTING &&
	    nft_fib_is_loopback(pkt->skb, nft_in(pkt))) {
		nft_fib_store_result(dest, priv, pkt,
				     nft_in(pkt)->ifindex);
		return;
	}

	iph = skb_header_pointer(pkt->skb, noff, sizeof(_iph), &_iph);
	if (!iph) {
		regs->verdict.code = NFT_BREAK;
		return;
	}

	if (ipv4_is_zeronet(iph->saddr)) {
		if (ipv4_is_lbcast(iph->daddr) ||
		    ipv4_is_local_multicast(iph->daddr)) {
			nft_fib_store_result(dest, priv, pkt,
					     get_ifindex(pkt->skb->dev));
			return;
		}
	}

	if (priv->flags & NFTA_FIB_F_MARK)
		fl4.flowi4_mark = pkt->skb->mark;

	fl4.flowi4_tos = iph->tos & DSCP_BITS;

	if (priv->flags & NFTA_FIB_F_DADDR) {
		fl4.daddr = iph->daddr;
		fl4.saddr = get_saddr(iph->saddr);
	} else {
		fl4.daddr = iph->saddr;
		fl4.saddr = get_saddr(iph->daddr);
	}

	*dest = 0;

	if (fib_lookup(nft_net(pkt), &fl4, &res, FIB_LOOKUP_IGNORE_LINKSTATE))
		return;

	switch (res.type) {
	case RTN_UNICAST:
		break;
	case RTN_LOCAL: /* Should not see RTN_LOCAL here */
		return;
	default:
		break;
	}

       if (!oif) {
               found = FIB_RES_DEV(res);
               goto ok;
       }

#ifdef CONFIG_IP_ROUTE_MULTIPATH
	for (i = 0; i < res.fi->fib_nhs; i++) {
		struct fib_nh *nh = &res.fi->fib_nh[i];

		if (nh->nh_dev == oif) {
			found = nh->nh_dev;
			goto ok;
		}
	}
	return;
#else
	found = FIB_RES_DEV(res);
	if (found != oif)
		return;
#endif
ok:
	switch (priv->result) {
	case NFT_FIB_RESULT_OIF:
		*dest = found->ifindex;
		break;
	case NFT_FIB_RESULT_OIFNAME:
		strncpy((char *)dest, found->name, IFNAMSIZ);
		break;
	default:
		WARN_ON_ONCE(1);
		break;
	}
}