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