xfrm4_policy.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. 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. xdst->u.rt.rt_table_id = rt->rt_table_id;
  88. INIT_LIST_HEAD(&xdst->u.rt.rt_uncached);
  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 inline int xfrm4_garbage_collect(struct dst_ops *ops)
  183. {
  184. struct net *net = container_of(ops, struct net, xfrm.xfrm4_dst_ops);
  185. xfrm4_policy_afinfo.garbage_collect(net);
  186. return (dst_entries_get_slow(ops) > ops->gc_thresh * 2);
  187. }
  188. static void xfrm4_update_pmtu(struct dst_entry *dst, struct sock *sk,
  189. struct sk_buff *skb, u32 mtu)
  190. {
  191. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  192. struct dst_entry *path = xdst->route;
  193. path->ops->update_pmtu(path, sk, skb, mtu);
  194. }
  195. static void xfrm4_redirect(struct dst_entry *dst, struct sock *sk,
  196. struct sk_buff *skb)
  197. {
  198. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  199. struct dst_entry *path = xdst->route;
  200. path->ops->redirect(path, sk, skb);
  201. }
  202. static void xfrm4_dst_destroy(struct dst_entry *dst)
  203. {
  204. struct xfrm_dst *xdst = (struct xfrm_dst *)dst;
  205. dst_destroy_metrics_generic(dst);
  206. xfrm_dst_destroy(xdst);
  207. }
  208. static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
  209. int unregister)
  210. {
  211. if (!unregister)
  212. return;
  213. xfrm_dst_ifdown(dst, dev);
  214. }
  215. static struct dst_ops xfrm4_dst_ops_template = {
  216. .family = AF_INET,
  217. .gc = xfrm4_garbage_collect,
  218. .update_pmtu = xfrm4_update_pmtu,
  219. .redirect = xfrm4_redirect,
  220. .cow_metrics = dst_cow_metrics_generic,
  221. .destroy = xfrm4_dst_destroy,
  222. .ifdown = xfrm4_dst_ifdown,
  223. .local_out = __ip_local_out,
  224. .gc_thresh = INT_MAX,
  225. };
  226. static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
  227. .family = AF_INET,
  228. .dst_ops = &xfrm4_dst_ops_template,
  229. .dst_lookup = xfrm4_dst_lookup,
  230. .get_saddr = xfrm4_get_saddr,
  231. .decode_session = _decode_session4,
  232. .get_tos = xfrm4_get_tos,
  233. .init_path = xfrm4_init_path,
  234. .fill_dst = xfrm4_fill_dst,
  235. .blackhole_route = ipv4_blackhole_route,
  236. };
  237. #ifdef CONFIG_SYSCTL
  238. static struct ctl_table xfrm4_policy_table[] = {
  239. {
  240. .procname = "xfrm4_gc_thresh",
  241. .data = &init_net.xfrm.xfrm4_dst_ops.gc_thresh,
  242. .maxlen = sizeof(int),
  243. .mode = 0644,
  244. .proc_handler = proc_dointvec,
  245. },
  246. { }
  247. };
  248. static __net_init int xfrm4_net_sysctl_init(struct net *net)
  249. {
  250. struct ctl_table *table;
  251. struct ctl_table_header *hdr;
  252. table = xfrm4_policy_table;
  253. if (!net_eq(net, &init_net)) {
  254. table = kmemdup(table, sizeof(xfrm4_policy_table), GFP_KERNEL);
  255. if (!table)
  256. goto err_alloc;
  257. table[0].data = &net->xfrm.xfrm4_dst_ops.gc_thresh;
  258. }
  259. hdr = register_net_sysctl(net, "net/ipv4", table);
  260. if (!hdr)
  261. goto err_reg;
  262. net->ipv4.xfrm4_hdr = hdr;
  263. return 0;
  264. err_reg:
  265. if (!net_eq(net, &init_net))
  266. kfree(table);
  267. err_alloc:
  268. return -ENOMEM;
  269. }
  270. static __net_exit void xfrm4_net_sysctl_exit(struct net *net)
  271. {
  272. struct ctl_table *table;
  273. if (!net->ipv4.xfrm4_hdr)
  274. return;
  275. table = net->ipv4.xfrm4_hdr->ctl_table_arg;
  276. unregister_net_sysctl_table(net->ipv4.xfrm4_hdr);
  277. if (!net_eq(net, &init_net))
  278. kfree(table);
  279. }
  280. #else /* CONFIG_SYSCTL */
  281. static inline int xfrm4_net_sysctl_init(struct net *net)
  282. {
  283. return 0;
  284. }
  285. static inline void xfrm4_net_sysctl_exit(struct net *net)
  286. {
  287. }
  288. #endif
  289. static int __net_init xfrm4_net_init(struct net *net)
  290. {
  291. int ret;
  292. memcpy(&net->xfrm.xfrm4_dst_ops, &xfrm4_dst_ops_template,
  293. sizeof(xfrm4_dst_ops_template));
  294. ret = dst_entries_init(&net->xfrm.xfrm4_dst_ops);
  295. if (ret)
  296. return ret;
  297. ret = xfrm4_net_sysctl_init(net);
  298. if (ret)
  299. dst_entries_destroy(&net->xfrm.xfrm4_dst_ops);
  300. return ret;
  301. }
  302. static void __net_exit xfrm4_net_exit(struct net *net)
  303. {
  304. xfrm4_net_sysctl_exit(net);
  305. dst_entries_destroy(&net->xfrm.xfrm4_dst_ops);
  306. }
  307. static struct pernet_operations __net_initdata xfrm4_net_ops = {
  308. .init = xfrm4_net_init,
  309. .exit = xfrm4_net_exit,
  310. };
  311. static void __init xfrm4_policy_init(void)
  312. {
  313. xfrm_policy_register_afinfo(&xfrm4_policy_afinfo);
  314. }
  315. void __init xfrm4_init(void)
  316. {
  317. xfrm4_state_init();
  318. xfrm4_policy_init();
  319. xfrm4_protocol_init();
  320. register_pernet_subsys(&xfrm4_net_ops);
  321. }