xfrm4_policy.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * xfrm4_policy.c
  3. *
  4. * Changes:
  5. * Kazunori MIYAZAWA @USAGI
  6. * YOSHIFUJI Hideaki @USAGI
  7. * Split up af-specific portion
  8. *
  9. */
  10. #include <linux/err.h>
  11. #include <linux/kernel.h>
  12. #include <linux/inetdevice.h>
  13. #include <linux/if_tunnel.h>
  14. #include <net/dst.h>
  15. #include <net/xfrm.h>
  16. #include <net/ip.h>
  17. static struct xfrm_policy_afinfo xfrm4_policy_afinfo;
  18. static struct dst_entry *__xfrm4_dst_lookup(struct net *net, struct flowi4 *fl4,
  19. int tos,
  20. const xfrm_address_t *saddr,
  21. const xfrm_address_t *daddr)
  22. {
  23. struct rtable *rt;
  24. memset(fl4, 0, sizeof(*fl4));
  25. fl4->daddr = daddr->a4;
  26. fl4->flowi4_tos = tos;
  27. if (saddr)
  28. fl4->saddr = saddr->a4;
  29. rt = __ip_route_output_key(net, fl4);
  30. if (!IS_ERR(rt))
  31. return &rt->dst;
  32. return ERR_CAST(rt);
  33. }
  34. static struct dst_entry *xfrm4_dst_lookup(struct net *net, int tos,
  35. const xfrm_address_t *saddr,
  36. const xfrm_address_t *daddr)
  37. {
  38. struct flowi4 fl4;
  39. return __xfrm4_dst_lookup(net, &fl4, tos, saddr, daddr);
  40. }
  41. static int xfrm4_get_saddr(struct net *net,
  42. xfrm_address_t *saddr, xfrm_address_t *daddr)
  43. {
  44. struct dst_entry *dst;
  45. struct flowi4 fl4;
  46. dst = __xfrm4_dst_lookup(net, &fl4, 0, NULL, daddr);
  47. if (IS_ERR(dst))
  48. return -EHOSTUNREACH;
  49. saddr->a4 = fl4.saddr;
  50. dst_release(dst);
  51. return 0;
  52. }
  53. static int xfrm4_get_tos(const struct flowi *fl)
  54. {
  55. return IPTOS_RT_MASK & fl->u.ip4.flowi4_tos; /* Strip ECN bits */
  56. }
  57. static int xfrm4_init_path(struct xfrm_dst *path, struct dst_entry *dst,
  58. int nfheader_len)
  59. {
  60. return 0;
  61. }
  62. static int xfrm4_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
  63. const struct flowi *fl)
  64. {
  65. struct rtable *rt = (struct rtable *)xdst->route;
  66. const struct flowi4 *fl4 = &fl->u.ip4;
  67. xdst->u.rt.rt_iif = fl4->flowi4_iif;
  68. xdst->u.dst.dev = dev;
  69. dev_hold(dev);
  70. /* Sheit... I remember I did this right. Apparently,
  71. * it was magically lost, so this code needs audit */
  72. xdst->u.rt.rt_is_input = rt->rt_is_input;
  73. xdst->u.rt.rt_flags = rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST |
  74. RTCF_LOCAL);
  75. xdst->u.rt.rt_type = rt->rt_type;
  76. xdst->u.rt.rt_gateway = rt->rt_gateway;
  77. xdst->u.rt.rt_pmtu = rt->rt_pmtu;
  78. INIT_LIST_HEAD(&xdst->u.rt.rt_uncached);
  79. return 0;
  80. }
  81. static void
  82. _decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse)
  83. {
  84. const struct iphdr *iph = ip_hdr(skb);
  85. u8 *xprth = skb_network_header(skb) + iph->ihl * 4;
  86. struct flowi4 *fl4 = &fl->u.ip4;
  87. memset(fl4, 0, sizeof(struct flowi4));
  88. fl4->flowi4_mark = skb->mark;
  89. if (!ip_is_fragment(iph)) {
  90. switch (iph->protocol) {
  91. case IPPROTO_UDP:
  92. case IPPROTO_UDPLITE:
  93. case IPPROTO_TCP:
  94. case IPPROTO_SCTP:
  95. case IPPROTO_DCCP:
  96. if (xprth + 4 < skb->data ||
  97. pskb_may_pull(skb, xprth + 4 - skb->data)) {
  98. __be16 *ports = (__be16 *)xprth;
  99. fl4->fl4_sport = ports[!!reverse];
  100. fl4->fl4_dport = ports[!reverse];
  101. }
  102. break;
  103. case IPPROTO_ICMP:
  104. if (pskb_may_pull(skb, xprth + 2 - skb->data)) {
  105. u8 *icmp = xprth;
  106. fl4->fl4_icmp_type = icmp[0];
  107. fl4->fl4_icmp_code = icmp[1];
  108. }
  109. break;
  110. case IPPROTO_ESP:
  111. if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
  112. __be32 *ehdr = (__be32 *)xprth;
  113. fl4->fl4_ipsec_spi = ehdr[0];
  114. }
  115. break;
  116. case IPPROTO_AH:
  117. if (pskb_may_pull(skb, xprth + 8 - skb->data)) {
  118. __be32 *ah_hdr = (__be32 *)xprth;
  119. fl4->fl4_ipsec_spi = ah_hdr[1];
  120. }
  121. break;
  122. case IPPROTO_COMP:
  123. if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
  124. __be16 *ipcomp_hdr = (__be16 *)xprth;
  125. fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
  126. }
  127. break;
  128. case IPPROTO_GRE:
  129. if (pskb_may_pull(skb, xprth + 12 - skb->data)) {
  130. __be16 *greflags = (__be16 *)xprth;
  131. __be32 *gre_hdr = (__be32 *)xprth;
  132. if (greflags[0] & GRE_KEY) {
  133. if (greflags[0] & GRE_CSUM)
  134. gre_hdr++;
  135. fl4->fl4_gre_key = gre_hdr[1];
  136. }
  137. }
  138. break;
  139. default:
  140. fl4->fl4_ipsec_spi = 0;
  141. break;
  142. }
  143. }
  144. fl4->flowi4_proto = iph->protocol;
  145. fl4->daddr = reverse ? iph->saddr : iph->daddr;
  146. fl4->saddr = reverse ? iph->daddr : iph->saddr;
  147. fl4->flowi4_tos = iph->tos;
  148. }
  149. static inline int xfrm4_garbage_collect(struct dst_ops *ops)
  150. {
  151. struct net *net = container_of(ops, struct net, xfrm.xfrm4_dst_ops);
  152. xfrm4_policy_afinfo.garbage_collect(net);
  153. return (dst_entries_get_slow(ops) > ops->gc_thresh * 2);
  154. }
  155. static void xfrm4_update_pmtu(struct dst_entry *dst, struct sock *sk,
  156. struct sk_buff *skb, u32 mtu)
  157. {
  158. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  159. struct dst_entry *path = xdst->route;
  160. path->ops->update_pmtu(path, sk, skb, mtu);
  161. }
  162. static void xfrm4_redirect(struct dst_entry *dst, struct sock *sk,
  163. struct sk_buff *skb)
  164. {
  165. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  166. struct dst_entry *path = xdst->route;
  167. path->ops->redirect(path, sk, skb);
  168. }
  169. static void xfrm4_dst_destroy(struct dst_entry *dst)
  170. {
  171. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  172. dst_destroy_metrics_generic(dst);
  173. xfrm_dst_destroy(xdst);
  174. }
  175. static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  176. int unregister)
  177. {
  178. if (!unregister)
  179. return;
  180. xfrm_dst_ifdown(dst, dev);
  181. }
  182. static struct dst_ops xfrm4_dst_ops = {
  183. .family = AF_INET,
  184. .protocol = cpu_to_be16(ETH_P_IP),
  185. .gc = xfrm4_garbage_collect,
  186. .update_pmtu = xfrm4_update_pmtu,
  187. .redirect = xfrm4_redirect,
  188. .cow_metrics = dst_cow_metrics_generic,
  189. .destroy = xfrm4_dst_destroy,
  190. .ifdown = xfrm4_dst_ifdown,
  191. .local_out = __ip_local_out,
  192. .gc_thresh = 1024,
  193. };
  194. static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
  195. .family = AF_INET,
  196. .dst_ops = &xfrm4_dst_ops,
  197. .dst_lookup = xfrm4_dst_lookup,
  198. .get_saddr = xfrm4_get_saddr,
  199. .decode_session = _decode_session4,
  200. .get_tos = xfrm4_get_tos,
  201. .init_path = xfrm4_init_path,
  202. .fill_dst = xfrm4_fill_dst,
  203. .blackhole_route = ipv4_blackhole_route,
  204. };
  205. #ifdef CONFIG_SYSCTL
  206. static struct ctl_table xfrm4_policy_table[] = {
  207. {
  208. .procname = "xfrm4_gc_thresh",
  209. .data = &init_net.xfrm.xfrm4_dst_ops.gc_thresh,
  210. .maxlen = sizeof(int),
  211. .mode = 0644,
  212. .proc_handler = proc_dointvec,
  213. },
  214. { }
  215. };
  216. static struct ctl_table_header *sysctl_hdr;
  217. #endif
  218. static void __init xfrm4_policy_init(void)
  219. {
  220. xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
  221. }
  222. static void __exit xfrm4_policy_fini(void)
  223. {
  224. #ifdef CONFIG_SYSCTL
  225. if (sysctl_hdr)
  226. unregister_net_sysctl_table(sysctl_hdr);
  227. #endif
  228. xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo);
  229. }
  230. void __init xfrm4_init(int rt_max_size)
  231. {
  232. /*
  233. * Select a default value for the gc_thresh based on the main route
  234. * table hash size. It seems to me the worst case scenario is when
  235. * we have ipsec operating in transport mode, in which we create a
  236. * dst_entry per socket. The xfrm gc algorithm starts trying to remove
  237. * entries at gc_thresh, and prevents new allocations as 2*gc_thresh
  238. * so lets set an initial xfrm gc_thresh value at the rt_max_size/2.
  239. * That will let us store an ipsec connection per route table entry,
  240. * and start cleaning when were 1/2 full
  241. */
  242. xfrm4_dst_ops.gc_thresh = rt_max_size/2;
  243. dst_entries_init(&xfrm4_dst_ops);
  244. xfrm4_state_init();
  245. xfrm4_policy_init();
  246. #ifdef CONFIG_SYSCTL
  247. sysctl_hdr = register_net_sysctl(&init_net, "net/ipv4",
  248. xfrm4_policy_table);
  249. #endif
  250. }