xfrm4_policy.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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_key_dst = fl4->daddr;
  68. xdst->u.rt.rt_key_src = fl4->saddr;
  69. xdst->u.rt.rt_key_tos = fl4->flowi4_tos;
  70. xdst->u.rt.rt_route_iif = fl4->flowi4_iif;
  71. xdst->u.rt.rt_iif = fl4->flowi4_iif;
  72. xdst->u.rt.rt_oif = fl4->flowi4_oif;
  73. xdst->u.rt.rt_mark = fl4->flowi4_mark;
  74. xdst->u.dst.dev = dev;
  75. dev_hold(dev);
  76. xdst->u.rt.peer = rt->peer;
  77. if (rt->peer)
  78. atomic_inc(&rt->peer->refcnt);
  79. /* Sheit... I remember I did this right. Apparently,
  80. * it was magically lost, so this code needs audit */
  81. xdst->u.rt.rt_flags = rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST |
  82. RTCF_LOCAL);
  83. xdst->u.rt.rt_type = rt->rt_type;
  84. xdst->u.rt.rt_src = rt->rt_src;
  85. xdst->u.rt.rt_dst = rt->rt_dst;
  86. xdst->u.rt.rt_gateway = rt->rt_gateway;
  87. xdst->u.rt.rt_spec_dst = rt->rt_spec_dst;
  88. return 0;
  89. }
  90. static void
  91. _decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse)
  92. {
  93. const struct iphdr *iph = ip_hdr(skb);
  94. u8 *xprth = skb_network_header(skb) + iph->ihl * 4;
  95. struct flowi4 *fl4 = &fl->u.ip4;
  96. memset(fl4, 0, sizeof(struct flowi4));
  97. fl4->flowi4_mark = skb->mark;
  98. if (!(iph->frag_off & htons(IP_MF | IP_OFFSET))) {
  99. switch (iph->protocol) {
  100. case IPPROTO_UDP:
  101. case IPPROTO_UDPLITE:
  102. case IPPROTO_TCP:
  103. case IPPROTO_SCTP:
  104. case IPPROTO_DCCP:
  105. if (xprth + 4 < skb->data ||
  106. pskb_may_pull(skb, xprth + 4 - skb->data)) {
  107. __be16 *ports = (__be16 *)xprth;
  108. fl4->fl4_sport = ports[!!reverse];
  109. fl4->fl4_dport = ports[!reverse];
  110. }
  111. break;
  112. case IPPROTO_ICMP:
  113. if (pskb_may_pull(skb, xprth + 2 - skb->data)) {
  114. u8 *icmp = xprth;
  115. fl4->fl4_icmp_type = icmp[0];
  116. fl4->fl4_icmp_code = icmp[1];
  117. }
  118. break;
  119. case IPPROTO_ESP:
  120. if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
  121. __be32 *ehdr = (__be32 *)xprth;
  122. fl4->fl4_ipsec_spi = ehdr[0];
  123. }
  124. break;
  125. case IPPROTO_AH:
  126. if (pskb_may_pull(skb, xprth + 8 - skb->data)) {
  127. __be32 *ah_hdr = (__be32*)xprth;
  128. fl4->fl4_ipsec_spi = ah_hdr[1];
  129. }
  130. break;
  131. case IPPROTO_COMP:
  132. if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
  133. __be16 *ipcomp_hdr = (__be16 *)xprth;
  134. fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
  135. }
  136. break;
  137. case IPPROTO_GRE:
  138. if (pskb_may_pull(skb, xprth + 12 - skb->data)) {
  139. __be16 *greflags = (__be16 *)xprth;
  140. __be32 *gre_hdr = (__be32 *)xprth;
  141. if (greflags[0] & GRE_KEY) {
  142. if (greflags[0] & GRE_CSUM)
  143. gre_hdr++;
  144. fl4->fl4_gre_key = gre_hdr[1];
  145. }
  146. }
  147. break;
  148. default:
  149. fl4->fl4_ipsec_spi = 0;
  150. break;
  151. }
  152. }
  153. fl4->flowi4_proto = iph->protocol;
  154. fl4->daddr = reverse ? iph->saddr : iph->daddr;
  155. fl4->saddr = reverse ? iph->daddr : iph->saddr;
  156. fl4->flowi4_tos = iph->tos;
  157. }
  158. static inline int xfrm4_garbage_collect(struct dst_ops *ops)
  159. {
  160. struct net *net = container_of(ops, struct net, xfrm.xfrm4_dst_ops);
  161. xfrm4_policy_afinfo.garbage_collect(net);
  162. return (dst_entries_get_slow(ops) > ops->gc_thresh * 2);
  163. }
  164. static void xfrm4_update_pmtu(struct dst_entry *dst, u32 mtu)
  165. {
  166. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  167. struct dst_entry *path = xdst->route;
  168. path->ops->update_pmtu(path, mtu);
  169. }
  170. static void xfrm4_dst_destroy(struct dst_entry *dst)
  171. {
  172. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  173. dst_destroy_metrics_generic(dst);
  174. if (likely(xdst->u.rt.peer))
  175. inet_putpeer(xdst->u.rt.peer);
  176. xfrm_dst_destroy(xdst);
  177. }
  178. static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  179. int unregister)
  180. {
  181. if (!unregister)
  182. return;
  183. xfrm_dst_ifdown(dst, dev);
  184. }
  185. static struct dst_ops xfrm4_dst_ops = {
  186. .family = AF_INET,
  187. .protocol = cpu_to_be16(ETH_P_IP),
  188. .gc = xfrm4_garbage_collect,
  189. .update_pmtu = xfrm4_update_pmtu,
  190. .cow_metrics = dst_cow_metrics_generic,
  191. .destroy = xfrm4_dst_destroy,
  192. .ifdown = xfrm4_dst_ifdown,
  193. .local_out = __ip_local_out,
  194. .gc_thresh = 1024,
  195. };
  196. static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
  197. .family = AF_INET,
  198. .dst_ops = &xfrm4_dst_ops,
  199. .dst_lookup = xfrm4_dst_lookup,
  200. .get_saddr = xfrm4_get_saddr,
  201. .decode_session = _decode_session4,
  202. .get_tos = xfrm4_get_tos,
  203. .init_path = xfrm4_init_path,
  204. .fill_dst = xfrm4_fill_dst,
  205. .blackhole_route = ipv4_blackhole_route,
  206. };
  207. #ifdef CONFIG_SYSCTL
  208. static struct ctl_table xfrm4_policy_table[] = {
  209. {
  210. .procname = "xfrm4_gc_thresh",
  211. .data = &init_net.xfrm.xfrm4_dst_ops.gc_thresh,
  212. .maxlen = sizeof(int),
  213. .mode = 0644,
  214. .proc_handler = proc_dointvec,
  215. },
  216. { }
  217. };
  218. static struct ctl_table_header *sysctl_hdr;
  219. #endif
  220. static void __init xfrm4_policy_init(void)
  221. {
  222. xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
  223. }
  224. static void __exit xfrm4_policy_fini(void)
  225. {
  226. #ifdef CONFIG_SYSCTL
  227. if (sysctl_hdr)
  228. unregister_net_sysctl_table(sysctl_hdr);
  229. #endif
  230. xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo);
  231. }
  232. void __init xfrm4_init(int rt_max_size)
  233. {
  234. /*
  235. * Select a default value for the gc_thresh based on the main route
  236. * table hash size. It seems to me the worst case scenario is when
  237. * we have ipsec operating in transport mode, in which we create a
  238. * dst_entry per socket. The xfrm gc algorithm starts trying to remove
  239. * entries at gc_thresh, and prevents new allocations as 2*gc_thresh
  240. * so lets set an initial xfrm gc_thresh value at the rt_max_size/2.
  241. * That will let us store an ipsec connection per route table entry,
  242. * and start cleaning when were 1/2 full
  243. */
  244. xfrm4_dst_ops.gc_thresh = rt_max_size/2;
  245. dst_entries_init(&xfrm4_dst_ops);
  246. xfrm4_state_init();
  247. xfrm4_policy_init();
  248. #ifdef CONFIG_SYSCTL
  249. sysctl_hdr = register_net_sysctl_table(&init_net, net_ipv4_ctl_path,
  250. xfrm4_policy_table);
  251. #endif
  252. }