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 nlmsghdr *nlh;
191 if (!nfnetlink_has_listeners(nft_net(pkt), NFNLGRP_NFTRACE))
194 size = nlmsg_total_size(sizeof(struct nfgenmsg)) +
195 nla_total_size(strlen(info->chain->table->name)) +
196 nla_total_size(strlen(info->chain->name)) +
197 nla_total_size_64bit(sizeof(__be64)) + /* rule handle */
198 nla_total_size(sizeof(__be32)) + /* trace type */
199 nla_total_size(0) + /* VERDICT, nested */
200 nla_total_size(sizeof(u32)) + /* verdict code */
201 nla_total_size(sizeof(u32)) + /* id */
202 nla_total_size(NFT_TRACETYPE_LL_HSIZE) +
203 nla_total_size(NFT_TRACETYPE_NETWORK_HSIZE) +
204 nla_total_size(NFT_TRACETYPE_TRANSPORT_HSIZE) +
205 nla_total_size(sizeof(u32)) + /* iif */
206 nla_total_size(sizeof(__be16)) + /* iiftype */
207 nla_total_size(sizeof(u32)) + /* oif */
208 nla_total_size(sizeof(__be16)) + /* oiftype */
209 nla_total_size(sizeof(u32)) + /* mark */
210 nla_total_size(sizeof(u32)) + /* nfproto */
211 nla_total_size(sizeof(u32)); /* policy */
213 if (nft_trace_have_verdict_chain(info))
214 size += nla_total_size(strlen(info->verdict->chain->name)); /* jump target */
216 skb = nlmsg_new(size, GFP_ATOMIC);
220 event = nfnl_msg_type(NFNL_SUBSYS_NFTABLES, NFT_MSG_TRACE);
221 nlh = nfnl_msg_put(skb, 0, 0, event, 0, info->basechain->type->family,
224 goto nla_put_failure;
226 if (nla_put_be32(skb, NFTA_TRACE_NFPROTO, htonl(nft_pf(pkt))))
227 goto nla_put_failure;
229 if (nla_put_be32(skb, NFTA_TRACE_TYPE, htonl(info->type)))
230 goto nla_put_failure;
232 if (trace_fill_id(skb, pkt->skb))
233 goto nla_put_failure;
235 if (nla_put_string(skb, NFTA_TRACE_CHAIN, info->chain->name))
236 goto nla_put_failure;
238 if (nla_put_string(skb, NFTA_TRACE_TABLE, info->chain->table->name))
239 goto nla_put_failure;
241 if (nf_trace_fill_rule_info(skb, info))
242 goto nla_put_failure;
244 switch (info->type) {
245 case NFT_TRACETYPE_UNSPEC:
246 case __NFT_TRACETYPE_MAX:
248 case NFT_TRACETYPE_RETURN:
249 case NFT_TRACETYPE_RULE:
250 if (nft_verdict_dump(skb, NFTA_TRACE_VERDICT, info->verdict))
251 goto nla_put_failure;
253 case NFT_TRACETYPE_POLICY:
254 if (nla_put_be32(skb, NFTA_TRACE_POLICY,
255 htonl(info->basechain->policy)))
256 goto nla_put_failure;
260 if (pkt->skb->mark &&
261 nla_put_be32(skb, NFTA_TRACE_MARK, htonl(pkt->skb->mark)))
262 goto nla_put_failure;
264 if (!info->packet_dumped) {
265 if (nf_trace_fill_dev_info(skb, nft_in(pkt), nft_out(pkt)))
266 goto nla_put_failure;
268 if (nf_trace_fill_pkt_info(skb, pkt))
269 goto nla_put_failure;
270 info->packet_dumped = true;
274 nfnetlink_send(skb, nft_net(pkt), 0, NFNLGRP_NFTRACE, 0, GFP_ATOMIC);
282 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
283 const struct nft_verdict *verdict,
284 const struct nft_chain *chain)
286 info->basechain = nft_base_chain(chain);
288 info->packet_dumped = false;
290 info->verdict = verdict;