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 <linux/netfilter_ipv6.h>
25 #include "../br_private.h"
27 static void nft_reject_br_push_etherhdr(struct sk_buff
*oldskb
,
32 eth
= (struct ethhdr
*)skb_push(nskb
, ETH_HLEN
);
33 skb_reset_mac_header(nskb
);
34 ether_addr_copy(eth
->h_source
, eth_hdr(oldskb
)->h_dest
);
35 ether_addr_copy(eth
->h_dest
, eth_hdr(oldskb
)->h_source
);
36 eth
->h_proto
= eth_hdr(oldskb
)->h_proto
;
37 skb_pull(nskb
, ETH_HLEN
);
40 /* We cannot use oldskb->dev, it can be either bridge device (NF_BRIDGE INPUT)
41 * or the bridge port (NF_BRIDGE PREROUTING).
43 static void nft_reject_br_send_v4_tcp_reset(struct sk_buff
*oldskb
,
44 const struct net_device
*dev
,
49 const struct tcphdr
*oth
;
52 if (!nft_bridge_iphdr_validate(oldskb
))
55 oth
= nf_reject_ip_tcphdr_get(oldskb
, &_oth
, hook
);
59 nskb
= alloc_skb(sizeof(struct iphdr
) + sizeof(struct tcphdr
) +
60 LL_MAX_HEADER
, GFP_ATOMIC
);
64 skb_reserve(nskb
, LL_MAX_HEADER
);
65 niph
= nf_reject_iphdr_put(nskb
, oldskb
, IPPROTO_TCP
,
66 sysctl_ip_default_ttl
);
67 nf_reject_ip_tcphdr_put(nskb
, oldskb
, oth
);
68 niph
->ttl
= sysctl_ip_default_ttl
;
69 niph
->tot_len
= htons(nskb
->len
);
72 nft_reject_br_push_etherhdr(oldskb
, nskb
);
74 br_deliver(br_port_get_rcu(dev
), nskb
);
77 static void nft_reject_br_send_v4_unreach(struct sk_buff
*oldskb
,
78 const struct net_device
*dev
,
83 struct icmphdr
*icmph
;
89 if (oldskb
->csum_bad
|| !nft_bridge_iphdr_validate(oldskb
))
92 /* IP header checks: fragment. */
93 if (ip_hdr(oldskb
)->frag_off
& htons(IP_OFFSET
))
96 /* RFC says return as much as we can without exceeding 576 bytes. */
97 len
= min_t(unsigned int, 536, oldskb
->len
);
99 if (!pskb_may_pull(oldskb
, len
))
102 if (pskb_trim_rcsum(oldskb
, htons(ip_hdr(oldskb
)->tot_len
)))
105 if (ip_hdr(oldskb
)->protocol
== IPPROTO_TCP
||
106 ip_hdr(oldskb
)->protocol
== IPPROTO_UDP
)
107 proto
= ip_hdr(oldskb
)->protocol
;
111 if (!skb_csum_unnecessary(oldskb
) &&
112 nf_ip_checksum(oldskb
, hook
, ip_hdrlen(oldskb
), proto
))
115 nskb
= alloc_skb(sizeof(struct iphdr
) + sizeof(struct icmphdr
) +
116 LL_MAX_HEADER
+ len
, GFP_ATOMIC
);
120 skb_reserve(nskb
, LL_MAX_HEADER
);
121 niph
= nf_reject_iphdr_put(nskb
, oldskb
, IPPROTO_ICMP
,
122 sysctl_ip_default_ttl
);
124 skb_reset_transport_header(nskb
);
125 icmph
= (struct icmphdr
*)skb_put(nskb
, sizeof(struct icmphdr
));
126 memset(icmph
, 0, sizeof(*icmph
));
127 icmph
->type
= ICMP_DEST_UNREACH
;
130 payload
= skb_put(nskb
, len
);
131 memcpy(payload
, skb_network_header(oldskb
), len
);
133 csum
= csum_partial((void *)icmph
, len
+ sizeof(struct icmphdr
), 0);
134 icmph
->checksum
= csum_fold(csum
);
136 niph
->tot_len
= htons(nskb
->len
);
139 nft_reject_br_push_etherhdr(oldskb
, nskb
);
141 br_deliver(br_port_get_rcu(dev
), nskb
);
144 static void nft_reject_br_send_v6_tcp_reset(struct net
*net
,
145 struct sk_buff
*oldskb
,
146 const struct net_device
*dev
,
149 struct sk_buff
*nskb
;
150 const struct tcphdr
*oth
;
152 unsigned int otcplen
;
153 struct ipv6hdr
*nip6h
;
155 if (!nft_bridge_ip6hdr_validate(oldskb
))
158 oth
= nf_reject_ip6_tcphdr_get(oldskb
, &_oth
, &otcplen
, hook
);
162 nskb
= alloc_skb(sizeof(struct ipv6hdr
) + sizeof(struct tcphdr
) +
163 LL_MAX_HEADER
, GFP_ATOMIC
);
167 skb_reserve(nskb
, LL_MAX_HEADER
);
168 nip6h
= nf_reject_ip6hdr_put(nskb
, oldskb
, IPPROTO_TCP
,
169 net
->ipv6
.devconf_all
->hop_limit
);
170 nf_reject_ip6_tcphdr_put(nskb
, oldskb
, oth
, otcplen
);
171 nip6h
->payload_len
= htons(nskb
->len
- sizeof(struct ipv6hdr
));
173 nft_reject_br_push_etherhdr(oldskb
, nskb
);
175 br_deliver(br_port_get_rcu(dev
), nskb
);
178 static bool reject6_br_csum_ok(struct sk_buff
*skb
, int hook
)
180 const struct ipv6hdr
*ip6h
= ipv6_hdr(skb
);
183 u8 proto
= ip6h
->nexthdr
;
188 if (skb_csum_unnecessary(skb
))
191 if (ip6h
->payload_len
&&
192 pskb_trim_rcsum(skb
, ntohs(ip6h
->payload_len
) + sizeof(*ip6h
)))
195 thoff
= ipv6_skip_exthdr(skb
, ((u8
*)(ip6h
+1) - skb
->data
), &proto
, &fo
);
196 if (thoff
< 0 || thoff
>= skb
->len
|| (fo
& htons(~0x7)) != 0)
199 return nf_ip6_checksum(skb
, hook
, thoff
, proto
) == 0;
202 static void nft_reject_br_send_v6_unreach(struct net
*net
,
203 struct sk_buff
*oldskb
,
204 const struct net_device
*dev
,
207 struct sk_buff
*nskb
;
208 struct ipv6hdr
*nip6h
;
209 struct icmp6hdr
*icmp6h
;
213 if (!nft_bridge_ip6hdr_validate(oldskb
))
216 /* Include "As much of invoking packet as possible without the ICMPv6
217 * packet exceeding the minimum IPv6 MTU" in the ICMP payload.
219 len
= min_t(unsigned int, 1220, oldskb
->len
);
221 if (!pskb_may_pull(oldskb
, len
))
224 if (!reject6_br_csum_ok(oldskb
, hook
))
227 nskb
= alloc_skb(sizeof(struct iphdr
) + sizeof(struct icmp6hdr
) +
228 LL_MAX_HEADER
+ len
, GFP_ATOMIC
);
232 skb_reserve(nskb
, LL_MAX_HEADER
);
233 nip6h
= nf_reject_ip6hdr_put(nskb
, oldskb
, IPPROTO_ICMPV6
,
234 net
->ipv6
.devconf_all
->hop_limit
);
236 skb_reset_transport_header(nskb
);
237 icmp6h
= (struct icmp6hdr
*)skb_put(nskb
, sizeof(struct icmp6hdr
));
238 memset(icmp6h
, 0, sizeof(*icmp6h
));
239 icmp6h
->icmp6_type
= ICMPV6_DEST_UNREACH
;
240 icmp6h
->icmp6_code
= code
;
242 payload
= skb_put(nskb
, len
);
243 memcpy(payload
, skb_network_header(oldskb
), len
);
244 nip6h
->payload_len
= htons(nskb
->len
- sizeof(struct ipv6hdr
));
246 icmp6h
->icmp6_cksum
=
247 csum_ipv6_magic(&nip6h
->saddr
, &nip6h
->daddr
,
248 nskb
->len
- sizeof(struct ipv6hdr
),
251 nskb
->len
- sizeof(struct ipv6hdr
),
254 nft_reject_br_push_etherhdr(oldskb
, nskb
);
256 br_deliver(br_port_get_rcu(dev
), nskb
);
259 static void nft_reject_bridge_eval(const struct nft_expr
*expr
,
260 struct nft_data data
[NFT_REG_MAX
+ 1],
261 const struct nft_pktinfo
*pkt
)
263 struct nft_reject
*priv
= nft_expr_priv(expr
);
264 struct net
*net
= dev_net((pkt
->in
!= NULL
) ? pkt
->in
: pkt
->out
);
265 const unsigned char *dest
= eth_hdr(pkt
->skb
)->h_dest
;
267 if (is_broadcast_ether_addr(dest
) ||
268 is_multicast_ether_addr(dest
))
271 switch (eth_hdr(pkt
->skb
)->h_proto
) {
272 case htons(ETH_P_IP
):
273 switch (priv
->type
) {
274 case NFT_REJECT_ICMP_UNREACH
:
275 nft_reject_br_send_v4_unreach(pkt
->skb
, pkt
->in
,
279 case NFT_REJECT_TCP_RST
:
280 nft_reject_br_send_v4_tcp_reset(pkt
->skb
, pkt
->in
,
283 case NFT_REJECT_ICMPX_UNREACH
:
284 nft_reject_br_send_v4_unreach(pkt
->skb
, pkt
->in
,
286 nft_reject_icmp_code(priv
->icmp_code
));
290 case htons(ETH_P_IPV6
):
291 switch (priv
->type
) {
292 case NFT_REJECT_ICMP_UNREACH
:
293 nft_reject_br_send_v6_unreach(net
, pkt
->skb
, pkt
->in
,
297 case NFT_REJECT_TCP_RST
:
298 nft_reject_br_send_v6_tcp_reset(net
, pkt
->skb
, pkt
->in
,
301 case NFT_REJECT_ICMPX_UNREACH
:
302 nft_reject_br_send_v6_unreach(net
, pkt
->skb
, pkt
->in
,
304 nft_reject_icmpv6_code(priv
->icmp_code
));
309 /* No explicit way to reject this protocol, drop it. */
313 data
[NFT_REG_VERDICT
].verdict
= NF_DROP
;
316 static int nft_reject_bridge_validate(const struct nft_ctx
*ctx
,
317 const struct nft_expr
*expr
,
318 const struct nft_data
**data
)
320 return nft_chain_validate_hooks(ctx
->chain
, (1 << NF_BR_PRE_ROUTING
) |
321 (1 << NF_BR_LOCAL_IN
));
324 static int nft_reject_bridge_init(const struct nft_ctx
*ctx
,
325 const struct nft_expr
*expr
,
326 const struct nlattr
* const tb
[])
328 struct nft_reject
*priv
= nft_expr_priv(expr
);
331 err
= nft_reject_bridge_validate(ctx
, expr
, NULL
);
335 if (tb
[NFTA_REJECT_TYPE
] == NULL
)
338 priv
->type
= ntohl(nla_get_be32(tb
[NFTA_REJECT_TYPE
]));
339 switch (priv
->type
) {
340 case NFT_REJECT_ICMP_UNREACH
:
341 case NFT_REJECT_ICMPX_UNREACH
:
342 if (tb
[NFTA_REJECT_ICMP_CODE
] == NULL
)
345 icmp_code
= nla_get_u8(tb
[NFTA_REJECT_ICMP_CODE
]);
346 if (priv
->type
== NFT_REJECT_ICMPX_UNREACH
&&
347 icmp_code
> NFT_REJECT_ICMPX_MAX
)
350 priv
->icmp_code
= icmp_code
;
352 case NFT_REJECT_TCP_RST
:
360 static int nft_reject_bridge_dump(struct sk_buff
*skb
,
361 const struct nft_expr
*expr
)
363 const struct nft_reject
*priv
= nft_expr_priv(expr
);
365 if (nla_put_be32(skb
, NFTA_REJECT_TYPE
, htonl(priv
->type
)))
366 goto nla_put_failure
;
368 switch (priv
->type
) {
369 case NFT_REJECT_ICMP_UNREACH
:
370 case NFT_REJECT_ICMPX_UNREACH
:
371 if (nla_put_u8(skb
, NFTA_REJECT_ICMP_CODE
, priv
->icmp_code
))
372 goto nla_put_failure
;
382 static struct nft_expr_type nft_reject_bridge_type
;
383 static const struct nft_expr_ops nft_reject_bridge_ops
= {
384 .type
= &nft_reject_bridge_type
,
385 .size
= NFT_EXPR_SIZE(sizeof(struct nft_reject
)),
386 .eval
= nft_reject_bridge_eval
,
387 .init
= nft_reject_bridge_init
,
388 .dump
= nft_reject_bridge_dump
,
389 .validate
= nft_reject_bridge_validate
,
392 static struct nft_expr_type nft_reject_bridge_type __read_mostly
= {
393 .family
= NFPROTO_BRIDGE
,
395 .ops
= &nft_reject_bridge_ops
,
396 .policy
= nft_reject_policy
,
397 .maxattr
= NFTA_REJECT_MAX
,
398 .owner
= THIS_MODULE
,
401 static int __init
nft_reject_bridge_module_init(void)
403 return nft_register_expr(&nft_reject_bridge_type
);
406 static void __exit
nft_reject_bridge_module_exit(void)
408 nft_unregister_expr(&nft_reject_bridge_type
);
411 module_init(nft_reject_bridge_module_init
);
412 module_exit(nft_reject_bridge_module_exit
);
414 MODULE_LICENSE("GPL");
415 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
416 MODULE_ALIAS_NFT_AF_EXPR(AF_BRIDGE
, "reject");