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/ipv4/nf_reject.h>
18 #include <net/netfilter/ipv6/nf_reject.h>
21 #include <net/ip6_checksum.h>
22 #include <linux/netfilter_bridge.h>
23 #include <linux/netfilter_ipv6.h>
24 #include "../br_private.h"
26 static void nft_reject_br_push_etherhdr(struct sk_buff *oldskb,
31 eth = 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 int nft_bridge_iphdr_validate(struct sk_buff *skb)
44 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
48 if (iph->ihl < 5 || iph->version != 4)
51 len = ntohs(iph->tot_len);
54 else if (len < (iph->ihl*4))
57 if (!pskb_may_pull(skb, iph->ihl*4))
63 /* We cannot use oldskb->dev, it can be either bridge device (NF_BRIDGE INPUT)
64 * or the bridge port (NF_BRIDGE PREROUTING).
66 static void nft_reject_br_send_v4_tcp_reset(struct net *net,
67 struct sk_buff *oldskb,
68 const struct net_device *dev,
73 const struct tcphdr *oth;
76 if (!nft_bridge_iphdr_validate(oldskb))
79 oth = nf_reject_ip_tcphdr_get(oldskb, &_oth, hook);
83 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) +
84 LL_MAX_HEADER, GFP_ATOMIC);
88 skb_reserve(nskb, LL_MAX_HEADER);
89 niph = nf_reject_iphdr_put(nskb, oldskb, IPPROTO_TCP,
90 net->ipv4.sysctl_ip_default_ttl);
91 nf_reject_ip_tcphdr_put(nskb, oldskb, oth);
92 niph->tot_len = htons(nskb->len);
95 nft_reject_br_push_etherhdr(oldskb, nskb);
97 br_forward(br_port_get_rcu(dev), nskb, false, true);
100 static void nft_reject_br_send_v4_unreach(struct net *net,
101 struct sk_buff *oldskb,
102 const struct net_device *dev,
105 struct sk_buff *nskb;
107 struct icmphdr *icmph;
112 if (!nft_bridge_iphdr_validate(oldskb))
115 /* IP header checks: fragment. */
116 if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
119 /* RFC says return as much as we can without exceeding 576 bytes. */
120 len = min_t(unsigned int, 536, oldskb->len);
122 if (!pskb_may_pull(oldskb, len))
125 if (pskb_trim_rcsum(oldskb, ntohs(ip_hdr(oldskb)->tot_len)))
128 if (ip_hdr(oldskb)->protocol == IPPROTO_TCP ||
129 ip_hdr(oldskb)->protocol == IPPROTO_UDP)
130 proto = ip_hdr(oldskb)->protocol;
134 if (!skb_csum_unnecessary(oldskb) &&
135 nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), proto))
138 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct icmphdr) +
139 LL_MAX_HEADER + len, GFP_ATOMIC);
143 skb_reserve(nskb, LL_MAX_HEADER);
144 niph = nf_reject_iphdr_put(nskb, oldskb, IPPROTO_ICMP,
145 net->ipv4.sysctl_ip_default_ttl);
147 skb_reset_transport_header(nskb);
148 icmph = skb_put_zero(nskb, sizeof(struct icmphdr));
149 icmph->type = ICMP_DEST_UNREACH;
152 skb_put_data(nskb, skb_network_header(oldskb), len);
154 csum = csum_partial((void *)icmph, len + sizeof(struct icmphdr), 0);
155 icmph->checksum = csum_fold(csum);
157 niph->tot_len = htons(nskb->len);
160 nft_reject_br_push_etherhdr(oldskb, nskb);
162 br_forward(br_port_get_rcu(dev), nskb, false, true);
165 static int nft_bridge_ip6hdr_validate(struct sk_buff *skb)
170 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
174 if (hdr->version != 6)
177 pkt_len = ntohs(hdr->payload_len);
178 if (pkt_len + sizeof(struct ipv6hdr) > skb->len)
184 static void nft_reject_br_send_v6_tcp_reset(struct net *net,
185 struct sk_buff *oldskb,
186 const struct net_device *dev,
189 struct sk_buff *nskb;
190 const struct tcphdr *oth;
192 unsigned int otcplen;
193 struct ipv6hdr *nip6h;
195 if (!nft_bridge_ip6hdr_validate(oldskb))
198 oth = nf_reject_ip6_tcphdr_get(oldskb, &_oth, &otcplen, hook);
202 nskb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(struct tcphdr) +
203 LL_MAX_HEADER, GFP_ATOMIC);
207 skb_reserve(nskb, LL_MAX_HEADER);
208 nip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_TCP,
209 net->ipv6.devconf_all->hop_limit);
210 nf_reject_ip6_tcphdr_put(nskb, oldskb, oth, otcplen);
211 nip6h->payload_len = htons(nskb->len - sizeof(struct ipv6hdr));
213 nft_reject_br_push_etherhdr(oldskb, nskb);
215 br_forward(br_port_get_rcu(dev), nskb, false, true);
218 static bool reject6_br_csum_ok(struct sk_buff *skb, int hook)
220 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
223 u8 proto = ip6h->nexthdr;
225 if (skb_csum_unnecessary(skb))
228 if (ip6h->payload_len &&
229 pskb_trim_rcsum(skb, ntohs(ip6h->payload_len) + sizeof(*ip6h)))
232 thoff = ipv6_skip_exthdr(skb, ((u8*)(ip6h+1) - skb->data), &proto, &fo);
233 if (thoff < 0 || thoff >= skb->len || (fo & htons(~0x7)) != 0)
236 return nf_ip6_checksum(skb, hook, thoff, proto) == 0;
239 static void nft_reject_br_send_v6_unreach(struct net *net,
240 struct sk_buff *oldskb,
241 const struct net_device *dev,
244 struct sk_buff *nskb;
245 struct ipv6hdr *nip6h;
246 struct icmp6hdr *icmp6h;
249 if (!nft_bridge_ip6hdr_validate(oldskb))
252 /* Include "As much of invoking packet as possible without the ICMPv6
253 * packet exceeding the minimum IPv6 MTU" in the ICMP payload.
255 len = min_t(unsigned int, 1220, oldskb->len);
257 if (!pskb_may_pull(oldskb, len))
260 if (!reject6_br_csum_ok(oldskb, hook))
263 nskb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(struct icmp6hdr) +
264 LL_MAX_HEADER + len, GFP_ATOMIC);
268 skb_reserve(nskb, LL_MAX_HEADER);
269 nip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_ICMPV6,
270 net->ipv6.devconf_all->hop_limit);
272 skb_reset_transport_header(nskb);
273 icmp6h = skb_put_zero(nskb, sizeof(struct icmp6hdr));
274 icmp6h->icmp6_type = ICMPV6_DEST_UNREACH;
275 icmp6h->icmp6_code = code;
277 skb_put_data(nskb, skb_network_header(oldskb), len);
278 nip6h->payload_len = htons(nskb->len - sizeof(struct ipv6hdr));
280 icmp6h->icmp6_cksum =
281 csum_ipv6_magic(&nip6h->saddr, &nip6h->daddr,
282 nskb->len - sizeof(struct ipv6hdr),
285 nskb->len - sizeof(struct ipv6hdr),
288 nft_reject_br_push_etherhdr(oldskb, nskb);
290 br_forward(br_port_get_rcu(dev), nskb, false, true);
293 static void nft_reject_bridge_eval(const struct nft_expr *expr,
294 struct nft_regs *regs,
295 const struct nft_pktinfo *pkt)
297 struct nft_reject *priv = nft_expr_priv(expr);
298 const unsigned char *dest = eth_hdr(pkt->skb)->h_dest;
300 if (is_broadcast_ether_addr(dest) ||
301 is_multicast_ether_addr(dest))
304 switch (eth_hdr(pkt->skb)->h_proto) {
305 case htons(ETH_P_IP):
306 switch (priv->type) {
307 case NFT_REJECT_ICMP_UNREACH:
308 nft_reject_br_send_v4_unreach(nft_net(pkt), pkt->skb,
313 case NFT_REJECT_TCP_RST:
314 nft_reject_br_send_v4_tcp_reset(nft_net(pkt), pkt->skb,
318 case NFT_REJECT_ICMPX_UNREACH:
319 nft_reject_br_send_v4_unreach(nft_net(pkt), pkt->skb,
322 nft_reject_icmp_code(priv->icmp_code));
326 case htons(ETH_P_IPV6):
327 switch (priv->type) {
328 case NFT_REJECT_ICMP_UNREACH:
329 nft_reject_br_send_v6_unreach(nft_net(pkt), pkt->skb,
334 case NFT_REJECT_TCP_RST:
335 nft_reject_br_send_v6_tcp_reset(nft_net(pkt), pkt->skb,
339 case NFT_REJECT_ICMPX_UNREACH:
340 nft_reject_br_send_v6_unreach(nft_net(pkt), pkt->skb,
343 nft_reject_icmpv6_code(priv->icmp_code));
348 /* No explicit way to reject this protocol, drop it. */
352 regs->verdict.code = NF_DROP;
355 static int nft_reject_bridge_validate(const struct nft_ctx *ctx,
356 const struct nft_expr *expr,
357 const struct nft_data **data)
359 return nft_chain_validate_hooks(ctx->chain, (1 << NF_BR_PRE_ROUTING) |
360 (1 << NF_BR_LOCAL_IN));
363 static int nft_reject_bridge_init(const struct nft_ctx *ctx,
364 const struct nft_expr *expr,
365 const struct nlattr * const tb[])
367 struct nft_reject *priv = nft_expr_priv(expr);
370 if (tb[NFTA_REJECT_TYPE] == NULL)
373 priv->type = ntohl(nla_get_be32(tb[NFTA_REJECT_TYPE]));
374 switch (priv->type) {
375 case NFT_REJECT_ICMP_UNREACH:
376 case NFT_REJECT_ICMPX_UNREACH:
377 if (tb[NFTA_REJECT_ICMP_CODE] == NULL)
380 icmp_code = nla_get_u8(tb[NFTA_REJECT_ICMP_CODE]);
381 if (priv->type == NFT_REJECT_ICMPX_UNREACH &&
382 icmp_code > NFT_REJECT_ICMPX_MAX)
385 priv->icmp_code = icmp_code;
387 case NFT_REJECT_TCP_RST:
395 static int nft_reject_bridge_dump(struct sk_buff *skb,
396 const struct nft_expr *expr)
398 const struct nft_reject *priv = nft_expr_priv(expr);
400 if (nla_put_be32(skb, NFTA_REJECT_TYPE, htonl(priv->type)))
401 goto nla_put_failure;
403 switch (priv->type) {
404 case NFT_REJECT_ICMP_UNREACH:
405 case NFT_REJECT_ICMPX_UNREACH:
406 if (nla_put_u8(skb, NFTA_REJECT_ICMP_CODE, priv->icmp_code))
407 goto nla_put_failure;
419 static struct nft_expr_type nft_reject_bridge_type;
420 static const struct nft_expr_ops nft_reject_bridge_ops = {
421 .type = &nft_reject_bridge_type,
422 .size = NFT_EXPR_SIZE(sizeof(struct nft_reject)),
423 .eval = nft_reject_bridge_eval,
424 .init = nft_reject_bridge_init,
425 .dump = nft_reject_bridge_dump,
426 .validate = nft_reject_bridge_validate,
429 static struct nft_expr_type nft_reject_bridge_type __read_mostly = {
430 .family = NFPROTO_BRIDGE,
432 .ops = &nft_reject_bridge_ops,
433 .policy = nft_reject_policy,
434 .maxattr = NFTA_REJECT_MAX,
435 .owner = THIS_MODULE,
438 static int __init nft_reject_bridge_module_init(void)
440 return nft_register_expr(&nft_reject_bridge_type);
443 static void __exit nft_reject_bridge_module_exit(void)
445 nft_unregister_expr(&nft_reject_bridge_type);
448 module_init(nft_reject_bridge_module_init);
449 module_exit(nft_reject_bridge_module_exit);
451 MODULE_LICENSE("GPL");
452 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
453 MODULE_ALIAS_NFT_AF_EXPR(AF_BRIDGE, "reject");