1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * ebt_redirect
4 *
5 * Authors:
6 * Bart De Schuymer <bdschuym@pandora.be>
7 *
8 * April, 2002
9 *
10 */
11#include <linux/module.h>
12#include <net/sock.h>
13#include "../br_private.h"
14#include <linux/netfilter.h>
15#include <linux/netfilter/x_tables.h>
16#include <linux/netfilter_bridge/ebtables.h>
17#include <linux/netfilter_bridge/ebt_redirect.h>
18
19static unsigned int
20ebt_redirect_tg(struct sk_buff *skb, const struct xt_action_param *par)
21{
22 const struct ebt_redirect_info *info = par->targinfo;
23
24 if (skb_ensure_writable(skb, write_len: 0))
25 return EBT_DROP;
26
27 if (xt_hooknum(par) != NF_BR_BROUTING)
28 /* rcu_read_lock()ed by nf_hook_thresh */
29 ether_addr_copy(dst: eth_hdr(skb)->h_dest,
30 src: br_port_get_rcu(dev: xt_in(par))->br->dev->dev_addr);
31 else
32 ether_addr_copy(dst: eth_hdr(skb)->h_dest, src: xt_in(par)->dev_addr);
33 skb->pkt_type = PACKET_HOST;
34 return info->target;
35}
36
37static int ebt_redirect_tg_check(const struct xt_tgchk_param *par)
38{
39 const struct ebt_redirect_info *info = par->targinfo;
40 unsigned int hook_mask;
41
42 if (BASE_CHAIN && info->target == EBT_RETURN)
43 return -EINVAL;
44
45 hook_mask = par->hook_mask & ~(1 << NF_BR_NUMHOOKS);
46 if ((strcmp(par->table, "nat") != 0 ||
47 hook_mask & ~(1 << NF_BR_PRE_ROUTING)) &&
48 (strcmp(par->table, "broute") != 0 ||
49 hook_mask & ~(1 << NF_BR_BROUTING)))
50 return -EINVAL;
51 if (ebt_invalid_target(target: info->target))
52 return -EINVAL;
53 return 0;
54}
55
56static struct xt_target ebt_redirect_tg_reg __read_mostly = {
57 .name = "redirect",
58 .revision = 0,
59 .family = NFPROTO_BRIDGE,
60 .hooks = (1 << NF_BR_NUMHOOKS) | (1 << NF_BR_PRE_ROUTING) |
61 (1 << NF_BR_BROUTING),
62 .target = ebt_redirect_tg,
63 .checkentry = ebt_redirect_tg_check,
64 .targetsize = sizeof(struct ebt_redirect_info),
65 .me = THIS_MODULE,
66};
67
68static int __init ebt_redirect_init(void)
69{
70 return xt_register_target(target: &ebt_redirect_tg_reg);
71}
72
73static void __exit ebt_redirect_fini(void)
74{
75 xt_unregister_target(target: &ebt_redirect_tg_reg);
76}
77
78module_init(ebt_redirect_init);
79module_exit(ebt_redirect_fini);
80MODULE_DESCRIPTION("Ebtables: Packet redirection to localhost");
81MODULE_LICENSE("GPL");
82

source code of linux/net/bridge/netfilter/ebt_redirect.c