@@ -66,6 +66,7 @@ impl VarkRule {
6666 & self . rule
6767 }
6868}
69+
6970// Varkchain is an iptable chain with extra info
7071pub struct VarkChain < ' a > {
7172 // name of chain
@@ -192,19 +193,23 @@ pub fn create_network_chains(chains: Vec<VarkChain<'_>>) -> NetavarkResult<()> {
192193 Ok ( ( ) )
193194}
194195
195- #[ allow( clippy:: too_many_arguments) ]
196- pub fn get_network_chains < ' a > (
197- conn : & ' a IPTables ,
198- network : IpNet ,
199- network_hash_name : & ' a str ,
200- is_ipv6 : bool ,
201- interface_name : String ,
202- isolation : IsolateOption ,
203- dns_port : u16 ,
204- outbound_addr : Option < IpAddr > ,
205- ) -> Vec < VarkChain < ' a > > {
196+ pub struct NetworkChainConfig {
197+ pub network : IpNet ,
198+ pub network_hash_name : String ,
199+ pub interface_name : String ,
200+ pub isolation : IsolateOption ,
201+ pub dns_port : u16 ,
202+ pub outbound_addr : Option < IpAddr > ,
203+ }
204+
205+ pub fn get_network_chains ( conn : & IPTables , config : NetworkChainConfig ) -> Vec < VarkChain < ' _ > > {
206206 let mut chains = Vec :: new ( ) ;
207- let prefixed_network_hash_name = format ! ( "{}-{}" , "NETAVARK" , network_hash_name) ;
207+ let prefixed_network_hash_name = format ! ( "{}-{}" , "NETAVARK" , config. network_hash_name) ;
208+
209+ let is_ipv6 = match config. network {
210+ IpNet :: V4 ( _) => false ,
211+ IpNet :: V6 ( _) => true ,
212+ } ;
208213
209214 // NETAVARK-HASH
210215 let mut hashed_network_chain = VarkChain :: new (
@@ -216,15 +221,15 @@ pub fn get_network_chains<'a>(
216221 hashed_network_chain. create = true ;
217222
218223 hashed_network_chain. build_rule ( VarkRule :: new (
219- format ! ( "-d {network } -j {ACCEPT}" ) ,
224+ format ! ( "-d {} -j {}" , config . network , ACCEPT ) ,
220225 Some ( TeardownPolicy :: OnComplete ) ,
221226 ) ) ;
222227
223228 let mut multicast_dest = MULTICAST_NET_V4 ;
224229 if is_ipv6 {
225230 multicast_dest = MULTICAST_NET_V6 ;
226231 }
227- if let Some ( addr) = outbound_addr {
232+ if let Some ( addr) = config . outbound_addr {
228233 if !is_ipv6 && addr. is_ipv4 ( ) {
229234 log:: trace!( "Creating SNAT rule with outbound address {}" , addr) ;
230235 hashed_network_chain. build_rule ( VarkRule :: new (
@@ -254,7 +259,7 @@ pub fn get_network_chains<'a>(
254259 let mut postrouting_chain =
255260 VarkChain :: new ( conn, NAT . to_string ( ) , POSTROUTING . to_string ( ) , None ) ;
256261 postrouting_chain. build_rule ( VarkRule :: new (
257- format ! ( "-s {network } -j {prefixed_network_hash_name}" ) ,
262+ format ! ( "-s {} -j {}" , config . network , prefixed_network_hash_name ) ,
258263 Some ( TeardownPolicy :: OnComplete ) ,
259264 ) ) ;
260265 chains. push ( postrouting_chain) ;
@@ -294,7 +299,7 @@ pub fn get_network_chains<'a>(
294299 ) ;
295300 netavark_isolation_chain_3. create = true ;
296301
297- if let IsolateOption :: Normal | IsolateOption :: Strict = isolation {
302+ if let IsolateOption :: Normal | IsolateOption :: Strict = config . isolation {
298303 debug ! ( "Add extra isolate rules" ) ;
299304 // NETAVARK_ISOLATION_1
300305 let mut netavark_isolation_chain_1 = VarkChain :: new (
@@ -312,7 +317,7 @@ pub fn get_network_chains<'a>(
312317 td_policy : Some ( TeardownPolicy :: OnComplete ) ,
313318 } ) ;
314319
315- let netavark_isolation_1_target = if let IsolateOption :: Strict = isolation {
320+ let netavark_isolation_1_target = if let IsolateOption :: Strict = config . isolation {
316321 // NETAVARK_ISOLATION_1 -i bridge_name ! -o bridge_name -j NETAVARK_ISOLATION_3
317322 NETAVARK_ISOLATION_3
318323 } else {
@@ -321,15 +326,16 @@ pub fn get_network_chains<'a>(
321326 } ;
322327 netavark_isolation_chain_1. build_rule ( VarkRule {
323328 rule : format ! (
324- "-i {interface_name} ! -o {interface_name} -j {netavark_isolation_1_target}"
329+ "-i {} ! -o {} -j {}" ,
330+ config. interface_name, config. interface_name, netavark_isolation_1_target
325331 ) ,
326332 position : Some ( ind) ,
327333 td_policy : Some ( TeardownPolicy :: OnComplete ) ,
328334 } ) ;
329335
330336 // NETAVARK_ISOLATION_2 -o bridge_name -j DROP
331337 netavark_isolation_chain_2. build_rule ( VarkRule {
332- rule : format ! ( "-o {} -j {}" , interface_name, "DROP" ) ,
338+ rule : format ! ( "-o {} -j {}" , config . interface_name, "DROP" ) ,
333339 position : Some ( ind) ,
334340 td_policy : Some ( TeardownPolicy :: OnComplete ) ,
335341 } ) ;
@@ -350,7 +356,7 @@ pub fn get_network_chains<'a>(
350356
351357 // NETAVARK_ISOLATION_3 -o bridge_name -j DROP
352358 netavark_isolation_chain_3. build_rule ( VarkRule {
353- rule : format ! ( "-o {} -j {}" , interface_name, "DROP" ) ,
359+ rule : format ! ( "-o {} -j {}" , config . interface_name, "DROP" ) ,
354360 position : Some ( ind) ,
355361 td_policy : Some ( TeardownPolicy :: OnComplete ) ,
356362 } ) ;
@@ -399,7 +405,7 @@ pub fn get_network_chains<'a>(
399405 netavark_input_chain. build_rule ( VarkRule :: new (
400406 format ! (
401407 "-p {} -s {} --dport {} -j {}" ,
402- proto, network, dns_port, ACCEPT
408+ proto, config . network, config . dns_port, ACCEPT
403409 ) ,
404410 Some ( TeardownPolicy :: OnComplete ) ,
405411 ) ) ;
@@ -417,14 +423,17 @@ pub fn get_network_chains<'a>(
417423 // Create incoming traffic rule
418424 // CNI did this by IP address, this is implemented per subnet
419425 netavark_forward_chain. build_rule ( VarkRule :: new (
420- format ! ( "-d {network} -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT" ) ,
426+ format ! (
427+ "-d {} -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT" ,
428+ config. network
429+ ) ,
421430 Some ( TeardownPolicy :: OnComplete ) ,
422431 ) ) ;
423432
424433 // Create outgoing traffic rule
425434 // CNI did this by IP address, this is implemented per subnet
426435 netavark_forward_chain. build_rule ( VarkRule :: new (
427- format ! ( "-s {network } -j ACCEPT" ) ,
436+ format ! ( "-s {} -j ACCEPT" , config . network ) ,
428437 Some ( TeardownPolicy :: OnComplete ) ,
429438 ) ) ;
430439 chains. push ( netavark_forward_chain) ;
0 commit comments