| 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_IP_ROUTE_FWMARK | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 48 | 	u32			fwmark; | 
| Patrick McHardy | bbfb39c | 2006-08-25 16:10:14 -0700 | [diff] [blame] | 49 | 	u32			fwmask; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #ifdef CONFIG_NET_CLS_ROUTE | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 52 | 	u32			tclassid; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | }; | 
 | 55 |  | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 56 | static struct fib4_rule default_rule = { | 
 | 57 | 	.common = { | 
 | 58 | 		.refcnt =	ATOMIC_INIT(2), | 
 | 59 | 		.pref =		0x7FFF, | 
 | 60 | 		.table =	RT_TABLE_DEFAULT, | 
 | 61 | 		.action =	FR_ACT_TO_TBL, | 
 | 62 | 	}, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | }; | 
 | 64 |  | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 65 | static struct fib4_rule main_rule = { | 
 | 66 | 	.common = { | 
 | 67 | 		.refcnt =	ATOMIC_INIT(2), | 
 | 68 | 		.pref =		0x7FFE, | 
 | 69 | 		.table =	RT_TABLE_MAIN, | 
 | 70 | 		.action =	FR_ACT_TO_TBL, | 
 | 71 | 	}, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | }; | 
 | 73 |  | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 74 | static struct fib4_rule local_rule = { | 
 | 75 | 	.common = { | 
 | 76 | 		.refcnt =	ATOMIC_INIT(2), | 
 | 77 | 		.table =	RT_TABLE_LOCAL, | 
 | 78 | 		.action =	FR_ACT_TO_TBL, | 
 | 79 | 		.flags =	FIB_RULE_PERMANENT, | 
 | 80 | 	}, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | }; | 
 | 82 |  | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 83 | static LIST_HEAD(fib4_rules); | 
| Robert Olsson | 7b204af | 2006-03-20 17:18:53 -0800 | [diff] [blame] | 84 |  | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 85 | #ifdef CONFIG_NET_CLS_ROUTE | 
 | 86 | u32 fib_rules_tclass(struct fib_result *res) | 
| 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 | 	return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0; | 
 | 89 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 |  | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 92 | int fib_lookup(struct flowi *flp, struct fib_result *res) | 
 | 93 | { | 
 | 94 | 	struct fib_lookup_arg arg = { | 
 | 95 | 		.result = res, | 
 | 96 | 	}; | 
 | 97 | 	int err; | 
 | 98 |  | 
 | 99 | 	err = fib_rules_lookup(&fib4_rules_ops, flp, 0, &arg); | 
 | 100 | 	res->r = arg.rule; | 
 | 101 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | 	return err; | 
 | 103 | } | 
 | 104 |  | 
| Adrian Bunk | 8ce11e6 | 2006-08-07 21:50:48 -0700 | [diff] [blame] | 105 | static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp, | 
 | 106 | 			    int flags, struct fib_lookup_arg *arg) | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 107 | { | 
 | 108 | 	int err = -EAGAIN; | 
 | 109 | 	struct fib_table *tbl; | 
 | 110 |  | 
 | 111 | 	switch (rule->action) { | 
 | 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; | 
 | 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; | 
 | 137 | } | 
 | 138 |  | 
 | 139 |  | 
 | 140 | void fib_select_default(const struct flowi *flp, struct fib_result *res) | 
 | 141 | { | 
 | 142 | 	if (res->r && res->r->action == FR_ACT_TO_TBL && | 
 | 143 | 	    FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) { | 
 | 144 | 		struct fib_table *tb; | 
 | 145 | 		if ((tb = fib_get_table(res->r->table)) != NULL) | 
 | 146 | 			tb->tb_select_default(tb, flp, res); | 
 | 147 | 	} | 
 | 148 | } | 
 | 149 |  | 
 | 150 | static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) | 
 | 151 | { | 
 | 152 | 	struct fib4_rule *r = (struct fib4_rule *) rule; | 
| Al Viro | 81f7bf6 | 2006-09-27 18:40:00 -0700 | [diff] [blame] | 153 | 	__be32 daddr = fl->fl4_dst; | 
 | 154 | 	__be32 saddr = fl->fl4_src; | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 155 |  | 
 | 156 | 	if (((saddr ^ r->src) & r->srcmask) || | 
 | 157 | 	    ((daddr ^ r->dst) & r->dstmask)) | 
 | 158 | 		return 0; | 
 | 159 |  | 
 | 160 | 	if (r->tos && (r->tos != fl->fl4_tos)) | 
 | 161 | 		return 0; | 
 | 162 |  | 
 | 163 | #ifdef CONFIG_IP_ROUTE_FWMARK | 
| Patrick McHardy | bbfb39c | 2006-08-25 16:10:14 -0700 | [diff] [blame] | 164 | 	if ((r->fwmark ^ fl->fl4_fwmark) & r->fwmask) | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 165 | 		return 0; | 
 | 166 | #endif | 
 | 167 |  | 
 | 168 | 	return 1; | 
 | 169 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 |  | 
 | 171 | static struct fib_table *fib_empty_table(void) | 
 | 172 | { | 
| Patrick McHardy | 2dfe55b | 2006-08-10 23:08:33 -0700 | [diff] [blame] | 173 | 	u32 id; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 |  | 
 | 175 | 	for (id = 1; id <= RT_TABLE_MAX; id++) | 
| Patrick McHardy | 1af5a8c | 2006-08-10 23:10:46 -0700 | [diff] [blame] | 176 | 		if (fib_get_table(id) == NULL) | 
 | 177 | 			return fib_new_table(id); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | 	return NULL; | 
 | 179 | } | 
 | 180 |  | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 181 | static struct nla_policy fib4_rule_policy[FRA_MAX+1] __read_mostly = { | 
| Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 182 | 	[FRA_IFNAME]	= { .type = NLA_STRING, .len = IFNAMSIZ - 1 }, | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 183 | 	[FRA_PRIORITY]	= { .type = NLA_U32 }, | 
 | 184 | 	[FRA_SRC]	= { .type = NLA_U32 }, | 
 | 185 | 	[FRA_DST]	= { .type = NLA_U32 }, | 
 | 186 | 	[FRA_FWMARK]	= { .type = NLA_U32 }, | 
| Patrick McHardy | bbfb39c | 2006-08-25 16:10:14 -0700 | [diff] [blame] | 187 | 	[FRA_FWMASK]	= { .type = NLA_U32 }, | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 188 | 	[FRA_FLOW]	= { .type = NLA_U32 }, | 
| Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 189 | 	[FRA_TABLE]	= { .type = NLA_U32 }, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | }; | 
 | 191 |  | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 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) | 
| 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 | 	int err = -EINVAL; | 
 | 197 | 	struct fib4_rule *rule4 = (struct fib4_rule *) rule; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 |  | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 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; | 
 | 206 |  | 
 | 207 | 			table = fib_empty_table(); | 
 | 208 | 			if (table == NULL) { | 
 | 209 | 				err = -ENOBUFS; | 
 | 210 | 				goto errout; | 
 | 211 | 			} | 
 | 212 |  | 
 | 213 | 			rule->table = table->tb_id; | 
 | 214 | 		} | 
 | 215 | 	} | 
 | 216 |  | 
 | 217 | 	if (tb[FRA_SRC]) | 
| Al Viro | 45d60b9 | 2006-09-27 18:40:27 -0700 | [diff] [blame] | 218 | 		rule4->src = nla_get_be32(tb[FRA_SRC]); | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 219 |  | 
 | 220 | 	if (tb[FRA_DST]) | 
| Al Viro | 45d60b9 | 2006-09-27 18:40:27 -0700 | [diff] [blame] | 221 | 		rule4->dst = nla_get_be32(tb[FRA_DST]); | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 222 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | #ifdef CONFIG_IP_ROUTE_FWMARK | 
| Patrick McHardy | bbfb39c | 2006-08-25 16:10:14 -0700 | [diff] [blame] | 224 | 	if (tb[FRA_FWMARK]) { | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 225 | 		rule4->fwmark = nla_get_u32(tb[FRA_FWMARK]); | 
| Patrick McHardy | bbfb39c | 2006-08-25 16:10:14 -0700 | [diff] [blame] | 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; | 
 | 231 | 	} | 
 | 232 |  | 
 | 233 | 	if (tb[FRA_FWMASK]) | 
 | 234 | 		rule4->fwmask = nla_get_u32(tb[FRA_FWMASK]); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | #ifdef CONFIG_NET_CLS_ROUTE | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 238 | 	if (tb[FRA_FLOW]) | 
 | 239 | 		rule4->tclassid = nla_get_u32(tb[FRA_FLOW]); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 |  | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 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; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } | 
 | 252 |  | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 253 | static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, | 
 | 254 | 			     struct nlattr **tb) | 
| Patrick McHardy | a5cdc03 | 2006-03-23 01:16:06 -0800 | [diff] [blame] | 255 | { | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 256 | 	struct fib4_rule *rule4 = (struct fib4_rule *) rule; | 
| Patrick McHardy | a5cdc03 | 2006-03-23 01:16:06 -0800 | [diff] [blame] | 257 |  | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 258 | 	if (frh->src_len && (rule4->src_len != frh->src_len)) | 
 | 259 | 		return 0; | 
 | 260 |  | 
 | 261 | 	if (frh->dst_len && (rule4->dst_len != frh->dst_len)) | 
 | 262 | 		return 0; | 
 | 263 |  | 
 | 264 | 	if (frh->tos && (rule4->tos != frh->tos)) | 
 | 265 | 		return 0; | 
 | 266 |  | 
 | 267 | #ifdef CONFIG_IP_ROUTE_FWMARK | 
 | 268 | 	if (tb[FRA_FWMARK] && (rule4->fwmark != nla_get_u32(tb[FRA_FWMARK]))) | 
 | 269 | 		return 0; | 
| Patrick McHardy | bbfb39c | 2006-08-25 16:10:14 -0700 | [diff] [blame] | 270 |  | 
 | 271 | 	if (tb[FRA_FWMASK] && (rule4->fwmask != nla_get_u32(tb[FRA_FWMASK]))) | 
 | 272 | 		return 0; | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 273 | #endif | 
 | 274 |  | 
 | 275 | #ifdef CONFIG_NET_CLS_ROUTE | 
 | 276 | 	if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW]))) | 
 | 277 | 		return 0; | 
 | 278 | #endif | 
 | 279 |  | 
| Al Viro | 45d60b9 | 2006-09-27 18:40:27 -0700 | [diff] [blame] | 280 | 	if (tb[FRA_SRC] && (rule4->src != nla_get_be32(tb[FRA_SRC]))) | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 281 | 		return 0; | 
 | 282 |  | 
| Al Viro | 45d60b9 | 2006-09-27 18:40:27 -0700 | [diff] [blame] | 283 | 	if (tb[FRA_DST] && (rule4->dst != nla_get_be32(tb[FRA_DST]))) | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 284 | 		return 0; | 
 | 285 |  | 
 | 286 | 	return 1; | 
 | 287 | } | 
 | 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; | 
 | 293 |  | 
 | 294 | 	frh->family = AF_INET; | 
 | 295 | 	frh->dst_len = rule4->dst_len; | 
 | 296 | 	frh->src_len = rule4->src_len; | 
 | 297 | 	frh->tos = rule4->tos; | 
 | 298 |  | 
 | 299 | #ifdef CONFIG_IP_ROUTE_FWMARK | 
 | 300 | 	if (rule4->fwmark) | 
 | 301 | 		NLA_PUT_U32(skb, FRA_FWMARK, rule4->fwmark); | 
| Patrick McHardy | bbfb39c | 2006-08-25 16:10:14 -0700 | [diff] [blame] | 302 |  | 
 | 303 | 	if (rule4->fwmask || rule4->fwmark) | 
 | 304 | 		NLA_PUT_U32(skb, FRA_FWMASK, rule4->fwmask); | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 305 | #endif | 
 | 306 |  | 
 | 307 | 	if (rule4->dst_len) | 
| Al Viro | 45d60b9 | 2006-09-27 18:40:27 -0700 | [diff] [blame] | 308 | 		NLA_PUT_BE32(skb, FRA_DST, rule4->dst); | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 309 |  | 
 | 310 | 	if (rule4->src_len) | 
| Al Viro | 45d60b9 | 2006-09-27 18:40:27 -0700 | [diff] [blame] | 311 | 		NLA_PUT_BE32(skb, FRA_SRC, rule4->src); | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 312 |  | 
 | 313 | #ifdef CONFIG_NET_CLS_ROUTE | 
 | 314 | 	if (rule4->tclassid) | 
 | 315 | 		NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid); | 
 | 316 | #endif | 
 | 317 | 	return 0; | 
 | 318 |  | 
 | 319 | nla_put_failure: | 
 | 320 | 	return -ENOBUFS; | 
 | 321 | } | 
 | 322 |  | 
 | 323 | int fib4_rules_dump(struct sk_buff *skb, struct netlink_callback *cb) | 
 | 324 | { | 
 | 325 | 	return fib_rules_dump(skb, cb, AF_INET); | 
 | 326 | } | 
 | 327 |  | 
 | 328 | static u32 fib4_rule_default_pref(void) | 
 | 329 | { | 
 | 330 | 	struct list_head *pos; | 
 | 331 | 	struct fib_rule *rule; | 
 | 332 |  | 
 | 333 | 	if (!list_empty(&fib4_rules)) { | 
 | 334 | 		pos = fib4_rules.next; | 
 | 335 | 		if (pos->next != &fib4_rules) { | 
 | 336 | 			rule = list_entry(pos->next, struct fib_rule, list); | 
 | 337 | 			if (rule->pref) | 
 | 338 | 				return rule->pref - 1; | 
 | 339 | 		} | 
| Patrick McHardy | a5cdc03 | 2006-03-23 01:16:06 -0800 | [diff] [blame] | 340 | 	} | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 341 |  | 
 | 342 | 	return 0; | 
| Patrick McHardy | a5cdc03 | 2006-03-23 01:16:06 -0800 | [diff] [blame] | 343 | } | 
 | 344 |  | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 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) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 362 | 	list_add_tail(&local_rule.common.list, &fib4_rules); | 
 | 363 | 	list_add_tail(&main_rule.common.list, &fib4_rules); | 
 | 364 | 	list_add_tail(&default_rule.common.list, &fib4_rules); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 |  | 
| Thomas Graf | e1ef4bf | 2006-08-04 03:39:22 -0700 | [diff] [blame] | 366 | 	fib_rules_register(&fib4_rules_ops); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |