LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 04-30-2023, 06:10 PM   #1
FlameSky
LQ Newbie
 
Registered: Apr 2023
Posts: 1

Rep: Reputation: 0
Unhappy Netfilter - Kernel crashes when I'm trying to substitute skb with new one


Hi guys, I'm trying to hack kernel networking stack. My linux-4.19 on vm forwards packets from eth0 to eth1 and encrypts them on their way. On NF_INET_POST_ROUTING nf hook I want to catch them and substitute with packets with bigger size. (I wanna implement kinda ESP tunneling mode by myself). I need encapsulate my ipv6 packet within new ipv6 and encrypt internals. So, I need packet with bigger size. I kfree_skb() old packet and allocate new, than substitute. You can see it in my_hook() func.

The problem is: it doesn't work for unknown reason - each time packet flows through my hook, kernel gets crashed(Null pointer dereferencing). Maybe there is my mistake in using api? What i should do? Maybe there is utterly other way to accomplish my idea?

Code:
#include <linux/module.h>
#include <linux/init.h>
#include <linux/netfilter.h>
#include <linux/string.h>
#include <uapi/linux/ipv6.h>
#include <linux/ipv6.h>
#include <uapi/linux/netfilter.h>
#include <uapi/linux/netfilter_ipv4.h> // it is for NF_IP_PRI_FIRST
#include <linux/gfp.h> 



unsigned int my_hook(void *priv,
                    struct sk_buff *skb,
                    const struct nf_hook_state *state) 
{
    // struct ipv6hdr *header = ipv6_hdr(skb); 
    struct sk_buff *n;
    if (strcmp("eth1", state->out->name)) // not equal
        return NF_ACCEPT;
    pr_info("Caught packet directed to eth1\n");
    n = skb_copy(skb, GFP_KERNEL);
    if (n == NULL) {
        pr_info("buffer is not allocated!");
        return NF_ACCEPT;
    }
    kfree_skb(skb);
    skb = n;
    return NF_ACCEPT;
}

struct nf_hook_ops my_hook_ops[] = {
    {
        .hook = my_hook, //nf_hookfn *
        .dev = NULL, //struct net_device *
        .priv = NULL, // void *
        .pf = NFPROTO_IPV6, // u_int8_t
        .hooknum = NF_INET_POST_ROUTING, //unsigned int
        .priority = NF_IP_PRI_FIRST, // int
    }
};
static int __init my_init(void) 
{
    int ret;
    pr_info("module loading\n");
    ret = nf_register_net_hook(&init_net, my_hook_ops);
    return 0;
}

static void __exit my_exit(void) 
{
    nf_unregister_net_hook(&init_net, my_hook_ops);
    pr_info("module unloading\n");
    return; 
}
 
Old 05-05-2023, 08:16 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,161

Rep: Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266
Changing skb in your subroutine has no effect on the caller, however freeing what it points to does.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Speeding Up Netfilter (by Avoiding Netfilter) LXer Syndicated Linux News 0 05-16-2018 01:36 PM
Recycle SKB using SKB pool Pankajgoyal38 Linux - Kernel 4 03-11-2013 10:39 PM
How to send copied skb in netfilter hook? simon_qwl Programming 5 04-18-2012 05:24 AM
[SOLVED] Problem writing new tcp option to skb yaplej Programming 3 08-17-2011 07:24 AM
group the outgoing skb's into single skb and then send grouped skb. venkatesh111 Linux - Networking 0 04-27-2006 01:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 06:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration