1 /* IPv4 specific functions of netfilter core */
2 #include <linux/kernel.h>
3 #include <linux/netfilter.h>
4 #include <linux/netfilter_ipv4.h>
10 /* route_me_harder function, used by iptable_nat, iptable_mangle + ip_queue */
11 int ip_route_me_harder(struct sk_buff
**pskb
, unsigned addr_type
)
13 struct iphdr
*iph
= (*pskb
)->nh
.iph
;
16 struct dst_entry
*odst
;
19 if (addr_type
== RTN_UNSPEC
)
20 addr_type
= inet_addr_type(iph
->saddr
);
22 /* some non-standard hacks like ipt_REJECT.c:send_reset() can cause
23 * packets with foreign saddr to appear on the NF_IP_LOCAL_OUT hook.
25 if (addr_type
== RTN_LOCAL
) {
26 fl
.nl_u
.ip4_u
.daddr
= iph
->daddr
;
27 fl
.nl_u
.ip4_u
.saddr
= iph
->saddr
;
28 fl
.nl_u
.ip4_u
.tos
= RT_TOS(iph
->tos
);
29 fl
.oif
= (*pskb
)->sk
? (*pskb
)->sk
->sk_bound_dev_if
: 0;
30 #ifdef CONFIG_IP_ROUTE_FWMARK
31 fl
.nl_u
.ip4_u
.fwmark
= (*pskb
)->mark
;
33 if (ip_route_output_key(&rt
, &fl
) != 0)
37 dst_release((*pskb
)->dst
);
38 (*pskb
)->dst
= &rt
->u
.dst
;
40 /* non-local src, find valid iif to satisfy
41 * rp-filter when calling ip_route_input. */
42 fl
.nl_u
.ip4_u
.daddr
= iph
->saddr
;
43 if (ip_route_output_key(&rt
, &fl
) != 0)
47 if (ip_route_input(*pskb
, iph
->daddr
, iph
->saddr
,
48 RT_TOS(iph
->tos
), rt
->u
.dst
.dev
) != 0) {
49 dst_release(&rt
->u
.dst
);
52 dst_release(&rt
->u
.dst
);
56 if ((*pskb
)->dst
->error
)
60 if (!(IPCB(*pskb
)->flags
& IPSKB_XFRM_TRANSFORMED
) &&
61 xfrm_decode_session(*pskb
, &fl
, AF_INET
) == 0)
62 if (xfrm_lookup(&(*pskb
)->dst
, &fl
, (*pskb
)->sk
, 0))
66 /* Change in oif may mean change in hh_len. */
67 hh_len
= (*pskb
)->dst
->dev
->hard_header_len
;
68 if (skb_headroom(*pskb
) < hh_len
) {
71 nskb
= skb_realloc_headroom(*pskb
, hh_len
);
75 skb_set_owner_w(nskb
, (*pskb
)->sk
);
82 EXPORT_SYMBOL(ip_route_me_harder
);
85 int ip_xfrm_me_harder(struct sk_buff
**pskb
)
89 struct dst_entry
*dst
;
91 if (IPCB(*pskb
)->flags
& IPSKB_XFRM_TRANSFORMED
)
93 if (xfrm_decode_session(*pskb
, &fl
, AF_INET
) < 0)
98 dst
= ((struct xfrm_dst
*)dst
)->route
;
101 if (xfrm_lookup(&dst
, &fl
, (*pskb
)->sk
, 0) < 0)
104 dst_release((*pskb
)->dst
);
107 /* Change in oif may mean change in hh_len. */
108 hh_len
= (*pskb
)->dst
->dev
->hard_header_len
;
109 if (skb_headroom(*pskb
) < hh_len
) {
110 struct sk_buff
*nskb
;
112 nskb
= skb_realloc_headroom(*pskb
, hh_len
);
116 skb_set_owner_w(nskb
, (*pskb
)->sk
);
122 EXPORT_SYMBOL(ip_xfrm_me_harder
);
125 void (*ip_nat_decode_session
)(struct sk_buff
*, struct flowi
*);
126 EXPORT_SYMBOL(ip_nat_decode_session
);
129 * Extra routing may needed on local out, as the QUEUE target never
130 * returns control to the table.
139 static void nf_ip_saveroute(const struct sk_buff
*skb
, struct nf_info
*info
)
141 struct ip_rt_info
*rt_info
= nf_info_reroute(info
);
143 if (info
->hook
== NF_IP_LOCAL_OUT
) {
144 const struct iphdr
*iph
= skb
->nh
.iph
;
146 rt_info
->tos
= iph
->tos
;
147 rt_info
->daddr
= iph
->daddr
;
148 rt_info
->saddr
= iph
->saddr
;
152 static int nf_ip_reroute(struct sk_buff
**pskb
, const struct nf_info
*info
)
154 const struct ip_rt_info
*rt_info
= nf_info_reroute(info
);
156 if (info
->hook
== NF_IP_LOCAL_OUT
) {
157 struct iphdr
*iph
= (*pskb
)->nh
.iph
;
159 if (!(iph
->tos
== rt_info
->tos
160 && iph
->daddr
== rt_info
->daddr
161 && iph
->saddr
== rt_info
->saddr
))
162 return ip_route_me_harder(pskb
, RTN_UNSPEC
);
167 unsigned int nf_ip_checksum(struct sk_buff
*skb
, unsigned int hook
,
168 unsigned int dataoff
, u_int8_t protocol
)
170 struct iphdr
*iph
= skb
->nh
.iph
;
171 unsigned int csum
= 0;
173 switch (skb
->ip_summed
) {
174 case CHECKSUM_COMPLETE
:
175 if (hook
!= NF_IP_PRE_ROUTING
&& hook
!= NF_IP_LOCAL_IN
)
177 if ((protocol
== 0 && !(u16
)csum_fold(skb
->csum
)) ||
178 !csum_tcpudp_magic(iph
->saddr
, iph
->daddr
,
179 skb
->len
- dataoff
, protocol
,
181 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
189 skb
->csum
= csum_tcpudp_nofold(iph
->saddr
, iph
->daddr
,
192 csum
= __skb_checksum_complete(skb
);
197 EXPORT_SYMBOL(nf_ip_checksum
);
199 static struct nf_afinfo nf_ip_afinfo
= {
201 .checksum
= nf_ip_checksum
,
202 .saveroute
= nf_ip_saveroute
,
203 .reroute
= nf_ip_reroute
,
204 .route_key_size
= sizeof(struct ip_rt_info
),
207 static int ipv4_netfilter_init(void)
209 return nf_register_afinfo(&nf_ip_afinfo
);
212 static void ipv4_netfilter_fini(void)
214 nf_unregister_afinfo(&nf_ip_afinfo
);
217 module_init(ipv4_netfilter_init
);
218 module_exit(ipv4_netfilter_fini
);