xfrm4_policy.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. xfrm_address_t *saddr,
  20. xfrm_address_t *daddr)
  21. {
  22. struct flowi fl = {
  23. .fl4_dst = daddr->a4,
  24. .fl4_tos = tos,
  25. };
  26. struct dst_entry *dst;
  27. struct rtable *rt;
  28. int err;
  29. if (saddr)
  30. fl.fl4_src = saddr->a4;
  31. err = __ip_route_output_key(net, &rt, &fl);
  32. dst = &rt->dst;
  33. if (err)
  34. dst = ERR_PTR(err);
  35. return dst;
  36. }
  37. static int xfrm4_get_saddr(struct net *net,
  38. xfrm_address_t *saddr, xfrm_address_t *daddr)
  39. {
  40. struct dst_entry *dst;
  41. struct rtable *rt;
  42. dst = xfrm4_dst_lookup(net, 0, NULL, daddr);
  43. if (IS_ERR(dst))
  44. return -EHOSTUNREACH;
  45. rt = (struct rtable *)dst;
  46. saddr->a4 = rt->rt_src;
  47. dst_release(dst);
  48. return 0;
  49. }
  50. static int xfrm4_get_tos(struct flowi *fl)
  51. {
  52. return IPTOS_RT_MASK & fl->fl4_tos; /* Strip ECN bits */
  53. }
  54. static int xfrm4_init_path(struct xfrm_dst *path, struct dst_entry *dst,
  55. int nfheader_len)
  56. {
  57. return 0;
  58. }
  59. static int xfrm4_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
  60. struct flowi *fl)
  61. {
  62. struct rtable *rt = (struct rtable *)xdst->route;
  63. xdst->u.rt.fl = *fl;
  64. xdst->u.dst.dev = dev;
  65. dev_hold(dev);
  66. xdst->u.rt.peer = rt->peer;
  67. if (rt->peer)
  68. atomic_inc(&rt->peer->refcnt);
  69. /* Sheit... I remember I did this right. Apparently,
  70. * it was magically lost, so this code needs audit */
  71. xdst->u.rt.rt_flags = rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST |
  72. RTCF_LOCAL);
  73. xdst->u.rt.rt_type = rt->rt_type;
  74. xdst->u.rt.rt_src = rt->rt_src;
  75. xdst->u.rt.rt_dst = rt->rt_dst;
  76. xdst->u.rt.rt_gateway = rt->rt_gateway;
  77. xdst->u.rt.rt_spec_dst = rt->rt_spec_dst;
  78. return 0;
  79. }
  80. static void
  81. _decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse)
  82. {
  83. struct iphdr *iph = ip_hdr(skb);
  84. u8 *xprth = skb_network_header(skb) + iph->ihl * 4;
  85. memset(fl, 0, sizeof(struct flowi));
  86. fl->mark = skb->mark;
  87. if (!(iph->frag_off & htons(IP_MF | IP_OFFSET))) {
  88. switch (iph->protocol) {
  89. case IPPROTO_UDP:
  90. case IPPROTO_UDPLITE:
  91. case IPPROTO_TCP:
  92. case IPPROTO_SCTP:
  93. case IPPROTO_DCCP:
  94. if (xprth + 4 < skb->data ||
  95. pskb_may_pull(skb, xprth + 4 - skb->data)) {
  96. __be16 *ports = (__be16 *)xprth;
  97. fl->fl_ip_sport = ports[!!reverse];
  98. fl->fl_ip_dport = ports[!reverse];
  99. }
  100. break;
  101. case IPPROTO_ICMP:
  102. if (pskb_may_pull(skb, xprth + 2 - skb->data)) {
  103. u8 *icmp = xprth;
  104. fl->fl_icmp_type = icmp[0];
  105. fl->fl_icmp_code = icmp[1];
  106. }
  107. break;
  108. case IPPROTO_ESP:
  109. if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
  110. __be32 *ehdr = (__be32 *)xprth;
  111. fl->fl_ipsec_spi = ehdr[0];
  112. }
  113. break;
  114. case IPPROTO_AH:
  115. if (pskb_may_pull(skb, xprth + 8 - skb->data)) {
  116. __be32 *ah_hdr = (__be32*)xprth;
  117. fl->fl_ipsec_spi = ah_hdr[1];
  118. }
  119. break;
  120. case IPPROTO_COMP:
  121. if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
  122. __be16 *ipcomp_hdr = (__be16 *)xprth;
  123. fl->fl_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
  124. }
  125. break;
  126. case IPPROTO_GRE:
  127. if (pskb_may_pull(skb, xprth + 12 - skb->data)) {
  128. __be16 *greflags = (__be16 *)xprth;
  129. __be32 *gre_hdr = (__be32 *)xprth;
  130. if (greflags[0] & GRE_KEY) {
  131. if (greflags[0] & GRE_CSUM)
  132. gre_hdr++;
  133. fl->fl_gre_key = gre_hdr[1];
  134. }
  135. }
  136. break;
  137. default:
  138. fl->fl_ipsec_spi = 0;
  139. break;
  140. }
  141. }
  142. fl->proto = iph->protocol;
  143. fl->fl4_dst = reverse ? iph->saddr : iph->daddr;
  144. fl->fl4_src = reverse ? iph->daddr : iph->saddr;
  145. fl->fl4_tos = iph->tos;
  146. }
  147. static inline int xfrm4_garbage_collect(struct dst_ops *ops)
  148. {
  149. struct net *net = container_of(ops, struct net, xfrm.xfrm4_dst_ops);
  150. xfrm4_policy_afinfo.garbage_collect(net);
  151. return (dst_entries_get_slow(ops) > ops->gc_thresh * 2);
  152. }
  153. static void xfrm4_update_pmtu(struct dst_entry *dst, u32 mtu)
  154. {
  155. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  156. struct dst_entry *path = xdst->route;
  157. path->ops->update_pmtu(path, mtu);
  158. }
  159. static void xfrm4_dst_destroy(struct dst_entry *dst)
  160. {
  161. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  162. if (likely(xdst->u.rt.peer))
  163. inet_putpeer(xdst->u.rt.peer);
  164. xfrm_dst_destroy(xdst);
  165. }
  166. static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  167. int unregister)
  168. {
  169. if (!unregister)
  170. return;
  171. xfrm_dst_ifdown(dst, dev);
  172. }
  173. static struct dst_ops xfrm4_dst_ops = {
  174. .family = AF_INET,
  175. .protocol = cpu_to_be16(ETH_P_IP),
  176. .gc = xfrm4_garbage_collect,
  177. .update_pmtu = xfrm4_update_pmtu,
  178. .destroy = xfrm4_dst_destroy,
  179. .ifdown = xfrm4_dst_ifdown,
  180. .local_out = __ip_local_out,
  181. .gc_thresh = 1024,
  182. };
  183. static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
  184. .family = AF_INET,
  185. .dst_ops = &xfrm4_dst_ops,
  186. .dst_lookup = xfrm4_dst_lookup,
  187. .get_saddr = xfrm4_get_saddr,
  188. .decode_session = _decode_session4,
  189. .get_tos = xfrm4_get_tos,
  190. .init_path = xfrm4_init_path,
  191. .fill_dst = xfrm4_fill_dst,
  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. }