@@ -112,8 +112,8 @@ func (e *encrMap) String() string {
112112 return b .String ()
113113}
114114
115- func (d * driver ) checkEncryption (nid string , rIP net.IP , vxlanID uint32 , isLocal , add bool ) error {
116- logrus .Debugf ("checkEncryption(%.7s, %v, %d, % t)" , nid , rIP , vxlanID , isLocal )
115+ func (d * driver ) checkEncryption (nid string , rIP net.IP , isLocal , add bool ) error {
116+ logrus .Debugf ("checkEncryption(%.7s, %v, %t)" , nid , rIP , isLocal )
117117
118118 n := d .network (nid )
119119 if n == nil || ! n .secure {
@@ -148,7 +148,7 @@ func (d *driver) checkEncryption(nid string, rIP net.IP, vxlanID uint32, isLocal
148148
149149 if add {
150150 for _ , rIP := range nodes {
151- if err := setupEncryption (lIP , aIP , rIP , vxlanID , d .secMap , d .keys ); err != nil {
151+ if err := setupEncryption (lIP , aIP , rIP , d .secMap , d .keys ); err != nil {
152152 logrus .Warnf ("Failed to program network encryption between %s and %s: %v" , lIP , rIP , err )
153153 }
154154 }
@@ -163,22 +163,14 @@ func (d *driver) checkEncryption(nid string, rIP net.IP, vxlanID uint32, isLocal
163163 return nil
164164}
165165
166- func setupEncryption (localIP , advIP , remoteIP net.IP , vni uint32 , em * encrMap , keys []* key ) error {
167- logrus .Debugf ("Programming encryption for vxlan %d between %s and %s" , vni , localIP , remoteIP )
166+ // setupEncryption programs the encryption parameters for secure communication
167+ // between the local node and a remote node.
168+ func setupEncryption (localIP , advIP , remoteIP net.IP , em * encrMap , keys []* key ) error {
169+ logrus .Debugf ("Programming encryption between %s and %s" , localIP , remoteIP )
168170 rIPs := remoteIP .String ()
169171
170172 indices := make ([]* spi , 0 , len (keys ))
171173
172- err := programMangle (vni , true )
173- if err != nil {
174- logrus .Warn (err )
175- }
176-
177- err = programInput (vni , true )
178- if err != nil {
179- logrus .Warn (err )
180- }
181-
182174 for i , k := range keys {
183175 spis := & spi {buildSPI (advIP , remoteIP , k .tag ), buildSPI (remoteIP , advIP , k .tag )}
184176 dir := reverse
@@ -233,37 +225,33 @@ func removeEncryption(localIP, remoteIP net.IP, em *encrMap) error {
233225 return nil
234226}
235227
236- func programMangle (vni uint32 , add bool ) ( err error ) {
228+ func programMangle (vni uint32 , add bool ) error {
237229 var (
238230 p = strconv .FormatUint (uint64 (overlayutils .VXLANUDPPort ()), 10 )
239231 c = fmt .Sprintf ("0>>22&0x3C@12&0xFFFFFF00=%d" , int (vni )<< 8 )
240232 m = strconv .FormatUint (mark , 10 )
241233 chain = "OUTPUT"
242234 rule = []string {"-p" , "udp" , "--dport" , p , "-m" , "u32" , "--u32" , c , "-j" , "MARK" , "--set-mark" , m }
243- a = "-A"
235+ a = iptables . Append
244236 action = "install"
245237 )
246238
247239 // TODO IPv6 support
248240 iptable := iptables .GetIptable (iptables .IPv4 )
249241
250- if add == iptable .Exists (iptables .Mangle , chain , rule ... ) {
251- return
252- }
253-
254242 if ! add {
255- a = "-D"
243+ a = iptables . Delete
256244 action = "remove"
257245 }
258246
259- if err = iptable .RawCombinedOutput ( append ([] string { "-t" , string ( iptables .Mangle ), a , chain } , rule ... ) ... ); err != nil {
260- logrus . Warnf ("could not %s mangle rule: %v " , action , err )
247+ if err : = iptable .ProgramRule ( iptables .Mangle , chain , a , rule ); err != nil {
248+ return fmt . Errorf ("could not %s mangle rule: %w " , action , err )
261249 }
262250
263- return
251+ return nil
264252}
265253
266- func programInput (vni uint32 , add bool ) ( err error ) {
254+ func programInput (vni uint32 , add bool ) error {
267255 var (
268256 port = strconv .FormatUint (uint64 (overlayutils .VXLANUDPPort ()), 10 )
269257 vniMatch = fmt .Sprintf ("0>>22&0x3C@12&0xFFFFFF00=%d" , int (vni )<< 8 )
@@ -286,15 +274,15 @@ func programInput(vni uint32, add bool) (err error) {
286274
287275 // Accept incoming VXLAN datagrams for the VNI which were subjected to IPSec processing.
288276 if err := iptable .ProgramRule (iptables .Filter , chain , action , accept ); err != nil {
289- logrus .Errorf ("could not %s input rule: %v. Please do it manually. " , msg , err )
277+ return fmt .Errorf ("could not %s input accept rule: %w " , msg , err )
290278 }
291279
292280 // Drop incoming VXLAN datagrams for the VNI which were received in cleartext.
293281 if err := iptable .ProgramRule (iptables .Filter , chain , action , block ); err != nil {
294- logrus .Errorf ("could not %s input rule: %v. Please do it manually. " , msg , err )
282+ return fmt .Errorf ("could not %s input drop rule: %w " , msg , err )
295283 }
296284
297- return
285+ return nil
298286}
299287
300288func programSA (localIP , remoteIP net.IP , spi * spi , k * key , dir int , add bool ) (fSA * netlink.XfrmState , rSA * netlink.XfrmState , err error ) {
0 commit comments