summaryrefslogtreecommitdiffstats
path: root/vrrpd/vrrp.c
blob: f0a106ba0519282bd5ee47db24919e9b17b8ddb0 (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
/*
 * VRRPD global definitions
 * Copyright (C) 2018 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 "memory.h"
#include "if.h"
#include "linklist.h"
#include "prefix.h"
#include "hash.h"
#include "vrf.h"
#include "hook.h"

#include "vrrp.h"

/* Utility functions ------------------------------------------------------- */

/*
 * Sets an ethaddr to RFC-defined Virtual Router MAC address.
 *
 * mac
 *    ethaddr to set
 *
 * v6
 *    Whether this is a V6 or V4 Virtual Router MAC
 *
 * vrid
 *    Virtual Router Identifier
 */
static void vrrp_mac_set(struct ethaddr *mac, bool v6, uint8_t vrid)
{
	/*
	 * V4: 00-00-5E-00-01-{VRID}
	 * V6: 00-00-5E-00-02-{VRID}
	 */
	mac->octet[0] = 0x00;
	mac->octet[1] = 0x00;
	mac->octet[2] = 0x5E;
	mac->octet[3] = 0x00;
	mac->octet[4] = v6 ? 0x02 : 0x01;
	mac->octet[5] = vrid;
}

/*
 * Sets advertisement_interval and master_adver_interval on a Virtual Router,
 * then recalculates and sets skew_time and master_down_interval based on these
 * values.
 *
 * vr
 *    Virtual Router to operate on
 *
 * advertisement_interval
 *    Advertisement_Interval to set
 *
 * master_adver_interval
 *    Master_Adver_Interval to set
 */
static void vrrp_update_times(struct vrrp_vrouter *vr, uint16_t advertisement_interval,
		       uint16_t master_adver_interval)
{
	vr->advertisement_interval = advertisement_interval;
	vr->master_adver_interval = master_adver_interval;
	vr->skew_time = (256 - vr->priority) * vr->master_adver_interval;
	vr->skew_time /= 256;
	vr->master_down_interval = (3 * vr->master_adver_interval);
	vr->master_down_interval /= 256;
}

struct vrrp_vrouter *vrrp_vrouter_create(struct interface *ifp, uint8_t vrid)
{
	struct vrrp_vrouter *vr =
		XCALLOC(MTYPE_TMP, sizeof(struct vrrp_vrouter));

	vr->sock = -1;
	vr->ifp = ifp;
	vr->vrid = vrid;
	vr->v4 = list_new();
	vr->v6 = list_new();
	vr->advint = VRRP_DEFAULT_ADVINT;
	vr->is_master = false;
	vr->priority = VRRP_DEFAULT_PRIORITY;
	vr->advertisement_interval = VRRP_DEFAULT_ADVINT;
	vr->master_adver_interval = 0;
	vr->skew_time = 0;
	vr->master_down_interval = 0;
	vr->preempt_mode = true;
	vr->accept_mode = false;
	vrrp_mac_set(&vr->vr_mac_v4, false, vrid);
	vrrp_mac_set(&vr->vr_mac_v6, true, vrid);
	vr->fsm.state = VRRP_STATE_INITIALIZE;

	hash_get(vrrp_vrouters_hash, vr, hash_alloc_intern);

	return vr;
}

struct vrrp_vrouter *vrrp_lookup(uint8_t vrid)
{
	struct vrrp_vrouter vr;
	vr.vrid = vrid;

	return hash_lookup(vrrp_vrouters_hash, &vr);
}

/* Network ----------------------------------------------------------------- */

/*
 * Create and broadcast VRRP ADVERTISEMENT message.
 *
 * vr
 *    Virtual Router for which to send ADVERTISEMENT
 */
static void vrrp_send_advertisement(struct vrrp_vrouter *vr)
{
}

/* FIXME:
static void vrrp_recv_advertisement(struct thread *thread)
{
}
*/

/*
 * Create Virtual Router listen socket and join it to the VRRP multicast group.
 *
 * The first connected address on the Virtual Router's interface is used as the
 * interface address.
 *
 * vr
 *    Virtual Router for which to create listen socket
 */
static int vrrp_socket(struct vrrp_vrouter *vr)
{
	struct ip_mreqn req;
	int ret;

	vr->sock = socket(AF_INET, SOCK_RAW, IPPROTO_VRRP);

	if (vr->sock < 0) {
		/* FIXME */
	}

	/* Join the multicast group.*/

	 /* FIXME: Use first address on the interface and for imr_interface */
	struct connected *c = listhead(vr->ifp->connected)->data;
	struct in_addr v4 = c->address->u.prefix4;

	memset(&req, 0, sizeof(req));
	req.imr_multiaddr.s_addr = htonl(VRRP_MCAST_GROUP_HEX);
	req.imr_address = v4;
	req.imr_ifindex = 0; // FIXME: vr->ifp->ifindex ?
	ret = setsockopt(vr->sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&req,
			 sizeof(struct ip_mreq));
	if (ret < 0) {
		// int err = errno;
		/* VRRP_LOG(("cant do IP_ADD_MEMBERSHIP errno=%d\n", err)); */
		return -1;
	}
	return 0;
}


/* State machine ----------------------------------------------------------- */

DEFINE_HOOK(vrrp_change_state_hook, (struct vrrp_vrouter *vr, int to), (vr, to));

/*
 * Handle any necessary actions during state change to MASTER state.
 *
 * vr
 *    Virtual Router to operate on
 */
static void vrrp_change_state_master(struct vrrp_vrouter *vr)
{
}

/*
 * Handle any necessary actions during state change to BACKUP state.
 *
 * vr
 *    Virtual Router to operate on
 */
static void vrrp_change_state_backup(struct vrrp_vrouter *vr)
{
	/* Uninstall ARP entry for vrouter MAC */
	/* ... */
}

/*
 * Handle any necessary actions during state change to INITIALIZE state.
 *
 * This is not called for initial startup, only when transitioning from MASTER
 * or BACKUP.
 *
 * vr
 *    Virtual Router to operate on
 */
static void vrrp_change_state_initialize(struct vrrp_vrouter *vr)
{
}

void (*vrrp_change_state_handlers[])(struct vrrp_vrouter *vr) = {
	[VRRP_STATE_MASTER] = vrrp_change_state_master,
	[VRRP_STATE_BACKUP] = vrrp_change_state_backup,
	[VRRP_STATE_INITIALIZE] = vrrp_change_state_initialize,
};

/*
 * Change Virtual Router FSM position. Handles transitional actions and calls
 * any subscribers to the state change hook.
 *
 * vr
 *    Virtual Router for which to change state
 *
 * to
 *    State to change to
 */
static void vrrp_change_state(struct vrrp_vrouter *vr, int to)
{
	/* Call our handlers, then any subscribers */
	vrrp_change_state_handlers[to](vr);
	hook_call(vrrp_change_state_hook, vr, to);
	vr->fsm.state = to;
}

/*
 * Called when Adver_Timer expires.
 */
static int vrrp_adver_timer_expire(struct thread *thread)
{
	struct vrrp_vrouter *vr = thread->arg;

	if (vr->fsm.state == VRRP_STATE_BACKUP) {
		vrrp_send_advertisement(vr);
		/* FIXME: vrrp_send_gratuitous_arp(vr); */
	} else if (vr->fsm.state == VRRP_STATE_MASTER) {

	} else if (vr->fsm.state == VRRP_STATE_INITIALIZE) {
		assert(!"FUCK");
	}
	return 0;
}

/*
 * Called when Master_Down timer expires.
 */
static int vrrp_master_down_timer_expire(struct thread *thread)
{
	/* struct vrrp_vrouter *vr = thread->arg; */

	return 0;
}

/*
 * Event handler for Startup event.
 *
 * Creates sockets, sends advertisements and ARP requests, starts timers,
 * updates state machine.
 *
 * vr
 *    Virtual Router on which to apply Startup event
 */
static void vrrp_startup(struct vrrp_vrouter *vr)
{
	/* Create socket */
	vrrp_socket(vr);

	/* Schedule listener */
	/* ... */

	if (vr->priority == VRRP_PRIO_MASTER) {
		vrrp_send_advertisement(vr);
		/* FIXME: vrrp_send_gratuitous_arp(vr); */

		thread_add_timer_msec(master, vrrp_adver_timer_expire, vr,
				      vr->advertisement_interval * 10,
				      &vr->t_adver_timer);
		vrrp_change_state(vr, VRRP_STATE_MASTER);
	} else {
		vrrp_update_times(vr, vr->advertisement_interval,
				  vr->advertisement_interval);
		thread_add_timer_msec(master, vrrp_master_down_timer_expire, vr,
				      vr->master_down_interval * 10,
				      &vr->t_master_down_timer);
		vrrp_change_state(vr, VRRP_STATE_BACKUP);
	}
}

static void vrrp_shutdown(struct vrrp_vrouter *vr)
{
	/* NOTHING */
}

static void (*vrrp_event_handlers[])(struct vrrp_vrouter *vr) = {
	[VRRP_EVENT_STARTUP] = vrrp_startup,
	[VRRP_EVENT_SHUTDOWN] = vrrp_shutdown,
};

/*
 * Spawn a VRRP FSM event on a Virtual Router.
 *
 * vr
 *    Virtual Router on which to spawn event
 *
 * event
 *    The event to spawn
 */
void vrrp_event(struct vrrp_vrouter *vr, int event)
{
	vrrp_event_handlers[event](vr);
}


/* Other ------------------------------------------------------------------- */

static unsigned int vrrp_hash_key(void *arg)
{
	struct vrrp_vrouter *vr = arg;

	return vr->vrid;
}

static bool vrrp_hash_cmp(const void *arg1, const void *arg2)
{
	const struct vrrp_vrouter *vr1 = arg1;
	const struct vrrp_vrouter *vr2 = arg2;

	return vr1->vrid > vr2->vrid;
}

void vrrp_init(void)
{
	vrrp_vrouters_hash = hash_create(&vrrp_hash_key, vrrp_hash_cmp,
					 "VRRP virtual router hash");
	vrf_init(NULL, NULL, NULL, NULL, NULL);
}