2 * Copyright (c) 2014 Patrick McHardy <kaber@trash.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
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>
16 #include <net/netfilter/nft_reject.h>
17 #include <net/netfilter/ipv4/nf_reject.h>
18 #include <net/netfilter/ipv6/nf_reject.h>
20 static void nft_reject_inet_eval(const struct nft_expr
*expr
,
21 struct nft_regs
*regs
,
22 const struct nft_pktinfo
*pkt
)
24 struct nft_reject
*priv
= nft_expr_priv(expr
);
26 switch (nft_pf(pkt
)) {
29 case NFT_REJECT_ICMP_UNREACH
:
30 nf_send_unreach(pkt
->skb
, priv
->icmp_code
,
33 case NFT_REJECT_TCP_RST
:
34 nf_send_reset(nft_net(pkt
), pkt
->skb
, nft_hook(pkt
));
36 case NFT_REJECT_ICMPX_UNREACH
:
37 nf_send_unreach(pkt
->skb
,
38 nft_reject_icmp_code(priv
->icmp_code
),
45 case NFT_REJECT_ICMP_UNREACH
:
46 nf_send_unreach6(nft_net(pkt
), pkt
->skb
,
47 priv
->icmp_code
, nft_hook(pkt
));
49 case NFT_REJECT_TCP_RST
:
50 nf_send_reset6(nft_net(pkt
), pkt
->skb
, nft_hook(pkt
));
52 case NFT_REJECT_ICMPX_UNREACH
:
53 nf_send_unreach6(nft_net(pkt
), pkt
->skb
,
54 nft_reject_icmpv6_code(priv
->icmp_code
),
61 regs
->verdict
.code
= NF_DROP
;
64 static int nft_reject_inet_init(const struct nft_ctx
*ctx
,
65 const struct nft_expr
*expr
,
66 const struct nlattr
* const tb
[])
68 struct nft_reject
*priv
= nft_expr_priv(expr
);
71 if (tb
[NFTA_REJECT_TYPE
] == NULL
)
74 priv
->type
= ntohl(nla_get_be32(tb
[NFTA_REJECT_TYPE
]));
76 case NFT_REJECT_ICMP_UNREACH
:
77 case NFT_REJECT_ICMPX_UNREACH
:
78 if (tb
[NFTA_REJECT_ICMP_CODE
] == NULL
)
81 icmp_code
= nla_get_u8(tb
[NFTA_REJECT_ICMP_CODE
]);
82 if (priv
->type
== NFT_REJECT_ICMPX_UNREACH
&&
83 icmp_code
> NFT_REJECT_ICMPX_MAX
)
86 priv
->icmp_code
= icmp_code
;
88 case NFT_REJECT_TCP_RST
:
96 static int nft_reject_inet_dump(struct sk_buff
*skb
,
97 const struct nft_expr
*expr
)
99 const struct nft_reject
*priv
= nft_expr_priv(expr
);
101 if (nla_put_be32(skb
, NFTA_REJECT_TYPE
, htonl(priv
->type
)))
102 goto nla_put_failure
;
104 switch (priv
->type
) {
105 case NFT_REJECT_ICMP_UNREACH
:
106 case NFT_REJECT_ICMPX_UNREACH
:
107 if (nla_put_u8(skb
, NFTA_REJECT_ICMP_CODE
, priv
->icmp_code
))
108 goto nla_put_failure
;
120 static struct nft_expr_type nft_reject_inet_type
;
121 static const struct nft_expr_ops nft_reject_inet_ops
= {
122 .type
= &nft_reject_inet_type
,
123 .size
= NFT_EXPR_SIZE(sizeof(struct nft_reject
)),
124 .eval
= nft_reject_inet_eval
,
125 .init
= nft_reject_inet_init
,
126 .dump
= nft_reject_inet_dump
,
127 .validate
= nft_reject_validate
,
130 static struct nft_expr_type nft_reject_inet_type __read_mostly
= {
131 .family
= NFPROTO_INET
,
133 .ops
= &nft_reject_inet_ops
,
134 .policy
= nft_reject_policy
,
135 .maxattr
= NFTA_REJECT_MAX
,
136 .owner
= THIS_MODULE
,
139 static int __init
nft_reject_inet_module_init(void)
141 return nft_register_expr(&nft_reject_inet_type
);
144 static void __exit
nft_reject_inet_module_exit(void)
146 nft_unregister_expr(&nft_reject_inet_type
);
149 module_init(nft_reject_inet_module_init
);
150 module_exit(nft_reject_inet_module_exit
);
152 MODULE_LICENSE("GPL");
153 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
154 MODULE_ALIAS_NFT_AF_EXPR(1, "reject");