1 // SPDX-License-Identifier: GPL-2.0-only
3 * (C) 2015 Red Hat GmbH
4 * Author: Florian Westphal <fw@strlen.de>
7 #include <linux/module.h>
8 #include <linux/static_key.h>
9 #include <linux/hash.h>
10 #include <linux/jhash.h>
11 #include <linux/if_vlan.h>
12 #include <linux/init.h>
13 #include <linux/skbuff.h>
14 #include <linux/netlink.h>
15 #include <linux/netfilter.h>
16 #include <linux/netfilter/nfnetlink.h>
17 #include <linux/netfilter/nf_tables.h>
18 #include <net/netfilter/nf_tables_core.h>
19 #include <net/netfilter/nf_tables.h>
21 #define NFT_TRACETYPE_LL_HSIZE 20
22 #define NFT_TRACETYPE_NETWORK_HSIZE 40
23 #define NFT_TRACETYPE_TRANSPORT_HSIZE 20
25 DEFINE_STATIC_KEY_FALSE(nft_trace_enabled);
26 EXPORT_SYMBOL_GPL(nft_trace_enabled);
28 static int trace_fill_id(struct sk_buff *nlskb, struct sk_buff *skb)
32 /* using skb address as ID results in a limited number of
33 * values (and quick reuse).
35 * So we attempt to use as many skb members that will not
36 * change while skb is with netfilter.
38 id = (__be32)jhash_2words(hash32_ptr(skb), skb_get_hash(skb),
41 return nla_put_be32(nlskb, NFTA_TRACE_ID, id);
44 static int trace_fill_header(struct sk_buff *nlskb, u16 type,
45 const struct sk_buff *skb,
46 int off, unsigned int len)
53 nla = nla_reserve(nlskb, type, len);
54 if (!nla || skb_copy_bits(skb, off, nla_data(nla), len))
60 static int nf_trace_fill_ll_header(struct sk_buff *nlskb,
61 const struct sk_buff *skb)
63 struct vlan_ethhdr veth;
66 BUILD_BUG_ON(sizeof(veth) > NFT_TRACETYPE_LL_HSIZE);
68 off = skb_mac_header(skb) - skb->data;
72 if (skb_copy_bits(skb, off, &veth, ETH_HLEN))
75 veth.h_vlan_proto = skb->vlan_proto;
76 veth.h_vlan_TCI = htons(skb_vlan_tag_get(skb));
77 veth.h_vlan_encapsulated_proto = skb->protocol;
79 return nla_put(nlskb, NFTA_TRACE_LL_HEADER, sizeof(veth), &veth);
82 static int nf_trace_fill_dev_info(struct sk_buff *nlskb,
83 const struct net_device *indev,
84 const struct net_device *outdev)
87 if (nla_put_be32(nlskb, NFTA_TRACE_IIF,
88 htonl(indev->ifindex)))
91 if (nla_put_be16(nlskb, NFTA_TRACE_IIFTYPE,
97 if (nla_put_be32(nlskb, NFTA_TRACE_OIF,
98 htonl(outdev->ifindex)))
101 if (nla_put_be16(nlskb, NFTA_TRACE_OIFTYPE,
102 htons(outdev->type)))
109 static int nf_trace_fill_pkt_info(struct sk_buff *nlskb,
110 const struct nft_pktinfo *pkt)
112 const struct sk_buff *skb = pkt->skb;
113 int off = skb_network_offset(skb);
114 unsigned int len, nh_end;
116 nh_end = pkt->tprot_set ? pkt->xt.thoff : skb->len;
117 len = min_t(unsigned int, nh_end - skb_network_offset(skb),
118 NFT_TRACETYPE_NETWORK_HSIZE);
119 if (trace_fill_header(nlskb, NFTA_TRACE_NETWORK_HEADER, skb, off, len))
122 if (pkt->tprot_set) {
123 len = min_t(unsigned int, skb->len - pkt->xt.thoff,
124 NFT_TRACETYPE_TRANSPORT_HSIZE);
125 if (trace_fill_header(nlskb, NFTA_TRACE_TRANSPORT_HEADER, skb,
130 if (!skb_mac_header_was_set(skb))
133 if (skb_vlan_tag_get(skb))
134 return nf_trace_fill_ll_header(nlskb, skb);
136 off = skb_mac_header(skb) - skb->data;
137 len = min_t(unsigned int, -off, NFT_TRACETYPE_LL_HSIZE);
138 return trace_fill_header(nlskb, NFTA_TRACE_LL_HEADER,
142 static int nf_trace_fill_rule_info(struct sk_buff *nlskb,
143 const struct nft_traceinfo *info)
148 /* a continue verdict with ->type == RETURN means that this is
149 * an implicit return (end of chain reached).
151 * Since no rule matched, the ->rule pointer is invalid.
153 if (info->type == NFT_TRACETYPE_RETURN &&
154 info->verdict->code == NFT_CONTINUE)
157 return nla_put_be64(nlskb, NFTA_TRACE_RULE_HANDLE,
158 cpu_to_be64(info->rule->handle),
162 static bool nft_trace_have_verdict_chain(struct nft_traceinfo *info)
164 switch (info->type) {
165 case NFT_TRACETYPE_RETURN:
166 case NFT_TRACETYPE_RULE:
172 switch (info->verdict->code) {
183 void nft_trace_notify(struct nft_traceinfo *info)
185 const struct nft_pktinfo *pkt = info->pkt;
186 struct nfgenmsg *nfmsg;
187 struct nlmsghdr *nlh;
192 if (!nfnetlink_has_listeners(nft_net(pkt), NFNLGRP_NFTRACE))
195 size = nlmsg_total_size(sizeof(struct nfgenmsg)) +
196 nla_total_size(strlen(info->chain->table->name)) +
197 nla_total_size(strlen(info->chain->name)) +
198 nla_total_size_64bit(sizeof(__be64)) + /* rule handle */
199 nla_total_size(sizeof(__be32)) + /* trace type */
200 nla_total_size(0) + /* VERDICT, nested */
201 nla_total_size(sizeof(u32)) + /* verdict code */
202 nla_total_size(sizeof(u32)) + /* id */
203 nla_total_size(NFT_TRACETYPE_LL_HSIZE) +
204 nla_total_size(NFT_TRACETYPE_NETWORK_HSIZE) +
205 nla_total_size(NFT_TRACETYPE_TRANSPORT_HSIZE) +
206 nla_total_size(sizeof(u32)) + /* iif */
207 nla_total_size(sizeof(__be16)) + /* iiftype */
208 nla_total_size(sizeof(u32)) + /* oif */
209 nla_total_size(sizeof(__be16)) + /* oiftype */
210 nla_total_size(sizeof(u32)) + /* mark */
211 nla_total_size(sizeof(u32)) + /* nfproto */
212 nla_total_size(sizeof(u32)); /* policy */
214 if (nft_trace_have_verdict_chain(info))
215 size += nla_total_size(strlen(info->verdict->chain->name)); /* jump target */
217 skb = nlmsg_new(size, GFP_ATOMIC);
221 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_TRACE);
222 nlh = nlmsg_put(skb, 0, 0, event, sizeof(struct nfgenmsg), 0);
224 goto nla_put_failure;
226 nfmsg = nlmsg_data(nlh);
227 nfmsg->nfgen_family = info->basechain->type->family;
228 nfmsg->version = NFNETLINK_V0;
231 if (nla_put_be32(skb, NFTA_TRACE_NFPROTO, htonl(nft_pf(pkt))))
232 goto nla_put_failure;
234 if (nla_put_be32(skb, NFTA_TRACE_TYPE, htonl(info->type)))
235 goto nla_put_failure;
237 if (trace_fill_id(skb, pkt->skb))
238 goto nla_put_failure;
240 if (nla_put_string(skb, NFTA_TRACE_CHAIN, info->chain->name))
241 goto nla_put_failure;
243 if (nla_put_string(skb, NFTA_TRACE_TABLE, info->chain->table->name))
244 goto nla_put_failure;
246 if (nf_trace_fill_rule_info(skb, info))
247 goto nla_put_failure;
249 switch (info->type) {
250 case NFT_TRACETYPE_UNSPEC:
251 case __NFT_TRACETYPE_MAX:
253 case NFT_TRACETYPE_RETURN:
254 case NFT_TRACETYPE_RULE:
255 if (nft_verdict_dump(skb, NFTA_TRACE_VERDICT, info->verdict))
256 goto nla_put_failure;
258 case NFT_TRACETYPE_POLICY:
259 if (nla_put_be32(skb, NFTA_TRACE_POLICY,
260 htonl(info->basechain->policy)))
261 goto nla_put_failure;
265 if (pkt->skb->mark &&
266 nla_put_be32(skb, NFTA_TRACE_MARK, htonl(pkt->skb->mark)))
267 goto nla_put_failure;
269 if (!info->packet_dumped) {
270 if (nf_trace_fill_dev_info(skb, nft_in(pkt), nft_out(pkt)))
271 goto nla_put_failure;
273 if (nf_trace_fill_pkt_info(skb, pkt))
274 goto nla_put_failure;
275 info->packet_dumped = true;
279 nfnetlink_send(skb, nft_net(pkt), 0, NFNLGRP_NFTRACE, 0, GFP_ATOMIC);
287 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
288 const struct nft_verdict *verdict,
289 const struct nft_chain *chain)
291 info->basechain = nft_base_chain(chain);
293 info->packet_dumped = false;
295 info->verdict = verdict;