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 651da177e4SLinus Torvalds return err; 661da177e4SLinus Torvalds } 67f4530fa5SDavid S. Miller EXPORT_SYMBOL_GPL(__fib_lookup); 681da177e4SLinus Torvalds 698ce11e6aSAdrian Bunk static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp, 708ce11e6aSAdrian Bunk int flags, struct fib_lookup_arg *arg) 71e1ef4bf2SThomas Graf { 72e1ef4bf2SThomas Graf int err = -EAGAIN; 73e1ef4bf2SThomas Graf struct fib_table *tbl; 74e1ef4bf2SThomas Graf 75e1ef4bf2SThomas Graf switch (rule->action) { 76e1ef4bf2SThomas Graf case FR_ACT_TO_TBL: 77e1ef4bf2SThomas Graf break; 78e1ef4bf2SThomas Graf 79e1ef4bf2SThomas Graf case FR_ACT_UNREACHABLE: 80e1ef4bf2SThomas Graf err = -ENETUNREACH; 81e1ef4bf2SThomas Graf goto errout; 82e1ef4bf2SThomas Graf 83e1ef4bf2SThomas Graf case FR_ACT_PROHIBIT: 84e1ef4bf2SThomas Graf err = -EACCES; 85e1ef4bf2SThomas Graf goto errout; 86e1ef4bf2SThomas Graf 87e1ef4bf2SThomas Graf case FR_ACT_BLACKHOLE: 88e1ef4bf2SThomas Graf default: 89e1ef4bf2SThomas Graf err = -EINVAL; 90e1ef4bf2SThomas Graf goto errout; 91e1ef4bf2SThomas Graf } 92e1ef4bf2SThomas Graf 936a31d2a9SEric Dumazet tbl = fib_get_table(rule->fr_net, rule->table); 946a31d2a9SEric Dumazet if (!tbl) 95e1ef4bf2SThomas Graf goto errout; 96e1ef4bf2SThomas Graf 9722bd5b9bSDavid S. Miller err = fib_table_lookup(tbl, &flp->u.ip4, (struct fib_result *) arg->result, arg->flags); 98e1ef4bf2SThomas Graf if (err > 0) 99e1ef4bf2SThomas Graf err = -EAGAIN; 100e1ef4bf2SThomas Graf errout: 101e1ef4bf2SThomas Graf return err; 102e1ef4bf2SThomas Graf } 103e1ef4bf2SThomas Graf 1047764a45aSStefan Tomanek static bool fib4_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg) 1057764a45aSStefan Tomanek { 1066ef94cfaSStefan Tomanek struct fib_result *result = (struct fib_result *) arg->result; 107*673498b8SStefan Tomanek struct net_device *dev = NULL; 108*673498b8SStefan Tomanek 109*673498b8SStefan Tomanek if (result->fi) 110*673498b8SStefan Tomanek dev = result->fi->fib_dev; 1116ef94cfaSStefan Tomanek 1127764a45aSStefan Tomanek /* do not accept result if the route does 1137764a45aSStefan Tomanek * not meet the required prefix length 1147764a45aSStefan Tomanek */ 11573f5698eSStefan Tomanek if (result->prefixlen <= rule->suppress_prefixlen) 1166ef94cfaSStefan Tomanek goto suppress_route; 1176ef94cfaSStefan Tomanek 1186ef94cfaSStefan Tomanek /* do not accept result if the route uses a device 1196ef94cfaSStefan Tomanek * belonging to a forbidden interface group 1206ef94cfaSStefan Tomanek */ 1216ef94cfaSStefan Tomanek if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup) 1226ef94cfaSStefan Tomanek goto suppress_route; 1236ef94cfaSStefan Tomanek 1246ef94cfaSStefan Tomanek return false; 1256ef94cfaSStefan Tomanek 1266ef94cfaSStefan Tomanek suppress_route: 1277764a45aSStefan Tomanek if (!(arg->flags & FIB_LOOKUP_NOREF)) 1287764a45aSStefan Tomanek fib_info_put(result->fi); 1297764a45aSStefan Tomanek return true; 1307764a45aSStefan Tomanek } 131e1ef4bf2SThomas Graf 132e1ef4bf2SThomas Graf static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) 133e1ef4bf2SThomas Graf { 134e1ef4bf2SThomas Graf struct fib4_rule *r = (struct fib4_rule *) rule; 1359ade2286SDavid S. Miller struct flowi4 *fl4 = &fl->u.ip4; 1369ade2286SDavid S. Miller __be32 daddr = fl4->daddr; 1379ade2286SDavid S. Miller __be32 saddr = fl4->saddr; 138e1ef4bf2SThomas Graf 139e1ef4bf2SThomas Graf if (((saddr ^ r->src) & r->srcmask) || 140e1ef4bf2SThomas Graf ((daddr ^ r->dst) & r->dstmask)) 141e1ef4bf2SThomas Graf return 0; 142e1ef4bf2SThomas Graf 1439ade2286SDavid S. Miller if (r->tos && (r->tos != fl4->flowi4_tos)) 144e1ef4bf2SThomas Graf return 0; 145e1ef4bf2SThomas Graf 146e1ef4bf2SThomas Graf return 1; 147e1ef4bf2SThomas Graf } 1481da177e4SLinus Torvalds 1498ad4942cSDenis V. Lunev static struct fib_table *fib_empty_table(struct net *net) 1501da177e4SLinus Torvalds { 1512dfe55b4SPatrick McHardy u32 id; 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds for (id = 1; id <= RT_TABLE_MAX; id++) 1548ad4942cSDenis V. Lunev if (fib_get_table(net, id) == NULL) 1558ad4942cSDenis V. Lunev return fib_new_table(net, id); 1561da177e4SLinus Torvalds return NULL; 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds 159ef7c79edSPatrick McHardy static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = { 1601f6c9557SThomas Graf FRA_GENERIC_POLICY, 161e1ef4bf2SThomas Graf [FRA_FLOW] = { .type = NLA_U32 }, 1621da177e4SLinus Torvalds }; 1631da177e4SLinus Torvalds 164e1ef4bf2SThomas Graf static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb, 1658b3521eeSRami Rosen struct fib_rule_hdr *frh, 166e1ef4bf2SThomas Graf struct nlattr **tb) 1671da177e4SLinus Torvalds { 1683b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 169e1ef4bf2SThomas Graf int err = -EINVAL; 170e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 1711da177e4SLinus Torvalds 172e1701c68SThomas Graf if (frh->tos & ~IPTOS_TOS_MASK) 173e1ef4bf2SThomas Graf goto errout; 174e1ef4bf2SThomas Graf 175e1ef4bf2SThomas Graf if (rule->table == RT_TABLE_UNSPEC) { 176e1ef4bf2SThomas Graf if (rule->action == FR_ACT_TO_TBL) { 177e1ef4bf2SThomas Graf struct fib_table *table; 178e1ef4bf2SThomas Graf 179e4e4971cSDenis V. Lunev table = fib_empty_table(net); 180e1ef4bf2SThomas Graf if (table == NULL) { 181e1ef4bf2SThomas Graf err = -ENOBUFS; 182e1ef4bf2SThomas Graf goto errout; 183e1ef4bf2SThomas Graf } 184e1ef4bf2SThomas Graf 185e1ef4bf2SThomas Graf rule->table = table->tb_id; 186e1ef4bf2SThomas Graf } 187e1ef4bf2SThomas Graf } 188e1ef4bf2SThomas Graf 189e1701c68SThomas Graf if (frh->src_len) 19045d60b9eSAl Viro rule4->src = nla_get_be32(tb[FRA_SRC]); 191e1ef4bf2SThomas Graf 192e1701c68SThomas Graf if (frh->dst_len) 19345d60b9eSAl Viro rule4->dst = nla_get_be32(tb[FRA_DST]); 194e1ef4bf2SThomas Graf 195c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 1967a9bc9b8SDavid S. Miller if (tb[FRA_FLOW]) { 197e1ef4bf2SThomas Graf rule4->tclassid = nla_get_u32(tb[FRA_FLOW]); 1987a9bc9b8SDavid S. Miller if (rule4->tclassid) 199f4530fa5SDavid S. Miller net->ipv4.fib_num_tclassid_users++; 2007a9bc9b8SDavid S. Miller } 2011da177e4SLinus Torvalds #endif 2021da177e4SLinus Torvalds 203e1ef4bf2SThomas Graf rule4->src_len = frh->src_len; 204e1ef4bf2SThomas Graf rule4->srcmask = inet_make_mask(rule4->src_len); 205e1ef4bf2SThomas Graf rule4->dst_len = frh->dst_len; 206e1ef4bf2SThomas Graf rule4->dstmask = inet_make_mask(rule4->dst_len); 207e1ef4bf2SThomas Graf rule4->tos = frh->tos; 208e1ef4bf2SThomas Graf 209f4530fa5SDavid S. Miller net->ipv4.fib_has_custom_rules = true; 210e1ef4bf2SThomas Graf err = 0; 211e1ef4bf2SThomas Graf errout: 212e1ef4bf2SThomas Graf return err; 2131da177e4SLinus Torvalds } 2141da177e4SLinus Torvalds 2157a9bc9b8SDavid S. Miller static void fib4_rule_delete(struct fib_rule *rule) 2167a9bc9b8SDavid S. Miller { 217f4530fa5SDavid S. Miller struct net *net = rule->fr_net; 2187a9bc9b8SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID 2197a9bc9b8SDavid S. Miller struct fib4_rule *rule4 = (struct fib4_rule *) rule; 2207a9bc9b8SDavid S. Miller 2217a9bc9b8SDavid S. Miller if (rule4->tclassid) 222f4530fa5SDavid S. Miller net->ipv4.fib_num_tclassid_users--; 2237a9bc9b8SDavid S. Miller #endif 224f4530fa5SDavid S. Miller net->ipv4.fib_has_custom_rules = true; 2257a9bc9b8SDavid S. Miller } 2267a9bc9b8SDavid S. Miller 227e1ef4bf2SThomas Graf static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, 228e1ef4bf2SThomas Graf struct nlattr **tb) 229a5cdc030SPatrick McHardy { 230e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 231a5cdc030SPatrick McHardy 232e1ef4bf2SThomas Graf if (frh->src_len && (rule4->src_len != frh->src_len)) 233e1ef4bf2SThomas Graf return 0; 234e1ef4bf2SThomas Graf 235e1ef4bf2SThomas Graf if (frh->dst_len && (rule4->dst_len != frh->dst_len)) 236e1ef4bf2SThomas Graf return 0; 237e1ef4bf2SThomas Graf 238e1ef4bf2SThomas Graf if (frh->tos && (rule4->tos != frh->tos)) 239e1ef4bf2SThomas Graf return 0; 240e1ef4bf2SThomas Graf 241c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 242e1ef4bf2SThomas Graf if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW]))) 243e1ef4bf2SThomas Graf return 0; 244e1ef4bf2SThomas Graf #endif 245e1ef4bf2SThomas Graf 246e1701c68SThomas Graf if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC]))) 247e1ef4bf2SThomas Graf return 0; 248e1ef4bf2SThomas Graf 249e1701c68SThomas Graf if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST]))) 250e1ef4bf2SThomas Graf return 0; 251e1ef4bf2SThomas Graf 252e1ef4bf2SThomas Graf return 1; 253a5cdc030SPatrick McHardy } 254a5cdc030SPatrick McHardy 255e1ef4bf2SThomas Graf static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb, 25604af8cf6SRami Rosen struct fib_rule_hdr *frh) 2571da177e4SLinus Torvalds { 258e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 2591da177e4SLinus Torvalds 260e1ef4bf2SThomas Graf frh->dst_len = rule4->dst_len; 261e1ef4bf2SThomas Graf frh->src_len = rule4->src_len; 262e1ef4bf2SThomas Graf frh->tos = rule4->tos; 2631da177e4SLinus Torvalds 264f3756b79SDavid S. Miller if ((rule4->dst_len && 265f3756b79SDavid S. Miller nla_put_be32(skb, FRA_DST, rule4->dst)) || 266f3756b79SDavid S. Miller (rule4->src_len && 267f3756b79SDavid S. Miller nla_put_be32(skb, FRA_SRC, rule4->src))) 268f3756b79SDavid S. Miller goto nla_put_failure; 269c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 270f3756b79SDavid S. Miller if (rule4->tclassid && 271f3756b79SDavid S. Miller nla_put_u32(skb, FRA_FLOW, rule4->tclassid)) 272f3756b79SDavid S. Miller goto nla_put_failure; 273e1ef4bf2SThomas Graf #endif 274e1ef4bf2SThomas Graf return 0; 275e1ef4bf2SThomas Graf 276e1ef4bf2SThomas Graf nla_put_failure: 277e1ef4bf2SThomas Graf return -ENOBUFS; 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds 280339bf98fSThomas Graf static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule) 281339bf98fSThomas Graf { 282339bf98fSThomas Graf return nla_total_size(4) /* dst */ 283339bf98fSThomas Graf + nla_total_size(4) /* src */ 284339bf98fSThomas Graf + nla_total_size(4); /* flow */ 285339bf98fSThomas Graf } 286339bf98fSThomas Graf 287ae299fc0SDenis V. Lunev static void fib4_rule_flush_cache(struct fib_rules_ops *ops) 28873417f61SThomas Graf { 289bafa6d9dSNicolas Dichtel rt_cache_flush(ops->fro_net); 29073417f61SThomas Graf } 29173417f61SThomas Graf 29204a6f82cSAndi Kleen static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = { 29325239ceeSPatrick McHardy .family = AF_INET, 294e1ef4bf2SThomas Graf .rule_size = sizeof(struct fib4_rule), 295e1701c68SThomas Graf .addr_size = sizeof(u32), 296e1ef4bf2SThomas Graf .action = fib4_rule_action, 2977764a45aSStefan Tomanek .suppress = fib4_rule_suppress, 298e1ef4bf2SThomas Graf .match = fib4_rule_match, 299e1ef4bf2SThomas Graf .configure = fib4_rule_configure, 3007a9bc9b8SDavid S. Miller .delete = fib4_rule_delete, 301e1ef4bf2SThomas Graf .compare = fib4_rule_compare, 302e1ef4bf2SThomas Graf .fill = fib4_rule_fill, 303d8a566beSPatrick McHardy .default_pref = fib_default_rule_pref, 304339bf98fSThomas Graf .nlmsg_payload = fib4_rule_nlmsg_payload, 30573417f61SThomas Graf .flush_cache = fib4_rule_flush_cache, 306e1ef4bf2SThomas Graf .nlgroup = RTNLGRP_IPV4_RULE, 307e1ef4bf2SThomas Graf .policy = fib4_rule_policy, 308e1ef4bf2SThomas Graf .owner = THIS_MODULE, 309e1ef4bf2SThomas Graf }; 310e1ef4bf2SThomas Graf 311e4e4971cSDenis V. Lunev static int fib_default_rules_init(struct fib_rules_ops *ops) 3122994c638SDenis V. Lunev { 3132994c638SDenis V. Lunev int err; 3142994c638SDenis V. Lunev 3155adef180SPatrick McHardy err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0); 3162994c638SDenis V. Lunev if (err < 0) 3172994c638SDenis V. Lunev return err; 318e4e4971cSDenis V. Lunev err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0); 3192994c638SDenis V. Lunev if (err < 0) 3202994c638SDenis V. Lunev return err; 321e4e4971cSDenis V. Lunev err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0); 3222994c638SDenis V. Lunev if (err < 0) 3232994c638SDenis V. Lunev return err; 3242994c638SDenis V. Lunev return 0; 3252994c638SDenis V. Lunev } 3262994c638SDenis V. Lunev 3277b1a74fdSDenis V. Lunev int __net_init fib4_rules_init(struct net *net) 328e1ef4bf2SThomas Graf { 329dbb50165SDenis V. Lunev int err; 330e4e4971cSDenis V. Lunev struct fib_rules_ops *ops; 331dbb50165SDenis V. Lunev 332e9c5158aSEric W. Biederman ops = fib_rules_register(&fib4_rules_ops_template, net); 333e9c5158aSEric W. Biederman if (IS_ERR(ops)) 334e9c5158aSEric W. Biederman return PTR_ERR(ops); 335e4e4971cSDenis V. Lunev 336e4e4971cSDenis V. Lunev err = fib_default_rules_init(ops); 337dbb50165SDenis V. Lunev if (err < 0) 338dbb50165SDenis V. Lunev goto fail; 339e4e4971cSDenis V. Lunev net->ipv4.rules_ops = ops; 340f4530fa5SDavid S. Miller net->ipv4.fib_has_custom_rules = false; 341dbb50165SDenis V. Lunev return 0; 342dbb50165SDenis V. Lunev 343dbb50165SDenis V. Lunev fail: 344dbb50165SDenis V. Lunev /* also cleans all rules already added */ 3459e3a5487SDenis V. Lunev fib_rules_unregister(ops); 346dbb50165SDenis V. Lunev return err; 347e1ef4bf2SThomas Graf } 3487b1a74fdSDenis V. Lunev 3497b1a74fdSDenis V. Lunev void __net_exit fib4_rules_exit(struct net *net) 3507b1a74fdSDenis V. Lunev { 3519e3a5487SDenis V. Lunev fib_rules_unregister(net->ipv4.rules_ops); 3527b1a74fdSDenis V. Lunev } 353