xfrm4_policy.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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_iif = fl4->flowi4_iif;
  68. xdst->u.dst.dev = dev;
  69. dev_hold(dev);
  70. /* Sheit... I remember I did this right. Apparently,
  71. * it was magically lost, so this code needs audit */
  72. xdst->u.rt.rt_is_input = rt->rt_is_input;
  73. xdst->u.rt.rt_flags = rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST |
  74. RTCF_LOCAL);
  75. xdst->u.rt.rt_type = rt->rt_type;
  76. xdst->u.rt.rt_gateway = rt->rt_gateway;
  77. xdst->u.rt.rt_pmtu = rt->rt_pmtu;
  78. return 0;
  79. }
  80. static void
  81. _decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse)
  82. {
  83. const struct iphdr *iph = ip_hdr(skb);
  84. u8 *xprth = skb_network_header(skb) + iph->ihl * 4;
  85. struct flowi4 *fl4 = &fl->u.ip4;
  86. memset(fl4, 0, sizeof(struct flowi4));
  87. fl4->flowi4_mark = skb->mark;
  88. if (!ip_is_fragment(iph)) {
  89. switch (iph->protocol) {
  90. case IPPROTO_UDP:
  91. case IPPROTO_UDPLITE:
  92. case IPPROTO_TCP:
  93. case IPPROTO_SCTP:
  94. case IPPROTO_DCCP:
  95. if (xprth + 4 < skb->data ||
  96. pskb_may_pull(skb, xprth + 4 - skb->data)) {
  97. __be16 *ports = (__be16 *)xprth;
  98. fl4->fl4_sport = ports[!!reverse];
  99. fl4->fl4_dport = ports[!reverse];
  100. }
  101. break;
  102. case IPPROTO_ICMP:
  103. if (pskb_may_pull(skb, xprth + 2 - skb->data)) {
  104. u8 *icmp = xprth;
  105. fl4->fl4_icmp_type = icmp[0];
  106. fl4->fl4_icmp_code = icmp[1];
  107. }
  108. break;
  109. case IPPROTO_ESP:
  110. if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
  111. __be32 *ehdr = (__be32 *)xprth;
  112. fl4->fl4_ipsec_spi = ehdr[0];
  113. }
  114. break;
  115. case IPPROTO_AH:
  116. if (pskb_may_pull(skb, xprth + 8 - skb->data)) {
  117. __be32 *ah_hdr = (__be32 *)xprth;
  118. fl4->fl4_ipsec_spi = ah_hdr[1];
  119. }
  120. break;
  121. case IPPROTO_COMP:
  122. if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
  123. __be16 *ipcomp_hdr = (__be16 *)xprth;
  124. fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
  125. }
  126. break;
  127. case IPPROTO_GRE:
  128. if (pskb_may_pull(skb, xprth + 12 - skb->data)) {
  129. __be16 *greflags = (__be16 *)xprth;
  130. __be32 *gre_hdr = (__be32 *)xprth;
  131. if (greflags[0] & GRE_KEY) {
  132. if (greflags[0] & GRE_CSUM)
  133. gre_hdr++;
  134. fl4->fl4_gre_key = gre_hdr[1];
  135. }
  136. }
  137. break;
  138. default:
  139. fl4->fl4_ipsec_spi = 0;
  140. break;
  141. }
  142. }
  143. fl4->flowi4_proto = iph->protocol;
  144. fl4->daddr = reverse ? iph->saddr : iph->daddr;
  145. fl4->saddr = reverse ? iph->daddr : iph->saddr;
  146. fl4->flowi4_tos = iph->tos;
  147. }
  148. static inline int xfrm4_garbage_collect(struct dst_ops *ops)
  149. {
  150. struct net *net = container_of(ops, struct net, xfrm.xfrm4_dst_ops);
  151. xfrm4_policy_afinfo.garbage_collect(net);
  152. return (dst_entries_get_slow(ops) > ops->gc_thresh * 2);
  153. }
  154. static void xfrm4_update_pmtu(struct dst_entry *dst, struct sock *sk,
  155. struct sk_buff *skb, u32 mtu)
  156. {
  157. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  158. struct dst_entry *path = xdst->route;
  159. path->ops->update_pmtu(path, sk, skb, mtu);
  160. }
  161. static void xfrm4_redirect(struct dst_entry *dst, struct sock *sk,
  162. struct sk_buff *skb)
  163. {
  164. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  165. struct dst_entry *path = xdst->route;
  166. path->ops->redirect(path, sk, skb);
  167. }
  168. static void xfrm4_dst_destroy(struct dst_entry *dst)
  169. {
  170. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  171. dst_destroy_metrics_generic(dst);
  172. xfrm_dst_destroy(xdst);
  173. }
  174. static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  175. int unregister)
  176. {
  177. if (!unregister)
  178. return;
  179. xfrm_dst_ifdown(dst, dev);
  180. }
  181. static struct dst_ops xfrm4_dst_ops = {
  182. .family = AF_INET,
  183. .protocol = cpu_to_be16(ETH_P_IP),
  184. .gc = xfrm4_garbage_collect,
  185. .update_pmtu = xfrm4_update_pmtu,
  186. .redirect = xfrm4_redirect,
  187. .cow_metrics = dst_cow_metrics_generic,
  188. .destroy = xfrm4_dst_destroy,
  189. .ifdown = xfrm4_dst_ifdown,
  190. .local_out = __ip_local_out,
  191. .gc_thresh = 1024,
  192. };
  193. static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
  194. .family = AF_INET,
  195. .dst_ops = &xfrm4_dst_ops,
  196. .dst_lookup = xfrm4_dst_lookup,
  197. .get_saddr = xfrm4_get_saddr,
  198. .decode_session = _decode_session4,
  199. .get_tos = xfrm4_get_tos,
  200. .init_path = xfrm4_init_path,
  201. .fill_dst = xfrm4_fill_dst,
  202. .blackhole_route = ipv4_blackhole_route,
  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. dst_entries_init(&xfrm4_dst_ops);
  243. xfrm4_state_init();
  244. xfrm4_policy_init();
  245. #ifdef CONFIG_SYSCTL
  246. sysctl_hdr = register_net_sysctl(&init_net, "net/ipv4",
  247. xfrm4_policy_table);
  248. #endif
  249. }