netfilter.c 6.2 KB

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