how to redirecting outgoing network traffic in kernel

How do I change the destination IP address in the kernel?

there is my code,but it doesn't work.

is there something wrong ,pls tell me? tks


static errno_t ipf_attach(void){
    errno_t err = 0;
    struct ipf_filter ipfv4;
    bzero(&ipfv4, sizeof(struct ipf_filter));
    ipfv4.cookie = (caddr_t)&ipf_v4;
    ipfv4.name = MYBUNDLEID;
    ipfv4.ipf_input = ipf_input;
    ipfv4.ipf_output = ipf_output;
    ipfv4.ipf_detach = ipf_detach;
    err = ipf_addv4(&ipfv4, &ipf_v4);
    if(err) return err;
}
static errno_t ipf_output(void *cookie, mbuf_t *data,ipf_pktopts_t options){
    errno_t retval = 0;
    if(cookie == &ipf_v4){
        struct ip *ip = (struct ip *)mbuf_data(*data);
        if(ip != NULL){
            if(ip->ip_p != IPPROTO_TCP)
                return retval;
        
            struct tcphdr *tcp = (struct tcphdr *)((uint8_t *)ip  + sizeof(struct ip));
            short dport = ntohs(tcp->th_dport);
            if((dport == 80 || dport == 8080 ) ){
            
                if(ipf_agent_http_addr == 0)
                    return retval;
            
                if(ipf_agent_http_addr != ip->ip_src.s_addr && ipf_agent_http_addr != ip->ip_dst.s_addr){
                
                    if(send == 0){
                    
                        mbuf_inbound_modified(*data);
                    
                        ip->ip_dst.s_addr = ipf_agent_http_addr;
                        tcp->th_dport = ipf_agent_http_port;
                    
                        ipf_chksm_update(*data);
                    
                        mbuf_outbound_finalize(*data, AF_INET, 0);
                    
                        ipf_inject_output(*data, ipf_v4, options);
                    
                        return EJUSTRETURN;
                    }
                }
            }
            else if (dport == 443) /
            {
            
            }
        }
    }
     return retval;
}

there is my code,but it doesn't work.

What do you mean by “doesn’t work”? Do the packets go out unchanged? Or get dropped? Or what?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
how to redirecting outgoing network traffic in kernel
 
 
Q