xfrm4_policy.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 <net/dst.h>
  14. #include <net/xfrm.h>
  15. #include <net/ip.h>
  16. static struct xfrm_policy_afinfo xfrm4_policy_afinfo;
  17. static struct dst_entry *xfrm4_dst_lookup(struct net *net, int tos,
  18. xfrm_address_t *saddr,
  19. xfrm_address_t *daddr)
  20. {
  21. struct flowi fl = {
  22. .nl_u = {
  23. .ip4_u = {
  24. .tos = tos,
  25. .daddr = daddr->a4,
  26. },
  27. },
  28. };
  29. struct dst_entry *dst;
  30. struct rtable *rt;
  31. int err;
  32. if (saddr)
  33. fl.fl4_src = saddr->a4;
  34. err = __ip_route_output_key(net, &rt, &fl);
  35. dst = &rt->dst;
  36. if (err)
  37. dst = ERR_PTR(err);
  38. return dst;
  39. }
  40. static int xfrm4_get_saddr(struct net *net,
  41. xfrm_address_t *saddr, xfrm_address_t *daddr)
  42. {
  43. struct dst_entry *dst;
  44. struct rtable *rt;
  45. dst = xfrm4_dst_lookup(net, 0, NULL, daddr);
  46. if (IS_ERR(dst))
  47. return -EHOSTUNREACH;
  48. rt = (struct rtable *)dst;
  49. saddr->a4 = rt->rt_src;
  50. dst_release(dst);
  51. return 0;
  52. }
  53. static int xfrm4_get_tos(struct flowi *fl)
  54. {
  55. return fl->fl4_tos;
  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. struct flowi *fl)
  64. {
  65. struct rtable *rt = (struct rtable *)xdst->route;
  66. xdst->u.rt.fl = *fl;
  67. xdst->u.dst.dev = dev;
  68. dev_hold(dev);
  69. xdst->u.rt.idev = in_dev_get(dev);
  70. if (!xdst->u.rt.idev)
  71. return -ENODEV;
  72. xdst->u.rt.peer = rt->peer;
  73. if (rt->peer)
  74. atomic_inc(&rt->peer->refcnt);
  75. /* Sheit... I remember I did this right. Apparently,
  76. * it was magically lost, so this code needs audit */
  77. xdst->u.rt.rt_flags = rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST |
  78. RTCF_LOCAL);
  79. xdst->u.rt.rt_type = rt->rt_type;
  80. xdst->u.rt.rt_src = rt->rt_src;
  81. xdst->u.rt.rt_dst = rt->rt_dst;
  82. xdst->u.rt.rt_gateway = rt->rt_gateway;
  83. xdst->u.rt.rt_spec_dst = rt->rt_spec_dst;
  84. return 0;
  85. }
  86. static void
  87. _decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse)
  88. {
  89. struct iphdr *iph = ip_hdr(skb);
  90. u8 *xprth = skb_network_header(skb) + iph->ihl * 4;
  91. memset(fl, 0, sizeof(struct flowi));
  92. fl->mark = skb->mark;
  93. if (!(iph->frag_off & htons(IP_MF | IP_OFFSET))) {
  94. switch (iph->protocol) {
  95. case IPPROTO_UDP:
  96. case IPPROTO_UDPLITE:
  97. case IPPROTO_TCP:
  98. case IPPROTO_SCTP:
  99. case IPPROTO_DCCP:
  100. if (xprth + 4 < skb->data ||
  101. pskb_may_pull(skb, xprth + 4 - skb->data)) {
  102. __be16 *ports = (__be16 *)xprth;
  103. fl->fl_ip_sport = ports[!!reverse];
  104. fl->fl_ip_dport = ports[!reverse];
  105. }
  106. break;
  107. case IPPROTO_ICMP:
  108. if (pskb_may_pull(skb, xprth + 2 - skb->data)) {
  109. u8 *icmp = xprth;
  110. fl->fl_icmp_type = icmp[0];
  111. fl->fl_icmp_code = icmp[1];
  112. }
  113. break;
  114. case IPPROTO_ESP:
  115. if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
  116. __be32 *ehdr = (__be32 *)xprth;
  117. fl->fl_ipsec_spi = ehdr[0];
  118. }
  119. break;
  120. case IPPROTO_AH:
  121. if (pskb_may_pull(skb, xprth + 8 - skb->data)) {
  122. __be32 *ah_hdr = (__be32*)xprth;
  123. fl->fl_ipsec_spi = ah_hdr[1];
  124. }
  125. break;
  126. case IPPROTO_COMP:
  127. if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
  128. __be16 *ipcomp_hdr = (__be16 *)xprth;
  129. fl->fl_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
  130. }
  131. break;
  132. default:
  133. fl->fl_ipsec_spi = 0;
  134. break;
  135. }
  136. }
  137. fl->proto = iph->protocol;
  138. fl->fl4_dst = reverse ? iph->saddr : iph->daddr;
  139. fl->fl4_src = reverse ? iph->daddr : iph->saddr;
  140. fl->fl4_tos = iph->tos;
  141. }
  142. static inline int xfrm4_garbage_collect(struct dst_ops *ops)
  143. {
  144. struct net *net = container_of(ops, struct net, xfrm.xfrm4_dst_ops);
  145. xfrm4_policy_afinfo.garbage_collect(net);
  146. return (atomic_read(&ops->entries) > ops->gc_thresh * 2);
  147. }
  148. static void xfrm4_update_pmtu(struct dst_entry *dst, u32 mtu)
  149. {
  150. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  151. struct dst_entry *path = xdst->route;
  152. path->ops->update_pmtu(path, mtu);
  153. }
  154. static void xfrm4_dst_destroy(struct dst_entry *dst)
  155. {
  156. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  157. if (likely(xdst->u.rt.idev))
  158. in_dev_put(xdst->u.rt.idev);
  159. if (likely(xdst->u.rt.peer))
  160. inet_putpeer(xdst->u.rt.peer);
  161. xfrm_dst_destroy(xdst);
  162. }
  163. static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  164. int unregister)
  165. {
  166. struct xfrm_dst *xdst;
  167. if (!unregister)
  168. return;
  169. xdst = (struct xfrm_dst *)dst;
  170. if (xdst->u.rt.idev->dev == dev) {
  171. struct in_device *loopback_idev =
  172. in_dev_get(dev_net(dev)->loopback_dev);
  173. BUG_ON(!loopback_idev);
  174. do {
  175. in_dev_put(xdst->u.rt.idev);
  176. xdst->u.rt.idev = loopback_idev;
  177. in_dev_hold(loopback_idev);
  178. xdst = (struct xfrm_dst *)xdst->u.dst.child;
  179. } while (xdst->u.dst.xfrm);
  180. __in_dev_put(loopback_idev);
  181. }
  182. xfrm_dst_ifdown(dst, dev);
  183. }
  184. static struct dst_ops xfrm4_dst_ops = {
  185. .family = AF_INET,
  186. .protocol = cpu_to_be16(ETH_P_IP),
  187. .gc = xfrm4_garbage_collect,
  188. .update_pmtu = xfrm4_update_pmtu,
  189. .destroy = xfrm4_dst_destroy,
  190. .ifdown = xfrm4_dst_ifdown,
  191. .local_out = __ip_local_out,
  192. .gc_thresh = 1024,
  193. .entries = ATOMIC_INIT(0),
  194. };
  195. static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
  196. .family = AF_INET,
  197. .dst_ops = &xfrm4_dst_ops,
  198. .dst_lookup = xfrm4_dst_lookup,
  199. .get_saddr = xfrm4_get_saddr,
  200. .decode_session = _decode_session4,
  201. .get_tos = xfrm4_get_tos,
  202. .init_path = xfrm4_init_path,
  203. .fill_dst = xfrm4_fill_dst,
  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. xfrm4_state_init();
  244. xfrm4_policy_init();
  245. #ifdef CONFIG_SYSCTL
  246. sysctl_hdr = register_net_sysctl_table(&init_net, net_ipv4_ctl_path,
  247. xfrm4_policy_table);
  248. #endif
  249. }