nhrpd.h 12 KB

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