Skip to contentSkip to conversation

Multiple IPV6 Routing Tables & Policy Routing

RFC In review Thomas Graf 9 participants 53 messages Jul 26, 2006 – Jul 31, 2006

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Hello,

Thought it might be time to go through a round of comments on this work. Even though I've almost rewritten all the code the patches are based on the work found on www.mobile-ipv6.org. I have no idea which code was written by whom so just email me to get the credits right.

Main differences to the version found on mobile-ipv6.org is that I removed table refcnt and defined that tables cannot disappear once created to simplify things and avoid too many atomic operations when looking up routes. I've replaced the table array with a hash table to prepare it for > 255 tables and made things aware of the new default router selection code and experimental route info stuff added recently.

It's not final but somewhat working, I'm eager to see comments or patches. I apologize if I've tramped onto anybody's foot by taking this up and submitting it, this isn't meant as an attempt to steal credits but rather to pick up good code and finally get it upstream after a very long while.

#1 lore raw

PATCH 1/5 : Remove ndiscs rt6_lock dependency

(Ab)using rt6_lock wouldn't work anymore if rt6_lock is converted into a per table lock.

net/ipv6/route.c +11 -5
Index: net-2.6.git/net/ipv6/route.c
===================================================================
@@ -745,8 +745,6 @@ static void ip6_rt_update_pmtu(struct ds
 	}
 }
 
-/* Protected by rt6_lock.  */
-static struct dst_entry *ndisc_dst_gc_list;
 static int ipv6_get_mtu(struct net_device *dev);
 
 static inline unsigned int ipv6_advmss(unsigned int mtu)
@@ -767,6 +765,9 @@ static inline unsigned int ipv6_advmss(u
 	return mtu;
 }
 
+static struct dst_entry *ndisc_dst_gc_list;
+DEFINE_SPINLOCK(ndisc_lock);
+
 struct dst_entry *ndisc_dst_alloc(struct net_device *dev, 
 				  struct neighbour *neigh,
 				  struct in6_addr *addr,
@@ -807,10 +808,10 @@ struct dst_entry *ndisc_dst_alloc(struct
 	rt->rt6i_dst.plen = 128;
 #endif
 
-	write_lock_bh(&rt6_lock);
+	spin_lock_bh(&ndisc_lock);
 	rt->u.dst.next = ndisc_dst_gc_list;
 	ndisc_dst_gc_list = &rt->u.dst;
-	write_unlock_bh(&rt6_lock);
+	spin_unlock_bh(&ndisc_lock);
 
 	fib6_force_start_gc();
 
@@ -824,8 +825,11 @@ int ndisc_dst_gc(int *more)
 	int freed;
 
 	next = NULL;
+ 	freed = 0;
+
+	spin_lock_bh(&ndisc_lock);
 	pprev = &ndisc_dst_gc_list;
-	freed = 0;
+
 	while ((dst = *pprev) != NULL) {
 		if (!atomic_read(&dst->__refcnt)) {
 			*pprev = dst->next;
@@ -837,6 +841,8 @@ int ndisc_dst_gc(int *more)
 		}
 	}
 
+	spin_unlock_bh(&ndisc_lock);
+
 	return freed;
 }
 
#3 lore raw

Re: [PATCH 1/5] [IPV6]: Remove ndiscs rt6_lock dependency

Are these changes scheduled to go into 2.6.18-rcX or 2.6.19?

Thanks.
- Tushar

Quoted 17 lines · David Miller

From: Thomas Graf <[email protected]>
Date: Thu, 27 Jul 2006 00:00:01 +0200

Quoted 4 lines

(Ab)using rt6_lock wouldn't work anymore if rt6_lock is converted into a per table lock.

This one looks great.

-
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer
#4 lore raw

Re: [PATCH 1/5] [IPV6]: Remove ndiscs rt6_lock dependency

From: Tushar Gohad <[email protected]>
Date: Wed, 26 Jul 2006 16:34:03 -0700

Quoted 1 line

Are these changes scheduled to go into 2.6.18-rcX or 2.6.19?

Since the feature freeze is frozen, net-2.6.19 is where it will likely go.

#5 lore raw

Re: [PATCH 1/5] [IPV6]: Remove ndiscs rt6_lock dependency

In reply to David Miller
Quoted 11 lines · David Miller

From: Thomas Graf <[email protected]>
Date: Thu, 27 Jul 2006 00:00:01 +0200

Quoted 4 lines

(Ab)using rt6_lock wouldn't work anymore if rt6_lock is converted into a per table lock.

This one looks great.

Ditto.

Quoted 5 lines

-
To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw

PATCH 2/5 : Multiple Routing Tables

In reply to Thomas Graf

Adds the framework to support multiple IPv6 routing tables. Currently all automatically generated routes are put into the same table. This could be changed at a later point after considering the produced locking overhead.

When locating routes for redirects only the main table is searched for now. Since policy rules will not be reversible it is unclear whether it makes sense to change this.

include/net/ip6_fib.h +36 -1
Index: net-2.6.git/include/net/ip6_fib.h
===================================================================
@@ -51,6 +51,8 @@ struct rt6key
 	int		plen;
 };
 
+struct fib6_table;
+
 struct rt6_info
 {
 	union {
@@ -71,6 +73,7 @@ struct rt6_info
 	u32				rt6i_flags;
 	u32				rt6i_metric;
 	atomic_t			rt6i_ref;
+	struct fib6_table		*rt6i_table;
 
 	struct rt6key			rt6i_dst;
 	struct rt6key			rt6i_src;
@@ -143,12 +146,41 @@ struct rt6_statistics {
 
 typedef void			(*f_pnode)(struct fib6_node *fn, void *);
 
-extern struct fib6_node		ip6_routing_table;
+struct fib6_table {
+	struct hlist_node	tb6_hlist;
+	u32			tb6_id;
+	rwlock_t		tb6_lock;
+	struct fib6_node	tb6_root;
+};
+
+#define RT6_TABLE_UNSPEC	RT_TABLE_UNSPEC
+#define RT6_TABLE_MAIN		RT_TABLE_MAIN
+#define RT6_TABLE_LOCAL		RT6_TABLE_MAIN
+#define RT6_TABLE_DFLT		RT6_TABLE_MAIN
+#define RT6_TABLE_INFO		RT6_TABLE_MAIN
+
+#ifdef CONFIG_IPV6_MULTIPLE_TABLES
+#define FIB6_TABLE_MIN		1
+#define FIB6_TABLE_MAX		RT_TABLE_MAX
+#else
+#define FIB6_TABLE_MIN		RT_TABLE_MAIN
+#define FIB6_TABLE_MAX		FIB6_TABLE_MIN
+#endif
+
+#define RT6_F_STRICT		1
+
+typedef struct rt6_info *(*pol_lookup_t)(struct fib6_table *,
+					 struct flowi *, int);
 
 /*
  *	exported functions
  */
 
+extern struct fib6_table *	fib6_get_table(u32 id);
+extern struct fib6_table *	fib6_new_table(u32 id);
+extern struct dst_entry *	fib6_rule_lookup(struct flowi *fl, int flags,
+						 pol_lookup_t lookup);
+
 extern struct fib6_node		*fib6_lookup(struct fib6_node *root,
 					     struct in6_addr *daddr,
 					     struct in6_addr *saddr);
@@ -161,6 +193,9 @@ extern void			fib6_clean_tree(struct fib
 						int (*func)(struct rt6_info *, void *arg),
 						int prune, void *arg);
 
+extern void			fib6_clean_all(int (*func)(struct rt6_info *, void *arg),
+					       int prune, void *arg);
+
 extern int			fib6_walk(struct fib6_walker_t *w);
 extern int			fib6_walk_continue(struct fib6_walker_t *w);
 
net/ipv6/ip6_fib.c +140 -4

175 lines not on this page · raw message

net/ipv6/route.c +255 -130

725 lines not on this page · raw message

net/ipv6/Kconfig +6 -0
Index: net-2.6.git/net/ipv6/Kconfig
===================================================================
@@ -135,3 +135,9 @@ config IPV6_TUNNEL
 
 	  If unsure, say N.
 
+config IPV6_MULTIPLE_TABLES
+	bool "IPv6: Multiple Routing Tables"
+	depends on IPV6 && EXPERIMENTAL
+	---help---
+	  Support multiple routing tables.
+
include/net/ip6_route.h +2 -1
Index: net-2.6.git/include/net/ip6_route.h
===================================================================
@@ -58,7 +58,8 @@ extern int			ipv6_route_ioctl(unsigned i
 extern int			ip6_route_add(struct in6_rtmsg *rtmsg,
 					      struct nlmsghdr *,
 					      void *rtattr,
-					      struct netlink_skb_parms *req);
+					      struct netlink_skb_parms *req,
+					      u32 table_id);
 extern int			ip6_ins_rt(struct rt6_info *,
 					   struct nlmsghdr *,
 					   void *rtattr,
net/ipv6/addrconf.c +3 -3
Index: net-2.6.git/net/ipv6/addrconf.c
===================================================================
@@ -1525,7 +1525,7 @@ addrconf_prefix_route(struct in6_addr *p
 	if (dev->type == ARPHRD_SIT && (dev->flags&IFF_POINTOPOINT))
 		rtmsg.rtmsg_flags |= RTF_NONEXTHOP;
 
-	ip6_route_add(&rtmsg, NULL, NULL, NULL);
+	ip6_route_add(&rtmsg, NULL, NULL, NULL, RT6_TABLE_MAIN);
 }
 
 /* Create "default" multicast route to the interface */
@@ -1542,7 +1542,7 @@ static void addrconf_add_mroute(struct n
 	rtmsg.rtmsg_ifindex = dev->ifindex;
 	rtmsg.rtmsg_flags = RTF_UP;
 	rtmsg.rtmsg_type = RTMSG_NEWROUTE;
-	ip6_route_add(&rtmsg, NULL, NULL, NULL);
+	ip6_route_add(&rtmsg, NULL, NULL, NULL, RT6_TABLE_MAIN);
 }
 
 static void sit_route_add(struct net_device *dev)
@@ -1559,7 +1559,7 @@ static void sit_route_add(struct net_dev
 	rtmsg.rtmsg_flags	= RTF_UP|RTF_NONEXTHOP;
 	rtmsg.rtmsg_ifindex	= dev->ifindex;
 
-	ip6_route_add(&rtmsg, NULL, NULL, NULL);
+	ip6_route_add(&rtmsg, NULL, NULL, NULL, RT6_TABLE_MAIN);
 }
 
 static void addrconf_add_lroute(struct net_device *dev)
maintainer
#7 lore raw

Re: [PATCH 2/5] [IPV6]: Multiple Routing Tables

From: Thomas Graf <[email protected]>
Date: Thu, 27 Jul 2006 00:00:02 +0200

Quoted 10 lines

Adds the framework to support multiple IPv6 routing tables. Currently all automatically generated routes are put into the same table. This could be changed at a later point after considering the produced locking overhead.

When locating routes for redirects only the main table is searched for now. Since policy rules will not be reversible it is unclear whether it makes sense to change this.

This looks good, and it seems we even fixed a bug:

net/ipv6/route.c restart: · line 618

Quoted 9 lines
net/ipv6/route.c +1 -1
@@ -586,7 +612,7 @@ restart:
 
 	dst_hold(&rt->u.dst);
 	if (nrt) {
-		err = ip6_ins_rt(nrt, NULL, NULL, &NETLINK_CB(skb));
+		err = ip6_ins_rt(nrt, NULL, NULL, NULL);
 		if (!err)
 			goto out2;
 	}

Wow, were we corrupting the IP6CB() of input packets on every route lookup that hit this path?

I'm probably the one who put that erroneous &NETLINK_CB(skb) there. Sorry :)

#8 lore raw

Re: [PATCH 2/5] [IPV6]: Multiple Routing Tables

* David Miller <[email protected]> 2006-07-26 15:39

Quoted 2 lines

Wow, were we corrupting the IP6CB() of input packets on every route lookup that hit this path?

I found that via compile error as passing on skb to this context didn't make sense when I rewrote the code around it :-)

Fortunately it's only used read-only when notifying to figure out the destination pid but that code seems bogus as well and we can probably remove the netlink cb passing hell throughout all the code there.

maintainer
#9 lore raw

Re: [PATCH 2/5] [IPV6]: Multiple Routing Tables

From: Thomas Graf <[email protected]>
Date: Thu, 27 Jul 2006 00:48:48 +0200

Quoted 4 lines

Fortunately it's only used read-only when notifying to figure out the destination pid but that code seems bogus as well and we can probably remove the netlink cb passing hell throughout all the code there.

Yes, that pid handling looks very strange. But I would search the history of this code that passes the netlink skb parms around before making any changes to things like inet6_rt_notify :)

It appears to have been added in 2.4.32, the original patch passes the raw pid around as the argument but that got changed to the current form which passes the netlink_skb_parms.

#10 lore raw

Re: [PATCH 2/5] [IPV6]: Multiple Routing Tables

In reply to Thomas Graf

Hello.

In article <[email protected]> (at Thu, 27 Jul 2006 00:00:02 +0200), Thomas Graf <[email protected]> says:

Quoted 1 line

Adds the framework to support multiple IPv6 routing tables.

Well, one design consideration that I have had for several months is performance impact.

Previously, we directly use address, ifindex etc., not flowi, in IPv6 routing code except for ip6_route_output(). This patch changes them to use flowi.
I know this should work and it is a good way for abstraction. However, initializing flowi for IPv6 is more expensive than one for IPv4, and it would result in poor performance (especially without CONFIG_IPV6_MULTIPLE_TABLES).

Am I too cautious?
Should we eat this?

--yoshfuji

maintainer
#11 lore raw

Re: [PATCH 2/5] [IPV6]: Multiple Routing Tables

From: YOSHIFUJI Hideaki <[email protected]>
Date: Sat, 29 Jul 2006 13:13:06 +0900 (JST)

Quoted 10 lines

Previously, we directly use address, ifindex etc., not flowi, in IPv6 routing code except for ip6_route_output(). This patch changes them to use flowi.
I know this should work and it is a good way for abstraction. However, initializing flowi for IPv6 is more expensive than one for IPv4, and it would result in poor performance (especially without CONFIG_IPV6_MULTIPLE_TABLES).

Am I too cautious?
Should we eat this?

I think it is a legitimate consideration.

For now I would suggest we use Thomas's approach, and we can make performance measurements to determine if it makes to optimize this or consider other methods.

#12 lore raw

Re: [PATCH 2/5] [IPV6]: Multiple Routing Tables

In article <[email protected]> (at Fri, 28 Jul 2006 21:14:28 -0700 (PDT)), David Miller <[email protected]> says:

Quoted 3 lines

For now I would suggest we use Thomas's approach, and we can make performance measurements to determine if it makes to optimize this or consider other methods.

Okay.

--yoshfuji

#13 lore raw

Re: [PATCH 2/5] [IPV6]: Multiple Routing Tables

In reply to YOSHIFUJI Hideaki / 吉藤英明

* YOSHIFUJI Hideaki / ?$B5HF#1QL@ <[email protected]> 2006-07-29 13:13

Quoted 10 lines

Well, one design consideration that I have had for several months is performance impact.

Previously, we directly use address, ifindex etc., not flowi, in IPv6 routing code except for ip6_route_output(). This patch changes them to use flowi.
I know this should work and it is a good way for abstraction. However, initializing flowi for IPv6 is more expensive than one for IPv4, and it would result in poor performance (especially without CONFIG_IPV6_MULTIPLE_TABLES).

Do you have numbers for this? Personally I'm more worried about the number of locks needed for a lookup and the selection.

#14 lore raw

Re: [PATCH 2/5] [IPV6]: Multiple Routing Tables

In reply to Thomas Graf
Quoted 4 lines · Thomas Graf

Adds the framework to support multiple IPv6 routing tables. Currently all automatically generated routes are put into the same table. This could be changed at a later point after considering the produced locking overhead.

Hi Thomes, some minor comments below.

Quoted 3 lines

When locating routes for redirects only the main table is searched for now. Since policy rules will not be reversible it is unclear whether it makes sense to change this.

This is a good point. You are absolutely correct about the policy rules.

IIRC, I initially looked through all the tables, but skipped this behavior when I rewrote the code for 2.6.11. Currently I'm once again in favor of looping through them all. This is IMO at least closer to the spirit of RFC 2461 section 8.3. where a host SHOULD update its destination cache upon receiving a redirect. If we don't look through all tables, we can't ensure this happens.

Quoted 4 lines

Index: net-2.6.git/include/net/ip6_fib.h
===================================================================

include/net/ip6_fib.h +0 -0

<snip>

Quoted 7 lines
include/net/ip6_fib.h +2 -1
@@ -143,12 +146,41 @@ struct rt6_statistics {
 
 typedef void			(*f_pnode)(struct fib6_node *fn, void *);
 
-extern struct fib6_node		ip6_routing_table;
+struct fib6_table {
+	struct hlist_node	tb6_hlist;

include/net/ip6_fib.h struct rt6_statistics { · line 160

Quoted 10 lines
include/net/ip6_fib.h +10 -0
+	u32			tb6_id;
+	rwlock_t		tb6_lock;
+	struct fib6_node	tb6_root;
+};
+
+#define RT6_TABLE_UNSPEC	RT_TABLE_UNSPEC
+#define RT6_TABLE_MAIN		RT_TABLE_MAIN
+#define RT6_TABLE_LOCAL		RT6_TABLE_MAIN
+#define RT6_TABLE_DFLT		RT6_TABLE_MAIN
+#define RT6_TABLE_INFO		RT6_TABLE_MAIN

IMO it's a bit inconsistent to define a separate table entry for Route Information generated routes, but not Prefix Information based ones. What do you say about adding a RT6_TABLE_PRFX?

Quoted 4 lines

Index: net-2.6.git/net/ipv6/route.c
===================================================================

net/ipv6/route.c +0 -0

<snip>

net/ipv6/route.c static struct rt6_info *rt6_add_route_in · line 1529

Quoted 10 lines
net/ipv6/route.c +3 -2
@@ -1435,12 +1523,15 @@ static struct rt6_info *rt6_add_route_in
 struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *dev)
 {	
 	struct rt6_info *rt;
-	struct fib6_node *fn;
+	struct fib6_table *table;
 
-	fn = &ip6_routing_table;
+	/* TODO: It might be better to search all tables */
+	table = fib6_get_table(RT6_TABLE_DFLT);

As long as the table for default routes is RT6_TABLE_DFLT and can't be configured by the user, I think the correct behavior is just to search RT6_TABLE_DFLT.

Otherwise it looks very good!

Regards,
Ville

#15 lore raw

Re: [PATCH 2/5] [IPV6]: Multiple Routing Tables

Quoted 5 lines · Ville Nuorvala
Quoted 4 lines

When locating routes for redirects only the main table is searched for now. Since policy rules will not be reversible it is unclear whether it makes sense to change this.

Quoted 8 lines

This is a good point. You are absolutely correct about the policy rules.

IIRC, I initially looked through all the tables, but skipped this behavior when I rewrote the code for 2.6.11. Currently I'm once again in favor of looping through them all. This is IMO at least closer to the spirit of RFC 2461 section 8.3. where a host SHOULD update its destination cache upon receiving a redirect. If we don't look through all tables, we can't ensure this happens.

Without a route cache, I think our only choice is to search through all tables. The same thing applies to PMTU updates as well.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#16 lore raw

Re: [PATCH 2/5] [IPV6]: Multiple Routing Tables

Quoted 15 lines · Herbert Xu
Quoted 5 lines · Ville Nuorvala
Quoted 4 lines

When locating routes for redirects only the main table is searched for now. Since policy rules will not be reversible it is unclear whether it makes sense to change this.

Quoted 8 lines

This is a good point. You are absolutely correct about the policy rules.

IIRC, I initially looked through all the tables, but skipped this behavior when I rewrote the code for 2.6.11. Currently I'm once again in favor of looping through them all. This is IMO at least closer to the spirit of RFC 2461 section 8.3. where a host SHOULD update its destination cache upon receiving a redirect. If we don't look through all tables, we can't ensure this happens.

Quoted 2 lines

Without a route cache, I think our only choice is to search through all tables. The same thing applies to PMTU updates as well.

Actually, if we're adding policy routing, we should seriously consider whether living without a routing cache is still viable or not because the cost of a route lookup has just gone up.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#17 lore raw

Re: [PATCH 2/5] [IPV6]: Multiple Routing Tables

* Herbert Xu <[email protected]> 2006-08-01 00:01

Quoted 2 lines

Without a route cache, I think our only choice is to search through all tables. The same thing applies to PMTU updates as well.

I think PMTU etc. should be moved out of the route into a some form of flow cache. It's currently using rt6_lookup() which even goes through the rules.

Doing a few thousand trie lookups after Patrick's changes in the worst case for every redirect might be acceptable but doing so for every PMTU update could become an issue.

Quoted 3 lines

Actually, if we're adding policy routing, we should seriously consider whether living without a routing cache is still viable or not because the cost of a route lookup has just gone up.

Absolutely.

maintainer
#18 lore raw

Re: [PATCH 2/5] [IPV6]: Multiple Routing Tables

From: Thomas Graf <[email protected]>
Date: Mon, 31 Jul 2006 17:41:42 +0200

Quoted 6 lines

* Herbert Xu <[email protected]> 2006-08-01 00:01

Quoted 3 lines

Actually, if we're adding policy routing, we should seriously consider whether living without a routing cache is still viable or not because the cost of a route lookup has just gone up.

Absolutely.

This is something I wanted to bring up too.

#19 lore raw

Re: [PATCH 2/5] [IPV6]: Multiple Routing Tables

In reply to Ville Nuorvala

* Ville Nuorvala <[email protected]> 2006-07-31 16:55

Quoted 4 lines
Quoted 3 lines

When locating routes for redirects only the main table is searched for now. Since policy rules will not be reversible it is unclear whether it makes sense to change this.

Quoted 8 lines

This is a good point. You are absolutely correct about the policy rules.

IIRC, I initially looked through all the tables, but skipped this behavior when I rewrote the code for 2.6.11. Currently I'm once again in favor of looping through them all. This is IMO at least closer to the spirit of RFC 2461 section 8.3. where a host SHOULD update its destination cache upon receiving a redirect. If we don't look through all tables, we can't ensure this happens.

I agree, it will depend on what way is being followed regarding a flow cache or route cache.

Quoted 9 lines

include/net/ip6_fib.h struct rt6_statistics { · line 160

Quoted 5 lines
include/net/ip6_fib.h +5 -0
+#define RT6_TABLE_UNSPEC	RT_TABLE_UNSPEC
+#define RT6_TABLE_MAIN		RT_TABLE_MAIN
+#define RT6_TABLE_LOCAL		RT6_TABLE_MAIN
+#define RT6_TABLE_DFLT		RT6_TABLE_MAIN
+#define RT6_TABLE_INFO		RT6_TABLE_MAIN

IMO it's a bit inconsistent to define a separate table entry for Route Information generated routes, but not Prefix Information based ones. What do you say about adding a RT6_TABLE_PRFX?

Sounds good.

Quoted 7 lines
Quoted 7 lines
net/ipv6/route.c +1 -1
@@ -1435,12 +1523,15 @@ static struct rt6_info *rt6_add_route_in
 struct rt6_info *rt6_get_dflt_router(struct in6_addr *addr, struct net_device *dev)
 {	
 	struct rt6_info *rt;
-	struct fib6_node *fn;
+	struct fib6_table *table;
 
Quoted 7 lines

net/ipv6/route.c static struct rt6_info *rt6_add_route_in · line 1529

Quoted 3 lines
net/ipv6/route.c +2 -1
-	fn = &ip6_routing_table;
+	/* TODO: It might be better to search all tables */
+	table = fib6_get_table(RT6_TABLE_DFLT);

As long as the table for default routes is RT6_TABLE_DFLT and can't be configured by the user, I think the correct behavior is just to search RT6_TABLE_DFLT.

I agree, I intended to remove that comment but missed it.

#20 lore raw

PATCH 3/5 : Protocol Independant Policy Routing Rules Framework

In reply to Thomas Graf

Derived from net/ipv6/fib_rules.c

include/linux/fib_rules.h new file +60 -0
Index: net-2.6.git/include/linux/fib_rules.h
===================================================================
@@ -0,0 +1,60 @@
+#ifndef __LINUX_FIB_RULES_H
+#define __LINUX_FIB_RULES_H
+
+#include <linux/types.h>
+#include <linux/rtnetlink.h>
+
+/* rule is permanent, and cannot be deleted */
+#define FIB_RULE_PERMANENT	1
+
+struct fib_rule_hdr
+{
+	__u8		family;
+	__u8		dst_len;
+	__u8		src_len;
+	__u8		tos;
+
+	__u8		table;
+	__u8		res1;	/* reserved */
+	__u8		res2;	/* reserved */
+	__u8		action;
+
+	__u32		flags;
+};
+
+enum
+{
+	FRA_UNSPEC,
+	FRA_DST,	/* destination address */
+	FRA_SRC,	/* source address */
+	FRA_IFNAME,	/* interface name */
+	FRA_UNUSED1,
+	FRA_UNUSED2,
+	FRA_PRIORITY,	/* priority/preference */
+	FRA_UNUSED3,
+	FRA_UNUSED4,
+	FRA_UNUSED5,
+	FRA_FWMARK,	/* netfilter mark (IPv4) */
+	FRA_FLOW,	/* flow/class id */
+	__FRA_MAX
+};
+
+#define FRA_MAX (__FRA_MAX - 1)
+
+enum
+{
+	FR_ACT_UNSPEC,
+	FR_ACT_TO_TBL,		/* Pass to fixed table */
+	FR_ACT_RES1,
+	FR_ACT_RES2,
+	FR_ACT_RES3,
+	FR_ACT_RES4,
+	FR_ACT_BLACKHOLE,	/* Drop without notification */
+	FR_ACT_UNREACHABLE,	/* Drop with ENETUNREACH */
+	FR_ACT_PROHIBIT,	/* Drop with EACCES */
+	__FR_ACT_MAX,
+};
+
+#define FR_ACT_MAX (__FR_ACT_MAX - 1)
+
+#endif
include/net/fib_rules.h new file +89 -0

89 lines not on this page · raw message

net/Kconfig +3 -0
Index: net-2.6.git/net/Kconfig
===================================================================
@@ -249,6 +249,9 @@ source "net/ieee80211/Kconfig"
 config WIRELESS_EXT
 	bool
 
+config FIB_RULES
+	bool
+
 endif   # if NET
 endmenu # Networking
 
net/core/Makefile +1 -0
Index: net-2.6.git/net/core/Makefile
===================================================================
@@ -17,3 +17,4 @@ obj-$(CONFIG_NET_PKTGEN) += pktgen.o
 obj-$(CONFIG_WIRELESS_EXT) += wireless.o
 obj-$(CONFIG_NETPOLL) += netpoll.o
 obj-$(CONFIG_NET_DMA) += user_dma.o
+obj-$(CONFIG_FIB_RULES) += fib_rules.o
net/core/fib_rules.c new file +410 -0

410 lines not on this page · raw message

net/core/rtnetlink.c +7 -2
Index: net-2.6.git/net/core/rtnetlink.c
===================================================================
@@ -49,6 +49,7 @@
 #include <net/udp.h>
 #include <net/sock.h>
 #include <net/pkt_sched.h>
+#include <net/fib_rules.h>
 #include <net/netlink.h>
 #ifdef CONFIG_NET_WIRELESS_RTNETLINK
 #include <linux/wireless.h>
@@ -103,7 +104,7 @@ static const int rtm_min[RTM_NR_FAMILIES
 	[RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
 	[RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
 	[RTM_FAM(RTM_NEWNEIGH)]     = NLMSG_LENGTH(sizeof(struct ndmsg)),
-	[RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct rtmsg)),
+	[RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
 	[RTM_FAM(RTM_NEWQDISC)]     = NLMSG_LENGTH(sizeof(struct tcmsg)),
 	[RTM_FAM(RTM_NEWTCLASS)]    = NLMSG_LENGTH(sizeof(struct tcmsg)),
 	[RTM_FAM(RTM_NEWTFILTER)]   = NLMSG_LENGTH(sizeof(struct tcmsg)),
@@ -120,7 +121,7 @@ static const int rta_max[RTM_NR_FAMILIES
 	[RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
 	[RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
 	[RTM_FAM(RTM_NEWNEIGH)]     = NDA_MAX,
-	[RTM_FAM(RTM_NEWRULE)]      = RTA_MAX,
+	[RTM_FAM(RTM_NEWRULE)]      = FRA_MAX,
 	[RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
 	[RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
 	[RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
@@ -744,6 +745,10 @@ static struct rtnetlink_link link_rtnetl
 	[RTM_NEWNEIGH    - RTM_BASE] = { .doit   = neigh_add		 },
 	[RTM_DELNEIGH    - RTM_BASE] = { .doit   = neigh_delete		 },
 	[RTM_GETNEIGH    - RTM_BASE] = { .dumpit = neigh_dump_info	 },
+#ifdef CONFIG_FIB_RULES
+	[RTM_NEWRULE     - RTM_BASE] = { .doit   = fib_nl_newrule	 },
+	[RTM_DELRULE     - RTM_BASE] = { .doit   = fib_nl_delrule	 },
+#endif
 	[RTM_GETRULE     - RTM_BASE] = { .dumpit = rtnetlink_dump_all	 },
 	[RTM_GETNEIGHTBL - RTM_BASE] = { .dumpit = neightbl_dump_info	 },
 	[RTM_SETNEIGHTBL - RTM_BASE] = { .doit   = neightbl_set		 },
#22 lore raw

Re: [PATCH 3/5] [NET]: Protocol Independant Policy Routing Rules Framework

In reply to Thomas Graf

net/core/fib_rules.c line 168

Quoted 7 lines · Thomas Graf
net/core/fib_rules.c new file +7 -0
+	rule = kmalloc(ops->rule_size, GFP_KERNEL);
+	if (rule == NULL) {
+		err = -ENOMEM;
+		goto errout;
+	}
+	memset(rule, 0, ops->rule_size);
+

kzalloc() ? :-)

- James

Signature · 2 lines
James Morris
<[email protected]>
maintainer
#23 lore raw

Re: [PATCH 3/5] [NET]: Protocol Independant Policy Routing Rules Framework

From: James Morris <[email protected]>
Date: Thu, 27 Jul 2006 01:58:58 -0400 (EDT)

Quoted 11 lines · Thomas Graf

net/core/fib_rules.c line 168

Quoted 8 lines
net/core/fib_rules.c new file +7 -0
+	rule = kmalloc(ops->rule_size, GFP_KERNEL);
+	if (rule == NULL) {
+		err = -ENOMEM;
+		goto errout;
+	}
+	memset(rule, 0, ops->rule_size);
+

kzalloc() ? :-)

Good catch :) He did use kzalloc() in a few other places, to his credit.

#24 lore raw

PATCH RESEND 3/5 : Protocol Independant Policy Routing Rules Framework

Derived from net/ipv6/fib_rules.c

include/linux/fib_rules.h new file +60 -0
Index: net-2.6.git/include/linux/fib_rules.h
===================================================================
@@ -0,0 +1,60 @@
+#ifndef __LINUX_FIB_RULES_H
+#define __LINUX_FIB_RULES_H
+
+#include <linux/types.h>
+#include <linux/rtnetlink.h>
+
+/* rule is permanent, and cannot be deleted */
+#define FIB_RULE_PERMANENT	1
+
+struct fib_rule_hdr
+{
+	__u8		family;
+	__u8		dst_len;
+	__u8		src_len;
+	__u8		tos;
+
+	__u8		table;
+	__u8		res1;	/* reserved */
+	__u8		res2;	/* reserved */
+	__u8		action;
+
+	__u32		flags;
+};
+
+enum
+{
+	FRA_UNSPEC,
+	FRA_DST,	/* destination address */
+	FRA_SRC,	/* source address */
+	FRA_IFNAME,	/* interface name */
+	FRA_UNUSED1,
+	FRA_UNUSED2,
+	FRA_PRIORITY,	/* priority/preference */
+	FRA_UNUSED3,
+	FRA_UNUSED4,
+	FRA_UNUSED5,
+	FRA_FWMARK,	/* netfilter mark (IPv4) */
+	FRA_FLOW,	/* flow/class id */
+	__FRA_MAX
+};
+
+#define FRA_MAX (__FRA_MAX - 1)
+
+enum
+{
+	FR_ACT_UNSPEC,
+	FR_ACT_TO_TBL,		/* Pass to fixed table */
+	FR_ACT_RES1,
+	FR_ACT_RES2,
+	FR_ACT_RES3,
+	FR_ACT_RES4,
+	FR_ACT_BLACKHOLE,	/* Drop without notification */
+	FR_ACT_UNREACHABLE,	/* Drop with ENETUNREACH */
+	FR_ACT_PROHIBIT,	/* Drop with EACCES */
+	__FR_ACT_MAX,
+};
+
+#define FR_ACT_MAX (__FR_ACT_MAX - 1)
+
+#endif
include/net/fib_rules.h new file +89 -0

89 lines not on this page · raw message

net/Kconfig +3 -0
Index: net-2.6.git/net/Kconfig
===================================================================
@@ -249,6 +249,9 @@ source "net/ieee80211/Kconfig"
 config WIRELESS_EXT
 	bool
 
+config FIB_RULES
+	bool
+
 endif   # if NET
 endmenu # Networking
 
net/core/Makefile +1 -0
Index: net-2.6.git/net/core/Makefile
===================================================================
@@ -17,3 +17,4 @@ obj-$(CONFIG_NET_PKTGEN) += pktgen.o
 obj-$(CONFIG_WIRELESS_EXT) += wireless.o
 obj-$(CONFIG_NETPOLL) += netpoll.o
 obj-$(CONFIG_NET_DMA) += user_dma.o
+obj-$(CONFIG_FIB_RULES) += fib_rules.o
net/core/fib_rules.c new file +417 -0

417 lines not on this page · raw message

net/core/rtnetlink.c +7 -2
Index: net-2.6.git/net/core/rtnetlink.c
===================================================================
@@ -49,6 +49,7 @@
 #include <net/udp.h>
 #include <net/sock.h>
 #include <net/pkt_sched.h>
+#include <net/fib_rules.h>
 #include <net/netlink.h>
 #ifdef CONFIG_NET_WIRELESS_RTNETLINK
 #include <linux/wireless.h>
@@ -103,7 +104,7 @@ static const int rtm_min[RTM_NR_FAMILIES
 	[RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
 	[RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
 	[RTM_FAM(RTM_NEWNEIGH)]     = NLMSG_LENGTH(sizeof(struct ndmsg)),
-	[RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct rtmsg)),
+	[RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
 	[RTM_FAM(RTM_NEWQDISC)]     = NLMSG_LENGTH(sizeof(struct tcmsg)),
 	[RTM_FAM(RTM_NEWTCLASS)]    = NLMSG_LENGTH(sizeof(struct tcmsg)),
 	[RTM_FAM(RTM_NEWTFILTER)]   = NLMSG_LENGTH(sizeof(struct tcmsg)),
@@ -120,7 +121,7 @@ static const int rta_max[RTM_NR_FAMILIES
 	[RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
 	[RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
 	[RTM_FAM(RTM_NEWNEIGH)]     = NDA_MAX,
-	[RTM_FAM(RTM_NEWRULE)]      = RTA_MAX,
+	[RTM_FAM(RTM_NEWRULE)]      = FRA_MAX,
 	[RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
 	[RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
 	[RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
@@ -744,6 +745,10 @@ static struct rtnetlink_link link_rtnetl
 	[RTM_NEWNEIGH    - RTM_BASE] = { .doit   = neigh_add		 },
 	[RTM_DELNEIGH    - RTM_BASE] = { .doit   = neigh_delete		 },
 	[RTM_GETNEIGH    - RTM_BASE] = { .dumpit = neigh_dump_info	 },
+#ifdef CONFIG_FIB_RULES
+	[RTM_NEWRULE     - RTM_BASE] = { .doit   = fib_nl_newrule	 },
+	[RTM_DELRULE     - RTM_BASE] = { .doit   = fib_nl_delrule	 },
+#endif
 	[RTM_GETRULE     - RTM_BASE] = { .dumpit = rtnetlink_dump_all	 },
 	[RTM_GETNEIGHTBL - RTM_BASE] = { .dumpit = neightbl_dump_info	 },
 	[RTM_SETNEIGHTBL - RTM_BASE] = { .doit   = neightbl_set		 },
#25 lore raw

Re: [RESEND 3/5] [NET]: Protocol Independant Policy Routing Rules Framework

Quoted 1 line · Thomas Graf

Derived from net/ipv6/fib_rules.c

This clashes with my routing table patch, guess we have to figure out who should go first :)

net/core/fib_rules.c line 118

Quoted 9 lines
net/core/fib_rules.c new file +9 -0
+int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
+		     int flags, struct fib_lookup_arg *arg)
+{
+	struct fib_rule *rule;
+	int err;
+
+	rcu_read_lock();
+
+	list_for_each_entry(rule, ops->rules_list, list) {

Shouldn't that be list_for_each_entry_rcu?

Quoted 9 lines
net/core/fib_rules.c new file +9 -0
+		if (rule->ifname[0] && (rule->ifindex != fl->iif))
+			continue;
+
+		if (!ops->match(rule, fl, flags))
+			continue;
+
+		rcu_read_unlock();
+
+		err = ops->action(rule, fl, flags, arg);

net/core/fib_rules.c line 137

Quoted 10 lines
net/core/fib_rules.c new file +10 -0
+		if (err != -EAGAIN) {
+			fib_rule_get(rule);
+			arg->rule = rule;
+			goto out;
+		}
+	}
+
+	err = -ENETUNREACH;
+out:
+	rcu_read_unlock();

rcu_read_unlock might get called multiple times in the list iteration and once again here.

maintainer
#26 lore raw

Re: [RESEND 3/5] [NET]: Protocol Independant Policy Routing Rules Framework

From: Patrick McHardy <[email protected]>
Date: Fri, 28 Jul 2006 00:58:49 +0200

Quoted 5 lines
Quoted 1 line · Thomas Graf

Derived from net/ipv6/fib_rules.c

This clashes with my routing table patch, guess we have to figure out who should go first :)

I think since USAGI has some work that depends on this, we should get Thomas's stuff in first.

It shouldn't be a big deal to rework your >256 tables patch against Thomas's should it?

#27 lore raw

Re: [RESEND 3/5] [NET]: Protocol Independant Policy Routing Rules Framework

Quoted 9 lines · David Miller

From: Patrick McHardy <[email protected]>
Date: Fri, 28 Jul 2006 00:58:49 +0200

Quoted 2 lines

This clashes with my routing table patch, guess we have to figure out who should go first :)

I think since USAGI has some work that depends on this, we should get Thomas's stuff in first.

OK.

Quoted 2 lines

It shouldn't be a big deal to rework your >256 tables patch against Thomas's should it?

No, that shouldn't be much work.

#28 lore raw

Re: [RESEND 3/5] [NET]: Protocol Independant Policy Routing Rules Framework

In reply to Patrick McHardy
Quoted 11 lines · Patrick McHardy

net/core/fib_rules.c line 118

Quoted 9 lines
net/core/fib_rules.c new file +9 -0
+int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
+		     int flags, struct fib_lookup_arg *arg)
+{
+	struct fib_rule *rule;
+	int err;
+
+	rcu_read_lock();
+
+	list_for_each_entry(rule, ops->rules_list, list) {

Shouldn't that be list_for_each_entry_rcu?

Yes that's correct, it should.

Quoted 14 lines
Quoted 14 lines
net/core/fib_rules.c new file +14 -0
+		if (rule->ifname[0] && (rule->ifindex != fl->iif))
+			continue;
+
+		if (!ops->match(rule, fl, flags))
+			continue;
+
+		rcu_read_unlock();
+
+		err = ops->action(rule, fl, flags, arg);
+		if (err != -EAGAIN) {
+			fib_rule_get(rule);
+			arg->rule = rule;
+			goto out;
+		}
Quoted 8 lines

net/core/fib_rules.c line 137

Quoted 5 lines
net/core/fib_rules.c new file +5 -0
+	}
+
+	err = -ENETUNREACH;
+out:
+	rcu_read_unlock();

rcu_read_unlock might get called multiple times in the list iteration and once again here.

Yes, the rcu_read_unlock() in the list iteration is misplaced, it shouldn't be there. Besides the unbalanced lock/unlocks it suffers from the general issue described below

As a somewhat related note, I've just digged a bit through RCU land, talked to dipankar and mckenney, and discovered that rcu_read_lock() / rcu_read_unlock() aren't strictly needed in softirqs since preempt is already disabled in softirqs. This means that you can use the result of the rcu read-side critical outside of the rcu_read_lock() / rcu_read_unlock() section. BUT this changes with the -rt kernel where softirqs are preemptable and where rcu_read_lock() / rcu_read_unlock() doesn't disable/enable preempt anymore, which means the rcu read-side critical section is also preemptable. This means that we can get preempted in the read-side critical section but the resulting grace period won't occur until rcu_read_unlock() is called, which means that using results of an read-side critical section outside of the critical section is just not going to work in softirqs in -rt kernels. I'm sure Ingo has reviewed the RCU usage in softirqs but I don't know if there's been any changes in this area after his review.

Signature · 1 line
/Martin
#29 lore raw

Re: [RESEND 3/5] [NET]: Protocol Independant Policy Routing Rules Framework

Quoted 5 lines · Martin Josefsson

As a somewhat related note, I've just digged a bit through RCU land, talked to dipankar and mckenney, and discovered that rcu_read_lock() / rcu_read_unlock() aren't strictly needed in softirqs since preempt is already disabled in softirqs. This means that you can use the result of the rcu read-side critical outside of the rcu_read_lock() /

Thats true, but in this case the code is executed both in softirq- and user-context. Using rcu_read_lock and still relying on softirq properties outside the locked section is also very confusing in my opinion.

#30 lore raw

Re: [RESEND 3/5] [NET]: Protocol Independant Policy Routing Rules Framework

Quoted 11 lines · Patrick McHardy
Quoted 5 lines · Martin Josefsson

As a somewhat related note, I've just digged a bit through RCU land, talked to dipankar and mckenney, and discovered that rcu_read_lock() / rcu_read_unlock() aren't strictly needed in softirqs since preempt is already disabled in softirqs. This means that you can use the result of the rcu read-side critical outside of the rcu_read_lock() /

Thats true, but in this case the code is executed both in softirq- and user-context. Using rcu_read_lock and still relying on softirq properties outside the locked section is also very confusing in my opinion.

Yes it is very fishy relying on the softirq properties, especially since they don't apply to -rt kernels and that there might be other changes in this area in the future. It's not recommended.

Signature · 1 line
/Martin
#31 lore raw

Re: [RESEND 3/5] [NET]: Protocol Independant Policy Routing Rules Framework

In reply to Thomas Graf
Quoted 6 lines · Thomas Graf
net/core/fib_rules.c new file +4 -0
+int fib_rules_register(struct fib_rules_ops *ops)
+{
+	int err = -EEXIST;
+	struct fib_rules_ops *o;

net/core/fib_rules.c line 60

Quoted 10 lines
net/core/fib_rules.c new file +10 -0
+
+	if (ops->rule_size < sizeof(struct fib_rule))
+		return -EINVAL;
+
+	if (ops->match == NULL || ops->configure == NULL ||
+	    ops->compare == NULL || ops->fill == NULL ||
+	    ops->action == NULL)
+		return -EINVAL;
+
+	spin_lock_bh(&rules_mod_lock);

This doesn't look like it needs bh protection.

Quoted 50 lines
net/core/fib_rules.c new file +50 -0
+	list_for_each_entry(o, &rules_ops, list)
+		if (ops->family == o->family)
+			goto errout;
+
+	list_add_tail_rcu(&ops->list, &rules_ops);
+	err = 0;
+errout:
+	spin_unlock_bh(&rules_mod_lock);
+
+	return err;
+}
+
+EXPORT_SYMBOL_GPL(fib_rules_register);
+
+static void cleanup_ops(struct fib_rules_ops *ops)
+{
+	struct fib_rule *rule, *tmp;
+
+	list_for_each_entry_safe(rule, tmp, ops->rules_list, list) {
+		list_del_rcu(&rule->list);
+		fib_rule_put(rule);
+	}
+}
+
+int fib_rules_unregister(struct fib_rules_ops *ops)
+{
+	int err = 0;
+	struct fib_rules_ops *o;
+
+	spin_lock_bh(&rules_mod_lock);
+	list_for_each_entry(o, &rules_ops, list) {
+		if (o == ops) {
+			list_del_rcu(&o->list);
+			cleanup_ops(ops);
+			goto out;
+		}
+	}
+
+	err = -ENOENT;
+out:
+	spin_unlock_bh(&rules_mod_lock);
+
+	synchronize_net();
+
+	return err;
+}
+
+EXPORT_SYMBOL_GPL(fib_rules_unregister);
+
+int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,

net/core/fib_rules.c line 120

Quoted 10 lines
net/core/fib_rules.c new file +10 -0
+		     int flags, struct fib_lookup_arg *arg)
+{
+	struct fib_rule *rule;
+	int err;
+
+	rcu_read_lock();
+
+	list_for_each_entry(rule, ops->rules_list, list) {
+		if (rule->ifname[0] && (rule->ifindex != fl->iif))
+			continue;

ifindex may be unset even if ifname is set (in case the interface does not exist yet). In that case it will match falsely on locally generated packets.

net/core/fib_rules.c line 132

Quoted 12 lines
net/core/fib_rules.c new file +12 -0
+
+		if (!ops->match(rule, fl, flags))
+			continue;
+
+		rcu_read_unlock();
+
+		err = ops->action(rule, fl, flags, arg);
+		if (err != -EAGAIN) {
+			fib_rule_get(rule);
+			arg->rule = rule;
+			goto out;
+		}

This seems to race with fib_nl_delrule:

CPU1 CPU2

			list_for_each_entry -> find matching entry

list_del_rcu
fib_rule_put
call_rcu(fib_rule_put_rcu)
fib_rule_get

Moving fib_rule_get inside the rcu protected section and calling synchronize_rcu before fib_rule_put in fib_nl_delrule looks like the easiest fix.

Quoted 217 lines
net/core/fib_rules.c new file +217 -0
+	}
+
+	err = -ENETUNREACH;
+out:
+	rcu_read_unlock();
+
+	return err;
+}
+
+EXPORT_SYMBOL_GPL(fib_rules_lookup);
+
+int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
+{
+	struct fib_rule_hdr *frh = nlmsg_data(nlh);
+	struct fib_rules_ops *ops = NULL;
+	struct fib_rule *rule, *r, *last = NULL;
+	struct nlattr *tb[FRA_MAX+1];
+	int err = -EINVAL;
+
+	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
+		goto errout;
+
+	ops = lookup_rules_ops(frh->family);
+	if (ops == NULL) {
+		err = EAFNOSUPPORT;
+		goto errout;
+	}
+
+	err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
+	if (err < 0)
+		goto errout;
+
+	if (tb[FRA_IFNAME] && nla_len(tb[FRA_IFNAME]) > IFNAMSIZ)
+		goto errout;
+
+	rule = kzalloc(ops->rule_size, GFP_KERNEL);
+	if (rule == NULL) {
+		err = -ENOMEM;
+		goto errout;
+	}
+
+	if (tb[FRA_PRIORITY])
+		rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
+
+	if (tb[FRA_IFNAME]) {
+		struct net_device *dev;
+
+		rule->ifindex = -1;
+		if (nla_strlcpy(rule->ifname, tb[FRA_IFNAME],
+				IFNAMSIZ) >= IFNAMSIZ)
+			goto errout_free;
+
+		dev = __dev_get_by_name(rule->ifname);
+		if (dev)
+			rule->ifindex = dev->ifindex;
+	}
+
+	rule->action = frh->action;
+	rule->flags = frh->flags;
+	rule->table = frh->table;
+
+	if (!rule->pref && ops->default_pref)
+		rule->pref = ops->default_pref();
+
+	err = ops->configure(rule, skb, nlh, frh, tb);
+	if (err < 0)
+		goto errout_free;
+
+	list_for_each_entry(r, ops->rules_list, list) {
+		if (r->pref > rule->pref)
+			break;
+		last = r;
+	}
+
+	fib_rule_get(rule);
+
+	if (last)
+		list_add_rcu(&rule->list, &last->list);
+	else
+		list_add_rcu(&rule->list, ops->rules_list);
+
+	notify_rule_change(RTM_NEWRULE, rule, ops);
+	rules_ops_put(ops);
+	return 0;
+
+errout_free:
+	kfree(rule);
+errout:
+	rules_ops_put(ops);
+	return err;
+}
+
+int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
+{
+	struct fib_rule_hdr *frh = nlmsg_data(nlh);
+	struct fib_rules_ops *ops = NULL;
+	struct fib_rule *rule;
+	struct nlattr *tb[FRA_MAX+1];
+	int err = -EINVAL;
+
+	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
+		goto errout;
+
+	ops = lookup_rules_ops(frh->family);
+	if (ops == NULL) {
+		err = EAFNOSUPPORT;
+		goto errout;
+	}
+
+	err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
+	if (err < 0)
+		goto errout;
+
+	list_for_each_entry(rule, ops->rules_list, list) {
+		if (frh->action && (frh->action != rule->action))
+			continue;
+
+		if (frh->table && (frh->table != rule->table))
+			continue;
+
+		if (tb[FRA_PRIORITY] &&
+		    (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
+			continue;
+
+		if (tb[FRA_IFNAME] &&
+		    nla_strcmp(tb[FRA_IFNAME], rule->ifname))
+			continue;
+
+		if (!ops->compare(rule, frh, tb))
+			continue;
+
+		if (rule->flags & FIB_RULE_PERMANENT) {
+			err = -EPERM;
+			goto errout;
+		}
+
+		list_del_rcu(&rule->list);
+		notify_rule_change(RTM_DELRULE, rule, ops);
+		fib_rule_put(rule);
+		rules_ops_put(ops);
+		return 0;
+	}
+
+	err = -ENOENT;
+errout:
+	rules_ops_put(ops);
+	return err;
+}
+
+static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
+			    u32 pid, u32 seq, int type, int flags,
+			    struct fib_rules_ops *ops)
+{
+	struct nlmsghdr *nlh;
+	struct fib_rule_hdr *frh;
+
+	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
+	if (nlh == NULL)
+		return -1;
+
+	frh = nlmsg_data(nlh);
+	frh->table = rule->table;
+	frh->res1 = 0;
+	frh->res2 = 0;
+	frh->action = rule->action;
+	frh->flags = rule->flags;
+
+	if (rule->ifname[0])
+		NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);
+
+	if (rule->pref)
+		NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
+
+	if (ops->fill(rule, skb, nlh, frh) < 0)
+		goto nla_put_failure;
+
+	return nlmsg_end(skb, nlh);
+
+nla_put_failure:
+	return nlmsg_cancel(skb, nlh);
+}
+
+int fib_rules_dump(struct sk_buff *skb, struct netlink_callback *cb, int family)
+{
+	int idx = 0;
+	struct fib_rule *rule;
+	struct fib_rules_ops *ops;
+
+	ops = lookup_rules_ops(family);
+	if (ops == NULL)
+		return -EAFNOSUPPORT;
+
+	rcu_read_lock();
+	list_for_each_entry(rule, ops->rules_list, list) {
+		if (idx < cb->args[0])
+			goto skip;
+
+		if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
+				     cb->nlh->nlmsg_seq, RTM_NEWRULE,
+				     NLM_F_MULTI, ops) < 0)
+			break;
+skip:
+		idx++;
+	}
+	rcu_read_unlock();
+	cb->args[0] = idx;
+	rules_ops_put(ops);
+
+	return skb->len;
+}
+
+EXPORT_SYMBOL_GPL(fib_rules_dump);
+
+static void notify_rule_change(int event, struct fib_rule *rule,
+			       struct fib_rules_ops *ops)
+{
+	int size = nlmsg_total_size(sizeof(struct fib_rule_hdr) + 128);

net/core/fib_rules.c line 359

Quoted 10 lines
net/core/fib_rules.c new file +10 -0
+	struct sk_buff *skb = alloc_skb(size, GFP_KERNEL);
+
+	if (skb == NULL)
+		netlink_set_err(rtnl, 0, RTNLGRP_IPV4_RULE, ENOBUFS);
+	else if (fib_nl_fill_rule(skb, rule, 0, 0, event, 0, ops) < 0) {
+		kfree_skb(skb);
+		netlink_set_err(rtnl, 0, RTNLGRP_IPV4_RULE, EINVAL);
+	} else
+		netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV4_RULE, GFP_KERNEL);
+}

Shouldn't different families use different groups? Userspace might (rightfully, I think) expect not to see anything but IPv4 rules on RTNLGRP_IPV4_RULE.

Quoted 57 lines
net/core/fib_rules.c new file +57 -0
+static void attach_rules(struct list_head *rules, struct net_device *dev)
+{
+	struct fib_rule *rule;
+
+	list_for_each_entry(rule, rules, list) {
+		if (rule->ifindex == -1 &&
+		    strcmp(dev->name, rule->ifname) == 0)
+			rule->ifindex = dev->ifindex;
+	}
+}
+
+static void detach_rules(struct list_head *rules, struct net_device *dev)
+{
+	struct fib_rule *rule;
+
+	list_for_each_entry(rule, rules, list)
+		if (rule->ifindex == dev->ifindex)
+			rule->ifindex = -1;
+}
+
+
+static int fib_rules_event(struct notifier_block *this, unsigned long event,
+			    void *ptr)
+{
+	struct net_device *dev = ptr;
+	struct fib_rules_ops *ops;
+
+	ASSERT_RTNL();
+	rcu_read_lock();
+
+	switch (event) {
+	case NETDEV_REGISTER:
+		list_for_each_entry(ops, &rules_ops, list)
+			attach_rules(ops->rules_list, dev);
+		break;
+
+	case NETDEV_UNREGISTER:
+		list_for_each_entry(ops, &rules_ops, list)
+			detach_rules(ops->rules_list, dev);
+		break;
+	}
+
+	rcu_read_unlock();
+
+	return NOTIFY_DONE;
+}
+
+static struct notifier_block fib_rules_notifier = {
+	.notifier_call = fib_rules_event,
+};
+
+static int __init fib_rules_init(void)
+{
+	return register_netdevice_notifier(&fib_rules_notifier);
+}
+
+subsys_initcall(fib_rules_init);
#32 lore raw

Re: [RESEND 3/5] [NET]: Protocol Independant Policy Routing Rules Framework

* Patrick McHardy <[email protected]> 2006-07-28 01:30

Quoted 7 lines
Quoted 7 lines
net/core/fib_rules.c new file +7 -0
+int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
+		     int flags, struct fib_lookup_arg *arg)
+{
+	struct fib_rule *rule;
+	int err;
+
+	rcu_read_lock();
Quoted 8 lines

net/core/fib_rules.c line 120

Quoted 4 lines
net/core/fib_rules.c new file +4 -0
+
+	list_for_each_entry(rule, ops->rules_list, list) {
+		if (rule->ifname[0] && (rule->ifindex != fl->iif))
+			continue;

ifindex may be unset even if ifname is set (in case the interface does not exist yet). In that case it will match falsely on locally generated packets.

Then rule->ifindex would be -1 and it shouldn't match but I changed it, it makes more sense.

Quoted 10 lines
Quoted 10 lines
net/core/fib_rules.c new file +10 -0
+static void notify_rule_change(int event, struct fib_rule *rule,
+			       struct fib_rules_ops *ops)
+{
+	int size = nlmsg_total_size(sizeof(struct fib_rule_hdr) + 128);
+	struct sk_buff *skb = alloc_skb(size, GFP_KERNEL);
+
+	if (skb == NULL)
+		netlink_set_err(rtnl, 0, RTNLGRP_IPV4_RULE, ENOBUFS);
+	else if (fib_nl_fill_rule(skb, rule, 0, 0, event, 0, ops) < 0) {
+		kfree_skb(skb);
Quoted 8 lines

net/core/fib_rules.c line 359

Quoted 4 lines
net/core/fib_rules.c new file +4 -0
+		netlink_set_err(rtnl, 0, RTNLGRP_IPV4_RULE, EINVAL);
+	} else
+		netlink_broadcast(rtnl, skb, 0, RTNLGRP_IPV4_RULE, GFP_KERNEL);
+}

Shouldn't different families use different groups? Userspace might (rightfully, I think) expect not to see anything but IPv4 rules on RTNLGRP_IPV4_RULE.

Right, I've added ops->nlgroup to fix this. Naturally I also fixed all the other issues you brought up, I have the feeling that there are more bugs, will look at the code again with some distance in a few days.

#33 lore raw

Re: [RESEND 3/5] [NET]: Protocol Independant Policy Routing Rules Framework

In reply to Thomas Graf

Thomas Graf wrote:

Hi Thomas,

Quoted 1 line

Derived from net/ipv6/fib_rules.c

do you mean net/ipv4/fib_rules.c or net/ipv6/fib6_rules.c? :-)

A couple of comments below.

Quoted 20 lines

Index: net-2.6.git/include/linux/fib_rules.h
===================================================================

include/linux/fib_rules.h new file +13 -0
@@ -0,0 +1,60 @@
+#ifndef __LINUX_FIB_RULES_H
+#define __LINUX_FIB_RULES_H
+
+#include <linux/types.h>
+#include <linux/rtnetlink.h>
+
+/* rule is permanent, and cannot be deleted */
+#define FIB_RULE_PERMANENT	1
+
+struct fib_rule_hdr
+{
+	__u8		family;
+	__u8		dst_len;

include/linux/fib_rules.h line 23

Quoted 10 lines
include/linux/fib_rules.h new file +10 -0
+	__u8		src_len;
+	__u8		tos;
+
+	__u8		table;
+	__u8		res1;	/* reserved */
+	__u8		res2;	/* reserved */
+	__u8		action;
+
+	__u32		flags;
+};

I'm wondering if this is guaranteed to be equvalent to struct rtmsg?

struct rtmsg

{
	unsigned char		rtm_family;
	unsigned char		rtm_dst_len;
	unsigned char		rtm_src_len;
	unsigned char		rtm_tos;

	unsigned char		rtm_table;	/* Routing table id */
	unsigned char		rtm_protocol;	/* Routing protocol; see below	*/
	unsigned char		rtm_scope;	/* See below */	
	unsigned char		rtm_type;	/* See below	*/

	unsigned		rtm_flags;
};

Won't we otherwise be breaking the existing userland interface?

Quoted 26 lines
include/linux/fib_rules.h new file +26 -0
+enum
+{
+	FRA_UNSPEC,
+	FRA_DST,	/* destination address */
+	FRA_SRC,	/* source address */
+	FRA_IFNAME,	/* interface name */
+	FRA_UNUSED1,
+	FRA_UNUSED2,
+	FRA_PRIORITY,	/* priority/preference */
+	FRA_UNUSED3,
+	FRA_UNUSED4,
+	FRA_UNUSED5,
+	FRA_FWMARK,	/* netfilter mark (IPv4) */
+	FRA_FLOW,	/* flow/class id */
+	__FRA_MAX
+};
+
+#define FRA_MAX (__FRA_MAX - 1)
+
+enum
+{
+	FR_ACT_UNSPEC,
+	FR_ACT_TO_TBL,		/* Pass to fixed table */
+	FR_ACT_RES1,
+	FR_ACT_RES2,
+	FR_ACT_RES3,

include/linux/fib_rules.h line 60

Quoted 10 lines
include/linux/fib_rules.h new file +10 -0
+	FR_ACT_RES4,
+	FR_ACT_BLACKHOLE,	/* Drop without notification */
+	FR_ACT_UNREACHABLE,	/* Drop with ENETUNREACH */
+	FR_ACT_PROHIBIT,	/* Drop with EACCES */
+	__FR_ACT_MAX,
+};
+
+#define FR_ACT_MAX (__FR_ACT_MAX - 1)
+
+#endif

Shouldn't all these (struct fib_rule_hdr included) actually be defined in include/linux/rtnetlink.h?

Otherwise, looks good.

Regards,
Ville

#34 lore raw

Re: [RESEND 3/5] [NET]: Protocol Independant Policy Routing Rules Framework

* Ville Nuorvala <[email protected]> 2006-07-31 17:46

Quoted 3 lines
Quoted 1 line

Derived from net/ipv6/fib_rules.c

do you mean net/ipv4/fib_rules.c or net/ipv6/fib6_rules.c? :-)

Hehe, I meant net/ipv4/fib_rules.c :-)

Quoted 32 lines
Quoted 4 lines
include/linux/fib_rules.h new file +4 -0
+struct fib_rule_hdr
+{
+	__u8		family;
+	__u8		dst_len;

include/linux/fib_rules.h line 23

Quoted 10 lines
include/linux/fib_rules.h new file +10 -0
+	__u8		src_len;
+	__u8		tos;
+
+	__u8		table;
+	__u8		res1;	/* reserved */
+	__u8		res2;	/* reserved */
+	__u8		action;
+
+	__u32		flags;
+};

I'm wondering if this is guaranteed to be equvalent to struct rtmsg?

struct rtmsg

{
	unsigned char		rtm_family;
	unsigned char		rtm_dst_len;
	unsigned char		rtm_src_len;
	unsigned char		rtm_tos;

	unsigned char		rtm_table;	/* Routing table id */
	unsigned char		rtm_protocol;	/* Routing protocol; see below	*/
	unsigned char		rtm_scope;	/* See below */	
	unsigned char		rtm_type;	/* See below	*/

	unsigned		rtm_flags;
};
Quoted 1 line

Won't we otherwise be breaking the existing userland interface?

It is equivalent but you're right, it would break userland interfaces otherwise. I've defined this new header to add implicit names and stop the confusion with unused fields.

Quoted 31 lines
Quoted 31 lines
include/linux/fib_rules.h new file +31 -0
+enum
+{
+	FRA_UNSPEC,
+	FRA_DST,	/* destination address */
+	FRA_SRC,	/* source address */
+	FRA_IFNAME,	/* interface name */
+	FRA_UNUSED1,
+	FRA_UNUSED2,
+	FRA_PRIORITY,	/* priority/preference */
+	FRA_UNUSED3,
+	FRA_UNUSED4,
+	FRA_UNUSED5,
+	FRA_FWMARK,	/* netfilter mark (IPv4) */
+	FRA_FLOW,	/* flow/class id */
+	__FRA_MAX
+};
+
+#define FRA_MAX (__FRA_MAX - 1)
+
+enum
+{
+	FR_ACT_UNSPEC,
+	FR_ACT_TO_TBL,		/* Pass to fixed table */
+	FR_ACT_RES1,
+	FR_ACT_RES2,
+	FR_ACT_RES3,
+	FR_ACT_RES4,
+	FR_ACT_BLACKHOLE,	/* Drop without notification */
+	FR_ACT_UNREACHABLE,	/* Drop with ENETUNREACH */
+	FR_ACT_PROHIBIT,	/* Drop with EACCES */
+	__FR_ACT_MAX,
Quoted 8 lines

include/linux/fib_rules.h line 60

Quoted 5 lines
include/linux/fib_rules.h new file +5 -0
+};
+
+#define FR_ACT_MAX (__FR_ACT_MAX - 1)
+
+#endif

Shouldn't all these (struct fib_rule_hdr included) actually be defined in include/linux/rtnetlink.h?

We used to stuff everything into rtnetlink.h for no good reason. Having independant include/linux/<subsystem>.h to export the interface to userspace and include/net/<subsystem>.h to export the kernel interface instead of contributing to the ifdef hell seems a lot cleaner to me.

#35 lore raw

Re: [RESEND 3/5] [NET]: Protocol Independant Policy Routing Rules Framework

Quoted 10 lines · Thomas Graf

* Ville Nuorvala <[email protected]> 2006-07-31 17:46

Quoted 2 lines

Shouldn't all these (struct fib_rule_hdr included) actually be defined in include/linux/rtnetlink.h?

We used to stuff everything into rtnetlink.h for no good reason. Having independant include/linux/<subsystem>.h to export the interface to userspace and include/net/<subsystem>.h to export the kernel interface instead of contributing to the ifdef hell seems a lot cleaner to me.

I agree, but then we should also split up rtnetlink.h. Having one special case will just make it harder to find.

#36 lore raw

Re: [RESEND 3/5] [NET]: Protocol Independant Policy Routing Rules Framework

* Patrick McHardy <[email protected]> 2006-07-31 20:01

Quoted 7 lines
Quoted 6 lines · Thomas Graf

* Ville Nuorvala <[email protected]> 2006-07-31 17:46

Quoted 2 lines

Shouldn't all these (struct fib_rule_hdr included) actually be defined in include/linux/rtnetlink.h?

Quoted 8 lines
Quoted 4 lines

We used to stuff everything into rtnetlink.h for no good reason. Having independant include/linux/<subsystem>.h to export the interface to userspace and include/net/<subsystem>.h to export the kernel interface instead of contributing to the ifdef hell seems a lot cleaner to me.

I agree, but then we should also split up rtnetlink.h. Having one special case will just make it harder to find.

Already done in the patchset converting things to the new netlink interface that I'll start submiting in the next days.

#37 lore raw

PATCH 4/5 : Policy Routing Rules

In reply to Thomas Graf

Adds support for policy routing rules including a new local table for routes with a local destination.

net/ipv6/fib6_rules.c new file +256 -0
Index: net-2.6.git/net/ipv6/fib6_rules.c
===================================================================
@@ -0,0 +1,256 @@
+/*
+ * net/ipv6/fib6_rules.c	IPv6 Routing Policy Rules
+ *
+ *	This program is free software; you can redistribute it and/or
+ *	modify it under the terms of the GNU General Public License as
+ *	published by the Free Software Foundation, version 2.
+ *
+ * Authors
+ *	Ville Nuorvala		<[email protected]>
+ *	Thomas Graf		<[email protected]>
+ */
+
+#include <linux/config.h>
+#include <linux/netdevice.h>
+
+#include <net/fib_rules.h>
+#include <net/ipv6.h>
+#include <net/ip6_route.h>
+#include <net/netlink.h>
+
+struct fib6_rule
+{
+	struct fib_rule		common;
+	struct rt6key		src;
+	struct rt6key		dst;
+	u8			tclass;
+};
+
+static struct fib_rules_ops fib6_rules_ops;
+
+static struct fib6_rule main_rule = {
+	.common = {
+		.refcnt =	ATOMIC_INIT(2),
+		.pref =		0x7FFE,
+		.action =	FR_ACT_TO_TBL,
+		.table =	RT6_TABLE_MAIN,
+	},
+};
+
+static struct fib6_rule local_rule = {
+	.common = {
+		.refcnt =	ATOMIC_INIT(2),
+		.pref =		0,
+		.action =	FR_ACT_TO_TBL,
+		.table =	RT6_TABLE_LOCAL,
+		.flags =	FIB_RULE_PERMANENT,
+	},
+};
+
+static LIST_HEAD(fib6_rules);
+
+struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags,
+				   pol_lookup_t lookup)
+{
+	struct fib_lookup_arg arg = {
+		.lookup_ptr = lookup,
+	};
+
+	fib_rules_lookup(&fib6_rules_ops, fl, flags, &arg);
+	fib_rule_put(arg.rule);
+
+	return (struct dst_entry *) arg.result;
+}
+
+int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
+		     int flags, struct fib_lookup_arg *arg)
+{
+	struct rt6_info *rt = NULL;
+	struct fib6_table *table;
+	pol_lookup_t lookup = arg->lookup_ptr;
+
+	switch (rule->action) {
+	case FR_ACT_TO_TBL:
+		break;
+	case FR_ACT_UNREACHABLE:
+		goto no_rt;
+	default:
+	case FR_ACT_BLACKHOLE:
+		rt = &ip6_blk_hole_entry;
+		goto discard_pkt;
+	case FR_ACT_PROHIBIT:
+		rt = &ip6_prohibit_entry;
+		goto discard_pkt;
+	}
+
+	table = fib6_get_table(rule->table);
+	if (table)
+		rt = lookup(table, flp, flags);
+
+	if (rt == &ip6_null_entry)
+		dst_release(&rt->u.dst);
+	else
+		goto out;
+no_rt:
+	rt = &ip6_null_entry;
+discard_pkt:
+	dst_hold(&rt->u.dst);
+out:
+
+	arg->result = rt;
+	return 0;
+}
+
+
+static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
+{
+	struct fib6_rule *r = (struct fib6_rule *) rule;
+
+	if (!ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen))
+		return 0;
+
+#ifdef CONFIG_IPV6_SUBTREES
+	if ((flags & RT6_F_HAS_SADDR) &&
+	    !ipv6_prefix_equal(&fl->fl6_src, &r->r_src.addr, r->r_src.plen))
+		return 0;
+#endif
+
+	return 1;
+}
+
+static struct nla_policy fib6_rule_policy[RTA_MAX+1] __read_mostly = {
+	[FRA_IFNAME]	= { .type = NLA_STRING },
+	[FRA_PRIORITY]	= { .type = NLA_U32 },
+	[FRA_SRC]	= { .minlen = sizeof(struct in6_addr) },
+	[FRA_DST]	= { .minlen = sizeof(struct in6_addr) },
+};
+
+static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
+			       struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
+			       struct nlattr **tb)
+{
+	int err = -EINVAL;
+	struct fib6_rule *rule6 = (struct fib6_rule *) rule;
+
+	if (frh->src_len > 128 || frh->dst_len > 128 ||
+	    (frh->tos & ~IPV6_FLOWINFO_MASK))
+		goto errout;
+
+#ifndef CONFIG_IPV6_SUBTREES
+	if (frh->src_len > 0)
+		goto errout;
+#endif
+
+	if (rule->action == FR_ACT_TO_TBL) {
+		if (rule->table == RT6_TABLE_UNSPEC)
+			goto errout;
+
+		if (fib6_new_table(rule->table) == NULL) {
+			err = -ENOBUFS;
+			goto errout;
+		}
+	}
+
+	if (tb[FRA_SRC])
+		nla_memcpy(&rule6->src.addr, tb[FRA_SRC],
+			   sizeof(struct in6_addr));
+
+	if (tb[FRA_DST])
+		nla_memcpy(&rule6->dst.addr, tb[FRA_DST],
+			   sizeof(struct in6_addr));
+
+	rule6->src.plen = frh->src_len;
+	rule6->dst.plen = frh->dst_len;
+	rule6->tclass = frh->tos;
+
+	err = 0;
+errout:
+	return err;
+}
+
+static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
+			     struct nlattr **tb)
+{
+	struct fib6_rule *rule6 = (struct fib6_rule *) rule;
+
+	if (frh->src_len && (rule6->src.plen != frh->src_len))
+		return 0;
+
+	if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
+		return 0;
+
+	if (frh->tos && (rule6->tclass != frh->tos))
+		return 0;
+
+	if (tb[FRA_SRC] &&
+	    nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
+		return 0;
+
+	if (tb[FRA_DST] &&
+	    nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
+		return 0;
+
+	return 1;
+}
+
+static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
+			  struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
+{
+	struct fib6_rule *rule6 = (struct fib6_rule *) rule;
+
+	frh->family = AF_INET6;
+	frh->dst_len = rule6->dst.plen;
+	frh->src_len = rule6->src.plen;
+	frh->tos = rule6->tclass;
+
+	if (rule6->dst.plen)
+		NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr),
+			&rule6->dst.addr);
+
+	if (rule6->src.plen)
+		NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr),
+			&rule6->src.addr);
+
+	return 0;
+
+nla_put_failure:
+	return -ENOBUFS;
+}
+
+int fib6_rules_dump(struct sk_buff *skb, struct netlink_callback *cb)
+{
+	return fib_rules_dump(skb, cb, AF_INET6);
+}
+
+static u32 fib6_rule_default_pref(void)
+{
+	return 0x3FFF;
+}
+
+static struct fib_rules_ops fib6_rules_ops = {
+	.family			= AF_INET6,
+	.rule_size		= sizeof(struct fib6_rule),
+	.action			= fib6_rule_action,
+	.match			= fib6_rule_match,
+	.configure		= fib6_rule_configure,
+	.compare		= fib6_rule_compare,
+	.fill			= fib6_rule_fill,
+	.default_pref		= fib6_rule_default_pref,
+	.policy			= fib6_rule_policy,
+	.rules_list		= &fib6_rules,
+	.owner			= THIS_MODULE,
+};
+
+void __init fib6_rules_init(void)
+{
+	list_add_tail(&local_rule.common.list, &fib6_rules);
+	list_add_tail(&main_rule.common.list, &fib6_rules);
+
+	fib_rules_register(&fib6_rules_ops);
+}
+
+void __exit fib6_rules_cleanup(void)
+{
+	fib_rules_unregister(&fib6_rules_ops);
+}
+
net/ipv6/Kconfig +1 -0
Index: net-2.6.git/net/ipv6/Kconfig
===================================================================
@@ -138,6 +138,7 @@ config IPV6_TUNNEL
 config IPV6_MULTIPLE_TABLES
 	bool "IPv6: Multiple Routing Tables"
 	depends on IPV6 && EXPERIMENTAL
+	select FIB_RULES
 	---help---
 	  Support multiple routing tables.
 
net/ipv6/Makefile +2 -0
Index: net-2.6.git/net/ipv6/Makefile
===================================================================
@@ -29,3 +29,5 @@ obj-$(CONFIG_IPV6_TUNNEL) += ip6_tunnel.
 obj-y += exthdrs_core.o
 
 obj-$(subst m,y,$(CONFIG_IPV6)) += inet6_hashtables.o
+
+obj-$(CONFIG_IPV6_MULTIPLE_TABLES) += fib6_rules.o
include/net/ip6_fib.h +8 -1
Index: net-2.6.git/include/net/ip6_fib.h
===================================================================
@@ -155,16 +155,17 @@ struct fib6_table {
 
 #define RT6_TABLE_UNSPEC	RT_TABLE_UNSPEC
 #define RT6_TABLE_MAIN		RT_TABLE_MAIN
-#define RT6_TABLE_LOCAL		RT6_TABLE_MAIN
 #define RT6_TABLE_DFLT		RT6_TABLE_MAIN
 #define RT6_TABLE_INFO		RT6_TABLE_MAIN
 
 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
 #define FIB6_TABLE_MIN		1
 #define FIB6_TABLE_MAX		RT_TABLE_MAX
+#define RT6_TABLE_LOCAL		RT_TABLE_LOCAL
 #else
 #define FIB6_TABLE_MIN		RT_TABLE_MAIN
 #define FIB6_TABLE_MAX		FIB6_TABLE_MIN
+#define RT6_TABLE_LOCAL		RT6_TABLE_MAIN
 #endif
 
 #define RT6_F_STRICT		1
@@ -219,5 +220,11 @@ extern void			fib6_run_gc(unsigned long 
 extern void			fib6_gc_cleanup(void);
 
 extern void			fib6_init(void);
+
+extern void			fib6_rules_init(void);
+extern void			fib6_rules_cleanup(void);
+extern int			fib6_rules_dump(struct sk_buff *,
+						struct netlink_callback *);
+
 #endif
 #endif
net/ipv6/route.c +50 -0

66 lines not on this page · raw message

net/ipv6/ip6_fib.c +10 -11
Index: net-2.6.git/net/ipv6/ip6_fib.c
===================================================================
@@ -159,6 +159,15 @@ static struct fib6_table fib6_main_tbl =
 
 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
 
+static struct fib6_table fib6_local_tbl = {
+	.tb6_id		= RT6_TABLE_LOCAL,
+	.tb6_lock	= RW_LOCK_UNLOCKED,
+	.tb6_root 	= {
+		.leaf		= &ip6_null_entry,
+		.fn_flags	= RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO,
+	},
+};
+
 #define FIB_TABLE_HASHSZ 256
 static struct hlist_head fib_table_hash[FIB_TABLE_HASHSZ];
 
@@ -228,20 +237,10 @@ struct fib6_table *fib6_get_table(u32 id
 	return NULL;
 }
 
-struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags,
-				   pol_lookup_t lookup)
-{
-	/*
-	 * TODO: Add rule lookup
-	 */
-	struct fib6_table *table = fib6_get_table(RT6_TABLE_MAIN);
-
-	return (struct dst_entry *) lookup(table, fl, flags);
-}
-
 static void __init fib6_tables_init(void)
 {
 	fib6_link_table(&fib6_main_tbl);
+	fib6_link_table(&fib6_local_tbl);
 }
 
 #else
include/net/ip6_route.h +5 -0
Index: net-2.6.git/include/net/ip6_route.h
===================================================================
@@ -41,6 +41,11 @@ struct pol_chain {
 
 extern struct rt6_info	ip6_null_entry;
 
+#ifdef CONFIG_IPV6_MULTIPLE_TABLES
+extern struct rt6_info	ip6_prohibit_entry;
+extern struct rt6_info	ip6_blk_hole_entry;
+#endif
+
 extern int ip6_rt_gc_interval;
 
 extern void			ip6_route_input(struct sk_buff *skb);
net/ipv6/addrconf.c +1 -0
Index: net-2.6.git/net/ipv6/addrconf.c
===================================================================
@@ -3370,6 +3370,7 @@ static struct rtnetlink_link inet6_rtnet
 	[RTM_DELROUTE - RTM_BASE] = { .doit	= inet6_rtm_delroute, },
 	[RTM_GETROUTE - RTM_BASE] = { .doit	= inet6_rtm_getroute,
 				      .dumpit	= inet6_dump_fib, },
+	[RTM_GETRULE  - RTM_BASE] = { .dumpit   = fib6_rules_dump,   },
 };
 
 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
maintainer
#39 lore raw

Re: [PATCH 4/5] [IPV6]: Policy Routing Rules

In reply to Thomas Graf

From: Thomas Graf <[email protected]>
Date: Thu, 27 Jul 2006 00:00:04 +0200

net/ipv6/Makefile obj-$(CONFIG_IPV6_TUNNEL) += ip6_tunnel. · line 33

Quoted 10 lines

Index: net-2.6.git/net/ipv6/Makefile
===================================================================

net/ipv6/Makefile +2 -0
@@ -29,3 +29,5 @@ obj-$(CONFIG_IPV6_TUNNEL) += ip6_tunnel.
 obj-y += exthdrs_core.o
 
 obj-$(subst m,y,$(CONFIG_IPV6)) += inet6_hashtables.o
+
+obj-$(CONFIG_IPV6_MULTIPLE_TABLES) += fib6_rules.o

This will cause a build failure if IPV6 is built modular.

The fix is simple, make CONFIG_IPV6_MULTIPLE_TABLES a tristate instead of a bool.

maintainer
#40 lore raw

Re: [PATCH 4/5] [IPV6]: Policy Routing Rules

In reply to Thomas Graf

From: Thomas Graf <[email protected]>
Date: Thu, 27 Jul 2006 00:00:04 +0200

Quoted 6 lines

Index: net-2.6.git/include/net/ip6_route.h
===================================================================

include/net/ip6_route.h +0 -0
@@ -41,6 +41,11 @@ struct pol_chain {
 

include/net/ip6_route.h struct pol_chain { · line 51

Quoted 10 lines
include/net/ip6_route.h +5 -0
 extern struct rt6_info	ip6_null_entry;
 
+#ifdef CONFIG_IPV6_MULTIPLE_TABLES
+extern struct rt6_info	ip6_prohibit_entry;
+extern struct rt6_info	ip6_blk_hole_entry;
+#endif
+
 extern int ip6_rt_gc_interval;
 
 extern void			ip6_route_input(struct sk_buff *skb);

Because of my other suggestion, changing IPV6_MULTIPLE_TABLES to tristate, the build will fail unless we add a second condition to this ifdef, namely "CONFIG_IPV6_MULTIPLE_TABLES_MODULE".

In fact, all this doesn't look so nice.

All we want to do is make fib6_rules.o get built into ipv6-objs if it is enabled.

Therfore, the better fix seems to be, to change the Makefile rule to be:

ipv6-$(CONFIG_IPV6_MULTIPLE_TABLES) += fib6_rules.o

Ok?

maintainer
#41 lore raw

Re: [PATCH 4/5] [IPV6]: Policy Routing Rules

From: David Miller <[email protected]>
Date: Wed, 26 Jul 2006 16:33:43 -0700 (PDT)

Quoted 6 lines

Therfore, the better fix seems to be, to change the Makefile rule to be:

ipv6-$(CONFIG_IPV6_MULTIPLE_TABLES) += fib6_rules.o

Ok?

And once this is fixed, new problems crop up :-)

WARNING: net/ipv6/ipv6.o - Section mismatch: reference to .exit.text:fib6_rules_cleanup from .text between 'ip6_route_cleanup' (at offset 0xbfd4) and 'rt6_mtu_change_route'
WARNING: "fib_rules_dump" [net/ipv6/ipv6.ko] undefined!
WARNING: "fib_rules_register" [net/ipv6/ipv6.ko] undefined!
WARNING: "fib_rules_lookup" [net/ipv6/ipv6.ko] undefined!
WARNING: "fib_rules_unregister" [net/ipv6/ipv6.ko] undefined!

The fib_rules_* cases are easy to fix, simply export those symbols using EXPORT_SYMBOL_GPL() in net/core/fib_rules.c

The other problem is a bit more onerous. ip6_route_cleanup is not marked __exit, since it is called from initialization as well as exit contexts. Therefore fib6_rules_cleanup() has to have it's __exit marker deleted too.

IPv6 seems to build modular now with all of these issues taken care of. Thomas, please integrate these fixes into your patch.

Thanks.

#42 lore raw

PATCH RESEND 4/5 : Policy Routing Rules

Adds support for policy routing rules including a new local table for routes with a local destination.

net/ipv6/fib6_rules.c new file +255 -0
Index: net-2.6.git/net/ipv6/fib6_rules.c
===================================================================
@@ -0,0 +1,255 @@
+/*
+ * net/ipv6/fib6_rules.c	IPv6 Routing Policy Rules
+ *
+ *	This program is free software; you can redistribute it and/or
+ *	modify it under the terms of the GNU General Public License as
+ *	published by the Free Software Foundation, version 2.
+ *
+ * Authors
+ *	Ville Nuorvala		<[email protected]>
+ *	Thomas Graf		<[email protected]>
+ */
+
+#include <linux/config.h>
+#include <linux/netdevice.h>
+
+#include <net/fib_rules.h>
+#include <net/ipv6.h>
+#include <net/ip6_route.h>
+#include <net/netlink.h>
+
+struct fib6_rule
+{
+	struct fib_rule		common;
+	struct rt6key		src;
+	struct rt6key		dst;
+	u8			tclass;
+};
+
+static struct fib_rules_ops fib6_rules_ops;
+
+static struct fib6_rule main_rule = {
+	.common = {
+		.refcnt =	ATOMIC_INIT(2),
+		.pref =		0x7FFE,
+		.action =	FR_ACT_TO_TBL,
+		.table =	RT6_TABLE_MAIN,
+	},
+};
+
+static struct fib6_rule local_rule = {
+	.common = {
+		.refcnt =	ATOMIC_INIT(2),
+		.pref =		0,
+		.action =	FR_ACT_TO_TBL,
+		.table =	RT6_TABLE_LOCAL,
+		.flags =	FIB_RULE_PERMANENT,
+	},
+};
+
+static LIST_HEAD(fib6_rules);
+
+struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags,
+				   pol_lookup_t lookup)
+{
+	struct fib_lookup_arg arg = {
+		.lookup_ptr = lookup,
+	};
+
+	fib_rules_lookup(&fib6_rules_ops, fl, flags, &arg);
+	if (arg.rule)
+		fib_rule_put(arg.rule);
+
+	return (struct dst_entry *) arg.result;
+}
+
+int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
+		     int flags, struct fib_lookup_arg *arg)
+{
+	struct rt6_info *rt = NULL;
+	struct fib6_table *table;
+	pol_lookup_t lookup = arg->lookup_ptr;
+
+	switch (rule->action) {
+	case FR_ACT_TO_TBL:
+		break;
+	case FR_ACT_UNREACHABLE:
+		rt = &ip6_null_entry;
+		goto discard_pkt;
+	default:
+	case FR_ACT_BLACKHOLE:
+		rt = &ip6_blk_hole_entry;
+		goto discard_pkt;
+	case FR_ACT_PROHIBIT:
+		rt = &ip6_prohibit_entry;
+		goto discard_pkt;
+	}
+
+	table = fib6_get_table(rule->table);
+	if (table)
+		rt = lookup(table, flp, flags);
+
+	if (rt != &ip6_null_entry)
+		goto out;
+
+	dst_release(&rt->u.dst);
+discard_pkt:
+	dst_hold(&rt->u.dst);
+out:
+	arg->result = rt;
+	return rt == NULL ? -EAGAIN : 0;
+}
+
+
+static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
+{
+	struct fib6_rule *r = (struct fib6_rule *) rule;
+
+	if (!ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen))
+		return 0;
+
+#ifdef CONFIG_IPV6_SUBTREES
+	if ((flags & RT6_F_HAS_SADDR) &&
+	    !ipv6_prefix_equal(&fl->fl6_src, &r->r_src.addr, r->r_src.plen))
+		return 0;
+#endif
+
+	return 1;
+}
+
+static struct nla_policy fib6_rule_policy[RTA_MAX+1] __read_mostly = {
+	[FRA_IFNAME]	= { .type = NLA_STRING },
+	[FRA_PRIORITY]	= { .type = NLA_U32 },
+	[FRA_SRC]	= { .minlen = sizeof(struct in6_addr) },
+	[FRA_DST]	= { .minlen = sizeof(struct in6_addr) },
+};
+
+static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
+			       struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
+			       struct nlattr **tb)
+{
+	int err = -EINVAL;
+	struct fib6_rule *rule6 = (struct fib6_rule *) rule;
+
+	if (frh->src_len > 128 || frh->dst_len > 128 ||
+	    (frh->tos & ~IPV6_FLOWINFO_MASK))
+		goto errout;
+
+#ifndef CONFIG_IPV6_SUBTREES
+	if (frh->src_len > 0)
+		goto errout;
+#endif
+
+	if (rule->action == FR_ACT_TO_TBL) {
+		if (rule->table == RT6_TABLE_UNSPEC)
+			goto errout;
+
+		if (fib6_new_table(rule->table) == NULL) {
+			err = -ENOBUFS;
+			goto errout;
+		}
+	}
+
+	if (tb[FRA_SRC])
+		nla_memcpy(&rule6->src.addr, tb[FRA_SRC],
+			   sizeof(struct in6_addr));
+
+	if (tb[FRA_DST])
+		nla_memcpy(&rule6->dst.addr, tb[FRA_DST],
+			   sizeof(struct in6_addr));
+
+	rule6->src.plen = frh->src_len;
+	rule6->dst.plen = frh->dst_len;
+	rule6->tclass = frh->tos;
+
+	err = 0;
+errout:
+	return err;
+}
+
+static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
+			     struct nlattr **tb)
+{
+	struct fib6_rule *rule6 = (struct fib6_rule *) rule;
+
+	if (frh->src_len && (rule6->src.plen != frh->src_len))
+		return 0;
+
+	if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
+		return 0;
+
+	if (frh->tos && (rule6->tclass != frh->tos))
+		return 0;
+
+	if (tb[FRA_SRC] &&
+	    nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
+		return 0;
+
+	if (tb[FRA_DST] &&
+	    nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
+		return 0;
+
+	return 1;
+}
+
+static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
+			  struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
+{
+	struct fib6_rule *rule6 = (struct fib6_rule *) rule;
+
+	frh->family = AF_INET6;
+	frh->dst_len = rule6->dst.plen;
+	frh->src_len = rule6->src.plen;
+	frh->tos = rule6->tclass;
+
+	if (rule6->dst.plen)
+		NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr),
+			&rule6->dst.addr);
+
+	if (rule6->src.plen)
+		NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr),
+			&rule6->src.addr);
+
+	return 0;
+
+nla_put_failure:
+	return -ENOBUFS;
+}
+
+int fib6_rules_dump(struct sk_buff *skb, struct netlink_callback *cb)
+{
+	return fib_rules_dump(skb, cb, AF_INET6);
+}
+
+static u32 fib6_rule_default_pref(void)
+{
+	return 0x3FFF;
+}
+
+static struct fib_rules_ops fib6_rules_ops = {
+	.family			= AF_INET6,
+	.rule_size		= sizeof(struct fib6_rule),
+	.action			= fib6_rule_action,
+	.match			= fib6_rule_match,
+	.configure		= fib6_rule_configure,
+	.compare		= fib6_rule_compare,
+	.fill			= fib6_rule_fill,
+	.default_pref		= fib6_rule_default_pref,
+	.policy			= fib6_rule_policy,
+	.rules_list		= &fib6_rules,
+	.owner			= THIS_MODULE,
+};
+
+void __init fib6_rules_init(void)
+{
+	list_add_tail(&local_rule.common.list, &fib6_rules);
+	list_add_tail(&main_rule.common.list, &fib6_rules);
+
+	fib_rules_register(&fib6_rules_ops);
+}
+
+void fib6_rules_cleanup(void)
+{
+	fib_rules_unregister(&fib6_rules_ops);
+}
+
net/ipv6/Kconfig +1 -0
Index: net-2.6.git/net/ipv6/Kconfig
===================================================================
@@ -138,6 +138,7 @@ config IPV6_TUNNEL
 config IPV6_MULTIPLE_TABLES
 	bool "IPv6: Multiple Routing Tables"
 	depends on IPV6 && EXPERIMENTAL
+	select FIB_RULES
 	---help---
 	  Support multiple routing tables.
 
net/ipv6/Makefile +1 -0
Index: net-2.6.git/net/ipv6/Makefile
===================================================================
@@ -13,6 +13,7 @@ ipv6-objs :=	af_inet6.o anycast.o ip6_ou
 ipv6-$(CONFIG_XFRM) += xfrm6_policy.o xfrm6_state.o xfrm6_input.o \
 	xfrm6_output.o
 ipv6-$(CONFIG_NETFILTER) += netfilter.o
+ipv6-$(CONFIG_IPV6_MULTIPLE_TABLES) += fib6_rules.o
 ipv6-objs += $(ipv6-y)
 
 obj-$(CONFIG_INET6_AH) += ah6.o
include/net/ip6_fib.h +8 -1
Index: net-2.6.git/include/net/ip6_fib.h
===================================================================
@@ -155,16 +155,17 @@ struct fib6_table {
 
 #define RT6_TABLE_UNSPEC	RT_TABLE_UNSPEC
 #define RT6_TABLE_MAIN		RT_TABLE_MAIN
-#define RT6_TABLE_LOCAL		RT6_TABLE_MAIN
 #define RT6_TABLE_DFLT		RT6_TABLE_MAIN
 #define RT6_TABLE_INFO		RT6_TABLE_MAIN
 
 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
 #define FIB6_TABLE_MIN		1
 #define FIB6_TABLE_MAX		RT_TABLE_MAX
+#define RT6_TABLE_LOCAL		RT_TABLE_LOCAL
 #else
 #define FIB6_TABLE_MIN		RT_TABLE_MAIN
 #define FIB6_TABLE_MAX		FIB6_TABLE_MIN
+#define RT6_TABLE_LOCAL		RT6_TABLE_MAIN
 #endif
 
 #define RT6_F_STRICT		1
@@ -219,5 +220,11 @@ extern void			fib6_run_gc(unsigned long 
 extern void			fib6_gc_cleanup(void);
 
 extern void			fib6_init(void);
+
+extern void			fib6_rules_init(void);
+extern void			fib6_rules_cleanup(void);
+extern int			fib6_rules_dump(struct sk_buff *,
+						struct netlink_callback *);
+
 #endif
 #endif
net/ipv6/route.c +50 -0

66 lines not on this page · raw message

net/ipv6/ip6_fib.c +10 -11
Index: net-2.6.git/net/ipv6/ip6_fib.c
===================================================================
@@ -159,6 +159,15 @@ static struct fib6_table fib6_main_tbl =
 
 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
 
+static struct fib6_table fib6_local_tbl = {
+	.tb6_id		= RT6_TABLE_LOCAL,
+	.tb6_lock	= RW_LOCK_UNLOCKED,
+	.tb6_root 	= {
+		.leaf		= &ip6_null_entry,
+		.fn_flags	= RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO,
+	},
+};
+
 #define FIB_TABLE_HASHSZ 256
 static struct hlist_head fib_table_hash[FIB_TABLE_HASHSZ];
 
@@ -228,20 +237,10 @@ struct fib6_table *fib6_get_table(u32 id
 	return NULL;
 }
 
-struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags,
-				   pol_lookup_t lookup)
-{
-	/*
-	 * TODO: Add rule lookup
-	 */
-	struct fib6_table *table = fib6_get_table(RT6_TABLE_MAIN);
-
-	return (struct dst_entry *) lookup(table, fl, flags);
-}
-
 static void __init fib6_tables_init(void)
 {
 	fib6_link_table(&fib6_main_tbl);
+	fib6_link_table(&fib6_local_tbl);
 }
 
 #else
include/net/ip6_route.h +5 -0
Index: net-2.6.git/include/net/ip6_route.h
===================================================================
@@ -41,6 +41,11 @@ struct pol_chain {
 
 extern struct rt6_info	ip6_null_entry;
 
+#ifdef CONFIG_IPV6_MULTIPLE_TABLES
+extern struct rt6_info	ip6_prohibit_entry;
+extern struct rt6_info	ip6_blk_hole_entry;
+#endif
+
 extern int ip6_rt_gc_interval;
 
 extern void			ip6_route_input(struct sk_buff *skb);
net/ipv6/addrconf.c +1 -0
Index: net-2.6.git/net/ipv6/addrconf.c
===================================================================
@@ -3370,6 +3370,7 @@ static struct rtnetlink_link inet6_rtnet
 	[RTM_DELROUTE - RTM_BASE] = { .doit	= inet6_rtm_delroute, },
 	[RTM_GETROUTE - RTM_BASE] = { .doit	= inet6_rtm_getroute,
 				      .dumpit	= inet6_dump_fib, },
+	[RTM_GETRULE  - RTM_BASE] = { .dumpit   = fib6_rules_dump,   },
 };
 
 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
#44 lore raw

PATCH 5/5 : Use Protocol Independant Policy Routing Rules Framework

In reply to Thomas Graf
include/net/ip_fib.h +7 -7
Index: net-2.6.git/include/net/ip_fib.h
===================================================================
@@ -18,6 +18,7 @@
 
 #include <net/flow.h>
 #include <linux/seq_file.h>
+#include <net/fib_rules.h>
 
 /* WARNING: The ordering of these elements must match ordering
  *          of RTA_* rtnetlink attribute numbers.
@@ -203,9 +204,8 @@ static inline void fib_select_default(co
 #define ip_fib_main_table (fib_tables[RT_TABLE_MAIN])
 
 extern struct fib_table * fib_tables[RT_TABLE_MAX+1];
-extern int fib_lookup(const struct flowi *flp, struct fib_result *res);
+extern int fib_lookup(struct flowi *flp, struct fib_result *res);
 extern struct fib_table *__fib_new_table(int id);
-extern void fib_rule_put(struct fib_rule *r);
 
 static inline struct fib_table *fib_get_table(int id)
 {
@@ -251,15 +251,15 @@ extern u32  __fib_res_prefsrc(struct fib
 extern struct fib_table *fib_hash_init(int id);
 
 #ifdef CONFIG_IP_MULTIPLE_TABLES
-/* Exported by fib_rules.c */
+extern int fib4_rules_dump(struct sk_buff *skb, struct netlink_callback *cb);
+
+extern void __init fib4_rules_init(void);
+extern void __exit fib4_rules_cleanup(void);
 
-extern int inet_rtm_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
-extern int inet_rtm_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
-extern int inet_dump_rules(struct sk_buff *skb, struct netlink_callback *cb);
 #ifdef CONFIG_NET_CLS_ROUTE
 extern u32 fib_rules_tclass(struct fib_result *res);
 #endif
-extern void fib_rules_init(void);
+
 #endif
 
 static inline void fib_combine_itag(u32 *itag, struct fib_result *res)
net/ipv4/Kconfig +1 -0
Index: net-2.6.git/net/ipv4/Kconfig
===================================================================
@@ -88,6 +88,7 @@ config IP_FIB_HASH
 config IP_MULTIPLE_TABLES
 	bool "IP: policy routing"
 	depends on IP_ADVANCED_ROUTER
+	select FIB_RULES
 	---help---
 	  Normally, a router decides what to do with a received packet based
 	  solely on the packet's final destination address. If you say Y here,
net/ipv4/devinet.c +1 -3
Index: net-2.6.git/net/ipv4/devinet.c
===================================================================
@@ -1153,9 +1153,7 @@ static struct rtnetlink_link inet_rtnetl
 	[RTM_GETROUTE - RTM_BASE] = { .doit	= inet_rtm_getroute,
 				      .dumpit	= inet_dump_fib,	},
 #ifdef CONFIG_IP_MULTIPLE_TABLES
-	[RTM_NEWRULE  - RTM_BASE] = { .doit	= inet_rtm_newrule,	},
-	[RTM_DELRULE  - RTM_BASE] = { .doit	= inet_rtm_delrule,	},
-	[RTM_GETRULE  - RTM_BASE] = { .dumpit	= inet_dump_rules,	},
+	[RTM_GETRULE  - RTM_BASE] = { .dumpit	= fib4_rules_dump,	},
 #endif
 };
 
net/ipv4/fib_rules.c +242 -371

710 lines not on this page · raw message

net/ipv4/fib_frontend.c +1 -1
Index: net-2.6.git/net/ipv4/fib_frontend.c
===================================================================
@@ -656,7 +656,7 @@ void __init ip_fib_init(void)
 	ip_fib_local_table = fib_hash_init(RT_TABLE_LOCAL);
 	ip_fib_main_table  = fib_hash_init(RT_TABLE_MAIN);
 #else
-	fib_rules_init();
+	fib4_rules_init();
 #endif
 
 	register_netdevice_notifier(&fib_netdev_notifier);
maintainer
#46 lore raw

Re: [PATCH 5/5] [IPV4]: Use Protocol Independant Policy Routing Rules Framework

In reply to Thomas Graf

From: Thomas Graf <[email protected]>
Date: Thu, 27 Jul 2006 00:00:05 +0200

Quoted 4 lines

Index: net-2.6.git/net/ipv4/fib_rules.c
===================================================================

net/ipv4/fib_rules.c +0 -0

...

net/ipv4/fib_rules.c static struct fib_table *fib_empty_table · line 202

Quoted 4 lines
net/ipv4/fib_rules.c +0 -4
-	new_r = kmalloc(sizeof(*new_r), GFP_KERNEL);
-	if (!new_r)
-		return -ENOMEM;
-	memset(new_r, 0, sizeof(*new_r));

When you respin these patches, please regenerate them against the current tree which now uses kzalloc() here.

Thanks.

#47 lore raw

PATCH RESEND 5/5 : Use Protocol Independant Policy Routing Rules Framework

include/net/ip_fib.h +7 -7
Index: net-2.6.git/include/net/ip_fib.h
===================================================================
@@ -18,6 +18,7 @@
 
 #include <net/flow.h>
 #include <linux/seq_file.h>
+#include <net/fib_rules.h>
 
 /* WARNING: The ordering of these elements must match ordering
  *          of RTA_* rtnetlink attribute numbers.
@@ -203,9 +204,8 @@ static inline void fib_select_default(co
 #define ip_fib_main_table (fib_tables[RT_TABLE_MAIN])
 
 extern struct fib_table * fib_tables[RT_TABLE_MAX+1];
-extern int fib_lookup(const struct flowi *flp, struct fib_result *res);
+extern int fib_lookup(struct flowi *flp, struct fib_result *res);
 extern struct fib_table *__fib_new_table(int id);
-extern void fib_rule_put(struct fib_rule *r);
 
 static inline struct fib_table *fib_get_table(int id)
 {
@@ -251,15 +251,15 @@ extern u32  __fib_res_prefsrc(struct fib
 extern struct fib_table *fib_hash_init(int id);
 
 #ifdef CONFIG_IP_MULTIPLE_TABLES
-/* Exported by fib_rules.c */
+extern int fib4_rules_dump(struct sk_buff *skb, struct netlink_callback *cb);
+
+extern void __init fib4_rules_init(void);
+extern void __exit fib4_rules_cleanup(void);
 
-extern int inet_rtm_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
-extern int inet_rtm_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg);
-extern int inet_dump_rules(struct sk_buff *skb, struct netlink_callback *cb);
 #ifdef CONFIG_NET_CLS_ROUTE
 extern u32 fib_rules_tclass(struct fib_result *res);
 #endif
-extern void fib_rules_init(void);
+
 #endif
 
 static inline void fib_combine_itag(u32 *itag, struct fib_result *res)
net/ipv4/Kconfig +1 -0
Index: net-2.6.git/net/ipv4/Kconfig
===================================================================
@@ -88,6 +88,7 @@ config IP_FIB_HASH
 config IP_MULTIPLE_TABLES
 	bool "IP: policy routing"
 	depends on IP_ADVANCED_ROUTER
+	select FIB_RULES
 	---help---
 	  Normally, a router decides what to do with a received packet based
 	  solely on the packet's final destination address. If you say Y here,
net/ipv4/devinet.c +1 -3
Index: net-2.6.git/net/ipv4/devinet.c
===================================================================
@@ -1151,9 +1151,7 @@ static struct rtnetlink_link inet_rtnetl
 	[RTM_GETROUTE - RTM_BASE] = { .doit	= inet_rtm_getroute,
 				      .dumpit	= inet_dump_fib,	},
 #ifdef CONFIG_IP_MULTIPLE_TABLES
-	[RTM_NEWRULE  - RTM_BASE] = { .doit	= inet_rtm_newrule,	},
-	[RTM_DELRULE  - RTM_BASE] = { .doit	= inet_rtm_delrule,	},
-	[RTM_GETRULE  - RTM_BASE] = { .dumpit	= inet_dump_rules,	},
+	[RTM_GETRULE  - RTM_BASE] = { .dumpit	= fib4_rules_dump,	},
 #endif
 };
 
net/ipv4/fib_rules.c +242 -370

709 lines not on this page · raw message

net/ipv4/fib_frontend.c +1 -1
Index: net-2.6.git/net/ipv4/fib_frontend.c
===================================================================
@@ -656,7 +656,7 @@ void __init ip_fib_init(void)
 	ip_fib_local_table = fib_hash_init(RT_TABLE_LOCAL);
 	ip_fib_main_table  = fib_hash_init(RT_TABLE_MAIN);
 #else
-	fib_rules_init();
+	fib4_rules_init();
 #endif
 
 	register_netdevice_notifier(&fib_netdev_notifier);
#48 lore raw
In reply to Thomas Graf

Hello.

In article <[email protected]> (at Thu, 27 Jul 2006 00:11:00 +0200), Thomas Graf <[email protected]> says:

Quoted 5 lines

It's not final but somewhat working, I'm eager to see comments or patches. I apologize if I've tramped onto anybody's foot by taking this up and submitting it, this isn't meant as an attempt to steal credits but rather to pick up good code and finally get it upstream after a very long while.

First of all, Thank you for giving us good starting point. I am interested in this area because I have been spending several month for this release engineering.

Well, as you stated, the code is based on the work of MIPL, which is being jointly developed by Helsinki University of Technology (HUT) and USAGI/WIDE Project. Please describe this on your commit logs. Please retain copyright information as well.

Thank you very much.

Signature · 2 lines
YOSHIFUJI Hideaki @ USAGI Project  <[email protected]>
GPG-FP  : 9022 65EB 1ECF 3AD1 0BDF  80D8 4807 F894 E062 0EEA
maintainer
#49 lore raw

From: YOSHIFUJI Hideaki <[email protected]>
Date: Fri, 28 Jul 2006 15:10:09 +0900 (JST)

Quoted 4 lines

Well, as you stated, the code is based on the work of MIPL, which is being jointly developed by Helsinki University of Technology (HUT) and USAGI/WIDE Project. Please describe this on your commit logs. Please retain copyright information as well.

Ok.

Thomas could you please respin the patches with:

1) Updated credits, based upon Yoshifuji's feedback

2) Fixes for the small issues Patrick pointer out in his review

Thanks a lot.

#50 lore raw
In reply to YOSHIFUJI Hideaki / 吉藤英明

* YOSHIFUJI Hideaki / ?$B5HF#1QL@ <[email protected]> 2006-07-28 15:10

Quoted 4 lines

Well, as you stated, the code is based on the work of MIPL, which is being jointly developed by Helsinki University of Technology (HUT) and USAGI/WIDE Project. Please describe this on your commit logs. Please retain copyright information as well.

Please tell me which copyright I missed?

#51 lore raw

In article <[email protected]> (at Fri, 28 Jul 2006 12:32:29 +0200), Thomas Graf <[email protected]> says:

Quoted 7 lines

* YOSHIFUJI Hideaki / ?$B5HF#1QL@ <[email protected]> 2006-07-28 15:10

Quoted 4 lines

Well, as you stated, the code is based on the work of MIPL, which is being jointly developed by Helsinki University of Technology (HUT) and USAGI/WIDE Project. Please describe this on your commit logs. Please retain copyright information as well.

Please tell me which copyright I missed?

Please put these lines in net/ipv6/fib6_rules.c
(and maybe, its derived files):

Copyright (C)2003-2006 Helsinki University of Technology Copyright (C)2003-2006 USAGI/WIDE Project

You can add your own copyright notice if you want, of course.

Thank you.

--yoshfuji

#52 lore raw
In reply to Thomas Graf
Quoted 1 line · Thomas Graf

Hello,

Hi Thomas!

Quoted 5 lines

Thought it might be time to go through a round of comments on this work. Even though I've almost rewritten all the code the patches are based on the work found on www.mobile-ipv6.org. I have no idea which code was written by whom so just email me to get the credits right.

The policy routing stuff (multiple tables and source address based routing) was almost entirely written by me. Therefore you can apply my name as you see fit ;-)

Tushar Gohad at MontaVista, Benjamin Thery at Bull and of course USAGI have also worked on the code.

Quoted 4 lines

Main differences to the version found on mobile-ipv6.org is that I removed table refcnt and defined that tables cannot disappear once created to simplify things and avoid too many atomic operations when looking up routes.

Yes, that sounds good. As the ipv6 module doesn't really seem to become unloadable anytime soon, there isn't really any good reason to refcount the tables.

Quoted 4 lines

I've replaced the
table array with a hash table to prepare it for > 255 tables and made things aware of the new default router selection code and experimental route info stuff added recently.

Good! I never had the time to merge our changes with 2.6.17.

Quoted 2 lines

It's not final but somewhat working, I'm eager to see comments or patches.

I'll try to comment on them the best I can.

Quoted 4 lines

I apologize if I've tramped onto anybody's foot
by taking this up and submitting it, this isn't meant as an attempt to steal credits but rather to pick up good code and finally get it upstream after a very long while.

No offense taken! It's great that someone wants to push these things upstream as I personally have neither had the time nor the energy to do so lately.

Regards,
Ville