4

I was wondering if someone has any info about how to implement a link layer portocol which uses the GPIOs (for example in a Raspberry Pi).

The idea is to implement a custom interface (similar to eth0) which would be point-to-point and when a message is sent through that interface instead of calling the ethernet module, my new kernel module (device driver) would be called which would use the GPIOs to send the packet.

I would like to know some stuff:

  • Does anyone know of any book/paper/... with an example of something similar or info I would need to know? I found quite a lot of stuff on kernel modules and so, but nothing about "connecting" the new interface with the kernel module.

  • Does it need to be implemented with TCP/IP if I want it to appear as a new interface? (I suppose the answer is yes)

My research lead me to some interesting webpages:

But still I'm quite lost when it comes to the interface implementation part, and how to "connect it" to the driver. So when I do a ping using the new interface, it sends the IP Packet to my device driver, so I should encapsulate it and send it through my hardware.

Thanks, hope to get some answers!

closed as off-topic by Gilles, Kusalananda, G-Man, GAD3R, countermode Feb 20 '17 at 8:58

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Requests for learning materials (tutorials, how-tos etc.) are off topic. The only exception is questions about where to find official documentation (e.g. POSIX specifications). See the Help Center and our Community Meta for more information." – Kusalananda, G-Man, GAD3R, countermode
If this question can be reworded to fit the rules in the help center, please edit the question.

1

Assuming it's sufficient for your protocol to control the GPIO pins from userspace via /sys/class/gpio/, the simplest way is to write a userspace tap driver. From the kernel documentation linux/Documentation/networking/tuntap.txt:

TUN/TAP provides packet reception and transmission for user space programs. It can be seen as a simple Point-to-Point or Ethernet device, which, instead of receiving packets from physical media, receives them from user space program and instead of sending packets via physical media writes them to the user space program.

In order to use the driver a program has to open /dev/net/tun and issue a corresponding ioctl() to register a network device with the kernel. A network device will appear as tunXX or tapXX, depending on the options chosen. When the program closes the file descriptor, the network device and all corresponding routes will disappear.

Depending on the type of device chosen the userspace program has to read/write IP packets (with tun) or ethernet frames (with tap). Which one is being used depends on the flags given with the ioctl().

The package from http://vtun.sourceforge.net/tun contains two simple examples for how to use tun and tap devices. Both programs work like a bridge between two network interfaces.

  • br_select.c - bridge based on select system call.
  • br_sigio.c - bridge based on async io and SIGIO signal.

However, the best example is VTun http://vtun.sourceforge.net :))

You can also google for lots of tutorials (though most will probably be about a tun interface, i.e. IP packets instead of Ethernet frames).

Even if the final product needs to be a kernel module, I'd still start out with a tap driver, because they are much easier to debug. You can still turn it into a kernel module after most of it already works.

Not the answer you're looking for? Browse other questions tagged or ask your own question.