-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathinterface.c
4646 lines (3920 loc) · 125 KB
/
interface.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Interface function.
* Copyright (C) 1997, 1999 Kunihiro Ishiguro
*
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* GNU Zebra is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <zebra.h>
#include "if.h"
#include "lib_errors.h"
#include "vty.h"
#include "sockunion.h"
#include "prefix.h"
#include "command.h"
#include "memory.h"
#include "ioctl.h"
#include "connected.h"
#include "log.h"
#include "zclient.h"
#include "vrf.h"
#include "zebra/rtadv.h"
#include "zebra_ns.h"
#include "zebra_vrf.h"
#include "zebra/interface.h"
#include "zebra/rib.h"
#include "zebra/rt.h"
#include "zebra/zebra_router.h"
#include "zebra/redistribute.h"
#include "zebra/debug.h"
#include "zebra/irdp.h"
#include "zebra/zebra_ptm.h"
#include "zebra/rt_netlink.h"
#include "zebra/if_netlink.h"
#include "zebra/interface.h"
#include "zebra/zebra_vxlan.h"
#include "zebra/zebra_errors.h"
#include "zebra/zebra_evpn_mh.h"
DEFINE_MTYPE_STATIC(ZEBRA, ZINFO, "Zebra Interface Information");
#define ZEBRA_PTM_SUPPORT
DEFINE_HOOK(zebra_if_extra_info, (struct vty * vty, struct interface *ifp),
(vty, ifp));
DEFINE_HOOK(zebra_if_config_wr, (struct vty * vty, struct interface *ifp),
(vty, ifp));
static void if_down_del_nbr_connected(struct interface *ifp);
static void if_zebra_speed_update(struct thread *thread)
{
struct interface *ifp = THREAD_ARG(thread);
struct zebra_if *zif = ifp->info;
uint32_t new_speed;
bool changed = false;
int error = 0;
new_speed = kernel_get_speed(ifp, &error);
/* error may indicate vrf not available or
* interfaces not available.
* note that loopback & virtual interfaces can return 0 as speed
*/
if (error < 0)
return;
if (new_speed != ifp->speed) {
zlog_info("%s: %s old speed: %u new speed: %u", __func__,
ifp->name, ifp->speed, new_speed);
ifp->speed = new_speed;
if_add_update(ifp);
changed = true;
}
if (changed || new_speed == UINT32_MAX) {
#define SPEED_UPDATE_SLEEP_TIME 5
#define SPEED_UPDATE_COUNT_MAX (4 * 60 / SPEED_UPDATE_SLEEP_TIME)
/*
* Some interfaces never actually have an associated speed
* with them ( I am looking at you bridges ).
* So instead of iterating forever, let's give the
* system 4 minutes to try to figure out the speed
* if after that it it's probably never going to become
* useful.
* Since I don't know all the wonderful types of interfaces
* that may come into existence in the future I am going
* to not update the system to keep track of that. This
* is far simpler to just stop trying after 4 minutes
*/
if (new_speed == UINT32_MAX &&
zif->speed_update_count == SPEED_UPDATE_COUNT_MAX)
return;
zif->speed_update_count++;
thread_add_timer(zrouter.master, if_zebra_speed_update, ifp,
SPEED_UPDATE_SLEEP_TIME, &zif->speed_update);
thread_ignore_late_timer(zif->speed_update);
}
}
static void zebra_if_node_destroy(route_table_delegate_t *delegate,
struct route_table *table,
struct route_node *node)
{
if (node->info)
list_delete((struct list **)&node->info);
route_node_destroy(delegate, table, node);
}
static void zebra_if_nhg_dependents_free(struct zebra_if *zebra_if)
{
nhg_connected_tree_free(&zebra_if->nhg_dependents);
}
static void zebra_if_nhg_dependents_init(struct zebra_if *zebra_if)
{
nhg_connected_tree_init(&zebra_if->nhg_dependents);
}
route_table_delegate_t zebra_if_table_delegate = {
.create_node = route_node_create,
.destroy_node = zebra_if_node_destroy};
/* Called when new interface is added. */
static int if_zebra_new_hook(struct interface *ifp)
{
struct zebra_if *zebra_if;
zebra_if = XCALLOC(MTYPE_ZINFO, sizeof(struct zebra_if));
zebra_if->ifp = ifp;
zebra_if->multicast = IF_ZEBRA_MULTICAST_UNSPEC;
zebra_if->shutdown = IF_ZEBRA_SHUTDOWN_OFF;
zebra_if_nhg_dependents_init(zebra_if);
zebra_ptm_if_init(zebra_if);
ifp->ptm_enable = zebra_ptm_get_enable_state();
#if defined(HAVE_RTADV)
{
/* Set default router advertise values. */
struct rtadvconf *rtadv;
rtadv = &zebra_if->rtadv;
rtadv->AdvSendAdvertisements = 0;
rtadv->MaxRtrAdvInterval = RTADV_MAX_RTR_ADV_INTERVAL;
rtadv->MinRtrAdvInterval = RTADV_MIN_RTR_ADV_INTERVAL;
rtadv->AdvIntervalTimer = 0;
rtadv->AdvManagedFlag = 0;
rtadv->AdvOtherConfigFlag = 0;
rtadv->AdvHomeAgentFlag = 0;
rtadv->AdvLinkMTU = 0;
rtadv->AdvReachableTime = 0;
rtadv->AdvRetransTimer = 0;
rtadv->AdvCurHopLimit = RTADV_DEFAULT_HOPLIMIT;
memset(&rtadv->lastadvcurhoplimit, 0,
sizeof(rtadv->lastadvcurhoplimit));
memset(&rtadv->lastadvmanagedflag, 0,
sizeof(rtadv->lastadvmanagedflag));
memset(&rtadv->lastadvotherconfigflag, 0,
sizeof(rtadv->lastadvotherconfigflag));
memset(&rtadv->lastadvreachabletime, 0,
sizeof(rtadv->lastadvreachabletime));
memset(&rtadv->lastadvretranstimer, 0,
sizeof(rtadv->lastadvretranstimer));
rtadv->AdvDefaultLifetime =
-1; /* derive from MaxRtrAdvInterval */
rtadv->HomeAgentPreference = 0;
rtadv->HomeAgentLifetime =
-1; /* derive from AdvDefaultLifetime */
rtadv->AdvIntervalOption = 0;
rtadv->UseFastRexmit = true;
rtadv->DefaultPreference = RTADV_PREF_MEDIUM;
rtadv->AdvPrefixList = list_new();
rtadv->AdvRDNSSList = list_new();
rtadv->AdvDNSSLList = list_new();
}
#endif /* HAVE_RTADV */
memset(&zebra_if->neigh_mac[0], 0, 6);
/* Initialize installed address chains tree. */
zebra_if->ipv4_subnets =
route_table_init_with_delegate(&zebra_if_table_delegate);
ifp->info = zebra_if;
/*
* Some platforms are telling us that the interface is
* up and ready to go. When we check the speed we
* sometimes get the wrong value. Wait a couple
* of seconds and ask again. Hopefully it's all settled
* down upon startup.
*/
zebra_if->speed_update_count = 0;
thread_add_timer(zrouter.master, if_zebra_speed_update, ifp, 15,
&zebra_if->speed_update);
thread_ignore_late_timer(zebra_if->speed_update);
return 0;
}
static void if_nhg_dependents_check_valid(struct nhg_hash_entry *nhe)
{
zebra_nhg_check_valid(nhe);
if (!CHECK_FLAG(nhe->flags, NEXTHOP_GROUP_VALID)) {
/* If we're in shutdown, this interface event needs to clean
* up installed NHGs, so don't clear that flag directly.
*/
if (!zrouter.in_shutdown)
UNSET_FLAG(nhe->flags, NEXTHOP_GROUP_INSTALLED);
}
}
static void if_down_nhg_dependents(const struct interface *ifp)
{
struct nhg_connected *rb_node_dep = NULL;
struct zebra_if *zif = (struct zebra_if *)ifp->info;
frr_each(nhg_connected_tree, &zif->nhg_dependents, rb_node_dep)
if_nhg_dependents_check_valid(rb_node_dep->nhe);
}
static void if_nhg_dependents_release(const struct interface *ifp)
{
struct nhg_connected *rb_node_dep = NULL;
struct zebra_if *zif = (struct zebra_if *)ifp->info;
frr_each(nhg_connected_tree, &zif->nhg_dependents, rb_node_dep) {
rb_node_dep->nhe->ifp = NULL; /* Null it out */
if_nhg_dependents_check_valid(rb_node_dep->nhe);
}
}
/* Called when interface is deleted. */
static int if_zebra_delete_hook(struct interface *ifp)
{
struct zebra_if *zebra_if;
if (ifp->info) {
zebra_if = ifp->info;
/* If we set protodown, clear our reason now from the kernel */
if (ZEBRA_IF_IS_PROTODOWN(zebra_if) && zebra_if->protodown_rc &&
!ZEBRA_IF_IS_PROTODOWN_ONLY_EXTERNAL(zebra_if))
zebra_if_update_protodown_rc(ifp, true,
(zebra_if->protodown_rc &
~ZEBRA_PROTODOWN_ALL));
/* Free installed address chains tree. */
if (zebra_if->ipv4_subnets)
route_table_finish(zebra_if->ipv4_subnets);
#if defined(HAVE_RTADV)
struct rtadvconf *rtadv;
rtadv = &zebra_if->rtadv;
list_delete(&rtadv->AdvPrefixList);
list_delete(&rtadv->AdvRDNSSList);
list_delete(&rtadv->AdvDNSSLList);
#endif /* HAVE_RTADV */
zebra_evpn_if_cleanup(zebra_if);
zebra_evpn_mac_ifp_del(ifp);
if_nhg_dependents_release(ifp);
zebra_if_nhg_dependents_free(zebra_if);
XFREE(MTYPE_TMP, zebra_if->desc);
THREAD_OFF(zebra_if->speed_update);
XFREE(MTYPE_ZINFO, zebra_if);
}
return 0;
}
/* Build the table key */
static void if_build_key(uint32_t ifindex, struct prefix *p)
{
p->family = AF_INET;
p->prefixlen = IPV4_MAX_BITLEN;
p->u.prefix4.s_addr = ifindex;
}
/* Link an interface in a per NS interface tree */
struct interface *if_link_per_ns(struct zebra_ns *ns, struct interface *ifp)
{
struct prefix p;
struct route_node *rn;
if (ifp->ifindex == IFINDEX_INTERNAL)
return NULL;
if_build_key(ifp->ifindex, &p);
rn = route_node_get(ns->if_table, &p);
if (rn->info) {
ifp = (struct interface *)rn->info;
route_unlock_node(rn); /* get */
return ifp;
}
rn->info = ifp;
ifp->node = rn;
return ifp;
}
/* Delete a VRF. This is called in vrf_terminate(). */
void if_unlink_per_ns(struct interface *ifp)
{
ifp->node->info = NULL;
route_unlock_node(ifp->node);
ifp->node = NULL;
}
/* Look up an interface by identifier within a NS */
struct interface *if_lookup_by_index_per_ns(struct zebra_ns *ns,
uint32_t ifindex)
{
struct prefix p;
struct route_node *rn;
struct interface *ifp = NULL;
if_build_key(ifindex, &p);
rn = route_node_lookup(ns->if_table, &p);
if (rn) {
ifp = (struct interface *)rn->info;
route_unlock_node(rn); /* lookup */
}
return ifp;
}
/* Look up an interface by name within a NS */
struct interface *if_lookup_by_name_per_ns(struct zebra_ns *ns,
const char *ifname)
{
struct route_node *rn;
struct interface *ifp;
for (rn = route_top(ns->if_table); rn; rn = route_next(rn)) {
ifp = (struct interface *)rn->info;
if (ifp && strcmp(ifp->name, ifname) == 0) {
route_unlock_node(rn);
return (ifp);
}
}
return NULL;
}
const char *ifindex2ifname_per_ns(struct zebra_ns *zns, unsigned int ifindex)
{
struct interface *ifp;
return ((ifp = if_lookup_by_index_per_ns(zns, ifindex)) != NULL)
? ifp->name
: "unknown";
}
/* Tie an interface address to its derived subnet list of addresses. */
int if_subnet_add(struct interface *ifp, struct connected *ifc)
{
struct route_node *rn;
struct zebra_if *zebra_if;
struct prefix cp;
struct list *addr_list;
assert(ifp && ifp->info && ifc);
zebra_if = ifp->info;
/* Get address derived subnet node and associated address list, while
marking
address secondary attribute appropriately. */
cp = *CONNECTED_PREFIX(ifc);
apply_mask(&cp);
rn = route_node_get(zebra_if->ipv4_subnets, &cp);
if ((addr_list = rn->info))
SET_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY);
else {
UNSET_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY);
rn->info = addr_list = list_new();
route_lock_node(rn);
}
/* Tie address at the tail of address list. */
listnode_add(addr_list, ifc);
/* Return list element count. */
return (addr_list->count);
}
/* Untie an interface address from its derived subnet list of addresses. */
int if_subnet_delete(struct interface *ifp, struct connected *ifc)
{
struct route_node *rn;
struct zebra_if *zebra_if;
struct list *addr_list;
struct prefix cp;
assert(ifp && ifp->info && ifc);
zebra_if = ifp->info;
cp = *CONNECTED_PREFIX(ifc);
apply_mask(&cp);
/* Get address derived subnet node. */
rn = route_node_lookup(zebra_if->ipv4_subnets, &cp);
if (!(rn && rn->info)) {
flog_warn(EC_ZEBRA_REMOVE_ADDR_UNKNOWN_SUBNET,
"Trying to remove an address from an unknown subnet. (please report this bug)");
return -1;
}
route_unlock_node(rn);
/* Untie address from subnet's address list. */
addr_list = rn->info;
/* Deleting an address that is not registered is a bug.
* In any case, we shouldn't decrement the lock counter if the address
* is unknown. */
if (!listnode_lookup(addr_list, ifc)) {
flog_warn(
EC_ZEBRA_REMOVE_UNREGISTERED_ADDR,
"Trying to remove an address from a subnet where it is not currently registered. (please report this bug)");
return -1;
}
listnode_delete(addr_list, ifc);
route_unlock_node(rn);
/* Return list element count, if not empty. */
if (addr_list->count) {
/* If deleted address is primary, mark subsequent one as such
* and distribute. */
if (!CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY)) {
ifc = listgetdata(
(struct listnode *)listhead(addr_list));
zebra_interface_address_delete_update(ifp, ifc);
UNSET_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY);
/* XXX: Linux kernel removes all the secondary addresses
* when the primary
* address is removed. We could try to work around that,
* though this is
* non-trivial. */
zebra_interface_address_add_update(ifp, ifc);
}
return addr_list->count;
}
/* Otherwise, free list and route node. */
list_delete(&addr_list);
rn->info = NULL;
route_unlock_node(rn);
return 0;
}
/* if_flags_mangle: A place for hacks that require mangling
* or tweaking the interface flags.
*
* ******************** Solaris flags hacks **************************
*
* Solaris IFF_UP flag reflects only the primary interface as the
* routing socket only sends IFINFO for the primary interface. Hence
* ~IFF_UP does not per se imply all the logical interfaces are also
* down - which we only know of as addresses. Instead we must determine
* whether the interface really is up or not according to how many
* addresses are still attached. (Solaris always sends RTM_DELADDR if
* an interface, logical or not, goes ~IFF_UP).
*
* Ie, we mangle IFF_UP to *additionally* reflect whether or not there
* are addresses left in struct connected, not just the actual underlying
* IFF_UP flag.
*
* We must hence remember the real state of IFF_UP, which we do in
* struct zebra_if.primary_state.
*
* Setting IFF_UP within zebra to administratively shutdown the
* interface will affect only the primary interface/address on Solaris.
************************End Solaris flags hacks ***********************
*/
static void if_flags_mangle(struct interface *ifp, uint64_t *newflags)
{
return;
}
/* Update the flags field of the ifp with the new flag set provided.
* Take whatever actions are required for any changes in flags we care
* about.
*
* newflags should be the raw value, as obtained from the OS.
*/
void if_flags_update(struct interface *ifp, uint64_t newflags)
{
if_flags_mangle(ifp, &newflags);
if (if_is_no_ptm_operative(ifp)) {
/* operative -> inoperative? */
ifp->flags = newflags;
if (!if_is_operative(ifp))
if_down(ifp);
} else {
/* inoperative -> operative? */
ifp->flags = newflags;
if (if_is_operative(ifp))
if_up(ifp, true);
}
}
/* Wake up configured address if it is not in current kernel
address. */
void if_addr_wakeup(struct interface *ifp)
{
struct listnode *node, *nnode;
struct connected *ifc;
struct prefix *p;
enum zebra_dplane_result dplane_res;
for (ALL_LIST_ELEMENTS(ifp->connected, node, nnode, ifc)) {
p = ifc->address;
if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED)
&& !CHECK_FLAG(ifc->conf, ZEBRA_IFC_QUEUED)) {
/* Address check. */
if (p->family == AF_INET) {
if (!if_is_up(ifp)) {
/* Assume zebra is configured like
* following:
*
* interface gre0
* ip addr 192.0.2.1/24
* !
*
* As soon as zebra becomes first aware
* that gre0 exists in the
* kernel, it will set gre0 up and
* configure its addresses.
*
* (This may happen at startup when the
* interface already exists
* or during runtime when the interface
* is added to the kernel)
*
* XXX: IRDP code is calling here via
* if_add_update - this seems
* somewhat weird.
* XXX: RUNNING is not a settable flag
* on any system
* I (paulj) am aware of.
*/
if_set_flags(ifp, IFF_UP | IFF_RUNNING);
if_refresh(ifp);
}
dplane_res = dplane_intf_addr_set(ifp, ifc);
if (dplane_res ==
ZEBRA_DPLANE_REQUEST_FAILURE) {
flog_err_sys(
EC_ZEBRA_IFACE_ADDR_ADD_FAILED,
"Can't set interface's address: %s",
dplane_res2str(dplane_res));
continue;
}
SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
/* The address will be advertised to zebra
* clients when the notification
* from the kernel has been received.
* It will also be added to the interface's
* subnet list then. */
}
if (p->family == AF_INET6) {
if (!if_is_up(ifp)) {
/* See long comment above */
if_set_flags(ifp, IFF_UP | IFF_RUNNING);
if_refresh(ifp);
}
dplane_res = dplane_intf_addr_set(ifp, ifc);
if (dplane_res ==
ZEBRA_DPLANE_REQUEST_FAILURE) {
flog_err_sys(
EC_ZEBRA_IFACE_ADDR_ADD_FAILED,
"Can't set interface's address: %s",
dplane_res2str(dplane_res));
continue;
}
SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
/* The address will be advertised to zebra
* clients when the notification
* from the kernel has been received. */
}
}
}
}
/* Handle interface addition */
void if_add_update(struct interface *ifp)
{
struct zebra_if *if_data;
struct zebra_ns *zns;
struct zebra_vrf *zvrf = ifp->vrf->info;
/* case interface populate before vrf enabled */
if (zvrf->zns)
zns = zvrf->zns;
else
zns = zebra_ns_lookup(NS_DEFAULT);
if_link_per_ns(zns, ifp);
if_data = ifp->info;
assert(if_data);
if (if_data->multicast == IF_ZEBRA_MULTICAST_ON)
if_set_flags(ifp, IFF_MULTICAST);
else if (if_data->multicast == IF_ZEBRA_MULTICAST_OFF)
if_unset_flags(ifp, IFF_MULTICAST);
zebra_ptm_if_set_ptm_state(ifp, if_data);
zebra_interface_add_update(ifp);
if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE)) {
SET_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE);
if (if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON) {
if (IS_ZEBRA_DEBUG_KERNEL) {
zlog_debug(
"interface %s vrf %s(%u) index %d is shutdown. Won't wake it up.",
ifp->name, ifp->vrf->name,
ifp->vrf->vrf_id, ifp->ifindex);
}
return;
}
if_addr_wakeup(ifp);
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug(
"interface %s vrf %s(%u) index %d becomes active.",
ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
ifp->ifindex);
} else {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("interface %s vrf %s(%u) index %d is added.",
ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
ifp->ifindex);
}
}
/* Install connected routes corresponding to an interface. */
static void if_install_connected(struct interface *ifp)
{
struct listnode *node;
struct listnode *next;
struct connected *ifc;
if (ifp->connected) {
for (ALL_LIST_ELEMENTS(ifp->connected, node, next, ifc)) {
if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
zebra_interface_address_add_update(ifp, ifc);
connected_up(ifp, ifc);
}
}
}
/* Uninstall connected routes corresponding to an interface. */
static void if_uninstall_connected(struct interface *ifp)
{
struct listnode *node;
struct listnode *next;
struct connected *ifc;
if (ifp->connected) {
for (ALL_LIST_ELEMENTS(ifp->connected, node, next, ifc)) {
zebra_interface_address_delete_update(ifp, ifc);
connected_down(ifp, ifc);
}
}
}
/* Uninstall and delete connected routes corresponding to an interface. */
/* TODO - Check why IPv4 handling here is different from install or if_down */
static void if_delete_connected(struct interface *ifp)
{
struct connected *ifc;
struct prefix cp;
struct route_node *rn;
struct zebra_if *zebra_if;
struct listnode *node;
struct listnode *last = NULL;
zebra_if = ifp->info;
if (!ifp->connected)
return;
while ((node = (last ? last->next : listhead(ifp->connected)))) {
ifc = listgetdata(node);
cp = *CONNECTED_PREFIX(ifc);
apply_mask(&cp);
if (cp.family == AF_INET
&& (rn = route_node_lookup(zebra_if->ipv4_subnets, &cp))) {
struct listnode *anode;
struct listnode *next;
struct listnode *first;
struct list *addr_list;
route_unlock_node(rn);
addr_list = (struct list *)rn->info;
/* Remove addresses, secondaries first. */
first = listhead(addr_list);
if (first)
for (anode = first->next; anode || first;
anode = next) {
if (!anode) {
anode = first;
first = NULL;
}
next = anode->next;
ifc = listgetdata(anode);
connected_down(ifp, ifc);
/* XXX: We have to send notifications
* here explicitly, because we destroy
* the ifc before receiving the
* notification about the address being
* deleted.
*/
zebra_interface_address_delete_update(
ifp, ifc);
UNSET_FLAG(ifc->conf, ZEBRA_IFC_REAL);
UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
/* Remove from subnet chain. */
list_delete_node(addr_list, anode);
route_unlock_node(rn);
/* Remove from interface address list
* (unconditionally). */
if (!CHECK_FLAG(ifc->conf,
ZEBRA_IFC_CONFIGURED)) {
listnode_delete(ifp->connected,
ifc);
connected_free(&ifc);
} else
last = node;
}
/* Free chain list and respective route node. */
list_delete(&addr_list);
rn->info = NULL;
route_unlock_node(rn);
} else if (cp.family == AF_INET6) {
connected_down(ifp, ifc);
zebra_interface_address_delete_update(ifp, ifc);
UNSET_FLAG(ifc->conf, ZEBRA_IFC_REAL);
UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED))
last = node;
else {
listnode_delete(ifp->connected, ifc);
connected_free(&ifc);
}
} else {
last = node;
}
}
}
/* Handle an interface delete event */
void if_delete_update(struct interface *ifp)
{
struct zebra_if *zif;
if (if_is_up(ifp)) {
flog_err(
EC_LIB_INTERFACE,
"interface %s vrf %s(%u) index %d is still up while being deleted.",
ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
ifp->ifindex);
return;
}
if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE))
return;
/* Mark interface as inactive */
UNSET_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE);
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("interface %s vrf %s(%u) index %d is now inactive.",
ifp->name, ifp->vrf->name, ifp->vrf->vrf_id,
ifp->ifindex);
/* Delete connected routes from the kernel. */
if_delete_connected(ifp);
/* Send out notification on interface delete. */
zebra_interface_delete_update(ifp);
if_unlink_per_ns(ifp);
/* Update ifindex after distributing the delete message. This is in
case any client needs to have the old value of ifindex available
while processing the deletion. Each client daemon is responsible
for setting ifindex to IFINDEX_INTERNAL after processing the
interface deletion message. */
if_set_index(ifp, IFINDEX_INTERNAL);
ifp->node = NULL;
/* if the ifp is in a vrf, move it to default so vrf can be deleted if
* desired. This operation is not done for netns implementation to avoid
* collision with interface with the same name in the default vrf (can
* occur with this implementation whereas it is not possible with
* vrf-lite).
*/
if (ifp->vrf->vrf_id && !vrf_is_backend_netns())
if_handle_vrf_change(ifp, VRF_DEFAULT);
/* Reset some zebra interface params to default values. */
zif = ifp->info;
if (zif) {
zif->zif_type = ZEBRA_IF_OTHER;
zif->zif_slave_type = ZEBRA_IF_SLAVE_NONE;
memset(&zif->l2info, 0, sizeof(union zebra_l2if_info));
memset(&zif->brslave_info, 0,
sizeof(struct zebra_l2info_brslave));
zebra_evpn_if_cleanup(zif);
zebra_evpn_mac_ifp_del(ifp);
}
if (!ifp->configured) {
if (IS_ZEBRA_DEBUG_KERNEL)
zlog_debug("interface %s is being deleted from the system",
ifp->name);
if_delete(&ifp);
}
}
/* VRF change for an interface */
void if_handle_vrf_change(struct interface *ifp, vrf_id_t vrf_id)
{
vrf_id_t old_vrf_id;
old_vrf_id = ifp->vrf->vrf_id;
/* Uninstall connected routes. */
if_uninstall_connected(ifp);
/* Delete any IPv4 neighbors created to implement RFC 5549 */
if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp);
/* Delete all neighbor addresses learnt through IPv6 RA */
if_down_del_nbr_connected(ifp);
/* Send out notification on interface VRF change. */
/* This is to issue an UPDATE or a DELETE, as appropriate. */
zebra_interface_vrf_update_del(ifp, vrf_id);
/* update VRF */
if_update_to_new_vrf(ifp, vrf_id);
/* Send out notification on interface VRF change. */
/* This is to issue an ADD, if needed. */
zebra_interface_vrf_update_add(ifp, old_vrf_id);
/* Install connected routes (in new VRF). */
if (if_is_operative(ifp))
if_install_connected(ifp);
}
static void ipv6_ll_address_to_mac(struct in6_addr *address, uint8_t *mac)
{
mac[0] = address->s6_addr[8] ^ 0x02;
mac[1] = address->s6_addr[9];
mac[2] = address->s6_addr[10];
mac[3] = address->s6_addr[13];
mac[4] = address->s6_addr[14];
mac[5] = address->s6_addr[15];
}
void if_nbr_mac_to_ipv4ll_neigh_update(struct interface *ifp,
char mac[6],
struct in6_addr *address,
int add)
{
struct zebra_vrf *zvrf = ifp->vrf->info;
struct zebra_if *zif = ifp->info;
char buf[16] = "169.254.0.1";
struct in_addr ipv4_ll;
ns_id_t ns_id;
inet_pton(AF_INET, buf, &ipv4_ll);
ns_id = zvrf->zns->ns_id;
/*
* Remove and re-add any existing neighbor entry for this address,
* since Netlink doesn't currently offer update message types.
*/
kernel_neigh_update(0, ifp->ifindex, (void *)&ipv4_ll.s_addr, mac, 6,
ns_id, AF_INET, true);
/* Add new neighbor entry.
*
* We force installation even if current neighbor entry is the same.
* Since this function is used to refresh our MAC entries after an
* interface flap, if we don't force in our custom entries with their
* state set to PERMANENT or REACHABLE then the kernel will attempt to
* resolve our leftover entries, fail, mark them unreachable and then
* they'll be useless to us.
*/
if (add)
kernel_neigh_update(add, ifp->ifindex, (void *)&ipv4_ll.s_addr,
mac, 6, ns_id, AF_INET, true);
memcpy(&zif->neigh_mac[0], &mac[0], 6);
/*
* We need to note whether or not we originated a v6
* neighbor entry for this interface. So that when
* someone unwisely accidently deletes this entry
* we can shove it back in.
*/
zif->v6_2_v4_ll_neigh_entry = !!add;
memcpy(&zif->v6_2_v4_ll_addr6, address, sizeof(*address));
zvrf->neigh_updates++;
}
void if_nbr_ipv6ll_to_ipv4ll_neigh_update(struct interface *ifp,
struct in6_addr *address, int add)
{
char mac[6];
ipv6_ll_address_to_mac(address, (uint8_t *)mac);
if_nbr_mac_to_ipv4ll_neigh_update(ifp, mac, address, add);
}
static void if_nbr_ipv6ll_to_ipv4ll_neigh_add_all(struct interface *ifp)
{
if (listhead(ifp->nbr_connected)) {
struct nbr_connected *nbr_connected;
struct listnode *node;
for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node,
nbr_connected))
if_nbr_ipv6ll_to_ipv4ll_neigh_update(
ifp, &nbr_connected->address->u.prefix6, 1);
}
}
void if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(struct interface *ifp)
{
if (listhead(ifp->nbr_connected)) {
struct nbr_connected *nbr_connected;
struct listnode *node;
for (ALL_LIST_ELEMENTS_RO(ifp->nbr_connected, node,
nbr_connected))
if_nbr_ipv6ll_to_ipv4ll_neigh_update(