xfrm4_policy.c 5.6 KB

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