blob: bb5d79e0beeaceccb5118c61dd7c034a9626521b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ebt_dnat
3 *
4 * Authors:
5 * Bart De Schuymer <bdschuym@pandora.be>
6 *
7 * June, 2002
8 *
9 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <net/sock.h>
Jan Engelhardt18219d32008-10-08 11:35:13 +020012#include <linux/netfilter.h>
13#include <linux/netfilter/x_tables.h>
14#include <linux/netfilter_bridge/ebtables.h>
15#include <linux/netfilter_bridge/ebt_nat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +020017static unsigned int
Jan Engelhardt7eb35582008-10-08 11:35:19 +020018ebt_dnat_tg(struct sk_buff *skb, const struct xt_target_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070019{
Jan Engelhardt7eb35582008-10-08 11:35:19 +020020 const struct ebt_nat_info *info = par->targinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Joonwoo Parkeb1197b2008-02-19 17:18:47 -080022 if (!skb_make_writable(skb, 0))
Joonwoo Park1b04ab42008-02-23 20:22:27 -080023 return EBT_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Herbert Xu3db05fe2007-10-15 00:53:15 -070025 memcpy(eth_hdr(skb)->h_dest, info->mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 return info->target;
27}
28
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +020029static bool
30ebt_dnat_tg_check(const char *tablename, const void *entry,
31 const struct xt_target *target, void *data,
32 unsigned int hookmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -080034 const struct ebt_nat_info *info = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36 if (BASE_CHAIN && info->target == EBT_RETURN)
Jan Engelhardt19eda872008-10-08 11:35:13 +020037 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 CLEAR_BASE_CHAIN_BIT;
39 if ( (strcmp(tablename, "nat") ||
40 (hookmask & ~((1 << NF_BR_PRE_ROUTING) | (1 << NF_BR_LOCAL_OUT)))) &&
41 (strcmp(tablename, "broute") || hookmask & ~(1 << NF_BR_BROUTING)) )
Jan Engelhardt19eda872008-10-08 11:35:13 +020042 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 if (INVALID_TARGET)
Jan Engelhardt19eda872008-10-08 11:35:13 +020044 return false;
45 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
Jan Engelhardt043ef462008-10-08 11:35:15 +020048static struct xt_target ebt_dnat_tg_reg __read_mostly = {
49 .name = "dnat",
Jan Engelhardt001a18d2008-10-08 11:35:14 +020050 .revision = 0,
51 .family = NFPROTO_BRIDGE,
Jan Engelhardtf2ff5252008-10-08 11:35:15 +020052 .hooks = (1 << NF_BR_NUMHOOKS) | (1 << NF_BR_PRE_ROUTING) |
53 (1 << NF_BR_LOCAL_OUT) | (1 << NF_BR_BROUTING),
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +020054 .target = ebt_dnat_tg,
55 .checkentry = ebt_dnat_tg_check,
Jan Engelhardt18219d32008-10-08 11:35:13 +020056 .targetsize = XT_ALIGN(sizeof(struct ebt_nat_info)),
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 .me = THIS_MODULE,
58};
59
Andrew Morton65b4b4e2006-03-28 16:37:06 -080060static int __init ebt_dnat_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Jan Engelhardt043ef462008-10-08 11:35:15 +020062 return xt_register_target(&ebt_dnat_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
Andrew Morton65b4b4e2006-03-28 16:37:06 -080065static void __exit ebt_dnat_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Jan Engelhardt043ef462008-10-08 11:35:15 +020067 xt_unregister_target(&ebt_dnat_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Andrew Morton65b4b4e2006-03-28 16:37:06 -080070module_init(ebt_dnat_init);
71module_exit(ebt_dnat_fini);
Jan Engelhardtf776c4c2008-01-31 04:00:30 -080072MODULE_DESCRIPTION("Ebtables: Destination MAC address translation");
Linus Torvalds1da177e2005-04-16 15:20:36 -070073MODULE_LICENSE("GPL");