Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit 0d435a2

Browse files
authored
Merge pull request #21 from aojea/legacy
Only install iptables rule in the FORWARD chain for legacy
2 parents e0459f5 + d9aca66 commit 0d435a2

File tree

2 files changed

+84
-84
lines changed

2 files changed

+84
-84
lines changed

pkg/networkpolicy/controller.go

-84
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package networkpolicy
33
import (
44
"bytes"
55
"context"
6-
"encoding/binary"
7-
"encoding/hex"
86
"fmt"
97
"net"
108
"os/exec"
@@ -387,10 +385,6 @@ func (c *Controller) syncIptablesRules() {
387385
if err := c.ipt.InsertUnique("filter", "FORWARD", 1, queueRule...); err != nil {
388386
klog.Infof("error syncing iptables rule %v", err)
389387
}
390-
391-
if err := c.ipt.InsertUnique("filter", "OUTPUT", 1, queueRule...); err != nil {
392-
klog.Infof("error syncing iptables rule %v", err)
393-
}
394388
}
395389

396390
func (c *Controller) cleanIptablesRules() {
@@ -402,10 +396,6 @@ func (c *Controller) cleanIptablesRules() {
402396
if err := c.ipt.Delete("filter", "FORWARD", queueRule...); err != nil {
403397
klog.Infof("error deleting iptables rule %v", err)
404398
}
405-
406-
if err := c.ipt.Delete("filter", "OUTPUT", queueRule...); err != nil {
407-
klog.Infof("error deleting iptables rule %v", err)
408-
}
409399
}
410400

411401
func (c *Controller) getNetworkPoliciesForPod(pod *v1.Pod) []*networkingv1.NetworkPolicy {
@@ -766,77 +756,3 @@ func (c *Controller) evaluatePorts(networkPolicyPorts []networkingv1.NetworkPoli
766756
}
767757
return false
768758
}
769-
770-
type packet struct {
771-
family v1.IPFamily
772-
srcIP net.IP
773-
dstIP net.IP
774-
proto v1.Protocol
775-
srcPort int
776-
dstPort int
777-
payload []byte
778-
}
779-
780-
func (p packet) String() string {
781-
return fmt.Sprintf("%s:%d %s:%d %s :: %s", p.srcIP.String(), p.srcPort, p.dstIP.String(), p.dstPort, p.proto, hex.Dump(p.payload))
782-
}
783-
784-
// https://en.wikipedia.org/wiki/Internet_Protocol_version_4#Packet_structure
785-
// https://en.wikipedia.org/wiki/IPv6_packet
786-
// https://github.com/golang/net/blob/master/ipv4/header.go
787-
func parsePacket(b []byte) (packet, error) {
788-
t := packet{}
789-
if b == nil {
790-
return t, fmt.Errorf("empty payload")
791-
}
792-
version := int(b[0] >> 4)
793-
// initialize variables
794-
hdrlen := -1
795-
protocol := -1
796-
switch version {
797-
case 4:
798-
t.family = v1.IPv4Protocol
799-
hdrlen = int(b[0]&0x0f) << 2
800-
if len(b) < hdrlen+4 {
801-
return t, fmt.Errorf("payload to short, received %d expected at least %d", len(b), hdrlen+4)
802-
}
803-
t.srcIP = net.IPv4(b[12], b[13], b[14], b[15])
804-
t.dstIP = net.IPv4(b[16], b[17], b[18], b[19])
805-
protocol = int(b[9])
806-
case 6:
807-
t.family = v1.IPv6Protocol
808-
hdrlen = 40
809-
if len(b) < hdrlen+4 {
810-
return t, fmt.Errorf("payload to short, received %d expected at least %d", len(b), hdrlen+4)
811-
}
812-
t.srcIP = make(net.IP, net.IPv6len)
813-
copy(t.srcIP, b[8:24])
814-
t.dstIP = make(net.IP, net.IPv6len)
815-
copy(t.dstIP, b[24:40])
816-
// NextHeader (not extension headers supported)
817-
protocol = int(b[6])
818-
default:
819-
return t, fmt.Errorf("unknown versions %d", version)
820-
}
821-
822-
switch protocol {
823-
case 6:
824-
t.proto = v1.ProtocolTCP
825-
case 17:
826-
t.proto = v1.ProtocolUDP
827-
case 132:
828-
t.proto = v1.ProtocolSCTP
829-
default:
830-
return t, fmt.Errorf("unknown protocol %d", protocol)
831-
}
832-
// TCP, UDP and SCTP srcPort and dstPort are the first 4 bytes after the IP header
833-
t.srcPort = int(binary.BigEndian.Uint16(b[hdrlen : hdrlen+2]))
834-
t.dstPort = int(binary.BigEndian.Uint16(b[hdrlen+2 : hdrlen+4]))
835-
// Obtain the offset of the payload
836-
// TODO allow to filter by the payload
837-
dataOffset := int(b[hdrlen+12] >> 4)
838-
if len(b) >= hdrlen+dataOffset {
839-
t.payload = b[hdrlen+dataOffset:]
840-
}
841-
return t, nil
842-
}

pkg/networkpolicy/packet.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package networkpolicy
2+
3+
import (
4+
"encoding/binary"
5+
"encoding/hex"
6+
"fmt"
7+
"net"
8+
9+
v1 "k8s.io/api/core/v1"
10+
)
11+
12+
type packet struct {
13+
family v1.IPFamily
14+
srcIP net.IP
15+
dstIP net.IP
16+
proto v1.Protocol
17+
srcPort int
18+
dstPort int
19+
payload []byte
20+
}
21+
22+
func (p packet) String() string {
23+
return fmt.Sprintf("%s:%d %s:%d %s :: %s", p.srcIP.String(), p.srcPort, p.dstIP.String(), p.dstPort, p.proto, hex.Dump(p.payload))
24+
}
25+
26+
// https://en.wikipedia.org/wiki/Internet_Protocol_version_4#Packet_structure
27+
// https://en.wikipedia.org/wiki/IPv6_packet
28+
// https://github.com/golang/net/blob/master/ipv4/header.go
29+
func parsePacket(b []byte) (packet, error) {
30+
t := packet{}
31+
if b == nil {
32+
return t, fmt.Errorf("empty payload")
33+
}
34+
version := int(b[0] >> 4)
35+
// initialize variables
36+
hdrlen := -1
37+
protocol := -1
38+
switch version {
39+
case 4:
40+
t.family = v1.IPv4Protocol
41+
hdrlen = int(b[0]&0x0f) << 2
42+
if len(b) < hdrlen+4 {
43+
return t, fmt.Errorf("payload to short, received %d expected at least %d", len(b), hdrlen+4)
44+
}
45+
t.srcIP = net.IPv4(b[12], b[13], b[14], b[15])
46+
t.dstIP = net.IPv4(b[16], b[17], b[18], b[19])
47+
protocol = int(b[9])
48+
case 6:
49+
t.family = v1.IPv6Protocol
50+
hdrlen = 40
51+
if len(b) < hdrlen+4 {
52+
return t, fmt.Errorf("payload to short, received %d expected at least %d", len(b), hdrlen+4)
53+
}
54+
t.srcIP = make(net.IP, net.IPv6len)
55+
copy(t.srcIP, b[8:24])
56+
t.dstIP = make(net.IP, net.IPv6len)
57+
copy(t.dstIP, b[24:40])
58+
// NextHeader (not extension headers supported)
59+
protocol = int(b[6])
60+
default:
61+
return t, fmt.Errorf("unknown versions %d", version)
62+
}
63+
64+
switch protocol {
65+
case 6:
66+
t.proto = v1.ProtocolTCP
67+
case 17:
68+
t.proto = v1.ProtocolUDP
69+
case 132:
70+
t.proto = v1.ProtocolSCTP
71+
default:
72+
return t, fmt.Errorf("unknown protocol %d", protocol)
73+
}
74+
// TCP, UDP and SCTP srcPort and dstPort are the first 4 bytes after the IP header
75+
t.srcPort = int(binary.BigEndian.Uint16(b[hdrlen : hdrlen+2]))
76+
t.dstPort = int(binary.BigEndian.Uint16(b[hdrlen+2 : hdrlen+4]))
77+
// Obtain the offset of the payload
78+
// TODO allow to filter by the payload
79+
dataOffset := int(b[hdrlen+12] >> 4)
80+
if len(b) >= hdrlen+dataOffset {
81+
t.payload = b[hdrlen+dataOffset:]
82+
}
83+
return t, nil
84+
}

0 commit comments

Comments
 (0)