]> git.proxmox.com Git - mirror_frr.git/blame - nhrpd/nhrp_nhs.c
Merge pull request #7945 from volta-networks/feat_isis_snmp
[mirror_frr.git] / nhrpd / nhrp_nhs.c
CommitLineData
2fb975da
TT
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
10#include "zebra.h"
11#include "zbuf.h"
12#include "memory.h"
13#include "thread.h"
14#include "nhrpd.h"
15#include "nhrp_protocol.h"
16
819dc8bb
DL
17DEFINE_MTYPE_STATIC(NHRPD, NHRP_NHS, "NHRP next hop server")
18DEFINE_MTYPE_STATIC(NHRPD, NHRP_REGISTRATION, "NHRP registration entries")
19
2fb975da 20static int nhrp_nhs_resolve(struct thread *t);
2fb975da
TT
21static int nhrp_reg_send_req(struct thread *t);
22
23static void nhrp_reg_reply(struct nhrp_reqid *reqid, void *arg)
24{
25 struct nhrp_packet_parser *p = arg;
996c9314
LB
26 struct nhrp_registration *r =
27 container_of(reqid, struct nhrp_registration, reqid);
2fb975da
TT
28 struct nhrp_nhs *nhs = r->nhs;
29 struct interface *ifp = nhs->ifp;
30 struct nhrp_interface *nifp = ifp->info;
31 struct nhrp_extension_header *ext;
32 struct nhrp_cie_header *cie;
33 struct nhrp_cache *c;
34 struct zbuf extpl;
35 union sockunion cie_nbma, cie_proto, *proto;
2fb975da 36 int ok = 0, holdtime;
659fde26 37 unsigned short mtu = 0;
2fb975da
TT
38
39 nhrp_reqid_free(&nhrp_packet_reqid, &r->reqid);
40
41 if (p->hdr->type != NHRP_PACKET_REGISTRATION_REPLY) {
42 debugf(NHRP_DEBUG_COMMON, "NHS: Registration failed");
43 return;
44 }
45
46 debugf(NHRP_DEBUG_COMMON, "NHS: Reg.reply received");
47
48 ok = 1;
996c9314
LB
49 while ((cie = nhrp_cie_pull(&p->payload, p->hdr, &cie_nbma, &cie_proto))
50 != NULL) {
51 proto = sockunion_family(&cie_proto) != AF_UNSPEC
52 ? &cie_proto
53 : &p->src_proto;
b6c48481
DS
54 debugf(NHRP_DEBUG_COMMON, "NHS: CIE registration: %pSU: %d",
55 proto, cie->code);
996c9314
LB
56 if (!((cie->code == NHRP_CODE_SUCCESS)
57 || (cie->code == NHRP_CODE_ADMINISTRATIVELY_PROHIBITED
58 && nhs->hub)))
2fb975da 59 ok = 0;
659fde26
GG
60 mtu = ntohs(cie->mtu);
61 debugf(NHRP_DEBUG_COMMON, "NHS: CIE MTU: %d", mtu);
2fb975da
TT
62 }
63
64 if (!ok)
65 return;
66
67 /* Parse extensions */
68 sockunion_family(&nifp->nat_nbma) = AF_UNSPEC;
69 while ((ext = nhrp_ext_pull(&p->extensions, &extpl)) != NULL) {
70 switch (htons(ext->type) & ~NHRP_EXTENSION_FLAG_COMPULSORY) {
71 case NHRP_EXTENSION_NAT_ADDRESS:
72 /* NHS adds second CIE if NAT is detected */
996c9314
LB
73 if (nhrp_cie_pull(&extpl, p->hdr, &cie_nbma, &cie_proto)
74 && nhrp_cie_pull(&extpl, p->hdr, &cie_nbma,
75 &cie_proto)) {
2fb975da 76 nifp->nat_nbma = cie_nbma;
996c9314 77 debugf(NHRP_DEBUG_IF,
b6c48481
DS
78 "%s: NAT detected, real NBMA address: %pSU",
79 ifp->name, &nifp->nbma);
2fb975da
TT
80 }
81 break;
82 }
83 }
84
85 /* Success - schedule next registration, and route NHS */
86 r->timeout = 2;
87 holdtime = nifp->afi[nhs->afi].holdtime;
88 THREAD_OFF(r->t_register);
89
90 /* RFC 2332 5.2.3 - Registration is recommend to be renewed
91 * every one third of holdtime */
ffa2c898
QY
92 thread_add_timer(master, nhrp_reg_send_req, r, holdtime / 3,
93 &r->t_register);
2fb975da
TT
94
95 r->proto_addr = p->dst_proto;
96 c = nhrp_cache_get(ifp, &p->dst_proto, 1);
996c9314
LB
97 if (c)
98 nhrp_cache_update_binding(c, NHRP_CACHE_NHS, holdtime,
659fde26 99 nhrp_peer_ref(r->peer), mtu, NULL);
2fb975da
TT
100}
101
102static int nhrp_reg_timeout(struct thread *t)
103{
104 struct nhrp_registration *r = THREAD_ARG(t);
105 struct nhrp_cache *c;
106
107 r->t_register = NULL;
108
109 if (r->timeout >= 16 && sockunion_family(&r->proto_addr) != AF_UNSPEC) {
110 nhrp_reqid_free(&nhrp_packet_reqid, &r->reqid);
111 c = nhrp_cache_get(r->nhs->ifp, &r->proto_addr, 0);
996c9314
LB
112 if (c)
113 nhrp_cache_update_binding(c, NHRP_CACHE_NHS, -1, NULL,
114 0, NULL);
2fb975da
TT
115 sockunion_family(&r->proto_addr) = AF_UNSPEC;
116 }
117
118 r->timeout <<= 1;
996c9314
LB
119 if (r->timeout > 64)
120 r->timeout = 2;
121 thread_add_timer_msec(master, nhrp_reg_send_req, r, 10, &r->t_register);
2fb975da
TT
122
123 return 0;
124}
125
126static void nhrp_reg_peer_notify(struct notifier_block *n, unsigned long cmd)
127{
996c9314
LB
128 struct nhrp_registration *r =
129 container_of(n, struct nhrp_registration, peer_notifier);
2fb975da
TT
130
131 switch (cmd) {
132 case NOTIFY_PEER_UP:
133 case NOTIFY_PEER_DOWN:
134 case NOTIFY_PEER_IFCONFIG_CHANGED:
135 case NOTIFY_PEER_MTU_CHANGED:
b6c48481
DS
136 debugf(NHRP_DEBUG_COMMON, "NHS: Flush timer for %pSU",
137 &r->peer->vc->remote.nbma);
50478845 138 THREAD_OFF(r->t_register);
ffa2c898
QY
139 thread_add_timer_msec(master, nhrp_reg_send_req, r, 10,
140 &r->t_register);
2fb975da
TT
141 break;
142 }
143}
144
145static int nhrp_reg_send_req(struct thread *t)
146{
147 struct nhrp_registration *r = THREAD_ARG(t);
148 struct nhrp_nhs *nhs = r->nhs;
149 char buf1[SU_ADDRSTRLEN], buf2[SU_ADDRSTRLEN];
150 struct interface *ifp = nhs->ifp;
151 struct nhrp_interface *nifp = ifp->info;
152 struct nhrp_afi_data *if_ad = &nifp->afi[nhs->afi];
153 union sockunion *dst_proto;
154 struct zbuf *zb;
155 struct nhrp_packet_header *hdr;
156 struct nhrp_extension_header *ext;
157 struct nhrp_cie_header *cie;
158
159 r->t_register = NULL;
160 if (!nhrp_peer_check(r->peer, 2)) {
b6c48481
DS
161 debugf(NHRP_DEBUG_COMMON, "NHS: Waiting link for %pSU",
162 &r->peer->vc->remote.nbma);
ffa2c898
QY
163 thread_add_timer(master, nhrp_reg_send_req, r, 120,
164 &r->t_register);
2fb975da
TT
165 return 0;
166 }
167
ffa2c898
QY
168 thread_add_timer(master, nhrp_reg_timeout, r, r->timeout,
169 &r->t_register);
2fb975da
TT
170
171 /* RFC2332 5.2.3 NHC uses it's own address as dst if NHS is unknown */
172 dst_proto = &nhs->proto_addr;
173 if (sockunion_family(dst_proto) == AF_UNSPEC)
174 dst_proto = &if_ad->addr;
175
176 sockunion2str(&if_ad->addr, buf1, sizeof(buf1));
177 sockunion2str(dst_proto, buf2, sizeof(buf2));
996c9314
LB
178 debugf(NHRP_DEBUG_COMMON, "NHS: Register %s -> %s (timeout %d)", buf1,
179 buf2, r->timeout);
2fb975da
TT
180
181 /* No protocol address configured for tunnel interface */
182 if (sockunion_family(&if_ad->addr) == AF_UNSPEC)
183 return 0;
184
185 zb = zbuf_alloc(1400);
996c9314
LB
186 hdr = nhrp_packet_push(zb, NHRP_PACKET_REGISTRATION_REQUEST,
187 &nifp->nbma, &if_ad->addr, dst_proto);
ef9329ac 188 hdr->hop_count = 1;
2fb975da
TT
189 if (!(if_ad->flags & NHRP_IFF_REG_NO_UNIQUE))
190 hdr->flags |= htons(NHRP_FLAG_REGISTRATION_UNIQUE);
191
996c9314
LB
192 hdr->u.request_id = htonl(nhrp_reqid_alloc(&nhrp_packet_reqid,
193 &r->reqid, nhrp_reg_reply));
2fb975da
TT
194
195 /* FIXME: push CIE for each local protocol address */
196 cie = nhrp_cie_push(zb, NHRP_CODE_SUCCESS, NULL, NULL);
f7f9a377
AL
197 /* RFC2332 5.2.1 if unique is set then prefix length must be 0xff */
198 cie->prefix_length = (if_ad->flags & NHRP_IFF_REG_NO_UNIQUE) ? 8 * sockunion_get_addrlen(dst_proto) : 0xff;
2fb975da
TT
199 cie->holding_time = htons(if_ad->holdtime);
200 cie->mtu = htons(if_ad->mtu);
201
202 nhrp_ext_request(zb, hdr, ifp);
203
204 /* Cisco NAT detection extension */
205 hdr->flags |= htons(NHRP_FLAG_REGISTRATION_NAT);
206 ext = nhrp_ext_push(zb, hdr, NHRP_EXTENSION_NAT_ADDRESS);
207 cie = nhrp_cie_push(zb, NHRP_CODE_SUCCESS, &nifp->nbma, &if_ad->addr);
55fd6ee9 208 cie->prefix_length = 8 * sockunion_get_addrlen(&if_ad->addr);
2fb975da
TT
209 nhrp_ext_complete(zb, ext);
210
211 nhrp_packet_complete(zb, hdr);
212 nhrp_peer_send(r->peer, zb);
213 zbuf_free(zb);
214
215 return 0;
216}
217
218static void nhrp_reg_delete(struct nhrp_registration *r)
219{
220 nhrp_peer_notify_del(r->peer, &r->peer_notifier);
221 nhrp_peer_unref(r->peer);
222 list_del(&r->reglist_entry);
223 THREAD_OFF(r->t_register);
224 XFREE(MTYPE_NHRP_REGISTRATION, r);
225}
226
996c9314
LB
227static struct nhrp_registration *
228nhrp_reg_by_nbma(struct nhrp_nhs *nhs, const union sockunion *nbma_addr)
2fb975da
TT
229{
230 struct nhrp_registration *r;
231
996c9314
LB
232 list_for_each_entry(
233 r, &nhs->reglist_head,
234 reglist_entry) if (sockunion_same(&r->peer->vc->remote.nbma,
235 nbma_addr)) return r;
2fb975da
TT
236 return NULL;
237}
238
3286ca07
DL
239static void nhrp_nhs_resolve_cb(struct resolver_query *q, const char *errstr,
240 int n, union sockunion *addrs)
2fb975da
TT
241{
242 struct nhrp_nhs *nhs = container_of(q, struct nhrp_nhs, dns_resolve);
243 struct nhrp_interface *nifp = nhs->ifp->info;
244 struct nhrp_registration *reg, *regn;
245 int i;
246
247 nhs->t_resolve = NULL;
248 if (n < 0) {
249 /* Failed, retry in a moment */
ffa2c898
QY
250 thread_add_timer(master, nhrp_nhs_resolve, nhs, 5,
251 &nhs->t_resolve);
2fb975da
TT
252 return;
253 }
254
ffa2c898
QY
255 thread_add_timer(master, nhrp_nhs_resolve, nhs, 2 * 60 * 60,
256 &nhs->t_resolve);
2fb975da 257
996c9314
LB
258 list_for_each_entry(reg, &nhs->reglist_head, reglist_entry) reg->mark =
259 1;
2fb975da
TT
260
261 nhs->hub = 0;
262 for (i = 0; i < n; i++) {
263 if (sockunion_same(&addrs[i], &nifp->nbma)) {
264 nhs->hub = 1;
265 continue;
266 }
267
268 reg = nhrp_reg_by_nbma(nhs, &addrs[i]);
269 if (reg) {
270 reg->mark = 0;
271 continue;
272 }
273
274 reg = XCALLOC(MTYPE_NHRP_REGISTRATION, sizeof(*reg));
275 reg->peer = nhrp_peer_get(nhs->ifp, &addrs[i]);
276 reg->nhs = nhs;
277 reg->timeout = 1;
278 list_init(&reg->reglist_entry);
279 list_add_tail(&reg->reglist_entry, &nhs->reglist_head);
996c9314
LB
280 nhrp_peer_notify_add(reg->peer, &reg->peer_notifier,
281 nhrp_reg_peer_notify);
ffa2c898
QY
282 thread_add_timer_msec(master, nhrp_reg_send_req, reg, 50,
283 &reg->t_register);
2fb975da
TT
284 }
285
996c9314
LB
286 list_for_each_entry_safe(reg, regn, &nhs->reglist_head, reglist_entry)
287 {
2fb975da
TT
288 if (reg->mark)
289 nhrp_reg_delete(reg);
290 }
291}
292
293static int nhrp_nhs_resolve(struct thread *t)
294{
295 struct nhrp_nhs *nhs = THREAD_ARG(t);
296
996c9314
LB
297 resolver_resolve(&nhs->dns_resolve, AF_INET, nhs->nbma_fqdn,
298 nhrp_nhs_resolve_cb);
2fb975da
TT
299
300 return 0;
301}
302
996c9314
LB
303int nhrp_nhs_add(struct interface *ifp, afi_t afi, union sockunion *proto_addr,
304 const char *nbma_fqdn)
2fb975da
TT
305{
306 struct nhrp_interface *nifp = ifp->info;
307 struct nhrp_nhs *nhs;
308
996c9314
LB
309 if (sockunion_family(proto_addr) != AF_UNSPEC
310 && sockunion_family(proto_addr) != afi2family(afi))
2fb975da
TT
311 return NHRP_ERR_PROTOCOL_ADDRESS_MISMATCH;
312
996c9314
LB
313 list_for_each_entry(nhs, &nifp->afi[afi].nhslist_head, nhslist_entry)
314 {
315 if (sockunion_family(&nhs->proto_addr) != AF_UNSPEC
316 && sockunion_family(proto_addr) != AF_UNSPEC
317 && sockunion_same(&nhs->proto_addr, proto_addr))
2fb975da
TT
318 return NHRP_ERR_ENTRY_EXISTS;
319
320 if (strcmp(nhs->nbma_fqdn, nbma_fqdn) == 0)
321 return NHRP_ERR_ENTRY_EXISTS;
322 }
323
324 nhs = XMALLOC(MTYPE_NHRP_NHS, sizeof(struct nhrp_nhs));
2fb975da 325
996c9314 326 *nhs = (struct nhrp_nhs){
2fb975da
TT
327 .afi = afi,
328 .ifp = ifp,
329 .proto_addr = *proto_addr,
330 .nbma_fqdn = strdup(nbma_fqdn),
331 .reglist_head = LIST_INITIALIZER(nhs->reglist_head),
332 };
333 list_add_tail(&nhs->nhslist_entry, &nifp->afi[afi].nhslist_head);
ffa2c898
QY
334 thread_add_timer_msec(master, nhrp_nhs_resolve, nhs, 1000,
335 &nhs->t_resolve);
2fb975da
TT
336
337 return NHRP_OK;
338}
339
996c9314
LB
340int nhrp_nhs_del(struct interface *ifp, afi_t afi, union sockunion *proto_addr,
341 const char *nbma_fqdn)
2fb975da
TT
342{
343 struct nhrp_interface *nifp = ifp->info;
344 struct nhrp_nhs *nhs, *nnhs;
345 int ret = NHRP_ERR_ENTRY_NOT_FOUND;
346
996c9314
LB
347 if (sockunion_family(proto_addr) != AF_UNSPEC
348 && sockunion_family(proto_addr) != afi2family(afi))
2fb975da
TT
349 return NHRP_ERR_PROTOCOL_ADDRESS_MISMATCH;
350
996c9314
LB
351 list_for_each_entry_safe(nhs, nnhs, &nifp->afi[afi].nhslist_head,
352 nhslist_entry)
353 {
2fb975da
TT
354 if (!sockunion_same(&nhs->proto_addr, proto_addr))
355 continue;
356 if (strcmp(nhs->nbma_fqdn, nbma_fqdn) != 0)
357 continue;
358
359 nhrp_nhs_free(nhs);
360 ret = NHRP_OK;
361 }
362
363 return ret;
364}
365
366int nhrp_nhs_free(struct nhrp_nhs *nhs)
367{
368 struct nhrp_registration *r, *rn;
369
370 list_for_each_entry_safe(r, rn, &nhs->reglist_head, reglist_entry)
371 nhrp_reg_delete(r);
372 THREAD_OFF(nhs->t_resolve);
373 list_del(&nhs->nhslist_entry);
996c9314 374 free((void *)nhs->nbma_fqdn);
2fb975da
TT
375 XFREE(MTYPE_NHRP_NHS, nhs);
376 return 0;
377}
378
ee72f0a0
RD
379void nhrp_nhs_interface_del(struct interface *ifp)
380{
381 struct nhrp_interface *nifp = ifp->info;
382 struct nhrp_nhs *nhs, *tmp;
383 afi_t afi;
384
385 for (afi = 0; afi < AFI_MAX; afi++) {
386 debugf(NHRP_DEBUG_COMMON, "Cleaning up nhs entries (%d)",
387 !list_empty(&nifp->afi[afi].nhslist_head));
388
389 list_for_each_entry_safe(nhs, tmp, &nifp->afi[afi].nhslist_head,
390 nhslist_entry)
391 {
392 nhrp_nhs_free(nhs);
393 }
394 }
395}
396
2fb975da
TT
397void nhrp_nhs_terminate(void)
398{
f4e14fdb 399 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
2fb975da
TT
400 struct interface *ifp;
401 struct nhrp_interface *nifp;
402 struct nhrp_nhs *nhs, *tmp;
2fb975da
TT
403 afi_t afi;
404
451fda4f 405 FOR_ALL_INTERFACES (vrf, ifp) {
2fb975da
TT
406 nifp = ifp->info;
407 for (afi = 0; afi < AFI_MAX; afi++) {
996c9314
LB
408 list_for_each_entry_safe(
409 nhs, tmp, &nifp->afi[afi].nhslist_head,
410 nhslist_entry) nhrp_nhs_free(nhs);
2fb975da
TT
411 }
412 }
413}
0ca036b4 414
996c9314
LB
415void nhrp_nhs_foreach(struct interface *ifp, afi_t afi,
416 void (*cb)(struct nhrp_nhs *, struct nhrp_registration *,
417 void *),
418 void *ctx)
0ca036b4
TT
419{
420 struct nhrp_interface *nifp = ifp->info;
421 struct nhrp_nhs *nhs;
422 struct nhrp_registration *reg;
423
996c9314
LB
424 list_for_each_entry(nhs, &nifp->afi[afi].nhslist_head, nhslist_entry)
425 {
0ca036b4 426 if (!list_empty(&nhs->reglist_head)) {
996c9314
LB
427 list_for_each_entry(reg, &nhs->reglist_head,
428 reglist_entry) cb(nhs, reg, ctx);
0ca036b4
TT
429 } else
430 cb(nhs, 0, ctx);
431 }
432}