0

I am trying send a list/array of struct to kernel space from userspace. Similar to Link As recommended there, I am thinking of using sockets for which i found link. Message is set hello in this line

strcpy(NLMSG_DATA(nlh), "Hello");

I tried

NLMSG_DATA(nlh) = my_list

That gave me error: lvalue required as left operand of assignment.

How can I change this to send an array/list using netlinks? If it can't be send this way, how else easily can I do this?

Update

My structure

typedef struct {
 int val1;
 int val2;
} mystruct;

I need to allocate an array/list of these in kernel memory so other system calls can access that list.

Community
  • 1
  • 1
Alkesh
  • 61
  • 1
  • 3
  • 9

2 Answers2

0

NLMSG_DATA() evaluates to a pointer rvalue, so you need to use a copying function like memcpy(NLMSG_DATA(nlh), my_list, sizeof my_list).

The exact details will depend on your data structure. Presumably you will want to send the number of list entries, then each entry separately.

caf
  • 233,326
  • 40
  • 323
  • 462
  • So, I build the list, and send the pointer value to kernel. By doing so, can't I access the list in kernel space by just my_userspace_list->next? – Alkesh Dec 14 '10 at 03:33
  • @Alkesh: No. All accesses to user memory in kernel space use `copy_from_user` or similar, and each must be checked for safety. Don't make the kernel chase a linked list through user memory. – ephemient Dec 14 '10 at 03:43
  • @Alkesh: No, userspace pointers should not be directly dereferenced from kernel space. The correct method is to use `copy_from_user()` and related functions - but it is likely that your list should be serialised into an array for transport between user and kernel space. – caf Dec 14 '10 at 03:44
  • @ephemient/@caf - so memcpy fixed the issue. Now, since I only have the address, how would I use copy_from_user() as I didnt pass the size? I tried the this: copy_from_user(list_in_kernel, (mystruct \*)nlmsg_data(nlh), sizeof((mystruct\*)nlmsg_data(nlh))); That didn't work. – Alkesh Dec 14 '10 at 04:58
  • @Alkesh: You should probably just be directly sending an array of `mystruct` s in the netlink message - then you would just need to allocate a block of kernel memory with `kmalloc()` and do a `memcpy()` on the kernel side. – caf Dec 14 '10 at 05:11
  • @caf- How can I send an array using netlink? I cant find any examples of sending an array directly? – Alkesh Dec 14 '10 at 05:21
  • can you pass `my_list` instead of `&my_list`? – gangadhars May 23 '14 at 05:24
0

You cannot send pointer-based structures using netlink sockets. See the packet structure of netlink: all data must be in a single block.

Martin v. Löwis
  • 124,830
  • 17
  • 198
  • 235