xfrm4_policy.c 6.6 KB

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