treewide: Add SPDX license identifier for more missed files
[linux-block.git] / net / bridge / netfilter / ebt_snat.c
CommitLineData
09c434b8 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * ebt_snat
4 *
5 * Authors:
6 * Bart De Schuymer <bdschuym@pandora.be>
7 *
8 * June, 2002
9 *
10 */
1da177e4
LT
11#include <linux/module.h>
12#include <net/sock.h>
d12cdc3c
BDS
13#include <linux/if_arp.h>
14#include <net/arp.h>
18219d3f
JE
15#include <linux/netfilter.h>
16#include <linux/netfilter/x_tables.h>
17#include <linux/netfilter_bridge/ebtables.h>
18#include <linux/netfilter_bridge/ebt_nat.h>
1da177e4 19
2d06d4a5 20static unsigned int
4b560b44 21ebt_snat_tg(struct sk_buff *skb, const struct xt_action_param *par)
1da177e4 22{
7eb35586 23 const struct ebt_nat_info *info = par->targinfo;
1da177e4 24
eb1197bc 25 if (!skb_make_writable(skb, 0))
1b04ab45 26 return EBT_DROP;
1da177e4 27
04091142 28 ether_addr_copy(eth_hdr(skb)->h_source, info->mac);
d12cdc3c 29 if (!(info->target & NAT_ARP_BIT) &&
3db05fea 30 eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) {
abfdf1c4
JE
31 const struct arphdr *ap;
32 struct arphdr _ah;
d12cdc3c 33
3db05fea 34 ap = skb_header_pointer(skb, 0, sizeof(_ah), &_ah);
d12cdc3c
BDS
35 if (ap == NULL)
36 return EBT_DROP;
37 if (ap->ar_hln != ETH_ALEN)
38 goto out;
31a5b837 39 if (skb_store_bits(skb, sizeof(_ah), info->mac, ETH_ALEN))
d12cdc3c
BDS
40 return EBT_DROP;
41 }
42out:
43 return info->target | ~EBT_VERDICT_BITS;
1da177e4
LT
44}
45
135367b8 46static int ebt_snat_tg_check(const struct xt_tgchk_param *par)
1da177e4 47{
af5d6dc2 48 const struct ebt_nat_info *info = par->targinfo;
d12cdc3c 49 int tmp;
1da177e4 50
d12cdc3c
BDS
51 tmp = info->target | ~EBT_VERDICT_BITS;
52 if (BASE_CHAIN && tmp == EBT_RETURN)
d6b00a53 53 return -EINVAL;
d12cdc3c 54
e15b9c50 55 if (ebt_invalid_target(tmp))
d6b00a53 56 return -EINVAL;
d12cdc3c
BDS
57 tmp = info->target | EBT_VERDICT_BITS;
58 if ((tmp & ~NAT_ARP_BIT) != ~NAT_ARP_BIT)
d6b00a53
JE
59 return -EINVAL;
60 return 0;
1da177e4
LT
61}
62
043ef46c
JE
63static struct xt_target ebt_snat_tg_reg __read_mostly = {
64 .name = "snat",
001a18d3
JE
65 .revision = 0,
66 .family = NFPROTO_BRIDGE,
f2ff525c
JE
67 .table = "nat",
68 .hooks = (1 << NF_BR_NUMHOOKS) | (1 << NF_BR_POST_ROUTING),
2d06d4a5
JE
69 .target = ebt_snat_tg,
70 .checkentry = ebt_snat_tg_check,
fc0e3df4 71 .targetsize = sizeof(struct ebt_nat_info),
1da177e4
LT
72 .me = THIS_MODULE,
73};
74
65b4b4e8 75static int __init ebt_snat_init(void)
1da177e4 76{
043ef46c 77 return xt_register_target(&ebt_snat_tg_reg);
1da177e4
LT
78}
79
65b4b4e8 80static void __exit ebt_snat_fini(void)
1da177e4 81{
043ef46c 82 xt_unregister_target(&ebt_snat_tg_reg);
1da177e4
LT
83}
84
65b4b4e8
AM
85module_init(ebt_snat_init);
86module_exit(ebt_snat_fini);
f776c4cd 87MODULE_DESCRIPTION("Ebtables: Source MAC address translation");
1da177e4 88MODULE_LICENSE("GPL");