2 * (C) 2015 Red Hat GmbH
3 * Author: Florian Westphal <fw@strlen.de>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/module.h>
11 #include <linux/static_key.h>
12 #include <linux/hash.h>
13 #include <linux/jhash.h>
14 #include <linux/if_vlan.h>
15 #include <linux/init.h>
16 #include <linux/skbuff.h>
17 #include <linux/netlink.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter/nfnetlink.h>
20 #include <linux/netfilter/nf_tables.h>
21 #include <net/netfilter/nf_tables_core.h>
22 #include <net/netfilter/nf_tables.h>
24 #define NFT_TRACETYPE_LL_HSIZE 20
25 #define NFT_TRACETYPE_NETWORK_HSIZE 40
26 #define NFT_TRACETYPE_TRANSPORT_HSIZE 20
28 DEFINE_STATIC_KEY_FALSE(nft_trace_enabled
);
29 EXPORT_SYMBOL_GPL(nft_trace_enabled
);
31 static int trace_fill_id(struct sk_buff
*nlskb
, struct sk_buff
*skb
)
35 /* using skb address as ID results in a limited number of
36 * values (and quick reuse).
38 * So we attempt to use as many skb members that will not
39 * change while skb is with netfilter.
41 id
= (__be32
)jhash_2words(hash32_ptr(skb
), skb_get_hash(skb
),
44 return nla_put_be32(nlskb
, NFTA_TRACE_ID
, id
);
47 static int trace_fill_header(struct sk_buff
*nlskb
, u16 type
,
48 const struct sk_buff
*skb
,
49 int off
, unsigned int len
)
56 nla
= nla_reserve(nlskb
, type
, len
);
57 if (!nla
|| skb_copy_bits(skb
, off
, nla_data(nla
), len
))
63 static int nf_trace_fill_ll_header(struct sk_buff
*nlskb
,
64 const struct sk_buff
*skb
)
66 struct vlan_ethhdr veth
;
69 BUILD_BUG_ON(sizeof(veth
) > NFT_TRACETYPE_LL_HSIZE
);
71 off
= skb_mac_header(skb
) - skb
->data
;
75 if (skb_copy_bits(skb
, off
, &veth
, ETH_HLEN
))
78 veth
.h_vlan_proto
= skb
->vlan_proto
;
79 veth
.h_vlan_TCI
= htons(skb_vlan_tag_get(skb
));
80 veth
.h_vlan_encapsulated_proto
= skb
->protocol
;
82 return nla_put(nlskb
, NFTA_TRACE_LL_HEADER
, sizeof(veth
), &veth
);
85 static int nf_trace_fill_dev_info(struct sk_buff
*nlskb
,
86 const struct net_device
*indev
,
87 const struct net_device
*outdev
)
90 if (nla_put_be32(nlskb
, NFTA_TRACE_IIF
,
91 htonl(indev
->ifindex
)))
94 if (nla_put_be16(nlskb
, NFTA_TRACE_IIFTYPE
,
100 if (nla_put_be32(nlskb
, NFTA_TRACE_OIF
,
101 htonl(outdev
->ifindex
)))
104 if (nla_put_be16(nlskb
, NFTA_TRACE_OIFTYPE
,
105 htons(outdev
->type
)))
112 static int nf_trace_fill_pkt_info(struct sk_buff
*nlskb
,
113 const struct nft_pktinfo
*pkt
)
115 const struct sk_buff
*skb
= pkt
->skb
;
116 unsigned int len
= min_t(unsigned int,
117 pkt
->xt
.thoff
- skb_network_offset(skb
),
118 NFT_TRACETYPE_NETWORK_HSIZE
);
119 int off
= skb_network_offset(skb
);
121 if (trace_fill_header(nlskb
, NFTA_TRACE_NETWORK_HEADER
, skb
, off
, len
))
124 len
= min_t(unsigned int, skb
->len
- pkt
->xt
.thoff
,
125 NFT_TRACETYPE_TRANSPORT_HSIZE
);
127 if (trace_fill_header(nlskb
, NFTA_TRACE_TRANSPORT_HEADER
, skb
,
131 if (!skb_mac_header_was_set(skb
))
134 if (skb_vlan_tag_get(skb
))
135 return nf_trace_fill_ll_header(nlskb
, skb
);
137 off
= skb_mac_header(skb
) - skb
->data
;
138 len
= min_t(unsigned int, -off
, NFT_TRACETYPE_LL_HSIZE
);
139 return trace_fill_header(nlskb
, NFTA_TRACE_LL_HEADER
,
143 static int nf_trace_fill_rule_info(struct sk_buff
*nlskb
,
144 const struct nft_traceinfo
*info
)
149 /* a continue verdict with ->type == RETURN means that this is
150 * an implicit return (end of chain reached).
152 * Since no rule matched, the ->rule pointer is invalid.
154 if (info
->type
== NFT_TRACETYPE_RETURN
&&
155 info
->verdict
->code
== NFT_CONTINUE
)
158 return nla_put_be64(nlskb
, NFTA_TRACE_RULE_HANDLE
,
159 cpu_to_be64(info
->rule
->handle
),
163 void nft_trace_notify(struct nft_traceinfo
*info
)
165 const struct nft_pktinfo
*pkt
= info
->pkt
;
166 struct nfgenmsg
*nfmsg
;
167 struct nlmsghdr
*nlh
;
170 int event
= (NFNL_SUBSYS_NFTABLES
<< 8) | NFT_MSG_TRACE
;
172 if (!nfnetlink_has_listeners(pkt
->net
, NFNLGRP_NFTRACE
))
175 size
= nlmsg_total_size(sizeof(struct nfgenmsg
)) +
176 nla_total_size(NFT_TABLE_MAXNAMELEN
) +
177 nla_total_size(NFT_CHAIN_MAXNAMELEN
) +
178 nla_total_size_64bit(sizeof(__be64
)) + /* rule handle */
179 nla_total_size(sizeof(__be32
)) + /* trace type */
180 nla_total_size(0) + /* VERDICT, nested */
181 nla_total_size(sizeof(u32
)) + /* verdict code */
182 nla_total_size(NFT_CHAIN_MAXNAMELEN
) + /* jump target */
183 nla_total_size(sizeof(u32
)) + /* id */
184 nla_total_size(NFT_TRACETYPE_LL_HSIZE
) +
185 nla_total_size(NFT_TRACETYPE_NETWORK_HSIZE
) +
186 nla_total_size(NFT_TRACETYPE_TRANSPORT_HSIZE
) +
187 nla_total_size(sizeof(u32
)) + /* iif */
188 nla_total_size(sizeof(__be16
)) + /* iiftype */
189 nla_total_size(sizeof(u32
)) + /* oif */
190 nla_total_size(sizeof(__be16
)) + /* oiftype */
191 nla_total_size(sizeof(u32
)) + /* mark */
192 nla_total_size(sizeof(u32
)) + /* nfproto */
193 nla_total_size(sizeof(u32
)); /* policy */
195 skb
= nlmsg_new(size
, GFP_ATOMIC
);
199 nlh
= nlmsg_put(skb
, 0, 0, event
, sizeof(struct nfgenmsg
), 0);
201 goto nla_put_failure
;
203 nfmsg
= nlmsg_data(nlh
);
204 nfmsg
->nfgen_family
= info
->basechain
->type
->family
;
205 nfmsg
->version
= NFNETLINK_V0
;
208 if (nla_put_be32(skb
, NFTA_TRACE_NFPROTO
, htonl(pkt
->pf
)))
209 goto nla_put_failure
;
211 if (nla_put_be32(skb
, NFTA_TRACE_TYPE
, htonl(info
->type
)))
212 goto nla_put_failure
;
214 if (trace_fill_id(skb
, pkt
->skb
))
215 goto nla_put_failure
;
218 if (nla_put_string(skb
, NFTA_TRACE_CHAIN
,
220 goto nla_put_failure
;
221 if (nla_put_string(skb
, NFTA_TRACE_TABLE
,
222 info
->chain
->table
->name
))
223 goto nla_put_failure
;
226 if (nf_trace_fill_rule_info(skb
, info
))
227 goto nla_put_failure
;
229 switch (info
->type
) {
230 case NFT_TRACETYPE_UNSPEC
:
231 case __NFT_TRACETYPE_MAX
:
233 case NFT_TRACETYPE_RETURN
:
234 case NFT_TRACETYPE_RULE
:
235 if (nft_verdict_dump(skb
, NFTA_TRACE_VERDICT
, info
->verdict
))
236 goto nla_put_failure
;
238 case NFT_TRACETYPE_POLICY
:
239 if (nla_put_be32(skb
, NFTA_TRACE_POLICY
,
240 info
->basechain
->policy
))
241 goto nla_put_failure
;
245 if (pkt
->skb
->mark
&&
246 nla_put_be32(skb
, NFTA_TRACE_MARK
, htonl(pkt
->skb
->mark
)))
247 goto nla_put_failure
;
249 if (!info
->packet_dumped
) {
250 if (nf_trace_fill_dev_info(skb
, pkt
->in
, pkt
->out
))
251 goto nla_put_failure
;
253 if (nf_trace_fill_pkt_info(skb
, pkt
))
254 goto nla_put_failure
;
255 info
->packet_dumped
= true;
259 nfnetlink_send(skb
, pkt
->net
, 0, NFNLGRP_NFTRACE
, 0, GFP_ATOMIC
);
267 void nft_trace_init(struct nft_traceinfo
*info
, const struct nft_pktinfo
*pkt
,
268 const struct nft_verdict
*verdict
,
269 const struct nft_chain
*chain
)
271 info
->basechain
= nft_base_chain(chain
);
273 info
->packet_dumped
= false;
275 info
->verdict
= verdict
;