int welcome_iface(int fd, char *iface) { struct ifreq ifr; struct ethtool_drvinfo drvinfo; char txt[256]; if (interface_auto_up) interface_up(fd, iface); memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name)-1); if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) snprintf(txt, sizeof(txt)-1, "Using interface %s", iface); else snprintf(txt, sizeof(txt)-1, "Using interface %s/%02X:%02X:%02X:%02X:%02X:%02X", iface, ifr.ifr_hwaddr.sa_data[0] & 0xFF, ifr.ifr_hwaddr.sa_data[1] & 0xFF, ifr.ifr_hwaddr.sa_data[2] & 0xFF, ifr.ifr_hwaddr.sa_data[3] & 0xFF, ifr.ifr_hwaddr.sa_data[4] & 0xFF, ifr.ifr_hwaddr.sa_data[5] & 0xFF); memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_name, iface, sizeof(ifr.ifr_name)-1); drvinfo.cmd = ETHTOOL_GDRVINFO; ifr.ifr_data = (caddr_t) &drvinfo; if (ioctl(fd, SIOCETHTOOL, &ifr) != -1) daemon_log(LOG_INFO, "%s with driver <%s> (version: %s)", txt, drvinfo.driver, drvinfo.version); else daemon_log(LOG_INFO, "%s", txt); cached_detect_beat_func = NULL; return 0; }
int handle(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg){ struct ifinfomsg *iface; switch(n->nlmsg_type){ case RTM_NEWLINK: iface = NLMSG_DATA(n); //int len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*iface)); int index = iface->ifi_index; if(iface->ifi_flags & IFF_RUNNING) interface_up(index); if(!(iface->ifi_flags & IFF_RUNNING )) interface_down(index); break; case RTM_DELLINK: printf("Interface Down\n"); break; } if(running){ return 0; }else{ return -1; } }
void flush_interface(char *ifname) { struct interface *ifp, *prev; prev = NULL; ifp = interfaces; while(ifp) { if(strcmp(ifp->name, ifname) == 0) break; prev = ifp; ifp = ifp->next; } if(ifp == NULL) { fprintf(stderr, "Warning: attempting to flush nonexistent interface.\n"); return; } interface_up(ifp, 0); if(prev) prev->next = ifp->next; else interfaces = ifp->next; local_notify_interface(ifp, LOCAL_FLUSH); if(ifp->conf != NULL && ifp->conf != default_interface_conf) flush_ifconf(ifp->conf); local_notify_interface(ifp, LOCAL_FLUSH); free(ifp); }
int netcfg_wireless_auto_connect(struct debconfclient *client, char *iface, wireless_config *wconf, int *couldnt_associate) { int i, success = 0; /* Default to any AP */ wconf->essid[0] = '\0'; wconf->essid_on = 0; iw_set_basic_config (wfd, iface, wconf); /* Wait for association.. (MAX_SECS seconds)*/ #ifndef MAX_SECS #define MAX_SECS 3 #endif debconf_capb(client, "backup progresscancel"); debconf_progress_start(client, 0, MAX_SECS, "netcfg/wifi_progress_title"); if (debconf_progress_info(client, "netcfg/wifi_progress_info") == 30) goto stop; netcfg_progress_displayed = 1; for (i = 0; i <= MAX_SECS; i++) { int progress_ret; interface_up(iface); sleep (1); iw_get_basic_config (wfd, iface, wconf); if (!empty_str(wconf->essid)) { /* Save for later */ debconf_set(client, "netcfg/wireless_essid", wconf->essid); debconf_progress_set(client, MAX_SECS); success = 1; break; } progress_ret = debconf_progress_step(client, 1); interface_down(iface); if (progress_ret == 30) break; } stop: debconf_progress_stop(client); debconf_capb(client, "backup"); netcfg_progress_displayed = 0; if (success) return 0; *couldnt_associate = 1; return *couldnt_associate; }
void check_interfaces(void) { struct interface *ifp; int rc, ifindex_changed = 0; unsigned int ifindex; FOR_ALL_INTERFACES(ifp) { ifindex = if_nametoindex(ifp->name); if(ifindex != ifp->ifindex) { debugf("Noticed ifindex change for %s.\n", ifp->name); ifp->ifindex = 0; interface_up(ifp, 0); ifp->ifindex = ifindex; ifindex_changed = 1; } if(ifp->ifindex > 0) rc = kernel_interface_operational(ifp->name, ifp->ifindex); else rc = 0; if((rc > 0) != if_up(ifp)) { debugf("Noticed status change for %s.\n", ifp->name); interface_up(ifp, rc > 0); } if(if_up(ifp)) { /* Bother, said Pooh. We should probably check for a change in IPv4 addresses at this point. */ check_link_local_addresses(ifp); check_interface_channel(ifp); rc = check_interface_ipv4(ifp); if(rc > 0) { send_request(ifp, NULL, 0, NULL, 0); send_update(ifp, 0, NULL, 0, NULL, 0); } } } if(ifindex_changed) renumber_filters(); }
void act_UP(void) { idle_filter_init(); flush_timeout_queue(); interface_up(); ppp_half_dead = 0; if (buffer_packets) forward_buffer(); /* Once we get here, we might as well kill off any outstanding * FIFO requests */ if (!use_req && req_pid) { killpg(req_pid, SIGKILL); kill(req_pid, SIGKILL); req_pid=0; syslog(LOG_INFO,"Cancelling link up requested denied in favour of existing link."); } }
int netcfg_wireless_show_essids(struct debconfclient *client, struct netcfg_interface *interface) { wireless_scan_head network_list; wireless_config wconf; char *buffer; int essid_list_len = 1; iw_get_basic_config (wfd, interface->name, &wconf); interface_up(interface->name); if (iw_scan(wfd, interface->name, iw_get_kernel_we_version(), &network_list) >= 0 ) { wireless_scan *network; di_info("Scan of wireless interface %s succeeded.", interface->name); /* Determine the actual length of the buffer. */ for (network = network_list.result; network; network = network->next) { if (!exists_in_network_list(network_list, network)) { essid_list_len += (strlen(network->b.essid) + 2); } } /* Buffer initialization. */ buffer = malloc(essid_list_len * sizeof(char)); if (buffer == NULL) { /* Error in memory allocation. */ di_warning("Unable to allocate memory for network list buffer."); return ENTER_MANUALLY; } strcpy(buffer, ""); /* Create list of available ESSIDs. */ for (network = network_list.result; network; network = network->next) { if (!exists_in_network_list(network_list, network)) { strcat(buffer, network->b.essid); strcat(buffer, ", "); } } /* Asking the user. */ debconf_capb(client, "backup"); debconf_subst(client, "netcfg/wireless_show_essids", "essid_list", buffer); debconf_fset(client, "netcfg/wireless_show_essids", "seen", "false"); debconf_input(client, "high", "netcfg/wireless_show_essids"); if (debconf_go(client) == CMD_GOBACK) { debconf_fset(client, "netcfg/wireless_show_essids", "seen", "false"); free_network_list(&network_list.result); free(buffer); return GO_BACK; } debconf_get(client, "netcfg/wireless_show_essids"); /* User wants to enter an ESSID manually. */ if (strcmp(client->value, "manual") == 0) { free_network_list(&network_list.result); free(buffer); return ENTER_MANUALLY; } /* User has chosen a network from the list, need to find which one and * get its cofiguration. */ for (network = network_list.result; network; network = network->next) { if (strcmp(network->b.essid, client->value) == 0) { wconf = network->b; interface->essid = strdup(network->b.essid); break; } } /* Free the network list. */ free_network_list(&network_list.result); free(buffer); } else { /* Go directly to choosing manually, use the wireless_essid_again * question. */ if (netcfg_wireless_choose_essid_manually(client, interface, "netcfg/wireless_essid_again") == GO_BACK) { return GO_BACK; } return 0; } iw_set_basic_config(wfd, interface->name, &wconf); interface_down(interface->name); di_info("Network chosen: %s. Proceeding to connect.", interface->essid); return 0; }
int main(int argc, char *argv[]) { int num_interfaces = 0; enum { BACKUP, GET_INTERFACE, GET_HOSTNAME_ONLY, GET_METHOD, GET_DHCP, GET_STATIC, WCONFIG, WCONFIG_ESSID, WCONFIG_SECURITY_TYPE, WCONFIG_WEP, WCONFIG_WPA, START_WPA, QUIT } state = GET_INTERFACE; static struct debconfclient *client; static int requested_wireless_tools = 0; char **ifaces; char *defiface = NULL, *defwireless = NULL; response_t res; struct netcfg_interface interface; #ifdef NM struct nm_config_info nmconf; #endif /* initialize libd-i */ di_system_init("netcfg"); netcfg_interface_init(&interface); if (strcmp(basename(argv[0]), "ptom") != 0) di_info("Starting netcfg v.%s", NETCFG_VERSION); parse_args (argc, argv); reap_old_files (); open_sockets(); /* initialize debconf */ client = debconfclient_new(); debconf_capb(client, "backup"); /* Check to see if netcfg should be run at all */ debconf_get(client, "netcfg/enable"); if (!strcmp(client->value, "false")) { netcfg_get_hostname(client, "netcfg/get_hostname", hostname, 0); netcfg_write_common("", hostname, NULL); return 0; } /* always always always default back to autoconfig, unless you've specified * disable_autoconfig on the command line. */ debconf_get(client, "netcfg/disable_autoconfig"); if (!strcmp(client->value, "true")) debconf_set(client, "netcfg/use_autoconfig", "false"); else debconf_set(client, "netcfg/use_autoconfig", "true"); /* also support disable_dhcp for compatibility */ debconf_get(client, "netcfg/disable_dhcp"); if (!strcmp(client->value, "true")) debconf_set(client, "netcfg/use_autoconfig", "false"); for (;;) { switch(state) { case BACKUP: return RETURN_TO_MAIN; case GET_INTERFACE: /* If we have returned from outside of netcfg and want to * reconfigure networking, check to see if wpasupplicant is * running, and kill it if it is. If left running when * the interfaces are taken up and down, it appears to * leave it in an inconsistant state */ kill_wpa_supplicant(); /* Choose a default by looking for link */ if (get_all_ifs(1, &ifaces) > 1) { while (*ifaces) { struct netcfg_interface link_interface; if (check_kill_switch(*ifaces)) { debconf_subst(client, "netcfg/kill_switch_enabled", "iface", *ifaces); debconf_input(client, "high", "netcfg/kill_switch_enabled"); if (debconf_go(client) == CMD_GOBACK) { state = BACKUP; break; } /* Is it still enabled? */ if (check_kill_switch(*ifaces)) { ifaces++; continue; } } interface_up(*ifaces); netcfg_interface_init(&link_interface); link_interface.name = strdup(*ifaces); if (netcfg_detect_link (client, &link_interface) == 1) /* CONNECTED */ { /* CONNECTED */ di_info("found link on interface %s, making it the default.", *ifaces); defiface = strdup(*ifaces); free(link_interface.name); break; } else { #ifdef WIRELESS struct wireless_config wc; #endif /* WIRELESS */ di_info("found no link on interface %s.", *ifaces); #ifdef WIRELESS if (iw_get_basic_config(wfd, *ifaces, &wc) == 0) { wc.essid[0] = '\0'; wc.essid_on = 0; iw_set_basic_config(wfd, *ifaces, &wc); sleep(1); iw_get_basic_config(wfd, *ifaces, &wc); if (!empty_str(wc.essid)) { di_info("%s is associated with %s. Selecting as default", *ifaces, wc.essid); defiface = strdup(*ifaces); interface_down(*ifaces); break; } else { di_info("%s is not associated. Relegating to defwireless", *ifaces); if (defwireless != NULL) free (defwireless); defwireless = strdup(*ifaces); } } else di_info("%s is not a wireless interface. Continuing.", *ifaces); interface_down(*ifaces); #endif } free(link_interface.name); interface_down(*ifaces); ifaces++; } } if (state == BACKUP) break; if (!defiface && defwireless) defiface = defwireless; if(netcfg_get_interface(client, &(interface.name), &num_interfaces, defiface)) state = BACKUP; else if (! interface.name || ! num_interfaces) state = GET_HOSTNAME_ONLY; else { if (is_wireless_iface (interface.name)) state = WCONFIG; else state = GET_METHOD; } break; case GET_HOSTNAME_ONLY: if(netcfg_get_hostname(client, "netcfg/get_hostname", hostname, 0)) state = BACKUP; else { netcfg_write_common("", hostname, NULL); state = QUIT; } break; case GET_METHOD: if ((res = netcfg_get_method(client)) == GO_BACK) state = (num_interfaces == 1) ? BACKUP : GET_INTERFACE; else { if (netcfg_method == DHCP) state = GET_DHCP; else state = GET_STATIC; } break; case GET_DHCP: switch (netcfg_activate_dhcp(client, &interface)) { case 0: state = QUIT; break; case RETURN_TO_MAIN: /* * It doesn't make sense to go back to GET_METHOD because * the user has already been asked whether they want to * try an alternate method. */ state = (num_interfaces == 1) ? BACKUP : GET_INTERFACE; break; case CONFIGURE_MANUALLY: state = GET_STATIC; break; default: return 1; } break; case GET_STATIC: { int ret; /* Misnomer - this should actually take care of activation */ if ((ret = netcfg_get_static(client, &interface)) == RETURN_TO_MAIN) state = GET_INTERFACE; else if (ret) state = GET_METHOD; else state = QUIT; break; } case WCONFIG: if (requested_wireless_tools == 0) { di_exec_shell_log("apt-install iw wireless-tools"); requested_wireless_tools = 1; } state = WCONFIG_ESSID; break; case WCONFIG_ESSID: if (netcfg_wireless_set_essid(client, &interface) == GO_BACK) state = BACKUP; else { init_wpa_supplicant_support(&interface); if (interface.wpa_supplicant_status == WPA_UNAVAIL) state = WCONFIG_WEP; else state = WCONFIG_SECURITY_TYPE; } break; case WCONFIG_SECURITY_TYPE: { int ret; ret = wireless_security_type(client, interface.name); if (ret == GO_BACK) state = WCONFIG_ESSID; else if (ret == REPLY_WPA) { state = WCONFIG_WPA; interface.wifi_security = REPLY_WPA; } else { state = WCONFIG_WEP; interface.wifi_security = REPLY_WEP; } break; } case WCONFIG_WEP: if (netcfg_wireless_set_wep(client, &interface) == GO_BACK) if (interface.wpa_supplicant_status == WPA_UNAVAIL) state = WCONFIG_ESSID; else state = WCONFIG_SECURITY_TYPE; else state = GET_METHOD; break; case WCONFIG_WPA: if (interface.wpa_supplicant_status == WPA_OK) { di_exec_shell_log("apt-install wpasupplicant"); interface.wpa_supplicant_status = WPA_QUEUED; } if (netcfg_set_passphrase(client, &interface) == GO_BACK) state = WCONFIG_SECURITY_TYPE; else state = START_WPA; break; case START_WPA: if (wpa_supplicant_start(client, &interface) == GO_BACK) state = WCONFIG_ESSID; else state = GET_METHOD; break; case QUIT: #ifdef NM if (num_interfaces > 0) { nm_get_configuration(&interface, &nmconf); nm_write_configuration(nmconf); } #endif netcfg_update_entropy(); return 0; } } }
static struct timeval * rtsol_check_timer(void) { static struct timeval returnval; struct timeval now, rtsol_timer; struct ifinfo *ifinfo; int flags; gettimeofday(&now, NULL); rtsol_timer = tm_max; for (ifinfo = iflist; ifinfo; ifinfo = ifinfo->next) { if (TIMEVAL_LEQ(ifinfo->expire, now)) { if (dflag > 1) warnmsg(LOG_DEBUG, __func__, "timer expiration on %s, " "state = %d", ifinfo->ifname, ifinfo->state); switch (ifinfo->state) { case IFS_DOWN: case IFS_TENTATIVE: /* interface_up returns 0 on success */ flags = interface_up(ifinfo->ifname); if (flags == 0) ifinfo->state = IFS_DELAY; else if (flags == IFS_TENTATIVE) ifinfo->state = IFS_TENTATIVE; else ifinfo->state = IFS_DOWN; break; case IFS_IDLE: { int oldstatus = ifinfo->active; int probe = 0; ifinfo->active = interface_status(ifinfo); if (oldstatus != ifinfo->active) { warnmsg(LOG_DEBUG, __func__, "%s status is changed" " from %d to %d", ifinfo->ifname, oldstatus, ifinfo->active); probe = 1; ifinfo->state = IFS_DELAY; } else if (ifinfo->probeinterval && (ifinfo->probetimer -= ifinfo->timer.tv_sec) <= 0) { /* probe timer expired */ ifinfo->probetimer = ifinfo->probeinterval; probe = 1; ifinfo->state = IFS_PROBE; } if (probe && mobile_node) defrouter_probe(ifinfo->sdl->sdl_index); break; } case IFS_DELAY: ifinfo->state = IFS_PROBE; sendpacket(ifinfo); break; case IFS_PROBE: if (ifinfo->probes < MAX_RTR_SOLICITATIONS) sendpacket(ifinfo); else { warnmsg(LOG_INFO, __func__, "No answer " "after sending %d RSs", ifinfo->probes); ifinfo->probes = 0; ifinfo->state = IFS_IDLE; } break; } rtsol_timer_update(ifinfo); } if (TIMEVAL_LT(ifinfo->expire, rtsol_timer)) rtsol_timer = ifinfo->expire; } if (TIMEVAL_EQ(rtsol_timer, tm_max)) { warnmsg(LOG_DEBUG, __func__, "there is no timer"); return(NULL); } else if (TIMEVAL_LT(rtsol_timer, now)) /* this may occur when the interval is too small */ returnval.tv_sec = returnval.tv_usec = 0; else TIMEVAL_SUB(&rtsol_timer, &now, &returnval); if (dflag > 1) warnmsg(LOG_DEBUG, __func__, "New timer is %ld:%08ld", (long)returnval.tv_sec, (long)returnval.tv_usec); return(&returnval); }
static int ifconfig(char *ifname) { struct ifinfo *ifinfo; struct sockaddr_dl *sdl; int flags; if ((sdl = if_nametosdl(ifname)) == NULL) { warnmsg(LOG_ERR, __func__, "failed to get link layer information for %s", ifname); return(-1); } if (find_ifinfo(sdl->sdl_index)) { warnmsg(LOG_ERR, __func__, "interface %s was already configured", ifname); free(sdl); return(-1); } if ((ifinfo = malloc(sizeof(*ifinfo))) == NULL) { warnmsg(LOG_ERR, __func__, "memory allocation failed"); free(sdl); return(-1); } memset(ifinfo, 0, sizeof(*ifinfo)); ifinfo->sdl = sdl; strncpy(ifinfo->ifname, ifname, sizeof(ifinfo->ifname)); /* construct a router solicitation message */ if (make_packet(ifinfo)) goto bad; /* * check if the interface is available. * also check if SIOCGIFMEDIA ioctl is OK on the interface. */ ifinfo->mediareqok = 1; ifinfo->active = interface_status(ifinfo); if (!ifinfo->mediareqok) { /* * probe routers periodically even if the link status * does not change. */ ifinfo->probeinterval = PROBE_INTERVAL; } /* activate interface: interface_up returns 0 on success */ flags = interface_up(ifinfo->ifname); if (flags == 0) ifinfo->state = IFS_DELAY; else if (flags == IFS_TENTATIVE) ifinfo->state = IFS_TENTATIVE; else ifinfo->state = IFS_DOWN; rtsol_timer_update(ifinfo); /* link into chain */ if (iflist) ifinfo->next = iflist; iflist = ifinfo; return(0); bad: free(ifinfo->sdl); free(ifinfo); return(-1); }
int netcfg_wireless_set_essid (struct debconfclient * client, char *iface, char* priority) { int ret, couldnt_associate = 0; wireless_config wconf; char* tf = NULL, *user_essid = NULL, *ptr = wconf.essid; iw_get_basic_config (wfd, iface, &wconf); debconf_subst(client, "netcfg/wireless_essid", "iface", iface); debconf_subst(client, "netcfg/wireless_essid_again", "iface", iface); debconf_subst(client, "netcfg/wireless_adhoc_managed", "iface", iface); debconf_input(client, priority ? priority : "low", "netcfg/wireless_adhoc_managed"); if (debconf_go(client) == 30) return GO_BACK; debconf_get(client, "netcfg/wireless_adhoc_managed"); if (!strcmp(client->value, "Ad-hoc network (Peer to peer)")) mode = ADHOC; wconf.has_mode = 1; wconf.mode = mode; debconf_input(client, priority ? priority : "low", "netcfg/wireless_essid"); if (debconf_go(client) == 30) return GO_BACK; debconf_get(client, "netcfg/wireless_essid"); tf = strdup(client->value); automatic: /* question not asked or user doesn't care or we're successfully associated */ if (!empty_str(wconf.essid) || empty_str(client->value)) { int i, success = 0; /* Default to any AP */ wconf.essid[0] = '\0'; wconf.essid_on = 0; iw_set_basic_config (wfd, iface, &wconf); /* Wait for association.. (MAX_SECS seconds)*/ #define MAX_SECS 3 debconf_capb(client, "backup progresscancel"); debconf_progress_start(client, 0, MAX_SECS, "netcfg/wifi_progress_title"); if (debconf_progress_info(client, "netcfg/wifi_progress_info") == 30) goto stop; netcfg_progress_displayed = 1; for (i = 0; i <= MAX_SECS; i++) { int progress_ret; interface_up(iface); sleep (1); iw_get_basic_config (wfd, iface, &wconf); if (!empty_str(wconf.essid)) { /* Save for later */ debconf_set(client, "netcfg/wireless_essid", wconf.essid); debconf_progress_set(client, MAX_SECS); success = 1; break; } progress_ret = debconf_progress_step(client, 1); interface_down(iface); if (progress_ret == 30) break; } stop: debconf_progress_stop(client); debconf_capb(client, "backup"); netcfg_progress_displayed = 0; if (success) return 0; couldnt_associate = 1; } /* yes, wants to set an essid by himself */ if (strlen(tf) <= IW_ESSID_MAX_SIZE) /* looks ok, let's use it */ user_essid = tf; while (!user_essid || empty_str(user_essid) || strlen(user_essid) > IW_ESSID_MAX_SIZE) { /* Misnomer of a check. Basically, if we went through autodetection, * we want to enter this loop, but we want to suppress anything that * relied on the checking of tf/user_essid (i.e. "", in most cases.) */ if (!couldnt_associate) { debconf_subst(client, "netcfg/invalid_essid", "essid", user_essid); debconf_input(client, "high", "netcfg/invalid_essid"); debconf_go(client); } if (couldnt_associate) ret = debconf_input(client, "critical", "netcfg/wireless_essid_again"); else ret = debconf_input(client, "low", "netcfg/wireless_essid"); /* we asked the question once, why can't we ask it again? */ assert (ret != 30); if (debconf_go(client) == 30) /* well, we did, but he wants to go back */ return GO_BACK; if (couldnt_associate) debconf_get(client, "netcfg/wireless_essid_again"); else debconf_get(client, "netcfg/wireless_essid"); if (empty_str(client->value)) { if (couldnt_associate) /* we've already tried the empty string here, so give up */ break; else goto automatic; } /* But now we'd not like to suppress any MORE errors */ couldnt_associate = 0; free(user_essid); user_essid = strdup(client->value); } essid = user_essid; memset(ptr, 0, IW_ESSID_MAX_SIZE + 1); snprintf(wconf.essid, IW_ESSID_MAX_SIZE + 1, "%s", essid); wconf.has_essid = 1; wconf.essid_on = 1; iw_set_basic_config (wfd, iface, &wconf); return 0; }
int interface_up(struct interface *ifp, int up) { int mtu, rc, wired; struct ipv6_mreq mreq; if((!!up) == if_up(ifp)) return 0; if(up) ifp->flags |= IF_UP; else ifp->flags &= ~IF_UP; if(up) { if(ifp->ifindex <= 0) { fprintf(stderr, "Upping unknown interface %s.\n", ifp->name); goto fail; } rc = kernel_setup_interface(1, ifp->name, ifp->ifindex); if(rc < 0) { fprintf(stderr, "kernel_setup_interface(%s, %d) failed.\n", ifp->name, ifp->ifindex); goto fail; } mtu = kernel_interface_mtu(ifp->name, ifp->ifindex); if(mtu < 0) { fprintf(stderr, "Warning: couldn't get MTU of interface %s (%d).\n", ifp->name, ifp->ifindex); mtu = 1280; } /* We need to be able to fit at least two messages into a packet, so MTUs below 116 require lower layer fragmentation. */ /* In IPv6, the minimum MTU is 1280, and every host must be able to reassemble up to 1500 bytes, but I'd rather not rely on this. */ if(mtu < 128) { fprintf(stderr, "Suspiciously low MTU %d on interface %s (%d).\n", mtu, ifp->name, ifp->ifindex); mtu = 128; } if(ifp->sendbuf) free(ifp->sendbuf); /* 40 for IPv6 header, 8 for UDP header, 12 for good luck. */ ifp->bufsize = mtu - sizeof(packet_header) - 60; ifp->sendbuf = malloc(ifp->bufsize); if(ifp->sendbuf == NULL) { fprintf(stderr, "Couldn't allocate sendbuf.\n"); ifp->bufsize = 0; goto fail; } rc = resize_receive_buffer(mtu); if(rc < 0) fprintf(stderr, "Warning: couldn't resize " "receive buffer for interface %s (%d) (%d bytes).\n", ifp->name, ifp->ifindex, mtu); if(IF_CONF(ifp, wired) == CONFIG_NO) { wired = 0; } else if(IF_CONF(ifp, wired) == CONFIG_YES) { wired = 1; } else if(all_wireless) { wired = 0; } else { rc = kernel_interface_wireless(ifp->name, ifp->ifindex); if(rc < 0) { fprintf(stderr, "Warning: couldn't determine whether %s (%d) " "is a wireless interface.\n", ifp->name, ifp->ifindex); wired = 0; } else { wired = !rc; } } if(wired) { ifp->flags |= IF_WIRED; ifp->cost = IF_CONF(ifp, cost); if(ifp->cost <= 0) ifp->cost = 96; if(IF_CONF(ifp, split_horizon) == CONFIG_NO) ifp->flags &= ~IF_SPLIT_HORIZON; else if(IF_CONF(ifp, split_horizon) == CONFIG_YES) ifp->flags |= IF_SPLIT_HORIZON; else if(split_horizon) ifp->flags |= IF_SPLIT_HORIZON; else ifp->flags &= ~IF_SPLIT_HORIZON; if(IF_CONF(ifp, lq) == CONFIG_YES) ifp->flags |= IF_LQ; else ifp->flags &= ~IF_LQ; } else { ifp->flags &= ~IF_WIRED; ifp->cost = IF_CONF(ifp, cost); if(ifp->cost <= 0) ifp->cost = 256; if(IF_CONF(ifp, split_horizon) == CONFIG_YES) ifp->flags |= IF_SPLIT_HORIZON; else ifp->flags &= ~IF_SPLIT_HORIZON; if(IF_CONF(ifp, lq) == CONFIG_NO) ifp->flags &= ~IF_LQ; else ifp->flags |= IF_LQ; } if(IF_CONF(ifp, faraway) == CONFIG_YES) ifp->flags |= IF_FARAWAY; if(IF_CONF(ifp, hello_interval) > 0) ifp->hello_interval = IF_CONF(ifp, hello_interval); else if((ifp->flags & IF_WIRED)) ifp->hello_interval = default_wired_hello_interval; else ifp->hello_interval = default_wireless_hello_interval; ifp->update_interval = IF_CONF(ifp, update_interval) > 0 ? IF_CONF(ifp, update_interval) : ifp->hello_interval * 4; ifp->rtt_decay = IF_CONF(ifp, rtt_decay) > 0 ? IF_CONF(ifp, rtt_decay) : 42; ifp->rtt_min = IF_CONF(ifp, rtt_min) > 0 ? IF_CONF(ifp, rtt_min) : 10000; ifp->rtt_max = IF_CONF(ifp, rtt_max) > 0 ? IF_CONF(ifp, rtt_max) : 120000; if(ifp->rtt_max <= ifp->rtt_min) { fprintf(stderr, "Uh, rtt-max is less than or equal to rtt-min (%d <= %d). " "Setting it to %d.\n", ifp->rtt_max, ifp->rtt_min, ifp->rtt_min + 10000); ifp->rtt_max = ifp->rtt_min + 10000; } ifp->max_rtt_penalty = IF_CONF(ifp, max_rtt_penalty); if(IF_CONF(ifp, enable_timestamps) == CONFIG_YES || (IF_CONF(ifp, enable_timestamps) == CONFIG_DEFAULT && ifp->max_rtt_penalty > 0)) ifp->flags |= IF_TIMESTAMPS; rc = check_link_local_addresses(ifp); if(rc < 0) { goto fail; } memset(&mreq, 0, sizeof(mreq)); memcpy(&mreq.ipv6mr_multiaddr, protocol_group, 16); mreq.ipv6mr_interface = ifp->ifindex; rc = setsockopt(protocol_socket, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char*)&mreq, sizeof(mreq)); if(rc < 0) { perror("setsockopt(IPV6_JOIN_GROUP)"); goto fail; } check_interface_channel(ifp); update_interface_metric(ifp); rc = check_interface_ipv4(ifp); debugf("Upped interface %s (%s, cost=%d, channel=%d%s).\n", ifp->name, (ifp->flags & IF_WIRED) ? "wired" : "wireless", ifp->cost, ifp->channel, ifp->ipv4 ? ", IPv4" : ""); set_timeout(&ifp->hello_timeout, ifp->hello_interval); set_timeout(&ifp->update_timeout, ifp->update_interval); send_hello(ifp); if(rc > 0) send_update(ifp, 0, NULL, 0, NULL, 0); send_request(ifp, NULL, 0, NULL, 0); } else { flush_interface_routes(ifp, 0); ifp->buffered = 0; ifp->bufsize = 0; free(ifp->sendbuf); ifp->num_buffered_updates = 0; ifp->update_bufsize = 0; if(ifp->buffered_updates) free(ifp->buffered_updates); ifp->buffered_updates = NULL; ifp->sendbuf = NULL; if(ifp->ifindex > 0) { memset(&mreq, 0, sizeof(mreq)); memcpy(&mreq.ipv6mr_multiaddr, protocol_group, 16); mreq.ipv6mr_interface = ifp->ifindex; rc = setsockopt(protocol_socket, IPPROTO_IPV6, IPV6_LEAVE_GROUP, (char*)&mreq, sizeof(mreq)); if(rc < 0) perror("setsockopt(IPV6_LEAVE_GROUP)"); kernel_setup_interface(0, ifp->name, ifp->ifindex); } if(ifp->ll) free(ifp->ll); ifp->ll = NULL; ifp->numll = 0; } local_notify_interface(ifp, LOCAL_CHANGE); return 1; fail: assert(up); interface_up(ifp, 0); local_notify_interface(ifp, LOCAL_CHANGE); return -1; }