-
Notifications
You must be signed in to change notification settings - Fork 313
/
iproute.c
2391 lines (2088 loc) · 61 KB
/
iproute.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
/*
* iproute.c "ip route".
*
* This program 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 of the License, or (at your option) any later version.
*
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <linux/in_route.h>
#include <linux/icmpv6.h>
#include <errno.h>
#include "rt_names.h"
#include "utils.h"
#include "ip_common.h"
#include "nh_common.h"
#ifndef RTAX_RTTVAR
#define RTAX_RTTVAR RTAX_HOPS
#endif
enum list_action {
IPROUTE_LIST,
IPROUTE_FLUSH,
IPROUTE_SAVE,
};
static const char *mx_names[RTAX_MAX+1] = {
[RTAX_MTU] = "mtu",
[RTAX_WINDOW] = "window",
[RTAX_RTT] = "rtt",
[RTAX_RTTVAR] = "rttvar",
[RTAX_SSTHRESH] = "ssthresh",
[RTAX_CWND] = "cwnd",
[RTAX_ADVMSS] = "advmss",
[RTAX_REORDERING] = "reordering",
[RTAX_HOPLIMIT] = "hoplimit",
[RTAX_INITCWND] = "initcwnd",
[RTAX_FEATURES] = "features",
[RTAX_RTO_MIN] = "rto_min",
[RTAX_INITRWND] = "initrwnd",
[RTAX_QUICKACK] = "quickack",
[RTAX_CC_ALGO] = "congctl",
[RTAX_FASTOPEN_NO_COOKIE] = "fastopen_no_cookie"
};
static void usage(void) __attribute__((noreturn));
static void usage(void)
{
fprintf(stderr,
"Usage: ip route { list | flush } SELECTOR\n"
" ip route save SELECTOR\n"
" ip route restore\n"
" ip route showdump\n"
" ip route get [ ROUTE_GET_FLAGS ] ADDRESS\n"
" [ from ADDRESS iif STRING ]\n"
" [ oif STRING ] [ tos TOS ]\n"
" [ mark NUMBER ] [ vrf NAME ]\n"
" [ uid NUMBER ] [ ipproto PROTOCOL ]\n"
" [ sport NUMBER ] [ dport NUMBER ]\n"
" ip route { add | del | change | append | replace } ROUTE\n"
"SELECTOR := [ root PREFIX ] [ match PREFIX ] [ exact PREFIX ]\n"
" [ table TABLE_ID ] [ vrf NAME ] [ proto RTPROTO ]\n"
" [ type TYPE ] [ scope SCOPE ]\n"
"ROUTE := NODE_SPEC [ INFO_SPEC ]\n"
"NODE_SPEC := [ TYPE ] PREFIX [ tos TOS ]\n"
" [ table TABLE_ID ] [ proto RTPROTO ]\n"
" [ scope SCOPE ] [ metric METRIC ]\n"
" [ ttl-propagate { enabled | disabled } ]\n"
"INFO_SPEC := { NH | nhid ID } OPTIONS FLAGS [ nexthop NH ]...\n"
"NH := [ encap ENCAPTYPE ENCAPHDR ] [ via [ FAMILY ] ADDRESS ]\n"
" [ dev STRING ] [ weight NUMBER ] NHFLAGS\n"
"FAMILY := [ inet | inet6 | mpls | bridge | link ]\n"
"OPTIONS := FLAGS [ mtu NUMBER ] [ advmss NUMBER ] [ as [ to ] ADDRESS ]\n"
" [ rtt TIME ] [ rttvar TIME ] [ reordering NUMBER ]\n"
" [ window NUMBER ] [ cwnd NUMBER ] [ initcwnd NUMBER ]\n"
" [ ssthresh NUMBER ] [ realms REALM ] [ src ADDRESS ]\n"
" [ rto_min TIME ] [ hoplimit NUMBER ] [ initrwnd NUMBER ]\n"
" [ features FEATURES ] [ quickack BOOL ] [ congctl NAME ]\n"
" [ pref PREF ] [ expires TIME ] [ fastopen_no_cookie BOOL ]\n"
"TYPE := { unicast | local | broadcast | multicast | throw |\n"
" unreachable | prohibit | blackhole | nat }\n"
"TABLE_ID := [ local | main | default | all | NUMBER ]\n"
"SCOPE := [ host | link | global | NUMBER ]\n"
"NHFLAGS := [ onlink | pervasive ]\n"
"RTPROTO := [ kernel | boot | static | NUMBER ]\n"
"PREF := [ low | medium | high ]\n"
"TIME := NUMBER[s|ms]\n"
"BOOL := [1|0]\n"
"FEATURES := ecn\n"
"ENCAPTYPE := [ mpls | ip | ip6 | seg6 | seg6local | rpl | ioam6 ]\n"
"ENCAPHDR := [ MPLSLABEL | SEG6HDR | SEG6LOCAL | IOAM6HDR ]\n"
"SEG6HDR := [ mode SEGMODE ] segs ADDR1,ADDRi,ADDRn [hmac HMACKEYID] [cleanup]\n"
"SEGMODE := [ encap | inline ]\n"
"SEG6LOCAL := action ACTION [ OPTIONS ] [ count ]\n"
"ACTION := { End | End.X | End.T | End.DX2 | End.DX6 | End.DX4 |\n"
" End.DT6 | End.DT4 | End.DT46 | End.B6 | End.B6.Encaps |\n"
" End.BM | End.S | End.AS | End.AM | End.BPF }\n"
"OPTIONS := OPTION [ OPTIONS ]\n"
"OPTION := { srh SEG6HDR | nh4 ADDR | nh6 ADDR | iif DEV | oif DEV |\n"
" table TABLEID | vrftable TABLEID | endpoint PROGNAME }\n"
"IOAM6HDR := trace prealloc type IOAM6_TRACE_TYPE ns IOAM6_NAMESPACE size IOAM6_TRACE_SIZE\n"
"ROUTE_GET_FLAGS := [ fibmatch ]\n");
exit(-1);
}
static struct
{
unsigned int tb;
int cloned;
int flushed;
char *flushb;
int flushp;
int flushe;
int protocol, protocolmask;
int scope, scopemask;
__u64 typemask;
int tos, tosmask;
int iif, iifmask;
int oif, oifmask;
int mark, markmask;
int realm, realmmask;
__u32 metric, metricmask;
inet_prefix rprefsrc;
inet_prefix rvia;
inet_prefix rdst;
inet_prefix mdst;
inet_prefix rsrc;
inet_prefix msrc;
} filter;
static int flush_update(void)
{
if (rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) {
perror("Failed to send flush request");
return -2;
}
filter.flushp = 0;
return 0;
}
static int filter_nlmsg(struct nlmsghdr *n, struct rtattr **tb, int host_len)
{
struct rtmsg *r = NLMSG_DATA(n);
inet_prefix dst = { .family = r->rtm_family };
inet_prefix src = { .family = r->rtm_family };
inet_prefix via = { .family = r->rtm_family };
inet_prefix prefsrc = { .family = r->rtm_family };
__u32 table;
static int ip6_multiple_tables;
table = rtm_get_table(r, tb);
if (preferred_family != AF_UNSPEC && r->rtm_family != preferred_family)
return 0;
if (r->rtm_family == AF_INET6 && table != RT_TABLE_MAIN)
ip6_multiple_tables = 1;
if (filter.cloned == !(r->rtm_flags & RTM_F_CLONED))
return 0;
if (r->rtm_family == AF_INET6 && !ip6_multiple_tables) {
if (filter.tb) {
if (filter.tb == RT_TABLE_LOCAL) {
if (r->rtm_type != RTN_LOCAL)
return 0;
} else if (filter.tb == RT_TABLE_MAIN) {
if (r->rtm_type == RTN_LOCAL)
return 0;
} else {
return 0;
}
}
} else {
if (filter.tb > 0 && filter.tb != table)
return 0;
}
if ((filter.protocol^r->rtm_protocol)&filter.protocolmask)
return 0;
if ((filter.scope^r->rtm_scope)&filter.scopemask)
return 0;
if (filter.typemask && !(filter.typemask & (1 << r->rtm_type)))
return 0;
if ((filter.tos^r->rtm_tos)&filter.tosmask)
return 0;
if (filter.rdst.family) {
if (r->rtm_family != filter.rdst.family ||
filter.rdst.bitlen > r->rtm_dst_len)
return 0;
} else if (filter.rdst.flags & PREFIXLEN_SPECIFIED) {
if (filter.rdst.bitlen > r->rtm_dst_len)
return 0;
}
if (filter.mdst.family) {
if (r->rtm_family != filter.mdst.family ||
(filter.mdst.bitlen >= 0 &&
filter.mdst.bitlen < r->rtm_dst_len))
return 0;
} else if (filter.mdst.flags & PREFIXLEN_SPECIFIED) {
if (filter.mdst.bitlen >= 0 &&
filter.mdst.bitlen < r->rtm_dst_len)
return 0;
}
if (filter.rsrc.family) {
if (r->rtm_family != filter.rsrc.family ||
filter.rsrc.bitlen > r->rtm_src_len)
return 0;
} else if (filter.rsrc.flags & PREFIXLEN_SPECIFIED) {
if (filter.rsrc.bitlen > r->rtm_src_len)
return 0;
}
if (filter.msrc.family) {
if (r->rtm_family != filter.msrc.family ||
(filter.msrc.bitlen >= 0 &&
filter.msrc.bitlen < r->rtm_src_len))
return 0;
} else if (filter.msrc.flags & PREFIXLEN_SPECIFIED) {
if (filter.msrc.bitlen >= 0 &&
filter.msrc.bitlen < r->rtm_src_len)
return 0;
}
if (filter.rvia.family) {
int family = r->rtm_family;
if (tb[RTA_VIA]) {
struct rtvia *via = RTA_DATA(tb[RTA_VIA]);
family = via->rtvia_family;
}
if (family != filter.rvia.family)
return 0;
}
if (filter.rprefsrc.family && r->rtm_family != filter.rprefsrc.family)
return 0;
if (tb[RTA_DST])
memcpy(&dst.data, RTA_DATA(tb[RTA_DST]), (r->rtm_dst_len+7)/8);
if (filter.rsrc.family || filter.msrc.family ||
filter.rsrc.flags & PREFIXLEN_SPECIFIED ||
filter.msrc.flags & PREFIXLEN_SPECIFIED) {
if (tb[RTA_SRC])
memcpy(&src.data, RTA_DATA(tb[RTA_SRC]), (r->rtm_src_len+7)/8);
}
if (filter.rvia.bitlen > 0) {
if (tb[RTA_GATEWAY])
memcpy(&via.data, RTA_DATA(tb[RTA_GATEWAY]), host_len/8);
if (tb[RTA_VIA]) {
size_t len = RTA_PAYLOAD(tb[RTA_VIA]) - 2;
struct rtvia *rtvia = RTA_DATA(tb[RTA_VIA]);
via.family = rtvia->rtvia_family;
memcpy(&via.data, rtvia->rtvia_addr, len);
}
}
if (filter.rprefsrc.bitlen > 0) {
if (tb[RTA_PREFSRC])
memcpy(&prefsrc.data, RTA_DATA(tb[RTA_PREFSRC]), host_len/8);
}
if ((filter.rdst.family || filter.rdst.flags & PREFIXLEN_SPECIFIED) &&
inet_addr_match(&dst, &filter.rdst, filter.rdst.bitlen))
return 0;
if ((filter.mdst.family || filter.mdst.flags & PREFIXLEN_SPECIFIED) &&
inet_addr_match(&dst, &filter.mdst, r->rtm_dst_len))
return 0;
if ((filter.rsrc.family || filter.rsrc.flags & PREFIXLEN_SPECIFIED) &&
inet_addr_match(&src, &filter.rsrc, filter.rsrc.bitlen))
return 0;
if ((filter.msrc.family || filter.msrc.flags & PREFIXLEN_SPECIFIED) &&
filter.msrc.bitlen >= 0 &&
inet_addr_match(&src, &filter.msrc, r->rtm_src_len))
return 0;
if (filter.rvia.family && inet_addr_match(&via, &filter.rvia, filter.rvia.bitlen))
return 0;
if (filter.rprefsrc.family && inet_addr_match(&prefsrc, &filter.rprefsrc, filter.rprefsrc.bitlen))
return 0;
if (filter.realmmask) {
__u32 realms = 0;
if (tb[RTA_FLOW])
realms = rta_getattr_u32(tb[RTA_FLOW]);
if ((realms^filter.realm)&filter.realmmask)
return 0;
}
if (filter.iifmask) {
int iif = 0;
if (tb[RTA_IIF])
iif = rta_getattr_u32(tb[RTA_IIF]);
if ((iif^filter.iif)&filter.iifmask)
return 0;
}
if (filter.oifmask) {
int oif = 0;
if (tb[RTA_OIF])
oif = rta_getattr_u32(tb[RTA_OIF]);
if ((oif^filter.oif)&filter.oifmask)
return 0;
}
if (filter.markmask) {
int mark = 0;
if (tb[RTA_MARK])
mark = rta_getattr_u32(tb[RTA_MARK]);
if ((mark ^ filter.mark) & filter.markmask)
return 0;
}
if (filter.metricmask) {
__u32 metric = 0;
if (tb[RTA_PRIORITY])
metric = rta_getattr_u32(tb[RTA_PRIORITY]);
if ((metric ^ filter.metric) & filter.metricmask)
return 0;
}
if (filter.flushb &&
r->rtm_family == AF_INET6 &&
r->rtm_dst_len == 0 &&
r->rtm_type == RTN_UNREACHABLE &&
tb[RTA_PRIORITY] &&
rta_getattr_u32(tb[RTA_PRIORITY]) == -1)
return 0;
return 1;
}
static void print_rtax_features(FILE *fp, unsigned int features)
{
unsigned int of = features;
if (features & RTAX_FEATURE_ECN) {
print_null(PRINT_ANY, "ecn", "ecn ", NULL);
features &= ~RTAX_FEATURE_ECN;
}
if (features)
print_0xhex(PRINT_ANY,
"features", "%#llx ", of);
}
void print_rt_flags(FILE *fp, unsigned int flags)
{
open_json_array(PRINT_JSON,
is_json_context() ? "flags" : "");
if (flags & RTNH_F_DEAD)
print_string(PRINT_ANY, NULL, "%s ", "dead");
if (flags & RTNH_F_ONLINK)
print_string(PRINT_ANY, NULL, "%s ", "onlink");
if (flags & RTNH_F_PERVASIVE)
print_string(PRINT_ANY, NULL, "%s ", "pervasive");
if (flags & RTNH_F_OFFLOAD)
print_string(PRINT_ANY, NULL, "%s ", "offload");
if (flags & RTNH_F_TRAP)
print_string(PRINT_ANY, NULL, "%s ", "trap");
if (flags & RTM_F_NOTIFY)
print_string(PRINT_ANY, NULL, "%s ", "notify");
if (flags & RTNH_F_LINKDOWN)
print_string(PRINT_ANY, NULL, "%s ", "linkdown");
if (flags & RTNH_F_UNRESOLVED)
print_string(PRINT_ANY, NULL, "%s ", "unresolved");
if (flags & RTM_F_OFFLOAD)
print_string(PRINT_ANY, NULL, "%s ", "rt_offload");
if (flags & RTM_F_TRAP)
print_string(PRINT_ANY, NULL, "%s ", "rt_trap");
if (flags & RTM_F_OFFLOAD_FAILED)
print_string(PRINT_ANY, NULL, "%s ", "rt_offload_failed");
close_json_array(PRINT_JSON, NULL);
}
static void print_rt_pref(FILE *fp, unsigned int pref)
{
switch (pref) {
case ICMPV6_ROUTER_PREF_LOW:
print_string(PRINT_ANY,
"pref", "pref %s", "low");
break;
case ICMPV6_ROUTER_PREF_MEDIUM:
print_string(PRINT_ANY,
"pref", "pref %s", "medium");
break;
case ICMPV6_ROUTER_PREF_HIGH:
print_string(PRINT_ANY,
"pref", "pref %s", "high");
break;
default:
print_uint(PRINT_ANY,
"pref", "%u", pref);
}
}
void print_rta_ifidx(FILE *fp, __u32 ifidx, const char *prefix)
{
const char *ifname = ll_index_to_name(ifidx);
if (is_json_context()) {
print_string(PRINT_JSON, prefix, NULL, ifname);
} else {
fprintf(fp, "%s ", prefix);
color_fprintf(fp, COLOR_IFNAME, "%s ", ifname);
}
}
static void print_cache_flags(FILE *fp, __u32 flags)
{
json_writer_t *jw = get_json_writer();
flags &= ~0xFFFF;
if (jw) {
jsonw_name(jw, "cache");
jsonw_start_array(jw);
} else {
fprintf(fp, "%s cache ", _SL_);
if (flags == 0)
return;
putc('<', fp);
}
#define PRTFL(fl, flname) \
if (flags & RTCF_##fl) { \
flags &= ~RTCF_##fl; \
if (jw) \
jsonw_string(jw, flname); \
else \
fprintf(fp, "%s%s", flname, flags ? "," : "> "); \
}
PRTFL(LOCAL, "local");
PRTFL(REJECT, "reject");
PRTFL(MULTICAST, "mc");
PRTFL(BROADCAST, "brd");
PRTFL(DNAT, "dst-nat");
PRTFL(SNAT, "src-nat");
PRTFL(MASQ, "masq");
PRTFL(DIRECTDST, "dst-direct");
PRTFL(DIRECTSRC, "src-direct");
PRTFL(REDIRECTED, "redirected");
PRTFL(DOREDIRECT, "redirect");
PRTFL(FAST, "fastroute");
PRTFL(NOTIFY, "notify");
PRTFL(TPROXY, "proxy");
#undef PRTFL
if (flags)
print_hex(PRINT_ANY, "flags", "%x>", flags);
if (jw)
jsonw_end_array(jw);
}
static void print_rta_cacheinfo(FILE *fp, const struct rta_cacheinfo *ci)
{
static int hz;
if (!hz)
hz = get_user_hz();
if (ci->rta_expires != 0)
print_int(PRINT_ANY, "expires",
"expires %dsec ", ci->rta_expires/hz);
if (ci->rta_error != 0)
print_uint(PRINT_ANY, "error",
"error %u ", ci->rta_error);
if (show_stats) {
if (ci->rta_clntref)
print_uint(PRINT_ANY, "users",
"users %u ", ci->rta_clntref);
if (ci->rta_used != 0)
print_uint(PRINT_ANY, "used",
"used %u ", ci->rta_used);
if (ci->rta_lastuse != 0)
print_uint(PRINT_ANY, "age",
"age %usec ", ci->rta_lastuse/hz);
}
if (ci->rta_id)
print_0xhex(PRINT_ANY, "ipid",
"ipid 0x%04llx ", ci->rta_id);
if (ci->rta_ts || ci->rta_tsage) {
print_0xhex(PRINT_ANY, "ts",
"ts 0x%llx", ci->rta_ts);
print_uint(PRINT_ANY, "tsage",
"tsage %usec ", ci->rta_tsage);
}
}
static void print_rta_flow(FILE *fp, const struct rtattr *rta)
{
__u32 to = rta_getattr_u32(rta);
__u32 from = to >> 16;
SPRINT_BUF(b1);
to &= 0xFFFF;
if (is_json_context()) {
open_json_object("flow");
if (from)
print_string(PRINT_JSON, "from", NULL,
rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
print_string(PRINT_JSON, "to", NULL,
rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
close_json_object();
} else {
fprintf(fp, "realm%s ", from ? "s" : "");
if (from)
print_string(PRINT_FP, NULL, "%s/",
rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
print_string(PRINT_FP, NULL, "%s ",
rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
}
}
static void print_rta_newdst(FILE *fp, const struct rtmsg *r,
const struct rtattr *rta)
{
const char *newdst = format_host_rta(r->rtm_family, rta);
if (is_json_context())
print_string(PRINT_JSON, "to", NULL, newdst);
else {
fprintf(fp, "as to ");
print_color_string(PRINT_FP,
ifa_family_color(r->rtm_family),
NULL, "%s ", newdst);
}
}
void __print_rta_gateway(FILE *fp, unsigned char family, const char *gateway)
{
if (is_json_context()) {
print_string(PRINT_JSON, "gateway", NULL, gateway);
} else {
fprintf(fp, "via ");
print_color_string(PRINT_FP,
ifa_family_color(family),
NULL, "%s ", gateway);
}
}
void print_rta_gateway(FILE *fp, unsigned char family, const struct rtattr *rta)
{
const char *gateway = format_host_rta(family, rta);
__print_rta_gateway(fp, family, gateway);
}
static void print_rta_via(FILE *fp, const struct rtattr *rta)
{
size_t len = RTA_PAYLOAD(rta) - 2;
const struct rtvia *via = RTA_DATA(rta);
if (is_json_context()) {
open_json_object("via");
print_string(PRINT_JSON, "family", NULL,
family_name(via->rtvia_family));
print_string(PRINT_JSON, "host", NULL,
format_host(via->rtvia_family, len,
via->rtvia_addr));
close_json_object();
} else {
print_string(PRINT_FP, NULL, "via %s ",
family_name(via->rtvia_family));
print_color_string(PRINT_FP,
ifa_family_color(via->rtvia_family),
NULL, "%s ",
format_host(via->rtvia_family,
len, via->rtvia_addr));
}
}
static void print_rta_metrics(FILE *fp, const struct rtattr *rta)
{
struct rtattr *mxrta[RTAX_MAX+1];
unsigned int mxlock = 0;
int i;
open_json_array(PRINT_JSON, "metrics");
open_json_object(NULL);
parse_rtattr(mxrta, RTAX_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta));
if (mxrta[RTAX_LOCK])
mxlock = rta_getattr_u32(mxrta[RTAX_LOCK]);
for (i = 2; i <= RTAX_MAX; i++) {
__u32 val = 0U;
if (mxrta[i] == NULL && !(mxlock & (1 << i)))
continue;
if (mxrta[i] != NULL && i != RTAX_CC_ALGO)
val = rta_getattr_u32(mxrta[i]);
if (i == RTAX_HOPLIMIT && (int)val == -1)
continue;
if (!is_json_context()) {
if (i < sizeof(mx_names)/sizeof(char *) && mx_names[i])
fprintf(fp, "%s ", mx_names[i]);
else
fprintf(fp, "metric %d ", i);
if (mxlock & (1<<i))
fprintf(fp, "lock ");
}
switch (i) {
case RTAX_FEATURES:
print_rtax_features(fp, val);
break;
default:
print_uint(PRINT_ANY, mx_names[i], "%u ", val);
break;
case RTAX_RTT:
case RTAX_RTTVAR:
case RTAX_RTO_MIN:
if (i == RTAX_RTT)
val /= 8;
else if (i == RTAX_RTTVAR)
val /= 4;
if (is_json_context())
print_uint(PRINT_JSON, mx_names[i],
NULL, val);
else {
if (val >= 1000)
fprintf(fp, "%gs ", val/1e3);
else
fprintf(fp, "%ums ", val);
}
break;
case RTAX_CC_ALGO:
print_string(PRINT_ANY, "congestion",
"%s ", rta_getattr_str(mxrta[i]));
break;
}
}
close_json_object();
close_json_array(PRINT_JSON, NULL);
}
static void print_rta_multipath(FILE *fp, const struct rtmsg *r,
struct rtattr *rta)
{
const struct rtnexthop *nh = RTA_DATA(rta);
int len = RTA_PAYLOAD(rta);
int first = 1;
open_json_array(PRINT_JSON, "nexthops");
while (len >= sizeof(*nh)) {
struct rtattr *tb[RTA_MAX + 1];
if (nh->rtnh_len > len)
break;
open_json_object(NULL);
if ((r->rtm_flags & RTM_F_CLONED) &&
r->rtm_type == RTN_MULTICAST) {
if (first) {
print_string(PRINT_FP, NULL, "Oifs: ", NULL);
first = 0;
} else {
print_string(PRINT_FP, NULL, " ", NULL);
}
} else
print_string(PRINT_FP, NULL, "%s\tnexthop ", _SL_);
if (nh->rtnh_len > sizeof(*nh)) {
parse_rtattr(tb, RTA_MAX, RTNH_DATA(nh),
nh->rtnh_len - sizeof(*nh));
if (tb[RTA_ENCAP])
lwt_print_encap(fp,
tb[RTA_ENCAP_TYPE],
tb[RTA_ENCAP]);
if (tb[RTA_NEWDST])
print_rta_newdst(fp, r, tb[RTA_NEWDST]);
if (tb[RTA_GATEWAY])
print_rta_gateway(fp, r->rtm_family,
tb[RTA_GATEWAY]);
if (tb[RTA_VIA])
print_rta_via(fp, tb[RTA_VIA]);
if (tb[RTA_FLOW])
print_rta_flow(fp, tb[RTA_FLOW]);
}
if ((r->rtm_flags & RTM_F_CLONED) &&
r->rtm_type == RTN_MULTICAST) {
print_string(PRINT_ANY, "dev",
"%s", ll_index_to_name(nh->rtnh_ifindex));
if (nh->rtnh_hops != 1)
print_int(PRINT_ANY, "ttl", "(ttl>%d)", nh->rtnh_hops);
print_string(PRINT_FP, NULL, " ", NULL);
} else {
print_string(PRINT_ANY, "dev",
"dev %s ", ll_index_to_name(nh->rtnh_ifindex));
if (r->rtm_family != AF_MPLS)
print_int(PRINT_ANY, "weight",
"weight %d ", nh->rtnh_hops + 1);
}
print_rt_flags(fp, nh->rtnh_flags);
len -= NLMSG_ALIGN(nh->rtnh_len);
nh = RTNH_NEXT(nh);
close_json_object();
}
close_json_array(PRINT_JSON, NULL);
}
int print_route(struct nlmsghdr *n, void *arg)
{
FILE *fp = (FILE *)arg;
struct rtmsg *r = NLMSG_DATA(n);
int len = n->nlmsg_len;
struct rtattr *tb[RTA_MAX+1];
int family, color, host_len;
__u32 table;
int ret;
SPRINT_BUF(b1);
if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) {
fprintf(stderr, "Not a route: %08x %08x %08x\n",
n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
return -1;
}
if (filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
return 0;
len -= NLMSG_LENGTH(sizeof(*r));
if (len < 0) {
fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
return -1;
}
host_len = af_bit_len(r->rtm_family);
parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
table = rtm_get_table(r, tb);
if (!filter_nlmsg(n, tb, host_len))
return 0;
if (filter.flushb) {
struct nlmsghdr *fn;
if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
ret = flush_update();
if (ret < 0)
return ret;
}
fn = (struct nlmsghdr *)(filter.flushb + NLMSG_ALIGN(filter.flushp));
memcpy(fn, n, n->nlmsg_len);
fn->nlmsg_type = RTM_DELROUTE;
fn->nlmsg_flags = NLM_F_REQUEST;
fn->nlmsg_seq = ++rth.seq;
filter.flushp = (((char *)fn) + n->nlmsg_len) - filter.flushb;
filter.flushed++;
if (show_stats < 2)
return 0;
}
open_json_object(NULL);
if (n->nlmsg_type == RTM_DELROUTE)
print_bool(PRINT_ANY, "deleted", "Deleted ", true);
if ((r->rtm_type != RTN_UNICAST || show_details > 0) &&
(!filter.typemask || (filter.typemask & (1 << r->rtm_type))))
print_string(PRINT_ANY, "type", "%s ",
rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
color = COLOR_NONE;
if (tb[RTA_DST]) {
family = get_real_family(r->rtm_type, r->rtm_family);
color = ifa_family_color(family);
if (r->rtm_dst_len != host_len) {
snprintf(b1, sizeof(b1),
"%s/%u", rt_addr_n2a_rta(family, tb[RTA_DST]),
r->rtm_dst_len);
} else {
const char *hostname = format_host_rta_r(family, tb[RTA_DST],
b1, sizeof(b1));
if (hostname)
strncpy(b1, hostname, sizeof(b1) - 1);
}
} else if (r->rtm_dst_len) {
snprintf(b1, sizeof(b1), "0/%d ", r->rtm_dst_len);
} else {
strncpy(b1, "default", sizeof(b1));
}
print_color_string(PRINT_ANY, color,
"dst", "%s ", b1);
if (tb[RTA_SRC]) {
family = get_real_family(r->rtm_type, r->rtm_family);
color = ifa_family_color(family);
if (r->rtm_src_len != host_len) {
snprintf(b1, sizeof(b1),
"%s/%u",
rt_addr_n2a_rta(family, tb[RTA_SRC]),
r->rtm_src_len);
} else {
const char *hostname = format_host_rta_r(family, tb[RTA_SRC],
b1, sizeof(b1));
if (hostname)
strncpy(b1, hostname, sizeof(b1) - 1);
}
print_color_string(PRINT_ANY, color,
"from", "from %s ", b1);
} else if (r->rtm_src_len) {
snprintf(b1, sizeof(b1), "0/%u", r->rtm_src_len);
print_string(PRINT_ANY, "src", "from %s ", b1);
}
if (tb[RTA_NH_ID])
print_uint(PRINT_ANY, "nhid", "nhid %u ",
rta_getattr_u32(tb[RTA_NH_ID]));
if (tb[RTA_NEWDST])
print_rta_newdst(fp, r, tb[RTA_NEWDST]);
if (tb[RTA_ENCAP])
lwt_print_encap(fp, tb[RTA_ENCAP_TYPE], tb[RTA_ENCAP]);
if (r->rtm_tos && filter.tosmask != -1) {
print_string(PRINT_ANY, "tos", "tos %s ",
rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
}
if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len)
print_rta_gateway(fp, r->rtm_family, tb[RTA_GATEWAY]);
if (tb[RTA_VIA])
print_rta_via(fp, tb[RTA_VIA]);
if (tb[RTA_OIF] && filter.oifmask != -1)
print_rta_ifidx(fp, rta_getattr_u32(tb[RTA_OIF]), "dev");
if (table && (table != RT_TABLE_MAIN || show_details > 0) && !filter.tb)
print_string(PRINT_ANY,
"table", "table %s ",
rtnl_rttable_n2a(table, b1, sizeof(b1)));
if (!(r->rtm_flags & RTM_F_CLONED)) {
if ((r->rtm_protocol != RTPROT_BOOT || show_details > 0) &&
filter.protocolmask != -1)
print_string(PRINT_ANY,
"protocol", "proto %s ",
rtnl_rtprot_n2a(r->rtm_protocol,
b1, sizeof(b1)));
if ((r->rtm_scope != RT_SCOPE_UNIVERSE || show_details > 0) &&
filter.scopemask != -1)
print_string(PRINT_ANY,
"scope", "scope %s ",
rtnl_rtscope_n2a(r->rtm_scope,
b1, sizeof(b1)));
}
if (tb[RTA_PREFSRC] && filter.rprefsrc.bitlen != host_len) {
const char *psrc
= rt_addr_n2a_rta(r->rtm_family, tb[RTA_PREFSRC]);
/* Do not use format_host(). It is our local addr
and symbolic name will not be useful.
*/
if (is_json_context())
print_string(PRINT_JSON, "prefsrc", NULL, psrc);
else {
fprintf(fp, "src ");
print_color_string(PRINT_FP,
ifa_family_color(r->rtm_family),
NULL, "%s ", psrc);
}
}
if (tb[RTA_PRIORITY] && filter.metricmask != -1)
print_uint(PRINT_ANY, "metric", "metric %u ",
rta_getattr_u32(tb[RTA_PRIORITY]));
print_rt_flags(fp, r->rtm_flags);
if (tb[RTA_MARK]) {
unsigned int mark = rta_getattr_u32(tb[RTA_MARK]);
if (mark) {
if (is_json_context())
print_uint(PRINT_JSON, "mark", NULL, mark);
else if (mark >= 16)
print_0xhex(PRINT_FP, NULL,
"mark 0x%llx ", mark);
else
print_uint(PRINT_FP, NULL,
"mark %u ", mark);
}
}
if (tb[RTA_FLOW] && filter.realmmask != ~0U)
print_rta_flow(fp, tb[RTA_FLOW]);
if (tb[RTA_UID])
print_uint(PRINT_ANY, "uid", "uid %u ",
rta_getattr_u32(tb[RTA_UID]));
if (r->rtm_family == AF_INET) {
if (r->rtm_flags & RTM_F_CLONED)
print_cache_flags(fp, r->rtm_flags);
if (tb[RTA_CACHEINFO])
print_rta_cacheinfo(fp, RTA_DATA(tb[RTA_CACHEINFO]));
} else if (r->rtm_family == AF_INET6) {
if (tb[RTA_CACHEINFO])
print_rta_cacheinfo(fp, RTA_DATA(tb[RTA_CACHEINFO]));
}
if (tb[RTA_METRICS])
print_rta_metrics(fp, tb[RTA_METRICS]);
if (tb[RTA_IIF] && filter.iifmask != -1)
print_rta_ifidx(fp, rta_getattr_u32(tb[RTA_IIF]), "iif");
if (tb[RTA_PREF])
print_rt_pref(fp, rta_getattr_u8(tb[RTA_PREF]));
if (tb[RTA_TTL_PROPAGATE]) {
bool propagate = rta_getattr_u8(tb[RTA_TTL_PROPAGATE]);
if (is_json_context())
print_bool(PRINT_JSON, "ttl-propogate", NULL,
propagate);
else
print_string(PRINT_FP, NULL,
"ttl-propogate %s",
propagate ? "enabled" : "disabled");
}
if (tb[RTA_NH_ID] && show_details)
print_cache_nexthop_id(fp, "\n\tnh_info ", "nh_info",
rta_getattr_u32(tb[RTA_NH_ID]));
if (tb[RTA_MULTIPATH])
print_rta_multipath(fp, r, tb[RTA_MULTIPATH]);
/* If you are adding new route RTA_XXXX then place it above
* the RTA_MULTIPATH else it will appear that the last nexthop
* in the ECMP has new attributes
*/
print_string(PRINT_FP, NULL, "\n", NULL);
close_json_object();
fflush(fp);
return 0;
}
static int parse_one_nh(struct nlmsghdr *n, struct rtmsg *r,
struct rtattr *rta, size_t len, struct rtnexthop *rtnh,
int *argcp, char ***argvp)
{
int argc = *argcp;
char **argv = *argvp;
while (++argv, --argc > 0) {
if (strcmp(*argv, "via") == 0) {
inet_prefix addr;
int family;