1
/* -*- coding: utf-8 -*- */
3
* iprouteadddel - Add or delete direct route to a local IP address
5
* Copyright © 2015-2017 Teddy Hogeborn
6
* Copyright © 2015-2017 Björn Påhlsson
8
* This program is free software: you can redistribute it and/or
9
* modify it under the terms of the GNU General Public License as
10
* published by the Free Software Foundation, either version 3 of the
11
* License, or (at your option) any later version.
13
* This program is distributed in the hope that it will be useful, but
14
* WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program. If not, see
20
* <http://www.gnu.org/licenses/>.
22
* Contact the authors at <mandos@recompile.se>.
25
#define _GNU_SOURCE /* asprintf(),
26
program_invocation_short_name */
27
#include <stdbool.h> /* bool, false, true */
28
#include <stdio.h> /* fprintf(), stderr, FILE, vfprintf */
29
#include <errno.h> /* program_invocation_short_name,
30
errno, perror(), EINVAL, ENOMEM */
31
#include <stdarg.h> /* va_list, va_start */
32
#include <stdlib.h> /* EXIT_SUCCESS */
33
#include <argp.h> /* struct argp_option, error_t, struct
34
argp_state, ARGP_KEY_ARG,
35
argp_usage(), ARGP_KEY_END,
36
ARGP_ERR_UNKNOWN, struct argp,
38
#include <sysexits.h> /* EX_USAGE, EX_OSERR */
39
#include <netinet/ip.h> /* sa_family_t, AF_INET6, AF_INET */
40
#include <inttypes.h> /* PRIdMAX, intmax_t */
42
#include <netlink/netlink.h> /* struct nl_addr, nl_addr_parse(),
46
#include <netlink/route/route.h> /* struct rtnl_route,
49
rtnl_route_set_family(),
50
rtnl_route_set_protocol(),
52
rtnl_route_set_scope(),
54
rtnl_route_set_type(),
57
rtnl_route_set_table(),
59
rtnl_route_nh_alloc(),
60
rtnl_route_nh_set_ifindex(),
61
rtnl_route_add_nexthop(),
65
rtnl_route_nh_free() */
66
#include <netlink/socket.h> /* struct nl_sock, nl_socket_alloc(),
67
nl_connect(), nl_socket_free() */
68
#include <netlink/route/link.h> /* rtnl_link_get_kernel(),
69
rtnl_link_get_ifindex(),
73
const char *argp_program_version = "mandos-client-iprouteadddel " VERSION;
74
const char *argp_program_bug_address = "<mandos@recompile.se>";
76
/* Function to use when printing errors */
77
void perror_plus(const char *print_text){
79
fprintf(stderr, "Mandos plugin helper %s: ",
80
program_invocation_short_name);
85
__attribute__((format (gnu_printf, 2, 3), nonnull))
86
int fprintf_plus(FILE *stream, const char *format, ...){
88
va_start (ap, format);
90
fprintf(stream, "Mandos plugin helper %s: ",
91
program_invocation_short_name);
92
return vfprintf(stream, format, ap);
95
int main(int argc, char *argv[]){
97
int exitcode = EXIT_SUCCESS;
99
bool add; /* true: add, false: delete */
100
char *address; /* IP address as string */
101
struct nl_addr *nl_addr; /* Netlink IP address */
102
char *interface; /* interface name */
103
} arguments = { .add = true, .address = NULL, .interface = NULL };
104
struct argp_option options[] = {
105
{ .name = "debug", .key = 128,
106
.doc = "Debug mode" },
109
struct rtnl_route *route = NULL;
110
struct rtnl_nexthop *nexthop = NULL;
111
struct nl_sock *sk = NULL;
113
error_t parse_opt(int key, char *arg, struct argp_state *state){
117
case 128: /* --debug */
121
switch(state->arg_num){
123
if(strcasecmp(arg, "add") == 0){
124
((struct arguments *)(state->input))->add = true;
125
} else if(strcasecmp(arg, "delete") == 0){
126
((struct arguments *)(state->input))->add = false;
128
fprintf_plus(stderr, "Unrecognized command: %s\n", arg);
133
((struct arguments *)(state->input))->address = arg;
134
lret = nl_addr_parse(arg, AF_UNSPEC, &(((struct arguments *)
138
fprintf_plus(stderr, "Failed to parse address %s: %s\n",
139
arg, nl_geterror(lret));
144
((struct arguments *)(state->input))->interface = arg;
151
if(state->arg_num < 3){
156
return ARGP_ERR_UNKNOWN;
161
struct argp argp = { .options = options, .parser = parse_opt,
162
.args_doc = "[ add | delete ] ADDRESS INTERFACE",
163
.doc = "Mandos client helper -- Add or delete"
164
" local route to IP address on interface" };
166
ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, &arguments);
175
perror_plus("argp_parse");
179
/* Get netlink socket */
180
sk = nl_socket_alloc();
182
fprintf_plus(stderr, "Failed to allocate netlink socket: %s\n",
187
/* Connect socket to netlink */
188
ret = nl_connect(sk, NETLINK_ROUTE);
190
fprintf_plus(stderr, "Failed to connect socket to netlink: %s\n",
195
/* Get link object of specified interface */
196
struct rtnl_link *link = NULL;
197
ret = rtnl_link_get_kernel(sk, 0, arguments.interface, &link);
199
fprintf_plus(stderr, "Failed to use interface %s: %s\n",
200
arguments.interface, nl_geterror(ret));
204
/* Get netlink route object */
205
route = rtnl_route_alloc();
207
fprintf_plus(stderr, "Failed to get netlink route:\n");
211
/* Get address family of specified address */
212
sa_family_t af = (sa_family_t)nl_addr_get_family(arguments.nl_addr);
214
fprintf_plus(stderr, "Address family of %s is %s (%" PRIdMAX
215
")\n", arguments.address,
216
af == AF_INET6 ? "AF_INET6" :
217
( af == AF_INET ? "AF_INET" : "UNKNOWN"),
220
/* Set route parameters: */
221
rtnl_route_set_family(route, (uint8_t)af); /* Address family */
222
rtnl_route_set_protocol(route, RTPROT_BOOT); /* protocol - see
224
rtnl_route_set_scope(route, RT_SCOPE_LINK); /* link scope */
225
rtnl_route_set_type(route, RTN_UNICAST); /* normal unicast
227
rtnl_route_set_dst(route, arguments.nl_addr); /* Destination
229
rtnl_route_set_table(route, RT_TABLE_MAIN); /* "main" routing
232
nexthop = rtnl_route_nh_alloc();
234
fprintf_plus(stderr, "Failed to get netlink route nexthop\n");
238
/* Get index number of specified interface */
239
int ifindex = rtnl_link_get_ifindex(link);
241
fprintf_plus(stderr, "ifindex of %s is %d\n", arguments.interface,
244
/* Set interface index number on nexthop object */
245
rtnl_route_nh_set_ifindex(nexthop, ifindex);
246
/* Set route tu use nexthop object */
247
rtnl_route_add_nexthop(route, nexthop);
248
/* Add or delete route? */
250
ret = rtnl_route_add(sk, route, NLM_F_EXCL);
252
ret = rtnl_route_delete(sk, route, 0);
255
fprintf_plus(stderr, "Failed to %s route: %s\n",
256
arguments.add ? "add" : "delete",
262
/* Deallocate route */
264
rtnl_route_put(route);
266
/* Deallocate route nexthop */
267
rtnl_route_nh_free(nexthop);
269
/* Deallocate parsed address */
270
if(arguments.nl_addr){
271
nl_addr_put(arguments.nl_addr);
273
/* Deallocate link struct */
277
/* Deallocate netlink socket struct */