diff options
Diffstat (limited to 'net/ipv4/fib_rules.c')
| -rw-r--r-- | net/ipv4/fib_rules.c | 622 |
1 files changed, 253 insertions, 369 deletions
diff --git a/net/ipv4/fib_rules.c b/net/ipv4/fib_rules.c index ec566f3e66c7..0852b9cd065a 100644 --- a/net/ipv4/fib_rules.c +++ b/net/ipv4/fib_rules.c | |||
| @@ -5,9 +5,8 @@ | |||
| 5 | * | 5 | * |
| 6 | * IPv4 Forwarding Information Base: policy rules. | 6 | * IPv4 Forwarding Information Base: policy rules. |
| 7 | * | 7 | * |
| 8 | * Version: $Id: fib_rules.c,v 1.17 2001/10/31 21:55:54 davem Exp $ | ||
| 9 | * | ||
| 10 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | 8 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 9 | * Thomas Graf <tgraf@suug.ch> | ||
| 11 | * | 10 | * |
| 12 | * This program is free software; you can redistribute it and/or | 11 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License | 12 | * modify it under the terms of the GNU General Public License |
| @@ -19,465 +18,350 @@ | |||
| 19 | * Marc Boucher : routing by fwmark | 18 | * Marc Boucher : routing by fwmark |
| 20 | */ | 19 | */ |
| 21 | 20 | ||
| 22 | #include <linux/config.h> | ||
| 23 | #include <asm/uaccess.h> | ||
| 24 | #include <asm/system.h> | ||
| 25 | #include <linux/bitops.h> | ||
| 26 | #include <linux/types.h> | 21 | #include <linux/types.h> |
| 27 | #include <linux/kernel.h> | 22 | #include <linux/kernel.h> |
| 28 | #include <linux/sched.h> | ||
| 29 | #include <linux/mm.h> | ||
| 30 | #include <linux/string.h> | ||
| 31 | #include <linux/socket.h> | ||
| 32 | #include <linux/sockios.h> | ||
| 33 | #include <linux/errno.h> | ||
| 34 | #include <linux/in.h> | ||
| 35 | #include <linux/inet.h> | ||
| 36 | #include <linux/inetdevice.h> | ||
| 37 | #include <linux/netdevice.h> | 23 | #include <linux/netdevice.h> |
| 38 | #include <linux/if_arp.h> | ||
| 39 | #include <linux/proc_fs.h> | ||
| 40 | #include <linux/skbuff.h> | ||
| 41 | #include <linux/netlink.h> | 24 | #include <linux/netlink.h> |
| 25 | #include <linux/inetdevice.h> | ||
| 42 | #include <linux/init.h> | 26 | #include <linux/init.h> |
| 43 | #include <linux/list.h> | 27 | #include <linux/list.h> |
| 44 | #include <linux/rcupdate.h> | 28 | #include <linux/rcupdate.h> |
| 45 | |||
| 46 | #include <net/ip.h> | 29 | #include <net/ip.h> |
| 47 | #include <net/protocol.h> | ||
| 48 | #include <net/route.h> | 30 | #include <net/route.h> |
| 49 | #include <net/tcp.h> | 31 | #include <net/tcp.h> |
| 50 | #include <net/sock.h> | ||
| 51 | #include <net/ip_fib.h> | 32 | #include <net/ip_fib.h> |
| 33 | #include <net/fib_rules.h> | ||
| 52 | 34 | ||
| 53 | #define FRprintk(a...) | 35 | static struct fib_rules_ops fib4_rules_ops; |
| 54 | 36 | ||
| 55 | struct fib_rule | 37 | struct fib4_rule |
| 56 | { | 38 | { |
| 57 | struct hlist_node hlist; | 39 | struct fib_rule common; |
| 58 | atomic_t r_clntref; | 40 | u8 dst_len; |
| 59 | u32 r_preference; | 41 | u8 src_len; |
| 60 | unsigned char r_table; | 42 | u8 tos; |
| 61 | unsigned char r_action; | 43 | __be32 src; |
| 62 | unsigned char r_dst_len; | 44 | __be32 srcmask; |
| 63 | unsigned char r_src_len; | 45 | __be32 dst; |
| 64 | u32 r_src; | 46 | __be32 dstmask; |
| 65 | u32 r_srcmask; | ||
| 66 | u32 r_dst; | ||
| 67 | u32 r_dstmask; | ||
| 68 | u32 r_srcmap; | ||
| 69 | u8 r_flags; | ||
| 70 | u8 r_tos; | ||
| 71 | #ifdef CONFIG_IP_ROUTE_FWMARK | 47 | #ifdef CONFIG_IP_ROUTE_FWMARK |
| 72 | u32 r_fwmark; | 48 | u32 fwmark; |
| 49 | u32 fwmask; | ||
| 73 | #endif | 50 | #endif |
| 74 | int r_ifindex; | ||
| 75 | #ifdef CONFIG_NET_CLS_ROUTE | 51 | #ifdef CONFIG_NET_CLS_ROUTE |
| 76 | __u32 r_tclassid; | 52 | u32 tclassid; |
| 77 | #endif | 53 | #endif |
| 78 | char r_ifname[IFNAMSIZ]; | ||
| 79 | int r_dead; | ||
| 80 | struct rcu_head rcu; | ||
| 81 | }; | 54 | }; |
| 82 | 55 | ||
| 83 | static struct fib_rule default_rule = { | 56 | static struct fib4_rule default_rule = { |
| 84 | .r_clntref = ATOMIC_INIT(2), | 57 | .common = { |
| 85 | .r_preference = 0x7FFF, | 58 | .refcnt = ATOMIC_INIT(2), |
| 86 | .r_table = RT_TABLE_DEFAULT, | 59 | .pref = 0x7FFF, |
| 87 | .r_action = RTN_UNICAST, | 60 | .table = RT_TABLE_DEFAULT, |
| 61 | .action = FR_ACT_TO_TBL, | ||
| 62 | }, | ||
| 88 | }; | 63 | }; |
| 89 | 64 | ||
| 90 | static struct fib_rule main_rule = { | 65 | static struct fib4_rule main_rule = { |
| 91 | .r_clntref = ATOMIC_INIT(2), | 66 | .common = { |
| 92 | .r_preference = 0x7FFE, | 67 | .refcnt = ATOMIC_INIT(2), |
| 93 | .r_table = RT_TABLE_MAIN, | 68 | .pref = 0x7FFE, |
| 94 | .r_action = RTN_UNICAST, | 69 | .table = RT_TABLE_MAIN, |
| 70 | .action = FR_ACT_TO_TBL, | ||
| 71 | }, | ||
| 95 | }; | 72 | }; |
| 96 | 73 | ||
| 97 | static struct fib_rule local_rule = { | 74 | static struct fib4_rule local_rule = { |
| 98 | .r_clntref = ATOMIC_INIT(2), | 75 | .common = { |
| 99 | .r_table = RT_TABLE_LOCAL, | 76 | .refcnt = ATOMIC_INIT(2), |
| 100 | .r_action = RTN_UNICAST, | 77 | .table = RT_TABLE_LOCAL, |
| 78 | .action = FR_ACT_TO_TBL, | ||
| 79 | .flags = FIB_RULE_PERMANENT, | ||
| 80 | }, | ||
| 101 | }; | 81 | }; |
| 102 | 82 | ||
| 103 | static struct hlist_head fib_rules; | 83 | static LIST_HEAD(fib4_rules); |
| 104 | 84 | ||
| 105 | /* writer func called from netlink -- rtnl_sem hold*/ | 85 | #ifdef CONFIG_NET_CLS_ROUTE |
| 106 | 86 | u32 fib_rules_tclass(struct fib_result *res) | |
| 107 | static void rtmsg_rule(int, struct fib_rule *); | ||
| 108 | |||
| 109 | int inet_rtm_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) | ||
| 110 | { | 87 | { |
| 111 | struct rtattr **rta = arg; | 88 | return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0; |
| 112 | struct rtmsg *rtm = NLMSG_DATA(nlh); | ||
| 113 | struct fib_rule *r; | ||
| 114 | struct hlist_node *node; | ||
| 115 | int err = -ESRCH; | ||
| 116 | |||
| 117 | hlist_for_each_entry(r, node, &fib_rules, hlist) { | ||
| 118 | if ((!rta[RTA_SRC-1] || memcmp(RTA_DATA(rta[RTA_SRC-1]), &r->r_src, 4) == 0) && | ||
| 119 | rtm->rtm_src_len == r->r_src_len && | ||
| 120 | rtm->rtm_dst_len == r->r_dst_len && | ||
| 121 | (!rta[RTA_DST-1] || memcmp(RTA_DATA(rta[RTA_DST-1]), &r->r_dst, 4) == 0) && | ||
| 122 | rtm->rtm_tos == r->r_tos && | ||
| 123 | #ifdef CONFIG_IP_ROUTE_FWMARK | ||
| 124 | (!rta[RTA_PROTOINFO-1] || memcmp(RTA_DATA(rta[RTA_PROTOINFO-1]), &r->r_fwmark, 4) == 0) && | ||
| 125 | #endif | ||
| 126 | (!rtm->rtm_type || rtm->rtm_type == r->r_action) && | ||
| 127 | (!rta[RTA_PRIORITY-1] || memcmp(RTA_DATA(rta[RTA_PRIORITY-1]), &r->r_preference, 4) == 0) && | ||
| 128 | (!rta[RTA_IIF-1] || rtattr_strcmp(rta[RTA_IIF-1], r->r_ifname) == 0) && | ||
| 129 | (!rtm->rtm_table || (r && rtm->rtm_table == r->r_table))) { | ||
| 130 | err = -EPERM; | ||
| 131 | if (r == &local_rule) | ||
| 132 | break; | ||
| 133 | |||
| 134 | hlist_del_rcu(&r->hlist); | ||
| 135 | r->r_dead = 1; | ||
| 136 | rtmsg_rule(RTM_DELRULE, r); | ||
| 137 | fib_rule_put(r); | ||
| 138 | err = 0; | ||
| 139 | break; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | return err; | ||
| 143 | } | 89 | } |
| 90 | #endif | ||
| 144 | 91 | ||
| 145 | /* Allocate new unique table id */ | 92 | int fib_lookup(struct flowi *flp, struct fib_result *res) |
| 146 | |||
| 147 | static struct fib_table *fib_empty_table(void) | ||
| 148 | { | 93 | { |
| 149 | int id; | 94 | struct fib_lookup_arg arg = { |
| 95 | .result = res, | ||
| 96 | }; | ||
| 97 | int err; | ||
| 150 | 98 | ||
| 151 | for (id = 1; id <= RT_TABLE_MAX; id++) | 99 | err = fib_rules_lookup(&fib4_rules_ops, flp, 0, &arg); |
| 152 | if (fib_tables[id] == NULL) | 100 | res->r = arg.rule; |
| 153 | return __fib_new_table(id); | ||
| 154 | return NULL; | ||
| 155 | } | ||
| 156 | 101 | ||
| 157 | static inline void fib_rule_put_rcu(struct rcu_head *head) | 102 | return err; |
| 158 | { | ||
| 159 | struct fib_rule *r = container_of(head, struct fib_rule, rcu); | ||
| 160 | kfree(r); | ||
| 161 | } | 103 | } |
| 162 | 104 | ||
| 163 | void fib_rule_put(struct fib_rule *r) | 105 | static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp, |
| 106 | int flags, struct fib_lookup_arg *arg) | ||
| 164 | { | 107 | { |
| 165 | if (atomic_dec_and_test(&r->r_clntref)) { | 108 | int err = -EAGAIN; |
| 166 | if (r->r_dead) | 109 | struct fib_table *tbl; |
| 167 | call_rcu(&r->rcu, fib_rule_put_rcu); | 110 | |
| 168 | else | 111 | switch (rule->action) { |
| 169 | printk("Freeing alive rule %p\n", r); | 112 | case FR_ACT_TO_TBL: |
| 113 | break; | ||
| 114 | |||
| 115 | case FR_ACT_UNREACHABLE: | ||
| 116 | err = -ENETUNREACH; | ||
| 117 | goto errout; | ||
| 118 | |||
| 119 | case FR_ACT_PROHIBIT: | ||
| 120 | err = -EACCES; | ||
| 121 | goto errout; | ||
| 122 | |||
| 123 | case FR_ACT_BLACKHOLE: | ||
| 124 | default: | ||
| 125 | err = -EINVAL; | ||
| 126 | goto errout; | ||
| 170 | } | 127 | } |
| 128 | |||
| 129 | if ((tbl = fib_get_table(rule->table)) == NULL) | ||
| 130 | goto errout; | ||
| 131 | |||
| 132 | err = tbl->tb_lookup(tbl, flp, (struct fib_result *) arg->result); | ||
| 133 | if (err > 0) | ||
| 134 | err = -EAGAIN; | ||
| 135 | errout: | ||
| 136 | return err; | ||
| 171 | } | 137 | } |
| 172 | 138 | ||
| 173 | /* writer func called from netlink -- rtnl_sem hold*/ | ||
| 174 | 139 | ||
| 175 | int inet_rtm_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) | 140 | void fib_select_default(const struct flowi *flp, struct fib_result *res) |
| 176 | { | 141 | { |
| 177 | struct rtattr **rta = arg; | 142 | if (res->r && res->r->action == FR_ACT_TO_TBL && |
| 178 | struct rtmsg *rtm = NLMSG_DATA(nlh); | 143 | FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) { |
| 179 | struct fib_rule *r, *new_r, *last = NULL; | 144 | struct fib_table *tb; |
| 180 | struct hlist_node *node = NULL; | 145 | if ((tb = fib_get_table(res->r->table)) != NULL) |
| 181 | unsigned char table_id; | 146 | tb->tb_select_default(tb, flp, res); |
| 182 | |||
| 183 | if (rtm->rtm_src_len > 32 || rtm->rtm_dst_len > 32 || | ||
| 184 | (rtm->rtm_tos & ~IPTOS_TOS_MASK)) | ||
| 185 | return -EINVAL; | ||
| 186 | |||
| 187 | if (rta[RTA_IIF-1] && RTA_PAYLOAD(rta[RTA_IIF-1]) > IFNAMSIZ) | ||
| 188 | return -EINVAL; | ||
| 189 | |||
| 190 | table_id = rtm->rtm_table; | ||
| 191 | if (table_id == RT_TABLE_UNSPEC) { | ||
| 192 | struct fib_table *table; | ||
| 193 | if (rtm->rtm_type == RTN_UNICAST) { | ||
| 194 | if ((table = fib_empty_table()) == NULL) | ||
| 195 | return -ENOBUFS; | ||
| 196 | table_id = table->tb_id; | ||
| 197 | } | ||
| 198 | } | 147 | } |
| 148 | } | ||
| 199 | 149 | ||
| 200 | new_r = kmalloc(sizeof(*new_r), GFP_KERNEL); | 150 | static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) |
| 201 | if (!new_r) | 151 | { |
| 202 | return -ENOMEM; | 152 | struct fib4_rule *r = (struct fib4_rule *) rule; |
| 203 | memset(new_r, 0, sizeof(*new_r)); | 153 | __be32 daddr = fl->fl4_dst; |
| 204 | 154 | __be32 saddr = fl->fl4_src; | |
| 205 | if (rta[RTA_SRC-1]) | ||
| 206 | memcpy(&new_r->r_src, RTA_DATA(rta[RTA_SRC-1]), 4); | ||
| 207 | if (rta[RTA_DST-1]) | ||
| 208 | memcpy(&new_r->r_dst, RTA_DATA(rta[RTA_DST-1]), 4); | ||
| 209 | if (rta[RTA_GATEWAY-1]) | ||
| 210 | memcpy(&new_r->r_srcmap, RTA_DATA(rta[RTA_GATEWAY-1]), 4); | ||
| 211 | new_r->r_src_len = rtm->rtm_src_len; | ||
| 212 | new_r->r_dst_len = rtm->rtm_dst_len; | ||
| 213 | new_r->r_srcmask = inet_make_mask(rtm->rtm_src_len); | ||
| 214 | new_r->r_dstmask = inet_make_mask(rtm->rtm_dst_len); | ||
| 215 | new_r->r_tos = rtm->rtm_tos; | ||
| 216 | #ifdef CONFIG_IP_ROUTE_FWMARK | ||
| 217 | if (rta[RTA_PROTOINFO-1]) | ||
| 218 | memcpy(&new_r->r_fwmark, RTA_DATA(rta[RTA_PROTOINFO-1]), 4); | ||
| 219 | #endif | ||
| 220 | new_r->r_action = rtm->rtm_type; | ||
| 221 | new_r->r_flags = rtm->rtm_flags; | ||
| 222 | if (rta[RTA_PRIORITY-1]) | ||
| 223 | memcpy(&new_r->r_preference, RTA_DATA(rta[RTA_PRIORITY-1]), 4); | ||
| 224 | new_r->r_table = table_id; | ||
| 225 | if (rta[RTA_IIF-1]) { | ||
| 226 | struct net_device *dev; | ||
| 227 | rtattr_strlcpy(new_r->r_ifname, rta[RTA_IIF-1], IFNAMSIZ); | ||
| 228 | new_r->r_ifindex = -1; | ||
| 229 | dev = __dev_get_by_name(new_r->r_ifname); | ||
| 230 | if (dev) | ||
| 231 | new_r->r_ifindex = dev->ifindex; | ||
| 232 | } | ||
| 233 | #ifdef CONFIG_NET_CLS_ROUTE | ||
| 234 | if (rta[RTA_FLOW-1]) | ||
| 235 | memcpy(&new_r->r_tclassid, RTA_DATA(rta[RTA_FLOW-1]), 4); | ||
| 236 | #endif | ||
| 237 | r = container_of(fib_rules.first, struct fib_rule, hlist); | ||
| 238 | 155 | ||
| 239 | if (!new_r->r_preference) { | 156 | if (((saddr ^ r->src) & r->srcmask) || |
| 240 | if (r && r->hlist.next != NULL) { | 157 | ((daddr ^ r->dst) & r->dstmask)) |
| 241 | r = container_of(r->hlist.next, struct fib_rule, hlist); | 158 | return 0; |
| 242 | if (r->r_preference) | ||
| 243 | new_r->r_preference = r->r_preference - 1; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | 159 | ||
| 247 | hlist_for_each_entry(r, node, &fib_rules, hlist) { | 160 | if (r->tos && (r->tos != fl->fl4_tos)) |
| 248 | if (r->r_preference > new_r->r_preference) | 161 | return 0; |
| 249 | break; | ||
| 250 | last = r; | ||
| 251 | } | ||
| 252 | atomic_inc(&new_r->r_clntref); | ||
| 253 | 162 | ||
| 254 | if (last) | 163 | #ifdef CONFIG_IP_ROUTE_FWMARK |
| 255 | hlist_add_after_rcu(&last->hlist, &new_r->hlist); | 164 | if ((r->fwmark ^ fl->fl4_fwmark) & r->fwmask) |
| 256 | else | 165 | return 0; |
| 257 | hlist_add_before_rcu(&new_r->hlist, &r->hlist); | 166 | #endif |
| 258 | 167 | ||
| 259 | rtmsg_rule(RTM_NEWRULE, new_r); | 168 | return 1; |
| 260 | return 0; | ||
| 261 | } | 169 | } |
| 262 | 170 | ||
| 263 | #ifdef CONFIG_NET_CLS_ROUTE | 171 | static struct fib_table *fib_empty_table(void) |
| 264 | u32 fib_rules_tclass(struct fib_result *res) | ||
| 265 | { | 172 | { |
| 266 | if (res->r) | 173 | u32 id; |
| 267 | return res->r->r_tclassid; | 174 | |
| 268 | return 0; | 175 | for (id = 1; id <= RT_TABLE_MAX; id++) |
| 176 | if (fib_get_table(id) == NULL) | ||
| 177 | return fib_new_table(id); | ||
| 178 | return NULL; | ||
| 269 | } | 179 | } |
| 270 | #endif | ||
| 271 | 180 | ||
| 272 | /* callers should hold rtnl semaphore */ | 181 | static struct nla_policy fib4_rule_policy[FRA_MAX+1] __read_mostly = { |
| 182 | [FRA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, | ||
| 183 | [FRA_PRIORITY] = { .type = NLA_U32 }, | ||
| 184 | [FRA_SRC] = { .type = NLA_U32 }, | ||
| 185 | [FRA_DST] = { .type = NLA_U32 }, | ||
| 186 | [FRA_FWMARK] = { .type = NLA_U32 }, | ||
| 187 | [FRA_FWMASK] = { .type = NLA_U32 }, | ||
| 188 | [FRA_FLOW] = { .type = NLA_U32 }, | ||
| 189 | [FRA_TABLE] = { .type = NLA_U32 }, | ||
| 190 | }; | ||
| 273 | 191 | ||
| 274 | static void fib_rules_detach(struct net_device *dev) | 192 | static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb, |
| 193 | struct nlmsghdr *nlh, struct fib_rule_hdr *frh, | ||
| 194 | struct nlattr **tb) | ||
| 275 | { | 195 | { |
| 276 | struct hlist_node *node; | 196 | int err = -EINVAL; |
| 277 | struct fib_rule *r; | 197 | struct fib4_rule *rule4 = (struct fib4_rule *) rule; |
| 198 | |||
| 199 | if (frh->src_len > 32 || frh->dst_len > 32 || | ||
| 200 | (frh->tos & ~IPTOS_TOS_MASK)) | ||
| 201 | goto errout; | ||
| 202 | |||
| 203 | if (rule->table == RT_TABLE_UNSPEC) { | ||
| 204 | if (rule->action == FR_ACT_TO_TBL) { | ||
| 205 | struct fib_table *table; | ||
| 278 | 206 | ||
| 279 | hlist_for_each_entry(r, node, &fib_rules, hlist) { | 207 | table = fib_empty_table(); |
| 280 | if (r->r_ifindex == dev->ifindex) | 208 | if (table == NULL) { |
| 281 | r->r_ifindex = -1; | 209 | err = -ENOBUFS; |
| 210 | goto errout; | ||
| 211 | } | ||
| 282 | 212 | ||
| 213 | rule->table = table->tb_id; | ||
| 214 | } | ||
| 283 | } | 215 | } |
| 284 | } | ||
| 285 | 216 | ||
| 286 | /* callers should hold rtnl semaphore */ | 217 | if (tb[FRA_SRC]) |
| 218 | rule4->src = nla_get_be32(tb[FRA_SRC]); | ||
| 287 | 219 | ||
| 288 | static void fib_rules_attach(struct net_device *dev) | 220 | if (tb[FRA_DST]) |
| 289 | { | 221 | rule4->dst = nla_get_be32(tb[FRA_DST]); |
| 290 | struct hlist_node *node; | ||
| 291 | struct fib_rule *r; | ||
| 292 | 222 | ||
| 293 | hlist_for_each_entry(r, node, &fib_rules, hlist) { | 223 | #ifdef CONFIG_IP_ROUTE_FWMARK |
| 294 | if (r->r_ifindex == -1 && strcmp(dev->name, r->r_ifname) == 0) | 224 | if (tb[FRA_FWMARK]) { |
| 295 | r->r_ifindex = dev->ifindex; | 225 | rule4->fwmark = nla_get_u32(tb[FRA_FWMARK]); |
| 226 | if (rule4->fwmark) | ||
| 227 | /* compatibility: if the mark value is non-zero all bits | ||
| 228 | * are compared unless a mask is explicitly specified. | ||
| 229 | */ | ||
| 230 | rule4->fwmask = 0xFFFFFFFF; | ||
| 296 | } | 231 | } |
| 232 | |||
| 233 | if (tb[FRA_FWMASK]) | ||
| 234 | rule4->fwmask = nla_get_u32(tb[FRA_FWMASK]); | ||
| 235 | #endif | ||
| 236 | |||
| 237 | #ifdef CONFIG_NET_CLS_ROUTE | ||
| 238 | if (tb[FRA_FLOW]) | ||
| 239 | rule4->tclassid = nla_get_u32(tb[FRA_FLOW]); | ||
| 240 | #endif | ||
| 241 | |||
| 242 | rule4->src_len = frh->src_len; | ||
| 243 | rule4->srcmask = inet_make_mask(rule4->src_len); | ||
| 244 | rule4->dst_len = frh->dst_len; | ||
| 245 | rule4->dstmask = inet_make_mask(rule4->dst_len); | ||
| 246 | rule4->tos = frh->tos; | ||
| 247 | |||
| 248 | err = 0; | ||
| 249 | errout: | ||
| 250 | return err; | ||
| 297 | } | 251 | } |
| 298 | 252 | ||
| 299 | int fib_lookup(const struct flowi *flp, struct fib_result *res) | 253 | static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, |
| 254 | struct nlattr **tb) | ||
| 300 | { | 255 | { |
| 301 | int err; | 256 | struct fib4_rule *rule4 = (struct fib4_rule *) rule; |
| 302 | struct fib_rule *r, *policy; | ||
| 303 | struct fib_table *tb; | ||
| 304 | struct hlist_node *node; | ||
| 305 | 257 | ||
| 306 | u32 daddr = flp->fl4_dst; | 258 | if (frh->src_len && (rule4->src_len != frh->src_len)) |
| 307 | u32 saddr = flp->fl4_src; | 259 | return 0; |
| 308 | 260 | ||
| 309 | FRprintk("Lookup: %u.%u.%u.%u <- %u.%u.%u.%u ", | 261 | if (frh->dst_len && (rule4->dst_len != frh->dst_len)) |
| 310 | NIPQUAD(flp->fl4_dst), NIPQUAD(flp->fl4_src)); | 262 | return 0; |
| 311 | 263 | ||
| 312 | rcu_read_lock(); | 264 | if (frh->tos && (rule4->tos != frh->tos)) |
| 265 | return 0; | ||
| 313 | 266 | ||
| 314 | hlist_for_each_entry_rcu(r, node, &fib_rules, hlist) { | ||
| 315 | if (((saddr^r->r_src) & r->r_srcmask) || | ||
| 316 | ((daddr^r->r_dst) & r->r_dstmask) || | ||
| 317 | (r->r_tos && r->r_tos != flp->fl4_tos) || | ||
| 318 | #ifdef CONFIG_IP_ROUTE_FWMARK | 267 | #ifdef CONFIG_IP_ROUTE_FWMARK |
| 319 | (r->r_fwmark && r->r_fwmark != flp->fl4_fwmark) || | 268 | if (tb[FRA_FWMARK] && (rule4->fwmark != nla_get_u32(tb[FRA_FWMARK]))) |
| 269 | return 0; | ||
| 270 | |||
| 271 | if (tb[FRA_FWMASK] && (rule4->fwmask != nla_get_u32(tb[FRA_FWMASK]))) | ||
| 272 | return 0; | ||
| 320 | #endif | 273 | #endif |
| 321 | (r->r_ifindex && r->r_ifindex != flp->iif)) | ||
| 322 | continue; | ||
| 323 | |||
| 324 | FRprintk("tb %d r %d ", r->r_table, r->r_action); | ||
| 325 | switch (r->r_action) { | ||
| 326 | case RTN_UNICAST: | ||
| 327 | policy = r; | ||
| 328 | break; | ||
| 329 | case RTN_UNREACHABLE: | ||
| 330 | rcu_read_unlock(); | ||
| 331 | return -ENETUNREACH; | ||
| 332 | default: | ||
| 333 | case RTN_BLACKHOLE: | ||
| 334 | rcu_read_unlock(); | ||
| 335 | return -EINVAL; | ||
| 336 | case RTN_PROHIBIT: | ||
| 337 | rcu_read_unlock(); | ||
| 338 | return -EACCES; | ||
| 339 | } | ||
| 340 | 274 | ||
| 341 | if ((tb = fib_get_table(r->r_table)) == NULL) | 275 | #ifdef CONFIG_NET_CLS_ROUTE |
| 342 | continue; | 276 | if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW]))) |
| 343 | err = tb->tb_lookup(tb, flp, res); | 277 | return 0; |
| 344 | if (err == 0) { | 278 | #endif |
| 345 | res->r = policy; | ||
| 346 | if (policy) | ||
| 347 | atomic_inc(&policy->r_clntref); | ||
| 348 | rcu_read_unlock(); | ||
| 349 | return 0; | ||
| 350 | } | ||
| 351 | if (err < 0 && err != -EAGAIN) { | ||
| 352 | rcu_read_unlock(); | ||
| 353 | return err; | ||
| 354 | } | ||
| 355 | } | ||
| 356 | FRprintk("FAILURE\n"); | ||
| 357 | rcu_read_unlock(); | ||
| 358 | return -ENETUNREACH; | ||
| 359 | } | ||
| 360 | 279 | ||
| 361 | void fib_select_default(const struct flowi *flp, struct fib_result *res) | 280 | if (tb[FRA_SRC] && (rule4->src != nla_get_be32(tb[FRA_SRC]))) |
| 362 | { | 281 | return 0; |
| 363 | if (res->r && res->r->r_action == RTN_UNICAST && | ||
| 364 | FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) { | ||
| 365 | struct fib_table *tb; | ||
| 366 | if ((tb = fib_get_table(res->r->r_table)) != NULL) | ||
| 367 | tb->tb_select_default(tb, flp, res); | ||
| 368 | } | ||
| 369 | } | ||
| 370 | 282 | ||
| 371 | static int fib_rules_event(struct notifier_block *this, unsigned long event, void *ptr) | 283 | if (tb[FRA_DST] && (rule4->dst != nla_get_be32(tb[FRA_DST]))) |
| 372 | { | 284 | return 0; |
| 373 | struct net_device *dev = ptr; | ||
| 374 | 285 | ||
| 375 | if (event == NETDEV_UNREGISTER) | 286 | return 1; |
| 376 | fib_rules_detach(dev); | ||
| 377 | else if (event == NETDEV_REGISTER) | ||
| 378 | fib_rules_attach(dev); | ||
| 379 | return NOTIFY_DONE; | ||
| 380 | } | 287 | } |
| 381 | 288 | ||
| 289 | static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb, | ||
| 290 | struct nlmsghdr *nlh, struct fib_rule_hdr *frh) | ||
| 291 | { | ||
| 292 | struct fib4_rule *rule4 = (struct fib4_rule *) rule; | ||
| 382 | 293 | ||
| 383 | static struct notifier_block fib_rules_notifier = { | 294 | frh->family = AF_INET; |
| 384 | .notifier_call =fib_rules_event, | 295 | frh->dst_len = rule4->dst_len; |
| 385 | }; | 296 | frh->src_len = rule4->src_len; |
| 297 | frh->tos = rule4->tos; | ||
| 386 | 298 | ||
| 387 | static __inline__ int inet_fill_rule(struct sk_buff *skb, | ||
| 388 | struct fib_rule *r, | ||
| 389 | u32 pid, u32 seq, int event, | ||
| 390 | unsigned int flags) | ||
| 391 | { | ||
| 392 | struct rtmsg *rtm; | ||
| 393 | struct nlmsghdr *nlh; | ||
| 394 | unsigned char *b = skb->tail; | ||
| 395 | |||
| 396 | nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*rtm), flags); | ||
| 397 | rtm = NLMSG_DATA(nlh); | ||
| 398 | rtm->rtm_family = AF_INET; | ||
| 399 | rtm->rtm_dst_len = r->r_dst_len; | ||
| 400 | rtm->rtm_src_len = r->r_src_len; | ||
| 401 | rtm->rtm_tos = r->r_tos; | ||
| 402 | #ifdef CONFIG_IP_ROUTE_FWMARK | 299 | #ifdef CONFIG_IP_ROUTE_FWMARK |
| 403 | if (r->r_fwmark) | 300 | if (rule4->fwmark) |
| 404 | RTA_PUT(skb, RTA_PROTOINFO, 4, &r->r_fwmark); | 301 | NLA_PUT_U32(skb, FRA_FWMARK, rule4->fwmark); |
| 302 | |||
| 303 | if (rule4->fwmask || rule4->fwmark) | ||
| 304 | NLA_PUT_U32(skb, FRA_FWMASK, rule4->fwmask); | ||
| 405 | #endif | 305 | #endif |
| 406 | rtm->rtm_table = r->r_table; | 306 | |
| 407 | rtm->rtm_protocol = 0; | 307 | if (rule4->dst_len) |
| 408 | rtm->rtm_scope = 0; | 308 | NLA_PUT_BE32(skb, FRA_DST, rule4->dst); |
| 409 | rtm->rtm_type = r->r_action; | 309 | |
| 410 | rtm->rtm_flags = r->r_flags; | 310 | if (rule4->src_len) |
| 411 | 311 | NLA_PUT_BE32(skb, FRA_SRC, rule4->src); | |
| 412 | if (r->r_dst_len) | 312 | |
| 413 | RTA_PUT(skb, RTA_DST, 4, &r->r_dst); | ||
| 414 | if (r->r_src_len) | ||
| 415 | RTA_PUT(skb, RTA_SRC, 4, &r->r_src); | ||
| 416 | if (r->r_ifname[0]) | ||
| 417 | RTA_PUT(skb, RTA_IIF, IFNAMSIZ, &r->r_ifname); | ||
| 418 | if (r->r_preference) | ||
| 419 | RTA_PUT(skb, RTA_PRIORITY, 4, &r->r_preference); | ||
| 420 | if (r->r_srcmap) | ||
| 421 | RTA_PUT(skb, RTA_GATEWAY, 4, &r->r_srcmap); | ||
| 422 | #ifdef CONFIG_NET_CLS_ROUTE | 313 | #ifdef CONFIG_NET_CLS_ROUTE |
| 423 | if (r->r_tclassid) | 314 | if (rule4->tclassid) |
| 424 | RTA_PUT(skb, RTA_FLOW, 4, &r->r_tclassid); | 315 | NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid); |
| 425 | #endif | 316 | #endif |
| 426 | nlh->nlmsg_len = skb->tail - b; | 317 | return 0; |
| 427 | return skb->len; | ||
| 428 | 318 | ||
| 429 | nlmsg_failure: | 319 | nla_put_failure: |
| 430 | rtattr_failure: | 320 | return -ENOBUFS; |
| 431 | skb_trim(skb, b - skb->data); | ||
| 432 | return -1; | ||
| 433 | } | 321 | } |
| 434 | 322 | ||
| 435 | /* callers should hold rtnl semaphore */ | 323 | int fib4_rules_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 436 | |||
| 437 | static void rtmsg_rule(int event, struct fib_rule *r) | ||
| 438 | { | 324 | { |
| 439 | int size = NLMSG_SPACE(sizeof(struct rtmsg) + 128); | 325 | return fib_rules_dump(skb, cb, AF_INET); |
| 440 | struct sk_buff *skb = alloc_skb(size, GFP_KERNEL); | ||
| 441 | |||
| 442 | if (!skb) | ||
| 443 | netlink_set_err(rtnl, 0, RTNLGRP_IPV4_RULE, ENOBUFS); | ||
| 444 | else if (inet_fill_rule(skb, r, 0, 0, event, 0) < 0) { | ||
| 445 | kfree_skb(skb); | ||
| 446 | netlink_set_err(rtnl, 0, RTNLGRP_IPV4_RULE, EINVAL); | ||
| 447 | } else { | ||
| 448 | netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV4_RULE, GFP_KERNEL); | ||
| 449 | } | ||
| 450 | } | 326 | } |
| 451 | 327 | ||
| 452 | int inet_dump_rules(struct sk_buff *skb, struct netlink_callback *cb) | 328 | static u32 fib4_rule_default_pref(void) |
| 453 | { | 329 | { |
| 454 | int idx = 0; | 330 | struct list_head *pos; |
| 455 | int s_idx = cb->args[0]; | 331 | struct fib_rule *rule; |
| 456 | struct fib_rule *r; | 332 | |
| 457 | struct hlist_node *node; | 333 | if (!list_empty(&fib4_rules)) { |
| 458 | 334 | pos = fib4_rules.next; | |
| 459 | rcu_read_lock(); | 335 | if (pos->next != &fib4_rules) { |
| 460 | hlist_for_each_entry(r, node, &fib_rules, hlist) { | 336 | rule = list_entry(pos->next, struct fib_rule, list); |
| 461 | 337 | if (rule->pref) | |
| 462 | if (idx < s_idx) | 338 | return rule->pref - 1; |
| 463 | continue; | 339 | } |
| 464 | if (inet_fill_rule(skb, r, NETLINK_CB(cb->skb).pid, | ||
| 465 | cb->nlh->nlmsg_seq, | ||
| 466 | RTM_NEWRULE, NLM_F_MULTI) < 0) | ||
| 467 | break; | ||
| 468 | idx++; | ||
| 469 | } | 340 | } |
| 470 | rcu_read_unlock(); | ||
| 471 | cb->args[0] = idx; | ||
| 472 | 341 | ||
| 473 | return skb->len; | 342 | return 0; |
| 474 | } | 343 | } |
| 475 | 344 | ||
| 476 | void __init fib_rules_init(void) | 345 | static struct fib_rules_ops fib4_rules_ops = { |
| 346 | .family = AF_INET, | ||
| 347 | .rule_size = sizeof(struct fib4_rule), | ||
| 348 | .action = fib4_rule_action, | ||
| 349 | .match = fib4_rule_match, | ||
| 350 | .configure = fib4_rule_configure, | ||
| 351 | .compare = fib4_rule_compare, | ||
| 352 | .fill = fib4_rule_fill, | ||
| 353 | .default_pref = fib4_rule_default_pref, | ||
| 354 | .nlgroup = RTNLGRP_IPV4_RULE, | ||
| 355 | .policy = fib4_rule_policy, | ||
| 356 | .rules_list = &fib4_rules, | ||
| 357 | .owner = THIS_MODULE, | ||
| 358 | }; | ||
| 359 | |||
| 360 | void __init fib4_rules_init(void) | ||
| 477 | { | 361 | { |
| 478 | INIT_HLIST_HEAD(&fib_rules); | 362 | list_add_tail(&local_rule.common.list, &fib4_rules); |
| 479 | hlist_add_head(&local_rule.hlist, &fib_rules); | 363 | list_add_tail(&main_rule.common.list, &fib4_rules); |
| 480 | hlist_add_after(&local_rule.hlist, &main_rule.hlist); | 364 | list_add_tail(&default_rule.common.list, &fib4_rules); |
| 481 | hlist_add_after(&main_rule.hlist, &default_rule.hlist); | 365 | |
| 482 | register_netdevice_notifier(&fib_rules_notifier); | 366 | fib_rules_register(&fib4_rules_ops); |
| 483 | } | 367 | } |
