]> git.proxmox.com Git - mirror_frr.git/blob - vrrpd/vrrp.h
doc: Add `show ipv6 rpf X:X::X:X` command to docs
[mirror_frr.git] / vrrpd / vrrp.h
1 /*
2 * VRRP global definitions and state machine.
3 * Copyright (C) 2018-2019 Cumulus Networks, Inc.
4 * Quentin Young
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #ifndef __VRRP_H__
21 #define __VRRP_H__
22
23 #include <zebra.h>
24 #include <netinet/ip.h>
25
26 #include "lib/memory.h"
27 #include "lib/hash.h"
28 #include "lib/hook.h"
29 #include "lib/if.h"
30 #include "lib/linklist.h"
31 #include "lib/northbound.h"
32 #include "lib/privs.h"
33 #include "lib/stream.h"
34 #include "lib/thread.h"
35 #include "lib/vty.h"
36
37 /* Global definitions */
38 #define VRRP_RADV_INT 16
39 #define VRRP_PRIO_MASTER 255
40 #define VRRP_MCASTV4_GROUP_STR "224.0.0.18"
41 #define VRRP_MCASTV6_GROUP_STR "ff02:0:0:0:0:0:0:12"
42 #define VRRP_MCASTV4_GROUP 0xe0000012
43 #define VRRP_MCASTV6_GROUP 0xff020000000000000000000000000012
44 #define IPPROTO_VRRP 112
45
46 #define VRRP_LOGPFX_VRID "[VRID %u] "
47 #define VRRP_LOGPFX_FAM "[%s] "
48
49 /* Default defaults */
50 #define VRRP_XPATH_FULL "/frr-interface:lib/interface/frr-vrrpd:vrrp/vrrp-group"
51 #define VRRP_XPATH "./frr-vrrpd:vrrp/vrrp-group"
52 #define VRRP_DEFAULT_PRIORITY 100
53 #define VRRP_DEFAULT_ADVINT 100
54 #define VRRP_DEFAULT_PREEMPT true
55 #define VRRP_DEFAULT_ACCEPT true
56 #define VRRP_DEFAULT_CHECKSUM_WITH_IPV4_PSEUDOHEADER true
57 #define VRRP_DEFAULT_SHUTDOWN false
58
59 /* User compatibility constant */
60 #define CS2MS 10
61
62 DECLARE_MGROUP(VRRPD);
63
64 /* Northbound */
65 extern const struct frr_yang_module_info frr_vrrpd_info;
66
67 /* Configured defaults */
68 struct vrrp_defaults {
69 uint8_t version;
70 uint8_t priority;
71 uint16_t advertisement_interval;
72 bool preempt_mode;
73 bool accept_mode;
74 bool checksum_with_ipv4_pseudoheader;
75 bool shutdown;
76 };
77
78 extern struct vrrp_defaults vd;
79
80 /* threadmaster */
81 extern struct thread_master *master;
82
83 /* privileges */
84 extern struct zebra_privs_t vrrp_privs;
85
86 /* Global hash of all Virtual Routers */
87 extern struct hash *vrrp_vrouters_hash;
88
89 /*
90 * VRRP Router.
91 *
92 * This struct contains all state for a particular VRRP Router operating
93 * in a Virtual Router for either IPv4 or IPv6.
94 */
95 struct vrrp_router {
96 /*
97 * Whether this VRRP Router is active.
98 */
99 bool is_active;
100
101 /* Whether we are the address owner */
102 bool is_owner;
103
104 /* Rx socket: Rx from parent of mvl_ifp */
105 int sock_rx;
106 /* Tx socket; Tx from mvl_ifp */
107 int sock_tx;
108
109 /* macvlan interface */
110 struct interface *mvl_ifp;
111
112 /* Source address for advertisements */
113 struct ipaddr src;
114
115 /* Socket read buffer */
116 uint8_t ibuf[IP_MAXPACKET];
117
118 /*
119 * Address family of this Virtual Router.
120 * Either AF_INET or AF_INET6.
121 */
122 int family;
123
124 /*
125 * Virtual Router this VRRP Router is participating in.
126 */
127 struct vrrp_vrouter *vr;
128
129 /*
130 * One or more IPvX addresses associated with this Virtual
131 * Router. The first address must be the "primary" address this
132 * Virtual Router is backing up in the case of IPv4. In the case of
133 * IPv6 it must be the link-local address of vr->ifp.
134 *
135 * Type: struct ipaddr *
136 */
137 struct list *addrs;
138
139 /*
140 * This flag says whether we are waiting on an interface up
141 * notification from Zebra before we send an ADVERTISEMENT.
142 */
143 bool advert_pending;
144
145 /*
146 * If this is an IPv4 VRRP router, this flag says whether we are
147 * waiting on an interface up notification from Zebra before we send
148 * gratuitous ARP packets for all our addresses. Should never be true
149 * if family == AF_INET6.
150 */
151 bool garp_pending;
152 /*
153 * If this is an IPv6 VRRP router, this flag says whether we are
154 * waiting on an interface up notification from Zebra before we send
155 * Unsolicited Neighbor Advertisement packets for all our addresses.
156 * Should never be true if family == AF_INET.
157 */
158 bool ndisc_pending;
159
160 /*
161 * Effective priority
162 * => vr->priority if we are Backup
163 * => 255 if we are Master
164 */
165 uint8_t priority;
166
167 /*
168 * Advertisement interval contained in ADVERTISEMENTS received from the
169 * Master (centiseconds)
170 */
171 uint16_t master_adver_interval;
172
173 /*
174 * Time to skew Master_Down_Interval in centiseconds. Calculated as:
175 * (((256 - priority) * Master_Adver_Interval) / 256)
176 */
177 uint16_t skew_time;
178
179 /*
180 * Time interval for Backup to declare Master down (centiseconds).
181 * Calculated as:
182 * (3 * Master_Adver_Interval) + Skew_time
183 */
184 uint16_t master_down_interval;
185
186 /*
187 * The MAC address used for the source MAC address in VRRP
188 * advertisements, advertised in ARP requests/responses, and advertised
189 * in ND Neighbor Advertisements.
190 */
191 struct ethaddr vmac;
192
193 struct {
194 int state;
195 } fsm;
196
197 struct {
198 /* Total number of advertisements sent and received */
199 uint32_t adver_tx_cnt;
200 uint32_t adver_rx_cnt;
201 /* Total number of gratuitous ARPs sent */
202 uint32_t garp_tx_cnt;
203 /* Total number of unsolicited Neighbor Advertisements sent */
204 uint32_t una_tx_cnt;
205 /* Total number of state transitions */
206 uint32_t trans_cnt;
207 } stats;
208
209 struct thread *t_master_down_timer;
210 struct thread *t_adver_timer;
211 struct thread *t_read;
212 struct thread *t_write;
213 };
214
215 /*
216 * VRRP Virtual Router.
217 *
218 * This struct contains all state and configuration for a given Virtual Router
219 * Identifier on a given interface, both v4 and v6.
220 *
221 * RFC5798 s. 1 states:
222 * "Within a VRRP router, the virtual routers in each of the IPv4 and IPv6
223 * address families are a domain unto themselves and do not overlap."
224 *
225 * This implementation has chosen the tuple (interface, VRID) as the key for a
226 * particular VRRP Router, and the rest of the program is designed around this
227 * assumption. Additionally, base protocol configuration parameters such as the
228 * advertisement interval and (configured) priority are shared between v4 and
229 * v6 instances. This corresponds to the choice made by other industrial
230 * implementations.
231 */
232 struct vrrp_vrouter {
233 /* Whether this instance was automatically configured */
234 bool autoconf;
235
236 /* Whether this VRRP router is in administrative shutdown */
237 bool shutdown;
238
239 /* Interface */
240 struct interface *ifp;
241
242 /* Version */
243 uint8_t version;
244
245 /* Virtual Router Identifier */
246 uint32_t vrid;
247
248 /* Configured priority */
249 uint8_t priority;
250
251 /*
252 * Time interval between ADVERTISEMENTS (centiseconds). Default is 100
253 * centiseconds (1 second).
254 */
255 uint16_t advertisement_interval;
256
257 /*
258 * Controls whether a (starting or restarting) higher-priority Backup
259 * router preempts a lower-priority Master router. Values are True to
260 * allow preemption and False to prohibit preemption. Default is True.
261 */
262 bool preempt_mode;
263
264 /*
265 * Controls whether a virtual router in Master state will accept
266 * packets addressed to the address owner's IPvX address as its own if
267 * it is not the IPvX address owner. The default is False.
268 */
269 bool accept_mode;
270
271 /*
272 * Indicates whether this router computes and accepts VRRPv3 checksums
273 * without pseudoheader, for device interoperability.
274 *
275 * This option should only affect IPv4 virtual routers.
276 */
277 bool checksum_with_ipv4_pseudoheader;
278
279 struct vrrp_router *v4;
280 struct vrrp_router *v6;
281 };
282
283 /*
284 * Initialize VRRP global datastructures.
285 */
286 void vrrp_init(void);
287
288 /*
289 * Destroy all VRRP instances and gracefully shutdown.
290 *
291 * For instances in Master state, VRRP advertisements with 0 priority will be
292 * sent if possible to notify Backup routers that we are going away.
293 */
294 void vrrp_fini(void);
295
296
297 /* Creation and destruction ------------------------------------------------ */
298
299 /*
300 * Create and register a new VRRP Virtual Router.
301 *
302 * ifp
303 * Base interface to configure VRRP on
304 *
305 * vrid
306 * Virtual Router Identifier
307 */
308 struct vrrp_vrouter *vrrp_vrouter_create(struct interface *ifp, uint8_t vrid,
309 uint8_t version);
310
311 /*
312 * Destroy a VRRP Virtual Router, freeing all its resources.
313 *
314 * If there are any running VRRP instances, these are stopped and destroyed.
315 */
316 void vrrp_vrouter_destroy(struct vrrp_vrouter *vr);
317
318
319 /* Configuration controllers ----------------------------------------------- */
320
321 /*
322 * Check if a Virtual Router ought to be started, and if so, start it.
323 *
324 * vr
325 * Virtual Router to checkstart
326 */
327 void vrrp_check_start(struct vrrp_vrouter *vr);
328
329 /*
330 * Change the configured priority of a VRRP Virtual Router.
331 *
332 * Note that this only changes the configured priority of the Virtual Router.
333 * The currently effective priority will not be changed; to change the
334 * effective priority, the Virtual Router must be restarted by issuing a
335 * VRRP_EVENT_SHUTDOWN followed by a VRRP_EVENT_STARTUP.
336 *
337 * vr
338 * Virtual Router to change priority of
339 *
340 * priority
341 * New priority
342 */
343 void vrrp_set_priority(struct vrrp_vrouter *vr, uint8_t priority);
344
345 /*
346 * Set Advertisement Interval on this Virtual Router.
347 *
348 * vr
349 * Virtual Router to change priority of
350 *
351 * advertisement_interval
352 * New advertisement interval
353 */
354 void vrrp_set_advertisement_interval(struct vrrp_vrouter *vr,
355 uint16_t advertisement_interval);
356
357 /*
358 * Add an IPvX address to a VRRP Virtual Router.
359 *
360 * vr
361 * Virtual Router to add IPvx address to
362 *
363 * ip
364 * Address to add
365 *
366 * activate
367 * Whether to automatically start the VRRP router if this is the first IP
368 * address added.
369 *
370 * Returns:
371 * -1 on error
372 * 0 otherwise
373 */
374 int vrrp_add_ip(struct vrrp_vrouter *vr, struct ipaddr *ip);
375
376 /*
377 * Add an IPv4 address to a VRRP Virtual Router.
378 *
379 * vr
380 * Virtual Router to add IPv4 address to
381 *
382 * v4
383 * Address to add
384 *
385 * activate
386 * Whether to automatically start the VRRP router if this is the first IP
387 * address added.
388 *
389 * Returns:
390 * -1 on error
391 * 0 otherwise
392 */
393 int vrrp_add_ipv4(struct vrrp_vrouter *vr, struct in_addr v4);
394
395 /*
396 * Add an IPv6 address to a VRRP Virtual Router.
397 *
398 * vr
399 * Virtual Router to add IPv6 address to
400 *
401 * v6
402 * Address to add
403 *
404 * activate
405 * Whether to automatically start the VRRP router if this is the first IP
406 * address added.
407 *
408 * Returns:
409 * -1 on error
410 * 0 otherwise
411 */
412 int vrrp_add_ipv6(struct vrrp_vrouter *vr, struct in6_addr v6);
413
414 /*
415 * Remove an IP address from a VRRP Virtual Router.
416 *
417 * vr
418 * Virtual Router to remove IP address from
419 *
420 * ip
421 * Address to remove
422 *
423 * deactivate
424 * Whether to automatically stop the VRRP router if removing v4 would leave
425 * us with an empty address list. If this is not true and ip is the only IP
426 * address backed up by this virtual router, this function will not remove
427 * the address and return failure.
428 *
429 * Returns:
430 * -1 on error
431 * 0 otherwise
432 */
433 int vrrp_del_ip(struct vrrp_vrouter *vr, struct ipaddr *ip);
434
435 /*
436 * Remove an IPv4 address from a VRRP Virtual Router.
437 *
438 * vr
439 * Virtual Router to remove IPv4 address from
440 *
441 * v4
442 * Address to remove
443 *
444 * deactivate
445 * Whether to automatically stop the VRRP router if removing v4 would leave
446 * us with an empty address list. If this is not true and v4 is the only
447 * IPv4 address backed up by this virtual router, this function will not
448 * remove the address and return failure.
449 *
450 * Returns:
451 * -1 on error
452 * 0 otherwise
453 */
454 int vrrp_del_ipv4(struct vrrp_vrouter *vr, struct in_addr v4);
455
456 /*
457 * Remove an IPv6 address from a VRRP Virtual Router.
458 *
459 * vr
460 * Virtual Router to remove IPv6 address from
461 *
462 * v6
463 * Address to remove
464 *
465 * deactivate
466 * Whether to automatically stop the VRRP router if removing v5 would leave
467 * us with an empty address list. If this is not true and v4 is the only
468 * IPv6 address backed up by this virtual router, this function will not
469 * remove the address and return failure.
470 *
471 * Returns:
472 * -1 on error
473 * 0 otherwise
474 */
475 int vrrp_del_ipv6(struct vrrp_vrouter *vr, struct in6_addr v6);
476
477 /* State machine ----------------------------------------------------------- */
478
479 #define VRRP_STATE_INITIALIZE 0
480 #define VRRP_STATE_MASTER 1
481 #define VRRP_STATE_BACKUP 2
482 #define VRRP_EVENT_STARTUP 0
483 #define VRRP_EVENT_SHUTDOWN 1
484
485 extern const char *const vrrp_state_names[3];
486
487 /*
488 * This hook called whenever the state of a Virtual Router changes, after the
489 * specific internal state handlers have run.
490 *
491 * Use this if you need to react to state changes to perform non-critical
492 * tasks. Critical tasks should go in the internal state change handlers.
493 */
494 DECLARE_HOOK(vrrp_change_state_hook, (struct vrrp_router *r, int to), (r, to));
495
496 /*
497 * Trigger a VRRP event on a given Virtual Router..
498 *
499 * vr
500 * Virtual Router to operate on
501 *
502 * event
503 * Event to kick off. All event related processing will have completed upon
504 * return of this function.
505 *
506 * Returns:
507 * < 0 if the event created an error
508 * 0 otherwise
509 */
510 int vrrp_event(struct vrrp_router *r, int event);
511
512 /* Autoconfig -------------------------------------------------------------- */
513
514 /*
515 * Search for and automatically configure VRRP instances on interfaces.
516 *
517 * ifp
518 * Interface to autoconfig. If it is a macvlan interface and has a VRRP MAC,
519 * a VRRP instance corresponding to VMAC assigned to macvlan will be created
520 * on the parent interface and all addresses on the macvlan interface except
521 * the v6 link local will be configured as VRRP addresses. If NULL, this
522 * treatment will be applied to all existing interfaces matching the above
523 * criterion.
524 *
525 * Returns:
526 * -1 on failure
527 * 0 otherwise
528 */
529 int vrrp_autoconfig(void);
530
531 /*
532 * Enable autoconfiguration.
533 *
534 * Calling this function will cause vrrpd to automatically configure VRRP
535 * instances on existing compatible macvlan interfaces. These instances will
536 * react to interface up/down and address add/delete events to keep themselves
537 * in sync with the available interfaces.
538 *
539 * version
540 * VRRP version to use for autoconfigured instances. Must be 2 or 3.
541 */
542 void vrrp_autoconfig_on(int version);
543
544 /*
545 * Disable autoconfiguration.
546 *
547 * Calling this function will delete all existing autoconfigured VRRP instances.
548 */
549 void vrrp_autoconfig_off(void);
550
551 /* Interface Tracking ------------------------------------------------------ */
552
553 void vrrp_if_add(struct interface *ifp);
554 void vrrp_if_del(struct interface *ifp);
555 void vrrp_if_up(struct interface *ifp);
556 void vrrp_if_down(struct interface *ifp);
557 void vrrp_if_address_add(struct interface *ifp);
558 void vrrp_if_address_del(struct interface *ifp);
559
560 /* Other ------------------------------------------------------------------- */
561
562 /*
563 * Write global level configuration to vty.
564 *
565 * vty
566 * vty to write config to
567 *
568 * Returns:
569 * # of lines written
570 */
571 int vrrp_config_write_global(struct vty *vty);
572
573 /*
574 * Find VRRP Virtual Router by Virtual Router ID
575 */
576 struct vrrp_vrouter *vrrp_lookup(const struct interface *ifp, uint8_t vrid);
577
578 #endif /* __VRRP_H__ */