Issue
In the file include/linux/ieee80211.h
we have:
struct ieee80211_mgmt {
...
union {
...
struct {
__le16 capab_info;
__le16 listen_interval;
/* followed by SSID and Supported rates */
u8 variable[0];
} __packed assoc_req;
...
} u;
} __packed __aligned(2);
I need to modify some fields in this struct. For instance, to modify capab_info
I would do it by:
...
struct ieee80211_mgmt *mgmt_hdr = skb->data;
mgmt_hdr->u.assoc_req.capab_info = 0xABCD;
But if I want to modify/insert the "SSID" field that would be localized somewhere in variable array, I do not know where and how I should allocate and modify it.
The above code I supposing skb->data struct was already allocated by mac80211 module, and what I want to do it just insert a new field (which is not listed in the static struct).
I did not find any similar code over kernel tree to use as an example. I appreciate any points you can provide me to understand it better. Thank you very much!
Solution
Permitting a structure to have a length-zero array as its final member is a GCC extension with substantially the same semantics as a standard flexible array member. The member is accessible by name and according to the element type of the array, like any other, and you may access as many elements as the actual allocated size of the structure permits. For example, mgmt_hdr->u.assoc_req.variable[i]
for i
within the allowed range.
Of course, to know how much data you can access you need either to rely on a stored length somewhere or to rely on some characteristic of the data itself, such as a terminator / sentinel. If you're hoping to extend the array in-place then you may be out of luck, and if you don't know how much space was allocated then you certainly are. In such cases, your only viable alternative is to reallocate the whole object larger, and replace all pointers to the original one with pointers to the new one. If you can't be sure of doing that, then extending the array is not an option for you, but you can still modify the existing content if you can tell where it ends.
Answered By - John Bollinger Answer Checked By - Pedro (WPSolving Volunteer)