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