xfrm4_policy.c 8.3 KB

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