summaryrefslogtreecommitdiffstats
path: root/vrrpd/vrrp_zebra.c
blob: 0844b90266e7d2c61252b2c1689a17c54caa8c31 (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
/*
 * VRRP Zebra interfacing.
 * Copyright (C) 2018-2019 Cumulus Networks, Inc.
 * Quentin Young
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; see the file COPYING; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */
#include <zebra.h>

#include "lib/if.h"
#include "lib/linklist.h"
#include "lib/log.h"
#include "lib/prefix.h"
#include "lib/vty.h"
#include "lib/zclient.h"

#include "vrrp.h"
#include "vrrp_debug.h"
#include "vrrp_zebra.h"

#define VRRP_LOGPFX "[ZEBRA] "

static struct zclient *zclient;

static void vrrp_zebra_debug_if_state(struct interface *ifp, vrf_id_t vrf_id,
				      const char *func)
{
	DEBUGD(&vrrp_dbg_zebra,
	       "%s: %s index %d(%u) parent %d mac %02x:%02x:%02x:%02x:%02x:%02x flags %ld metric %d mtu %d operative %d",
	       func, ifp->name, vrf_id, ifp->link_ifindex, ifp->ifindex,
	       ifp->hw_addr[0], ifp->hw_addr[1], ifp->hw_addr[2],
	       ifp->hw_addr[3], ifp->hw_addr[4], ifp->hw_addr[5],
	       (long)ifp->flags, ifp->metric, ifp->mtu, if_is_operative(ifp));
}

static void vrrp_zebra_debug_if_dump_address(struct interface *ifp,
					     const char *func)
{
	struct connected *ifc;
	struct listnode *node;

	DEBUGD(&vrrp_dbg_zebra, "%s: interface %s addresses:", func, ifp->name);

	for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
		struct prefix *p = ifc->address;

		DEBUGD(&vrrp_dbg_zebra, "%s: interface %s address %s %s", func,
		       ifp->name, inet_ntoa(p->u.prefix4),
		       CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY) ? "secondary"
								   : "primary");
	}
}


static void vrrp_zebra_connected(struct zclient *zclient)
{
	zclient_send_reg_requests(zclient, VRF_DEFAULT);
}

/* Router-id update message from zebra. */
static int vrrp_router_id_update_zebra(int command, struct zclient *zclient,
				       zebra_size_t length, vrf_id_t vrf_id)
{
	struct prefix router_id;

	zebra_router_id_update_read(zclient->ibuf, &router_id);

	return 0;
}

static int vrrp_zebra_if_add(int command, struct zclient *zclient,
			     zebra_size_t length, vrf_id_t vrf_id)
{
	struct interface *ifp;

	/*
	 * zebra api adds/dels interfaces using the same call
	 * interface_add_read below, see comments in lib/zclient.c
	 */
	ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);

	if (!ifp)
		return 0;

	vrrp_zebra_debug_if_state(ifp, vrf_id, __func__);

	vrrp_if_add(ifp);

	return 0;
}

static int vrrp_zebra_if_del(int command, struct zclient *zclient,
			     zebra_size_t length, vrf_id_t vrf_id)
{
	struct interface *ifp;

	ifp = zebra_interface_state_read(zclient->ibuf, vrf_id);

	if (!ifp)
		return 0;

	vrrp_zebra_debug_if_state(ifp, vrf_id, __func__);

	vrrp_if_del(ifp);

	if_set_index(ifp, IFINDEX_INTERNAL);

	return 0;
}

static int vrrp_zebra_if_state_up(int command, struct zclient *zclient,
				  zebra_size_t length, vrf_id_t vrf_id)
{
	struct interface *ifp;

	/*
	 * zebra api notifies interface up/down events by using the same call
	 * zebra_interface_state_read below, see comments in lib/zclient.c ifp =
	 * zebra_interface_state_read(zclient->ibuf, vrf_id);
	 */
	ifp = zebra_interface_state_read(zclient->ibuf, vrf_id);

	if (!ifp)
		return 0;

	vrrp_zebra_debug_if_state(ifp, vrf_id, __func__);

	vrrp_if_up(ifp);

	return 0;
}

static int vrrp_zebra_if_state_down(int command, struct zclient *zclient,
				    zebra_size_t length, vrf_id_t vrf_id)
{
	struct interface *ifp;

	/*
	 * zebra api notifies interface up/down events by using the same call
	 * zebra_interface_state_read below, see comments in lib/zclient.c
	 */
	ifp = zebra_interface_state_read(zclient->ibuf, vrf_id);

	if (!ifp)
		return 0;

	vrrp_zebra_debug_if_state(ifp, vrf_id, __func__);

	vrrp_if_down(ifp);

	return 0;
}

static int vrrp_zebra_if_address_add(int command, struct zclient *zclient,
				     zebra_size_t length, vrf_id_t vrf_id)
{
	struct connected *c;

	/*
	 * zebra api notifies address adds/dels events by using the same call
	 * interface_add_read below, see comments in lib/zclient.c
	 *
	 * zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD, ...)
	 * will add address to interface list by calling
	 * connected_add_by_prefix()
	 */
	c = zebra_interface_address_read(command, zclient->ibuf, vrf_id);

	if (!c)
		return 0;

	vrrp_zebra_debug_if_state(c->ifp, vrf_id, __func__);
	vrrp_zebra_debug_if_dump_address(c->ifp, __func__);

	vrrp_if_address_add(c->ifp);

	return 0;
}

static int vrrp_zebra_if_address_del(int command, struct zclient *client,
				     zebra_size_t length, vrf_id_t vrf_id)
{
	struct connected *c;

	/*
	 * zebra api notifies address adds/dels events by using the same call
	 * interface_add_read below, see comments in lib/zclient.c
	 *
	 * zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE, ...)
	 * will remove address from interface list by calling
	 * connected_delete_by_prefix()
	 */
	c = zebra_interface_address_read(command, client->ibuf, vrf_id);

	if (!c)
		return 0;

	vrrp_zebra_debug_if_state(c->ifp, vrf_id, __func__);
	vrrp_zebra_debug_if_dump_address(c->ifp, __func__);

	vrrp_if_address_del(c->ifp);

	return 0;
}

void vrrp_zebra_radv_set(struct vrrp_router *r, bool enable)
{
	DEBUGD(&vrrp_dbg_zebra,
	       VRRP_LOGPFX VRRP_LOGPFX_VRID
	       "Requesting Zebra to turn router advertisements %s for %s",
	       r->vr->vrid, enable ? "on" : "off", r->mvl_ifp->name);

	zclient_send_interface_radv_req(zclient, VRF_DEFAULT, r->mvl_ifp,
					enable, VRRP_RADV_INT);
}

int vrrp_zclient_send_interface_protodown(struct interface *ifp, bool down)
{
	DEBUGD(&vrrp_dbg_zebra,
	       VRRP_LOGPFX "Requesting Zebra to set %s protodown %s", ifp->name,
	       down ? "on" : "off");

	return zclient_send_interface_protodown(zclient, VRF_DEFAULT, ifp,
						down);
}

int vrrp_ifp_create(struct interface *ifp)
{
	return 0;
}

int vrrp_ifp_up(struct interface *ifp)
{
	return 0;
}

int vrrp_ifp_down(struct interface *ifp)
{
	return 0;
}

int vrrp_ifp_destroy(struct interface *ifp)
{
	return 0;
}

void vrrp_zebra_init(void)
{
	if_zapi_callbacks(vrrp_ifp_create, vrrp_ifp_up,
			  vrrp_ifp_down, vrrp_ifp_destroy);

	/* Socket for receiving updates from Zebra daemon */
	zclient = zclient_new(master, &zclient_options_default);

	zclient->zebra_connected = vrrp_zebra_connected;
	zclient->router_id_update = vrrp_router_id_update_zebra;
	zclient->interface_add = vrrp_zebra_if_add;
	zclient->interface_delete = vrrp_zebra_if_del;
	zclient->interface_up = vrrp_zebra_if_state_up;
	zclient->interface_down = vrrp_zebra_if_state_down;
	zclient->interface_address_add = vrrp_zebra_if_address_add;
	zclient->interface_address_delete = vrrp_zebra_if_address_del;

	zclient_init(zclient, ZEBRA_ROUTE_VRRP, 0, &vrrp_privs);

	zlog_notice("%s: zclient socket initialized", __PRETTY_FUNCTION__);
}