nl-route-add.c revision d84430702496f617c01c5e2d27d0e82e02390bb7
169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/*
269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * src/nl-route-add.c     Route addition utility
369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *	This library is free software; you can redistribute it and/or
569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *	modify it under the terms of the GNU Lesser General Public
669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *	License as published by the Free Software Foundation version 2.1
769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *	of the License.
869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
1069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
1169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal#include "route-utils.h"
1369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalstatic int quiet = 0;
1569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalstatic struct nl_cache *link_cache, *route_cache;
1669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalstatic void print_usage(void)
1869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal{
1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	printf(
2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	"Usage: nl-route-add [OPTION]... [ROUTE]\n"
2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	"\n"
2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	"Options\n"
2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	" -q, --quiet		Do not print informal notifications\n"
2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	" -h, --help            Show this help\n"
2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	" -v, --version		Show versioning information\n"
2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	"\n"
2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	"Route Options\n"
2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	" -d, --dst=ADDR        destination prefix, e.g. 10.10.0.0/16\n"
2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	" -n, --nexthop=NH      nexthop configuration:\n"
3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	"                         dev=DEV         route via device\n"
3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	"                         weight=WEIGHT   weight of nexthop\n"
3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	"                         flags=FLAGS\n"
3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	"                         via=GATEWAY     route via other node\n"
3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	"                         realms=REALMS\n"
3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	"                         e.g. dev=eth0,via=192.168.1.12\n"
3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	" -t, --table=TABLE     Routing table\n"
3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	"     --family=FAMILY	Address family\n"
3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	"     --src=ADDR        Source prefix\n"
3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	"     --iif=DEV         Incomming interface\n"
4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	"     --pref-src=ADDR   Preferred source address\n"
4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	"     --metrics=OPTS    Metrics configurations\n"
4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	"     --priority=NUM    Priotity\n"
4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	"     --scope=SCOPE     Scope\n"
4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	"     --protocol=PROTO  Protocol\n"
4569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	"     --type=TYPE       { unicast | local | broadcast | multicast }\n"
4669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	);
4769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	exit(0);
4869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
4969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
5069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalint main(int argc, char *argv[])
5169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal{
5269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	struct nl_handle *sock;
5369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	struct rtnl_route *route;
5469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	struct nl_dump_params dp = {
5569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		.dp_type = NL_DUMP_LINE,
5669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		.dp_fd = stdout,
5769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	};
5869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	int err = 1;
5969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
6069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	sock = nlt_alloc_socket();
6169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	nlt_connect(sock, NETLINK_ROUTE);
6269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	link_cache = nlt_alloc_link_cache(sock);
6369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	route_cache = nlt_alloc_route_cache(sock, 0);
6469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	route = nlt_alloc_route();
6569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
6669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	for (;;) {
6769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		int c, optidx = 0;
6869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		enum {
6969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			ARG_FAMILY = 257,
7069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			ARG_SRC = 258,
7169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			ARG_IIF,
7269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			ARG_PREF_SRC,
7369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			ARG_METRICS,
7469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			ARG_PRIORITY,
7569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			ARG_SCOPE,
7669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			ARG_PROTOCOL,
7769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			ARG_TYPE,
7869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		};
7969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		static struct option long_opts[] = {
8069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			{ "quiet", 0, 0, 'q' },
8169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			{ "help", 0, 0, 'h' },
8269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			{ "version", 0, 0, 'v' },
8369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			{ "dst", 1, 0, 'd' },
8469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			{ "nexthop", 1, 0, 'n' },
8569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			{ "table", 1, 0, 't' },
8669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			{ "family", 1, 0, ARG_FAMILY },
8769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			{ "src", 1, 0, ARG_SRC },
8869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			{ "iif", 1, 0, ARG_IIF },
8969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			{ "pref-src", 1, 0, ARG_PREF_SRC },
9069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			{ "metrics", 1, 0, ARG_METRICS },
9169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			{ "priority", 1, 0, ARG_PRIORITY },
9269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			{ "scope", 1, 0, ARG_SCOPE },
9369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			{ "protocol", 1, 0, ARG_PROTOCOL },
9469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			{ "type", 1, 0, ARG_TYPE },
9569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			{ 0, 0, 0, 0 }
9669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		};
9769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
9869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		c = getopt_long(argc, argv, "qhvd:n:t:", long_opts, &optidx);
9969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		if (c == -1)
10069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal			break;
10169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
10269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		switch (c) {
10369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		case 'q': quiet = 1; break;
10469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		case 'h': print_usage(); break;
10569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		case 'v': nlt_print_version(); break;
10669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		case 'd': parse_dst(route, optarg); break;
10769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		case 'n': parse_nexthop(route, optarg, link_cache); break;
10869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		case 't': parse_table(route, optarg); break;
10969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		case ARG_FAMILY: parse_family(route, optarg); break;
11069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		case ARG_SRC: parse_src(route, optarg); break;
11169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		case ARG_IIF: parse_iif(route, optarg, link_cache); break;
11269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		case ARG_PREF_SRC: parse_pref_src(route, optarg); break;
11369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		case ARG_METRICS: parse_metric(route, optarg); break;
11469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		case ARG_PRIORITY: parse_prio(route, optarg); break;
11569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		case ARG_SCOPE: parse_scope(route, optarg); break;
11669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		case ARG_PROTOCOL: parse_protocol(route, optarg); break;
11769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		case ARG_TYPE: parse_type(route, optarg); break;
11869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		}
11969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	}
12069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
12169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	if ((err = rtnl_route_add(sock, route, 0)) < 0)
12269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		fatal(err, "Unable to add route: %s", nl_geterror(err));
12369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
12469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	if (!quiet) {
12569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		printf("Added ");
12669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal		nl_object_dump(OBJ_CAST(route), &dp);
12769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	}
12869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
12969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal	return 0;
13069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
13169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal