Accessing wireless interface (802.11) at MAC layer (Linux) - linux

I spent the last days reading through man pages, documentations and anything else google brought up, but I suppose I'm even more confused now than I was at the beginning.
Here is what I want to do: I want to send and receive data packets with my own layer 3-x protocol(s) via a wireless interface (802.11) on Linux systems with C/C++.
So far, so good. I do not require beacons, association or any AP/SSID related stuff. However, for data transmissions I'd like the MAC layer to behave "as usual", meaning unicast packets are ACK'd, retransmissions, backoff etc. I'd also like to enjoy the extended QoS capabilites (802.11e with 4 queues and different access categories). Promiscuous mode on the other hand is not a concern, I require only broadcast packets and packets sent to the specific station.
What would be the right way to go about it? Most of the documentation out there on raw socket access seems to be focused on network sniffing and that does not help. I've been playing around with the monitor mode for some time now, but from what I've read so far, received packets are not ACK'd in monitor mode etc.
Without monitor mode, what would be the alternative? Using ad hoc mode and unix raw sockets? Or do I have to fiddle around with the drivers?
I'm not looking for a complete solution, just some good ideas, where to start. I read through the man pages for socket(2), socket(7) and packet(7) but that did not help concerning the behaviour of the MAC layer in different modes.
Thanks in advance.

802.11 is layer 2 (and 1) protocol specification. It was designed in a way, which allows higher-layer protocols to treat it as Ethernet network. Addressing and behaviour is generally the same. So for a layer 3 protocol you should not be concerned about 802.11 at all and write your code as if you were expecting it to run on Ethernet network.
To make it work you should first connect to a wireless network of some sort (which is conceptually equal to plugging a wire into a Ethernet card). Here you may choose ad-hoc (aka IBSS) or infrastructural (aka BSS) network (or PBSS once 802.11ad is approved ;).
Operating cards without any sort of association with network (just spitting out packets on air) is not a good idea for a couple of reasons. Most importantly it's very hardware dependent and unreliable. You can still do it using RF mon (AKA monitor mode) interface on one side and packet injection (using radiotap header) on the other but I don't recommend that. Even if you have a set of identical cards you'll most likely encounter hard to explain and random behaviour at some point. 802.11 NICs are just not designed for this kind of operation and keep different mount of state inside firmware (read about FullMAC vs. SoftMAC cards). Even SoftMAC cards differ significantly. For example theoretically in monitor mode, as you said, card should not ACK received packets. There are cards though that will ACK received frame anyway, because they base their decision exclusively on the fact that said frame is addressed to them. Some cards may even try to ACK all frames they see. Similar thing will happen with retransmissions: some cards will send injected packet only once (that's how it should work). In other NICs, retransmissions are handled by hardware (and firmware) and driver cannot turn it off, so you will get automatic retransmission even with injected data.
Sticking with layer 3 and using existing modes (like ad hoc), will give you all capabilities you want and more (QoS etc.). Ethernet frame that you send to interface will be "translated" by the kernel to 802.11 format with QoS mapping etc.
If you want to find out about MAC behaviour in various modes you'll have to either read the mac80211 code or 802.11 standard itself. http://linuxwireless.org wiki my help you with a few things, but kernel hackers are usually to busy to write documentation other than comments in the code ;)
L3 protocol implementation itself can be also done either in kernel or user mode (using raw sockets). As usual kernel-side will be harder to do, but more powerful.

Because you want to create own network layer protocol (replacement for IP), the keyword is: "raw ethernet socket". So ignore "Raw IP socket" stuff.
This is where to start:
int sockfd = socket( PF_PACKET, SOCK_RAW, htons(XXX) );
Correct man page is: packet(7).
Find more information by googling with the keyword.
One quite complete example here.
Edit: The link to the example seems to be currently broken: another examples

Probably you want something like libpcap.
Libpcap allows you to read/inject raw packets from/into a network interface.

First, there’s something you should be aware of when trying to transmit raw 802.12 frames- the device driver must support packet injection.
You mentioned monitor mode, which is at a high level the rx equivalent of the injection capability- which is not a “mode”, jist a capability/feature. I say this because some 892.11 device drivers on Linux either:
Support monitor mode and frame injection
Support monitor mode and not frame injection
Support neither
I don’t know any straightforward way to check if the driver supports frame injection aside from attempting frame injection and sniffing the air on another device to confirm it was seen.
Monitor mode is usually easy to check by using sudo wlan0 set monitor and seeing what the return code and/or output is.
It’s been a few years since I’ve worked on this but at the time, very few devices supported monitor mode and frame injection “out of the box”. Many only supported monitor mode with a modified version of the vendor or kernel driver
You’ll want to make sure your device has a driver available that fully supports both. This sort of task (frame monitoring and injection) is common for Penetration Testers who tend to use Kali Linux, which is really just an Ubuntu distribution with a bunch of “hacking” tools and (modified) 802.11 device drivers preloaded and in its repositories. You can often save time finding a well supported card by using a search engine to find the device and driver recommended for Kali users
I’m bringing this monitor/injection capability up explicitly because when I first worked on a similar project a few years ago, I needed to use a patched version of the official kernel driver to support monitor mode- it was an rtl8812au chipset. At that time, I made an incorrect assumption that monitor mode support in the driver implied full injection support. I spent 2 days banging my head against the wall, convinced my frames weren’t built correctly in my application, causing no frames to leave the card. Turned out I needed a more recent branch of the driver I was using to get the full injection support. This driver in particular supports both monitor mode and frame injection now. The most frustrating thing about diagnosing that problem was that I did not receive any errors from system calls or in kernel messages while trying to transmit the frames- they were just being silently discarded somewhere, presumably in the driver
To your main question about how to do this- the answer is almost certainly libpcap if you’re writing your application in C/C++ as libpcap provides not only packet capture APIs but also packet injection APIs
If you do it in Python, scapy is an excellent option. The benefit of Python/scapy is that
Python code is much quicker to write than C
scapy provides a significant amount of classes that you can use to intuitively create a frame layer by layer
Because the layers are implemented as classes, you can also extend and “register” existing classes to make certain frames easier to create (or parse when received)
You can do this in straight C using the UNIX sockets API with raw sockets directly- but you’ll have to deal with things that libpcap exists to abstract from you- like underlying system calls that may be required when doing raw frame transmission, aside from the standard socket(), send(), recv() calls. I’m speculating that there are a handful of ioctl calls you may need at the least, specific to the kernel 802.11x subsystem/framework- and these ioctl() calls and their values may not be completely portable across different major kernel versions. I’ll admit I ended up not using the pure C (without libpcap) approach, so I’m not 100% sure about this potential problem. It’s something you should look more into if you plan to do it without libpcap. I don’t recommend it unless you have a really good reason to

It sounds like you are getting the media and transport layers mixed up.
802.11 is what's commonly referred to as a "link", "physical", or "media" layer, meaning it only deals with the transmission of raw datagrams.
Concepts like ACKs, retransmissions, backoff (flow control) apply to the "transport" layer, and those particular terms are strongly associated with TCP/IP.
Implementing your own complete transport layer from scratch is very difficult and almost certainly not what you want to do. If instead you want to use the existing TCP/IP stack on top of your own custom interpretation of 802.11, then you probably want to create a virtual network interface. This would act as an intermediary between TCP/IP and the media layer.
Hopefully this gives you some better context and keywords to look for.

Related

Is it possible to estimate the arbitration time on MAC layer with linux kernel tracing? (ieee802.11, ath9k, ad hoc network)

I want to measure the arbitration time on the MAC layer at runtime.
Setup: I have installed a patched Linux kernel with an ath9k driver. I have no tools other than my two computers to find out what I am looking for. Both exchange messages via a 5G ad hoc network, so nothing else between them. Moreover, both use ptpd via ethernet. I followed the instructions on https://wireless.wiki.kernel.org/en/users/drivers/ath9k/debug to enable debug mode. However, when I enter dmesg I am not sure I see more information than before. Additionally, retransmissions on the MAC layer are disabled.
Anyway, I found out that I cannot see what exactly happens on my Wifi adapter with my setup. But still, I want to get close, for instance, to see a tendency, rise, or fall of arbitration time. I had the idea to use kernel traces to get as close as possible to the MAC layer. Btw, if I have wrong assumptions, I appreciate any hint. I assume the messages pass the following layers (omitting the application here):
kernel (tx) UDP socket -> IP -> 80211a -> PHY -> 80211a -> IP -> UDP socket (rx) kernel
Current state: So far, I figured out with a little help of trace-cmd, ftrace, and the source code of ath9k that the closest I can get happens in functions of xmit and mac. In my traces I can see skb is consumed at one point and a DMA for the wifi device. From there, I suppose, it is up to my wifi adapter to do the arbitration and transmission. The next I can trace happens on the receiver kernel. There I can see something like an rx interrupt. So between those two measurements occur MAC layer operations and the physical transmission. Am I correct so far? Is it true that the duration on IP is negligible because there is no routing in ad hoc (A)? If so, I can measure the mac layer operations and transmission time so far, but I can not separate them from each other.
In the source code of ath9k are lots of other functions that did not appear in my traces like ath_tx_send_normal, ath_tx_complete. So I am wondering if I missed something in the ath9k debug tutorial. Is there a possibility at all of retrieving the information I am looking for (B)? If not, is it sufficient enough to calculate the physical transmission time and subtract it from the duration it takes from one kernel to the other (C)?
Besides, I would like to know how I can access strings that appear in functions like read_file_phy_err, ath_tx_complete, and the ath_dbg information (D).
I would really appreciate any hints to help me understand the processes on Linux kernel, ath9k, and 80211. If I could not make clear what I do, I will try to clarify. Thanks in advance.

how can we send/received frames in layer 2 (Using MAC address ) in Linux kernel

I am doing research in parallel computing side in my master. we are creating a real TriBA-Network a Triple based architecture (for multi core processor network). For this i am working on network part. So i have to implement routing in this network on layer 2 level. i have done routing on layer 3 (network layer) using TCP\IP protocol. But we have to send/received frames (not packets) on layer 2.
Maybe i can use RAW socket for sending frames using network programming. but how we can received these frames in remote PC and forward.
If somebody know about lower level communication that i can use for this task kindly share here.
Advance Thank you.
Computer's are connected in that topology over LAN locally TriBA Topology Network
From the question it's not that clear why this is to be done in kernel.
However, I understand from the question that some lower-layer approach is needed. If there are some explicit latency or throughput requirements then it needs to be stated in the question.
So, if we postpone the hypothetical in-kernel approach, indeed, you may use PF_PACKET sockets to send them. The same way, using PF_PACKET sockets you may receive them at the remote side. However, typically, this still involves certain overhead since the kernel copies data between its own buffers and the socket buffers accessed by the user in the userland.
From this perspective you may consider PACKET_MMAP technique to set up direct memory mapping to access data from kernel-side buffers. This might be a really good improvement in terms of, say, throughput, but it's rather controversial whether this will fit your need of forwarding.
Another one suggestion is to use some sort of like data plane kit (please find the link on that page) which may have a set of libraries designed specially for kernel-bypass networking (similar to PACKET_MMAP, but much better) and for certain tasks like L2 forwarding.
All in all, it is likely that you have enough tools to do what you need, however, a more detailed description would be better for understanding.

Packet injection, filtering and mangling WITHOUT GPL?

I'll have to do packet inspection, mangling, dropping and injection of packets on a Linux system. Ideally, this would be in user space and on IP packets and Ethernet frames, too.
Unfortunately, I cannot go OpenSource for this which basically rules out any approach based on NFQUEUE and libnetfilter_queue, since all of netfilter (and their dog) is GPL only.
I thought about using TAP/TUN devices in parallel to controlling netfilter by simply calling iptables, but this seems to be messy at best...
So, are there any alternatives to netfilter?
I believe your issue is that libnetfilter is subject to the GPLv2 licence (not LGPL) and any project building on these would thus be subject to the GPLv2 licence too; this is what you want to avoid (I think).
An alternative would be to use a language binding which is not subject to the GPLv2 licence. One candidate would appear to be the Go bindings - see here for example, which appears to be under the Apache licence. I have obviously not checked the provenance of every file therein. Another way would be to divide your application into two - a small layer that communicates with Netfilter, communicating via (e.g.) an RPC interface with the rest of your application.
However, the last time I faced this, I used libpcap instead, which is BSD licensed. It's a little known fact that libpcap can send raw packets as well as receive them. However, it is much lower level than netfilter - you get raw packets and that's about it.
The license does not apply to your userspace application.

MAU data needed for SNMP agent

I am trying to write an snmp agent for RFC 4836, Definitions of Managed Objects for IEEE 802.3 Medium Attachment Units (MAUs), to run on an embedded Linux system (Linux server 2.6.35.12+). I've used mib2c to set up my frameworks, but am stalled on finding where to get the data to fill in. I am not sure where I should be looking: if the MAU is part of the eth device or a separate interface, if I should be looking somewhere in the /sys or /proc fs, or if I need to access the device registers directly (or both?).
I know there is a lot of different data needed, and am not asking for a roadmap with everything, but at this point I am hung up and not sure where I should be concentrating my efforts - driver code?
Sorry if this seems a dumb question, but I have been looking online and in StackOverflow without finding what I need.
It turned out the information I needed is available from mii-tool. Despite what you may see on the web, this is not obsolete (it may have been a few years ago but it seems to have been updates). Or, you can access the device registers directly using ioctl calls to SIOCGMIIREG, to avoid having to parse command output.

How to implement web services on an embedded device?

We have an embedded device that needs to interact with an enterprise software system.
The enterprise system currently uses many different mechanisms for communication between its components: ODBC, RPC, proprietary protocol over TCP/IP, and is moving to .Net-implmented web services.
The embedded device runs a flavor of *nix, so we're looking at what the best interaction mechanism is.
The requirements for the communication are:
Must run over TCP/IP.
Must also run over RS-232 or USB.
Must be secure (e.g. HTTPS or SSL).
Must be capable of transferring ~32MB of data.
Our current best option is gSOAP.
Does anyone out there in SO-land have any other suggestions?
Edit: Steven's answer gave me the most new pointers. Thanks to all!
You can define RESTful services the use HTTPS (which uses TCP/IP by definition) and is capable of transferring any amount of data.
The advantage of REST over SOAP is that REST is simpler. It can use JSON instead of XML which is simpler.
It has less overhead than the SOAP protocol.
Can't you just use SSL over TCP?
If you have some kind of *nix (may I guess? It's either QNX or embedded linux, right?) it should work pretty much out of the box via Ethernet, USB and RS232. Keep thing simple.
32mb is plenty of memory for this task. I would allocate between 2 and 4 mb of memory for networking & encryption (code + data).
It's not real clear why you want to tie this to a remote-procedure-call protocol like SOAP. Are there other requirements you aren't mentioning?
In general, though, this sort of thing is handled very easily using normal web-based services. You can get very lightweight http processors written in C; see this Wikipedia article for comparisons of a number of them. Then a REST interface will work fine. There are network interfaces that treat USB as a TCP connection, as well.
If you must be able to run over RS232, you might want to look elsewhere; in that case, something like sftp might do better. Or write a simple application-layer protocol that you can run over an encrypted connection.
If you are going to connect your application using RS232, I assume that you will be using PPP to connect the device to the internet. The amount of data that you are proposing to transfer is somewhat worrisome, however. Most RS232 connections are limited to 115200 baud which, ignoring the overhead required for TCP/IP/PPP framing is going to yield a transfer rate of at most 11,000 bytes per second. This implies that it will take a minimum of approximately 2800 seconds or 46 minutes to make whatever transfer that you intend.

Resources