| 1 |
#include <unistd.h> |
| 2 |
#include <stdio.h> |
| 3 |
#include <string.h> |
| 4 |
#include <errno.h> |
| 5 |
#include <sys/socket.h> |
| 6 |
#include <linux/types.h> |
| 7 |
#include <linux/if.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <assert.h> |
| 10 |
|
| 11 |
#include "nlrequest.h" |
| 12 |
#include "getifn.h" |
| 13 |
|
| 14 |
#define MAX_ROUTES 64 |
| 15 |
|
| 16 |
struct nlmsghdr* routes[MAX_ROUTES]; |
| 17 |
int n_routes = 0; |
| 18 |
|
| 19 |
int enumerate_callback(struct nlmsghdr *n, void *u) { |
| 20 |
struct rtmsg *r; |
| 21 |
struct rtattr *a = NULL; |
| 22 |
int l, *ifn = u; |
| 23 |
|
| 24 |
if (n->nlmsg_type != RTM_NEWROUTE) { |
| 25 |
fprintf(stderr, "NETLINK: Got response for wrong request.\n"); |
| 26 |
return -1; |
| 27 |
} |
| 28 |
|
| 29 |
r = NLMSG_DATA(n); |
| 30 |
l = NLMSG_PAYLOAD(n, sizeof(struct rtmsg)); |
| 31 |
a = RTM_RTA(r); |
| 32 |
|
| 33 |
if (r->rtm_table != RT_TABLE_MAIN) |
| 34 |
return 0; |
| 35 |
|
| 36 |
while(RTA_OK(a, l)) { |
| 37 |
switch(a->rta_type) { |
| 38 |
case RTA_OIF: |
| 39 |
|
| 40 |
if (RTA_PAYLOAD(a) != sizeof(int)) { |
| 41 |
fprintf(stderr, "NETLINK: Recieved corrupt RTA_OIF payload.\n"); |
| 42 |
return -1; |
| 43 |
} |
| 44 |
|
| 45 |
if (*((int*) RTA_DATA(a)) == *ifn) { |
| 46 |
|
| 47 |
if (n_routes < MAX_ROUTES) { |
| 48 |
struct nlmsghdr* copy; |
| 49 |
|
| 50 |
if (!(copy = malloc(n->nlmsg_len))) { |
| 51 |
fprintf(stderr, "Could not allocate memory.\n"); |
| 52 |
return -1; |
| 53 |
} |
| 54 |
|
| 55 |
memcpy(copy, n, n->nlmsg_len); |
| 56 |
routes[n_routes++] = copy; |
| 57 |
|
| 58 |
} else |
| 59 |
fprintf(stderr, "Found too many routes.\n"); |
| 60 |
|
| 61 |
break; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
a = RTA_NEXT(a, l); |
| 68 |
} |
| 69 |
|
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
int enumerate(int s, int ifn) { |
| 74 |
struct { |
| 75 |
struct nlmsghdr n; |
| 76 |
struct rtmsg r; |
| 77 |
char a[1024]; |
| 78 |
} req; |
| 79 |
|
| 80 |
memset(&req, 0, sizeof(req)); |
| 81 |
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); |
| 82 |
req.n.nlmsg_flags = NLM_F_REQUEST|NLM_F_MATCH; |
| 83 |
req.n.nlmsg_type = RTM_GETROUTE; |
| 84 |
|
| 85 |
req.r.rtm_family = AF_INET; |
| 86 |
req.r.rtm_table = RT_TABLE_MAIN; |
| 87 |
|
| 88 |
return netlink_request(s, (struct nlmsghdr*) &req, enumerate_callback, &ifn); |
| 89 |
} |
| 90 |
|
| 91 |
struct nlmsghdr* set_route_metric(struct nlmsghdr* n, int metric) { |
| 92 |
struct rtmsg *r; |
| 93 |
struct rtattr *a = NULL; |
| 94 |
int l, t; |
| 95 |
|
| 96 |
r = NLMSG_DATA(n); |
| 97 |
l = NLMSG_PAYLOAD(n, sizeof(struct rtmsg)); |
| 98 |
a = RTM_RTA(r); |
| 99 |
|
| 100 |
while(RTA_OK(a, l)) { |
| 101 |
switch(a->rta_type) { |
| 102 |
case RTA_PRIORITY: |
| 103 |
|
| 104 |
if (RTA_PAYLOAD(a) != sizeof(int)) { |
| 105 |
fprintf(stderr, "NETLINK: Recieved corrupt RTA_PRIORITY payload.\n"); |
| 106 |
return NULL; |
| 107 |
} |
| 108 |
|
| 109 |
*((int*) RTA_DATA(a)) = metric; |
| 110 |
return n; |
| 111 |
} |
| 112 |
|
| 113 |
a = RTA_NEXT(a, l); |
| 114 |
} |
| 115 |
|
| 116 |
if ((n = realloc(n, (t = n->nlmsg_len+1024)))) |
| 117 |
addattr32(n, t, RTA_PRIORITY, metric); |
| 118 |
else |
| 119 |
fprintf(stderr, "realloc() failed.\n"); |
| 120 |
|
| 121 |
return n; |
| 122 |
} |
| 123 |
|
| 124 |
int delete_route(int s, struct nlmsghdr* n) { |
| 125 |
assert(s >= 0 && n); |
| 126 |
|
| 127 |
n->nlmsg_type = RTM_DELROUTE; |
| 128 |
n->nlmsg_flags = NLM_F_REQUEST; |
| 129 |
|
| 130 |
return netlink_request(s, n, NULL, NULL); |
| 131 |
} |
| 132 |
|
| 133 |
int add_route(int s, struct nlmsghdr* n) { |
| 134 |
assert(s >= 0 && n); |
| 135 |
|
| 136 |
n->nlmsg_type = RTM_NEWROUTE; |
| 137 |
n->nlmsg_flags = NLM_F_REQUEST|NLM_F_CREATE; |
| 138 |
|
| 139 |
return netlink_request(s, n, NULL, NULL); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
int go(char *iface, int metric) { |
| 144 |
int r = -1, j; |
| 145 |
int s = -1, ifn; |
| 146 |
|
| 147 |
if ((s = netlink_open()) < 0) |
| 148 |
return -1; |
| 149 |
|
| 150 |
if ((ifn = getifn(s, iface)) < 0) |
| 151 |
goto finish; |
| 152 |
|
| 153 |
n_routes = 0; |
| 154 |
if (enumerate(s, ifn) < 0) |
| 155 |
goto finish; |
| 156 |
|
| 157 |
if (n_routes) { |
| 158 |
for (j = 0; j < n_routes; j++) { |
| 159 |
if (delete_route(s, routes[j]) >= 0) |
| 160 |
if ((routes[j] = set_route_metric(routes[j], metric))) |
| 161 |
add_route(s, routes[j]); |
| 162 |
|
| 163 |
free(routes[j]); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
r = 0; |
| 168 |
|
| 169 |
finish: |
| 170 |
|
| 171 |
if (s >= 0) |
| 172 |
close(s); |
| 173 |
|
| 174 |
return r; |
| 175 |
} |
| 176 |
|
| 177 |
int main(int argc, char *argv[]) { |
| 178 |
char *iface; |
| 179 |
int metric; |
| 180 |
|
| 181 |
if (argc <= 1 || (argc > 1 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")))) { |
| 182 |
char *b; |
| 183 |
|
| 184 |
if ((b = strrchr(argv[0], '/'))) |
| 185 |
b++; |
| 186 |
else |
| 187 |
b = argv[0]; |
| 188 |
|
| 189 |
printf("Usage: %s <iface> [metric]\n" |
| 190 |
"\n" |
| 191 |
"%s is a tool for setting the metrics of all IPv4 routes\n" |
| 192 |
"attached to a given network interface at once.\n" |
| 193 |
"\n" |
| 194 |
" <iface> The interface\n" |
| 195 |
" <metric> The new metric (default: 0)\n", b, b); |
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
iface = argv[1]; |
| 200 |
metric = argc > 2 ? atoi(argv[2]) : 0; |
| 201 |
|
| 202 |
return go(iface, metric) < 0 ? 1 : 0; |
| 203 |
|
| 204 |
} |