netfilter.c 6.3 KB

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