Commit fb7c6be0 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Implement exporting of IPv4 routes.

parent 3880d2e5
...@@ -503,9 +503,9 @@ kernel_setup_socket(int setup) ...@@ -503,9 +503,9 @@ kernel_setup_socket(int setup)
int rc; int rc;
if(setup) { if(setup) {
rc = netlink_socket(&nl_listen, RTMGRP_IPV6_ROUTE); rc = netlink_socket(&nl_listen, RTMGRP_IPV6_ROUTE | RTMGRP_IPV4_ROUTE);
if(rc < 0) { if(rc < 0) {
perror("netlink_socket(RTMGRP_IPV6_ROUTE)"); perror("netlink_socket(RTMGRP_ROUTE)");
kernel_socket = -1; kernel_socket = -1;
return -1; return -1;
} }
...@@ -752,16 +752,27 @@ parse_kernel_route_rta(struct rtmsg *rtm, int len, struct kernel_route *route) ...@@ -752,16 +752,27 @@ parse_kernel_route_rta(struct rtmsg *rtm, int len, struct kernel_route *route)
memset(&route->prefix, 0, sizeof(struct in6_addr)); memset(&route->prefix, 0, sizeof(struct in6_addr));
memset(&route->gw, 0, sizeof(struct in6_addr)); memset(&route->gw, 0, sizeof(struct in6_addr));
route->plen = rtm->rtm_dst_len; route->plen = rtm->rtm_dst_len;
if(rtm->rtm_family == AF_INET) route->plen += 96;
route->metric = KERNEL_INFINITY; route->metric = KERNEL_INFINITY;
route->ifindex = 0; route->ifindex = 0;
#define COPY_ADDR(d, s) \
do { \
if(rtm->rtm_family == AF_INET6) \
memcpy(d, s, 16); \
else if(rtm->rtm_family == AF_INET) \
v4tov6(d, s); \
else \
return -1; \
} while(0)
while(RTA_OK(rta, len)) { while(RTA_OK(rta, len)) {
switch (rta->rta_type) { switch (rta->rta_type) {
case RTA_DST: case RTA_DST:
memcpy(&route->prefix, RTA_DATA(rta), 16); COPY_ADDR(route->prefix, RTA_DATA(rta));
break; break;
case RTA_GATEWAY: case RTA_GATEWAY:
memcpy(&route->gw, RTA_DATA(rta), 16); COPY_ADDR(route->gw, RTA_DATA(rta));
break; break;
case RTA_OIF: case RTA_OIF:
route->ifindex = *(int*)RTA_DATA(rta); route->ifindex = *(int*)RTA_DATA(rta);
...@@ -779,6 +790,7 @@ parse_kernel_route_rta(struct rtmsg *rtm, int len, struct kernel_route *route) ...@@ -779,6 +790,7 @@ parse_kernel_route_rta(struct rtmsg *rtm, int len, struct kernel_route *route)
} }
rta = RTA_NEXT(rta, len); rta = RTA_NEXT(rta, len);
} }
#undef COPY_ADDR
if(table != RT_TABLE_MAIN) if(table != RT_TABLE_MAIN)
return -1; return -1;
...@@ -890,11 +902,12 @@ filter_kernel_routes(struct nlmsghdr *nh, void *data) ...@@ -890,11 +902,12 @@ filter_kernel_routes(struct nlmsghdr *nh, void *data)
int int
kernel_routes(int maxplen, struct kernel_route *routes, int maxroutes) kernel_routes(int maxplen, struct kernel_route *routes, int maxroutes)
{ {
int rc; int i, rc;
int maxp = maxplen; int maxp = maxplen;
int maxr = maxroutes; int maxr = maxroutes;
int found = 0; int found = 0;
void *data[4] = { &maxp, &maxr, routes, &found }; void *data[4] = { &maxp, &maxr, routes, &found };
int families[2] = { AF_INET6, AF_INET };
struct rtgenmsg g; struct rtgenmsg g;
if(!nl_setup) { if(!nl_setup) {
...@@ -911,8 +924,9 @@ kernel_routes(int maxplen, struct kernel_route *routes, int maxroutes) ...@@ -911,8 +924,9 @@ kernel_routes(int maxplen, struct kernel_route *routes, int maxroutes)
} }
} }
for(i = 0; i < 2; i++) {
memset(&g, 0, sizeof(g)); memset(&g, 0, sizeof(g));
g.rtgen_family = AF_INET6; g.rtgen_family = families[i];
rc = netlink_send_dump(RTM_GETROUTE, &g, sizeof(g)); rc = netlink_send_dump(RTM_GETROUTE, &g, sizeof(g));
if(rc < 0) if(rc < 0)
return -1; return -1;
...@@ -922,6 +936,7 @@ kernel_routes(int maxplen, struct kernel_route *routes, int maxroutes) ...@@ -922,6 +936,7 @@ kernel_routes(int maxplen, struct kernel_route *routes, int maxroutes)
if(rc < 0) if(rc < 0)
return -1; return -1;
}
return found; return found;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment