2 * Copyright (c) 2014 Pablo Neira Ayuso <pablo@netfilter.org>
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/nf_tables_bridge.h>
18 #include <net/netfilter/ipv4/nf_reject.h>
19 #include <net/netfilter/ipv6/nf_reject.h>
22 #include <net/ip6_checksum.h>
23 #include <linux/netfilter_bridge.h>
24 #include "../br_private.h"
26 static void nft_reject_br_push_etherhdr(struct sk_buff *oldskb,
31 eth = (struct ethhdr *)skb_push(nskb, ETH_HLEN);
32 skb_reset_mac_header(nskb);
33 ether_addr_copy(eth->h_source, eth_hdr(oldskb)->h_dest);
34 ether_addr_copy(eth->h_dest, eth_hdr(oldskb)->h_source);
35 eth->h_proto = eth_hdr(oldskb)->h_proto;
36 skb_pull(nskb, ETH_HLEN);
39 static void nft_reject_br_send_v4_tcp_reset(struct sk_buff *oldskb, int hook)
43 const struct tcphdr *oth;
46 if (!nft_bridge_iphdr_validate(oldskb))
49 oth = nf_reject_ip_tcphdr_get(oldskb, &_oth, hook);
53 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) +
54 LL_MAX_HEADER, GFP_ATOMIC);
58 skb_reserve(nskb, LL_MAX_HEADER);
59 niph = nf_reject_iphdr_put(nskb, oldskb, IPPROTO_TCP,
60 sysctl_ip_default_ttl);
61 nf_reject_ip_tcphdr_put(nskb, oldskb, oth);
62 niph->ttl = sysctl_ip_default_ttl;
63 niph->tot_len = htons(nskb->len);
66 nft_reject_br_push_etherhdr(oldskb, nskb);
68 br_deliver(br_port_get_rcu(oldskb->dev), nskb);
71 static void nft_reject_br_send_v4_unreach(struct sk_buff *oldskb, int hook,
76 struct icmphdr *icmph;
81 if (!nft_bridge_iphdr_validate(oldskb))
84 /* IP header checks: fragment. */
85 if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
88 /* RFC says return as much as we can without exceeding 576 bytes. */
89 len = min_t(unsigned int, 536, oldskb->len);
91 if (!pskb_may_pull(oldskb, len))
94 if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), 0))
97 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct icmphdr) +
98 LL_MAX_HEADER + len, GFP_ATOMIC);
102 skb_reserve(nskb, LL_MAX_HEADER);
103 niph = nf_reject_iphdr_put(nskb, oldskb, IPPROTO_ICMP,
104 sysctl_ip_default_ttl);
106 skb_reset_transport_header(nskb);
107 icmph = (struct icmphdr *)skb_put(nskb, sizeof(struct icmphdr));
108 memset(icmph, 0, sizeof(*icmph));
109 icmph->type = ICMP_DEST_UNREACH;
112 payload = skb_put(nskb, len);
113 memcpy(payload, skb_network_header(oldskb), len);
115 csum = csum_partial((void *)icmph, len + sizeof(struct icmphdr), 0);
116 icmph->checksum = csum_fold(csum);
118 niph->tot_len = htons(nskb->len);
121 nft_reject_br_push_etherhdr(oldskb, nskb);
123 br_deliver(br_port_get_rcu(oldskb->dev), nskb);
126 static void nft_reject_br_send_v6_tcp_reset(struct net *net,
127 struct sk_buff *oldskb, int hook)
129 struct sk_buff *nskb;
130 const struct tcphdr *oth;
132 unsigned int otcplen;
133 struct ipv6hdr *nip6h;
135 if (!nft_bridge_ip6hdr_validate(oldskb))
138 oth = nf_reject_ip6_tcphdr_get(oldskb, &_oth, &otcplen, hook);
142 nskb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(struct tcphdr) +
143 LL_MAX_HEADER, GFP_ATOMIC);
147 skb_reserve(nskb, LL_MAX_HEADER);
148 nip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_TCP,
149 net->ipv6.devconf_all->hop_limit);
150 nf_reject_ip6_tcphdr_put(nskb, oldskb, oth, otcplen);
151 nip6h->payload_len = htons(nskb->len - sizeof(struct ipv6hdr));
153 nft_reject_br_push_etherhdr(oldskb, nskb);
155 br_deliver(br_port_get_rcu(oldskb->dev), nskb);
158 static void nft_reject_br_send_v6_unreach(struct net *net,
159 struct sk_buff *oldskb, int hook,
162 struct sk_buff *nskb;
163 struct ipv6hdr *nip6h;
164 struct icmp6hdr *icmp6h;
168 if (!nft_bridge_ip6hdr_validate(oldskb))
171 /* Include "As much of invoking packet as possible without the ICMPv6
172 * packet exceeding the minimum IPv6 MTU" in the ICMP payload.
174 len = min_t(unsigned int, 1220, oldskb->len);
176 if (!pskb_may_pull(oldskb, len))
179 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct icmp6hdr) +
180 LL_MAX_HEADER + len, GFP_ATOMIC);
184 skb_reserve(nskb, LL_MAX_HEADER);
185 nip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_ICMPV6,
186 net->ipv6.devconf_all->hop_limit);
188 skb_reset_transport_header(nskb);
189 icmp6h = (struct icmp6hdr *)skb_put(nskb, sizeof(struct icmp6hdr));
190 memset(icmp6h, 0, sizeof(*icmp6h));
191 icmp6h->icmp6_type = ICMPV6_DEST_UNREACH;
192 icmp6h->icmp6_code = code;
194 payload = skb_put(nskb, len);
195 memcpy(payload, skb_network_header(oldskb), len);
196 nip6h->payload_len = htons(nskb->len - sizeof(struct ipv6hdr));
198 icmp6h->icmp6_cksum =
199 csum_ipv6_magic(&nip6h->saddr, &nip6h->daddr,
200 nskb->len - sizeof(struct ipv6hdr),
203 nskb->len - sizeof(struct ipv6hdr),
206 nft_reject_br_push_etherhdr(oldskb, nskb);
208 br_deliver(br_port_get_rcu(oldskb->dev), nskb);
211 static void nft_reject_bridge_eval(const struct nft_expr *expr,
212 struct nft_data data[NFT_REG_MAX + 1],
213 const struct nft_pktinfo *pkt)
215 struct nft_reject *priv = nft_expr_priv(expr);
216 struct net *net = dev_net((pkt->in != NULL) ? pkt->in : pkt->out);
217 const unsigned char *dest = eth_hdr(pkt->skb)->h_dest;
219 if (is_broadcast_ether_addr(dest) ||
220 is_multicast_ether_addr(dest))
223 switch (eth_hdr(pkt->skb)->h_proto) {
224 case htons(ETH_P_IP):
225 switch (priv->type) {
226 case NFT_REJECT_ICMP_UNREACH:
227 nft_reject_br_send_v4_unreach(pkt->skb,
231 case NFT_REJECT_TCP_RST:
232 nft_reject_br_send_v4_tcp_reset(pkt->skb,
235 case NFT_REJECT_ICMPX_UNREACH:
236 nft_reject_br_send_v4_unreach(pkt->skb,
238 nft_reject_icmp_code(priv->icmp_code));
242 case htons(ETH_P_IPV6):
243 switch (priv->type) {
244 case NFT_REJECT_ICMP_UNREACH:
245 nft_reject_br_send_v6_unreach(net, pkt->skb,
249 case NFT_REJECT_TCP_RST:
250 nft_reject_br_send_v6_tcp_reset(net, pkt->skb,
253 case NFT_REJECT_ICMPX_UNREACH:
254 nft_reject_br_send_v6_unreach(net, pkt->skb,
256 nft_reject_icmpv6_code(priv->icmp_code));
261 /* No explicit way to reject this protocol, drop it. */
265 data[NFT_REG_VERDICT].verdict = NF_DROP;
268 static int nft_reject_bridge_validate_hooks(const struct nft_chain *chain)
270 struct nft_base_chain *basechain;
272 if (chain->flags & NFT_BASE_CHAIN) {
273 basechain = nft_base_chain(chain);
275 switch (basechain->ops[0].hooknum) {
276 case NF_BR_PRE_ROUTING:
286 static int nft_reject_bridge_init(const struct nft_ctx *ctx,
287 const struct nft_expr *expr,
288 const struct nlattr * const tb[])
290 struct nft_reject *priv = nft_expr_priv(expr);
293 err = nft_reject_bridge_validate_hooks(ctx->chain);
297 if (tb[NFTA_REJECT_TYPE] == NULL)
300 priv->type = ntohl(nla_get_be32(tb[NFTA_REJECT_TYPE]));
301 switch (priv->type) {
302 case NFT_REJECT_ICMP_UNREACH:
303 case NFT_REJECT_ICMPX_UNREACH:
304 if (tb[NFTA_REJECT_ICMP_CODE] == NULL)
307 icmp_code = nla_get_u8(tb[NFTA_REJECT_ICMP_CODE]);
308 if (priv->type == NFT_REJECT_ICMPX_UNREACH &&
309 icmp_code > NFT_REJECT_ICMPX_MAX)
312 priv->icmp_code = icmp_code;
314 case NFT_REJECT_TCP_RST:
322 static int nft_reject_bridge_dump(struct sk_buff *skb,
323 const struct nft_expr *expr)
325 const struct nft_reject *priv = nft_expr_priv(expr);
327 if (nla_put_be32(skb, NFTA_REJECT_TYPE, htonl(priv->type)))
328 goto nla_put_failure;
330 switch (priv->type) {
331 case NFT_REJECT_ICMP_UNREACH:
332 case NFT_REJECT_ICMPX_UNREACH:
333 if (nla_put_u8(skb, NFTA_REJECT_ICMP_CODE, priv->icmp_code))
334 goto nla_put_failure;
344 static int nft_reject_bridge_validate(const struct nft_ctx *ctx,
345 const struct nft_expr *expr,
346 const struct nft_data **data)
348 return nft_reject_bridge_validate_hooks(ctx->chain);
351 static struct nft_expr_type nft_reject_bridge_type;
352 static const struct nft_expr_ops nft_reject_bridge_ops = {
353 .type = &nft_reject_bridge_type,
354 .size = NFT_EXPR_SIZE(sizeof(struct nft_reject)),
355 .eval = nft_reject_bridge_eval,
356 .init = nft_reject_bridge_init,
357 .dump = nft_reject_bridge_dump,
358 .validate = nft_reject_bridge_validate,
361 static struct nft_expr_type nft_reject_bridge_type __read_mostly = {
362 .family = NFPROTO_BRIDGE,
364 .ops = &nft_reject_bridge_ops,
365 .policy = nft_reject_policy,
366 .maxattr = NFTA_REJECT_MAX,
367 .owner = THIS_MODULE,
370 static int __init nft_reject_bridge_module_init(void)
372 return nft_register_expr(&nft_reject_bridge_type);
375 static void __exit nft_reject_bridge_module_exit(void)
377 nft_unregister_expr(&nft_reject_bridge_type);
380 module_init(nft_reject_bridge_module_init);
381 module_exit(nft_reject_bridge_module_exit);
383 MODULE_LICENSE("GPL");
384 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
385 MODULE_ALIAS_NFT_AF_EXPR(AF_BRIDGE, "reject");