xfrm4_policy.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. .nl_u = {
  24. .ip4_u = {
  25. .tos = tos,
  26. .daddr = daddr->a4,
  27. },
  28. },
  29. };
  30. struct dst_entry *dst;
  31. struct rtable *rt;
  32. int err;
  33. if (saddr)
  34. fl.fl4_src = saddr->a4;
  35. err = __ip_route_output_key(net, &rt, &fl);
  36. dst = &rt->dst;
  37. if (err)
  38. dst = ERR_PTR(err);
  39. return dst;
  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 rtable *rt;
  46. dst = xfrm4_dst_lookup(net, 0, NULL, daddr);
  47. if (IS_ERR(dst))
  48. return -EHOSTUNREACH;
  49. rt = (struct rtable *)dst;
  50. saddr->a4 = rt->rt_src;
  51. dst_release(dst);
  52. return 0;
  53. }
  54. static int xfrm4_get_tos(struct flowi *fl)
  55. {
  56. return IPTOS_RT_MASK & fl->fl4_tos; /* Strip ECN bits */
  57. }
  58. static int xfrm4_init_path(struct xfrm_dst *path, struct dst_entry *dst,
  59. int nfheader_len)
  60. {
  61. return 0;
  62. }
  63. static int xfrm4_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
  64. struct flowi *fl)
  65. {
  66. struct rtable *rt = (struct rtable *)xdst->route;
  67. xdst->u.rt.fl = *fl;
  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. memset(fl, 0, sizeof(struct flowi));
  90. fl->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. fl->fl_ip_sport = ports[!!reverse];
  102. fl->fl_ip_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. fl->fl_icmp_type = icmp[0];
  109. fl->fl_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. fl->fl_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. fl->fl_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. fl->fl_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. fl->fl_gre_key = gre_hdr[1];
  138. }
  139. }
  140. break;
  141. default:
  142. fl->fl_ipsec_spi = 0;
  143. break;
  144. }
  145. }
  146. fl->proto = iph->protocol;
  147. fl->fl4_dst = reverse ? iph->saddr : iph->daddr;
  148. fl->fl4_src = reverse ? iph->daddr : iph->saddr;
  149. fl->fl4_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. if (likely(xdst->u.rt.peer))
  167. inet_putpeer(xdst->u.rt.peer);
  168. xfrm_dst_destroy(xdst);
  169. }
  170. static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  171. int unregister)
  172. {
  173. if (!unregister)
  174. return;
  175. xfrm_dst_ifdown(dst, dev);
  176. }
  177. static struct dst_ops xfrm4_dst_ops = {
  178. .family = AF_INET,
  179. .protocol = cpu_to_be16(ETH_P_IP),
  180. .gc = xfrm4_garbage_collect,
  181. .update_pmtu = xfrm4_update_pmtu,
  182. .destroy = xfrm4_dst_destroy,
  183. .ifdown = xfrm4_dst_ifdown,
  184. .local_out = __ip_local_out,
  185. .gc_thresh = 1024,
  186. };
  187. static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
  188. .family = AF_INET,
  189. .dst_ops = &xfrm4_dst_ops,
  190. .dst_lookup = xfrm4_dst_lookup,
  191. .get_saddr = xfrm4_get_saddr,
  192. .decode_session = _decode_session4,
  193. .get_tos = xfrm4_get_tos,
  194. .init_path = xfrm4_init_path,
  195. .fill_dst = xfrm4_fill_dst,
  196. };
  197. #ifdef CONFIG_SYSCTL
  198. static struct ctl_table xfrm4_policy_table[] = {
  199. {
  200. .procname = "xfrm4_gc_thresh",
  201. .data = &init_net.xfrm.xfrm4_dst_ops.gc_thresh,
  202. .maxlen = sizeof(int),
  203. .mode = 0644,
  204. .proc_handler = proc_dointvec,
  205. },
  206. { }
  207. };
  208. static struct ctl_table_header *sysctl_hdr;
  209. #endif
  210. static void __init xfrm4_policy_init(void)
  211. {
  212. xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
  213. }
  214. static void __exit xfrm4_policy_fini(void)
  215. {
  216. #ifdef CONFIG_SYSCTL
  217. if (sysctl_hdr)
  218. unregister_net_sysctl_table(sysctl_hdr);
  219. #endif
  220. xfrm_policy_unregister_afinfo(&xfrm4_policy_afinfo);
  221. }
  222. void __init xfrm4_init(int rt_max_size)
  223. {
  224. /*
  225. * Select a default value for the gc_thresh based on the main route
  226. * table hash size. It seems to me the worst case scenario is when
  227. * we have ipsec operating in transport mode, in which we create a
  228. * dst_entry per socket. The xfrm gc algorithm starts trying to remove
  229. * entries at gc_thresh, and prevents new allocations as 2*gc_thresh
  230. * so lets set an initial xfrm gc_thresh value at the rt_max_size/2.
  231. * That will let us store an ipsec connection per route table entry,
  232. * and start cleaning when were 1/2 full
  233. */
  234. xfrm4_dst_ops.gc_thresh = rt_max_size/2;
  235. dst_entries_init(&xfrm4_dst_ops);
  236. xfrm4_state_init();
  237. xfrm4_policy_init();
  238. #ifdef CONFIG_SYSCTL
  239. sysctl_hdr = register_net_sysctl_table(&init_net, net_ipv4_ctl_path,
  240. xfrm4_policy_table);
  241. #endif
  242. }