views:

1141

answers:

2

Is it possible to compile a linux kernel(2.6) module that includes functionality defined by non-kernel includes?

For example:


kernelmodule.h

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>   // printk()
// ...
#include <openssl/sha.h>
// ...


Makefile

obj-m := kernelmodule.o
all:
    $(MAKE) -C /lib/modules/`uname -r`/build M=`pwd` modules

clean:
    $(MAKE) -C /lib/modules/`uname -r`/build M=`pwd` clean
    $(RM) Module.markers modules.order

The kernel module I have written and are trying to compile contains functionality found in a number of openssl include files.

The standard makefile presented above doesn't allow includes outside of the linux headers. Is it possible to include this functionality, and if so, could you please point me in the right direction.

Thanks, Mike

+6  A: 

The kernel cannot use userspace code and must stand alone (i.e. be completely self contained, no libraries), therefore it does not pick up standard headers.

It is not clear what benefit trying to pick up userspace headers is. If there are things in there that it would be valid to use (constants, some macros perhaps provided they don't call any userspace functions), then it may be better to duplicate them and include only the kernel-compatible parts that you need.

It is not possible to link the kernel with libraries designed for userspace use - even if they don't make any OS calls - because the linking environment in the kernel cannot pick them up.

Instead, recompile any functions to be used in the kernel (assuming they don't make any OS or library calls - e.g. malloc - in which case they'll need to be modified anyway). Incorporate them into your own library to be used in your kernel modules.


Recent versions of linux contain cryptographic functions anyway, including various SHA hashes - perhaps you can use one of those instead.


Another idea would be to stop trying to do crypto in kernel-space and move the code to userspace. Userspace code is easier to write / debug / maintain etc.

MarkR
+3  A: 

I have taken bits of userspace code that I've written and converted it to work in kernel space (i.e. using kmalloc(), etc), its not that difficult. However, you are confined to the kernel's understanding of C, not userspace, which differs slightly .. especially with various standard int types.

Just linking against user space DSO's is not possible, the Linux kernel is monolithic, completely self contained. It does not user user space libc, libraries or other bits as others have noted.

9/10 times, you will find what you need somewhere in the kernel. Its very likely that someone else ran into the same need you have and wrote some static functions in some module to do what you want .. just grab those and re-use them.

In the case of crypto, as others have said, just use what's in the kernel. One thing to note, you'll need them to be enabled in kconfig which may or may not happen depending on what the user selects when building it. So, watch out for dependencies and be explicit, you may have to hack a few entries in kconfig that also select the crypto API you want when your module is selected. Doing that can be a bit of a pain when building out of tree.

So on the one hand we have "just copy and rename stuff while adding overall bloat", on the other you have "tell people they must have the full kernel source". Its one of the quirks that come with a monolithic kernel.

With a Microkernel, almost everything runs in userspace, no worries linking against a DSO for some driver ... its a non issue. Please don't take that statement as a cue to re-start kernel design philosophy in comments, that's not in the scope of this question.

Tim Post