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
= (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 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
->ttl
= net
->ipv4
.sysctl_ip_default_ttl
;
93 niph
->tot_len
= htons(nskb
->len
);
96 nft_reject_br_push_etherhdr(oldskb
, nskb
);
98 br_forward(br_port_get_rcu(dev
), nskb
, false, true);
101 static void nft_reject_br_send_v4_unreach(struct net
*net
,
102 struct sk_buff
*oldskb
,
103 const struct net_device
*dev
,
106 struct sk_buff
*nskb
;
108 struct icmphdr
*icmph
;
114 if (!nft_bridge_iphdr_validate(oldskb
))
117 /* IP header checks: fragment. */
118 if (ip_hdr(oldskb
)->frag_off
& htons(IP_OFFSET
))
121 /* RFC says return as much as we can without exceeding 576 bytes. */
122 len
= min_t(unsigned int, 536, oldskb
->len
);
124 if (!pskb_may_pull(oldskb
, len
))
127 if (pskb_trim_rcsum(oldskb
, ntohs(ip_hdr(oldskb
)->tot_len
)))
130 if (ip_hdr(oldskb
)->protocol
== IPPROTO_TCP
||
131 ip_hdr(oldskb
)->protocol
== IPPROTO_UDP
)
132 proto
= ip_hdr(oldskb
)->protocol
;
136 if (!skb_csum_unnecessary(oldskb
) &&
137 nf_ip_checksum(oldskb
, hook
, ip_hdrlen(oldskb
), proto
))
140 nskb
= alloc_skb(sizeof(struct iphdr
) + sizeof(struct icmphdr
) +
141 LL_MAX_HEADER
+ len
, GFP_ATOMIC
);
145 skb_reserve(nskb
, LL_MAX_HEADER
);
146 niph
= nf_reject_iphdr_put(nskb
, oldskb
, IPPROTO_ICMP
,
147 net
->ipv4
.sysctl_ip_default_ttl
);
149 skb_reset_transport_header(nskb
);
150 icmph
= skb_put_zero(nskb
, sizeof(struct icmphdr
));
151 icmph
->type
= ICMP_DEST_UNREACH
;
154 payload
= skb_put_data(nskb
, skb_network_header(oldskb
), len
);
156 csum
= csum_partial((void *)icmph
, len
+ sizeof(struct icmphdr
), 0);
157 icmph
->checksum
= csum_fold(csum
);
159 niph
->tot_len
= htons(nskb
->len
);
162 nft_reject_br_push_etherhdr(oldskb
, nskb
);
164 br_forward(br_port_get_rcu(dev
), nskb
, false, true);
167 static int nft_bridge_ip6hdr_validate(struct sk_buff
*skb
)
172 if (!pskb_may_pull(skb
, sizeof(struct ipv6hdr
)))
176 if (hdr
->version
!= 6)
179 pkt_len
= ntohs(hdr
->payload_len
);
180 if (pkt_len
+ sizeof(struct ipv6hdr
) > skb
->len
)
186 static void nft_reject_br_send_v6_tcp_reset(struct net
*net
,
187 struct sk_buff
*oldskb
,
188 const struct net_device
*dev
,
191 struct sk_buff
*nskb
;
192 const struct tcphdr
*oth
;
194 unsigned int otcplen
;
195 struct ipv6hdr
*nip6h
;
197 if (!nft_bridge_ip6hdr_validate(oldskb
))
200 oth
= nf_reject_ip6_tcphdr_get(oldskb
, &_oth
, &otcplen
, hook
);
204 nskb
= alloc_skb(sizeof(struct ipv6hdr
) + sizeof(struct tcphdr
) +
205 LL_MAX_HEADER
, GFP_ATOMIC
);
209 skb_reserve(nskb
, LL_MAX_HEADER
);
210 nip6h
= nf_reject_ip6hdr_put(nskb
, oldskb
, IPPROTO_TCP
,
211 net
->ipv6
.devconf_all
->hop_limit
);
212 nf_reject_ip6_tcphdr_put(nskb
, oldskb
, oth
, otcplen
);
213 nip6h
->payload_len
= htons(nskb
->len
- sizeof(struct ipv6hdr
));
215 nft_reject_br_push_etherhdr(oldskb
, nskb
);
217 br_forward(br_port_get_rcu(dev
), nskb
, false, true);
220 static bool reject6_br_csum_ok(struct sk_buff
*skb
, int hook
)
222 const struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
225 u8 proto
= ip6h
->nexthdr
;
227 if (skb_csum_unnecessary(skb
))
230 if (ip6h
->payload_len
&&
231 pskb_trim_rcsum(skb
, ntohs(ip6h
->payload_len
) + sizeof(*ip6h
)))
234 thoff
= ipv6_skip_exthdr(skb
, ((u8
*)(ip6h
+1) - skb
->data
), &proto
, &fo
);
235 if (thoff
< 0 || thoff
>= skb
->len
|| (fo
& htons(~0x7)) != 0)
238 return nf_ip6_checksum(skb
, hook
, thoff
, proto
) == 0;
241 static void nft_reject_br_send_v6_unreach(struct net
*net
,
242 struct sk_buff
*oldskb
,
243 const struct net_device
*dev
,
246 struct sk_buff
*nskb
;
247 struct ipv6hdr
*nip6h
;
248 struct icmp6hdr
*icmp6h
;
252 if (!nft_bridge_ip6hdr_validate(oldskb
))
255 /* Include "As much of invoking packet as possible without the ICMPv6
256 * packet exceeding the minimum IPv6 MTU" in the ICMP payload.
258 len
= min_t(unsigned int, 1220, oldskb
->len
);
260 if (!pskb_may_pull(oldskb
, len
))
263 if (!reject6_br_csum_ok(oldskb
, hook
))
266 nskb
= alloc_skb(sizeof(struct iphdr
) + sizeof(struct icmp6hdr
) +
267 LL_MAX_HEADER
+ len
, GFP_ATOMIC
);
271 skb_reserve(nskb
, LL_MAX_HEADER
);
272 nip6h
= nf_reject_ip6hdr_put(nskb
, oldskb
, IPPROTO_ICMPV6
,
273 net
->ipv6
.devconf_all
->hop_limit
);
275 skb_reset_transport_header(nskb
);
276 icmp6h
= skb_put_zero(nskb
, sizeof(struct icmp6hdr
));
277 icmp6h
->icmp6_type
= ICMPV6_DEST_UNREACH
;
278 icmp6h
->icmp6_code
= code
;
280 payload
= skb_put_data(nskb
, skb_network_header(oldskb
), len
);
281 nip6h
->payload_len
= htons(nskb
->len
- sizeof(struct ipv6hdr
));
283 icmp6h
->icmp6_cksum
=
284 csum_ipv6_magic(&nip6h
->saddr
, &nip6h
->daddr
,
285 nskb
->len
- sizeof(struct ipv6hdr
),
288 nskb
->len
- sizeof(struct ipv6hdr
),
291 nft_reject_br_push_etherhdr(oldskb
, nskb
);
293 br_forward(br_port_get_rcu(dev
), nskb
, false, true);
296 static void nft_reject_bridge_eval(const struct nft_expr
*expr
,
297 struct nft_regs
*regs
,
298 const struct nft_pktinfo
*pkt
)
300 struct nft_reject
*priv
= nft_expr_priv(expr
);
301 const unsigned char *dest
= eth_hdr(pkt
->skb
)->h_dest
;
303 if (is_broadcast_ether_addr(dest
) ||
304 is_multicast_ether_addr(dest
))
307 switch (eth_hdr(pkt
->skb
)->h_proto
) {
308 case htons(ETH_P_IP
):
309 switch (priv
->type
) {
310 case NFT_REJECT_ICMP_UNREACH
:
311 nft_reject_br_send_v4_unreach(nft_net(pkt
), pkt
->skb
,
316 case NFT_REJECT_TCP_RST
:
317 nft_reject_br_send_v4_tcp_reset(nft_net(pkt
), pkt
->skb
,
321 case NFT_REJECT_ICMPX_UNREACH
:
322 nft_reject_br_send_v4_unreach(nft_net(pkt
), pkt
->skb
,
325 nft_reject_icmp_code(priv
->icmp_code
));
329 case htons(ETH_P_IPV6
):
330 switch (priv
->type
) {
331 case NFT_REJECT_ICMP_UNREACH
:
332 nft_reject_br_send_v6_unreach(nft_net(pkt
), pkt
->skb
,
337 case NFT_REJECT_TCP_RST
:
338 nft_reject_br_send_v6_tcp_reset(nft_net(pkt
), pkt
->skb
,
342 case NFT_REJECT_ICMPX_UNREACH
:
343 nft_reject_br_send_v6_unreach(nft_net(pkt
), pkt
->skb
,
346 nft_reject_icmpv6_code(priv
->icmp_code
));
351 /* No explicit way to reject this protocol, drop it. */
355 regs
->verdict
.code
= NF_DROP
;
358 static int nft_reject_bridge_validate(const struct nft_ctx
*ctx
,
359 const struct nft_expr
*expr
,
360 const struct nft_data
**data
)
362 return nft_chain_validate_hooks(ctx
->chain
, (1 << NF_BR_PRE_ROUTING
) |
363 (1 << NF_BR_LOCAL_IN
));
366 static int nft_reject_bridge_init(const struct nft_ctx
*ctx
,
367 const struct nft_expr
*expr
,
368 const struct nlattr
* const tb
[])
370 struct nft_reject
*priv
= nft_expr_priv(expr
);
373 if (tb
[NFTA_REJECT_TYPE
] == NULL
)
376 priv
->type
= ntohl(nla_get_be32(tb
[NFTA_REJECT_TYPE
]));
377 switch (priv
->type
) {
378 case NFT_REJECT_ICMP_UNREACH
:
379 case NFT_REJECT_ICMPX_UNREACH
:
380 if (tb
[NFTA_REJECT_ICMP_CODE
] == NULL
)
383 icmp_code
= nla_get_u8(tb
[NFTA_REJECT_ICMP_CODE
]);
384 if (priv
->type
== NFT_REJECT_ICMPX_UNREACH
&&
385 icmp_code
> NFT_REJECT_ICMPX_MAX
)
388 priv
->icmp_code
= icmp_code
;
390 case NFT_REJECT_TCP_RST
:
398 static int nft_reject_bridge_dump(struct sk_buff
*skb
,
399 const struct nft_expr
*expr
)
401 const struct nft_reject
*priv
= nft_expr_priv(expr
);
403 if (nla_put_be32(skb
, NFTA_REJECT_TYPE
, htonl(priv
->type
)))
404 goto nla_put_failure
;
406 switch (priv
->type
) {
407 case NFT_REJECT_ICMP_UNREACH
:
408 case NFT_REJECT_ICMPX_UNREACH
:
409 if (nla_put_u8(skb
, NFTA_REJECT_ICMP_CODE
, priv
->icmp_code
))
410 goto nla_put_failure
;
422 static struct nft_expr_type nft_reject_bridge_type
;
423 static const struct nft_expr_ops nft_reject_bridge_ops
= {
424 .type
= &nft_reject_bridge_type
,
425 .size
= NFT_EXPR_SIZE(sizeof(struct nft_reject
)),
426 .eval
= nft_reject_bridge_eval
,
427 .init
= nft_reject_bridge_init
,
428 .dump
= nft_reject_bridge_dump
,
429 .validate
= nft_reject_bridge_validate
,
432 static struct nft_expr_type nft_reject_bridge_type __read_mostly
= {
433 .family
= NFPROTO_BRIDGE
,
435 .ops
= &nft_reject_bridge_ops
,
436 .policy
= nft_reject_policy
,
437 .maxattr
= NFTA_REJECT_MAX
,
438 .owner
= THIS_MODULE
,
441 static int __init
nft_reject_bridge_module_init(void)
443 return nft_register_expr(&nft_reject_bridge_type
);
446 static void __exit
nft_reject_bridge_module_exit(void)
448 nft_unregister_expr(&nft_reject_bridge_type
);
451 module_init(nft_reject_bridge_module_init
);
452 module_exit(nft_reject_bridge_module_exit
);
454 MODULE_LICENSE("GPL");
455 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
456 MODULE_ALIAS_NFT_AF_EXPR(AF_BRIDGE
, "reject");