Harald Welte | 2cc7d57 | 2005-08-09 19:42:34 -0700 | [diff] [blame] | 1 | /* IPv4 specific functions of netfilter core */ |
Harald Welte | 020b4c1 | 2005-08-09 19:39:00 -0700 | [diff] [blame] | 2 | #include <linux/kernel.h> |
| 3 | #include <linux/netfilter.h> |
Harald Welte | 2cc7d57 | 2005-08-09 19:42:34 -0700 | [diff] [blame] | 4 | #include <linux/netfilter_ipv4.h> |
Patrick McHardy | 3e3850e | 2006-01-06 23:04:54 -0800 | [diff] [blame] | 5 | #include <linux/ip.h> |
Harald Welte | 020b4c1 | 2005-08-09 19:39:00 -0700 | [diff] [blame] | 6 | #include <net/route.h> |
Patrick McHardy | 3e3850e | 2006-01-06 23:04:54 -0800 | [diff] [blame] | 7 | #include <net/xfrm.h> |
| 8 | #include <net/ip.h> |
Harald Welte | 020b4c1 | 2005-08-09 19:39:00 -0700 | [diff] [blame] | 9 | |
| 10 | /* route_me_harder function, used by iptable_nat, iptable_mangle + ip_queue */ |
| 11 | int ip_route_me_harder(struct sk_buff **pskb) |
| 12 | { |
| 13 | struct iphdr *iph = (*pskb)->nh.iph; |
| 14 | struct rtable *rt; |
| 15 | struct flowi fl = {}; |
| 16 | struct dst_entry *odst; |
| 17 | unsigned int hh_len; |
| 18 | |
| 19 | /* some non-standard hacks like ipt_REJECT.c:send_reset() can cause |
| 20 | * packets with foreign saddr to appear on the NF_IP_LOCAL_OUT hook. |
| 21 | */ |
| 22 | if (inet_addr_type(iph->saddr) == RTN_LOCAL) { |
| 23 | fl.nl_u.ip4_u.daddr = iph->daddr; |
| 24 | fl.nl_u.ip4_u.saddr = iph->saddr; |
| 25 | fl.nl_u.ip4_u.tos = RT_TOS(iph->tos); |
| 26 | fl.oif = (*pskb)->sk ? (*pskb)->sk->sk_bound_dev_if : 0; |
| 27 | #ifdef CONFIG_IP_ROUTE_FWMARK |
| 28 | fl.nl_u.ip4_u.fwmark = (*pskb)->nfmark; |
| 29 | #endif |
Harald Welte | 020b4c1 | 2005-08-09 19:39:00 -0700 | [diff] [blame] | 30 | if (ip_route_output_key(&rt, &fl) != 0) |
| 31 | return -1; |
| 32 | |
| 33 | /* Drop old route. */ |
| 34 | dst_release((*pskb)->dst); |
| 35 | (*pskb)->dst = &rt->u.dst; |
| 36 | } else { |
| 37 | /* non-local src, find valid iif to satisfy |
| 38 | * rp-filter when calling ip_route_input. */ |
| 39 | fl.nl_u.ip4_u.daddr = iph->saddr; |
| 40 | if (ip_route_output_key(&rt, &fl) != 0) |
| 41 | return -1; |
| 42 | |
| 43 | odst = (*pskb)->dst; |
| 44 | if (ip_route_input(*pskb, iph->daddr, iph->saddr, |
| 45 | RT_TOS(iph->tos), rt->u.dst.dev) != 0) { |
| 46 | dst_release(&rt->u.dst); |
| 47 | return -1; |
| 48 | } |
| 49 | dst_release(&rt->u.dst); |
| 50 | dst_release(odst); |
| 51 | } |
| 52 | |
| 53 | if ((*pskb)->dst->error) |
| 54 | return -1; |
| 55 | |
Patrick McHardy | 3e3850e | 2006-01-06 23:04:54 -0800 | [diff] [blame] | 56 | #ifdef CONFIG_XFRM |
| 57 | if (!(IPCB(*pskb)->flags & IPSKB_XFRM_TRANSFORMED) && |
| 58 | xfrm_decode_session(*pskb, &fl, AF_INET) == 0) |
| 59 | if (xfrm_lookup(&(*pskb)->dst, &fl, (*pskb)->sk, 0)) |
| 60 | return -1; |
| 61 | #endif |
| 62 | |
Harald Welte | 020b4c1 | 2005-08-09 19:39:00 -0700 | [diff] [blame] | 63 | /* Change in oif may mean change in hh_len. */ |
| 64 | hh_len = (*pskb)->dst->dev->hard_header_len; |
| 65 | if (skb_headroom(*pskb) < hh_len) { |
| 66 | struct sk_buff *nskb; |
| 67 | |
| 68 | nskb = skb_realloc_headroom(*pskb, hh_len); |
| 69 | if (!nskb) |
| 70 | return -1; |
| 71 | if ((*pskb)->sk) |
| 72 | skb_set_owner_w(nskb, (*pskb)->sk); |
| 73 | kfree_skb(*pskb); |
| 74 | *pskb = nskb; |
| 75 | } |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | EXPORT_SYMBOL(ip_route_me_harder); |
Harald Welte | 2cc7d57 | 2005-08-09 19:42:34 -0700 | [diff] [blame] | 80 | |
Patrick McHardy | ee68cea | 2006-02-15 01:34:23 -0800 | [diff] [blame] | 81 | #ifdef CONFIG_XFRM |
| 82 | int ip_xfrm_me_harder(struct sk_buff **pskb) |
| 83 | { |
| 84 | struct flowi fl; |
| 85 | unsigned int hh_len; |
| 86 | struct dst_entry *dst; |
| 87 | |
| 88 | if (IPCB(*pskb)->flags & IPSKB_XFRM_TRANSFORMED) |
| 89 | return 0; |
| 90 | if (xfrm_decode_session(*pskb, &fl, AF_INET) < 0) |
| 91 | return -1; |
| 92 | |
| 93 | dst = (*pskb)->dst; |
| 94 | if (dst->xfrm) |
| 95 | dst = ((struct xfrm_dst *)dst)->route; |
| 96 | dst_hold(dst); |
| 97 | |
| 98 | if (xfrm_lookup(&dst, &fl, (*pskb)->sk, 0) < 0) |
| 99 | return -1; |
| 100 | |
| 101 | dst_release((*pskb)->dst); |
| 102 | (*pskb)->dst = dst; |
| 103 | |
| 104 | /* Change in oif may mean change in hh_len. */ |
| 105 | hh_len = (*pskb)->dst->dev->hard_header_len; |
| 106 | if (skb_headroom(*pskb) < hh_len) { |
| 107 | struct sk_buff *nskb; |
| 108 | |
| 109 | nskb = skb_realloc_headroom(*pskb, hh_len); |
| 110 | if (!nskb) |
| 111 | return -1; |
| 112 | if ((*pskb)->sk) |
| 113 | skb_set_owner_w(nskb, (*pskb)->sk); |
| 114 | kfree_skb(*pskb); |
| 115 | *pskb = nskb; |
| 116 | } |
| 117 | return 0; |
| 118 | } |
| 119 | EXPORT_SYMBOL(ip_xfrm_me_harder); |
| 120 | #endif |
| 121 | |
Patrick McHardy | eb9c7eb | 2006-01-06 23:06:30 -0800 | [diff] [blame] | 122 | void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *); |
| 123 | EXPORT_SYMBOL(ip_nat_decode_session); |
| 124 | |
Harald Welte | 2cc7d57 | 2005-08-09 19:42:34 -0700 | [diff] [blame] | 125 | /* |
| 126 | * Extra routing may needed on local out, as the QUEUE target never |
| 127 | * returns control to the table. |
| 128 | */ |
| 129 | |
| 130 | struct ip_rt_info { |
Al Viro | 59b8bfd | 2006-09-28 14:21:07 -0700 | [diff] [blame^] | 131 | __be32 daddr; |
| 132 | __be32 saddr; |
Harald Welte | 2cc7d57 | 2005-08-09 19:42:34 -0700 | [diff] [blame] | 133 | u_int8_t tos; |
| 134 | }; |
| 135 | |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 136 | static void nf_ip_saveroute(const struct sk_buff *skb, struct nf_info *info) |
Harald Welte | 2cc7d57 | 2005-08-09 19:42:34 -0700 | [diff] [blame] | 137 | { |
| 138 | struct ip_rt_info *rt_info = nf_info_reroute(info); |
| 139 | |
| 140 | if (info->hook == NF_IP_LOCAL_OUT) { |
| 141 | const struct iphdr *iph = skb->nh.iph; |
| 142 | |
| 143 | rt_info->tos = iph->tos; |
| 144 | rt_info->daddr = iph->daddr; |
| 145 | rt_info->saddr = iph->saddr; |
| 146 | } |
| 147 | } |
| 148 | |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 149 | static int nf_ip_reroute(struct sk_buff **pskb, const struct nf_info *info) |
Harald Welte | 2cc7d57 | 2005-08-09 19:42:34 -0700 | [diff] [blame] | 150 | { |
| 151 | const struct ip_rt_info *rt_info = nf_info_reroute(info); |
| 152 | |
| 153 | if (info->hook == NF_IP_LOCAL_OUT) { |
| 154 | struct iphdr *iph = (*pskb)->nh.iph; |
| 155 | |
| 156 | if (!(iph->tos == rt_info->tos |
| 157 | && iph->daddr == rt_info->daddr |
| 158 | && iph->saddr == rt_info->saddr)) |
| 159 | return ip_route_me_harder(pskb); |
| 160 | } |
| 161 | return 0; |
| 162 | } |
| 163 | |
Patrick McHardy | 422c346 | 2006-04-06 14:18:43 -0700 | [diff] [blame] | 164 | unsigned int nf_ip_checksum(struct sk_buff *skb, unsigned int hook, |
| 165 | unsigned int dataoff, u_int8_t protocol) |
| 166 | { |
| 167 | struct iphdr *iph = skb->nh.iph; |
| 168 | unsigned int csum = 0; |
| 169 | |
| 170 | switch (skb->ip_summed) { |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 171 | case CHECKSUM_COMPLETE: |
Patrick McHardy | 422c346 | 2006-04-06 14:18:43 -0700 | [diff] [blame] | 172 | if (hook != NF_IP_PRE_ROUTING && hook != NF_IP_LOCAL_IN) |
| 173 | break; |
| 174 | if ((protocol == 0 && !(u16)csum_fold(skb->csum)) || |
| 175 | !csum_tcpudp_magic(iph->saddr, iph->daddr, |
| 176 | skb->len - dataoff, protocol, |
| 177 | skb->csum)) { |
| 178 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 179 | break; |
| 180 | } |
| 181 | /* fall through */ |
| 182 | case CHECKSUM_NONE: |
| 183 | if (protocol == 0) |
| 184 | skb->csum = 0; |
| 185 | else |
| 186 | skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr, |
| 187 | skb->len - dataoff, |
| 188 | protocol, 0); |
| 189 | csum = __skb_checksum_complete(skb); |
| 190 | } |
| 191 | return csum; |
| 192 | } |
| 193 | |
| 194 | EXPORT_SYMBOL(nf_ip_checksum); |
| 195 | |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 196 | static struct nf_afinfo nf_ip_afinfo = { |
| 197 | .family = AF_INET, |
Patrick McHardy | 422c346 | 2006-04-06 14:18:43 -0700 | [diff] [blame] | 198 | .checksum = nf_ip_checksum, |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 199 | .saveroute = nf_ip_saveroute, |
| 200 | .reroute = nf_ip_reroute, |
| 201 | .route_key_size = sizeof(struct ip_rt_info), |
Harald Welte | 2cc7d57 | 2005-08-09 19:42:34 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 204 | static int ipv4_netfilter_init(void) |
Harald Welte | 2cc7d57 | 2005-08-09 19:42:34 -0700 | [diff] [blame] | 205 | { |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 206 | return nf_register_afinfo(&nf_ip_afinfo); |
Harald Welte | 2cc7d57 | 2005-08-09 19:42:34 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 209 | static void ipv4_netfilter_fini(void) |
Harald Welte | 2cc7d57 | 2005-08-09 19:42:34 -0700 | [diff] [blame] | 210 | { |
Patrick McHardy | bce8032 | 2006-04-06 14:18:09 -0700 | [diff] [blame] | 211 | nf_unregister_afinfo(&nf_ip_afinfo); |
Harald Welte | 2cc7d57 | 2005-08-09 19:42:34 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 214 | module_init(ipv4_netfilter_init); |
| 215 | module_exit(ipv4_netfilter_fini); |