summaryrefslogtreecommitdiffstats
path: root/ip/ipmonitor.c
blob: 9b055264ca7bf4e821074cc630f14c78597ef0e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * ipmonitor.c		"ip monitor".
 *
 * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <time.h>

#include "utils.h"
#include "ip_common.h"
#include "nh_common.h"

static void usage(void) __attribute__((noreturn));
static int prefix_banner;
int listen_all_nsid;

static void usage(void)
{
	fprintf(stderr,
		"Usage: ip monitor [ all | OBJECTS ] [ FILE ] [ label ] [ all-nsid ]\n"
		"                  [ dev DEVICE ]\n"
		"OBJECTS :=  address | link | mroute | neigh | netconf |\n"
		"            nexthop | nsid | prefix | route | rule | stats\n"
		"FILE := file FILENAME\n");
	exit(-1);
}

static void print_headers(FILE *fp, char *label, struct rtnl_ctrl_data *ctrl)
{
	if (timestamp)
		print_timestamp(fp);

	if (listen_all_nsid) {
		if (ctrl == NULL || ctrl->nsid < 0)
			fprintf(fp, "[nsid current]");
		else
			fprintf(fp, "[nsid %d]", ctrl->nsid);
	}

	if (prefix_banner)
		fprintf(fp, "%s", label);
}

static int accept_msg(struct rtnl_ctrl_data *ctrl,
		      struct nlmsghdr *n, void *arg)
{
	FILE *fp = (FILE *)arg;

	switch (n->nlmsg_type) {
	case RTM_NEWROUTE:
	case RTM_DELROUTE: {
		struct rtmsg *r = NLMSG_DATA(n);
		int len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*r));

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

		if (r->rtm_flags & RTM_F_CLONED)
			return 0;

		if (r->rtm_family == RTNL_FAMILY_IPMR ||
		    r->rtm_family == RTNL_FAMILY_IP6MR) {
			print_headers(fp, "[MROUTE]", ctrl);
			print_mroute(n, arg);
			return 0;
		} else {
			print_headers(fp, "[ROUTE]", ctrl);
			print_route(n, arg);
			return 0;
		}
	}

	case RTM_NEWNEXTHOP:
	case RTM_DELNEXTHOP:
		print_headers(fp, "[NEXTHOP]", ctrl);
		print_cache_nexthop(n, arg, true);
		return 0;

	case RTM_NEWNEXTHOPBUCKET:
	case RTM_DELNEXTHOPBUCKET:
		print_headers(fp, "[NEXTHOPBUCKET]", ctrl);
		print_nexthop_bucket(n, arg);
		return 0;

	case RTM_NEWLINK:
	case RTM_DELLINK:
		ll_remember_index(n, NULL);
		print_headers(fp, "[LINK]", ctrl);
		print_linkinfo(n, arg);
		return 0;

	case RTM_NEWADDR:
	case RTM_DELADDR:
		print_headers(fp, "[ADDR]", ctrl);
		print_addrinfo(n, arg);
		return 0;

	case RTM_NEWADDRLABEL:
	case RTM_DELADDRLABEL:
		print_headers(fp, "[ADDRLABEL]", ctrl);
		print_addrlabel(n, arg);
		return 0;

	case RTM_NEWNEIGH:
	case RTM_DELNEIGH:
	case RTM_GETNEIGH:
		if (preferred_family) {
			struct ndmsg *r = NLMSG_DATA(n);

			if (r->ndm_family != preferred_family)
				return 0;
		}

		print_headers(fp, "[NEIGH]", ctrl);
		print_neigh(n, arg);
		return 0;

	case RTM_NEWPREFIX:
		print_headers(fp, "[PREFIX]", ctrl);
		print_prefix(n, arg);
		return 0;

	case RTM_NEWRULE:
	case RTM_DELRULE:
		print_headers(fp, "[RULE]", ctrl);
		print_rule(n, arg);
		return 0;

	case NLMSG_TSTAMP:
		print_nlmsg_timestamp(fp, n);
		return 0;

	case RTM_NEWNETCONF:
	case RTM_DELNETCONF:
		print_headers(fp, "[NETCONF]", ctrl);
		print_netconf(ctrl, n, arg);
		return 0;

	case RTM_DELNSID:
	case RTM_NEWNSID:
		print_headers(fp, "[NSID]", ctrl);
		print_nsid(n, arg);
		return 0;

	case RTM_NEWSTATS:
		print_headers(fp, "[STATS]", ctrl);
		ipstats_print(n, arg);
		return 0;

	case NLMSG_ERROR:
	case NLMSG_NOOP:
	case NLMSG_DONE:
		break;	/* ignore */

	default:
		fprintf(stderr,
			"Unknown message: type=0x%08x(%d) flags=0x%08x(%d) len=0x%08x(%d)\n",
			n->nlmsg_type, n->nlmsg_type,
			n->nlmsg_flags, n->nlmsg_flags, n->nlmsg_len,
			n->nlmsg_len);
	}
	return 0;
}

#define IPMON_LLINK		BIT(0)
#define IPMON_LADDR		BIT(1)
#define IPMON_LROUTE		BIT(2)
#define IPMON_LMROUTE		BIT(3)
#define IPMON_LPREFIX		BIT(4)
#define IPMON_LNEIGH		BIT(5)
#define IPMON_LNETCONF		BIT(6)
#define IPMON_LSTATS		BIT(7)
#define IPMON_LRULE		BIT(8)
#define IPMON_LNSID		BIT(9)
#define IPMON_LNEXTHOP		BIT(10)

#define IPMON_L_ALL		(~0)

int do_ipmonitor(int argc, char **argv)
{
	unsigned int groups = 0, lmask = 0;
	/* "needed" mask, failure to enable is an error */
	unsigned int nmask;
	char *file = NULL;
	int ifindex = 0;

	rtnl_close(&rth);

	while (argc > 0) {
		if (matches(*argv, "file") == 0) {
			NEXT_ARG();
			file = *argv;
		} else if (matches(*argv, "label") == 0) {
			prefix_banner = 1;
		} else if (matches(*argv, "link") == 0) {
			lmask |= IPMON_LLINK;
		} else if (matches(*argv, "address") == 0) {
			lmask |= IPMON_LADDR;
		} else if (matches(*argv, "route") == 0) {
			lmask |= IPMON_LROUTE;
		} else if (matches(*argv, "mroute") == 0) {
			lmask |= IPMON_LMROUTE;
		} else if (matches(*argv, "prefix") == 0) {
			lmask |= IPMON_LPREFIX;
		} else if (matches(*argv, "neigh") == 0) {
			lmask |= IPMON_LNEIGH;
		} else if (matches(*argv, "netconf") == 0) {
			lmask |= IPMON_LNETCONF;
		} else if (matches(*argv, "rule") == 0) {
			lmask |= IPMON_LRULE;
		} else if (matches(*argv, "nsid") == 0) {
			lmask |= IPMON_LNSID;
		} else if (matches(*argv, "nexthop") == 0) {
			lmask |= IPMON_LNEXTHOP;
		} else if (strcmp(*argv, "stats") == 0) {
			lmask |= IPMON_LSTATS;
		} else if (strcmp(*argv, "all") == 0) {
			prefix_banner = 1;
		} else if (matches(*argv, "all-nsid") == 0) {
			listen_all_nsid = 1;
		} else if (matches(*argv, "help") == 0) {
			usage();
		} else if (strcmp(*argv, "dev") == 0) {
			NEXT_ARG();

			ifindex = ll_name_to_index(*argv);
			if (!ifindex)
				invarg("Device does not exist\n", *argv);
		} else {
			fprintf(stderr, "Argument \"%s\" is unknown, try \"ip monitor help\".\n", *argv);
			exit(-1);
		}
		argc--;	argv++;
	}

	ipaddr_reset_filter(1, ifindex);
	iproute_reset_filter(ifindex);
	ipmroute_reset_filter(ifindex);
	ipneigh_reset_filter(ifindex);
	ipnetconf_reset_filter(ifindex);

	nmask = lmask;
	if (!lmask)
		lmask = IPMON_L_ALL;

	if (lmask & IPMON_LLINK)
		groups |= nl_mgrp(RTNLGRP_LINK);
	if (lmask & IPMON_LADDR) {
		if (!preferred_family || preferred_family == AF_INET)
			groups |= nl_mgrp(RTNLGRP_IPV4_IFADDR);
		if (!preferred_family || preferred_family == AF_INET6)
			groups |= nl_mgrp(RTNLGRP_IPV6_IFADDR);
	}
	if (lmask & IPMON_LROUTE) {
		if (!preferred_family || preferred_family == AF_INET)
			groups |= nl_mgrp(RTNLGRP_IPV4_ROUTE);
		if (!preferred_family || preferred_family == AF_INET6)
			groups |= nl_mgrp(RTNLGRP_IPV6_ROUTE);
		if (!preferred_family || preferred_family == AF_MPLS)
			groups |= nl_mgrp(RTNLGRP_MPLS_ROUTE);
	}
	if (lmask & IPMON_LMROUTE) {
		if (!preferred_family || preferred_family == AF_INET)
			groups |= nl_mgrp(RTNLGRP_IPV4_MROUTE);
		if (!preferred_family || preferred_family == AF_INET6)
			groups |= nl_mgrp(RTNLGRP_IPV6_MROUTE);
	}
	if (lmask & IPMON_LPREFIX) {
		if (!preferred_family || preferred_family == AF_INET6)
			groups |= nl_mgrp(RTNLGRP_IPV6_PREFIX);
	}
	if (lmask & IPMON_LNEIGH) {
		groups |= nl_mgrp(RTNLGRP_NEIGH);
	}
	if (lmask & IPMON_LNETCONF) {
		if (!preferred_family || preferred_family == AF_INET)
			groups |= nl_mgrp(RTNLGRP_IPV4_NETCONF);
		if (!preferred_family || preferred_family == AF_INET6)
			groups |= nl_mgrp(RTNLGRP_IPV6_NETCONF);
		if (!preferred_family || preferred_family == AF_MPLS)
			groups |= nl_mgrp(RTNLGRP_MPLS_NETCONF);
	}
	if (lmask & IPMON_LRULE) {
		if (!preferred_family || preferred_family == AF_INET)
			groups |= nl_mgrp(RTNLGRP_IPV4_RULE);
		if (!preferred_family || preferred_family == AF_INET6)
			groups |= nl_mgrp(RTNLGRP_IPV6_RULE);
	}
	if (lmask & IPMON_LNSID) {
		groups |= nl_mgrp(RTNLGRP_NSID);
	}

	if (file) {
		FILE *fp;
		int err;

		fp = fopen(file, "r");
		if (fp == NULL) {
			perror("Cannot fopen");
			exit(-1);
		}
		err = rtnl_from_file(fp, accept_msg, stdout);
		fclose(fp);
		return err;
	}

	if (rtnl_open(&rth, groups) < 0)
		exit(1);

	if (lmask & IPMON_LNEXTHOP &&
	    rtnl_add_nl_group(&rth, RTNLGRP_NEXTHOP) < 0) {
		fprintf(stderr, "Failed to add nexthop group to list\n");
		exit(1);
	}

	if (lmask & IPMON_LSTATS &&
	    rtnl_add_nl_group(&rth, RTNLGRP_STATS) < 0 &&
	    nmask & IPMON_LSTATS) {
		fprintf(stderr, "Failed to add stats group to list\n");
		exit(1);
	}

	if (listen_all_nsid && rtnl_listen_all_nsid(&rth) < 0)
		exit(1);

	ll_init_map(&rth);
	netns_nsid_socket_init();
	netns_map_init();

	if (rtnl_listen(&rth, accept_msg, stdout) < 0)
		exit(2);

	return 0;
}