]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - net/netfilter/nft_reject_inet.c
UBUNTU: Ubuntu-5.3.0-29.31
[mirror_ubuntu-eoan-kernel.git] / net / netfilter / nft_reject_inet.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
05513e9e
PM
2/*
3 * Copyright (c) 2014 Patrick McHardy <kaber@trash.net>
05513e9e
PM
4 */
5
6#include <linux/kernel.h>
7#include <linux/init.h>
8#include <linux/module.h>
9#include <linux/netlink.h>
10#include <linux/netfilter.h>
11#include <linux/netfilter/nf_tables.h>
12#include <net/netfilter/nf_tables.h>
13#include <net/netfilter/nft_reject.h>
51b0a5d8
PNA
14#include <net/netfilter/ipv4/nf_reject.h>
15#include <net/netfilter/ipv6/nf_reject.h>
05513e9e
PM
16
17static void nft_reject_inet_eval(const struct nft_expr *expr,
a55e22e9 18 struct nft_regs *regs,
05513e9e
PM
19 const struct nft_pktinfo *pkt)
20{
51b0a5d8 21 struct nft_reject *priv = nft_expr_priv(expr);
51b0a5d8 22
0e5a1c7e 23 switch (nft_pf(pkt)) {
05513e9e 24 case NFPROTO_IPV4:
51b0a5d8
PNA
25 switch (priv->type) {
26 case NFT_REJECT_ICMP_UNREACH:
ee586bbc 27 nf_send_unreach(pkt->skb, priv->icmp_code,
0e5a1c7e 28 nft_hook(pkt));
51b0a5d8
PNA
29 break;
30 case NFT_REJECT_TCP_RST:
0e5a1c7e 31 nf_send_reset(nft_net(pkt), pkt->skb, nft_hook(pkt));
51b0a5d8
PNA
32 break;
33 case NFT_REJECT_ICMPX_UNREACH:
34 nf_send_unreach(pkt->skb,
ee586bbc 35 nft_reject_icmp_code(priv->icmp_code),
0e5a1c7e 36 nft_hook(pkt));
51b0a5d8
PNA
37 break;
38 }
39 break;
05513e9e 40 case NFPROTO_IPV6:
51b0a5d8
PNA
41 switch (priv->type) {
42 case NFT_REJECT_ICMP_UNREACH:
0e5a1c7e
PNA
43 nf_send_unreach6(nft_net(pkt), pkt->skb,
44 priv->icmp_code, nft_hook(pkt));
51b0a5d8
PNA
45 break;
46 case NFT_REJECT_TCP_RST:
0e5a1c7e 47 nf_send_reset6(nft_net(pkt), pkt->skb, nft_hook(pkt));
51b0a5d8
PNA
48 break;
49 case NFT_REJECT_ICMPX_UNREACH:
0e5a1c7e 50 nf_send_unreach6(nft_net(pkt), pkt->skb,
51b0a5d8 51 nft_reject_icmpv6_code(priv->icmp_code),
0e5a1c7e 52 nft_hook(pkt));
51b0a5d8
PNA
53 break;
54 }
55 break;
56 }
a55e22e9
PM
57
58 regs->verdict.code = NF_DROP;
51b0a5d8
PNA
59}
60
61static int nft_reject_inet_init(const struct nft_ctx *ctx,
62 const struct nft_expr *expr,
63 const struct nlattr * const tb[])
64{
65 struct nft_reject *priv = nft_expr_priv(expr);
c56e3956 66 int icmp_code;
51b0a5d8
PNA
67
68 if (tb[NFTA_REJECT_TYPE] == NULL)
69 return -EINVAL;
70
71 priv->type = ntohl(nla_get_be32(tb[NFTA_REJECT_TYPE]));
72 switch (priv->type) {
73 case NFT_REJECT_ICMP_UNREACH:
74 case NFT_REJECT_ICMPX_UNREACH:
75 if (tb[NFTA_REJECT_ICMP_CODE] == NULL)
76 return -EINVAL;
77
78 icmp_code = nla_get_u8(tb[NFTA_REJECT_ICMP_CODE]);
79 if (priv->type == NFT_REJECT_ICMPX_UNREACH &&
80 icmp_code > NFT_REJECT_ICMPX_MAX)
81 return -EINVAL;
82
83 priv->icmp_code = icmp_code;
84 break;
85 case NFT_REJECT_TCP_RST:
86 break;
87 default:
88 return -EINVAL;
05513e9e 89 }
51b0a5d8
PNA
90 return 0;
91}
92
93static int nft_reject_inet_dump(struct sk_buff *skb,
94 const struct nft_expr *expr)
95{
96 const struct nft_reject *priv = nft_expr_priv(expr);
97
98 if (nla_put_be32(skb, NFTA_REJECT_TYPE, htonl(priv->type)))
99 goto nla_put_failure;
100
101 switch (priv->type) {
102 case NFT_REJECT_ICMP_UNREACH:
103 case NFT_REJECT_ICMPX_UNREACH:
104 if (nla_put_u8(skb, NFTA_REJECT_ICMP_CODE, priv->icmp_code))
105 goto nla_put_failure;
106 break;
129d23a5
DM
107 default:
108 break;
51b0a5d8
PNA
109 }
110
111 return 0;
112
113nla_put_failure:
114 return -1;
05513e9e
PM
115}
116
117static struct nft_expr_type nft_reject_inet_type;
118static const struct nft_expr_ops nft_reject_inet_ops = {
119 .type = &nft_reject_inet_type,
120 .size = NFT_EXPR_SIZE(sizeof(struct nft_reject)),
121 .eval = nft_reject_inet_eval,
51b0a5d8
PNA
122 .init = nft_reject_inet_init,
123 .dump = nft_reject_inet_dump,
89e1f6d2 124 .validate = nft_reject_validate,
05513e9e
PM
125};
126
127static struct nft_expr_type nft_reject_inet_type __read_mostly = {
128 .family = NFPROTO_INET,
129 .name = "reject",
130 .ops = &nft_reject_inet_ops,
131 .policy = nft_reject_policy,
132 .maxattr = NFTA_REJECT_MAX,
133 .owner = THIS_MODULE,
134};
135
136static int __init nft_reject_inet_module_init(void)
137{
138 return nft_register_expr(&nft_reject_inet_type);
139}
140
141static void __exit nft_reject_inet_module_exit(void)
142{
143 nft_unregister_expr(&nft_reject_inet_type);
144}
145
146module_init(nft_reject_inet_module_init);
147module_exit(nft_reject_inet_module_exit);
148
149MODULE_LICENSE("GPL");
150MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
151MODULE_ALIAS_NFT_AF_EXPR(1, "reject");