1 // SPDX-License-Identifier: GPL-2.0-only
4 * Generic part shared by ipv4 and ipv6 backends.
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/netlink.h>
11 #include <linux/netfilter.h>
12 #include <linux/netfilter/nf_tables.h>
13 #include <net/netfilter/nf_tables_core.h>
14 #include <net/netfilter/nf_tables.h>
18 static const struct nla_policy nft_xfrm_policy
[NFTA_XFRM_MAX
+ 1] = {
19 [NFTA_XFRM_KEY
] = NLA_POLICY_MAX(NLA_BE32
, 255),
20 [NFTA_XFRM_DIR
] = { .type
= NLA_U8
},
21 [NFTA_XFRM_SPNUM
] = NLA_POLICY_MAX(NLA_BE32
, 255),
22 [NFTA_XFRM_DREG
] = { .type
= NLA_U32
},
26 enum nft_xfrm_keys key
:8;
33 static int nft_xfrm_get_init(const struct nft_ctx
*ctx
,
34 const struct nft_expr
*expr
,
35 const struct nlattr
* const tb
[])
37 struct nft_xfrm
*priv
= nft_expr_priv(expr
);
42 if (!tb
[NFTA_XFRM_KEY
] || !tb
[NFTA_XFRM_DIR
] || !tb
[NFTA_XFRM_DREG
])
45 switch (ctx
->family
) {
54 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_XFRM_KEY
]));
56 case NFT_XFRM_KEY_REQID
:
57 case NFT_XFRM_KEY_SPI
:
60 case NFT_XFRM_KEY_DADDR_IP4
:
61 case NFT_XFRM_KEY_SADDR_IP4
:
62 len
= sizeof(struct in_addr
);
64 case NFT_XFRM_KEY_DADDR_IP6
:
65 case NFT_XFRM_KEY_SADDR_IP6
:
66 len
= sizeof(struct in6_addr
);
72 dir
= nla_get_u8(tb
[NFTA_XFRM_DIR
]);
82 if (tb
[NFTA_XFRM_SPNUM
])
83 spnum
= ntohl(nla_get_be32(tb
[NFTA_XFRM_SPNUM
]));
85 if (spnum
>= XFRM_MAX_DEPTH
)
91 return nft_parse_register_store(ctx
, tb
[NFTA_XFRM_DREG
], &priv
->dreg
,
92 NULL
, NFT_DATA_VALUE
, len
);
95 /* Return true if key asks for daddr/saddr and current
96 * state does have a valid address (BEET, TUNNEL).
98 static bool xfrm_state_addr_ok(enum nft_xfrm_keys k
, u8 family
, u8 mode
)
101 case NFT_XFRM_KEY_DADDR_IP4
:
102 case NFT_XFRM_KEY_SADDR_IP4
:
103 if (family
== NFPROTO_IPV4
)
106 case NFT_XFRM_KEY_DADDR_IP6
:
107 case NFT_XFRM_KEY_SADDR_IP6
:
108 if (family
== NFPROTO_IPV6
)
115 return mode
== XFRM_MODE_BEET
|| mode
== XFRM_MODE_TUNNEL
;
118 static void nft_xfrm_state_get_key(const struct nft_xfrm
*priv
,
119 struct nft_regs
*regs
,
120 const struct xfrm_state
*state
)
122 u32
*dest
= ®s
->data
[priv
->dreg
];
124 if (!xfrm_state_addr_ok(priv
->key
,
126 state
->props
.mode
)) {
127 regs
->verdict
.code
= NFT_BREAK
;
132 case NFT_XFRM_KEY_UNSPEC
:
133 case __NFT_XFRM_KEY_MAX
:
136 case NFT_XFRM_KEY_DADDR_IP4
:
137 *dest
= (__force __u32
)state
->id
.daddr
.a4
;
139 case NFT_XFRM_KEY_DADDR_IP6
:
140 memcpy(dest
, &state
->id
.daddr
.in6
, sizeof(struct in6_addr
));
142 case NFT_XFRM_KEY_SADDR_IP4
:
143 *dest
= (__force __u32
)state
->props
.saddr
.a4
;
145 case NFT_XFRM_KEY_SADDR_IP6
:
146 memcpy(dest
, &state
->props
.saddr
.in6
, sizeof(struct in6_addr
));
148 case NFT_XFRM_KEY_REQID
:
149 *dest
= state
->props
.reqid
;
151 case NFT_XFRM_KEY_SPI
:
152 *dest
= (__force __u32
)state
->id
.spi
;
156 regs
->verdict
.code
= NFT_BREAK
;
159 static void nft_xfrm_get_eval_in(const struct nft_xfrm
*priv
,
160 struct nft_regs
*regs
,
161 const struct nft_pktinfo
*pkt
)
163 const struct sec_path
*sp
= skb_sec_path(pkt
->skb
);
164 const struct xfrm_state
*state
;
166 if (sp
== NULL
|| sp
->len
<= priv
->spnum
) {
167 regs
->verdict
.code
= NFT_BREAK
;
171 state
= sp
->xvec
[priv
->spnum
];
172 nft_xfrm_state_get_key(priv
, regs
, state
);
175 static void nft_xfrm_get_eval_out(const struct nft_xfrm
*priv
,
176 struct nft_regs
*regs
,
177 const struct nft_pktinfo
*pkt
)
179 const struct dst_entry
*dst
= skb_dst(pkt
->skb
);
182 for (i
= 0; dst
&& dst
->xfrm
;
183 dst
= ((const struct xfrm_dst
*)dst
)->child
, i
++) {
187 nft_xfrm_state_get_key(priv
, regs
, dst
->xfrm
);
191 regs
->verdict
.code
= NFT_BREAK
;
194 static void nft_xfrm_get_eval(const struct nft_expr
*expr
,
195 struct nft_regs
*regs
,
196 const struct nft_pktinfo
*pkt
)
198 const struct nft_xfrm
*priv
= nft_expr_priv(expr
);
202 nft_xfrm_get_eval_in(priv
, regs
, pkt
);
204 case XFRM_POLICY_OUT
:
205 nft_xfrm_get_eval_out(priv
, regs
, pkt
);
209 regs
->verdict
.code
= NFT_BREAK
;
214 static int nft_xfrm_get_dump(struct sk_buff
*skb
,
215 const struct nft_expr
*expr
, bool reset
)
217 const struct nft_xfrm
*priv
= nft_expr_priv(expr
);
219 if (nft_dump_register(skb
, NFTA_XFRM_DREG
, priv
->dreg
))
222 if (nla_put_be32(skb
, NFTA_XFRM_KEY
, htonl(priv
->key
)))
224 if (nla_put_u8(skb
, NFTA_XFRM_DIR
, priv
->dir
))
226 if (nla_put_be32(skb
, NFTA_XFRM_SPNUM
, htonl(priv
->spnum
)))
232 static int nft_xfrm_validate(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
)
234 const struct nft_xfrm
*priv
= nft_expr_priv(expr
);
237 if (ctx
->family
!= NFPROTO_IPV4
&&
238 ctx
->family
!= NFPROTO_IPV6
&&
239 ctx
->family
!= NFPROTO_INET
)
244 hooks
= (1 << NF_INET_FORWARD
) |
245 (1 << NF_INET_LOCAL_IN
) |
246 (1 << NF_INET_PRE_ROUTING
);
248 case XFRM_POLICY_OUT
:
249 hooks
= (1 << NF_INET_FORWARD
) |
250 (1 << NF_INET_LOCAL_OUT
) |
251 (1 << NF_INET_POST_ROUTING
);
258 return nft_chain_validate_hooks(ctx
->chain
, hooks
);
261 static bool nft_xfrm_reduce(struct nft_regs_track
*track
,
262 const struct nft_expr
*expr
)
264 const struct nft_xfrm
*priv
= nft_expr_priv(expr
);
265 const struct nft_xfrm
*xfrm
;
267 if (!nft_reg_track_cmp(track
, expr
, priv
->dreg
)) {
268 nft_reg_track_update(track
, expr
, priv
->dreg
, priv
->len
);
272 xfrm
= nft_expr_priv(track
->regs
[priv
->dreg
].selector
);
273 if (priv
->key
!= xfrm
->key
||
274 priv
->dreg
!= xfrm
->dreg
||
275 priv
->dir
!= xfrm
->dir
||
276 priv
->spnum
!= xfrm
->spnum
) {
277 nft_reg_track_update(track
, expr
, priv
->dreg
, priv
->len
);
281 if (!track
->regs
[priv
->dreg
].bitwise
)
284 return nft_expr_reduce_bitwise(track
, expr
);
287 static struct nft_expr_type nft_xfrm_type
;
288 static const struct nft_expr_ops nft_xfrm_get_ops
= {
289 .type
= &nft_xfrm_type
,
290 .size
= NFT_EXPR_SIZE(sizeof(struct nft_xfrm
)),
291 .eval
= nft_xfrm_get_eval
,
292 .init
= nft_xfrm_get_init
,
293 .dump
= nft_xfrm_get_dump
,
294 .validate
= nft_xfrm_validate
,
295 .reduce
= nft_xfrm_reduce
,
298 static struct nft_expr_type nft_xfrm_type __read_mostly
= {
300 .ops
= &nft_xfrm_get_ops
,
301 .policy
= nft_xfrm_policy
,
302 .maxattr
= NFTA_XFRM_MAX
,
303 .owner
= THIS_MODULE
,
306 static int __init
nft_xfrm_module_init(void)
308 return nft_register_expr(&nft_xfrm_type
);
311 static void __exit
nft_xfrm_module_exit(void)
313 nft_unregister_expr(&nft_xfrm_type
);
316 module_init(nft_xfrm_module_init
);
317 module_exit(nft_xfrm_module_exit
);
319 MODULE_LICENSE("GPL");
320 MODULE_DESCRIPTION("nf_tables: xfrm/IPSec matching");
321 MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
322 MODULE_AUTHOR("Máté Eckl <ecklm94@gmail.com>");
323 MODULE_ALIAS_NFT_EXPR("xfrm");