4c628108bcdabd5f13d55939513ea4c6ad978262
[firefly-linux-kernel-4.4.55.git] / net / bridge / netfilter / ebt_redirect.c
1 /*
2  *  ebt_redirect
3  *
4  *      Authors:
5  *      Bart De Schuymer <bdschuym@pandora.be>
6  *
7  *  April, 2002
8  *
9  */
10 #include <linux/module.h>
11 #include <net/sock.h>
12 #include "../br_private.h"
13 #include <linux/netfilter.h>
14 #include <linux/netfilter/x_tables.h>
15 #include <linux/netfilter_bridge/ebtables.h>
16 #include <linux/netfilter_bridge/ebt_redirect.h>
17
18 static unsigned int ebt_target_redirect(struct sk_buff *skb,
19    unsigned int hooknr,
20    const struct net_device *in, const struct net_device *out,
21    const void *data, unsigned int datalen)
22 {
23         const struct ebt_redirect_info *info = data;
24
25         if (!skb_make_writable(skb, 0))
26                 return EBT_DROP;
27
28         if (hooknr != NF_BR_BROUTING)
29                 memcpy(eth_hdr(skb)->h_dest,
30                        in->br_port->br->dev->dev_addr, ETH_ALEN);
31         else
32                 memcpy(eth_hdr(skb)->h_dest, in->dev_addr, ETH_ALEN);
33         skb->pkt_type = PACKET_HOST;
34         return info->target;
35 }
36
37 static bool ebt_target_redirect_check(const char *tablename, unsigned int hookmask,
38    const struct ebt_entry *e, void *data, unsigned int datalen)
39 {
40         const struct ebt_redirect_info *info = data;
41
42         if (BASE_CHAIN && info->target == EBT_RETURN)
43                 return false;
44         CLEAR_BASE_CHAIN_BIT;
45         if ( (strcmp(tablename, "nat") || hookmask & ~(1 << NF_BR_PRE_ROUTING)) &&
46              (strcmp(tablename, "broute") || hookmask & ~(1 << NF_BR_BROUTING)) )
47                 return false;
48         if (INVALID_TARGET)
49                 return false;
50         return true;
51 }
52
53 static struct ebt_target redirect_target __read_mostly = {
54         .name           = EBT_REDIRECT_TARGET,
55         .revision       = 0,
56         .family         = NFPROTO_BRIDGE,
57         .target         = ebt_target_redirect,
58         .check          = ebt_target_redirect_check,
59         .targetsize     = XT_ALIGN(sizeof(struct ebt_redirect_info)),
60         .me             = THIS_MODULE,
61 };
62
63 static int __init ebt_redirect_init(void)
64 {
65         return ebt_register_target(&redirect_target);
66 }
67
68 static void __exit ebt_redirect_fini(void)
69 {
70         ebt_unregister_target(&redirect_target);
71 }
72
73 module_init(ebt_redirect_init);
74 module_exit(ebt_redirect_fini);
75 MODULE_DESCRIPTION("Ebtables: Packet redirection to localhost");
76 MODULE_LICENSE("GPL");