]>
Commit | Line | Data |
---|---|---|
2cc7d573 | 1 | /* IPv4 specific functions of netfilter core */ |
020b4c12 | 2 | |
2cc7d573 | 3 | #include <linux/config.h> |
020b4c12 HW |
4 | #ifdef CONFIG_NETFILTER |
5 | ||
020b4c12 HW |
6 | #include <linux/kernel.h> |
7 | #include <linux/netfilter.h> | |
2cc7d573 | 8 | #include <linux/netfilter_ipv4.h> |
020b4c12 | 9 | |
3e3850e9 | 10 | #include <linux/ip.h> |
020b4c12 HW |
11 | #include <linux/tcp.h> |
12 | #include <linux/udp.h> | |
13 | #include <linux/icmp.h> | |
14 | #include <net/route.h> | |
3e3850e9 PM |
15 | #include <net/xfrm.h> |
16 | #include <net/ip.h> | |
020b4c12 HW |
17 | |
18 | /* route_me_harder function, used by iptable_nat, iptable_mangle + ip_queue */ | |
19 | int ip_route_me_harder(struct sk_buff **pskb) | |
20 | { | |
21 | struct iphdr *iph = (*pskb)->nh.iph; | |
22 | struct rtable *rt; | |
23 | struct flowi fl = {}; | |
24 | struct dst_entry *odst; | |
25 | unsigned int hh_len; | |
26 | ||
27 | /* some non-standard hacks like ipt_REJECT.c:send_reset() can cause | |
28 | * packets with foreign saddr to appear on the NF_IP_LOCAL_OUT hook. | |
29 | */ | |
30 | if (inet_addr_type(iph->saddr) == RTN_LOCAL) { | |
31 | fl.nl_u.ip4_u.daddr = iph->daddr; | |
32 | fl.nl_u.ip4_u.saddr = iph->saddr; | |
33 | fl.nl_u.ip4_u.tos = RT_TOS(iph->tos); | |
34 | fl.oif = (*pskb)->sk ? (*pskb)->sk->sk_bound_dev_if : 0; | |
35 | #ifdef CONFIG_IP_ROUTE_FWMARK | |
36 | fl.nl_u.ip4_u.fwmark = (*pskb)->nfmark; | |
37 | #endif | |
020b4c12 HW |
38 | if (ip_route_output_key(&rt, &fl) != 0) |
39 | return -1; | |
40 | ||
41 | /* Drop old route. */ | |
42 | dst_release((*pskb)->dst); | |
43 | (*pskb)->dst = &rt->u.dst; | |
44 | } else { | |
45 | /* non-local src, find valid iif to satisfy | |
46 | * rp-filter when calling ip_route_input. */ | |
47 | fl.nl_u.ip4_u.daddr = iph->saddr; | |
48 | if (ip_route_output_key(&rt, &fl) != 0) | |
49 | return -1; | |
50 | ||
51 | odst = (*pskb)->dst; | |
52 | if (ip_route_input(*pskb, iph->daddr, iph->saddr, | |
53 | RT_TOS(iph->tos), rt->u.dst.dev) != 0) { | |
54 | dst_release(&rt->u.dst); | |
55 | return -1; | |
56 | } | |
57 | dst_release(&rt->u.dst); | |
58 | dst_release(odst); | |
59 | } | |
60 | ||
61 | if ((*pskb)->dst->error) | |
62 | return -1; | |
63 | ||
3e3850e9 PM |
64 | #ifdef CONFIG_XFRM |
65 | if (!(IPCB(*pskb)->flags & IPSKB_XFRM_TRANSFORMED) && | |
66 | xfrm_decode_session(*pskb, &fl, AF_INET) == 0) | |
67 | if (xfrm_lookup(&(*pskb)->dst, &fl, (*pskb)->sk, 0)) | |
68 | return -1; | |
69 | #endif | |
70 | ||
020b4c12 HW |
71 | /* Change in oif may mean change in hh_len. */ |
72 | hh_len = (*pskb)->dst->dev->hard_header_len; | |
73 | if (skb_headroom(*pskb) < hh_len) { | |
74 | struct sk_buff *nskb; | |
75 | ||
76 | nskb = skb_realloc_headroom(*pskb, hh_len); | |
77 | if (!nskb) | |
78 | return -1; | |
79 | if ((*pskb)->sk) | |
80 | skb_set_owner_w(nskb, (*pskb)->sk); | |
81 | kfree_skb(*pskb); | |
82 | *pskb = nskb; | |
83 | } | |
84 | ||
85 | return 0; | |
86 | } | |
87 | EXPORT_SYMBOL(ip_route_me_harder); | |
2cc7d573 HW |
88 | |
89 | /* | |
90 | * Extra routing may needed on local out, as the QUEUE target never | |
91 | * returns control to the table. | |
92 | */ | |
93 | ||
94 | struct ip_rt_info { | |
95 | u_int32_t daddr; | |
96 | u_int32_t saddr; | |
97 | u_int8_t tos; | |
98 | }; | |
99 | ||
100 | static void queue_save(const struct sk_buff *skb, struct nf_info *info) | |
101 | { | |
102 | struct ip_rt_info *rt_info = nf_info_reroute(info); | |
103 | ||
104 | if (info->hook == NF_IP_LOCAL_OUT) { | |
105 | const struct iphdr *iph = skb->nh.iph; | |
106 | ||
107 | rt_info->tos = iph->tos; | |
108 | rt_info->daddr = iph->daddr; | |
109 | rt_info->saddr = iph->saddr; | |
110 | } | |
111 | } | |
112 | ||
113 | static int queue_reroute(struct sk_buff **pskb, const struct nf_info *info) | |
114 | { | |
115 | const struct ip_rt_info *rt_info = nf_info_reroute(info); | |
116 | ||
117 | if (info->hook == NF_IP_LOCAL_OUT) { | |
118 | struct iphdr *iph = (*pskb)->nh.iph; | |
119 | ||
120 | if (!(iph->tos == rt_info->tos | |
121 | && iph->daddr == rt_info->daddr | |
122 | && iph->saddr == rt_info->saddr)) | |
123 | return ip_route_me_harder(pskb); | |
124 | } | |
125 | return 0; | |
126 | } | |
127 | ||
128 | static struct nf_queue_rerouter ip_reroute = { | |
129 | .rer_size = sizeof(struct ip_rt_info), | |
130 | .save = queue_save, | |
131 | .reroute = queue_reroute, | |
132 | }; | |
133 | ||
134 | static int init(void) | |
135 | { | |
136 | return nf_register_queue_rerouter(PF_INET, &ip_reroute); | |
137 | } | |
138 | ||
139 | static void fini(void) | |
140 | { | |
141 | nf_unregister_queue_rerouter(PF_INET); | |
142 | } | |
143 | ||
144 | module_init(init); | |
145 | module_exit(fini); | |
146 | ||
020b4c12 | 147 | #endif /* CONFIG_NETFILTER */ |