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