1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016 Anders K. Pedersen <akp@cohaesio.com>
6 #include <linux/kernel.h>
7 #include <linux/netlink.h>
8 #include <linux/netfilter.h>
9 #include <linux/netfilter/nf_tables.h>
11 #include <net/ip6_route.h>
12 #include <net/route.h>
13 #include <net/netfilter/nf_tables.h>
14 #include <net/netfilter/nf_tables_core.h>
17 enum nft_rt_keys key
:8;
21 static u16
get_tcpmss(const struct nft_pktinfo
*pkt
, const struct dst_entry
*skbdst
)
23 u32 minlen
= sizeof(struct ipv6hdr
), mtu
= dst_mtu(skbdst
);
24 const struct sk_buff
*skb
= pkt
->skb
;
25 struct dst_entry
*dst
= NULL
;
28 memset(&fl
, 0, sizeof(fl
));
30 switch (nft_pf(pkt
)) {
32 fl
.u
.ip4
.daddr
= ip_hdr(skb
)->saddr
;
33 minlen
= sizeof(struct iphdr
) + sizeof(struct tcphdr
);
36 fl
.u
.ip6
.daddr
= ipv6_hdr(skb
)->saddr
;
37 minlen
= sizeof(struct ipv6hdr
) + sizeof(struct tcphdr
);
41 nf_route(nft_net(pkt
), &dst
, &fl
, false, nft_pf(pkt
));
43 mtu
= min(mtu
, dst_mtu(dst
));
47 if (mtu
<= minlen
|| mtu
> 0xffff)
48 return TCP_MSS_DEFAULT
;
53 void nft_rt_get_eval(const struct nft_expr
*expr
,
54 struct nft_regs
*regs
,
55 const struct nft_pktinfo
*pkt
)
57 const struct nft_rt
*priv
= nft_expr_priv(expr
);
58 const struct sk_buff
*skb
= pkt
->skb
;
59 u32
*dest
= ®s
->data
[priv
->dreg
];
60 const struct dst_entry
*dst
;
67 #ifdef CONFIG_IP_ROUTE_CLASSID
69 *dest
= dst
->tclassid
;
73 if (nft_pf(pkt
) != NFPROTO_IPV4
)
76 *dest
= (__force u32
)rt_nexthop(dst_rtable(dst
),
80 if (nft_pf(pkt
) != NFPROTO_IPV6
)
83 memcpy(dest
, rt6_nexthop(dst_rt6_info(dst
),
84 &ipv6_hdr(skb
)->daddr
),
85 sizeof(struct in6_addr
));
88 nft_reg_store16(dest
, get_tcpmss(pkt
, dst
));
92 nft_reg_store8(dest
, !!dst
->xfrm
);
102 regs
->verdict
.code
= NFT_BREAK
;
105 static const struct nla_policy nft_rt_policy
[NFTA_RT_MAX
+ 1] = {
106 [NFTA_RT_DREG
] = { .type
= NLA_U32
},
107 [NFTA_RT_KEY
] = NLA_POLICY_MAX(NLA_BE32
, 255),
110 static int nft_rt_get_init(const struct nft_ctx
*ctx
,
111 const struct nft_expr
*expr
,
112 const struct nlattr
* const tb
[])
114 struct nft_rt
*priv
= nft_expr_priv(expr
);
117 if (tb
[NFTA_RT_KEY
] == NULL
||
118 tb
[NFTA_RT_DREG
] == NULL
)
121 priv
->key
= ntohl(nla_get_be32(tb
[NFTA_RT_KEY
]));
123 #ifdef CONFIG_IP_ROUTE_CLASSID
126 case NFT_RT_NEXTHOP4
:
129 case NFT_RT_NEXTHOP6
:
130 len
= sizeof(struct in6_addr
);
144 return nft_parse_register_store(ctx
, tb
[NFTA_RT_DREG
], &priv
->dreg
,
145 NULL
, NFT_DATA_VALUE
, len
);
148 static int nft_rt_get_dump(struct sk_buff
*skb
,
149 const struct nft_expr
*expr
, bool reset
)
151 const struct nft_rt
*priv
= nft_expr_priv(expr
);
153 if (nla_put_be32(skb
, NFTA_RT_KEY
, htonl(priv
->key
)))
154 goto nla_put_failure
;
155 if (nft_dump_register(skb
, NFTA_RT_DREG
, priv
->dreg
))
156 goto nla_put_failure
;
163 static int nft_rt_validate(const struct nft_ctx
*ctx
, const struct nft_expr
*expr
)
165 const struct nft_rt
*priv
= nft_expr_priv(expr
);
168 if (ctx
->family
!= NFPROTO_IPV4
&&
169 ctx
->family
!= NFPROTO_IPV6
&&
170 ctx
->family
!= NFPROTO_INET
)
174 case NFT_RT_NEXTHOP4
:
175 case NFT_RT_NEXTHOP6
:
180 hooks
= (1 << NF_INET_FORWARD
) |
181 (1 << NF_INET_LOCAL_OUT
) |
182 (1 << NF_INET_POST_ROUTING
);
188 return nft_chain_validate_hooks(ctx
->chain
, hooks
);
191 static const struct nft_expr_ops nft_rt_get_ops
= {
192 .type
= &nft_rt_type
,
193 .size
= NFT_EXPR_SIZE(sizeof(struct nft_rt
)),
194 .eval
= nft_rt_get_eval
,
195 .init
= nft_rt_get_init
,
196 .dump
= nft_rt_get_dump
,
197 .validate
= nft_rt_validate
,
198 .reduce
= NFT_REDUCE_READONLY
,
201 struct nft_expr_type nft_rt_type __read_mostly
= {
203 .ops
= &nft_rt_get_ops
,
204 .policy
= nft_rt_policy
,
205 .maxattr
= NFTA_RT_MAX
,
206 .owner
= THIS_MODULE
,