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 fl.mark = (*pskb)->mark;
31 if (ip_route_output_key(&rt, &fl) != 0)
35 dst_release((*pskb)->dst);
36 (*pskb)->dst = &rt->u.dst;
38 /* non-local src, find valid iif to satisfy
39 * rp-filter when calling ip_route_input. */
40 fl.nl_u.ip4_u.daddr = iph->saddr;
41 if (ip_route_output_key(&rt, &fl) != 0)
45 if (ip_route_input(*pskb, iph->daddr, iph->saddr,
46 RT_TOS(iph->tos), rt->u.dst.dev) != 0) {
47 dst_release(&rt->u.dst);
50 dst_release(&rt->u.dst);
54 if ((*pskb)->dst->error)
58 if (!(IPCB(*pskb)->flags & IPSKB_XFRM_TRANSFORMED) &&
59 xfrm_decode_session(*pskb, &fl, AF_INET) == 0)
60 if (xfrm_lookup(&(*pskb)->dst, &fl, (*pskb)->sk, 0))
64 /* Change in oif may mean change in hh_len. */
65 hh_len = (*pskb)->dst->dev->hard_header_len;
66 if (skb_headroom(*pskb) < hh_len) {
69 nskb = skb_realloc_headroom(*pskb, hh_len);
73 skb_set_owner_w(nskb, (*pskb)->sk);
80 EXPORT_SYMBOL(ip_route_me_harder);
83 int ip_xfrm_me_harder(struct sk_buff **pskb)
87 struct dst_entry *dst;
89 if (IPCB(*pskb)->flags & IPSKB_XFRM_TRANSFORMED)
91 if (xfrm_decode_session(*pskb, &fl, AF_INET) < 0)
96 dst = ((struct xfrm_dst *)dst)->route;
99 if (xfrm_lookup(&dst, &fl, (*pskb)->sk, 0) < 0)
102 dst_release((*pskb)->dst);
105 /* Change in oif may mean change in hh_len. */
106 hh_len = (*pskb)->dst->dev->hard_header_len;
107 if (skb_headroom(*pskb) < hh_len) {
108 struct sk_buff *nskb;
110 nskb = skb_realloc_headroom(*pskb, hh_len);
114 skb_set_owner_w(nskb, (*pskb)->sk);
120 EXPORT_SYMBOL(ip_xfrm_me_harder);
123 void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
124 EXPORT_SYMBOL(ip_nat_decode_session);
127 * Extra routing may needed on local out, as the QUEUE target never
128 * returns control to the table.
137 static void nf_ip_saveroute(const struct sk_buff *skb, struct nf_info *info)
139 struct ip_rt_info *rt_info = nf_info_reroute(info);
141 if (info->hook == NF_IP_LOCAL_OUT) {
142 const struct iphdr *iph = skb->nh.iph;
144 rt_info->tos = iph->tos;
145 rt_info->daddr = iph->daddr;
146 rt_info->saddr = iph->saddr;
150 static int nf_ip_reroute(struct sk_buff **pskb, const struct nf_info *info)
152 const struct ip_rt_info *rt_info = nf_info_reroute(info);
154 if (info->hook == NF_IP_LOCAL_OUT) {
155 struct iphdr *iph = (*pskb)->nh.iph;
157 if (!(iph->tos == rt_info->tos
158 && iph->daddr == rt_info->daddr
159 && iph->saddr == rt_info->saddr))
160 return ip_route_me_harder(pskb, RTN_UNSPEC);
165 __sum16 nf_ip_checksum(struct sk_buff *skb, unsigned int hook,
166 unsigned int dataoff, u_int8_t protocol)
168 struct iphdr *iph = skb->nh.iph;
171 switch (skb->ip_summed) {
172 case CHECKSUM_COMPLETE:
173 if (hook != NF_IP_PRE_ROUTING && hook != NF_IP_LOCAL_IN)
175 if ((protocol == 0 && !csum_fold(skb->csum)) ||
176 !csum_tcpudp_magic(iph->saddr, iph->daddr,
177 skb->len - dataoff, protocol,
179 skb->ip_summed = CHECKSUM_UNNECESSARY;
187 skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
190 csum = __skb_checksum_complete(skb);
195 EXPORT_SYMBOL(nf_ip_checksum);
197 static struct nf_afinfo nf_ip_afinfo = {
199 .checksum = nf_ip_checksum,
200 .saveroute = nf_ip_saveroute,
201 .reroute = nf_ip_reroute,
202 .route_key_size = sizeof(struct ip_rt_info),
205 static int ipv4_netfilter_init(void)
207 return nf_register_afinfo(&nf_ip_afinfo);
210 static void ipv4_netfilter_fini(void)
212 nf_unregister_afinfo(&nf_ip_afinfo);
215 module_init(ipv4_netfilter_init);
216 module_exit(ipv4_netfilter_fini);