您的位置:首页 > 其它

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包。

相关结构体

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: