예제 #1
0
파일: vrf.c 프로젝트: AT-Corp/quagga-atc
/* Get a VRF. If not found, create one. */
static struct vrf *
vrf_get (vrf_id_t vrf_id)
{
  struct prefix p;
  struct route_node *rn;
  struct vrf *vrf;

  vrf_build_key (vrf_id, &p);
  rn = route_node_get (vrf_table, &p);
  if (rn->info)
    {
      vrf = (struct vrf *)rn->info;
      route_unlock_node (rn); /* get */
      return vrf;
    }

  vrf = XCALLOC (MTYPE_VRF, sizeof (struct vrf));
  vrf->vrf_id = vrf_id;
  vrf->fd = -1;
  rn->info = vrf;

  /* Initialize interfaces. */
  if_init (vrf_id, &vrf->iflist);

  zlog_info ("VRF %u is created.", vrf_id);

  if (vrf_master.vrf_new_hook)
    (*vrf_master.vrf_new_hook) (vrf_id, &vrf->info);

  return vrf;
}
예제 #2
0
파일: slirp.c 프로젝트: AlexandreCo/macemu
int slirp_init(void)
{
    //    debug_init("/tmp/slirp.log", DEBUG_DEFAULT);
    
#ifdef _WIN32
    {
        WSADATA Data;
        WSAStartup(MAKEWORD(2,0), &Data);
	atexit(slirp_cleanup);
    }
#endif

    link_up = 1;

    if_init();
    ip_init();

    /* Initialise mbufs *after* setting the MTU */
    m_init();

    /* set default addresses */
    inet_aton("127.0.0.1", &loopback_addr);

    if (get_dns_addr(&dns_addr) < 0)
        return -1;

    inet_aton(CTL_SPECIAL, &special_addr);
	alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
	getouraddr();
    return 0;
}
예제 #3
0
파일: bgp_zebra.c 프로젝트: DorChen/quagga
void
bgp_zebra_init (void)
{
  /* Set default values. */
  zclient = zclient_new ();
  zclient_init (zclient, ZEBRA_ROUTE_BGP);
  zclient->router_id_update = bgp_router_id_update;
  zclient->interface_add = bgp_interface_add;
  zclient->interface_delete = bgp_interface_delete;
  zclient->interface_address_add = bgp_interface_address_add;
  zclient->interface_address_delete = bgp_interface_address_delete;
  zclient->ipv4_route_add = zebra_read_ipv4;
  zclient->ipv4_route_delete = zebra_read_ipv4;
  zclient->interface_up = bgp_interface_up;
  zclient->interface_down = bgp_interface_down;
#ifdef HAVE_IPV6
  zclient->ipv6_route_add = zebra_read_ipv6;
  zclient->ipv6_route_delete = zebra_read_ipv6;
#endif /* HAVE_IPV6 */

  /* Interface related init. */
  if_init ();

  bgp_nexthop_buf = stream_new(BGP_NEXTHOP_BUF_SIZE);
}
예제 #4
0
void ospf_if_init()
{
	/* Initialize Zebra interface data structure. */
	if_init();
	om->iflist = iflist;
	if_add_hook(IF_NEW_HOOK, ospf_if_new_hook);
	if_add_hook(IF_DELETE_HOOK, ospf_if_delete_hook);
}
예제 #5
0
void slirp_init(int restricted, const char *special_ip)
{
#if DEBUG
    int   slirp_logmask = 0;
    char  slirp_logfile[512];
    {
        const char*  env = getenv( "ANDROID_SLIRP_LOGMASK" );
        if (env != NULL)
            slirp_logmask = atoi(env);
         else if (VERBOSE_CHECK(slirp))
            slirp_logmask = DEBUG_DEFAULT;
    }

    {
        char*  p   = slirp_logfile;
        char*  end = p + sizeof(slirp_logfile);

        p = bufprint_temp_file( p, end, "slirp.log" );
        if (p >= end) {
            dprint( "cannot create slirp log file in temporary directory" );
            slirp_logmask = 0;
        }
    }
    if (slirp_logmask) {
        dprint( "sending slirp logs with mask %x to %s", slirp_logmask, slirp_logfile );
        debug_init( slirp_logfile, slirp_logmask );
    }
#endif

    link_up = 1;
    slirp_restrict = restricted;

    if_init();
    ip_init();

    
    m_init();

    
    inet_strtoip("127.0.0.1", &loopback_addr_ip);

    if (dns_addr_count == 0) {
        if (slirp_get_system_dns_servers() < 0) {
            dns_addr[0]    = loopback_addr_ip;
            dns_addr_count = 1;
            fprintf (stderr, "Warning: No DNS servers found\n");
        }
    }

    inet_strtoip(CTL_SPECIAL, &special_addr_ip);

    alias_addr_ip = special_addr_ip | CTL_ALIAS;
    getouraddr();
    register_savevm("slirp", 0, 1, slirp_state_save, slirp_state_load, NULL);

    slirp_net_forward_init();
}
예제 #6
0
int
main() {
	FD_ZERO(&allset);
	if_init();
	un_init();
	cache_init();
	arpModel();
	return 0;
}
예제 #7
0
파일: routenif.c 프로젝트: aunali1/exopc
static void
ip_init_table() {
  ip_table->hostname[0] = 0;
  ip_table->domainname[0] = 0;
  bzero(ip_table,sizeof(ip_table_t));

  if_init();
  route_init();

}
예제 #8
0
/* Allocate and initialize interface vector. */
void
stp_if_init (void)
{
  /* Default initial size of interface vector. */
  if_init();
  if_add_hook (IF_NEW_HOOK, stp_interface_new_hook);
  if_add_hook (IF_DELETE_HOOK, stp_interface_delete_hook);
  
  /* Install interface node. */
  install_node (&interface_node, stp_interface_config_write);

  /* Install commands. */
  install_element (CONFIG_NODE, &interface_cmd);
  install_element (CONFIG_NODE, &no_interface_cmd);
  install_default (INTERFACE_NODE);
  install_element (INTERFACE_NODE, &interface_desc_cmd);
  install_element (INTERFACE_NODE, &no_interface_desc_cmd);
}
예제 #9
0
void slirp_init(int restricted, char *special_ip)
{
#if defined(DEBUG)
    debug_init("/tmp/slirp.log", DEBUG_DEFAULT);
#endif

#ifdef _WIN32
    {
        WSADATA Data;
        WSAStartup(MAKEWORD(2,0), &Data);
	atexit(slirp_cleanup);
    }
#endif

    link_up = 1;
    slirp_restrict = restricted;

    if_init();
    ip_init();

    /* Initialise mbufs *after* setting the MTU */
    m_init();

    /* set default addresses */
    inet_aton("127.0.0.1", &loopback_addr);

    if (get_dns_addr(&dns_addr) < 0) {
        dns_addr = loopback_addr;
        fprintf (stderr, "Warning: No DNS servers found\n");
    }

    if (special_ip)
        slirp_special_ip = special_ip;

    inet_aton(slirp_special_ip, &special_addr);
    alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
    getouraddr();
    register_savevm("slirp", 0, 1, slirp_state_save, slirp_state_load, NULL);
}
예제 #10
0
void slirp_init(int restricted, const char *special_ip)
{
    

#ifdef _WIN32
    {
        WSADATA Data;
        WSAStartup(MAKEWORD(2,0), &Data);
	atexit(slirp_cleanup);
    }
#endif

    link_up = 1;
    slirp_restrict = restricted;

    if_init();
    ip_init();

    
    m_init();

    
    inet_aton("127.0.0.1", &loopback_addr);

    if (get_dns_addr(&dns_addr) < 0) {
        dns_addr = loopback_addr;
        fprintf (stderr, "Warning: No DNS servers found\n");
    }

    if (special_ip)
        slirp_special_ip = special_ip;

    inet_aton(slirp_special_ip, &special_addr);
    alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS);
    getouraddr();
    register_savevm("slirp", 0, 1, slirp_state_save, slirp_state_load, NULL);
}
void pim_if_init()
{
  if_init();
}
예제 #12
0
파일: cgdb.c 프로젝트: i4fumi/cgdb
int main(int argc, char *argv[])
{
/* Uncomment to debug and attach */
#if 0
    int c;

    read(0, &c, 1);
#endif

    parse_long_options(&argc, &argv);

    current_line = ibuf_init();

    cgdbrc_init();

    if (create_and_init_pair() == -1) {
        fprintf(stderr, "%s:%d Unable to create PTY pair", __FILE__, __LINE__);
        exit(-1);
    }

    /* First create tgdb, because it has the error log */
    if (start_gdb(argc, argv) == -1) {
        fprintf(stderr, "%s:%d Unable to invoke GDB", __FILE__, __LINE__);
        exit(-1);
    }

    /* From here on, the logger is initialized */

    /* Create the home directory */
    if (init_home_dir() == -1) {
        logger_write_pos(logger, __FILE__, __LINE__,
                "Unable to create home dir ~/.cgdb");
        cleanup();
        exit(-1);
    }

    if (init_readline() == -1) {
        logger_write_pos(logger, __FILE__, __LINE__, "Unable to init readline");
        cleanup();
        exit(-1);
    }

    if (tty_cbreak(STDIN_FILENO, &term_attributes) == -1) {
        logger_write_pos(logger, __FILE__, __LINE__, "tty_cbreak error");
        cleanup();
        exit(-1);
    }

    if (init_kui() == -1) {
        logger_write_pos(logger, __FILE__, __LINE__, "init_kui error");
        cleanup();
        exit(-1);
    }

    /* Initialize the display */
    switch (if_init()) {
        case 1:
            logger_write_pos(logger, __FILE__, __LINE__,
                    "Unable to initialize the curses library");
            cleanup();
            exit(-1);
        case 2:
            logger_write_pos(logger, __FILE__, __LINE__,
                    "Unable to handle signal: SIGWINCH");
            cleanup();
            exit(-1);
        case 3:
            logger_write_pos(logger, __FILE__, __LINE__,
                    "Unable to setup highlighting groups");
            cleanup();
            exit(-1);
        case 4:
            logger_write_pos(logger, __FILE__, __LINE__,
                    "New GDB window failed -- out of memory?");
            cleanup();
            exit(-1);
    }

    /* Initialize the pipe that is used for resize */
    if (init_resize_pipe() == -1) {
        logger_write_pos(logger, __FILE__, __LINE__, "init_resize_pipe error");
        cleanup();
        exit(-1);
    }

    {
        char config_file[FSUTIL_PATH_MAX];
        FILE *config;

        fs_util_get_path(cgdb_home_dir, "cgdbrc", config_file);
        config = fopen(config_file, "r");
        if (config) {
            command_parse_file(config);
            fclose(config);
        }
    }

    /* Enter main loop */
    main_loop();

    /* Shut down curses and exit */
    cleanup();
    return 0;
}
예제 #13
0
uint_least32_t sysclock(uint_least32_t clock)
{
  static uint_least32_t current_clock=12000000UL;
  uint_least32_t i, pllclksel=0, pllctrl=0;

  //get clock
  if(clock == 0)
  {
    return current_clock;
  }

  //set clock
  //Fclkout = M * Fclkin = FCCO / (2 * P)
  //FCCO = 2 * P * Fclkout
  //  Fclkin  Fclkout  MSEL   Mdiv PSEL Pdiv FCCO
  //  12 MHz  24 MHz   00001  2    10   4    192 MHz
  //  12 MHz  36 MHz   00010  3    10   4    288 MHz
  //  12 MHz  48 MHz   00011  4    01   2    192 MHz
  //  16 MHz  32 MHz   00001  2    10   4    256 MHz
  //  16 MHz  48 MHz   00010  3    01   2    192 MHz
  switch(clock)
  {
    default:
    case 12000000UL:
      current_clock = clock;
      pllclksel = (0x0<<0); //IRC osc
      pllctrl   = 0;
      break;
    case 16000000UL:
      current_clock = clock;
      pllclksel = (0x1<<0); //SYS osc
      pllctrl   = 0;
      break;
    case 24000000UL:
      current_clock = clock;
      pllclksel = (0x0<<0); //IRC osc
      pllctrl   = (0x1<<0) | (0x2<<5);
      break;
    case 32000000UL:
      current_clock = clock;
      pllclksel = (0x1<<0); //SYS osc
      pllctrl   = (0x1<<0) | (0x2<<5);
      break;
    case 36000000UL:
      current_clock = clock;
      pllclksel = (0x0<<0); //IRC osc
      pllctrl   = (0x2<<0) | (0x2<<5);
      break;
    case 48000000UL:
      current_clock = clock;
      pllclksel = (0x1<<0); //SYS osc
      pllctrl   = (0x2<<0) | (0x1<<5);
      break;
  }

  DISABLE_IRQ();

  if(pllclksel != 0) //SYS osc
  {
    LPC_SYSCON->PDRUNCFG &= ~(1<<5); //enable SYS osc
    LPC_SYSCON->SYSOSCCTRL = (0<<0); //0-20MHz
  }
  else //IRC osc
  {
    LPC_SYSCON->PDRUNCFG &= ~((1<<0)|(1<<1)); //enable IRC osc
  }
  for(i=0; i<200; i++){ NOP(); }
  LPC_SYSCON->SYSPLLCLKSEL = pllclksel;
  LPC_SYSCON->SYSPLLCLKUEN = (1<<0); //update clock source
  LPC_SYSCON->SYSPLLCLKUEN = (0<<0); //toggle update register
  LPC_SYSCON->SYSPLLCLKUEN = (1<<0); //update clock source again
  while(!(LPC_SYSCON->SYSPLLCLKUEN & (1<<0))); //wait until updated

  if(pllctrl != 0) // PLL on
  {
    LPC_SYSCON->SYSPLLCTRL = pllctrl;
    LPC_SYSCON->PDRUNCFG &= ~(1<<7); //enable PLL
    while(!(LPC_SYSCON->SYSPLLSTAT & (1<<0))); //wait for PLL to lock
    LPC_SYSCON->MAINCLKSEL = (0x3<<0); //select PLL
    LPC_SYSCON->MAINCLKUEN = (1<<0); //update clock source
    LPC_SYSCON->MAINCLKUEN = (0<<0); //toggle update reg
    LPC_SYSCON->MAINCLKUEN = (1<<0); //update clock source again
    while(!(LPC_SYSCON->MAINCLKUEN & (1<<0))); //wait until updated
  }
  else
  {
    LPC_SYSCON->MAINCLKSEL = pllclksel; //select IRC osc
    LPC_SYSCON->MAINCLKUEN = (1<<0); //update clock source
    LPC_SYSCON->MAINCLKUEN = (0<<0); //toggle update reg
    LPC_SYSCON->MAINCLKUEN = (1<<0); //update clock source again
    while(!(LPC_SYSCON->MAINCLKUEN & (1<<0))); //wait until updated
    LPC_SYSCON->PDRUNCFG |= (1<<7); //disable PLL
  }

  if(pllclksel != 0) //SYS osc
  {
    LPC_SYSCON->PDRUNCFG |= ((1<<0)|(1<<1)); //disable IRC osc
  }
  else //IRC osc
  {
    LPC_SYSCON->PDRUNCFG |= (1<<5); //disable SYS osc
  }

  //set prescaler for backlight PWM
  if(current_clock >= 40000000UL)
  {
    LPC_TMR16B1->PR = 2; //div 4
  }
  else if(current_clock >= 20000000UL)
  {
    LPC_TMR16B1->PR = 1; //div 2
  }
  else
  {
    LPC_TMR16B1->PR = 0; //no prescaler
  }
  LPC_TMR16B1->PC = 0; //reset prescaler

  //set prescaler for adc
  LPC_ADC->CR = (((current_clock/4000000UL)-1)<<8) | (LPC_ADC->CR&0xFFFF00FF);  //stop

  //re-init systick timer
  SysTick_Config(current_clock / 1000); //1000 Hz

  //re-init interface
  if_init(INTERFACE_RESET);

  ENABLE_IRQ();

  return current_clock;
}
예제 #14
0
파일: net.c 프로젝트: enukane/netbsd-src
struct if_head *
discover_interfaces(struct dhcpcd_ctx *ctx, int argc, char * const *argv)
{
	struct ifaddrs *ifaddrs, *ifa;
	char *p;
	int i, sdl_type;
	struct if_head *ifs;
	struct interface *ifp;
#ifdef __linux__
	char ifn[IF_NAMESIZE];
#endif
#ifdef INET
	const struct sockaddr_in *addr;
	const struct sockaddr_in *net;
	const struct sockaddr_in *dst;
#endif
#ifdef INET6
	const struct sockaddr_in6 *sin6;
	int ifa_flags;
#endif
#ifdef AF_LINK
	const struct sockaddr_dl *sdl;
#ifdef SIOCGIFPRIORITY
	struct ifreq ifr;
	int s_inet;
#endif
#ifdef IFLR_ACTIVE
	struct if_laddrreq iflr;
	int s_link;
#endif

#ifdef SIOCGIFPRIORITY
	if ((s_inet = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
		return NULL;
#endif
#ifdef IFLR_ACTIVE
	if ((s_link = socket(AF_LINK, SOCK_DGRAM, 0)) == -1) {
#ifdef SIOCGIFPRIORITY
		close(s_inet);
#endif
		return NULL;
	}
	memset(&iflr, 0, sizeof(iflr));
#endif
#elif AF_PACKET
	const struct sockaddr_ll *sll;
#endif

	if (getifaddrs(&ifaddrs) == -1)
		return NULL;
	ifs = malloc(sizeof(*ifs));
	if (ifs == NULL)
		return NULL;
	TAILQ_INIT(ifs);

	for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
		if (ifa->ifa_addr != NULL) {
#ifdef AF_LINK
			if (ifa->ifa_addr->sa_family != AF_LINK)
				continue;
#elif AF_PACKET
			if (ifa->ifa_addr->sa_family != AF_PACKET)
				continue;
#endif
		}

		/* Ensure that the interface name has settled */
		if (!dev_initialized(ctx, ifa->ifa_name))
			continue;

		/* It's possible for an interface to have >1 AF_LINK.
		 * For our purposes, we use the first one. */
		TAILQ_FOREACH(ifp, ifs, next) {
			if (strcmp(ifp->name, ifa->ifa_name) == 0)
				break;
		}
		if (ifp)
			continue;
		if (argc > 0) {
			for (i = 0; i < argc; i++) {
#ifdef __linux__
				/* Check the real interface name */
				strlcpy(ifn, argv[i], sizeof(ifn));
				p = strchr(ifn, ':');
				if (p)
					*p = '\0';
				if (strcmp(ifn, ifa->ifa_name) == 0)
					break;
#else
				if (strcmp(argv[i], ifa->ifa_name) == 0)
					break;
#endif
			}
			if (i == argc)
				continue;
			p = argv[i];
		} else {
			p = ifa->ifa_name;
			/* -1 means we're discovering against a specific
			 * interface, but we still need the below rules
			 * to apply. */
			if (argc == -1 && strcmp(argv[0], ifa->ifa_name) != 0)
				continue;
		}
		for (i = 0; i < ctx->ifdc; i++)
			if (!fnmatch(ctx->ifdv[i], p, 0))
				break;
		if (i < ctx->ifdc)
			continue;
		for (i = 0; i < ctx->ifac; i++)
			if (!fnmatch(ctx->ifav[i], p, 0))
				break;
		if (ctx->ifac && i == ctx->ifac)
			continue;

		if (if_vimaster(ifa->ifa_name) == 1) {
			syslog(argc ? LOG_ERR : LOG_DEBUG,
				"%s: is a Virtual Interface Master, skipping",
				ifa->ifa_name);
			continue;
		}

		ifp = calloc(1, sizeof(*ifp));
		if (ifp == NULL) {
			syslog(LOG_ERR, "%s: %m", __func__);
			break;
		}
		ifp->ctx = ctx;
		strlcpy(ifp->name, p, sizeof(ifp->name));
		ifp->flags = ifa->ifa_flags;

		/* Bring the interface up if not already */
		if (!(ifp->flags & IFF_UP)
#ifdef SIOCGIFMEDIA
		    && carrier_status(ifp) != LINK_UNKNOWN
#endif
		   )
		{
			if (up_interface(ifp) == 0)
				ctx->options |= DHCPCD_WAITUP;
			else
				syslog(LOG_ERR, "%s: up_interface: %m",
				    ifp->name);
		}

		sdl_type = 0;
		/* Don't allow loopback unless explicit */
		if (ifp->flags & IFF_LOOPBACK) {
			if (argc == 0 && ctx->ifac == 0) {
				free_interface(ifp);
				continue;
			}
		} else if (ifa->ifa_addr != NULL) {
#ifdef AF_LINK
			sdl = (const struct sockaddr_dl *)(void *)ifa->ifa_addr;

#ifdef IFLR_ACTIVE
			/* We need to check for active address */
			strlcpy(iflr.iflr_name, ifp->name,
			    sizeof(iflr.iflr_name));
			memcpy(&iflr.addr, ifa->ifa_addr,
			    MIN(ifa->ifa_addr->sa_len, sizeof(iflr.addr)));
			iflr.flags = IFLR_PREFIX;
			iflr.prefixlen = sdl->sdl_alen * NBBY;
			if (ioctl(s_link, SIOCGLIFADDR, &iflr) == -1 ||
			    !(iflr.flags & IFLR_ACTIVE))
			{
				free_interface(ifp);
				continue;
			}
#endif

			ifp->index = sdl->sdl_index;
			sdl_type = sdl->sdl_type;
			switch(sdl->sdl_type) {
			case IFT_BRIDGE: /* FALLTHROUGH */
			case IFT_L2VLAN: /* FALLTHOUGH */
			case IFT_L3IPVLAN: /* FALLTHROUGH */
			case IFT_ETHER:
				ifp->family = ARPHRD_ETHER;
				break;
			case IFT_IEEE1394:
				ifp->family = ARPHRD_IEEE1394;
				break;
#ifdef IFT_INFINIBAND
			case IFT_INFINIBAND:
				ifp->family = ARPHRD_INFINIBAND;
				break;
#endif
			}
			ifp->hwlen = sdl->sdl_alen;
#ifndef CLLADDR
#  define CLLADDR(s) ((const char *)((s)->sdl_data + (s)->sdl_nlen))
#endif
			memcpy(ifp->hwaddr, CLLADDR(sdl), ifp->hwlen);
#elif AF_PACKET
			sll = (const struct sockaddr_ll *)(void *)ifa->ifa_addr;
			ifp->index = sll->sll_ifindex;
			ifp->family = sdl_type = sll->sll_hatype;
			ifp->hwlen = sll->sll_halen;
			if (ifp->hwlen != 0)
				memcpy(ifp->hwaddr, sll->sll_addr, ifp->hwlen);
#endif
		}
#ifdef __linux__
		/* PPP addresses on Linux don't have hardware addresses */
		else
			ifp->index = if_nametoindex(ifp->name);
#endif

		/* We only work on ethernet by default */
		if (!(ifp->flags & IFF_POINTOPOINT) &&
		    ifp->family != ARPHRD_ETHER)
		{
			if (argc == 0 && ctx->ifac == 0) {
				free_interface(ifp);
				continue;
			}
			switch (ifp->family) {
			case ARPHRD_IEEE1394: /* FALLTHROUGH */
			case ARPHRD_INFINIBAND:
				/* We don't warn for supported families */
				break;
			default:
				syslog(LOG_WARNING,
				    "%s: unsupported interface type %.2x"
				    ", falling back to ethernet",
				    ifp->name, sdl_type);
				ifp->family = ARPHRD_ETHER;
				break;
			}
		}

		/* Handle any platform init for the interface */
		if (if_init(ifp) == -1) {
			syslog(LOG_ERR, "%s: if_init: %m", p);
			free_interface(ifp);
			continue;
		}

		/* Ensure that the MTU is big enough for DHCP */
		if (get_mtu(ifp->name) < MTU_MIN &&
		    set_mtu(ifp->name, MTU_MIN) == -1)
		{
			syslog(LOG_ERR, "%s: set_mtu: %m", p);
			free_interface(ifp);
			continue;
		}

#ifdef SIOCGIFPRIORITY
		/* Respect the interface priority */
		memset(&ifr, 0, sizeof(ifr));
		strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name));
		if (ioctl(s_inet, SIOCGIFPRIORITY, &ifr) == 0)
			ifp->metric = ifr.ifr_metric;
#else
		/* We reserve the 100 range for virtual interfaces, if and when
		 * we can work them out. */
		ifp->metric = 200 + ifp->index;
		if (getifssid(ifp->name, ifp->ssid) != -1) {
			ifp->wireless = 1;
			ifp->metric += 100;
		}
#endif

		TAILQ_INSERT_TAIL(ifs, ifp, next);
	}

	for (ifa = ifaddrs; ifa; ifa = ifa->ifa_next) {
		if (ifa->ifa_addr == NULL)
			continue;
		switch(ifa->ifa_addr->sa_family) {
#ifdef INET
		case AF_INET:
			addr = (const struct sockaddr_in *)
			    (void *)ifa->ifa_addr;
			net = (const struct sockaddr_in *)
			    (void *)ifa->ifa_netmask;
			if (ifa->ifa_flags & IFF_POINTOPOINT)
				dst = (const struct sockaddr_in *)
				    (void *)ifa->ifa_dstaddr;
			else
				dst = NULL;
			ipv4_handleifa(ctx, RTM_NEWADDR, ifs, ifa->ifa_name,
				&addr->sin_addr,
				&net->sin_addr,
				dst ? &dst->sin_addr : NULL);
			break;
#endif
#ifdef INET6
		case AF_INET6:
			sin6 = (const struct sockaddr_in6 *)
			    (void *)ifa->ifa_addr;
			ifa_flags = in6_addr_flags(ifa->ifa_name,
			    &sin6->sin6_addr);
			if (ifa_flags != -1)
				ipv6_handleifa(ctx, RTM_NEWADDR, ifs,
				    ifa->ifa_name,
				    &sin6->sin6_addr, ifa_flags);
			break;
#endif
		}
	}

	freeifaddrs(ifaddrs);

#ifdef SIOCGIFPRIORITY
	close(s_inet);
#endif
#ifdef IFLR_ACTIVE
	close(s_link);
#endif

	return ifs;
}
예제 #15
0
// cc-firewall; fire it up!
int main(int argc, char *argv[])
{
    int c;
    float seconds;
    struct timeval tva, tvb;

    iptables_globals.program_name = "firewall";
    if (xtables_init_all(&iptables_globals, FIREWALL_NFPROTO) < 0) {
        fprintf(stderr, "%s: Failed to initialize xtables\n", argv[0]);
        exit(1);
    }
#if defined(ALL_INCLUSIVE) || defined(NO_SHARED_LIBS)
    init_extensions();
#endif
    // Command-line processing
    while((c = getopt_long(argc, argv, "hVdps", options, NULL)) != -1)
    {
        switch(c)
        {
        case 'p':
            pretend = 1;
            // fall through to enable debug also...
        case 'd':
            debug = 1;
            break;
        case 's':
            strict = 1;
            break;
        case 'V':
            fprintf(stderr, "%s v%s\n",
                iptables_globals.program_name, iptables_globals.program_version);
            fprintf(stderr, "Copyright (C) 2005-2008 Point Clark Networks\n");
            fprintf(stderr, "Copyright (C) 2009-2011 ClearFoundation\n");
            exit(1);
        case 'h':
        default:
            fprintf(stderr, "%s [-h|--help]\n", argv[0]);
            fprintf(stderr, "%s [-V|--version]\n", argv[0]);
            fprintf(stderr, "%s [-s|--strict] [-d|--debug] [-p|--pretend] <configuration>\n", argv[0]);
            exit(1);
        }
    }

    // Can only be run by the superuser (root)
    if(getuid() != 0 && geteuid() != 0)
    {
        fprintf(stderr, "%s: %s.\n", argv[0], strerror(EPERM));
        exit(1);
    }

    // Must be passed a Lua script filename to execute
    if(optind != argc - 1)
    {
        // No script filename supplied
        fprintf(stderr, "%s: Required argument missing.\n", iptables_globals.program_name);
        exit(1);
    }

    // Pre-load ip_tables kernel module
    //iptables_insmod("ip_tables", NULL);

    // Install exit handler
    atexit(exit_handler);

    // Initialize ifconfig context
    IFC = if_init();

    // Initialize Lua state
    LUA = lua_open();

    // Load Lua libraries
    luaL_openlibs(LUA);

    // Utility bindings
    lua_pushcfunction(LUA, __lua_b_and);
    lua_setglobal(LUA, "b_and");

    lua_pushcfunction(LUA, __lua_b_or);
    lua_setglobal(LUA, "b_or");

    lua_pushcfunction(LUA, __lua_iptc_ip2bin);
    lua_setglobal(LUA, "ip2bin");

    lua_pushcfunction(LUA, __lua_iptc_bin2ip);
    lua_setglobal(LUA, "bin2ip");

    lua_pushcfunction(LUA, __lua_p_name);
    lua_setglobal(LUA, "p_name");

    lua_pushcfunction(LUA, __lua_gethostbyname);
    lua_setglobal(LUA, "gethostbyname");

    lua_pushcfunction(LUA, __lua_debug);
    lua_setglobal(LUA, "debug");

    lua_pushcfunction(LUA, __lua_echo);
    lua_setglobal(LUA, "echo");

    lua_pushcfunction(LUA, __lua_execute);
    lua_setglobal(LUA, "execute");

    // Iptables bindings
    lua_pushcfunction(LUA, __lua_iptc_init);
    lua_setglobal(LUA, "iptc_init");

    lua_pushcfunction(LUA, __lua_iptc_create_chain);
    lua_setglobal(LUA, "iptc_create_chain");

    lua_pushcfunction(LUA, __lua_iptc_delete_chain);
    lua_setglobal(LUA, "iptc_delete_chain");

    lua_pushcfunction(LUA, __lua_iptc_delete_user_chains);
    lua_setglobal(LUA, "iptc_delete_user_chains");

    lua_pushcfunction(LUA, __lua_iptc_flush_chain);
    lua_setglobal(LUA, "iptc_flush_chain");

    lua_pushcfunction(LUA, __lua_iptc_flush_all_chains);
    lua_setglobal(LUA, "iptc_flush_all_chains");

    lua_pushcfunction(LUA, __lua_iptables);
    lua_setglobal(LUA, "iptables");

    lua_pushcfunction(LUA, __lua_iptc_set_policy);
    lua_setglobal(LUA, "iptc_set_policy");

    lua_pushcfunction(LUA, __lua_iptc_commit);
    lua_setglobal(LUA, "iptc_commit");

    // Network interface bindings (ifconfig.o)
    lua_pushcfunction(LUA, __lua_if_list);
    lua_setglobal(LUA, "if_list");

    lua_pushcfunction(LUA, __lua_if_list_pppoe);
    lua_setglobal(LUA, "if_list_pppoe");

    lua_pushcfunction(LUA, __lua_if_address);
    lua_setglobal(LUA, "if_address");

    lua_pushcfunction(LUA, __lua_if_dst_address);
    lua_setglobal(LUA, "if_dst_address");

    lua_pushcfunction(LUA, __lua_if_netmask);
    lua_setglobal(LUA, "if_netmask");

    lua_pushcfunction(LUA, __lua_if_network);
    lua_setglobal(LUA, "ip_network");

    lua_pushcfunction(LUA, __lua_if_prefix);
    lua_setglobal(LUA, "ip_prefix");

    lua_pushcfunction(LUA, __lua_if_isup);
    lua_setglobal(LUA, "if_isup");

    lua_pushcfunction(LUA, __lua_if_isppp);
    lua_setglobal(LUA, "if_isppp");

    // Open syslog
    {
        int f;
        char *facility = getenv("FW_FACILITY");

        if(facility == NULL)
            f = LOG_LOCAL0;
        else if(strncasecmp(facility, "local0", 6) == 0)
            f = LOG_LOCAL0;
        else if(strncasecmp(facility, "local1", 6) == 0)
            f = LOG_LOCAL1;
        else if(strncasecmp(facility, "local2", 6) == 0)
            f = LOG_LOCAL2;
        else if(strncasecmp(facility, "local3", 6) == 0)
            f = LOG_LOCAL3;
        else if(strncasecmp(facility, "local4", 6) == 0)
            f = LOG_LOCAL4;
        else if(strncasecmp(facility, "local5", 6) == 0)
            f = LOG_LOCAL5;
        else if(strncasecmp(facility, "local6", 6) == 0)
            f = LOG_LOCAL6;
        else
            f = LOG_LOCAL0;

        if(!debug)
            openlog(iptables_globals.program_name, 0, f);
        else
            openlog(iptables_globals.program_name, LOG_PERROR, f);
    }

    gettimeofday(&tva, NULL); 
    
    // Load, compile, and execute supplied Lua script
    if(luaL_loadfile(LUA, (source = argv[optind])) || lua_pcall(LUA, 0, 0, 0))
    {
        syslog(LOG_ERR, "Error: %s\n", lua_tostring(LUA, -1));
        return 1;
    }

    gettimeofday(&tvb, NULL); 

    if(!pretend)
    {
        seconds = tvb.tv_sec - tva.tv_sec;
        seconds += (tvb.tv_usec - tva.tv_usec) / 1000000.0f;
        syslog(LOG_INFO, "Execution time: %.03fs\n", seconds);
    }

    closelog();

    return grc;
}