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