xfrm4_policy.c 6.9 KB

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