xfrm4_policy.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 <linux/if_tunnel.h>
  15. #include <net/dst.h>
  16. #include <net/xfrm.h>
  17. #include <net/ip.h>
  18. #include <net/l3mdev.h>
  19. static struct dst_entry *__xfrm4_dst_lookup(struct net *net, struct flowi4 *fl4,
  20. int tos, int oif,
  21. const xfrm_address_t *saddr,
  22. const xfrm_address_t *daddr,
  23. u32 mark)
  24. {
  25. struct rtable *rt;
  26. memset(fl4, 0, sizeof(*fl4));
  27. fl4->daddr = daddr->a4;
  28. fl4->flowi4_tos = tos;
  29. fl4->flowi4_oif = l3mdev_master_ifindex_by_index(net, oif);
  30. fl4->flowi4_mark = mark;
  31. if (saddr)
  32. fl4->saddr = saddr->a4;
  33. fl4->flowi4_flags = FLOWI_FLAG_SKIP_NH_OIF;
  34. rt = __ip_route_output_key(net, fl4);
  35. if (!IS_ERR(rt))
  36. return &rt->dst;
  37. return ERR_CAST(rt);
  38. }
  39. static struct dst_entry *xfrm4_dst_lookup(struct net *net, int tos, int oif,
  40. const xfrm_address_t *saddr,
  41. const xfrm_address_t *daddr,
  42. u32 mark)
  43. {
  44. struct flowi4 fl4;
  45. return __xfrm4_dst_lookup(net, &fl4, tos, oif, saddr, daddr, mark);
  46. }
  47. static int xfrm4_get_saddr(struct net *net, int oif,
  48. xfrm_address_t *saddr, xfrm_address_t *daddr,
  49. u32 mark)
  50. {
  51. struct dst_entry *dst;
  52. struct flowi4 fl4;
  53. dst = __xfrm4_dst_lookup(net, &fl4, 0, oif, NULL, daddr, mark);
  54. if (IS_ERR(dst))
  55. return -EHOSTUNREACH;
  56. saddr->a4 = fl4.saddr;
  57. dst_release(dst);
  58. return 0;
  59. }
  60. static int xfrm4_get_tos(const struct flowi *fl)
  61. {
  62. return IPTOS_RT_MASK & fl->u.ip4.flowi4_tos; /* Strip ECN bits */
  63. }
  64. static int xfrm4_init_path(struct xfrm_dst *path, struct dst_entry *dst,
  65. int nfheader_len)
  66. {
  67. return 0;
  68. }
  69. static int xfrm4_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
  70. const struct flowi *fl)
  71. {
  72. struct rtable *rt = (struct rtable *)xdst->route;
  73. const struct flowi4 *fl4 = &fl->u.ip4;
  74. xdst->u.rt.rt_iif = fl4->flowi4_iif;
  75. xdst->u.dst.dev = dev;
  76. dev_hold(dev);
  77. /* Sheit... I remember I did this right. Apparently,
  78. * it was magically lost, so this code needs audit */
  79. xdst->u.rt.rt_is_input = rt->rt_is_input;
  80. xdst->u.rt.rt_flags = rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST |
  81. RTCF_LOCAL);
  82. xdst->u.rt.rt_type = rt->rt_type;
  83. xdst->u.rt.rt_gateway = rt->rt_gateway;
  84. xdst->u.rt.rt_uses_gateway = rt->rt_uses_gateway;
  85. xdst->u.rt.rt_pmtu = rt->rt_pmtu;
  86. xdst->u.rt.rt_mtu_locked = rt->rt_mtu_locked;
  87. INIT_LIST_HEAD(&xdst->u.rt.rt_uncached);
  88. rt_add_uncached_list(&xdst->u.rt);
  89. return 0;
  90. }
  91. static void
  92. _decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse)
  93. {
  94. const struct iphdr *iph = ip_hdr(skb);
  95. int ihl = iph->ihl;
  96. u8 *xprth = skb_network_header(skb) + ihl * 4;
  97. struct flowi4 *fl4 = &fl->u.ip4;
  98. int oif = 0;
  99. if (skb_dst(skb))
  100. oif = skb_dst(skb)->dev->ifindex;
  101. memset(fl4, 0, sizeof(struct flowi4));
  102. fl4->flowi4_mark = skb->mark;
  103. fl4->flowi4_oif = reverse ? skb->skb_iif : oif;
  104. fl4->flowi4_proto = iph->protocol;
  105. fl4->daddr = reverse ? iph->saddr : iph->daddr;
  106. fl4->saddr = reverse ? iph->daddr : iph->saddr;
  107. fl4->flowi4_tos = iph->tos;
  108. if (!ip_is_fragment(iph)) {
  109. switch (iph->protocol) {
  110. case IPPROTO_UDP:
  111. case IPPROTO_UDPLITE:
  112. case IPPROTO_TCP:
  113. case IPPROTO_SCTP:
  114. case IPPROTO_DCCP:
  115. if (xprth + 4 < skb->data ||
  116. pskb_may_pull(skb, xprth + 4 - skb->data)) {
  117. __be16 *ports;
  118. xprth = skb_network_header(skb) + ihl * 4;
  119. ports = (__be16 *)xprth;
  120. fl4->fl4_sport = ports[!!reverse];
  121. fl4->fl4_dport = ports[!reverse];
  122. }
  123. break;
  124. case IPPROTO_ICMP:
  125. if (xprth + 2 < skb->data ||
  126. pskb_may_pull(skb, xprth + 2 - skb->data)) {
  127. u8 *icmp;
  128. xprth = skb_network_header(skb) + ihl * 4;
  129. icmp = xprth;
  130. fl4->fl4_icmp_type = icmp[0];
  131. fl4->fl4_icmp_code = icmp[1];
  132. }
  133. break;
  134. case IPPROTO_ESP:
  135. if (xprth + 4 < skb->data ||
  136. pskb_may_pull(skb, xprth + 4 - skb->data)) {
  137. __be32 *ehdr;
  138. xprth = skb_network_header(skb) + ihl * 4;
  139. ehdr = (__be32 *)xprth;
  140. fl4->fl4_ipsec_spi = ehdr[0];
  141. }
  142. break;
  143. case IPPROTO_AH:
  144. if (xprth + 8 < skb->data ||
  145. pskb_may_pull(skb, xprth + 8 - skb->data)) {
  146. __be32 *ah_hdr;
  147. xprth = skb_network_header(skb) + ihl * 4;
  148. ah_hdr = (__be32 *)xprth;
  149. fl4->fl4_ipsec_spi = ah_hdr[1];
  150. }
  151. break;
  152. case IPPROTO_COMP:
  153. if (xprth + 4 < skb->data ||
  154. pskb_may_pull(skb, xprth + 4 - skb->data)) {
  155. __be16 *ipcomp_hdr;
  156. xprth = skb_network_header(skb) + ihl * 4;
  157. ipcomp_hdr = (__be16 *)xprth;
  158. fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
  159. }
  160. break;
  161. case IPPROTO_GRE:
  162. if (xprth + 12 < skb->data ||
  163. pskb_may_pull(skb, xprth + 12 - skb->data)) {
  164. __be16 *greflags;
  165. __be32 *gre_hdr;
  166. xprth = skb_network_header(skb) + ihl * 4;
  167. greflags = (__be16 *)xprth;
  168. gre_hdr = (__be32 *)xprth;
  169. if (greflags[0] & GRE_KEY) {
  170. if (greflags[0] & GRE_CSUM)
  171. gre_hdr++;
  172. fl4->fl4_gre_key = gre_hdr[1];
  173. }
  174. }
  175. break;
  176. default:
  177. fl4->fl4_ipsec_spi = 0;
  178. break;
  179. }
  180. }
  181. }
  182. static void xfrm4_update_pmtu(struct dst_entry *dst, struct sock *sk,
  183. struct sk_buff *skb, u32 mtu,
  184. bool confirm_neigh)
  185. {
  186. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  187. struct dst_entry *path = xdst->route;
  188. path->ops->update_pmtu(path, sk, skb, mtu, confirm_neigh);
  189. }
  190. static void xfrm4_redirect(struct dst_entry *dst, struct sock *sk,
  191. struct sk_buff *skb)
  192. {
  193. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  194. struct dst_entry *path = xdst->route;
  195. path->ops->redirect(path, sk, skb);
  196. }
  197. static void xfrm4_dst_destroy(struct dst_entry *dst)
  198. {
  199. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  200. dst_destroy_metrics_generic(dst);
  201. if (xdst->u.rt.rt_uncached_list)
  202. rt_del_uncached_list(&xdst->u.rt);
  203. xfrm_dst_destroy(xdst);
  204. }
  205. static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  206. int unregister)
  207. {
  208. if (!unregister)
  209. return;
  210. xfrm_dst_ifdown(dst, dev);
  211. }
  212. static struct dst_ops xfrm4_dst_ops_template = {
  213. .family = AF_INET,
  214. .update_pmtu = xfrm4_update_pmtu,
  215. .redirect = xfrm4_redirect,
  216. .cow_metrics = dst_cow_metrics_generic,
  217. .destroy = xfrm4_dst_destroy,
  218. .ifdown = xfrm4_dst_ifdown,
  219. .local_out = __ip_local_out,
  220. .gc_thresh = 32768,
  221. };
  222. static const struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
  223. .dst_ops = &xfrm4_dst_ops_template,
  224. .dst_lookup = xfrm4_dst_lookup,
  225. .get_saddr = xfrm4_get_saddr,
  226. .decode_session = _decode_session4,
  227. .get_tos = xfrm4_get_tos,
  228. .init_path = xfrm4_init_path,
  229. .fill_dst = xfrm4_fill_dst,
  230. .blackhole_route = ipv4_blackhole_route,
  231. };
  232. #ifdef CONFIG_SYSCTL
  233. static struct ctl_table xfrm4_policy_table[] = {
  234. {
  235. .procname = "xfrm4_gc_thresh",
  236. .data = &init_net.xfrm.xfrm4_dst_ops.gc_thresh,
  237. .maxlen = sizeof(int),
  238. .mode = 0644,
  239. .proc_handler = proc_dointvec,
  240. },
  241. { }
  242. };
  243. static __net_init int xfrm4_net_sysctl_init(struct net *net)
  244. {
  245. struct ctl_table *table;
  246. struct ctl_table_header *hdr;
  247. table = xfrm4_policy_table;
  248. if (!net_eq(net, &init_net)) {
  249. table = kmemdup(table, sizeof(xfrm4_policy_table), GFP_KERNEL);
  250. if (!table)
  251. goto err_alloc;
  252. table[0].data = &net->xfrm.xfrm4_dst_ops.gc_thresh;
  253. }
  254. hdr = register_net_sysctl(net, "net/ipv4", table);
  255. if (!hdr)
  256. goto err_reg;
  257. net->ipv4.xfrm4_hdr = hdr;
  258. return 0;
  259. err_reg:
  260. if (!net_eq(net, &init_net))
  261. kfree(table);
  262. err_alloc:
  263. return -ENOMEM;
  264. }
  265. static __net_exit void xfrm4_net_sysctl_exit(struct net *net)
  266. {
  267. struct ctl_table *table;
  268. if (!net->ipv4.xfrm4_hdr)
  269. return;
  270. table = net->ipv4.xfrm4_hdr->ctl_table_arg;
  271. unregister_net_sysctl_table(net->ipv4.xfrm4_hdr);
  272. if (!net_eq(net, &init_net))
  273. kfree(table);
  274. }
  275. #else /* CONFIG_SYSCTL */
  276. static inline int xfrm4_net_sysctl_init(struct net *net)
  277. {
  278. return 0;
  279. }
  280. static inline void xfrm4_net_sysctl_exit(struct net *net)
  281. {
  282. }
  283. #endif
  284. static int __net_init xfrm4_net_init(struct net *net)
  285. {
  286. int ret;
  287. memcpy(&net->xfrm.xfrm4_dst_ops, &xfrm4_dst_ops_template,
  288. sizeof(xfrm4_dst_ops_template));
  289. ret = dst_entries_init(&net->xfrm.xfrm4_dst_ops);
  290. if (ret)
  291. return ret;
  292. ret = xfrm4_net_sysctl_init(net);
  293. if (ret)
  294. dst_entries_destroy(&net->xfrm.xfrm4_dst_ops);
  295. return ret;
  296. }
  297. static void __net_exit xfrm4_net_exit(struct net *net)
  298. {
  299. xfrm4_net_sysctl_exit(net);
  300. dst_entries_destroy(&net->xfrm.xfrm4_dst_ops);
  301. }
  302. static struct pernet_operations __net_initdata xfrm4_net_ops = {
  303. .init = xfrm4_net_init,
  304. .exit = xfrm4_net_exit,
  305. };
  306. static void __init xfrm4_policy_init(void)
  307. {
  308. xfrm_policy_register_afinfo(&xfrm4_policy_afinfo, AF_INET);
  309. }
  310. void __init xfrm4_init(void)
  311. {
  312. xfrm4_state_init();
  313. xfrm4_policy_init();
  314. xfrm4_protocol_init();
  315. register_pernet_subsys(&xfrm4_net_ops);
  316. }