PF_PACKET在内核的流程

时间:2021-03-23 15:22:44
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包。
 
 
相关结构体
 struct net_proto_family {
int family;
int (*create)(struct net *net, struct socket *sock, int protocol);
struct module *owner;
};
"include/linux/net.h" 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:, /* prot_hook is attached*/
auxdata:,
origdev:;
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:;
#endif
};
"./net/packet/af_packet.c" 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" lines