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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
|
/*
* Interface functions.
* Copyright (C) 1997, 98 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 GNU Zebra; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <zebra.h>
#include "linklist.h"
#include "vector.h"
#include "vty.h"
#include "command.h"
#include "vrf.h"
#include "if.h"
#include "sockunion.h"
#include "prefix.h"
#include "memory.h"
#include "table.h"
#include "buffer.h"
#include "str.h"
#include "log.h"
DEFINE_MTYPE( LIB, IF, "Interface")
DEFINE_MTYPE_STATIC(LIB, CONNECTED, "Connected")
DEFINE_MTYPE_STATIC(LIB, NBR_CONNECTED, "Neighbor Connected")
DEFINE_MTYPE( LIB, CONNECTED_LABEL, "Connected interface label")
DEFINE_MTYPE_STATIC(LIB, IF_LINK_PARAMS, "Informational Link Parameters")
/* List of interfaces in only the default VRF */
int ptm_enable = 0;
/* One for each program. This structure is needed to store hooks. */
struct if_master
{
int (*if_new_hook) (struct interface *);
int (*if_delete_hook) (struct interface *);
} if_master = {0,};
/* Compare interface names, returning an integer greater than, equal to, or
* less than 0, (following the strcmp convention), according to the
* relationship between ifp1 and ifp2. Interface names consist of an
* alphabetic prefix and a numeric suffix. The primary sort key is
* lexicographic by name, and then numeric by number. No number sorts
* before all numbers. Examples: de0 < de1, de100 < fxp0 < xl0, devpty <
* devpty0, de0 < del0
*/
int
if_cmp_name_func (char *p1, char *p2)
{
unsigned int l1, l2;
long int x1, x2;
int res;
while (*p1 && *p2) {
/* look up to any number */
l1 = strcspn(p1, "0123456789");
l2 = strcspn(p2, "0123456789");
/* name lengths are different -> compare names */
if (l1 != l2)
return (strcmp(p1, p2));
/* Note that this relies on all numbers being less than all letters, so
* that de0 < del0.
*/
res = strncmp(p1, p2, l1);
/* names are different -> compare them */
if (res)
return res;
/* with identical name part, go to numeric part */
p1 += l1;
p2 += l1;
if (!*p1)
return -1;
if (!*p2)
return 1;
x1 = strtol(p1, &p1, 10);
x2 = strtol(p2, &p2, 10);
/* let's compare numbers now */
if (x1 < x2)
return -1;
if (x1 > x2)
return 1;
/* numbers were equal, lets do it again..
(it happens with name like "eth123.456:789") */
}
if (*p1)
return 1;
if (*p2)
return -1;
return 0;
}
static int
if_cmp_func (struct interface *ifp1, struct interface *ifp2)
{
return if_cmp_name_func (ifp1->name, ifp2->name);
}
/* Create new interface structure. */
struct interface *
if_create_vrf (const char *name, int namelen, vrf_id_t vrf_id)
{
struct interface *ifp;
struct list *intf_list = vrf_iflist_get (vrf_id);
ifp = XCALLOC (MTYPE_IF, sizeof (struct interface));
ifp->ifindex = IFINDEX_INTERNAL;
assert (name);
assert (namelen <= INTERFACE_NAMSIZ); /* Need space for '\0' at end. */
strncpy (ifp->name, name, namelen);
ifp->name[namelen] = '\0';
ifp->vrf_id = vrf_id;
if (if_lookup_by_name_vrf (ifp->name, vrf_id) == NULL)
listnode_add_sort (intf_list, ifp);
else
zlog_err("if_create(%s): corruption detected -- interface with this "
"name exists already in VRF %u!", ifp->name, vrf_id);
ifp->connected = list_new ();
ifp->connected->del = (void (*) (void *)) connected_free;
ifp->nbr_connected = list_new ();
ifp->nbr_connected->del = (void (*) (void *)) nbr_connected_free;
/* Enable Link-detection by default */
SET_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION);
if (if_master.if_new_hook)
(*if_master.if_new_hook) (ifp);
return ifp;
}
struct interface *
if_create (const char *name, int namelen)
{
return if_create_vrf (name, namelen, VRF_DEFAULT);
}
/* Create new interface structure. */
void
if_update_vrf (struct interface *ifp, const char *name, int namelen, vrf_id_t vrf_id)
{
struct list *intf_list = vrf_iflist_get (vrf_id);
/* remove interface from old master vrf list */
if (vrf_iflist (ifp->vrf_id))
listnode_delete (vrf_iflist (ifp->vrf_id), ifp);
assert (name);
assert (namelen <= INTERFACE_NAMSIZ); /* Need space for '\0' at end. */
strncpy (ifp->name, name, namelen);
ifp->name[namelen] = '\0';
ifp->vrf_id = vrf_id;
if (if_lookup_by_name_vrf (ifp->name, vrf_id) == NULL)
listnode_add_sort (intf_list, ifp);
else
zlog_err("if_create(%s): corruption detected -- interface with this "
"name exists already in VRF %u!", ifp->name, vrf_id);
return;
}
/* Delete interface structure. */
void
if_delete_retain (struct interface *ifp)
{
if (if_master.if_delete_hook)
(*if_master.if_delete_hook) (ifp);
/* Free connected address list */
list_delete_all_node (ifp->connected);
/* Free connected nbr address list */
list_delete_all_node (ifp->nbr_connected);
}
/* Delete and free interface structure. */
void
if_delete (struct interface *ifp)
{
listnode_delete (vrf_iflist (ifp->vrf_id), ifp);
if_delete_retain(ifp);
list_free (ifp->connected);
list_free (ifp->nbr_connected);
if_link_params_free (ifp);
XFREE (MTYPE_IF, ifp);
}
/* Add hook to interface master. */
void
if_add_hook (int type, int (*func)(struct interface *ifp))
{
switch (type) {
case IF_NEW_HOOK:
if_master.if_new_hook = func;
break;
case IF_DELETE_HOOK:
if_master.if_delete_hook = func;
break;
default:
break;
}
}
/* Interface existance check by index. */
struct interface *
if_lookup_by_index_vrf (ifindex_t ifindex, vrf_id_t vrf_id)
{
struct listnode *node;
struct interface *ifp;
for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
{
if (ifp->ifindex == ifindex)
return ifp;
}
return NULL;
}
struct interface *
if_lookup_by_index (ifindex_t ifindex)
{
return if_lookup_by_index_vrf (ifindex, VRF_DEFAULT);
}
const char *
ifindex2ifname_vrf (ifindex_t ifindex, vrf_id_t vrf_id)
{
struct interface *ifp;
return ((ifp = if_lookup_by_index_vrf (ifindex, vrf_id)) != NULL) ?
ifp->name : "unknown";
}
const char *
ifindex2ifname (ifindex_t ifindex)
{
return ifindex2ifname_vrf (ifindex, VRF_DEFAULT);
}
ifindex_t
ifname2ifindex_vrf (const char *name, vrf_id_t vrf_id)
{
struct interface *ifp;
return ((ifp = if_lookup_by_name_vrf (name, vrf_id)) != NULL) ? ifp->ifindex
: IFINDEX_INTERNAL;
}
ifindex_t
ifname2ifindex (const char *name)
{
return ifname2ifindex_vrf (name, VRF_DEFAULT);
}
/* Interface existance check by interface name. */
struct interface *
if_lookup_by_name_vrf (const char *name, vrf_id_t vrf_id)
{
struct listnode *node;
struct interface *ifp;
if (name)
for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
{
if (strcmp(name, ifp->name) == 0)
return ifp;
}
return NULL;
}
struct interface *
if_lookup_by_name_all_vrf (const char *name)
{
struct interface *ifp;
struct vrf *vrf = NULL;
vrf_iter_t iter;
for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
{
vrf = vrf_iter2vrf (iter);
ifp = if_lookup_by_name_vrf (name, vrf->vrf_id);
if (ifp)
return ifp;
}
return NULL;
}
struct interface *
if_lookup_by_name (const char *name)
{
return if_lookup_by_name_vrf (name, VRF_DEFAULT);
}
struct interface *
if_lookup_by_name_len_vrf (const char *name, size_t namelen, vrf_id_t vrf_id)
{
struct listnode *node;
struct interface *ifp;
if (namelen > INTERFACE_NAMSIZ)
return NULL;
for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
{
if (!memcmp(name, ifp->name, namelen) && (ifp->name[namelen] == '\0'))
return ifp;
}
return NULL;
}
struct interface *
if_lookup_by_name_len(const char *name, size_t namelen)
{
return if_lookup_by_name_len_vrf (name, namelen, VRF_DEFAULT);
}
/* Lookup interface by IPv4 address. */
struct interface *
if_lookup_exact_address_vrf (void *src, int family, vrf_id_t vrf_id)
{
struct listnode *node;
struct listnode *cnode;
struct interface *ifp;
struct prefix *p;
struct connected *c;
for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
{
for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
{
p = c->address;
if (p && (p->family == family))
{
if (family == AF_INET)
{
if (IPV4_ADDR_SAME (&p->u.prefix4, (struct in_addr *)src))
return ifp;
}
else if (family == AF_INET6)
{
if (IPV6_ADDR_SAME (&p->u.prefix4, (struct in6_addr *)src))
return ifp;
}
}
}
}
return NULL;
}
struct interface *
if_lookup_exact_address (void *src, int family)
{
return if_lookup_exact_address_vrf (src, family, VRF_DEFAULT);
}
/* Lookup interface by IPv4 address. */
struct interface *
if_lookup_address_vrf (void *matchaddr, int family, vrf_id_t vrf_id)
{
struct listnode *node;
struct prefix addr;
int bestlen = 0;
struct listnode *cnode;
struct interface *ifp;
struct connected *c;
struct interface *match;
if (family == AF_INET)
{
addr.family = AF_INET;
addr.u.prefix4 = *((struct in_addr *)matchaddr);
addr.prefixlen = IPV4_MAX_BITLEN;
}
else if (family == AF_INET6)
{
addr.family = AF_INET6;
addr.u.prefix6 = *((struct in6_addr *)matchaddr);
addr.prefixlen = IPV6_MAX_BITLEN;
}
match = NULL;
for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
{
for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
{
if (c->address && (c->address->family == AF_INET) &&
prefix_match(CONNECTED_PREFIX(c), &addr) &&
(c->address->prefixlen > bestlen))
{
bestlen = c->address->prefixlen;
match = ifp;
}
}
}
return match;
}
struct interface *
if_lookup_address (void *matchaddr, int family)
{
return if_lookup_address_vrf (matchaddr, family, VRF_DEFAULT);
}
/* Lookup interface by prefix */
struct interface *
if_lookup_prefix_vrf (struct prefix *prefix, vrf_id_t vrf_id)
{
struct listnode *node;
struct listnode *cnode;
struct interface *ifp;
struct connected *c;
for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
{
for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
{
if (prefix_cmp(c->address, prefix) == 0)
{
return ifp;
}
}
}
return NULL;
}
struct interface *
if_lookup_prefix (struct prefix *prefix)
{
return if_lookup_prefix_vrf (prefix, VRF_DEFAULT);
}
/* Get interface by name if given name interface doesn't exist create
one. */
struct interface *
if_get_by_name_vrf (const char *name, vrf_id_t vrf_id)
{
struct interface *ifp;
return ((ifp = if_lookup_by_name_vrf (name, vrf_id)) != NULL) ? ifp :
if_create_vrf (name, strlen(name), vrf_id);
}
struct interface *
if_get_by_name (const char *name)
{
return if_get_by_name_vrf (name, VRF_DEFAULT);
}
struct interface *
if_get_by_name_len_vrf (const char *name, size_t namelen, vrf_id_t vrf_id, int vty)
{
struct interface *ifp;
struct listnode *node;
struct vrf *vrf = NULL;
vrf_iter_t iter;
ifp = if_lookup_by_name_len_vrf (name, namelen, vrf_id);
if (ifp)
return ifp;
/* Didn't find the interface on that vrf. Defined on a different one? */
for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
{
vrf = vrf_iter2vrf(iter);
for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf->vrf_id), node, ifp))
{
if (!memcmp(name, ifp->name, namelen) && (ifp->name[namelen] == '\0'))
{
/* Found a match. If the interface command was entered in vty without a
* VRF (passed as VRF_DEFAULT), accept the ifp we found. If a vrf was
* entered and there is a mismatch, reject it if from vty. If it came
* from the kernel by way of zclient, believe it and update
* the ifp accordingly.
*/
if (vty)
{
if (vrf_id == VRF_DEFAULT)
return ifp;
return NULL;
}
else
{
if_update_vrf (ifp, name, namelen, vrf_id);
return ifp;
}
}
}
}
return (if_create_vrf (name, namelen, vrf_id));
}
struct interface *
if_get_by_name_len (const char *name, size_t namelen)
{
return if_get_by_name_len_vrf (name, namelen, VRF_DEFAULT, 0);
}
/* Does interface up ? */
int
if_is_up (struct interface *ifp)
{
return ifp->flags & IFF_UP;
}
/* Is interface running? */
int
if_is_running (struct interface *ifp)
{
return ifp->flags & IFF_RUNNING;
}
/* Is the interface operative, eg. either UP & RUNNING
or UP & !ZEBRA_INTERFACE_LINK_DETECTION and
if ptm checking is enabled, then ptm check has passed */
int
if_is_operative (struct interface *ifp)
{
return ((ifp->flags & IFF_UP) &&
(((ifp->flags & IFF_RUNNING) &&
(ifp->ptm_status || !ifp->ptm_enable)) ||
!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)));
}
/* Is the interface operative, eg. either UP & RUNNING
or UP & !ZEBRA_INTERFACE_LINK_DETECTION, without PTM check */
int
if_is_no_ptm_operative (struct interface *ifp)
{
return ((ifp->flags & IFF_UP) &&
((ifp->flags & IFF_RUNNING) ||
!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_LINKDETECTION)));
}
/* Is this loopback interface ? */
int
if_is_loopback (struct interface *ifp)
{
/* XXX: Do this better, eg what if IFF_WHATEVER means X on platform M
* but Y on platform N?
*/
return (ifp->flags & (IFF_LOOPBACK|IFF_NOXMIT|IFF_VIRTUAL));
}
/* Does this interface support broadcast ? */
int
if_is_broadcast (struct interface *ifp)
{
return ifp->flags & IFF_BROADCAST;
}
/* Does this interface support broadcast ? */
int
if_is_pointopoint (struct interface *ifp)
{
return ifp->flags & IFF_POINTOPOINT;
}
/* Does this interface support multicast ? */
int
if_is_multicast (struct interface *ifp)
{
return ifp->flags & IFF_MULTICAST;
}
/* Printout flag information into log */
const char *
if_flag_dump (unsigned long flag)
{
int separator = 0;
static char logbuf[BUFSIZ];
#define IFF_OUT_LOG(X,STR) \
if (flag & (X)) \
{ \
if (separator) \
strlcat (logbuf, ",", BUFSIZ); \
else \
separator = 1; \
strlcat (logbuf, STR, BUFSIZ); \
}
strlcpy (logbuf, "<", BUFSIZ);
IFF_OUT_LOG (IFF_UP, "UP");
IFF_OUT_LOG (IFF_BROADCAST, "BROADCAST");
IFF_OUT_LOG (IFF_DEBUG, "DEBUG");
IFF_OUT_LOG (IFF_LOOPBACK, "LOOPBACK");
IFF_OUT_LOG (IFF_POINTOPOINT, "POINTOPOINT");
IFF_OUT_LOG (IFF_NOTRAILERS, "NOTRAILERS");
IFF_OUT_LOG (IFF_RUNNING, "RUNNING");
IFF_OUT_LOG (IFF_NOARP, "NOARP");
IFF_OUT_LOG (IFF_PROMISC, "PROMISC");
IFF_OUT_LOG (IFF_ALLMULTI, "ALLMULTI");
IFF_OUT_LOG (IFF_OACTIVE, "OACTIVE");
IFF_OUT_LOG (IFF_SIMPLEX, "SIMPLEX");
IFF_OUT_LOG (IFF_LINK0, "LINK0");
IFF_OUT_LOG (IFF_LINK1, "LINK1");
IFF_OUT_LOG (IFF_LINK2, "LINK2");
IFF_OUT_LOG (IFF_MULTICAST, "MULTICAST");
IFF_OUT_LOG (IFF_NOXMIT, "NOXMIT");
IFF_OUT_LOG (IFF_NORTEXCH, "NORTEXCH");
IFF_OUT_LOG (IFF_VIRTUAL, "VIRTUAL");
IFF_OUT_LOG (IFF_IPV4, "IPv4");
IFF_OUT_LOG (IFF_IPV6, "IPv6");
strlcat (logbuf, ">", BUFSIZ);
return logbuf;
#undef IFF_OUT_LOG
}
/* For debugging */
static void
if_dump (const struct interface *ifp)
{
struct listnode *node;
struct connected *c __attribute__((unused));
for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, c))
zlog_info ("Interface %s vrf %u index %d metric %d mtu %d "
"mtu6 %d %s",
ifp->name, ifp->vrf_id, ifp->ifindex, ifp->metric, ifp->mtu,
ifp->mtu6, if_flag_dump (ifp->flags));
}
/* Interface printing for all interface. */
void
if_dump_all (void)
{
struct list *intf_list;
struct listnode *node;
void *p;
vrf_iter_t iter;
for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
if ((intf_list = vrf_iter2iflist (iter)) != NULL)
for (ALL_LIST_ELEMENTS_RO (intf_list, node, p))
if_dump (p);
}
DEFUN (interface_desc,
interface_desc_cmd,
"description LINE...",
"Interface specific description\n"
"Characters describing this interface\n")
{
struct interface *ifp;
if (argc == 1)
return CMD_SUCCESS;
ifp = vty->index;
if (ifp->desc)
XFREE (MTYPE_TMP, ifp->desc);
ifp->desc = argv_concat(argv, argc, 1);
return CMD_SUCCESS;
}
DEFUN (no_interface_desc,
no_interface_desc_cmd,
"no description",
NO_STR
"Interface specific description\n")
{
struct interface *ifp;
ifp = vty->index;
if (ifp->desc)
XFREE (MTYPE_TMP, ifp->desc);
ifp->desc = NULL;
return CMD_SUCCESS;
}
#ifdef SUNOS_5
/* Need to handle upgrade from SUNWzebra to Quagga. SUNWzebra created
* a seperate struct interface for each logical interface, so config
* file may be full of 'interface fooX:Y'. Solaris however does not
* expose logical interfaces via PF_ROUTE, so trying to track logical
* interfaces can be fruitless, for that reason Quagga only tracks
* the primary IP interface.
*
* We try accomodate SUNWzebra by:
* - looking up the interface name, to see whether it exists, if so
* its useable
* - for protocol daemons, this could only because zebra told us of
* the interface
* - for zebra, only because it learnt from kernel
* - if not:
* - search the name to see if it contains a sub-ipif / logical interface
* seperator, the ':' char. If it does:
* - text up to that char must be the primary name - get that name.
* if not:
* - no idea, just get the name in its entirety.
*/
static struct interface *
if_sunwzebra_get (const char *name, size_t nlen, vrf_id_t vrf_id)
{
struct interface *ifp;
size_t seppos = 0;
if ( (ifp = if_lookup_by_name_len_vrf (name, nlen, vrf_id)) != NULL)
return ifp;
/* hunt the primary interface name... */
while (seppos < nlen && name[seppos] != ':')
seppos++;
/* Wont catch seperator as last char, e.g. 'foo0:' but thats invalid */
if (seppos < nlen)
return if_get_by_name_len_vrf (name, seppos, vrf_id, 1);
else
return if_get_by_name_len_vrf (name, nlen, vrf_id, 1);
}
#endif /* SUNOS_5 */
DEFUN (interface,
interface_cmd,
"interface IFNAME [vrf NAME]",
"Select an interface to configure\n"
"Interface's name\n"
VRF_CMD_HELP_STR)
{
int idx_ifname = 1;
const char *ifname = argv[idx_ifname]->arg;
const char *vrfname = (argc > 2) ? argv[3]->arg : NULL;
struct interface *ifp;
size_t sl;
vrf_id_t vrf_id = VRF_DEFAULT;
if ((sl = strlen(ifname)) > INTERFACE_NAMSIZ)
{
vty_out (vty, "%% Interface name %s is invalid: length exceeds "
"%d characters%s",
ifname, INTERFACE_NAMSIZ, VTY_NEWLINE);
return CMD_WARNING;
}
/*Pending: need proper vrf name based lookup/(possible creation of VRF)
Imagine forward reference of a vrf by name in this interface config */
if (vrfname)
VRF_GET_ID (vrf_id, vrfname);
#ifdef SUNOS_5
ifp = if_sunwzebra_get (ifname, sl, vrf_id);
#else
ifp = if_get_by_name_len_vrf (ifname, sl, vrf_id, 1);
#endif /* SUNOS_5 */
if (!ifp)
{
vty_out (vty, "%% interface %s not in %s%s", ifname, vrfname, VTY_NEWLINE);
return CMD_WARNING;
}
vty->index = ifp;
vty->node = INTERFACE_NODE;
return CMD_SUCCESS;
}
DEFUN_NOSH (no_interface,
no_interface_cmd,
"no interface IFNAME [vrf NAME]",
NO_STR
"Delete a pseudo interface's configuration\n"
"Interface's name\n"
VRF_CMD_HELP_STR)
{
const char *ifname = argv[2]->arg;
const char *vrfname = (argc > 3) ? argv[3]->arg : NULL;
// deleting interface
struct interface *ifp;
vrf_id_t vrf_id = VRF_DEFAULT;
if (argc > 3)
VRF_GET_ID (vrf_id, vrfname);
ifp = if_lookup_by_name_vrf (ifname, vrf_id);
if (ifp == NULL)
{
vty_out (vty, "%% Interface %s does not exist%s", ifname, VTY_NEWLINE);
return CMD_WARNING;
}
if (CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
{
vty_out (vty, "%% Only inactive interfaces can be deleted%s",
VTY_NEWLINE);
return CMD_WARNING;
}
if_delete(ifp);
return CMD_SUCCESS;
}
DEFUN (vrf,
vrf_cmd,
"vrf NAME",
"Select a VRF to configure\n"
"VRF's name\n")
{
int idx_name = 1;
const char *vrfname = argv[idx_name]->arg;
struct vrf *vrfp;
size_t sl;
if ((sl = strlen(vrfname)) > VRF_NAMSIZ)
{
vty_out (vty, "%% VRF name %s is invalid: length exceeds "
"%d characters%s",
vrfname, VRF_NAMSIZ, VTY_NEWLINE);
return CMD_WARNING;
}
vrfp = vrf_get (VRF_UNKNOWN, vrfname);
vty->index = vrfp;
vty->node = VRF_NODE;
return CMD_SUCCESS;
}
DEFUN_NOSH (no_vrf,
no_vrf_cmd,
"no vrf NAME",
NO_STR
"Delete a pseudo VRF's configuration\n"
"VRF's name\n")
{
const char *vrfname = argv[2]->arg;
struct vrf *vrfp;
vrfp = vrf_list_lookup_by_name (vrfname);;
if (vrfp == NULL)
{
vty_out (vty, "%% VRF %s does not exist%s", vrfname, VTY_NEWLINE);
return CMD_WARNING;
}
if (CHECK_FLAG (vrfp->status, VRF_ACTIVE))
{
vty_out (vty, "%% Only inactive VRFs can be deleted%s",
VTY_NEWLINE);
return CMD_WARNING;
}
vrf_delete(vrfp);
return CMD_SUCCESS;
}
/* For debug purpose. */
DEFUN (show_address,
show_address_cmd,
"show address [vrf NAME]",
SHOW_STR
"address\n"
VRF_CMD_HELP_STR)
{
int idx_vrf_cmd_str = 2;
struct listnode *node;
struct listnode *node2;
struct interface *ifp;
struct connected *ifc;
struct prefix *p;
vrf_id_t vrf_id = VRF_DEFAULT;
if (argc > 2)
VRF_GET_ID (vrf_id, argv[idx_vrf_cmd_str]->arg);
for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
{
for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
{
p = ifc->address;
if (p->family == AF_INET)
vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen,
VTY_NEWLINE);
}
}
return CMD_SUCCESS;
}
DEFUN (show_address_vrf_all,
show_address_vrf_all_cmd,
"show address vrf all",
SHOW_STR
"address\n"
VRF_ALL_CMD_HELP_STR)
{
struct list *intf_list;
struct listnode *node;
struct listnode *node2;
struct interface *ifp;
struct connected *ifc;
struct prefix *p;
vrf_iter_t iter;
for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
{
intf_list = vrf_iter2iflist (iter);
if (!intf_list || !listcount (intf_list))
continue;
vty_out (vty, "%sVRF %u%s%s", VTY_NEWLINE, vrf_iter2id (iter),
VTY_NEWLINE, VTY_NEWLINE);
for (ALL_LIST_ELEMENTS_RO (intf_list, node, ifp))
{
for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
{
p = ifc->address;
if (p->family == AF_INET)
vty_out (vty, "%s/%d%s", inet_ntoa (p->u.prefix4), p->prefixlen,
VTY_NEWLINE);
}
}
}
return CMD_SUCCESS;
}
/* Allocate connected structure. */
struct connected *
connected_new (void)
{
return XCALLOC (MTYPE_CONNECTED, sizeof (struct connected));
}
/* Allocate nbr connected structure. */
struct nbr_connected *
nbr_connected_new (void)
{
return XCALLOC (MTYPE_NBR_CONNECTED, sizeof (struct nbr_connected));
}
/* Free connected structure. */
void
connected_free (struct connected *connected)
{
if (connected->address)
prefix_free (connected->address);
if (connected->destination)
prefix_free (connected->destination);
if (connected->label)
XFREE (MTYPE_CONNECTED_LABEL, connected->label);
XFREE (MTYPE_CONNECTED, connected);
}
/* Free nbr connected structure. */
void
nbr_connected_free (struct nbr_connected *connected)
{
if (connected->address)
prefix_free (connected->address);
XFREE (MTYPE_NBR_CONNECTED, connected);
}
/* If same interface nbr address already exists... */
struct nbr_connected *
nbr_connected_check (struct interface *ifp, struct prefix *p)
{
struct nbr_connected *ifc;
struct listnode *node;
for (ALL_LIST_ELEMENTS_RO (ifp->nbr_connected, node, ifc))
if (prefix_same (ifc->address, p))
return ifc;
return NULL;
}
/* Print if_addr structure. */
static void __attribute__ ((unused))
connected_log (struct connected *connected, char *str)
{
struct prefix *p;
struct interface *ifp;
char logbuf[BUFSIZ];
char buf[BUFSIZ];
ifp = connected->ifp;
p = connected->address;
snprintf (logbuf, BUFSIZ, "%s interface %s vrf %u %s %s/%d ",
str, ifp->name, ifp->vrf_id, prefix_family_str (p),
inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
p->prefixlen);
p = connected->destination;
if (p)
{
strncat (logbuf, inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
BUFSIZ - strlen(logbuf));
}
zlog (NULL, LOG_INFO, "%s", logbuf);
}
/* Print if_addr structure. */
static void __attribute__ ((unused))
nbr_connected_log (struct nbr_connected *connected, char *str)
{
struct prefix *p;
struct interface *ifp;
char logbuf[BUFSIZ];
char buf[BUFSIZ];
ifp = connected->ifp;
p = connected->address;
snprintf (logbuf, BUFSIZ, "%s interface %s %s %s/%d ",
str, ifp->name, prefix_family_str (p),
inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),
p->prefixlen);
zlog (NULL, LOG_INFO, "%s", logbuf);
}
/* If two connected address has same prefix return 1. */
static int
connected_same_prefix (struct prefix *p1, struct prefix *p2)
{
if (p1->family == p2->family)
{
if (p1->family == AF_INET &&
IPV4_ADDR_SAME (&p1->u.prefix4, &p2->u.prefix4))
return 1;
#ifdef HAVE_IPV6
if (p1->family == AF_INET6 &&
IPV6_ADDR_SAME (&p1->u.prefix6, &p2->u.prefix6))
return 1;
#endif /* HAVE_IPV6 */
}
return 0;
}
struct connected *
connected_lookup_prefix_exact (struct interface *ifp, struct prefix *p)
{
struct listnode *node;
struct listnode *next;
struct connected *ifc;
for (node = listhead (ifp->connected); node; node = next)
{
ifc = listgetdata (node);
next = node->next;
if (connected_same_prefix (ifc->address, p))
return ifc;
}
return NULL;
}
struct connected *
connected_delete_by_prefix (struct interface *ifp, struct prefix *p)
{
struct listnode *node;
struct listnode *next;
struct connected *ifc;
/* In case of same prefix come, replace it with new one. */
for (node = listhead (ifp->connected); node; node = next)
{
ifc = listgetdata (node);
next = node->next;
if (connected_same_prefix (ifc->address, p))
{
listnode_delete (ifp->connected, ifc);
return ifc;
}
}
return NULL;
}
/* Find the address on our side that will be used when packets
are sent to dst. */
struct connected *
connected_lookup_prefix (struct interface *ifp, struct prefix *addr)
{
struct listnode *cnode;
struct connected *c;
struct connected *match;
match = NULL;
for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
{
if (c->address && (c->address->family == addr->family) &&
prefix_match(CONNECTED_PREFIX(c), addr) &&
(!match || (c->address->prefixlen > match->address->prefixlen)))
match = c;
}
return match;
}
struct connected *
connected_add_by_prefix (struct interface *ifp, struct prefix *p,
struct prefix *destination)
{
struct connected *ifc;
/* Allocate new connected address. */
ifc = connected_new ();
ifc->ifp = ifp;
/* Fetch interface address */
ifc->address = prefix_new();
memcpy (ifc->address, p, sizeof(struct prefix));
/* Fetch dest address */
if (destination)
{
ifc->destination = prefix_new();
memcpy (ifc->destination, destination, sizeof(struct prefix));
}
/* Add connected address to the interface. */
listnode_add (ifp->connected, ifc);
return ifc;
}
#ifndef HAVE_IF_NAMETOINDEX
ifindex_t
if_nametoindex (const char *name)
{
struct interface *ifp;
return ((ifp = if_lookup_by_name_len(name, strnlen(name, IFNAMSIZ))) != NULL)
? ifp->ifindex : 0;
}
#endif
#ifndef HAVE_IF_INDEXTONAME
char *
if_indextoname (ifindex_t ifindex, char *name)
{
struct interface *ifp;
if (!(ifp = if_lookup_by_index(ifindex)))
return NULL;
strncpy (name, ifp->name, IFNAMSIZ);
return ifp->name;
}
#endif
#if 0 /* this route_table of struct connected's is unused
* however, it would be good to use a route_table rather than
* a list..
*/
/* Interface looking up by interface's address. */
/* Interface's IPv4 address reverse lookup table. */
struct route_table *ifaddr_ipv4_table;
/* struct route_table *ifaddr_ipv6_table; */
static void
ifaddr_ipv4_add (struct in_addr *ifaddr, struct interface *ifp)
{
struct route_node *rn;
struct prefix_ipv4 p;
p.family = AF_INET;
p.prefixlen = IPV4_MAX_PREFIXLEN;
p.prefix = *ifaddr;
rn = route_node_get (ifaddr_ipv4_table, (struct prefix *) &p);
if (rn)
{
route_unlock_node (rn);
zlog_info ("ifaddr_ipv4_add(): address %s is already added",
inet_ntoa (*ifaddr));
return;
}
rn->info = ifp;
}
static void
ifaddr_ipv4_delete (struct in_addr *ifaddr, struct interface *ifp)
{
struct route_node *rn;
struct prefix_ipv4 p;
p.family = AF_INET;
p.prefixlen = IPV4_MAX_PREFIXLEN;
p.prefix = *ifaddr;
rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
if (! rn)
{
zlog_info ("ifaddr_ipv4_delete(): can't find address %s",
inet_ntoa (*ifaddr));
return;
}
rn->info = NULL;
route_unlock_node (rn);
route_unlock_node (rn);
}
/* Lookup interface by interface's IP address or interface index. */
static struct interface *
ifaddr_ipv4_lookup (struct in_addr *addr, ifindex_t ifindex)
{
struct prefix_ipv4 p;
struct route_node *rn;
struct interface *ifp;
if (addr)
{
p.family = AF_INET;
p.prefixlen = IPV4_MAX_PREFIXLEN;
p.prefix = *addr;
rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p);
if (! rn)
return NULL;
ifp = rn->info;
route_unlock_node (rn);
return ifp;
}
else
return if_lookup_by_index(ifindex);
}
#endif /* ifaddr_ipv4_table */
/* Initialize interface list. */
void
if_init (struct list **intf_list)
{
*intf_list = list_new ();
#if 0
ifaddr_ipv4_table = route_table_init ();
#endif /* ifaddr_ipv4_table */
(*intf_list)->cmp = (int (*)(void *, void *))if_cmp_func;
}
void
if_terminate (struct list **intf_list)
{
for (;;)
{
struct interface *ifp;
ifp = listnode_head (*intf_list);
if (ifp == NULL)
break;
if (ifp->node)
{
ifp->node->info = NULL;
route_unlock_node (ifp->node);
}
if_delete (ifp);
}
list_delete (*intf_list);
*intf_list = NULL;
}
const char *
if_link_type_str (enum zebra_link_type llt)
{
switch (llt)
{
#define llts(T,S) case (T): return (S)
llts(ZEBRA_LLT_UNKNOWN, "Unknown");
llts(ZEBRA_LLT_ETHER, "Ethernet");
llts(ZEBRA_LLT_EETHER, "Experimental Ethernet");
llts(ZEBRA_LLT_AX25, "AX.25 Level 2");
llts(ZEBRA_LLT_PRONET, "PROnet token ring");
llts(ZEBRA_LLT_IEEE802, "IEEE 802.2 Ethernet/TR/TB");
llts(ZEBRA_LLT_ARCNET, "ARCnet");
llts(ZEBRA_LLT_APPLETLK, "AppleTalk");
llts(ZEBRA_LLT_DLCI, "Frame Relay DLCI");
llts(ZEBRA_LLT_ATM, "ATM");
llts(ZEBRA_LLT_METRICOM, "Metricom STRIP");
llts(ZEBRA_LLT_IEEE1394, "IEEE 1394 IPv4");
llts(ZEBRA_LLT_EUI64, "EUI-64");
llts(ZEBRA_LLT_INFINIBAND, "InfiniBand");
llts(ZEBRA_LLT_SLIP, "SLIP");
llts(ZEBRA_LLT_CSLIP, "Compressed SLIP");
llts(ZEBRA_LLT_SLIP6, "SLIPv6");
llts(ZEBRA_LLT_CSLIP6, "Compressed SLIPv6");
llts(ZEBRA_LLT_ROSE, "ROSE packet radio");
llts(ZEBRA_LLT_X25, "CCITT X.25");
llts(ZEBRA_LLT_PPP, "PPP");
llts(ZEBRA_LLT_CHDLC, "Cisco HDLC");
llts(ZEBRA_LLT_RAWHDLC, "Raw HDLC");
llts(ZEBRA_LLT_LAPB, "LAPB");
llts(ZEBRA_LLT_IPIP, "IPIP Tunnel");
llts(ZEBRA_LLT_IPIP6, "IPIP6 Tunnel");
llts(ZEBRA_LLT_FRAD, "FRAD");
llts(ZEBRA_LLT_SKIP, "SKIP vif");
llts(ZEBRA_LLT_LOOPBACK, "Loopback");
llts(ZEBRA_LLT_LOCALTLK, "Localtalk");
llts(ZEBRA_LLT_FDDI, "FDDI");
llts(ZEBRA_LLT_SIT, "IPv6-in-IPv4 SIT");
llts(ZEBRA_LLT_IPDDP, "IP-in-DDP tunnel");
llts(ZEBRA_LLT_IPGRE, "GRE over IP");
llts(ZEBRA_LLT_PIMREG, "PIMSM registration");
llts(ZEBRA_LLT_HIPPI, "HiPPI");
llts(ZEBRA_LLT_IRDA, "IrDA");
llts(ZEBRA_LLT_FCPP, "Fibre-Channel PtP");
llts(ZEBRA_LLT_FCAL, "Fibre-Channel Arbitrated Loop");
llts(ZEBRA_LLT_FCPL, "Fibre-Channel Public Loop");
llts(ZEBRA_LLT_FCFABRIC, "Fibre-Channel Fabric");
llts(ZEBRA_LLT_IEEE802_TR, "IEEE 802.2 Token Ring");
llts(ZEBRA_LLT_IEEE80211, "IEEE 802.11");
llts(ZEBRA_LLT_IEEE80211_RADIOTAP, "IEEE 802.11 Radiotap");
llts(ZEBRA_LLT_IEEE802154, "IEEE 802.15.4");
llts(ZEBRA_LLT_IEEE802154_PHY, "IEEE 802.15.4 Phy");
default:
zlog_warn ("Unknown value %d", llt);
return "Unknown type!";
#undef llts
}
return NULL;
}
struct if_link_params *
if_link_params_get (struct interface *ifp)
{
int i;
if (ifp->link_params != NULL)
return ifp->link_params;
struct if_link_params *iflp = XCALLOC(MTYPE_IF_LINK_PARAMS,
sizeof (struct if_link_params));
if (iflp == NULL) return NULL;
/* Set TE metric == standard metric */
iflp->te_metric = ifp->metric;
/* Compute default bandwidth based on interface */
int bw = (float)((ifp->bandwidth ? ifp->bandwidth : DEFAULT_BANDWIDTH)
* TE_KILO_BIT / TE_BYTE);
/* Set Max, Reservable and Unreserved Bandwidth */
iflp->max_bw = bw;
iflp->max_rsv_bw = bw;
for (i = 0; i < MAX_CLASS_TYPE; i++)
iflp->unrsv_bw[i] = bw;
/* Update Link parameters status */
iflp->lp_status = LP_TE | LP_MAX_BW | LP_MAX_RSV_BW | LP_UNRSV_BW;
/* Finally attach newly created Link Parameters */
ifp->link_params = iflp;
return iflp;
}
void
if_link_params_free (struct interface *ifp)
{
if (ifp->link_params == NULL) return;
XFREE(MTYPE_IF_LINK_PARAMS, ifp->link_params);
ifp->link_params = NULL;
}
|