Add a new protocol to Linux Kernel December 2, 2008
Posted by linuxwarrior in Linux.Tags: kernel, Linux, protocol
trackback
Written By : Vishal Thanki
The Linux Network sub-system supports several protocols. It is flexible enough to allow addition of new protocols. These protocols are accessible to user application through socket interface by means of protocol family. The subsequent sections will cover major steps to add a new protocol family (with Linux kernel 2.6.24 as reference). The implementation of the new protocol is not in scope of this document.
Top level view of Linux Network (Kernel) Sub-system:
For the scope of this document, we can consider the network sub-system in Linux consisting of three layers as shown above.
1. The top most “SOCKET” layer takes care of all socket related system calls. It identifies the protocol family and forwards the call to respective protocol implementation.
2.The next layer implements transport and network layer protocols, where we can introduce our new protocol family.
3. The lowest layer is the network controller device driver providing hardware access.
Adding a New Protocol Family:
The Linux kernel network subsystem data structures, “struct proto” (/include/net/sock.h) and the “struct net_proto_family” (/include/linux/net.h) encapsulates the protocol family implementation.
Following step by step code snippets show a simplified example to register the new protocol family similar to TCP/IP stack (using IP as the network layer). Please note, all the protocol specific new functions (to be implemented) has prefix “my_”.
1) Initialize an instance of “struct proto” and register to Linux network sub-system with call “proto_register()”.
/* Protocol specific socket structure */
struct my_sock {
struct inet_sock isk;
/* Add the Protocol implementation specific data members per socket here from here on */
};
struct proto my_proto = {
.close = my_close,
.connect = my_connect,
.disconnect = my_disconnect,
.accept = my_accept,
.ioctl = my_ioctl,
.init = my_init_sock,
.shutdown = my_shutdown,
.setsockopt = my_setsockopt,
.getsockopt = my_getsockopt,
.sendmsg = my_sendmsg,
.recvmsg = my_recvmsg,
.unhash = my_unhash,
.get_port = my_get_port,
.enter_memory_pressure = my_enter_memory_pressure,
.sockets_allocated = &sockets_allocated,
.memory_allocated = &memory_allocated,
.memory_pressure = &memory_pressure,
.orphan_count = &orphan_count,
.sysctl_mem = sysctl_tcp_mem,
.sysctl_wmem = sysctl_tcp_wmem,
.sysctl_rmem = sysctl_tcp_rmem,
.max_header = 0,
.obj_size = sizeof(struct my_sock),
.owner = THIS_MODULE,
.name = "NEW_TCP",
};
rc = proto_register(&my_proto, 1);
2) Provide an interface to create the new protocol specific socket creation routine. Register our handler to socket layer using call “sock_register()”. The “family” member specifies the address family for the new protocol.
struct net_proto_family my_net_proto = {
.family = AF_INET_NEW_TCP,
.create = my_create_socket,
.owner = THIS_MODULE,
};
rc = sock_register(&my_net_proto, 1);
3) The new protocol’s address family is the only interface for user level socket calls to reach the new protocol implementation. The new protocol’s address family AF_INET_NEW_TCP should be added in /include/linux/socket.h. Any socket() call with this new address family will be directed to my_create_socket() function in kernel, and which establishes the use of new protocol stack for all subsequent socket operations.
4) The protocol can be connection oriented or connection less as chosen by the protocol implementer. In the socket creation routine, protocol implementer specifies a “struct proto_ops” (/include/linux/net.h) instance. The socket layer calls function members of this proto_ops instance before the protocol specific functions are called (as defined in step# 1). A typical implementation of the create socket routine for TCP/IP like (connection oriented) new protocol:
static struct proto_ops my_proto_ops = {
.family = PF_INET,
.owner = THIS_MODULE,
.release = inet_release,
.bind = my_bind,
.connect = inet_stream_connect,
.socketpair = sock_no_socketpair,
.accept = inet_accept,
.getname = inet_getname,
.poll = my_poll,
.ioctl = inet_ioctl,
.listen = my_inet_listen,
.shutdown = inet_shutdown,
.setsockopt = sock_common_setsockopt,
.getsockopt = sock_common_getsockopt,
.sendmsg = inet_sendmsg,
.recvmsg = sock_common_recvmsg,
};
static int my_create_socket(struct socket *sock, int protocol)
{
struct sock *sk;
int rc;
sk = sk_alloc(PF_INET_NEW_TCP, GFP_KERNEL, &my_proto, 1);
if (!sk) {
printk("failed to allocate socket.\n");
return -ENOMEM;
}
sock_init_data(sock, sk);
sk->sk_protocol = 0x0;
sock->ops = &my_proto_ops;
sock->state = SS_UNCONNECTED;
/* Do the protocol specific socket object initialization */
return 0;
};
Thats good one
I was really looking for this thing! Thanks a lot!
Hola
I like your post. It is very interesting to read.
I am curious how come I didn’t know about this blog earlier.
I will try to share. Many of my friends will appreciate this.
Thank you and keep’em coming!
🙂
Hi
Thanks a lot
It was very helpful
Can you please add also some of the functions implementation:
my_close
my_bind
and so?
(I had some errors when I tried to implement them)
Thanks
Moran
Thanks a lot. I have one question however, If I add the protocol as module to the kernel and then write an application to create a socket() using the added protocol, the application enter infinate loop and does not exist. When I check the messages in /var/log/messages I get message of 8 lines with question marks and memery address. Do you know what is the problem.
super….thanks a lot!!!!!!
thank u Thanki! you rock! it is so hard to find such good help on kernel stuff!
Very useful, Thanks
A lot of of the comments on this web site dont make sense.
Could you message me with some hints & tips on how you made your blog look this good , I would appreciate it!
If you dont mind, exactly where do you host your weblog? I am hunting for a great web host and your blog seams to be extremely fast and up most the time
Just to let you know your blog appears a little bit different on Safari on my pc using Linux .
When I initially commented I clicked the Notify me any time new comments are added checkbox and now each time a comment is added I get 4 messages with the exact same comment.
I like the website layout . How do you make it. Its so cool.
Is it alright to place part of this in my web site if I submit a reference point to this webpage?
I love this blog site layout . How do you make it!? It is so cool!
If you dont mind, where do you host your web site? I am looking for a very good web host and your website appears to be extremely fast and up most the time
Jesus Christ there is plenty of spammy feedback on this webpage. Have you ever before thought about attempting to eliminate them or installing a plugin?
When I open up your Feed it seems to be a ton of nonsense, is the issue on my part?
It seems to me that this site doesnt download in a Motorola Droid. Are other folks getting the same problem? I enjoy this blog and dont want to have to miss it whenever Im away from my computer.
I tried taking a look at your web site on my iphone and the page layout does not seem to be right. Might want to check it out on WAP as well as it seems most cellphone layouts are not really working with your website.
Just to let you know your web-site looks a little bit unusual in Firefox on my laptop with Linux .
Whilst I actually like this publish, I think there was an punctuational error close towards the end of the 3rd section.
Oh man! This blog is amazing. How did you make it look this good ?
Wow. This blog site is sick. How can I make it look like this !?
Heya, I just hopped over to your web site using StumbleUpon. Not somthing I would typically browse, but I enjoyed your thoughts none the less. Thank you for making something well worth reading through.
Is it fine to insert part of this in my web site if I submit a reference point to this web page?
Hi, I just hopped over to your site using StumbleUpon. Not somthing I might usually read, but I appreciated your views none the less. Thanks for making some thing worthy of browsing.
Whenever I originally left a comment I clicked on the Notify me any time new comments are added checkbox and now each time a comment is added I get four messages with the same comment.
Just to let you know your webpage looks a little bit strange on Firefox on my notebook using Linux .
I was basically wanting to know if you ever considered adjusting the design of your web site? It is very well written; I enjoy what you have got to state. But maybe you could add a little more in the way of content so people can connect with it better. You have got a great deal of text for only having one or two graphics. Maybe you can space it out better?
Have you considered adding some social bookmarking links to these blogs. At the very least for myspace.
Nice post . Thanks for, posting on this blog page man. Ill message you some time. I did not realise that!
This site has got a lot of really helpful information on it. Thank you for informing me!
Wanted to drop a remark and let you know your Feed isnt working today. I tried including it to my Google reader account and got nothing.
I believe one of your current ads initiated my internet browser to resize, you may well want to set that on your blacklist.
A lot of of the opinions on this blog dont make sense.
An interesting post right there mate . Thanks for posting .
quite helpful material, in general I picture this is worthy of a bookmark, cheers
Even though I truly like this publish, I believe there was an spelling error close to the finish of the third section.
You should really moderate the responses at this site
I love this site layout ! How did you make it? It is rather nice.
The design for the site is a little bit off in Epiphany. Nevertheless I like your blog. I may have to use a normal web browser just to enjoy it.
Could you message me with some hints on how you made your blog site look this awesome, I would be appreciative!
Strange , your posting turns up with a black hue to it, what color is the primary color on your site?
Weird , your posting shows up with a black hue to it, what color is the primary color on your web site?
Have you given any kind of consideration at all with converting your current webpage in to Spanish? I know a several of translaters here that will would certainly help you do it for free if you want to make contact with me personally.
Could you email me with any hints about how you made this blog look this awesome, Id appreciate it!
Have you considered adding a few social bookmarking buttons to these sites. At the very least for facebook.
How do you make a site look this sick!? Email me if you want and share your wisdom. Id be thankful!
You should really control the comments listed here
This site has got a lot of very helpful information on it. Cheers for sharing it with me.
Odd , this post turns up with a black color to it, what color is the primary color on your webpage?
This blog has got lots of really useful information on it! Thank you for informing me.
I had been wanting to know if you ever thought of modifying the layout of your blog? Its well written; I love what you have got to say. But maybe you could create a a bit more in the way of content so people might connect to it better. You have got an awful lot of wording for only having one or two pictures. Maybe you can space it out better?
Have you given any kind of consideration at all with translating your main blog in to Chinese? I know a couple of of translaters right here which would help you do it for free if you wanna contact me.
Hello, I think your site might be having browser compatibility issues.
When I look at your website in Ie, it looks fine but when opening in Internet
Explorer, it has some overlapping. I just wanted to give you a
quick heads up! Other then that, awesome blog!
I go to see daily some web pages and websites to read content, except this weblog provides quality based content.
Greetings! Very useful advice within this article!
It’s the little changes that produce the largest changes. Thanks a lot for sharing!
Oh,and one more thing jaws is just a normal shark and the megaladon is a ANCIENT ANIMAL!!n:@ Click http://s.intmainreturn0.com/people3091630
what is the point of step 1 here. Can you explain?
What`s the difference between proto and proto_ops?