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 35e1ef4bf2SThomas Graf static struct fib_rules_ops fib4_rules_ops; 361da177e4SLinus Torvalds 37e1ef4bf2SThomas Graf struct fib4_rule 381da177e4SLinus Torvalds { 39e1ef4bf2SThomas Graf struct fib_rule common; 40e1ef4bf2SThomas Graf u8 dst_len; 41e1ef4bf2SThomas Graf u8 src_len; 42e1ef4bf2SThomas Graf u8 tos; 4381f7bf6cSAl Viro __be32 src; 4481f7bf6cSAl Viro __be32 srcmask; 4581f7bf6cSAl Viro __be32 dst; 4681f7bf6cSAl Viro __be32 dstmask; 471da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE 48e1ef4bf2SThomas Graf u32 tclassid; 491da177e4SLinus Torvalds #endif 501da177e4SLinus Torvalds }; 511da177e4SLinus Torvalds 52e1ef4bf2SThomas Graf #ifdef CONFIG_NET_CLS_ROUTE 53e1ef4bf2SThomas Graf u32 fib_rules_tclass(struct fib_result *res) 541da177e4SLinus Torvalds { 55e1ef4bf2SThomas Graf return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0; 56e1ef4bf2SThomas Graf } 571da177e4SLinus Torvalds #endif 581da177e4SLinus Torvalds 59e1ef4bf2SThomas Graf int fib_lookup(struct flowi *flp, struct fib_result *res) 60e1ef4bf2SThomas Graf { 61e1ef4bf2SThomas Graf struct fib_lookup_arg arg = { 62e1ef4bf2SThomas Graf .result = res, 63e1ef4bf2SThomas Graf }; 64e1ef4bf2SThomas Graf int err; 65e1ef4bf2SThomas Graf 66e1ef4bf2SThomas Graf err = fib_rules_lookup(&fib4_rules_ops, flp, 0, &arg); 67e1ef4bf2SThomas Graf res->r = arg.rule; 68e1ef4bf2SThomas Graf 691da177e4SLinus Torvalds return err; 701da177e4SLinus Torvalds } 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 96e1ef4bf2SThomas Graf if ((tbl = fib_get_table(rule->table)) == NULL) 97e1ef4bf2SThomas Graf goto errout; 98e1ef4bf2SThomas Graf 99e1ef4bf2SThomas Graf err = tbl->tb_lookup(tbl, flp, (struct fib_result *) arg->result); 100e1ef4bf2SThomas Graf if (err > 0) 101e1ef4bf2SThomas Graf err = -EAGAIN; 102e1ef4bf2SThomas Graf errout: 103e1ef4bf2SThomas Graf return err; 104e1ef4bf2SThomas Graf } 105e1ef4bf2SThomas Graf 106e1ef4bf2SThomas Graf 107e1ef4bf2SThomas Graf void fib_select_default(const struct flowi *flp, struct fib_result *res) 108e1ef4bf2SThomas Graf { 109e1ef4bf2SThomas Graf if (res->r && res->r->action == FR_ACT_TO_TBL && 110e1ef4bf2SThomas Graf FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) { 111e1ef4bf2SThomas Graf struct fib_table *tb; 112e1ef4bf2SThomas Graf if ((tb = fib_get_table(res->r->table)) != NULL) 113e1ef4bf2SThomas Graf tb->tb_select_default(tb, flp, res); 114e1ef4bf2SThomas Graf } 115e1ef4bf2SThomas Graf } 116e1ef4bf2SThomas Graf 117e1ef4bf2SThomas Graf static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) 118e1ef4bf2SThomas Graf { 119e1ef4bf2SThomas Graf struct fib4_rule *r = (struct fib4_rule *) rule; 12081f7bf6cSAl Viro __be32 daddr = fl->fl4_dst; 12181f7bf6cSAl Viro __be32 saddr = fl->fl4_src; 122e1ef4bf2SThomas Graf 123e1ef4bf2SThomas Graf if (((saddr ^ r->src) & r->srcmask) || 124e1ef4bf2SThomas Graf ((daddr ^ r->dst) & r->dstmask)) 125e1ef4bf2SThomas Graf return 0; 126e1ef4bf2SThomas Graf 127e1ef4bf2SThomas Graf if (r->tos && (r->tos != fl->fl4_tos)) 128e1ef4bf2SThomas Graf return 0; 129e1ef4bf2SThomas Graf 130e1ef4bf2SThomas Graf return 1; 131e1ef4bf2SThomas Graf } 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds static struct fib_table *fib_empty_table(void) 1341da177e4SLinus Torvalds { 1352dfe55b4SPatrick McHardy u32 id; 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds for (id = 1; id <= RT_TABLE_MAX; id++) 1381af5a8c4SPatrick McHardy if (fib_get_table(id) == NULL) 1391af5a8c4SPatrick McHardy return fib_new_table(id); 1401da177e4SLinus Torvalds return NULL; 1411da177e4SLinus Torvalds } 1421da177e4SLinus Torvalds 143ef7c79edSPatrick McHardy static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = { 1441f6c9557SThomas Graf FRA_GENERIC_POLICY, 145e1ef4bf2SThomas Graf [FRA_FLOW] = { .type = NLA_U32 }, 1461da177e4SLinus Torvalds }; 1471da177e4SLinus Torvalds 148e1ef4bf2SThomas Graf static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb, 149e1ef4bf2SThomas Graf struct nlmsghdr *nlh, struct fib_rule_hdr *frh, 150e1ef4bf2SThomas Graf struct nlattr **tb) 1511da177e4SLinus Torvalds { 152e1ef4bf2SThomas Graf int err = -EINVAL; 153e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 1541da177e4SLinus Torvalds 155e1701c68SThomas Graf if (frh->tos & ~IPTOS_TOS_MASK) 156e1ef4bf2SThomas Graf goto errout; 157e1ef4bf2SThomas Graf 158e1ef4bf2SThomas Graf if (rule->table == RT_TABLE_UNSPEC) { 159e1ef4bf2SThomas Graf if (rule->action == FR_ACT_TO_TBL) { 160e1ef4bf2SThomas Graf struct fib_table *table; 161e1ef4bf2SThomas Graf 162e1ef4bf2SThomas Graf table = fib_empty_table(); 163e1ef4bf2SThomas Graf if (table == NULL) { 164e1ef4bf2SThomas Graf err = -ENOBUFS; 165e1ef4bf2SThomas Graf goto errout; 166e1ef4bf2SThomas Graf } 167e1ef4bf2SThomas Graf 168e1ef4bf2SThomas Graf rule->table = table->tb_id; 169e1ef4bf2SThomas Graf } 170e1ef4bf2SThomas Graf } 171e1ef4bf2SThomas Graf 172e1701c68SThomas Graf if (frh->src_len) 17345d60b9eSAl Viro rule4->src = nla_get_be32(tb[FRA_SRC]); 174e1ef4bf2SThomas Graf 175e1701c68SThomas Graf if (frh->dst_len) 17645d60b9eSAl Viro rule4->dst = nla_get_be32(tb[FRA_DST]); 177e1ef4bf2SThomas Graf 1781da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE 179e1ef4bf2SThomas Graf if (tb[FRA_FLOW]) 180e1ef4bf2SThomas Graf rule4->tclassid = nla_get_u32(tb[FRA_FLOW]); 1811da177e4SLinus Torvalds #endif 1821da177e4SLinus Torvalds 183e1ef4bf2SThomas Graf rule4->src_len = frh->src_len; 184e1ef4bf2SThomas Graf rule4->srcmask = inet_make_mask(rule4->src_len); 185e1ef4bf2SThomas Graf rule4->dst_len = frh->dst_len; 186e1ef4bf2SThomas Graf rule4->dstmask = inet_make_mask(rule4->dst_len); 187e1ef4bf2SThomas Graf rule4->tos = frh->tos; 188e1ef4bf2SThomas Graf 189e1ef4bf2SThomas Graf err = 0; 190e1ef4bf2SThomas Graf errout: 191e1ef4bf2SThomas Graf return err; 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 194e1ef4bf2SThomas Graf static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, 195e1ef4bf2SThomas Graf struct nlattr **tb) 196a5cdc030SPatrick McHardy { 197e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 198a5cdc030SPatrick McHardy 199e1ef4bf2SThomas Graf if (frh->src_len && (rule4->src_len != frh->src_len)) 200e1ef4bf2SThomas Graf return 0; 201e1ef4bf2SThomas Graf 202e1ef4bf2SThomas Graf if (frh->dst_len && (rule4->dst_len != frh->dst_len)) 203e1ef4bf2SThomas Graf return 0; 204e1ef4bf2SThomas Graf 205e1ef4bf2SThomas Graf if (frh->tos && (rule4->tos != frh->tos)) 206e1ef4bf2SThomas Graf return 0; 207e1ef4bf2SThomas Graf 208e1ef4bf2SThomas Graf #ifdef CONFIG_NET_CLS_ROUTE 209e1ef4bf2SThomas Graf if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW]))) 210e1ef4bf2SThomas Graf return 0; 211e1ef4bf2SThomas Graf #endif 212e1ef4bf2SThomas Graf 213e1701c68SThomas Graf if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC]))) 214e1ef4bf2SThomas Graf return 0; 215e1ef4bf2SThomas Graf 216e1701c68SThomas Graf if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST]))) 217e1ef4bf2SThomas Graf return 0; 218e1ef4bf2SThomas Graf 219e1ef4bf2SThomas Graf return 1; 220a5cdc030SPatrick McHardy } 221a5cdc030SPatrick McHardy 222e1ef4bf2SThomas Graf static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb, 223e1ef4bf2SThomas Graf struct nlmsghdr *nlh, struct fib_rule_hdr *frh) 2241da177e4SLinus Torvalds { 225e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 2261da177e4SLinus Torvalds 227e1ef4bf2SThomas Graf frh->family = AF_INET; 228e1ef4bf2SThomas Graf frh->dst_len = rule4->dst_len; 229e1ef4bf2SThomas Graf frh->src_len = rule4->src_len; 230e1ef4bf2SThomas Graf frh->tos = rule4->tos; 2311da177e4SLinus Torvalds 232e1ef4bf2SThomas Graf if (rule4->dst_len) 23345d60b9eSAl Viro NLA_PUT_BE32(skb, FRA_DST, rule4->dst); 234e1ef4bf2SThomas Graf 235e1ef4bf2SThomas Graf if (rule4->src_len) 23645d60b9eSAl Viro NLA_PUT_BE32(skb, FRA_SRC, rule4->src); 237e1ef4bf2SThomas Graf 238e1ef4bf2SThomas Graf #ifdef CONFIG_NET_CLS_ROUTE 239e1ef4bf2SThomas Graf if (rule4->tclassid) 240e1ef4bf2SThomas Graf NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid); 241e1ef4bf2SThomas Graf #endif 242e1ef4bf2SThomas Graf return 0; 243e1ef4bf2SThomas Graf 244e1ef4bf2SThomas Graf nla_put_failure: 245e1ef4bf2SThomas Graf return -ENOBUFS; 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds 248868d13acSDenis V. Lunev static u32 fib4_rule_default_pref(struct fib_rules_ops *ops) 249e1ef4bf2SThomas Graf { 250e1ef4bf2SThomas Graf struct list_head *pos; 251e1ef4bf2SThomas Graf struct fib_rule *rule; 252e1ef4bf2SThomas Graf 25376c72d4fSDenis V. Lunev if (!list_empty(&fib4_rules_ops.rules_list)) { 25476c72d4fSDenis V. Lunev pos = fib4_rules_ops.rules_list.next; 25576c72d4fSDenis V. Lunev if (pos->next != &fib4_rules_ops.rules_list) { 256e1ef4bf2SThomas Graf rule = list_entry(pos->next, struct fib_rule, list); 257e1ef4bf2SThomas Graf if (rule->pref) 258e1ef4bf2SThomas Graf return rule->pref - 1; 259e1ef4bf2SThomas Graf } 260e1ef4bf2SThomas Graf } 261e1ef4bf2SThomas Graf 262e1ef4bf2SThomas Graf return 0; 263e1ef4bf2SThomas Graf } 264e1ef4bf2SThomas Graf 265339bf98fSThomas Graf static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule) 266339bf98fSThomas Graf { 267339bf98fSThomas Graf return nla_total_size(4) /* dst */ 268339bf98fSThomas Graf + nla_total_size(4) /* src */ 269339bf98fSThomas Graf + nla_total_size(4); /* flow */ 270339bf98fSThomas Graf } 271339bf98fSThomas Graf 27273417f61SThomas Graf static void fib4_rule_flush_cache(void) 27373417f61SThomas Graf { 2744b19ca44SThomas Graf rt_cache_flush(-1); 27573417f61SThomas Graf } 27673417f61SThomas Graf 277e1ef4bf2SThomas Graf static struct fib_rules_ops fib4_rules_ops = { 278e1ef4bf2SThomas Graf .family = AF_INET, 279e1ef4bf2SThomas Graf .rule_size = sizeof(struct fib4_rule), 280e1701c68SThomas Graf .addr_size = sizeof(u32), 281e1ef4bf2SThomas Graf .action = fib4_rule_action, 282e1ef4bf2SThomas Graf .match = fib4_rule_match, 283e1ef4bf2SThomas Graf .configure = fib4_rule_configure, 284e1ef4bf2SThomas Graf .compare = fib4_rule_compare, 285e1ef4bf2SThomas Graf .fill = fib4_rule_fill, 286e1ef4bf2SThomas Graf .default_pref = fib4_rule_default_pref, 287339bf98fSThomas Graf .nlmsg_payload = fib4_rule_nlmsg_payload, 28873417f61SThomas Graf .flush_cache = fib4_rule_flush_cache, 289e1ef4bf2SThomas Graf .nlgroup = RTNLGRP_IPV4_RULE, 290e1ef4bf2SThomas Graf .policy = fib4_rule_policy, 29176c72d4fSDenis V. Lunev .rules_list = LIST_HEAD_INIT(fib4_rules_ops.rules_list), 292e1ef4bf2SThomas Graf .owner = THIS_MODULE, 293e1ef4bf2SThomas Graf }; 294e1ef4bf2SThomas Graf 2952994c638SDenis V. Lunev static int __init fib_default_rules_init(void) 2962994c638SDenis V. Lunev { 2972994c638SDenis V. Lunev int err; 2982994c638SDenis V. Lunev 2992994c638SDenis V. Lunev err = fib_default_rule_add(&fib4_rules_ops, 0, 3002994c638SDenis V. Lunev RT_TABLE_LOCAL, FIB_RULE_PERMANENT); 3012994c638SDenis V. Lunev if (err < 0) 3022994c638SDenis V. Lunev return err; 3032994c638SDenis V. Lunev err = fib_default_rule_add(&fib4_rules_ops, 0x7FFE, 3042994c638SDenis V. Lunev RT_TABLE_MAIN, 0); 3052994c638SDenis V. Lunev if (err < 0) 3062994c638SDenis V. Lunev return err; 3072994c638SDenis V. Lunev err = fib_default_rule_add(&fib4_rules_ops, 0x7FFF, 3082994c638SDenis V. Lunev RT_TABLE_DEFAULT, 0); 3092994c638SDenis V. Lunev if (err < 0) 3102994c638SDenis V. Lunev return err; 3112994c638SDenis V. Lunev return 0; 3122994c638SDenis V. Lunev } 3132994c638SDenis V. Lunev 314*dbb50165SDenis V. Lunev int __init fib4_rules_init() 315e1ef4bf2SThomas Graf { 316*dbb50165SDenis V. Lunev int err; 317*dbb50165SDenis V. Lunev 318f8c26b8dSDenis V. Lunev fib_rules_register(&init_net, &fib4_rules_ops); 319*dbb50165SDenis V. Lunev err = fib_default_rules_init(); 320*dbb50165SDenis V. Lunev if (err < 0) 321*dbb50165SDenis V. Lunev goto fail; 322*dbb50165SDenis V. Lunev return 0; 323*dbb50165SDenis V. Lunev 324*dbb50165SDenis V. Lunev fail: 325*dbb50165SDenis V. Lunev /* also cleans all rules already added */ 326*dbb50165SDenis V. Lunev fib_rules_unregister(&init_net, &fib4_rules_ops); 327*dbb50165SDenis V. Lunev return err; 328e1ef4bf2SThomas Graf } 329