netfilter.c 6.6 KB

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