Skip to content

Commit b69bca5

Browse files
authored
bird: add option to use vrf (#47)
1 parent febea78 commit b69bca5

3 files changed

Lines changed: 57 additions & 13 deletions

File tree

modules/bird2.nix

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
let
44
cfg = config.networking.dn42;
5+
useVrf = cfg.vrf.name != null && cfg.vrf.table != null;
56
in
67
{
78
config = lib.mkIf cfg.enable {
@@ -35,8 +36,12 @@ in
3536
return net ~ [${builtins.concatStringsSep ", " cfg.nets.v6}];
3637
}
3738
38-
roa4 table dnroa4;
39-
roa6 table dnroa6;
39+
${lib.optionalString useVrf ''
40+
ipv4 table ${cfg.vrf.name}_4;
41+
ipv6 table ${cfg.vrf.name}_6;
42+
''}
43+
roa4 table dnroa_4;
44+
roa6 table dnroa_6;
4045
4146
include "${../resources/community_filter.conf}";
4247
include "${../resources/filters.conf}";
@@ -62,8 +67,8 @@ in
6267
protocols = {
6368

6469
rpki.roa_dn42 = lib.mkIf config.networking.dn42.stayrtr.enable ''
65-
roa4 { table dnroa4; };
66-
roa6 { table dnroa6; };
70+
roa4 { table dnroa_4; };
71+
roa6 { table dnroa_6; };
6772
remote 127.0.0.1;
6873
port 8082;
6974
refresh 600;
@@ -73,22 +78,28 @@ in
7378

7479
static = {
7580
static_roa_4 = lib.mkIf config.networking.dn42.roagen.enable ''
76-
roa4 { table dnroa4; };
81+
roa4 { table dnroa_4; };
7782
include "${config.networking.dn42.roagen.outputDir}/dn42-roa4.conf";
7883
'';
7984
static_roa_6 = lib.mkIf config.networking.dn42.roagen.enable ''
80-
roa6 { table dnroa6; };
85+
roa6 { table dnroa_6; };
8186
include "${config.networking.dn42.roagen.outputDir}/dn42-roa6.conf";
8287
'';
8388
static_4 = ''
84-
ipv4;
89+
${lib.optionalString useVrf "vrf \"${cfg.vrf.name}\";"}
90+
ipv4 {
91+
${lib.optionalString useVrf "table ${cfg.vrf.name}_4;"}
92+
};
8593
8694
${lib.concatMapStrings (net: ''
8795
route ${net} unreachable;
8896
'') cfg.nets.v4}
8997
'';
9098
static_6 = ''
91-
ipv6;
99+
${lib.optionalString useVrf "vrf \"${cfg.vrf.name}\";"}
100+
ipv6 {
101+
${lib.optionalString useVrf "table ${cfg.vrf.name}_6;"}
102+
};
92103
93104
${lib.concatMapStrings (net: ''
94105
route ${net} unreachable;
@@ -102,9 +113,15 @@ in
102113

103114
kernel = {
104115
kernel_4 = ''
116+
${lib.optionalString useVrf ''
117+
vrf "${cfg.vrf.name}";
118+
kernel table ${toString cfg.vrf.table};
119+
''}
105120
scan time 20;
106121
107122
ipv4 {
123+
${lib.optionalString useVrf "table ${cfg.vrf.name}_4;"}
124+
108125
import none;
109126
export filter {
110127
if source = RTS_STATIC then reject;
@@ -115,9 +132,15 @@ in
115132
'';
116133

117134
kernel_6 = ''
135+
${lib.optionalString useVrf ''
136+
vrf "${cfg.vrf.name}";
137+
kernel table ${toString cfg.vrf.table};
138+
''}
118139
scan time 20;
119140
120141
ipv6 {
142+
${lib.optionalString useVrf "table ${cfg.vrf.name}_6;"}
143+
121144
import none;
122145
export filter {
123146
if source = RTS_STATIC then reject;
@@ -133,10 +156,12 @@ in
133156
(name: conf:
134157
{
135158
"${name}_4 from dn42_peer" = lib.mkIf (!conf.extendedNextHop) ''
159+
${lib.optionalString useVrf "vrf \"${cfg.vrf.name}\";"}
136160
neighbor ${conf.addr.v4} as ${builtins.toString conf.as};
137161
source address ${conf.srcAddr.v4};
138162
139163
ipv4 {
164+
${lib.optionalString useVrf "table ${cfg.vrf.name}_4;"}
140165
import limit 9000 action block;
141166
import table on;
142167
import where dn_import_filter4(${toString conf.latency}, ${toString conf.bandwidth}, ${toString conf.crypto});
@@ -145,10 +170,15 @@ in
145170
'';
146171

147172
"${name}_6 from dn42_peer" = ''
173+
${lib.optionalString useVrf "vrf \"${cfg.vrf.name}\";"}
174+
neighbor ${conf.addr.v6}%'${conf.interface}' as ${builtins.toString conf.as};
175+
source address ${conf.srcAddr.v6};
176+
148177
${lib.optionalString conf.extendedNextHop ''
149178
enable extended messages on;
150179
151180
ipv4 {
181+
${lib.optionalString useVrf "table ${cfg.vrf.name}_4;"}
152182
import limit 9000 action block;
153183
import table on;
154184
@@ -159,27 +189,27 @@ in
159189
''}
160190
161191
ipv6 {
192+
${lib.optionalString useVrf "table ${cfg.vrf.name}_6;"}
162193
import limit 9000 action block;
163194
import table on;
164195
165196
import where dn_import_filter6(${toString conf.latency}, ${toString conf.bandwidth}, ${toString conf.crypto});
166197
export where dn_export_filter6(${toString conf.latency}, ${toString conf.bandwidth}, ${toString conf.crypto}, ${lib.boolToString conf.transit});
167198
};
168-
169-
neighbor ${conf.addr.v6}%'${conf.interface}' as ${builtins.toString conf.as};
170-
source address ${conf.srcAddr.v6};
171199
'';
172200
})
173201
cfg.peers)
174202
++ [{
175203
"collector_6 from dn42_peer" = ''
204+
${lib.optionalString useVrf "vrf \"${cfg.vrf.name}\";"}
176205
neighbor fd42:4242:2601:ac12::1 as 4242422602;
177206
source address ${cfg.addr.v6};
178207
179208
# enable multihop as the collector is not locally connected
180209
multihop;
181210
182211
ipv4 {
212+
${lib.optionalString useVrf "table ${cfg.vrf.name}_4;"}
183213
# export all available paths to the collector
184214
add paths tx;
185215
@@ -189,6 +219,7 @@ in
189219
};
190220
191221
ipv6 {
222+
${lib.optionalString useVrf "table ${cfg.vrf.name}_6;"}
192223
# export all available paths to the collector
193224
add paths tx;
194225

modules/default.nix

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,18 @@ in
130130
};
131131
});
132132
};
133+
134+
vrf = {
135+
name = lib.mkOption {
136+
type = lib.types.strMatching "^[A-Za-z0-9_]+$";
137+
default = "vrf0";
138+
description = "Name of the vrf to use. May differ from the kernel vrf name.";
139+
};
140+
table = lib.mkOption {
141+
type = with lib.types; nullOr int;
142+
default = null;
143+
description = "Kernel routing table number to use.";
144+
};
145+
};
133146
};
134147
}

resources/filters.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ function kernel_export() {
2424
}
2525

2626
function reject_invalid_roa4() {
27-
if (roa_check(dnroa4, net, bgp_path.last) != ROA_VALID) then {
27+
if (roa_check(dnroa_4, net, bgp_path.last) != ROA_VALID) then {
2828
print "Reject: ROA failed|", net, "|", bgp_path;
2929
reject;
3030
}
3131
}
3232

3333
function reject_invalid_roa6() {
34-
if (roa_check(dnroa6, net, bgp_path.last) != ROA_VALID) then {
34+
if (roa_check(dnroa_6, net, bgp_path.last) != ROA_VALID) then {
3535
print "Reject: ROA failed|", net, "|", bgp_path;
3636
reject;
3737
}

0 commit comments

Comments
 (0)