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 500eeb075fSAndy Gospodarek int __fib_lookup(struct net *net, struct flowi4 *flp, 510eeb075fSAndy Gospodarek struct fib_result *res, unsigned int flags) 52e1ef4bf2SThomas Graf { 53e1ef4bf2SThomas Graf struct fib_lookup_arg arg = { 54e1ef4bf2SThomas Graf .result = res, 550eeb075fSAndy Gospodarek .flags = flags, 56e1ef4bf2SThomas Graf }; 57e1ef4bf2SThomas Graf int err; 58e1ef4bf2SThomas Graf 59*9ee0034bSDavid Ahern /* update flow if oif or iif point to device enslaved to l3mdev */ 60*9ee0034bSDavid Ahern l3mdev_update_flow(net, flowi4_to_flowi(flp)); 61*9ee0034bSDavid Ahern 6222bd5b9bSDavid S. Miller err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg); 6385b91b03SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID 6485b91b03SDavid S. Miller if (arg.rule) 6585b91b03SDavid S. Miller res->tclassid = ((struct fib4_rule *)arg.rule)->tclassid; 6685b91b03SDavid S. Miller else 6785b91b03SDavid S. Miller res->tclassid = 0; 6885b91b03SDavid S. Miller #endif 6949dd18baSPanu Matilainen 7049dd18baSPanu Matilainen if (err == -ESRCH) 7149dd18baSPanu Matilainen err = -ENETUNREACH; 7249dd18baSPanu Matilainen 731da177e4SLinus Torvalds return err; 741da177e4SLinus Torvalds } 75f4530fa5SDavid S. Miller EXPORT_SYMBOL_GPL(__fib_lookup); 761da177e4SLinus Torvalds 778ce11e6aSAdrian Bunk static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp, 788ce11e6aSAdrian Bunk int flags, struct fib_lookup_arg *arg) 79e1ef4bf2SThomas Graf { 80e1ef4bf2SThomas Graf int err = -EAGAIN; 81e1ef4bf2SThomas Graf struct fib_table *tbl; 8296c63fa7SDavid Ahern u32 tb_id; 83e1ef4bf2SThomas Graf 84e1ef4bf2SThomas Graf switch (rule->action) { 85e1ef4bf2SThomas Graf case FR_ACT_TO_TBL: 86e1ef4bf2SThomas Graf break; 87e1ef4bf2SThomas Graf 88e1ef4bf2SThomas Graf case FR_ACT_UNREACHABLE: 89345e9b54SAlexander Duyck return -ENETUNREACH; 90e1ef4bf2SThomas Graf 91e1ef4bf2SThomas Graf case FR_ACT_PROHIBIT: 92345e9b54SAlexander Duyck return -EACCES; 93e1ef4bf2SThomas Graf 94e1ef4bf2SThomas Graf case FR_ACT_BLACKHOLE: 95e1ef4bf2SThomas Graf default: 96345e9b54SAlexander Duyck return -EINVAL; 97e1ef4bf2SThomas Graf } 98e1ef4bf2SThomas Graf 99345e9b54SAlexander Duyck rcu_read_lock(); 100e1ef4bf2SThomas Graf 10196c63fa7SDavid Ahern tb_id = fib_rule_get_table(rule, arg); 10296c63fa7SDavid Ahern tbl = fib_get_table(rule->fr_net, tb_id); 103345e9b54SAlexander Duyck if (tbl) 104345e9b54SAlexander Duyck err = fib_table_lookup(tbl, &flp->u.ip4, 105345e9b54SAlexander Duyck (struct fib_result *)arg->result, 106345e9b54SAlexander Duyck arg->flags); 107345e9b54SAlexander Duyck 108345e9b54SAlexander Duyck rcu_read_unlock(); 109e1ef4bf2SThomas Graf return err; 110e1ef4bf2SThomas Graf } 111e1ef4bf2SThomas Graf 1127764a45aSStefan Tomanek static bool fib4_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg) 1137764a45aSStefan Tomanek { 1146ef94cfaSStefan Tomanek struct fib_result *result = (struct fib_result *) arg->result; 115673498b8SStefan Tomanek struct net_device *dev = NULL; 116673498b8SStefan Tomanek 117673498b8SStefan Tomanek if (result->fi) 118673498b8SStefan Tomanek dev = result->fi->fib_dev; 1196ef94cfaSStefan Tomanek 1207764a45aSStefan Tomanek /* do not accept result if the route does 1217764a45aSStefan Tomanek * not meet the required prefix length 1227764a45aSStefan Tomanek */ 12373f5698eSStefan Tomanek if (result->prefixlen <= rule->suppress_prefixlen) 1246ef94cfaSStefan Tomanek goto suppress_route; 1256ef94cfaSStefan Tomanek 1266ef94cfaSStefan Tomanek /* do not accept result if the route uses a device 1276ef94cfaSStefan Tomanek * belonging to a forbidden interface group 1286ef94cfaSStefan Tomanek */ 1296ef94cfaSStefan Tomanek if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup) 1306ef94cfaSStefan Tomanek goto suppress_route; 1316ef94cfaSStefan Tomanek 1326ef94cfaSStefan Tomanek return false; 1336ef94cfaSStefan Tomanek 1346ef94cfaSStefan Tomanek suppress_route: 1357764a45aSStefan Tomanek if (!(arg->flags & FIB_LOOKUP_NOREF)) 1367764a45aSStefan Tomanek fib_info_put(result->fi); 1377764a45aSStefan Tomanek return true; 1387764a45aSStefan Tomanek } 139e1ef4bf2SThomas Graf 140e1ef4bf2SThomas Graf static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) 141e1ef4bf2SThomas Graf { 142e1ef4bf2SThomas Graf struct fib4_rule *r = (struct fib4_rule *) rule; 1439ade2286SDavid S. Miller struct flowi4 *fl4 = &fl->u.ip4; 1449ade2286SDavid S. Miller __be32 daddr = fl4->daddr; 1459ade2286SDavid S. Miller __be32 saddr = fl4->saddr; 146e1ef4bf2SThomas Graf 147e1ef4bf2SThomas Graf if (((saddr ^ r->src) & r->srcmask) || 148e1ef4bf2SThomas Graf ((daddr ^ r->dst) & r->dstmask)) 149e1ef4bf2SThomas Graf return 0; 150e1ef4bf2SThomas Graf 1519ade2286SDavid S. Miller if (r->tos && (r->tos != fl4->flowi4_tos)) 152e1ef4bf2SThomas Graf return 0; 153e1ef4bf2SThomas Graf 154e1ef4bf2SThomas Graf return 1; 155e1ef4bf2SThomas Graf } 1561da177e4SLinus Torvalds 1578ad4942cSDenis V. Lunev static struct fib_table *fib_empty_table(struct net *net) 1581da177e4SLinus Torvalds { 1592dfe55b4SPatrick McHardy u32 id; 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds for (id = 1; id <= RT_TABLE_MAX; id++) 16251456b29SIan Morris if (!fib_get_table(net, id)) 1638ad4942cSDenis V. Lunev return fib_new_table(net, id); 1641da177e4SLinus Torvalds return NULL; 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds 167ef7c79edSPatrick McHardy static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = { 1681f6c9557SThomas Graf FRA_GENERIC_POLICY, 169e1ef4bf2SThomas Graf [FRA_FLOW] = { .type = NLA_U32 }, 1701da177e4SLinus Torvalds }; 1711da177e4SLinus Torvalds 172e1ef4bf2SThomas Graf static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb, 1738b3521eeSRami Rosen struct fib_rule_hdr *frh, 174e1ef4bf2SThomas Graf struct nlattr **tb) 1751da177e4SLinus Torvalds { 1763b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 177e1ef4bf2SThomas Graf int err = -EINVAL; 178e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 1791da177e4SLinus Torvalds 180e1701c68SThomas Graf if (frh->tos & ~IPTOS_TOS_MASK) 181e1ef4bf2SThomas Graf goto errout; 182e1ef4bf2SThomas Graf 1830ddcf43dSAlexander Duyck /* split local/main if they are not already split */ 1840ddcf43dSAlexander Duyck err = fib_unmerge(net); 1850ddcf43dSAlexander Duyck if (err) 1860ddcf43dSAlexander Duyck goto errout; 1870ddcf43dSAlexander Duyck 18896c63fa7SDavid Ahern if (rule->table == RT_TABLE_UNSPEC && !rule->l3mdev) { 189e1ef4bf2SThomas Graf if (rule->action == FR_ACT_TO_TBL) { 190e1ef4bf2SThomas Graf struct fib_table *table; 191e1ef4bf2SThomas Graf 192e4e4971cSDenis V. Lunev table = fib_empty_table(net); 19351456b29SIan Morris if (!table) { 194e1ef4bf2SThomas Graf err = -ENOBUFS; 195e1ef4bf2SThomas Graf goto errout; 196e1ef4bf2SThomas Graf } 197e1ef4bf2SThomas Graf 198e1ef4bf2SThomas Graf rule->table = table->tb_id; 199e1ef4bf2SThomas Graf } 200e1ef4bf2SThomas Graf } 201e1ef4bf2SThomas Graf 202e1701c68SThomas Graf if (frh->src_len) 20367b61f6cSJiri Benc rule4->src = nla_get_in_addr(tb[FRA_SRC]); 204e1ef4bf2SThomas Graf 205e1701c68SThomas Graf if (frh->dst_len) 20667b61f6cSJiri Benc rule4->dst = nla_get_in_addr(tb[FRA_DST]); 207e1ef4bf2SThomas Graf 208c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 2097a9bc9b8SDavid S. Miller if (tb[FRA_FLOW]) { 210e1ef4bf2SThomas Graf rule4->tclassid = nla_get_u32(tb[FRA_FLOW]); 2117a9bc9b8SDavid S. Miller if (rule4->tclassid) 212f4530fa5SDavid S. Miller net->ipv4.fib_num_tclassid_users++; 2137a9bc9b8SDavid S. Miller } 2141da177e4SLinus Torvalds #endif 2151da177e4SLinus Torvalds 216e1ef4bf2SThomas Graf rule4->src_len = frh->src_len; 217e1ef4bf2SThomas Graf rule4->srcmask = inet_make_mask(rule4->src_len); 218e1ef4bf2SThomas Graf rule4->dst_len = frh->dst_len; 219e1ef4bf2SThomas Graf rule4->dstmask = inet_make_mask(rule4->dst_len); 220e1ef4bf2SThomas Graf rule4->tos = frh->tos; 221e1ef4bf2SThomas Graf 222f4530fa5SDavid S. Miller net->ipv4.fib_has_custom_rules = true; 223104616e7SScott Feldman fib_flush_external(rule->fr_net); 224104616e7SScott Feldman 225e1ef4bf2SThomas Graf err = 0; 226e1ef4bf2SThomas Graf errout: 227e1ef4bf2SThomas Graf return err; 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds 2300ddcf43dSAlexander Duyck static int fib4_rule_delete(struct fib_rule *rule) 2317a9bc9b8SDavid S. Miller { 232f4530fa5SDavid S. Miller struct net *net = rule->fr_net; 2330ddcf43dSAlexander Duyck int err; 2347a9bc9b8SDavid S. Miller 2350ddcf43dSAlexander Duyck /* split local/main if they are not already split */ 2360ddcf43dSAlexander Duyck err = fib_unmerge(net); 2370ddcf43dSAlexander Duyck if (err) 2380ddcf43dSAlexander Duyck goto errout; 2390ddcf43dSAlexander Duyck 2400ddcf43dSAlexander Duyck #ifdef CONFIG_IP_ROUTE_CLASSID 2410ddcf43dSAlexander Duyck if (((struct fib4_rule *)rule)->tclassid) 242f4530fa5SDavid S. Miller net->ipv4.fib_num_tclassid_users--; 2437a9bc9b8SDavid S. Miller #endif 244f4530fa5SDavid S. Miller net->ipv4.fib_has_custom_rules = true; 245104616e7SScott Feldman fib_flush_external(rule->fr_net); 2460ddcf43dSAlexander Duyck errout: 2470ddcf43dSAlexander Duyck return err; 2487a9bc9b8SDavid S. Miller } 2497a9bc9b8SDavid S. Miller 250e1ef4bf2SThomas Graf static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, 251e1ef4bf2SThomas Graf struct nlattr **tb) 252a5cdc030SPatrick McHardy { 253e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 254a5cdc030SPatrick McHardy 255e1ef4bf2SThomas Graf if (frh->src_len && (rule4->src_len != frh->src_len)) 256e1ef4bf2SThomas Graf return 0; 257e1ef4bf2SThomas Graf 258e1ef4bf2SThomas Graf if (frh->dst_len && (rule4->dst_len != frh->dst_len)) 259e1ef4bf2SThomas Graf return 0; 260e1ef4bf2SThomas Graf 261e1ef4bf2SThomas Graf if (frh->tos && (rule4->tos != frh->tos)) 262e1ef4bf2SThomas Graf return 0; 263e1ef4bf2SThomas Graf 264c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 265e1ef4bf2SThomas Graf if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW]))) 266e1ef4bf2SThomas Graf return 0; 267e1ef4bf2SThomas Graf #endif 268e1ef4bf2SThomas Graf 26967b61f6cSJiri Benc if (frh->src_len && (rule4->src != nla_get_in_addr(tb[FRA_SRC]))) 270e1ef4bf2SThomas Graf return 0; 271e1ef4bf2SThomas Graf 27267b61f6cSJiri Benc if (frh->dst_len && (rule4->dst != nla_get_in_addr(tb[FRA_DST]))) 273e1ef4bf2SThomas Graf return 0; 274e1ef4bf2SThomas Graf 275e1ef4bf2SThomas Graf return 1; 276a5cdc030SPatrick McHardy } 277a5cdc030SPatrick McHardy 278e1ef4bf2SThomas Graf static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb, 27904af8cf6SRami Rosen struct fib_rule_hdr *frh) 2801da177e4SLinus Torvalds { 281e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 2821da177e4SLinus Torvalds 283e1ef4bf2SThomas Graf frh->dst_len = rule4->dst_len; 284e1ef4bf2SThomas Graf frh->src_len = rule4->src_len; 285e1ef4bf2SThomas Graf frh->tos = rule4->tos; 2861da177e4SLinus Torvalds 287f3756b79SDavid S. Miller if ((rule4->dst_len && 288930345eaSJiri Benc nla_put_in_addr(skb, FRA_DST, rule4->dst)) || 289f3756b79SDavid S. Miller (rule4->src_len && 290930345eaSJiri Benc nla_put_in_addr(skb, FRA_SRC, rule4->src))) 291f3756b79SDavid S. Miller goto nla_put_failure; 292c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 293f3756b79SDavid S. Miller if (rule4->tclassid && 294f3756b79SDavid S. Miller nla_put_u32(skb, FRA_FLOW, rule4->tclassid)) 295f3756b79SDavid S. Miller goto nla_put_failure; 296e1ef4bf2SThomas Graf #endif 297e1ef4bf2SThomas Graf return 0; 298e1ef4bf2SThomas Graf 299e1ef4bf2SThomas Graf nla_put_failure: 300e1ef4bf2SThomas Graf return -ENOBUFS; 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 303339bf98fSThomas Graf static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule) 304339bf98fSThomas Graf { 305339bf98fSThomas Graf return nla_total_size(4) /* dst */ 306339bf98fSThomas Graf + nla_total_size(4) /* src */ 307339bf98fSThomas Graf + nla_total_size(4); /* flow */ 308339bf98fSThomas Graf } 309339bf98fSThomas Graf 310ae299fc0SDenis V. Lunev static void fib4_rule_flush_cache(struct fib_rules_ops *ops) 31173417f61SThomas Graf { 312bafa6d9dSNicolas Dichtel rt_cache_flush(ops->fro_net); 31373417f61SThomas Graf } 31473417f61SThomas Graf 31504a6f82cSAndi Kleen static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = { 31625239ceeSPatrick McHardy .family = AF_INET, 317e1ef4bf2SThomas Graf .rule_size = sizeof(struct fib4_rule), 318e1701c68SThomas Graf .addr_size = sizeof(u32), 319e1ef4bf2SThomas Graf .action = fib4_rule_action, 3207764a45aSStefan Tomanek .suppress = fib4_rule_suppress, 321e1ef4bf2SThomas Graf .match = fib4_rule_match, 322e1ef4bf2SThomas Graf .configure = fib4_rule_configure, 3237a9bc9b8SDavid S. Miller .delete = fib4_rule_delete, 324e1ef4bf2SThomas Graf .compare = fib4_rule_compare, 325e1ef4bf2SThomas Graf .fill = fib4_rule_fill, 326339bf98fSThomas Graf .nlmsg_payload = fib4_rule_nlmsg_payload, 32773417f61SThomas Graf .flush_cache = fib4_rule_flush_cache, 328e1ef4bf2SThomas Graf .nlgroup = RTNLGRP_IPV4_RULE, 329e1ef4bf2SThomas Graf .policy = fib4_rule_policy, 330e1ef4bf2SThomas Graf .owner = THIS_MODULE, 331e1ef4bf2SThomas Graf }; 332e1ef4bf2SThomas Graf 333e4e4971cSDenis V. Lunev static int fib_default_rules_init(struct fib_rules_ops *ops) 3342994c638SDenis V. Lunev { 3352994c638SDenis V. Lunev int err; 3362994c638SDenis V. Lunev 3375adef180SPatrick McHardy err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0); 3382994c638SDenis V. Lunev if (err < 0) 3392994c638SDenis V. Lunev return err; 340e4e4971cSDenis V. Lunev err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0); 3412994c638SDenis V. Lunev if (err < 0) 3422994c638SDenis V. Lunev return err; 343e4e4971cSDenis V. Lunev err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0); 3442994c638SDenis V. Lunev if (err < 0) 3452994c638SDenis V. Lunev return err; 3462994c638SDenis V. Lunev return 0; 3472994c638SDenis V. Lunev } 3482994c638SDenis V. Lunev 3497b1a74fdSDenis V. Lunev int __net_init fib4_rules_init(struct net *net) 350e1ef4bf2SThomas Graf { 351dbb50165SDenis V. Lunev int err; 352e4e4971cSDenis V. Lunev struct fib_rules_ops *ops; 353dbb50165SDenis V. Lunev 354e9c5158aSEric W. Biederman ops = fib_rules_register(&fib4_rules_ops_template, net); 355e9c5158aSEric W. Biederman if (IS_ERR(ops)) 356e9c5158aSEric W. Biederman return PTR_ERR(ops); 357e4e4971cSDenis V. Lunev 358e4e4971cSDenis V. Lunev err = fib_default_rules_init(ops); 359dbb50165SDenis V. Lunev if (err < 0) 360dbb50165SDenis V. Lunev goto fail; 361e4e4971cSDenis V. Lunev net->ipv4.rules_ops = ops; 362f4530fa5SDavid S. Miller net->ipv4.fib_has_custom_rules = false; 363dbb50165SDenis V. Lunev return 0; 364dbb50165SDenis V. Lunev 365dbb50165SDenis V. Lunev fail: 366dbb50165SDenis V. Lunev /* also cleans all rules already added */ 3679e3a5487SDenis V. Lunev fib_rules_unregister(ops); 368dbb50165SDenis V. Lunev return err; 369e1ef4bf2SThomas Graf } 3707b1a74fdSDenis V. Lunev 3717b1a74fdSDenis V. Lunev void __net_exit fib4_rules_exit(struct net *net) 3727b1a74fdSDenis V. Lunev { 3739e3a5487SDenis V. Lunev fib_rules_unregister(net->ipv4.rules_ops); 3747b1a74fdSDenis V. Lunev } 375