]>
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, | |
21 | struct nft_data data[NFT_REG_MAX + 1], | |
22 | const struct nft_pktinfo *pkt) | |
23 | { | |
51b0a5d8 PNA |
24 | struct nft_reject *priv = nft_expr_priv(expr); |
25 | struct net *net = dev_net((pkt->in != NULL) ? pkt->in : pkt->out); | |
26 | ||
05513e9e PM |
27 | switch (pkt->ops->pf) { |
28 | case NFPROTO_IPV4: | |
51b0a5d8 PNA |
29 | switch (priv->type) { |
30 | case NFT_REJECT_ICMP_UNREACH: | |
ee586bbc FW |
31 | nf_send_unreach(pkt->skb, priv->icmp_code, |
32 | pkt->ops->hooknum); | |
51b0a5d8 PNA |
33 | break; |
34 | case NFT_REJECT_TCP_RST: | |
35 | nf_send_reset(pkt->skb, pkt->ops->hooknum); | |
36 | break; | |
37 | case NFT_REJECT_ICMPX_UNREACH: | |
38 | nf_send_unreach(pkt->skb, | |
ee586bbc FW |
39 | nft_reject_icmp_code(priv->icmp_code), |
40 | pkt->ops->hooknum); | |
51b0a5d8 PNA |
41 | break; |
42 | } | |
43 | break; | |
05513e9e | 44 | case NFPROTO_IPV6: |
51b0a5d8 PNA |
45 | switch (priv->type) { |
46 | case NFT_REJECT_ICMP_UNREACH: | |
47 | nf_send_unreach6(net, pkt->skb, priv->icmp_code, | |
48 | pkt->ops->hooknum); | |
49 | break; | |
50 | case NFT_REJECT_TCP_RST: | |
51 | nf_send_reset6(net, pkt->skb, pkt->ops->hooknum); | |
52 | break; | |
53 | case NFT_REJECT_ICMPX_UNREACH: | |
54 | nf_send_unreach6(net, pkt->skb, | |
55 | nft_reject_icmpv6_code(priv->icmp_code), | |
56 | pkt->ops->hooknum); | |
57 | break; | |
58 | } | |
59 | break; | |
60 | } | |
61 | data[NFT_REG_VERDICT].verdict = NF_DROP; | |
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); | |
69 | int icmp_code; | |
70 | ||
71 | if (tb[NFTA_REJECT_TYPE] == NULL) | |
72 | return -EINVAL; | |
73 | ||
74 | priv->type = ntohl(nla_get_be32(tb[NFTA_REJECT_TYPE])); | |
75 | switch (priv->type) { | |
76 | case NFT_REJECT_ICMP_UNREACH: | |
77 | case NFT_REJECT_ICMPX_UNREACH: | |
78 | if (tb[NFTA_REJECT_ICMP_CODE] == NULL) | |
79 | return -EINVAL; | |
80 | ||
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) | |
84 | return -EINVAL; | |
85 | ||
86 | priv->icmp_code = icmp_code; | |
87 | break; | |
88 | case NFT_REJECT_TCP_RST: | |
89 | break; | |
90 | default: | |
91 | return -EINVAL; | |
05513e9e | 92 | } |
51b0a5d8 PNA |
93 | return 0; |
94 | } | |
95 | ||
96 | static int nft_reject_inet_dump(struct sk_buff *skb, | |
97 | const struct nft_expr *expr) | |
98 | { | |
99 | const struct nft_reject *priv = nft_expr_priv(expr); | |
100 | ||
101 | if (nla_put_be32(skb, NFTA_REJECT_TYPE, htonl(priv->type))) | |
102 | goto nla_put_failure; | |
103 | ||
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; | |
109 | break; | |
110 | } | |
111 | ||
112 | return 0; | |
113 | ||
114 | nla_put_failure: | |
115 | return -1; | |
05513e9e PM |
116 | } |
117 | ||
118 | static struct nft_expr_type nft_reject_inet_type; | |
119 | static const struct nft_expr_ops nft_reject_inet_ops = { | |
120 | .type = &nft_reject_inet_type, | |
121 | .size = NFT_EXPR_SIZE(sizeof(struct nft_reject)), | |
122 | .eval = nft_reject_inet_eval, | |
51b0a5d8 PNA |
123 | .init = nft_reject_inet_init, |
124 | .dump = nft_reject_inet_dump, | |
05513e9e PM |
125 | }; |
126 | ||
127 | static 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 | ||
136 | static int __init nft_reject_inet_module_init(void) | |
137 | { | |
138 | return nft_register_expr(&nft_reject_inet_type); | |
139 | } | |
140 | ||
141 | static void __exit nft_reject_inet_module_exit(void) | |
142 | { | |
143 | nft_unregister_expr(&nft_reject_inet_type); | |
144 | } | |
145 | ||
146 | module_init(nft_reject_inet_module_init); | |
147 | module_exit(nft_reject_inet_module_exit); | |
148 | ||
149 | MODULE_LICENSE("GPL"); | |
150 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
151 | MODULE_ALIAS_NFT_AF_EXPR(1, "reject"); |