1
/* -*- coding: utf-8 -*- */
3
* iprouteadddel - Add or delete direct route to a local IP address
5
* Copyright © 2015-2018 Teddy Hogeborn
6
* Copyright © 2015-2018 Björn Påhlsson
8
* This file is part of Mandos.
10
* Mandos is free software: you can redistribute it and/or modify it
11
* under the terms of the GNU General Public License as published by
12
* the Free Software Foundation, either version 3 of the License, or
13
* (at your option) any later version.
15
* Mandos is distributed in the hope that it will be useful, but
16
* WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
* General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with Mandos. If not, see <http://www.gnu.org/licenses/>.
23
* Contact the authors at <mandos@recompile.se>.
26
#define _GNU_SOURCE /* asprintf(),
27
program_invocation_short_name */
28
#include <stdbool.h> /* bool, false, true */
29
#include <stdio.h> /* fprintf(), stderr, FILE, vfprintf */
30
#include <errno.h> /* program_invocation_short_name,
31
errno, perror(), EINVAL, ENOMEM */
32
#include <stdarg.h> /* va_list, va_start */
33
#include <stdlib.h> /* EXIT_SUCCESS */
34
#include <argp.h> /* struct argp_option, error_t, struct
35
argp_state, ARGP_KEY_ARG,
36
argp_usage(), ARGP_KEY_END,
37
ARGP_ERR_UNKNOWN, struct argp,
39
#include <sysexits.h> /* EX_USAGE, EX_OSERR */
40
#include <netinet/ip.h> /* sa_family_t, AF_INET6, AF_INET */
41
#include <inttypes.h> /* PRIdMAX, intmax_t */
43
#include <netlink/netlink.h> /* struct nl_addr, nl_addr_parse(),
47
#include <netlink/route/route.h> /* struct rtnl_route,
50
rtnl_route_set_family(),
51
rtnl_route_set_protocol(),
53
rtnl_route_set_scope(),
55
rtnl_route_set_type(),
58
rtnl_route_set_table(),
60
rtnl_route_nh_alloc(),
61
rtnl_route_nh_set_ifindex(),
62
rtnl_route_add_nexthop(),
66
rtnl_route_nh_free() */
67
#include <netlink/socket.h> /* struct nl_sock, nl_socket_alloc(),
68
nl_connect(), nl_socket_free() */
69
#include <netlink/route/link.h> /* rtnl_link_get_kernel(),
70
rtnl_link_get_ifindex(),
74
const char *argp_program_version = "mandos-client-iprouteadddel " VERSION;
75
const char *argp_program_bug_address = "<mandos@recompile.se>";
77
/* Function to use when printing errors */
78
void perror_plus(const char *print_text){
80
fprintf(stderr, "Mandos plugin helper %s: ",
81
program_invocation_short_name);
86
__attribute__((format (gnu_printf, 2, 3), nonnull))
87
int fprintf_plus(FILE *stream, const char *format, ...){
89
va_start (ap, format);
91
fprintf(stream, "Mandos plugin helper %s: ",
92
program_invocation_short_name);
93
return vfprintf(stream, format, ap);
96
int main(int argc, char *argv[]){
98
int exitcode = EXIT_SUCCESS;
100
bool add; /* true: add, false: delete */
101
char *address; /* IP address as string */
102
struct nl_addr *nl_addr; /* Netlink IP address */
103
char *interface; /* interface name */
104
} arguments = { .add = true, .address = NULL, .interface = NULL };
105
struct argp_option options[] = {
106
{ .name = "debug", .key = 128,
107
.doc = "Debug mode" },
110
struct rtnl_route *route = NULL;
111
struct rtnl_nexthop *nexthop = NULL;
112
struct nl_sock *sk = NULL;
114
error_t parse_opt(int key, char *arg, struct argp_state *state){
118
case 128: /* --debug */
122
switch(state->arg_num){
124
if(strcasecmp(arg, "add") == 0){
125
((struct arguments *)(state->input))->add = true;
126
} else if(strcasecmp(arg, "delete") == 0){
127
((struct arguments *)(state->input))->add = false;
129
fprintf_plus(stderr, "Unrecognized command: %s\n", arg);
134
((struct arguments *)(state->input))->address = arg;
135
lret = nl_addr_parse(arg, AF_UNSPEC, &(((struct arguments *)
139
fprintf_plus(stderr, "Failed to parse address %s: %s\n",
140
arg, nl_geterror(lret));
145
((struct arguments *)(state->input))->interface = arg;
152
if(state->arg_num < 3){
157
return ARGP_ERR_UNKNOWN;
162
struct argp argp = { .options = options, .parser = parse_opt,
163
.args_doc = "[ add | delete ] ADDRESS INTERFACE",
164
.doc = "Mandos client helper -- Add or delete"
165
" local route to IP address on interface" };
167
ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, 0, &arguments);
176
perror_plus("argp_parse");
180
/* Get netlink socket */
181
sk = nl_socket_alloc();
183
fprintf_plus(stderr, "Failed to allocate netlink socket: %s\n",
188
/* Connect socket to netlink */
189
ret = nl_connect(sk, NETLINK_ROUTE);
191
fprintf_plus(stderr, "Failed to connect socket to netlink: %s\n",
196
/* Get link object of specified interface */
197
struct rtnl_link *link = NULL;
198
ret = rtnl_link_get_kernel(sk, 0, arguments.interface, &link);
200
fprintf_plus(stderr, "Failed to use interface %s: %s\n",
201
arguments.interface, nl_geterror(ret));
205
/* Get netlink route object */
206
route = rtnl_route_alloc();
208
fprintf_plus(stderr, "Failed to get netlink route:\n");
212
/* Get address family of specified address */
213
sa_family_t af = (sa_family_t)nl_addr_get_family(arguments.nl_addr);
215
fprintf_plus(stderr, "Address family of %s is %s (%" PRIdMAX
216
")\n", arguments.address,
217
af == AF_INET6 ? "AF_INET6" :
218
( af == AF_INET ? "AF_INET" : "UNKNOWN"),
221
/* Set route parameters: */
222
rtnl_route_set_family(route, (uint8_t)af); /* Address family */
223
rtnl_route_set_protocol(route, RTPROT_BOOT); /* protocol - see
225
rtnl_route_set_scope(route, RT_SCOPE_LINK); /* link scope */
226
rtnl_route_set_type(route, RTN_UNICAST); /* normal unicast
228
rtnl_route_set_dst(route, arguments.nl_addr); /* Destination
230
rtnl_route_set_table(route, RT_TABLE_MAIN); /* "main" routing
233
nexthop = rtnl_route_nh_alloc();
235
fprintf_plus(stderr, "Failed to get netlink route nexthop\n");
239
/* Get index number of specified interface */
240
int ifindex = rtnl_link_get_ifindex(link);
242
fprintf_plus(stderr, "ifindex of %s is %d\n", arguments.interface,
245
/* Set interface index number on nexthop object */
246
rtnl_route_nh_set_ifindex(nexthop, ifindex);
247
/* Set route tu use nexthop object */
248
rtnl_route_add_nexthop(route, nexthop);
249
/* Add or delete route? */
251
ret = rtnl_route_add(sk, route, NLM_F_EXCL);
253
ret = rtnl_route_delete(sk, route, 0);
256
fprintf_plus(stderr, "Failed to %s route: %s\n",
257
arguments.add ? "add" : "delete",
263
/* Deallocate route */
265
rtnl_route_put(route);
267
/* Deallocate route nexthop */
268
rtnl_route_nh_free(nexthop);
270
/* Deallocate parsed address */
271
if(arguments.nl_addr){
272
nl_addr_put(arguments.nl_addr);
274
/* Deallocate link struct */
278
/* Deallocate netlink socket struct */