nl-route-add.c revision 10cf5a586c149fdb7e2639000dbfae5e6f8522a5
1/*
2 * src/nl-route-add.c     Route addition utility
3 *
4 *	This library is free software; you can redistribute it and/or
5 *	modify it under the terms of the GNU Lesser General Public
6 *	License as published by the Free Software Foundation version 2.1
7 *	of the License.
8 *
9 * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
10 */
11
12#include "route-utils.h"
13
14static int quiet = 0;
15static struct nl_cache *link_cache, *route_cache;
16
17static void print_usage(void)
18{
19	printf(
20	"Usage: nl-route-add [OPTION]... [ROUTE]\n"
21	"\n"
22	"Options\n"
23	" -q, --quiet		Do not print informal notifications\n"
24	" -h, --help            Show this help\n"
25	" -v, --version		Show versioning information\n"
26	"\n"
27	"Route Options\n"
28	" -d, --dst=ADDR        destination prefix, e.g. 10.10.0.0/16\n"
29	" -n, --nexthop=NH      nexthop configuration:\n"
30	"                         dev=DEV         route via device\n"
31	"                         weight=WEIGHT   weight of nexthop\n"
32	"                         flags=FLAGS\n"
33	"                         via=GATEWAY     route via other node\n"
34	"                         realms=REALMS\n"
35	"                         e.g. dev=eth0,via=192.168.1.12\n"
36	" -t, --table=TABLE     Routing table\n"
37	"     --family=FAMILY	Address family\n"
38	"     --src=ADDR        Source prefix\n"
39	"     --iif=DEV         Incomming interface\n"
40	"     --pref-src=ADDR   Preferred source address\n"
41	"     --metrics=OPTS    Metrics configurations\n"
42	"     --priority=NUM    Priotity\n"
43	"     --scope=SCOPE     Scope\n"
44	"     --protocol=PROTO  Protocol\n"
45	"     --type=TYPE       { unicast | local | broadcast | multicast }\n"
46	);
47	exit(0);
48}
49
50int main(int argc, char *argv[])
51{
52	struct nl_handle *sock;
53	struct rtnl_route *route;
54	struct nl_dump_params dp = {
55		.dp_type = NL_DUMP_ONELINE,
56		.dp_fd = stdout,
57	};
58	int err = 1;
59
60	sock = nlt_alloc_socket();
61	nlt_connect(sock, NETLINK_ROUTE);
62	link_cache = nlt_alloc_link_cache(sock);
63	route_cache = nlt_alloc_route_cache(sock, 0);
64	route = nlt_alloc_route();
65
66	for (;;) {
67		int c, optidx = 0;
68		enum {
69			ARG_FAMILY = 257,
70			ARG_SRC = 258,
71			ARG_IIF,
72			ARG_PREF_SRC,
73			ARG_METRICS,
74			ARG_PRIORITY,
75			ARG_SCOPE,
76			ARG_PROTOCOL,
77			ARG_TYPE,
78		};
79		static struct option long_opts[] = {
80			{ "quiet", 0, 0, 'q' },
81			{ "help", 0, 0, 'h' },
82			{ "version", 0, 0, 'v' },
83			{ "dst", 1, 0, 'd' },
84			{ "nexthop", 1, 0, 'n' },
85			{ "table", 1, 0, 't' },
86			{ "family", 1, 0, ARG_FAMILY },
87			{ "src", 1, 0, ARG_SRC },
88			{ "iif", 1, 0, ARG_IIF },
89			{ "pref-src", 1, 0, ARG_PREF_SRC },
90			{ "metrics", 1, 0, ARG_METRICS },
91			{ "priority", 1, 0, ARG_PRIORITY },
92			{ "scope", 1, 0, ARG_SCOPE },
93			{ "protocol", 1, 0, ARG_PROTOCOL },
94			{ "type", 1, 0, ARG_TYPE },
95			{ 0, 0, 0, 0 }
96		};
97
98		c = getopt_long(argc, argv, "qhvd:n:t:", long_opts, &optidx);
99		if (c == -1)
100			break;
101
102		switch (c) {
103		case 'q': quiet = 1; break;
104		case 'h': print_usage(); break;
105		case 'v': nlt_print_version(); break;
106		case 'd': parse_dst(route, optarg); break;
107		case 'n': parse_nexthop(route, optarg, link_cache); break;
108		case 't': parse_table(route, optarg); break;
109		case ARG_FAMILY: parse_family(route, optarg); break;
110		case ARG_SRC: parse_src(route, optarg); break;
111		case ARG_IIF: parse_iif(route, optarg, link_cache); break;
112		case ARG_PREF_SRC: parse_pref_src(route, optarg); break;
113		case ARG_METRICS: parse_metric(route, optarg); break;
114		case ARG_PRIORITY: parse_prio(route, optarg); break;
115		case ARG_SCOPE: parse_scope(route, optarg); break;
116		case ARG_PROTOCOL: parse_protocol(route, optarg); break;
117		case ARG_TYPE: parse_type(route, optarg); break;
118		}
119	}
120
121	if ((err = rtnl_route_add(sock, route, 0)) < 0)
122		fatal(err, "Unable to add route: %s", nl_geterror(err));
123
124	if (!quiet) {
125		printf("Added ");
126		nl_object_dump(OBJ_CAST(route), &dp);
127	}
128
129	return 0;
130}
131