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 50c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 51982721f3SDavid S. Miller u32 fib_rules_tclass(const struct fib_result *res) 521da177e4SLinus Torvalds { 53e1ef4bf2SThomas Graf return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0; 54e1ef4bf2SThomas Graf } 551da177e4SLinus Torvalds #endif 561da177e4SLinus Torvalds 5722bd5b9bSDavid S. Miller int fib_lookup(struct net *net, struct flowi4 *flp, struct fib_result *res) 58e1ef4bf2SThomas Graf { 59e1ef4bf2SThomas Graf struct fib_lookup_arg arg = { 60e1ef4bf2SThomas Graf .result = res, 61ebc0ffaeSEric Dumazet .flags = FIB_LOOKUP_NOREF, 62e1ef4bf2SThomas Graf }; 63e1ef4bf2SThomas Graf int err; 64e1ef4bf2SThomas Graf 6522bd5b9bSDavid S. Miller err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg); 66e1ef4bf2SThomas Graf res->r = arg.rule; 67e1ef4bf2SThomas Graf 681da177e4SLinus Torvalds return err; 691da177e4SLinus Torvalds } 706fc01438SFlorian Westphal EXPORT_SYMBOL_GPL(fib_lookup); 711da177e4SLinus Torvalds 728ce11e6aSAdrian Bunk static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp, 738ce11e6aSAdrian Bunk int flags, struct fib_lookup_arg *arg) 74e1ef4bf2SThomas Graf { 75e1ef4bf2SThomas Graf int err = -EAGAIN; 76e1ef4bf2SThomas Graf struct fib_table *tbl; 77e1ef4bf2SThomas Graf 78e1ef4bf2SThomas Graf switch (rule->action) { 79e1ef4bf2SThomas Graf case FR_ACT_TO_TBL: 80e1ef4bf2SThomas Graf break; 81e1ef4bf2SThomas Graf 82e1ef4bf2SThomas Graf case FR_ACT_UNREACHABLE: 83e1ef4bf2SThomas Graf err = -ENETUNREACH; 84e1ef4bf2SThomas Graf goto errout; 85e1ef4bf2SThomas Graf 86e1ef4bf2SThomas Graf case FR_ACT_PROHIBIT: 87e1ef4bf2SThomas Graf err = -EACCES; 88e1ef4bf2SThomas Graf goto errout; 89e1ef4bf2SThomas Graf 90e1ef4bf2SThomas Graf case FR_ACT_BLACKHOLE: 91e1ef4bf2SThomas Graf default: 92e1ef4bf2SThomas Graf err = -EINVAL; 93e1ef4bf2SThomas Graf goto errout; 94e1ef4bf2SThomas Graf } 95e1ef4bf2SThomas Graf 966a31d2a9SEric Dumazet tbl = fib_get_table(rule->fr_net, rule->table); 976a31d2a9SEric Dumazet if (!tbl) 98e1ef4bf2SThomas Graf goto errout; 99e1ef4bf2SThomas Graf 10022bd5b9bSDavid S. Miller err = fib_table_lookup(tbl, &flp->u.ip4, (struct fib_result *) arg->result, arg->flags); 101e1ef4bf2SThomas Graf if (err > 0) 102e1ef4bf2SThomas Graf err = -EAGAIN; 103e1ef4bf2SThomas Graf errout: 104e1ef4bf2SThomas Graf return err; 105e1ef4bf2SThomas Graf } 106e1ef4bf2SThomas Graf 107e1ef4bf2SThomas Graf 108e1ef4bf2SThomas Graf static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) 109e1ef4bf2SThomas Graf { 110e1ef4bf2SThomas Graf struct fib4_rule *r = (struct fib4_rule *) rule; 1119ade2286SDavid S. Miller struct flowi4 *fl4 = &fl->u.ip4; 1129ade2286SDavid S. Miller __be32 daddr = fl4->daddr; 1139ade2286SDavid S. Miller __be32 saddr = fl4->saddr; 114e1ef4bf2SThomas Graf 115e1ef4bf2SThomas Graf if (((saddr ^ r->src) & r->srcmask) || 116e1ef4bf2SThomas Graf ((daddr ^ r->dst) & r->dstmask)) 117e1ef4bf2SThomas Graf return 0; 118e1ef4bf2SThomas Graf 1199ade2286SDavid S. Miller if (r->tos && (r->tos != fl4->flowi4_tos)) 120e1ef4bf2SThomas Graf return 0; 121e1ef4bf2SThomas Graf 122e1ef4bf2SThomas Graf return 1; 123e1ef4bf2SThomas Graf } 1241da177e4SLinus Torvalds 1258ad4942cSDenis V. Lunev static struct fib_table *fib_empty_table(struct net *net) 1261da177e4SLinus Torvalds { 1272dfe55b4SPatrick McHardy u32 id; 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds for (id = 1; id <= RT_TABLE_MAX; id++) 1308ad4942cSDenis V. Lunev if (fib_get_table(net, id) == NULL) 1318ad4942cSDenis V. Lunev return fib_new_table(net, id); 1321da177e4SLinus Torvalds return NULL; 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 135ef7c79edSPatrick McHardy static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = { 1361f6c9557SThomas Graf FRA_GENERIC_POLICY, 137e1ef4bf2SThomas Graf [FRA_FLOW] = { .type = NLA_U32 }, 1381da177e4SLinus Torvalds }; 1391da177e4SLinus Torvalds 140e1ef4bf2SThomas Graf static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb, 1418b3521eeSRami Rosen struct fib_rule_hdr *frh, 142e1ef4bf2SThomas Graf struct nlattr **tb) 1431da177e4SLinus Torvalds { 1443b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 145e1ef4bf2SThomas Graf int err = -EINVAL; 146e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 1471da177e4SLinus Torvalds 148e1701c68SThomas Graf if (frh->tos & ~IPTOS_TOS_MASK) 149e1ef4bf2SThomas Graf goto errout; 150e1ef4bf2SThomas Graf 151e1ef4bf2SThomas Graf if (rule->table == RT_TABLE_UNSPEC) { 152e1ef4bf2SThomas Graf if (rule->action == FR_ACT_TO_TBL) { 153e1ef4bf2SThomas Graf struct fib_table *table; 154e1ef4bf2SThomas Graf 155e4e4971cSDenis V. Lunev table = fib_empty_table(net); 156e1ef4bf2SThomas Graf if (table == NULL) { 157e1ef4bf2SThomas Graf err = -ENOBUFS; 158e1ef4bf2SThomas Graf goto errout; 159e1ef4bf2SThomas Graf } 160e1ef4bf2SThomas Graf 161e1ef4bf2SThomas Graf rule->table = table->tb_id; 162e1ef4bf2SThomas Graf } 163e1ef4bf2SThomas Graf } 164e1ef4bf2SThomas Graf 165e1701c68SThomas Graf if (frh->src_len) 16645d60b9eSAl Viro rule4->src = nla_get_be32(tb[FRA_SRC]); 167e1ef4bf2SThomas Graf 168e1701c68SThomas Graf if (frh->dst_len) 16945d60b9eSAl Viro rule4->dst = nla_get_be32(tb[FRA_DST]); 170e1ef4bf2SThomas Graf 171c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 172*7a9bc9b8SDavid S. Miller if (tb[FRA_FLOW]) { 173e1ef4bf2SThomas Graf rule4->tclassid = nla_get_u32(tb[FRA_FLOW]); 174*7a9bc9b8SDavid S. Miller if (rule4->tclassid) 175*7a9bc9b8SDavid S. Miller fib_num_tclassid_users++; 176*7a9bc9b8SDavid S. Miller } 1771da177e4SLinus Torvalds #endif 1781da177e4SLinus Torvalds 179e1ef4bf2SThomas Graf rule4->src_len = frh->src_len; 180e1ef4bf2SThomas Graf rule4->srcmask = inet_make_mask(rule4->src_len); 181e1ef4bf2SThomas Graf rule4->dst_len = frh->dst_len; 182e1ef4bf2SThomas Graf rule4->dstmask = inet_make_mask(rule4->dst_len); 183e1ef4bf2SThomas Graf rule4->tos = frh->tos; 184e1ef4bf2SThomas Graf 185e1ef4bf2SThomas Graf err = 0; 186e1ef4bf2SThomas Graf errout: 187e1ef4bf2SThomas Graf return err; 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds 190*7a9bc9b8SDavid S. Miller static void fib4_rule_delete(struct fib_rule *rule) 191*7a9bc9b8SDavid S. Miller { 192*7a9bc9b8SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID 193*7a9bc9b8SDavid S. Miller struct fib4_rule *rule4 = (struct fib4_rule *) rule; 194*7a9bc9b8SDavid S. Miller 195*7a9bc9b8SDavid S. Miller if (rule4->tclassid) 196*7a9bc9b8SDavid S. Miller fib_num_tclassid_users--; 197*7a9bc9b8SDavid S. Miller #endif 198*7a9bc9b8SDavid S. Miller } 199*7a9bc9b8SDavid S. Miller 200e1ef4bf2SThomas Graf static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, 201e1ef4bf2SThomas Graf struct nlattr **tb) 202a5cdc030SPatrick McHardy { 203e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 204a5cdc030SPatrick McHardy 205e1ef4bf2SThomas Graf if (frh->src_len && (rule4->src_len != frh->src_len)) 206e1ef4bf2SThomas Graf return 0; 207e1ef4bf2SThomas Graf 208e1ef4bf2SThomas Graf if (frh->dst_len && (rule4->dst_len != frh->dst_len)) 209e1ef4bf2SThomas Graf return 0; 210e1ef4bf2SThomas Graf 211e1ef4bf2SThomas Graf if (frh->tos && (rule4->tos != frh->tos)) 212e1ef4bf2SThomas Graf return 0; 213e1ef4bf2SThomas Graf 214c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 215e1ef4bf2SThomas Graf if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW]))) 216e1ef4bf2SThomas Graf return 0; 217e1ef4bf2SThomas Graf #endif 218e1ef4bf2SThomas Graf 219e1701c68SThomas Graf if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC]))) 220e1ef4bf2SThomas Graf return 0; 221e1ef4bf2SThomas Graf 222e1701c68SThomas Graf if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST]))) 223e1ef4bf2SThomas Graf return 0; 224e1ef4bf2SThomas Graf 225e1ef4bf2SThomas Graf return 1; 226a5cdc030SPatrick McHardy } 227a5cdc030SPatrick McHardy 228e1ef4bf2SThomas Graf static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb, 22904af8cf6SRami Rosen struct fib_rule_hdr *frh) 2301da177e4SLinus Torvalds { 231e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 2321da177e4SLinus Torvalds 233e1ef4bf2SThomas Graf frh->dst_len = rule4->dst_len; 234e1ef4bf2SThomas Graf frh->src_len = rule4->src_len; 235e1ef4bf2SThomas Graf frh->tos = rule4->tos; 2361da177e4SLinus Torvalds 237f3756b79SDavid S. Miller if ((rule4->dst_len && 238f3756b79SDavid S. Miller nla_put_be32(skb, FRA_DST, rule4->dst)) || 239f3756b79SDavid S. Miller (rule4->src_len && 240f3756b79SDavid S. Miller nla_put_be32(skb, FRA_SRC, rule4->src))) 241f3756b79SDavid S. Miller goto nla_put_failure; 242c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 243f3756b79SDavid S. Miller if (rule4->tclassid && 244f3756b79SDavid S. Miller nla_put_u32(skb, FRA_FLOW, rule4->tclassid)) 245f3756b79SDavid S. Miller goto nla_put_failure; 246e1ef4bf2SThomas Graf #endif 247e1ef4bf2SThomas Graf return 0; 248e1ef4bf2SThomas Graf 249e1ef4bf2SThomas Graf nla_put_failure: 250e1ef4bf2SThomas Graf return -ENOBUFS; 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds 253339bf98fSThomas Graf static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule) 254339bf98fSThomas Graf { 255339bf98fSThomas Graf return nla_total_size(4) /* dst */ 256339bf98fSThomas Graf + nla_total_size(4) /* src */ 257339bf98fSThomas Graf + nla_total_size(4); /* flow */ 258339bf98fSThomas Graf } 259339bf98fSThomas Graf 260ae299fc0SDenis V. Lunev static void fib4_rule_flush_cache(struct fib_rules_ops *ops) 26173417f61SThomas Graf { 262ae299fc0SDenis V. Lunev rt_cache_flush(ops->fro_net, -1); 26373417f61SThomas Graf } 26473417f61SThomas Graf 2653d0c9c4eSPatrick McHardy static const struct fib_rules_ops __net_initdata fib4_rules_ops_template = { 26625239ceeSPatrick McHardy .family = AF_INET, 267e1ef4bf2SThomas Graf .rule_size = sizeof(struct fib4_rule), 268e1701c68SThomas Graf .addr_size = sizeof(u32), 269e1ef4bf2SThomas Graf .action = fib4_rule_action, 270e1ef4bf2SThomas Graf .match = fib4_rule_match, 271e1ef4bf2SThomas Graf .configure = fib4_rule_configure, 272*7a9bc9b8SDavid S. Miller .delete = fib4_rule_delete, 273e1ef4bf2SThomas Graf .compare = fib4_rule_compare, 274e1ef4bf2SThomas Graf .fill = fib4_rule_fill, 275d8a566beSPatrick McHardy .default_pref = fib_default_rule_pref, 276339bf98fSThomas Graf .nlmsg_payload = fib4_rule_nlmsg_payload, 27773417f61SThomas Graf .flush_cache = fib4_rule_flush_cache, 278e1ef4bf2SThomas Graf .nlgroup = RTNLGRP_IPV4_RULE, 279e1ef4bf2SThomas Graf .policy = fib4_rule_policy, 280e1ef4bf2SThomas Graf .owner = THIS_MODULE, 281e1ef4bf2SThomas Graf }; 282e1ef4bf2SThomas Graf 283e4e4971cSDenis V. Lunev static int fib_default_rules_init(struct fib_rules_ops *ops) 2842994c638SDenis V. Lunev { 2852994c638SDenis V. Lunev int err; 2862994c638SDenis V. Lunev 2875adef180SPatrick McHardy err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0); 2882994c638SDenis V. Lunev if (err < 0) 2892994c638SDenis V. Lunev return err; 290e4e4971cSDenis V. Lunev err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0); 2912994c638SDenis V. Lunev if (err < 0) 2922994c638SDenis V. Lunev return err; 293e4e4971cSDenis V. Lunev err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0); 2942994c638SDenis V. Lunev if (err < 0) 2952994c638SDenis V. Lunev return err; 2962994c638SDenis V. Lunev return 0; 2972994c638SDenis V. Lunev } 2982994c638SDenis V. Lunev 2997b1a74fdSDenis V. Lunev int __net_init fib4_rules_init(struct net *net) 300e1ef4bf2SThomas Graf { 301dbb50165SDenis V. Lunev int err; 302e4e4971cSDenis V. Lunev struct fib_rules_ops *ops; 303dbb50165SDenis V. Lunev 304e9c5158aSEric W. Biederman ops = fib_rules_register(&fib4_rules_ops_template, net); 305e9c5158aSEric W. Biederman if (IS_ERR(ops)) 306e9c5158aSEric W. Biederman return PTR_ERR(ops); 307e4e4971cSDenis V. Lunev 308e4e4971cSDenis V. Lunev err = fib_default_rules_init(ops); 309dbb50165SDenis V. Lunev if (err < 0) 310dbb50165SDenis V. Lunev goto fail; 311e4e4971cSDenis V. Lunev net->ipv4.rules_ops = ops; 312dbb50165SDenis V. Lunev return 0; 313dbb50165SDenis V. Lunev 314dbb50165SDenis V. Lunev fail: 315dbb50165SDenis V. Lunev /* also cleans all rules already added */ 3169e3a5487SDenis V. Lunev fib_rules_unregister(ops); 317dbb50165SDenis V. Lunev return err; 318e1ef4bf2SThomas Graf } 3197b1a74fdSDenis V. Lunev 3207b1a74fdSDenis V. Lunev void __net_exit fib4_rules_exit(struct net *net) 3217b1a74fdSDenis V. Lunev { 3229e3a5487SDenis V. Lunev fib_rules_unregister(net->ipv4.rules_ops); 3237b1a74fdSDenis V. Lunev } 324