@@ -22,15 +22,15 @@ use crate::{
2222 libs:: mutex:: MutexGuard ,
2323 net:: {
2424 address:: {
25- ipv6_autoconf:: { AddressSetting , PrefixSetting , Setting } ,
25+ ipv6_autoconf:: { self , AddressSetting , PrefixSetting , Setting } ,
2626 set_device_ipv6_disabled,
2727 } ,
2828 rtnl,
2929 } ,
3030 process:: namespace:: net_namespace:: NetNamespace ,
3131} ;
3232
33- use super :: { super :: numeric:: parse_numeric_sysctl, namespace:: SysctlNamespace } ;
33+ use super :: { super :: numeric:: parse_numeric_sysctl, ipv6_stable_secret , namespace:: SysctlNamespace } ;
3434
3535#[ derive( Debug , Clone , Copy ) ]
3636pub ( super ) enum DeviceConfFamily {
@@ -53,6 +53,7 @@ impl DeviceConfFamily {
5353 "forwarding" ,
5454 "keep_addr_on_down" ,
5555 "router_solicitations" ,
56+ "stable_secret" ,
5657 ] ,
5758 }
5859 }
@@ -231,6 +232,7 @@ impl DirOps for DeviceDirOps {
231232 ( DeviceConfFamily :: Ipv6 , "accept_dad" ) => {
232233 DeviceField :: Ipv6Autoconf ( Setting :: Address ( AddressSetting :: AcceptDad ) )
233234 }
235+ ( DeviceConfFamily :: Ipv6 , "stable_secret" ) => DeviceField :: StableSecret ,
234236 ( DeviceConfFamily :: Ipv6 , "addr_gen_mode" ) => {
235237 DeviceField :: Ipv6Autoconf ( Setting :: Address ( AddressSetting :: AddrGenMode ) )
236238 }
@@ -245,6 +247,7 @@ impl DirOps for DeviceDirOps {
245247 }
246248 _ => return Err ( SystemError :: ENOENT ) ,
247249 } ;
250+ let mode = field. mode ( ) ;
248251 Ok ( dir
249252 . cached_children ( )
250253 . write ( )
@@ -255,7 +258,7 @@ impl DirOps for DeviceDirOps {
255258 identity : self . identity . clone ( ) ,
256259 field,
257260 } ,
258- InodeMode :: from_bits_truncate ( 0o644 ) ,
261+ InodeMode :: from_bits_truncate ( mode ) ,
259262 )
260263 . parent ( dir. self_ref_weak ( ) . clone ( ) )
261264 . sysctl_permissions ( )
@@ -280,6 +283,17 @@ enum DeviceField {
280283 Ipv6AcceptRa ,
281284 Ipv6Autoconf ( Setting ) ,
282285 Ipv6Prefix ( PrefixSetting ) ,
286+ StableSecret ,
287+ }
288+
289+ impl DeviceField {
290+ fn mode ( & self ) -> u32 {
291+ if matches ! ( self , Self :: StableSecret ) {
292+ ipv6_stable_secret:: MODE
293+ } else {
294+ 0o644
295+ }
296+ }
283297}
284298
285299#[ derive( Debug ) ]
@@ -306,7 +320,10 @@ impl FileOps for DeviceFileOps {
306320
307321 fn sysctl_access_bits ( & self ) -> Result < Option < u32 > , SystemError > {
308322 let ( netns, _) = self . identity . resolve ( ) ?;
309- Ok ( Some ( SysctlNamespace :: access_bits_for ( & netns) ) )
323+ Ok ( Some ( SysctlNamespace :: access_bits_for_mode (
324+ & netns,
325+ self . field . mode ( ) ,
326+ ) ) )
310327 }
311328
312329 fn owner ( & self ) -> Option < ( usize , usize ) > {
@@ -321,15 +338,23 @@ impl FileOps for DeviceFileOps {
321338 Some ( |ops, offset, _, buf, _data| {
322339 let rtnl = rtnl:: lock ( ) ;
323340 let ( netns, iface) = ops. identity . resolve ( ) ?;
324- if SysctlNamespace :: access_bits_for ( & netns) & 2 == 0 {
341+ if SysctlNamespace :: access_bits_for_mode ( & netns, ops . field . mode ( ) ) & 2 == 0 {
325342 return Err ( SystemError :: EPERM ) ;
326343 }
344+ if matches ! ( ops. field, DeviceField :: StableSecret ) {
345+ let current = ipv6_autoconf:: device_stable_secret ( iface. common ( ) ) ;
346+ if let Some ( secret) = ipv6_stable_secret:: parse_write ( current, offset, buf) ? {
347+ ipv6_autoconf:: set_device_stable_secret ( & rtnl, iface. common ( ) , secret) ;
348+ }
349+ return Ok ( PrimaryWriteResult :: new ( buf. len ( ) ) ) ;
350+ }
327351 if buf. is_empty ( ) || offset != 0 {
328352 return Ok ( PrimaryWriteResult :: new ( buf. len ( ) ) ) ;
329353 }
330354 let ( value, consumed) = parse_numeric_sysctl ( buf) ?;
331355 let value = i32:: try_from ( value) . map_err ( |_| SystemError :: EINVAL ) ?;
332356 match ops. field {
357+ DeviceField :: StableSecret => unreachable ! ( ) ,
333358 DeviceField :: RouteLocalnet => iface. common ( ) . set_route_localnet ( & rtnl, value) ,
334359 DeviceField :: DisableIpv6 => set_device_ipv6_disabled ( & rtnl, & netns, & iface, value) ?,
335360 DeviceField :: Ipv6Forwarding => {
@@ -365,11 +390,23 @@ impl FileOps for DeviceFileOps {
365390 _data : MutexGuard < FilePrivateData > ,
366391 ) -> Result < usize , SystemError > {
367392 let _rtnl = rtnl:: lock ( ) ;
368- let ( _, iface) = self . identity . resolve ( ) ?;
393+ let ( netns, iface) = self . identity . resolve ( ) ?;
394+ if matches ! ( self . field, DeviceField :: StableSecret ) {
395+ if SysctlNamespace :: access_bits_for_mode ( & netns, self . field . mode ( ) ) & 4 == 0 {
396+ return Err ( SystemError :: EPERM ) ;
397+ }
398+ return ipv6_stable_secret:: read (
399+ ipv6_autoconf:: device_stable_secret ( iface. common ( ) ) ,
400+ offset,
401+ len,
402+ buf,
403+ ) ;
404+ }
369405 if offset != 0 {
370406 return Ok ( 0 ) ;
371407 }
372408 let value = match self . field {
409+ DeviceField :: StableSecret => unreachable ! ( ) ,
373410 DeviceField :: RouteLocalnet => iface. common ( ) . route_localnet_value ( ) ,
374411 DeviceField :: DisableIpv6 => iface. common ( ) . ipv6_disabled_value ( ) ,
375412 DeviceField :: Ipv6Forwarding => iface. common ( ) . ipv6_forwarding ( ) ,
0 commit comments