aboutsummaryrefslogtreecommitdiff
path: root/src/RawSocket.zig
blob: e43a8e41b60e9e0e123658a9be7dc9ae4c5eb0e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright 2026 Robby Zambito
//
// This file is part of zaprus.
//
// Zaprus is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// Zaprus is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// Zaprus. If not, see <https://www.gnu.org/licenses/>.

const RawSocket = @This();

const is_debug = builtin.mode == .Debug;

fd: i32,
sockaddr_ll: std.posix.sockaddr.ll,
mac: [6]u8,

pub const max_payload_len = 1000;

const Ifconf = extern struct {
    ifc_len: i32,
    ifc_ifcu: extern union {
        ifcu_buf: ?[*]u8,
        ifcu_req: ?[*]std.os.linux.ifreq,
    },
};

pub fn init() !RawSocket {
    const socket: i32 = std.math.cast(i32, std.os.linux.socket(std.os.linux.AF.PACKET, std.os.linux.SOCK.RAW, 0)) orelse return error.SocketError;
    if (socket < 0) return error.SocketError;

    var ifreq_storage: [16]std.os.linux.ifreq = undefined;
    var ifc = Ifconf{
        .ifc_len = @sizeOf(@TypeOf(ifreq_storage)),
        .ifc_ifcu = .{ .ifcu_req = &ifreq_storage },
    };

    if (std.os.linux.ioctl(socket, std.os.linux.SIOCGIFCONF, @intFromPtr(&ifc)) != 0) {
        return error.NicError;
    }

    const count = @divExact(ifc.ifc_len, @sizeOf(std.os.linux.ifreq));

    // Get the first non loopback interface
    var ifr = for (ifreq_storage[0..@intCast(count)]) |*ifr| {
        if (std.os.linux.ioctl(socket, std.os.linux.SIOCGIFFLAGS, @intFromPtr(ifr)) == 0) {
            if (ifr.ifru.flags.LOOPBACK) continue;
            break ifr;
        }
    } else return error.NoInterfaceFound;

    // 2. Get Interface Index
    if (std.os.linux.ioctl(socket, std.os.linux.SIOCGIFINDEX, @intFromPtr(ifr)) != 0) {
        return error.NicError;
    }
    const ifindex: i32 = ifr.ifru.ivalue;

    // 3. Get Real MAC Address
    if (std.os.linux.ioctl(socket, std.os.linux.SIOCGIFHWADDR, @intFromPtr(ifr)) != 0) {
        return error.NicError;
    }
    var mac: [6]u8 = ifr.ifru.hwaddr.data[0..6].*;
    if (builtin.cpu.arch.endian() == .little) std.mem.reverse(u8, &mac);

    // 4. Set Flags (Promiscuous/Broadcast)
    if (std.os.linux.ioctl(socket, std.os.linux.SIOCGIFFLAGS, @intFromPtr(ifr)) != 0) {
        return error.NicError;
    }
    ifr.ifru.flags.BROADCAST = true;
    ifr.ifru.flags.PROMISC = true;
    if (std.os.linux.ioctl(socket, std.os.linux.SIOCSIFFLAGS, @intFromPtr(ifr)) != 0) {
        return error.NicError;
    }

    const sockaddr_ll = std.mem.zeroInit(std.posix.sockaddr.ll, .{
        .family = std.posix.AF.PACKET,
        .ifindex = ifindex,
        .protocol = std.mem.nativeToBig(u16, @as(u16, std.os.linux.ETH.P.IP)),
    });

    const bind_ret = std.os.linux.bind(socket, @ptrCast(&sockaddr_ll), @sizeOf(@TypeOf(sockaddr_ll)));
    if (bind_ret != 0) return error.BindError;

    return .{
        .fd = socket,
        .sockaddr_ll = sockaddr_ll,
        .mac = mac,
    };
}

pub fn setTimeout(self: *RawSocket, sec: isize, usec: i64) !void {
    const timeout: std.os.linux.timeval = .{ .sec = sec, .usec = usec };
    const timeout_ret = std.os.linux.setsockopt(self.fd, std.os.linux.SOL.SOCKET, std.os.linux.SO.RCVTIMEO, @ptrCast(&timeout), @sizeOf(@TypeOf(timeout)));
    if (timeout_ret != 0) return error.SetTimeoutError;
}

pub fn deinit(self: *RawSocket) void {
    _ = std.os.linux.close(self.fd);
    self.* = undefined;
}

pub fn send(self: RawSocket, payload: []const u8) !void {
    const sent_bytes = std.os.linux.sendto(
        self.fd,
        payload.ptr,
        payload.len,
        0,
        @ptrCast(&self.sockaddr_ll),
        @sizeOf(@TypeOf(self.sockaddr_ll)),
    );
    _ = sent_bytes;
}

pub fn receive(self: RawSocket, buf: []u8) ![]u8 {
    const len = std.os.linux.recvfrom(
        self.fd,
        buf.ptr,
        buf.len,
        0,
        null,
        null,
    );
    if (std.os.linux.errno(len) != .SUCCESS) {
        return error.Timeout; // TODO: get the real error, assume timeout for now.
    }
    return buf[0..len];
}

pub fn attachSaprusPortFilter(self: RawSocket, incoming_src_port: ?u16, incoming_dest_port: u16) !void {
    const BPF = std.os.linux.BPF;
    // BPF instruction structure for classic BPF
    const SockFilter = extern struct {
        code: u16,
        jt: u8,
        jf: u8,
        k: u32,
    };

    const SockFprog = extern struct {
        len: u16,
        filter: [*]const SockFilter,
    };

    // Build the filter program
    const filter = if (incoming_src_port) |inc_src| &[_]SockFilter{
        // Load 2 bytes at offset 46 (absolute)
        .{ .code = BPF.LD | BPF.H | BPF.ABS, .jt = 0, .jf = 0, .k = 46 },
        // Jump if equal to port (skip 1 if true, skip 0 if false)
        .{ .code = BPF.JMP | BPF.JEQ | BPF.K, .jt = 1, .jf = 0, .k = @as(u32, inc_src) },
        // Return 0x0 (fail)
        .{ .code = BPF.RET | BPF.K, .jt = 0, .jf = 0, .k = 0x0 },
        // Load 2 bytes at offset 48 (absolute)
        .{ .code = BPF.LD | BPF.H | BPF.ABS, .jt = 0, .jf = 0, .k = 48 },
        // Jump if equal to port (skip 0 if true, skip 1 if false)
        .{ .code = BPF.JMP | BPF.JEQ | BPF.K, .jt = 0, .jf = 1, .k = @as(u32, incoming_dest_port) },
        // Return 0xffff (pass)
        .{ .code = BPF.RET | BPF.K, .jt = 0, .jf = 0, .k = 0xffff },
        // Return 0x0 (fail)
        .{ .code = BPF.RET | BPF.K, .jt = 0, .jf = 0, .k = 0x0 },
    } else &[_]SockFilter{
        // Load 2 bytes at offset 48 (absolute)
        .{ .code = BPF.LD | BPF.H | BPF.ABS, .jt = 0, .jf = 0, .k = 48 },
        // Jump if equal to port (skip 0 if true, skip 1 if false)
        .{ .code = BPF.JMP | BPF.JEQ | BPF.K, .jt = 0, .jf = 1, .k = @as(u32, incoming_dest_port) },
        // Return 0xffff (pass)
        .{ .code = BPF.RET | BPF.K, .jt = 0, .jf = 0, .k = 0xffff },
        // Return 0x0 (fail)
        .{ .code = BPF.RET | BPF.K, .jt = 0, .jf = 0, .k = 0x0 },
    };

    const fprog = SockFprog{
        .len = @intCast(filter.len),
        .filter = filter.ptr,
    };

    // Attach filter to socket using setsockopt
    const rc = std.os.linux.setsockopt(
        self.fd,
        std.os.linux.SOL.SOCKET,
        std.os.linux.SO.ATTACH_FILTER,
        @ptrCast(&fprog),
        @sizeOf(SockFprog),
    );

    if (rc != 0) {
        return error.BpfAttachFailed;
    }
}

const std = @import("std");
const builtin = @import("builtin");