Skip to content

Commit

Permalink
Merge pull request #95 from ddddddO/form-ipv6
Browse files Browse the repository at this point in the history
Support IPv6 of Generator
  • Loading branch information
ddddddO authored Jan 26, 2025
2 parents b7c8781 + 0053ff3 commit 44c37f2
Show file tree
Hide file tree
Showing 19 changed files with 559 additions and 78 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ Packemon's Monitor allows user to select each packet by pressing `Enter` key. Th
- [x] Ethernet
- [x] ARP (WIP)
- [x] IPv4 (WIP)
- [x] ICMP (WIP)
- [x] IPv6 (WIP)
- [x] ICMPv4 (WIP)
- [ ] ICMPv6
- [x] TCP (WIP)
- [x] UDP (WIP)
- [ ] TLSv1.2 (WIP)
Expand Down Expand Up @@ -71,7 +73,9 @@ Packemon's Monitor allows user to select each packet by pressing `Enter` key. Th
- [x] Ethernet
- [x] ARP
- [x] IPv4 (WIP)
- [x] ICMP (WIP)
- [x] IPv6 (WIP)
- [x] ICMPv4 (WIP)
- [ ] ICMPv6
- [x] TCP (WIP)
- [x] UDP
- [ ] DNS (WIP)
Expand Down
9 changes: 7 additions & 2 deletions cmd/packemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,13 @@ func run(ctx context.Context, columns string, nwInterface string, wantSend bool,
if err != nil {
return err
}
tui.DEFAULT_IP_SOURCE = strings.Split(ipAddr[0].String(), "/")[0]
tui.DEFAULT_ARP_SENDER_IP = tui.DEFAULT_IP_SOURCE
if len(ipAddr) > 0 {
tui.DEFAULT_IP_SOURCE = strings.Split(ipAddr[0].String(), "/")[0]
tui.DEFAULT_ARP_SENDER_IP = tui.DEFAULT_IP_SOURCE
}
if len(ipAddr) > 1 {
tui.DEFAULT_IPv6_SOURCE = strings.Split(ipAddr[1].String(), "/")[0]
}

if debug {
if wantSend {
Expand Down
37 changes: 37 additions & 0 deletions egress_control/egress_packet.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// #include <vmlinux.h>
// #include "common.h"
#include <linux/bpf.h>
#include <linux/ipv6.h> // ipv6hdr ヘッダの定義あり
#include <bpf/bpf_endian.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
Expand All @@ -17,6 +18,7 @@
#define TC_ACT_SHOT 2

#define ETH_P_IPv4 0x0800
#define ETH_P_IPv6 0x86dd
#define ETH_P_ARP 0x0806

#define IP_P_ICMP 0x01
Expand Down Expand Up @@ -91,6 +93,7 @@ int control_egress(struct __sk_buff *skb)
void *data = (void *)(__u64)skb->data;
struct ethhdr *eth;
struct iphdr *iph;
struct ipv6hdr *ip6h;
struct tcphdr *tcph;

__u32 key = 0;
Expand Down Expand Up @@ -173,5 +176,39 @@ int control_egress(struct __sk_buff *skb)
return TC_ACT_OK;
}

if (bpf_ntohs(eth->h_proto) == ETH_P_IPv6) {
ip6h = (struct ip6hdr *)(eth + 1);

if (ip6h->nexthdr == IP_P_TCP) {
bpf_printk("TCP");

tcph = (struct tcphdr *)(ip6h + 1);
if ((void *)(tcph + 1) > data_end) {
bpf_printk("d");
return TC_ACT_OK;
}

bpf_printk(" sport : %x", bpf_ntohs(tcph->sport));
bpf_printk(" dport : %x", bpf_ntohs(tcph->dport));
bpf_printk(" controlflg: %x", bpf_ntohs(tcph->controlflg));
bpf_printk(" controlflg: %x", tcph->controlflg);

if (tcph->controlflg == TCP_FLG_RST_ACK) {
bpf_printk("TCP RST-ACK");
return TC_ACT_SHOT;
// return TC_ACT_OK;
}
if (tcph->controlflg == TCP_FLG_RST) {
bpf_printk("TCP RST");
return TC_ACT_SHOT;
// return TC_ACT_OK;
}

return TC_ACT_OK;
}

return TC_ACT_OK;
}

return TC_ACT_OK;
}
16 changes: 15 additions & 1 deletion egress_control/egress_packet_bpfeb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified egress_control/egress_packet_bpfeb.o
Binary file not shown.
16 changes: 15 additions & 1 deletion egress_control/egress_packet_bpfel.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified egress_control/egress_packet_bpfel.o
Binary file not shown.
1 change: 1 addition & 0 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (hrh *HTTPResponseHeader) Bytes() []byte {
}

// TODO: 多分このあたりバグってる。Monitor の http response の hexadecimal dump と Wireshark で異なる
// TODO: panic になることある
func ParsedHTTPResponse(payload []byte) *HTTPResponse {
sep := []byte{0x0d, 0x0a} // "\r\n"

Expand Down
88 changes: 61 additions & 27 deletions internal/tui/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,20 @@ var (
DEFAULT_IP_SOURCE = ""
DEFAULT_IP_DESTINATION = ""

DEFAULT_IPv6_PROTOCOL = "ICMPv6"
DEFAULT_IPv6_SOURCE = ""
DEFAULT_IPv6_DESTINATION = ""

DEFAULT_ICMP_TYPE = "0x08"
DEFAULT_ICMP_CODE = "0x00"
DEFAULT_ICMP_IDENTIFIER = "0x34a1"
DEFAULT_ICMP_SEQUENCE = "0x0001"

DEFAULT_UDP_PORT_SOURCE = "12345"
DEFAULT_UDP_PORT_SOURCE = "47000"
DEFAULT_UDP_PORT_DESTINATION = "53"
DEFAULT_UDP_LENGTH = "0x0030"

DEFAULT_DNS_TRANSACTION = "0x1234"
DEFAULT_DNS_TRANSACTION = "0xaa78"
DEFAULT_DNS_FLAGS = "0x0100"
DEFAULT_DNS_QUESTIONS = "0x0001"
DEFAULT_DNS_ANSWERS_RRs = "0x0000"
Expand All @@ -54,7 +58,7 @@ var (
DEFAULT_DNS_QUERIES_TYPE = "0x0001"
DEFAULT_DNS_QUERIES_CLASS = "0x0001"

DEFAULT_TCP_PORT_SOURCE = "12345"
DEFAULT_TCP_PORT_SOURCE = "47000"
DEFAULT_TCP_PORT_DESTINATION = "80"
DEFAULT_TCP_SEQUENCE = "0x1f6e9499"
DEFAULT_TCP_FLAGS = "0x02"
Expand All @@ -74,27 +78,29 @@ func (t *tui) form(ctx context.Context, sendFn func(*packemon.EthernetFrame) err
if err != nil {
return err
}
ethernetHeader, arp, ipv4, icmp, udp, tcp, dns, http := d.e, d.a, d.ip, d.ic, d.u, d.t, d.d, d.h
ethernetHeader, arp, ipv4, ipv6, icmp, udp, tcp, dns, http := d.e, d.a, d.ip, d.ipv6, d.ic, d.u, d.t, d.d, d.h

// L7
httpForm := t.httpForm(ctx, sendFn, ethernetHeader, ipv4, tcp, http)
httpForm := t.httpForm(ctx, sendFn, ethernetHeader, ipv4, ipv6, tcp, http)
httpForm.SetBorder(true).SetTitle(" HTTP ").SetTitleAlign(tview.AlignLeft)
dnsForm := t.dnsForm(sendFn, ethernetHeader, ipv4, udp, dns)
dnsForm := t.dnsForm(sendFn, ethernetHeader, ipv4, ipv6, udp, dns)
dnsForm.SetBorder(true).SetTitle(" DNS ").SetTitleAlign(tview.AlignLeft)

// L5~L6
tlsv1_2Form := t.tlsv1_2Form(sendFn, ethernetHeader)
tlsv1_2Form.SetBorder(true).SetTitle(" TLSv1.2 ").SetTitleAlign(tview.AlignLeft)

// L4
tcpForm := t.tcpForm(sendFn, ethernetHeader, ipv4, tcp)
tcpForm := t.tcpForm(sendFn, ethernetHeader, ipv4, ipv6, tcp)
tcpForm.SetBorder(true).SetTitle(" TCP ").SetTitleAlign(tview.AlignLeft)
udpForm := t.udpForm(sendFn, ethernetHeader, ipv4, udp)
udpForm := t.udpForm(sendFn, ethernetHeader, ipv4, ipv6, udp)
udpForm.SetBorder(true).SetTitle(" UDP ").SetTitleAlign(tview.AlignLeft)
icmpForm := t.icmpForm(sendFn, ethernetHeader, ipv4, icmp)
icmpForm.SetBorder(true).SetTitle(" ICMP ").SetTitleAlign(tview.AlignLeft)

// L3
ipv6Form := t.ipv6Form(sendFn, ethernetHeader, ipv6)
ipv6Form.SetBorder(true).SetTitle(" IPv6 Header ").SetTitleAlign(tview.AlignLeft)
ipv4Form := t.ipv4Form(sendFn, ethernetHeader, ipv4)
ipv4Form.SetBorder(true).SetTitle(" IPv4 Header ").SetTitleAlign(tview.AlignLeft)
arpForm := t.arpForm(sendFn, ethernetHeader, arp)
Expand All @@ -111,6 +117,7 @@ func (t *tui) form(ctx context.Context, sendFn func(*packemon.EthernetFrame) err
AddPage("UDP", udpForm, true, true).
AddPage("TCP", tcpForm, true, true).
AddPage("ICMP", icmpForm, true, true).
AddPage("IPv6", ipv6Form, true, true).
AddPage("IPv4", ipv4Form, true, true).
AddPage("ARP", arpForm, true, true).
AddPage("Ethernet", ethernetForm, true, true)
Expand All @@ -130,6 +137,9 @@ func (t *tui) form(ctx context.Context, sendFn func(*packemon.EthernetFrame) err
}).AddItem("IPv4", "", '2', func() {
t.pages.SwitchToPage("IPv4")
t.app.SetFocus(t.pages)
}).AddItem("IPv6", "", '3', func() {
t.pages.SwitchToPage("IPv6")
t.app.SetFocus(t.pages)
})

l4Protocols := tview.NewList()
Expand Down Expand Up @@ -211,14 +221,15 @@ func (t *tui) form(ctx context.Context, sendFn func(*packemon.EthernetFrame) err
}

type defaults struct {
e *packemon.EthernetHeader
a *packemon.ARP
ip *packemon.IPv4
ic *packemon.ICMP
t *packemon.TCP
u *packemon.UDP
d *packemon.DNS
h *packemon.HTTP
e *packemon.EthernetHeader
a *packemon.ARP
ip *packemon.IPv4
ipv6 *packemon.IPv6
ic *packemon.ICMP
t *packemon.TCP
u *packemon.UDP
d *packemon.DNS
h *packemon.HTTP
}

func defaultPackets() (*defaults, error) {
Expand Down Expand Up @@ -290,9 +301,10 @@ func defaultPackets() (*defaults, error) {
return nil, err
}
udp := &packemon.UDP{
SrcPort: udpSrcPort,
DstPort: udpDstPort,
Length: binary.BigEndian.Uint16(udpLength),
SrcPort: udpSrcPort,
DstPort: udpDstPort,
Checksum: 0x0000,
Length: binary.BigEndian.Uint16(udpLength),
}

tcp := &packemon.TCP{
Expand Down Expand Up @@ -372,6 +384,27 @@ func defaultPackets() (*defaults, error) {
DstAddr: binary.BigEndian.Uint32(dstIP),
}

srcIPv6 := net.ParseIP(DEFAULT_IPv6_SOURCE)
if srcIPv6 == nil {
return nil, err
}
dstIPv6 := net.ParseIP(DEFAULT_IPv6_DESTINATION)
if dstIPv6 == nil {
return nil, err
}

ipv6 := &packemon.IPv6{
Version: 0x06,
TrafficClass: 0x00,
// FlowLabel: 0x7d77b,
FlowLabel: 0x00000,
PayloadLength: 0x0000,
NextHeader: packemon.IPv6_NEXT_HEADER_ICMPv6,
HopLimit: 0x40,
SrcAddr: srcIPv6.To16(),
DstAddr: dstIPv6.To16(),
}

hardwareType, err := packemon.StrHexToBytes2(DEFAULT_ARP_HARDWARE_TYPE)
if err != nil {
return nil, err
Expand Down Expand Up @@ -438,14 +471,15 @@ func defaultPackets() (*defaults, error) {
}

return &defaults{
e: ethernetHeader,
a: arp,
ip: ipv4,
ic: icmp,
u: udp,
t: tcp,
d: dns,
h: http,
e: ethernetHeader,
a: arp,
ip: ipv4,
ipv6: ipv6,
ic: icmp,
u: udp,
t: tcp,
d: dns,
h: http,
}, nil
}

Expand Down
Loading

0 comments on commit 44c37f2

Please sign in to comment.