Blame ip/iproute.c

Packit Service 3880ab
/*
Packit Service 3880ab
 * iproute.c		"ip route".
Packit Service 3880ab
 *
Packit Service 3880ab
 *		This program is free software; you can redistribute it and/or
Packit Service 3880ab
 *		modify it under the terms of the GNU General Public License
Packit Service 3880ab
 *		as published by the Free Software Foundation; either version
Packit Service 3880ab
 *		2 of the License, or (at your option) any later version.
Packit Service 3880ab
 *
Packit Service 3880ab
 * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Packit Service 3880ab
 *
Packit Service 3880ab
 */
Packit Service 3880ab
Packit Service 3880ab
#include <stdio.h>
Packit Service 3880ab
#include <stdlib.h>
Packit Service 3880ab
#include <unistd.h>
Packit Service 3880ab
#include <fcntl.h>
Packit Service 3880ab
#include <string.h>
Packit Service 3880ab
#include <time.h>
Packit Service 3880ab
#include <sys/time.h>
Packit Service 3880ab
#include <sys/socket.h>
Packit Service 3880ab
#include <netinet/in.h>
Packit Service 3880ab
#include <netinet/ip.h>
Packit Service 3880ab
#include <arpa/inet.h>
Packit Service 3880ab
#include <linux/in_route.h>
Packit Service 3880ab
#include <linux/icmpv6.h>
Packit Service 3880ab
#include <errno.h>
Packit Service 3880ab
Packit Service 3880ab
#include "rt_names.h"
Packit Service 3880ab
#include "utils.h"
Packit Service 3880ab
#include "ip_common.h"
Packit Service 3880ab
Packit Service 3880ab
#ifndef RTAX_RTTVAR
Packit Service 3880ab
#define RTAX_RTTVAR RTAX_HOPS
Packit Service 3880ab
#endif
Packit Service 3880ab
Packit Service 3880ab
enum list_action {
Packit Service 3880ab
	IPROUTE_LIST,
Packit Service 3880ab
	IPROUTE_FLUSH,
Packit Service 3880ab
	IPROUTE_SAVE,
Packit Service 3880ab
};
Packit Service 3880ab
static const char *mx_names[RTAX_MAX+1] = {
Packit Service 3880ab
	[RTAX_MTU]			= "mtu",
Packit Service 3880ab
	[RTAX_WINDOW]			= "window",
Packit Service 3880ab
	[RTAX_RTT]			= "rtt",
Packit Service 3880ab
	[RTAX_RTTVAR]			= "rttvar",
Packit Service 3880ab
	[RTAX_SSTHRESH]			= "ssthresh",
Packit Service 3880ab
	[RTAX_CWND]			= "cwnd",
Packit Service 3880ab
	[RTAX_ADVMSS]			= "advmss",
Packit Service 3880ab
	[RTAX_REORDERING]		= "reordering",
Packit Service 3880ab
	[RTAX_HOPLIMIT]			= "hoplimit",
Packit Service 3880ab
	[RTAX_INITCWND]			= "initcwnd",
Packit Service 3880ab
	[RTAX_FEATURES]			= "features",
Packit Service 3880ab
	[RTAX_RTO_MIN]			= "rto_min",
Packit Service 3880ab
	[RTAX_INITRWND]			= "initrwnd",
Packit Service 3880ab
	[RTAX_QUICKACK]			= "quickack",
Packit Service 3880ab
	[RTAX_CC_ALGO]			= "congctl",
Packit Service 3880ab
	[RTAX_FASTOPEN_NO_COOKIE]	= "fastopen_no_cookie"
Packit Service 3880ab
};
Packit Service 3880ab
static void usage(void) __attribute__((noreturn));
Packit Service 3880ab
Packit Service 3880ab
static void usage(void)
Packit Service 3880ab
{
Packit Service 3880ab
	fprintf(stderr,
Packit Service 3880ab
		"Usage: ip route { list | flush } SELECTOR\n"
Packit Service 3880ab
		"       ip route save SELECTOR\n"
Packit Service 3880ab
		"       ip route restore\n"
Packit Service 3880ab
		"       ip route showdump\n"
Packit Service 3880ab
		"       ip route get [ ROUTE_GET_FLAGS ] ADDRESS\n"
Packit Service 3880ab
		"                            [ from ADDRESS iif STRING ]\n"
Packit Service 3880ab
		"                            [ oif STRING ] [ tos TOS ]\n"
Packit Service 3880ab
		"                            [ mark NUMBER ] [ vrf NAME ]\n"
Packit Service 3880ab
		"                            [ uid NUMBER ] [ ipproto PROTOCOL ]\n"
Packit Service 3880ab
		"                            [ sport NUMBER ] [ dport NUMBER ]\n"
Packit Service 3880ab
		"       ip route { add | del | change | append | replace } ROUTE\n"
Packit Service 3880ab
		"SELECTOR := [ root PREFIX ] [ match PREFIX ] [ exact PREFIX ]\n"
Packit Service 3880ab
		"            [ table TABLE_ID ] [ vrf NAME ] [ proto RTPROTO ]\n"
Packit Service 3880ab
		"            [ type TYPE ] [ scope SCOPE ]\n"
Packit Service 3880ab
		"ROUTE := NODE_SPEC [ INFO_SPEC ]\n"
Packit Service 3880ab
		"NODE_SPEC := [ TYPE ] PREFIX [ tos TOS ]\n"
Packit Service 3880ab
		"             [ table TABLE_ID ] [ proto RTPROTO ]\n"
Packit Service 3880ab
		"             [ scope SCOPE ] [ metric METRIC ]\n"
Packit Service 3880ab
		"             [ ttl-propagate { enabled | disabled } ]\n"
Packit Service 3880ab
		"INFO_SPEC := { NH | nhid ID } OPTIONS FLAGS [ nexthop NH ]...\n"
Packit Service 3880ab
		"NH := [ encap ENCAPTYPE ENCAPHDR ] [ via [ FAMILY ] ADDRESS ]\n"
Packit Service 3880ab
		"	    [ dev STRING ] [ weight NUMBER ] NHFLAGS\n"
Packit Service 3880ab
		"FAMILY := [ inet | inet6 | mpls | bridge | link ]\n"
Packit Service 3880ab
		"OPTIONS := FLAGS [ mtu NUMBER ] [ advmss NUMBER ] [ as [ to ] ADDRESS ]\n"
Packit Service 3880ab
		"           [ rtt TIME ] [ rttvar TIME ] [ reordering NUMBER ]\n"
Packit Service 3880ab
		"           [ window NUMBER ] [ cwnd NUMBER ] [ initcwnd NUMBER ]\n"
Packit Service 3880ab
		"           [ ssthresh NUMBER ] [ realms REALM ] [ src ADDRESS ]\n"
Packit Service 3880ab
		"           [ rto_min TIME ] [ hoplimit NUMBER ] [ initrwnd NUMBER ]\n"
Packit Service 3880ab
		"           [ features FEATURES ] [ quickack BOOL ] [ congctl NAME ]\n"
Packit Service 3880ab
		"           [ pref PREF ] [ expires TIME ] [ fastopen_no_cookie BOOL ]\n"
Packit Service 3880ab
		"TYPE := { unicast | local | broadcast | multicast | throw |\n"
Packit Service 3880ab
		"          unreachable | prohibit | blackhole | nat }\n"
Packit Service 3880ab
		"TABLE_ID := [ local | main | default | all | NUMBER ]\n"
Packit Service 3880ab
		"SCOPE := [ host | link | global | NUMBER ]\n"
Packit Service 3880ab
		"NHFLAGS := [ onlink | pervasive ]\n"
Packit Service 3880ab
		"RTPROTO := [ kernel | boot | static | NUMBER ]\n"
Packit Service 3880ab
		"PREF := [ low | medium | high ]\n"
Packit Service 3880ab
		"TIME := NUMBER[s|ms]\n"
Packit Service 3880ab
		"BOOL := [1|0]\n"
Packit Service 3880ab
		"FEATURES := ecn\n"
Packit Service 3880ab
		"ENCAPTYPE := [ mpls | ip | ip6 | seg6 | seg6local | rpl ]\n"
Packit Service 3880ab
		"ENCAPHDR := [ MPLSLABEL | SEG6HDR ]\n"
Packit Service 3880ab
		"SEG6HDR := [ mode SEGMODE ] segs ADDR1,ADDRi,ADDRn [hmac HMACKEYID] [cleanup]\n"
Packit Service 3880ab
		"SEGMODE := [ encap | inline ]\n"
Packit Service 3880ab
		"ROUTE_GET_FLAGS := [ fibmatch ]\n");
Packit Service 3880ab
	exit(-1);
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
Packit Service 3880ab
static struct
Packit Service 3880ab
{
Packit Service 3880ab
	unsigned int tb;
Packit Service 3880ab
	int cloned;
Packit Service 3880ab
	int flushed;
Packit Service 3880ab
	char *flushb;
Packit Service 3880ab
	int flushp;
Packit Service 3880ab
	int flushe;
Packit Service 3880ab
	int protocol, protocolmask;
Packit Service 3880ab
	int scope, scopemask;
Packit Service 3880ab
	__u64 typemask;
Packit Service 3880ab
	int tos, tosmask;
Packit Service 3880ab
	int iif, iifmask;
Packit Service 3880ab
	int oif, oifmask;
Packit Service 3880ab
	int mark, markmask;
Packit Service 3880ab
	int realm, realmmask;
Packit Service 3880ab
	__u32 metric, metricmask;
Packit Service 3880ab
	inet_prefix rprefsrc;
Packit Service 3880ab
	inet_prefix rvia;
Packit Service 3880ab
	inet_prefix rdst;
Packit Service 3880ab
	inet_prefix mdst;
Packit Service 3880ab
	inet_prefix rsrc;
Packit Service 3880ab
	inet_prefix msrc;
Packit Service 3880ab
} filter;
Packit Service 3880ab
Packit Service 3880ab
static int flush_update(void)
Packit Service 3880ab
{
Packit Service 3880ab
	if (rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) {
Packit Service 3880ab
		perror("Failed to send flush request");
Packit Service 3880ab
		return -2;
Packit Service 3880ab
	}
Packit Service 3880ab
	filter.flushp = 0;
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int filter_nlmsg(struct nlmsghdr *n, struct rtattr **tb, int host_len)
Packit Service 3880ab
{
Packit Service 3880ab
	struct rtmsg *r = NLMSG_DATA(n);
Packit Service 3880ab
	inet_prefix dst = { .family = r->rtm_family };
Packit Service 3880ab
	inet_prefix src = { .family = r->rtm_family };
Packit Service 3880ab
	inet_prefix via = { .family = r->rtm_family };
Packit Service 3880ab
	inet_prefix prefsrc = { .family = r->rtm_family };
Packit Service 3880ab
	__u32 table;
Packit Service 3880ab
	static int ip6_multiple_tables;
Packit Service 3880ab
Packit Service 3880ab
	table = rtm_get_table(r, tb);
Packit Service 3880ab
Packit Service 3880ab
	if (preferred_family != AF_UNSPEC && r->rtm_family != preferred_family)
Packit Service 3880ab
		return 0;
Packit Service 3880ab
Packit Service 3880ab
	if (r->rtm_family == AF_INET6 && table != RT_TABLE_MAIN)
Packit Service 3880ab
		ip6_multiple_tables = 1;
Packit Service 3880ab
Packit Service 3880ab
	if (filter.cloned == !(r->rtm_flags & RTM_F_CLONED))
Packit Service 3880ab
		return 0;
Packit Service 3880ab
Packit Service 3880ab
	if (r->rtm_family == AF_INET6 && !ip6_multiple_tables) {
Packit Service 3880ab
		if (filter.tb) {
Packit Service 3880ab
			if (filter.tb == RT_TABLE_LOCAL) {
Packit Service 3880ab
				if (r->rtm_type != RTN_LOCAL)
Packit Service 3880ab
					return 0;
Packit Service 3880ab
			} else if (filter.tb == RT_TABLE_MAIN) {
Packit Service 3880ab
				if (r->rtm_type == RTN_LOCAL)
Packit Service 3880ab
					return 0;
Packit Service 3880ab
			} else {
Packit Service 3880ab
				return 0;
Packit Service 3880ab
			}
Packit Service 3880ab
		}
Packit Service 3880ab
	} else {
Packit Service 3880ab
		if (filter.tb > 0 && filter.tb != table)
Packit Service 3880ab
			return 0;
Packit Service 3880ab
	}
Packit Service 3880ab
	if ((filter.protocol^r->rtm_protocol)&filter.protocolmask)
Packit Service 3880ab
		return 0;
Packit Service 3880ab
	if ((filter.scope^r->rtm_scope)&filter.scopemask)
Packit Service 3880ab
		return 0;
Packit Service 3880ab
Packit Service 3880ab
	if (filter.typemask && !(filter.typemask & (1 << r->rtm_type)))
Packit Service 3880ab
		return 0;
Packit Service 3880ab
	if ((filter.tos^r->rtm_tos)&filter.tosmask)
Packit Service 3880ab
		return 0;
Packit Service 3880ab
	if (filter.rdst.family) {
Packit Service 3880ab
		if (r->rtm_family != filter.rdst.family ||
Packit Service 3880ab
		    filter.rdst.bitlen > r->rtm_dst_len)
Packit Service 3880ab
			return 0;
Packit Service 3880ab
	} else if (filter.rdst.flags & PREFIXLEN_SPECIFIED) {
Packit Service 3880ab
		if (filter.rdst.bitlen > r->rtm_dst_len)
Packit Service 3880ab
			return 0;
Packit Service 3880ab
	}
Packit Service 3880ab
	if (filter.mdst.family) {
Packit Service 3880ab
		if (r->rtm_family != filter.mdst.family ||
Packit Service 3880ab
		    (filter.mdst.bitlen >= 0 &&
Packit Service 3880ab
		     filter.mdst.bitlen < r->rtm_dst_len))
Packit Service 3880ab
			return 0;
Packit Service 3880ab
	} else if (filter.mdst.flags & PREFIXLEN_SPECIFIED) {
Packit Service 3880ab
		if (filter.mdst.bitlen >= 0 &&
Packit Service 3880ab
		    filter.mdst.bitlen < r->rtm_dst_len)
Packit Service 3880ab
			return 0;
Packit Service 3880ab
	}
Packit Service 3880ab
	if (filter.rsrc.family) {
Packit Service 3880ab
		if (r->rtm_family != filter.rsrc.family ||
Packit Service 3880ab
		    filter.rsrc.bitlen > r->rtm_src_len)
Packit Service 3880ab
			return 0;
Packit Service 3880ab
	} else if (filter.rsrc.flags & PREFIXLEN_SPECIFIED) {
Packit Service 3880ab
		if (filter.rsrc.bitlen > r->rtm_src_len)
Packit Service 3880ab
			return 0;
Packit Service 3880ab
	}
Packit Service 3880ab
	if (filter.msrc.family) {
Packit Service 3880ab
		if (r->rtm_family != filter.msrc.family ||
Packit Service 3880ab
		    (filter.msrc.bitlen >= 0 &&
Packit Service 3880ab
		     filter.msrc.bitlen < r->rtm_src_len))
Packit Service 3880ab
			return 0;
Packit Service 3880ab
	} else if (filter.msrc.flags & PREFIXLEN_SPECIFIED) {
Packit Service 3880ab
		if (filter.msrc.bitlen >= 0 &&
Packit Service 3880ab
		    filter.msrc.bitlen < r->rtm_src_len)
Packit Service 3880ab
			return 0;
Packit Service 3880ab
	}
Packit Service 3880ab
	if (filter.rvia.family) {
Packit Service 3880ab
		int family = r->rtm_family;
Packit Service 3880ab
Packit Service 3880ab
		if (tb[RTA_VIA]) {
Packit Service 3880ab
			struct rtvia *via = RTA_DATA(tb[RTA_VIA]);
Packit Service 3880ab
Packit Service 3880ab
			family = via->rtvia_family;
Packit Service 3880ab
		}
Packit Service 3880ab
		if (family != filter.rvia.family)
Packit Service 3880ab
			return 0;
Packit Service 3880ab
	}
Packit Service 3880ab
	if (filter.rprefsrc.family && r->rtm_family != filter.rprefsrc.family)
Packit Service 3880ab
		return 0;
Packit Service 3880ab
Packit Service 3880ab
	if (tb[RTA_DST])
Packit Service 3880ab
		memcpy(&dst.data, RTA_DATA(tb[RTA_DST]), (r->rtm_dst_len+7)/8);
Packit Service 3880ab
	if (filter.rsrc.family || filter.msrc.family ||
Packit Service 3880ab
	    filter.rsrc.flags & PREFIXLEN_SPECIFIED ||
Packit Service 3880ab
	    filter.msrc.flags & PREFIXLEN_SPECIFIED) {
Packit Service 3880ab
		if (tb[RTA_SRC])
Packit Service 3880ab
			memcpy(&src.data, RTA_DATA(tb[RTA_SRC]), (r->rtm_src_len+7)/8);
Packit Service 3880ab
	}
Packit Service 3880ab
	if (filter.rvia.bitlen > 0) {
Packit Service 3880ab
		if (tb[RTA_GATEWAY])
Packit Service 3880ab
			memcpy(&via.data, RTA_DATA(tb[RTA_GATEWAY]), host_len/8);
Packit Service 3880ab
		if (tb[RTA_VIA]) {
Packit Service 3880ab
			size_t len = RTA_PAYLOAD(tb[RTA_VIA]) - 2;
Packit Service 3880ab
			struct rtvia *rtvia = RTA_DATA(tb[RTA_VIA]);
Packit Service 3880ab
Packit Service 3880ab
			via.family = rtvia->rtvia_family;
Packit Service 3880ab
			memcpy(&via.data, rtvia->rtvia_addr, len);
Packit Service 3880ab
		}
Packit Service 3880ab
	}
Packit Service 3880ab
	if (filter.rprefsrc.bitlen > 0) {
Packit Service 3880ab
		if (tb[RTA_PREFSRC])
Packit Service 3880ab
			memcpy(&prefsrc.data, RTA_DATA(tb[RTA_PREFSRC]), host_len/8);
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if ((filter.rdst.family || filter.rdst.flags & PREFIXLEN_SPECIFIED) &&
Packit Service 3880ab
	    inet_addr_match(&dst, &filter.rdst, filter.rdst.bitlen))
Packit Service 3880ab
		return 0;
Packit Service 3880ab
	if ((filter.mdst.family || filter.mdst.flags & PREFIXLEN_SPECIFIED) &&
Packit Service 3880ab
	    inet_addr_match(&dst, &filter.mdst, r->rtm_dst_len))
Packit Service 3880ab
		return 0;
Packit Service 3880ab
Packit Service 3880ab
	if ((filter.rsrc.family || filter.rsrc.flags & PREFIXLEN_SPECIFIED) &&
Packit Service 3880ab
	    inet_addr_match(&src, &filter.rsrc, filter.rsrc.bitlen))
Packit Service 3880ab
		return 0;
Packit Service 3880ab
	if ((filter.msrc.family || filter.msrc.flags & PREFIXLEN_SPECIFIED) &&
Packit Service 3880ab
	    filter.msrc.bitlen >= 0 &&
Packit Service 3880ab
	    inet_addr_match(&src, &filter.msrc, r->rtm_src_len))
Packit Service 3880ab
		return 0;
Packit Service 3880ab
Packit Service 3880ab
	if (filter.rvia.family && inet_addr_match(&via, &filter.rvia, filter.rvia.bitlen))
Packit Service 3880ab
		return 0;
Packit Service 3880ab
	if (filter.rprefsrc.family && inet_addr_match(&prefsrc, &filter.rprefsrc, filter.rprefsrc.bitlen))
Packit Service 3880ab
		return 0;
Packit Service 3880ab
	if (filter.realmmask) {
Packit Service 3880ab
		__u32 realms = 0;
Packit Service 3880ab
Packit Service 3880ab
		if (tb[RTA_FLOW])
Packit Service 3880ab
			realms = rta_getattr_u32(tb[RTA_FLOW]);
Packit Service 3880ab
		if ((realms^filter.realm)&filter.realmmask)
Packit Service 3880ab
			return 0;
Packit Service 3880ab
	}
Packit Service 3880ab
	if (filter.iifmask) {
Packit Service 3880ab
		int iif = 0;
Packit Service 3880ab
Packit Service 3880ab
		if (tb[RTA_IIF])
Packit Service 3880ab
			iif = rta_getattr_u32(tb[RTA_IIF]);
Packit Service 3880ab
		if ((iif^filter.iif)&filter.iifmask)
Packit Service 3880ab
			return 0;
Packit Service 3880ab
	}
Packit Service 3880ab
	if (filter.oifmask) {
Packit Service 3880ab
		int oif = 0;
Packit Service 3880ab
Packit Service 3880ab
		if (tb[RTA_OIF])
Packit Service 3880ab
			oif = rta_getattr_u32(tb[RTA_OIF]);
Packit Service 3880ab
		if ((oif^filter.oif)&filter.oifmask)
Packit Service 3880ab
			return 0;
Packit Service 3880ab
	}
Packit Service 3880ab
	if (filter.markmask) {
Packit Service 3880ab
		int mark = 0;
Packit Service 3880ab
Packit Service 3880ab
		if (tb[RTA_MARK])
Packit Service 3880ab
			mark = rta_getattr_u32(tb[RTA_MARK]);
Packit Service 3880ab
		if ((mark ^ filter.mark) & filter.markmask)
Packit Service 3880ab
			return 0;
Packit Service 3880ab
	}
Packit Service 3880ab
	if (filter.metricmask) {
Packit Service 3880ab
		__u32 metric = 0;
Packit Service 3880ab
Packit Service 3880ab
		if (tb[RTA_PRIORITY])
Packit Service 3880ab
			metric = rta_getattr_u32(tb[RTA_PRIORITY]);
Packit Service 3880ab
		if ((metric ^ filter.metric) & filter.metricmask)
Packit Service 3880ab
			return 0;
Packit Service 3880ab
	}
Packit Service 3880ab
	if (filter.flushb &&
Packit Service 3880ab
	    r->rtm_family == AF_INET6 &&
Packit Service 3880ab
	    r->rtm_dst_len == 0 &&
Packit Service 3880ab
	    r->rtm_type == RTN_UNREACHABLE &&
Packit Service 3880ab
	    tb[RTA_PRIORITY] &&
Packit Service 3880ab
	    rta_getattr_u32(tb[RTA_PRIORITY]) == -1)
Packit Service 3880ab
		return 0;
Packit Service 3880ab
Packit Service 3880ab
	return 1;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static void print_rtax_features(FILE *fp, unsigned int features)
Packit Service 3880ab
{
Packit Service 3880ab
	unsigned int of = features;
Packit Service 3880ab
Packit Service 3880ab
	if (features & RTAX_FEATURE_ECN) {
Packit Service 3880ab
		print_null(PRINT_ANY, "ecn", "ecn ", NULL);
Packit Service 3880ab
		features &= ~RTAX_FEATURE_ECN;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (features)
Packit Service 3880ab
		print_0xhex(PRINT_ANY,
Packit Service 3880ab
			    "features", "%#llx ", of);
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
void print_rt_flags(FILE *fp, unsigned int flags)
Packit Service 3880ab
{
Packit Service 3880ab
	open_json_array(PRINT_JSON,
Packit Service 3880ab
			is_json_context() ?  "flags" : "");
Packit Service 3880ab
Packit Service 3880ab
	if (flags & RTNH_F_DEAD)
Packit Service 3880ab
		print_string(PRINT_ANY, NULL, "%s ", "dead");
Packit Service 3880ab
	if (flags & RTNH_F_ONLINK)
Packit Service 3880ab
		print_string(PRINT_ANY, NULL, "%s ", "onlink");
Packit Service 3880ab
	if (flags & RTNH_F_PERVASIVE)
Packit Service 3880ab
		print_string(PRINT_ANY, NULL, "%s ", "pervasive");
Packit Service 3880ab
	if (flags & RTNH_F_OFFLOAD)
Packit Service 3880ab
		print_string(PRINT_ANY, NULL, "%s ", "offload");
Packit Service 3880ab
	if (flags & RTM_F_NOTIFY)
Packit Service 3880ab
		print_string(PRINT_ANY, NULL, "%s ", "notify");
Packit Service 3880ab
	if (flags & RTNH_F_LINKDOWN)
Packit Service 3880ab
		print_string(PRINT_ANY, NULL, "%s ", "linkdown");
Packit Service 3880ab
	if (flags & RTNH_F_UNRESOLVED)
Packit Service 3880ab
		print_string(PRINT_ANY, NULL, "%s ", "unresolved");
Packit Service 3880ab
	if (flags & RTM_F_OFFLOAD)
Packit Service 3880ab
		print_string(PRINT_ANY, NULL, "%s ", "rt_offload");
Packit Service 3880ab
	if (flags & RTM_F_TRAP)
Packit Service 3880ab
		print_string(PRINT_ANY, NULL, "%s ", "rt_trap");
Packit Service 3880ab
Packit Service 3880ab
	close_json_array(PRINT_JSON, NULL);
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static void print_rt_pref(FILE *fp, unsigned int pref)
Packit Service 3880ab
{
Packit Service 3880ab
Packit Service 3880ab
	switch (pref) {
Packit Service 3880ab
	case ICMPV6_ROUTER_PREF_LOW:
Packit Service 3880ab
		print_string(PRINT_ANY,
Packit Service 3880ab
			     "pref", "pref %s", "low");
Packit Service 3880ab
		break;
Packit Service 3880ab
	case ICMPV6_ROUTER_PREF_MEDIUM:
Packit Service 3880ab
		print_string(PRINT_ANY,
Packit Service 3880ab
			     "pref", "pref %s", "medium");
Packit Service 3880ab
		break;
Packit Service 3880ab
	case ICMPV6_ROUTER_PREF_HIGH:
Packit Service 3880ab
		print_string(PRINT_ANY,
Packit Service 3880ab
			     "pref", "pref %s", "high");
Packit Service 3880ab
		break;
Packit Service 3880ab
	default:
Packit Service 3880ab
		print_uint(PRINT_ANY,
Packit Service 3880ab
			   "pref", "%u", pref);
Packit Service 3880ab
	}
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
void print_rta_if(FILE *fp, const struct rtattr *rta, const char *prefix)
Packit Service 3880ab
{
Packit Service 3880ab
	const char *ifname = ll_index_to_name(rta_getattr_u32(rta));
Packit Service 3880ab
Packit Service 3880ab
	if (is_json_context())
Packit Service 3880ab
		print_string(PRINT_JSON, prefix, NULL, ifname);
Packit Service 3880ab
	else {
Packit Service 3880ab
		fprintf(fp, "%s ", prefix);
Packit Service 3880ab
		color_fprintf(fp, COLOR_IFNAME, "%s ", ifname);
Packit Service 3880ab
	}
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static void print_cache_flags(FILE *fp, __u32 flags)
Packit Service 3880ab
{
Packit Service 3880ab
	json_writer_t *jw = get_json_writer();
Packit Service 3880ab
	flags &= ~0xFFFF;
Packit Service 3880ab
Packit Service 3880ab
	if (jw) {
Packit Service 3880ab
		jsonw_name(jw, "cache");
Packit Service 3880ab
		jsonw_start_array(jw);
Packit Service 3880ab
	} else {
Packit Service 3880ab
		fprintf(fp, "%s    cache ", _SL_);
Packit Service 3880ab
		if (flags == 0)
Packit Service 3880ab
			return;
Packit Service 3880ab
		putc('<', fp);
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
#define PRTFL(fl, flname)						\
Packit Service 3880ab
	if (flags & RTCF_##fl) {					\
Packit Service 3880ab
		flags &= ~RTCF_##fl;					\
Packit Service 3880ab
		if (jw)							\
Packit Service 3880ab
			jsonw_string(jw, flname);			\
Packit Service 3880ab
		else							\
Packit Service 3880ab
			fprintf(fp, "%s%s", flname, flags ? "," : "> "); \
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	PRTFL(LOCAL, "local");
Packit Service 3880ab
	PRTFL(REJECT, "reject");
Packit Service 3880ab
	PRTFL(MULTICAST, "mc");
Packit Service 3880ab
	PRTFL(BROADCAST, "brd");
Packit Service 3880ab
	PRTFL(DNAT, "dst-nat");
Packit Service 3880ab
	PRTFL(SNAT, "src-nat");
Packit Service 3880ab
	PRTFL(MASQ, "masq");
Packit Service 3880ab
	PRTFL(DIRECTDST, "dst-direct");
Packit Service 3880ab
	PRTFL(DIRECTSRC, "src-direct");
Packit Service 3880ab
	PRTFL(REDIRECTED, "redirected");
Packit Service 3880ab
	PRTFL(DOREDIRECT, "redirect");
Packit Service 3880ab
	PRTFL(FAST, "fastroute");
Packit Service 3880ab
	PRTFL(NOTIFY, "notify");
Packit Service 3880ab
	PRTFL(TPROXY, "proxy");
Packit Service 3880ab
#undef PRTFL
Packit Service 3880ab
Packit Service 3880ab
	if (flags)
Packit Service 3880ab
		print_hex(PRINT_ANY, "flags", "%x>", flags);
Packit Service 3880ab
Packit Service 3880ab
	if (jw)
Packit Service 3880ab
		jsonw_end_array(jw);
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static void print_rta_cacheinfo(FILE *fp, const struct rta_cacheinfo *ci)
Packit Service 3880ab
{
Packit Service 3880ab
	static int hz;
Packit Service 3880ab
Packit Service 3880ab
	if (!hz)
Packit Service 3880ab
		hz = get_user_hz();
Packit Service 3880ab
Packit Service 3880ab
	if (ci->rta_expires != 0)
Packit Service 3880ab
		print_int(PRINT_ANY, "expires",
Packit Service 3880ab
			   "expires %dsec ", ci->rta_expires/hz);
Packit Service 3880ab
	if (ci->rta_error != 0)
Packit Service 3880ab
		print_uint(PRINT_ANY, "error",
Packit Service 3880ab
			   "error %u ", ci->rta_error);
Packit Service 3880ab
Packit Service 3880ab
	if (show_stats) {
Packit Service 3880ab
		if (ci->rta_clntref)
Packit Service 3880ab
			print_uint(PRINT_ANY, "users",
Packit Service 3880ab
				   "users %u ", ci->rta_clntref);
Packit Service 3880ab
		if (ci->rta_used != 0)
Packit Service 3880ab
			print_uint(PRINT_ANY, "used",
Packit Service 3880ab
				   "used %u ", ci->rta_used);
Packit Service 3880ab
		if (ci->rta_lastuse != 0)
Packit Service 3880ab
			print_uint(PRINT_ANY, "age",
Packit Service 3880ab
				   "age %usec ", ci->rta_lastuse/hz);
Packit Service 3880ab
	}
Packit Service 3880ab
	if (ci->rta_id)
Packit Service 3880ab
		print_0xhex(PRINT_ANY, "ipid",
Packit Service 3880ab
			    "ipid 0x%04llx ", ci->rta_id);
Packit Service 3880ab
	if (ci->rta_ts || ci->rta_tsage) {
Packit Service 3880ab
		print_0xhex(PRINT_ANY, "ts",
Packit Service 3880ab
			    "ts 0x%llx", ci->rta_ts);
Packit Service 3880ab
		print_uint(PRINT_ANY, "tsage",
Packit Service 3880ab
			   "tsage %usec ", ci->rta_tsage);
Packit Service 3880ab
	}
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static void print_rta_flow(FILE *fp, const struct rtattr *rta)
Packit Service 3880ab
{
Packit Service 3880ab
	__u32 to = rta_getattr_u32(rta);
Packit Service 3880ab
	__u32 from = to >> 16;
Packit Service 3880ab
	SPRINT_BUF(b1);
Packit Service 3880ab
Packit Service 3880ab
	to &= 0xFFFF;
Packit Service 3880ab
	if (is_json_context()) {
Packit Service 3880ab
		open_json_object("flow");
Packit Service 3880ab
Packit Service 3880ab
		if (from)
Packit Service 3880ab
			print_string(PRINT_JSON, "from", NULL,
Packit Service 3880ab
				     rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
Packit Service 3880ab
		print_string(PRINT_JSON, "to", NULL,
Packit Service 3880ab
			     rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
Packit Service 3880ab
		close_json_object();
Packit Service 3880ab
	} else {
Packit Service 3880ab
		fprintf(fp, "realm%s ", from ? "s" : "");
Packit Service 3880ab
Packit Service 3880ab
		if (from)
Packit Service 3880ab
			print_string(PRINT_FP, NULL, "%s/",
Packit Service 3880ab
				     rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
Packit Service 3880ab
		print_string(PRINT_FP, NULL, "%s ",
Packit Service 3880ab
			     rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
Packit Service 3880ab
	}
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static void print_rta_newdst(FILE *fp, const struct rtmsg *r,
Packit Service 3880ab
			     const struct rtattr *rta)
Packit Service 3880ab
{
Packit Service 3880ab
	const char *newdst = format_host_rta(r->rtm_family, rta);
Packit Service 3880ab
Packit Service 3880ab
	if (is_json_context())
Packit Service 3880ab
		print_string(PRINT_JSON, "to", NULL, newdst);
Packit Service 3880ab
	else {
Packit Service 3880ab
		fprintf(fp, "as to ");
Packit Service 3880ab
		print_color_string(PRINT_FP,
Packit Service 3880ab
				   ifa_family_color(r->rtm_family),
Packit Service 3880ab
				   NULL, "%s ", newdst);
Packit Service 3880ab
	}
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
void print_rta_gateway(FILE *fp, unsigned char family, const struct rtattr *rta)
Packit Service 3880ab
{
Packit Service 3880ab
	const char *gateway = format_host_rta(family, rta);
Packit Service 3880ab
Packit Service 3880ab
	if (is_json_context())
Packit Service 3880ab
		print_string(PRINT_JSON, "gateway", NULL, gateway);
Packit Service 3880ab
	else {
Packit Service 3880ab
		fprintf(fp, "via ");
Packit Service 3880ab
		print_color_string(PRINT_FP,
Packit Service 3880ab
				   ifa_family_color(family),
Packit Service 3880ab
				   NULL, "%s ", gateway);
Packit Service 3880ab
	}
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static void print_rta_via(FILE *fp, const struct rtattr *rta)
Packit Service 3880ab
{
Packit Service 3880ab
	size_t len = RTA_PAYLOAD(rta) - 2;
Packit Service 3880ab
	const struct rtvia *via = RTA_DATA(rta);
Packit Service 3880ab
Packit Service 3880ab
	if (is_json_context()) {
Packit Service 3880ab
		open_json_object("via");
Packit Service 3880ab
		print_string(PRINT_JSON, "family", NULL,
Packit Service 3880ab
			     family_name(via->rtvia_family));
Packit Service 3880ab
		print_string(PRINT_JSON, "host", NULL,
Packit Service 3880ab
			     format_host(via->rtvia_family, len,
Packit Service 3880ab
					 via->rtvia_addr));
Packit Service 3880ab
		close_json_object();
Packit Service 3880ab
	} else {
Packit Service 3880ab
		print_string(PRINT_FP, NULL, "via %s ",
Packit Service 3880ab
			     family_name(via->rtvia_family));
Packit Service 3880ab
		print_color_string(PRINT_FP,
Packit Service 3880ab
				   ifa_family_color(via->rtvia_family),
Packit Service 3880ab
				   NULL, "%s ",
Packit Service 3880ab
				   format_host(via->rtvia_family,
Packit Service 3880ab
					       len, via->rtvia_addr));
Packit Service 3880ab
	}
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static void print_rta_metrics(FILE *fp, const struct rtattr *rta)
Packit Service 3880ab
{
Packit Service 3880ab
	struct rtattr *mxrta[RTAX_MAX+1];
Packit Service 3880ab
	unsigned int mxlock = 0;
Packit Service 3880ab
	int i;
Packit Service 3880ab
Packit Service 3880ab
	open_json_array(PRINT_JSON, "metrics");
Packit Service 3880ab
	open_json_object(NULL);
Packit Service 3880ab
Packit Service 3880ab
	parse_rtattr(mxrta, RTAX_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta));
Packit Service 3880ab
Packit Service 3880ab
	if (mxrta[RTAX_LOCK])
Packit Service 3880ab
		mxlock = rta_getattr_u32(mxrta[RTAX_LOCK]);
Packit Service 3880ab
Packit Service 3880ab
	for (i = 2; i <= RTAX_MAX; i++) {
Packit Service 3880ab
		__u32 val = 0U;
Packit Service 3880ab
Packit Service 3880ab
		if (mxrta[i] == NULL && !(mxlock & (1 << i)))
Packit Service 3880ab
			continue;
Packit Service 3880ab
Packit Service 3880ab
		if (mxrta[i] != NULL && i != RTAX_CC_ALGO)
Packit Service 3880ab
			val = rta_getattr_u32(mxrta[i]);
Packit Service 3880ab
Packit Service 3880ab
		if (i == RTAX_HOPLIMIT && (int)val == -1)
Packit Service 3880ab
			continue;
Packit Service 3880ab
Packit Service 3880ab
		if (!is_json_context()) {
Packit Service 3880ab
			if (i < sizeof(mx_names)/sizeof(char *) && mx_names[i])
Packit Service 3880ab
				fprintf(fp, "%s ", mx_names[i]);
Packit Service 3880ab
			else
Packit Service 3880ab
				fprintf(fp, "metric %d ", i);
Packit Service 3880ab
Packit Service 3880ab
			if (mxlock & (1<
Packit Service 3880ab
				fprintf(fp, "lock ");
Packit Service 3880ab
		}
Packit Service 3880ab
Packit Service 3880ab
		switch (i) {
Packit Service 3880ab
		case RTAX_FEATURES:
Packit Service 3880ab
			print_rtax_features(fp, val);
Packit Service 3880ab
			break;
Packit Service 3880ab
		default:
Packit Service 3880ab
			print_uint(PRINT_ANY, mx_names[i], "%u ", val);
Packit Service 3880ab
			break;
Packit Service 3880ab
Packit Service 3880ab
		case RTAX_RTT:
Packit Service 3880ab
		case RTAX_RTTVAR:
Packit Service 3880ab
		case RTAX_RTO_MIN:
Packit Service 3880ab
			if (i == RTAX_RTT)
Packit Service 3880ab
				val /= 8;
Packit Service 3880ab
			else if (i == RTAX_RTTVAR)
Packit Service 3880ab
				val /= 4;
Packit Service 3880ab
Packit Service 3880ab
			if (is_json_context())
Packit Service 3880ab
				print_uint(PRINT_JSON, mx_names[i],
Packit Service 3880ab
					   NULL, val);
Packit Service 3880ab
			else {
Packit Service 3880ab
				if (val >= 1000)
Packit Service 3880ab
					fprintf(fp, "%gs ", val/1e3);
Packit Service 3880ab
				else
Packit Service 3880ab
					fprintf(fp, "%ums ", val);
Packit Service 3880ab
			}
Packit Service 3880ab
			break;
Packit Service 3880ab
		case RTAX_CC_ALGO:
Packit Service 3880ab
			print_string(PRINT_ANY, "congestion",
Packit Service 3880ab
				     "%s ", rta_getattr_str(mxrta[i]));
Packit Service 3880ab
			break;
Packit Service 3880ab
		}
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	close_json_object();
Packit Service 3880ab
	close_json_array(PRINT_JSON, NULL);
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static void print_rta_multipath(FILE *fp, const struct rtmsg *r,
Packit Service 3880ab
				struct rtattr *rta)
Packit Service 3880ab
{
Packit Service 3880ab
	const struct rtnexthop *nh = RTA_DATA(rta);
Packit Service 3880ab
	int len = RTA_PAYLOAD(rta);
Packit Service 3880ab
	int first = 1;
Packit Service 3880ab
Packit Service 3880ab
	open_json_array(PRINT_JSON, "nexthops");
Packit Service 3880ab
Packit Service 3880ab
	while (len >= sizeof(*nh)) {
Packit Service 3880ab
		struct rtattr *tb[RTA_MAX + 1];
Packit Service 3880ab
Packit Service 3880ab
		if (nh->rtnh_len > len)
Packit Service 3880ab
			break;
Packit Service 3880ab
Packit Service 3880ab
		open_json_object(NULL);
Packit Service 3880ab
Packit Service 3880ab
		if ((r->rtm_flags & RTM_F_CLONED) &&
Packit Service 3880ab
		    r->rtm_type == RTN_MULTICAST) {
Packit Service 3880ab
			if (first) {
Packit Service 3880ab
				print_string(PRINT_FP, NULL, "Oifs: ", NULL);
Packit Service 3880ab
				first = 0;
Packit Service 3880ab
			} else {
Packit Service 3880ab
				print_string(PRINT_FP, NULL, " ", NULL);
Packit Service 3880ab
			}
Packit Service 3880ab
		} else
Packit Service 3880ab
			print_string(PRINT_FP, NULL, "%s\tnexthop ", _SL_);
Packit Service 3880ab
Packit Service 3880ab
		if (nh->rtnh_len > sizeof(*nh)) {
Packit Service 3880ab
			parse_rtattr(tb, RTA_MAX, RTNH_DATA(nh),
Packit Service 3880ab
				     nh->rtnh_len - sizeof(*nh));
Packit Service 3880ab
Packit Service 3880ab
			if (tb[RTA_ENCAP])
Packit Service 3880ab
				lwt_print_encap(fp,
Packit Service 3880ab
						tb[RTA_ENCAP_TYPE],
Packit Service 3880ab
						tb[RTA_ENCAP]);
Packit Service 3880ab
			if (tb[RTA_NEWDST])
Packit Service 3880ab
				print_rta_newdst(fp, r, tb[RTA_NEWDST]);
Packit Service 3880ab
			if (tb[RTA_GATEWAY])
Packit Service 3880ab
				print_rta_gateway(fp, r->rtm_family,
Packit Service 3880ab
						  tb[RTA_GATEWAY]);
Packit Service 3880ab
			if (tb[RTA_VIA])
Packit Service 3880ab
				print_rta_via(fp, tb[RTA_VIA]);
Packit Service 3880ab
			if (tb[RTA_FLOW])
Packit Service 3880ab
				print_rta_flow(fp, tb[RTA_FLOW]);
Packit Service 3880ab
		}
Packit Service 3880ab
Packit Service 3880ab
		if ((r->rtm_flags & RTM_F_CLONED) &&
Packit Service 3880ab
		    r->rtm_type == RTN_MULTICAST) {
Packit Service 3880ab
			print_string(PRINT_ANY, "dev",
Packit Service 3880ab
				     "%s", ll_index_to_name(nh->rtnh_ifindex));
Packit Service 3880ab
Packit Service 3880ab
			if (nh->rtnh_hops != 1)
Packit Service 3880ab
				print_int(PRINT_ANY, "ttl", "(ttl>%d)", nh->rtnh_hops);
Packit Service 3880ab
Packit Service 3880ab
			print_string(PRINT_FP, NULL, " ", NULL);
Packit Service 3880ab
		} else {
Packit Service 3880ab
			print_string(PRINT_ANY, "dev",
Packit Service 3880ab
				     "dev %s ", ll_index_to_name(nh->rtnh_ifindex));
Packit Service 3880ab
Packit Service 3880ab
			if (r->rtm_family != AF_MPLS)
Packit Service 3880ab
				print_int(PRINT_ANY, "weight",
Packit Service 3880ab
					  "weight %d ", nh->rtnh_hops + 1);
Packit Service 3880ab
		}
Packit Service 3880ab
Packit Service 3880ab
		print_rt_flags(fp, nh->rtnh_flags);
Packit Service 3880ab
Packit Service 3880ab
		len -= NLMSG_ALIGN(nh->rtnh_len);
Packit Service 3880ab
		nh = RTNH_NEXT(nh);
Packit Service 3880ab
Packit Service 3880ab
		close_json_object();
Packit Service 3880ab
	}
Packit Service 3880ab
	close_json_array(PRINT_JSON, NULL);
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
int print_route(struct nlmsghdr *n, void *arg)
Packit Service 3880ab
{
Packit Service 3880ab
	FILE *fp = (FILE *)arg;
Packit Service 3880ab
	struct rtmsg *r = NLMSG_DATA(n);
Packit Service 3880ab
	int len = n->nlmsg_len;
Packit Service 3880ab
	struct rtattr *tb[RTA_MAX+1];
Packit Service 3880ab
	int family, color, host_len;
Packit Service 3880ab
	__u32 table;
Packit Service 3880ab
	int ret;
Packit Service 3880ab
Packit Service 3880ab
	SPRINT_BUF(b1);
Packit Service 3880ab
Packit Service 3880ab
	if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) {
Packit Service 3880ab
		fprintf(stderr, "Not a route: %08x %08x %08x\n",
Packit Service 3880ab
			n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
Packit Service 3880ab
		return -1;
Packit Service 3880ab
	}
Packit Service 3880ab
	if (filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
Packit Service 3880ab
		return 0;
Packit Service 3880ab
	len -= NLMSG_LENGTH(sizeof(*r));
Packit Service 3880ab
	if (len < 0) {
Packit Service 3880ab
		fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
Packit Service 3880ab
		return -1;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	host_len = af_bit_len(r->rtm_family);
Packit Service 3880ab
Packit Service 3880ab
	parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
Packit Service 3880ab
	table = rtm_get_table(r, tb);
Packit Service 3880ab
Packit Service 3880ab
	if (!filter_nlmsg(n, tb, host_len))
Packit Service 3880ab
		return 0;
Packit Service 3880ab
Packit Service 3880ab
	if (filter.flushb) {
Packit Service 3880ab
		struct nlmsghdr *fn;
Packit Service 3880ab
Packit Service 3880ab
		if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
Packit Service 3880ab
			ret = flush_update();
Packit Service 3880ab
			if (ret < 0)
Packit Service 3880ab
				return ret;
Packit Service 3880ab
		}
Packit Service 3880ab
		fn = (struct nlmsghdr *)(filter.flushb + NLMSG_ALIGN(filter.flushp));
Packit Service 3880ab
		memcpy(fn, n, n->nlmsg_len);
Packit Service 3880ab
		fn->nlmsg_type = RTM_DELROUTE;
Packit Service 3880ab
		fn->nlmsg_flags = NLM_F_REQUEST;
Packit Service 3880ab
		fn->nlmsg_seq = ++rth.seq;
Packit Service 3880ab
		filter.flushp = (((char *)fn) + n->nlmsg_len) - filter.flushb;
Packit Service 3880ab
		filter.flushed++;
Packit Service 3880ab
		if (show_stats < 2)
Packit Service 3880ab
			return 0;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	open_json_object(NULL);
Packit Service 3880ab
	if (n->nlmsg_type == RTM_DELROUTE)
Packit Service 3880ab
		print_bool(PRINT_ANY, "deleted", "Deleted ", true);
Packit Service 3880ab
Packit Service 3880ab
	if ((r->rtm_type != RTN_UNICAST || show_details > 0) &&
Packit Service 3880ab
	    (!filter.typemask || (filter.typemask & (1 << r->rtm_type))))
Packit Service 3880ab
		print_string(PRINT_ANY, "type", "%s ",
Packit Service 3880ab
			     rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
Packit Service 3880ab
Packit Service 3880ab
	color = COLOR_NONE;
Packit Service 3880ab
	if (tb[RTA_DST]) {
Packit Service 3880ab
		family = get_real_family(r->rtm_type, r->rtm_family);
Packit Service 3880ab
		color = ifa_family_color(family);
Packit Service 3880ab
Packit Service 3880ab
		if (r->rtm_dst_len != host_len) {
Packit Service 3880ab
			snprintf(b1, sizeof(b1),
Packit Service 3880ab
				 "%s/%u", rt_addr_n2a_rta(family, tb[RTA_DST]),
Packit Service 3880ab
				 r->rtm_dst_len);
Packit Service 3880ab
		} else {
Packit Service 3880ab
			format_host_rta_r(family, tb[RTA_DST],
Packit Service 3880ab
					  b1, sizeof(b1));
Packit Service 3880ab
Packit Service 3880ab
		}
Packit Service 3880ab
	} else if (r->rtm_dst_len) {
Packit Service 3880ab
		snprintf(b1, sizeof(b1), "0/%d ", r->rtm_dst_len);
Packit Service 3880ab
	} else {
Packit Service 3880ab
		strncpy(b1, "default", sizeof(b1));
Packit Service 3880ab
	}
Packit Service 3880ab
	print_color_string(PRINT_ANY, color,
Packit Service 3880ab
			   "dst", "%s ", b1);
Packit Service 3880ab
Packit Service 3880ab
	if (tb[RTA_SRC]) {
Packit Service 3880ab
		family = get_real_family(r->rtm_type, r->rtm_family);
Packit Service 3880ab
		color = ifa_family_color(family);
Packit Service 3880ab
Packit Service 3880ab
		if (r->rtm_src_len != host_len) {
Packit Service 3880ab
			snprintf(b1, sizeof(b1),
Packit Service 3880ab
				 "%s/%u",
Packit Service 3880ab
				 rt_addr_n2a_rta(family, tb[RTA_SRC]),
Packit Service 3880ab
				 r->rtm_src_len);
Packit Service 3880ab
		} else {
Packit Service 3880ab
			format_host_rta_r(family, tb[RTA_SRC],
Packit Service 3880ab
					  b1, sizeof(b1));
Packit Service 3880ab
		}
Packit Service 3880ab
		print_color_string(PRINT_ANY, color,
Packit Service 3880ab
				   "from", "from %s ", b1);
Packit Service 3880ab
	} else if (r->rtm_src_len) {
Packit Service 3880ab
		snprintf(b1, sizeof(b1), "0/%u", r->rtm_src_len);
Packit Service 3880ab
Packit Service 3880ab
		print_string(PRINT_ANY, "src", "from %s ", b1);
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (tb[RTA_NH_ID])
Packit Service 3880ab
		print_uint(PRINT_ANY, "nhid", "nhid %u ",
Packit Service 3880ab
			   rta_getattr_u32(tb[RTA_NH_ID]));
Packit Service 3880ab
Packit Service 3880ab
	if (tb[RTA_NEWDST])
Packit Service 3880ab
		print_rta_newdst(fp, r, tb[RTA_NEWDST]);
Packit Service 3880ab
Packit Service 3880ab
	if (tb[RTA_ENCAP])
Packit Service 3880ab
		lwt_print_encap(fp, tb[RTA_ENCAP_TYPE], tb[RTA_ENCAP]);
Packit Service 3880ab
Packit Service 3880ab
	if (r->rtm_tos && filter.tosmask != -1) {
Packit Service 3880ab
		print_string(PRINT_ANY, "tos", "tos %s ",
Packit Service 3880ab
			     rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len)
Packit Service 3880ab
		print_rta_gateway(fp, r->rtm_family, tb[RTA_GATEWAY]);
Packit Service 3880ab
Packit Service 3880ab
	if (tb[RTA_VIA])
Packit Service 3880ab
		print_rta_via(fp, tb[RTA_VIA]);
Packit Service 3880ab
Packit Service 3880ab
	if (tb[RTA_OIF] && filter.oifmask != -1)
Packit Service 3880ab
		print_rta_if(fp, tb[RTA_OIF], "dev");
Packit Service 3880ab
Packit Service 3880ab
	if (table && (table != RT_TABLE_MAIN || show_details > 0) && !filter.tb)
Packit Service 3880ab
		print_string(PRINT_ANY,
Packit Service 3880ab
			     "table", "table %s ",
Packit Service 3880ab
			     rtnl_rttable_n2a(table, b1, sizeof(b1)));
Packit Service 3880ab
Packit Service 3880ab
	if (!(r->rtm_flags & RTM_F_CLONED)) {
Packit Service 3880ab
		if ((r->rtm_protocol != RTPROT_BOOT || show_details > 0) &&
Packit Service 3880ab
		    filter.protocolmask != -1)
Packit Service 3880ab
			print_string(PRINT_ANY,
Packit Service 3880ab
				     "protocol", "proto %s ",
Packit Service 3880ab
				     rtnl_rtprot_n2a(r->rtm_protocol,
Packit Service 3880ab
						     b1, sizeof(b1)));
Packit Service 3880ab
Packit Service 3880ab
		if ((r->rtm_scope != RT_SCOPE_UNIVERSE || show_details > 0) &&
Packit Service 3880ab
		    filter.scopemask != -1)
Packit Service 3880ab
			print_string(PRINT_ANY,
Packit Service 3880ab
				     "scope", "scope %s ",
Packit Service 3880ab
				     rtnl_rtscope_n2a(r->rtm_scope,
Packit Service 3880ab
						      b1, sizeof(b1)));
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (tb[RTA_PREFSRC] && filter.rprefsrc.bitlen != host_len) {
Packit Service 3880ab
		const char *psrc
Packit Service 3880ab
			= rt_addr_n2a_rta(r->rtm_family, tb[RTA_PREFSRC]);
Packit Service 3880ab
Packit Service 3880ab
		/* Do not use format_host(). It is our local addr
Packit Service 3880ab
		   and symbolic name will not be useful.
Packit Service 3880ab
		*/
Packit Service 3880ab
		if (is_json_context())
Packit Service 3880ab
			print_string(PRINT_JSON, "prefsrc", NULL, psrc);
Packit Service 3880ab
		else {
Packit Service 3880ab
			fprintf(fp, "src ");
Packit Service 3880ab
			print_color_string(PRINT_FP,
Packit Service 3880ab
					   ifa_family_color(r->rtm_family),
Packit Service 3880ab
					   NULL, "%s ", psrc);
Packit Service 3880ab
		}
Packit Service 3880ab
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (tb[RTA_PRIORITY] && filter.metricmask != -1)
Packit Service 3880ab
		print_uint(PRINT_ANY, "metric", "metric %u ",
Packit Service 3880ab
			   rta_getattr_u32(tb[RTA_PRIORITY]));
Packit Service 3880ab
Packit Service 3880ab
	print_rt_flags(fp, r->rtm_flags);
Packit Service 3880ab
Packit Service 3880ab
	if (tb[RTA_MARK]) {
Packit Service 3880ab
		unsigned int mark = rta_getattr_u32(tb[RTA_MARK]);
Packit Service 3880ab
Packit Service 3880ab
		if (mark) {
Packit Service 3880ab
			if (is_json_context())
Packit Service 3880ab
				print_uint(PRINT_JSON, "mark", NULL, mark);
Packit Service 3880ab
			else if (mark >= 16)
Packit Service 3880ab
				print_0xhex(PRINT_FP, NULL,
Packit Service 3880ab
					    "mark 0x%llx ", mark);
Packit Service 3880ab
			else
Packit Service 3880ab
				print_uint(PRINT_FP, NULL,
Packit Service 3880ab
					   "mark %u ", mark);
Packit Service 3880ab
		}
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (tb[RTA_FLOW] && filter.realmmask != ~0U)
Packit Service 3880ab
		print_rta_flow(fp, tb[RTA_FLOW]);
Packit Service 3880ab
Packit Service 3880ab
	if (tb[RTA_UID])
Packit Service 3880ab
		print_uint(PRINT_ANY, "uid", "uid %u ",
Packit Service 3880ab
			   rta_getattr_u32(tb[RTA_UID]));
Packit Service 3880ab
Packit Service 3880ab
	if (r->rtm_family == AF_INET) {
Packit Service 3880ab
		if (r->rtm_flags & RTM_F_CLONED)
Packit Service 3880ab
			print_cache_flags(fp, r->rtm_flags);
Packit Service 3880ab
Packit Service 3880ab
		if (tb[RTA_CACHEINFO])
Packit Service 3880ab
			print_rta_cacheinfo(fp, RTA_DATA(tb[RTA_CACHEINFO]));
Packit Service 3880ab
	} else if (r->rtm_family == AF_INET6) {
Packit Service 3880ab
		if (tb[RTA_CACHEINFO])
Packit Service 3880ab
			print_rta_cacheinfo(fp, RTA_DATA(tb[RTA_CACHEINFO]));
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (tb[RTA_METRICS])
Packit Service 3880ab
		print_rta_metrics(fp, tb[RTA_METRICS]);
Packit Service 3880ab
Packit Service 3880ab
	if (tb[RTA_IIF] && filter.iifmask != -1)
Packit Service 3880ab
		print_rta_if(fp, tb[RTA_IIF], "iif");
Packit Service 3880ab
Packit Service 3880ab
	if (tb[RTA_PREF])
Packit Service 3880ab
		print_rt_pref(fp, rta_getattr_u8(tb[RTA_PREF]));
Packit Service 3880ab
Packit Service 3880ab
	if (tb[RTA_TTL_PROPAGATE]) {
Packit Service 3880ab
		bool propagate = rta_getattr_u8(tb[RTA_TTL_PROPAGATE]);
Packit Service 3880ab
Packit Service 3880ab
		if (is_json_context())
Packit Service 3880ab
			print_bool(PRINT_JSON, "ttl-propogate", NULL,
Packit Service 3880ab
				   propagate);
Packit Service 3880ab
		else
Packit Service 3880ab
			print_string(PRINT_FP, NULL,
Packit Service 3880ab
				     "ttl-propogate %s",
Packit Service 3880ab
				     propagate ? "enabled" : "disabled");
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (tb[RTA_MULTIPATH])
Packit Service 3880ab
		print_rta_multipath(fp, r, tb[RTA_MULTIPATH]);
Packit Service 3880ab
Packit Service 3880ab
	/* If you are adding new route RTA_XXXX then place it above
Packit Service 3880ab
	 * the RTA_MULTIPATH else it will appear that the last nexthop
Packit Service 3880ab
	 * in the ECMP has new attributes
Packit Service 3880ab
	 */
Packit Service 3880ab
Packit Service 3880ab
	print_string(PRINT_FP, NULL, "\n", NULL);
Packit Service 3880ab
	close_json_object();
Packit Service 3880ab
	fflush(fp);
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int parse_one_nh(struct nlmsghdr *n, struct rtmsg *r,
Packit Service 3880ab
			struct rtattr *rta, size_t len, struct rtnexthop *rtnh,
Packit Service 3880ab
			int *argcp, char ***argvp)
Packit Service 3880ab
{
Packit Service 3880ab
	int argc = *argcp;
Packit Service 3880ab
	char **argv = *argvp;
Packit Service 3880ab
Packit Service 3880ab
	while (++argv, --argc > 0) {
Packit Service 3880ab
		if (strcmp(*argv, "via") == 0) {
Packit Service 3880ab
			inet_prefix addr;
Packit Service 3880ab
			int family;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			family = read_family(*argv);
Packit Service 3880ab
			if (family == AF_UNSPEC)
Packit Service 3880ab
				family = r->rtm_family;
Packit Service 3880ab
			else
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
			get_addr(&addr, *argv, family);
Packit Service 3880ab
			if (r->rtm_family == AF_UNSPEC)
Packit Service 3880ab
				r->rtm_family = addr.family;
Packit Service 3880ab
			if (addr.family == r->rtm_family) {
Packit Service 3880ab
				if (rta_addattr_l(rta, len, RTA_GATEWAY,
Packit Service 3880ab
						  &addr.data, addr.bytelen))
Packit Service 3880ab
					return -1;
Packit Service 3880ab
				rtnh->rtnh_len += sizeof(struct rtattr)
Packit Service 3880ab
						  + addr.bytelen;
Packit Service 3880ab
			} else {
Packit Service 3880ab
				if (rta_addattr_l(rta, len, RTA_VIA,
Packit Service 3880ab
						  &addr.family, addr.bytelen + 2))
Packit Service 3880ab
					return -1;
Packit Service 3880ab
				rtnh->rtnh_len += RTA_SPACE(addr.bytelen + 2);
Packit Service 3880ab
			}
Packit Service 3880ab
		} else if (strcmp(*argv, "dev") == 0) {
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			rtnh->rtnh_ifindex = ll_name_to_index(*argv);
Packit Service 3880ab
			if (!rtnh->rtnh_ifindex)
Packit Service 3880ab
				return nodev(*argv);
Packit Service 3880ab
		} else if (strcmp(*argv, "weight") == 0) {
Packit Service 3880ab
			unsigned int w;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (get_unsigned(&w, *argv, 0) || w == 0 || w > 256)
Packit Service 3880ab
				invarg("\"weight\" is invalid\n", *argv);
Packit Service 3880ab
			rtnh->rtnh_hops = w - 1;
Packit Service 3880ab
		} else if (strcmp(*argv, "onlink") == 0) {
Packit Service 3880ab
			rtnh->rtnh_flags |= RTNH_F_ONLINK;
Packit Service 3880ab
		} else if (matches(*argv, "realms") == 0) {
Packit Service 3880ab
			__u32 realm;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (get_rt_realms_or_raw(&realm, *argv))
Packit Service 3880ab
				invarg("\"realm\" value is invalid\n", *argv);
Packit Service 3880ab
			if (rta_addattr32(rta, len, RTA_FLOW, realm))
Packit Service 3880ab
				return -1;
Packit Service 3880ab
			rtnh->rtnh_len += sizeof(struct rtattr) + 4;
Packit Service 3880ab
		} else if (strcmp(*argv, "encap") == 0) {
Packit Service 3880ab
			int old_len = rta->rta_len;
Packit Service 3880ab
Packit Service 3880ab
			if (lwt_parse_encap(rta, len, &argc, &argv,
Packit Service 3880ab
					    RTA_ENCAP, RTA_ENCAP_TYPE))
Packit Service 3880ab
				return -1;
Packit Service 3880ab
			rtnh->rtnh_len += rta->rta_len - old_len;
Packit Service 3880ab
		} else if (strcmp(*argv, "as") == 0) {
Packit Service 3880ab
			inet_prefix addr;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (strcmp(*argv, "to") == 0)
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
			get_addr(&addr, *argv, r->rtm_family);
Packit Service 3880ab
			if (rta_addattr_l(rta, len, RTA_NEWDST,
Packit Service 3880ab
					  &addr.data, addr.bytelen))
Packit Service 3880ab
				return -1;
Packit Service 3880ab
			rtnh->rtnh_len += sizeof(struct rtattr) + addr.bytelen;
Packit Service 3880ab
		} else
Packit Service 3880ab
			break;
Packit Service 3880ab
	}
Packit Service 3880ab
	*argcp = argc;
Packit Service 3880ab
	*argvp = argv;
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int parse_nexthops(struct nlmsghdr *n, struct rtmsg *r,
Packit Service 3880ab
			  int argc, char **argv)
Packit Service 3880ab
{
Packit Service 3880ab
	char buf[4096];
Packit Service 3880ab
	struct rtattr *rta = (void *)buf;
Packit Service 3880ab
	struct rtnexthop *rtnh;
Packit Service 3880ab
Packit Service 3880ab
	rta->rta_type = RTA_MULTIPATH;
Packit Service 3880ab
	rta->rta_len = RTA_LENGTH(0);
Packit Service 3880ab
	rtnh = RTA_DATA(rta);
Packit Service 3880ab
Packit Service 3880ab
	while (argc > 0) {
Packit Service 3880ab
		if (strcmp(*argv, "nexthop") != 0) {
Packit Service 3880ab
			fprintf(stderr, "Error: \"nexthop\" or end of line is expected instead of \"%s\"\n", *argv);
Packit Service 3880ab
			exit(-1);
Packit Service 3880ab
		}
Packit Service 3880ab
		if (argc <= 1) {
Packit Service 3880ab
			fprintf(stderr, "Error: unexpected end of line after \"nexthop\"\n");
Packit Service 3880ab
			exit(-1);
Packit Service 3880ab
		}
Packit Service 3880ab
		memset(rtnh, 0, sizeof(*rtnh));
Packit Service 3880ab
		rtnh->rtnh_len = sizeof(*rtnh);
Packit Service 3880ab
		rta->rta_len += rtnh->rtnh_len;
Packit Service 3880ab
		if (parse_one_nh(n, r, rta, 4096, rtnh, &argc, &argv)) {
Packit Service 3880ab
			fprintf(stderr, "Error: cannot parse nexthop\n");
Packit Service 3880ab
			exit(-1);
Packit Service 3880ab
		}
Packit Service 3880ab
		rtnh = RTNH_NEXT(rtnh);
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (rta->rta_len > RTA_LENGTH(0))
Packit Service 3880ab
		return addattr_l(n, 4096, RTA_MULTIPATH,
Packit Service 3880ab
				 RTA_DATA(rta), RTA_PAYLOAD(rta));
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int iproute_modify(int cmd, unsigned int flags, int argc, char **argv)
Packit Service 3880ab
{
Packit Service 3880ab
	struct {
Packit Service 3880ab
		struct nlmsghdr	n;
Packit Service 3880ab
		struct rtmsg		r;
Packit Service 3880ab
		char			buf[4096];
Packit Service 3880ab
	} req = {
Packit Service 3880ab
		.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)),
Packit Service 3880ab
		.n.nlmsg_flags = NLM_F_REQUEST | flags,
Packit Service 3880ab
		.n.nlmsg_type = cmd,
Packit Service 3880ab
		.r.rtm_family = preferred_family,
Packit Service 3880ab
		.r.rtm_table = RT_TABLE_MAIN,
Packit Service 3880ab
		.r.rtm_scope = RT_SCOPE_NOWHERE,
Packit Service 3880ab
	};
Packit Service 3880ab
	char  mxbuf[256];
Packit Service 3880ab
	struct rtattr *mxrta = (void *)mxbuf;
Packit Service 3880ab
	unsigned int mxlock = 0;
Packit Service 3880ab
	char  *d = NULL;
Packit Service 3880ab
	int gw_ok = 0;
Packit Service 3880ab
	int dst_ok = 0;
Packit Service 3880ab
	int nhs_ok = 0;
Packit Service 3880ab
	int scope_ok = 0;
Packit Service 3880ab
	int table_ok = 0;
Packit Service 3880ab
	int raw = 0;
Packit Service 3880ab
	int type_ok = 0;
Packit Service 3880ab
	__u32 nhid = 0;
Packit Service 3880ab
Packit Service 3880ab
	if (cmd != RTM_DELROUTE) {
Packit Service 3880ab
		req.r.rtm_protocol = RTPROT_BOOT;
Packit Service 3880ab
		req.r.rtm_scope = RT_SCOPE_UNIVERSE;
Packit Service 3880ab
		req.r.rtm_type = RTN_UNICAST;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	mxrta->rta_type = RTA_METRICS;
Packit Service 3880ab
	mxrta->rta_len = RTA_LENGTH(0);
Packit Service 3880ab
Packit Service 3880ab
	while (argc > 0) {
Packit Service 3880ab
		if (strcmp(*argv, "src") == 0) {
Packit Service 3880ab
			inet_prefix addr;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			get_addr(&addr, *argv, req.r.rtm_family);
Packit Service 3880ab
			if (req.r.rtm_family == AF_UNSPEC)
Packit Service 3880ab
				req.r.rtm_family = addr.family;
Packit Service 3880ab
			addattr_l(&req.n, sizeof(req),
Packit Service 3880ab
				  RTA_PREFSRC, &addr.data, addr.bytelen);
Packit Service 3880ab
		} else if (strcmp(*argv, "as") == 0) {
Packit Service 3880ab
			inet_prefix addr;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (strcmp(*argv, "to") == 0) {
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
			}
Packit Service 3880ab
			get_addr(&addr, *argv, req.r.rtm_family);
Packit Service 3880ab
			if (req.r.rtm_family == AF_UNSPEC)
Packit Service 3880ab
				req.r.rtm_family = addr.family;
Packit Service 3880ab
			addattr_l(&req.n, sizeof(req),
Packit Service 3880ab
				  RTA_NEWDST, &addr.data, addr.bytelen);
Packit Service 3880ab
		} else if (strcmp(*argv, "via") == 0) {
Packit Service 3880ab
			inet_prefix addr;
Packit Service 3880ab
			int family;
Packit Service 3880ab
Packit Service 3880ab
			if (gw_ok) {
Packit Service 3880ab
				invarg("use nexthop syntax to specify multiple via\n",
Packit Service 3880ab
				       *argv);
Packit Service 3880ab
			}
Packit Service 3880ab
			gw_ok = 1;
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			family = read_family(*argv);
Packit Service 3880ab
			if (family == AF_UNSPEC)
Packit Service 3880ab
				family = req.r.rtm_family;
Packit Service 3880ab
			else
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
			get_addr(&addr, *argv, family);
Packit Service 3880ab
			if (req.r.rtm_family == AF_UNSPEC)
Packit Service 3880ab
				req.r.rtm_family = addr.family;
Packit Service 3880ab
			if (addr.family == req.r.rtm_family)
Packit Service 3880ab
				addattr_l(&req.n, sizeof(req), RTA_GATEWAY,
Packit Service 3880ab
					  &addr.data, addr.bytelen);
Packit Service 3880ab
			else
Packit Service 3880ab
				addattr_l(&req.n, sizeof(req), RTA_VIA,
Packit Service 3880ab
					  &addr.family, addr.bytelen+2);
Packit Service 3880ab
		} else if (strcmp(*argv, "from") == 0) {
Packit Service 3880ab
			inet_prefix addr;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			get_prefix(&addr, *argv, req.r.rtm_family);
Packit Service 3880ab
			if (req.r.rtm_family == AF_UNSPEC)
Packit Service 3880ab
				req.r.rtm_family = addr.family;
Packit Service 3880ab
			if (addr.bytelen)
Packit Service 3880ab
				addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
Packit Service 3880ab
			req.r.rtm_src_len = addr.bitlen;
Packit Service 3880ab
		} else if (strcmp(*argv, "tos") == 0 ||
Packit Service 3880ab
			   matches(*argv, "dsfield") == 0) {
Packit Service 3880ab
			__u32 tos;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (rtnl_dsfield_a2n(&tos, *argv))
Packit Service 3880ab
				invarg("\"tos\" value is invalid\n", *argv);
Packit Service 3880ab
			req.r.rtm_tos = tos;
Packit Service 3880ab
		} else if (strcmp(*argv, "expires") == 0) {
Packit Service 3880ab
			__u32 expires;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (get_u32(&expires, *argv, 0))
Packit Service 3880ab
				invarg("\"expires\" value is invalid\n", *argv);
Packit Service 3880ab
			addattr32(&req.n, sizeof(req), RTA_EXPIRES, expires);
Packit Service 3880ab
		} else if (matches(*argv, "metric") == 0 ||
Packit Service 3880ab
			   matches(*argv, "priority") == 0 ||
Packit Service 3880ab
			   strcmp(*argv, "preference") == 0) {
Packit Service 3880ab
			__u32 metric;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (get_u32(&metric, *argv, 0))
Packit Service 3880ab
				invarg("\"metric\" value is invalid\n", *argv);
Packit Service 3880ab
			addattr32(&req.n, sizeof(req), RTA_PRIORITY, metric);
Packit Service 3880ab
		} else if (strcmp(*argv, "scope") == 0) {
Packit Service 3880ab
			__u32 scope = 0;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (rtnl_rtscope_a2n(&scope, *argv))
Packit Service 3880ab
				invarg("invalid \"scope\" value\n", *argv);
Packit Service 3880ab
			req.r.rtm_scope = scope;
Packit Service 3880ab
			scope_ok = 1;
Packit Service 3880ab
		} else if (strcmp(*argv, "mtu") == 0) {
Packit Service 3880ab
			unsigned int mtu;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (strcmp(*argv, "lock") == 0) {
Packit Service 3880ab
				mxlock |= (1<
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
			}
Packit Service 3880ab
			if (get_unsigned(&mtu, *argv, 0))
Packit Service 3880ab
				invarg("\"mtu\" value is invalid\n", *argv);
Packit Service 3880ab
			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
Packit Service 3880ab
		} else if (strcmp(*argv, "hoplimit") == 0) {
Packit Service 3880ab
			unsigned int hoplimit;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (strcmp(*argv, "lock") == 0) {
Packit Service 3880ab
				mxlock |= (1<
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
			}
Packit Service 3880ab
			if (get_unsigned(&hoplimit, *argv, 0) || hoplimit > 255)
Packit Service 3880ab
				invarg("\"hoplimit\" value is invalid\n", *argv);
Packit Service 3880ab
			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_HOPLIMIT, hoplimit);
Packit Service 3880ab
		} else if (strcmp(*argv, "advmss") == 0) {
Packit Service 3880ab
			unsigned int mss;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (strcmp(*argv, "lock") == 0) {
Packit Service 3880ab
				mxlock |= (1<
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
			}
Packit Service 3880ab
			if (get_unsigned(&mss, *argv, 0))
Packit Service 3880ab
				invarg("\"mss\" value is invalid\n", *argv);
Packit Service 3880ab
			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_ADVMSS, mss);
Packit Service 3880ab
		} else if (matches(*argv, "reordering") == 0) {
Packit Service 3880ab
			unsigned int reord;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (strcmp(*argv, "lock") == 0) {
Packit Service 3880ab
				mxlock |= (1<
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
			}
Packit Service 3880ab
			if (get_unsigned(&reord, *argv, 0))
Packit Service 3880ab
				invarg("\"reordering\" value is invalid\n", *argv);
Packit Service 3880ab
			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_REORDERING, reord);
Packit Service 3880ab
		} else if (strcmp(*argv, "rtt") == 0) {
Packit Service 3880ab
			unsigned int rtt;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (strcmp(*argv, "lock") == 0) {
Packit Service 3880ab
				mxlock |= (1<
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
			}
Packit Service 3880ab
			if (get_time_rtt(&rtt, *argv, &raw))
Packit Service 3880ab
				invarg("\"rtt\" value is invalid\n", *argv);
Packit Service 3880ab
			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTT,
Packit Service 3880ab
				(raw) ? rtt : rtt * 8);
Packit Service 3880ab
		} else if (strcmp(*argv, "rto_min") == 0) {
Packit Service 3880ab
			unsigned int rto_min;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			mxlock |= (1<
Packit Service 3880ab
			if (get_time_rtt(&rto_min, *argv, &raw))
Packit Service 3880ab
				invarg("\"rto_min\" value is invalid\n",
Packit Service 3880ab
				       *argv);
Packit Service 3880ab
			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTO_MIN,
Packit Service 3880ab
				      rto_min);
Packit Service 3880ab
		} else if (matches(*argv, "window") == 0) {
Packit Service 3880ab
			unsigned int win;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (strcmp(*argv, "lock") == 0) {
Packit Service 3880ab
				mxlock |= (1<
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
			}
Packit Service 3880ab
			if (get_unsigned(&win, *argv, 0))
Packit Service 3880ab
				invarg("\"window\" value is invalid\n", *argv);
Packit Service 3880ab
			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_WINDOW, win);
Packit Service 3880ab
		} else if (matches(*argv, "cwnd") == 0) {
Packit Service 3880ab
			unsigned int win;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (strcmp(*argv, "lock") == 0) {
Packit Service 3880ab
				mxlock |= (1<
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
			}
Packit Service 3880ab
			if (get_unsigned(&win, *argv, 0))
Packit Service 3880ab
				invarg("\"cwnd\" value is invalid\n", *argv);
Packit Service 3880ab
			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_CWND, win);
Packit Service 3880ab
		} else if (matches(*argv, "initcwnd") == 0) {
Packit Service 3880ab
			unsigned int win;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (strcmp(*argv, "lock") == 0) {
Packit Service 3880ab
				mxlock |= (1<
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
			}
Packit Service 3880ab
			if (get_unsigned(&win, *argv, 0))
Packit Service 3880ab
				invarg("\"initcwnd\" value is invalid\n", *argv);
Packit Service 3880ab
			rta_addattr32(mxrta, sizeof(mxbuf),
Packit Service 3880ab
				      RTAX_INITCWND, win);
Packit Service 3880ab
		} else if (matches(*argv, "initrwnd") == 0) {
Packit Service 3880ab
			unsigned int win;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (strcmp(*argv, "lock") == 0) {
Packit Service 3880ab
				mxlock |= (1<
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
			}
Packit Service 3880ab
			if (get_unsigned(&win, *argv, 0))
Packit Service 3880ab
				invarg("\"initrwnd\" value is invalid\n", *argv);
Packit Service 3880ab
			rta_addattr32(mxrta, sizeof(mxbuf),
Packit Service 3880ab
				      RTAX_INITRWND, win);
Packit Service 3880ab
		} else if (matches(*argv, "features") == 0) {
Packit Service 3880ab
			unsigned int features = 0;
Packit Service 3880ab
Packit Service 3880ab
			while (argc > 0) {
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
Packit Service 3880ab
				if (strcmp(*argv, "ecn") == 0)
Packit Service 3880ab
					features |= RTAX_FEATURE_ECN;
Packit Service 3880ab
				else
Packit Service 3880ab
					invarg("\"features\" value not valid\n", *argv);
Packit Service 3880ab
				break;
Packit Service 3880ab
			}
Packit Service 3880ab
Packit Service 3880ab
			rta_addattr32(mxrta, sizeof(mxbuf),
Packit Service 3880ab
				      RTAX_FEATURES, features);
Packit Service 3880ab
		} else if (matches(*argv, "quickack") == 0) {
Packit Service 3880ab
			unsigned int quickack;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (get_unsigned(&quickack, *argv, 0))
Packit Service 3880ab
				invarg("\"quickack\" value is invalid\n", *argv);
Packit Service 3880ab
			if (quickack != 1 && quickack != 0)
Packit Service 3880ab
				invarg("\"quickack\" value should be 0 or 1\n", *argv);
Packit Service 3880ab
			rta_addattr32(mxrta, sizeof(mxbuf),
Packit Service 3880ab
				      RTAX_QUICKACK, quickack);
Packit Service 3880ab
		} else if (matches(*argv, "congctl") == 0) {
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (strcmp(*argv, "lock") == 0) {
Packit Service 3880ab
				mxlock |= 1 << RTAX_CC_ALGO;
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
			}
Packit Service 3880ab
			rta_addattr_l(mxrta, sizeof(mxbuf), RTAX_CC_ALGO, *argv,
Packit Service 3880ab
				      strlen(*argv));
Packit Service 3880ab
		} else if (matches(*argv, "rttvar") == 0) {
Packit Service 3880ab
			unsigned int win;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (strcmp(*argv, "lock") == 0) {
Packit Service 3880ab
				mxlock |= (1<
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
			}
Packit Service 3880ab
			if (get_time_rtt(&win, *argv, &raw))
Packit Service 3880ab
				invarg("\"rttvar\" value is invalid\n", *argv);
Packit Service 3880ab
			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTTVAR,
Packit Service 3880ab
				(raw) ? win : win * 4);
Packit Service 3880ab
		} else if (matches(*argv, "ssthresh") == 0) {
Packit Service 3880ab
			unsigned int win;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (strcmp(*argv, "lock") == 0) {
Packit Service 3880ab
				mxlock |= (1<
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
			}
Packit Service 3880ab
			if (get_unsigned(&win, *argv, 0))
Packit Service 3880ab
				invarg("\"ssthresh\" value is invalid\n", *argv);
Packit Service 3880ab
			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_SSTHRESH, win);
Packit Service 3880ab
		} else if (matches(*argv, "realms") == 0) {
Packit Service 3880ab
			__u32 realm;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (get_rt_realms_or_raw(&realm, *argv))
Packit Service 3880ab
				invarg("\"realm\" value is invalid\n", *argv);
Packit Service 3880ab
			addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
Packit Service 3880ab
		} else if (strcmp(*argv, "onlink") == 0) {
Packit Service 3880ab
			req.r.rtm_flags |= RTNH_F_ONLINK;
Packit Service 3880ab
		} else if (strcmp(*argv, "nexthop") == 0) {
Packit Service 3880ab
			nhs_ok = 1;
Packit Service 3880ab
			break;
Packit Service 3880ab
		} else if (!strcmp(*argv, "nhid")) {
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (get_u32(&nhid, *argv, 0))
Packit Service 3880ab
				invarg("\"id\" value is invalid\n", *argv);
Packit Service 3880ab
			addattr32(&req.n, sizeof(req), RTA_NH_ID, nhid);
Packit Service 3880ab
		} else if (matches(*argv, "protocol") == 0) {
Packit Service 3880ab
			__u32 prot;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (rtnl_rtprot_a2n(&prot, *argv))
Packit Service 3880ab
				invarg("\"protocol\" value is invalid\n", *argv);
Packit Service 3880ab
			req.r.rtm_protocol = prot;
Packit Service 3880ab
		} else if (matches(*argv, "table") == 0) {
Packit Service 3880ab
			__u32 tid;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (rtnl_rttable_a2n(&tid, *argv))
Packit Service 3880ab
				invarg("\"table\" value is invalid\n", *argv);
Packit Service 3880ab
			if (tid < 256)
Packit Service 3880ab
				req.r.rtm_table = tid;
Packit Service 3880ab
			else {
Packit Service 3880ab
				req.r.rtm_table = RT_TABLE_UNSPEC;
Packit Service 3880ab
				addattr32(&req.n, sizeof(req), RTA_TABLE, tid);
Packit Service 3880ab
			}
Packit Service 3880ab
			table_ok = 1;
Packit Service 3880ab
		} else if (matches(*argv, "vrf") == 0) {
Packit Service 3880ab
			__u32 tid;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			tid = ipvrf_get_table(*argv);
Packit Service 3880ab
			if (tid == 0)
Packit Service 3880ab
				invarg("Invalid VRF\n", *argv);
Packit Service 3880ab
			if (tid < 256)
Packit Service 3880ab
				req.r.rtm_table = tid;
Packit Service 3880ab
			else {
Packit Service 3880ab
				req.r.rtm_table = RT_TABLE_UNSPEC;
Packit Service 3880ab
				addattr32(&req.n, sizeof(req), RTA_TABLE, tid);
Packit Service 3880ab
			}
Packit Service 3880ab
			table_ok = 1;
Packit Service 3880ab
		} else if (strcmp(*argv, "dev") == 0 ||
Packit Service 3880ab
			   strcmp(*argv, "oif") == 0) {
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			d = *argv;
Packit Service 3880ab
		} else if (matches(*argv, "pref") == 0) {
Packit Service 3880ab
			__u8 pref;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (strcmp(*argv, "low") == 0)
Packit Service 3880ab
				pref = ICMPV6_ROUTER_PREF_LOW;
Packit Service 3880ab
			else if (strcmp(*argv, "medium") == 0)
Packit Service 3880ab
				pref = ICMPV6_ROUTER_PREF_MEDIUM;
Packit Service 3880ab
			else if (strcmp(*argv, "high") == 0)
Packit Service 3880ab
				pref = ICMPV6_ROUTER_PREF_HIGH;
Packit Service 3880ab
			else if (get_u8(&pref, *argv, 0))
Packit Service 3880ab
				invarg("\"pref\" value is invalid\n", *argv);
Packit Service 3880ab
			addattr8(&req.n, sizeof(req), RTA_PREF, pref);
Packit Service 3880ab
		} else if (strcmp(*argv, "encap") == 0) {
Packit Service 3880ab
			char buf[1024];
Packit Service 3880ab
			struct rtattr *rta = (void *)buf;
Packit Service 3880ab
Packit Service 3880ab
			rta->rta_type = RTA_ENCAP;
Packit Service 3880ab
			rta->rta_len = RTA_LENGTH(0);
Packit Service 3880ab
Packit Service 3880ab
			lwt_parse_encap(rta, sizeof(buf), &argc, &argv,
Packit Service 3880ab
					RTA_ENCAP, RTA_ENCAP_TYPE);
Packit Service 3880ab
Packit Service 3880ab
			if (rta->rta_len > RTA_LENGTH(0))
Packit Service 3880ab
				addraw_l(&req.n, 1024
Packit Service 3880ab
					 , RTA_DATA(rta), RTA_PAYLOAD(rta));
Packit Service 3880ab
		} else if (strcmp(*argv, "ttl-propagate") == 0) {
Packit Service 3880ab
			__u8 ttl_prop;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (matches(*argv, "enabled") == 0)
Packit Service 3880ab
				ttl_prop = 1;
Packit Service 3880ab
			else if (matches(*argv, "disabled") == 0)
Packit Service 3880ab
				ttl_prop = 0;
Packit Service 3880ab
			else
Packit Service 3880ab
				invarg("\"ttl-propagate\" value is invalid\n",
Packit Service 3880ab
				       *argv);
Packit Service 3880ab
Packit Service 3880ab
			addattr8(&req.n, sizeof(req), RTA_TTL_PROPAGATE,
Packit Service 3880ab
				 ttl_prop);
Packit Service 3880ab
		} else if (matches(*argv, "fastopen_no_cookie") == 0) {
Packit Service 3880ab
			unsigned int fastopen_no_cookie;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (get_unsigned(&fastopen_no_cookie, *argv, 0))
Packit Service 3880ab
				invarg("\"fastopen_no_cookie\" value is invalid\n", *argv);
Packit Service 3880ab
			if (fastopen_no_cookie != 1 && fastopen_no_cookie != 0)
Packit Service 3880ab
				invarg("\"fastopen_no_cookie\" value should be 0 or 1\n", *argv);
Packit Service 3880ab
			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_FASTOPEN_NO_COOKIE, fastopen_no_cookie);
Packit Service 3880ab
		} else {
Packit Service 3880ab
			int type;
Packit Service 3880ab
			inet_prefix dst;
Packit Service 3880ab
Packit Service 3880ab
			if (strcmp(*argv, "to") == 0) {
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
			}
Packit Service 3880ab
			if ((**argv < '0' || **argv > '9') &&
Packit Service 3880ab
			    rtnl_rtntype_a2n(&type, *argv) == 0) {
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
				req.r.rtm_type = type;
Packit Service 3880ab
				type_ok = 1;
Packit Service 3880ab
			}
Packit Service 3880ab
Packit Service 3880ab
			if (matches(*argv, "help") == 0)
Packit Service 3880ab
				usage();
Packit Service 3880ab
			if (dst_ok)
Packit Service 3880ab
				duparg2("to", *argv);
Packit Service 3880ab
			get_prefix(&dst, *argv, req.r.rtm_family);
Packit Service 3880ab
			if (req.r.rtm_family == AF_UNSPEC)
Packit Service 3880ab
				req.r.rtm_family = dst.family;
Packit Service 3880ab
			req.r.rtm_dst_len = dst.bitlen;
Packit Service 3880ab
			dst_ok = 1;
Packit Service 3880ab
			if (dst.bytelen)
Packit Service 3880ab
				addattr_l(&req.n, sizeof(req),
Packit Service 3880ab
					  RTA_DST, &dst.data, dst.bytelen);
Packit Service 3880ab
		}
Packit Service 3880ab
		argc--; argv++;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (!dst_ok)
Packit Service 3880ab
		usage();
Packit Service 3880ab
Packit Service 3880ab
	if (d) {
Packit Service 3880ab
		int idx = ll_name_to_index(d);
Packit Service 3880ab
Packit Service 3880ab
		if (!idx)
Packit Service 3880ab
			return nodev(d);
Packit Service 3880ab
		addattr32(&req.n, sizeof(req), RTA_OIF, idx);
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (mxrta->rta_len > RTA_LENGTH(0)) {
Packit Service 3880ab
		if (mxlock)
Packit Service 3880ab
			rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
Packit Service 3880ab
		addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (nhs_ok && parse_nexthops(&req.n, &req.r, argc, argv))
Packit Service 3880ab
		return -1;
Packit Service 3880ab
Packit Service 3880ab
	if (req.r.rtm_family == AF_UNSPEC)
Packit Service 3880ab
		req.r.rtm_family = AF_INET;
Packit Service 3880ab
Packit Service 3880ab
	if (!table_ok) {
Packit Service 3880ab
		if (req.r.rtm_type == RTN_LOCAL ||
Packit Service 3880ab
		    req.r.rtm_type == RTN_BROADCAST ||
Packit Service 3880ab
		    req.r.rtm_type == RTN_NAT ||
Packit Service 3880ab
		    req.r.rtm_type == RTN_ANYCAST)
Packit Service 3880ab
			req.r.rtm_table = RT_TABLE_LOCAL;
Packit Service 3880ab
	}
Packit Service 3880ab
	if (!scope_ok) {
Packit Service 3880ab
		if (req.r.rtm_family == AF_INET6 ||
Packit Service 3880ab
		    req.r.rtm_family == AF_MPLS)
Packit Service 3880ab
			req.r.rtm_scope = RT_SCOPE_UNIVERSE;
Packit Service 3880ab
		else if (req.r.rtm_type == RTN_LOCAL ||
Packit Service 3880ab
			 req.r.rtm_type == RTN_NAT)
Packit Service 3880ab
			req.r.rtm_scope = RT_SCOPE_HOST;
Packit Service 3880ab
		else if (req.r.rtm_type == RTN_BROADCAST ||
Packit Service 3880ab
			 req.r.rtm_type == RTN_MULTICAST ||
Packit Service 3880ab
			 req.r.rtm_type == RTN_ANYCAST)
Packit Service 3880ab
			req.r.rtm_scope = RT_SCOPE_LINK;
Packit Service 3880ab
		else if (req.r.rtm_type == RTN_UNICAST ||
Packit Service 3880ab
			 req.r.rtm_type == RTN_UNSPEC) {
Packit Service 3880ab
			if (cmd == RTM_DELROUTE)
Packit Service 3880ab
				req.r.rtm_scope = RT_SCOPE_NOWHERE;
Packit Service 3880ab
			else if (!gw_ok && !nhs_ok && !nhid)
Packit Service 3880ab
				req.r.rtm_scope = RT_SCOPE_LINK;
Packit Service 3880ab
		}
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (!type_ok && req.r.rtm_family == AF_MPLS)
Packit Service 3880ab
		req.r.rtm_type = RTN_UNICAST;
Packit Service 3880ab
Packit Service 3880ab
	if (rtnl_talk(&rth, &req.n, NULL) < 0)
Packit Service 3880ab
		return -2;
Packit Service 3880ab
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int iproute_flush_cache(void)
Packit Service 3880ab
{
Packit Service 3880ab
#define ROUTE_FLUSH_PATH "/proc/sys/net/ipv4/route/flush"
Packit Service 3880ab
Packit Service 3880ab
	int len;
Packit Service 3880ab
	int flush_fd = open(ROUTE_FLUSH_PATH, O_WRONLY);
Packit Service 3880ab
	char *buffer = "-1";
Packit Service 3880ab
Packit Service 3880ab
	if (flush_fd < 0) {
Packit Service 3880ab
		fprintf(stderr, "Cannot open \"%s\": %s\n",
Packit Service 3880ab
				ROUTE_FLUSH_PATH, strerror(errno));
Packit Service 3880ab
		return -1;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	len = strlen(buffer);
Packit Service 3880ab
Packit Service 3880ab
	if ((write(flush_fd, (void *)buffer, len)) < len) {
Packit Service 3880ab
		fprintf(stderr, "Cannot flush routing cache\n");
Packit Service 3880ab
		close(flush_fd);
Packit Service 3880ab
		return -1;
Packit Service 3880ab
	}
Packit Service 3880ab
	close(flush_fd);
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static __u32 route_dump_magic = 0x45311224;
Packit Service 3880ab
Packit Service 3880ab
static int save_route(struct nlmsghdr *n, void *arg)
Packit Service 3880ab
{
Packit Service 3880ab
	int ret;
Packit Service 3880ab
	int len = n->nlmsg_len;
Packit Service 3880ab
	struct rtmsg *r = NLMSG_DATA(n);
Packit Service 3880ab
	struct rtattr *tb[RTA_MAX+1];
Packit Service 3880ab
	int host_len;
Packit Service 3880ab
Packit Service 3880ab
	host_len = af_bit_len(r->rtm_family);
Packit Service 3880ab
	len -= NLMSG_LENGTH(sizeof(*r));
Packit Service 3880ab
	parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
Packit Service 3880ab
Packit Service 3880ab
	if (!filter_nlmsg(n, tb, host_len))
Packit Service 3880ab
		return 0;
Packit Service 3880ab
Packit Service 3880ab
	ret = write(STDOUT_FILENO, n, n->nlmsg_len);
Packit Service 3880ab
	if ((ret > 0) && (ret != n->nlmsg_len)) {
Packit Service 3880ab
		fprintf(stderr, "Short write while saving nlmsg\n");
Packit Service 3880ab
		ret = -EIO;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	return ret == n->nlmsg_len ? 0 : ret;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int save_route_prep(void)
Packit Service 3880ab
{
Packit Service 3880ab
	int ret;
Packit Service 3880ab
Packit Service 3880ab
	if (isatty(STDOUT_FILENO)) {
Packit Service 3880ab
		fprintf(stderr, "Not sending a binary stream to stdout\n");
Packit Service 3880ab
		return -1;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	ret = write(STDOUT_FILENO, &route_dump_magic, sizeof(route_dump_magic));
Packit Service 3880ab
	if (ret != sizeof(route_dump_magic)) {
Packit Service 3880ab
		fprintf(stderr, "Can't write magic to dump file\n");
Packit Service 3880ab
		return -1;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int iproute_dump_filter(struct nlmsghdr *nlh, int reqlen)
Packit Service 3880ab
{
Packit Service 3880ab
	struct rtmsg *rtm = NLMSG_DATA(nlh);
Packit Service 3880ab
	int err;
Packit Service 3880ab
Packit Service 3880ab
	rtm->rtm_protocol = filter.protocol;
Packit Service 3880ab
	if (filter.cloned)
Packit Service 3880ab
		rtm->rtm_flags |= RTM_F_CLONED;
Packit Service 3880ab
Packit Service 3880ab
	if (filter.tb) {
Packit Service 3880ab
		err = addattr32(nlh, reqlen, RTA_TABLE, filter.tb);
Packit Service 3880ab
		if (err)
Packit Service 3880ab
			return err;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (filter.oif) {
Packit Service 3880ab
		err = addattr32(nlh, reqlen, RTA_OIF, filter.oif);
Packit Service 3880ab
		if (err)
Packit Service 3880ab
			return err;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int iproute_flush(int family, rtnl_filter_t filter_fn)
Packit Service 3880ab
{
Packit Service 3880ab
	time_t start = time(0);
Packit Service 3880ab
	char flushb[4096-512];
Packit Service 3880ab
	int round = 0;
Packit Service 3880ab
	int ret;
Packit Service 3880ab
Packit Service 3880ab
	if (filter.cloned) {
Packit Service 3880ab
		if (family != AF_INET6) {
Packit Service 3880ab
			iproute_flush_cache();
Packit Service 3880ab
			if (show_stats)
Packit Service 3880ab
				printf("*** IPv4 routing cache is flushed.\n");
Packit Service 3880ab
		}
Packit Service 3880ab
		if (family == AF_INET)
Packit Service 3880ab
			return 0;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	filter.flushb = flushb;
Packit Service 3880ab
	filter.flushp = 0;
Packit Service 3880ab
	filter.flushe = sizeof(flushb);
Packit Service 3880ab
Packit Service 3880ab
	for (;;) {
Packit Service 3880ab
		if (rtnl_routedump_req(&rth, family, iproute_dump_filter) < 0) {
Packit Service 3880ab
			perror("Cannot send dump request");
Packit Service 3880ab
			return -2;
Packit Service 3880ab
		}
Packit Service 3880ab
		filter.flushed = 0;
Packit Service 3880ab
		if (rtnl_dump_filter(&rth, filter_fn, stdout) < 0) {
Packit Service 3880ab
			fprintf(stderr, "Flush terminated\n");
Packit Service 3880ab
			return -2;
Packit Service 3880ab
		}
Packit Service 3880ab
		if (filter.flushed == 0) {
Packit Service 3880ab
			if (show_stats) {
Packit Service 3880ab
				if (round == 0 &&
Packit Service 3880ab
				    (!filter.cloned || family == AF_INET6))
Packit Service 3880ab
					printf("Nothing to flush.\n");
Packit Service 3880ab
				else
Packit Service 3880ab
					printf("*** Flush is complete after %d round%s ***\n",
Packit Service 3880ab
					       round, round > 1 ? "s" : "");
Packit Service 3880ab
			}
Packit Service 3880ab
			fflush(stdout);
Packit Service 3880ab
			return 0;
Packit Service 3880ab
		}
Packit Service 3880ab
		round++;
Packit Service 3880ab
		ret = flush_update();
Packit Service 3880ab
		if (ret < 0)
Packit Service 3880ab
			return ret;
Packit Service 3880ab
Packit Service 3880ab
		if (time(0) - start > 30) {
Packit Service 3880ab
			printf("\n*** Flush not completed after %ld seconds, %d entries remain ***\n",
Packit Service 3880ab
			       (long)(time(0) - start), filter.flushed);
Packit Service 3880ab
			return -1;
Packit Service 3880ab
		}
Packit Service 3880ab
Packit Service 3880ab
		if (show_stats) {
Packit Service 3880ab
			printf("\n*** Round %d, deleting %d entries ***\n",
Packit Service 3880ab
			       round, filter.flushed);
Packit Service 3880ab
			fflush(stdout);
Packit Service 3880ab
		}
Packit Service 3880ab
	}
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int iproute_list_flush_or_save(int argc, char **argv, int action)
Packit Service 3880ab
{
Packit Service 3880ab
	int dump_family = preferred_family;
Packit Service 3880ab
	char *id = NULL;
Packit Service 3880ab
	char *od = NULL;
Packit Service 3880ab
	unsigned int mark = 0;
Packit Service 3880ab
	rtnl_filter_t filter_fn;
Packit Service 3880ab
Packit Service 3880ab
	if (action == IPROUTE_SAVE) {
Packit Service 3880ab
		if (save_route_prep())
Packit Service 3880ab
			return -1;
Packit Service 3880ab
Packit Service 3880ab
		filter_fn = save_route;
Packit Service 3880ab
	} else
Packit Service 3880ab
		filter_fn = print_route;
Packit Service 3880ab
Packit Service 3880ab
	iproute_reset_filter(0);
Packit Service 3880ab
	filter.tb = RT_TABLE_MAIN;
Packit Service 3880ab
Packit Service 3880ab
	if ((action == IPROUTE_FLUSH) && argc <= 0) {
Packit Service 3880ab
		fprintf(stderr, "\"ip route flush\" requires arguments.\n");
Packit Service 3880ab
		return -1;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	while (argc > 0) {
Packit Service 3880ab
		if (matches(*argv, "table") == 0) {
Packit Service 3880ab
			__u32 tid;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (rtnl_rttable_a2n(&tid, *argv)) {
Packit Service 3880ab
				if (strcmp(*argv, "all") == 0) {
Packit Service 3880ab
					filter.tb = 0;
Packit Service 3880ab
				} else if (strcmp(*argv, "cache") == 0) {
Packit Service 3880ab
					filter.cloned = 1;
Packit Service 3880ab
				} else if (strcmp(*argv, "help") == 0) {
Packit Service 3880ab
					usage();
Packit Service 3880ab
				} else {
Packit Service 3880ab
					invarg("table id value is invalid\n", *argv);
Packit Service 3880ab
				}
Packit Service 3880ab
			} else
Packit Service 3880ab
				filter.tb = tid;
Packit Service 3880ab
		} else if (matches(*argv, "vrf") == 0) {
Packit Service 3880ab
			__u32 tid;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			tid = ipvrf_get_table(*argv);
Packit Service 3880ab
			if (tid == 0)
Packit Service 3880ab
				invarg("Invalid VRF\n", *argv);
Packit Service 3880ab
			filter.tb = tid;
Packit Service 3880ab
			filter.typemask = ~(1 << RTN_LOCAL | 1<
Packit Service 3880ab
		} else if (matches(*argv, "cached") == 0 ||
Packit Service 3880ab
			   matches(*argv, "cloned") == 0) {
Packit Service 3880ab
			filter.cloned = 1;
Packit Service 3880ab
		} else if (strcmp(*argv, "tos") == 0 ||
Packit Service 3880ab
			   matches(*argv, "dsfield") == 0) {
Packit Service 3880ab
			__u32 tos;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (rtnl_dsfield_a2n(&tos, *argv))
Packit Service 3880ab
				invarg("TOS value is invalid\n", *argv);
Packit Service 3880ab
			filter.tos = tos;
Packit Service 3880ab
			filter.tosmask = -1;
Packit Service 3880ab
		} else if (matches(*argv, "protocol") == 0) {
Packit Service 3880ab
			__u32 prot = 0;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			filter.protocolmask = -1;
Packit Service 3880ab
			if (rtnl_rtprot_a2n(&prot, *argv)) {
Packit Service 3880ab
				if (strcmp(*argv, "all") != 0)
Packit Service 3880ab
					invarg("invalid \"protocol\"\n", *argv);
Packit Service 3880ab
				prot = 0;
Packit Service 3880ab
				filter.protocolmask = 0;
Packit Service 3880ab
			}
Packit Service 3880ab
			filter.protocol = prot;
Packit Service 3880ab
		} else if (matches(*argv, "scope") == 0) {
Packit Service 3880ab
			__u32 scope = 0;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			filter.scopemask = -1;
Packit Service 3880ab
			if (rtnl_rtscope_a2n(&scope, *argv)) {
Packit Service 3880ab
				if (strcmp(*argv, "all") != 0)
Packit Service 3880ab
					invarg("invalid \"scope\"\n", *argv);
Packit Service 3880ab
				scope = RT_SCOPE_NOWHERE;
Packit Service 3880ab
				filter.scopemask = 0;
Packit Service 3880ab
			}
Packit Service 3880ab
			filter.scope = scope;
Packit Service 3880ab
		} else if (matches(*argv, "type") == 0) {
Packit Service 3880ab
			int type;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (rtnl_rtntype_a2n(&type, *argv))
Packit Service 3880ab
				invarg("node type value is invalid\n", *argv);
Packit Service 3880ab
			filter.typemask = (1<
Packit Service 3880ab
		} else if (strcmp(*argv, "dev") == 0 ||
Packit Service 3880ab
			   strcmp(*argv, "oif") == 0) {
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			od = *argv;
Packit Service 3880ab
		} else if (strcmp(*argv, "iif") == 0) {
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			id = *argv;
Packit Service 3880ab
		} else if (strcmp(*argv, "mark") == 0) {
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (get_unsigned(&mark, *argv, 0))
Packit Service 3880ab
				invarg("invalid mark value", *argv);
Packit Service 3880ab
			filter.markmask = -1;
Packit Service 3880ab
		} else if (matches(*argv, "metric") == 0 ||
Packit Service 3880ab
			   matches(*argv, "priority") == 0 ||
Packit Service 3880ab
			   strcmp(*argv, "preference") == 0) {
Packit Service 3880ab
			__u32 metric;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (get_u32(&metric, *argv, 0))
Packit Service 3880ab
				invarg("\"metric\" value is invalid\n", *argv);
Packit Service 3880ab
			filter.metric = metric;
Packit Service 3880ab
			filter.metricmask = -1;
Packit Service 3880ab
		} else if (strcmp(*argv, "via") == 0) {
Packit Service 3880ab
			int family;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			family = read_family(*argv);
Packit Service 3880ab
			if (family == AF_UNSPEC)
Packit Service 3880ab
				family = dump_family;
Packit Service 3880ab
			else
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
			get_prefix(&filter.rvia, *argv, family);
Packit Service 3880ab
		} else if (strcmp(*argv, "src") == 0) {
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			get_prefix(&filter.rprefsrc, *argv, dump_family);
Packit Service 3880ab
		} else if (matches(*argv, "realms") == 0) {
Packit Service 3880ab
			__u32 realm;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (get_rt_realms_or_raw(&realm, *argv))
Packit Service 3880ab
				invarg("invalid realms\n", *argv);
Packit Service 3880ab
			filter.realm = realm;
Packit Service 3880ab
			filter.realmmask = ~0U;
Packit Service 3880ab
			if ((filter.realm&0xFFFF) == 0 &&
Packit Service 3880ab
			    (*argv)[strlen(*argv) - 1] == '/')
Packit Service 3880ab
				filter.realmmask &= ~0xFFFF;
Packit Service 3880ab
			if ((filter.realm&0xFFFF0000U) == 0 &&
Packit Service 3880ab
			    (strchr(*argv, '/') == NULL ||
Packit Service 3880ab
			     (*argv)[0] == '/'))
Packit Service 3880ab
				filter.realmmask &= ~0xFFFF0000U;
Packit Service 3880ab
		} else if (matches(*argv, "from") == 0) {
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (matches(*argv, "root") == 0) {
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
				get_prefix(&filter.rsrc, *argv, dump_family);
Packit Service 3880ab
			} else if (matches(*argv, "match") == 0) {
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
				get_prefix(&filter.msrc, *argv, dump_family);
Packit Service 3880ab
			} else {
Packit Service 3880ab
				if (matches(*argv, "exact") == 0) {
Packit Service 3880ab
					NEXT_ARG();
Packit Service 3880ab
				}
Packit Service 3880ab
				get_prefix(&filter.msrc, *argv, dump_family);
Packit Service 3880ab
				filter.rsrc = filter.msrc;
Packit Service 3880ab
			}
Packit Service 3880ab
		} else {
Packit Service 3880ab
			if (matches(*argv, "to") == 0) {
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
			}
Packit Service 3880ab
			if (matches(*argv, "root") == 0) {
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
				get_prefix(&filter.rdst, *argv, dump_family);
Packit Service 3880ab
			} else if (matches(*argv, "match") == 0) {
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
				get_prefix(&filter.mdst, *argv, dump_family);
Packit Service 3880ab
			} else {
Packit Service 3880ab
				if (matches(*argv, "exact") == 0) {
Packit Service 3880ab
					NEXT_ARG();
Packit Service 3880ab
				}
Packit Service 3880ab
				get_prefix(&filter.mdst, *argv, dump_family);
Packit Service 3880ab
				filter.rdst = filter.mdst;
Packit Service 3880ab
			}
Packit Service 3880ab
		}
Packit Service 3880ab
		argc--; argv++;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (dump_family == AF_UNSPEC && filter.tb)
Packit Service 3880ab
		dump_family = AF_INET;
Packit Service 3880ab
Packit Service 3880ab
	if (id || od)  {
Packit Service 3880ab
		int idx;
Packit Service 3880ab
Packit Service 3880ab
		if (id) {
Packit Service 3880ab
			idx = ll_name_to_index(id);
Packit Service 3880ab
			if (!idx)
Packit Service 3880ab
				return nodev(id);
Packit Service 3880ab
			filter.iif = idx;
Packit Service 3880ab
			filter.iifmask = -1;
Packit Service 3880ab
		}
Packit Service 3880ab
		if (od) {
Packit Service 3880ab
			idx = ll_name_to_index(od);
Packit Service 3880ab
			if (!idx)
Packit Service 3880ab
				return nodev(od);
Packit Service 3880ab
			filter.oif = idx;
Packit Service 3880ab
			filter.oifmask = -1;
Packit Service 3880ab
		}
Packit Service 3880ab
	}
Packit Service 3880ab
	filter.mark = mark;
Packit Service 3880ab
Packit Service 3880ab
	if (action == IPROUTE_FLUSH)
Packit Service 3880ab
		return iproute_flush(dump_family, filter_fn);
Packit Service 3880ab
Packit Service 3880ab
	if (rtnl_routedump_req(&rth, dump_family, iproute_dump_filter) < 0) {
Packit Service 3880ab
		perror("Cannot send dump request");
Packit Service 3880ab
		return -2;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	new_json_obj(json);
Packit Service 3880ab
Packit Service 3880ab
	if (rtnl_dump_filter(&rth, filter_fn, stdout) < 0) {
Packit Service 3880ab
		fprintf(stderr, "Dump terminated\n");
Packit Service 3880ab
		return -2;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	delete_json_obj();
Packit Service 3880ab
	fflush(stdout);
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
Packit Service 3880ab
static int iproute_get(int argc, char **argv)
Packit Service 3880ab
{
Packit Service 3880ab
	struct {
Packit Service 3880ab
		struct nlmsghdr	n;
Packit Service 3880ab
		struct rtmsg		r;
Packit Service 3880ab
		char			buf[1024];
Packit Service 3880ab
	} req = {
Packit Service 3880ab
		.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)),
Packit Service 3880ab
		.n.nlmsg_flags = NLM_F_REQUEST,
Packit Service 3880ab
		.n.nlmsg_type = RTM_GETROUTE,
Packit Service 3880ab
		.r.rtm_family = preferred_family,
Packit Service 3880ab
	};
Packit Service 3880ab
	char  *idev = NULL;
Packit Service 3880ab
	char  *odev = NULL;
Packit Service 3880ab
	struct nlmsghdr *answer;
Packit Service 3880ab
	int connected = 0;
Packit Service 3880ab
	int fib_match = 0;
Packit Service 3880ab
	int from_ok = 0;
Packit Service 3880ab
	unsigned int mark = 0;
Packit Service 3880ab
	bool address_found = false;
Packit Service 3880ab
Packit Service 3880ab
	iproute_reset_filter(0);
Packit Service 3880ab
	filter.cloned = 2;
Packit Service 3880ab
Packit Service 3880ab
	while (argc > 0) {
Packit Service 3880ab
		if (strcmp(*argv, "tos") == 0 ||
Packit Service 3880ab
		    matches(*argv, "dsfield") == 0) {
Packit Service 3880ab
			__u32 tos;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (rtnl_dsfield_a2n(&tos, *argv))
Packit Service 3880ab
				invarg("TOS value is invalid\n", *argv);
Packit Service 3880ab
			req.r.rtm_tos = tos;
Packit Service 3880ab
		} else if (matches(*argv, "from") == 0) {
Packit Service 3880ab
			inet_prefix addr;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (matches(*argv, "help") == 0)
Packit Service 3880ab
				usage();
Packit Service 3880ab
			from_ok = 1;
Packit Service 3880ab
			get_prefix(&addr, *argv, req.r.rtm_family);
Packit Service 3880ab
			if (req.r.rtm_family == AF_UNSPEC)
Packit Service 3880ab
				req.r.rtm_family = addr.family;
Packit Service 3880ab
			if (addr.bytelen)
Packit Service 3880ab
				addattr_l(&req.n, sizeof(req), RTA_SRC,
Packit Service 3880ab
					  &addr.data, addr.bytelen);
Packit Service 3880ab
			req.r.rtm_src_len = addr.bitlen;
Packit Service 3880ab
		} else if (matches(*argv, "iif") == 0) {
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			idev = *argv;
Packit Service 3880ab
		} else if (matches(*argv, "mark") == 0) {
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (get_unsigned(&mark, *argv, 0))
Packit Service 3880ab
				invarg("invalid mark value", *argv);
Packit Service 3880ab
		} else if (matches(*argv, "oif") == 0 ||
Packit Service 3880ab
			   strcmp(*argv, "dev") == 0) {
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			odev = *argv;
Packit Service 3880ab
		} else if (matches(*argv, "notify") == 0) {
Packit Service 3880ab
			req.r.rtm_flags |= RTM_F_NOTIFY;
Packit Service 3880ab
		} else if (matches(*argv, "connected") == 0) {
Packit Service 3880ab
			connected = 1;
Packit Service 3880ab
		} else if (matches(*argv, "vrf") == 0) {
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (!name_is_vrf(*argv))
Packit Service 3880ab
				invarg("Invalid VRF\n", *argv);
Packit Service 3880ab
			odev = *argv;
Packit Service 3880ab
		} else if (matches(*argv, "uid") == 0) {
Packit Service 3880ab
			uid_t uid;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (get_unsigned(&uid, *argv, 0))
Packit Service 3880ab
				invarg("invalid UID\n", *argv);
Packit Service 3880ab
			addattr32(&req.n, sizeof(req), RTA_UID, uid);
Packit Service 3880ab
		} else if (matches(*argv, "fibmatch") == 0) {
Packit Service 3880ab
			fib_match = 1;
Packit Service 3880ab
		} else if (strcmp(*argv, "as") == 0) {
Packit Service 3880ab
			inet_prefix addr;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (strcmp(*argv, "to") == 0)
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
			get_addr(&addr, *argv, req.r.rtm_family);
Packit Service 3880ab
			if (req.r.rtm_family == AF_UNSPEC)
Packit Service 3880ab
				req.r.rtm_family = addr.family;
Packit Service 3880ab
			addattr_l(&req.n, sizeof(req), RTA_NEWDST,
Packit Service 3880ab
				  &addr.data, addr.bytelen);
Packit Service 3880ab
		} else if (matches(*argv, "sport") == 0) {
Packit Service 3880ab
			__be16 sport;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (get_be16(&sport, *argv, 0))
Packit Service 3880ab
				invarg("invalid sport\n", *argv);
Packit Service 3880ab
			addattr16(&req.n, sizeof(req), RTA_SPORT, sport);
Packit Service 3880ab
		} else if (matches(*argv, "dport") == 0) {
Packit Service 3880ab
			__be16 dport;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			if (get_be16(&dport, *argv, 0))
Packit Service 3880ab
				invarg("invalid dport\n", *argv);
Packit Service 3880ab
			addattr16(&req.n, sizeof(req), RTA_DPORT, dport);
Packit Service 3880ab
		} else if (matches(*argv, "ipproto") == 0) {
Packit Service 3880ab
			int ipproto;
Packit Service 3880ab
Packit Service 3880ab
			NEXT_ARG();
Packit Service 3880ab
			ipproto = inet_proto_a2n(*argv);
Packit Service 3880ab
			if (ipproto < 0)
Packit Service 3880ab
				invarg("Invalid \"ipproto\" value\n",
Packit Service 3880ab
				       *argv);
Packit Service 3880ab
			addattr8(&req.n, sizeof(req), RTA_IP_PROTO, ipproto);
Packit Service 3880ab
		} else {
Packit Service 3880ab
			inet_prefix addr;
Packit Service 3880ab
Packit Service 3880ab
			if (strcmp(*argv, "to") == 0) {
Packit Service 3880ab
				NEXT_ARG();
Packit Service 3880ab
			}
Packit Service 3880ab
			if (matches(*argv, "help") == 0)
Packit Service 3880ab
				usage();
Packit Service 3880ab
			get_prefix(&addr, *argv, req.r.rtm_family);
Packit Service 3880ab
			if (req.r.rtm_family == AF_UNSPEC)
Packit Service 3880ab
				req.r.rtm_family = addr.family;
Packit Service 3880ab
			if (addr.bytelen)
Packit Service 3880ab
				addattr_l(&req.n, sizeof(req),
Packit Service 3880ab
					  RTA_DST, &addr.data, addr.bytelen);
Packit Service 6ea21a
			if (req.r.rtm_family == AF_INET && addr.bitlen != 32) {
Packit Service 6ea21a
				fprintf(stderr,
Packit Service 6ea21a
					"Warning: /%u as prefix is invalid, only /32 (or none) is supported.\n",
Packit Service 6ea21a
					addr.bitlen);
Packit Service 6ea21a
				req.r.rtm_dst_len = 32;
Packit Service 6ea21a
			} else if (req.r.rtm_family == AF_INET6 && addr.bitlen != 128) {
Packit Service 6ea21a
				fprintf(stderr,
Packit Service 6ea21a
					"Warning: /%u as prefix is invalid, only /128 (or none) is supported.\n",
Packit Service 6ea21a
					addr.bitlen);
Packit Service 6ea21a
				req.r.rtm_dst_len = 128;
Packit Service 6ea21a
			} else
Packit Service 6ea21a
				req.r.rtm_dst_len = addr.bitlen;
Packit Service 3880ab
			address_found = true;
Packit Service 3880ab
		}
Packit Service 3880ab
		argc--; argv++;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (!address_found) {
Packit Service 3880ab
		fprintf(stderr, "need at least a destination address\n");
Packit Service 3880ab
		return -1;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (idev || odev)  {
Packit Service 3880ab
		int idx;
Packit Service 3880ab
Packit Service 3880ab
		if (idev) {
Packit Service 3880ab
			idx = ll_name_to_index(idev);
Packit Service 3880ab
			if (!idx)
Packit Service 3880ab
				return nodev(idev);
Packit Service 3880ab
			addattr32(&req.n, sizeof(req), RTA_IIF, idx);
Packit Service 3880ab
		}
Packit Service 3880ab
		if (odev) {
Packit Service 3880ab
			idx = ll_name_to_index(odev);
Packit Service 3880ab
			if (!idx)
Packit Service 3880ab
				return nodev(odev);
Packit Service 3880ab
			addattr32(&req.n, sizeof(req), RTA_OIF, idx);
Packit Service 3880ab
		}
Packit Service 3880ab
	}
Packit Service 3880ab
	if (mark)
Packit Service 3880ab
		addattr32(&req.n, sizeof(req), RTA_MARK, mark);
Packit Service 3880ab
Packit Service 3880ab
	if (req.r.rtm_family == AF_UNSPEC)
Packit Service 3880ab
		req.r.rtm_family = AF_INET;
Packit Service 3880ab
Packit Service 3880ab
	/* Only IPv4 supports the RTM_F_LOOKUP_TABLE flag */
Packit Service 3880ab
	if (req.r.rtm_family == AF_INET)
Packit Service 3880ab
		req.r.rtm_flags |= RTM_F_LOOKUP_TABLE;
Packit Service 3880ab
	if (fib_match)
Packit Service 3880ab
		req.r.rtm_flags |= RTM_F_FIB_MATCH;
Packit Service 3880ab
Packit Service 3880ab
	if (rtnl_talk(&rth, &req.n, &answer) < 0)
Packit Service 3880ab
		return -2;
Packit Service 3880ab
Packit Service 3880ab
	new_json_obj(json);
Packit Service 3880ab
Packit Service 3880ab
	if (connected && !from_ok) {
Packit Service 3880ab
		struct rtmsg *r = NLMSG_DATA(answer);
Packit Service 3880ab
		int len = answer->nlmsg_len;
Packit Service 3880ab
		struct rtattr *tb[RTA_MAX+1];
Packit Service 3880ab
Packit Service 3880ab
		if (print_route(answer, (void *)stdout) < 0) {
Packit Service 3880ab
			fprintf(stderr, "An error :-)\n");
Packit Service 3880ab
			free(answer);
Packit Service 3880ab
			return -1;
Packit Service 3880ab
		}
Packit Service 3880ab
Packit Service 3880ab
		if (answer->nlmsg_type != RTM_NEWROUTE) {
Packit Service 3880ab
			fprintf(stderr, "Not a route?\n");
Packit Service 3880ab
			free(answer);
Packit Service 3880ab
			return -1;
Packit Service 3880ab
		}
Packit Service 3880ab
		len -= NLMSG_LENGTH(sizeof(*r));
Packit Service 3880ab
		if (len < 0) {
Packit Service 3880ab
			fprintf(stderr, "Wrong len %d\n", len);
Packit Service 3880ab
			free(answer);
Packit Service 3880ab
			return -1;
Packit Service 3880ab
		}
Packit Service 3880ab
Packit Service 3880ab
		parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
Packit Service 3880ab
Packit Service 3880ab
		if (tb[RTA_PREFSRC]) {
Packit Service 3880ab
			tb[RTA_PREFSRC]->rta_type = RTA_SRC;
Packit Service 3880ab
			r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
Packit Service 3880ab
		} else if (!tb[RTA_SRC]) {
Packit Service 3880ab
			fprintf(stderr, "Failed to connect the route\n");
Packit Service 3880ab
			free(answer);
Packit Service 3880ab
			return -1;
Packit Service 3880ab
		}
Packit Service 3880ab
		if (!odev && tb[RTA_OIF])
Packit Service 3880ab
			tb[RTA_OIF]->rta_type = 0;
Packit Service 3880ab
		if (tb[RTA_GATEWAY])
Packit Service 3880ab
			tb[RTA_GATEWAY]->rta_type = 0;
Packit Service 3880ab
		if (tb[RTA_VIA])
Packit Service 3880ab
			tb[RTA_VIA]->rta_type = 0;
Packit Service 3880ab
		if (!idev && tb[RTA_IIF])
Packit Service 3880ab
			tb[RTA_IIF]->rta_type = 0;
Packit Service 3880ab
		req.n.nlmsg_flags = NLM_F_REQUEST;
Packit Service 3880ab
		req.n.nlmsg_type = RTM_GETROUTE;
Packit Service 3880ab
Packit Service 3880ab
		delete_json_obj();
Packit Service 3880ab
		free(answer);
Packit Service 3880ab
		if (rtnl_talk(&rth, &req.n, &answer) < 0)
Packit Service 3880ab
			return -2;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	if (print_route(answer, (void *)stdout) < 0) {
Packit Service 3880ab
		fprintf(stderr, "An error :-)\n");
Packit Service 3880ab
		free(answer);
Packit Service 3880ab
		return -1;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	delete_json_obj();
Packit Service 3880ab
	free(answer);
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int rtattr_cmp(const struct rtattr *rta1, const struct rtattr *rta2)
Packit Service 3880ab
{
Packit Service 3880ab
	if (!rta1 || !rta2 || rta1->rta_len != rta2->rta_len)
Packit Service 3880ab
		return 1;
Packit Service 3880ab
Packit Service 3880ab
	return memcmp(RTA_DATA(rta1), RTA_DATA(rta2), RTA_PAYLOAD(rta1));
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int restore_handler(struct rtnl_ctrl_data *ctrl,
Packit Service 3880ab
			   struct nlmsghdr *n, void *arg)
Packit Service 3880ab
{
Packit Service 3880ab
	struct rtmsg *r = NLMSG_DATA(n);
Packit Service 3880ab
	struct rtattr *tb[RTA_MAX+1];
Packit Service 3880ab
	int len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
Packit Service 3880ab
	int ret, prio = *(int *)arg;
Packit Service 3880ab
Packit Service 3880ab
	parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
Packit Service 3880ab
Packit Service 3880ab
	/* Restore routes in correct order:
Packit Service 3880ab
	 * 0. ones for local addresses,
Packit Service 3880ab
	 * 1. ones for local networks,
Packit Service 3880ab
	 * 2. others (remote networks/hosts).
Packit Service 3880ab
	 */
Packit Service 3880ab
	if (!prio && !tb[RTA_GATEWAY] && (!tb[RTA_PREFSRC] ||
Packit Service 3880ab
	    !rtattr_cmp(tb[RTA_PREFSRC], tb[RTA_DST])))
Packit Service 3880ab
		goto restore;
Packit Service 3880ab
	else if (prio == 1 && !tb[RTA_GATEWAY] && tb[RTA_PREFSRC] &&
Packit Service 3880ab
		 rtattr_cmp(tb[RTA_PREFSRC], tb[RTA_DST]))
Packit Service 3880ab
		goto restore;
Packit Service 3880ab
	else if (prio == 2 && tb[RTA_GATEWAY])
Packit Service 3880ab
		goto restore;
Packit Service 3880ab
Packit Service 3880ab
	return 0;
Packit Service 3880ab
Packit Service 3880ab
restore:
Packit Service 3880ab
	n->nlmsg_flags |= NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK;
Packit Service 3880ab
Packit Service 3880ab
	ll_init_map(&rth);
Packit Service 3880ab
Packit Service 3880ab
	ret = rtnl_talk(&rth, n, NULL);
Packit Service 3880ab
	if ((ret < 0) && (errno == EEXIST))
Packit Service 3880ab
		ret = 0;
Packit Service 3880ab
Packit Service 3880ab
	return ret;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int route_dump_check_magic(void)
Packit Service 3880ab
{
Packit Service 3880ab
	int ret;
Packit Service 3880ab
	__u32 magic = 0;
Packit Service 3880ab
Packit Service 3880ab
	if (isatty(STDIN_FILENO)) {
Packit Service 3880ab
		fprintf(stderr, "Can't restore route dump from a terminal\n");
Packit Service 3880ab
		return -1;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	ret = fread(&magic, sizeof(magic), 1, stdin);
Packit Service 3880ab
	if (magic != route_dump_magic) {
Packit Service 3880ab
		fprintf(stderr, "Magic mismatch (%d elems, %x magic)\n", ret, magic);
Packit Service 3880ab
		return -1;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int iproute_restore(void)
Packit Service 3880ab
{
Packit Service 3880ab
	int pos, prio;
Packit Service 3880ab
Packit Service 3880ab
	if (route_dump_check_magic())
Packit Service 3880ab
		return -1;
Packit Service 3880ab
Packit Service 3880ab
	pos = ftell(stdin);
Packit Service 3880ab
	if (pos == -1) {
Packit Service 3880ab
		perror("Failed to restore: ftell");
Packit Service 3880ab
		return -1;
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	for (prio = 0; prio < 3; prio++) {
Packit Service 3880ab
		int err;
Packit Service 3880ab
Packit Service 3880ab
		err = rtnl_from_file(stdin, &restore_handler, &prio;;
Packit Service 3880ab
		if (err)
Packit Service 3880ab
			return -2;
Packit Service 3880ab
Packit Service 3880ab
		if (fseek(stdin, pos, SEEK_SET) == -1) {
Packit Service 3880ab
			perror("Failed to restore: fseek");
Packit Service 3880ab
			return -1;
Packit Service 3880ab
		}
Packit Service 3880ab
	}
Packit Service 3880ab
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int show_handler(struct rtnl_ctrl_data *ctrl,
Packit Service 3880ab
			struct nlmsghdr *n, void *arg)
Packit Service 3880ab
{
Packit Service 3880ab
	print_route(n, stdout);
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
static int iproute_showdump(void)
Packit Service 3880ab
{
Packit Service 3880ab
	if (route_dump_check_magic())
Packit Service 3880ab
		return -1;
Packit Service 3880ab
Packit Service 3880ab
	if (rtnl_from_file(stdin, &show_handler, NULL))
Packit Service 3880ab
		return -2;
Packit Service 3880ab
Packit Service 3880ab
	return 0;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
void iproute_reset_filter(int ifindex)
Packit Service 3880ab
{
Packit Service 3880ab
	memset(&filter, 0, sizeof(filter));
Packit Service 3880ab
	filter.mdst.bitlen = -1;
Packit Service 3880ab
	filter.msrc.bitlen = -1;
Packit Service 3880ab
	filter.oif = ifindex;
Packit Service 3880ab
	if (filter.oif > 0)
Packit Service 3880ab
		filter.oifmask = -1;
Packit Service 3880ab
}
Packit Service 3880ab
Packit Service 3880ab
int do_iproute(int argc, char **argv)
Packit Service 3880ab
{
Packit Service 3880ab
	if (argc < 1)
Packit Service 3880ab
		return iproute_list_flush_or_save(0, NULL, IPROUTE_LIST);
Packit Service 3880ab
Packit Service 3880ab
	if (matches(*argv, "add") == 0)
Packit Service 3880ab
		return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_EXCL,
Packit Service 3880ab
				      argc-1, argv+1);
Packit Service 3880ab
	if (matches(*argv, "change") == 0 || strcmp(*argv, "chg") == 0)
Packit Service 3880ab
		return iproute_modify(RTM_NEWROUTE, NLM_F_REPLACE,
Packit Service 3880ab
				      argc-1, argv+1);
Packit Service 3880ab
	if (matches(*argv, "replace") == 0)
Packit Service 3880ab
		return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_REPLACE,
Packit Service 3880ab
				      argc-1, argv+1);
Packit Service 3880ab
	if (matches(*argv, "prepend") == 0)
Packit Service 3880ab
		return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE,
Packit Service 3880ab
				      argc-1, argv+1);
Packit Service 3880ab
	if (matches(*argv, "append") == 0)
Packit Service 3880ab
		return iproute_modify(RTM_NEWROUTE, NLM_F_CREATE|NLM_F_APPEND,
Packit Service 3880ab
				      argc-1, argv+1);
Packit Service 3880ab
	if (matches(*argv, "test") == 0)
Packit Service 3880ab
		return iproute_modify(RTM_NEWROUTE, NLM_F_EXCL,
Packit Service 3880ab
				      argc-1, argv+1);
Packit Service 3880ab
	if (matches(*argv, "delete") == 0)
Packit Service 3880ab
		return iproute_modify(RTM_DELROUTE, 0,
Packit Service 3880ab
				      argc-1, argv+1);
Packit Service 3880ab
	if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0
Packit Service 3880ab
	    || matches(*argv, "lst") == 0)
Packit Service 3880ab
		return iproute_list_flush_or_save(argc-1, argv+1, IPROUTE_LIST);
Packit Service 3880ab
	if (matches(*argv, "get") == 0)
Packit Service 3880ab
		return iproute_get(argc-1, argv+1);
Packit Service 3880ab
	if (matches(*argv, "flush") == 0)
Packit Service 3880ab
		return iproute_list_flush_or_save(argc-1, argv+1, IPROUTE_FLUSH);
Packit Service 3880ab
	if (matches(*argv, "save") == 0)
Packit Service 3880ab
		return iproute_list_flush_or_save(argc-1, argv+1, IPROUTE_SAVE);
Packit Service 3880ab
	if (matches(*argv, "restore") == 0)
Packit Service 3880ab
		return iproute_restore();
Packit Service 3880ab
	if (matches(*argv, "showdump") == 0)
Packit Service 3880ab
		return iproute_showdump();
Packit Service 3880ab
	if (matches(*argv, "help") == 0)
Packit Service 3880ab
		usage();
Packit Service 3880ab
Packit Service 3880ab
	fprintf(stderr,
Packit Service 3880ab
		"Command \"%s\" is unknown, try \"ip route help\".\n", *argv);
Packit Service 3880ab
	exit(-1);
Packit Service 3880ab
}