]> git.proxmox.com Git - mirror_frr.git/blob - nhrpd/nhrp_interface.c
Merge pull request #11867 from sri-mohan1/sri-ospf-dbg1
[mirror_frr.git] / nhrpd / nhrp_interface.c
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
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
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 "hash.h"
23
24 DEFINE_MTYPE_STATIC(NHRPD, NHRP_IF, "NHRP interface");
25 DEFINE_MTYPE_STATIC(NHRPD, NHRP_IF_GRE, "NHRP GRE interface");
26
27 struct hash *nhrp_gre_list;
28
29 static void nhrp_interface_update_cache_config(struct interface *ifp,
30 bool available,
31 uint8_t family);
32
33 static unsigned int nhrp_gre_info_key(const void *data)
34 {
35 const struct nhrp_gre_info *r = data;
36
37 return r->ifindex;
38 }
39
40 static bool nhrp_gre_info_cmp(const void *data, const void *key)
41 {
42 const struct nhrp_gre_info *a = data, *b = key;
43
44 if (a->ifindex == b->ifindex)
45 return true;
46 return false;
47 }
48
49 static void *nhrp_interface_gre_alloc(void *data)
50 {
51 struct nhrp_gre_info *a;
52 struct nhrp_gre_info *b = data;
53
54 a = XMALLOC(MTYPE_NHRP_IF_GRE, sizeof(struct nhrp_gre_info));
55 memcpy(a, b, sizeof(struct nhrp_gre_info));
56 return a;
57 }
58
59 struct nhrp_gre_info *nhrp_gre_info_alloc(struct nhrp_gre_info *p)
60 {
61 struct nhrp_gre_info *a;
62
63 a = (struct nhrp_gre_info *)hash_get(nhrp_gre_list, p,
64 nhrp_interface_gre_alloc);
65 return a;
66 }
67
68 static int nhrp_if_new_hook(struct interface *ifp)
69 {
70 struct nhrp_interface *nifp;
71 afi_t afi;
72
73 nifp = XCALLOC(MTYPE_NHRP_IF, sizeof(struct nhrp_interface));
74
75 ifp->info = nifp;
76 nifp->ifp = ifp;
77
78 notifier_init(&nifp->notifier_list);
79 for (afi = 0; afi < AFI_MAX; afi++) {
80 struct nhrp_afi_data *ad = &nifp->afi[afi];
81 ad->holdtime = NHRPD_DEFAULT_HOLDTIME;
82 nhrp_nhslist_init(&ad->nhslist_head);
83 nhrp_mcastlist_init(&ad->mcastlist_head);
84 }
85
86 return 0;
87 }
88
89 static int nhrp_if_delete_hook(struct interface *ifp)
90 {
91 struct nhrp_interface *nifp = ifp->info;
92
93 debugf(NHRP_DEBUG_IF, "Deleted interface (%s)", ifp->name);
94
95 nhrp_cache_interface_del(ifp);
96 nhrp_nhs_interface_del(ifp);
97 nhrp_multicast_interface_del(ifp);
98 nhrp_peer_interface_del(ifp);
99
100 if (nifp->ipsec_profile)
101 free(nifp->ipsec_profile);
102 if (nifp->ipsec_fallback_profile)
103 free(nifp->ipsec_fallback_profile);
104 if (nifp->source)
105 free(nifp->source);
106
107 XFREE(MTYPE_NHRP_IF, ifp->info);
108 return 0;
109 }
110
111 void nhrp_interface_init(void)
112 {
113 hook_register_prio(if_add, 0, nhrp_if_new_hook);
114 hook_register_prio(if_del, 0, nhrp_if_delete_hook);
115
116 nhrp_gre_list = hash_create(nhrp_gre_info_key, nhrp_gre_info_cmp,
117 "NHRP GRE list Hash");
118 }
119
120 void nhrp_interface_update_mtu(struct interface *ifp, afi_t afi)
121 {
122 struct nhrp_interface *nifp = ifp->info;
123 struct nhrp_afi_data *if_ad = &nifp->afi[afi];
124 unsigned short new_mtu;
125
126 if (if_ad->configured_mtu < 0)
127 new_mtu = nifp->nbmaifp ? nifp->nbmaifp->mtu : 0;
128 else
129 new_mtu = if_ad->configured_mtu;
130 if (new_mtu >= 1500)
131 new_mtu = 0;
132
133 if (new_mtu != if_ad->mtu) {
134 debugf(NHRP_DEBUG_IF, "%s: MTU changed to %d", ifp->name,
135 new_mtu);
136 if_ad->mtu = new_mtu;
137 notifier_call(&nifp->notifier_list,
138 NOTIFY_INTERFACE_MTU_CHANGED);
139 }
140 }
141
142 static void nhrp_interface_update_source(struct interface *ifp)
143 {
144 struct nhrp_interface *nifp = ifp->info;
145
146 if (!nifp->source || !nifp->nbmaifp
147 || ((ifindex_t)nifp->link_idx == nifp->nbmaifp->ifindex
148 && (nifp->link_vrf_id == nifp->nbmaifp->vrf->vrf_id)))
149 return;
150
151 nifp->link_idx = nifp->nbmaifp->ifindex;
152 nifp->link_vrf_id = nifp->nbmaifp->vrf->vrf_id;
153 debugf(NHRP_DEBUG_IF, "%s: bound device index changed to %d, vr %u",
154 ifp->name, nifp->link_idx, nifp->link_vrf_id);
155 nhrp_send_zebra_gre_source_set(ifp, nifp->link_idx, nifp->link_vrf_id);
156 }
157
158 static void nhrp_interface_interface_notifier(struct notifier_block *n,
159 unsigned long cmd)
160 {
161 struct nhrp_interface *nifp =
162 container_of(n, struct nhrp_interface, nbmanifp_notifier);
163 struct interface *nbmaifp = nifp->nbmaifp;
164 struct nhrp_interface *nbmanifp = nbmaifp->info;
165
166 switch (cmd) {
167 case NOTIFY_INTERFACE_CHANGED:
168 nhrp_interface_update_nbma(nifp->ifp, NULL);
169 break;
170 case NOTIFY_INTERFACE_ADDRESS_CHANGED:
171 nifp->nbma = nbmanifp->afi[AFI_IP].addr;
172 nhrp_interface_update(nifp->ifp);
173 notifier_call(&nifp->notifier_list,
174 NOTIFY_INTERFACE_NBMA_CHANGED);
175 debugf(NHRP_DEBUG_IF, "%s: NBMA change: address %pSU",
176 nifp->ifp->name, &nifp->nbma);
177 break;
178 }
179 }
180
181 void nhrp_interface_update_nbma(struct interface *ifp,
182 struct nhrp_gre_info *gre_info)
183 {
184 struct nhrp_interface *nifp = ifp->info, *nbmanifp = NULL;
185 struct interface *nbmaifp = NULL;
186 union sockunion nbma;
187
188 sockunion_family(&nbma) = AF_UNSPEC;
189
190 if (nifp->source)
191 nbmaifp = if_lookup_by_name(nifp->source, nifp->link_vrf_id);
192
193 switch (ifp->ll_type) {
194 case ZEBRA_LLT_IPGRE: {
195 struct in_addr saddr = {0};
196
197 if (!gre_info) {
198 nhrp_send_zebra_gre_request(ifp);
199 return;
200 }
201 nifp->i_grekey = gre_info->ikey;
202 nifp->o_grekey = gre_info->okey;
203 nifp->link_idx = gre_info->ifindex_link;
204 nifp->link_vrf_id = gre_info->vrfid_link;
205 saddr.s_addr = gre_info->vtep_ip.s_addr;
206
207 debugf(NHRP_DEBUG_IF, "%s: GRE: %x %x %x", ifp->name,
208 nifp->i_grekey, nifp->link_idx, saddr.s_addr);
209 if (saddr.s_addr)
210 sockunion_set(&nbma, AF_INET,
211 (uint8_t *)&saddr.s_addr,
212 sizeof(saddr.s_addr));
213 else if (!nbmaifp && nifp->link_idx != IFINDEX_INTERNAL)
214 nbmaifp =
215 if_lookup_by_index(nifp->link_idx,
216 nifp->link_vrf_id);
217 } break;
218 default:
219 break;
220 }
221
222 if (nbmaifp)
223 nbmanifp = nbmaifp->info;
224
225 if (nbmaifp != nifp->nbmaifp) {
226 if (nifp->nbmaifp) {
227 struct nhrp_interface *prev_nifp = nifp->nbmaifp->info;
228
229 notifier_del(&nifp->nbmanifp_notifier,
230 &prev_nifp->notifier_list);
231 }
232 nifp->nbmaifp = nbmaifp;
233 if (nbmaifp) {
234 notifier_add(&nifp->nbmanifp_notifier,
235 &nbmanifp->notifier_list,
236 nhrp_interface_interface_notifier);
237 debugf(NHRP_DEBUG_IF, "%s: bound to %s", ifp->name,
238 nbmaifp->name);
239 }
240 }
241
242 if (nbmaifp) {
243 if (sockunion_family(&nbma) == AF_UNSPEC)
244 nbma = nbmanifp->afi[AFI_IP].addr;
245 nhrp_interface_update_mtu(ifp, AFI_IP);
246 nhrp_interface_update_source(ifp);
247 }
248
249 if (!sockunion_same(&nbma, &nifp->nbma)) {
250 nifp->nbma = nbma;
251 nhrp_interface_update(nifp->ifp);
252 debugf(NHRP_DEBUG_IF, "%s: NBMA address changed", ifp->name);
253 notifier_call(&nifp->notifier_list,
254 NOTIFY_INTERFACE_NBMA_CHANGED);
255 }
256
257 nhrp_interface_update(ifp);
258 }
259
260 static void nhrp_interface_update_address(struct interface *ifp, afi_t afi,
261 int force)
262 {
263 const int family = afi2family(afi);
264 struct nhrp_interface *nifp = ifp->info;
265 struct nhrp_afi_data *if_ad = &nifp->afi[afi];
266 struct nhrp_cache *nc;
267 struct connected *c, *best;
268 struct listnode *cnode;
269 union sockunion addr;
270 char buf[PREFIX_STRLEN];
271
272 /* Select new best match preferring primary address */
273 best = NULL;
274 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
275 if (PREFIX_FAMILY(c->address) != family)
276 continue;
277 if (best == NULL) {
278 best = c;
279 continue;
280 }
281 if ((best->flags & ZEBRA_IFA_SECONDARY)
282 && !(c->flags & ZEBRA_IFA_SECONDARY)) {
283 best = c;
284 continue;
285 }
286 if (!(best->flags & ZEBRA_IFA_SECONDARY)
287 && (c->flags & ZEBRA_IFA_SECONDARY))
288 continue;
289 if (best->address->prefixlen > c->address->prefixlen) {
290 best = c;
291 continue;
292 }
293 if (best->address->prefixlen < c->address->prefixlen)
294 continue;
295 }
296
297 /* On NHRP interfaces a host prefix is required */
298 if (best && if_ad->configured
299 && best->address->prefixlen != 8 * prefix_blen(best->address)) {
300 zlog_notice("%s: %pFX is not a host prefix", ifp->name,
301 best->address);
302 best = NULL;
303 }
304
305 /* Update address if it changed */
306 if (best)
307 prefix2sockunion(best->address, &addr);
308 else
309 memset(&addr, 0, sizeof(addr));
310
311 if (!force && sockunion_same(&if_ad->addr, &addr))
312 return;
313
314 if (sockunion_family(&if_ad->addr) != AF_UNSPEC) {
315 nc = nhrp_cache_get(ifp, &if_ad->addr, 0);
316 if (nc)
317 nhrp_cache_update_binding(nc, NHRP_CACHE_LOCAL, -1,
318 NULL, 0, NULL, NULL);
319 }
320
321 debugf(NHRP_DEBUG_KERNEL, "%s: IPv%d address changed to %s", ifp->name,
322 afi == AFI_IP ? 4 : 6,
323 best ? prefix2str(best->address, buf, sizeof(buf)) : "(none)");
324 if_ad->addr = addr;
325
326 if (if_ad->configured && sockunion_family(&if_ad->addr) != AF_UNSPEC) {
327 nc = nhrp_cache_get(ifp, &addr, 1);
328 if (nc)
329 nhrp_cache_update_binding(nc, NHRP_CACHE_LOCAL, 0, NULL,
330 0, NULL, NULL);
331 }
332
333 notifier_call(&nifp->notifier_list, NOTIFY_INTERFACE_ADDRESS_CHANGED);
334 }
335
336 void nhrp_interface_update(struct interface *ifp)
337 {
338 struct nhrp_interface *nifp = ifp->info;
339 struct nhrp_afi_data *if_ad;
340 afi_t afi;
341 int enabled = 0;
342
343 notifier_call(&nifp->notifier_list, NOTIFY_INTERFACE_CHANGED);
344
345 for (afi = 0; afi < AFI_MAX; afi++) {
346 if_ad = &nifp->afi[afi];
347
348 if (sockunion_family(&nifp->nbma) == AF_UNSPEC
349 || ifp->ifindex == IFINDEX_INTERNAL || !if_is_up(ifp)
350 || !if_ad->network_id) {
351 if (if_ad->configured) {
352 if_ad->configured = 0;
353 nhrp_interface_update_address(ifp, afi, 1);
354 }
355 continue;
356 }
357
358 if (!if_ad->configured) {
359 os_configure_dmvpn(ifp->ifindex, ifp->name,
360 afi2family(afi));
361 nhrp_send_zebra_configure_arp(ifp, afi2family(afi));
362 if_ad->configured = 1;
363 nhrp_interface_update_address(ifp, afi, 1);
364 }
365
366 enabled = 1;
367 }
368
369 if (enabled != nifp->enabled) {
370 nifp->enabled = enabled;
371 notifier_call(&nifp->notifier_list,
372 enabled ? NOTIFY_INTERFACE_UP
373 : NOTIFY_INTERFACE_DOWN);
374 }
375 }
376
377 int nhrp_ifp_create(struct interface *ifp)
378 {
379 debugf(NHRP_DEBUG_IF, "if-add: %s, ifindex: %u, hw_type: %d %s",
380 ifp->name, ifp->ifindex, ifp->ll_type,
381 if_link_type_str(ifp->ll_type));
382
383 nhrp_interface_update_nbma(ifp, NULL);
384
385 return 0;
386 }
387
388 int nhrp_ifp_destroy(struct interface *ifp)
389 {
390 debugf(NHRP_DEBUG_IF, "if-delete: %s", ifp->name);
391
392 nhrp_interface_update_cache_config(ifp, false, AF_INET);
393 nhrp_interface_update_cache_config(ifp, false, AF_INET6);
394 nhrp_interface_update(ifp);
395
396 return 0;
397 }
398
399 struct map_ctx {
400 int family;
401 bool enabled;
402 };
403
404 static void interface_config_update_nhrp_map(struct nhrp_cache_config *cc,
405 void *data)
406 {
407 struct map_ctx *ctx = data;
408 struct interface *ifp = cc->ifp;
409 struct nhrp_cache *c;
410 union sockunion nbma_addr;
411
412 if (sockunion_family(&cc->remote_addr) != ctx->family)
413 return;
414
415 /* gre layer not ready */
416 if (ifp->vrf->vrf_id == VRF_UNKNOWN)
417 return;
418
419 c = nhrp_cache_get(ifp, &cc->remote_addr, ctx->enabled ? 1 : 0);
420 if (!c && !ctx->enabled)
421 return;
422
423 /* suppress */
424 if (!ctx->enabled) {
425 if (c && c->map) {
426 nhrp_cache_update_binding(
427 c, c->cur.type, -1,
428 nhrp_peer_get(ifp, &nbma_addr), 0, NULL, NULL);
429 }
430 return;
431 }
432
433 /* Newly created */
434 assert(c != NULL);
435
436 c->map = 1;
437 if (cc->type == NHRP_CACHE_LOCAL)
438 nhrp_cache_update_binding(c, NHRP_CACHE_LOCAL, 0, NULL, 0,
439 NULL, NULL);
440 else {
441 nhrp_cache_update_binding(c, NHRP_CACHE_STATIC, 0,
442 nhrp_peer_get(ifp, &cc->nbma), 0,
443 NULL, NULL);
444 }
445 }
446
447 static void nhrp_interface_update_cache_config(struct interface *ifp, bool available, uint8_t family)
448 {
449 struct map_ctx mapctx;
450
451 mapctx = (struct map_ctx){
452 .family = family,
453 .enabled = available
454 };
455 nhrp_cache_config_foreach(ifp, interface_config_update_nhrp_map,
456 &mapctx);
457
458 }
459
460 int nhrp_ifp_up(struct interface *ifp)
461 {
462 debugf(NHRP_DEBUG_IF, "if-up: %s", ifp->name);
463 nhrp_interface_update_nbma(ifp, NULL);
464
465 return 0;
466 }
467
468 int nhrp_ifp_down(struct interface *ifp)
469 {
470 debugf(NHRP_DEBUG_IF, "if-down: %s", ifp->name);
471 nhrp_interface_update(ifp);
472
473 return 0;
474 }
475
476 int nhrp_interface_address_add(ZAPI_CALLBACK_ARGS)
477 {
478 struct connected *ifc;
479
480 ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
481 if (ifc == NULL)
482 return 0;
483
484 debugf(NHRP_DEBUG_IF, "if-addr-add: %s: %pFX", ifc->ifp->name,
485 ifc->address);
486
487 nhrp_interface_update_address(
488 ifc->ifp, family2afi(PREFIX_FAMILY(ifc->address)), 0);
489 nhrp_interface_update_cache_config(ifc->ifp, true, PREFIX_FAMILY(ifc->address));
490 return 0;
491 }
492
493 int nhrp_interface_address_delete(ZAPI_CALLBACK_ARGS)
494 {
495 struct connected *ifc;
496
497 ifc = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
498 if (ifc == NULL)
499 return 0;
500
501 debugf(NHRP_DEBUG_IF, "if-addr-del: %s: %pFX", ifc->ifp->name,
502 ifc->address);
503
504 nhrp_interface_update_address(
505 ifc->ifp, family2afi(PREFIX_FAMILY(ifc->address)), 0);
506 connected_free(&ifc);
507
508 return 0;
509 }
510
511 void nhrp_interface_notify_add(struct interface *ifp, struct notifier_block *n,
512 notifier_fn_t fn)
513 {
514 struct nhrp_interface *nifp = ifp->info;
515
516 notifier_add(n, &nifp->notifier_list, fn);
517 }
518
519 void nhrp_interface_notify_del(struct interface *ifp, struct notifier_block *n)
520 {
521 struct nhrp_interface *nifp = ifp->info;
522
523 notifier_del(n, &nifp->notifier_list);
524 }
525
526 void nhrp_interface_set_protection(struct interface *ifp, const char *profile,
527 const char *fallback_profile)
528 {
529 struct nhrp_interface *nifp = ifp->info;
530
531 if (nifp->ipsec_profile) {
532 vici_terminate_vc_by_profile_name(nifp->ipsec_profile);
533 nhrp_vc_reset();
534 free(nifp->ipsec_profile);
535 }
536 nifp->ipsec_profile = profile ? strdup(profile) : NULL;
537
538 if (nifp->ipsec_fallback_profile) {
539 vici_terminate_vc_by_profile_name(nifp->ipsec_fallback_profile);
540 nhrp_vc_reset();
541 free(nifp->ipsec_fallback_profile);
542 }
543 nifp->ipsec_fallback_profile =
544 fallback_profile ? strdup(fallback_profile) : NULL;
545
546 notifier_call(&nifp->notifier_list, NOTIFY_INTERFACE_IPSEC_CHANGED);
547 }
548
549 void nhrp_interface_set_source(struct interface *ifp, const char *ifname)
550 {
551 struct nhrp_interface *nifp = ifp->info;
552
553 if (nifp->source)
554 free(nifp->source);
555 nifp->source = ifname ? strdup(ifname) : NULL;
556
557 nhrp_interface_update_nbma(ifp, NULL);
558 }