xfrm4_policy.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * xfrm4_policy.c
  4. *
  5. * Changes:
  6. * Kazunori MIYAZAWA @USAGI
  7. * YOSHIFUJI Hideaki @USAGI
  8. * Split up af-specific portion
  9. *
  10. */
  11. #include <linux/err.h>
  12. #include <linux/kernel.h>
  13. #include <linux/inetdevice.h>
  14. #include <net/dst.h>
  15. #include <net/xfrm.h>
  16. #include <net/ip.h>
  17. #include <net/l3mdev.h>
  18. static struct dst_entry *__xfrm4_dst_lookup(struct net *net, struct flowi4 *fl4,
  19. int tos, int oif,
  20. const xfrm_address_t *saddr,
  21. const xfrm_address_t *daddr,
  22. u32 mark)
  23. {
  24. struct rtable *rt;
  25. memset(fl4, 0, sizeof(*fl4));
  26. fl4->daddr = daddr->a4;
  27. fl4->flowi4_tos = tos;
  28. fl4->flowi4_l3mdev = l3mdev_master_ifindex_by_index(net, oif);
  29. fl4->flowi4_mark = mark;
  30. if (saddr)
  31. fl4->saddr = saddr->a4;
  32. rt = __ip_route_output_key(net, fl4);
  33. if (!IS_ERR(rt))
  34. return &rt->dst;
  35. return ERR_CAST(rt);
  36. }
  37. static struct dst_entry *xfrm4_dst_lookup(struct net *net, int tos, int oif,
  38. const xfrm_address_t *saddr,
  39. const xfrm_address_t *daddr,
  40. u32 mark)
  41. {
  42. struct flowi4 fl4;
  43. return __xfrm4_dst_lookup(net, &fl4, tos, oif, saddr, daddr, mark);
  44. }
  45. static int xfrm4_get_saddr(struct net *net, int oif,
  46. xfrm_address_t *saddr, xfrm_address_t *daddr,
  47. u32 mark)
  48. {
  49. struct dst_entry *dst;
  50. struct flowi4 fl4;
  51. dst = __xfrm4_dst_lookup(net, &fl4, 0, oif, NULL, daddr, mark);
  52. if (IS_ERR(dst))
  53. return -EHOSTUNREACH;
  54. saddr->a4 = fl4.saddr;
  55. dst_release(dst);
  56. return 0;
  57. }
  58. static int xfrm4_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
  59. const struct flowi *fl)
  60. {
  61. struct rtable *rt = (struct rtable *)xdst->route;
  62. const struct flowi4 *fl4 = &fl->u.ip4;
  63. xdst->u.rt.rt_iif = fl4->flowi4_iif;
  64. xdst->u.dst.dev = dev;
  65. netdev_hold(dev, &xdst->u.dst.dev_tracker, GFP_ATOMIC);
  66. /* Sheit... I remember I did this right. Apparently,
  67. * it was magically lost, so this code needs audit */
  68. xdst->u.rt.rt_is_input = rt->rt_is_input;
  69. xdst->u.rt.rt_flags = rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST |
  70. RTCF_LOCAL);
  71. xdst->u.rt.rt_type = rt->rt_type;
  72. xdst->u.rt.rt_uses_gateway = rt->rt_uses_gateway;
  73. xdst->u.rt.rt_gw_family = rt->rt_gw_family;
  74. if (rt->rt_gw_family == AF_INET)
  75. xdst->u.rt.rt_gw4 = rt->rt_gw4;
  76. else if (rt->rt_gw_family == AF_INET6)
  77. xdst->u.rt.rt_gw6 = rt->rt_gw6;
  78. xdst->u.rt.rt_pmtu = rt->rt_pmtu;
  79. xdst->u.rt.rt_mtu_locked = rt->rt_mtu_locked;
  80. INIT_LIST_HEAD(&xdst->u.rt.rt_uncached);
  81. rt_add_uncached_list(&xdst->u.rt);
  82. return 0;
  83. }
  84. static void xfrm4_update_pmtu(struct dst_entry *dst, struct sock *sk,
  85. struct sk_buff *skb, u32 mtu,
  86. bool confirm_neigh)
  87. {
  88. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  89. struct dst_entry *path = xdst->route;
  90. path->ops->update_pmtu(path, sk, skb, mtu, confirm_neigh);
  91. }
  92. static void xfrm4_redirect(struct dst_entry *dst, struct sock *sk,
  93. struct sk_buff *skb)
  94. {
  95. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  96. struct dst_entry *path = xdst->route;
  97. path->ops->redirect(path, sk, skb);
  98. }
  99. static void xfrm4_dst_destroy(struct dst_entry *dst)
  100. {
  101. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  102. dst_destroy_metrics_generic(dst);
  103. if (xdst->u.rt.rt_uncached_list)
  104. rt_del_uncached_list(&xdst->u.rt);
  105. xfrm_dst_destroy(xdst);
  106. }
  107. static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  108. int unregister)
  109. {
  110. if (!unregister)
  111. return;
  112. xfrm_dst_ifdown(dst, dev);
  113. }
  114. static struct dst_ops xfrm4_dst_ops_template = {
  115. .family = AF_INET,
  116. .update_pmtu = xfrm4_update_pmtu,
  117. .redirect = xfrm4_redirect,
  118. .cow_metrics = dst_cow_metrics_generic,
  119. .destroy = xfrm4_dst_destroy,
  120. .ifdown = xfrm4_dst_ifdown,
  121. .local_out = __ip_local_out,
  122. .gc_thresh = 32768,
  123. };
  124. static const struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
  125. .dst_ops = &xfrm4_dst_ops_template,
  126. .dst_lookup = xfrm4_dst_lookup,
  127. .get_saddr = xfrm4_get_saddr,
  128. .fill_dst = xfrm4_fill_dst,
  129. .blackhole_route = ipv4_blackhole_route,
  130. };
  131. #ifdef CONFIG_SYSCTL
  132. static struct ctl_table xfrm4_policy_table[] = {
  133. {
  134. .procname = "xfrm4_gc_thresh",
  135. .data = &init_net.xfrm.xfrm4_dst_ops.gc_thresh,
  136. .maxlen = sizeof(int),
  137. .mode = 0644,
  138. .proc_handler = proc_dointvec,
  139. },
  140. { }
  141. };
  142. static __net_init int xfrm4_net_sysctl_init(struct net *net)
  143. {
  144. struct ctl_table *table;
  145. struct ctl_table_header *hdr;
  146. table = xfrm4_policy_table;
  147. if (!net_eq(net, &init_net)) {
  148. table = kmemdup(table, sizeof(xfrm4_policy_table), GFP_KERNEL);
  149. if (!table)
  150. goto err_alloc;
  151. table[0].data = &net->xfrm.xfrm4_dst_ops.gc_thresh;
  152. }
  153. hdr = register_net_sysctl(net, "net/ipv4", table);
  154. if (!hdr)
  155. goto err_reg;
  156. net->ipv4.xfrm4_hdr = hdr;
  157. return 0;
  158. err_reg:
  159. if (!net_eq(net, &init_net))
  160. kfree(table);
  161. err_alloc:
  162. return -ENOMEM;
  163. }
  164. static __net_exit void xfrm4_net_sysctl_exit(struct net *net)
  165. {
  166. struct ctl_table *table;
  167. if (!net->ipv4.xfrm4_hdr)
  168. return;
  169. table = net->ipv4.xfrm4_hdr->ctl_table_arg;
  170. unregister_net_sysctl_table(net->ipv4.xfrm4_hdr);
  171. if (!net_eq(net, &init_net))
  172. kfree(table);
  173. }
  174. #else /* CONFIG_SYSCTL */
  175. static inline int xfrm4_net_sysctl_init(struct net *net)
  176. {
  177. return 0;
  178. }
  179. static inline void xfrm4_net_sysctl_exit(struct net *net)
  180. {
  181. }
  182. #endif
  183. static int __net_init xfrm4_net_init(struct net *net)
  184. {
  185. int ret;
  186. memcpy(&net->xfrm.xfrm4_dst_ops, &xfrm4_dst_ops_template,
  187. sizeof(xfrm4_dst_ops_template));
  188. ret = dst_entries_init(&net->xfrm.xfrm4_dst_ops);
  189. if (ret)
  190. return ret;
  191. ret = xfrm4_net_sysctl_init(net);
  192. if (ret)
  193. dst_entries_destroy(&net->xfrm.xfrm4_dst_ops);
  194. return ret;
  195. }
  196. static void __net_exit xfrm4_net_exit(struct net *net)
  197. {
  198. xfrm4_net_sysctl_exit(net);
  199. dst_entries_destroy(&net->xfrm.xfrm4_dst_ops);
  200. }
  201. static struct pernet_operations __net_initdata xfrm4_net_ops = {
  202. .init = xfrm4_net_init,
  203. .exit = xfrm4_net_exit,
  204. };
  205. static void __init xfrm4_policy_init(void)
  206. {
  207. xfrm_policy_register_afinfo(&xfrm4_policy_afinfo, AF_INET);
  208. }
  209. void __init xfrm4_init(void)
  210. {
  211. xfrm4_state_init();
  212. xfrm4_policy_init();
  213. xfrm4_protocol_init();
  214. register_pernet_subsys(&xfrm4_net_ops);
  215. }