11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 31da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 41da177e4SLinus Torvalds * interface as the means of communication with the user level. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * IPv4 Forwarding Information Base: policy rules. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 9e1ef4bf2SThomas Graf * Thomas Graf <tgraf@suug.ch> 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or 121da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 131da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 141da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * Fixes: 171da177e4SLinus Torvalds * Rani Assaf : local_rule cannot be deleted 181da177e4SLinus Torvalds * Marc Boucher : routing by fwmark 191da177e4SLinus Torvalds */ 201da177e4SLinus Torvalds 211da177e4SLinus Torvalds #include <linux/types.h> 221da177e4SLinus Torvalds #include <linux/kernel.h> 231da177e4SLinus Torvalds #include <linux/netdevice.h> 241da177e4SLinus Torvalds #include <linux/netlink.h> 25e1ef4bf2SThomas Graf #include <linux/inetdevice.h> 261da177e4SLinus Torvalds #include <linux/init.h> 277b204afdSRobert Olsson #include <linux/list.h> 287b204afdSRobert Olsson #include <linux/rcupdate.h> 29bc3b2d7fSPaul Gortmaker #include <linux/export.h> 301da177e4SLinus Torvalds #include <net/ip.h> 311da177e4SLinus Torvalds #include <net/route.h> 321da177e4SLinus Torvalds #include <net/tcp.h> 331da177e4SLinus Torvalds #include <net/ip_fib.h> 34e1ef4bf2SThomas Graf #include <net/fib_rules.h> 351da177e4SLinus Torvalds 366a31d2a9SEric Dumazet struct fib4_rule { 37e1ef4bf2SThomas Graf struct fib_rule common; 38e1ef4bf2SThomas Graf u8 dst_len; 39e1ef4bf2SThomas Graf u8 src_len; 40e1ef4bf2SThomas Graf u8 tos; 4181f7bf6cSAl Viro __be32 src; 4281f7bf6cSAl Viro __be32 srcmask; 4381f7bf6cSAl Viro __be32 dst; 4481f7bf6cSAl Viro __be32 dstmask; 45c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 46e1ef4bf2SThomas Graf u32 tclassid; 471da177e4SLinus Torvalds #endif 481da177e4SLinus Torvalds }; 491da177e4SLinus Torvalds 50f4530fa5SDavid S. Miller int __fib_lookup(struct net *net, struct flowi4 *flp, struct fib_result *res) 51e1ef4bf2SThomas Graf { 52e1ef4bf2SThomas Graf struct fib_lookup_arg arg = { 53e1ef4bf2SThomas Graf .result = res, 54ebc0ffaeSEric Dumazet .flags = FIB_LOOKUP_NOREF, 55e1ef4bf2SThomas Graf }; 56e1ef4bf2SThomas Graf int err; 57e1ef4bf2SThomas Graf 5822bd5b9bSDavid S. Miller err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg); 5985b91b03SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID 6085b91b03SDavid S. Miller if (arg.rule) 6185b91b03SDavid S. Miller res->tclassid = ((struct fib4_rule *)arg.rule)->tclassid; 6285b91b03SDavid S. Miller else 6385b91b03SDavid S. Miller res->tclassid = 0; 6485b91b03SDavid S. Miller #endif 6549dd18baSPanu Matilainen 6649dd18baSPanu Matilainen if (err == -ESRCH) 6749dd18baSPanu Matilainen err = -ENETUNREACH; 6849dd18baSPanu Matilainen 691da177e4SLinus Torvalds return err; 701da177e4SLinus Torvalds } 71f4530fa5SDavid S. Miller EXPORT_SYMBOL_GPL(__fib_lookup); 721da177e4SLinus Torvalds 738ce11e6aSAdrian Bunk static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp, 748ce11e6aSAdrian Bunk int flags, struct fib_lookup_arg *arg) 75e1ef4bf2SThomas Graf { 76e1ef4bf2SThomas Graf int err = -EAGAIN; 77e1ef4bf2SThomas Graf struct fib_table *tbl; 78e1ef4bf2SThomas Graf 79e1ef4bf2SThomas Graf switch (rule->action) { 80e1ef4bf2SThomas Graf case FR_ACT_TO_TBL: 81e1ef4bf2SThomas Graf break; 82e1ef4bf2SThomas Graf 83e1ef4bf2SThomas Graf case FR_ACT_UNREACHABLE: 84345e9b54SAlexander Duyck return -ENETUNREACH; 85e1ef4bf2SThomas Graf 86e1ef4bf2SThomas Graf case FR_ACT_PROHIBIT: 87345e9b54SAlexander Duyck return -EACCES; 88e1ef4bf2SThomas Graf 89e1ef4bf2SThomas Graf case FR_ACT_BLACKHOLE: 90e1ef4bf2SThomas Graf default: 91345e9b54SAlexander Duyck return -EINVAL; 92e1ef4bf2SThomas Graf } 93e1ef4bf2SThomas Graf 94345e9b54SAlexander Duyck rcu_read_lock(); 95e1ef4bf2SThomas Graf 96345e9b54SAlexander Duyck tbl = fib_get_table(rule->fr_net, rule->table); 97345e9b54SAlexander Duyck if (tbl) 98345e9b54SAlexander Duyck err = fib_table_lookup(tbl, &flp->u.ip4, 99345e9b54SAlexander Duyck (struct fib_result *)arg->result, 100345e9b54SAlexander Duyck arg->flags); 101345e9b54SAlexander Duyck 102345e9b54SAlexander Duyck rcu_read_unlock(); 103e1ef4bf2SThomas Graf return err; 104e1ef4bf2SThomas Graf } 105e1ef4bf2SThomas Graf 1067764a45aSStefan Tomanek static bool fib4_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg) 1077764a45aSStefan Tomanek { 1086ef94cfaSStefan Tomanek struct fib_result *result = (struct fib_result *) arg->result; 109673498b8SStefan Tomanek struct net_device *dev = NULL; 110673498b8SStefan Tomanek 111673498b8SStefan Tomanek if (result->fi) 112673498b8SStefan Tomanek dev = result->fi->fib_dev; 1136ef94cfaSStefan Tomanek 1147764a45aSStefan Tomanek /* do not accept result if the route does 1157764a45aSStefan Tomanek * not meet the required prefix length 1167764a45aSStefan Tomanek */ 11773f5698eSStefan Tomanek if (result->prefixlen <= rule->suppress_prefixlen) 1186ef94cfaSStefan Tomanek goto suppress_route; 1196ef94cfaSStefan Tomanek 1206ef94cfaSStefan Tomanek /* do not accept result if the route uses a device 1216ef94cfaSStefan Tomanek * belonging to a forbidden interface group 1226ef94cfaSStefan Tomanek */ 1236ef94cfaSStefan Tomanek if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup) 1246ef94cfaSStefan Tomanek goto suppress_route; 1256ef94cfaSStefan Tomanek 1266ef94cfaSStefan Tomanek return false; 1276ef94cfaSStefan Tomanek 1286ef94cfaSStefan Tomanek suppress_route: 1297764a45aSStefan Tomanek if (!(arg->flags & FIB_LOOKUP_NOREF)) 1307764a45aSStefan Tomanek fib_info_put(result->fi); 1317764a45aSStefan Tomanek return true; 1327764a45aSStefan Tomanek } 133e1ef4bf2SThomas Graf 134e1ef4bf2SThomas Graf static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) 135e1ef4bf2SThomas Graf { 136e1ef4bf2SThomas Graf struct fib4_rule *r = (struct fib4_rule *) rule; 1379ade2286SDavid S. Miller struct flowi4 *fl4 = &fl->u.ip4; 1389ade2286SDavid S. Miller __be32 daddr = fl4->daddr; 1399ade2286SDavid S. Miller __be32 saddr = fl4->saddr; 140e1ef4bf2SThomas Graf 141e1ef4bf2SThomas Graf if (((saddr ^ r->src) & r->srcmask) || 142e1ef4bf2SThomas Graf ((daddr ^ r->dst) & r->dstmask)) 143e1ef4bf2SThomas Graf return 0; 144e1ef4bf2SThomas Graf 1459ade2286SDavid S. Miller if (r->tos && (r->tos != fl4->flowi4_tos)) 146e1ef4bf2SThomas Graf return 0; 147e1ef4bf2SThomas Graf 148e1ef4bf2SThomas Graf return 1; 149e1ef4bf2SThomas Graf } 1501da177e4SLinus Torvalds 1518ad4942cSDenis V. Lunev static struct fib_table *fib_empty_table(struct net *net) 1521da177e4SLinus Torvalds { 1532dfe55b4SPatrick McHardy u32 id; 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds for (id = 1; id <= RT_TABLE_MAX; id++) 156*51456b29SIan Morris if (!fib_get_table(net, id)) 1578ad4942cSDenis V. Lunev return fib_new_table(net, id); 1581da177e4SLinus Torvalds return NULL; 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 161ef7c79edSPatrick McHardy static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = { 1621f6c9557SThomas Graf FRA_GENERIC_POLICY, 163e1ef4bf2SThomas Graf [FRA_FLOW] = { .type = NLA_U32 }, 1641da177e4SLinus Torvalds }; 1651da177e4SLinus Torvalds 166e1ef4bf2SThomas Graf static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb, 1678b3521eeSRami Rosen struct fib_rule_hdr *frh, 168e1ef4bf2SThomas Graf struct nlattr **tb) 1691da177e4SLinus Torvalds { 1703b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 171e1ef4bf2SThomas Graf int err = -EINVAL; 172e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 1731da177e4SLinus Torvalds 174e1701c68SThomas Graf if (frh->tos & ~IPTOS_TOS_MASK) 175e1ef4bf2SThomas Graf goto errout; 176e1ef4bf2SThomas Graf 1770ddcf43dSAlexander Duyck /* split local/main if they are not already split */ 1780ddcf43dSAlexander Duyck err = fib_unmerge(net); 1790ddcf43dSAlexander Duyck if (err) 1800ddcf43dSAlexander Duyck goto errout; 1810ddcf43dSAlexander Duyck 182e1ef4bf2SThomas Graf if (rule->table == RT_TABLE_UNSPEC) { 183e1ef4bf2SThomas Graf if (rule->action == FR_ACT_TO_TBL) { 184e1ef4bf2SThomas Graf struct fib_table *table; 185e1ef4bf2SThomas Graf 186e4e4971cSDenis V. Lunev table = fib_empty_table(net); 187*51456b29SIan Morris if (!table) { 188e1ef4bf2SThomas Graf err = -ENOBUFS; 189e1ef4bf2SThomas Graf goto errout; 190e1ef4bf2SThomas Graf } 191e1ef4bf2SThomas Graf 192e1ef4bf2SThomas Graf rule->table = table->tb_id; 193e1ef4bf2SThomas Graf } 194e1ef4bf2SThomas Graf } 195e1ef4bf2SThomas Graf 196e1701c68SThomas Graf if (frh->src_len) 19767b61f6cSJiri Benc rule4->src = nla_get_in_addr(tb[FRA_SRC]); 198e1ef4bf2SThomas Graf 199e1701c68SThomas Graf if (frh->dst_len) 20067b61f6cSJiri Benc rule4->dst = nla_get_in_addr(tb[FRA_DST]); 201e1ef4bf2SThomas Graf 202c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 2037a9bc9b8SDavid S. Miller if (tb[FRA_FLOW]) { 204e1ef4bf2SThomas Graf rule4->tclassid = nla_get_u32(tb[FRA_FLOW]); 2057a9bc9b8SDavid S. Miller if (rule4->tclassid) 206f4530fa5SDavid S. Miller net->ipv4.fib_num_tclassid_users++; 2077a9bc9b8SDavid S. Miller } 2081da177e4SLinus Torvalds #endif 2091da177e4SLinus Torvalds 210e1ef4bf2SThomas Graf rule4->src_len = frh->src_len; 211e1ef4bf2SThomas Graf rule4->srcmask = inet_make_mask(rule4->src_len); 212e1ef4bf2SThomas Graf rule4->dst_len = frh->dst_len; 213e1ef4bf2SThomas Graf rule4->dstmask = inet_make_mask(rule4->dst_len); 214e1ef4bf2SThomas Graf rule4->tos = frh->tos; 215e1ef4bf2SThomas Graf 216f4530fa5SDavid S. Miller net->ipv4.fib_has_custom_rules = true; 217104616e7SScott Feldman fib_flush_external(rule->fr_net); 218104616e7SScott Feldman 219e1ef4bf2SThomas Graf err = 0; 220e1ef4bf2SThomas Graf errout: 221e1ef4bf2SThomas Graf return err; 2221da177e4SLinus Torvalds } 2231da177e4SLinus Torvalds 2240ddcf43dSAlexander Duyck static int fib4_rule_delete(struct fib_rule *rule) 2257a9bc9b8SDavid S. Miller { 226f4530fa5SDavid S. Miller struct net *net = rule->fr_net; 2270ddcf43dSAlexander Duyck int err; 2287a9bc9b8SDavid S. Miller 2290ddcf43dSAlexander Duyck /* split local/main if they are not already split */ 2300ddcf43dSAlexander Duyck err = fib_unmerge(net); 2310ddcf43dSAlexander Duyck if (err) 2320ddcf43dSAlexander Duyck goto errout; 2330ddcf43dSAlexander Duyck 2340ddcf43dSAlexander Duyck #ifdef CONFIG_IP_ROUTE_CLASSID 2350ddcf43dSAlexander Duyck if (((struct fib4_rule *)rule)->tclassid) 236f4530fa5SDavid S. Miller net->ipv4.fib_num_tclassid_users--; 2377a9bc9b8SDavid S. Miller #endif 238f4530fa5SDavid S. Miller net->ipv4.fib_has_custom_rules = true; 239104616e7SScott Feldman fib_flush_external(rule->fr_net); 2400ddcf43dSAlexander Duyck errout: 2410ddcf43dSAlexander Duyck return err; 2427a9bc9b8SDavid S. Miller } 2437a9bc9b8SDavid S. Miller 244e1ef4bf2SThomas Graf static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, 245e1ef4bf2SThomas Graf struct nlattr **tb) 246a5cdc030SPatrick McHardy { 247e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 248a5cdc030SPatrick McHardy 249e1ef4bf2SThomas Graf if (frh->src_len && (rule4->src_len != frh->src_len)) 250e1ef4bf2SThomas Graf return 0; 251e1ef4bf2SThomas Graf 252e1ef4bf2SThomas Graf if (frh->dst_len && (rule4->dst_len != frh->dst_len)) 253e1ef4bf2SThomas Graf return 0; 254e1ef4bf2SThomas Graf 255e1ef4bf2SThomas Graf if (frh->tos && (rule4->tos != frh->tos)) 256e1ef4bf2SThomas Graf return 0; 257e1ef4bf2SThomas Graf 258c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 259e1ef4bf2SThomas Graf if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW]))) 260e1ef4bf2SThomas Graf return 0; 261e1ef4bf2SThomas Graf #endif 262e1ef4bf2SThomas Graf 26367b61f6cSJiri Benc if (frh->src_len && (rule4->src != nla_get_in_addr(tb[FRA_SRC]))) 264e1ef4bf2SThomas Graf return 0; 265e1ef4bf2SThomas Graf 26667b61f6cSJiri Benc if (frh->dst_len && (rule4->dst != nla_get_in_addr(tb[FRA_DST]))) 267e1ef4bf2SThomas Graf return 0; 268e1ef4bf2SThomas Graf 269e1ef4bf2SThomas Graf return 1; 270a5cdc030SPatrick McHardy } 271a5cdc030SPatrick McHardy 272e1ef4bf2SThomas Graf static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb, 27304af8cf6SRami Rosen struct fib_rule_hdr *frh) 2741da177e4SLinus Torvalds { 275e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 2761da177e4SLinus Torvalds 277e1ef4bf2SThomas Graf frh->dst_len = rule4->dst_len; 278e1ef4bf2SThomas Graf frh->src_len = rule4->src_len; 279e1ef4bf2SThomas Graf frh->tos = rule4->tos; 2801da177e4SLinus Torvalds 281f3756b79SDavid S. Miller if ((rule4->dst_len && 282930345eaSJiri Benc nla_put_in_addr(skb, FRA_DST, rule4->dst)) || 283f3756b79SDavid S. Miller (rule4->src_len && 284930345eaSJiri Benc nla_put_in_addr(skb, FRA_SRC, rule4->src))) 285f3756b79SDavid S. Miller goto nla_put_failure; 286c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 287f3756b79SDavid S. Miller if (rule4->tclassid && 288f3756b79SDavid S. Miller nla_put_u32(skb, FRA_FLOW, rule4->tclassid)) 289f3756b79SDavid S. Miller goto nla_put_failure; 290e1ef4bf2SThomas Graf #endif 291e1ef4bf2SThomas Graf return 0; 292e1ef4bf2SThomas Graf 293e1ef4bf2SThomas Graf nla_put_failure: 294e1ef4bf2SThomas Graf return -ENOBUFS; 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds 297339bf98fSThomas Graf static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule) 298339bf98fSThomas Graf { 299339bf98fSThomas Graf return nla_total_size(4) /* dst */ 300339bf98fSThomas Graf + nla_total_size(4) /* src */ 301339bf98fSThomas Graf + nla_total_size(4); /* flow */ 302339bf98fSThomas Graf } 303339bf98fSThomas Graf 304ae299fc0SDenis V. Lunev static void fib4_rule_flush_cache(struct fib_rules_ops *ops) 30573417f61SThomas Graf { 306bafa6d9dSNicolas Dichtel rt_cache_flush(ops->fro_net); 30773417f61SThomas Graf } 30873417f61SThomas Graf 30904a6f82cSAndi Kleen static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = { 31025239ceeSPatrick McHardy .family = AF_INET, 311e1ef4bf2SThomas Graf .rule_size = sizeof(struct fib4_rule), 312e1701c68SThomas Graf .addr_size = sizeof(u32), 313e1ef4bf2SThomas Graf .action = fib4_rule_action, 3147764a45aSStefan Tomanek .suppress = fib4_rule_suppress, 315e1ef4bf2SThomas Graf .match = fib4_rule_match, 316e1ef4bf2SThomas Graf .configure = fib4_rule_configure, 3177a9bc9b8SDavid S. Miller .delete = fib4_rule_delete, 318e1ef4bf2SThomas Graf .compare = fib4_rule_compare, 319e1ef4bf2SThomas Graf .fill = fib4_rule_fill, 320d8a566beSPatrick McHardy .default_pref = fib_default_rule_pref, 321339bf98fSThomas Graf .nlmsg_payload = fib4_rule_nlmsg_payload, 32273417f61SThomas Graf .flush_cache = fib4_rule_flush_cache, 323e1ef4bf2SThomas Graf .nlgroup = RTNLGRP_IPV4_RULE, 324e1ef4bf2SThomas Graf .policy = fib4_rule_policy, 325e1ef4bf2SThomas Graf .owner = THIS_MODULE, 326e1ef4bf2SThomas Graf }; 327e1ef4bf2SThomas Graf 328e4e4971cSDenis V. Lunev static int fib_default_rules_init(struct fib_rules_ops *ops) 3292994c638SDenis V. Lunev { 3302994c638SDenis V. Lunev int err; 3312994c638SDenis V. Lunev 3325adef180SPatrick McHardy err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0); 3332994c638SDenis V. Lunev if (err < 0) 3342994c638SDenis V. Lunev return err; 335e4e4971cSDenis V. Lunev err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0); 3362994c638SDenis V. Lunev if (err < 0) 3372994c638SDenis V. Lunev return err; 338e4e4971cSDenis V. Lunev err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0); 3392994c638SDenis V. Lunev if (err < 0) 3402994c638SDenis V. Lunev return err; 3412994c638SDenis V. Lunev return 0; 3422994c638SDenis V. Lunev } 3432994c638SDenis V. Lunev 3447b1a74fdSDenis V. Lunev int __net_init fib4_rules_init(struct net *net) 345e1ef4bf2SThomas Graf { 346dbb50165SDenis V. Lunev int err; 347e4e4971cSDenis V. Lunev struct fib_rules_ops *ops; 348dbb50165SDenis V. Lunev 349e9c5158aSEric W. Biederman ops = fib_rules_register(&fib4_rules_ops_template, net); 350e9c5158aSEric W. Biederman if (IS_ERR(ops)) 351e9c5158aSEric W. Biederman return PTR_ERR(ops); 352e4e4971cSDenis V. Lunev 353e4e4971cSDenis V. Lunev err = fib_default_rules_init(ops); 354dbb50165SDenis V. Lunev if (err < 0) 355dbb50165SDenis V. Lunev goto fail; 356e4e4971cSDenis V. Lunev net->ipv4.rules_ops = ops; 357f4530fa5SDavid S. Miller net->ipv4.fib_has_custom_rules = false; 358dbb50165SDenis V. Lunev return 0; 359dbb50165SDenis V. Lunev 360dbb50165SDenis V. Lunev fail: 361dbb50165SDenis V. Lunev /* also cleans all rules already added */ 3629e3a5487SDenis V. Lunev fib_rules_unregister(ops); 363dbb50165SDenis V. Lunev return err; 364e1ef4bf2SThomas Graf } 3657b1a74fdSDenis V. Lunev 3667b1a74fdSDenis V. Lunev void __net_exit fib4_rules_exit(struct net *net) 3677b1a74fdSDenis V. Lunev { 3689e3a5487SDenis V. Lunev fib_rules_unregister(net->ipv4.rules_ops); 3697b1a74fdSDenis V. Lunev } 370