Pavel Emelyanov | 96ec632 | 2012-08-13 05:53:28 +0000 | [diff] [blame^] | 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 */); |