nhrp_nhs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* NHRP NHC nexthop server functions (registration)
  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. #include "zebra.h"
  10. #include "zbuf.h"
  11. #include "memory.h"
  12. #include "thread.h"
  13. #include "nhrpd.h"
  14. #include "nhrp_protocol.h"
  15. static int nhrp_nhs_resolve(struct thread *t);
  16. struct nhrp_registration {
  17. struct list_head reglist_entry;
  18. struct thread *t_register;
  19. struct nhrp_nhs *nhs;
  20. struct nhrp_reqid reqid;
  21. unsigned int timeout;
  22. unsigned mark : 1;
  23. union sockunion proto_addr;
  24. struct nhrp_peer *peer;
  25. struct notifier_block peer_notifier;
  26. };
  27. static int nhrp_reg_send_req(struct thread *t);
  28. static void nhrp_reg_reply(struct nhrp_reqid *reqid, void *arg)
  29. {
  30. struct nhrp_packet_parser *p = arg;
  31. struct nhrp_registration *r = container_of(reqid, struct nhrp_registration, reqid);
  32. struct nhrp_nhs *nhs = r->nhs;
  33. struct interface *ifp = nhs->ifp;
  34. struct nhrp_interface *nifp = ifp->info;
  35. struct nhrp_extension_header *ext;
  36. struct nhrp_cie_header *cie;
  37. struct nhrp_cache *c;
  38. struct zbuf extpl;
  39. union sockunion cie_nbma, cie_proto, *proto;
  40. char buf[64];
  41. int ok = 0, holdtime;
  42. nhrp_reqid_free(&nhrp_packet_reqid, &r->reqid);
  43. if (p->hdr->type != NHRP_PACKET_REGISTRATION_REPLY) {
  44. debugf(NHRP_DEBUG_COMMON, "NHS: Registration failed");
  45. return;
  46. }
  47. debugf(NHRP_DEBUG_COMMON, "NHS: Reg.reply received");
  48. ok = 1;
  49. while ((cie = nhrp_cie_pull(&p->payload, p->hdr, &cie_nbma, &cie_proto)) != NULL) {
  50. proto = sockunion_family(&cie_proto) != AF_UNSPEC ? &cie_proto : &p->src_proto;
  51. debugf(NHRP_DEBUG_COMMON, "NHS: CIE registration: %s: %d",
  52. sockunion2str(proto, buf, sizeof(buf)),
  53. cie->code);
  54. if (!((cie->code == NHRP_CODE_SUCCESS) ||
  55. (cie->code == NHRP_CODE_ADMINISTRATIVELY_PROHIBITED && nhs->hub)))
  56. ok = 0;
  57. }
  58. if (!ok)
  59. return;
  60. /* Parse extensions */
  61. sockunion_family(&nifp->nat_nbma) = AF_UNSPEC;
  62. while ((ext = nhrp_ext_pull(&p->extensions, &extpl)) != NULL) {
  63. switch (htons(ext->type) & ~NHRP_EXTENSION_FLAG_COMPULSORY) {
  64. case NHRP_EXTENSION_NAT_ADDRESS:
  65. /* NHS adds second CIE if NAT is detected */
  66. if (nhrp_cie_pull(&extpl, p->hdr, &cie_nbma, &cie_proto) &&
  67. nhrp_cie_pull(&extpl, p->hdr, &cie_nbma, &cie_proto)) {
  68. nifp->nat_nbma = cie_nbma;
  69. debugf(NHRP_DEBUG_IF, "%s: NAT detected, real NBMA address: %s",
  70. ifp->name, sockunion2str(&nifp->nbma, buf, sizeof(buf)));
  71. }
  72. break;
  73. }
  74. }
  75. /* Success - schedule next registration, and route NHS */
  76. r->timeout = 2;
  77. holdtime = nifp->afi[nhs->afi].holdtime;
  78. THREAD_OFF(r->t_register);
  79. /* RFC 2332 5.2.3 - Registration is recommend to be renewed
  80. * every one third of holdtime */
  81. THREAD_TIMER_ON(master, r->t_register, nhrp_reg_send_req, r, holdtime / 3);
  82. r->proto_addr = p->dst_proto;
  83. c = nhrp_cache_get(ifp, &p->dst_proto, 1);
  84. if (c) nhrp_cache_update_binding(c, NHRP_CACHE_NHS, holdtime, nhrp_peer_ref(r->peer), 0, NULL);
  85. }
  86. static int nhrp_reg_timeout(struct thread *t)
  87. {
  88. struct nhrp_registration *r = THREAD_ARG(t);
  89. struct nhrp_cache *c;
  90. r->t_register = NULL;
  91. if (r->timeout >= 16 && sockunion_family(&r->proto_addr) != AF_UNSPEC) {
  92. nhrp_reqid_free(&nhrp_packet_reqid, &r->reqid);
  93. c = nhrp_cache_get(r->nhs->ifp, &r->proto_addr, 0);
  94. if (c) nhrp_cache_update_binding(c, NHRP_CACHE_NHS, -1, NULL, 0, NULL);
  95. sockunion_family(&r->proto_addr) = AF_UNSPEC;
  96. }
  97. r->timeout <<= 1;
  98. if (r->timeout > 64) r->timeout = 2;
  99. THREAD_TIMER_MSEC_ON(master, r->t_register, nhrp_reg_send_req, r, 10);
  100. return 0;
  101. }
  102. static void nhrp_reg_peer_notify(struct notifier_block *n, unsigned long cmd)
  103. {
  104. struct nhrp_registration *r = container_of(n, struct nhrp_registration, peer_notifier);
  105. char buf[SU_ADDRSTRLEN];
  106. switch (cmd) {
  107. case NOTIFY_PEER_UP:
  108. case NOTIFY_PEER_DOWN:
  109. case NOTIFY_PEER_IFCONFIG_CHANGED:
  110. case NOTIFY_PEER_MTU_CHANGED:
  111. debugf(NHRP_DEBUG_COMMON, "NHS: Flush timer for %s",
  112. sockunion2str(&r->peer->vc->remote.nbma, buf, sizeof buf));
  113. THREAD_TIMER_OFF(r->t_register);
  114. THREAD_TIMER_MSEC_ON(master, r->t_register, nhrp_reg_send_req, r, 10);
  115. break;
  116. }
  117. }
  118. static int nhrp_reg_send_req(struct thread *t)
  119. {
  120. struct nhrp_registration *r = THREAD_ARG(t);
  121. struct nhrp_nhs *nhs = r->nhs;
  122. char buf1[SU_ADDRSTRLEN], buf2[SU_ADDRSTRLEN];
  123. struct interface *ifp = nhs->ifp;
  124. struct nhrp_interface *nifp = ifp->info;
  125. struct nhrp_afi_data *if_ad = &nifp->afi[nhs->afi];
  126. union sockunion *dst_proto;
  127. struct zbuf *zb;
  128. struct nhrp_packet_header *hdr;
  129. struct nhrp_extension_header *ext;
  130. struct nhrp_cie_header *cie;
  131. r->t_register = NULL;
  132. if (!nhrp_peer_check(r->peer, 2)) {
  133. debugf(NHRP_DEBUG_COMMON, "NHS: Waiting link for %s",
  134. sockunion2str(&r->peer->vc->remote.nbma, buf1, sizeof buf1));
  135. THREAD_TIMER_ON(master, r->t_register, nhrp_reg_send_req, r, 120);
  136. return 0;
  137. }
  138. THREAD_TIMER_ON(master, r->t_register, nhrp_reg_timeout, r, r->timeout);
  139. /* RFC2332 5.2.3 NHC uses it's own address as dst if NHS is unknown */
  140. dst_proto = &nhs->proto_addr;
  141. if (sockunion_family(dst_proto) == AF_UNSPEC)
  142. dst_proto = &if_ad->addr;
  143. sockunion2str(&if_ad->addr, buf1, sizeof(buf1));
  144. sockunion2str(dst_proto, buf2, sizeof(buf2));
  145. debugf(NHRP_DEBUG_COMMON, "NHS: Register %s -> %s (timeout %d)", buf1, buf2, r->timeout);
  146. /* No protocol address configured for tunnel interface */
  147. if (sockunion_family(&if_ad->addr) == AF_UNSPEC)
  148. return 0;
  149. zb = zbuf_alloc(1400);
  150. hdr = nhrp_packet_push(zb, NHRP_PACKET_REGISTRATION_REQUEST, &nifp->nbma, &if_ad->addr, dst_proto);
  151. hdr->hop_count = 0;
  152. if (!(if_ad->flags & NHRP_IFF_REG_NO_UNIQUE))
  153. hdr->flags |= htons(NHRP_FLAG_REGISTRATION_UNIQUE);
  154. hdr->u.request_id = htonl(nhrp_reqid_alloc(&nhrp_packet_reqid, &r->reqid, nhrp_reg_reply));
  155. /* FIXME: push CIE for each local protocol address */
  156. cie = nhrp_cie_push(zb, NHRP_CODE_SUCCESS, NULL, NULL);
  157. cie->prefix_length = 0xff;
  158. cie->holding_time = htons(if_ad->holdtime);
  159. cie->mtu = htons(if_ad->mtu);
  160. nhrp_ext_request(zb, hdr, ifp);
  161. /* Cisco NAT detection extension */
  162. hdr->flags |= htons(NHRP_FLAG_REGISTRATION_NAT);
  163. ext = nhrp_ext_push(zb, hdr, NHRP_EXTENSION_NAT_ADDRESS);
  164. cie = nhrp_cie_push(zb, NHRP_CODE_SUCCESS, &nifp->nbma, &if_ad->addr);
  165. cie->prefix_length = 8 * sockunion_get_addrlen(&nifp->nbma);
  166. nhrp_ext_complete(zb, ext);
  167. nhrp_packet_complete(zb, hdr);
  168. nhrp_peer_send(r->peer, zb);
  169. zbuf_free(zb);
  170. return 0;
  171. }
  172. static void nhrp_reg_delete(struct nhrp_registration *r)
  173. {
  174. nhrp_peer_notify_del(r->peer, &r->peer_notifier);
  175. nhrp_peer_unref(r->peer);
  176. list_del(&r->reglist_entry);
  177. THREAD_OFF(r->t_register);
  178. XFREE(MTYPE_NHRP_REGISTRATION, r);
  179. }
  180. static struct nhrp_registration *nhrp_reg_by_nbma(struct nhrp_nhs *nhs, const union sockunion *nbma_addr)
  181. {
  182. struct nhrp_registration *r;
  183. list_for_each_entry(r, &nhs->reglist_head, reglist_entry)
  184. if (sockunion_same(&r->peer->vc->remote.nbma, nbma_addr))
  185. return r;
  186. return NULL;
  187. }
  188. static void nhrp_nhs_resolve_cb(struct resolver_query *q, int n, union sockunion *addrs)
  189. {
  190. struct nhrp_nhs *nhs = container_of(q, struct nhrp_nhs, dns_resolve);
  191. struct nhrp_interface *nifp = nhs->ifp->info;
  192. struct nhrp_registration *reg, *regn;
  193. int i;
  194. nhs->t_resolve = NULL;
  195. if (n < 0) {
  196. /* Failed, retry in a moment */
  197. THREAD_TIMER_ON(master, nhs->t_resolve, nhrp_nhs_resolve, nhs, 5);
  198. return;
  199. }
  200. THREAD_TIMER_ON(master, nhs->t_resolve, nhrp_nhs_resolve, nhs, 2*60*60);
  201. list_for_each_entry(reg, &nhs->reglist_head, reglist_entry)
  202. reg->mark = 1;
  203. nhs->hub = 0;
  204. for (i = 0; i < n; i++) {
  205. if (sockunion_same(&addrs[i], &nifp->nbma)) {
  206. nhs->hub = 1;
  207. continue;
  208. }
  209. reg = nhrp_reg_by_nbma(nhs, &addrs[i]);
  210. if (reg) {
  211. reg->mark = 0;
  212. continue;
  213. }
  214. reg = XCALLOC(MTYPE_NHRP_REGISTRATION, sizeof(*reg));
  215. reg->peer = nhrp_peer_get(nhs->ifp, &addrs[i]);
  216. reg->nhs = nhs;
  217. reg->timeout = 1;
  218. list_init(&reg->reglist_entry);
  219. list_add_tail(&reg->reglist_entry, &nhs->reglist_head);
  220. nhrp_peer_notify_add(reg->peer, &reg->peer_notifier, nhrp_reg_peer_notify);
  221. THREAD_TIMER_MSEC_ON(master, reg->t_register, nhrp_reg_send_req, reg, 50);
  222. }
  223. list_for_each_entry_safe(reg, regn, &nhs->reglist_head, reglist_entry) {
  224. if (reg->mark)
  225. nhrp_reg_delete(reg);
  226. }
  227. }
  228. static int nhrp_nhs_resolve(struct thread *t)
  229. {
  230. struct nhrp_nhs *nhs = THREAD_ARG(t);
  231. resolver_resolve(&nhs->dns_resolve, AF_INET, nhs->nbma_fqdn, nhrp_nhs_resolve_cb);
  232. return 0;
  233. }
  234. int nhrp_nhs_add(struct interface *ifp, afi_t afi, union sockunion *proto_addr, const char *nbma_fqdn)
  235. {
  236. struct nhrp_interface *nifp = ifp->info;
  237. struct nhrp_nhs *nhs;
  238. if (sockunion_family(proto_addr) != AF_UNSPEC &&
  239. sockunion_family(proto_addr) != afi2family(afi))
  240. return NHRP_ERR_PROTOCOL_ADDRESS_MISMATCH;
  241. list_for_each_entry(nhs, &nifp->afi[afi].nhslist_head, nhslist_entry) {
  242. if (sockunion_family(&nhs->proto_addr) != AF_UNSPEC &&
  243. sockunion_family(proto_addr) != AF_UNSPEC &&
  244. sockunion_same(&nhs->proto_addr, proto_addr))
  245. return NHRP_ERR_ENTRY_EXISTS;
  246. if (strcmp(nhs->nbma_fqdn, nbma_fqdn) == 0)
  247. return NHRP_ERR_ENTRY_EXISTS;
  248. }
  249. nhs = XMALLOC(MTYPE_NHRP_NHS, sizeof(struct nhrp_nhs));
  250. if (!nhs) return NHRP_ERR_NO_MEMORY;
  251. *nhs = (struct nhrp_nhs) {
  252. .afi = afi,
  253. .ifp = ifp,
  254. .proto_addr = *proto_addr,
  255. .nbma_fqdn = strdup(nbma_fqdn),
  256. .reglist_head = LIST_INITIALIZER(nhs->reglist_head),
  257. };
  258. list_add_tail(&nhs->nhslist_entry, &nifp->afi[afi].nhslist_head);
  259. THREAD_TIMER_MSEC_ON(master, nhs->t_resolve, nhrp_nhs_resolve, nhs, 1000);
  260. return NHRP_OK;
  261. }
  262. int nhrp_nhs_del(struct interface *ifp, afi_t afi, union sockunion *proto_addr, const char *nbma_fqdn)
  263. {
  264. struct nhrp_interface *nifp = ifp->info;
  265. struct nhrp_nhs *nhs, *nnhs;
  266. int ret = NHRP_ERR_ENTRY_NOT_FOUND;
  267. if (sockunion_family(proto_addr) != AF_UNSPEC &&
  268. sockunion_family(proto_addr) != afi2family(afi))
  269. return NHRP_ERR_PROTOCOL_ADDRESS_MISMATCH;
  270. list_for_each_entry_safe(nhs, nnhs, &nifp->afi[afi].nhslist_head, nhslist_entry) {
  271. if (!sockunion_same(&nhs->proto_addr, proto_addr))
  272. continue;
  273. if (strcmp(nhs->nbma_fqdn, nbma_fqdn) != 0)
  274. continue;
  275. nhrp_nhs_free(nhs);
  276. ret = NHRP_OK;
  277. }
  278. return ret;
  279. }
  280. int nhrp_nhs_free(struct nhrp_nhs *nhs)
  281. {
  282. struct nhrp_registration *r, *rn;
  283. list_for_each_entry_safe(r, rn, &nhs->reglist_head, reglist_entry)
  284. nhrp_reg_delete(r);
  285. THREAD_OFF(nhs->t_resolve);
  286. list_del(&nhs->nhslist_entry);
  287. free((void*) nhs->nbma_fqdn);
  288. XFREE(MTYPE_NHRP_NHS, nhs);
  289. return 0;
  290. }
  291. void nhrp_nhs_terminate(void)
  292. {
  293. struct interface *ifp;
  294. struct nhrp_interface *nifp;
  295. struct nhrp_nhs *nhs, *tmp;
  296. struct listnode *node;
  297. afi_t afi;
  298. for (ALL_LIST_ELEMENTS_RO(iflist, node, ifp)) {
  299. nifp = ifp->info;
  300. for (afi = 0; afi < AFI_MAX; afi++) {
  301. list_for_each_entry_safe(nhs, tmp, &nifp->afi[afi].nhslist_head, nhslist_entry)
  302. nhrp_nhs_free(nhs);
  303. }
  304. }
  305. }