1 #include <linux/module.h>
2 #include <linux/sock_diag.h>
4 #include <linux/netdevice.h>
5 #include <linux/packet_diag.h>
6 #include <net/net_namespace.h>
11 static int pdiag_put_info(const struct packet_sock *po, struct sk_buff *nlskb)
13 struct packet_diag_info pinfo;
15 pinfo.pdi_index = po->ifindex;
16 pinfo.pdi_version = po->tp_version;
17 pinfo.pdi_reserve = po->tp_reserve;
18 pinfo.pdi_copy_thresh = po->copy_thresh;
19 pinfo.pdi_tstamp = po->tp_tstamp;
23 pinfo.pdi_flags |= PDI_RUNNING;
25 pinfo.pdi_flags |= PDI_AUXDATA;
27 pinfo.pdi_flags |= PDI_ORIGDEV;
29 pinfo.pdi_flags |= PDI_VNETHDR;
31 pinfo.pdi_flags |= PDI_LOSS;
33 return nla_put(nlskb, PACKET_DIAG_INFO, sizeof(pinfo), &pinfo);
36 static int pdiag_put_mclist(const struct packet_sock *po, struct sk_buff *nlskb)
39 struct packet_mclist *ml;
41 mca = nla_nest_start(nlskb, PACKET_DIAG_MCLIST);
46 for (ml = po->mclist; ml; ml = ml->next) {
47 struct packet_diag_mclist *dml;
49 dml = nla_reserve_nohdr(nlskb, sizeof(*dml));
52 nla_nest_cancel(nlskb, mca);
56 dml->pdmc_index = ml->ifindex;
57 dml->pdmc_type = ml->type;
58 dml->pdmc_alen = ml->alen;
59 dml->pdmc_count = ml->count;
60 BUILD_BUG_ON(sizeof(dml->pdmc_addr) != sizeof(ml->addr));
61 memcpy(dml->pdmc_addr, ml->addr, sizeof(ml->addr));
65 nla_nest_end(nlskb, mca);
70 static int pdiag_put_ring(struct packet_ring_buffer *ring, int ver, int nl_type,
71 struct sk_buff *nlskb)
73 struct packet_diag_ring pdr;
75 if (!ring->pg_vec || ((ver > TPACKET_V2) &&
76 (nl_type == PACKET_DIAG_TX_RING)))
79 pdr.pdr_block_size = ring->pg_vec_pages << PAGE_SHIFT;
80 pdr.pdr_block_nr = ring->pg_vec_len;
81 pdr.pdr_frame_size = ring->frame_size;
82 pdr.pdr_frame_nr = ring->frame_max + 1;
84 if (ver > TPACKET_V2) {
85 pdr.pdr_retire_tmo = ring->prb_bdqc.retire_blk_tov;
86 pdr.pdr_sizeof_priv = ring->prb_bdqc.blk_sizeof_priv;
87 pdr.pdr_features = ring->prb_bdqc.feature_req_word;
89 pdr.pdr_retire_tmo = 0;
90 pdr.pdr_sizeof_priv = 0;
94 return nla_put(nlskb, nl_type, sizeof(pdr), &pdr);
97 static int pdiag_put_rings_cfg(struct packet_sock *po, struct sk_buff *skb)
101 mutex_lock(&po->pg_vec_lock);
102 ret = pdiag_put_ring(&po->rx_ring, po->tp_version,
103 PACKET_DIAG_RX_RING, skb);
105 ret = pdiag_put_ring(&po->tx_ring, po->tp_version,
106 PACKET_DIAG_TX_RING, skb);
107 mutex_unlock(&po->pg_vec_lock);
112 static int pdiag_put_fanout(struct packet_sock *po, struct sk_buff *nlskb)
116 mutex_lock(&fanout_mutex);
120 val = (u32)po->fanout->id | ((u32)po->fanout->type << 16);
121 ret = nla_put_u32(nlskb, PACKET_DIAG_FANOUT, val);
123 mutex_unlock(&fanout_mutex);
128 static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, struct packet_diag_req *req,
129 u32 portid, u32 seq, u32 flags, int sk_ino)
131 struct nlmsghdr *nlh;
132 struct packet_diag_msg *rp;
133 struct packet_sock *po = pkt_sk(sk);
135 nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rp), flags);
139 rp = nlmsg_data(nlh);
140 rp->pdiag_family = AF_PACKET;
141 rp->pdiag_type = sk->sk_type;
142 rp->pdiag_num = ntohs(po->num);
143 rp->pdiag_ino = sk_ino;
144 sock_diag_save_cookie(sk, rp->pdiag_cookie);
146 if ((req->pdiag_show & PACKET_SHOW_INFO) &&
147 pdiag_put_info(po, skb))
150 if ((req->pdiag_show & PACKET_SHOW_MCLIST) &&
151 pdiag_put_mclist(po, skb))
154 if ((req->pdiag_show & PACKET_SHOW_RING_CFG) &&
155 pdiag_put_rings_cfg(po, skb))
158 if ((req->pdiag_show & PACKET_SHOW_FANOUT) &&
159 pdiag_put_fanout(po, skb))
162 return nlmsg_end(skb, nlh);
165 nlmsg_cancel(skb, nlh);
169 static int packet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
171 int num = 0, s_num = cb->args[0];
172 struct packet_diag_req *req;
175 struct hlist_node *node;
177 net = sock_net(skb->sk);
178 req = nlmsg_data(cb->nlh);
180 mutex_lock(&net->packet.sklist_lock);
181 sk_for_each(sk, node, &net->packet.sklist) {
182 if (!net_eq(sock_net(sk), net))
187 if (sk_diag_fill(sk, skb, req, NETLINK_CB(cb->skb).portid,
188 cb->nlh->nlmsg_seq, NLM_F_MULTI,
195 mutex_unlock(&net->packet.sklist_lock);
201 static int packet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
203 int hdrlen = sizeof(struct packet_diag_req);
204 struct net *net = sock_net(skb->sk);
205 struct packet_diag_req *req;
207 if (nlmsg_len(h) < hdrlen)
211 /* Make it possible to support protocol filtering later */
212 if (req->sdiag_protocol)
215 if (h->nlmsg_flags & NLM_F_DUMP) {
216 struct netlink_dump_control c = {
217 .dump = packet_diag_dump,
219 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
224 static const struct sock_diag_handler packet_diag_handler = {
226 .dump = packet_diag_handler_dump,
229 static int __init packet_diag_init(void)
231 return sock_diag_register(&packet_diag_handler);
234 static void __exit packet_diag_exit(void)
236 sock_diag_unregister(&packet_diag_handler);
239 module_init(packet_diag_init);
240 module_exit(packet_diag_exit);
241 MODULE_LICENSE("GPL");
242 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 17 /* AF_PACKET */);