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