xfrm4_policy.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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->u.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. if (!(iph->frag_off & htons(IP_MF | IP_OFFSET))) {
  93. switch (iph->protocol) {
  94. case IPPROTO_UDP:
  95. case IPPROTO_UDPLITE:
  96. case IPPROTO_TCP:
  97. case IPPROTO_SCTP:
  98. case IPPROTO_DCCP:
  99. if (xprth + 4 < skb->data ||
  100. pskb_may_pull(skb, xprth + 4 - skb->data)) {
  101. __be16 *ports = (__be16 *)xprth;
  102. fl->fl_ip_sport = ports[!!reverse];
  103. fl->fl_ip_dport = ports[!reverse];
  104. }
  105. break;
  106. case IPPROTO_ICMP:
  107. if (pskb_may_pull(skb, xprth + 2 - skb->data)) {
  108. u8 *icmp = xprth;
  109. fl->fl_icmp_type = icmp[0];
  110. fl->fl_icmp_code = icmp[1];
  111. }
  112. break;
  113. case IPPROTO_ESP:
  114. if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
  115. __be32 *ehdr = (__be32 *)xprth;
  116. fl->fl_ipsec_spi = ehdr[0];
  117. }
  118. break;
  119. case IPPROTO_AH:
  120. if (pskb_may_pull(skb, xprth + 8 - skb->data)) {
  121. __be32 *ah_hdr = (__be32*)xprth;
  122. fl->fl_ipsec_spi = ah_hdr[1];
  123. }
  124. break;
  125. case IPPROTO_COMP:
  126. if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
  127. __be16 *ipcomp_hdr = (__be16 *)xprth;
  128. fl->fl_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
  129. }
  130. break;
  131. default:
  132. fl->fl_ipsec_spi = 0;
  133. break;
  134. }
  135. }
  136. fl->proto = iph->protocol;
  137. fl->fl4_dst = reverse ? iph->saddr : iph->daddr;
  138. fl->fl4_src = reverse ? iph->daddr : iph->saddr;
  139. fl->fl4_tos = iph->tos;
  140. }
  141. static inline int xfrm4_garbage_collect(struct dst_ops *ops)
  142. {
  143. struct net *net = container_of(ops, struct net, xfrm.xfrm4_dst_ops);
  144. xfrm4_policy_afinfo.garbage_collect(net);
  145. return (atomic_read(&ops->entries) > ops->gc_thresh * 2);
  146. }
  147. static void xfrm4_update_pmtu(struct dst_entry *dst, u32 mtu)
  148. {
  149. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  150. struct dst_entry *path = xdst->route;
  151. path->ops->update_pmtu(path, mtu);
  152. }
  153. static void xfrm4_dst_destroy(struct dst_entry *dst)
  154. {
  155. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  156. if (likely(xdst->u.rt.idev))
  157. in_dev_put(xdst->u.rt.idev);
  158. if (likely(xdst->u.rt.peer))
  159. inet_putpeer(xdst->u.rt.peer);
  160. xfrm_dst_destroy(xdst);
  161. }
  162. static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  163. int unregister)
  164. {
  165. struct xfrm_dst *xdst;
  166. if (!unregister)
  167. return;
  168. xdst = (struct xfrm_dst *)dst;
  169. if (xdst->u.rt.idev->dev == dev) {
  170. struct in_device *loopback_idev =
  171. in_dev_get(dev_net(dev)->loopback_dev);
  172. BUG_ON(!loopback_idev);
  173. do {
  174. in_dev_put(xdst->u.rt.idev);
  175. xdst->u.rt.idev = loopback_idev;
  176. in_dev_hold(loopback_idev);
  177. xdst = (struct xfrm_dst *)xdst->u.dst.child;
  178. } while (xdst->u.dst.xfrm);
  179. __in_dev_put(loopback_idev);
  180. }
  181. xfrm_dst_ifdown(dst, dev);
  182. }
  183. static struct dst_ops xfrm4_dst_ops = {
  184. .family = AF_INET,
  185. .protocol = cpu_to_be16(ETH_P_IP),
  186. .gc = xfrm4_garbage_collect,
  187. .update_pmtu = xfrm4_update_pmtu,
  188. .destroy = xfrm4_dst_destroy,
  189. .ifdown = xfrm4_dst_ifdown,
  190. .local_out = __ip_local_out,
  191. .gc_thresh = 1024,
  192. .entries = ATOMIC_INIT(0),
  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. };
  204. #ifdef CONFIG_SYSCTL
  205. static struct ctl_table xfrm4_policy_table[] = {
  206. {
  207. .procname = "xfrm4_gc_thresh",
  208. .data = &init_net.xfrm.xfrm4_dst_ops.gc_thresh,
  209. .maxlen = sizeof(int),
  210. .mode = 0644,
  211. .proc_handler = proc_dointvec,
  212. },
  213. { }
  214. };
  215. static struct ctl_table_header *sysctl_hdr;
  216. #endif
  217. static void __init xfrm4_policy_init(void)
  218. {
  219. xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
  220. }
  221. static void __exit xfrm4_policy_fini(void)
  222. {
  223. #ifdef CONFIG_SYSCTL
  224. if (sysctl_hdr)
  225. unregister_net_sysctl_table(sysctl_hdr);
  226. #endif
  227. xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo);
  228. }
  229. void __init xfrm4_init(int rt_max_size)
  230. {
  231. /*
  232. * Select a default value for the gc_thresh based on the main route
  233. * table hash size. It seems to me the worst case scenario is when
  234. * we have ipsec operating in transport mode, in which we create a
  235. * dst_entry per socket. The xfrm gc algorithm starts trying to remove
  236. * entries at gc_thresh, and prevents new allocations as 2*gc_thresh
  237. * so lets set an initial xfrm gc_thresh value at the rt_max_size/2.
  238. * That will let us store an ipsec connection per route table entry,
  239. * and start cleaning when were 1/2 full
  240. */
  241. xfrm4_dst_ops.gc_thresh = rt_max_size/2;
  242. xfrm4_state_init();
  243. xfrm4_policy_init();
  244. #ifdef CONFIG_SYSCTL
  245. sysctl_hdr = register_net_sysctl_table(&init_net, net_ipv4_ctl_path,
  246. xfrm4_policy_table);
  247. #endif
  248. }