void menu_borrar_regla(){ long int puerto; long int ip; char sentido[1]; new_comunication(); if(read_data(&ip,&puerto, sentido, print_borrar_header)){ del_rule(); send_msg(ip,puerto,sentido[0]); system_pause(); } }
/* * Remove all the created rules to the source routing tables */ void remove_created_rules() { lispd_iface_list_elt *interface_list = NULL; lispd_iface_elt *iface = NULL; interface_list = head_interface_list; while (interface_list != NULL){ iface = interface_list->iface; if (iface->ipv4_address->afi != AF_UNSPEC){ if (iface->ipv4_gateway != NULL){ del_route(AF_INET,iface->iface_index,NULL,NULL,iface->ipv4_gateway,0,0,iface->iface_index); } del_rule(AF_INET,0,iface->iface_index,iface->iface_index,RTN_UNICAST,iface->ipv4_address,32,NULL,0,0); } if (iface->ipv6_address->afi != AF_UNSPEC){ if (iface->ipv6_gateway != NULL){ del_route(AF_INET6,iface->iface_index,NULL,NULL,iface->ipv6_gateway,0,0,iface->iface_index); } del_rule(AF_INET6,0,iface->iface_index,iface->iface_index,RTN_UNICAST,iface->ipv6_address,128,NULL,0,0); } interface_list = interface_list->next; } }
/* * Accpet the commands from user space to set the rules */ void nl_data_ready(struct sk_buff *__skb) { struct sk_buff *skb; struct nlmsghdr *nlh; char str[100]; //struct completion cmpl; int i=10; int interval = 0; char command[6] ={'\0'},ipaddr[60]={'\0'}; struct in6_addr recvaddr; printk("nl_data_ready......in\n"); skb = skb_get (__skb); if(skb->len >= NLMSG_SPACE(0)) { nlh = nlmsg_hdr(skb); memcpy(str, NLMSG_DATA(nlh), sizeof(str)); printk("Message received:%s\n",str) ; sscanf(str, "cmd=%s ip=%s interval=%d", command, ipaddr,&interval); memcpy(&recvaddr,ipaddr,sizeof(recvaddr)); //convert ipaddr to struct in6_addr // the comand format // ADD>x:x:x:x // DEL>x:x:x:x if(strcmp(command,"ADD")==0) { add_rule(&ipaddrs, &recvaddr); } else if(strcmp(command,"DEL")==0) { del_rule(&ipaddrs, &recvaddr); } pid = nlh->nlmsg_pid; //the source process id sendnlmsg("command executed."); kfree_skb(skb); } }
/*OPREACION IP,PUERTO, DIRECCION *OPREACION: 1 ADD * 2 DEL */ void extern_program(char *argv[]){ printf("XTERM %s\n", argv[1]); char *token = argv[1]; new_comunication(); if(strcmp(token,"ADD")==0) add_rule(); else if(strcmp(token,"DEL")==0) del_rule(); else exit(1); char *ipstr=argv[2]; long int ip=translate_ip(ipstr); printf("IP:%s %ld\n",ipstr,ip); char *aux=argv[3]; long int puerto=atol(aux); printf("PUERTO: %ld\n", puerto); char *sentido=argv[4]; printf("WAY: %s\n", sentido); send_msg(ip,puerto,sentido[0]); }
int main() { /* char IPdotdec[40]; //存放点分十进制IP地址 struct in6_addr s; // IPv4地址结构体 // 输入IP地址 printf("Please input IP address: "); scanf("%s", IPdotdec); // 转换 inet_pton(AF_INET6, IPdotdec, (void *)&s); print_6addr(&s); //printf("inet_pton: 0x%x\n", s.s6_addr32[0]); // 注意得到的字节序 // 反转换 inet_ntop(AF_INET6, (void *)&s, IPdotdec, 16); printf("inet_ntop: %s\n", IPdotdec); */ ip_list addrs = NULL; char command[6] ={'\0'},ipaddr[60]={'\0'}; char inputs[60]; char *cmds; struct in6_addr s; ip_node node; while(1) { printf("Please input command: 'ADD/DEL + addr' or 'q' \n"); scanf("%s", inputs); if(strncmp(inputs,"q",1) == 0) break; else { sscanf(inputs, "%[A-Za-z]>%s", command, ipaddr); cmds = inputs; // analysis_info(command, ipaddr, cmds, ">"); if(strcmp(command,"ADD")==0) { add_rule(&addrs, ipaddr); } else if(strcmp(command,"DEL")==0) { del_rule(&addrs, ipaddr); } } } if(addrs != NULL) { if(match_rule(addrs,"ff15::1")) printf("match"); /*node = addrs->head; while(node != NULL) { /*if(ipaddrs->addr.u6_addr32[0] == check_ip.u6_addr32[0] && ipaddrs->addr.u6_addr32[1] == check_ip.u6_addr32[1] && ipaddrs->addr.u6_addr32[2] == check_ip.u6_addr32[2] && ipaddrs->addr.u6_addr32[3] == check_ip.u6_addr32[3] ) return true; //the check_ip's type is char* inet_pton(AF_INET6, node->addr, (void *)&s); print_6addr(&s); node = node->next; }*/ } }
void process_nl_new_link (struct nlmsghdr *nlh) { struct ifinfomsg *ifi = NULL; lispd_iface_elt *iface = NULL; int iface_index = 0; uint8_t status = UP; char iface_name[IF_NAMESIZE]; uint32_t old_iface_index = 0; ifi = (struct ifinfomsg *) NLMSG_DATA (nlh); iface_index = ifi->ifi_index; iface = get_interface_from_index(iface_index); if (iface == NULL) { /* * In some OS when a virtual interface is removed and added again, the index of the interface change. * Search lispd_iface_elt by the interface name and update the index. */ if (if_indextoname(iface_index, iface_name) != NULL) { iface = get_interface(iface_name); } if (iface == NULL) { lispd_log_msg(LISP_LOG_DEBUG_2, "process_nl_new_link: the netlink message is not for any interface associated with RLOCs (%s)", iface_name); return; } else { old_iface_index = iface->iface_index; iface->iface_index = iface_index; lispd_log_msg(LISP_LOG_DEBUG_2,"process_nl_new_link: The new index of the interface %s is: %d. Updating tables", iface_name, iface->iface_index); /* Update routing tables and reopen sockets*/ if (iface->ipv4_address->afi != AF_UNSPEC) { del_rule(AF_INET,0,old_iface_index,old_iface_index,RTN_UNICAST,iface->ipv4_address,32,NULL,0,0); add_rule(AF_INET,0,iface_index,iface_index,RTN_UNICAST,iface->ipv4_address,32,NULL,0,0); close(iface->out_socket_v4); iface->out_socket_v4 = open_device_binded_raw_socket(iface->iface_name,AF_INET); bind_socket_src_address(iface->out_socket_v4,iface->ipv4_address); } if (iface->ipv6_address->afi != AF_UNSPEC) { del_rule(AF_INET6,0,old_iface_index,old_iface_index,RTN_UNICAST,iface->ipv6_address,128,NULL,0,0); add_rule(AF_INET6,0,iface_index,iface_index,RTN_UNICAST,iface->ipv6_address,128,NULL,0,0); close(iface->out_socket_v6); iface->out_socket_v6 = open_device_binded_raw_socket(iface->iface_name,AF_INET6); bind_socket_src_address(iface->out_socket_v6,iface->ipv6_address); } } } if ((ifi->ifi_flags & IFF_RUNNING) != 0) { lispd_log_msg(LISP_LOG_DEBUG_1, "process_nl_new_link: Interface %s changes its status to UP",iface->iface_name); status = UP; } else { lispd_log_msg(LISP_LOG_DEBUG_1, "process_nl_new_link: Interface %s changes its status to DOWN",iface->iface_name); status = DOWN; } process_link_status_change (iface, status); }
void process_address_change ( lispd_iface_elt *iface, lisp_addr_t new_addr) { lisp_addr_t *iface_addr = NULL; lispd_iface_mappings_list *mapping_list = NULL; int aux_afi = 0; // XXX To be modified when full NAT implemented --> When Nat Aware active no IPv6 RLOCs supported if (nat_aware == TRUE && new_addr.afi == AF_INET6) { return; } /* Check if the addres is a global address*/ if (is_link_local_addr(new_addr) == TRUE) { lispd_log_msg(LISP_LOG_DEBUG_2,"precess_address_change: the extractet address from the netlink " "messages is a local link address: %s discarded", get_char_from_lisp_addr_t(new_addr)); return; } /* If default RLOC afi defined (-a 4 or 6), only accept addresses of the specified afi */ if (default_rloc_afi != AF_UNSPEC && default_rloc_afi != new_addr.afi) { lispd_log_msg(LISP_LOG_DEBUG_2,"precess_address_change: Default RLOC afi defined (-a #): Skipped %s address in iface %s", (new_addr.afi == AF_INET) ? "IPv4" : "IPv6",iface->iface_name); return; } /* * Actions to be done due to a change of address: SMR */ switch (new_addr.afi) { case AF_INET: iface_addr = iface->ipv4_address; break; case AF_INET6: iface_addr = iface->ipv6_address; break; } // Same address that we already have if (compare_lisp_addr_t(iface_addr,&new_addr)==0) { lispd_log_msg(LISP_LOG_DEBUG_2,"precess_address_change: The detected change of address for interface %s " "doesn't affect",iface->iface_name); /* We must rebind the socket just in case the address is from a virtual interface who has changed its interafce number */ switch (new_addr.afi) { case AF_INET: bind_socket_src_address(iface->out_socket_v4,&new_addr); break; case AF_INET6: bind_socket_src_address(iface->out_socket_v6,&new_addr); break; } return; } /* * Change source routing rules for this interface and binding */ if (iface_addr->afi != AF_UNSPEC) { del_rule(iface_addr->afi, 0, iface->iface_index, iface->iface_index, RTN_UNICAST, iface_addr, (iface_addr->afi == AF_INET) ? 32 : 128, NULL,0,0); } add_rule(new_addr.afi, 0, iface->iface_index, iface->iface_index, RTN_UNICAST, &new_addr, (new_addr.afi == AF_INET) ? 32 : 128, NULL,0,0); switch (new_addr.afi) { case AF_INET: bind_socket_src_address(iface->out_socket_v4,&new_addr); break; case AF_INET6: bind_socket_src_address(iface->out_socket_v6,&new_addr); break; } aux_afi = iface_addr->afi; // Update the new address copy_lisp_addr(iface_addr, &new_addr); /* The interface was down during initial configuratiopn process and now it is up. Activate address */ if (aux_afi == AF_UNSPEC) { lispd_log_msg(LISP_LOG_DEBUG_1,"process_address_change: Activating the locator address %s" , get_char_from_lisp_addr_t(new_addr)); activate_interface_address(iface, new_addr); if (iface->status == UP) { iface_balancing_vectors_calc(iface); /* * If no default control and data interface, recalculate it */ if ((default_ctrl_iface_v4 == NULL && new_addr.afi == AF_INET) || (default_ctrl_iface_v6 == NULL && new_addr.afi == AF_INET6)) { lispd_log_msg(LISP_LOG_DEBUG_2,"No default control interface. Recalculate new control interface"); set_default_ctrl_ifaces(); } if ((default_out_iface_v4 == NULL && new_addr.afi == AF_INET) || (default_out_iface_v6 == NULL && new_addr.afi == AF_INET6)) { lispd_log_msg(LISP_LOG_DEBUG_2,"No default output interface. Recalculate new output interface"); set_default_output_ifaces(); } } } lispd_log_msg(LISP_LOG_DEBUG_1,"precess_address_change: New address detected for interface %s -> %s", iface->iface_name, get_char_from_lisp_addr_t(new_addr)); mapping_list = iface->head_mappings_list; /* Sort again the locators list of the affected mappings*/ while (mapping_list != NULL) { if (aux_afi != AF_UNSPEC && // When the locator is activated, it is automatically sorted ((new_addr.afi == AF_INET && mapping_list->use_ipv4_address == TRUE) || (new_addr.afi == AF_INET6 && mapping_list->use_ipv6_address == TRUE))) { sort_locators_list_elt (mapping_list->mapping, iface_addr); } mapping_list = mapping_list->next; } /* Indicate change of address in the interface */ switch (new_addr.afi) { case AF_INET: iface->ipv4_changed = TRUE; break; case AF_INET6: iface->ipv6_changed = TRUE; break; } /* If it is compiled in router mode, then recompile default routes changing the indicated src address*/ if (router_mode == TRUE) { switch (new_addr.afi) { case AF_INET: if (iface == default_out_iface_v4) { set_tun_default_route_v4(); } break; case AF_INET6: if (iface == default_out_iface_v6) { del_tun_default_route_v6(); set_tun_default_route_v6(); } break; } } /* Check if the new address is behind NAT */ if(nat_aware==TRUE) { // TODO : To be modified when implementing NAT per multiple interfaces nat_status = UNKNOWN; clear_rtr_from_locators (iface); if (iface->status == UP) { initial_info_request_process(); } else { nat_aware_iface_address_change = TRUE; } } /* Reprograming SMR timer*/ if (smr_timer == NULL) { smr_timer = create_timer (SMR_TIMER); } start_timer(smr_timer, LISPD_SMR_TIMEOUT,(timer_callback)init_smr, NULL); }
int mrp_debug_set_config(const char *cmd) { char buf[2 * PATH_MAX + 1], *colon, *at, *eq; char *func, *file, *end; size_t len; int del, off, line; if (*cmd == '+' || *cmd == '-') del = (*cmd++ == '-'); else del = FALSE; eq = strchr(cmd, '='); if (eq == NULL) { strncpy(buf, cmd, sizeof(buf) - 1); buf[sizeof(buf) - 1] = '\0'; off = FALSE; } else { if (!strcmp(eq + 1, "on")) off = FALSE; else if (!strcmp(eq + 1, "off")) off = TRUE; else return FALSE; len = eq - cmd; if (len >= sizeof(buf)) len = sizeof(buf) - 1; strncpy(buf, cmd, len); buf[len] = '\0'; } colon = strchr(buf, ':'); if (colon != NULL) { if (strchr(buf, '@') != NULL) return FALSE; *colon = '\0'; func = NULL; file = buf; line = strtoul(colon + 1, &end, 10); if (end && *end) return FALSE; mrp_log_info("%s file='%s', line=%d, %s", del ? "del" : "add", file, line, off ? "off" : "on"); } else { at = strchr(buf, '@'); if (at != NULL) { *at = '\0'; func = (at == buf ? NULL : buf); file = at + 1; line = 0; mrp_log_info("%s func='%s', file='%s', %s", del ? "del" : "add", func ? func : "", file, off ? "off" : "on"); } else { func = buf; file = NULL; line = 0; mrp_log_info("%s func='%s' %s", del ? "del" : "add", func, off ? "off" : "on"); } } if (!del) return add_rule(func, file, line, off); else return del_rule(func, file, line, off); return TRUE; }