Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

packet: Diag core and basic socket info dumping

The diag module can be built independently from the af_packet.ko one,
just like it's done in unix sockets.

The core dumping message carries the info available at socket creation
time, i.e. family, type and protocol (in the same byte order as shown in
the proc file).

The socket inode number and cookie is reserved for future per-socket info
retrieving. The per-protocol filtering is also reserved for future by
requiring the sdiag_protocol to be zero.

Signed-off-by: Pavel Emelyanov <xemul@parallels.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Pavel Emelyanov and committed by
David S. Miller
96ec6327 2787b04b

+139
+1
include/linux/Kbuild
··· 195 195 header-y += sock_diag.h 196 196 header-y += inet_diag.h 197 197 header-y += unix_diag.h 198 + header-y += packet_diag.h 198 199 header-y += inotify.h 199 200 header-y += input.h 200 201 header-y += ioctl.h
+24
include/linux/packet_diag.h
··· 1 + #ifndef __PACKET_DIAG_H__ 2 + #define __PACKET_DIAG_H__ 3 + 4 + #include <linux/types.h> 5 + 6 + struct packet_diag_req { 7 + __u8 sdiag_family; 8 + __u8 sdiag_protocol; 9 + __u16 pad; 10 + __u32 pdiag_ino; 11 + __u32 pdiag_show; 12 + __u32 pdiag_cookie[2]; 13 + }; 14 + 15 + struct packet_diag_msg { 16 + __u8 pdiag_family; 17 + __u8 pdiag_type; 18 + __u16 pdiag_num; 19 + 20 + __u32 pdiag_ino; 21 + __u32 pdiag_cookie[2]; 22 + }; 23 + 24 + #endif
+8
net/packet/Kconfig
··· 14 14 be called af_packet. 15 15 16 16 If unsure, say Y. 17 + 18 + config PACKET_DIAG 19 + tristate "Packet: sockets monitoring interface" 20 + depends on PACKET 21 + default n 22 + ---help--- 23 + Support for PF_PACKET sockets monitoring interface used by the ss tool. 24 + If unsure, say Y.
+2
net/packet/Makefile
··· 3 3 # 4 4 5 5 obj-$(CONFIG_PACKET) += af_packet.o 6 + obj-$(CONFIG_PACKET_DIAG) += af_packet_diag.o 7 + af_packet_diag-y += diag.o
+104
net/packet/diag.c
··· 1 + #include <linux/module.h> 2 + #include <linux/sock_diag.h> 3 + #include <linux/net.h> 4 + #include <linux/packet_diag.h> 5 + #include <net/net_namespace.h> 6 + #include <net/sock.h> 7 + 8 + #include "internal.h" 9 + 10 + static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, struct packet_diag_req *req, 11 + u32 pid, u32 seq, u32 flags, int sk_ino) 12 + { 13 + struct nlmsghdr *nlh; 14 + struct packet_diag_msg *rp; 15 + const struct packet_sock *po = pkt_sk(sk); 16 + 17 + nlh = nlmsg_put(skb, pid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rp), flags); 18 + if (!nlh) 19 + return -EMSGSIZE; 20 + 21 + rp = nlmsg_data(nlh); 22 + rp->pdiag_family = AF_PACKET; 23 + rp->pdiag_type = sk->sk_type; 24 + rp->pdiag_num = ntohs(po->num); 25 + rp->pdiag_ino = sk_ino; 26 + sock_diag_save_cookie(sk, rp->pdiag_cookie); 27 + 28 + return nlmsg_end(skb, nlh); 29 + } 30 + 31 + static int packet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) 32 + { 33 + int num = 0, s_num = cb->args[0]; 34 + struct packet_diag_req *req; 35 + struct net *net; 36 + struct sock *sk; 37 + struct hlist_node *node; 38 + 39 + net = sock_net(skb->sk); 40 + req = nlmsg_data(cb->nlh); 41 + 42 + rcu_read_lock(); 43 + sk_for_each_rcu(sk, node, &net->packet.sklist) { 44 + if (!net_eq(sock_net(sk), net)) 45 + continue; 46 + if (num < s_num) 47 + goto next; 48 + 49 + if (sk_diag_fill(sk, skb, req, NETLINK_CB(cb->skb).pid, 50 + cb->nlh->nlmsg_seq, NLM_F_MULTI, 51 + sock_i_ino(sk)) < 0) 52 + goto done; 53 + next: 54 + num++; 55 + } 56 + done: 57 + rcu_read_unlock(); 58 + cb->args[0] = num; 59 + 60 + return skb->len; 61 + } 62 + 63 + static int packet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h) 64 + { 65 + int hdrlen = sizeof(struct packet_diag_req); 66 + struct net *net = sock_net(skb->sk); 67 + struct packet_diag_req *req; 68 + 69 + if (nlmsg_len(h) < hdrlen) 70 + return -EINVAL; 71 + 72 + req = nlmsg_data(h); 73 + /* Make it possible to support protocol filtering later */ 74 + if (req->sdiag_protocol) 75 + return -EINVAL; 76 + 77 + if (h->nlmsg_flags & NLM_F_DUMP) { 78 + struct netlink_dump_control c = { 79 + .dump = packet_diag_dump, 80 + }; 81 + return netlink_dump_start(net->diag_nlsk, skb, h, &c); 82 + } else 83 + return -EOPNOTSUPP; 84 + } 85 + 86 + static const struct sock_diag_handler packet_diag_handler = { 87 + .family = AF_PACKET, 88 + .dump = packet_diag_handler_dump, 89 + }; 90 + 91 + static int __init packet_diag_init(void) 92 + { 93 + return sock_diag_register(&packet_diag_handler); 94 + } 95 + 96 + static void __exit packet_diag_exit(void) 97 + { 98 + sock_diag_unregister(&packet_diag_handler); 99 + } 100 + 101 + module_init(packet_diag_init); 102 + module_exit(packet_diag_exit); 103 + MODULE_LICENSE("GPL"); 104 + MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 17 /* AF_PACKET */);