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