xref: /openbmc/linux/net/bridge/netfilter/ebt_dnat.c (revision cfba5de9)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  ebt_dnat
4  *
5  *	Authors:
6  *	Bart De Schuymer <bdschuym@pandora.be>
7  *
8  *  June, 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_nat.h>
18 
19 static unsigned int
20 ebt_dnat_tg(struct sk_buff *skb, const struct xt_action_param *par)
21 {
22 	const struct ebt_nat_info *info = par->targinfo;
23 	struct net_device *dev;
24 
25 	if (skb_ensure_writable(skb, ETH_ALEN))
26 		return EBT_DROP;
27 
28 	ether_addr_copy(eth_hdr(skb)->h_dest, info->mac);
29 
30 	if (is_multicast_ether_addr(info->mac)) {
31 		if (is_broadcast_ether_addr(info->mac))
32 			skb->pkt_type = PACKET_BROADCAST;
33 		else
34 			skb->pkt_type = PACKET_MULTICAST;
35 	} else {
36 		if (xt_hooknum(par) != NF_BR_BROUTING)
37 			dev = br_port_get_rcu(xt_in(par))->br->dev;
38 		else
39 			dev = xt_in(par);
40 
41 		if (ether_addr_equal(info->mac, dev->dev_addr))
42 			skb->pkt_type = PACKET_HOST;
43 		else
44 			skb->pkt_type = PACKET_OTHERHOST;
45 	}
46 
47 	return info->target;
48 }
49 
50 static int ebt_dnat_tg_check(const struct xt_tgchk_param *par)
51 {
52 	const struct ebt_nat_info *info = par->targinfo;
53 	unsigned int hook_mask;
54 
55 	if (BASE_CHAIN && info->target == EBT_RETURN)
56 		return -EINVAL;
57 
58 	hook_mask = par->hook_mask & ~(1 << NF_BR_NUMHOOKS);
59 	if ((strcmp(par->table, "nat") != 0 ||
60 	    (hook_mask & ~((1 << NF_BR_PRE_ROUTING) |
61 	    (1 << NF_BR_LOCAL_OUT)))) &&
62 	    (strcmp(par->table, "broute") != 0 ||
63 	    hook_mask & ~(1 << NF_BR_BROUTING)))
64 		return -EINVAL;
65 	if (ebt_invalid_target(info->target))
66 		return -EINVAL;
67 	return 0;
68 }
69 
70 static struct xt_target ebt_dnat_tg_reg __read_mostly = {
71 	.name		= "dnat",
72 	.revision	= 0,
73 	.family		= NFPROTO_BRIDGE,
74 	.hooks		= (1 << NF_BR_NUMHOOKS) | (1 << NF_BR_PRE_ROUTING) |
75 			  (1 << NF_BR_LOCAL_OUT) | (1 << NF_BR_BROUTING),
76 	.target		= ebt_dnat_tg,
77 	.checkentry	= ebt_dnat_tg_check,
78 	.targetsize	= sizeof(struct ebt_nat_info),
79 	.me		= THIS_MODULE,
80 };
81 
82 static int __init ebt_dnat_init(void)
83 {
84 	return xt_register_target(&ebt_dnat_tg_reg);
85 }
86 
87 static void __exit ebt_dnat_fini(void)
88 {
89 	xt_unregister_target(&ebt_dnat_tg_reg);
90 }
91 
92 module_init(ebt_dnat_init);
93 module_exit(ebt_dnat_fini);
94 MODULE_DESCRIPTION("Ebtables: Destination MAC address translation");
95 MODULE_LICENSE("GPL");
96