summaryrefslogtreecommitdiffstats
path: root/zebra/router-id.c
blob: ef87d924fe54eb58ae7f971516ff77f1a65630f2 (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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Router ID for zebra daemon.
 *
 * Copyright (C) 2004 James R. Leu
 *
 * This file is part of Quagga routing suite.
 */

#include <zebra.h>

#include "if.h"
#include "vty.h"
#include "sockunion.h"
#include "prefix.h"
#include "stream.h"
#include "command.h"
#include "memory.h"
#include "ioctl.h"
#include "connected.h"
#include "network.h"
#include "log.h"
#include "table.h"
#include "rib.h"
#include "vrf.h"

#include "zebra/zebra_router.h"
#include "zebra/zapi_msg.h"
#include "zebra/zebra_vrf.h"
#include "zebra/router-id.h"
#include "zebra/redistribute.h"

static struct connected *router_id_find_node(struct list *l,
					     struct connected *ifc)
{
	struct listnode *node;
	struct connected *c;

	for (ALL_LIST_ELEMENTS_RO(l, node, c))
		if (prefix_same(ifc->address, c->address))
			return c;

	return NULL;
}

static int router_id_bad_address(struct connected *ifc)
{
	/* non-redistributable addresses shouldn't be used for RIDs either */
	if (!zebra_check_addr(ifc->address))
		return 1;

	return 0;
}

static bool router_id_v6_is_any(struct prefix *p)
{
	return memcmp(&p->u.prefix6, &in6addr_any, sizeof(struct in6_addr))
	       == 0;
}

int router_id_get(afi_t afi, struct prefix *p, struct zebra_vrf *zvrf)
{
	struct listnode *node;
	struct connected *c;
	struct in6_addr *addr = NULL;

	switch (afi) {
	case AFI_IP:
		p->u.prefix4.s_addr = INADDR_ANY;
		p->family = AF_INET;
		p->prefixlen = IPV4_MAX_BITLEN;
		if (zvrf->rid_user_assigned.u.prefix4.s_addr != INADDR_ANY)
			p->u.prefix4.s_addr =
				zvrf->rid_user_assigned.u.prefix4.s_addr;
		else if (!list_isempty(zvrf->rid_lo_sorted_list)) {
			node = listtail(zvrf->rid_lo_sorted_list);
			c = listgetdata(node);
			p->u.prefix4.s_addr = c->address->u.prefix4.s_addr;
		} else if (!list_isempty(zvrf->rid_all_sorted_list)) {
			node = listtail(zvrf->rid_all_sorted_list);
			c = listgetdata(node);
			p->u.prefix4.s_addr = c->address->u.prefix4.s_addr;
		}
		return 0;
	case AFI_IP6:
		p->u.prefix6 = in6addr_any;
		p->family = AF_INET6;
		p->prefixlen = IPV6_MAX_BITLEN;
		if (!router_id_v6_is_any(&zvrf->rid6_user_assigned))
			addr = &zvrf->rid6_user_assigned.u.prefix6;
		else if (!list_isempty(zvrf->rid6_lo_sorted_list)) {
			node = listtail(zvrf->rid6_lo_sorted_list);
			c = listgetdata(node);
			addr = &c->address->u.prefix6;
		} else if (!list_isempty(zvrf->rid6_all_sorted_list)) {
			node = listtail(zvrf->rid6_all_sorted_list);
			c = listgetdata(node);
			addr = &c->address->u.prefix6;
		}
		if (addr)
			memcpy(&p->u.prefix6, addr, sizeof(struct in6_addr));
		return 0;
	case AFI_UNSPEC:
	case AFI_L2VPN:
	case AFI_MAX:
		return -1;
	}

	assert(!"Reached end of function we should never hit");
}

static int router_id_set(afi_t afi, struct prefix *p, struct zebra_vrf *zvrf)
{
	struct prefix after, before;
	struct listnode *node;
	struct zserv *client;

	router_id_get(afi, &before, zvrf);

	switch (afi) {
	case AFI_IP:
		zvrf->rid_user_assigned.u.prefix4.s_addr = p->u.prefix4.s_addr;
		break;
	case AFI_IP6:
		zvrf->rid6_user_assigned.u.prefix6 = p->u.prefix6;
		break;
	case AFI_UNSPEC:
	case AFI_L2VPN:
	case AFI_MAX:
		return -1;
	}

	router_id_get(afi, &after, zvrf);

	/*
	 * If we've been told that the router-id is exactly the same
	 * do we need to really do anything here?
	 */
	if (prefix_same(&before, &after))
		return 0;

	for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client))
		zsend_router_id_update(client, afi, &after, zvrf->vrf->vrf_id);

	return 0;
}

void router_id_add_address(struct connected *ifc)
{
	struct list *l = NULL;
	struct listnode *node;
	struct prefix before;
	struct prefix after;
	struct zserv *client;
	struct zebra_vrf *zvrf = ifc->ifp->vrf->info;
	afi_t afi;
	struct list *rid_lo;
	struct list *rid_all;

	if (router_id_bad_address(ifc))
		return;

	switch (ifc->address->family) {
	case AF_INET:
		afi = AFI_IP;
		rid_lo = zvrf->rid_lo_sorted_list;
		rid_all = zvrf->rid_all_sorted_list;
		break;
	case AF_INET6:
		afi = AFI_IP6;
		rid_lo = zvrf->rid6_lo_sorted_list;
		rid_all = zvrf->rid6_all_sorted_list;
		break;
	default:
		return;
	}

	router_id_get(afi, &before, zvrf);

	l = if_is_loopback(ifc->ifp) ? rid_lo : rid_all;

	if (!router_id_find_node(l, ifc))
		listnode_add_sort(l, ifc);

	router_id_get(afi, &after, zvrf);

	if (prefix_same(&before, &after))
		return;

	for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client))
		zsend_router_id_update(client, afi, &after, zvrf_id(zvrf));
}

void router_id_del_address(struct connected *ifc)
{
	struct connected *c;
	struct list *l;
	struct prefix after;
	struct prefix before;
	struct listnode *node;
	struct zserv *client;
	struct zebra_vrf *zvrf = ifc->ifp->vrf->info;
	afi_t afi;
	struct list *rid_lo;
	struct list *rid_all;

	if (router_id_bad_address(ifc))
		return;

	switch (ifc->address->family) {
	case AF_INET:
		afi = AFI_IP;
		rid_lo = zvrf->rid_lo_sorted_list;
		rid_all = zvrf->rid_all_sorted_list;
		break;
	case AF_INET6:
		afi = AFI_IP6;
		rid_lo = zvrf->rid6_lo_sorted_list;
		rid_all = zvrf->rid6_all_sorted_list;
		break;
	default:
		return;
	}

	router_id_get(afi, &before, zvrf);

	if (if_is_loopback(ifc->ifp))
		l = rid_lo;
	else
		l = rid_all;

	if ((c = router_id_find_node(l, ifc)))
		listnode_delete(l, c);

	router_id_get(afi, &after, zvrf);

	if (prefix_same(&before, &after))
		return;

	for (ALL_LIST_ELEMENTS_RO(zrouter.client_list, node, client))
		zsend_router_id_update(client, afi, &after, zvrf_id(zvrf));
}

void router_id_write(struct vty *vty, struct zebra_vrf *zvrf)
{
	char space[2];

	memset(space, 0, sizeof(space));

	if (zvrf_id(zvrf) != VRF_DEFAULT)
		snprintf(space, sizeof(space), "%s", " ");

	if (zvrf->rid_user_assigned.u.prefix4.s_addr != INADDR_ANY) {
		vty_out(vty, "%sip router-id %pI4\n", space,
			&zvrf->rid_user_assigned.u.prefix4);
	}
	if (!router_id_v6_is_any(&zvrf->rid6_user_assigned)) {
		vty_out(vty, "%sipv6 router-id %pI6\n", space,
			&zvrf->rid_user_assigned.u.prefix6);
	}
}

DEFUN (ip_router_id,
       ip_router_id_cmd,
       "ip router-id A.B.C.D vrf NAME",
       IP_STR
       "Manually set the router-id\n"
       "IP address to use for router-id\n"
       VRF_CMD_HELP_STR)
{
	int idx = 0;
	struct prefix rid;
	vrf_id_t vrf_id;
	struct zebra_vrf *zvrf;

	argv_find(argv, argc, "A.B.C.D", &idx);

	if (!inet_pton(AF_INET, argv[idx]->arg, &rid.u.prefix4))
		return CMD_WARNING_CONFIG_FAILED;

	rid.prefixlen = IPV4_MAX_BITLEN;
	rid.family = AF_INET;

	argv_find(argv, argc, "NAME", &idx);
	VRF_GET_ID(vrf_id, argv[idx]->arg, false);

	zvrf = zebra_vrf_lookup_by_id(vrf_id);
	router_id_set(AFI_IP, &rid, zvrf);

	return CMD_SUCCESS;
}

ALIAS (ip_router_id,
       router_id_cmd,
       "router-id A.B.C.D vrf NAME",
       "Manually set the router-id\n"
       "IP address to use for router-id\n"
       VRF_CMD_HELP_STR);

DEFUN (ipv6_router_id,
       ipv6_router_id_cmd,
       "ipv6 router-id X:X::X:X vrf NAME",
       IPV6_STR
       "Manually set the router-id\n"
       "IPv6 address to use for router-id\n"
       VRF_CMD_HELP_STR)
{
	int idx = 0;
	struct prefix rid;
	vrf_id_t vrf_id;
	struct zebra_vrf *zvrf;

	argv_find(argv, argc, "X:X::X:X", &idx);

	if (!inet_pton(AF_INET6, argv[idx]->arg, &rid.u.prefix6))
		return CMD_WARNING_CONFIG_FAILED;

	rid.prefixlen = IPV6_MAX_BITLEN;
	rid.family = AF_INET6;

	argv_find(argv, argc, "NAME", &idx);
	VRF_GET_ID(vrf_id, argv[idx]->arg, false);

	zvrf = zebra_vrf_lookup_by_id(vrf_id);
	router_id_set(AFI_IP6, &rid, zvrf);

	return CMD_SUCCESS;
}


DEFUN (ip_router_id_in_vrf,
       ip_router_id_in_vrf_cmd,
       "ip router-id A.B.C.D",
       IP_STR
       "Manually set the router-id\n"
       "IP address to use for router-id\n")
{
	ZEBRA_DECLVAR_CONTEXT_VRF(vrf, zvrf);
	int idx = 0;
	struct prefix rid;

	argv_find(argv, argc, "A.B.C.D", &idx);

	if (!inet_pton(AF_INET, argv[idx]->arg, &rid.u.prefix4))
		return CMD_WARNING_CONFIG_FAILED;

	rid.prefixlen = IPV4_MAX_BITLEN;
	rid.family = AF_INET;

	router_id_set(AFI_IP, &rid, zvrf);

	return CMD_SUCCESS;
}

ALIAS (ip_router_id_in_vrf,
       router_id_in_vrf_cmd,
       "router-id A.B.C.D",
       "Manually set the router-id\n"
       "IP address to use for router-id\n");

DEFUN (ipv6_router_id_in_vrf,
       ipv6_router_id_in_vrf_cmd,
       "ipv6 router-id X:X::X:X",
       IP6_STR
       "Manually set the IPv6 router-id\n"
       "IPV6 address to use for router-id\n")
{
	ZEBRA_DECLVAR_CONTEXT_VRF(vrf, zvrf);
	int idx = 0;
	struct prefix rid;

	argv_find(argv, argc, "X:X::X:X", &idx);

	if (!inet_pton(AF_INET6, argv[idx]->arg, &rid.u.prefix6))
		return CMD_WARNING_CONFIG_FAILED;

	rid.prefixlen = IPV6_MAX_BITLEN;
	rid.family = AF_INET6;

	router_id_set(AFI_IP6, &rid, zvrf);

	return CMD_SUCCESS;
}

DEFUN (no_ip_router_id,
       no_ip_router_id_cmd,
       "no ip router-id [A.B.C.D vrf NAME]",
       NO_STR
       IP_STR
       "Remove the manually configured router-id\n"
       "IP address to use for router-id\n"
       VRF_CMD_HELP_STR)
{
	int idx = 0;
	struct prefix rid;
	vrf_id_t vrf_id = VRF_DEFAULT;
	struct zebra_vrf *zvrf;

	rid.u.prefix4.s_addr = 0;
	rid.prefixlen = 0;
	rid.family = AF_INET;

	if (argv_find(argv, argc, "NAME", &idx))
		VRF_GET_ID(vrf_id, argv[idx]->arg, false);

	zvrf = zebra_vrf_lookup_by_id(vrf_id);
	router_id_set(AFI_IP, &rid, zvrf);

	return CMD_SUCCESS;
}

ALIAS (no_ip_router_id,
       no_router_id_cmd,
       "no router-id [A.B.C.D vrf NAME]",
       NO_STR
       "Remove the manually configured router-id\n"
       "IP address to use for router-id\n"
       VRF_CMD_HELP_STR);

DEFUN (no_ipv6_router_id,
       no_ipv6_router_id_cmd,
       "no ipv6 router-id [X:X::X:X vrf NAME]",
       NO_STR
       IPV6_STR
       "Remove the manually configured IPv6 router-id\n"
       "IPv6 address to use for router-id\n"
       VRF_CMD_HELP_STR)
{
	int idx = 0;
	struct prefix rid;
	vrf_id_t vrf_id = VRF_DEFAULT;
	struct zebra_vrf *zvrf;

	memset(&rid, 0, sizeof(rid));
	rid.family = AF_INET;

	if (argv_find(argv, argc, "NAME", &idx))
		VRF_GET_ID(vrf_id, argv[idx]->arg, false);

	zvrf = zebra_vrf_lookup_by_id(vrf_id);
	router_id_set(AFI_IP6, &rid, zvrf);

	return CMD_SUCCESS;
}

DEFUN (no_ip_router_id_in_vrf,
       no_ip_router_id_in_vrf_cmd,
       "no ip router-id [A.B.C.D]",
       NO_STR
       IP_STR
       "Remove the manually configured router-id\n"
       "IP address to use for router-id\n")
{
	ZEBRA_DECLVAR_CONTEXT_VRF(vrf, zvrf);

	struct prefix rid;

	rid.u.prefix4.s_addr = 0;
	rid.prefixlen = 0;
	rid.family = AF_INET;

	router_id_set(AFI_IP, &rid, zvrf);

	return CMD_SUCCESS;
}

ALIAS (no_ip_router_id_in_vrf,
       no_router_id_in_vrf_cmd,
       "no router-id [A.B.C.D]",
       NO_STR
       "Remove the manually configured router-id\n"
       "IP address to use for router-id\n");

DEFUN (no_ipv6_router_id_in_vrf,
       no_ipv6_router_id_in_vrf_cmd,
       "no ipv6 router-id [X:X::X:X]",
       NO_STR
       IP6_STR
       "Remove the manually configured IPv6 router-id\n"
       "IPv6 address to use for router-id\n")
{
	ZEBRA_DECLVAR_CONTEXT_VRF(vrf, zvrf);

	struct prefix rid;

	memset(&rid, 0, sizeof(rid));
	rid.family = AF_INET;

	router_id_set(AFI_IP6, &rid, zvrf);

	return CMD_SUCCESS;
}

DEFUN (show_ip_router_id,
       show_ip_router_id_cmd,
       "show [ip|ipv6] router-id [vrf NAME]",
       SHOW_STR
       IP_STR
       IPV6_STR
       "Show the configured router-id\n"
       VRF_CMD_HELP_STR)
{
	int idx = 0;
	vrf_id_t vrf_id = VRF_DEFAULT;
	struct zebra_vrf *zvrf;
	const char *vrf_name = "default";
	char addr_name[INET6_ADDRSTRLEN];
	int is_ipv6 = 0;

	is_ipv6 = argv_find(argv, argc, "ipv6", &idx);

	if (argv_find(argv, argc, "NAME", &idx)) {
		VRF_GET_ID(vrf_id, argv[idx]->arg, false);
		vrf_name = argv[idx]->arg;
	}

	zvrf = zebra_vrf_lookup_by_id(vrf_id);

	if (zvrf != NULL) {
		if (is_ipv6) {
			if (router_id_v6_is_any(&zvrf->rid6_user_assigned))
				return CMD_SUCCESS;
			inet_ntop(AF_INET6, &zvrf->rid6_user_assigned.u.prefix6,
				  addr_name, sizeof(addr_name));
		} else {
			if (zvrf->rid_user_assigned.u.prefix4.s_addr
			    == INADDR_ANY)
				return CMD_SUCCESS;
			inet_ntop(AF_INET, &zvrf->rid_user_assigned.u.prefix4,
				  addr_name, sizeof(addr_name));
		}

		vty_out(vty, "zebra:\n");
		vty_out(vty, "     router-id %s vrf %s\n", addr_name, vrf_name);
	}

	return CMD_SUCCESS;
}

static int router_id_cmp(void *a, void *b)
{
	const struct connected *ifa = (const struct connected *)a;
	const struct connected *ifb = (const struct connected *)b;

	return IPV4_ADDR_CMP(&ifa->address->u.prefix4.s_addr,
			     &ifb->address->u.prefix4.s_addr);
}

static int router_id_v6_cmp(void *a, void *b)
{
	const struct connected *ifa = (const struct connected *)a;
	const struct connected *ifb = (const struct connected *)b;

	return IPV6_ADDR_CMP(&ifa->address->u.prefix6,
			     &ifb->address->u.prefix6);
}

void router_id_cmd_init(void)
{
	install_element(CONFIG_NODE, &ip_router_id_cmd);
	install_element(CONFIG_NODE, &router_id_cmd);
	install_element(CONFIG_NODE, &ipv6_router_id_cmd);
	install_element(CONFIG_NODE, &no_ip_router_id_cmd);
	install_element(CONFIG_NODE, &no_router_id_cmd);
	install_element(CONFIG_NODE, &ip_router_id_in_vrf_cmd);
	install_element(VRF_NODE, &ip_router_id_in_vrf_cmd);
	install_element(CONFIG_NODE, &router_id_in_vrf_cmd);
	install_element(VRF_NODE, &router_id_in_vrf_cmd);
	install_element(CONFIG_NODE, &ipv6_router_id_in_vrf_cmd);
	install_element(VRF_NODE, &ipv6_router_id_in_vrf_cmd);
	install_element(CONFIG_NODE, &no_ipv6_router_id_cmd);
	install_element(CONFIG_NODE, &no_ip_router_id_in_vrf_cmd);
	install_element(VRF_NODE, &no_ip_router_id_in_vrf_cmd);
	install_element(CONFIG_NODE, &no_router_id_in_vrf_cmd);
	install_element(VRF_NODE, &no_router_id_in_vrf_cmd);
	install_element(CONFIG_NODE, &no_ipv6_router_id_in_vrf_cmd);
	install_element(VRF_NODE, &no_ipv6_router_id_in_vrf_cmd);
	install_element(VIEW_NODE, &show_ip_router_id_cmd);
}

void router_id_init(struct zebra_vrf *zvrf)
{
	zvrf->rid_all_sorted_list = &zvrf->_rid_all_sorted_list;
	zvrf->rid_lo_sorted_list = &zvrf->_rid_lo_sorted_list;
	zvrf->rid6_all_sorted_list = &zvrf->_rid6_all_sorted_list;
	zvrf->rid6_lo_sorted_list = &zvrf->_rid6_lo_sorted_list;

	memset(zvrf->rid_all_sorted_list, 0,
	       sizeof(zvrf->_rid_all_sorted_list));
	memset(zvrf->rid_lo_sorted_list, 0, sizeof(zvrf->_rid_lo_sorted_list));
	memset(&zvrf->rid_user_assigned, 0, sizeof(zvrf->rid_user_assigned));
	memset(zvrf->rid6_all_sorted_list, 0,
	       sizeof(zvrf->_rid6_all_sorted_list));
	memset(zvrf->rid6_lo_sorted_list, 0,
	       sizeof(zvrf->_rid6_lo_sorted_list));
	memset(&zvrf->rid6_user_assigned, 0, sizeof(zvrf->rid6_user_assigned));

	zvrf->rid_all_sorted_list->cmp = router_id_cmp;
	zvrf->rid_lo_sorted_list->cmp = router_id_cmp;
	zvrf->rid6_all_sorted_list->cmp = router_id_v6_cmp;
	zvrf->rid6_lo_sorted_list->cmp = router_id_v6_cmp;

	zvrf->rid_user_assigned.family = AF_INET;
	zvrf->rid_user_assigned.prefixlen = IPV4_MAX_BITLEN;
	zvrf->rid6_user_assigned.family = AF_INET6;
	zvrf->rid6_user_assigned.prefixlen = IPV6_MAX_BITLEN;
}