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> |
Pavel Emelyanov | eea68e2 | 2012-08-13 05:57:44 +0000 | [diff] [blame^] | 4 | #include <linux/netdevice.h> |
Pavel Emelyanov | 96ec632 | 2012-08-13 05:53:28 +0000 | [diff] [blame] | 5 | #include <linux/packet_diag.h> |
| 6 | #include <net/net_namespace.h> |
| 7 | #include <net/sock.h> |
| 8 | |
| 9 | #include "internal.h" |
| 10 | |
Pavel Emelyanov | 8a360be | 2012-08-13 05:55:46 +0000 | [diff] [blame] | 11 | static int pdiag_put_info(const struct packet_sock *po, struct sk_buff *nlskb) |
| 12 | { |
| 13 | struct packet_diag_info pinfo; |
| 14 | |
| 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; |
| 20 | |
| 21 | pinfo.pdi_flags = 0; |
| 22 | if (po->running) |
| 23 | pinfo.pdi_flags |= PDI_RUNNING; |
| 24 | if (po->auxdata) |
| 25 | pinfo.pdi_flags |= PDI_AUXDATA; |
| 26 | if (po->origdev) |
| 27 | pinfo.pdi_flags |= PDI_ORIGDEV; |
| 28 | if (po->has_vnet_hdr) |
| 29 | pinfo.pdi_flags |= PDI_VNETHDR; |
| 30 | if (po->tp_loss) |
| 31 | pinfo.pdi_flags |= PDI_LOSS; |
| 32 | |
| 33 | return nla_put(nlskb, PACKET_DIAG_INFO, sizeof(pinfo), &pinfo); |
| 34 | } |
| 35 | |
Pavel Emelyanov | eea68e2 | 2012-08-13 05:57:44 +0000 | [diff] [blame^] | 36 | static int pdiag_put_mclist(const struct packet_sock *po, struct sk_buff *nlskb) |
| 37 | { |
| 38 | struct nlattr *mca; |
| 39 | struct packet_mclist *ml; |
| 40 | |
| 41 | mca = nla_nest_start(nlskb, PACKET_DIAG_MCLIST); |
| 42 | if (!mca) |
| 43 | return -EMSGSIZE; |
| 44 | |
| 45 | rtnl_lock(); |
| 46 | for (ml = po->mclist; ml; ml = ml->next) { |
| 47 | struct packet_diag_mclist *dml; |
| 48 | |
| 49 | dml = nla_reserve_nohdr(nlskb, sizeof(*dml)); |
| 50 | if (!dml) { |
| 51 | rtnl_unlock(); |
| 52 | nla_nest_cancel(nlskb, mca); |
| 53 | return -EMSGSIZE; |
| 54 | } |
| 55 | |
| 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)); |
| 62 | } |
| 63 | |
| 64 | rtnl_unlock(); |
| 65 | nla_nest_end(nlskb, mca); |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
Pavel Emelyanov | 96ec632 | 2012-08-13 05:53:28 +0000 | [diff] [blame] | 70 | static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, struct packet_diag_req *req, |
| 71 | u32 pid, u32 seq, u32 flags, int sk_ino) |
| 72 | { |
| 73 | struct nlmsghdr *nlh; |
| 74 | struct packet_diag_msg *rp; |
| 75 | const struct packet_sock *po = pkt_sk(sk); |
| 76 | |
| 77 | nlh = nlmsg_put(skb, pid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rp), flags); |
| 78 | if (!nlh) |
| 79 | return -EMSGSIZE; |
| 80 | |
| 81 | rp = nlmsg_data(nlh); |
| 82 | rp->pdiag_family = AF_PACKET; |
| 83 | rp->pdiag_type = sk->sk_type; |
| 84 | rp->pdiag_num = ntohs(po->num); |
| 85 | rp->pdiag_ino = sk_ino; |
| 86 | sock_diag_save_cookie(sk, rp->pdiag_cookie); |
| 87 | |
Pavel Emelyanov | 8a360be | 2012-08-13 05:55:46 +0000 | [diff] [blame] | 88 | if ((req->pdiag_show & PACKET_SHOW_INFO) && |
| 89 | pdiag_put_info(po, skb)) |
| 90 | goto out_nlmsg_trim; |
| 91 | |
Pavel Emelyanov | eea68e2 | 2012-08-13 05:57:44 +0000 | [diff] [blame^] | 92 | if ((req->pdiag_show & PACKET_SHOW_MCLIST) && |
| 93 | pdiag_put_mclist(po, skb)) |
| 94 | goto out_nlmsg_trim; |
| 95 | |
Pavel Emelyanov | 96ec632 | 2012-08-13 05:53:28 +0000 | [diff] [blame] | 96 | return nlmsg_end(skb, nlh); |
Pavel Emelyanov | 8a360be | 2012-08-13 05:55:46 +0000 | [diff] [blame] | 97 | |
| 98 | out_nlmsg_trim: |
| 99 | nlmsg_cancel(skb, nlh); |
| 100 | return -EMSGSIZE; |
Pavel Emelyanov | 96ec632 | 2012-08-13 05:53:28 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static int packet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 104 | { |
| 105 | int num = 0, s_num = cb->args[0]; |
| 106 | struct packet_diag_req *req; |
| 107 | struct net *net; |
| 108 | struct sock *sk; |
| 109 | struct hlist_node *node; |
| 110 | |
| 111 | net = sock_net(skb->sk); |
| 112 | req = nlmsg_data(cb->nlh); |
| 113 | |
| 114 | rcu_read_lock(); |
| 115 | sk_for_each_rcu(sk, node, &net->packet.sklist) { |
| 116 | if (!net_eq(sock_net(sk), net)) |
| 117 | continue; |
| 118 | if (num < s_num) |
| 119 | goto next; |
| 120 | |
| 121 | if (sk_diag_fill(sk, skb, req, NETLINK_CB(cb->skb).pid, |
| 122 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
| 123 | sock_i_ino(sk)) < 0) |
| 124 | goto done; |
| 125 | next: |
| 126 | num++; |
| 127 | } |
| 128 | done: |
| 129 | rcu_read_unlock(); |
| 130 | cb->args[0] = num; |
| 131 | |
| 132 | return skb->len; |
| 133 | } |
| 134 | |
| 135 | static int packet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h) |
| 136 | { |
| 137 | int hdrlen = sizeof(struct packet_diag_req); |
| 138 | struct net *net = sock_net(skb->sk); |
| 139 | struct packet_diag_req *req; |
| 140 | |
| 141 | if (nlmsg_len(h) < hdrlen) |
| 142 | return -EINVAL; |
| 143 | |
| 144 | req = nlmsg_data(h); |
| 145 | /* Make it possible to support protocol filtering later */ |
| 146 | if (req->sdiag_protocol) |
| 147 | return -EINVAL; |
| 148 | |
| 149 | if (h->nlmsg_flags & NLM_F_DUMP) { |
| 150 | struct netlink_dump_control c = { |
| 151 | .dump = packet_diag_dump, |
| 152 | }; |
| 153 | return netlink_dump_start(net->diag_nlsk, skb, h, &c); |
| 154 | } else |
| 155 | return -EOPNOTSUPP; |
| 156 | } |
| 157 | |
| 158 | static const struct sock_diag_handler packet_diag_handler = { |
| 159 | .family = AF_PACKET, |
| 160 | .dump = packet_diag_handler_dump, |
| 161 | }; |
| 162 | |
| 163 | static int __init packet_diag_init(void) |
| 164 | { |
| 165 | return sock_diag_register(&packet_diag_handler); |
| 166 | } |
| 167 | |
| 168 | static void __exit packet_diag_exit(void) |
| 169 | { |
| 170 | sock_diag_unregister(&packet_diag_handler); |
| 171 | } |
| 172 | |
| 173 | module_init(packet_diag_init); |
| 174 | module_exit(packet_diag_exit); |
| 175 | MODULE_LICENSE("GPL"); |
| 176 | MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 17 /* AF_PACKET */); |