struct interface *if_get_by_name_len(const char *name, size_t namelen) { struct interface *ifp; return ((ifp = if_lookup_by_name_len(name, namelen)) != NULL) ? ifp : if_create(name, namelen); }
/* Get interface by name if given name interface doesn't exist create one. */ struct interface *if_get_by_name(const char *name) { struct interface *ifp; return ((ifp = if_lookup_by_name(name)) != NULL) ? ifp : if_create(name, strlen(name)); }
struct interface_FOO * zebra_interface_add_read (struct stream *s) { struct interface_FOO *ifp; char ifname_tmp[INTERFACE_NAMSIZ]; /* Read interface name. */ stream_get (ifname_tmp, s, INTERFACE_NAMSIZ); /* Lookup this by interface name. */ ifp = if_lookup_by_name (ifname_tmp); /* If such interface does not exist, make new one. */ if (! ifp) ifp = if_create (ifname_tmp, INTERFACE_NAMSIZ); /* Read interface's index. */ ifp->ifindex = stream_getl (s); /* Read interface's value. */ ifp->status = stream_getc (s); ifp->flags = stream_getl (s); ifp->metric = stream_getl (s); ifp->mtu = stream_getl (s); ifp->mtu6 = stream_getl (s); ifp->bandwidth = stream_getl (s); #ifdef HAVE_SOCKADDR_DL stream_get (&ifp->sdl, s, sizeof (ifp->sdl)); #else ifp->hw_addr_len = stream_getl (s); if (ifp->hw_addr_len) stream_get (ifp->hw_addr, s, ifp->hw_addr_len); #endif /* HAVE_SOCKADDR_DL */ return ifp; }
/* Interface adding function called from interface_list. */ int ifm_read (struct if_msghdr *ifm) { struct interface *ifp; struct sockaddr_dl *sdl = NULL; sdl = (struct sockaddr_dl *)(ifm + 1); /* Use sdl index. */ ifp = if_lookup_by_index (ifm->ifm_index); if (ifp == NULL) { /* Check interface's address.*/ if (! (ifm->ifm_addrs & RTA_IFP)) { zlog_warn ("There must be RTA_IFP address for ifindex %d\n", ifm->ifm_index); return -1; } ifp = if_create (); strncpy (ifp->name, sdl->sdl_data, sdl->sdl_nlen); ifp->ifindex = ifm->ifm_index; ifp->flags = ifm->ifm_flags; #if defined(__bsdi__) if_kvm_get_mtu (ifp); #else if_get_mtu (ifp); #endif /* __bsdi__ */ if_get_metric (ifp); /* Fetch hardware address. */ if (sdl->sdl_family != AF_LINK) { zlog_warn ("sockaddr_dl->sdl_family is not AF_LINK"); return -1; } memcpy (&ifp->sdl, sdl, sizeof (struct sockaddr_dl)); if_add_update (ifp); } else { /* There is a case of promisc, allmulti flag modification. */ if (if_is_up (ifp)) { ifp->flags = ifm->ifm_flags; if (! if_is_up (ifp)) if_down (ifp); } else { ifp->flags = ifm->ifm_flags; if (if_is_up (ifp)) if_up (ifp); } } #ifdef HAVE_NET_RT_IFLIST ifp->stats = ifm->ifm_data; #endif /* HAVE_NET_RT_IFLIST */ if (IS_ZEBRA_DEBUG_KERNEL) zlog_info ("interface %s index %d", ifp->name, ifp->ifindex); return 0; }
/* * Handle struct if_msghdr obtained from reading routing socket or * sysctl (from interface_list). There may or may not be sockaddrs * present after the header. */ int ifm_read (struct if_msghdr *ifm) { struct interface *ifp = NULL; char ifname[IFNAMSIZ]; short ifnlen = 0; caddr_t *cp; /* terminate ifname at head (for strnlen) and tail (for safety) */ ifname[IFNAMSIZ - 1] = '\0'; /* paranoia: sanity check structure */ if (ifm->ifm_msglen < sizeof(struct if_msghdr)) { zlog_err ("ifm_read: ifm->ifm_msglen %d too short\n", ifm->ifm_msglen); return -1; } /* * Check for a sockaddr_dl following the message. First, point to * where a socakddr might be if one follows the message. */ cp = (void *)(ifm + 1); #ifdef SUNOS_5 /* * XXX This behavior should be narrowed to only the kernel versions * for which the structures returned do not match the headers. * * if_msghdr_t on 64 bit kernels in Solaris 9 and earlier versions * is 12 bytes larger than the 32 bit version. */ if (((struct sockaddr *) cp)->sa_family == AF_UNSPEC) cp = cp + 12; #endif RTA_ADDR_GET (NULL, RTA_DST, ifm->ifm_addrs, cp); RTA_ADDR_GET (NULL, RTA_GATEWAY, ifm->ifm_addrs, cp); RTA_ATTR_GET (NULL, RTA_NETMASK, ifm->ifm_addrs, cp); RTA_ADDR_GET (NULL, RTA_GENMASK, ifm->ifm_addrs, cp); RTA_NAME_GET (ifname, RTA_IFP, ifm->ifm_addrs, cp, ifnlen); RTA_ADDR_GET (NULL, RTA_IFA, ifm->ifm_addrs, cp); RTA_ADDR_GET (NULL, RTA_AUTHOR, ifm->ifm_addrs, cp); RTA_ADDR_GET (NULL, RTA_BRD, ifm->ifm_addrs, cp); if (IS_ZEBRA_DEBUG_KERNEL) zlog_debug ("%s: sdl ifname %s", __func__, (ifnlen ? ifname : "(nil)")); /* * Look up on ifindex first, because ifindices are the primary handle for * interfaces across the user/kernel boundary, for most systems. (Some * messages, such as up/down status changes on NetBSD, do not include a * sockaddr_dl). */ if ( (ifp = if_lookup_by_index (ifm->ifm_index)) != NULL ) { /* we have an ifp, verify that the name matches as some systems, * eg Solaris, have a 1:many association of ifindex:ifname * if they dont match, we dont have the correct ifp and should * set it back to NULL to let next check do lookup by name */ if (ifnlen && (strncmp (ifp->name, ifname, IFNAMSIZ) != 0) ) { if (IS_ZEBRA_DEBUG_KERNEL) zlog_debug ("%s: ifp name %s doesnt match sdl name %s", __func__, ifp->name, ifname); ifp = NULL; } } /* * If we dont have an ifp, try looking up by name. Particularly as some * systems (Solaris) have a 1:many mapping of ifindex:ifname - the ifname * is therefore our unique handle to that interface. * * Interfaces specified in the configuration file for which the ifindex * has not been determined will have ifindex == IFINDEX_INTERNAL, and such * interfaces are found by this search, and then their ifindex values can * be filled in. */ if ( (ifp == NULL) && ifnlen) ifp = if_lookup_by_name (ifname); /* * If ifp still does not exist or has an invalid index (IFINDEX_INTERNAL), * create or fill in an interface. */ if ((ifp == NULL) || (ifp->ifindex == IFINDEX_INTERNAL)) { /* * To create or fill in an interface, a sockaddr_dl (via * RTA_IFP) is required. */ if (!ifnlen) { zlog_warn ("Interface index %d (new) missing ifname\n", ifm->ifm_index); return -1; } #ifndef RTM_IFANNOUNCE /* Down->Down interface should be ignored here. * See further comment below. */ if (!CHECK_FLAG (ifm->ifm_flags, IFF_UP)) return 0; #endif /* !RTM_IFANNOUNCE */ if (ifp == NULL) { /* Interface that zebra was not previously aware of, so create. */ ifp = if_create (ifname, ifnlen); if (IS_ZEBRA_DEBUG_KERNEL) zlog_debug ("%s: creating ifp for ifindex %d", __func__, ifm->ifm_index); } if (IS_ZEBRA_DEBUG_KERNEL) zlog_debug ("%s: updated/created ifp, ifname %s, ifindex %d", __func__, ifp->name, ifp->ifindex); /* * Fill in newly created interface structure, or larval * structure with ifindex IFINDEX_INTERNAL. */ ifp->ifindex = ifm->ifm_index; #ifdef HAVE_BSD_LINK_DETECT /* translate BSD kernel msg for link-state */ bsd_linkdetect_translate(ifm); #endif /* HAVE_BSD_LINK_DETECT */ if_flags_update (ifp, ifm->ifm_flags); #if defined(__bsdi__) if_kvm_get_mtu (ifp); #else if_get_mtu (ifp); #endif /* __bsdi__ */ if_get_metric (ifp); if_add_update (ifp); } else /* * Interface structure exists. Adjust stored flags from * notification. If interface has up->down or down->up * transition, call state change routines (to adjust routes, * notify routing daemons, etc.). (Other flag changes are stored * but apparently do not trigger action.) */ { if (ifp->ifindex != ifm->ifm_index) { zlog_warn ("%s: index mismatch, ifname %s, ifp index %d, " "ifm index %d", __func__, ifp->name, ifp->ifindex, ifm->ifm_index); return -1; } #ifdef HAVE_BSD_LINK_DETECT /* translate BSD kernel msg for link-state */ bsd_linkdetect_translate(ifm); #endif /* HAVE_BSD_LINK_DETECT */ /* update flags and handle operative->inoperative transition, if any */ if_flags_update (ifp, ifm->ifm_flags); #ifndef RTM_IFANNOUNCE if (!if_is_up (ifp)) { /* No RTM_IFANNOUNCE on this platform, so we can never * distinguish between ~IFF_UP and delete. We must presume * it has been deleted. * Eg, Solaris will not notify us of unplumb. * * XXX: Fixme - this should be runtime detected * So that a binary compiled on a system with IFANNOUNCE * will still behave correctly if run on a platform without */ if_delete_update (ifp); } #endif /* RTM_IFANNOUNCE */ if (if_is_up (ifp)) { #if defined(__bsdi__) if_kvm_get_mtu (ifp); #else if_get_mtu (ifp); #endif /* __bsdi__ */ if_get_metric (ifp); } } #ifdef HAVE_NET_RT_IFLIST ifp->stats = ifm->ifm_data; #endif /* HAVE_NET_RT_IFLIST */ if (IS_ZEBRA_DEBUG_KERNEL) zlog_debug ("%s: interface %s index %d", __func__, ifp->name, ifp->ifindex); return 0; }
struct ospf_interface * ospf_vl_new (struct ospf *ospf, struct ospf_vl_data *vl_data) { struct ospf_interface * voi; struct interface * vi; char ifname[INTERFACE_NAMSIZ + 1]; struct ospf_area *area; struct in_addr area_id; struct connected *co; struct prefix_ipv4 *p; if (IS_DEBUG_OSPF_EVENT) zlog_debug ("ospf_vl_new(): Start"); if (vlink_count == OSPF_VL_MAX_COUNT) { if (IS_DEBUG_OSPF_EVENT) zlog_debug ("ospf_vl_new(): Alarm: " "cannot create more than OSPF_MAX_VL_COUNT virtual links"); return NULL; } if (IS_DEBUG_OSPF_EVENT) zlog_debug ("ospf_vl_new(): creating pseudo zebra interface"); snprintf (ifname, sizeof(ifname), "VLINK%d", vlink_count); vi = if_create (ifname, strnlen(ifname, sizeof(ifname))); co = connected_new (); co->ifp = vi; listnode_add (vi->connected, co); p = prefix_ipv4_new (); p->family = AF_INET; p->prefix.s_addr = 0; p->prefixlen = 0; co->address = (struct prefix *)p; voi = ospf_if_new (ospf, vi, co->address); if (voi == NULL) { if (IS_DEBUG_OSPF_EVENT) zlog_debug ("ospf_vl_new(): Alarm: OSPF int structure is not created"); return NULL; } voi->connected = co; voi->vl_data = vl_data; voi->ifp->mtu = OSPF_VL_MTU; voi->type = OSPF_IFTYPE_VIRTUALLINK; vlink_count++; if (IS_DEBUG_OSPF_EVENT) zlog_debug ("ospf_vl_new(): Created name: %s", ifname); if (IS_DEBUG_OSPF_EVENT) zlog_debug ("ospf_vl_new(): set if->name to %s", vi->name); area_id.s_addr = 0; area = ospf_area_get (ospf, area_id, OSPF_AREA_ID_FORMAT_ADDRESS); voi->area = area; if (IS_DEBUG_OSPF_EVENT) zlog_debug ("ospf_vl_new(): set associated area to the backbone"); ospf_nbr_add_self (voi); ospf_area_add_if (voi->area, voi); ospf_if_stream_set (voi); if (IS_DEBUG_OSPF_EVENT) zlog_debug ("ospf_vl_new(): Stop"); return voi; }
int main(int argc, char **argv) { struct sigaction sigact; struct sigevent sev; struct itimerspec its; int r = 1; parse_args(argc, argv); sigact.sa_handler = sighandler_exit; sigemptyset(&sigact.sa_mask); sigact.sa_flags = 0; sigaction(SIGINT, &sigact, NULL); sigaction(SIGTERM, &sigact, NULL); sigaction(SIGQUIT, &sigact, NULL); sigact.sa_handler = sighandler_wait_child; sigaction(SIGCHLD, &sigact, NULL); sigact.sa_handler = sighandler_stats; sigfillset(&sigact.sa_mask); if (stats_period == 0) sigaction(SIGUSR1, &sigact, NULL); if (logfile != NULL) { set_wmlogger(argv[0], WMLOGGER_FILE, logfile); } else if (daemonize) { set_wmlogger(argv[0], WMLOGGER_SYSLOG, NULL); } else { set_wmlogger(argv[0], WMLOGGER_FILE, stderr); } if (daemonize) { daemon(0, 0); } if (stats_period != 0) { sigemptyset(&sigact.sa_mask); sigaction(SIGRTMIN, &sigact, NULL); // create the POSIX timer sev.sigev_notify = SIGEV_SIGNAL; sev.sigev_signo = SIGRTMIN; sev.sigev_value.sival_ptr = (void *) &timerid; if (timer_create(CLOCK_MONOTONIC, &sev, &timerid) == 0) { // start the POSIX timer its.it_value.tv_sec = stats_period; its.it_value.tv_nsec = 0; its.it_interval.tv_sec = its.it_value.tv_sec; its.it_interval.tv_nsec = its.it_value.tv_nsec; timer_settime(timerid, 0, &its, NULL); } } r = libusb_init(&ctx); if (r < 0) { wmlog_msg(0, "failed to initialise libusb"); exit_release_resources(1); } if (pid_fname && !write_pidfile(pid_fname)) { wmlog_msg(0, "failed to create pid file %s", pid_fname); exit_release_resources(1); } devh = find_wimax_device(); if (devh == NULL) { wmlog_msg(0, "Could not find/open device"); exit_release_resources(1); } wmlog_msg(0, "Device found"); if (detach_dvd && libusb_kernel_driver_active(devh, IF_DVD) == 1) { r = libusb_detach_kernel_driver(devh, IF_DVD); if (r < 0) { wmlog_msg(0, "kernel driver detach error %d", r); } else { wmlog_msg(0, "detached pseudo-DVD kernel driver"); } } if (libusb_kernel_driver_active(devh, IF_MODEM) == 1) { kernel_driver_active = 1; r = libusb_detach_kernel_driver(devh, IF_MODEM); if (r < 0) { wmlog_msg(0, "kernel driver detach error %d", r); } else { wmlog_msg(0, "detached modem kernel driver"); } } r = libusb_claim_interface(devh, IF_MODEM); if (r < 0) { wmlog_msg(0, "Claim usb interface error %d", r); exit_release_resources(1); } wmlog_msg(0, "Claimed interface"); alloc_fds(); libusb_set_pollfd_notifiers(ctx, cb_add_pollfd, cb_remove_pollfd, NULL); r = init(); if (r < 0) { wmlog_msg(0, "init error %d", r); exit_release_resources(1); } if_create(); cb_add_pollfd(tap_fd, POLLIN, NULL); r = scan_loop(); if (r < 0) { wmlog_msg(0, "scan_loop error %d", r); exit_release_resources(1); } exit_release_resources(0); return 0; }
int main(int argc, char **argv) { struct sigaction sigact; int r = 1; parse_args(argc, argv); sigact.sa_handler = sighandler_exit; sigemptyset(&sigact.sa_mask); sigact.sa_flags = 0; sigaction(SIGINT, &sigact, NULL); sigaction(SIGTERM, &sigact, NULL); sigaction(SIGQUIT, &sigact, NULL); sigact.sa_handler = sighandler_wait_child; sigaction(SIGCHLD, &sigact, NULL); if (logfile != NULL) { set_wmlogger(argv[0], WMLOGGER_FILE, logfile); } else if (daemonize) { set_wmlogger(argv[0], WMLOGGER_SYSLOG, NULL); } else { set_wmlogger(argv[0], WMLOGGER_FILE, stderr); } if (daemonize) { daemon(0, 0); } r = libusb_init(&ctx); if (r < 0) { wmlog_msg(0, "failed to initialise libusb"); exit_release_resources(1); } devh = find_wimax_device(); if (devh == NULL) { wmlog_msg(0, "Could not find/open device"); exit_release_resources(1); } wmlog_msg(0, "Device found"); if (detach_dvd && libusb_kernel_driver_active(devh, IF_DVD) == 1) { r = libusb_detach_kernel_driver(devh, IF_DVD); if (r < 0) { wmlog_msg(0, "kernel driver detach error %d", r); } else { wmlog_msg(0, "detached pseudo-DVD kernel driver"); } } if (libusb_kernel_driver_active(devh, IF_MODEM) == 1) { kernel_driver_active = 1; r = libusb_detach_kernel_driver(devh, IF_MODEM); if (r < 0) { wmlog_msg(0, "kernel driver detach error %d", r); } else { wmlog_msg(0, "detached modem kernel driver"); } } r = libusb_claim_interface(devh, IF_MODEM); if (r < 0) { wmlog_msg(0, "Claim usb interface error %d", r); exit_release_resources(1); } wmlog_msg(0, "Claimed interface"); alloc_fds(); libusb_set_pollfd_notifiers(ctx, cb_add_pollfd, cb_remove_pollfd, NULL); r = init(); if (r < 0) { wmlog_msg(0, "init error %d", r); exit_release_resources(1); } if_create(); cb_add_pollfd(tap_fd, POLLIN, NULL); r = scan_loop(); if (r < 0) { wmlog_msg(0, "scan_loop error %d", r); exit_release_resources(1); } exit_release_resources(0); return 0; }