/net/ipv4/xfrm4_policy.c

http://github.com/mirrors/linux · C · 261 lines · 204 code · 45 blank · 12 comment · 18 complexity · e4e1d72bd9dbd0cd1155fcab0e5bb0d0 MD5 · raw file

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