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 /* called with rcu lock held; no reference taken on fib6_info */ 64 int fib6_lookup(struct net *net, int oif, struct flowi6 *fl6, 65 struct fib6_result *res, int flags) 66 { 67 int err; 68 69 if (net->ipv6.fib6_has_custom_rules) { 70 struct fib_lookup_arg arg = { 71 .lookup_ptr = fib6_table_lookup, 72 .lookup_data = &oif, 73 .result = res, 74 .flags = FIB_LOOKUP_NOREF, 75 }; 76 77 l3mdev_update_flow(net, flowi6_to_flowi(fl6)); 78 79 err = fib_rules_lookup(net->ipv6.fib6_rules_ops, 80 flowi6_to_flowi(fl6), flags, &arg); 81 } else { 82 err = fib6_table_lookup(net, net->ipv6.fib6_local_tbl, oif, 83 fl6, res, flags); 84 if (err || res->f6i == net->ipv6.fib6_null_entry) 85 err = fib6_table_lookup(net, net->ipv6.fib6_main_tbl, 86 oif, fl6, res, flags); 87 } 88 89 return err; 90 } 91 92 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6, 93 const struct sk_buff *skb, 94 int flags, pol_lookup_t lookup) 95 { 96 if (net->ipv6.fib6_has_custom_rules) { 97 struct fib_lookup_arg arg = { 98 .lookup_ptr = lookup, 99 .lookup_data = skb, 100 .flags = FIB_LOOKUP_NOREF, 101 }; 102 103 /* update flow if oif or iif point to device enslaved to l3mdev */ 104 l3mdev_update_flow(net, flowi6_to_flowi(fl6)); 105 106 fib_rules_lookup(net->ipv6.fib6_rules_ops, 107 flowi6_to_flowi(fl6), flags, &arg); 108 109 if (arg.result) 110 return arg.result; 111 } else { 112 struct rt6_info *rt; 113 114 rt = lookup(net, net->ipv6.fib6_local_tbl, fl6, skb, flags); 115 if (rt != net->ipv6.ip6_null_entry && rt->dst.error != -EAGAIN) 116 return &rt->dst; 117 ip6_rt_put(rt); 118 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, skb, flags); 119 if (rt->dst.error != -EAGAIN) 120 return &rt->dst; 121 ip6_rt_put(rt); 122 } 123 124 dst_hold(&net->ipv6.ip6_null_entry->dst); 125 return &net->ipv6.ip6_null_entry->dst; 126 } 127 128 static int fib6_rule_saddr(struct net *net, struct fib_rule *rule, int flags, 129 struct flowi6 *flp6, const struct net_device *dev) 130 { 131 struct fib6_rule *r = (struct fib6_rule *)rule; 132 133 /* If we need to find a source address for this traffic, 134 * we check the result if it meets requirement of the rule. 135 */ 136 if ((rule->flags & FIB_RULE_FIND_SADDR) && 137 r->src.plen && !(flags & RT6_LOOKUP_F_HAS_SADDR)) { 138 struct in6_addr saddr; 139 140 if (ipv6_dev_get_saddr(net, dev, &flp6->daddr, 141 rt6_flags2srcprefs(flags), &saddr)) 142 return -EAGAIN; 143 144 if (!ipv6_prefix_equal(&saddr, &r->src.addr, r->src.plen)) 145 return -EAGAIN; 146 147 flp6->saddr = saddr; 148 } 149 150 return 0; 151 } 152 153 static int fib6_rule_action_alt(struct fib_rule *rule, struct flowi *flp, 154 int flags, struct fib_lookup_arg *arg) 155 { 156 struct fib6_result *res = arg->result; 157 struct flowi6 *flp6 = &flp->u.ip6; 158 struct net *net = rule->fr_net; 159 struct fib6_table *table; 160 int err = -EAGAIN, *oif; 161 u32 tb_id; 162 163 switch (rule->action) { 164 case FR_ACT_TO_TBL: 165 break; 166 case FR_ACT_UNREACHABLE: 167 return -ENETUNREACH; 168 case FR_ACT_PROHIBIT: 169 return -EACCES; 170 case FR_ACT_BLACKHOLE: 171 default: 172 return -EINVAL; 173 } 174 175 tb_id = fib_rule_get_table(rule, arg); 176 table = fib6_get_table(net, tb_id); 177 if (!table) 178 return -EAGAIN; 179 180 oif = (int *)arg->lookup_data; 181 err = fib6_table_lookup(net, table, *oif, flp6, res, flags); 182 if (!err && res->f6i != net->ipv6.fib6_null_entry) 183 err = fib6_rule_saddr(net, rule, flags, flp6, 184 res->nh->fib_nh_dev); 185 186 return err; 187 } 188 189 static int __fib6_rule_action(struct fib_rule *rule, struct flowi *flp, 190 int flags, struct fib_lookup_arg *arg) 191 { 192 struct flowi6 *flp6 = &flp->u.ip6; 193 struct rt6_info *rt = NULL; 194 struct fib6_table *table; 195 struct net *net = rule->fr_net; 196 pol_lookup_t lookup = arg->lookup_ptr; 197 int err = 0; 198 u32 tb_id; 199 200 switch (rule->action) { 201 case FR_ACT_TO_TBL: 202 break; 203 case FR_ACT_UNREACHABLE: 204 err = -ENETUNREACH; 205 rt = net->ipv6.ip6_null_entry; 206 goto discard_pkt; 207 default: 208 case FR_ACT_BLACKHOLE: 209 err = -EINVAL; 210 rt = net->ipv6.ip6_blk_hole_entry; 211 goto discard_pkt; 212 case FR_ACT_PROHIBIT: 213 err = -EACCES; 214 rt = net->ipv6.ip6_prohibit_entry; 215 goto discard_pkt; 216 } 217 218 tb_id = fib_rule_get_table(rule, arg); 219 table = fib6_get_table(net, tb_id); 220 if (!table) { 221 err = -EAGAIN; 222 goto out; 223 } 224 225 rt = lookup(net, table, flp6, arg->lookup_data, flags); 226 if (rt != net->ipv6.ip6_null_entry) { 227 err = fib6_rule_saddr(net, rule, flags, flp6, 228 ip6_dst_idev(&rt->dst)->dev); 229 230 if (err == -EAGAIN) 231 goto again; 232 233 err = rt->dst.error; 234 if (err != -EAGAIN) 235 goto out; 236 } 237 again: 238 ip6_rt_put(rt); 239 err = -EAGAIN; 240 rt = NULL; 241 goto out; 242 243 discard_pkt: 244 dst_hold(&rt->dst); 245 out: 246 arg->result = rt; 247 return err; 248 } 249 250 static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp, 251 int flags, struct fib_lookup_arg *arg) 252 { 253 if (arg->lookup_ptr == fib6_table_lookup) 254 return fib6_rule_action_alt(rule, flp, flags, arg); 255 256 return __fib6_rule_action(rule, flp, flags, arg); 257 } 258 259 static bool fib6_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg) 260 { 261 struct rt6_info *rt = (struct rt6_info *) arg->result; 262 struct net_device *dev = NULL; 263 264 if (rt->rt6i_idev) 265 dev = rt->rt6i_idev->dev; 266 267 /* do not accept result if the route does 268 * not meet the required prefix length 269 */ 270 if (rt->rt6i_dst.plen <= rule->suppress_prefixlen) 271 goto suppress_route; 272 273 /* do not accept result if the route uses a device 274 * belonging to a forbidden interface group 275 */ 276 if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup) 277 goto suppress_route; 278 279 return false; 280 281 suppress_route: 282 ip6_rt_put(rt); 283 return true; 284 } 285 286 static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) 287 { 288 struct fib6_rule *r = (struct fib6_rule *) rule; 289 struct flowi6 *fl6 = &fl->u.ip6; 290 291 if (r->dst.plen && 292 !ipv6_prefix_equal(&fl6->daddr, &r->dst.addr, r->dst.plen)) 293 return 0; 294 295 /* 296 * If FIB_RULE_FIND_SADDR is set and we do not have a 297 * source address for the traffic, we defer check for 298 * source address. 299 */ 300 if (r->src.plen) { 301 if (flags & RT6_LOOKUP_F_HAS_SADDR) { 302 if (!ipv6_prefix_equal(&fl6->saddr, &r->src.addr, 303 r->src.plen)) 304 return 0; 305 } else if (!(r->common.flags & FIB_RULE_FIND_SADDR)) 306 return 0; 307 } 308 309 if (r->tclass && r->tclass != ip6_tclass(fl6->flowlabel)) 310 return 0; 311 312 if (rule->ip_proto && (rule->ip_proto != fl6->flowi6_proto)) 313 return 0; 314 315 if (fib_rule_port_range_set(&rule->sport_range) && 316 !fib_rule_port_inrange(&rule->sport_range, fl6->fl6_sport)) 317 return 0; 318 319 if (fib_rule_port_range_set(&rule->dport_range) && 320 !fib_rule_port_inrange(&rule->dport_range, fl6->fl6_dport)) 321 return 0; 322 323 return 1; 324 } 325 326 static const struct nla_policy fib6_rule_policy[FRA_MAX+1] = { 327 FRA_GENERIC_POLICY, 328 }; 329 330 static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb, 331 struct fib_rule_hdr *frh, 332 struct nlattr **tb, 333 struct netlink_ext_ack *extack) 334 { 335 int err = -EINVAL; 336 struct net *net = sock_net(skb->sk); 337 struct fib6_rule *rule6 = (struct fib6_rule *) rule; 338 339 if (rule->action == FR_ACT_TO_TBL && !rule->l3mdev) { 340 if (rule->table == RT6_TABLE_UNSPEC) { 341 NL_SET_ERR_MSG(extack, "Invalid table"); 342 goto errout; 343 } 344 345 if (fib6_new_table(net, rule->table) == NULL) { 346 err = -ENOBUFS; 347 goto errout; 348 } 349 } 350 351 if (frh->src_len) 352 rule6->src.addr = nla_get_in6_addr(tb[FRA_SRC]); 353 354 if (frh->dst_len) 355 rule6->dst.addr = nla_get_in6_addr(tb[FRA_DST]); 356 357 rule6->src.plen = frh->src_len; 358 rule6->dst.plen = frh->dst_len; 359 rule6->tclass = frh->tos; 360 361 if (fib_rule_requires_fldissect(rule)) 362 net->ipv6.fib6_rules_require_fldissect++; 363 364 net->ipv6.fib6_has_custom_rules = true; 365 err = 0; 366 errout: 367 return err; 368 } 369 370 static int fib6_rule_delete(struct fib_rule *rule) 371 { 372 struct net *net = rule->fr_net; 373 374 if (net->ipv6.fib6_rules_require_fldissect && 375 fib_rule_requires_fldissect(rule)) 376 net->ipv6.fib6_rules_require_fldissect--; 377 378 return 0; 379 } 380 381 static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, 382 struct nlattr **tb) 383 { 384 struct fib6_rule *rule6 = (struct fib6_rule *) rule; 385 386 if (frh->src_len && (rule6->src.plen != frh->src_len)) 387 return 0; 388 389 if (frh->dst_len && (rule6->dst.plen != frh->dst_len)) 390 return 0; 391 392 if (frh->tos && (rule6->tclass != frh->tos)) 393 return 0; 394 395 if (frh->src_len && 396 nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr))) 397 return 0; 398 399 if (frh->dst_len && 400 nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr))) 401 return 0; 402 403 return 1; 404 } 405 406 static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb, 407 struct fib_rule_hdr *frh) 408 { 409 struct fib6_rule *rule6 = (struct fib6_rule *) rule; 410 411 frh->dst_len = rule6->dst.plen; 412 frh->src_len = rule6->src.plen; 413 frh->tos = rule6->tclass; 414 415 if ((rule6->dst.plen && 416 nla_put_in6_addr(skb, FRA_DST, &rule6->dst.addr)) || 417 (rule6->src.plen && 418 nla_put_in6_addr(skb, FRA_SRC, &rule6->src.addr))) 419 goto nla_put_failure; 420 return 0; 421 422 nla_put_failure: 423 return -ENOBUFS; 424 } 425 426 static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule) 427 { 428 return nla_total_size(16) /* dst */ 429 + nla_total_size(16); /* src */ 430 } 431 432 static const struct fib_rules_ops __net_initconst fib6_rules_ops_template = { 433 .family = AF_INET6, 434 .rule_size = sizeof(struct fib6_rule), 435 .addr_size = sizeof(struct in6_addr), 436 .action = fib6_rule_action, 437 .match = fib6_rule_match, 438 .suppress = fib6_rule_suppress, 439 .configure = fib6_rule_configure, 440 .delete = fib6_rule_delete, 441 .compare = fib6_rule_compare, 442 .fill = fib6_rule_fill, 443 .nlmsg_payload = fib6_rule_nlmsg_payload, 444 .nlgroup = RTNLGRP_IPV6_RULE, 445 .policy = fib6_rule_policy, 446 .owner = THIS_MODULE, 447 .fro_net = &init_net, 448 }; 449 450 static int __net_init fib6_rules_net_init(struct net *net) 451 { 452 struct fib_rules_ops *ops; 453 int err = -ENOMEM; 454 455 ops = fib_rules_register(&fib6_rules_ops_template, net); 456 if (IS_ERR(ops)) 457 return PTR_ERR(ops); 458 459 err = fib_default_rule_add(ops, 0, RT6_TABLE_LOCAL, 0); 460 if (err) 461 goto out_fib6_rules_ops; 462 463 err = fib_default_rule_add(ops, 0x7FFE, RT6_TABLE_MAIN, 0); 464 if (err) 465 goto out_fib6_rules_ops; 466 467 net->ipv6.fib6_rules_ops = ops; 468 net->ipv6.fib6_rules_require_fldissect = 0; 469 out: 470 return err; 471 472 out_fib6_rules_ops: 473 fib_rules_unregister(ops); 474 goto out; 475 } 476 477 static void __net_exit fib6_rules_net_exit(struct net *net) 478 { 479 rtnl_lock(); 480 fib_rules_unregister(net->ipv6.fib6_rules_ops); 481 rtnl_unlock(); 482 } 483 484 static struct pernet_operations fib6_rules_net_ops = { 485 .init = fib6_rules_net_init, 486 .exit = fib6_rules_net_exit, 487 }; 488 489 int __init fib6_rules_init(void) 490 { 491 return register_pernet_subsys(&fib6_rules_net_ops); 492 } 493 494 495 void fib6_rules_cleanup(void) 496 { 497 unregister_pernet_subsys(&fib6_rules_net_ops); 498 } 499