@@ -6,16 +6,18 @@ package ebtables
66import (
77 "fmt"
88 "io/ioutil"
9+ "net"
910 "os/exec"
1011 "strings"
1112
1213 "github.com/Azure/azure-container-networking/log"
1314)
1415
15- // Init initializes the ebtables module.
16- func init () {
17- installEbtables ()
18- }
16+ const (
17+ // Ebtables actions.
18+ Append = "-A"
19+ Delete = "-D"
20+ )
1921
2022// InstallEbtables installs the ebtables package.
2123func installEbtables () {
@@ -31,64 +33,31 @@ func installEbtables() {
3133 }
3234}
3335
34- // SetupSnatForOutgoingPackets sets up snat
35- func SetupSnatForOutgoingPackets (interfaceName string , snatAddress string ) error {
36- command := fmt .Sprintf ("ebtables -t nat -A POSTROUTING -o %s -j snat --to-source %s --snat-arp" , interfaceName , snatAddress )
37- err := executeShellCommand (command )
38- if err != nil {
39- return err
40- }
41- return nil
42- }
36+ // SetSnatForInterface sets a MAC SNAT rule for an interface.
37+ func SetSnatForInterface (interfaceName string , macAddress net.HardwareAddr , action string ) error {
38+ command := fmt .Sprintf (
39+ "ebtables -t nat %s POSTROUTING -o %s -j snat --to-src %s --snat-arp" ,
40+ action , interfaceName , macAddress .String ())
4341
44- // CleanupSnatForOutgoingPackets cleans up snat
45- func CleanupSnatForOutgoingPackets (interfaceName string , snatAddress string ) error {
46- command := fmt .Sprintf ("ebtables -t nat -D POSTROUTING -o %s -j snat --to-source %s --snat-arp" , interfaceName , snatAddress )
47- err := executeShellCommand (command )
48- if err != nil {
49- return err
50- }
51- return nil
42+ return executeShellCommand (command )
5243}
5344
54- // SetupDnatForArpReplies sets up dnat
55- func SetupDnatForArpReplies (interfaceName string ) error {
56- command := fmt .Sprintf ("ebtables -t nat -A PREROUTING -i %s -p arp -j dnat --to-destination ff:ff:ff:ff:ff:ff" , interfaceName )
57- err := executeShellCommand (command )
58- if err != nil {
59- return err
60- }
61- return nil
62- }
45+ // SetDnatForArpReplies sets a MAC DNAT rule for ARP replies received on an interface.
46+ func SetDnatForArpReplies (interfaceName string , action string ) error {
47+ command := fmt .Sprintf (
48+ "ebtables -t nat %s PREROUTING -p ARP -i %s -j dnat --to-dst ff:ff:ff:ff:ff:ff" ,
49+ action , interfaceName )
6350
64- // CleanupDnatForArpReplies cleans up dnat
65- func CleanupDnatForArpReplies (interfaceName string ) error {
66- command := fmt .Sprintf ("ebtables -t nat -D PREROUTING -i %s -p arp -j dnat --to-destination ff:ff:ff:ff:ff:ff" , interfaceName )
67- err := executeShellCommand (command )
68- if err != nil {
69- return err
70- }
71- return nil
51+ return executeShellCommand (command )
7252}
7353
74- // SetupDnatBasedOnIPV4Address sets up dnat
75- func SetupDnatBasedOnIPV4Address (ipv4Address string , macAddress string ) error {
76- command := fmt .Sprintf ("ebtables -t nat -A PREROUTING -p IPv4 --ip-dst %s -j dnat --to-dst %s --dnat-target ACCEPT" , ipv4Address , macAddress )
77- err := executeShellCommand (command )
78- if err != nil {
79- return err
80- }
81- return nil
82- }
54+ // SetDnatForIPAddress sets a MAC DNAT rule for an IP address.
55+ func SetDnatForIPAddress (ipAddress net.IP , macAddress net.HardwareAddr , action string ) error {
56+ command := fmt .Sprintf (
57+ "ebtables -t nat %s PREROUTING -p IPv4 --ip-dst %s -j dnat --to-dst %s" ,
58+ action , ipAddress .String (), macAddress .String ())
8359
84- // RemoveDnatBasedOnIPV4Address cleans up dnat
85- func RemoveDnatBasedOnIPV4Address (ipv4Address string , macAddress string ) error {
86- command := fmt .Sprintf ("ebtables -t nat -D PREROUTING -p IPv4 --ip-dst %s -j dnat --to-dst %s --dnat-target ACCEPT" , ipv4Address , macAddress )
87- err := executeShellCommand (command )
88- if err != nil {
89- return err
90- }
91- return nil
60+ return executeShellCommand (command )
9261}
9362
9463func executeShellCommand (command string ) error {
0 commit comments