text
stringlengths
0
1.99k
Finally, since my tool operates at Layer 2, I must manually resolve the
gateway's MAC address via ARP.
----[ 3.2.1 - The Packet Transmission Loop
Instead of sending packets one by one, we can send packets in large batches
to amortize the cost of syscalls.
--------------------------------------------------------------------------
// file: cmd/portscanner/main.go (conceptual)
// A template packet is pre-crafted to avoid building from scratch every
time
packer, _ := newSynPacker(srcMAC, gatewayMAC, srcIP, srcPort)
// Ensure COMPLETION ring is checked to reclaim UMEM frames for reuse
check_completion_ring_and_refill_umem();
for outstandingCount > 0 {
numFree := xsk.NumFreeTxSlots()
if numFree > 0 {
descs := xsk.GetDescs(min(numFree, BATCH_SIZE), false)
for i := range descs {
target := getNextTarget()
frame := xsk.GetFrame(descs[i]) // Pointer to shared memory
packer.pack(frame, target.ip, target.port, randomSeq())
descs[i].Len = pktLen
}
xsk.Transmit(descs)
}
}
--------------------------------------------------------------------------
----[ 3.2.2 - The Packet Reception Loop
My receive loop, running in a dedicated goroutine, can be simple because
the eBPF program has already handled the filtering.
--------------------------------------------------------------------------
// file: cmd/portscanner/main.go (conceptual)
// Pre-populate the FILL ring with available UMEM frames
populate_fill_ring();
for {
numRx, _, err := xsk.Poll(10) // 10ms timeout
if numRx > 0 {
rxDescs := xsk.Receive(rxDescs)
for _, desc := range rxDescs {
frame := xsk.GetFrame(desc)
ip, port, status := processPacket(frame)
if status == "open" || status == "closed" {
updateStatus(ip, port, status)
}
}
// Return the now-empty frame descriptors to the kernel's FILL ring
xsk.Fill(rxDescs)
}
}
--------------------------------------------------------------------------
--[ 4 - Performance Analysis
---[ 4.0 - A Note on Benchmarking
To validate this architecture, a performance comparison is necessary. I
chose masscan as the benchmark, as it represents the gold standard for
high-speed, internet-scale scanning. It must be stated that masscan is a
mature, highly-tuned project. It has years of optimization in its custom
networking code and supports advanced kernel-bypass techniques such as
PF_RING with DNA drivers. This driver DMAs packets directly from user-mode
memory to the network driver with zero kernel involvement, allowing it to
transmit at the maximum rate the hardware allows. Therefore, the goal here
is not to "beat" masscan, but to determine if an AF_XDP-based tool, even as
a proof-of-concept, can be competitive and where its architectural
strengths lie.
The benchmark consists of two scenarios: a high-density scan against a
single host (45.33.32.156) on all 65,535 ports, and a wide-range scan
against a /9 network (8.3 million IPs) on a single port.
---[ 4.1 - Head-to-Head: AF_XDP vs. masscan
A critical factor in masscan's design is a built-in 10-second delay at the
end of each scan to receive late-arriving packets.
--------------------------------------------------------------------------
rate: 0.00-kpps, 100.00% done, waiting 10-secs, found=3 ~
--------------------------------------------------------------------------
When this delay is factored out to compare raw transmission times, the
results are revealing. For the wide-range /9 scan, masscan clocked in at
69.2 seconds total, meaning its active scanning time was only ~59.2
seconds.
--------------------------------------------------------------------------
real 1m9.174s
--------------------------------------------------------------------------