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> 9*e1ef4bf2SThomas 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> 25*e1ef4bf2SThomas 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> 33*e1ef4bf2SThomas Graf #include <net/fib_rules.h> 341da177e4SLinus Torvalds 35*e1ef4bf2SThomas Graf static struct fib_rules_ops fib4_rules_ops; 361da177e4SLinus Torvalds 37*e1ef4bf2SThomas Graf struct fib4_rule 381da177e4SLinus Torvalds { 39*e1ef4bf2SThomas Graf struct fib_rule common; 40*e1ef4bf2SThomas Graf u8 dst_len; 41*e1ef4bf2SThomas Graf u8 src_len; 42*e1ef4bf2SThomas Graf u8 tos; 43*e1ef4bf2SThomas Graf u32 src; 44*e1ef4bf2SThomas Graf u32 srcmask; 45*e1ef4bf2SThomas Graf u32 dst; 46*e1ef4bf2SThomas Graf u32 dstmask; 471da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_FWMARK 48*e1ef4bf2SThomas Graf u32 fwmark; 491da177e4SLinus Torvalds #endif 501da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE 51*e1ef4bf2SThomas Graf u32 tclassid; 521da177e4SLinus Torvalds #endif 531da177e4SLinus Torvalds }; 541da177e4SLinus Torvalds 55*e1ef4bf2SThomas Graf static struct fib4_rule default_rule = { 56*e1ef4bf2SThomas Graf .common = { 57*e1ef4bf2SThomas Graf .refcnt = ATOMIC_INIT(2), 58*e1ef4bf2SThomas Graf .pref = 0x7FFF, 59*e1ef4bf2SThomas Graf .table = RT_TABLE_DEFAULT, 60*e1ef4bf2SThomas Graf .action = FR_ACT_TO_TBL, 61*e1ef4bf2SThomas Graf }, 621da177e4SLinus Torvalds }; 631da177e4SLinus Torvalds 64*e1ef4bf2SThomas Graf static struct fib4_rule main_rule = { 65*e1ef4bf2SThomas Graf .common = { 66*e1ef4bf2SThomas Graf .refcnt = ATOMIC_INIT(2), 67*e1ef4bf2SThomas Graf .pref = 0x7FFE, 68*e1ef4bf2SThomas Graf .table = RT_TABLE_MAIN, 69*e1ef4bf2SThomas Graf .action = FR_ACT_TO_TBL, 70*e1ef4bf2SThomas Graf }, 711da177e4SLinus Torvalds }; 721da177e4SLinus Torvalds 73*e1ef4bf2SThomas Graf static struct fib4_rule local_rule = { 74*e1ef4bf2SThomas Graf .common = { 75*e1ef4bf2SThomas Graf .refcnt = ATOMIC_INIT(2), 76*e1ef4bf2SThomas Graf .table = RT_TABLE_LOCAL, 77*e1ef4bf2SThomas Graf .action = FR_ACT_TO_TBL, 78*e1ef4bf2SThomas Graf .flags = FIB_RULE_PERMANENT, 79*e1ef4bf2SThomas Graf }, 801da177e4SLinus Torvalds }; 811da177e4SLinus Torvalds 82*e1ef4bf2SThomas Graf static LIST_HEAD(fib4_rules); 837b204afdSRobert Olsson 84*e1ef4bf2SThomas Graf #ifdef CONFIG_NET_CLS_ROUTE 85*e1ef4bf2SThomas Graf u32 fib_rules_tclass(struct fib_result *res) 861da177e4SLinus Torvalds { 87*e1ef4bf2SThomas Graf return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0; 88*e1ef4bf2SThomas Graf } 891da177e4SLinus Torvalds #endif 901da177e4SLinus Torvalds 91*e1ef4bf2SThomas Graf int fib_lookup(struct flowi *flp, struct fib_result *res) 92*e1ef4bf2SThomas Graf { 93*e1ef4bf2SThomas Graf struct fib_lookup_arg arg = { 94*e1ef4bf2SThomas Graf .result = res, 95*e1ef4bf2SThomas Graf }; 96*e1ef4bf2SThomas Graf int err; 97*e1ef4bf2SThomas Graf 98*e1ef4bf2SThomas Graf err = fib_rules_lookup(&fib4_rules_ops, flp, 0, &arg); 99*e1ef4bf2SThomas Graf res->r = arg.rule; 100*e1ef4bf2SThomas Graf 1011da177e4SLinus Torvalds return err; 1021da177e4SLinus Torvalds } 1031da177e4SLinus Torvalds 104*e1ef4bf2SThomas Graf int fib4_rule_action(struct fib_rule *rule, struct flowi *flp, int flags, 105*e1ef4bf2SThomas Graf struct fib_lookup_arg *arg) 106*e1ef4bf2SThomas Graf { 107*e1ef4bf2SThomas Graf int err = -EAGAIN; 108*e1ef4bf2SThomas Graf struct fib_table *tbl; 109*e1ef4bf2SThomas Graf 110*e1ef4bf2SThomas Graf switch (rule->action) { 111*e1ef4bf2SThomas Graf case FR_ACT_TO_TBL: 112*e1ef4bf2SThomas Graf break; 113*e1ef4bf2SThomas Graf 114*e1ef4bf2SThomas Graf case FR_ACT_UNREACHABLE: 115*e1ef4bf2SThomas Graf err = -ENETUNREACH; 116*e1ef4bf2SThomas Graf goto errout; 117*e1ef4bf2SThomas Graf 118*e1ef4bf2SThomas Graf case FR_ACT_PROHIBIT: 119*e1ef4bf2SThomas Graf err = -EACCES; 120*e1ef4bf2SThomas Graf goto errout; 121*e1ef4bf2SThomas Graf 122*e1ef4bf2SThomas Graf case FR_ACT_BLACKHOLE: 123*e1ef4bf2SThomas Graf default: 124*e1ef4bf2SThomas Graf err = -EINVAL; 125*e1ef4bf2SThomas Graf goto errout; 126*e1ef4bf2SThomas Graf } 127*e1ef4bf2SThomas Graf 128*e1ef4bf2SThomas Graf if ((tbl = fib_get_table(rule->table)) == NULL) 129*e1ef4bf2SThomas Graf goto errout; 130*e1ef4bf2SThomas Graf 131*e1ef4bf2SThomas Graf err = tbl->tb_lookup(tbl, flp, (struct fib_result *) arg->result); 132*e1ef4bf2SThomas Graf if (err > 0) 133*e1ef4bf2SThomas Graf err = -EAGAIN; 134*e1ef4bf2SThomas Graf errout: 135*e1ef4bf2SThomas Graf return err; 136*e1ef4bf2SThomas Graf } 137*e1ef4bf2SThomas Graf 138*e1ef4bf2SThomas Graf 139*e1ef4bf2SThomas Graf void fib_select_default(const struct flowi *flp, struct fib_result *res) 140*e1ef4bf2SThomas Graf { 141*e1ef4bf2SThomas Graf if (res->r && res->r->action == FR_ACT_TO_TBL && 142*e1ef4bf2SThomas Graf FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) { 143*e1ef4bf2SThomas Graf struct fib_table *tb; 144*e1ef4bf2SThomas Graf if ((tb = fib_get_table(res->r->table)) != NULL) 145*e1ef4bf2SThomas Graf tb->tb_select_default(tb, flp, res); 146*e1ef4bf2SThomas Graf } 147*e1ef4bf2SThomas Graf } 148*e1ef4bf2SThomas Graf 149*e1ef4bf2SThomas Graf static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) 150*e1ef4bf2SThomas Graf { 151*e1ef4bf2SThomas Graf struct fib4_rule *r = (struct fib4_rule *) rule; 152*e1ef4bf2SThomas Graf u32 daddr = fl->fl4_dst; 153*e1ef4bf2SThomas Graf u32 saddr = fl->fl4_src; 154*e1ef4bf2SThomas Graf 155*e1ef4bf2SThomas Graf if (((saddr ^ r->src) & r->srcmask) || 156*e1ef4bf2SThomas Graf ((daddr ^ r->dst) & r->dstmask)) 157*e1ef4bf2SThomas Graf return 0; 158*e1ef4bf2SThomas Graf 159*e1ef4bf2SThomas Graf if (r->tos && (r->tos != fl->fl4_tos)) 160*e1ef4bf2SThomas Graf return 0; 161*e1ef4bf2SThomas Graf 162*e1ef4bf2SThomas Graf #ifdef CONFIG_IP_ROUTE_FWMARK 163*e1ef4bf2SThomas Graf if (r->fwmark && (r->fwmark != fl->fl4_fwmark)) 164*e1ef4bf2SThomas Graf return 0; 165*e1ef4bf2SThomas Graf #endif 166*e1ef4bf2SThomas Graf 167*e1ef4bf2SThomas Graf return 1; 168*e1ef4bf2SThomas Graf } 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds static struct fib_table *fib_empty_table(void) 1711da177e4SLinus Torvalds { 1721da177e4SLinus Torvalds int id; 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds for (id = 1; id <= RT_TABLE_MAX; id++) 1751da177e4SLinus Torvalds if (fib_tables[id] == NULL) 1761da177e4SLinus Torvalds return __fib_new_table(id); 1771da177e4SLinus Torvalds return NULL; 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds 180*e1ef4bf2SThomas Graf static struct nla_policy fib4_rule_policy[FRA_MAX+1] __read_mostly = { 181*e1ef4bf2SThomas Graf [FRA_IFNAME] = { .type = NLA_STRING }, 182*e1ef4bf2SThomas Graf [FRA_PRIORITY] = { .type = NLA_U32 }, 183*e1ef4bf2SThomas Graf [FRA_SRC] = { .type = NLA_U32 }, 184*e1ef4bf2SThomas Graf [FRA_DST] = { .type = NLA_U32 }, 185*e1ef4bf2SThomas Graf [FRA_FWMARK] = { .type = NLA_U32 }, 186*e1ef4bf2SThomas Graf [FRA_FLOW] = { .type = NLA_U32 }, 1871da177e4SLinus Torvalds }; 1881da177e4SLinus Torvalds 189*e1ef4bf2SThomas Graf static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb, 190*e1ef4bf2SThomas Graf struct nlmsghdr *nlh, struct fib_rule_hdr *frh, 191*e1ef4bf2SThomas Graf struct nlattr **tb) 1921da177e4SLinus Torvalds { 193*e1ef4bf2SThomas Graf int err = -EINVAL; 194*e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 1951da177e4SLinus Torvalds 196*e1ef4bf2SThomas Graf if (frh->src_len > 32 || frh->dst_len > 32 || 197*e1ef4bf2SThomas Graf (frh->tos & ~IPTOS_TOS_MASK)) 198*e1ef4bf2SThomas Graf goto errout; 199*e1ef4bf2SThomas Graf 200*e1ef4bf2SThomas Graf if (rule->table == RT_TABLE_UNSPEC) { 201*e1ef4bf2SThomas Graf if (rule->action == FR_ACT_TO_TBL) { 202*e1ef4bf2SThomas Graf struct fib_table *table; 203*e1ef4bf2SThomas Graf 204*e1ef4bf2SThomas Graf table = fib_empty_table(); 205*e1ef4bf2SThomas Graf if (table == NULL) { 206*e1ef4bf2SThomas Graf err = -ENOBUFS; 207*e1ef4bf2SThomas Graf goto errout; 208*e1ef4bf2SThomas Graf } 209*e1ef4bf2SThomas Graf 210*e1ef4bf2SThomas Graf rule->table = table->tb_id; 211*e1ef4bf2SThomas Graf } 212*e1ef4bf2SThomas Graf } 213*e1ef4bf2SThomas Graf 214*e1ef4bf2SThomas Graf if (tb[FRA_SRC]) 215*e1ef4bf2SThomas Graf rule4->src = nla_get_u32(tb[FRA_SRC]); 216*e1ef4bf2SThomas Graf 217*e1ef4bf2SThomas Graf if (tb[FRA_DST]) 218*e1ef4bf2SThomas Graf rule4->dst = nla_get_u32(tb[FRA_DST]); 219*e1ef4bf2SThomas Graf 2201da177e4SLinus Torvalds #ifdef CONFIG_IP_ROUTE_FWMARK 221*e1ef4bf2SThomas Graf if (tb[FRA_FWMARK]) 222*e1ef4bf2SThomas Graf rule4->fwmark = nla_get_u32(tb[FRA_FWMARK]); 2231da177e4SLinus Torvalds #endif 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds #ifdef CONFIG_NET_CLS_ROUTE 226*e1ef4bf2SThomas Graf if (tb[FRA_FLOW]) 227*e1ef4bf2SThomas Graf rule4->tclassid = nla_get_u32(tb[FRA_FLOW]); 2281da177e4SLinus Torvalds #endif 2291da177e4SLinus Torvalds 230*e1ef4bf2SThomas Graf rule4->src_len = frh->src_len; 231*e1ef4bf2SThomas Graf rule4->srcmask = inet_make_mask(rule4->src_len); 232*e1ef4bf2SThomas Graf rule4->dst_len = frh->dst_len; 233*e1ef4bf2SThomas Graf rule4->dstmask = inet_make_mask(rule4->dst_len); 234*e1ef4bf2SThomas Graf rule4->tos = frh->tos; 235*e1ef4bf2SThomas Graf 236*e1ef4bf2SThomas Graf err = 0; 237*e1ef4bf2SThomas Graf errout: 238*e1ef4bf2SThomas Graf return err; 2391da177e4SLinus Torvalds } 2401da177e4SLinus Torvalds 241*e1ef4bf2SThomas Graf static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, 242*e1ef4bf2SThomas Graf struct nlattr **tb) 243a5cdc030SPatrick McHardy { 244*e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 245a5cdc030SPatrick McHardy 246*e1ef4bf2SThomas Graf if (frh->src_len && (rule4->src_len != frh->src_len)) 247*e1ef4bf2SThomas Graf return 0; 248*e1ef4bf2SThomas Graf 249*e1ef4bf2SThomas Graf if (frh->dst_len && (rule4->dst_len != frh->dst_len)) 250*e1ef4bf2SThomas Graf return 0; 251*e1ef4bf2SThomas Graf 252*e1ef4bf2SThomas Graf if (frh->tos && (rule4->tos != frh->tos)) 253*e1ef4bf2SThomas Graf return 0; 254*e1ef4bf2SThomas Graf 255*e1ef4bf2SThomas Graf #ifdef CONFIG_IP_ROUTE_FWMARK 256*e1ef4bf2SThomas Graf if (tb[FRA_FWMARK] && (rule4->fwmark != nla_get_u32(tb[FRA_FWMARK]))) 257*e1ef4bf2SThomas Graf return 0; 258*e1ef4bf2SThomas Graf #endif 259*e1ef4bf2SThomas Graf 260*e1ef4bf2SThomas Graf #ifdef CONFIG_NET_CLS_ROUTE 261*e1ef4bf2SThomas Graf if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW]))) 262*e1ef4bf2SThomas Graf return 0; 263*e1ef4bf2SThomas Graf #endif 264*e1ef4bf2SThomas Graf 265*e1ef4bf2SThomas Graf if (tb[FRA_SRC] && (rule4->src != nla_get_u32(tb[FRA_SRC]))) 266*e1ef4bf2SThomas Graf return 0; 267*e1ef4bf2SThomas Graf 268*e1ef4bf2SThomas Graf if (tb[FRA_DST] && (rule4->dst != nla_get_u32(tb[FRA_DST]))) 269*e1ef4bf2SThomas Graf return 0; 270*e1ef4bf2SThomas Graf 271*e1ef4bf2SThomas Graf return 1; 272a5cdc030SPatrick McHardy } 273a5cdc030SPatrick McHardy 274*e1ef4bf2SThomas Graf static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb, 275*e1ef4bf2SThomas Graf struct nlmsghdr *nlh, struct fib_rule_hdr *frh) 2761da177e4SLinus Torvalds { 277*e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 2781da177e4SLinus Torvalds 279*e1ef4bf2SThomas Graf frh->family = AF_INET; 280*e1ef4bf2SThomas Graf frh->dst_len = rule4->dst_len; 281*e1ef4bf2SThomas Graf frh->src_len = rule4->src_len; 282*e1ef4bf2SThomas Graf frh->tos = rule4->tos; 2831da177e4SLinus Torvalds 284*e1ef4bf2SThomas Graf #ifdef CONFIG_IP_ROUTE_FWMARK 285*e1ef4bf2SThomas Graf if (rule4->fwmark) 286*e1ef4bf2SThomas Graf NLA_PUT_U32(skb, FRA_FWMARK, rule4->fwmark); 287*e1ef4bf2SThomas Graf #endif 288*e1ef4bf2SThomas Graf 289*e1ef4bf2SThomas Graf if (rule4->dst_len) 290*e1ef4bf2SThomas Graf NLA_PUT_U32(skb, FRA_DST, rule4->dst); 291*e1ef4bf2SThomas Graf 292*e1ef4bf2SThomas Graf if (rule4->src_len) 293*e1ef4bf2SThomas Graf NLA_PUT_U32(skb, FRA_SRC, rule4->src); 294*e1ef4bf2SThomas Graf 295*e1ef4bf2SThomas Graf #ifdef CONFIG_NET_CLS_ROUTE 296*e1ef4bf2SThomas Graf if (rule4->tclassid) 297*e1ef4bf2SThomas Graf NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid); 298*e1ef4bf2SThomas Graf #endif 299*e1ef4bf2SThomas Graf return 0; 300*e1ef4bf2SThomas Graf 301*e1ef4bf2SThomas Graf nla_put_failure: 302*e1ef4bf2SThomas Graf return -ENOBUFS; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 305*e1ef4bf2SThomas Graf int fib4_rules_dump(struct sk_buff *skb, struct netlink_callback *cb) 3061da177e4SLinus Torvalds { 307*e1ef4bf2SThomas Graf return fib_rules_dump(skb, cb, AF_INET); 308*e1ef4bf2SThomas Graf } 309*e1ef4bf2SThomas Graf 310*e1ef4bf2SThomas Graf static u32 fib4_rule_default_pref(void) 311*e1ef4bf2SThomas Graf { 312*e1ef4bf2SThomas Graf struct list_head *pos; 313*e1ef4bf2SThomas Graf struct fib_rule *rule; 314*e1ef4bf2SThomas Graf 315*e1ef4bf2SThomas Graf if (!list_empty(&fib4_rules)) { 316*e1ef4bf2SThomas Graf pos = fib4_rules.next; 317*e1ef4bf2SThomas Graf if (pos->next != &fib4_rules) { 318*e1ef4bf2SThomas Graf rule = list_entry(pos->next, struct fib_rule, list); 319*e1ef4bf2SThomas Graf if (rule->pref) 320*e1ef4bf2SThomas Graf return rule->pref - 1; 321*e1ef4bf2SThomas Graf } 322*e1ef4bf2SThomas Graf } 323*e1ef4bf2SThomas Graf 324*e1ef4bf2SThomas Graf return 0; 325*e1ef4bf2SThomas Graf } 326*e1ef4bf2SThomas Graf 327*e1ef4bf2SThomas Graf static struct fib_rules_ops fib4_rules_ops = { 328*e1ef4bf2SThomas Graf .family = AF_INET, 329*e1ef4bf2SThomas Graf .rule_size = sizeof(struct fib4_rule), 330*e1ef4bf2SThomas Graf .action = fib4_rule_action, 331*e1ef4bf2SThomas Graf .match = fib4_rule_match, 332*e1ef4bf2SThomas Graf .configure = fib4_rule_configure, 333*e1ef4bf2SThomas Graf .compare = fib4_rule_compare, 334*e1ef4bf2SThomas Graf .fill = fib4_rule_fill, 335*e1ef4bf2SThomas Graf .default_pref = fib4_rule_default_pref, 336*e1ef4bf2SThomas Graf .nlgroup = RTNLGRP_IPV4_RULE, 337*e1ef4bf2SThomas Graf .policy = fib4_rule_policy, 338*e1ef4bf2SThomas Graf .rules_list = &fib4_rules, 339*e1ef4bf2SThomas Graf .owner = THIS_MODULE, 340*e1ef4bf2SThomas Graf }; 341*e1ef4bf2SThomas Graf 342*e1ef4bf2SThomas Graf void __init fib4_rules_init(void) 343*e1ef4bf2SThomas Graf { 344*e1ef4bf2SThomas Graf list_add_tail(&local_rule.common.list, &fib4_rules); 345*e1ef4bf2SThomas Graf list_add_tail(&main_rule.common.list, &fib4_rules); 346*e1ef4bf2SThomas Graf list_add_tail(&default_rule.common.list, &fib4_rules); 347*e1ef4bf2SThomas Graf 348*e1ef4bf2SThomas Graf fib_rules_register(&fib4_rules_ops); 349*e1ef4bf2SThomas Graf } 350*e1ef4bf2SThomas Graf 351*e1ef4bf2SThomas Graf void __exit fib4_rules_cleanup(void) 352*e1ef4bf2SThomas Graf { 353*e1ef4bf2SThomas Graf fib_rules_unregister(&fib4_rules_ops); 3541da177e4SLinus Torvalds } 355