netfilter.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 (addr_type == RTN_UNSPEC)
  22. addr_type = type;
  23. /* some non-standard hacks like ipt_REJECT.c:send_reset() can cause
  24. * packets with foreign saddr to appear on the NF_INET_LOCAL_OUT hook.
  25. */
  26. if (addr_type == RTN_LOCAL) {
  27. fl.nl_u.ip4_u.daddr = iph->daddr;
  28. if (type == RTN_LOCAL)
  29. fl.nl_u.ip4_u.saddr = iph->saddr;
  30. fl.nl_u.ip4_u.tos = RT_TOS(iph->tos);
  31. fl.oif = skb->sk ? skb->sk->sk_bound_dev_if : 0;
  32. fl.mark = skb->mark;
  33. if (ip_route_output_key(&init_net, &rt, &fl) != 0)
  34. return -1;
  35. /* Drop old route. */
  36. dst_release(skb->dst);
  37. skb->dst = &rt->u.dst;
  38. } else {
  39. /* non-local src, find valid iif to satisfy
  40. * rp-filter when calling ip_route_input. */
  41. fl.nl_u.ip4_u.daddr = iph->saddr;
  42. if (ip_route_output_key(&init_net, &rt, &fl) != 0)
  43. return -1;
  44. odst = skb->dst;
  45. if (ip_route_input(skb, iph->daddr, iph->saddr,
  46. RT_TOS(iph->tos), rt->u.dst.dev) != 0) {
  47. dst_release(&rt->u.dst);
  48. return -1;
  49. }
  50. dst_release(&rt->u.dst);
  51. dst_release(odst);
  52. }
  53. if (skb->dst->error)
  54. return -1;
  55. #ifdef CONFIG_XFRM
  56. if (!(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
  57. xfrm_decode_session(skb, &fl, AF_INET) == 0)
  58. if (xfrm_lookup(&skb->dst, &fl, skb->sk, 0))
  59. return -1;
  60. #endif
  61. /* Change in oif may mean change in hh_len. */
  62. hh_len = skb->dst->dev->hard_header_len;
  63. if (skb_headroom(skb) < hh_len &&
  64. pskb_expand_head(skb, hh_len - skb_headroom(skb), 0, GFP_ATOMIC))
  65. return -1;
  66. return 0;
  67. }
  68. EXPORT_SYMBOL(ip_route_me_harder);
  69. #ifdef CONFIG_XFRM
  70. int ip_xfrm_me_harder(struct sk_buff *skb)
  71. {
  72. struct flowi fl;
  73. unsigned int hh_len;
  74. struct dst_entry *dst;
  75. if (IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
  76. return 0;
  77. if (xfrm_decode_session(skb, &fl, AF_INET) < 0)
  78. return -1;
  79. dst = skb->dst;
  80. if (dst->xfrm)
  81. dst = ((struct xfrm_dst *)dst)->route;
  82. dst_hold(dst);
  83. if (xfrm_lookup(&dst, &fl, skb->sk, 0) < 0)
  84. return -1;
  85. dst_release(skb->dst);
  86. skb->dst = dst;
  87. /* Change in oif may mean change in hh_len. */
  88. hh_len = skb->dst->dev->hard_header_len;
  89. if (skb_headroom(skb) < hh_len &&
  90. pskb_expand_head(skb, hh_len - skb_headroom(skb), 0, GFP_ATOMIC))
  91. return -1;
  92. return 0;
  93. }
  94. EXPORT_SYMBOL(ip_xfrm_me_harder);
  95. #endif
  96. void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
  97. EXPORT_SYMBOL(ip_nat_decode_session);
  98. /*
  99. * Extra routing may needed on local out, as the QUEUE target never
  100. * returns control to the table.
  101. */
  102. struct ip_rt_info {
  103. __be32 daddr;
  104. __be32 saddr;
  105. u_int8_t tos;
  106. };
  107. static void nf_ip_saveroute(const struct sk_buff *skb,
  108. struct nf_queue_entry *entry)
  109. {
  110. struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
  111. if (entry->hook == NF_INET_LOCAL_OUT) {
  112. const struct iphdr *iph = ip_hdr(skb);
  113. rt_info->tos = iph->tos;
  114. rt_info->daddr = iph->daddr;
  115. rt_info->saddr = iph->saddr;
  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. && 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. csum = __skb_checksum_complete_head(skb, dataoff + len);
  176. if (!csum)
  177. skb->ip_summed = CHECKSUM_UNNECESSARY;
  178. }
  179. return csum;
  180. }
  181. static int nf_ip_route(struct dst_entry **dst, struct flowi *fl)
  182. {
  183. return ip_route_output_key(&init_net, (struct rtable **)dst, fl);
  184. }
  185. static const struct nf_afinfo nf_ip_afinfo = {
  186. .family = AF_INET,
  187. .checksum = nf_ip_checksum,
  188. .checksum_partial = nf_ip_checksum_partial,
  189. .route = nf_ip_route,
  190. .saveroute = nf_ip_saveroute,
  191. .reroute = nf_ip_reroute,
  192. .route_key_size = sizeof(struct ip_rt_info),
  193. };
  194. static int ipv4_netfilter_init(void)
  195. {
  196. return nf_register_afinfo(&nf_ip_afinfo);
  197. }
  198. static void ipv4_netfilter_fini(void)
  199. {
  200. nf_unregister_afinfo(&nf_ip_afinfo);
  201. }
  202. module_init(ipv4_netfilter_init);
  203. module_exit(ipv4_netfilter_fini);
  204. #ifdef CONFIG_SYSCTL
  205. struct ctl_path nf_net_ipv4_netfilter_sysctl_path[] = {
  206. { .procname = "net", .ctl_name = CTL_NET, },
  207. { .procname = "ipv4", .ctl_name = NET_IPV4, },
  208. { .procname = "netfilter", .ctl_name = NET_IPV4_NETFILTER, },
  209. { }
  210. };
  211. EXPORT_SYMBOL_GPL(nf_net_ipv4_netfilter_sysctl_path);
  212. #endif /* CONFIG_SYSCTL */