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> 291da177e4SLinus Torvalds #include <net/ip.h> 301da177e4SLinus Torvalds #include <net/route.h> 311da177e4SLinus Torvalds #include <net/tcp.h> 321da177e4SLinus Torvalds #include <net/ip_fib.h> 33e1ef4bf2SThomas Graf #include <net/fib_rules.h> 341da177e4SLinus Torvalds 356a31d2a9SEric Dumazet struct fib4_rule { 36e1ef4bf2SThomas Graf struct fib_rule common; 37e1ef4bf2SThomas Graf u8 dst_len; 38e1ef4bf2SThomas Graf u8 src_len; 39e1ef4bf2SThomas Graf u8 tos; 4081f7bf6cSAl Viro __be32 src; 4181f7bf6cSAl Viro __be32 srcmask; 4281f7bf6cSAl Viro __be32 dst; 4381f7bf6cSAl Viro __be32 dstmask; 44c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 45e1ef4bf2SThomas Graf u32 tclassid; 461da177e4SLinus Torvalds #endif 471da177e4SLinus Torvalds }; 481da177e4SLinus Torvalds 49c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 50982721f3SDavid S. Miller u32 fib_rules_tclass(const struct fib_result *res) 511da177e4SLinus Torvalds { 52e1ef4bf2SThomas Graf return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0; 53e1ef4bf2SThomas Graf } 541da177e4SLinus Torvalds #endif 551da177e4SLinus Torvalds 5622bd5b9bSDavid S. Miller int fib_lookup(struct net *net, struct flowi4 *flp, struct fib_result *res) 57e1ef4bf2SThomas Graf { 58e1ef4bf2SThomas Graf struct fib_lookup_arg arg = { 59e1ef4bf2SThomas Graf .result = res, 60ebc0ffaeSEric Dumazet .flags = FIB_LOOKUP_NOREF, 61e1ef4bf2SThomas Graf }; 62e1ef4bf2SThomas Graf int err; 63e1ef4bf2SThomas Graf 6422bd5b9bSDavid S. Miller err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg); 65e1ef4bf2SThomas Graf res->r = arg.rule; 66e1ef4bf2SThomas Graf 671da177e4SLinus Torvalds return err; 681da177e4SLinus Torvalds } 691da177e4SLinus Torvalds 708ce11e6aSAdrian Bunk static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp, 718ce11e6aSAdrian Bunk int flags, struct fib_lookup_arg *arg) 72e1ef4bf2SThomas Graf { 73e1ef4bf2SThomas Graf int err = -EAGAIN; 74e1ef4bf2SThomas Graf struct fib_table *tbl; 75e1ef4bf2SThomas Graf 76e1ef4bf2SThomas Graf switch (rule->action) { 77e1ef4bf2SThomas Graf case FR_ACT_TO_TBL: 78e1ef4bf2SThomas Graf break; 79e1ef4bf2SThomas Graf 80e1ef4bf2SThomas Graf case FR_ACT_UNREACHABLE: 81e1ef4bf2SThomas Graf err = -ENETUNREACH; 82e1ef4bf2SThomas Graf goto errout; 83e1ef4bf2SThomas Graf 84e1ef4bf2SThomas Graf case FR_ACT_PROHIBIT: 85e1ef4bf2SThomas Graf err = -EACCES; 86e1ef4bf2SThomas Graf goto errout; 87e1ef4bf2SThomas Graf 88e1ef4bf2SThomas Graf case FR_ACT_BLACKHOLE: 89e1ef4bf2SThomas Graf default: 90e1ef4bf2SThomas Graf err = -EINVAL; 91e1ef4bf2SThomas Graf goto errout; 92e1ef4bf2SThomas Graf } 93e1ef4bf2SThomas Graf 946a31d2a9SEric Dumazet tbl = fib_get_table(rule->fr_net, rule->table); 956a31d2a9SEric Dumazet if (!tbl) 96e1ef4bf2SThomas Graf goto errout; 97e1ef4bf2SThomas Graf 9822bd5b9bSDavid S. Miller err = fib_table_lookup(tbl, &flp->u.ip4, (struct fib_result *) arg->result, arg->flags); 99e1ef4bf2SThomas Graf if (err > 0) 100e1ef4bf2SThomas Graf err = -EAGAIN; 101e1ef4bf2SThomas Graf errout: 102e1ef4bf2SThomas Graf return err; 103e1ef4bf2SThomas Graf } 104e1ef4bf2SThomas Graf 105e1ef4bf2SThomas Graf 106e1ef4bf2SThomas Graf static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) 107e1ef4bf2SThomas Graf { 108e1ef4bf2SThomas Graf struct fib4_rule *r = (struct fib4_rule *) rule; 109*9ade2286SDavid S. Miller struct flowi4 *fl4 = &fl->u.ip4; 110*9ade2286SDavid S. Miller __be32 daddr = fl4->daddr; 111*9ade2286SDavid S. Miller __be32 saddr = fl4->saddr; 112e1ef4bf2SThomas Graf 113e1ef4bf2SThomas Graf if (((saddr ^ r->src) & r->srcmask) || 114e1ef4bf2SThomas Graf ((daddr ^ r->dst) & r->dstmask)) 115e1ef4bf2SThomas Graf return 0; 116e1ef4bf2SThomas Graf 117*9ade2286SDavid S. Miller if (r->tos && (r->tos != fl4->flowi4_tos)) 118e1ef4bf2SThomas Graf return 0; 119e1ef4bf2SThomas Graf 120e1ef4bf2SThomas Graf return 1; 121e1ef4bf2SThomas Graf } 1221da177e4SLinus Torvalds 1238ad4942cSDenis V. Lunev static struct fib_table *fib_empty_table(struct net *net) 1241da177e4SLinus Torvalds { 1252dfe55b4SPatrick McHardy u32 id; 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds for (id = 1; id <= RT_TABLE_MAX; id++) 1288ad4942cSDenis V. Lunev if (fib_get_table(net, id) == NULL) 1298ad4942cSDenis V. Lunev return fib_new_table(net, id); 1301da177e4SLinus Torvalds return NULL; 1311da177e4SLinus Torvalds } 1321da177e4SLinus Torvalds 133ef7c79edSPatrick McHardy static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = { 1341f6c9557SThomas Graf FRA_GENERIC_POLICY, 135e1ef4bf2SThomas Graf [FRA_FLOW] = { .type = NLA_U32 }, 1361da177e4SLinus Torvalds }; 1371da177e4SLinus Torvalds 138e1ef4bf2SThomas Graf static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb, 1398b3521eeSRami Rosen struct fib_rule_hdr *frh, 140e1ef4bf2SThomas Graf struct nlattr **tb) 1411da177e4SLinus Torvalds { 1423b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 143e1ef4bf2SThomas Graf int err = -EINVAL; 144e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 1451da177e4SLinus Torvalds 146e1701c68SThomas Graf if (frh->tos & ~IPTOS_TOS_MASK) 147e1ef4bf2SThomas Graf goto errout; 148e1ef4bf2SThomas Graf 149e1ef4bf2SThomas Graf if (rule->table == RT_TABLE_UNSPEC) { 150e1ef4bf2SThomas Graf if (rule->action == FR_ACT_TO_TBL) { 151e1ef4bf2SThomas Graf struct fib_table *table; 152e1ef4bf2SThomas Graf 153e4e4971cSDenis V. Lunev table = fib_empty_table(net); 154e1ef4bf2SThomas Graf if (table == NULL) { 155e1ef4bf2SThomas Graf err = -ENOBUFS; 156e1ef4bf2SThomas Graf goto errout; 157e1ef4bf2SThomas Graf } 158e1ef4bf2SThomas Graf 159e1ef4bf2SThomas Graf rule->table = table->tb_id; 160e1ef4bf2SThomas Graf } 161e1ef4bf2SThomas Graf } 162e1ef4bf2SThomas Graf 163e1701c68SThomas Graf if (frh->src_len) 16445d60b9eSAl Viro rule4->src = nla_get_be32(tb[FRA_SRC]); 165e1ef4bf2SThomas Graf 166e1701c68SThomas Graf if (frh->dst_len) 16745d60b9eSAl Viro rule4->dst = nla_get_be32(tb[FRA_DST]); 168e1ef4bf2SThomas Graf 169c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 170e1ef4bf2SThomas Graf if (tb[FRA_FLOW]) 171e1ef4bf2SThomas Graf rule4->tclassid = nla_get_u32(tb[FRA_FLOW]); 1721da177e4SLinus Torvalds #endif 1731da177e4SLinus Torvalds 174e1ef4bf2SThomas Graf rule4->src_len = frh->src_len; 175e1ef4bf2SThomas Graf rule4->srcmask = inet_make_mask(rule4->src_len); 176e1ef4bf2SThomas Graf rule4->dst_len = frh->dst_len; 177e1ef4bf2SThomas Graf rule4->dstmask = inet_make_mask(rule4->dst_len); 178e1ef4bf2SThomas Graf rule4->tos = frh->tos; 179e1ef4bf2SThomas Graf 180e1ef4bf2SThomas Graf err = 0; 181e1ef4bf2SThomas Graf errout: 182e1ef4bf2SThomas Graf return err; 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds 185e1ef4bf2SThomas Graf static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, 186e1ef4bf2SThomas Graf struct nlattr **tb) 187a5cdc030SPatrick McHardy { 188e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 189a5cdc030SPatrick McHardy 190e1ef4bf2SThomas Graf if (frh->src_len && (rule4->src_len != frh->src_len)) 191e1ef4bf2SThomas Graf return 0; 192e1ef4bf2SThomas Graf 193e1ef4bf2SThomas Graf if (frh->dst_len && (rule4->dst_len != frh->dst_len)) 194e1ef4bf2SThomas Graf return 0; 195e1ef4bf2SThomas Graf 196e1ef4bf2SThomas Graf if (frh->tos && (rule4->tos != frh->tos)) 197e1ef4bf2SThomas Graf return 0; 198e1ef4bf2SThomas Graf 199c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 200e1ef4bf2SThomas Graf if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW]))) 201e1ef4bf2SThomas Graf return 0; 202e1ef4bf2SThomas Graf #endif 203e1ef4bf2SThomas Graf 204e1701c68SThomas Graf if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC]))) 205e1ef4bf2SThomas Graf return 0; 206e1ef4bf2SThomas Graf 207e1701c68SThomas Graf if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST]))) 208e1ef4bf2SThomas Graf return 0; 209e1ef4bf2SThomas Graf 210e1ef4bf2SThomas Graf return 1; 211a5cdc030SPatrick McHardy } 212a5cdc030SPatrick McHardy 213e1ef4bf2SThomas Graf static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb, 21404af8cf6SRami Rosen struct fib_rule_hdr *frh) 2151da177e4SLinus Torvalds { 216e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 2171da177e4SLinus Torvalds 218e1ef4bf2SThomas Graf frh->dst_len = rule4->dst_len; 219e1ef4bf2SThomas Graf frh->src_len = rule4->src_len; 220e1ef4bf2SThomas Graf frh->tos = rule4->tos; 2211da177e4SLinus Torvalds 222e1ef4bf2SThomas Graf if (rule4->dst_len) 22345d60b9eSAl Viro NLA_PUT_BE32(skb, FRA_DST, rule4->dst); 224e1ef4bf2SThomas Graf 225e1ef4bf2SThomas Graf if (rule4->src_len) 22645d60b9eSAl Viro NLA_PUT_BE32(skb, FRA_SRC, rule4->src); 227e1ef4bf2SThomas Graf 228c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 229e1ef4bf2SThomas Graf if (rule4->tclassid) 230e1ef4bf2SThomas Graf NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid); 231e1ef4bf2SThomas Graf #endif 232e1ef4bf2SThomas Graf return 0; 233e1ef4bf2SThomas Graf 234e1ef4bf2SThomas Graf nla_put_failure: 235e1ef4bf2SThomas Graf return -ENOBUFS; 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 238339bf98fSThomas Graf static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule) 239339bf98fSThomas Graf { 240339bf98fSThomas Graf return nla_total_size(4) /* dst */ 241339bf98fSThomas Graf + nla_total_size(4) /* src */ 242339bf98fSThomas Graf + nla_total_size(4); /* flow */ 243339bf98fSThomas Graf } 244339bf98fSThomas Graf 245ae299fc0SDenis V. Lunev static void fib4_rule_flush_cache(struct fib_rules_ops *ops) 24673417f61SThomas Graf { 247ae299fc0SDenis V. Lunev rt_cache_flush(ops->fro_net, -1); 24873417f61SThomas Graf } 24973417f61SThomas Graf 2503d0c9c4eSPatrick McHardy static const struct fib_rules_ops __net_initdata fib4_rules_ops_template = { 25125239ceeSPatrick McHardy .family = AF_INET, 252e1ef4bf2SThomas Graf .rule_size = sizeof(struct fib4_rule), 253e1701c68SThomas Graf .addr_size = sizeof(u32), 254e1ef4bf2SThomas Graf .action = fib4_rule_action, 255e1ef4bf2SThomas Graf .match = fib4_rule_match, 256e1ef4bf2SThomas Graf .configure = fib4_rule_configure, 257e1ef4bf2SThomas Graf .compare = fib4_rule_compare, 258e1ef4bf2SThomas Graf .fill = fib4_rule_fill, 259d8a566beSPatrick McHardy .default_pref = fib_default_rule_pref, 260339bf98fSThomas Graf .nlmsg_payload = fib4_rule_nlmsg_payload, 26173417f61SThomas Graf .flush_cache = fib4_rule_flush_cache, 262e1ef4bf2SThomas Graf .nlgroup = RTNLGRP_IPV4_RULE, 263e1ef4bf2SThomas Graf .policy = fib4_rule_policy, 264e1ef4bf2SThomas Graf .owner = THIS_MODULE, 265e1ef4bf2SThomas Graf }; 266e1ef4bf2SThomas Graf 267e4e4971cSDenis V. Lunev static int fib_default_rules_init(struct fib_rules_ops *ops) 2682994c638SDenis V. Lunev { 2692994c638SDenis V. Lunev int err; 2702994c638SDenis V. Lunev 2715adef180SPatrick McHardy err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0); 2722994c638SDenis V. Lunev if (err < 0) 2732994c638SDenis V. Lunev return err; 274e4e4971cSDenis V. Lunev err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0); 2752994c638SDenis V. Lunev if (err < 0) 2762994c638SDenis V. Lunev return err; 277e4e4971cSDenis V. Lunev err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0); 2782994c638SDenis V. Lunev if (err < 0) 2792994c638SDenis V. Lunev return err; 2802994c638SDenis V. Lunev return 0; 2812994c638SDenis V. Lunev } 2822994c638SDenis V. Lunev 2837b1a74fdSDenis V. Lunev int __net_init fib4_rules_init(struct net *net) 284e1ef4bf2SThomas Graf { 285dbb50165SDenis V. Lunev int err; 286e4e4971cSDenis V. Lunev struct fib_rules_ops *ops; 287dbb50165SDenis V. Lunev 288e9c5158aSEric W. Biederman ops = fib_rules_register(&fib4_rules_ops_template, net); 289e9c5158aSEric W. Biederman if (IS_ERR(ops)) 290e9c5158aSEric W. Biederman return PTR_ERR(ops); 291e4e4971cSDenis V. Lunev 292e4e4971cSDenis V. Lunev err = fib_default_rules_init(ops); 293dbb50165SDenis V. Lunev if (err < 0) 294dbb50165SDenis V. Lunev goto fail; 295e4e4971cSDenis V. Lunev net->ipv4.rules_ops = ops; 296dbb50165SDenis V. Lunev return 0; 297dbb50165SDenis V. Lunev 298dbb50165SDenis V. Lunev fail: 299dbb50165SDenis V. Lunev /* also cleans all rules already added */ 3009e3a5487SDenis V. Lunev fib_rules_unregister(ops); 301dbb50165SDenis V. Lunev return err; 302e1ef4bf2SThomas Graf } 3037b1a74fdSDenis V. Lunev 3047b1a74fdSDenis V. Lunev void __net_exit fib4_rules_exit(struct net *net) 3057b1a74fdSDenis V. Lunev { 3069e3a5487SDenis V. Lunev fib_rules_unregister(net->ipv4.rules_ops); 3077b1a74fdSDenis V. Lunev } 308