]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - net/netfilter/nft_reject.c
Merge tag 'kbuild-fixes-v5.10-2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-hirsute-kernel.git] / net / netfilter / nft_reject.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
96518518 2/*
ef1f7df9 3 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
bee11dc7 4 * Copyright (c) 2013 Eric Leblond <eric@regit.org>
96518518 5 *
96518518
PM
6 * Development of this code funded by Astaro AG (http://www.astaro.com/)
7 */
8
9#include <linux/kernel.h>
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/netlink.h>
13#include <linux/netfilter.h>
14#include <linux/netfilter/nf_tables.h>
15#include <net/netfilter/nf_tables.h>
cc4723ca 16#include <net/netfilter/nft_reject.h>
51b0a5d8
PNA
17#include <linux/icmp.h>
18#include <linux/icmpv6.h>
bee11dc7 19
cc4723ca 20const struct nla_policy nft_reject_policy[NFTA_REJECT_MAX + 1] = {
96518518
PM
21 [NFTA_REJECT_TYPE] = { .type = NLA_U32 },
22 [NFTA_REJECT_ICMP_CODE] = { .type = NLA_U8 },
23};
cc4723ca 24EXPORT_SYMBOL_GPL(nft_reject_policy);
96518518 25
89e1f6d2
LZ
26int nft_reject_validate(const struct nft_ctx *ctx,
27 const struct nft_expr *expr,
28 const struct nft_data **data)
29{
30 return nft_chain_validate_hooks(ctx->chain,
31 (1 << NF_INET_LOCAL_IN) |
32 (1 << NF_INET_FORWARD) |
f53b9b0b
LGL
33 (1 << NF_INET_LOCAL_OUT) |
34 (1 << NF_INET_PRE_ROUTING));
89e1f6d2
LZ
35}
36EXPORT_SYMBOL_GPL(nft_reject_validate);
37
cc4723ca
PM
38int nft_reject_init(const struct nft_ctx *ctx,
39 const struct nft_expr *expr,
40 const struct nlattr * const tb[])
96518518
PM
41{
42 struct nft_reject *priv = nft_expr_priv(expr);
43
44 if (tb[NFTA_REJECT_TYPE] == NULL)
45 return -EINVAL;
46
47 priv->type = ntohl(nla_get_be32(tb[NFTA_REJECT_TYPE]));
48 switch (priv->type) {
49 case NFT_REJECT_ICMP_UNREACH:
50 if (tb[NFTA_REJECT_ICMP_CODE] == NULL)
51 return -EINVAL;
52 priv->icmp_code = nla_get_u8(tb[NFTA_REJECT_ICMP_CODE]);
53 case NFT_REJECT_TCP_RST:
54 break;
55 default:
56 return -EINVAL;
57 }
58
59 return 0;
60}
cc4723ca 61EXPORT_SYMBOL_GPL(nft_reject_init);
96518518 62
cc4723ca 63int nft_reject_dump(struct sk_buff *skb, const struct nft_expr *expr)
96518518
PM
64{
65 const struct nft_reject *priv = nft_expr_priv(expr);
66
bee11dc7 67 if (nla_put_be32(skb, NFTA_REJECT_TYPE, htonl(priv->type)))
96518518
PM
68 goto nla_put_failure;
69
70 switch (priv->type) {
71 case NFT_REJECT_ICMP_UNREACH:
72 if (nla_put_u8(skb, NFTA_REJECT_ICMP_CODE, priv->icmp_code))
73 goto nla_put_failure;
74 break;
129d23a5
DM
75 default:
76 break;
96518518
PM
77 }
78
79 return 0;
80
81nla_put_failure:
82 return -1;
83}
cc4723ca 84EXPORT_SYMBOL_GPL(nft_reject_dump);
96518518 85
f0d1f04f 86static u8 icmp_code_v4[NFT_REJECT_ICMPX_MAX + 1] = {
51b0a5d8
PNA
87 [NFT_REJECT_ICMPX_NO_ROUTE] = ICMP_NET_UNREACH,
88 [NFT_REJECT_ICMPX_PORT_UNREACH] = ICMP_PORT_UNREACH,
89 [NFT_REJECT_ICMPX_HOST_UNREACH] = ICMP_HOST_UNREACH,
90 [NFT_REJECT_ICMPX_ADMIN_PROHIBITED] = ICMP_PKT_FILTERED,
91};
92
93int nft_reject_icmp_code(u8 code)
94{
fa5950e4
FW
95 if (WARN_ON_ONCE(code > NFT_REJECT_ICMPX_MAX))
96 return ICMP_NET_UNREACH;
51b0a5d8
PNA
97
98 return icmp_code_v4[code];
99}
100
101EXPORT_SYMBOL_GPL(nft_reject_icmp_code);
102
103
f0d1f04f 104static u8 icmp_code_v6[NFT_REJECT_ICMPX_MAX + 1] = {
51b0a5d8
PNA
105 [NFT_REJECT_ICMPX_NO_ROUTE] = ICMPV6_NOROUTE,
106 [NFT_REJECT_ICMPX_PORT_UNREACH] = ICMPV6_PORT_UNREACH,
107 [NFT_REJECT_ICMPX_HOST_UNREACH] = ICMPV6_ADDR_UNREACH,
108 [NFT_REJECT_ICMPX_ADMIN_PROHIBITED] = ICMPV6_ADM_PROHIBITED,
109};
110
111int nft_reject_icmpv6_code(u8 code)
112{
fa5950e4
FW
113 if (WARN_ON_ONCE(code > NFT_REJECT_ICMPX_MAX))
114 return ICMPV6_NOROUTE;
51b0a5d8
PNA
115
116 return icmp_code_v6[code];
117}
118
119EXPORT_SYMBOL_GPL(nft_reject_icmpv6_code);
120
96518518
PM
121MODULE_LICENSE("GPL");
122MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
4cacc395 123MODULE_DESCRIPTION("Netfilter x_tables over nftables module");