1 /* 2 * net/ipv6/fib6_rules.c IPv6 Routing Policy Rules 3 * 4 * Copyright (C)2003-2006 Helsinki University of Technology 5 * Copyright (C)2003-2006 USAGI/WIDE Project 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License as 9 * published by the Free Software Foundation, version 2. 10 * 11 * Authors 12 * Thomas Graf <tgraf@suug.ch> 13 * Ville Nuorvala <vnuorval@tcs.hut.fi> 14 */ 15 16 #include <linux/netdevice.h> 17 #include <linux/notifier.h> 18 #include <linux/export.h> 19 20 #include <net/fib_rules.h> 21 #include <net/ipv6.h> 22 #include <net/addrconf.h> 23 #include <net/ip6_route.h> 24 #include <net/netlink.h> 25 26 struct fib6_rule { 27 struct fib_rule common; 28 struct rt6key src; 29 struct rt6key dst; 30 u8 tclass; 31 }; 32 33 static bool fib6_rule_matchall(const struct fib_rule *rule) 34 { 35 struct fib6_rule *r = container_of(rule, struct fib6_rule, common); 36 37 if (r->dst.plen || r->src.plen || r->tclass) 38 return false; 39 return fib_rule_matchall(rule); 40 } 41 42 bool fib6_rule_default(const struct fib_rule *rule) 43 { 44 if (!fib6_rule_matchall(rule) || rule->action != FR_ACT_TO_TBL || 45 rule->l3mdev) 46 return false; 47 if (rule->table != RT6_TABLE_LOCAL && rule->table != RT6_TABLE_MAIN) 48 return false; 49 return true; 50 } 51 EXPORT_SYMBOL_GPL(fib6_rule_default); 52 53 int fib6_rules_dump(struct net *net, struct notifier_block *nb) 54 { 55 return fib_rules_dump(net, nb, AF_INET6); 56 } 57 58 unsigned int fib6_rules_seq_read(struct net *net) 59 { 60 return fib_rules_seq_read(net, AF_INET6); 61 } 62 63 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6, 64 int flags, pol_lookup_t lookup) 65 { 66 if (net->ipv6.fib6_has_custom_rules) { 67 struct fib_lookup_arg arg = { 68 .lookup_ptr = lookup, 69 .flags = FIB_LOOKUP_NOREF, 70 }; 71 72 /* update flow if oif or iif point to device enslaved to l3mdev */ 73 l3mdev_update_flow(net, flowi6_to_flowi(fl6)); 74 75 fib_rules_lookup(net->ipv6.fib6_rules_ops, 76 flowi6_to_flowi(fl6), flags, &arg); 77 78 if (arg.result) 79 return arg.result; 80 } else { 81 struct rt6_info *rt; 82 83 rt = lookup(net, net->ipv6.fib6_local_tbl, fl6, flags); 84 if (rt != net->ipv6.ip6_null_entry && rt->dst.error != -EAGAIN) 85 return &rt->dst; 86 ip6_rt_put(rt); 87 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, flags); 88 if (rt->dst.error != -EAGAIN) 89 return &rt->dst; 90 ip6_rt_put(rt); 91 } 92 93 dst_hold(&net->ipv6.ip6_null_entry->dst); 94 return &net->ipv6.ip6_null_entry->dst; 95 } 96 97 static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp, 98 int flags, struct fib_lookup_arg *arg) 99 { 100 struct flowi6 *flp6 = &flp->u.ip6; 101 struct rt6_info *rt = NULL; 102 struct fib6_table *table; 103 struct net *net = rule->fr_net; 104 pol_lookup_t lookup = arg->lookup_ptr; 105 int err = 0; 106 u32 tb_id; 107 108 switch (rule->action) { 109 case FR_ACT_TO_TBL: 110 break; 111 case FR_ACT_UNREACHABLE: 112 err = -ENETUNREACH; 113 rt = net->ipv6.ip6_null_entry; 114 goto discard_pkt; 115 default: 116 case FR_ACT_BLACKHOLE: 117 err = -EINVAL; 118 rt = net->ipv6.ip6_blk_hole_entry; 119 goto discard_pkt; 120 case FR_ACT_PROHIBIT: 121 err = -EACCES; 122 rt = net->ipv6.ip6_prohibit_entry; 123 goto discard_pkt; 124 } 125 126 tb_id = fib_rule_get_table(rule, arg); 127 table = fib6_get_table(net, tb_id); 128 if (!table) { 129 err = -EAGAIN; 130 goto out; 131 } 132 133 rt = lookup(net, table, flp6, flags); 134 if (rt != net->ipv6.ip6_null_entry) { 135 struct fib6_rule *r = (struct fib6_rule *)rule; 136 137 /* 138 * If we need to find a source address for this traffic, 139 * we check the result if it meets requirement of the rule. 140 */ 141 if ((rule->flags & FIB_RULE_FIND_SADDR) && 142 r->src.plen && !(flags & RT6_LOOKUP_F_HAS_SADDR)) { 143 struct in6_addr saddr; 144 145 if (ipv6_dev_get_saddr(net, 146 ip6_dst_idev(&rt->dst)->dev, 147 &flp6->daddr, 148 rt6_flags2srcprefs(flags), 149 &saddr)) 150 goto again; 151 if (!ipv6_prefix_equal(&saddr, &r->src.addr, 152 r->src.plen)) 153 goto again; 154 flp6->saddr = saddr; 155 } 156 err = rt->dst.error; 157 if (err != -EAGAIN) 158 goto out; 159 } 160 again: 161 ip6_rt_put(rt); 162 err = -EAGAIN; 163 rt = NULL; 164 goto out; 165 166 discard_pkt: 167 dst_hold(&rt->dst); 168 out: 169 arg->result = rt; 170 return err; 171 } 172 173 static bool fib6_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg) 174 { 175 struct rt6_info *rt = (struct rt6_info *) arg->result; 176 struct net_device *dev = NULL; 177 178 if (rt->rt6i_idev) 179 dev = rt->rt6i_idev->dev; 180 181 /* do not accept result if the route does 182 * not meet the required prefix length 183 */ 184 if (rt->rt6i_dst.plen <= rule->suppress_prefixlen) 185 goto suppress_route; 186 187 /* do not accept result if the route uses a device 188 * belonging to a forbidden interface group 189 */ 190 if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup) 191 goto suppress_route; 192 193 return false; 194 195 suppress_route: 196 ip6_rt_put(rt); 197 return true; 198 } 199 200 static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) 201 { 202 struct fib6_rule *r = (struct fib6_rule *) rule; 203 struct flowi6 *fl6 = &fl->u.ip6; 204 205 if (r->dst.plen && 206 !ipv6_prefix_equal(&fl6->daddr, &r->dst.addr, r->dst.plen)) 207 return 0; 208 209 /* 210 * If FIB_RULE_FIND_SADDR is set and we do not have a 211 * source address for the traffic, we defer check for 212 * source address. 213 */ 214 if (r->src.plen) { 215 if (flags & RT6_LOOKUP_F_HAS_SADDR) { 216 if (!ipv6_prefix_equal(&fl6->saddr, &r->src.addr, 217 r->src.plen)) 218 return 0; 219 } else if (!(r->common.flags & FIB_RULE_FIND_SADDR)) 220 return 0; 221 } 222 223 if (r->tclass && r->tclass != ip6_tclass(fl6->flowlabel)) 224 return 0; 225 226 if (rule->ip_proto && (rule->ip_proto != fl6->flowi6_proto)) 227 return 0; 228 229 if (fib_rule_port_range_set(&rule->sport_range) && 230 !fib_rule_port_inrange(&rule->sport_range, fl6->fl6_sport)) 231 return 0; 232 233 if (fib_rule_port_range_set(&rule->dport_range) && 234 !fib_rule_port_inrange(&rule->dport_range, fl6->fl6_dport)) 235 return 0; 236 237 return 1; 238 } 239 240 static const struct nla_policy fib6_rule_policy[FRA_MAX+1] = { 241 FRA_GENERIC_POLICY, 242 }; 243 244 static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb, 245 struct fib_rule_hdr *frh, 246 struct nlattr **tb) 247 { 248 int err = -EINVAL; 249 struct net *net = sock_net(skb->sk); 250 struct fib6_rule *rule6 = (struct fib6_rule *) rule; 251 252 if (rule->action == FR_ACT_TO_TBL && !rule->l3mdev) { 253 if (rule->table == RT6_TABLE_UNSPEC) 254 goto errout; 255 256 if (fib6_new_table(net, rule->table) == NULL) { 257 err = -ENOBUFS; 258 goto errout; 259 } 260 } 261 262 if (frh->src_len) 263 rule6->src.addr = nla_get_in6_addr(tb[FRA_SRC]); 264 265 if (frh->dst_len) 266 rule6->dst.addr = nla_get_in6_addr(tb[FRA_DST]); 267 268 rule6->src.plen = frh->src_len; 269 rule6->dst.plen = frh->dst_len; 270 rule6->tclass = frh->tos; 271 272 if (fib_rule_requires_fldissect(rule)) 273 net->ipv6.fib6_rules_require_fldissect++; 274 275 net->ipv6.fib6_has_custom_rules = true; 276 err = 0; 277 errout: 278 return err; 279 } 280 281 static int fib6_rule_delete(struct fib_rule *rule) 282 { 283 struct net *net = rule->fr_net; 284 285 if (net->ipv6.fib6_rules_require_fldissect && 286 fib_rule_requires_fldissect(rule)) 287 net->ipv6.fib6_rules_require_fldissect--; 288 289 return 0; 290 } 291 292 static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, 293 struct nlattr **tb) 294 { 295 struct fib6_rule *rule6 = (struct fib6_rule *) rule; 296 297 if (frh->src_len && (rule6->src.plen != frh->src_len)) 298 return 0; 299 300 if (frh->dst_len && (rule6->dst.plen != frh->dst_len)) 301 return 0; 302 303 if (frh->tos && (rule6->tclass != frh->tos)) 304 return 0; 305 306 if (frh->src_len && 307 nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr))) 308 return 0; 309 310 if (frh->dst_len && 311 nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr))) 312 return 0; 313 314 return 1; 315 } 316 317 static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb, 318 struct fib_rule_hdr *frh) 319 { 320 struct fib6_rule *rule6 = (struct fib6_rule *) rule; 321 322 frh->dst_len = rule6->dst.plen; 323 frh->src_len = rule6->src.plen; 324 frh->tos = rule6->tclass; 325 326 if ((rule6->dst.plen && 327 nla_put_in6_addr(skb, FRA_DST, &rule6->dst.addr)) || 328 (rule6->src.plen && 329 nla_put_in6_addr(skb, FRA_SRC, &rule6->src.addr))) 330 goto nla_put_failure; 331 return 0; 332 333 nla_put_failure: 334 return -ENOBUFS; 335 } 336 337 static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule) 338 { 339 return nla_total_size(16) /* dst */ 340 + nla_total_size(16); /* src */ 341 } 342 343 static const struct fib_rules_ops __net_initconst fib6_rules_ops_template = { 344 .family = AF_INET6, 345 .rule_size = sizeof(struct fib6_rule), 346 .addr_size = sizeof(struct in6_addr), 347 .action = fib6_rule_action, 348 .match = fib6_rule_match, 349 .suppress = fib6_rule_suppress, 350 .configure = fib6_rule_configure, 351 .delete = fib6_rule_delete, 352 .compare = fib6_rule_compare, 353 .fill = fib6_rule_fill, 354 .nlmsg_payload = fib6_rule_nlmsg_payload, 355 .nlgroup = RTNLGRP_IPV6_RULE, 356 .policy = fib6_rule_policy, 357 .owner = THIS_MODULE, 358 .fro_net = &init_net, 359 }; 360 361 static int __net_init fib6_rules_net_init(struct net *net) 362 { 363 struct fib_rules_ops *ops; 364 int err = -ENOMEM; 365 366 ops = fib_rules_register(&fib6_rules_ops_template, net); 367 if (IS_ERR(ops)) 368 return PTR_ERR(ops); 369 370 err = fib_default_rule_add(ops, 0, RT6_TABLE_LOCAL, 0); 371 if (err) 372 goto out_fib6_rules_ops; 373 374 err = fib_default_rule_add(ops, 0x7FFE, RT6_TABLE_MAIN, 0); 375 if (err) 376 goto out_fib6_rules_ops; 377 378 net->ipv6.fib6_rules_ops = ops; 379 net->ipv6.fib6_rules_require_fldissect = 0; 380 out: 381 return err; 382 383 out_fib6_rules_ops: 384 fib_rules_unregister(ops); 385 goto out; 386 } 387 388 static void __net_exit fib6_rules_net_exit(struct net *net) 389 { 390 rtnl_lock(); 391 fib_rules_unregister(net->ipv6.fib6_rules_ops); 392 rtnl_unlock(); 393 } 394 395 static struct pernet_operations fib6_rules_net_ops = { 396 .init = fib6_rules_net_init, 397 .exit = fib6_rules_net_exit, 398 .async = true, 399 }; 400 401 int __init fib6_rules_init(void) 402 { 403 return register_pernet_subsys(&fib6_rules_net_ops); 404 } 405 406 407 void fib6_rules_cleanup(void) 408 { 409 unregister_pernet_subsys(&fib6_rules_net_ops); 410 } 411