Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * INET An implementation of the TCP/IP protocol suite for the LINUX |
| 3 | * operating system. INET is implemented using the BSD Socket |
| 4 | * interface as the means of communication with the user level. |
| 5 | * |
| 6 | * IPv4 Forwarding Information Base: policy rules. |
| 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 9 | * Thomas Graf <tgraf@suug.ch> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License |
| 13 | * as published by the Free Software Foundation; either version |
| 14 | * 2 of the License, or (at your option) any later version. |
| 15 | * |
| 16 | * Fixes: |
| 17 | * Rani Assaf : local_rule cannot be deleted |
| 18 | * Marc Boucher : routing by fwmark |
| 19 | */ |
| 20 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/types.h> |
| 22 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/netdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/netlink.h> |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 25 | #include <linux/inetdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/init.h> |
Robert Olsson | 7b204af | 2006-03-20 17:18:53 -0800 | [diff] [blame] | 27 | #include <linux/list.h> |
| 28 | #include <linux/rcupdate.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <net/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <net/route.h> |
| 31 | #include <net/tcp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <net/ip_fib.h> |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 33 | #include <net/fib_rules.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 35 | static struct fib_rules_ops fib4_rules_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 37 | struct fib4_rule |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 39 | struct fib_rule common; |
| 40 | u8 dst_len; |
| 41 | u8 src_len; |
| 42 | u8 tos; |
Al Viro | 81f7bf6 | 2006-09-27 18:40:00 -0700 | [diff] [blame] | 43 | __be32 src; |
| 44 | __be32 srcmask; |
| 45 | __be32 dst; |
| 46 | __be32 dstmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #ifdef CONFIG_NET_CLS_ROUTE |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 48 | u32 tclassid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 52 | static struct fib4_rule default_rule = { |
| 53 | .common = { |
| 54 | .refcnt = ATOMIC_INIT(2), |
| 55 | .pref = 0x7FFF, |
| 56 | .table = RT_TABLE_DEFAULT, |
| 57 | .action = FR_ACT_TO_TBL, |
| 58 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 61 | static struct fib4_rule main_rule = { |
| 62 | .common = { |
| 63 | .refcnt = ATOMIC_INIT(2), |
| 64 | .pref = 0x7FFE, |
| 65 | .table = RT_TABLE_MAIN, |
| 66 | .action = FR_ACT_TO_TBL, |
| 67 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 70 | static struct fib4_rule local_rule = { |
| 71 | .common = { |
| 72 | .refcnt = ATOMIC_INIT(2), |
| 73 | .table = RT_TABLE_LOCAL, |
| 74 | .action = FR_ACT_TO_TBL, |
| 75 | .flags = FIB_RULE_PERMANENT, |
| 76 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 79 | static LIST_HEAD(fib4_rules); |
Robert Olsson | 7b204af | 2006-03-20 17:18:53 -0800 | [diff] [blame] | 80 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 81 | #ifdef CONFIG_NET_CLS_ROUTE |
| 82 | u32 fib_rules_tclass(struct fib_result *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | { |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 84 | return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0; |
| 85 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 88 | int fib_lookup(struct flowi *flp, struct fib_result *res) |
| 89 | { |
| 90 | struct fib_lookup_arg arg = { |
| 91 | .result = res, |
| 92 | }; |
| 93 | int err; |
| 94 | |
| 95 | err = fib_rules_lookup(&fib4_rules_ops, flp, 0, &arg); |
| 96 | res->r = arg.rule; |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | return err; |
| 99 | } |
| 100 | |
Adrian Bunk | 8ce11e6 | 2006-08-07 21:50:48 -0700 | [diff] [blame] | 101 | static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp, |
| 102 | int flags, struct fib_lookup_arg *arg) |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 103 | { |
| 104 | int err = -EAGAIN; |
| 105 | struct fib_table *tbl; |
| 106 | |
| 107 | switch (rule->action) { |
| 108 | case FR_ACT_TO_TBL: |
| 109 | break; |
| 110 | |
| 111 | case FR_ACT_UNREACHABLE: |
| 112 | err = -ENETUNREACH; |
| 113 | goto errout; |
| 114 | |
| 115 | case FR_ACT_PROHIBIT: |
| 116 | err = -EACCES; |
| 117 | goto errout; |
| 118 | |
| 119 | case FR_ACT_BLACKHOLE: |
| 120 | default: |
| 121 | err = -EINVAL; |
| 122 | goto errout; |
| 123 | } |
| 124 | |
| 125 | if ((tbl = fib_get_table(rule->table)) == NULL) |
| 126 | goto errout; |
| 127 | |
| 128 | err = tbl->tb_lookup(tbl, flp, (struct fib_result *) arg->result); |
| 129 | if (err > 0) |
| 130 | err = -EAGAIN; |
| 131 | errout: |
| 132 | return err; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | void fib_select_default(const struct flowi *flp, struct fib_result *res) |
| 137 | { |
| 138 | if (res->r && res->r->action == FR_ACT_TO_TBL && |
| 139 | FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) { |
| 140 | struct fib_table *tb; |
| 141 | if ((tb = fib_get_table(res->r->table)) != NULL) |
| 142 | tb->tb_select_default(tb, flp, res); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) |
| 147 | { |
| 148 | struct fib4_rule *r = (struct fib4_rule *) rule; |
Al Viro | 81f7bf6 | 2006-09-27 18:40:00 -0700 | [diff] [blame] | 149 | __be32 daddr = fl->fl4_dst; |
| 150 | __be32 saddr = fl->fl4_src; |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 151 | |
| 152 | if (((saddr ^ r->src) & r->srcmask) || |
| 153 | ((daddr ^ r->dst) & r->dstmask)) |
| 154 | return 0; |
| 155 | |
| 156 | if (r->tos && (r->tos != fl->fl4_tos)) |
| 157 | return 0; |
| 158 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 159 | return 1; |
| 160 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
| 162 | static struct fib_table *fib_empty_table(void) |
| 163 | { |
Patrick McHardy | 2dfe55b | 2006-08-10 23:08:33 -0700 | [diff] [blame] | 164 | u32 id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
| 166 | for (id = 1; id <= RT_TABLE_MAX; id++) |
Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 167 | if (fib_get_table(id) == NULL) |
| 168 | return fib_new_table(id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | return NULL; |
| 170 | } |
| 171 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 172 | static struct nla_policy fib4_rule_policy[FRA_MAX+1] __read_mostly = { |
Thomas Graf | 1f6c955 | 2006-11-09 15:22:48 -0800 | [diff] [blame] | 173 | FRA_GENERIC_POLICY, |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 174 | [FRA_FLOW] = { .type = NLA_U32 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | }; |
| 176 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 177 | static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb, |
| 178 | struct nlmsghdr *nlh, struct fib_rule_hdr *frh, |
| 179 | struct nlattr **tb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 181 | int err = -EINVAL; |
| 182 | struct fib4_rule *rule4 = (struct fib4_rule *) rule; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Thomas Graf | e1701c6 | 2007-03-24 12:46:02 -0700 | [diff] [blame^] | 184 | if (frh->tos & ~IPTOS_TOS_MASK) |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 185 | goto errout; |
| 186 | |
| 187 | if (rule->table == RT_TABLE_UNSPEC) { |
| 188 | if (rule->action == FR_ACT_TO_TBL) { |
| 189 | struct fib_table *table; |
| 190 | |
| 191 | table = fib_empty_table(); |
| 192 | if (table == NULL) { |
| 193 | err = -ENOBUFS; |
| 194 | goto errout; |
| 195 | } |
| 196 | |
| 197 | rule->table = table->tb_id; |
| 198 | } |
| 199 | } |
| 200 | |
Thomas Graf | e1701c6 | 2007-03-24 12:46:02 -0700 | [diff] [blame^] | 201 | if (frh->src_len) |
Al Viro | 45d60b9 | 2006-09-27 18:40:27 -0700 | [diff] [blame] | 202 | rule4->src = nla_get_be32(tb[FRA_SRC]); |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 203 | |
Thomas Graf | e1701c6 | 2007-03-24 12:46:02 -0700 | [diff] [blame^] | 204 | if (frh->dst_len) |
Al Viro | 45d60b9 | 2006-09-27 18:40:27 -0700 | [diff] [blame] | 205 | rule4->dst = nla_get_be32(tb[FRA_DST]); |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | #ifdef CONFIG_NET_CLS_ROUTE |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 208 | if (tb[FRA_FLOW]) |
| 209 | rule4->tclassid = nla_get_u32(tb[FRA_FLOW]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 212 | rule4->src_len = frh->src_len; |
| 213 | rule4->srcmask = inet_make_mask(rule4->src_len); |
| 214 | rule4->dst_len = frh->dst_len; |
| 215 | rule4->dstmask = inet_make_mask(rule4->dst_len); |
| 216 | rule4->tos = frh->tos; |
| 217 | |
| 218 | err = 0; |
| 219 | errout: |
| 220 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 223 | static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, |
| 224 | struct nlattr **tb) |
Patrick McHardy | a5cdc03 | 2006-03-23 01:16:06 -0800 | [diff] [blame] | 225 | { |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 226 | struct fib4_rule *rule4 = (struct fib4_rule *) rule; |
Patrick McHardy | a5cdc03 | 2006-03-23 01:16:06 -0800 | [diff] [blame] | 227 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 228 | if (frh->src_len && (rule4->src_len != frh->src_len)) |
| 229 | return 0; |
| 230 | |
| 231 | if (frh->dst_len && (rule4->dst_len != frh->dst_len)) |
| 232 | return 0; |
| 233 | |
| 234 | if (frh->tos && (rule4->tos != frh->tos)) |
| 235 | return 0; |
| 236 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 237 | #ifdef CONFIG_NET_CLS_ROUTE |
| 238 | if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW]))) |
| 239 | return 0; |
| 240 | #endif |
| 241 | |
Thomas Graf | e1701c6 | 2007-03-24 12:46:02 -0700 | [diff] [blame^] | 242 | if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC]))) |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 243 | return 0; |
| 244 | |
Thomas Graf | e1701c6 | 2007-03-24 12:46:02 -0700 | [diff] [blame^] | 245 | if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST]))) |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 246 | return 0; |
| 247 | |
| 248 | return 1; |
| 249 | } |
| 250 | |
| 251 | static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb, |
| 252 | struct nlmsghdr *nlh, struct fib_rule_hdr *frh) |
| 253 | { |
| 254 | struct fib4_rule *rule4 = (struct fib4_rule *) rule; |
| 255 | |
| 256 | frh->family = AF_INET; |
| 257 | frh->dst_len = rule4->dst_len; |
| 258 | frh->src_len = rule4->src_len; |
| 259 | frh->tos = rule4->tos; |
| 260 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 261 | if (rule4->dst_len) |
Al Viro | 45d60b9 | 2006-09-27 18:40:27 -0700 | [diff] [blame] | 262 | NLA_PUT_BE32(skb, FRA_DST, rule4->dst); |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 263 | |
| 264 | if (rule4->src_len) |
Al Viro | 45d60b9 | 2006-09-27 18:40:27 -0700 | [diff] [blame] | 265 | NLA_PUT_BE32(skb, FRA_SRC, rule4->src); |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 266 | |
| 267 | #ifdef CONFIG_NET_CLS_ROUTE |
| 268 | if (rule4->tclassid) |
| 269 | NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid); |
| 270 | #endif |
| 271 | return 0; |
| 272 | |
| 273 | nla_put_failure: |
| 274 | return -ENOBUFS; |
| 275 | } |
| 276 | |
| 277 | int fib4_rules_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 278 | { |
| 279 | return fib_rules_dump(skb, cb, AF_INET); |
| 280 | } |
| 281 | |
| 282 | static u32 fib4_rule_default_pref(void) |
| 283 | { |
| 284 | struct list_head *pos; |
| 285 | struct fib_rule *rule; |
| 286 | |
| 287 | if (!list_empty(&fib4_rules)) { |
| 288 | pos = fib4_rules.next; |
| 289 | if (pos->next != &fib4_rules) { |
| 290 | rule = list_entry(pos->next, struct fib_rule, list); |
| 291 | if (rule->pref) |
| 292 | return rule->pref - 1; |
| 293 | } |
Patrick McHardy | a5cdc03 | 2006-03-23 01:16:06 -0800 | [diff] [blame] | 294 | } |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 295 | |
| 296 | return 0; |
Patrick McHardy | a5cdc03 | 2006-03-23 01:16:06 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 299 | static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule) |
| 300 | { |
| 301 | return nla_total_size(4) /* dst */ |
| 302 | + nla_total_size(4) /* src */ |
| 303 | + nla_total_size(4); /* flow */ |
| 304 | } |
| 305 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 306 | static struct fib_rules_ops fib4_rules_ops = { |
| 307 | .family = AF_INET, |
| 308 | .rule_size = sizeof(struct fib4_rule), |
Thomas Graf | e1701c6 | 2007-03-24 12:46:02 -0700 | [diff] [blame^] | 309 | .addr_size = sizeof(u32), |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 310 | .action = fib4_rule_action, |
| 311 | .match = fib4_rule_match, |
| 312 | .configure = fib4_rule_configure, |
| 313 | .compare = fib4_rule_compare, |
| 314 | .fill = fib4_rule_fill, |
| 315 | .default_pref = fib4_rule_default_pref, |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 316 | .nlmsg_payload = fib4_rule_nlmsg_payload, |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 317 | .nlgroup = RTNLGRP_IPV4_RULE, |
| 318 | .policy = fib4_rule_policy, |
| 319 | .rules_list = &fib4_rules, |
| 320 | .owner = THIS_MODULE, |
| 321 | }; |
| 322 | |
| 323 | void __init fib4_rules_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 325 | list_add_tail(&local_rule.common.list, &fib4_rules); |
| 326 | list_add_tail(&main_rule.common.list, &fib4_rules); |
| 327 | list_add_tail(&default_rule.common.list, &fib4_rules); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 329 | fib_rules_register(&fib4_rules_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |