670-ipv6-allow-rejecting-with-source-address-failed-policy.patch 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. From: Jonas Gorski <[email protected]>
  2. Subject: ipv6: allow rejecting with "source address failed policy"
  3. RFC6204 L-14 requires rejecting traffic from invalid addresses with
  4. ICMPv6 Destination Unreachable, Code 5 (Source address failed ingress/
  5. egress policy) on the LAN side, so add an appropriate rule for that.
  6. Signed-off-by: Jonas Gorski <[email protected]>
  7. ---
  8. include/net/netns/ipv6.h | 1 +
  9. include/uapi/linux/fib_rules.h | 4 +++
  10. include/uapi/linux/rtnetlink.h | 1 +
  11. net/ipv4/fib_semantics.c | 4 +++
  12. net/ipv4/fib_trie.c | 1 +
  13. net/ipv4/ipmr.c | 1 +
  14. net/ipv6/fib6_rules.c | 4 +++
  15. net/ipv6/ip6mr.c | 2 ++
  16. net/ipv6/route.c | 58 +++++++++++++++++++++++++++++++++++++++++-
  17. 9 files changed, 75 insertions(+), 1 deletion(-)
  18. --- a/include/net/netns/ipv6.h
  19. +++ b/include/net/netns/ipv6.h
  20. @@ -86,6 +86,7 @@ struct netns_ipv6 {
  21. unsigned int fib6_routes_require_src;
  22. #endif
  23. struct rt6_info *ip6_prohibit_entry;
  24. + struct rt6_info *ip6_policy_failed_entry;
  25. struct rt6_info *ip6_blk_hole_entry;
  26. struct fib6_table *fib6_local_tbl;
  27. struct fib_rules_ops *fib6_rules_ops;
  28. --- a/include/uapi/linux/fib_rules.h
  29. +++ b/include/uapi/linux/fib_rules.h
  30. @@ -82,6 +82,10 @@ enum {
  31. FR_ACT_BLACKHOLE, /* Drop without notification */
  32. FR_ACT_UNREACHABLE, /* Drop with ENETUNREACH */
  33. FR_ACT_PROHIBIT, /* Drop with EACCES */
  34. + FR_ACT_RES9,
  35. + FR_ACT_RES10,
  36. + FR_ACT_RES11,
  37. + FR_ACT_POLICY_FAILED, /* Drop with EACCES */
  38. __FR_ACT_MAX,
  39. };
  40. --- a/include/uapi/linux/rtnetlink.h
  41. +++ b/include/uapi/linux/rtnetlink.h
  42. @@ -265,6 +265,7 @@ enum {
  43. RTN_THROW, /* Not in this table */
  44. RTN_NAT, /* Translate this address */
  45. RTN_XRESOLVE, /* Use external resolver */
  46. + RTN_POLICY_FAILED, /* Failed ingress/egress policy */
  47. __RTN_MAX
  48. };
  49. --- a/net/ipv4/fib_semantics.c
  50. +++ b/net/ipv4/fib_semantics.c
  51. @@ -145,6 +145,10 @@ const struct fib_prop fib_props[RTN_MAX
  52. .error = -EINVAL,
  53. .scope = RT_SCOPE_NOWHERE,
  54. },
  55. + [RTN_POLICY_FAILED] = {
  56. + .error = -EACCES,
  57. + .scope = RT_SCOPE_UNIVERSE,
  58. + },
  59. };
  60. static void rt_fibinfo_free(struct rtable __rcu **rtp)
  61. --- a/net/ipv4/fib_trie.c
  62. +++ b/net/ipv4/fib_trie.c
  63. @@ -2783,6 +2783,7 @@ static const char *const rtn_type_names[
  64. [RTN_THROW] = "THROW",
  65. [RTN_NAT] = "NAT",
  66. [RTN_XRESOLVE] = "XRESOLVE",
  67. + [RTN_POLICY_FAILED] = "POLICY_FAILED",
  68. };
  69. static inline const char *rtn_type(char *buf, size_t len, unsigned int t)
  70. --- a/net/ipv4/ipmr.c
  71. +++ b/net/ipv4/ipmr.c
  72. @@ -180,6 +180,7 @@ static int ipmr_rule_action(struct fib_r
  73. case FR_ACT_UNREACHABLE:
  74. return -ENETUNREACH;
  75. case FR_ACT_PROHIBIT:
  76. + case FR_ACT_POLICY_FAILED:
  77. return -EACCES;
  78. case FR_ACT_BLACKHOLE:
  79. default:
  80. --- a/net/ipv6/fib6_rules.c
  81. +++ b/net/ipv6/fib6_rules.c
  82. @@ -221,6 +221,10 @@ static int __fib6_rule_action(struct fib
  83. err = -EACCES;
  84. rt = net->ipv6.ip6_prohibit_entry;
  85. goto discard_pkt;
  86. + case FR_ACT_POLICY_FAILED:
  87. + err = -EACCES;
  88. + rt = net->ipv6.ip6_policy_failed_entry;
  89. + goto discard_pkt;
  90. }
  91. tb_id = fib_rule_get_table(rule, arg);
  92. --- a/net/ipv6/ip6mr.c
  93. +++ b/net/ipv6/ip6mr.c
  94. @@ -170,6 +170,8 @@ static int ip6mr_rule_action(struct fib_
  95. return -ENETUNREACH;
  96. case FR_ACT_PROHIBIT:
  97. return -EACCES;
  98. + case FR_ACT_POLICY_FAILED:
  99. + return -EACCES;
  100. case FR_ACT_BLACKHOLE:
  101. default:
  102. return -EINVAL;
  103. --- a/net/ipv6/route.c
  104. +++ b/net/ipv6/route.c
  105. @@ -97,6 +97,8 @@ static int ip6_pkt_discard(struct sk_bu
  106. static int ip6_pkt_discard_out(struct net *net, struct sock *sk, struct sk_buff *skb);
  107. static int ip6_pkt_prohibit(struct sk_buff *skb);
  108. static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff *skb);
  109. +static int ip6_pkt_policy_failed(struct sk_buff *skb);
  110. +static int ip6_pkt_policy_failed_out(struct net *net, struct sock *sk, struct sk_buff *skb);
  111. static void ip6_link_failure(struct sk_buff *skb);
  112. static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
  113. struct sk_buff *skb, u32 mtu,
  114. @@ -317,6 +319,18 @@ static const struct rt6_info ip6_prohibi
  115. .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
  116. };
  117. +static const struct rt6_info ip6_policy_failed_entry_template = {
  118. + .dst = {
  119. + .__rcuref = RCUREF_INIT(1),
  120. + .__use = 1,
  121. + .obsolete = DST_OBSOLETE_FORCE_CHK,
  122. + .error = -EACCES,
  123. + .input = ip6_pkt_policy_failed,
  124. + .output = ip6_pkt_policy_failed_out,
  125. + },
  126. + .rt6i_flags = (RTF_REJECT | RTF_NONEXTHOP),
  127. +};
  128. +
  129. static const struct rt6_info ip6_blk_hole_entry_template = {
  130. .dst = {
  131. .__rcuref = RCUREF_INIT(1),
  132. @@ -1037,6 +1051,7 @@ static const int fib6_prop[RTN_MAX + 1]
  133. [RTN_BLACKHOLE] = -EINVAL,
  134. [RTN_UNREACHABLE] = -EHOSTUNREACH,
  135. [RTN_PROHIBIT] = -EACCES,
  136. + [RTN_POLICY_FAILED] = -EACCES,
  137. [RTN_THROW] = -EAGAIN,
  138. [RTN_NAT] = -EINVAL,
  139. [RTN_XRESOLVE] = -EINVAL,
  140. @@ -1072,6 +1087,10 @@ static void ip6_rt_init_dst_reject(struc
  141. rt->dst.output = ip6_pkt_prohibit_out;
  142. rt->dst.input = ip6_pkt_prohibit;
  143. break;
  144. + case RTN_POLICY_FAILED:
  145. + rt->dst.output = ip6_pkt_policy_failed_out;
  146. + rt->dst.input = ip6_pkt_policy_failed;
  147. + break;
  148. case RTN_THROW:
  149. case RTN_UNREACHABLE:
  150. default:
  151. @@ -4539,6 +4558,17 @@ static int ip6_pkt_prohibit_out(struct n
  152. return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
  153. }
  154. +static int ip6_pkt_policy_failed(struct sk_buff *skb)
  155. +{
  156. + return ip6_pkt_drop(skb, ICMPV6_POLICY_FAIL, IPSTATS_MIB_INNOROUTES);
  157. +}
  158. +
  159. +static int ip6_pkt_policy_failed_out(struct net *net, struct sock *sk, struct sk_buff *skb)
  160. +{
  161. + skb->dev = skb_dst(skb)->dev;
  162. + return ip6_pkt_drop(skb, ICMPV6_POLICY_FAIL, IPSTATS_MIB_OUTNOROUTES);
  163. +}
  164. +
  165. /*
  166. * Allocate a dst for local (unicast / anycast) address.
  167. */
  168. @@ -5030,7 +5060,8 @@ static int rtm_to_fib6_config(struct sk_
  169. if (rtm->rtm_type == RTN_UNREACHABLE ||
  170. rtm->rtm_type == RTN_BLACKHOLE ||
  171. rtm->rtm_type == RTN_PROHIBIT ||
  172. - rtm->rtm_type == RTN_THROW)
  173. + rtm->rtm_type == RTN_THROW ||
  174. + rtm->rtm_type == RTN_POLICY_FAILED)
  175. cfg->fc_flags |= RTF_REJECT;
  176. if (rtm->rtm_type == RTN_LOCAL)
  177. @@ -6277,6 +6308,8 @@ static int ip6_route_dev_notify(struct n
  178. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  179. net->ipv6.ip6_prohibit_entry->dst.dev = dev;
  180. net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev);
  181. + net->ipv6.ip6_policy_failed_entry->dst.dev = dev;
  182. + net->ipv6.ip6_policy_failed_entry->rt6i_idev = in6_dev_get(dev);
  183. net->ipv6.ip6_blk_hole_entry->dst.dev = dev;
  184. net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev);
  185. #endif
  186. @@ -6288,6 +6321,7 @@ static int ip6_route_dev_notify(struct n
  187. in6_dev_put_clear(&net->ipv6.ip6_null_entry->rt6i_idev);
  188. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  189. in6_dev_put_clear(&net->ipv6.ip6_prohibit_entry->rt6i_idev);
  190. + in6_dev_put_clear(&net->ipv6.ip6_policy_failed_entry->rt6i_idev);
  191. in6_dev_put_clear(&net->ipv6.ip6_blk_hole_entry->rt6i_idev);
  192. #endif
  193. }
  194. @@ -6488,6 +6522,8 @@ static int __net_init ip6_route_net_init
  195. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  196. net->ipv6.fib6_has_custom_rules = false;
  197. +
  198. +
  199. net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template,
  200. sizeof(*net->ipv6.ip6_prohibit_entry),
  201. GFP_KERNEL);
  202. @@ -6498,11 +6534,21 @@ static int __net_init ip6_route_net_init
  203. ip6_template_metrics, true);
  204. INIT_LIST_HEAD(&net->ipv6.ip6_prohibit_entry->dst.rt_uncached);
  205. + net->ipv6.ip6_policy_failed_entry =
  206. + kmemdup(&ip6_policy_failed_entry_template,
  207. + sizeof(*net->ipv6.ip6_policy_failed_entry), GFP_KERNEL);
  208. + if (!net->ipv6.ip6_policy_failed_entry)
  209. + goto out_ip6_prohibit_entry;
  210. + net->ipv6.ip6_policy_failed_entry->dst.ops = &net->ipv6.ip6_dst_ops;
  211. + dst_init_metrics(&net->ipv6.ip6_policy_failed_entry->dst,
  212. + ip6_template_metrics, true);
  213. + INIT_LIST_HEAD(&net->ipv6.ip6_policy_failed_entry->dst.rt_uncached);
  214. +
  215. net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template,
  216. sizeof(*net->ipv6.ip6_blk_hole_entry),
  217. GFP_KERNEL);
  218. if (!net->ipv6.ip6_blk_hole_entry)
  219. - goto out_ip6_prohibit_entry;
  220. + goto out_ip6_policy_failed_entry;
  221. net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops;
  222. dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst,
  223. ip6_template_metrics, true);
  224. @@ -6529,6 +6575,8 @@ out:
  225. return ret;
  226. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  227. +out_ip6_policy_failed_entry:
  228. + kfree(net->ipv6.ip6_policy_failed_entry);
  229. out_ip6_prohibit_entry:
  230. kfree(net->ipv6.ip6_prohibit_entry);
  231. out_ip6_null_entry:
  232. @@ -6548,6 +6596,7 @@ static void __net_exit ip6_route_net_exi
  233. kfree(net->ipv6.ip6_null_entry);
  234. #ifdef CONFIG_IPV6_MULTIPLE_TABLES
  235. kfree(net->ipv6.ip6_prohibit_entry);
  236. + kfree(net->ipv6.ip6_policy_failed_entry);
  237. kfree(net->ipv6.ip6_blk_hole_entry);
  238. #endif
  239. dst_entries_destroy(&net->ipv6.ip6_dst_ops);
  240. @@ -6631,6 +6680,9 @@ void __init ip6_route_init_special_entri
  241. init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
  242. init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev;
  243. init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev);
  244. + init_net.ipv6.ip6_policy_failed_entry->dst.dev = init_net.loopback_dev;
  245. + init_net.ipv6.ip6_policy_failed_entry->rt6i_idev =
  246. + in6_dev_get(init_net.loopback_dev);
  247. #endif
  248. }