]> git.proxmox.com Git - mirror_frr.git/blob - vrrpd/vrrp.h
vrrpd: add initial macvlan support
[mirror_frr.git] / vrrpd / vrrp.h
1 /*
2 * VRRPD global definitions and state machine
3 * Copyright (C) 2018 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/hash.h"
27 #include "lib/hook.h"
28 #include "lib/if.h"
29 #include "lib/linklist.h"
30 #include "lib/privs.h"
31 #include "lib/stream.h"
32 #include "lib/thread.h"
33
34 /* Global definitions */
35 #define VRRP_DEFAULT_ADVINT 100
36 #define VRRP_DEFAULT_PRIORITY 100
37 #define VRRP_PRIO_MASTER 255
38 #define VRRP_MCASTV4_GROUP_STR "224.0.0.18"
39 #define VRRP_MCASTV6_GROUP_STR "ff02:0:0:0:0:0:0:12"
40 #define VRRP_MCASTV4_GROUP 0xe0000012
41 #define VRRP_MCASTV6_GROUP 0xff020000000000000000000000000012
42 #define IPPROTO_VRRP 112
43
44 #define VRRP_LOGPFX_VRID "[VRID: %u] "
45
46 /* threadmaster */
47 extern struct thread_master *master;
48
49 /* privileges */
50 extern struct zebra_privs_t vrrp_privs;
51
52 /* Global hash of all Virtual Routers */
53 struct hash *vrrp_vrouters_hash;
54
55 /*
56 * VRRP Router.
57 *
58 * This struct contains all state for a particular VRRP Router operating in a
59 * Virtual Router for either IPv4 or IPv6.
60 */
61 struct vrrp_router {
62 /*
63 * Whether this VRRP Router is active.
64 */
65 bool is_active;
66
67 /* Rx socket: Rx from parent of mvl_ifp */
68 int sock_rx;
69 /* Tx socket; Tx from mvl_ifp */
70 int sock_tx;
71
72 /* macvlan interface */
73 struct interface *mvl_ifp;
74
75 /* Socket read buffer */
76 uint8_t ibuf[IP_MAXPACKET];
77
78 /*
79 * Address family of this Virtual Router.
80 * Either AF_INET or AF_INET6.
81 */
82 int family;
83
84 /*
85 * Virtual Router this VRRP Router is participating in.
86 */
87 struct vrrp_vrouter *vr;
88
89 /*
90 * One or more IPvX addresses associated with this Virtual
91 * Router. The first address must be the "primary" address this
92 * Virtual Router is backing up in the case of IPv4. In the case of
93 * IPv6 it must be the link-local address of vr->ifp.
94 *
95 * Type: struct ipaddr *
96 */
97 struct list *addrs;
98
99 /*
100 * Effective priority
101 * => vr->priority if we are Backup
102 * => 255 if we are Master
103 */
104 uint8_t priority;
105
106 /*
107 * Advertisement interval contained in ADVERTISEMENTS received from the
108 * Master (centiseconds)
109 */
110 uint16_t master_adver_interval;
111
112 /*
113 * Time to skew Master_Down_Interval in centiseconds. Calculated as:
114 * (((256 - priority) * Master_Adver_Interval) / 256)
115 */
116 uint16_t skew_time;
117
118 /*
119 * Time interval for Backup to declare Master down (centiseconds).
120 * Calculated as:
121 * (3 * Master_Adver_Interval) + Skew_time
122 */
123 uint16_t master_down_interval;
124
125 /*
126 * The MAC address used for the source MAC address in VRRP
127 * advertisements, advertised in ARP requests/responses, and advertised
128 * in ND Neighbor Advertisements.
129 */
130 struct ethaddr vmac;
131
132 struct {
133 int state;
134 } fsm;
135
136 struct thread *t_master_down_timer;
137 struct thread *t_adver_timer;
138 struct thread *t_read;
139 struct thread *t_write;
140 };
141
142 /*
143 * VRRP Virtual Router.
144 *
145 * This struct contains all state and configuration for a given Virtual Router
146 * Identifier on a given interface, both v4 and v6.
147 *
148 * RFC5798 s. 1 states:
149 * "Within a VRRP router, the virtual routers in each of the IPv4 and IPv6
150 * address families are a domain unto themselves and do not overlap."
151 *
152 * This implementation has chosen the tuple (interface, VRID) as the key for a
153 * particular VRRP Router, and the rest of the program is designed around this
154 * assumption. Additionally, base protocol configuration parameters such as the
155 * advertisement interval and (configured) priority are shared between v4 and
156 * v6 instances. This corresponds to the choice made by other industrial
157 * implementations.
158 */
159 struct vrrp_vrouter {
160 /* Interface */
161 struct interface *ifp;
162
163 /* Version */
164 uint8_t version;
165
166 /* Virtual Router Identifier */
167 uint32_t vrid;
168
169 /* Configured priority */
170 uint8_t priority;
171
172 /*
173 * Time interval between ADVERTISEMENTS (centiseconds). Default is 100
174 * centiseconds (1 second).
175 */
176 uint16_t advertisement_interval;
177
178 /*
179 * Controls whether a (starting or restarting) higher-priority Backup
180 * router preempts a lower-priority Master router. Values are True to
181 * allow preemption and False to prohibit preemption. Default is True.
182 */
183 bool preempt_mode;
184
185 /*
186 * Controls whether a virtual router in Master state will accept
187 * packets addressed to the address owner's IPvX address as its own if
188 * it is not the IPvX address owner. The default is False.
189 */
190 bool accept_mode;
191
192 struct vrrp_router *v4;
193 struct vrrp_router *v6;
194 };
195
196 /*
197 * Initialize VRRP global datastructures.
198 */
199 void vrrp_init(void);
200
201
202 /* Creation and destruction ------------------------------------------------ */
203
204 /*
205 * Create and register a new VRRP Virtual Router.
206 *
207 * ifp
208 * Base interface to configure VRRP on
209 *
210 * vrid
211 * Virtual Router Identifier
212 */
213 struct vrrp_vrouter *vrrp_vrouter_create(struct interface *ifp, uint8_t vrid);
214
215 /*
216 * Destroy a VRRP Virtual Router.
217 */
218 void vrrp_vrouter_destroy(struct vrrp_vrouter *vr);
219
220
221 /* Configuration controllers ----------------------------------------------- */
222
223 /*
224 * Change the configured priority of a VRRP Virtual Router.
225 *
226 * Note that this only changes the configured priority of the Virtual Router.
227 * The currently effective priority will not be changed; to change the
228 * effective priority, the Virtual Router must be restarted by issuing a
229 * VRRP_EVENT_SHUTDOWN followed by a VRRP_EVENT_STARTUP.
230 *
231 * vr
232 * Virtual Router to change priority of
233 *
234 * priority
235 * New priority
236 */
237 void vrrp_set_priority(struct vrrp_vrouter *vr, uint8_t priority);
238
239 /*
240 * Set Advertisement Interval on this Virtual Router.
241 *
242 * vr
243 * Virtual Router to change priority of
244 *
245 * advertisement_interval
246 * New advertisement interval
247 */
248 void vrrp_set_advertisement_interval(struct vrrp_vrouter *vr,
249 uint16_t advertisement_interval);
250
251 /*
252 * Add an IPvX address to a VRRP Virtual Router.
253 *
254 * vr
255 * Virtual Router to add IPvx address to
256 *
257 * ip
258 * Address to add
259 */
260 void vrrp_add_ip(struct vrrp_vrouter *vr, struct ipaddr ip);
261
262 /*
263 * Add an IPv4 address to a VRRP Virtual Router.
264 *
265 * vr
266 * Virtual Router to add IPv4 address to
267 *
268 * v4
269 * Address to add
270 */
271 void vrrp_add_ipv4(struct vrrp_vrouter *vr, struct in_addr v4);
272
273 /*
274 * Add an IPv6 address to a VRRP Virtual Router.
275 *
276 * vr
277 * Virtual Router to add IPv6 address to
278 *
279 * v6
280 * Address to add
281 */
282 void vrrp_add_ipv6(struct vrrp_vrouter *vr, struct in6_addr v6);
283
284
285 /* State machine ----------------------------------------------------------- */
286
287 #define VRRP_STATE_INITIALIZE 0
288 #define VRRP_STATE_MASTER 1
289 #define VRRP_STATE_BACKUP 2
290 #define VRRP_EVENT_STARTUP 0
291 #define VRRP_EVENT_SHUTDOWN 1
292
293 extern const char *vrrp_state_names[3];
294 extern const char *vrrp_event_names[2];
295
296 /*
297 * This hook called whenever the state of a Virtual Router changes, after the
298 * specific internal state handlers have run.
299 *
300 * Use this if you need to react to state changes to perform non-critical
301 * tasks. Critical tasks should go in the internal state change handlers.
302 */
303 DECLARE_HOOK(vrrp_change_state_hook, (struct vrrp_router * r, int to), (r, to));
304
305 /*
306 * Trigger a VRRP event on a given Virtual Router..
307 *
308 * vr
309 * Virtual Router to operate on
310 *
311 * event
312 * Event to kick off. All event related processing will have completed upon
313 * return of this function.
314 *
315 * Returns:
316 * < 0 if the event created an error
317 * 0 otherwise
318 */
319 int vrrp_event(struct vrrp_router *r, int event);
320
321
322 /* Other ------------------------------------------------------------------- */
323
324 /*
325 * Find VRRP Virtual Router by Virtual Router ID
326 */
327 struct vrrp_vrouter *vrrp_lookup(uint8_t vrid);
328
329 #endif /* _VRRP_H */