1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * INET An implementation of the TCP/IP protocol suite for the LINUX 41da177e4SLinus Torvalds * operating system. INET is implemented using the BSD Socket 51da177e4SLinus Torvalds * interface as the means of communication with the user level. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * IPv4 Forwarding Information Base: policy rules. 81da177e4SLinus Torvalds * 91da177e4SLinus Torvalds * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> 10e1ef4bf2SThomas Graf * Thomas Graf <tgraf@suug.ch> 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * Fixes: 131da177e4SLinus Torvalds * Rani Assaf : local_rule cannot be deleted 141da177e4SLinus Torvalds * Marc Boucher : routing by fwmark 151da177e4SLinus Torvalds */ 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include <linux/types.h> 181da177e4SLinus Torvalds #include <linux/kernel.h> 191da177e4SLinus Torvalds #include <linux/netdevice.h> 201da177e4SLinus Torvalds #include <linux/netlink.h> 21e1ef4bf2SThomas Graf #include <linux/inetdevice.h> 221da177e4SLinus Torvalds #include <linux/init.h> 237b204afdSRobert Olsson #include <linux/list.h> 247b204afdSRobert Olsson #include <linux/rcupdate.h> 25bc3b2d7fSPaul Gortmaker #include <linux/export.h> 261da177e4SLinus Torvalds #include <net/ip.h> 271da177e4SLinus Torvalds #include <net/route.h> 281da177e4SLinus Torvalds #include <net/tcp.h> 291da177e4SLinus Torvalds #include <net/ip_fib.h> 30e1ef4bf2SThomas Graf #include <net/fib_rules.h> 311da177e4SLinus Torvalds 326a31d2a9SEric Dumazet struct fib4_rule { 33e1ef4bf2SThomas Graf struct fib_rule common; 34e1ef4bf2SThomas Graf u8 dst_len; 35e1ef4bf2SThomas Graf u8 src_len; 36e1ef4bf2SThomas Graf u8 tos; 3781f7bf6cSAl Viro __be32 src; 3881f7bf6cSAl Viro __be32 srcmask; 3981f7bf6cSAl Viro __be32 dst; 4081f7bf6cSAl Viro __be32 dstmask; 41c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 42e1ef4bf2SThomas Graf u32 tclassid; 431da177e4SLinus Torvalds #endif 441da177e4SLinus Torvalds }; 451da177e4SLinus Torvalds 463c71006dSIdo Schimmel static bool fib4_rule_matchall(const struct fib_rule *rule) 473c71006dSIdo Schimmel { 483c71006dSIdo Schimmel struct fib4_rule *r = container_of(rule, struct fib4_rule, common); 493c71006dSIdo Schimmel 503c71006dSIdo Schimmel if (r->dst_len || r->src_len || r->tos) 513c71006dSIdo Schimmel return false; 523c71006dSIdo Schimmel return fib_rule_matchall(rule); 533c71006dSIdo Schimmel } 543c71006dSIdo Schimmel 553c71006dSIdo Schimmel bool fib4_rule_default(const struct fib_rule *rule) 563c71006dSIdo Schimmel { 573c71006dSIdo Schimmel if (!fib4_rule_matchall(rule) || rule->action != FR_ACT_TO_TBL || 583c71006dSIdo Schimmel rule->l3mdev) 593c71006dSIdo Schimmel return false; 603c71006dSIdo Schimmel if (rule->table != RT_TABLE_LOCAL && rule->table != RT_TABLE_MAIN && 613c71006dSIdo Schimmel rule->table != RT_TABLE_DEFAULT) 623c71006dSIdo Schimmel return false; 633c71006dSIdo Schimmel return true; 643c71006dSIdo Schimmel } 653c71006dSIdo Schimmel EXPORT_SYMBOL_GPL(fib4_rule_default); 663c71006dSIdo Schimmel 671b2a4440SIdo Schimmel int fib4_rules_dump(struct net *net, struct notifier_block *nb) 681b2a4440SIdo Schimmel { 691b2a4440SIdo Schimmel return fib_rules_dump(net, nb, AF_INET); 701b2a4440SIdo Schimmel } 711b2a4440SIdo Schimmel 721b2a4440SIdo Schimmel unsigned int fib4_rules_seq_read(struct net *net) 731b2a4440SIdo Schimmel { 741b2a4440SIdo Schimmel return fib_rules_seq_read(net, AF_INET); 751b2a4440SIdo Schimmel } 761b2a4440SIdo Schimmel 770eeb075fSAndy Gospodarek int __fib_lookup(struct net *net, struct flowi4 *flp, 780eeb075fSAndy Gospodarek struct fib_result *res, unsigned int flags) 79e1ef4bf2SThomas Graf { 80e1ef4bf2SThomas Graf struct fib_lookup_arg arg = { 81e1ef4bf2SThomas Graf .result = res, 820eeb075fSAndy Gospodarek .flags = flags, 83e1ef4bf2SThomas Graf }; 84e1ef4bf2SThomas Graf int err; 85e1ef4bf2SThomas Graf 869ee0034bSDavid Ahern /* update flow if oif or iif point to device enslaved to l3mdev */ 879ee0034bSDavid Ahern l3mdev_update_flow(net, flowi4_to_flowi(flp)); 889ee0034bSDavid Ahern 8922bd5b9bSDavid S. Miller err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg); 9085b91b03SDavid S. Miller #ifdef CONFIG_IP_ROUTE_CLASSID 9185b91b03SDavid S. Miller if (arg.rule) 9285b91b03SDavid S. Miller res->tclassid = ((struct fib4_rule *)arg.rule)->tclassid; 9385b91b03SDavid S. Miller else 9485b91b03SDavid S. Miller res->tclassid = 0; 9585b91b03SDavid S. Miller #endif 9649dd18baSPanu Matilainen 9749dd18baSPanu Matilainen if (err == -ESRCH) 9849dd18baSPanu Matilainen err = -ENETUNREACH; 9949dd18baSPanu Matilainen 1001da177e4SLinus Torvalds return err; 1011da177e4SLinus Torvalds } 102f4530fa5SDavid S. Miller EXPORT_SYMBOL_GPL(__fib_lookup); 1031da177e4SLinus Torvalds 1048ce11e6aSAdrian Bunk static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp, 1058ce11e6aSAdrian Bunk int flags, struct fib_lookup_arg *arg) 106e1ef4bf2SThomas Graf { 107e1ef4bf2SThomas Graf int err = -EAGAIN; 108e1ef4bf2SThomas Graf struct fib_table *tbl; 10996c63fa7SDavid Ahern u32 tb_id; 110e1ef4bf2SThomas Graf 111e1ef4bf2SThomas Graf switch (rule->action) { 112e1ef4bf2SThomas Graf case FR_ACT_TO_TBL: 113e1ef4bf2SThomas Graf break; 114e1ef4bf2SThomas Graf 115e1ef4bf2SThomas Graf case FR_ACT_UNREACHABLE: 116345e9b54SAlexander Duyck return -ENETUNREACH; 117e1ef4bf2SThomas Graf 118e1ef4bf2SThomas Graf case FR_ACT_PROHIBIT: 119345e9b54SAlexander Duyck return -EACCES; 120e1ef4bf2SThomas Graf 121e1ef4bf2SThomas Graf case FR_ACT_BLACKHOLE: 122e1ef4bf2SThomas Graf default: 123345e9b54SAlexander Duyck return -EINVAL; 124e1ef4bf2SThomas Graf } 125e1ef4bf2SThomas Graf 126345e9b54SAlexander Duyck rcu_read_lock(); 127e1ef4bf2SThomas Graf 12896c63fa7SDavid Ahern tb_id = fib_rule_get_table(rule, arg); 12996c63fa7SDavid Ahern tbl = fib_get_table(rule->fr_net, tb_id); 130345e9b54SAlexander Duyck if (tbl) 131345e9b54SAlexander Duyck err = fib_table_lookup(tbl, &flp->u.ip4, 132345e9b54SAlexander Duyck (struct fib_result *)arg->result, 133345e9b54SAlexander Duyck arg->flags); 134345e9b54SAlexander Duyck 135345e9b54SAlexander Duyck rcu_read_unlock(); 136e1ef4bf2SThomas Graf return err; 137e1ef4bf2SThomas Graf } 138e1ef4bf2SThomas Graf 1397764a45aSStefan Tomanek static bool fib4_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg) 1407764a45aSStefan Tomanek { 1416ef94cfaSStefan Tomanek struct fib_result *result = (struct fib_result *) arg->result; 142673498b8SStefan Tomanek struct net_device *dev = NULL; 143673498b8SStefan Tomanek 144673498b8SStefan Tomanek if (result->fi) 145673498b8SStefan Tomanek dev = result->fi->fib_dev; 1466ef94cfaSStefan Tomanek 1477764a45aSStefan Tomanek /* do not accept result if the route does 1487764a45aSStefan Tomanek * not meet the required prefix length 1497764a45aSStefan Tomanek */ 15073f5698eSStefan Tomanek if (result->prefixlen <= rule->suppress_prefixlen) 1516ef94cfaSStefan Tomanek goto suppress_route; 1526ef94cfaSStefan Tomanek 1536ef94cfaSStefan Tomanek /* do not accept result if the route uses a device 1546ef94cfaSStefan Tomanek * belonging to a forbidden interface group 1556ef94cfaSStefan Tomanek */ 1566ef94cfaSStefan Tomanek if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup) 1576ef94cfaSStefan Tomanek goto suppress_route; 1586ef94cfaSStefan Tomanek 1596ef94cfaSStefan Tomanek return false; 1606ef94cfaSStefan Tomanek 1616ef94cfaSStefan Tomanek suppress_route: 1627764a45aSStefan Tomanek if (!(arg->flags & FIB_LOOKUP_NOREF)) 1637764a45aSStefan Tomanek fib_info_put(result->fi); 1647764a45aSStefan Tomanek return true; 1657764a45aSStefan Tomanek } 166e1ef4bf2SThomas Graf 167e1ef4bf2SThomas Graf static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) 168e1ef4bf2SThomas Graf { 169e1ef4bf2SThomas Graf struct fib4_rule *r = (struct fib4_rule *) rule; 1709ade2286SDavid S. Miller struct flowi4 *fl4 = &fl->u.ip4; 1719ade2286SDavid S. Miller __be32 daddr = fl4->daddr; 1729ade2286SDavid S. Miller __be32 saddr = fl4->saddr; 173e1ef4bf2SThomas Graf 174e1ef4bf2SThomas Graf if (((saddr ^ r->src) & r->srcmask) || 175e1ef4bf2SThomas Graf ((daddr ^ r->dst) & r->dstmask)) 176e1ef4bf2SThomas Graf return 0; 177e1ef4bf2SThomas Graf 1789ade2286SDavid S. Miller if (r->tos && (r->tos != fl4->flowi4_tos)) 179e1ef4bf2SThomas Graf return 0; 180e1ef4bf2SThomas Graf 1814a2d73a4SRoopa Prabhu if (rule->ip_proto && (rule->ip_proto != fl4->flowi4_proto)) 1824a2d73a4SRoopa Prabhu return 0; 1834a2d73a4SRoopa Prabhu 1844a2d73a4SRoopa Prabhu if (fib_rule_port_range_set(&rule->sport_range) && 1854a2d73a4SRoopa Prabhu !fib_rule_port_inrange(&rule->sport_range, fl4->fl4_sport)) 1864a2d73a4SRoopa Prabhu return 0; 1874a2d73a4SRoopa Prabhu 1884a2d73a4SRoopa Prabhu if (fib_rule_port_range_set(&rule->dport_range) && 1894a2d73a4SRoopa Prabhu !fib_rule_port_inrange(&rule->dport_range, fl4->fl4_dport)) 1904a2d73a4SRoopa Prabhu return 0; 1914a2d73a4SRoopa Prabhu 192e1ef4bf2SThomas Graf return 1; 193e1ef4bf2SThomas Graf } 1941da177e4SLinus Torvalds 1958ad4942cSDenis V. Lunev static struct fib_table *fib_empty_table(struct net *net) 1961da177e4SLinus Torvalds { 19758075ff5SYueHaibing u32 id = 1; 1981da177e4SLinus Torvalds 19958075ff5SYueHaibing while (1) { 20051456b29SIan Morris if (!fib_get_table(net, id)) 2018ad4942cSDenis V. Lunev return fib_new_table(net, id); 20258075ff5SYueHaibing 20358075ff5SYueHaibing if (id++ == RT_TABLE_MAX) 20458075ff5SYueHaibing break; 20558075ff5SYueHaibing } 2061da177e4SLinus Torvalds return NULL; 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds 209ef7c79edSPatrick McHardy static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = { 2101f6c9557SThomas Graf FRA_GENERIC_POLICY, 211e1ef4bf2SThomas Graf [FRA_FLOW] = { .type = NLA_U32 }, 2121da177e4SLinus Torvalds }; 2131da177e4SLinus Torvalds 214e1ef4bf2SThomas Graf static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb, 2158b3521eeSRami Rosen struct fib_rule_hdr *frh, 216b16fb418SRoopa Prabhu struct nlattr **tb, 217b16fb418SRoopa Prabhu struct netlink_ext_ack *extack) 2181da177e4SLinus Torvalds { 2193b1e0a65SYOSHIFUJI Hideaki struct net *net = sock_net(skb->sk); 220e1ef4bf2SThomas Graf int err = -EINVAL; 221e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 2221da177e4SLinus Torvalds 223b16fb418SRoopa Prabhu if (frh->tos & ~IPTOS_TOS_MASK) { 224b16fb418SRoopa Prabhu NL_SET_ERR_MSG(extack, "Invalid tos"); 225e1ef4bf2SThomas Graf goto errout; 226b16fb418SRoopa Prabhu } 227e1ef4bf2SThomas Graf 2280ddcf43dSAlexander Duyck /* split local/main if they are not already split */ 2290ddcf43dSAlexander Duyck err = fib_unmerge(net); 2300ddcf43dSAlexander Duyck if (err) 2310ddcf43dSAlexander Duyck goto errout; 2320ddcf43dSAlexander Duyck 23396c63fa7SDavid Ahern if (rule->table == RT_TABLE_UNSPEC && !rule->l3mdev) { 234e1ef4bf2SThomas Graf if (rule->action == FR_ACT_TO_TBL) { 235e1ef4bf2SThomas Graf struct fib_table *table; 236e1ef4bf2SThomas Graf 237e4e4971cSDenis V. Lunev table = fib_empty_table(net); 23851456b29SIan Morris if (!table) { 239e1ef4bf2SThomas Graf err = -ENOBUFS; 240e1ef4bf2SThomas Graf goto errout; 241e1ef4bf2SThomas Graf } 242e1ef4bf2SThomas Graf 243e1ef4bf2SThomas Graf rule->table = table->tb_id; 244e1ef4bf2SThomas Graf } 245e1ef4bf2SThomas Graf } 246e1ef4bf2SThomas Graf 247e1701c68SThomas Graf if (frh->src_len) 24867b61f6cSJiri Benc rule4->src = nla_get_in_addr(tb[FRA_SRC]); 249e1ef4bf2SThomas Graf 250e1701c68SThomas Graf if (frh->dst_len) 25167b61f6cSJiri Benc rule4->dst = nla_get_in_addr(tb[FRA_DST]); 252e1ef4bf2SThomas Graf 253c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 2547a9bc9b8SDavid S. Miller if (tb[FRA_FLOW]) { 255e1ef4bf2SThomas Graf rule4->tclassid = nla_get_u32(tb[FRA_FLOW]); 2567a9bc9b8SDavid S. Miller if (rule4->tclassid) 257f4530fa5SDavid S. Miller net->ipv4.fib_num_tclassid_users++; 2587a9bc9b8SDavid S. Miller } 2591da177e4SLinus Torvalds #endif 2601da177e4SLinus Torvalds 261e37b1e97SRoopa Prabhu if (fib_rule_requires_fldissect(rule)) 262e37b1e97SRoopa Prabhu net->ipv4.fib_rules_require_fldissect++; 263e37b1e97SRoopa Prabhu 264e1ef4bf2SThomas Graf rule4->src_len = frh->src_len; 265e1ef4bf2SThomas Graf rule4->srcmask = inet_make_mask(rule4->src_len); 266e1ef4bf2SThomas Graf rule4->dst_len = frh->dst_len; 267e1ef4bf2SThomas Graf rule4->dstmask = inet_make_mask(rule4->dst_len); 268e1ef4bf2SThomas Graf rule4->tos = frh->tos; 269e1ef4bf2SThomas Graf 270f4530fa5SDavid S. Miller net->ipv4.fib_has_custom_rules = true; 271104616e7SScott Feldman 272e1ef4bf2SThomas Graf err = 0; 273e1ef4bf2SThomas Graf errout: 274e1ef4bf2SThomas Graf return err; 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds 2770ddcf43dSAlexander Duyck static int fib4_rule_delete(struct fib_rule *rule) 2787a9bc9b8SDavid S. Miller { 279f4530fa5SDavid S. Miller struct net *net = rule->fr_net; 2800ddcf43dSAlexander Duyck int err; 2817a9bc9b8SDavid S. Miller 2820ddcf43dSAlexander Duyck /* split local/main if they are not already split */ 2830ddcf43dSAlexander Duyck err = fib_unmerge(net); 2840ddcf43dSAlexander Duyck if (err) 2850ddcf43dSAlexander Duyck goto errout; 2860ddcf43dSAlexander Duyck 2870ddcf43dSAlexander Duyck #ifdef CONFIG_IP_ROUTE_CLASSID 2880ddcf43dSAlexander Duyck if (((struct fib4_rule *)rule)->tclassid) 289f4530fa5SDavid S. Miller net->ipv4.fib_num_tclassid_users--; 2907a9bc9b8SDavid S. Miller #endif 291f4530fa5SDavid S. Miller net->ipv4.fib_has_custom_rules = true; 292e37b1e97SRoopa Prabhu 293e37b1e97SRoopa Prabhu if (net->ipv4.fib_rules_require_fldissect && 294e37b1e97SRoopa Prabhu fib_rule_requires_fldissect(rule)) 295e37b1e97SRoopa Prabhu net->ipv4.fib_rules_require_fldissect--; 2960ddcf43dSAlexander Duyck errout: 2970ddcf43dSAlexander Duyck return err; 2987a9bc9b8SDavid S. Miller } 2997a9bc9b8SDavid S. Miller 300e1ef4bf2SThomas Graf static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, 301e1ef4bf2SThomas Graf struct nlattr **tb) 302a5cdc030SPatrick McHardy { 303e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 304a5cdc030SPatrick McHardy 305e1ef4bf2SThomas Graf if (frh->src_len && (rule4->src_len != frh->src_len)) 306e1ef4bf2SThomas Graf return 0; 307e1ef4bf2SThomas Graf 308e1ef4bf2SThomas Graf if (frh->dst_len && (rule4->dst_len != frh->dst_len)) 309e1ef4bf2SThomas Graf return 0; 310e1ef4bf2SThomas Graf 311e1ef4bf2SThomas Graf if (frh->tos && (rule4->tos != frh->tos)) 312e1ef4bf2SThomas Graf return 0; 313e1ef4bf2SThomas Graf 314c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 315e1ef4bf2SThomas Graf if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW]))) 316e1ef4bf2SThomas Graf return 0; 317e1ef4bf2SThomas Graf #endif 318e1ef4bf2SThomas Graf 31967b61f6cSJiri Benc if (frh->src_len && (rule4->src != nla_get_in_addr(tb[FRA_SRC]))) 320e1ef4bf2SThomas Graf return 0; 321e1ef4bf2SThomas Graf 32267b61f6cSJiri Benc if (frh->dst_len && (rule4->dst != nla_get_in_addr(tb[FRA_DST]))) 323e1ef4bf2SThomas Graf return 0; 324e1ef4bf2SThomas Graf 325e1ef4bf2SThomas Graf return 1; 326a5cdc030SPatrick McHardy } 327a5cdc030SPatrick McHardy 328e1ef4bf2SThomas Graf static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb, 32904af8cf6SRami Rosen struct fib_rule_hdr *frh) 3301da177e4SLinus Torvalds { 331e1ef4bf2SThomas Graf struct fib4_rule *rule4 = (struct fib4_rule *) rule; 3321da177e4SLinus Torvalds 333e1ef4bf2SThomas Graf frh->dst_len = rule4->dst_len; 334e1ef4bf2SThomas Graf frh->src_len = rule4->src_len; 335e1ef4bf2SThomas Graf frh->tos = rule4->tos; 3361da177e4SLinus Torvalds 337f3756b79SDavid S. Miller if ((rule4->dst_len && 338930345eaSJiri Benc nla_put_in_addr(skb, FRA_DST, rule4->dst)) || 339f3756b79SDavid S. Miller (rule4->src_len && 340930345eaSJiri Benc nla_put_in_addr(skb, FRA_SRC, rule4->src))) 341f3756b79SDavid S. Miller goto nla_put_failure; 342c7066f70SPatrick McHardy #ifdef CONFIG_IP_ROUTE_CLASSID 343f3756b79SDavid S. Miller if (rule4->tclassid && 344f3756b79SDavid S. Miller nla_put_u32(skb, FRA_FLOW, rule4->tclassid)) 345f3756b79SDavid S. Miller goto nla_put_failure; 346e1ef4bf2SThomas Graf #endif 347e1ef4bf2SThomas Graf return 0; 348e1ef4bf2SThomas Graf 349e1ef4bf2SThomas Graf nla_put_failure: 350e1ef4bf2SThomas Graf return -ENOBUFS; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds 353339bf98fSThomas Graf static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule) 354339bf98fSThomas Graf { 355339bf98fSThomas Graf return nla_total_size(4) /* dst */ 356339bf98fSThomas Graf + nla_total_size(4) /* src */ 357339bf98fSThomas Graf + nla_total_size(4); /* flow */ 358339bf98fSThomas Graf } 359339bf98fSThomas Graf 360ae299fc0SDenis V. Lunev static void fib4_rule_flush_cache(struct fib_rules_ops *ops) 36173417f61SThomas Graf { 362bafa6d9dSNicolas Dichtel rt_cache_flush(ops->fro_net); 36373417f61SThomas Graf } 36473417f61SThomas Graf 36504a6f82cSAndi Kleen static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = { 36625239ceeSPatrick McHardy .family = AF_INET, 367e1ef4bf2SThomas Graf .rule_size = sizeof(struct fib4_rule), 368e1701c68SThomas Graf .addr_size = sizeof(u32), 369e1ef4bf2SThomas Graf .action = fib4_rule_action, 3707764a45aSStefan Tomanek .suppress = fib4_rule_suppress, 371e1ef4bf2SThomas Graf .match = fib4_rule_match, 372e1ef4bf2SThomas Graf .configure = fib4_rule_configure, 3737a9bc9b8SDavid S. Miller .delete = fib4_rule_delete, 374e1ef4bf2SThomas Graf .compare = fib4_rule_compare, 375e1ef4bf2SThomas Graf .fill = fib4_rule_fill, 376339bf98fSThomas Graf .nlmsg_payload = fib4_rule_nlmsg_payload, 37773417f61SThomas Graf .flush_cache = fib4_rule_flush_cache, 378e1ef4bf2SThomas Graf .nlgroup = RTNLGRP_IPV4_RULE, 379e1ef4bf2SThomas Graf .policy = fib4_rule_policy, 380e1ef4bf2SThomas Graf .owner = THIS_MODULE, 381e1ef4bf2SThomas Graf }; 382e1ef4bf2SThomas Graf 383e4e4971cSDenis V. Lunev static int fib_default_rules_init(struct fib_rules_ops *ops) 3842994c638SDenis V. Lunev { 3852994c638SDenis V. Lunev int err; 3862994c638SDenis V. Lunev 3875adef180SPatrick McHardy err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0); 3882994c638SDenis V. Lunev if (err < 0) 3892994c638SDenis V. Lunev return err; 390e4e4971cSDenis V. Lunev err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0); 3912994c638SDenis V. Lunev if (err < 0) 3922994c638SDenis V. Lunev return err; 393e4e4971cSDenis V. Lunev err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0); 3942994c638SDenis V. Lunev if (err < 0) 3952994c638SDenis V. Lunev return err; 3962994c638SDenis V. Lunev return 0; 3972994c638SDenis V. Lunev } 3982994c638SDenis V. Lunev 3997b1a74fdSDenis V. Lunev int __net_init fib4_rules_init(struct net *net) 400e1ef4bf2SThomas Graf { 401dbb50165SDenis V. Lunev int err; 402e4e4971cSDenis V. Lunev struct fib_rules_ops *ops; 403dbb50165SDenis V. Lunev 404e9c5158aSEric W. Biederman ops = fib_rules_register(&fib4_rules_ops_template, net); 405e9c5158aSEric W. Biederman if (IS_ERR(ops)) 406e9c5158aSEric W. Biederman return PTR_ERR(ops); 407e4e4971cSDenis V. Lunev 408e4e4971cSDenis V. Lunev err = fib_default_rules_init(ops); 409dbb50165SDenis V. Lunev if (err < 0) 410dbb50165SDenis V. Lunev goto fail; 411e4e4971cSDenis V. Lunev net->ipv4.rules_ops = ops; 412f4530fa5SDavid S. Miller net->ipv4.fib_has_custom_rules = false; 413e37b1e97SRoopa Prabhu net->ipv4.fib_rules_require_fldissect = 0; 414dbb50165SDenis V. Lunev return 0; 415dbb50165SDenis V. Lunev 416dbb50165SDenis V. Lunev fail: 417dbb50165SDenis V. Lunev /* also cleans all rules already added */ 4189e3a5487SDenis V. Lunev fib_rules_unregister(ops); 419dbb50165SDenis V. Lunev return err; 420e1ef4bf2SThomas Graf } 4217b1a74fdSDenis V. Lunev 4227b1a74fdSDenis V. Lunev void __net_exit fib4_rules_exit(struct net *net) 4237b1a74fdSDenis V. Lunev { 4249e3a5487SDenis V. Lunev fib_rules_unregister(net->ipv4.rules_ops); 4257b1a74fdSDenis V. Lunev } 426