How to append data on a packet from kernel space?

后端 未结 2 1414
清歌不尽
清歌不尽 2020-12-09 06:58

I am trying to append some data on a packet from kernel space. I have an echo client and server. I type in the command line like: ./client \"message\" and the server just

2条回答
  •  忘掉有多难
    2020-12-09 07:26

    About one year ago for kernel 2.6.26 I did it like this:

    // Do we need extra space?
    if(len - skb_tailroom(skb) > 0){
    
      // Expand skb tail until we have enough room for the extra data
      if (pskb_expand_head(skb, 0, extra_data_len - skb_tailroom(skb), GFP_ATOMIC)) {
        // allocation failed. Do whatever you need to do
      }
    
      // Allocation succeeded
    
      // Reserve space in skb and return the starting point
      your_favourite_structure* ptr = (your_favourite_structure*) 
                                      skb_push(skb, sizeof(*ptr)); 
    
      // Now either set each field of your structure or memcpy into it.
      // Remember you can use a char*
    
    }
    

    Don't forget:

    • Recalculate UDP checksum, because you changed data in the transported data.

    • Change the field tot_len(total length) in the ip header, because you added data to the packet.

    • Recalculate the IP header checksum, because you changed the tot_len field.

    Extra note: This is just a simple thing. I see in your code you're allocating tmp as a 200 byte array and using that to store the data of your message. If you send a bigger packet you'll have a hard time debugging this as kernel crashes due to memory overflows are too painful.

提交回复
热议问题