xfrm4_policy.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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_uses_gateway = rt->rt_uses_gateway;
  78. xdst->u.rt.rt_pmtu = rt->rt_pmtu;
  79. INIT_LIST_HEAD(&xdst->u.rt.rt_uncached);
  80. return 0;
  81. }
  82. static void
  83. _decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse)
  84. {
  85. const struct iphdr *iph = ip_hdr(skb);
  86. u8 *xprth = skb_network_header(skb) + iph->ihl * 4;
  87. struct flowi4 *fl4 = &fl->u.ip4;
  88. int oif = 0;
  89. if (skb_dst(skb))
  90. oif = skb_dst(skb)->dev->ifindex;
  91. memset(fl4, 0, sizeof(struct flowi4));
  92. fl4->flowi4_mark = skb->mark;
  93. fl4->flowi4_oif = reverse ? skb->skb_iif : oif;
  94. if (!ip_is_fragment(iph)) {
  95. switch (iph->protocol) {
  96. case IPPROTO_UDP:
  97. case IPPROTO_UDPLITE:
  98. case IPPROTO_TCP:
  99. case IPPROTO_SCTP:
  100. case IPPROTO_DCCP:
  101. if (xprth + 4 < skb->data ||
  102. pskb_may_pull(skb, xprth + 4 - skb->data)) {
  103. __be16 *ports = (__be16 *)xprth;
  104. fl4->fl4_sport = ports[!!reverse];
  105. fl4->fl4_dport = ports[!reverse];
  106. }
  107. break;
  108. case IPPROTO_ICMP:
  109. if (pskb_may_pull(skb, xprth + 2 - skb->data)) {
  110. u8 *icmp = xprth;
  111. fl4->fl4_icmp_type = icmp[0];
  112. fl4->fl4_icmp_code = icmp[1];
  113. }
  114. break;
  115. case IPPROTO_ESP:
  116. if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
  117. __be32 *ehdr = (__be32 *)xprth;
  118. fl4->fl4_ipsec_spi = ehdr[0];
  119. }
  120. break;
  121. case IPPROTO_AH:
  122. if (pskb_may_pull(skb, xprth + 8 - skb->data)) {
  123. __be32 *ah_hdr = (__be32 *)xprth;
  124. fl4->fl4_ipsec_spi = ah_hdr[1];
  125. }
  126. break;
  127. case IPPROTO_COMP:
  128. if (pskb_may_pull(skb, xprth + 4 - skb->data)) {
  129. __be16 *ipcomp_hdr = (__be16 *)xprth;
  130. fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
  131. }
  132. break;
  133. case IPPROTO_GRE:
  134. if (pskb_may_pull(skb, xprth + 12 - skb->data)) {
  135. __be16 *greflags = (__be16 *)xprth;
  136. __be32 *gre_hdr = (__be32 *)xprth;
  137. if (greflags[0] & GRE_KEY) {
  138. if (greflags[0] & GRE_CSUM)
  139. gre_hdr++;
  140. fl4->fl4_gre_key = gre_hdr[1];
  141. }
  142. }
  143. break;
  144. default:
  145. fl4->fl4_ipsec_spi = 0;
  146. break;
  147. }
  148. }
  149. fl4->flowi4_proto = iph->protocol;
  150. fl4->daddr = reverse ? iph->saddr : iph->daddr;
  151. fl4->saddr = reverse ? iph->daddr : iph->saddr;
  152. fl4->flowi4_tos = iph->tos;
  153. }
  154. static inline int xfrm4_garbage_collect(struct dst_ops *ops)
  155. {
  156. struct net *net = container_of(ops, struct net, xfrm.xfrm4_dst_ops);
  157. xfrm4_policy_afinfo.garbage_collect(net);
  158. return (dst_entries_get_slow(ops) > ops->gc_thresh * 2);
  159. }
  160. static void xfrm4_update_pmtu(struct dst_entry *dst, struct sock *sk,
  161. struct sk_buff *skb, u32 mtu)
  162. {
  163. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  164. struct dst_entry *path = xdst->route;
  165. path->ops->update_pmtu(path, sk, skb, mtu);
  166. }
  167. static void xfrm4_redirect(struct dst_entry *dst, struct sock *sk,
  168. struct sk_buff *skb)
  169. {
  170. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  171. struct dst_entry *path = xdst->route;
  172. path->ops->redirect(path, sk, skb);
  173. }
  174. static void xfrm4_dst_destroy(struct dst_entry *dst)
  175. {
  176. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  177. dst_destroy_metrics_generic(dst);
  178. xfrm_dst_destroy(xdst);
  179. }
  180. static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  181. int unregister)
  182. {
  183. if (!unregister)
  184. return;
  185. xfrm_dst_ifdown(dst, dev);
  186. }
  187. static struct dst_ops xfrm4_dst_ops = {
  188. .family = AF_INET,
  189. .gc = xfrm4_garbage_collect,
  190. .update_pmtu = xfrm4_update_pmtu,
  191. .redirect = xfrm4_redirect,
  192. .cow_metrics = dst_cow_metrics_generic,
  193. .destroy = xfrm4_dst_destroy,
  194. .ifdown = xfrm4_dst_ifdown,
  195. .local_out = __ip_local_out,
  196. .gc_thresh = 32768,
  197. };
  198. static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
  199. .family = AF_INET,
  200. .dst_ops = &xfrm4_dst_ops,
  201. .dst_lookup = xfrm4_dst_lookup,
  202. .get_saddr = xfrm4_get_saddr,
  203. .decode_session = _decode_session4,
  204. .get_tos = xfrm4_get_tos,
  205. .init_path = xfrm4_init_path,
  206. .fill_dst = xfrm4_fill_dst,
  207. .blackhole_route = ipv4_blackhole_route,
  208. };
  209. #ifdef CONFIG_SYSCTL
  210. static struct ctl_table xfrm4_policy_table[] = {
  211. {
  212. .procname = "xfrm4_gc_thresh",
  213. .data = &init_net.xfrm.xfrm4_dst_ops.gc_thresh,
  214. .maxlen = sizeof(int),
  215. .mode = 0644,
  216. .proc_handler = proc_dointvec,
  217. },
  218. { }
  219. };
  220. static int __net_init xfrm4_net_init(struct net *net)
  221. {
  222. struct ctl_table *table;
  223. struct ctl_table_header *hdr;
  224. table = xfrm4_policy_table;
  225. if (!net_eq(net, &init_net)) {
  226. table = kmemdup(table, sizeof(xfrm4_policy_table), GFP_KERNEL);
  227. if (!table)
  228. goto err_alloc;
  229. table[0].data = &net->xfrm.xfrm4_dst_ops.gc_thresh;
  230. }
  231. hdr = register_net_sysctl(net, "net/ipv4", table);
  232. if (!hdr)
  233. goto err_reg;
  234. net->ipv4.xfrm4_hdr = hdr;
  235. return 0;
  236. err_reg:
  237. if (!net_eq(net, &init_net))
  238. kfree(table);
  239. err_alloc:
  240. return -ENOMEM;
  241. }
  242. static void __net_exit xfrm4_net_exit(struct net *net)
  243. {
  244. struct ctl_table *table;
  245. if (!net->ipv4.xfrm4_hdr)
  246. return;
  247. table = net->ipv4.xfrm4_hdr->ctl_table_arg;
  248. unregister_net_sysctl_table(net->ipv4.xfrm4_hdr);
  249. if (!net_eq(net, &init_net))
  250. kfree(table);
  251. }
  252. static struct pernet_operations __net_initdata xfrm4_net_ops = {
  253. .init = xfrm4_net_init,
  254. .exit = xfrm4_net_exit,
  255. };
  256. #endif
  257. static void __init xfrm4_policy_init(void)
  258. {
  259. xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
  260. }
  261. void __init xfrm4_init(void)
  262. {
  263. dst_entries_init(&xfrm4_dst_ops);
  264. xfrm4_state_init();
  265. xfrm4_policy_init();
  266. xfrm4_protocol_init();
  267. #ifdef CONFIG_SYSCTL
  268. register_pernet_subsys(&xfrm4_net_ops);
  269. #endif
  270. }