]> git.proxmox.com Git - mirror_frr.git/blame - nhrpd/nhrp_interface.c
nhrpd: Add Claimed NBMA field in sh ip nhrp cache output
[mirror_frr.git] / nhrpd / nhrp_interface.c
CommitLineData
2fb975da
TT
1/* NHRP interface
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
b45ac5f5
DL
10#ifdef HAVE_CONFIG_H
11#include "config.h"
12#endif
13
2fb975da
TT
14#include <net/if_arp.h>
15#include "zebra.h"
16#include "linklist.h"
17#include "memory.h"
18#include "thread.h"
19
20#include "nhrpd.h"
21#include "os.h"
22#include "netlink.h"
23
819dc8bb
DL
24DEFINE_MTYPE_STATIC(NHRPD, NHRP_IF, "NHRP interface")
25
fef2ed13
PG
26static void nhrp_interface_update_cache_config(struct interface *ifp,
27 bool available,
28 uint8_t family);
29
2fb975da
TT
30static int nhrp_if_new_hook(struct interface *ifp)
31{
32 struct nhrp_interface *nifp;
33 afi_t afi;
34
35 nifp = XCALLOC(MTYPE_NHRP_IF, sizeof(struct nhrp_interface));
2fb975da
TT
36
37 ifp->info = nifp;
38 nifp->ifp = ifp;
39
40 notifier_init(&nifp->notifier_list);
41 for (afi = 0; afi < AFI_MAX; afi++) {
42 struct nhrp_afi_data *ad = &nifp->afi[afi];
43 ad->holdtime = NHRPD_DEFAULT_HOLDTIME;
44 list_init(&ad->nhslist_head);
45 }
46
47 return 0;
48}
49
50static int nhrp_if_delete_hook(struct interface *ifp)
51{
ee72f0a0
RD
52 struct nhrp_interface *nifp = ifp->info;
53
54 debugf(NHRP_DEBUG_IF, "Deleted interface (%s)", ifp->name);
55
56 nhrp_cache_interface_del(ifp);
57 nhrp_nhs_interface_del(ifp);
58 nhrp_peer_interface_del(ifp);
59
60 if (nifp->ipsec_profile)
61 free(nifp->ipsec_profile);
62 if (nifp->ipsec_fallback_profile)
63 free(nifp->ipsec_fallback_profile);
64 if (nifp->source)
65 free(nifp->source);
66
2fb975da
TT
67 XFREE(MTYPE_NHRP_IF, ifp->info);
68 return 0;
69}
70
71void nhrp_interface_init(void)
72{
ce19a04a
DL
73 hook_register_prio(if_add, 0, nhrp_if_new_hook);
74 hook_register_prio(if_del, 0, nhrp_if_delete_hook);
2fb975da
TT
75}
76
77void nhrp_interface_update_mtu(struct interface *ifp, afi_t afi)
78{
79 struct nhrp_interface *nifp = ifp->info;
80 struct nhrp_afi_data *if_ad = &nifp->afi[afi];
81 unsigned short new_mtu;
82
83 if (if_ad->configured_mtu < 0)
84 new_mtu = nifp->nbmaifp ? nifp->nbmaifp->mtu : 0;
85 else
86 new_mtu = if_ad->configured_mtu;
87 if (new_mtu >= 1500)
88 new_mtu = 0;
89
90 if (new_mtu != if_ad->mtu) {
996c9314
LB
91 debugf(NHRP_DEBUG_IF, "%s: MTU changed to %d", ifp->name,
92 new_mtu);
2fb975da 93 if_ad->mtu = new_mtu;
996c9314
LB
94 notifier_call(&nifp->notifier_list,
95 NOTIFY_INTERFACE_MTU_CHANGED);
2fb975da
TT
96 }
97}
98
99static void nhrp_interface_update_source(struct interface *ifp)
100{
101 struct nhrp_interface *nifp = ifp->info;
102
996c9314
LB
103 if (!nifp->source || !nifp->nbmaifp
104 || (ifindex_t)nifp->linkidx == nifp->nbmaifp->ifindex)
2fb975da
TT
105 return;
106
107 nifp->linkidx = nifp->nbmaifp->ifindex;
996c9314
LB
108 debugf(NHRP_DEBUG_IF, "%s: bound device index changed to %d", ifp->name,
109 nifp->linkidx);
2fb975da
TT
110 netlink_gre_set_link(ifp->ifindex, nifp->linkidx);
111}
112
996c9314
LB
113static void nhrp_interface_interface_notifier(struct notifier_block *n,
114 unsigned long cmd)
2fb975da 115{
996c9314
LB
116 struct nhrp_interface *nifp =
117 container_of(n, struct nhrp_interface, nbmanifp_notifier);
2fb975da
TT
118 struct interface *nbmaifp = nifp->nbmaifp;
119 struct nhrp_interface *nbmanifp = nbmaifp->info;
2fb975da
TT
120
121 switch (cmd) {
122 case NOTIFY_INTERFACE_CHANGED:
123 nhrp_interface_update_mtu(nifp->ifp, AFI_IP);
124 nhrp_interface_update_source(nifp->ifp);
125 break;
126 case NOTIFY_INTERFACE_ADDRESS_CHANGED:
127 nifp->nbma = nbmanifp->afi[AFI_IP].addr;
128 nhrp_interface_update(nifp->ifp);
996c9314
LB
129 notifier_call(&nifp->notifier_list,
130 NOTIFY_INTERFACE_NBMA_CHANGED);
b6c48481
DS
131 debugf(NHRP_DEBUG_IF, "%s: NBMA change: address %pSU",
132 nifp->ifp->name, &nifp->nbma);
2fb975da
TT
133 break;
134 }
135}
136
137static void nhrp_interface_update_nbma(struct interface *ifp)
138{
139 struct nhrp_interface *nifp = ifp->info, *nbmanifp = NULL;
140 struct interface *nbmaifp = NULL;
141 union sockunion nbma;
142
143 sockunion_family(&nbma) = AF_UNSPEC;
144
145 if (nifp->source)
a36898e7 146 nbmaifp = if_lookup_by_name(nifp->source, VRF_DEFAULT);
2fb975da
TT
147
148 switch (ifp->ll_type) {
149 case ZEBRA_LLT_IPGRE: {
996c9314
LB
150 struct in_addr saddr = {0};
151 netlink_gre_get_info(ifp->ifindex, &nifp->grekey,
152 &nifp->linkidx, &saddr);
153 debugf(NHRP_DEBUG_IF, "%s: GRE: %x %x %x", ifp->name,
154 nifp->grekey, nifp->linkidx, saddr.s_addr);
975a328e 155 if (saddr.s_addr != INADDR_ANY)
d7c0a89a 156 sockunion_set(&nbma, AF_INET, (uint8_t *)&saddr.s_addr,
996c9314
LB
157 sizeof(saddr.s_addr));
158 else if (!nbmaifp && nifp->linkidx != IFINDEX_INTERNAL)
159 nbmaifp =
160 if_lookup_by_index(nifp->linkidx, VRF_DEFAULT);
161 } break;
2fb975da
TT
162 default:
163 break;
164 }
165
166 if (nbmaifp)
167 nbmanifp = nbmaifp->info;
168
169 if (nbmaifp != nifp->nbmaifp) {
170 if (nifp->nbmaifp)
171 notifier_del(&nifp->nbmanifp_notifier);
172 nifp->nbmaifp = nbmaifp;
173 if (nbmaifp) {
996c9314
LB
174 notifier_add(&nifp->nbmanifp_notifier,
175 &nbmanifp->notifier_list,
176 nhrp_interface_interface_notifier);
177 debugf(NHRP_DEBUG_IF, "%s: bound to %s", ifp->name,
178 nbmaifp->name);
2fb975da
TT
179 }
180 }
181
182 if (nbmaifp) {
183 if (sockunion_family(&nbma) == AF_UNSPEC)
184 nbma = nbmanifp->afi[AFI_IP].addr;
185 nhrp_interface_update_mtu(ifp, AFI_IP);
186 nhrp_interface_update_source(ifp);
187 }
188
189 if (!sockunion_same(&nbma, &nifp->nbma)) {
190 nifp->nbma = nbma;
191 nhrp_interface_update(nifp->ifp);
192 debugf(NHRP_DEBUG_IF, "%s: NBMA address changed", ifp->name);
996c9314
LB
193 notifier_call(&nifp->notifier_list,
194 NOTIFY_INTERFACE_NBMA_CHANGED);
2fb975da
TT
195 }
196
197 nhrp_interface_update(ifp);
198}
199
996c9314
LB
200static void nhrp_interface_update_address(struct interface *ifp, afi_t afi,
201 int force)
2fb975da
TT
202{
203 const int family = afi2family(afi);
204 struct nhrp_interface *nifp = ifp->info;
205 struct nhrp_afi_data *if_ad = &nifp->afi[afi];
206 struct nhrp_cache *nc;
207 struct connected *c, *best;
208 struct listnode *cnode;
209 union sockunion addr;
210 char buf[PREFIX_STRLEN];
211
212 /* Select new best match preferring primary address */
213 best = NULL;
214 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
215 if (PREFIX_FAMILY(c->address) != family)
216 continue;
217 if (best == NULL) {
218 best = c;
219 continue;
220 }
996c9314
LB
221 if ((best->flags & ZEBRA_IFA_SECONDARY)
222 && !(c->flags & ZEBRA_IFA_SECONDARY)) {
2fb975da
TT
223 best = c;
224 continue;
225 }
996c9314
LB
226 if (!(best->flags & ZEBRA_IFA_SECONDARY)
227 && (c->flags & ZEBRA_IFA_SECONDARY))
2fb975da
TT
228 continue;
229 if (best->address->prefixlen > c->address->prefixlen) {
230 best = c;
231 continue;
232 }
233 if (best->address->prefixlen < c->address->prefixlen)
234 continue;
235 }
236
237 /* On NHRP interfaces a host prefix is required */
996c9314
LB
238 if (best && if_ad->configured
239 && best->address->prefixlen != 8 * prefix_blen(best->address)) {
2dbe669b
DA
240 zlog_notice("%s: %pFX is not a host prefix", ifp->name,
241 best->address);
2fb975da
TT
242 best = NULL;
243 }
244
245 /* Update address if it changed */
246 if (best)
247 prefix2sockunion(best->address, &addr);
248 else
249 memset(&addr, 0, sizeof(addr));
250
251 if (!force && sockunion_same(&if_ad->addr, &addr))
252 return;
253
254 if (sockunion_family(&if_ad->addr) != AF_UNSPEC) {
255 nc = nhrp_cache_get(ifp, &if_ad->addr, 0);
996c9314
LB
256 if (nc)
257 nhrp_cache_update_binding(nc, NHRP_CACHE_LOCAL, -1,
85365e51 258 NULL, 0, NULL, NULL);
2fb975da
TT
259 }
260
996c9314
LB
261 debugf(NHRP_DEBUG_KERNEL, "%s: IPv%d address changed to %s", ifp->name,
262 afi == AFI_IP ? 4 : 6,
0d6f7fd6 263 best ? prefix2str(best->address, buf, sizeof(buf)) : "(none)");
2fb975da
TT
264 if_ad->addr = addr;
265
266 if (if_ad->configured && sockunion_family(&if_ad->addr) != AF_UNSPEC) {
267 nc = nhrp_cache_get(ifp, &addr, 1);
996c9314
LB
268 if (nc)
269 nhrp_cache_update_binding(nc, NHRP_CACHE_LOCAL, 0, NULL,
85365e51 270 0, NULL, NULL);
2fb975da
TT
271 }
272
273 notifier_call(&nifp->notifier_list, NOTIFY_INTERFACE_ADDRESS_CHANGED);
274}
275
276void nhrp_interface_update(struct interface *ifp)
277{
278 struct nhrp_interface *nifp = ifp->info;
279 struct nhrp_afi_data *if_ad;
280 afi_t afi;
281 int enabled = 0;
282
283 notifier_call(&nifp->notifier_list, NOTIFY_INTERFACE_CHANGED);
284
285 for (afi = 0; afi < AFI_MAX; afi++) {
286 if_ad = &nifp->afi[afi];
287
996c9314
LB
288 if (sockunion_family(&nifp->nbma) == AF_UNSPEC
289 || ifp->ifindex == IFINDEX_INTERNAL || !if_is_up(ifp)
290 || !if_ad->network_id) {
2fb975da
TT
291 if (if_ad->configured) {
292 if_ad->configured = 0;
293 nhrp_interface_update_address(ifp, afi, 1);
294 }
295 continue;
296 }
297
298 if (!if_ad->configured) {
996c9314
LB
299 os_configure_dmvpn(ifp->ifindex, ifp->name,
300 afi2family(afi));
2fb975da
TT
301 if_ad->configured = 1;
302 nhrp_interface_update_address(ifp, afi, 1);
303 }
304
305 enabled = 1;
306 }
307
308 if (enabled != nifp->enabled) {
309 nifp->enabled = enabled;
996c9314
LB
310 notifier_call(&nifp->notifier_list,
311 enabled ? NOTIFY_INTERFACE_UP
312 : NOTIFY_INTERFACE_DOWN);
2fb975da
TT
313 }
314}
315
ef7bd2a3 316int nhrp_ifp_create(struct interface *ifp)
2fb975da 317{
2fb975da 318 debugf(NHRP_DEBUG_IF, "if-add: %s, ifindex: %u, hw_type: %d %s",
996c9314
LB
319 ifp->name, ifp->ifindex, ifp->ll_type,
320 if_link_type_str(ifp->ll_type));
2fb975da
TT
321
322 nhrp_interface_update_nbma(ifp);
323
324 return 0;
325}
326
3c3c3252 327int nhrp_ifp_destroy(struct interface *ifp)
2fb975da 328{
2fb975da 329 debugf(NHRP_DEBUG_IF, "if-delete: %s", ifp->name);
9d6c33ea 330
fef2ed13
PG
331 nhrp_interface_update_cache_config(ifp, false, AF_INET);
332 nhrp_interface_update_cache_config(ifp, false, AF_INET6);
2fb975da 333 nhrp_interface_update(ifp);
9d6c33ea 334
2fb975da
TT
335 return 0;
336}
337
fef2ed13
PG
338struct map_ctx {
339 int family;
340 bool enabled;
341};
342
e5773617
MS
343static void interface_config_update_nhrp_map(struct nhrp_cache_config *cc,
344 void *data)
fef2ed13
PG
345{
346 struct map_ctx *ctx = data;
347 struct interface *ifp = cc->ifp;
348 struct nhrp_cache *c;
349 union sockunion nbma_addr;
350
351 if (sockunion_family(&cc->remote_addr) != ctx->family)
352 return;
353
354 /* gre layer not ready */
355 if (ifp->vrf_id == VRF_UNKNOWN)
356 return;
357
358 c = nhrp_cache_get(ifp, &cc->remote_addr, ctx->enabled ? 1 : 0);
359 if (!c && !ctx->enabled)
360 return;
e5773617 361
fef2ed13
PG
362 /* suppress */
363 if (!ctx->enabled) {
364 if (c && c->map) {
e5773617
MS
365 nhrp_cache_update_binding(
366 c, c->cur.type, -1,
85365e51 367 nhrp_peer_get(ifp, &nbma_addr), 0, NULL, NULL);
fef2ed13
PG
368 }
369 return;
370 }
e5773617
MS
371
372 /* Newly created */
373 assert(c != NULL);
374
fef2ed13
PG
375 c->map = 1;
376 if (cc->type == NHRP_CACHE_LOCAL)
377 nhrp_cache_update_binding(c, NHRP_CACHE_LOCAL, 0, NULL, 0,
85365e51 378 NULL, NULL);
fef2ed13
PG
379 else {
380 nhrp_cache_update_binding(c, NHRP_CACHE_STATIC, 0,
381 nhrp_peer_get(ifp, &cc->nbma), 0,
85365e51 382 NULL, NULL);
fef2ed13
PG
383 }
384}
385
386static void nhrp_interface_update_cache_config(struct interface *ifp, bool available, uint8_t family)
387{
388 struct map_ctx mapctx;
389
390 mapctx = (struct map_ctx){
391 .family = family,
392 .enabled = available
393 };
394 nhrp_cache_config_foreach(ifp, interface_config_update_nhrp_map,
395 &mapctx);
396
397}
398
ddbf3e60 399int nhrp_ifp_up(struct interface *ifp)
2fb975da 400{
2fb975da
TT
401 debugf(NHRP_DEBUG_IF, "if-up: %s", ifp->name);
402 nhrp_interface_update_nbma(ifp);
403
404 return 0;
405}
406
b0b69e59 407int nhrp_ifp_down(struct interface *ifp)
2fb975da 408{
2fb975da
TT
409 debugf(NHRP_DEBUG_IF, "if-down: %s", ifp->name);
410 nhrp_interface_update(ifp);
b0b69e59 411
2fb975da
TT
412 return 0;
413}
414
121f9dee 415int nhrp_interface_address_add(ZAPI_CALLBACK_ARGS)
2fb975da
TT
416{
417 struct connected *ifc;
2fb975da 418
121f9dee 419 ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
2fb975da
TT
420 if (ifc == NULL)
421 return 0;
422
2dbe669b
DA
423 debugf(NHRP_DEBUG_IF, "if-addr-add: %s: %pFX", ifc->ifp->name,
424 ifc->address);
2fb975da 425
996c9314
LB
426 nhrp_interface_update_address(
427 ifc->ifp, family2afi(PREFIX_FAMILY(ifc->address)), 0);
fef2ed13 428 nhrp_interface_update_cache_config(ifc->ifp, true, PREFIX_FAMILY(ifc->address));
2fb975da
TT
429 return 0;
430}
431
121f9dee 432int nhrp_interface_address_delete(ZAPI_CALLBACK_ARGS)
2fb975da
TT
433{
434 struct connected *ifc;
2fb975da 435
121f9dee 436 ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
2fb975da
TT
437 if (ifc == NULL)
438 return 0;
439
2dbe669b
DA
440 debugf(NHRP_DEBUG_IF, "if-addr-del: %s: %pFX", ifc->ifp->name,
441 ifc->address);
2fb975da 442
996c9314
LB
443 nhrp_interface_update_address(
444 ifc->ifp, family2afi(PREFIX_FAMILY(ifc->address)), 0);
721c0857 445 connected_free(&ifc);
2fb975da
TT
446
447 return 0;
448}
449
996c9314
LB
450void nhrp_interface_notify_add(struct interface *ifp, struct notifier_block *n,
451 notifier_fn_t fn)
2fb975da
TT
452{
453 struct nhrp_interface *nifp = ifp->info;
454 notifier_add(n, &nifp->notifier_list, fn);
455}
456
457void nhrp_interface_notify_del(struct interface *ifp, struct notifier_block *n)
458{
459 notifier_del(n);
460}
461
996c9314
LB
462void nhrp_interface_set_protection(struct interface *ifp, const char *profile,
463 const char *fallback_profile)
2fb975da
TT
464{
465 struct nhrp_interface *nifp = ifp->info;
466
74e5ba3a 467 if (nifp->ipsec_profile) {
083bbfae 468 vici_terminate_vc_by_profile_name(nifp->ipsec_profile);
58ef1668 469 nhrp_vc_reset();
996c9314 470 free(nifp->ipsec_profile);
083bbfae 471 }
2fb975da
TT
472 nifp->ipsec_profile = profile ? strdup(profile) : NULL;
473
74e5ba3a 474 if (nifp->ipsec_fallback_profile) {
083bbfae 475 vici_terminate_vc_by_profile_name(nifp->ipsec_fallback_profile);
58ef1668 476 nhrp_vc_reset();
996c9314 477 free(nifp->ipsec_fallback_profile);
083bbfae 478 }
996c9314
LB
479 nifp->ipsec_fallback_profile =
480 fallback_profile ? strdup(fallback_profile) : NULL;
111aec1e 481
58ef1668 482 notifier_call(&nifp->notifier_list, NOTIFY_INTERFACE_IPSEC_CHANGED);
2fb975da
TT
483}
484
485void nhrp_interface_set_source(struct interface *ifp, const char *ifname)
486{
487 struct nhrp_interface *nifp = ifp->info;
488
996c9314
LB
489 if (nifp->source)
490 free(nifp->source);
2fb975da
TT
491 nifp->source = ifname ? strdup(ifname) : NULL;
492
493 nhrp_interface_update_nbma(ifp);
494}