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