jump to navigation

Add a new protocol to Linux Kernel December 2, 2008

Posted by linuxwarrior in Linux.
Tags: , ,
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:

socket1

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;
};

Advertisements

Comments»

1. James - May 17, 2009

Thats good one

2. edo - October 30, 2009

I was really looking for this thing! Thanks a lot!

3. funnystuff - September 7, 2010

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!
🙂

4. Moran - December 26, 2010

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

5. Safa - February 7, 2011

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.

6. yogs - December 4, 2011

super….thanks a lot!!!!!!

7. holy baig - January 14, 2012

thank u Thanki! you rock! it is so hard to find such good help on kernel stuff!

8. Ahmed - March 30, 2012

Very useful, Thanks

9. click - June 7, 2012

A lot of of the comments on this web site dont make sense.

10. brisbane deals - June 21, 2012

Could you message me with some hints & tips on how you made your blog look this good , I would appreciate it!

11. this month - June 21, 2012

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

12. address - June 22, 2012

Just to let you know your blog appears a little bit different on Safari on my pc using Linux .

13. michael leach talk - June 23, 2012

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.

14. account - June 23, 2012

I like the website layout . How do you make it. Its so cool.

15. save on - June 24, 2012

Is it alright to place part of this in my web site if I submit a reference point to this webpage?

16. 18k gold chain - June 24, 2012

I love this blog site layout . How do you make it!? It is so cool!

17. forex education - June 25, 2012

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

18. nursing job description - June 25, 2012

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?

19. commercial function rooms surrey - June 26, 2012

When I open up your Feed it seems to be a ton of nonsense, is the issue on my part?

20. senukexcrreviews.co.uk - June 26, 2012

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.

21. sailing holiday - June 29, 2012

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.

22. buy fridge freezer - June 30, 2012

Just to let you know your web-site looks a little bit unusual in Firefox on my laptop with Linux .

23. thumbnail - June 30, 2012

Whilst I actually like this publish, I think there was an punctuational error close towards the end of the 3rd section.

24. investigate motorbike games - July 1, 2012

Oh man! This blog is amazing. How did you make it look this good ?

25. designer rings - July 2, 2012

Wow. This blog site is sick. How can I make it look like this !?

26. video ecommerce - July 2, 2012

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.

27. home page - July 2, 2012

Is it fine to insert part of this in my web site if I submit a reference point to this web page?

28. visit budapest - July 3, 2012

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.

29. bathroom remodeling phoenix, bathroom remodeling az, phoenix kitchen remodeling, kitchen remodeling az, home remodeling phoenix - July 4, 2012

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.

30. freelaptopwithmobilephone discussions - July 6, 2012

Just to let you know your webpage looks a little bit strange on Firefox on my notebook using Linux .

31. personalisation agenda - July 6, 2012

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?

32. live your passion - July 7, 2012

Have you considered adding some social bookmarking links to these blogs. At the very least for myspace.

33. this month - July 7, 2012

Nice post . Thanks for, posting on this blog page man. Ill message you some time. I did not realise that!

34. snoring treatment boca raton - July 8, 2012

This site has got a lot of really helpful information on it. Thank you for informing me!

35. investigate how to be attractive - July 8, 2012

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.

36. e cig cartridge - July 8, 2012

I believe one of your current ads initiated my internet browser to resize, you may well want to set that on your blacklist.

37. montell william loan - July 9, 2012

A lot of of the opinions on this blog dont make sense.

38. milwaukee garage door - July 10, 2012

An interesting post right there mate . Thanks for posting .

39. cosmetic dentistry nyc - July 10, 2012

quite helpful material, in general I picture this is worthy of a bookmark, cheers

40. all inclusive honeymoon packages united states - July 11, 2012

Even though I truly like this publish, I believe there was an spelling error close to the finish of the third section.

41. tour us all inclusive resorts - July 12, 2012

You should really moderate the responses at this site

42. discount san diego electrical - July 12, 2012

I love this site layout ! How did you make it? It is rather nice.

43. dentists manhattan - July 12, 2012

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.

44. spinach recipe - July 12, 2012

Could you message me with some hints on how you made your blog site look this awesome, I would be appreciative!

45. shades waukesha - July 13, 2012

Strange , your posting turns up with a black hue to it, what color is the primary color on your site?

46. work from home site - July 14, 2012

Weird , your posting shows up with a black hue to it, what color is the primary color on your web site?

47. tanning big bear - July 14, 2012

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.

48. san francisco dentist, san francisco cosmetic dentist, dental implants san francisco , cosmetic dentist san francisco, san francisco implant dentist - July 14, 2012

Could you email me with any hints about how you made this blog look this awesome, Id appreciate it!

49. like us on facebook - July 14, 2012

Have you considered adding a few social bookmarking buttons to these sites. At the very least for facebook.

50. snoring specialist in flagstaff - July 15, 2012

How do you make a site look this sick!? Email me if you want and share your wisdom. Id be thankful!

51. patent pending - July 16, 2012

You should really control the comments listed here

52. sponsor - July 16, 2012

This site has got a lot of very helpful information on it. Cheers for sharing it with me.

53. appstar complaint - July 16, 2012

Odd , this post turns up with a black color to it, what color is the primary color on your webpage?

54. bathroom mirrors - July 16, 2012

This blog has got lots of really useful information on it! Thank you for informing me.

55. clicky - July 17, 2012

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?

56. homepage - July 17, 2012

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.

57. Jan - April 20, 2013

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!

58. argument essay - April 22, 2013

I go to see daily some web pages and websites to read content, except this weblog provides quality based content.

59. 2 place motorized astm grease working machine - August 14, 2013

Greetings! Very useful advice within this article!
It’s the little changes that produce the largest changes. Thanks a lot for sharing!

60. mcdonalddowns76275 - April 9, 2016

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

61. Tho Huynh - August 23, 2016

what is the point of step 1 here. Can you explain?
What`s the difference between proto and proto_ops?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: