int main(int argc, char const *argv[]) { route_entry cur_rte1, cur_rte2; interface cif; int res; cif.ifnumber = 2; cur_rte1.dst = inet_addr("192.168.201.0") ; cur_rte1.genmask = inet_addr("255.255.255.0"); cur_rte1.gateway = inet_addr("10.103.240.202"); cur_rte1.metric = 1; cur_rte1.recvif = &cif; cur_rte2.dst = inet_addr("192.168.201.0") ; cur_rte2.genmask = inet_addr("255.255.255.0"); cur_rte2.gateway = inet_addr("10.103.240.202"); cur_rte2.metric = 2; cur_rte2.recvif = &cif; //res = kernel_route(ROUTE_ADD, &cur_rte1, NULL); //res = kernel_route(ROUTE_MOD, &cur_rte1, &cur_rte2); res = kernel_route(ROUTE_DEL, &cur_rte2, NULL); printf("res = %d\n", res); return 0; }
static int del_route(const struct zone *zone, const struct babel_route *route) { int table = find_table(zone->dst_prefix, zone->dst_plen, zone->src_prefix, zone->src_plen); return kernel_route(ROUTE_FLUSH, table, zone->dst_prefix, zone->dst_plen, zone->src_prefix, zone->src_plen, route->nexthop, route->neigh->ifp->ifindex, metric_to_kernel(route_metric(route)), NULL, 0, 0, 0); }
void uninstall_route(struct babel_route *route) { int rc; if(!route->installed) return; rc = kernel_route(ROUTE_FLUSH, route->src->prefix, route->src->plen, route->nexthop, route->neigh->ifp->ifindex, metric_to_kernel(route_metric(route)), NULL, 0, 0); if(rc < 0) flog_err(EC_BABEL_ROUTE, "kernel_route(FLUSH): %s", safe_strerror(errno)); route->installed = 0; }
void uninstall_route(struct route *route) { int rc; if(!route->installed) return; rc = kernel_route(ROUTE_FLUSH, route->src->prefix, route->src->plen, route->nexthop, route->neigh->ifp->ifindex, metric_to_kernel(route_metric(route)), NULL, 0, 0); if(rc < 0) perror("kernel_route(FLUSH)"); route->installed = 0; local_notify_route(route, LOCAL_CHANGE); }
void install_route(struct babel_route *route) { int i, rc; if(route->installed) return; if(!route_feasible(route)) flog_err(EC_BABEL_ROUTE, "WARNING: installing unfeasible route " "(this shouldn't happen)."); i = find_route_slot(route->src->prefix, route->src->plen, NULL); assert(i >= 0 && i < route_slots); if(routes[i] != route && routes[i]->installed) { flog_err(EC_BABEL_ROUTE, "WARNING: attempting to install duplicate route " "(this shouldn't happen)."); return; } rc = kernel_route(ROUTE_ADD, route->src->prefix, route->src->plen, route->nexthop, route->neigh->ifp->ifindex, metric_to_kernel(route_metric(route)), NULL, 0, 0); if(rc < 0) { int save = errno; flog_err(EC_BABEL_ROUTE, "kernel_route(ADD): %s", safe_strerror(errno)); if(save != EEXIST) return; } route->installed = 1; move_installed_route(route, i); }
void install_route(struct route *route) { int i, rc; if(route->installed) return; if(!route_feasible(route)) fprintf(stderr, "WARNING: installing unfeasible route " "(this shouldn't happen)."); i = find_route_slot(route->src->prefix, route->src->plen, NULL); assert(i >= 0 && i < route_slots); if(routes[i] != route && routes[i]->installed) { fprintf(stderr, "WARNING: attempting to install duplicate route " "(this shouldn't happen)."); return; } rc = kernel_route(ROUTE_ADD, route->src->prefix, route->src->plen, route->nexthop, route->neigh->ifp->ifindex, metric_to_kernel(route_metric(route)), NULL, 0, 0); if(rc < 0) { int save = errno; perror("kernel_route(ADD)"); if(save != EEXIST) return; } route->installed = 1; move_installed_route(route, i); local_notify_route(route, LOCAL_CHANGE); }
int kernel_route(int operation, const unsigned char *dest, unsigned short plen, const unsigned char *gate, int ifindex, unsigned int metric, const unsigned char *newgate, int newifindex, unsigned int newmetric) { struct { struct rt_msghdr m_rtm; char m_space[512]; } msg; char *data = msg.m_space; int rc, ipv4; char local6[1][1][16] = IN6ADDR_LOOPBACK_INIT; char local4[1][1][16] = {{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x01 }}}; /* Check that the protocol family is consistent. */ if(plen >= 96 && v4mapped(dest)) { if(!v4mapped(gate)) { errno = EINVAL; return -1; } ipv4 = 1; } else { if(v4mapped(gate)) { errno = EINVAL; return -1; } ipv4 = 0; } if(operation == ROUTE_MODIFY && newmetric == metric && memcmp(newgate, gate, 16) == 0 && newifindex == ifindex) return 0; if(operation == ROUTE_MODIFY) { /* Avoid atomic route changes that is buggy on OS X. */ kernel_route(ROUTE_FLUSH, dest, plen, gate, ifindex, metric, NULL, 0, 0); return kernel_route(ROUTE_ADD, dest, plen, newgate, newifindex, newmetric, NULL, 0, 0); } kdebugf("kernel_route: %s %s/%d metric %d dev %d nexthop %s\n", operation == ROUTE_ADD ? "add" : operation == ROUTE_FLUSH ? "flush" : "change", format_address(dest), plen, metric, ifindex, format_address(gate)); if(kernel_socket < 0) kernel_setup_socket(1); memset(&msg, 0, sizeof(msg)); msg.m_rtm.rtm_version = RTM_VERSION; switch(operation) { case ROUTE_FLUSH: msg.m_rtm.rtm_type = RTM_DELETE; break; case ROUTE_ADD: msg.m_rtm.rtm_type = RTM_ADD; break; case ROUTE_MODIFY: msg.m_rtm.rtm_type = RTM_CHANGE; break; default: return -1; }; msg.m_rtm.rtm_index = ifindex; msg.m_rtm.rtm_flags = RTF_UP | RTF_PROTO2; if(plen == 128) msg.m_rtm.rtm_flags |= RTF_HOST; if(metric == KERNEL_INFINITY) { msg.m_rtm.rtm_flags |= RTF_BLACKHOLE; if(ifindex_lo < 0) { ifindex_lo = if_nametoindex("lo0"); if(ifindex_lo <= 0) return -1; } msg.m_rtm.rtm_index = ifindex_lo; } msg.m_rtm.rtm_seq = ++seq; msg.m_rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; if(plen != 128) msg.m_rtm.rtm_addrs |= RTA_NETMASK; #define PUSHEUI(ifindex) \ do { char ifname[IFNAMSIZ]; \ struct sockaddr_dl *sdl = (struct sockaddr_dl*) data; \ if(!if_indextoname((ifindex), ifname)) \ return -1; \ if(get_sdl(sdl, ifname) < 0) \ return -1; \ data = data + ROUNDUP(sdl->sdl_len); \ } while (0) #define PUSHADDR(src) \ do { struct sockaddr_in *sin = (struct sockaddr_in*) data; \ sin->sin_len = sizeof(struct sockaddr_in); \ sin->sin_family = AF_INET; \ memcpy(&sin->sin_addr, (src) + 12, 4); \ data = data + ROUNDUP(sin->sin_len); \ } while (0) #define PUSHADDR6(src) \ do { struct sockaddr_in6 *sin6 = (struct sockaddr_in6*) data; \ sin6->sin6_len = sizeof(struct sockaddr_in6); \ sin6->sin6_family = AF_INET6; \ memcpy(&sin6->sin6_addr, (src), 16); \ if(IN6_IS_ADDR_LINKLOCAL (&sin6->sin6_addr)) \ SET_IN6_LINKLOCAL_IFINDEX (sin6->sin6_addr, ifindex); \ data = data + ROUNDUP(sin6->sin6_len); \ } while (0) /* KAME ipv6 stack does not support IPv4 mapped IPv6, so we have to * duplicate the codepath */ if(ipv4) { PUSHADDR(dest); if (metric == KERNEL_INFINITY) { PUSHADDR(**local4); } else if(plen == 128 && memcmp(dest+12, gate+12, 4) == 0) { #if defined(RTF_CLONING) msg.m_rtm.rtm_flags |= RTF_CLONING; #endif PUSHEUI(ifindex); } else { msg.m_rtm.rtm_flags |= RTF_GATEWAY; PUSHADDR(gate); } if((msg.m_rtm.rtm_addrs & RTA_NETMASK) != 0) { struct in6_addr tmp_sin6_addr; plen2mask(plen, &tmp_sin6_addr); PUSHADDR((char *)&tmp_sin6_addr); } } else { PUSHADDR6(dest); if (metric == KERNEL_INFINITY) { PUSHADDR6(**local6); } else { msg.m_rtm.rtm_flags |= RTF_GATEWAY; PUSHADDR6(gate); } if((msg.m_rtm.rtm_addrs & RTA_NETMASK) != 0) { struct in6_addr tmp_sin6_addr; plen2mask(plen, &tmp_sin6_addr); PUSHADDR6((char*)&tmp_sin6_addr); } } #undef PUSHEUI #undef PUSHADDR #undef PUSHADDR6 msg.m_rtm.rtm_msglen = data - (char *)&msg; rc = write(kernel_socket, (char*)&msg, msg.m_rtm.rtm_msglen); if (rc < msg.m_rtm.rtm_msglen) return -1; return 1; }
int kernel_route(int operation, const unsigned char *dest, unsigned short plen, const unsigned char *gate, int ifindex, unsigned int metric, const unsigned char *newgate, int newifindex, unsigned int newmetric) { union { char raw[1024]; struct nlmsghdr nh; } buf; struct rtmsg *rtm; struct rtattr *rta; int len = sizeof(buf.raw); int rc, ipv4; if(!nl_setup) { fprintf(stderr,"kernel_route: netlink not initialized.\n"); errno = EIO; return -1; } /* if the socket has been closed after an IO error, */ /* we try to re-open it. */ if(nl_command.sock < 0) { rc = netlink_socket(&nl_command, 0); if(rc < 0) { int olderrno = errno; perror("kernel_route: netlink_socket()"); errno = olderrno; return -1; } } /* Check that the protocol family is consistent. */ if(plen >= 96 && v4mapped(dest)) { if(!v4mapped(gate)) { errno = EINVAL; return -1; } } else { if(v4mapped(gate)) { errno = EINVAL; return -1; } } ipv4 = v4mapped(gate); if(operation == ROUTE_MODIFY) { if(newmetric == metric && memcmp(newgate, gate, 16) == 0 && newifindex == ifindex) return 0; /* It would be better to add the new route before removing the old one, to avoid losing packets. However, this causes problems with non-multipath kernels, which sometimes silently fail the request, causing "stuck" routes. Let's stick with the naive approach, and hope that the window is small enough to be negligible. */ kernel_route(ROUTE_FLUSH, dest, plen, gate, ifindex, metric, NULL, 0, 0); rc = kernel_route(ROUTE_ADD, dest, plen, newgate, newifindex, newmetric, NULL, 0, 0); if(rc < 0) { if(errno == EEXIST) rc = 1; /* Should we try to re-install the flushed route on failure? Error handling is hard. */ } return rc; } kdebugf("kernel_route: %s %s/%d metric %d dev %d nexthop %s\n", operation == ROUTE_ADD ? "add" : operation == ROUTE_FLUSH ? "flush" : "???", format_address(dest), plen, metric, ifindex, format_address(gate)); /* Unreachable default routes cause all sort of weird interactions; ignore them. */ if(metric >= KERNEL_INFINITY && (plen == 0 || (ipv4 && plen == 96))) return 0; memset(buf.raw, 0, sizeof(buf.raw)); if(operation == ROUTE_ADD) { buf.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL; buf.nh.nlmsg_type = RTM_NEWROUTE; } else { buf.nh.nlmsg_flags = NLM_F_REQUEST; buf.nh.nlmsg_type = RTM_DELROUTE; } rtm = NLMSG_DATA(&buf.nh); rtm->rtm_family = ipv4 ? AF_INET : AF_INET6; rtm->rtm_dst_len = ipv4 ? plen - 96 : plen; rtm->rtm_table = export_table; rtm->rtm_scope = RT_SCOPE_UNIVERSE; if(metric < KERNEL_INFINITY) rtm->rtm_type = RTN_UNICAST; else rtm->rtm_type = RTN_UNREACHABLE; rtm->rtm_protocol = RTPROT_BABEL; rtm->rtm_flags |= RTNH_F_ONLINK; rta = RTM_RTA(rtm); if(ipv4) { rta = RTA_NEXT(rta, len); rta->rta_len = RTA_LENGTH(sizeof(struct in_addr)); rta->rta_type = RTA_DST; memcpy(RTA_DATA(rta), dest + 12, sizeof(struct in_addr)); } else { rta = RTA_NEXT(rta, len); rta->rta_len = RTA_LENGTH(sizeof(struct in6_addr)); rta->rta_type = RTA_DST; memcpy(RTA_DATA(rta), dest, sizeof(struct in6_addr)); } rta = RTA_NEXT(rta, len); rta->rta_len = RTA_LENGTH(sizeof(int)); rta->rta_type = RTA_PRIORITY; if(metric < KERNEL_INFINITY) { *(int*)RTA_DATA(rta) = metric; rta = RTA_NEXT(rta, len); rta->rta_len = RTA_LENGTH(sizeof(int)); rta->rta_type = RTA_OIF; *(int*)RTA_DATA(rta) = ifindex; if(ipv4) { rta = RTA_NEXT(rta, len); rta->rta_len = RTA_LENGTH(sizeof(struct in_addr)); rta->rta_type = RTA_GATEWAY; memcpy(RTA_DATA(rta), gate + 12, sizeof(struct in_addr)); } else { rta = RTA_NEXT(rta, len); rta->rta_len = RTA_LENGTH(sizeof(struct in6_addr)); rta->rta_type = RTA_GATEWAY; memcpy(RTA_DATA(rta), gate, sizeof(struct in6_addr)); } } else { *(int*)RTA_DATA(rta) = -1; } buf.nh.nlmsg_len = (char*)rta + rta->rta_len - buf.raw; return netlink_talk(&buf.nh); }