xref: /aosp_15_r20/external/libnl/src/nl-route-add.c (revision 4dc78e53d49367fa8e61b07018507c90983a077d)
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2003-2009 Thomas Graf <[email protected]>
4  */
5 
6 #include "nl-default.h"
7 
8 #include <linux/netlink.h>
9 
10 #include <netlink/cli/utils.h>
11 #include <netlink/cli/route.h>
12 #include <netlink/cli/link.h>
13 
14 static int quiet = 0;
15 static struct nl_cache *link_cache, *route_cache;
16 
print_usage(void)17 static 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 
main(int argc,char * argv[])50 int main(int argc, char *argv[])
51 {
52 	struct nl_sock *sock;
53 	struct rtnl_route *route;
54 	struct nl_dump_params dp = {
55 		.dp_type = NL_DUMP_LINE,
56 		.dp_fd = stdout,
57 	};
58 	int err = 1;
59 
60 	sock = nl_cli_alloc_socket();
61 	nl_cli_connect(sock, NETLINK_ROUTE);
62 	link_cache = nl_cli_link_alloc_cache(sock);
63 	route_cache = nl_cli_route_alloc_cache(sock, 0);
64 	route = nl_cli_route_alloc();
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': nl_cli_print_version(); break;
106 		case 'd': nl_cli_route_parse_dst(route, optarg); break;
107 		case 'n': nl_cli_route_parse_nexthop(route, optarg, link_cache); break;
108 		case 't': nl_cli_route_parse_table(route, optarg); break;
109 		case ARG_FAMILY: nl_cli_route_parse_family(route, optarg); break;
110 		case ARG_SRC: nl_cli_route_parse_src(route, optarg); break;
111 		case ARG_IIF: nl_cli_route_parse_iif(route, optarg, link_cache); break;
112 		case ARG_PREF_SRC: nl_cli_route_parse_pref_src(route, optarg); break;
113 		case ARG_METRICS: nl_cli_route_parse_metric(route, optarg); break;
114 		case ARG_PRIORITY: nl_cli_route_parse_prio(route, optarg); break;
115 		case ARG_SCOPE: nl_cli_route_parse_scope(route, optarg); break;
116 		case ARG_PROTOCOL: nl_cli_route_parse_protocol(route, optarg); break;
117 		case ARG_TYPE: nl_cli_route_parse_type(route, optarg); break;
118 		}
119 	}
120 
121 	if ((err = rtnl_route_add(sock, route, NLM_F_EXCL)) < 0)
122 		nl_cli_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