]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/ipv4/netfilter/nf_nat_redirect_ipv4.c
netfilter: nf_tables_bridge: set the pktinfo for IPv4/IPv6 traffic
[mirror_ubuntu-jammy-kernel.git] / net / ipv4 / netfilter / nf_nat_redirect_ipv4.c
CommitLineData
8b13eddf
AB
1/*
2 * (C) 1999-2001 Paul `Rusty' Russell
3 * (C) 2002-2006 Netfilter Core Team <coreteam@netfilter.org>
4 * Copyright (c) 2011 Patrick McHardy <kaber@trash.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Based on Rusty Russell's IPv4 REDIRECT target. Development of IPv6
11 * NAT funded by Astaro.
12 */
13
14#include <linux/if.h>
15#include <linux/inetdevice.h>
16#include <linux/ip.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/netdevice.h>
20#include <linux/netfilter.h>
21#include <linux/types.h>
22#include <linux/netfilter_ipv4.h>
23#include <linux/netfilter/x_tables.h>
24#include <net/addrconf.h>
25#include <net/checksum.h>
26#include <net/protocol.h>
27#include <net/netfilter/nf_nat.h>
28#include <net/netfilter/ipv4/nf_nat_redirect.h>
29
30unsigned int
31nf_nat_redirect_ipv4(struct sk_buff *skb,
32 const struct nf_nat_ipv4_multi_range_compat *mr,
33 unsigned int hooknum)
34{
35 struct nf_conn *ct;
36 enum ip_conntrack_info ctinfo;
37 __be32 newdst;
38 struct nf_nat_range newrange;
39
40 NF_CT_ASSERT(hooknum == NF_INET_PRE_ROUTING ||
41 hooknum == NF_INET_LOCAL_OUT);
42
43 ct = nf_ct_get(skb, &ctinfo);
44 NF_CT_ASSERT(ct && (ctinfo == IP_CT_NEW || ctinfo == IP_CT_RELATED));
45
46 /* Local packets: make them go to loopback */
47 if (hooknum == NF_INET_LOCAL_OUT) {
48 newdst = htonl(0x7F000001);
49 } else {
50 struct in_device *indev;
51 struct in_ifaddr *ifa;
52
53 newdst = 0;
54
55 rcu_read_lock();
56 indev = __in_dev_get_rcu(skb->dev);
57 if (indev != NULL) {
58 ifa = indev->ifa_list;
59 newdst = ifa->ifa_local;
60 }
61 rcu_read_unlock();
62
63 if (!newdst)
64 return NF_DROP;
65 }
66
67 /* Transfer from original range. */
68 memset(&newrange.min_addr, 0, sizeof(newrange.min_addr));
69 memset(&newrange.max_addr, 0, sizeof(newrange.max_addr));
70 newrange.flags = mr->range[0].flags | NF_NAT_RANGE_MAP_IPS;
71 newrange.min_addr.ip = newdst;
72 newrange.max_addr.ip = newdst;
73 newrange.min_proto = mr->range[0].min;
74 newrange.max_proto = mr->range[0].max;
75
76 /* Hand modified range to generic setup. */
77 return nf_nat_setup_info(ct, &newrange, NF_NAT_MANIP_DST);
78}
79EXPORT_SYMBOL_GPL(nf_nat_redirect_ipv4);
80
81MODULE_LICENSE("GPL");
82MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");