xfrm4_policy.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. #include <net/l3mdev.h>
  18. static struct xfrm_policy_afinfo xfrm4_policy_afinfo;
  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. {
  24. struct rtable *rt;
  25. memset(fl4, 0, sizeof(*fl4));
  26. fl4->daddr = daddr->a4;
  27. fl4->flowi4_tos = tos;
  28. fl4->flowi4_oif = oif;
  29. if (saddr)
  30. fl4->saddr = saddr->a4;
  31. fl4->flowi4_flags = FLOWI_FLAG_SKIP_NH_OIF;
  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. {
  41. struct flowi4 fl4;
  42. return __xfrm4_dst_lookup(net, &fl4, tos, oif, saddr, daddr);
  43. }
  44. static int xfrm4_get_saddr(struct net *net, int oif,
  45. xfrm_address_t *saddr, xfrm_address_t *daddr)
  46. {
  47. struct dst_entry *dst;
  48. struct flowi4 fl4;
  49. dst = __xfrm4_dst_lookup(net, &fl4, 0, oif, NULL, daddr);
  50. if (IS_ERR(dst))
  51. return -EHOSTUNREACH;
  52. saddr->a4 = fl4.saddr;
  53. dst_release(dst);
  54. return 0;
  55. }
  56. static int xfrm4_get_tos(const struct flowi *fl)
  57. {
  58. return IPTOS_RT_MASK & fl->u.ip4.flowi4_tos; /* Strip ECN bits */
  59. }
  60. static int xfrm4_init_path(struct xfrm_dst *path, struct dst_entry *dst,
  61. int nfheader_len)
  62. {
  63. return 0;
  64. }
  65. static int xfrm4_fill_dst(struct xfrm_dst *xdst, struct net_device *dev,
  66. const struct flowi *fl)
  67. {
  68. struct rtable *rt = (struct rtable *)xdst->route;
  69. const struct flowi4 *fl4 = &fl->u.ip4;
  70. xdst->u.rt.rt_iif = fl4->flowi4_iif;
  71. xdst->u.dst.dev = dev;
  72. dev_hold(dev);
  73. /* Sheit... I remember I did this right. Apparently,
  74. * it was magically lost, so this code needs audit */
  75. xdst->u.rt.rt_is_input = rt->rt_is_input;
  76. xdst->u.rt.rt_flags = rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST |
  77. RTCF_LOCAL);
  78. xdst->u.rt.rt_type = rt->rt_type;
  79. xdst->u.rt.rt_gateway = rt->rt_gateway;
  80. xdst->u.rt.rt_uses_gateway = rt->rt_uses_gateway;
  81. xdst->u.rt.rt_pmtu = rt->rt_pmtu;
  82. xdst->u.rt.rt_mtu_locked = rt->rt_mtu_locked;
  83. xdst->u.rt.rt_table_id = rt->rt_table_id;
  84. INIT_LIST_HEAD(&xdst->u.rt.rt_uncached);
  85. return 0;
  86. }
  87. static void
  88. _decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse)
  89. {
  90. const struct iphdr *iph = ip_hdr(skb);
  91. u8 *xprth = skb_network_header(skb) + iph->ihl * 4;
  92. struct flowi4 *fl4 = &fl->u.ip4;
  93. int oif = 0;
  94. if (skb_dst(skb))
  95. oif = l3mdev_fib_oif(skb_dst(skb)->dev);
  96. memset(fl4, 0, sizeof(struct flowi4));
  97. fl4->flowi4_mark = skb->mark;
  98. fl4->flowi4_oif = reverse ? skb->skb_iif : oif;
  99. if (!ip_is_fragment(iph)) {
  100. switch (iph->protocol) {
  101. case IPPROTO_UDP:
  102. case IPPROTO_UDPLITE:
  103. case IPPROTO_TCP:
  104. case IPPROTO_SCTP:
  105. case IPPROTO_DCCP:
  106. if (xprth + 4 < skb->data ||
  107. pskb_may_pull(skb, xprth + 4 - skb->data)) {
  108. __be16 *ports;
  109. xprth = skb_network_header(skb) + iph->ihl * 4;
  110. ports = (__be16 *)xprth;
  111. fl4->fl4_sport = ports[!!reverse];
  112. fl4->fl4_dport = ports[!reverse];
  113. }
  114. break;
  115. case IPPROTO_ICMP:
  116. if (xprth + 2 < skb->data ||
  117. pskb_may_pull(skb, xprth + 2 - skb->data)) {
  118. u8 *icmp;
  119. xprth = skb_network_header(skb) + iph->ihl * 4;
  120. icmp = xprth;
  121. fl4->fl4_icmp_type = icmp[0];
  122. fl4->fl4_icmp_code = icmp[1];
  123. }
  124. break;
  125. case IPPROTO_ESP:
  126. if (xprth + 4 < skb->data ||
  127. pskb_may_pull(skb, xprth + 4 - skb->data)) {
  128. __be32 *ehdr;
  129. xprth = skb_network_header(skb) + iph->ihl * 4;
  130. ehdr = (__be32 *)xprth;
  131. fl4->fl4_ipsec_spi = ehdr[0];
  132. }
  133. break;
  134. case IPPROTO_AH:
  135. if (xprth + 8 < skb->data ||
  136. pskb_may_pull(skb, xprth + 8 - skb->data)) {
  137. __be32 *ah_hdr;
  138. xprth = skb_network_header(skb) + iph->ihl * 4;
  139. ah_hdr = (__be32 *)xprth;
  140. fl4->fl4_ipsec_spi = ah_hdr[1];
  141. }
  142. break;
  143. case IPPROTO_COMP:
  144. if (xprth + 4 < skb->data ||
  145. pskb_may_pull(skb, xprth + 4 - skb->data)) {
  146. __be16 *ipcomp_hdr;
  147. xprth = skb_network_header(skb) + iph->ihl * 4;
  148. ipcomp_hdr = (__be16 *)xprth;
  149. fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
  150. }
  151. break;
  152. case IPPROTO_GRE:
  153. if (xprth + 12 < skb->data ||
  154. pskb_may_pull(skb, xprth + 12 - skb->data)) {
  155. __be16 *greflags;
  156. __be32 *gre_hdr;
  157. xprth = skb_network_header(skb) + iph->ihl * 4;
  158. greflags = (__be16 *)xprth;
  159. gre_hdr = (__be32 *)xprth;
  160. if (greflags[0] & GRE_KEY) {
  161. if (greflags[0] & GRE_CSUM)
  162. gre_hdr++;
  163. fl4->fl4_gre_key = gre_hdr[1];
  164. }
  165. }
  166. break;
  167. default:
  168. fl4->fl4_ipsec_spi = 0;
  169. break;
  170. }
  171. }
  172. fl4->flowi4_proto = iph->protocol;
  173. fl4->daddr = reverse ? iph->saddr : iph->daddr;
  174. fl4->saddr = reverse ? iph->daddr : iph->saddr;
  175. fl4->flowi4_tos = iph->tos;
  176. }
  177. static inline int xfrm4_garbage_collect(struct dst_ops *ops)
  178. {
  179. struct net *net = container_of(ops, struct net, xfrm.xfrm4_dst_ops);
  180. xfrm4_policy_afinfo.garbage_collect(net);
  181. return (dst_entries_get_slow(ops) > ops->gc_thresh * 2);
  182. }
  183. static void xfrm4_update_pmtu(struct dst_entry *dst, struct sock *sk,
  184. struct sk_buff *skb, u32 mtu)
  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);
  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. xfrm_dst_destroy(xdst);
  202. }
  203. static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  204. int unregister)
  205. {
  206. if (!unregister)
  207. return;
  208. xfrm_dst_ifdown(dst, dev);
  209. }
  210. static struct dst_ops xfrm4_dst_ops_template = {
  211. .family = AF_INET,
  212. .gc = xfrm4_garbage_collect,
  213. .update_pmtu = xfrm4_update_pmtu,
  214. .redirect = xfrm4_redirect,
  215. .cow_metrics = dst_cow_metrics_generic,
  216. .destroy = xfrm4_dst_destroy,
  217. .ifdown = xfrm4_dst_ifdown,
  218. .local_out = __ip_local_out,
  219. .gc_thresh = INT_MAX,
  220. };
  221. static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
  222. .family = AF_INET,
  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 int __net_init 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 void __net_exit 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 int inline xfrm4_net_sysctl_init(struct net *net)
  277. {
  278. return 0;
  279. }
  280. static void inline 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);
  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. }