在 linux 内核中添加了新的网络协议 [英] addition of a new network protocol in the linux kernel

查看:30
本文介绍了在 linux 内核中添加了新的网络协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在linux内核中我们可以在传输层添加我们自己的协议,类似于TCP、UDP等

I know that in the linux kernel we can add our own protocol at the transport layer, similar to TCP, UDP etc.

是否有任何钩子可以在网络层注册一个新的协议,类似于IP、ARP,可以将数据包传输到应用程序,以及如何在linux内核中添加该协议?

Are there any hooks to register a new protocol, at the network layer, similar to IP, ARP, which could transfer the packets to the application and how to add this protocol in the linux kernel?

推荐答案

要处理从用户空间到您的协议的通信,请向内核注册您的协议 套接字 API.这允许你从用户空间创建一个普通的套接字.

To handle communication from userspace to your protocol, register your protocol with the kernel sockets API. This allows you to create a normal socket from userspace.

查看相关代码示例.

static const struct proto_ops rfcomm_sock_ops = {
     .family         = PF_BLUETOOTH,
     .owner          = THIS_MODULE,
     .bind           = rfcomm_sock_bind,
     .connect        = rfcomm_sock_connect,
     .listen         = rfcomm_sock_listen,
     .
     .
     .
     .accept         = rfcomm_sock_accept,

};
 
static const struct net_proto_family rfcomm_sock_family_ops = {
     .family         = PF_BLUETOOTH,
     .owner          = THIS_MODULE,
     .create         = rfcomm_sock_create
};


要注册协议,您必须填写proto_ops 结构.此结构遵循在内核内部其他地方观察到的面向对象模式.此结构定义了一个接口,供开发人员实现自己的套接字接口.


To register a protocol you will have to fill the proto_ops structure. This structure follows the object oriented pattern observed elsewhere inside the kernel. This structure defines an interface to follow for developers implementing their own socket interface.

实现接口定义的bind、connect、listen等函数,并将函数指针赋值给结构体入口.为操作界面未涵盖的功能定义 ioctl.

Implement the functions the interface defines such as bind, connect, listen, and assign the function pointer to the structure entry. Define ioctl's for functionality not covered by the operations interface.

您最终得到一个结构,稍后您将其嵌入 socket struct 我们从 create 函数返回.

You end up with a structure that later you embed at the socket struct we return from the create function.

结构 net_proto_family 定义一个新的协议族.此结构包括 create 函数,您的函数实现应在其中填充填充有 proto_ops 结构的套接字结构.

Struct net_proto_family defines a new protocol family. This structure includes the create function where your function implementation should populate a socket struct filled with the proto_ops struct.

然后使用 sock_register 注册家庭,如果一切正常,您应该能够从用户空间创建一个合适的套接字.

After that register the family with sock_register, and if everything is ok you should be able to create a proper socket from userspace.

在内部,协议可能应该使用 skbuffs(请参阅此处到这里交流网络设备.

Internally the protocol should probably use skbuffs (see here, and here) to communicate with the networking devices.

skbuffs 是 Linux 内核中处理网络数据包的通用方式.数据包由网卡接收,放入一些skbuff,然后传递到网络堆栈,网络堆栈一直使用skbuff.

skbuffs are the universal way of handling network packets in the linux kernel. The packets are received by the network card, put into some skbuffs and then passed to the network stack, which uses the skbuff all the time.

这是在linux内核中实现网络协议的基本数据结构和io路径.

This is the basic data structure and io path to implement a networking protocol inside the linux kernel.

我不知道从头到尾描述此过程的文档.来源与您同在.

I am not aware of a document that describes this procedure from start to finish. The source is with you on this one.

这篇关于在 linux 内核中添加了新的网络协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆