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; |
| 43 | u32 src; |
| 44 | u32 srcmask; |
| 45 | u32 dst; |
| 46 | u32 dstmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #ifdef CONFIG_IP_ROUTE_FWMARK |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 48 | u32 fwmark; |
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 | #ifdef CONFIG_NET_CLS_ROUTE |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 51 | u32 tclassid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 55 | static struct fib4_rule default_rule = { |
| 56 | .common = { |
| 57 | .refcnt = ATOMIC_INIT(2), |
| 58 | .pref = 0x7FFF, |
| 59 | .table = RT_TABLE_DEFAULT, |
| 60 | .action = FR_ACT_TO_TBL, |
| 61 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 64 | static struct fib4_rule main_rule = { |
| 65 | .common = { |
| 66 | .refcnt = ATOMIC_INIT(2), |
| 67 | .pref = 0x7FFE, |
| 68 | .table = RT_TABLE_MAIN, |
| 69 | .action = FR_ACT_TO_TBL, |
| 70 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 73 | static struct fib4_rule local_rule = { |
| 74 | .common = { |
| 75 | .refcnt = ATOMIC_INIT(2), |
| 76 | .table = RT_TABLE_LOCAL, |
| 77 | .action = FR_ACT_TO_TBL, |
| 78 | .flags = FIB_RULE_PERMANENT, |
| 79 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 82 | static LIST_HEAD(fib4_rules); |
Robert Olsson | 7b204af | 2006-03-20 17:18:53 -0800 | [diff] [blame] | 83 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 84 | #ifdef CONFIG_NET_CLS_ROUTE |
| 85 | u32 fib_rules_tclass(struct fib_result *res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 87 | return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0; |
| 88 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 91 | int fib_lookup(struct flowi *flp, struct fib_result *res) |
| 92 | { |
| 93 | struct fib_lookup_arg arg = { |
| 94 | .result = res, |
| 95 | }; |
| 96 | int err; |
| 97 | |
| 98 | err = fib_rules_lookup(&fib4_rules_ops, flp, 0, &arg); |
| 99 | res->r = arg.rule; |
| 100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | return err; |
| 102 | } |
| 103 | |
Adrian Bunk | 8ce11e6 | 2006-08-07 21:50:48 -0700 | [diff] [blame^] | 104 | static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp, |
| 105 | int flags, struct fib_lookup_arg *arg) |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 106 | { |
| 107 | int err = -EAGAIN; |
| 108 | struct fib_table *tbl; |
| 109 | |
| 110 | switch (rule->action) { |
| 111 | case FR_ACT_TO_TBL: |
| 112 | break; |
| 113 | |
| 114 | case FR_ACT_UNREACHABLE: |
| 115 | err = -ENETUNREACH; |
| 116 | goto errout; |
| 117 | |
| 118 | case FR_ACT_PROHIBIT: |
| 119 | err = -EACCES; |
| 120 | goto errout; |
| 121 | |
| 122 | case FR_ACT_BLACKHOLE: |
| 123 | default: |
| 124 | err = -EINVAL; |
| 125 | goto errout; |
| 126 | } |
| 127 | |
| 128 | if ((tbl = fib_get_table(rule->table)) == NULL) |
| 129 | goto errout; |
| 130 | |
| 131 | err = tbl->tb_lookup(tbl, flp, (struct fib_result *) arg->result); |
| 132 | if (err > 0) |
| 133 | err = -EAGAIN; |
| 134 | errout: |
| 135 | return err; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | void fib_select_default(const struct flowi *flp, struct fib_result *res) |
| 140 | { |
| 141 | if (res->r && res->r->action == FR_ACT_TO_TBL && |
| 142 | FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) { |
| 143 | struct fib_table *tb; |
| 144 | if ((tb = fib_get_table(res->r->table)) != NULL) |
| 145 | tb->tb_select_default(tb, flp, res); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) |
| 150 | { |
| 151 | struct fib4_rule *r = (struct fib4_rule *) rule; |
| 152 | u32 daddr = fl->fl4_dst; |
| 153 | u32 saddr = fl->fl4_src; |
| 154 | |
| 155 | if (((saddr ^ r->src) & r->srcmask) || |
| 156 | ((daddr ^ r->dst) & r->dstmask)) |
| 157 | return 0; |
| 158 | |
| 159 | if (r->tos && (r->tos != fl->fl4_tos)) |
| 160 | return 0; |
| 161 | |
| 162 | #ifdef CONFIG_IP_ROUTE_FWMARK |
| 163 | if (r->fwmark && (r->fwmark != fl->fl4_fwmark)) |
| 164 | return 0; |
| 165 | #endif |
| 166 | |
| 167 | return 1; |
| 168 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
| 170 | static struct fib_table *fib_empty_table(void) |
| 171 | { |
| 172 | int id; |
| 173 | |
| 174 | for (id = 1; id <= RT_TABLE_MAX; id++) |
| 175 | if (fib_tables[id] == NULL) |
| 176 | return __fib_new_table(id); |
| 177 | return NULL; |
| 178 | } |
| 179 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 180 | static struct nla_policy fib4_rule_policy[FRA_MAX+1] __read_mostly = { |
| 181 | [FRA_IFNAME] = { .type = NLA_STRING }, |
| 182 | [FRA_PRIORITY] = { .type = NLA_U32 }, |
| 183 | [FRA_SRC] = { .type = NLA_U32 }, |
| 184 | [FRA_DST] = { .type = NLA_U32 }, |
| 185 | [FRA_FWMARK] = { .type = NLA_U32 }, |
| 186 | [FRA_FLOW] = { .type = NLA_U32 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | }; |
| 188 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 189 | static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb, |
| 190 | struct nlmsghdr *nlh, struct fib_rule_hdr *frh, |
| 191 | struct nlattr **tb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 193 | int err = -EINVAL; |
| 194 | struct fib4_rule *rule4 = (struct fib4_rule *) rule; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 196 | if (frh->src_len > 32 || frh->dst_len > 32 || |
| 197 | (frh->tos & ~IPTOS_TOS_MASK)) |
| 198 | goto errout; |
| 199 | |
| 200 | if (rule->table == RT_TABLE_UNSPEC) { |
| 201 | if (rule->action == FR_ACT_TO_TBL) { |
| 202 | struct fib_table *table; |
| 203 | |
| 204 | table = fib_empty_table(); |
| 205 | if (table == NULL) { |
| 206 | err = -ENOBUFS; |
| 207 | goto errout; |
| 208 | } |
| 209 | |
| 210 | rule->table = table->tb_id; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | if (tb[FRA_SRC]) |
| 215 | rule4->src = nla_get_u32(tb[FRA_SRC]); |
| 216 | |
| 217 | if (tb[FRA_DST]) |
| 218 | rule4->dst = nla_get_u32(tb[FRA_DST]); |
| 219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | #ifdef CONFIG_IP_ROUTE_FWMARK |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 221 | if (tb[FRA_FWMARK]) |
| 222 | rule4->fwmark = nla_get_u32(tb[FRA_FWMARK]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | #ifdef CONFIG_NET_CLS_ROUTE |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 226 | if (tb[FRA_FLOW]) |
| 227 | rule4->tclassid = nla_get_u32(tb[FRA_FLOW]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 230 | rule4->src_len = frh->src_len; |
| 231 | rule4->srcmask = inet_make_mask(rule4->src_len); |
| 232 | rule4->dst_len = frh->dst_len; |
| 233 | rule4->dstmask = inet_make_mask(rule4->dst_len); |
| 234 | rule4->tos = frh->tos; |
| 235 | |
| 236 | err = 0; |
| 237 | errout: |
| 238 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 241 | static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, |
| 242 | struct nlattr **tb) |
Patrick McHardy | a5cdc03 | 2006-03-23 01:16:06 -0800 | [diff] [blame] | 243 | { |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 244 | struct fib4_rule *rule4 = (struct fib4_rule *) rule; |
Patrick McHardy | a5cdc03 | 2006-03-23 01:16:06 -0800 | [diff] [blame] | 245 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 246 | if (frh->src_len && (rule4->src_len != frh->src_len)) |
| 247 | return 0; |
| 248 | |
| 249 | if (frh->dst_len && (rule4->dst_len != frh->dst_len)) |
| 250 | return 0; |
| 251 | |
| 252 | if (frh->tos && (rule4->tos != frh->tos)) |
| 253 | return 0; |
| 254 | |
| 255 | #ifdef CONFIG_IP_ROUTE_FWMARK |
| 256 | if (tb[FRA_FWMARK] && (rule4->fwmark != nla_get_u32(tb[FRA_FWMARK]))) |
| 257 | return 0; |
| 258 | #endif |
| 259 | |
| 260 | #ifdef CONFIG_NET_CLS_ROUTE |
| 261 | if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW]))) |
| 262 | return 0; |
| 263 | #endif |
| 264 | |
| 265 | if (tb[FRA_SRC] && (rule4->src != nla_get_u32(tb[FRA_SRC]))) |
| 266 | return 0; |
| 267 | |
| 268 | if (tb[FRA_DST] && (rule4->dst != nla_get_u32(tb[FRA_DST]))) |
| 269 | return 0; |
| 270 | |
| 271 | return 1; |
| 272 | } |
| 273 | |
| 274 | static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb, |
| 275 | struct nlmsghdr *nlh, struct fib_rule_hdr *frh) |
| 276 | { |
| 277 | struct fib4_rule *rule4 = (struct fib4_rule *) rule; |
| 278 | |
| 279 | frh->family = AF_INET; |
| 280 | frh->dst_len = rule4->dst_len; |
| 281 | frh->src_len = rule4->src_len; |
| 282 | frh->tos = rule4->tos; |
| 283 | |
| 284 | #ifdef CONFIG_IP_ROUTE_FWMARK |
| 285 | if (rule4->fwmark) |
| 286 | NLA_PUT_U32(skb, FRA_FWMARK, rule4->fwmark); |
| 287 | #endif |
| 288 | |
| 289 | if (rule4->dst_len) |
| 290 | NLA_PUT_U32(skb, FRA_DST, rule4->dst); |
| 291 | |
| 292 | if (rule4->src_len) |
| 293 | NLA_PUT_U32(skb, FRA_SRC, rule4->src); |
| 294 | |
| 295 | #ifdef CONFIG_NET_CLS_ROUTE |
| 296 | if (rule4->tclassid) |
| 297 | NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid); |
| 298 | #endif |
| 299 | return 0; |
| 300 | |
| 301 | nla_put_failure: |
| 302 | return -ENOBUFS; |
| 303 | } |
| 304 | |
| 305 | int fib4_rules_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 306 | { |
| 307 | return fib_rules_dump(skb, cb, AF_INET); |
| 308 | } |
| 309 | |
| 310 | static u32 fib4_rule_default_pref(void) |
| 311 | { |
| 312 | struct list_head *pos; |
| 313 | struct fib_rule *rule; |
| 314 | |
| 315 | if (!list_empty(&fib4_rules)) { |
| 316 | pos = fib4_rules.next; |
| 317 | if (pos->next != &fib4_rules) { |
| 318 | rule = list_entry(pos->next, struct fib_rule, list); |
| 319 | if (rule->pref) |
| 320 | return rule->pref - 1; |
| 321 | } |
Patrick McHardy | a5cdc03 | 2006-03-23 01:16:06 -0800 | [diff] [blame] | 322 | } |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 323 | |
| 324 | return 0; |
Patrick McHardy | a5cdc03 | 2006-03-23 01:16:06 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 327 | static struct fib_rules_ops fib4_rules_ops = { |
| 328 | .family = AF_INET, |
| 329 | .rule_size = sizeof(struct fib4_rule), |
| 330 | .action = fib4_rule_action, |
| 331 | .match = fib4_rule_match, |
| 332 | .configure = fib4_rule_configure, |
| 333 | .compare = fib4_rule_compare, |
| 334 | .fill = fib4_rule_fill, |
| 335 | .default_pref = fib4_rule_default_pref, |
| 336 | .nlgroup = RTNLGRP_IPV4_RULE, |
| 337 | .policy = fib4_rule_policy, |
| 338 | .rules_list = &fib4_rules, |
| 339 | .owner = THIS_MODULE, |
| 340 | }; |
| 341 | |
| 342 | void __init fib4_rules_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | { |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 344 | list_add_tail(&local_rule.common.list, &fib4_rules); |
| 345 | list_add_tail(&main_rule.common.list, &fib4_rules); |
| 346 | list_add_tail(&default_rule.common.list, &fib4_rules); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 348 | fib_rules_register(&fib4_rules_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 351 | void __exit fib4_rules_cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | { |
Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 353 | fib_rules_unregister(&fib4_rules_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | } |