Example #1
0
// Called by go code
int ipv4_route_init(uint32_t nb_entries)
{
    uint32_t i;
    uint32_t bcast_ip = IPv4(0xff, 0xff, 0xff, 0xff);
    ipv4_route_table = (mtrie_t*) malloc (sizeof(mtrie_t) * nb_entries);
    if (!ipv4_route_table)
        return -EAGAIN;

    bcast_nh.data = NULL;
    bcast_nh.fn = bcast_pkt_handler;

    for (i = 0; i < nb_entries; i++) {
        mtrie_init(&ipv4_route_table[i], 3);
        ipv4_route_add(i, (uint8_t*)&bcast_ip, &bcast_nh);
    }

    return 0;
}
Example #2
0
// Attach a VIF to a vrf
struct vif* vif_add(char* name, uint8_t* ip, uint8_t mask, uint8_t* macaddr,
    uint32_t label, char* path, int cpus, int cpusets[])
{
    int i;
    struct vif* vif = (struct vif*) malloc (sizeof(struct vif));
    if (!vif) {
        log_crit("Failed to allocated memory for vif struct (%s)\n", name);
        return NULL;
    }

    strcpy(vif->name, name);
    vif->label = label;
    vif->mask = mask;
    memcpy(vif->ip, ip, 4);
    memcpy(vif->macaddr, macaddr, 4);
    strcpy(vif->path, path);
    rte_atomic64_clear(&vif->rx_packets);
    rte_atomic64_clear(&vif->tx_packets);
    rte_atomic64_clear(&vif->dropped_packets);
    rte_atomic64_clear(&vif->error_packets);

    vif->cpus = cpus;
    for (i = 0; i < cpus; i++) {
        CPU_ZERO(&vif->cpusets[i]);
        CPU_SET(cpusets[i], &vif->cpusets[i]);
    }

    vif->lldev = NULL;

    // Add route to this VM in VRF/RIB.
    vif->nh.data = vif;
    vif->nh.fn = virtio_tx_packet;
    ipv4_route_add(label, ip, &vif->nh);

    /* Create VHOST-User socket */
    unlink(vif->path);
    if (rte_vhost_driver_register(vif->path) < 0) {
        free(vif);
        return NULL;
    }

    return vif;
}
Example #3
0
void if_simple_setup(ifnet *interface, int addr, int netmask, int bcast, int net, int router, int def_router)
{
    ifaddr *address;

    //addr = htonl(addr);
    //netmask = htonl(netmask);
    //bcast = htonl(bcast);

    // set the ip address for this net interface
    address = malloc(sizeof(ifaddr));
    address->addr.len = 4;
    address->addr.type = ADDR_TYPE_IP;
    //NETADDR_TO_IPV4(address->addr) = htonl(addr);
    NETADDR_TO_IPV4(address->addr) = addr;

    address->netmask.len = 4;
    address->netmask.type = ADDR_TYPE_IP;
    //NETADDR_TO_IPV4(address->netmask) = htonl(netmask);
    NETADDR_TO_IPV4(address->netmask) = netmask;

    address->broadcast.len = 4;
    address->broadcast.type = ADDR_TYPE_IP;
    //NETADDR_TO_IPV4(address->broadcast) = htonl(bcast);
    NETADDR_TO_IPV4(address->broadcast) = bcast;

    if_bind_address(interface, address);

#if 1
    printf("if a ");
    dump_ipv4_addr(addr);
    printf(" mask ");
    dump_ipv4_addr(netmask);
    printf(" broad ");
    dump_ipv4_addr(bcast);
    printf("\n");
#endif

    // set up an initial routing table
    int rc;

    if( (rc = ipv4_route_add( net, netmask, router, interface->id) ) )
    {
        SHOW_ERROR( 1, "Adding route - failed, rc = %d", rc);
    }
    else
    {
        SHOW_INFO0( 2, "Adding route - ok");
    }


    SHOW_INFO0( 2, "Adding default route...");
    if( (rc = ipv4_route_add_default( router, interface->id, def_router ) ) )
    {
        SHOW_ERROR( 1, "Adding route - failed, rc = %d", rc);
    }
    else
    {
        SHOW_INFO0( 2, "Adding route - ok");
    }

#if 0
    hal_start_kernel_thread_arg( bootp_thread, interface );
#else
    // Now try to get something real :)
    bootp(interface);
#endif
}
Example #4
0
int cmd_ipcfg(FILE *f, int argc, char ** argv)
{
	struct ifnet * ifn;
	struct route * rt;
	in_addr_t ipv4_addr;
	in_addr_t ipv4_mask = INADDR_ANY;
	in_addr_t gw_addr = INADDR_ANY;
	int use_dhcp = 0;
	int change = 0;
	char s[64];
	char ip[16];
	char * env;
	char * cp;

	if (argc > 1)
		return SHELL_ERR_EXTRA_ARGS;

	fprintf(f, "\n** IP configuration utility **\n\n");

	/* get current configuration */
	if ((ifn = get_ifn_byname("eth0")) == NULL)
		return -1;

	ifn_ipv4_get(ifn, &ipv4_addr, &ipv4_mask);
	
	if ((rt = ipv4_route_get(INADDR_ANY, INADDR_ANY)) == NULL)
		gw_addr = INADDR_ANY;
	else
		gw_addr = rt->rt_gateway;
	use_dhcp = 0;

	/* get environment variable */
	if ((env = getenv("IPCFG")) != NULL) {
		strcpy(s, env);

		if (!inet_aton(strtok(s, " ,"), (struct in_addr *)&ipv4_addr)) {
			return -1;
		}

		if (inet_aton(strtok(NULL, " ,"), (struct in_addr *)&ipv4_mask)) {
			if (inet_aton(strtok(NULL, " ,"), (struct in_addr *)&gw_addr)) {
				use_dhcp = strtoul(strtok(NULL, " , "), NULL, 0);
			}
		}
	} 

	for(;;) {
		fprintf(f, " - IP addr(%s): ", 
		   inet_ntop(AF_INET, (void *)&ipv4_addr, ip, 16));
		fgets(s, 32, f);
		if (s[0] == '\n')
			break;
		if (inet_aton(s, (struct in_addr *)&ipv4_addr)) {
			change++;
			break;
		}
	}

	for(;;) {
		fprintf(f, " - Network mask(%s): ", 
		   inet_ntop(AF_INET, (void *)&ipv4_mask, ip, 16));
		fgets(s, 32, f);
		if (s[0] == '\n')
			break;
		if (inet_aton(s, (struct in_addr *)&ipv4_mask)) {
			change++;
			break;
		}
	}

	for (;;) {
		fprintf(f, " - Gateway addr(%s): ", 
		   inet_ntop(AF_INET, (void *)&gw_addr, ip, 16));
		fgets(s, 32, f);
		if (s[0] == '\n')
			break;
		if (inet_aton(s, (struct in_addr *)&gw_addr)) {
			change++;
			break;	
		}
	} 

#if 0
	for (;;) {
		int c;

		fprintf(f, " - Enable DHCP [y/n]? (%c): ", use_dhcp ? 'y' : 'n');
		fgets(s, 32, f);
		if (s[0] == '\n')
			break;
		c = tolower(s[0]);
		if ((c == 'y') || (c == 'n')) {
			use_dhcp = (c == 'y') ? 1 : 0;
			change++;
			break;
		}
	};
#endif

	if (!change) {
		fprintf(f, "\nKeeping current configuration.\n");
		return 0;
	}

	cp = s + sprintf(s, "%s", inet_ntop(AF_INET, (void *)&ipv4_addr, ip, 16));
	cp += sprintf(cp, " %s", inet_ntop(AF_INET, (void *)&ipv4_mask, ip, 16));
	cp += sprintf(cp, " %s", inet_ntop(AF_INET, (void *)&gw_addr, ip, 16));
	sprintf(cp, " %d", use_dhcp);

	if (setenv("IPCFG", s, 1) < 0) {
		fprintf(f, "setenv() error!\n");
		return -1; 
	}

	fprintf(f, "\nConfiguration saved.\n");

	ifn_ipv4_set(ifn, ipv4_addr, ipv4_mask);
	/* set the default route */
	ipv4_route_del(INADDR_ANY);
	ipv4_route_add(INADDR_ANY, INADDR_ANY, gw_addr, ifn);


#if 0
	fprintf(f, "\n - Restart the system [y/n]? ");
	fgets(s, 32, f);

	if ((tolower(s[0]) == 'y')) {
		cmd_reboot(f, 0, NULL);
	};
#endif

	return 0;
}
Example #5
0
errno_t bootp(ifnet *iface)
{
    struct bootp_state          _bstate;
    void *			udp_sock;

    struct bootp_state          *bstate = &_bstate;

    memset( &_bstate, 0, sizeof(struct bootp_state) );

    _bstate.expected_dhcpmsgtype = -1;


    int err = iface->dev->dops.get_address(iface->dev, &_bstate.mac_addr, sizeof(_bstate.mac_addr));
    if(err < 0) {
        SHOW_ERROR0( 0, "can't get interface MAC address");
        return ENXIO;
    }


    if( xid == 0 )
        xid = (int)time(0) ^ 0x1E0A4F; // Some strange number :)

    int tries = 3;
    errno_t e;

    do {
        if( udp_open(&udp_sock) )
        {
            SHOW_ERROR0( 0, "UDP - can't prepare endpoint");
            return ENOTSOCK;
        }

        e = do_bootp( bstate, udp_sock, 0);

        udp_close(udp_sock);

    } while( e && (tries-- > 0) );


    if(e)
        SHOW_ERROR( 0, "error %d", e);
    else
    {
        SHOW_FLOW( 1, "DHCP netmask: 0x%08X", bstate->smask);

        SHOW_FLOW( 1, "DHCP ip:      %s", inet_ntoa(bstate->myip) );
        SHOW_FLOW( 2, "gateway ip:   %s", inet_ntoa(bstate->gateip) );
        SHOW_FLOW( 2, "root ip:      %s", inet_ntoa(bstate->rootip) );
        SHOW_FLOW( 2, "server ip:    %s", inet_ntoa(bstate->servip) );

        SHOW_FLOW( 2, "rootpath:     '%s'", bstate->rootpath );
        SHOW_FLOW( 2, "hostname:     '%s'", bstate->hostname );
        SHOW_FLOW( 2, "bootfile:     '%s'", bstate->bootfile );

        // Now apply it to interface

        ifaddr *address;

        // set the ip address for this net interface
        address = malloc(sizeof(ifaddr));

        address->addr.len = 4;
        address->addr.type = ADDR_TYPE_IP;
        NETADDR_TO_IPV4(address->addr) = bstate->myip.s_addr;

        address->netmask.len = 4;
        address->netmask.type = ADDR_TYPE_IP;
        NETADDR_TO_IPV4(address->netmask) = bstate->smask;

        u_int32_t bcast = (bstate->myip.s_addr) | bstate->smask;

        address->broadcast.len = 4;
        address->broadcast.type = ADDR_TYPE_IP;
        NETADDR_TO_IPV4(address->broadcast) = bcast;

        if_bind_address(iface, address);

        u_int32_t net = (bstate->myip.s_addr) & ~(bstate->smask);


        int rc;
        if( (rc = ipv4_route_add( net, ~(bstate->smask), bstate->myip.s_addr, iface->id) ) )
        {
            SHOW_ERROR( 1, "Adding route - failed, rc = %d", rc);
        }
        else
        {
            SHOW_INFO0( 1, "Adding route - ok");
        }

        if( (rc = ipv4_route_add_default( bstate->myip.s_addr, iface->id, bstate->gateip.s_addr ) ) )
        {
            SHOW_ERROR( 1, "Adding default route - failed, rc = %d", rc);
        }
        else
        {
            SHOW_INFO0( 1, "Adding default route - ok");
        }

        // At least one char!
        if(*bstate->hostname)
            strlcpy( phantom_uname.nodename, bstate->hostname, _UTSNAME_NODENAME_LENGTH );

    }


    return e;
}
Example #6
0
int network_config(void)
{
	struct ifnet * ifn;
	in_addr_t ip_addr;
	in_addr_t netmask = INADDR_ANY;
	in_addr_t gw_addr = INADDR_ANY;
	char s[64];
	char s1[16];
	char s2[16];
	char * env;
	uint8_t * ethaddr;
	uint64_t esn;
	int dhcp;

	/* Initialize MAC address with the MCU's UID */
	ethaddr = (uint8_t *)&esn;
	esn = *((uint64_t *)STM32F_UID);
	DCC_LOG2(LOG_TRACE, "ESN=0x%08x%08x", esn >> 32, esn);
	ethaddr[0] = (ethaddr[0] & 0xfc) | 0x02; /* Locally administered MAC */


	DCC_LOG(LOG_TRACE, "tcpip_init().");
	tcpip_init();

	if ((env = getenv("IPCFG")) == NULL) {
		tracef("IPCFG not set, using defaults!\n");
		/* default configuration */
		strcpy(s, "192.168.0.128 255.255.255.0 192.168.0.1 0");
		/* set the default configuration */
		setenv("IPCFG", s, 1);
	} else {
		strcpy(s, env);
	}

	if ((env = getenv("ETHADDR")) != NULL) {
		eth_strtomac(ethaddr, env);
	} else {
		trace("Ethernet MAC address not set, using defaults!");
		DCC_LOG(LOG_WARNING, "Ethernet MAC address not set.");
	}

    tracef("* mac addr: %02x-%02x-%02x-%02x-%02x-%02x",
		   ethaddr[0], ethaddr[1], ethaddr[2],
		   ethaddr[3], ethaddr[4], ethaddr[5]);

	if (!inet_aton(strtok(s, " ,"), (struct in_addr *)&ip_addr)) {
		DCC_LOG(LOG_WARNING, "inet_aton() failed.");
		return -1;
	}

	if (inet_aton(strtok(NULL, " ,"), (struct in_addr *)&netmask)) {
		if (inet_aton(strtok(NULL, " ,"), (struct in_addr *)&gw_addr)) {
			dhcp = strtoul(strtok(NULL, ""), NULL, 0);
		}
	}

	/* initialize the Ethernet interface */
	/* configure the ip address */
	ifn = ethif_init(ethaddr, ip_addr, netmask);
//	ifn = loopif_init(ip_addr, netmask);

	ifn_getname(ifn, s);
	ifn_ipv4_get(ifn, &ip_addr, &netmask);
	tracef("* netif %s: %s, %s", s, 
		   inet_ntop(AF_INET, (void *)&ip_addr, s1, 16),
		   inet_ntop(AF_INET, (void *)&netmask, s2, 16));

	if (gw_addr != INADDR_ANY) {
		/* add the default route (gateway) to ethif */
		ipv4_route_add(INADDR_ANY, INADDR_ANY, gw_addr, ifn);
		tracef("* default route gw: %s", 
			   inet_ntop(AF_INET, (void *)&gw_addr, s1, 16));
	}

	if (dhcp) {
#if 0
		/* configure the initial ip address */
		dhcp_start();
		/* schedule the interface to be configured through dhcp */
		dhcp_ifconfig(ethif, dhcp_callback);
		tracef("DHCP started.\n");
#endif
	}

	return 0;
}