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