title: Sending a UDP packet within a kernel module
body: <p>Background: I'm a fourth year computer engineering major at UCSB. I've taken networking and operating systems courses. I created a program in userspace that broadcasts UDP packets onto the subnet and receives UDP packets in an adhoc network. What I'm trying to accomplish is to convert this program into a kernel module that will work on an ARM embedded system with Angstrom Linux, kernel version 2.6.39 (the x86 to ARM architecture cross compilation is an issue for another day). The reason for this move to the kernel is to shed some of the overhead of userspace functions and to make the sending and receiving part as quick as possible.</p>
  
  <p>I've never done anything like this before in any of the courses I've taken, so please tell me if anything I am saying is incorrect, useless or inefficient!</p>
  
  <p>After research with Google, I've concluded the typical way is to do away with sockets entirely and work with the <code>sockbuf</code> structure and fill in the necessary headers myself. Would this have an effect on the ability to broadcast packets on the subnet?
  I am currently trying to follow the code here:
  <a href="https://stackoverflow.com/questions/13227315/udp-packet-send-with-linux-kernel-module-without-using-sockets">UDP packet send with linux-kernel module without using sockets</a></p>
  
  <p>I've figured out the reasoning behind most of the code, but the last part is what confuses me:</p>
  
  <pre><code>eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
  skb_reset_mac_header(skb);
  skb-&gt;protocol = eth-&gt;h_proto = htons(ETH_P_IP);
  memcpy(eth-&gt;h_source, dev-&gt;dev_addr, ETH_ALEN);
  memcpy(eth-&gt;h_dest, remote_mac, ETH_ALEN);
  
  skb-&gt;dev = dev;
  
  
  dev_queue_xmit(skb);
  </code></pre>
  
  <ul>
  <li>All of the ethernet header seems to be constructed purely out of headers defined in the kernel besides the source MAC address, is this correct? I am going to be broadcasting my packets, so what exactly should be put into the destination MAC address field?</li>
  <li>More importantly, what is dev in the <code>skb-&gt;dev = dev;</code> line? From my investigation, it is a pointer to the device driver it is associated with. From my understanding, I would want this to point to the wireless chip device driver as I am using 802.11 to communicate. Do I have to create my own <code>dev</code> struct for the wireless driver? If so, there any guidance on how to accomplish this? If not, how can I access the existing device driver and use this in a kernel module?</li>
  </ul>
  
  <p>I've tried commenting out the dev line and running the code but unsurprisingly I get a kernel panic once it executes <code>dev_queue_xmit(skb);</code>.</p>
  
  <p>Again, I've never done anything like this before, so any advice would be helpful, even if it means changing my approach entirely! I also understand that this could be a niche of a question, but any sort of guidance is appreciated!</p>
  
  <p>Thank you in advance!</p>