xfrm4_policy.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 = l3mdev_master_ifindex_by_index(net, 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_table_id = rt->rt_table_id;
  83. INIT_LIST_HEAD(&xdst->u.rt.rt_uncached);
  84. return 0;
  85. }
  86. static void
  87. _decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse)
  88. {
  89. const struct iphdr *iph = ip_hdr(skb);
  90. u8 *xprth = skb_network_header(skb) + iph->ihl * 4;
  91. struct flowi4 *fl4 = &fl->u.ip4;
  92. int oif = 0;
  93. if (skb_dst(skb))
  94. oif = skb_dst(skb)->dev->ifindex;
  95. memset(fl4, 0, sizeof(struct flowi4));
  96. fl4->flowi4_mark = skb->mark;
  97. fl4->flowi4_oif = reverse ? skb->skb_iif : oif;
  98. if (!ip_is_fragment(iph)) {
  99. switch (iph->protocol) {
  100. case IPPROTO_UDP:
  101. case IPPROTO_UDPLITE:
  102. case IPPROTO_TCP:
  103. case IPPROTO_SCTP:
  104. case IPPROTO_DCCP:
  105. if (xprth + 4 < skb->data ||
  106. pskb_may_pull(skb, xprth + 4 - skb->data)) {
  107. __be16 *ports;
  108. xprth = skb_network_header(skb) + iph->ihl * 4;
  109. ports = (__be16 *)xprth;
  110. fl4->fl4_sport = ports[!!reverse];
  111. fl4->fl4_dport = ports[!reverse];
  112. }
  113. break;
  114. case IPPROTO_ICMP:
  115. if (xprth + 2 < skb->data ||
  116. pskb_may_pull(skb, xprth + 2 - skb->data)) {
  117. u8 *icmp;
  118. xprth = skb_network_header(skb) + iph->ihl * 4;
  119. icmp = xprth;
  120. fl4->fl4_icmp_type = icmp[0];
  121. fl4->fl4_icmp_code = icmp[1];
  122. }
  123. break;
  124. case IPPROTO_ESP:
  125. if (xprth + 4 < skb->data ||
  126. pskb_may_pull(skb, xprth + 4 - skb->data)) {
  127. __be32 *ehdr;
  128. xprth = skb_network_header(skb) + iph->ihl * 4;
  129. ehdr = (__be32 *)xprth;
  130. fl4->fl4_ipsec_spi = ehdr[0];
  131. }
  132. break;
  133. case IPPROTO_AH:
  134. if (xprth + 8 < skb->data ||
  135. pskb_may_pull(skb, xprth + 8 - skb->data)) {
  136. __be32 *ah_hdr;
  137. xprth = skb_network_header(skb) + iph->ihl * 4;
  138. ah_hdr = (__be32 *)xprth;
  139. fl4->fl4_ipsec_spi = ah_hdr[1];
  140. }
  141. break;
  142. case IPPROTO_COMP:
  143. if (xprth + 4 < skb->data ||
  144. pskb_may_pull(skb, xprth + 4 - skb->data)) {
  145. __be16 *ipcomp_hdr;
  146. xprth = skb_network_header(skb) + iph->ihl * 4;
  147. ipcomp_hdr = (__be16 *)xprth;
  148. fl4->fl4_ipsec_spi = htonl(ntohs(ipcomp_hdr[1]));
  149. }
  150. break;
  151. case IPPROTO_GRE:
  152. if (xprth + 12 < skb->data ||
  153. pskb_may_pull(skb, xprth + 12 - skb->data)) {
  154. __be16 *greflags;
  155. __be32 *gre_hdr;
  156. xprth = skb_network_header(skb) + iph->ihl * 4;
  157. greflags = (__be16 *)xprth;
  158. gre_hdr = (__be32 *)xprth;
  159. if (greflags[0] & GRE_KEY) {
  160. if (greflags[0] & GRE_CSUM)
  161. gre_hdr++;
  162. fl4->fl4_gre_key = gre_hdr[1];
  163. }
  164. }
  165. break;
  166. default:
  167. fl4->fl4_ipsec_spi = 0;
  168. break;
  169. }
  170. }
  171. fl4->flowi4_proto = iph->protocol;
  172. fl4->daddr = reverse ? iph->saddr : iph->daddr;
  173. fl4->saddr = reverse ? iph->daddr : iph->saddr;
  174. fl4->flowi4_tos = iph->tos;
  175. }
  176. static inline int xfrm4_garbage_collect(struct dst_ops *ops)
  177. {
  178. struct net *net = container_of(ops, struct net, xfrm.xfrm4_dst_ops);
  179. xfrm4_policy_afinfo.garbage_collect(net);
  180. return (dst_entries_get_slow(ops) > ops->gc_thresh * 2);
  181. }
  182. static void xfrm4_update_pmtu(struct dst_entry *dst, struct sock *sk,
  183. struct sk_buff *skb, u32 mtu)
  184. {
  185. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  186. struct dst_entry *path = xdst->route;
  187. path->ops->update_pmtu(path, sk, skb, mtu);
  188. }
  189. static void xfrm4_redirect(struct dst_entry *dst, struct sock *sk,
  190. struct sk_buff *skb)
  191. {
  192. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  193. struct dst_entry *path = xdst->route;
  194. path->ops->redirect(path, sk, skb);
  195. }
  196. static void xfrm4_dst_destroy(struct dst_entry *dst)
  197. {
  198. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  199. dst_destroy_metrics_generic(dst);
  200. xfrm_dst_destroy(xdst);
  201. }
  202. static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  203. int unregister)
  204. {
  205. if (!unregister)
  206. return;
  207. xfrm_dst_ifdown(dst, dev);
  208. }
  209. static struct dst_ops xfrm4_dst_ops_template = {
  210. .family = AF_INET,
  211. .gc = xfrm4_garbage_collect,
  212. .update_pmtu = xfrm4_update_pmtu,
  213. .redirect = xfrm4_redirect,
  214. .cow_metrics = dst_cow_metrics_generic,
  215. .destroy = xfrm4_dst_destroy,
  216. .ifdown = xfrm4_dst_ifdown,
  217. .local_out = __ip_local_out,
  218. .gc_thresh = INT_MAX,
  219. };
  220. static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
  221. .family = AF_INET,
  222. .dst_ops = &xfrm4_dst_ops_template,
  223. .dst_lookup = xfrm4_dst_lookup,
  224. .get_saddr = xfrm4_get_saddr,
  225. .decode_session = _decode_session4,
  226. .get_tos = xfrm4_get_tos,
  227. .init_path = xfrm4_init_path,
  228. .fill_dst = xfrm4_fill_dst,
  229. .blackhole_route = ipv4_blackhole_route,
  230. };
  231. #ifdef CONFIG_SYSCTL
  232. static struct ctl_table xfrm4_policy_table[] = {
  233. {
  234. .procname = "xfrm4_gc_thresh",
  235. .data = &init_net.xfrm.xfrm4_dst_ops.gc_thresh,
  236. .maxlen = sizeof(int),
  237. .mode = 0644,
  238. .proc_handler = proc_dointvec,
  239. },
  240. { }
  241. };
  242. static __net_init int xfrm4_net_sysctl_init(struct net *net)
  243. {
  244. struct ctl_table *table;
  245. struct ctl_table_header *hdr;
  246. table = xfrm4_policy_table;
  247. if (!net_eq(net, &init_net)) {
  248. table = kmemdup(table, sizeof(xfrm4_policy_table), GFP_KERNEL);
  249. if (!table)
  250. goto err_alloc;
  251. table[0].data = &net->xfrm.xfrm4_dst_ops.gc_thresh;
  252. }
  253. hdr = register_net_sysctl(net, "net/ipv4", table);
  254. if (!hdr)
  255. goto err_reg;
  256. net->ipv4.xfrm4_hdr = hdr;
  257. return 0;
  258. err_reg:
  259. if (!net_eq(net, &init_net))
  260. kfree(table);
  261. err_alloc:
  262. return -ENOMEM;
  263. }
  264. static __net_exit void xfrm4_net_sysctl_exit(struct net *net)
  265. {
  266. struct ctl_table *table;
  267. if (!net->ipv4.xfrm4_hdr)
  268. return;
  269. table = net->ipv4.xfrm4_hdr->ctl_table_arg;
  270. unregister_net_sysctl_table(net->ipv4.xfrm4_hdr);
  271. if (!net_eq(net, &init_net))
  272. kfree(table);
  273. }
  274. #else /* CONFIG_SYSCTL */
  275. static inline int xfrm4_net_sysctl_init(struct net *net)
  276. {
  277. return 0;
  278. }
  279. static inline void xfrm4_net_sysctl_exit(struct net *net)
  280. {
  281. }
  282. #endif
  283. static int __net_init xfrm4_net_init(struct net *net)
  284. {
  285. int ret;
  286. memcpy(&net->xfrm.xfrm4_dst_ops, &xfrm4_dst_ops_template,
  287. sizeof(xfrm4_dst_ops_template));
  288. ret = dst_entries_init(&net->xfrm.xfrm4_dst_ops);
  289. if (ret)
  290. return ret;
  291. ret = xfrm4_net_sysctl_init(net);
  292. if (ret)
  293. dst_entries_destroy(&net->xfrm.xfrm4_dst_ops);
  294. return ret;
  295. }
  296. static void __net_exit xfrm4_net_exit(struct net *net)
  297. {
  298. xfrm4_net_sysctl_exit(net);
  299. dst_entries_destroy(&net->xfrm.xfrm4_dst_ops);
  300. }
  301. static struct pernet_operations __net_initdata xfrm4_net_ops = {
  302. .init = xfrm4_net_init,
  303. .exit = xfrm4_net_exit,
  304. };
  305. static void __init xfrm4_policy_init(void)
  306. {
  307. xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
  308. }
  309. void __init xfrm4_init(void)
  310. {
  311. xfrm4_state_init();
  312. xfrm4_policy_init();
  313. xfrm4_protocol_init();
  314. register_pernet_subsys(&xfrm4_net_ops);
  315. }