PF_PACKET在内核的流程
2013-08-06 17:32
155 查看
PF_PACKET在内核的流程
套接字创建
packet_create() --> 赋值packet_ops
接收流程
packet_recvmsg()
skb_recv_datagram()
__skb_recv_datagram() --> 检测sk->sk_receive_queue
自下而上
netif_receive_skb()
__netif_receive_skb()
list_for_each_entry_rcu(ptype, &ptype_all, list) --> packet_create()时dev_add_pack()添加的("./net/packet/af_packet.c" )
deliver_skb()
pt_prev->func()
packet_rcv()
__skb_queue_tail()
发送流程
packet_sendmsg()
packet_snd()
dev_queue_xmit()
....
附注:
PF_PACKET接口可以操作链路层的数据。
isock = socket(PF_PACKET, SOCK_DGRAM, 0); //为0,之后会用setsockopt之类的设置
第二个参数,在使用SOCK_RAW, SOCK_DGRAM和SOCK_PACKET的区别:
(1)使用SOCK_RAW发送的数据必须包含链路层的协议头,接受得到的数据包,包含链路层协议头。而使用SOCK_DGRAM则都不含链路层的协议头。
(2)SOCK_PACKET也是可以使用的,但是已经废弃,以后不保证还能支持,不推荐使用。
(3)在使用SOCK_RAW或SOCK_DGRAM和SOCK_PACKET时,在sendto和recvfrom中使用的地址类型不同,前两者使用sockaddr_ll类型的地址,
而后者使用sockaddr类型的地址。
(4)如socket的第一个参数使用PF_INET,第二个参数使用SOCK_RAW,则可以得到原始的IP包。
相关结构体
套接字创建
packet_create() --> 赋值packet_ops
接收流程
packet_recvmsg()
skb_recv_datagram()
__skb_recv_datagram() --> 检测sk->sk_receive_queue
自下而上
netif_receive_skb()
__netif_receive_skb()
list_for_each_entry_rcu(ptype, &ptype_all, list) --> packet_create()时dev_add_pack()添加的("./net/packet/af_packet.c" )
deliver_skb()
pt_prev->func()
packet_rcv()
__skb_queue_tail()
发送流程
packet_sendmsg()
packet_snd()
dev_queue_xmit()
....
附注:
PF_PACKET接口可以操作链路层的数据。
isock = socket(PF_PACKET, SOCK_DGRAM, 0); //为0,之后会用setsockopt之类的设置
第二个参数,在使用SOCK_RAW, SOCK_DGRAM和SOCK_PACKET的区别:
(1)使用SOCK_RAW发送的数据必须包含链路层的协议头,接受得到的数据包,包含链路层协议头。而使用SOCK_DGRAM则都不含链路层的协议头。
(2)SOCK_PACKET也是可以使用的,但是已经废弃,以后不保证还能支持,不推荐使用。
(3)在使用SOCK_RAW或SOCK_DGRAM和SOCK_PACKET时,在sendto和recvfrom中使用的地址类型不同,前两者使用sockaddr_ll类型的地址,
而后者使用sockaddr类型的地址。
(4)如socket的第一个参数使用PF_INET,第二个参数使用SOCK_RAW,则可以得到原始的IP包。
相关结构体
struct net_proto_family { int family; int (*create)(struct net *net, struct socket *sock, int protocol); struct module *owner; }; "include/linux/net.h" 365 lines struct packet_sock { /* struct sock has to be the first member of packet_sock */ struct sock sk; struct tpacket_stats stats; #ifdef CONFIG_PACKET_MMAP struct packet_ring_buffer rx_ring; struct packet_ring_buffer tx_ring; int copy_thresh; #endif struct packet_type prot_hook; spinlock_t bind_lock; struct mutex pg_vec_lock; unsigned int running:1, /* prot_hook is attached*/ auxdata:1, origdev:1; int ifindex; /* bound device */ __be16 num; struct packet_mclist *mclist; #ifdef CONFIG_PACKET_MMAP atomic_t mapped; enum tpacket_versions tp_version; unsigned int tp_hdrlen; unsigned int tp_reserve; unsigned int tp_loss:1; #endif }; "./net/packet/af_packet.c" 2504 lines struct socket { socket_state state; kmemcheck_bitfield_begin(type); short type; kmemcheck_bitfield_end(type); unsigned long flags; /* * Please keep fasync_list & wait fields in the same cache line */ struct fasync_struct *fasync_list; wait_queue_head_t wait; struct file *file; struct sock *sk; const struct proto_ops *ops; }; "./include/linux/net.h" 365 lines
相关文章推荐
- kernel中 PF_PACKET 的流程
- linux kernel packet receive flow(内核封包处理流程)
- Linux内核加载流程
- Android 上层界面到内核代码的完整的流程分析,以alarm为例子
- 嵌入式Linux内核启动流程
- linux2.6.32内核Suspend流程
- 原厂内核移植流程
- 网络数据包收发流程(四):协议栈之packet_type .
- Linux 内核引导流程
- 国嵌视频学习---linux内核启动流程
- c#网络通信框架networkcomms内核解析之二 消息处理流程
- Linux 2.6.28内核SD卡初始化流程
- 内核启动流程分析
- [转载] linux启动流程分析(1)---bootloader启动内核过程
- 银联内核与pboc内核的区别(闪付流程)
- [树莓派内核学习]Qt的开发流程
- Linux 内核编译 —— make localmodconfig 简化内核配置流程
- 重读uclinux-2008r1-rc8(bf561)内核的中断处理(3):中断处理流程
- PHP扩展开发-内核执行流程与扩展结构
- 网络数据包收发流程(四):协议栈之packet_type