Exemplo n.º 1
0
void
format_iproute(ip_route_t *route, char *buf, size_t buf_len)
{
	char *op = buf;
	char *buf_end = buf + buf_len;
	nexthop_t *nh;
	element e;

	if (route->type != RTN_UNICAST)
		op += snprintf(op, buf_end - op, " %s", get_rttables_rtntype(route->type));
	if (route->dst) {
		op += snprintf(op, buf_end - op, " %s", ipaddresstos(NULL, route->dst));
		if ((route->dst->ifa.ifa_family == AF_INET && route->dst->ifa.ifa_prefixlen != 32 ) ||
		    (route->dst->ifa.ifa_family == AF_INET6 && route->dst->ifa.ifa_prefixlen != 128 ))
			op += snprintf(op, buf_end - op, "/%u", route->dst->ifa.ifa_prefixlen);
	}
	else
		op += snprintf(op, buf_end - op, " %s", "default");

	if (route->src) {
		op += snprintf(op, buf_end - op, " from %s", ipaddresstos(NULL, route->src));
		if ((route->src->ifa.ifa_family == AF_INET && route->src->ifa.ifa_prefixlen != 32 ) ||
		    (route->src->ifa.ifa_family == AF_INET6 && route->src->ifa.ifa_prefixlen != 128 ))
			op += snprintf(op, buf_end - op, "/%u", route->src->ifa.ifa_prefixlen);
	}

//#ifdef _HAVE_RTA_NEWDST_
//	/* MPLS only */
//	if (route->as_to)
//		op += snprintf(op, buf_end - op, " as to %s", ipaddresstos(NULL, route->as_to));
//#endif

	if (route->pref_src)
		op += snprintf(op, buf_end - op, " src %s", ipaddresstos(NULL, route->pref_src));

	if (route->mask & IPROUTE_BIT_DSFIELD)
		op += snprintf(op, buf_end - op, " tos %u", route->tos);

#ifdef _HAVE_RTA_ENCAP_
	if (route->encap.type != LWTUNNEL_ENCAP_NONE)
		op += print_encap(op, buf_end - op, &route->encap);
#endif

	if (route->via)
		op += snprintf(op, buf_end - op, " via %s %s", route->via->ifa.ifa_family == AF_INET6 ? "inet6" : "inet", ipaddresstos(NULL, route->via));

	if (route->oif)
		op += snprintf(op, buf_end - op, " dev %s", route->oif->ifname);

	if (route->table != RT_TABLE_MAIN)
		op += snprintf(op, buf_end - op, " table %u", route->table);

	if (route->mask & IPROUTE_BIT_PROTOCOL)
		op += snprintf(op, buf_end - op, " proto %u", route->protocol);

	if (route->mask & IPROUTE_BIT_SCOPE)
		op += snprintf(op, buf_end - op, " scope %u", route->scope);

	if (route->mask & IPROUTE_BIT_METRIC)
		op += snprintf(op, buf_end - op, " metric %u", route->metric);

	if (route->family == AF_INET && route->flags & RTNH_F_ONLINK)
		op += snprintf(op, buf_end - op, " %s", "onlink");

	if (route->realms) {
		if (route->realms & 0xFFFF0000)
			op += snprintf(op, buf_end - op, " realms %d/", route->realms >> 16);
		else
			op += snprintf(op, buf_end - op, " realm ");
		op += snprintf(op, buf_end - op, "%d", route->realms & 0xFFFF);
	}
Exemplo n.º 2
0
void
format_iprule(ip_rule_t *rule, char *buf, size_t buf_len)
{
	char *op = buf;
	char *buf_end = buf + buf_len;

	if (!rule->to_addr && !rule->from_addr && rule->family == AF_INET6)
		op += snprintf(op, (size_t)(buf_end - op), "inet6 ");

	if (rule->invert)
		op += snprintf(op, (size_t)(buf_end - op), "not ");

	if (rule->from_addr)
		op += snprintf(op, (size_t)(buf_end - op), "from %s", ipaddresstos(NULL, rule->from_addr));
	else
		op += snprintf(op, (size_t)(buf_end - op), "from all" );

	if (rule->to_addr)
		op += snprintf(op, (size_t)(buf_end - op), " to %s", ipaddresstos(NULL, rule->to_addr));

	if (rule->mask & IPRULE_BIT_PRIORITY)
		op += snprintf(op, (size_t)(buf_end - op), " priority %u", rule->priority);

	op += snprintf(op, (size_t)(buf_end - op), " tos 0x%x", rule->tos);

	if (rule->mask & (IPRULE_BIT_FWMARK | IPRULE_BIT_FWMASK)) {
		op += snprintf(op, (size_t)(buf_end - op), " fwmark 0x%x", rule->fwmark);

		if (rule->mask & IPRULE_BIT_FWMASK && rule->fwmask != 0xffffffff)
			op += snprintf(op, (size_t)(buf_end - op), "/0x%x", rule->fwmask);
	}

	if (rule->iif)
#if HAVE_DECL_FRA_OIFNAME
		op += snprintf(op, (size_t)(buf_end - op), " iif %s", rule->iif->ifname);
#else
		op += snprintf(op, (size_t)(buf_end - op), " dev %s", rule->iif->ifname);
#endif

#if HAVE_DECL_FRA_OIFNAME
	if (rule->oif)
		op += snprintf(op, (size_t)(buf_end - op), " oif %s", rule->oif->ifname);
#endif

#if HAVE_DECL_FRA_SUPPRESS_PREFIXLEN
	if (rule->suppress_prefix_len != -1)
		op += snprintf(op, (size_t)(buf_end - op), " suppress_prefixlen %u", rule->suppress_prefix_len);
#endif

#if HAVE_DECL_FRA_SUPPRESS_IFGROUP
	if (rule->mask & IPRULE_BIT_SUP_GROUP)
		op += snprintf(op, (size_t)(buf_end - op), " suppress_ifgroup %d", rule->suppress_group);
#endif

#if HAVE_DECL_FRA_TUN_ID
	if (rule->tunnel_id)
		op += snprintf(op, (size_t)(buf_end - op), " tunnel-id %" PRIu64, rule->tunnel_id);
#endif

#if HAVE_DECL_FRA_UID_RANGE
	if (rule->mask & IPRULE_BIT_UID_RANGE)
		op += snprintf(op, (size_t)(buf_end - op), " uidrange %" PRIu32 "-%" PRIu32, rule->uid_range.start, rule->uid_range.end);
#endif

#if HAVE_DECL_FRA_L3MDEV
	if (rule->l3mdev)
		op += snprintf(op, (size_t)(buf_end - op), " l3mdev");
#endif

#if HAVE_DECL_FRA_PROTOCOL
	if (rule->mask & IPRULE_BIT_PROTOCOL)
		op += snprintf(op, (size_t)(buf_end - op), " protocol %u", rule->protocol);
#endif

#if HAVE_DECL_FRA_IP_PROTO
	if (rule->mask & IPRULE_BIT_IP_PROTO)
		op += snprintf(op, (size_t)(buf_end - op), " ipproto %u", rule->ip_proto);
#endif

#if HAVE_DECL_FRA_SPORT_RANGE
	if (rule->mask & IPRULE_BIT_SPORT_RANGE)
		op += snprintf(op, (size_t)(buf_end - op), " sport %hu-%hu", rule->src_port.start, rule->src_port.end);
#endif

#if HAVE_DECL_FRA_DPORT_RANGE
	if (rule->mask & IPRULE_BIT_DPORT_RANGE)
		op += snprintf(op, (size_t)(buf_end - op), " dport %hu-%hu", rule->dst_port.start, rule->dst_port.end);
#endif

	if (rule->realms)
		op += snprintf(op, (size_t)(buf_end - op), " realms %d/%d", rule->realms >> 16, rule->realms & 0xffff);

	if (rule->action == FR_ACT_TO_TBL)
		op += snprintf(op, (size_t)(buf_end - op), " lookup %u", rule->table);
	else if (rule->action == FR_ACT_GOTO)
		op += snprintf(op, (size_t)(buf_end - op), " goto %u", rule->goto_target);
	else if (rule->action == FR_ACT_NOP)
		op += snprintf(op, (size_t)(buf_end - op), " nop");
	else
		op += snprintf(op, (size_t)(buf_end - op), " type %s", get_rttables_rtntype(rule->action));
	if (rule->dont_track)
		op += snprintf(op, (size_t)(buf_end - op), " no_track");
	if (rule->track_group)
		op += snprintf(op, (size_t)(buf_end - op), " track_group %s", rule->track_group->gname);
}