Import Upstream version 1.2.2
[quagga-debian.git] / nhrpd / nhrpd.h
1 /* NHRP daemon internal structures and function prototypes
2  * Copyright (c) 2014-2015 Timo Teräs
3  *
4  * This file is free software: you may copy, redistribute and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  */
9
10 #ifndef NHRPD_H
11 #define NHRPD_H
12
13 #include "list.h"
14
15 #include "zbuf.h"
16 #include "zclient.h"
17 #include "debug.h"
18
19 #define NHRPD_DEFAULT_HOLDTIME  7200
20
21 #define NHRP_VTY_PORT           2612
22 #define NHRP_DEFAULT_CONFIG     "nhrpd.conf"
23
24 extern struct thread_master *master;
25
26 enum {
27         NHRP_OK = 0,
28         NHRP_ERR_FAIL,
29         NHRP_ERR_NO_MEMORY,
30         NHRP_ERR_UNSUPPORTED_INTERFACE,
31         NHRP_ERR_NHRP_NOT_ENABLED,
32         NHRP_ERR_ENTRY_EXISTS,
33         NHRP_ERR_ENTRY_NOT_FOUND,
34         NHRP_ERR_PROTOCOL_ADDRESS_MISMATCH,
35 };
36
37 struct notifier_block;
38
39 typedef void (*notifier_fn_t)(struct notifier_block *, unsigned long);
40
41 struct notifier_block {
42         struct list_head notifier_entry;
43         notifier_fn_t action;
44 };
45
46 struct notifier_list {
47         struct list_head notifier_head;
48 };
49
50 #define NOTIFIER_LIST_INITIALIZER(l) \
51         { .notifier_head = LIST_INITIALIZER((l)->notifier_head) }
52
53 static inline void notifier_init(struct notifier_list *l)
54 {
55         list_init(&l->notifier_head);
56 }
57
58 static inline void notifier_add(struct notifier_block *n, struct notifier_list *l, notifier_fn_t action)
59 {
60         n->action = action;
61         list_add_tail(&n->notifier_entry, &l->notifier_head);
62 }
63
64 static inline void notifier_del(struct notifier_block *n)
65 {
66         list_del(&n->notifier_entry);
67 }
68
69 static inline void notifier_call(struct notifier_list *l, int cmd)
70 {
71         struct notifier_block *n, *nn;
72         list_for_each_entry_safe(n, nn, &l->notifier_head, notifier_entry)
73                 n->action(n, cmd);
74 }
75
76 static inline int notifier_active(struct notifier_list *l)
77 {
78         return !list_empty(&l->notifier_head);
79 }
80
81 struct resolver_query {
82         void (*callback)(struct resolver_query *, int n, union sockunion *);
83 };
84
85 void resolver_init(void);
86 void resolver_resolve(struct resolver_query *query, int af, const char *hostname, void (*cb)(struct resolver_query *, int, union sockunion *));
87
88 void nhrp_zebra_init(void);
89 void nhrp_zebra_terminate(void);
90
91 struct zbuf;
92 struct nhrp_vc;
93 struct nhrp_cache;
94 struct nhrp_nhs;
95 struct nhrp_interface;
96
97 #define MAX_ID_LENGTH                   64
98 #define MAX_CERT_LENGTH                 2048
99
100 enum nhrp_notify_type {
101         NOTIFY_INTERFACE_UP,
102         NOTIFY_INTERFACE_DOWN,
103         NOTIFY_INTERFACE_CHANGED,
104         NOTIFY_INTERFACE_ADDRESS_CHANGED,
105         NOTIFY_INTERFACE_NBMA_CHANGED,
106         NOTIFY_INTERFACE_MTU_CHANGED,
107
108         NOTIFY_VC_IPSEC_CHANGED,
109         NOTIFY_VC_IPSEC_UPDATE_NBMA,
110
111         NOTIFY_PEER_UP,
112         NOTIFY_PEER_DOWN,
113         NOTIFY_PEER_IFCONFIG_CHANGED,
114         NOTIFY_PEER_MTU_CHANGED,
115         NOTIFY_PEER_NBMA_CHANGING,
116
117         NOTIFY_CACHE_UP,
118         NOTIFY_CACHE_DOWN,
119         NOTIFY_CACHE_DELETE,
120         NOTIFY_CACHE_USED,
121         NOTIFY_CACHE_BINDING_CHANGE,
122 };
123
124 struct nhrp_vc {
125         struct notifier_list notifier_list;
126         uint8_t ipsec;
127         uint8_t updating;
128         uint8_t abort_migration;
129
130         struct nhrp_vc_peer {
131                 union sockunion nbma;
132                 char id[MAX_ID_LENGTH];
133                 uint16_t certlen;
134                 uint8_t cert[MAX_CERT_LENGTH];
135         } local, remote;
136 };
137
138 enum nhrp_route_type {
139         NHRP_ROUTE_BLACKHOLE,
140         NHRP_ROUTE_LOCAL,
141         NHRP_ROUTE_NBMA_NEXTHOP,
142         NHRP_ROUTE_OFF_NBMA,
143 };
144
145 struct nhrp_peer {
146         unsigned int ref;
147         unsigned online : 1;
148         unsigned requested : 1;
149         unsigned fallback_requested : 1;
150         unsigned prio : 1;
151         struct notifier_list notifier_list;
152         struct interface *ifp;
153         struct nhrp_vc *vc;
154         struct thread *t_fallback;
155         struct notifier_block vc_notifier, ifp_notifier;
156 };
157
158 struct nhrp_packet_parser {
159         struct interface *ifp;
160         struct nhrp_afi_data *if_ad;
161         struct nhrp_peer *peer;
162         struct zbuf *pkt;
163         struct zbuf payload;
164         struct zbuf extensions;
165         struct nhrp_packet_header *hdr;
166         enum nhrp_route_type route_type;
167         struct prefix route_prefix;
168         union sockunion src_nbma, src_proto, dst_proto;
169 };
170
171 struct nhrp_reqid_pool {
172         struct hash *reqid_hash;
173         uint32_t next_request_id;
174 };
175
176 struct nhrp_reqid {
177         uint32_t request_id;
178         void (*cb)(struct nhrp_reqid *, void *);
179 };
180
181 extern struct nhrp_reqid_pool nhrp_packet_reqid;
182 extern struct nhrp_reqid_pool nhrp_event_reqid;
183
184 enum nhrp_cache_type {
185         NHRP_CACHE_INVALID = 0,
186         NHRP_CACHE_INCOMPLETE,
187         NHRP_CACHE_NEGATIVE,
188         NHRP_CACHE_CACHED,
189         NHRP_CACHE_DYNAMIC,
190         NHRP_CACHE_NHS,
191         NHRP_CACHE_STATIC,
192         NHRP_CACHE_LOCAL,
193         NHRP_CACHE_NUM_TYPES
194 };
195
196 extern const char * const nhrp_cache_type_str[];
197 extern unsigned long nhrp_cache_counts[NHRP_CACHE_NUM_TYPES];
198
199 struct nhrp_cache {
200         struct interface *ifp;
201         union sockunion remote_addr;
202
203         unsigned map : 1;
204         unsigned used : 1;
205         unsigned route_installed : 1;
206         unsigned nhrp_route_installed : 1;
207
208         struct notifier_block peer_notifier;
209         struct notifier_block newpeer_notifier;
210         struct notifier_list notifier_list;
211         struct nhrp_reqid eventid;
212         struct thread *t_timeout;
213         struct thread *t_auth;
214
215         struct {
216                 enum nhrp_cache_type type;
217                 union sockunion remote_nbma_natoa;
218                 struct nhrp_peer *peer;
219                 time_t expires;
220                 uint32_t mtu;
221         } cur, new;
222 };
223
224 struct nhrp_shortcut {
225         struct prefix *p;
226         union sockunion addr;
227
228         struct nhrp_reqid reqid;
229         struct thread *t_timer;
230
231         enum nhrp_cache_type type;
232         unsigned int holding_time;
233         unsigned route_installed : 1;
234         unsigned expiring : 1;
235
236         struct nhrp_cache *cache;
237         struct notifier_block cache_notifier;
238 };
239
240 struct nhrp_nhs {
241         struct interface *ifp;
242         struct list_head nhslist_entry;
243
244         unsigned hub : 1;
245         afi_t afi;
246         union sockunion proto_addr;
247         const char *nbma_fqdn;                  /* IP-address or FQDN */
248
249         struct thread *t_resolve;
250         struct resolver_query dns_resolve;
251         struct list_head reglist_head;
252 };
253
254 struct nhrp_registration {
255         struct list_head reglist_entry;
256         struct thread *t_register;
257         struct nhrp_nhs *nhs;
258         struct nhrp_reqid reqid;
259         unsigned int timeout;
260         unsigned mark : 1;
261         union sockunion proto_addr;
262         struct nhrp_peer *peer;
263         struct notifier_block peer_notifier;
264 };
265
266 #define NHRP_IFF_SHORTCUT               0x0001
267 #define NHRP_IFF_REDIRECT               0x0002
268 #define NHRP_IFF_REG_NO_UNIQUE          0x0100
269
270 struct nhrp_interface {
271         struct interface *ifp;
272
273         unsigned enabled : 1;
274
275         char *ipsec_profile, *ipsec_fallback_profile, *source;
276         union sockunion nbma;
277         union sockunion nat_nbma;
278         unsigned int linkidx;
279         uint32_t grekey;
280
281         struct hash *peer_hash;
282         struct hash *cache_hash;
283
284         struct notifier_list notifier_list;
285
286         struct interface *nbmaifp;
287         struct notifier_block nbmanifp_notifier;
288
289         struct nhrp_afi_data {
290                 unsigned flags;
291                 unsigned short configured : 1;
292                 union sockunion addr;
293                 uint32_t network_id;
294                 short configured_mtu;
295                 unsigned short mtu;
296                 unsigned int holdtime;
297                 struct list_head nhslist_head;
298         } afi[AFI_MAX];
299 };
300
301 int sock_open_unix(const char *path);
302
303 void nhrp_interface_init(void);
304 void nhrp_interface_update(struct interface *ifp);
305 void nhrp_interface_update_mtu(struct interface *ifp, afi_t afi);
306
307 int nhrp_interface_add(int cmd, struct zclient *client, zebra_size_t length, vrf_id_t vrf_id);
308 int nhrp_interface_delete(int cmd, struct zclient *client, zebra_size_t length, vrf_id_t vrf_id);
309 int nhrp_interface_up(int cmd, struct zclient *client, zebra_size_t length, vrf_id_t vrf_id);
310 int nhrp_interface_down(int cmd, struct zclient *client, zebra_size_t length, vrf_id_t vrf_id);
311 int nhrp_interface_address_add(int cmd, struct zclient *client, zebra_size_t length, vrf_id_t vrf_id);
312 int nhrp_interface_address_delete(int cmd, struct zclient *client, zebra_size_t length, vrf_id_t vrf_id);
313
314 void nhrp_interface_notify_add(struct interface *ifp, struct notifier_block *n, notifier_fn_t fn);
315 void nhrp_interface_notify_del(struct interface *ifp, struct notifier_block *n);
316 void nhrp_interface_set_protection(struct interface *ifp, const char *profile, const char *fallback_profile);
317 void nhrp_interface_set_source(struct interface *ifp, const char *ifname);
318
319 int nhrp_nhs_add(struct interface *ifp, afi_t afi, union sockunion *proto_addr, const char *nbma_fqdn);
320 int nhrp_nhs_del(struct interface *ifp, afi_t afi, union sockunion *proto_addr, const char *nbma_fqdn);
321 int nhrp_nhs_free(struct nhrp_nhs *nhs);
322 void nhrp_nhs_terminate(void);
323 void nhrp_nhs_foreach(struct interface *ifp, afi_t afi, void (*cb)(struct nhrp_nhs *, struct nhrp_registration *, void *), void *ctx);
324
325 void nhrp_route_update_nhrp(const struct prefix *p, struct interface *ifp);
326 void nhrp_route_announce(int add, enum nhrp_cache_type type, const struct prefix *p, struct interface *ifp, const union sockunion *nexthop, uint32_t mtu);
327 int nhrp_route_read(int command, struct zclient *zclient, zebra_size_t length, vrf_id_t vrf_id);
328 int nhrp_route_get_nexthop(const union sockunion *addr, struct prefix *p, union sockunion *via, struct interface **ifp);
329 enum nhrp_route_type nhrp_route_address(struct interface *in_ifp, union sockunion *addr, struct prefix *p, struct nhrp_peer **peer);
330
331 void nhrp_config_init(void);
332
333 void nhrp_shortcut_init(void);
334 void nhrp_shortcut_terminate(void);
335 void nhrp_shortcut_initiate(union sockunion *addr);
336 void nhrp_shortcut_foreach(afi_t afi, void (*cb)(struct nhrp_shortcut *, void *), void *ctx);
337 void nhrp_shortcut_purge(struct nhrp_shortcut *s, int force);
338 void nhrp_shortcut_prefix_change(const struct prefix *p, int deleted);
339
340 struct nhrp_cache *nhrp_cache_get(struct interface *ifp, union sockunion *remote_addr, int create);
341 void nhrp_cache_foreach(struct interface *ifp, void (*cb)(struct nhrp_cache *, void *), void *ctx);
342 void nhrp_cache_set_used(struct nhrp_cache *, int);
343 int nhrp_cache_update_binding(struct nhrp_cache *, enum nhrp_cache_type type, int holding_time, struct nhrp_peer *p, uint32_t mtu, union sockunion *nbma_natoa);
344 void nhrp_cache_notify_add(struct nhrp_cache *c, struct notifier_block *, notifier_fn_t);
345 void nhrp_cache_notify_del(struct nhrp_cache *c, struct notifier_block *);
346
347 void nhrp_vc_init(void);
348 void nhrp_vc_terminate(void);
349 struct nhrp_vc *nhrp_vc_get(const union sockunion *src, const union sockunion *dst, int create);
350 int nhrp_vc_ipsec_updown(uint32_t child_id, struct nhrp_vc *vc);
351 void nhrp_vc_notify_add(struct nhrp_vc *, struct notifier_block *, notifier_fn_t);
352 void nhrp_vc_notify_del(struct nhrp_vc *, struct notifier_block *);
353 void nhrp_vc_foreach(void (*cb)(struct nhrp_vc *, void *), void *ctx);
354 void nhrp_vc_reset(void);
355
356 void vici_init(void);
357 void vici_terminate(void);
358 void vici_request_vc(const char *profile, union sockunion *src, union sockunion *dst, int prio);
359
360 extern const char *nhrp_event_socket_path;
361
362 void evmgr_init(void);
363 void evmgr_terminate(void);
364 void evmgr_set_socket(const char *socket);
365 void evmgr_notify(const char *name, struct nhrp_cache *c, void (*cb)(struct nhrp_reqid *, void *));
366
367 struct nhrp_packet_header *nhrp_packet_push(
368         struct zbuf *zb, uint8_t type,
369         const union sockunion *src_nbma,
370         const union sockunion *src_proto,
371         const union sockunion *dst_proto);
372 void nhrp_packet_complete(struct zbuf *zb, struct nhrp_packet_header *hdr);
373 uint16_t nhrp_packet_calculate_checksum(const uint8_t *pdu, uint16_t len);
374
375 struct nhrp_packet_header *nhrp_packet_pull(
376         struct zbuf *zb,
377         union sockunion *src_nbma,
378         union sockunion *src_proto,
379         union sockunion *dst_proto);
380
381 struct nhrp_cie_header *nhrp_cie_push(
382         struct zbuf *zb, uint8_t code,
383         const union sockunion *nbma,
384         const union sockunion *proto);
385 struct nhrp_cie_header *nhrp_cie_pull(
386         struct zbuf *zb,
387         struct nhrp_packet_header *hdr,
388         union sockunion *nbma,
389         union sockunion *proto);
390
391 struct nhrp_extension_header *nhrp_ext_push(struct zbuf *zb, struct nhrp_packet_header *hdr, uint16_t type);
392 void nhrp_ext_complete(struct zbuf *zb, struct nhrp_extension_header *ext);
393 struct nhrp_extension_header *nhrp_ext_pull(struct zbuf *zb, struct zbuf *payload);
394 void nhrp_ext_request(struct zbuf *zb, struct nhrp_packet_header *hdr, struct interface *);
395 int nhrp_ext_reply(struct zbuf *zb, struct nhrp_packet_header *hdr, struct interface *ifp, struct nhrp_extension_header *ext, struct zbuf *extpayload);
396
397 uint32_t nhrp_reqid_alloc(struct nhrp_reqid_pool *, struct nhrp_reqid *r, void (*cb)(struct nhrp_reqid *, void *));
398 void nhrp_reqid_free(struct nhrp_reqid_pool *, struct nhrp_reqid *r);
399 struct nhrp_reqid *nhrp_reqid_lookup(struct nhrp_reqid_pool *, uint32_t reqid);
400
401 int nhrp_packet_init(void);
402
403 struct nhrp_peer *nhrp_peer_get(struct interface *ifp, const union sockunion *remote_nbma);
404 struct nhrp_peer *nhrp_peer_ref(struct nhrp_peer *p);
405 void nhrp_peer_unref(struct nhrp_peer *p);
406 int nhrp_peer_check(struct nhrp_peer *p, int establish);
407 void nhrp_peer_notify_add(struct nhrp_peer *p, struct notifier_block *, notifier_fn_t);
408 void nhrp_peer_notify_del(struct nhrp_peer *p, struct notifier_block *);
409 void nhrp_peer_recv(struct nhrp_peer *p, struct zbuf *zb);
410 void nhrp_peer_send(struct nhrp_peer *p, struct zbuf *zb);
411 void nhrp_peer_send_indication(struct interface *ifp, uint16_t, struct zbuf *);
412
413 #endif