Creating sk_buff for the linux kernel output kernel

In short, I am trying to collect very meager UDP SKB bones to get something on the wire. The scenario is as follows:

I have a kernel module load which (among other things) overrides the memory location of the standard udp_sendmsg function in / net / ipv 4 / udp.c. From here I would like to build skb to the point where I can just put it on the wire. Usually udp_sendmsg just runs a small UDP account, writes UDP headers and sends them to the IP layer for routing, L3 / L2 headers, etc. I mainly bring some of these functions to the sendmsg function. At this time, I just highlight skb:

    skb = alloc_skb(1500, GFP_KERNEL);
    //skb has 1500 bytes of tail room only
    skb_reserve(skb, 500);
    //now has head and tail but no data space
    data = skb_put(skb, 500);
    //now we have an skb with 500 head, 500 data sec, 500 tail

And then (after some set of route table) I try to add udp_hdr:

    struct udphdr *uh;
    skb->transport_header = skb_push(skb, sizeof(struct udphdr));
    uh = udp_hdr(skb);
    uh->source = 5555;
    uh->dest = dport;
    uh->len =  18;
    uh->check = 0;

and ip_hdr (only filled basics):

    struct iphdr *iph;
    skb->network_header = skb_push(skb, sizeof(struct iphdr));
    iph = ip_hdr(skb);
    iph->version = 4;
    iph->ihl = 5;
    iph->tos = inet->tos;
    iph->tot_len = htons(skb->len);
    iph->protocol = IPPROTO_UDP;
    iph->saddr = saddr;
    iph->daddr = daddr;
    skb->dst = dst_clone(&rt->u.dst);

. , ​​(pre 2.6.24), nh h . skb- > transport_header/skb- > network_header , , , - , , udp_sendmsg

: , :

 skb->transport_header = skb_push(skb, sizeof(struct udphdr));

:

skb_reset_transport_header(skb);

( network_header. reset linux/sk_buff.h, , , .

, ( ) undefined , .

, , skb . google .

:

 [<ffffffff813dbf98>] oops_end+0xb9/0xc1
 [<ffffffff81030e21>] no_context+0x1f6/0x205
 [<ffffffff81030fd3>] __bad_area_nosemaphore+0x1a3/0x1c9
 [<ffffffff8101184e>] ? apic_timer_interrupt+0xe/0x20
 [<ffffffff8103100c>] bad_area_nosemaphore+0x13/0x15
 [<ffffffff813dd30a>] do_page_fault+0x125/0x222
 [<ffffffff813db485>] page_fault+0x25/0x30
 [<ffffffffa010924f>] ? udp_sendmsg_offload+0x1e3/0x250 [testmodule]
 [<ffffffffa010922e>] ? udp_sendmsg_offload+0x1c2/0x250 [testmodule]
 [<ffffffff81390a00>] inet_sendmsg+0x54/0x5d
 [<ffffffff8132f142>] __sock_sendmsg+0x61/0x6c
 [<ffffffff8132f8b9>] sock_sendmsg+0xcc/0xe5
+3
1

skb_push() , skb->transport_header - sk_buff_data_t, - ., skb_transport_header(). .

, UDP-, :

struct udphdr *uh;
skb_push(skb, sizeof(struct udphdr));
skb_reset_transport_header(skb);
uh = udp_hdr(skb);
uh->source = __constant_htons(5555);
/* ... */

IP:

struct iphdr *iph;
skb_push(skb, sizeof(struct iphdr));
skb_reset_network_header(skb);
iph = ip_hdr(skb);
iph->version = 4;
/* ... */
+1

Source: https://habr.com/ru/post/1754663/


All Articles