Ejemplo n.º 1
0
Archivo: utils.c Proyecto: dtaht/tc-adv
unsigned int print_name_and_link(const char *fmt,
				 const char *name, struct rtattr *tb[])
{
	const char *link = NULL;
	unsigned int m_flag = 0;
	SPRINT_BUF(b1);

	if (tb[IFLA_LINK]) {
		int iflink = rta_getattr_u32(tb[IFLA_LINK]);

		if (iflink) {
			if (tb[IFLA_LINK_NETNSID]) {
				if (is_json_context()) {
					print_int(PRINT_JSON,
						  "link_index", NULL, iflink);
				} else {
					link = ll_idx_n2a(iflink);
				}
			} else {
				link = ll_index_to_name(iflink);

				if (is_json_context()) {
					print_string(PRINT_JSON,
						     "link", NULL, link);
					link = NULL;
				}

				m_flag = ll_index_to_flags(iflink);
				m_flag = !(m_flag & IFF_UP);
			}
		} else {
			if (is_json_context())
				print_null(PRINT_JSON, "link", NULL, NULL);
			else
				link = "NONE";
		}

		if (link) {
			snprintf(b1, sizeof(b1), "%s@%s", name, link);
			name = b1;
		}
	}

	print_color_string(PRINT_ANY, COLOR_IFNAME, "ifname", fmt, name);

	return m_flag;
}
Ejemplo n.º 2
0
int print_neigh(struct nlmsghdr *n, void *arg)
{
	FILE *fp = (FILE *)arg;
	struct ndmsg *r = NLMSG_DATA(n);
	int len = n->nlmsg_len;
	struct rtattr *tb[NDA_MAX+1];
	static int logit = 1;
	__u8 protocol = 0;

	if (n->nlmsg_type != RTM_NEWNEIGH && n->nlmsg_type != RTM_DELNEIGH &&
	    n->nlmsg_type != RTM_GETNEIGH) {
		fprintf(stderr, "Not RTM_NEWNEIGH: %08x %08x %08x\n",
			n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);

		return 0;
	}
	len -= NLMSG_LENGTH(sizeof(*r));
	if (len < 0) {
		fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
		return -1;
	}

	if (filter.flushb && n->nlmsg_type != RTM_NEWNEIGH)
		return 0;

	if (filter.family && filter.family != r->ndm_family)
		return 0;
	if (filter.index && filter.index != r->ndm_ifindex)
		return 0;
	if (!(filter.state&r->ndm_state) &&
	    !(r->ndm_flags & NTF_PROXY) &&
	    !(r->ndm_flags & NTF_EXT_LEARNED) &&
	    (r->ndm_state || !(filter.state&0x100)) &&
	    (r->ndm_family != AF_DECnet))
		return 0;

	if (filter.master && !(n->nlmsg_flags & NLM_F_DUMP_FILTERED)) {
		if (logit) {
			logit = 0;
			fprintf(fp,
				"\nWARNING: Kernel does not support filtering by master device\n\n");
		}
	}

	parse_rtattr(tb, NDA_MAX, NDA_RTA(r), n->nlmsg_len - NLMSG_LENGTH(sizeof(*r)));

	if (inet_addr_match_rta(&filter.pfx, tb[NDA_DST]))
		return 0;

	if (tb[NDA_PROTOCOL])
		protocol = rta_getattr_u8(tb[NDA_PROTOCOL]);

	if (filter.protocol && filter.protocol != protocol)
		return 0;

	if (filter.unused_only && tb[NDA_CACHEINFO]) {
		struct nda_cacheinfo *ci = RTA_DATA(tb[NDA_CACHEINFO]);

		if (ci->ndm_refcnt)
			return 0;
	}

	if (filter.flushb) {
		struct nlmsghdr *fn;

		if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
			if (flush_update())
				return -1;
		}
		fn = (struct nlmsghdr *)(filter.flushb + NLMSG_ALIGN(filter.flushp));
		memcpy(fn, n, n->nlmsg_len);
		fn->nlmsg_type = RTM_DELNEIGH;
		fn->nlmsg_flags = NLM_F_REQUEST;
		fn->nlmsg_seq = ++rth.seq;
		filter.flushp = (((char *)fn) + n->nlmsg_len) - filter.flushb;
		filter.flushed++;
		if (show_stats < 2)
			return 0;
	}

	open_json_object(NULL);
	if (n->nlmsg_type == RTM_DELNEIGH)
		print_bool(PRINT_ANY, "deleted", "Deleted ", true);
	else if (n->nlmsg_type == RTM_GETNEIGH)
		print_null(PRINT_ANY, "miss", "%s ", "miss");

	if (tb[NDA_DST]) {
		const char *dst;
		int family = r->ndm_family;

		if (family == AF_BRIDGE) {
			if (RTA_PAYLOAD(tb[NDA_DST]) == sizeof(struct in6_addr))
				family = AF_INET6;
			else
				family = AF_INET;
		}

		dst = format_host_rta(family, tb[NDA_DST]);
		print_color_string(PRINT_ANY,
				   ifa_family_color(family),
				   "dst", "%s ", dst);
	}

	if (!filter.index && r->ndm_ifindex) {
		if (!is_json_context())
			fprintf(fp, "dev ");

		print_color_string(PRINT_ANY, COLOR_IFNAME,
				   "dev", "%s ",
				   ll_index_to_name(r->ndm_ifindex));
	}

	if (tb[NDA_LLADDR]) {
		const char *lladdr;
		SPRINT_BUF(b1);

		lladdr = ll_addr_n2a(RTA_DATA(tb[NDA_LLADDR]),
				     RTA_PAYLOAD(tb[NDA_LLADDR]),
				     ll_index_to_type(r->ndm_ifindex),
				     b1, sizeof(b1));

		if (!is_json_context())
			fprintf(fp, "lladdr ");

		print_color_string(PRINT_ANY, COLOR_MAC,
				   "lladdr", "%s", lladdr);
	}

	if (r->ndm_flags & NTF_ROUTER)
		print_null(PRINT_ANY, "router", " %s", "router");

	if (r->ndm_flags & NTF_PROXY)
		print_null(PRINT_ANY, "proxy", " %s", "proxy");

	if (r->ndm_flags & NTF_EXT_LEARNED)
		print_null(PRINT_ANY, "extern_learn", " %s ", "extern_learn");

	if (show_stats) {
		if (tb[NDA_CACHEINFO])
			print_cacheinfo(RTA_DATA(tb[NDA_CACHEINFO]));

		if (tb[NDA_PROBES])
			print_uint(PRINT_ANY, "probes", " probes %u",
				   rta_getattr_u32(tb[NDA_PROBES]));
	}

	if (r->ndm_state)
		print_neigh_state(r->ndm_state);

	if (protocol) {
		SPRINT_BUF(b1);

		print_string(PRINT_ANY, "protocol", " proto %s ",
			     rtnl_rtprot_n2a(protocol, b1, sizeof(b1)));
	}

	print_string(PRINT_FP, NULL, "\n", "");
	close_json_object();
	fflush(stdout);

	return 0;
}
Ejemplo n.º 3
0
static int process(const struct sockaddr_nl *who, struct nlmsghdr *n,
		   void *arg)
{
	struct genlmsghdr *ghdr;
	struct rtattr *attrs[MACSEC_ATTR_MAX + 1];
	struct rtattr *attrs_secy[MACSEC_SECY_ATTR_MAX + 1];
	int len = n->nlmsg_len;
	int ifindex;
	__u64 sci;
	__u8 encoding_sa;

	if (n->nlmsg_type != genl_family)
		return -1;

	len -= NLMSG_LENGTH(GENL_HDRLEN);
	if (len < 0)
		return -1;

	ghdr = NLMSG_DATA(n);
	if (ghdr->cmd != MACSEC_CMD_GET_TXSC)
		return 0;

	parse_rtattr(attrs, MACSEC_ATTR_MAX, (void *) ghdr + GENL_HDRLEN, len);
	if (!validate_dump(attrs)) {
		fprintf(stderr, "incomplete dump message\n");
		return -1;
	}

	ifindex = rta_getattr_u32(attrs[MACSEC_ATTR_IFINDEX]);
	parse_rtattr_nested(attrs_secy, MACSEC_SECY_ATTR_MAX + 1,
			    attrs[MACSEC_ATTR_SECY]);

	if (!validate_secy_dump(attrs_secy)) {
		fprintf(stderr, "incomplete dump message\n");
		return -1;
	}

	sci = rta_getattr_u64(attrs_secy[MACSEC_SECY_ATTR_SCI]);
	encoding_sa = rta_getattr_u8(attrs_secy[MACSEC_SECY_ATTR_ENCODING_SA]);

	if (filter.ifindex && ifindex != filter.ifindex)
		return 0;

	if (filter.sci && sci != filter.sci)
		return 0;

	open_json_object(NULL);
	print_uint(PRINT_ANY, "ifindex", "%u: ", ifindex);
	print_color_string(PRINT_ANY, COLOR_IFNAME, "ifname",
			   "%s: ", ll_index_to_name(ifindex));

	print_attrs(attrs_secy);

	print_tx_sc("    ", sci, encoding_sa,
		    attrs[MACSEC_ATTR_TXSC_STATS],
		    attrs[MACSEC_ATTR_SECY_STATS],
		    attrs[MACSEC_ATTR_TXSA_LIST]);

	if (attrs[MACSEC_ATTR_RXSC_LIST])
		print_rxsc_list(attrs[MACSEC_ATTR_RXSC_LIST]);

	close_json_object();

	return 0;
}
Ejemplo n.º 4
0
static void print_ndtparams(struct rtattr *tpb[])
{

	if (tpb[NDTPA_IFINDEX]) {
		__u32 ifindex = rta_getattr_u32(tpb[NDTPA_IFINDEX]);

		print_string(PRINT_FP, NULL, "    dev ", NULL);
		print_color_string(PRINT_ANY, COLOR_IFNAME,
				   "dev", "%s ", ll_index_to_name(ifindex));
		print_nl();
	}

	print_string(PRINT_FP, NULL, "    ", NULL);
	if (tpb[NDTPA_REFCNT]) {
		__u32 refcnt = rta_getattr_u32(tpb[NDTPA_REFCNT]);

		print_uint(PRINT_ANY, "refcnt", "refcnt %u ", refcnt);
	}

	if (tpb[NDTPA_REACHABLE_TIME]) {
		__u64 reachable = rta_getattr_u64(tpb[NDTPA_REACHABLE_TIME]);

		print_u64(PRINT_ANY, "reachable",
			   "reachable %llu ", reachable);
	}

	if (tpb[NDTPA_BASE_REACHABLE_TIME]) {
		__u64 breachable
			= rta_getattr_u64(tpb[NDTPA_BASE_REACHABLE_TIME]);

		print_u64(PRINT_ANY, "base_reachable",
			   "base_reachable %llu ", breachable);
	}

	if (tpb[NDTPA_RETRANS_TIME]) {
		__u64 retrans = rta_getattr_u64(tpb[NDTPA_RETRANS_TIME]);

		print_u64(PRINT_ANY, "retrans", "retrans %llu ", retrans);
	}

	print_string(PRINT_FP, NULL, "%s    ", _SL_);

	if (tpb[NDTPA_GC_STALETIME]) {
		__u64 gc_stale = rta_getattr_u64(tpb[NDTPA_GC_STALETIME]);

		print_u64(PRINT_ANY, "gc_stale", "gc_stale %llu ", gc_stale);
	}

	if (tpb[NDTPA_DELAY_PROBE_TIME]) {
		__u64 delay_probe
			= rta_getattr_u64(tpb[NDTPA_DELAY_PROBE_TIME]);

		print_u64(PRINT_ANY, "delay_probe",
			   "delay_probe %llu ", delay_probe);
	}

	if (tpb[NDTPA_QUEUE_LEN]) {
		__u32 queue = rta_getattr_u32(tpb[NDTPA_QUEUE_LEN]);

		print_uint(PRINT_ANY, "queue", "queue %u ", queue);
	}

	print_string(PRINT_FP, NULL, "%s    ", _SL_);

	if (tpb[NDTPA_APP_PROBES]) {
		__u32 aprobe = rta_getattr_u32(tpb[NDTPA_APP_PROBES]);

		print_uint(PRINT_ANY, "app_probes", "app_probes %u ", aprobe);
	}

	if (tpb[NDTPA_UCAST_PROBES]) {
		__u32 uprobe = rta_getattr_u32(tpb[NDTPA_UCAST_PROBES]);

		print_uint(PRINT_ANY, "ucast_probes",
			   "ucast_probes %u ", uprobe);
	}

	if (tpb[NDTPA_MCAST_PROBES]) {
		__u32 mprobe = rta_getattr_u32(tpb[NDTPA_MCAST_PROBES]);

		print_uint(PRINT_ANY, "mcast_probes",
			   "mcast_probes %u ", mprobe);
	}

	print_string(PRINT_FP, NULL, "%s    ", _SL_);

	if (tpb[NDTPA_ANYCAST_DELAY]) {
		__u64 anycast_delay = rta_getattr_u64(tpb[NDTPA_ANYCAST_DELAY]);

		print_u64(PRINT_ANY, "anycast_delay",
			   "anycast_delay %llu ", anycast_delay);
	}

	if (tpb[NDTPA_PROXY_DELAY]) {
		__u64 proxy_delay = rta_getattr_u64(tpb[NDTPA_PROXY_DELAY]);

		print_u64(PRINT_ANY, "proxy_delay",
			   "proxy_delay %llu ", proxy_delay);
	}

	if (tpb[NDTPA_PROXY_QLEN]) {
		__u32 pqueue = rta_getattr_u32(tpb[NDTPA_PROXY_QLEN]);

		print_uint(PRINT_ANY, "proxy_queue", "proxy_queue %u ", pqueue);
	}

	if (tpb[NDTPA_LOCKTIME]) {
		__u64 locktime = rta_getattr_u64(tpb[NDTPA_LOCKTIME]);

		print_u64(PRINT_ANY, "locktime", "locktime %llu ", locktime);
	}

	print_nl();
}