| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | *	Linux INET6 implementation | 
|  | 3 | *	FIB front-end. | 
|  | 4 | * | 
|  | 5 | *	Authors: | 
| YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 6 | *	Pedro Roque		<roque@di.fc.ul.pt> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | *	This program is free software; you can redistribute it and/or | 
|  | 9 | *      modify it under the terms of the GNU General Public License | 
|  | 10 | *      as published by the Free Software Foundation; either version | 
|  | 11 | *      2 of the License, or (at your option) any later version. | 
|  | 12 | */ | 
|  | 13 |  | 
|  | 14 | /*	Changes: | 
|  | 15 | * | 
|  | 16 | *	YOSHIFUJI Hideaki @USAGI | 
|  | 17 | *		reworked default router selection. | 
|  | 18 | *		- respect outgoing interface | 
|  | 19 | *		- select from (probably) reachable routers (i.e. | 
|  | 20 | *		routers in REACHABLE, STALE, DELAY or PROBE states). | 
|  | 21 | *		- always select the same router if it is (probably) | 
|  | 22 | *		reachable.  otherwise, round-robin the list. | 
| YOSHIFUJI Hideaki | c0bece9 | 2006-08-23 17:23:25 -0700 | [diff] [blame] | 23 | *	Ville Nuorvala | 
|  | 24 | *		Fixed routing subtrees. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | */ | 
|  | 26 |  | 
| Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 27 | #include <linux/capability.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/errno.h> | 
|  | 29 | #include <linux/types.h> | 
|  | 30 | #include <linux/times.h> | 
|  | 31 | #include <linux/socket.h> | 
|  | 32 | #include <linux/sockios.h> | 
|  | 33 | #include <linux/net.h> | 
|  | 34 | #include <linux/route.h> | 
|  | 35 | #include <linux/netdevice.h> | 
|  | 36 | #include <linux/in6.h> | 
| YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 37 | #include <linux/mroute6.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/init.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <linux/if_arp.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <linux/proc_fs.h> | 
|  | 41 | #include <linux/seq_file.h> | 
| Daniel Lezcano | 5b7c931 | 2008-03-03 23:28:58 -0800 | [diff] [blame] | 42 | #include <linux/nsproxy.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 43 | #include <linux/slab.h> | 
| Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 44 | #include <net/net_namespace.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <net/snmp.h> | 
|  | 46 | #include <net/ipv6.h> | 
|  | 47 | #include <net/ip6_fib.h> | 
|  | 48 | #include <net/ip6_route.h> | 
|  | 49 | #include <net/ndisc.h> | 
|  | 50 | #include <net/addrconf.h> | 
|  | 51 | #include <net/tcp.h> | 
|  | 52 | #include <linux/rtnetlink.h> | 
|  | 53 | #include <net/dst.h> | 
|  | 54 | #include <net/xfrm.h> | 
| Tom Tucker | 8d71740 | 2006-07-30 20:43:36 -0700 | [diff] [blame] | 55 | #include <net/netevent.h> | 
| Thomas Graf | 21713eb | 2006-08-15 00:35:24 -0700 | [diff] [blame] | 56 | #include <net/netlink.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 |  | 
|  | 58 | #include <asm/uaccess.h> | 
|  | 59 |  | 
|  | 60 | #ifdef CONFIG_SYSCTL | 
|  | 61 | #include <linux/sysctl.h> | 
|  | 62 | #endif | 
|  | 63 |  | 
|  | 64 | /* Set to 3 to get tracing. */ | 
|  | 65 | #define RT6_DEBUG 2 | 
|  | 66 |  | 
|  | 67 | #if RT6_DEBUG >= 3 | 
|  | 68 | #define RDBG(x) printk x | 
|  | 69 | #define RT6_TRACE(x...) printk(KERN_DEBUG x) | 
|  | 70 | #else | 
|  | 71 | #define RDBG(x) | 
|  | 72 | #define RT6_TRACE(x...) do { ; } while (0) | 
|  | 73 | #endif | 
|  | 74 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | static struct rt6_info * ip6_rt_copy(struct rt6_info *ort); | 
|  | 76 | static struct dst_entry	*ip6_dst_check(struct dst_entry *dst, u32 cookie); | 
| David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 77 | static unsigned int	 ip6_default_advmss(const struct dst_entry *dst); | 
| David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 78 | static unsigned int	 ip6_default_mtu(const struct dst_entry *dst); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | static struct dst_entry *ip6_negative_advice(struct dst_entry *); | 
|  | 80 | static void		ip6_dst_destroy(struct dst_entry *); | 
|  | 81 | static void		ip6_dst_ifdown(struct dst_entry *, | 
|  | 82 | struct net_device *dev, int how); | 
| Daniel Lezcano | 569d364 | 2008-01-18 03:56:57 -0800 | [diff] [blame] | 83 | static int		 ip6_dst_gc(struct dst_ops *ops); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 |  | 
|  | 85 | static int		ip6_pkt_discard(struct sk_buff *skb); | 
|  | 86 | static int		ip6_pkt_discard_out(struct sk_buff *skb); | 
|  | 87 | static void		ip6_link_failure(struct sk_buff *skb); | 
|  | 88 | static void		ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu); | 
|  | 89 |  | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 90 | #ifdef CONFIG_IPV6_ROUTE_INFO | 
| Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 91 | static struct rt6_info *rt6_add_route_info(struct net *net, | 
| Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 92 | const struct in6_addr *prefix, int prefixlen, | 
|  | 93 | const struct in6_addr *gwaddr, int ifindex, | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 94 | unsigned pref); | 
| Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 95 | static struct rt6_info *rt6_get_route_info(struct net *net, | 
| Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 96 | const struct in6_addr *prefix, int prefixlen, | 
|  | 97 | const struct in6_addr *gwaddr, int ifindex); | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 98 | #endif | 
|  | 99 |  | 
| David S. Miller | 0658254 | 2011-01-27 14:58:42 -0800 | [diff] [blame] | 100 | static u32 *ipv6_cow_metrics(struct dst_entry *dst, unsigned long old) | 
|  | 101 | { | 
|  | 102 | struct rt6_info *rt = (struct rt6_info *) dst; | 
|  | 103 | struct inet_peer *peer; | 
|  | 104 | u32 *p = NULL; | 
|  | 105 |  | 
|  | 106 | if (!rt->rt6i_peer) | 
|  | 107 | rt6_bind_peer(rt, 1); | 
|  | 108 |  | 
|  | 109 | peer = rt->rt6i_peer; | 
|  | 110 | if (peer) { | 
|  | 111 | u32 *old_p = __DST_METRICS_PTR(old); | 
|  | 112 | unsigned long prev, new; | 
|  | 113 |  | 
|  | 114 | p = peer->metrics; | 
|  | 115 | if (inet_metrics_new(peer)) | 
|  | 116 | memcpy(p, old_p, sizeof(u32) * RTAX_MAX); | 
|  | 117 |  | 
|  | 118 | new = (unsigned long) p; | 
|  | 119 | prev = cmpxchg(&dst->_metrics, old, new); | 
|  | 120 |  | 
|  | 121 | if (prev != old) { | 
|  | 122 | p = __DST_METRICS_PTR(prev); | 
|  | 123 | if (prev & DST_METRICS_READ_ONLY) | 
|  | 124 | p = NULL; | 
|  | 125 | } | 
|  | 126 | } | 
|  | 127 | return p; | 
|  | 128 | } | 
|  | 129 |  | 
| Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 130 | static struct dst_ops ip6_dst_ops_template = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | .family			=	AF_INET6, | 
| Harvey Harrison | 09640e6 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 132 | .protocol		=	cpu_to_be16(ETH_P_IPV6), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | .gc			=	ip6_dst_gc, | 
|  | 134 | .gc_thresh		=	1024, | 
|  | 135 | .check			=	ip6_dst_check, | 
| David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 136 | .default_advmss		=	ip6_default_advmss, | 
| David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 137 | .default_mtu		=	ip6_default_mtu, | 
| David S. Miller | 0658254 | 2011-01-27 14:58:42 -0800 | [diff] [blame] | 138 | .cow_metrics		=	ipv6_cow_metrics, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | .destroy		=	ip6_dst_destroy, | 
|  | 140 | .ifdown			=	ip6_dst_ifdown, | 
|  | 141 | .negative_advice	=	ip6_negative_advice, | 
|  | 142 | .link_failure		=	ip6_link_failure, | 
|  | 143 | .update_pmtu		=	ip6_rt_update_pmtu, | 
| Herbert Xu | 1ac06e0 | 2008-05-20 14:32:14 -0700 | [diff] [blame] | 144 | .local_out		=	__ip6_local_out, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | }; | 
|  | 146 |  | 
| Roland Dreier | ec831ea | 2011-01-31 13:16:00 -0800 | [diff] [blame] | 147 | static unsigned int ip6_blackhole_default_mtu(const struct dst_entry *dst) | 
|  | 148 | { | 
|  | 149 | return 0; | 
|  | 150 | } | 
|  | 151 |  | 
| David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 152 | static void ip6_rt_blackhole_update_pmtu(struct dst_entry *dst, u32 mtu) | 
|  | 153 | { | 
|  | 154 | } | 
|  | 155 |  | 
| Held Bernhard | 0972ddb | 2011-04-24 22:07:32 +0000 | [diff] [blame] | 156 | static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst, | 
|  | 157 | unsigned long old) | 
|  | 158 | { | 
|  | 159 | return NULL; | 
|  | 160 | } | 
|  | 161 |  | 
| David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 162 | static struct dst_ops ip6_dst_blackhole_ops = { | 
|  | 163 | .family			=	AF_INET6, | 
| Harvey Harrison | 09640e6 | 2009-02-01 00:45:17 -0800 | [diff] [blame] | 164 | .protocol		=	cpu_to_be16(ETH_P_IPV6), | 
| David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 165 | .destroy		=	ip6_dst_destroy, | 
|  | 166 | .check			=	ip6_dst_check, | 
| Roland Dreier | ec831ea | 2011-01-31 13:16:00 -0800 | [diff] [blame] | 167 | .default_mtu		=	ip6_blackhole_default_mtu, | 
| Eric Dumazet | 214f45c | 2011-02-18 11:39:01 -0800 | [diff] [blame] | 168 | .default_advmss		=	ip6_default_advmss, | 
| David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 169 | .update_pmtu		=	ip6_rt_blackhole_update_pmtu, | 
| Held Bernhard | 0972ddb | 2011-04-24 22:07:32 +0000 | [diff] [blame] | 170 | .cow_metrics		=	ip6_rt_blackhole_cow_metrics, | 
| David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 171 | }; | 
|  | 172 |  | 
| David S. Miller | 62fa8a8 | 2011-01-26 20:51:05 -0800 | [diff] [blame] | 173 | static const u32 ip6_template_metrics[RTAX_MAX] = { | 
|  | 174 | [RTAX_HOPLIMIT - 1] = 255, | 
|  | 175 | }; | 
|  | 176 |  | 
| Daniel Lezcano | bdb3289 | 2008-03-04 13:48:10 -0800 | [diff] [blame] | 177 | static struct rt6_info ip6_null_entry_template = { | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 178 | .dst = { | 
|  | 179 | .__refcnt	= ATOMIC_INIT(1), | 
|  | 180 | .__use		= 1, | 
|  | 181 | .obsolete	= -1, | 
|  | 182 | .error		= -ENETUNREACH, | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 183 | .input		= ip6_pkt_discard, | 
|  | 184 | .output		= ip6_pkt_discard_out, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | }, | 
|  | 186 | .rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP), | 
| Jean-Mickael Guerin | 4f72427 | 2009-05-20 17:38:59 -0700 | [diff] [blame] | 187 | .rt6i_protocol  = RTPROT_KERNEL, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | .rt6i_metric	= ~(u32) 0, | 
|  | 189 | .rt6i_ref	= ATOMIC_INIT(1), | 
|  | 190 | }; | 
|  | 191 |  | 
| Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 192 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | 
|  | 193 |  | 
| David S. Miller | 6723ab5 | 2006-10-18 21:20:57 -0700 | [diff] [blame] | 194 | static int ip6_pkt_prohibit(struct sk_buff *skb); | 
|  | 195 | static int ip6_pkt_prohibit_out(struct sk_buff *skb); | 
| David S. Miller | 6723ab5 | 2006-10-18 21:20:57 -0700 | [diff] [blame] | 196 |  | 
| Adrian Bunk | 280a34c | 2008-04-21 02:29:32 -0700 | [diff] [blame] | 197 | static struct rt6_info ip6_prohibit_entry_template = { | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 198 | .dst = { | 
|  | 199 | .__refcnt	= ATOMIC_INIT(1), | 
|  | 200 | .__use		= 1, | 
|  | 201 | .obsolete	= -1, | 
|  | 202 | .error		= -EACCES, | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 203 | .input		= ip6_pkt_prohibit, | 
|  | 204 | .output		= ip6_pkt_prohibit_out, | 
| Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 205 | }, | 
|  | 206 | .rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP), | 
| Jean-Mickael Guerin | 4f72427 | 2009-05-20 17:38:59 -0700 | [diff] [blame] | 207 | .rt6i_protocol  = RTPROT_KERNEL, | 
| Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 208 | .rt6i_metric	= ~(u32) 0, | 
|  | 209 | .rt6i_ref	= ATOMIC_INIT(1), | 
|  | 210 | }; | 
|  | 211 |  | 
| Daniel Lezcano | bdb3289 | 2008-03-04 13:48:10 -0800 | [diff] [blame] | 212 | static struct rt6_info ip6_blk_hole_entry_template = { | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 213 | .dst = { | 
|  | 214 | .__refcnt	= ATOMIC_INIT(1), | 
|  | 215 | .__use		= 1, | 
|  | 216 | .obsolete	= -1, | 
|  | 217 | .error		= -EINVAL, | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 218 | .input		= dst_discard, | 
|  | 219 | .output		= dst_discard, | 
| Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 220 | }, | 
|  | 221 | .rt6i_flags	= (RTF_REJECT | RTF_NONEXTHOP), | 
| Jean-Mickael Guerin | 4f72427 | 2009-05-20 17:38:59 -0700 | [diff] [blame] | 222 | .rt6i_protocol  = RTPROT_KERNEL, | 
| Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 223 | .rt6i_metric	= ~(u32) 0, | 
|  | 224 | .rt6i_ref	= ATOMIC_INIT(1), | 
|  | 225 | }; | 
|  | 226 |  | 
|  | 227 | #endif | 
|  | 228 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | /* allocate dst with ip6_dst_ops */ | 
| David S. Miller | 5c1e6aa | 2011-04-28 14:13:38 -0700 | [diff] [blame] | 230 | static inline struct rt6_info *ip6_dst_alloc(struct dst_ops *ops, | 
| David S. Miller | 957c665 | 2011-06-24 15:25:00 -0700 | [diff] [blame] | 231 | struct net_device *dev, | 
|  | 232 | int flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { | 
| David S. Miller | 957c665 | 2011-06-24 15:25:00 -0700 | [diff] [blame] | 234 | struct rt6_info *rt = dst_alloc(ops, dev, 0, 0, flags); | 
| David S. Miller | cf91166 | 2011-04-28 14:31:47 -0700 | [diff] [blame] | 235 |  | 
|  | 236 | memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry)); | 
|  | 237 |  | 
|  | 238 | return rt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } | 
|  | 240 |  | 
|  | 241 | static void ip6_dst_destroy(struct dst_entry *dst) | 
|  | 242 | { | 
|  | 243 | struct rt6_info *rt = (struct rt6_info *)dst; | 
|  | 244 | struct inet6_dev *idev = rt->rt6i_idev; | 
| David S. Miller | b341936 | 2010-11-30 12:27:11 -0800 | [diff] [blame] | 245 | struct inet_peer *peer = rt->rt6i_peer; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 |  | 
|  | 247 | if (idev != NULL) { | 
|  | 248 | rt->rt6i_idev = NULL; | 
|  | 249 | in6_dev_put(idev); | 
| YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 250 | } | 
| David S. Miller | b341936 | 2010-11-30 12:27:11 -0800 | [diff] [blame] | 251 | if (peer) { | 
| David S. Miller | b341936 | 2010-11-30 12:27:11 -0800 | [diff] [blame] | 252 | rt->rt6i_peer = NULL; | 
|  | 253 | inet_putpeer(peer); | 
|  | 254 | } | 
|  | 255 | } | 
|  | 256 |  | 
| David S. Miller | 6431cbc | 2011-02-07 20:38:06 -0800 | [diff] [blame] | 257 | static atomic_t __rt6_peer_genid = ATOMIC_INIT(0); | 
|  | 258 |  | 
|  | 259 | static u32 rt6_peer_genid(void) | 
|  | 260 | { | 
|  | 261 | return atomic_read(&__rt6_peer_genid); | 
|  | 262 | } | 
|  | 263 |  | 
| David S. Miller | b341936 | 2010-11-30 12:27:11 -0800 | [diff] [blame] | 264 | void rt6_bind_peer(struct rt6_info *rt, int create) | 
|  | 265 | { | 
|  | 266 | struct inet_peer *peer; | 
|  | 267 |  | 
| David S. Miller | b341936 | 2010-11-30 12:27:11 -0800 | [diff] [blame] | 268 | peer = inet_getpeer_v6(&rt->rt6i_dst.addr, create); | 
|  | 269 | if (peer && cmpxchg(&rt->rt6i_peer, NULL, peer) != NULL) | 
|  | 270 | inet_putpeer(peer); | 
| David S. Miller | 6431cbc | 2011-02-07 20:38:06 -0800 | [diff] [blame] | 271 | else | 
|  | 272 | rt->rt6i_peer_genid = rt6_peer_genid(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } | 
|  | 274 |  | 
|  | 275 | static void ip6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, | 
|  | 276 | int how) | 
|  | 277 | { | 
|  | 278 | struct rt6_info *rt = (struct rt6_info *)dst; | 
|  | 279 | struct inet6_dev *idev = rt->rt6i_idev; | 
| Denis V. Lunev | 5a3e55d | 2007-12-07 00:38:10 -0800 | [diff] [blame] | 280 | struct net_device *loopback_dev = | 
| YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 281 | dev_net(dev)->loopback_dev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 |  | 
| Denis V. Lunev | 5a3e55d | 2007-12-07 00:38:10 -0800 | [diff] [blame] | 283 | if (dev != loopback_dev && idev != NULL && idev->dev == dev) { | 
|  | 284 | struct inet6_dev *loopback_idev = | 
|  | 285 | in6_dev_get(loopback_dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | if (loopback_idev != NULL) { | 
|  | 287 | rt->rt6i_idev = loopback_idev; | 
|  | 288 | in6_dev_put(idev); | 
|  | 289 | } | 
|  | 290 | } | 
|  | 291 | } | 
|  | 292 |  | 
|  | 293 | static __inline__ int rt6_check_expired(const struct rt6_info *rt) | 
|  | 294 | { | 
| Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 295 | return (rt->rt6i_flags & RTF_EXPIRES) && | 
|  | 296 | time_after(jiffies, rt->rt6i_expires); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } | 
|  | 298 |  | 
| Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 299 | static inline int rt6_need_strict(const struct in6_addr *daddr) | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 300 | { | 
| Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 301 | return ipv6_addr_type(daddr) & | 
|  | 302 | (IPV6_ADDR_MULTICAST | IPV6_ADDR_LINKLOCAL | IPV6_ADDR_LOOPBACK); | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 303 | } | 
|  | 304 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | /* | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 306 | *	Route lookup. Any table->tb6_lock is implied. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | */ | 
|  | 308 |  | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 309 | static inline struct rt6_info *rt6_device_match(struct net *net, | 
|  | 310 | struct rt6_info *rt, | 
| Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 311 | const struct in6_addr *saddr, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | int oif, | 
| YOSHIFUJI Hideaki | d420895 | 2008-06-27 20:14:54 -0700 | [diff] [blame] | 313 | int flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { | 
|  | 315 | struct rt6_info *local = NULL; | 
|  | 316 | struct rt6_info *sprt; | 
|  | 317 |  | 
| YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 318 | if (!oif && ipv6_addr_any(saddr)) | 
|  | 319 | goto out; | 
|  | 320 |  | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 321 | for (sprt = rt; sprt; sprt = sprt->dst.rt6_next) { | 
| YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 322 | struct net_device *dev = sprt->rt6i_dev; | 
|  | 323 |  | 
|  | 324 | if (oif) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | if (dev->ifindex == oif) | 
|  | 326 | return sprt; | 
|  | 327 | if (dev->flags & IFF_LOOPBACK) { | 
|  | 328 | if (sprt->rt6i_idev == NULL || | 
|  | 329 | sprt->rt6i_idev->dev->ifindex != oif) { | 
| YOSHIFUJI Hideaki | d420895 | 2008-06-27 20:14:54 -0700 | [diff] [blame] | 330 | if (flags & RT6_LOOKUP_F_IFACE && oif) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | continue; | 
| YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 332 | if (local && (!oif || | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | local->rt6i_idev->dev->ifindex == oif)) | 
|  | 334 | continue; | 
|  | 335 | } | 
|  | 336 | local = sprt; | 
|  | 337 | } | 
| YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 338 | } else { | 
|  | 339 | if (ipv6_chk_addr(net, saddr, dev, | 
|  | 340 | flags & RT6_LOOKUP_F_IFACE)) | 
|  | 341 | return sprt; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | } | 
| YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 343 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 |  | 
| YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 345 | if (oif) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | if (local) | 
|  | 347 | return local; | 
|  | 348 |  | 
| YOSHIFUJI Hideaki | d420895 | 2008-06-27 20:14:54 -0700 | [diff] [blame] | 349 | if (flags & RT6_LOOKUP_F_IFACE) | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 350 | return net->ipv6.ip6_null_entry; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | } | 
| YOSHIFUJI Hideaki | dd3abc4 | 2008-07-02 18:30:18 +0900 | [diff] [blame] | 352 | out: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | return rt; | 
|  | 354 | } | 
|  | 355 |  | 
| YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 356 | #ifdef CONFIG_IPV6_ROUTER_PREF | 
|  | 357 | static void rt6_probe(struct rt6_info *rt) | 
|  | 358 | { | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 359 | struct neighbour *neigh; | 
| YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 360 | /* | 
|  | 361 | * Okay, this does not seem to be appropriate | 
|  | 362 | * for now, however, we need to check if it | 
|  | 363 | * is really so; aka Router Reachability Probing. | 
|  | 364 | * | 
|  | 365 | * Router Reachability Probe MUST be rate-limited | 
|  | 366 | * to no more than one per minute. | 
|  | 367 | */ | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 368 | rcu_read_lock(); | 
|  | 369 | neigh = rt ? dst_get_neighbour(&rt->dst) : NULL; | 
| YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 370 | if (!neigh || (neigh->nud_state & NUD_VALID)) | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 371 | goto out; | 
| YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 372 | read_lock_bh(&neigh->lock); | 
|  | 373 | if (!(neigh->nud_state & NUD_VALID) && | 
| YOSHIFUJI Hideaki | 52e1635 | 2006-03-20 17:05:47 -0800 | [diff] [blame] | 374 | time_after(jiffies, neigh->updated + rt->rt6i_idev->cnf.rtr_probe_interval)) { | 
| YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 375 | struct in6_addr mcaddr; | 
|  | 376 | struct in6_addr *target; | 
|  | 377 |  | 
|  | 378 | neigh->updated = jiffies; | 
|  | 379 | read_unlock_bh(&neigh->lock); | 
|  | 380 |  | 
|  | 381 | target = (struct in6_addr *)&neigh->primary_key; | 
|  | 382 | addrconf_addr_solict_mult(target, &mcaddr); | 
|  | 383 | ndisc_send_ns(rt->rt6i_dev, NULL, target, &mcaddr, NULL); | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 384 | } else { | 
| YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 385 | read_unlock_bh(&neigh->lock); | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 386 | } | 
|  | 387 | out: | 
|  | 388 | rcu_read_unlock(); | 
| YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 389 | } | 
|  | 390 | #else | 
|  | 391 | static inline void rt6_probe(struct rt6_info *rt) | 
|  | 392 | { | 
| YOSHIFUJI Hideaki | 2709725 | 2006-03-20 17:05:13 -0800 | [diff] [blame] | 393 | } | 
|  | 394 | #endif | 
|  | 395 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | /* | 
| YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 397 | * Default Router Selection (RFC 2461 6.3.6) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | */ | 
| Dave Jones | b6f99a2 | 2007-03-22 12:27:49 -0700 | [diff] [blame] | 399 | static inline int rt6_check_dev(struct rt6_info *rt, int oif) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | { | 
| YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 401 | struct net_device *dev = rt->rt6i_dev; | 
| David S. Miller | 161980f | 2007-04-06 11:42:27 -0700 | [diff] [blame] | 402 | if (!oif || dev->ifindex == oif) | 
| YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 403 | return 2; | 
| David S. Miller | 161980f | 2007-04-06 11:42:27 -0700 | [diff] [blame] | 404 | if ((dev->flags & IFF_LOOPBACK) && | 
|  | 405 | rt->rt6i_idev && rt->rt6i_idev->dev->ifindex == oif) | 
|  | 406 | return 1; | 
|  | 407 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | } | 
|  | 409 |  | 
| Dave Jones | b6f99a2 | 2007-03-22 12:27:49 -0700 | [diff] [blame] | 410 | static inline int rt6_check_neigh(struct rt6_info *rt) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | { | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 412 | struct neighbour *neigh; | 
| YOSHIFUJI Hideaki | 398bcbe | 2008-01-19 00:35:16 -0800 | [diff] [blame] | 413 | int m; | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 414 |  | 
|  | 415 | rcu_read_lock(); | 
|  | 416 | neigh = dst_get_neighbour(&rt->dst); | 
| YOSHIFUJI Hideaki | 4d0c591 | 2006-05-26 13:23:41 -0700 | [diff] [blame] | 417 | if (rt->rt6i_flags & RTF_NONEXTHOP || | 
|  | 418 | !(rt->rt6i_flags & RTF_GATEWAY)) | 
|  | 419 | m = 1; | 
|  | 420 | else if (neigh) { | 
| YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 421 | read_lock_bh(&neigh->lock); | 
|  | 422 | if (neigh->nud_state & NUD_VALID) | 
| YOSHIFUJI Hideaki | 4d0c591 | 2006-05-26 13:23:41 -0700 | [diff] [blame] | 423 | m = 2; | 
| YOSHIFUJI Hideaki | 398bcbe | 2008-01-19 00:35:16 -0800 | [diff] [blame] | 424 | #ifdef CONFIG_IPV6_ROUTER_PREF | 
|  | 425 | else if (neigh->nud_state & NUD_FAILED) | 
|  | 426 | m = 0; | 
|  | 427 | #endif | 
|  | 428 | else | 
| YOSHIFUJI Hideaki | ea73ee2 | 2006-11-06 09:45:44 -0800 | [diff] [blame] | 429 | m = 1; | 
| YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 430 | read_unlock_bh(&neigh->lock); | 
| YOSHIFUJI Hideaki | 398bcbe | 2008-01-19 00:35:16 -0800 | [diff] [blame] | 431 | } else | 
|  | 432 | m = 0; | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 433 | rcu_read_unlock(); | 
| YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 434 | return m; | 
|  | 435 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 |  | 
| YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 437 | static int rt6_score_route(struct rt6_info *rt, int oif, | 
|  | 438 | int strict) | 
|  | 439 | { | 
| YOSHIFUJI Hideaki | 4d0c591 | 2006-05-26 13:23:41 -0700 | [diff] [blame] | 440 | int m, n; | 
| YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 441 |  | 
| YOSHIFUJI Hideaki | 4d0c591 | 2006-05-26 13:23:41 -0700 | [diff] [blame] | 442 | m = rt6_check_dev(rt, oif); | 
| YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 443 | if (!m && (strict & RT6_LOOKUP_F_IFACE)) | 
| YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 444 | return -1; | 
| YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 445 | #ifdef CONFIG_IPV6_ROUTER_PREF | 
|  | 446 | m |= IPV6_DECODE_PREF(IPV6_EXTRACT_PREF(rt->rt6i_flags)) << 2; | 
|  | 447 | #endif | 
| YOSHIFUJI Hideaki | 4d0c591 | 2006-05-26 13:23:41 -0700 | [diff] [blame] | 448 | n = rt6_check_neigh(rt); | 
| YOSHIFUJI Hideaki | 557e92e | 2006-11-06 09:45:45 -0800 | [diff] [blame] | 449 | if (!n && (strict & RT6_LOOKUP_F_REACHABLE)) | 
| YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 450 | return -1; | 
|  | 451 | return m; | 
|  | 452 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 |  | 
| David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 454 | static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict, | 
|  | 455 | int *mpri, struct rt6_info *match) | 
| YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 456 | { | 
| David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 457 | int m; | 
|  | 458 |  | 
|  | 459 | if (rt6_check_expired(rt)) | 
|  | 460 | goto out; | 
|  | 461 |  | 
|  | 462 | m = rt6_score_route(rt, oif, strict); | 
|  | 463 | if (m < 0) | 
|  | 464 | goto out; | 
|  | 465 |  | 
|  | 466 | if (m > *mpri) { | 
|  | 467 | if (strict & RT6_LOOKUP_F_REACHABLE) | 
|  | 468 | rt6_probe(match); | 
|  | 469 | *mpri = m; | 
|  | 470 | match = rt; | 
|  | 471 | } else if (strict & RT6_LOOKUP_F_REACHABLE) { | 
|  | 472 | rt6_probe(rt); | 
|  | 473 | } | 
|  | 474 |  | 
|  | 475 | out: | 
|  | 476 | return match; | 
|  | 477 | } | 
|  | 478 |  | 
|  | 479 | static struct rt6_info *find_rr_leaf(struct fib6_node *fn, | 
|  | 480 | struct rt6_info *rr_head, | 
|  | 481 | u32 metric, int oif, int strict) | 
|  | 482 | { | 
|  | 483 | struct rt6_info *rt, *match; | 
| YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 484 | int mpri = -1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 |  | 
| David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 486 | match = NULL; | 
|  | 487 | for (rt = rr_head; rt && rt->rt6i_metric == metric; | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 488 | rt = rt->dst.rt6_next) | 
| David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 489 | match = find_match(rt, oif, strict, &mpri, match); | 
|  | 490 | for (rt = fn->leaf; rt && rt != rr_head && rt->rt6i_metric == metric; | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 491 | rt = rt->dst.rt6_next) | 
| David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 492 | match = find_match(rt, oif, strict, &mpri, match); | 
| YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 493 |  | 
| David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 494 | return match; | 
|  | 495 | } | 
| YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 496 |  | 
| David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 497 | static struct rt6_info *rt6_select(struct fib6_node *fn, int oif, int strict) | 
|  | 498 | { | 
|  | 499 | struct rt6_info *match, *rt0; | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 500 | struct net *net; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 |  | 
| David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 502 | RT6_TRACE("%s(fn->leaf=%p, oif=%d)\n", | 
| Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 503 | __func__, fn->leaf, oif); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 |  | 
| David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 505 | rt0 = fn->rr_ptr; | 
|  | 506 | if (!rt0) | 
|  | 507 | fn->rr_ptr = rt0 = fn->leaf; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 |  | 
| David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 509 | match = find_rr_leaf(fn, rt0, rt0->rt6i_metric, oif, strict); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 |  | 
| YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 511 | if (!match && | 
| David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 512 | (strict & RT6_LOOKUP_F_REACHABLE)) { | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 513 | struct rt6_info *next = rt0->dst.rt6_next; | 
| David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 514 |  | 
| YOSHIFUJI Hideaki | 554cfb7 | 2006-03-20 17:00:26 -0800 | [diff] [blame] | 515 | /* no entries matched; do round-robin */ | 
| David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 516 | if (!next || next->rt6i_metric != rt0->rt6i_metric) | 
|  | 517 | next = fn->leaf; | 
|  | 518 |  | 
|  | 519 | if (next != rt0) | 
|  | 520 | fn->rr_ptr = next; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | } | 
|  | 522 |  | 
| David S. Miller | f11e665 | 2007-03-24 20:36:25 -0700 | [diff] [blame] | 523 | RT6_TRACE("%s() => %p\n", | 
| Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 524 | __func__, match); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 |  | 
| YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 526 | net = dev_net(rt0->rt6i_dev); | 
| Eric Dumazet | a02cec2 | 2010-09-22 20:43:57 +0000 | [diff] [blame] | 527 | return match ? match : net->ipv6.ip6_null_entry; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | } | 
|  | 529 |  | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 530 | #ifdef CONFIG_IPV6_ROUTE_INFO | 
|  | 531 | int rt6_route_rcv(struct net_device *dev, u8 *opt, int len, | 
| Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 532 | const struct in6_addr *gwaddr) | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 533 | { | 
| YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 534 | struct net *net = dev_net(dev); | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 535 | struct route_info *rinfo = (struct route_info *) opt; | 
|  | 536 | struct in6_addr prefix_buf, *prefix; | 
|  | 537 | unsigned int pref; | 
| YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 538 | unsigned long lifetime; | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 539 | struct rt6_info *rt; | 
|  | 540 |  | 
|  | 541 | if (len < sizeof(struct route_info)) { | 
|  | 542 | return -EINVAL; | 
|  | 543 | } | 
|  | 544 |  | 
|  | 545 | /* Sanity check for prefix_len and length */ | 
|  | 546 | if (rinfo->length > 3) { | 
|  | 547 | return -EINVAL; | 
|  | 548 | } else if (rinfo->prefix_len > 128) { | 
|  | 549 | return -EINVAL; | 
|  | 550 | } else if (rinfo->prefix_len > 64) { | 
|  | 551 | if (rinfo->length < 2) { | 
|  | 552 | return -EINVAL; | 
|  | 553 | } | 
|  | 554 | } else if (rinfo->prefix_len > 0) { | 
|  | 555 | if (rinfo->length < 1) { | 
|  | 556 | return -EINVAL; | 
|  | 557 | } | 
|  | 558 | } | 
|  | 559 |  | 
|  | 560 | pref = rinfo->route_pref; | 
|  | 561 | if (pref == ICMPV6_ROUTER_PREF_INVALID) | 
| Jens Rosenboom | 3933fc9 | 2009-09-10 06:25:11 +0000 | [diff] [blame] | 562 | return -EINVAL; | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 563 |  | 
| YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 564 | lifetime = addrconf_timeout_fixup(ntohl(rinfo->lifetime), HZ); | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 565 |  | 
|  | 566 | if (rinfo->length == 3) | 
|  | 567 | prefix = (struct in6_addr *)rinfo->prefix; | 
|  | 568 | else { | 
|  | 569 | /* this function is safe */ | 
|  | 570 | ipv6_addr_prefix(&prefix_buf, | 
|  | 571 | (struct in6_addr *)rinfo->prefix, | 
|  | 572 | rinfo->prefix_len); | 
|  | 573 | prefix = &prefix_buf; | 
|  | 574 | } | 
|  | 575 |  | 
| Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 576 | rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr, | 
|  | 577 | dev->ifindex); | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 578 |  | 
|  | 579 | if (rt && !lifetime) { | 
| Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 580 | ip6_del_rt(rt); | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 581 | rt = NULL; | 
|  | 582 | } | 
|  | 583 |  | 
|  | 584 | if (!rt && lifetime) | 
| Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 585 | rt = rt6_add_route_info(net, prefix, rinfo->prefix_len, gwaddr, dev->ifindex, | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 586 | pref); | 
|  | 587 | else if (rt) | 
|  | 588 | rt->rt6i_flags = RTF_ROUTEINFO | | 
|  | 589 | (rt->rt6i_flags & ~RTF_PREF_MASK) | RTF_PREF(pref); | 
|  | 590 |  | 
|  | 591 | if (rt) { | 
| YOSHIFUJI Hideaki | 4bed72e | 2008-05-27 17:37:49 +0900 | [diff] [blame] | 592 | if (!addrconf_finite_timeout(lifetime)) { | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 593 | rt->rt6i_flags &= ~RTF_EXPIRES; | 
|  | 594 | } else { | 
|  | 595 | rt->rt6i_expires = jiffies + HZ * lifetime; | 
|  | 596 | rt->rt6i_flags |= RTF_EXPIRES; | 
|  | 597 | } | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 598 | dst_release(&rt->dst); | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 599 | } | 
|  | 600 | return 0; | 
|  | 601 | } | 
|  | 602 | #endif | 
|  | 603 |  | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 604 | #define BACKTRACK(__net, saddr)			\ | 
| YOSHIFUJI Hideaki | 982f56f | 2006-08-23 17:22:39 -0700 | [diff] [blame] | 605 | do { \ | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 606 | if (rt == __net->ipv6.ip6_null_entry) {	\ | 
| YOSHIFUJI Hideaki | 982f56f | 2006-08-23 17:22:39 -0700 | [diff] [blame] | 607 | struct fib6_node *pn; \ | 
| Ville Nuorvala | e0eda7b | 2006-10-16 22:11:11 -0700 | [diff] [blame] | 608 | while (1) { \ | 
| YOSHIFUJI Hideaki | 982f56f | 2006-08-23 17:22:39 -0700 | [diff] [blame] | 609 | if (fn->fn_flags & RTN_TL_ROOT) \ | 
|  | 610 | goto out; \ | 
|  | 611 | pn = fn->parent; \ | 
|  | 612 | if (FIB6_SUBTREE(pn) && FIB6_SUBTREE(pn) != fn) \ | 
| Kim Nordlund | 8bce65b | 2006-12-13 16:38:29 -0800 | [diff] [blame] | 613 | fn = fib6_lookup(FIB6_SUBTREE(pn), NULL, saddr); \ | 
| YOSHIFUJI Hideaki | 982f56f | 2006-08-23 17:22:39 -0700 | [diff] [blame] | 614 | else \ | 
|  | 615 | fn = pn; \ | 
|  | 616 | if (fn->fn_flags & RTN_RTINFO) \ | 
|  | 617 | goto restart; \ | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 618 | } \ | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 619 | } \ | 
| YOSHIFUJI Hideaki | 982f56f | 2006-08-23 17:22:39 -0700 | [diff] [blame] | 620 | } while(0) | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 621 |  | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 622 | static struct rt6_info *ip6_pol_route_lookup(struct net *net, | 
|  | 623 | struct fib6_table *table, | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 624 | struct flowi6 *fl6, int flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | { | 
|  | 626 | struct fib6_node *fn; | 
|  | 627 | struct rt6_info *rt; | 
|  | 628 |  | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 629 | read_lock_bh(&table->tb6_lock); | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 630 | fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 631 | restart: | 
|  | 632 | rt = fn->leaf; | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 633 | rt = rt6_device_match(net, rt, &fl6->saddr, fl6->flowi6_oif, flags); | 
|  | 634 | BACKTRACK(net, &fl6->saddr); | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 635 | out: | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 636 | dst_use(&rt->dst, jiffies); | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 637 | read_unlock_bh(&table->tb6_lock); | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 638 | return rt; | 
|  | 639 |  | 
|  | 640 | } | 
|  | 641 |  | 
| YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 642 | struct rt6_info *rt6_lookup(struct net *net, const struct in6_addr *daddr, | 
|  | 643 | const struct in6_addr *saddr, int oif, int strict) | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 644 | { | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 645 | struct flowi6 fl6 = { | 
|  | 646 | .flowi6_oif = oif, | 
|  | 647 | .daddr = *daddr, | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 648 | }; | 
|  | 649 | struct dst_entry *dst; | 
| YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 650 | int flags = strict ? RT6_LOOKUP_F_IFACE : 0; | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 651 |  | 
| Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 652 | if (saddr) { | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 653 | memcpy(&fl6.saddr, saddr, sizeof(*saddr)); | 
| Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 654 | flags |= RT6_LOOKUP_F_HAS_SADDR; | 
|  | 655 | } | 
|  | 656 |  | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 657 | dst = fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_lookup); | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 658 | if (dst->error == 0) | 
|  | 659 | return (struct rt6_info *) dst; | 
|  | 660 |  | 
|  | 661 | dst_release(dst); | 
|  | 662 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | return NULL; | 
|  | 664 | } | 
|  | 665 |  | 
| YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 666 | EXPORT_SYMBOL(rt6_lookup); | 
|  | 667 |  | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 668 | /* ip6_ins_rt is called with FREE table->tb6_lock. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | It takes new route entry, the addition fails by any reason the | 
|  | 670 | route is freed. In any case, if caller does not hold it, it may | 
|  | 671 | be destroyed. | 
|  | 672 | */ | 
|  | 673 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 674 | static int __ip6_ins_rt(struct rt6_info *rt, struct nl_info *info) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | { | 
|  | 676 | int err; | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 677 | struct fib6_table *table; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 |  | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 679 | table = rt->rt6i_table; | 
|  | 680 | write_lock_bh(&table->tb6_lock); | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 681 | err = fib6_add(&table->tb6_root, rt, info); | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 682 | write_unlock_bh(&table->tb6_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 |  | 
|  | 684 | return err; | 
|  | 685 | } | 
|  | 686 |  | 
| Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 687 | int ip6_ins_rt(struct rt6_info *rt) | 
|  | 688 | { | 
| Denis V. Lunev | 4d1169c | 2008-01-10 03:26:13 -0800 | [diff] [blame] | 689 | struct nl_info info = { | 
| YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 690 | .nl_net = dev_net(rt->rt6i_dev), | 
| Denis V. Lunev | 4d1169c | 2008-01-10 03:26:13 -0800 | [diff] [blame] | 691 | }; | 
| Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 692 | return __ip6_ins_rt(rt, &info); | 
| Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 693 | } | 
|  | 694 |  | 
| Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 695 | static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort, const struct in6_addr *daddr, | 
|  | 696 | const struct in6_addr *saddr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | struct rt6_info *rt; | 
|  | 699 |  | 
|  | 700 | /* | 
|  | 701 | *	Clone the route. | 
|  | 702 | */ | 
|  | 703 |  | 
|  | 704 | rt = ip6_rt_copy(ort); | 
|  | 705 |  | 
|  | 706 | if (rt) { | 
| David S. Miller | 14deae4 | 2009-01-04 16:04:39 -0800 | [diff] [blame] | 707 | struct neighbour *neigh; | 
|  | 708 | int attempts = !in_softirq(); | 
|  | 709 |  | 
| YOSHIFUJI Hideaki | 58c4fb8 | 2005-12-21 22:56:42 +0900 | [diff] [blame] | 710 | if (!(rt->rt6i_flags&RTF_GATEWAY)) { | 
|  | 711 | if (rt->rt6i_dst.plen != 128 && | 
|  | 712 | ipv6_addr_equal(&rt->rt6i_dst.addr, daddr)) | 
|  | 713 | rt->rt6i_flags |= RTF_ANYCAST; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | ipv6_addr_copy(&rt->rt6i_gateway, daddr); | 
| YOSHIFUJI Hideaki | 58c4fb8 | 2005-12-21 22:56:42 +0900 | [diff] [blame] | 715 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 |  | 
| YOSHIFUJI Hideaki | 58c4fb8 | 2005-12-21 22:56:42 +0900 | [diff] [blame] | 717 | ipv6_addr_copy(&rt->rt6i_dst.addr, daddr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | rt->rt6i_dst.plen = 128; | 
|  | 719 | rt->rt6i_flags |= RTF_CACHE; | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 720 | rt->dst.flags |= DST_HOST; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 |  | 
|  | 722 | #ifdef CONFIG_IPV6_SUBTREES | 
|  | 723 | if (rt->rt6i_src.plen && saddr) { | 
|  | 724 | ipv6_addr_copy(&rt->rt6i_src.addr, saddr); | 
|  | 725 | rt->rt6i_src.plen = 128; | 
|  | 726 | } | 
|  | 727 | #endif | 
|  | 728 |  | 
| David S. Miller | 14deae4 | 2009-01-04 16:04:39 -0800 | [diff] [blame] | 729 | retry: | 
|  | 730 | neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway); | 
|  | 731 | if (IS_ERR(neigh)) { | 
|  | 732 | struct net *net = dev_net(rt->rt6i_dev); | 
|  | 733 | int saved_rt_min_interval = | 
|  | 734 | net->ipv6.sysctl.ip6_rt_gc_min_interval; | 
|  | 735 | int saved_rt_elasticity = | 
|  | 736 | net->ipv6.sysctl.ip6_rt_gc_elasticity; | 
|  | 737 |  | 
|  | 738 | if (attempts-- > 0) { | 
|  | 739 | net->ipv6.sysctl.ip6_rt_gc_elasticity = 1; | 
|  | 740 | net->ipv6.sysctl.ip6_rt_gc_min_interval = 0; | 
|  | 741 |  | 
| Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 742 | ip6_dst_gc(&net->ipv6.ip6_dst_ops); | 
| David S. Miller | 14deae4 | 2009-01-04 16:04:39 -0800 | [diff] [blame] | 743 |  | 
|  | 744 | net->ipv6.sysctl.ip6_rt_gc_elasticity = | 
|  | 745 | saved_rt_elasticity; | 
|  | 746 | net->ipv6.sysctl.ip6_rt_gc_min_interval = | 
|  | 747 | saved_rt_min_interval; | 
|  | 748 | goto retry; | 
|  | 749 | } | 
|  | 750 |  | 
|  | 751 | if (net_ratelimit()) | 
|  | 752 | printk(KERN_WARNING | 
| Ulrich Weber | 7e1b33e | 2010-09-27 15:02:18 -0700 | [diff] [blame] | 753 | "ipv6: Neighbour table overflow.\n"); | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 754 | dst_free(&rt->dst); | 
| David S. Miller | 14deae4 | 2009-01-04 16:04:39 -0800 | [diff] [blame] | 755 | return NULL; | 
|  | 756 | } | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 757 | dst_set_neighbour(&rt->dst, neigh); | 
| YOSHIFUJI Hideaki | 95a9a5b | 2006-03-20 16:55:51 -0800 | [diff] [blame] | 758 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 |  | 
| YOSHIFUJI Hideaki | 95a9a5b | 2006-03-20 16:55:51 -0800 | [diff] [blame] | 760 | return rt; | 
|  | 761 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 |  | 
| Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 763 | static struct rt6_info *rt6_alloc_clone(struct rt6_info *ort, const struct in6_addr *daddr) | 
| YOSHIFUJI Hideaki | 299d993 | 2006-03-20 16:58:32 -0800 | [diff] [blame] | 764 | { | 
|  | 765 | struct rt6_info *rt = ip6_rt_copy(ort); | 
|  | 766 | if (rt) { | 
|  | 767 | ipv6_addr_copy(&rt->rt6i_dst.addr, daddr); | 
|  | 768 | rt->rt6i_dst.plen = 128; | 
|  | 769 | rt->rt6i_flags |= RTF_CACHE; | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 770 | rt->dst.flags |= DST_HOST; | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 771 | dst_set_neighbour(&rt->dst, neigh_clone(dst_get_neighbour_raw(&ort->dst))); | 
| YOSHIFUJI Hideaki | 299d993 | 2006-03-20 16:58:32 -0800 | [diff] [blame] | 772 | } | 
|  | 773 | return rt; | 
|  | 774 | } | 
|  | 775 |  | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 776 | static struct rt6_info *ip6_pol_route(struct net *net, struct fib6_table *table, int oif, | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 777 | struct flowi6 *fl6, int flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | { | 
|  | 779 | struct fib6_node *fn; | 
| YOSHIFUJI Hideaki | 519fbd8 | 2006-03-20 17:00:05 -0800 | [diff] [blame] | 780 | struct rt6_info *rt, *nrt; | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 781 | int strict = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | int attempts = 3; | 
| YOSHIFUJI Hideaki | 519fbd8 | 2006-03-20 17:00:05 -0800 | [diff] [blame] | 783 | int err; | 
| YOSHIFUJI Hideaki | 53b7997 | 2008-07-19 22:35:03 -0700 | [diff] [blame] | 784 | int reachable = net->ipv6.devconf_all->forwarding ? 0 : RT6_LOOKUP_F_REACHABLE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 |  | 
| YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 786 | strict |= flags & RT6_LOOKUP_F_IFACE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 |  | 
|  | 788 | relookup: | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 789 | read_lock_bh(&table->tb6_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 |  | 
| YOSHIFUJI Hideaki | 8238dd0 | 2006-03-20 17:04:35 -0800 | [diff] [blame] | 791 | restart_2: | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 792 | fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 |  | 
|  | 794 | restart: | 
| Pavel Emelyanov | 4acad72 | 2007-10-15 13:02:51 -0700 | [diff] [blame] | 795 | rt = rt6_select(fn, oif, strict | reachable); | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 796 |  | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 797 | BACKTRACK(net, &fl6->saddr); | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 798 | if (rt == net->ipv6.ip6_null_entry || | 
| YOSHIFUJI Hideaki | 8238dd0 | 2006-03-20 17:04:35 -0800 | [diff] [blame] | 799 | rt->rt6i_flags & RTF_CACHE) | 
| YOSHIFUJI Hideaki | 1ddef04 | 2006-03-20 17:01:24 -0800 | [diff] [blame] | 800 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 |  | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 802 | dst_hold(&rt->dst); | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 803 | read_unlock_bh(&table->tb6_lock); | 
| YOSHIFUJI Hideaki | fb9de91 | 2006-03-20 16:59:08 -0800 | [diff] [blame] | 804 |  | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 805 | if (!dst_get_neighbour_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP)) | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 806 | nrt = rt6_alloc_cow(rt, &fl6->daddr, &fl6->saddr); | 
| David S. Miller | 7343ff3 | 2011-03-09 19:55:25 -0800 | [diff] [blame] | 807 | else if (!(rt->dst.flags & DST_HOST)) | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 808 | nrt = rt6_alloc_clone(rt, &fl6->daddr); | 
| David S. Miller | 7343ff3 | 2011-03-09 19:55:25 -0800 | [diff] [blame] | 809 | else | 
|  | 810 | goto out2; | 
| YOSHIFUJI Hideaki | e40cf35 | 2006-03-20 16:59:27 -0800 | [diff] [blame] | 811 |  | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 812 | dst_release(&rt->dst); | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 813 | rt = nrt ? : net->ipv6.ip6_null_entry; | 
| YOSHIFUJI Hideaki | 519fbd8 | 2006-03-20 17:00:05 -0800 | [diff] [blame] | 814 |  | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 815 | dst_hold(&rt->dst); | 
| YOSHIFUJI Hideaki | 519fbd8 | 2006-03-20 17:00:05 -0800 | [diff] [blame] | 816 | if (nrt) { | 
| Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 817 | err = ip6_ins_rt(nrt); | 
| YOSHIFUJI Hideaki | 519fbd8 | 2006-03-20 17:00:05 -0800 | [diff] [blame] | 818 | if (!err) | 
|  | 819 | goto out2; | 
|  | 820 | } | 
|  | 821 |  | 
|  | 822 | if (--attempts <= 0) | 
|  | 823 | goto out2; | 
|  | 824 |  | 
|  | 825 | /* | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 826 | * Race condition! In the gap, when table->tb6_lock was | 
| YOSHIFUJI Hideaki | 519fbd8 | 2006-03-20 17:00:05 -0800 | [diff] [blame] | 827 | * released someone could insert this route.  Relookup. | 
|  | 828 | */ | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 829 | dst_release(&rt->dst); | 
| YOSHIFUJI Hideaki | 519fbd8 | 2006-03-20 17:00:05 -0800 | [diff] [blame] | 830 | goto relookup; | 
|  | 831 |  | 
|  | 832 | out: | 
| YOSHIFUJI Hideaki | 8238dd0 | 2006-03-20 17:04:35 -0800 | [diff] [blame] | 833 | if (reachable) { | 
|  | 834 | reachable = 0; | 
|  | 835 | goto restart_2; | 
|  | 836 | } | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 837 | dst_hold(&rt->dst); | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 838 | read_unlock_bh(&table->tb6_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | out2: | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 840 | rt->dst.lastuse = jiffies; | 
|  | 841 | rt->dst.__use++; | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 842 |  | 
|  | 843 | return rt; | 
|  | 844 | } | 
|  | 845 |  | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 846 | static struct rt6_info *ip6_pol_route_input(struct net *net, struct fib6_table *table, | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 847 | struct flowi6 *fl6, int flags) | 
| Pavel Emelyanov | 4acad72 | 2007-10-15 13:02:51 -0700 | [diff] [blame] | 848 | { | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 849 | return ip6_pol_route(net, table, fl6->flowi6_iif, fl6, flags); | 
| Pavel Emelyanov | 4acad72 | 2007-10-15 13:02:51 -0700 | [diff] [blame] | 850 | } | 
|  | 851 |  | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 852 | void ip6_route_input(struct sk_buff *skb) | 
|  | 853 | { | 
| Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 854 | const struct ipv6hdr *iph = ipv6_hdr(skb); | 
| YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 855 | struct net *net = dev_net(skb->dev); | 
| Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 856 | int flags = RT6_LOOKUP_F_HAS_SADDR; | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 857 | struct flowi6 fl6 = { | 
|  | 858 | .flowi6_iif = skb->dev->ifindex, | 
|  | 859 | .daddr = iph->daddr, | 
|  | 860 | .saddr = iph->saddr, | 
|  | 861 | .flowlabel = (* (__be32 *) iph)&IPV6_FLOWINFO_MASK, | 
|  | 862 | .flowi6_mark = skb->mark, | 
|  | 863 | .flowi6_proto = iph->nexthdr, | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 864 | }; | 
| Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 865 |  | 
| Thomas Goff | 1d6e55f | 2009-01-27 22:39:59 -0800 | [diff] [blame] | 866 | if (rt6_need_strict(&iph->daddr) && skb->dev->type != ARPHRD_PIMREG) | 
| Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 867 | flags |= RT6_LOOKUP_F_IFACE; | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 868 |  | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 869 | skb_dst_set(skb, fib6_rule_lookup(net, &fl6, flags, ip6_pol_route_input)); | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 870 | } | 
|  | 871 |  | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 872 | static struct rt6_info *ip6_pol_route_output(struct net *net, struct fib6_table *table, | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 873 | struct flowi6 *fl6, int flags) | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 874 | { | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 875 | return ip6_pol_route(net, table, fl6->flowi6_oif, fl6, flags); | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 876 | } | 
|  | 877 |  | 
| Florian Westphal | 9c7a4f9 | 2011-03-22 19:17:36 -0700 | [diff] [blame] | 878 | struct dst_entry * ip6_route_output(struct net *net, const struct sock *sk, | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 879 | struct flowi6 *fl6) | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 880 | { | 
|  | 881 | int flags = 0; | 
|  | 882 |  | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 883 | if ((sk && sk->sk_bound_dev_if) || rt6_need_strict(&fl6->daddr)) | 
| YOSHIFUJI Hideaki | 77d16f4 | 2006-08-23 17:25:05 -0700 | [diff] [blame] | 884 | flags |= RT6_LOOKUP_F_IFACE; | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 885 |  | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 886 | if (!ipv6_addr_any(&fl6->saddr)) | 
| Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 887 | flags |= RT6_LOOKUP_F_HAS_SADDR; | 
| YOSHIFUJI Hideaki / 吉藤英明 | 0c9a2ac | 2010-03-07 00:14:44 +0000 | [diff] [blame] | 888 | else if (sk) | 
|  | 889 | flags |= rt6_srcprefs2flags(inet6_sk(sk)->srcprefs); | 
| Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 890 |  | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 891 | return fib6_rule_lookup(net, fl6, flags, ip6_pol_route_output); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | } | 
|  | 893 |  | 
| YOSHIFUJI Hideaki | 7159039 | 2007-02-22 22:05:40 +0900 | [diff] [blame] | 894 | EXPORT_SYMBOL(ip6_route_output); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 |  | 
| David S. Miller | 2774c13 | 2011-03-01 14:59:04 -0800 | [diff] [blame] | 896 | struct dst_entry *ip6_blackhole_route(struct net *net, struct dst_entry *dst_orig) | 
| David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 897 | { | 
| David S. Miller | 5c1e6aa | 2011-04-28 14:13:38 -0700 | [diff] [blame] | 898 | struct rt6_info *rt, *ort = (struct rt6_info *) dst_orig; | 
| David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 899 | struct dst_entry *new = NULL; | 
|  | 900 |  | 
| David S. Miller | 5c1e6aa | 2011-04-28 14:13:38 -0700 | [diff] [blame] | 901 | rt = dst_alloc(&ip6_dst_blackhole_ops, ort->dst.dev, 1, 0, 0); | 
| David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 902 | if (rt) { | 
| David S. Miller | cf91166 | 2011-04-28 14:31:47 -0700 | [diff] [blame] | 903 | memset(&rt->rt6i_table, 0, sizeof(*rt) - sizeof(struct dst_entry)); | 
|  | 904 |  | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 905 | new = &rt->dst; | 
| David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 906 |  | 
| David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 907 | new->__use = 1; | 
| Herbert Xu | 352e512 | 2007-11-13 21:34:06 -0800 | [diff] [blame] | 908 | new->input = dst_discard; | 
|  | 909 | new->output = dst_discard; | 
| David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 910 |  | 
| David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 911 | dst_copy_metrics(new, &ort->dst); | 
| David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 912 | rt->rt6i_idev = ort->rt6i_idev; | 
|  | 913 | if (rt->rt6i_idev) | 
|  | 914 | in6_dev_hold(rt->rt6i_idev); | 
|  | 915 | rt->rt6i_expires = 0; | 
|  | 916 |  | 
|  | 917 | ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway); | 
|  | 918 | rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES; | 
|  | 919 | rt->rt6i_metric = 0; | 
|  | 920 |  | 
|  | 921 | memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); | 
|  | 922 | #ifdef CONFIG_IPV6_SUBTREES | 
|  | 923 | memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); | 
|  | 924 | #endif | 
|  | 925 |  | 
|  | 926 | dst_free(new); | 
|  | 927 | } | 
|  | 928 |  | 
| David S. Miller | 69ead7a | 2011-03-01 14:45:33 -0800 | [diff] [blame] | 929 | dst_release(dst_orig); | 
|  | 930 | return new ? new : ERR_PTR(-ENOMEM); | 
| David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 931 | } | 
| David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 932 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | /* | 
|  | 934 | *	Destination cache support functions | 
|  | 935 | */ | 
|  | 936 |  | 
|  | 937 | static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie) | 
|  | 938 | { | 
|  | 939 | struct rt6_info *rt; | 
|  | 940 |  | 
|  | 941 | rt = (struct rt6_info *) dst; | 
|  | 942 |  | 
| David S. Miller | 6431cbc | 2011-02-07 20:38:06 -0800 | [diff] [blame] | 943 | if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) { | 
|  | 944 | if (rt->rt6i_peer_genid != rt6_peer_genid()) { | 
|  | 945 | if (!rt->rt6i_peer) | 
|  | 946 | rt6_bind_peer(rt, 0); | 
|  | 947 | rt->rt6i_peer_genid = rt6_peer_genid(); | 
|  | 948 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | return dst; | 
| David S. Miller | 6431cbc | 2011-02-07 20:38:06 -0800 | [diff] [blame] | 950 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | return NULL; | 
|  | 952 | } | 
|  | 953 |  | 
|  | 954 | static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) | 
|  | 955 | { | 
|  | 956 | struct rt6_info *rt = (struct rt6_info *) dst; | 
|  | 957 |  | 
|  | 958 | if (rt) { | 
| YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 959 | if (rt->rt6i_flags & RTF_CACHE) { | 
|  | 960 | if (rt6_check_expired(rt)) { | 
|  | 961 | ip6_del_rt(rt); | 
|  | 962 | dst = NULL; | 
|  | 963 | } | 
|  | 964 | } else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | dst_release(dst); | 
| YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 966 | dst = NULL; | 
|  | 967 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | } | 
| YOSHIFUJI Hideaki / 吉藤英明 | 54c1a85 | 2010-03-28 07:15:45 +0000 | [diff] [blame] | 969 | return dst; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | } | 
|  | 971 |  | 
|  | 972 | static void ip6_link_failure(struct sk_buff *skb) | 
|  | 973 | { | 
|  | 974 | struct rt6_info *rt; | 
|  | 975 |  | 
| Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 976 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 |  | 
| Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 978 | rt = (struct rt6_info *) skb_dst(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | if (rt) { | 
|  | 980 | if (rt->rt6i_flags&RTF_CACHE) { | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 981 | dst_set_expires(&rt->dst, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | rt->rt6i_flags |= RTF_EXPIRES; | 
|  | 983 | } else if (rt->rt6i_node && (rt->rt6i_flags & RTF_DEFAULT)) | 
|  | 984 | rt->rt6i_node->fn_sernum = -1; | 
|  | 985 | } | 
|  | 986 | } | 
|  | 987 |  | 
|  | 988 | static void ip6_rt_update_pmtu(struct dst_entry *dst, u32 mtu) | 
|  | 989 | { | 
|  | 990 | struct rt6_info *rt6 = (struct rt6_info*)dst; | 
|  | 991 |  | 
|  | 992 | if (mtu < dst_mtu(dst) && rt6->rt6i_dst.plen == 128) { | 
|  | 993 | rt6->rt6i_flags |= RTF_MODIFIED; | 
|  | 994 | if (mtu < IPV6_MIN_MTU) { | 
| David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 995 | u32 features = dst_metric(dst, RTAX_FEATURES); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | mtu = IPV6_MIN_MTU; | 
| David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 997 | features |= RTAX_FEATURE_ALLFRAG; | 
|  | 998 | dst_metric_set(dst, RTAX_FEATURES, features); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | } | 
| David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 1000 | dst_metric_set(dst, RTAX_MTU, mtu); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | } | 
|  | 1002 | } | 
|  | 1003 |  | 
| David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 1004 | static unsigned int ip6_default_advmss(const struct dst_entry *dst) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | { | 
| David S. Miller | 0dbaee3 | 2010-12-13 12:52:14 -0800 | [diff] [blame] | 1006 | struct net_device *dev = dst->dev; | 
|  | 1007 | unsigned int mtu = dst_mtu(dst); | 
|  | 1008 | struct net *net = dev_net(dev); | 
|  | 1009 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | mtu -= sizeof(struct ipv6hdr) + sizeof(struct tcphdr); | 
|  | 1011 |  | 
| Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1012 | if (mtu < net->ipv6.sysctl.ip6_rt_min_advmss) | 
|  | 1013 | mtu = net->ipv6.sysctl.ip6_rt_min_advmss; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 |  | 
|  | 1015 | /* | 
| YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1016 | * Maximal non-jumbo IPv6 payload is IPV6_MAXPLEN and | 
|  | 1017 | * corresponding MSS is IPV6_MAXPLEN - tcp_header_size. | 
|  | 1018 | * IPV6_MAXPLEN is also valid and means: "any MSS, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | * rely only on pmtu discovery" | 
|  | 1020 | */ | 
|  | 1021 | if (mtu > IPV6_MAXPLEN - sizeof(struct tcphdr)) | 
|  | 1022 | mtu = IPV6_MAXPLEN; | 
|  | 1023 | return mtu; | 
|  | 1024 | } | 
|  | 1025 |  | 
| David S. Miller | d33e455 | 2010-12-14 13:01:14 -0800 | [diff] [blame] | 1026 | static unsigned int ip6_default_mtu(const struct dst_entry *dst) | 
|  | 1027 | { | 
|  | 1028 | unsigned int mtu = IPV6_MIN_MTU; | 
|  | 1029 | struct inet6_dev *idev; | 
|  | 1030 |  | 
|  | 1031 | rcu_read_lock(); | 
|  | 1032 | idev = __in6_dev_get(dst->dev); | 
|  | 1033 | if (idev) | 
|  | 1034 | mtu = idev->cnf.mtu6; | 
|  | 1035 | rcu_read_unlock(); | 
|  | 1036 |  | 
|  | 1037 | return mtu; | 
|  | 1038 | } | 
|  | 1039 |  | 
| YOSHIFUJI Hideaki | 3b00944 | 2007-12-06 16:11:48 -0800 | [diff] [blame] | 1040 | static struct dst_entry *icmp6_dst_gc_list; | 
|  | 1041 | static DEFINE_SPINLOCK(icmp6_dst_lock); | 
| Thomas Graf | 5d0bbee | 2006-08-04 03:37:36 -0700 | [diff] [blame] | 1042 |  | 
| YOSHIFUJI Hideaki | 3b00944 | 2007-12-06 16:11:48 -0800 | [diff] [blame] | 1043 | struct dst_entry *icmp6_dst_alloc(struct net_device *dev, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | struct neighbour *neigh, | 
| YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 1045 | const struct in6_addr *addr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | { | 
|  | 1047 | struct rt6_info *rt; | 
|  | 1048 | struct inet6_dev *idev = in6_dev_get(dev); | 
| YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1049 | struct net *net = dev_net(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 |  | 
|  | 1051 | if (unlikely(idev == NULL)) | 
|  | 1052 | return NULL; | 
|  | 1053 |  | 
| David S. Miller | 957c665 | 2011-06-24 15:25:00 -0700 | [diff] [blame] | 1054 | rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, dev, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | if (unlikely(rt == NULL)) { | 
|  | 1056 | in6_dev_put(idev); | 
|  | 1057 | goto out; | 
|  | 1058 | } | 
|  | 1059 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | if (neigh) | 
|  | 1061 | neigh_hold(neigh); | 
| David S. Miller | 14deae4 | 2009-01-04 16:04:39 -0800 | [diff] [blame] | 1062 | else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | neigh = ndisc_get_neigh(dev, addr); | 
| David S. Miller | 14deae4 | 2009-01-04 16:04:39 -0800 | [diff] [blame] | 1064 | if (IS_ERR(neigh)) | 
|  | 1065 | neigh = NULL; | 
|  | 1066 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | rt->rt6i_idev     = idev; | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 1069 | dst_set_neighbour(&rt->dst, neigh); | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1070 | atomic_set(&rt->dst.__refcnt, 1); | 
| David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 1071 | dst_metric_set(&rt->dst, RTAX_HOPLIMIT, 255); | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1072 | rt->dst.output  = ip6_output; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 |  | 
| YOSHIFUJI Hideaki | 3b00944 | 2007-12-06 16:11:48 -0800 | [diff] [blame] | 1074 | spin_lock_bh(&icmp6_dst_lock); | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1075 | rt->dst.next = icmp6_dst_gc_list; | 
|  | 1076 | icmp6_dst_gc_list = &rt->dst; | 
| YOSHIFUJI Hideaki | 3b00944 | 2007-12-06 16:11:48 -0800 | [diff] [blame] | 1077 | spin_unlock_bh(&icmp6_dst_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 |  | 
| Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1079 | fib6_force_start_gc(net); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 |  | 
|  | 1081 | out: | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1082 | return &rt->dst; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | } | 
|  | 1084 |  | 
| Stephen Hemminger | 3d0f24a | 2008-07-22 14:35:50 -0700 | [diff] [blame] | 1085 | int icmp6_dst_gc(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | { | 
| Hagen Paul Pfeifer | e9476e9 | 2011-02-25 05:45:19 +0000 | [diff] [blame] | 1087 | struct dst_entry *dst, **pprev; | 
| Stephen Hemminger | 3d0f24a | 2008-07-22 14:35:50 -0700 | [diff] [blame] | 1088 | int more = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 |  | 
| YOSHIFUJI Hideaki | 3b00944 | 2007-12-06 16:11:48 -0800 | [diff] [blame] | 1090 | spin_lock_bh(&icmp6_dst_lock); | 
|  | 1091 | pprev = &icmp6_dst_gc_list; | 
| Thomas Graf | 5d0bbee | 2006-08-04 03:37:36 -0700 | [diff] [blame] | 1092 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | while ((dst = *pprev) != NULL) { | 
|  | 1094 | if (!atomic_read(&dst->__refcnt)) { | 
|  | 1095 | *pprev = dst->next; | 
|  | 1096 | dst_free(dst); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | } else { | 
|  | 1098 | pprev = &dst->next; | 
| Stephen Hemminger | 3d0f24a | 2008-07-22 14:35:50 -0700 | [diff] [blame] | 1099 | ++more; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | } | 
|  | 1101 | } | 
|  | 1102 |  | 
| YOSHIFUJI Hideaki | 3b00944 | 2007-12-06 16:11:48 -0800 | [diff] [blame] | 1103 | spin_unlock_bh(&icmp6_dst_lock); | 
| Thomas Graf | 5d0bbee | 2006-08-04 03:37:36 -0700 | [diff] [blame] | 1104 |  | 
| Stephen Hemminger | 3d0f24a | 2008-07-22 14:35:50 -0700 | [diff] [blame] | 1105 | return more; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | } | 
|  | 1107 |  | 
| David S. Miller | 1e493d1 | 2008-09-10 17:27:15 -0700 | [diff] [blame] | 1108 | static void icmp6_clean_all(int (*func)(struct rt6_info *rt, void *arg), | 
|  | 1109 | void *arg) | 
|  | 1110 | { | 
|  | 1111 | struct dst_entry *dst, **pprev; | 
|  | 1112 |  | 
|  | 1113 | spin_lock_bh(&icmp6_dst_lock); | 
|  | 1114 | pprev = &icmp6_dst_gc_list; | 
|  | 1115 | while ((dst = *pprev) != NULL) { | 
|  | 1116 | struct rt6_info *rt = (struct rt6_info *) dst; | 
|  | 1117 | if (func(rt, arg)) { | 
|  | 1118 | *pprev = dst->next; | 
|  | 1119 | dst_free(dst); | 
|  | 1120 | } else { | 
|  | 1121 | pprev = &dst->next; | 
|  | 1122 | } | 
|  | 1123 | } | 
|  | 1124 | spin_unlock_bh(&icmp6_dst_lock); | 
|  | 1125 | } | 
|  | 1126 |  | 
| Daniel Lezcano | 569d364 | 2008-01-18 03:56:57 -0800 | [diff] [blame] | 1127 | static int ip6_dst_gc(struct dst_ops *ops) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | unsigned long now = jiffies; | 
| Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 1130 | struct net *net = container_of(ops, struct net, ipv6.ip6_dst_ops); | 
| Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 1131 | int rt_min_interval = net->ipv6.sysctl.ip6_rt_gc_min_interval; | 
|  | 1132 | int rt_max_size = net->ipv6.sysctl.ip6_rt_max_size; | 
|  | 1133 | int rt_elasticity = net->ipv6.sysctl.ip6_rt_gc_elasticity; | 
|  | 1134 | int rt_gc_timeout = net->ipv6.sysctl.ip6_rt_gc_timeout; | 
|  | 1135 | unsigned long rt_last_gc = net->ipv6.ip6_rt_last_gc; | 
| Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 1136 | int entries; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 |  | 
| Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 1138 | entries = dst_entries_get_fast(ops); | 
| Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 1139 | if (time_after(rt_last_gc + rt_min_interval, now) && | 
| Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 1140 | entries <= rt_max_size) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | goto out; | 
|  | 1142 |  | 
| Benjamin Thery | 6891a34 | 2008-03-04 13:49:47 -0800 | [diff] [blame] | 1143 | net->ipv6.ip6_rt_gc_expire++; | 
|  | 1144 | fib6_run_gc(net->ipv6.ip6_rt_gc_expire, net); | 
|  | 1145 | net->ipv6.ip6_rt_last_gc = now; | 
| Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 1146 | entries = dst_entries_get_slow(ops); | 
|  | 1147 | if (entries < ops->gc_thresh) | 
| Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 1148 | net->ipv6.ip6_rt_gc_expire = rt_gc_timeout>>1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | out: | 
| Daniel Lezcano | 7019b78 | 2008-03-04 13:50:14 -0800 | [diff] [blame] | 1150 | net->ipv6.ip6_rt_gc_expire -= net->ipv6.ip6_rt_gc_expire>>rt_elasticity; | 
| Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 1151 | return entries > rt_max_size; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | } | 
|  | 1153 |  | 
|  | 1154 | /* Clean host part of a prefix. Not necessary in radix tree, | 
|  | 1155 | but results in cleaner routing tables. | 
|  | 1156 |  | 
|  | 1157 | Remove it only when all the things will work! | 
|  | 1158 | */ | 
|  | 1159 |  | 
| YOSHIFUJI Hideaki | 6b75d09 | 2008-03-10 06:00:30 -0400 | [diff] [blame] | 1160 | int ip6_dst_hoplimit(struct dst_entry *dst) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | { | 
| David S. Miller | 5170ae8 | 2010-12-12 21:35:57 -0800 | [diff] [blame] | 1162 | int hoplimit = dst_metric_raw(dst, RTAX_HOPLIMIT); | 
| David S. Miller | a02e4b7 | 2010-12-12 21:39:02 -0800 | [diff] [blame] | 1163 | if (hoplimit == 0) { | 
| YOSHIFUJI Hideaki | 6b75d09 | 2008-03-10 06:00:30 -0400 | [diff] [blame] | 1164 | struct net_device *dev = dst->dev; | 
| Eric Dumazet | c68f24c | 2010-06-14 04:46:20 +0000 | [diff] [blame] | 1165 | struct inet6_dev *idev; | 
|  | 1166 |  | 
|  | 1167 | rcu_read_lock(); | 
|  | 1168 | idev = __in6_dev_get(dev); | 
|  | 1169 | if (idev) | 
| YOSHIFUJI Hideaki | 6b75d09 | 2008-03-10 06:00:30 -0400 | [diff] [blame] | 1170 | hoplimit = idev->cnf.hop_limit; | 
| Eric Dumazet | c68f24c | 2010-06-14 04:46:20 +0000 | [diff] [blame] | 1171 | else | 
| YOSHIFUJI Hideaki | 53b7997 | 2008-07-19 22:35:03 -0700 | [diff] [blame] | 1172 | hoplimit = dev_net(dev)->ipv6.devconf_all->hop_limit; | 
| Eric Dumazet | c68f24c | 2010-06-14 04:46:20 +0000 | [diff] [blame] | 1173 | rcu_read_unlock(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | } | 
|  | 1175 | return hoplimit; | 
|  | 1176 | } | 
| David S. Miller | abbf46a | 2010-12-12 21:14:46 -0800 | [diff] [blame] | 1177 | EXPORT_SYMBOL(ip6_dst_hoplimit); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 |  | 
|  | 1179 | /* | 
|  | 1180 | * | 
|  | 1181 | */ | 
|  | 1182 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1183 | int ip6_route_add(struct fib6_config *cfg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | { | 
|  | 1185 | int err; | 
| Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1186 | struct net *net = cfg->fc_nlinfo.nl_net; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | struct rt6_info *rt = NULL; | 
|  | 1188 | struct net_device *dev = NULL; | 
|  | 1189 | struct inet6_dev *idev = NULL; | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1190 | struct fib6_table *table; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | int addr_type; | 
|  | 1192 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1193 | if (cfg->fc_dst_len > 128 || cfg->fc_src_len > 128) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | return -EINVAL; | 
|  | 1195 | #ifndef CONFIG_IPV6_SUBTREES | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1196 | if (cfg->fc_src_len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | return -EINVAL; | 
|  | 1198 | #endif | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1199 | if (cfg->fc_ifindex) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | err = -ENODEV; | 
| Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1201 | dev = dev_get_by_index(net, cfg->fc_ifindex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | if (!dev) | 
|  | 1203 | goto out; | 
|  | 1204 | idev = in6_dev_get(dev); | 
|  | 1205 | if (!idev) | 
|  | 1206 | goto out; | 
|  | 1207 | } | 
|  | 1208 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1209 | if (cfg->fc_metric == 0) | 
|  | 1210 | cfg->fc_metric = IP6_RT_PRIO_USER; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 |  | 
| Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1212 | table = fib6_new_table(net, cfg->fc_table); | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1213 | if (table == NULL) { | 
|  | 1214 | err = -ENOBUFS; | 
|  | 1215 | goto out; | 
|  | 1216 | } | 
|  | 1217 |  | 
| David S. Miller | 957c665 | 2011-06-24 15:25:00 -0700 | [diff] [blame] | 1218 | rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, NULL, DST_NOCOUNT); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 |  | 
|  | 1220 | if (rt == NULL) { | 
|  | 1221 | err = -ENOMEM; | 
|  | 1222 | goto out; | 
|  | 1223 | } | 
|  | 1224 |  | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1225 | rt->dst.obsolete = -1; | 
| YOSHIFUJI Hideaki | 6f70499 | 2008-05-19 16:56:11 -0700 | [diff] [blame] | 1226 | rt->rt6i_expires = (cfg->fc_flags & RTF_EXPIRES) ? | 
|  | 1227 | jiffies + clock_t_to_jiffies(cfg->fc_expires) : | 
|  | 1228 | 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1230 | if (cfg->fc_protocol == RTPROT_UNSPEC) | 
|  | 1231 | cfg->fc_protocol = RTPROT_BOOT; | 
|  | 1232 | rt->rt6i_protocol = cfg->fc_protocol; | 
|  | 1233 |  | 
|  | 1234 | addr_type = ipv6_addr_type(&cfg->fc_dst); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 |  | 
|  | 1236 | if (addr_type & IPV6_ADDR_MULTICAST) | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1237 | rt->dst.input = ip6_mc_input; | 
| Maciej Żenczykowski | ab79ad1 | 2010-09-27 00:07:02 +0000 | [diff] [blame] | 1238 | else if (cfg->fc_flags & RTF_LOCAL) | 
|  | 1239 | rt->dst.input = ip6_input; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | else | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1241 | rt->dst.input = ip6_forward; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 |  | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1243 | rt->dst.output = ip6_output; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1245 | ipv6_addr_prefix(&rt->rt6i_dst.addr, &cfg->fc_dst, cfg->fc_dst_len); | 
|  | 1246 | rt->rt6i_dst.plen = cfg->fc_dst_len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1247 | if (rt->rt6i_dst.plen == 128) | 
| David S. Miller | 11d53b4 | 2011-06-24 15:23:34 -0700 | [diff] [blame] | 1248 | rt->dst.flags |= DST_HOST; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 |  | 
|  | 1250 | #ifdef CONFIG_IPV6_SUBTREES | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1251 | ipv6_addr_prefix(&rt->rt6i_src.addr, &cfg->fc_src, cfg->fc_src_len); | 
|  | 1252 | rt->rt6i_src.plen = cfg->fc_src_len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | #endif | 
|  | 1254 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1255 | rt->rt6i_metric = cfg->fc_metric; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 |  | 
|  | 1257 | /* We cannot add true routes via loopback here, | 
|  | 1258 | they would result in kernel looping; promote them to reject routes | 
|  | 1259 | */ | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1260 | if ((cfg->fc_flags & RTF_REJECT) || | 
| Maciej Żenczykowski | ab79ad1 | 2010-09-27 00:07:02 +0000 | [diff] [blame] | 1261 | (dev && (dev->flags&IFF_LOOPBACK) && !(addr_type&IPV6_ADDR_LOOPBACK) | 
|  | 1262 | && !(cfg->fc_flags&RTF_LOCAL))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | /* hold loopback dev/idev if we haven't done so. */ | 
| Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1264 | if (dev != net->loopback_dev) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | if (dev) { | 
|  | 1266 | dev_put(dev); | 
|  | 1267 | in6_dev_put(idev); | 
|  | 1268 | } | 
| Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1269 | dev = net->loopback_dev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | dev_hold(dev); | 
|  | 1271 | idev = in6_dev_get(dev); | 
|  | 1272 | if (!idev) { | 
|  | 1273 | err = -ENODEV; | 
|  | 1274 | goto out; | 
|  | 1275 | } | 
|  | 1276 | } | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1277 | rt->dst.output = ip6_pkt_discard_out; | 
|  | 1278 | rt->dst.input = ip6_pkt_discard; | 
|  | 1279 | rt->dst.error = -ENETUNREACH; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; | 
|  | 1281 | goto install_route; | 
|  | 1282 | } | 
|  | 1283 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1284 | if (cfg->fc_flags & RTF_GATEWAY) { | 
| Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1285 | const struct in6_addr *gw_addr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | int gwa_type; | 
|  | 1287 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1288 | gw_addr = &cfg->fc_gateway; | 
|  | 1289 | ipv6_addr_copy(&rt->rt6i_gateway, gw_addr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | gwa_type = ipv6_addr_type(gw_addr); | 
|  | 1291 |  | 
|  | 1292 | if (gwa_type != (IPV6_ADDR_LINKLOCAL|IPV6_ADDR_UNICAST)) { | 
|  | 1293 | struct rt6_info *grt; | 
|  | 1294 |  | 
|  | 1295 | /* IPv6 strictly inhibits using not link-local | 
|  | 1296 | addresses as nexthop address. | 
|  | 1297 | Otherwise, router will not able to send redirects. | 
|  | 1298 | It is very good, but in some (rare!) circumstances | 
|  | 1299 | (SIT, PtP, NBMA NOARP links) it is handy to allow | 
|  | 1300 | some exceptions. --ANK | 
|  | 1301 | */ | 
|  | 1302 | err = -EINVAL; | 
|  | 1303 | if (!(gwa_type&IPV6_ADDR_UNICAST)) | 
|  | 1304 | goto out; | 
|  | 1305 |  | 
| Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1306 | grt = rt6_lookup(net, gw_addr, NULL, cfg->fc_ifindex, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 |  | 
|  | 1308 | err = -EHOSTUNREACH; | 
|  | 1309 | if (grt == NULL) | 
|  | 1310 | goto out; | 
|  | 1311 | if (dev) { | 
|  | 1312 | if (dev != grt->rt6i_dev) { | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1313 | dst_release(&grt->dst); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | goto out; | 
|  | 1315 | } | 
|  | 1316 | } else { | 
|  | 1317 | dev = grt->rt6i_dev; | 
|  | 1318 | idev = grt->rt6i_idev; | 
|  | 1319 | dev_hold(dev); | 
|  | 1320 | in6_dev_hold(grt->rt6i_idev); | 
|  | 1321 | } | 
|  | 1322 | if (!(grt->rt6i_flags&RTF_GATEWAY)) | 
|  | 1323 | err = 0; | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1324 | dst_release(&grt->dst); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 |  | 
|  | 1326 | if (err) | 
|  | 1327 | goto out; | 
|  | 1328 | } | 
|  | 1329 | err = -EINVAL; | 
|  | 1330 | if (dev == NULL || (dev->flags&IFF_LOOPBACK)) | 
|  | 1331 | goto out; | 
|  | 1332 | } | 
|  | 1333 |  | 
|  | 1334 | err = -ENODEV; | 
|  | 1335 | if (dev == NULL) | 
|  | 1336 | goto out; | 
|  | 1337 |  | 
| Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 1338 | if (!ipv6_addr_any(&cfg->fc_prefsrc)) { | 
|  | 1339 | if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) { | 
|  | 1340 | err = -EINVAL; | 
|  | 1341 | goto out; | 
|  | 1342 | } | 
|  | 1343 | ipv6_addr_copy(&rt->rt6i_prefsrc.addr, &cfg->fc_prefsrc); | 
|  | 1344 | rt->rt6i_prefsrc.plen = 128; | 
|  | 1345 | } else | 
|  | 1346 | rt->rt6i_prefsrc.plen = 0; | 
|  | 1347 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1348 | if (cfg->fc_flags & (RTF_GATEWAY | RTF_NONEXTHOP)) { | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 1349 | struct neighbour *neigh = __neigh_lookup_errno(&nd_tbl, &rt->rt6i_gateway, dev); | 
|  | 1350 | if (IS_ERR(neigh)) { | 
|  | 1351 | err = PTR_ERR(neigh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | goto out; | 
|  | 1353 | } | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 1354 | dst_set_neighbour(&rt->dst, neigh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | } | 
|  | 1356 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1357 | rt->rt6i_flags = cfg->fc_flags; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 |  | 
|  | 1359 | install_route: | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1360 | if (cfg->fc_mx) { | 
|  | 1361 | struct nlattr *nla; | 
|  | 1362 | int remaining; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1364 | nla_for_each_attr(nla, cfg->fc_mx, cfg->fc_mx_len, remaining) { | 
| Thomas Graf | 8f4c1f9 | 2007-09-12 14:44:36 +0200 | [diff] [blame] | 1365 | int type = nla_type(nla); | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1366 |  | 
|  | 1367 | if (type) { | 
|  | 1368 | if (type > RTAX_MAX) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | err = -EINVAL; | 
|  | 1370 | goto out; | 
|  | 1371 | } | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1372 |  | 
| David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 1373 | dst_metric_set(&rt->dst, type, nla_get_u32(nla)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | } | 
|  | 1376 | } | 
|  | 1377 |  | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1378 | rt->dst.dev = dev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | rt->rt6i_idev = idev; | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1380 | rt->rt6i_table = table; | 
| Daniel Lezcano | 63152fc | 2008-03-03 23:31:11 -0800 | [diff] [blame] | 1381 |  | 
| YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1382 | cfg->fc_nlinfo.nl_net = dev_net(dev); | 
| Daniel Lezcano | 63152fc | 2008-03-03 23:31:11 -0800 | [diff] [blame] | 1383 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1384 | return __ip6_ins_rt(rt, &cfg->fc_nlinfo); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 |  | 
|  | 1386 | out: | 
|  | 1387 | if (dev) | 
|  | 1388 | dev_put(dev); | 
|  | 1389 | if (idev) | 
|  | 1390 | in6_dev_put(idev); | 
|  | 1391 | if (rt) | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1392 | dst_free(&rt->dst); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | return err; | 
|  | 1394 | } | 
|  | 1395 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1396 | static int __ip6_del_rt(struct rt6_info *rt, struct nl_info *info) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | { | 
|  | 1398 | int err; | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1399 | struct fib6_table *table; | 
| YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1400 | struct net *net = dev_net(rt->rt6i_dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 |  | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1402 | if (rt == net->ipv6.ip6_null_entry) | 
| Patrick McHardy | 6c813a7 | 2006-08-06 22:22:47 -0700 | [diff] [blame] | 1403 | return -ENOENT; | 
|  | 1404 |  | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1405 | table = rt->rt6i_table; | 
|  | 1406 | write_lock_bh(&table->tb6_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1408 | err = fib6_del(rt, info); | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1409 | dst_release(&rt->dst); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 |  | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1411 | write_unlock_bh(&table->tb6_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 |  | 
|  | 1413 | return err; | 
|  | 1414 | } | 
|  | 1415 |  | 
| Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 1416 | int ip6_del_rt(struct rt6_info *rt) | 
|  | 1417 | { | 
| Denis V. Lunev | 4d1169c | 2008-01-10 03:26:13 -0800 | [diff] [blame] | 1418 | struct nl_info info = { | 
| YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1419 | .nl_net = dev_net(rt->rt6i_dev), | 
| Denis V. Lunev | 4d1169c | 2008-01-10 03:26:13 -0800 | [diff] [blame] | 1420 | }; | 
| Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 1421 | return __ip6_del_rt(rt, &info); | 
| Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 1422 | } | 
|  | 1423 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1424 | static int ip6_route_del(struct fib6_config *cfg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | { | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1426 | struct fib6_table *table; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | struct fib6_node *fn; | 
|  | 1428 | struct rt6_info *rt; | 
|  | 1429 | int err = -ESRCH; | 
|  | 1430 |  | 
| Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1431 | table = fib6_get_table(cfg->fc_nlinfo.nl_net, cfg->fc_table); | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1432 | if (table == NULL) | 
|  | 1433 | return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 |  | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1435 | read_lock_bh(&table->tb6_lock); | 
|  | 1436 |  | 
|  | 1437 | fn = fib6_locate(&table->tb6_root, | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1438 | &cfg->fc_dst, cfg->fc_dst_len, | 
|  | 1439 | &cfg->fc_src, cfg->fc_src_len); | 
| YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1440 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1441 | if (fn) { | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1442 | for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1443 | if (cfg->fc_ifindex && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | (rt->rt6i_dev == NULL || | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1445 | rt->rt6i_dev->ifindex != cfg->fc_ifindex)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | continue; | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1447 | if (cfg->fc_flags & RTF_GATEWAY && | 
|  | 1448 | !ipv6_addr_equal(&cfg->fc_gateway, &rt->rt6i_gateway)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | continue; | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1450 | if (cfg->fc_metric && cfg->fc_metric != rt->rt6i_metric) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | continue; | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1452 | dst_hold(&rt->dst); | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1453 | read_unlock_bh(&table->tb6_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1455 | return __ip6_del_rt(rt, &cfg->fc_nlinfo); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | } | 
|  | 1457 | } | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1458 | read_unlock_bh(&table->tb6_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 |  | 
|  | 1460 | return err; | 
|  | 1461 | } | 
|  | 1462 |  | 
|  | 1463 | /* | 
|  | 1464 | *	Handle redirects | 
|  | 1465 | */ | 
| YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1466 | struct ip6rd_flowi { | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1467 | struct flowi6 fl6; | 
| YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1468 | struct in6_addr gateway; | 
|  | 1469 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 |  | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1471 | static struct rt6_info *__ip6_route_redirect(struct net *net, | 
|  | 1472 | struct fib6_table *table, | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1473 | struct flowi6 *fl6, | 
| YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1474 | int flags) | 
|  | 1475 | { | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1476 | struct ip6rd_flowi *rdfl = (struct ip6rd_flowi *)fl6; | 
| YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1477 | struct rt6_info *rt; | 
|  | 1478 | struct fib6_node *fn; | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1479 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | /* | 
| YOSHIFUJI Hideaki | e843b9e | 2006-03-20 17:07:49 -0800 | [diff] [blame] | 1481 | * Get the "current" route for this destination and | 
|  | 1482 | * check if the redirect has come from approriate router. | 
|  | 1483 | * | 
|  | 1484 | * RFC 2461 specifies that redirects should only be | 
|  | 1485 | * accepted if they come from the nexthop to the target. | 
|  | 1486 | * Due to the way the routes are chosen, this notion | 
|  | 1487 | * is a bit fuzzy and one might need to check all possible | 
|  | 1488 | * routes. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 |  | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1491 | read_lock_bh(&table->tb6_lock); | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1492 | fn = fib6_lookup(&table->tb6_root, &fl6->daddr, &fl6->saddr); | 
| YOSHIFUJI Hideaki | e843b9e | 2006-03-20 17:07:49 -0800 | [diff] [blame] | 1493 | restart: | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1494 | for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { | 
| YOSHIFUJI Hideaki | e843b9e | 2006-03-20 17:07:49 -0800 | [diff] [blame] | 1495 | /* | 
|  | 1496 | * Current route is on-link; redirect is always invalid. | 
|  | 1497 | * | 
|  | 1498 | * Seems, previous statement is not true. It could | 
|  | 1499 | * be node, which looks for us as on-link (f.e. proxy ndisc) | 
|  | 1500 | * But then router serving it might decide, that we should | 
|  | 1501 | * know truth 8)8) --ANK (980726). | 
|  | 1502 | */ | 
|  | 1503 | if (rt6_check_expired(rt)) | 
|  | 1504 | continue; | 
|  | 1505 | if (!(rt->rt6i_flags & RTF_GATEWAY)) | 
|  | 1506 | continue; | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1507 | if (fl6->flowi6_oif != rt->rt6i_dev->ifindex) | 
| YOSHIFUJI Hideaki | e843b9e | 2006-03-20 17:07:49 -0800 | [diff] [blame] | 1508 | continue; | 
| YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1509 | if (!ipv6_addr_equal(&rdfl->gateway, &rt->rt6i_gateway)) | 
| YOSHIFUJI Hideaki | e843b9e | 2006-03-20 17:07:49 -0800 | [diff] [blame] | 1510 | continue; | 
|  | 1511 | break; | 
|  | 1512 | } | 
| YOSHIFUJI Hideaki | e843b9e | 2006-03-20 17:07:49 -0800 | [diff] [blame] | 1513 |  | 
| YOSHIFUJI Hideaki | cb15d9c | 2006-08-23 17:23:11 -0700 | [diff] [blame] | 1514 | if (!rt) | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1515 | rt = net->ipv6.ip6_null_entry; | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1516 | BACKTRACK(net, &fl6->saddr); | 
| YOSHIFUJI Hideaki | cb15d9c | 2006-08-23 17:23:11 -0700 | [diff] [blame] | 1517 | out: | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1518 | dst_hold(&rt->dst); | 
| YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1519 |  | 
|  | 1520 | read_unlock_bh(&table->tb6_lock); | 
|  | 1521 |  | 
|  | 1522 | return rt; | 
|  | 1523 | }; | 
|  | 1524 |  | 
| Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1525 | static struct rt6_info *ip6_route_redirect(const struct in6_addr *dest, | 
|  | 1526 | const struct in6_addr *src, | 
|  | 1527 | const struct in6_addr *gateway, | 
| YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1528 | struct net_device *dev) | 
|  | 1529 | { | 
| Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 1530 | int flags = RT6_LOOKUP_F_HAS_SADDR; | 
| YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1531 | struct net *net = dev_net(dev); | 
| YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1532 | struct ip6rd_flowi rdfl = { | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1533 | .fl6 = { | 
|  | 1534 | .flowi6_oif = dev->ifindex, | 
|  | 1535 | .daddr = *dest, | 
|  | 1536 | .saddr = *src, | 
| YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1537 | }, | 
| YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1538 | }; | 
| Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 1539 |  | 
| Brian Haley | 86c36ce | 2009-10-07 13:58:01 -0700 | [diff] [blame] | 1540 | ipv6_addr_copy(&rdfl.gateway, gateway); | 
|  | 1541 |  | 
| Thomas Graf | adaa70b | 2006-10-13 15:01:03 -0700 | [diff] [blame] | 1542 | if (rt6_need_strict(dest)) | 
|  | 1543 | flags |= RT6_LOOKUP_F_IFACE; | 
| YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1544 |  | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1545 | return (struct rt6_info *)fib6_rule_lookup(net, &rdfl.fl6, | 
| Daniel Lezcano | 58f09b7 | 2008-03-03 23:25:27 -0800 | [diff] [blame] | 1546 | flags, __ip6_route_redirect); | 
| YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1547 | } | 
|  | 1548 |  | 
| Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1549 | void rt6_redirect(const struct in6_addr *dest, const struct in6_addr *src, | 
|  | 1550 | const struct in6_addr *saddr, | 
| YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1551 | struct neighbour *neigh, u8 *lladdr, int on_link) | 
|  | 1552 | { | 
|  | 1553 | struct rt6_info *rt, *nrt = NULL; | 
|  | 1554 | struct netevent_redirect netevent; | 
| YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1555 | struct net *net = dev_net(neigh->dev); | 
| YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1556 |  | 
|  | 1557 | rt = ip6_route_redirect(dest, src, saddr, neigh->dev); | 
|  | 1558 |  | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 1559 | if (rt == net->ipv6.ip6_null_entry) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | if (net_ratelimit()) | 
|  | 1561 | printk(KERN_DEBUG "rt6_redirect: source isn't a valid nexthop " | 
|  | 1562 | "for redirect target\n"); | 
| YOSHIFUJI Hideaki | a627945 | 2006-08-23 17:18:26 -0700 | [diff] [blame] | 1563 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | } | 
|  | 1565 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | /* | 
|  | 1567 | *	We have finally decided to accept it. | 
|  | 1568 | */ | 
|  | 1569 |  | 
| YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1570 | neigh_update(neigh, lladdr, NUD_STALE, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | NEIGH_UPDATE_F_WEAK_OVERRIDE| | 
|  | 1572 | NEIGH_UPDATE_F_OVERRIDE| | 
|  | 1573 | (on_link ? 0 : (NEIGH_UPDATE_F_OVERRIDE_ISROUTER| | 
|  | 1574 | NEIGH_UPDATE_F_ISROUTER)) | 
|  | 1575 | ); | 
|  | 1576 |  | 
|  | 1577 | /* | 
|  | 1578 | * Redirect received -> path was valid. | 
|  | 1579 | * Look, redirects are sent only in response to data packets, | 
|  | 1580 | * so that this nexthop apparently is reachable. --ANK | 
|  | 1581 | */ | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1582 | dst_confirm(&rt->dst); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 |  | 
|  | 1584 | /* Duplicate redirect: silently ignore. */ | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 1585 | if (neigh == dst_get_neighbour_raw(&rt->dst)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | goto out; | 
|  | 1587 |  | 
|  | 1588 | nrt = ip6_rt_copy(rt); | 
|  | 1589 | if (nrt == NULL) | 
|  | 1590 | goto out; | 
|  | 1591 |  | 
|  | 1592 | nrt->rt6i_flags = RTF_GATEWAY|RTF_UP|RTF_DYNAMIC|RTF_CACHE; | 
|  | 1593 | if (on_link) | 
|  | 1594 | nrt->rt6i_flags &= ~RTF_GATEWAY; | 
|  | 1595 |  | 
|  | 1596 | ipv6_addr_copy(&nrt->rt6i_dst.addr, dest); | 
|  | 1597 | nrt->rt6i_dst.plen = 128; | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1598 | nrt->dst.flags |= DST_HOST; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 |  | 
|  | 1600 | ipv6_addr_copy(&nrt->rt6i_gateway, (struct in6_addr*)neigh->primary_key); | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 1601 | dst_set_neighbour(&nrt->dst, neigh_clone(neigh)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 |  | 
| Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 1603 | if (ip6_ins_rt(nrt)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | goto out; | 
|  | 1605 |  | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1606 | netevent.old = &rt->dst; | 
|  | 1607 | netevent.new = &nrt->dst; | 
| Tom Tucker | 8d71740 | 2006-07-30 20:43:36 -0700 | [diff] [blame] | 1608 | call_netevent_notifiers(NETEVENT_REDIRECT, &netevent); | 
|  | 1609 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | if (rt->rt6i_flags&RTF_CACHE) { | 
| Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 1611 | ip6_del_rt(rt); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | return; | 
|  | 1613 | } | 
|  | 1614 |  | 
|  | 1615 | out: | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1616 | dst_release(&rt->dst); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | } | 
|  | 1618 |  | 
|  | 1619 | /* | 
|  | 1620 | *	Handle ICMP "packet too big" messages | 
|  | 1621 | *	i.e. Path MTU discovery | 
|  | 1622 | */ | 
|  | 1623 |  | 
| Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1624 | static void rt6_do_pmtu_disc(const struct in6_addr *daddr, const struct in6_addr *saddr, | 
| Maciej Żenczykowski | ae878ae | 2010-10-03 14:49:00 -0700 | [diff] [blame] | 1625 | struct net *net, u32 pmtu, int ifindex) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | { | 
|  | 1627 | struct rt6_info *rt, *nrt; | 
|  | 1628 | int allfrag = 0; | 
| Andrey Vagin | d3052b5 | 2010-12-11 15:20:11 +0000 | [diff] [blame] | 1629 | again: | 
| Maciej Żenczykowski | ae878ae | 2010-10-03 14:49:00 -0700 | [diff] [blame] | 1630 | rt = rt6_lookup(net, daddr, saddr, ifindex, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | if (rt == NULL) | 
|  | 1632 | return; | 
|  | 1633 |  | 
| Andrey Vagin | d3052b5 | 2010-12-11 15:20:11 +0000 | [diff] [blame] | 1634 | if (rt6_check_expired(rt)) { | 
|  | 1635 | ip6_del_rt(rt); | 
|  | 1636 | goto again; | 
|  | 1637 | } | 
|  | 1638 |  | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1639 | if (pmtu >= dst_mtu(&rt->dst)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | goto out; | 
|  | 1641 |  | 
|  | 1642 | if (pmtu < IPV6_MIN_MTU) { | 
|  | 1643 | /* | 
| YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1644 | * According to RFC2460, PMTU is set to the IPv6 Minimum Link | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | * MTU (1280) and a fragment header should always be included | 
|  | 1646 | * after a node receiving Too Big message reporting PMTU is | 
|  | 1647 | * less than the IPv6 Minimum Link MTU. | 
|  | 1648 | */ | 
|  | 1649 | pmtu = IPV6_MIN_MTU; | 
|  | 1650 | allfrag = 1; | 
|  | 1651 | } | 
|  | 1652 |  | 
|  | 1653 | /* New mtu received -> path was valid. | 
|  | 1654 | They are sent only in response to data packets, | 
|  | 1655 | so that this nexthop apparently is reachable. --ANK | 
|  | 1656 | */ | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1657 | dst_confirm(&rt->dst); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 |  | 
|  | 1659 | /* Host route. If it is static, it would be better | 
|  | 1660 | not to override it, but add new one, so that | 
|  | 1661 | when cache entry will expire old pmtu | 
|  | 1662 | would return automatically. | 
|  | 1663 | */ | 
|  | 1664 | if (rt->rt6i_flags & RTF_CACHE) { | 
| David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 1665 | dst_metric_set(&rt->dst, RTAX_MTU, pmtu); | 
|  | 1666 | if (allfrag) { | 
|  | 1667 | u32 features = dst_metric(&rt->dst, RTAX_FEATURES); | 
|  | 1668 | features |= RTAX_FEATURE_ALLFRAG; | 
|  | 1669 | dst_metric_set(&rt->dst, RTAX_FEATURES, features); | 
|  | 1670 | } | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1671 | dst_set_expires(&rt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | rt->rt6i_flags |= RTF_MODIFIED|RTF_EXPIRES; | 
|  | 1673 | goto out; | 
|  | 1674 | } | 
|  | 1675 |  | 
|  | 1676 | /* Network route. | 
|  | 1677 | Two cases are possible: | 
|  | 1678 | 1. It is connected route. Action: COW | 
|  | 1679 | 2. It is gatewayed route or NONEXTHOP route. Action: clone it. | 
|  | 1680 | */ | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 1681 | if (!dst_get_neighbour_raw(&rt->dst) && !(rt->rt6i_flags & RTF_NONEXTHOP)) | 
| YOSHIFUJI Hideaki | a1e7836 | 2006-03-20 16:56:32 -0800 | [diff] [blame] | 1682 | nrt = rt6_alloc_cow(rt, daddr, saddr); | 
| YOSHIFUJI Hideaki | d5315b5 | 2006-03-20 16:58:48 -0800 | [diff] [blame] | 1683 | else | 
|  | 1684 | nrt = rt6_alloc_clone(rt, daddr); | 
| YOSHIFUJI Hideaki | a1e7836 | 2006-03-20 16:56:32 -0800 | [diff] [blame] | 1685 |  | 
| YOSHIFUJI Hideaki | d5315b5 | 2006-03-20 16:58:48 -0800 | [diff] [blame] | 1686 | if (nrt) { | 
| David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 1687 | dst_metric_set(&nrt->dst, RTAX_MTU, pmtu); | 
|  | 1688 | if (allfrag) { | 
|  | 1689 | u32 features = dst_metric(&nrt->dst, RTAX_FEATURES); | 
|  | 1690 | features |= RTAX_FEATURE_ALLFRAG; | 
|  | 1691 | dst_metric_set(&nrt->dst, RTAX_FEATURES, features); | 
|  | 1692 | } | 
| YOSHIFUJI Hideaki | a1e7836 | 2006-03-20 16:56:32 -0800 | [diff] [blame] | 1693 |  | 
|  | 1694 | /* According to RFC 1981, detecting PMTU increase shouldn't be | 
|  | 1695 | * happened within 5 mins, the recommended timer is 10 mins. | 
|  | 1696 | * Here this route expiration time is set to ip6_rt_mtu_expires | 
|  | 1697 | * which is 10 mins. After 10 mins the decreased pmtu is expired | 
|  | 1698 | * and detecting PMTU increase will be automatically happened. | 
|  | 1699 | */ | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1700 | dst_set_expires(&nrt->dst, net->ipv6.sysctl.ip6_rt_mtu_expires); | 
| YOSHIFUJI Hideaki | a1e7836 | 2006-03-20 16:56:32 -0800 | [diff] [blame] | 1701 | nrt->rt6i_flags |= RTF_DYNAMIC|RTF_EXPIRES; | 
|  | 1702 |  | 
| Thomas Graf | 40e22e8 | 2006-08-22 00:00:45 -0700 | [diff] [blame] | 1703 | ip6_ins_rt(nrt); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | out: | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1706 | dst_release(&rt->dst); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | } | 
|  | 1708 |  | 
| Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1709 | void rt6_pmtu_discovery(const struct in6_addr *daddr, const struct in6_addr *saddr, | 
| Maciej Żenczykowski | ae878ae | 2010-10-03 14:49:00 -0700 | [diff] [blame] | 1710 | struct net_device *dev, u32 pmtu) | 
|  | 1711 | { | 
|  | 1712 | struct net *net = dev_net(dev); | 
|  | 1713 |  | 
|  | 1714 | /* | 
|  | 1715 | * RFC 1981 states that a node "MUST reduce the size of the packets it | 
|  | 1716 | * is sending along the path" that caused the Packet Too Big message. | 
|  | 1717 | * Since it's not possible in the general case to determine which | 
|  | 1718 | * interface was used to send the original packet, we update the MTU | 
|  | 1719 | * on the interface that will be used to send future packets. We also | 
|  | 1720 | * update the MTU on the interface that received the Packet Too Big in | 
|  | 1721 | * case the original packet was forced out that interface with | 
|  | 1722 | * SO_BINDTODEVICE or similar. This is the next best thing to the | 
|  | 1723 | * correct behaviour, which would be to update the MTU on all | 
|  | 1724 | * interfaces. | 
|  | 1725 | */ | 
|  | 1726 | rt6_do_pmtu_disc(daddr, saddr, net, pmtu, 0); | 
|  | 1727 | rt6_do_pmtu_disc(daddr, saddr, net, pmtu, dev->ifindex); | 
|  | 1728 | } | 
|  | 1729 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | /* | 
|  | 1731 | *	Misc support functions | 
|  | 1732 | */ | 
|  | 1733 |  | 
|  | 1734 | static struct rt6_info * ip6_rt_copy(struct rt6_info *ort) | 
|  | 1735 | { | 
| YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1736 | struct net *net = dev_net(ort->rt6i_dev); | 
| David S. Miller | 5c1e6aa | 2011-04-28 14:13:38 -0700 | [diff] [blame] | 1737 | struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, | 
| David S. Miller | 957c665 | 2011-06-24 15:25:00 -0700 | [diff] [blame] | 1738 | ort->dst.dev, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 |  | 
|  | 1740 | if (rt) { | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1741 | rt->dst.input = ort->dst.input; | 
|  | 1742 | rt->dst.output = ort->dst.output; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 |  | 
| David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 1744 | dst_copy_metrics(&rt->dst, &ort->dst); | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1745 | rt->dst.error = ort->dst.error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | rt->rt6i_idev = ort->rt6i_idev; | 
|  | 1747 | if (rt->rt6i_idev) | 
|  | 1748 | in6_dev_hold(rt->rt6i_idev); | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1749 | rt->dst.lastuse = jiffies; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | rt->rt6i_expires = 0; | 
|  | 1751 |  | 
|  | 1752 | ipv6_addr_copy(&rt->rt6i_gateway, &ort->rt6i_gateway); | 
|  | 1753 | rt->rt6i_flags = ort->rt6i_flags & ~RTF_EXPIRES; | 
|  | 1754 | rt->rt6i_metric = 0; | 
|  | 1755 |  | 
|  | 1756 | memcpy(&rt->rt6i_dst, &ort->rt6i_dst, sizeof(struct rt6key)); | 
|  | 1757 | #ifdef CONFIG_IPV6_SUBTREES | 
|  | 1758 | memcpy(&rt->rt6i_src, &ort->rt6i_src, sizeof(struct rt6key)); | 
|  | 1759 | #endif | 
| Florian Westphal | 0f6c639 | 2011-05-20 11:27:24 +0000 | [diff] [blame] | 1760 | memcpy(&rt->rt6i_prefsrc, &ort->rt6i_prefsrc, sizeof(struct rt6key)); | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1761 | rt->rt6i_table = ort->rt6i_table; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | } | 
|  | 1763 | return rt; | 
|  | 1764 | } | 
|  | 1765 |  | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1766 | #ifdef CONFIG_IPV6_ROUTE_INFO | 
| Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 1767 | static struct rt6_info *rt6_get_route_info(struct net *net, | 
| Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1768 | const struct in6_addr *prefix, int prefixlen, | 
|  | 1769 | const struct in6_addr *gwaddr, int ifindex) | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1770 | { | 
|  | 1771 | struct fib6_node *fn; | 
|  | 1772 | struct rt6_info *rt = NULL; | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1773 | struct fib6_table *table; | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1774 |  | 
| Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 1775 | table = fib6_get_table(net, RT6_TABLE_INFO); | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1776 | if (table == NULL) | 
|  | 1777 | return NULL; | 
|  | 1778 |  | 
|  | 1779 | write_lock_bh(&table->tb6_lock); | 
|  | 1780 | fn = fib6_locate(&table->tb6_root, prefix ,prefixlen, NULL, 0); | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1781 | if (!fn) | 
|  | 1782 | goto out; | 
|  | 1783 |  | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1784 | for (rt = fn->leaf; rt; rt = rt->dst.rt6_next) { | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1785 | if (rt->rt6i_dev->ifindex != ifindex) | 
|  | 1786 | continue; | 
|  | 1787 | if ((rt->rt6i_flags & (RTF_ROUTEINFO|RTF_GATEWAY)) != (RTF_ROUTEINFO|RTF_GATEWAY)) | 
|  | 1788 | continue; | 
|  | 1789 | if (!ipv6_addr_equal(&rt->rt6i_gateway, gwaddr)) | 
|  | 1790 | continue; | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1791 | dst_hold(&rt->dst); | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1792 | break; | 
|  | 1793 | } | 
|  | 1794 | out: | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1795 | write_unlock_bh(&table->tb6_lock); | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1796 | return rt; | 
|  | 1797 | } | 
|  | 1798 |  | 
| Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 1799 | static struct rt6_info *rt6_add_route_info(struct net *net, | 
| Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1800 | const struct in6_addr *prefix, int prefixlen, | 
|  | 1801 | const struct in6_addr *gwaddr, int ifindex, | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1802 | unsigned pref) | 
|  | 1803 | { | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1804 | struct fib6_config cfg = { | 
|  | 1805 | .fc_table	= RT6_TABLE_INFO, | 
| Rami Rosen | 238fc7e | 2008-02-09 23:43:11 -0800 | [diff] [blame] | 1806 | .fc_metric	= IP6_RT_PRIO_USER, | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1807 | .fc_ifindex	= ifindex, | 
|  | 1808 | .fc_dst_len	= prefixlen, | 
|  | 1809 | .fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_ROUTEINFO | | 
|  | 1810 | RTF_UP | RTF_PREF(pref), | 
| Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 1811 | .fc_nlinfo.pid = 0, | 
|  | 1812 | .fc_nlinfo.nlh = NULL, | 
|  | 1813 | .fc_nlinfo.nl_net = net, | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1814 | }; | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1815 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1816 | ipv6_addr_copy(&cfg.fc_dst, prefix); | 
|  | 1817 | ipv6_addr_copy(&cfg.fc_gateway, gwaddr); | 
|  | 1818 |  | 
| YOSHIFUJI Hideaki | e317da9 | 2006-03-20 17:06:42 -0800 | [diff] [blame] | 1819 | /* We should treat it as a default route if prefix length is 0. */ | 
|  | 1820 | if (!prefixlen) | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1821 | cfg.fc_flags |= RTF_DEFAULT; | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1822 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1823 | ip6_route_add(&cfg); | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1824 |  | 
| Daniel Lezcano | efa2cea | 2008-03-04 13:46:48 -0800 | [diff] [blame] | 1825 | return rt6_get_route_info(net, prefix, prefixlen, gwaddr, ifindex); | 
| YOSHIFUJI Hideaki | 70ceb4f | 2006-03-20 17:06:24 -0800 | [diff] [blame] | 1826 | } | 
|  | 1827 | #endif | 
|  | 1828 |  | 
| Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1829 | struct rt6_info *rt6_get_dflt_router(const struct in6_addr *addr, struct net_device *dev) | 
| YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1830 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | struct rt6_info *rt; | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1832 | struct fib6_table *table; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 |  | 
| YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1834 | table = fib6_get_table(dev_net(dev), RT6_TABLE_DFLT); | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1835 | if (table == NULL) | 
|  | 1836 | return NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 |  | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1838 | write_lock_bh(&table->tb6_lock); | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1839 | for (rt = table->tb6_root.leaf; rt; rt=rt->dst.rt6_next) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | if (dev == rt->rt6i_dev && | 
| YOSHIFUJI Hideaki | 045927f | 2006-03-20 17:00:48 -0800 | [diff] [blame] | 1841 | ((rt->rt6i_flags & (RTF_ADDRCONF | RTF_DEFAULT)) == (RTF_ADDRCONF | RTF_DEFAULT)) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | ipv6_addr_equal(&rt->rt6i_gateway, addr)) | 
|  | 1843 | break; | 
|  | 1844 | } | 
|  | 1845 | if (rt) | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1846 | dst_hold(&rt->dst); | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1847 | write_unlock_bh(&table->tb6_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | return rt; | 
|  | 1849 | } | 
|  | 1850 |  | 
| Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1851 | struct rt6_info *rt6_add_dflt_router(const struct in6_addr *gwaddr, | 
| YOSHIFUJI Hideaki | ebacaaa | 2006-03-20 17:04:53 -0800 | [diff] [blame] | 1852 | struct net_device *dev, | 
|  | 1853 | unsigned int pref) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1854 | { | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1855 | struct fib6_config cfg = { | 
|  | 1856 | .fc_table	= RT6_TABLE_DFLT, | 
| Rami Rosen | 238fc7e | 2008-02-09 23:43:11 -0800 | [diff] [blame] | 1857 | .fc_metric	= IP6_RT_PRIO_USER, | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1858 | .fc_ifindex	= dev->ifindex, | 
|  | 1859 | .fc_flags	= RTF_GATEWAY | RTF_ADDRCONF | RTF_DEFAULT | | 
|  | 1860 | RTF_UP | RTF_EXPIRES | RTF_PREF(pref), | 
| Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1861 | .fc_nlinfo.pid = 0, | 
|  | 1862 | .fc_nlinfo.nlh = NULL, | 
| YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1863 | .fc_nlinfo.nl_net = dev_net(dev), | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1864 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1866 | ipv6_addr_copy(&cfg.fc_gateway, gwaddr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1868 | ip6_route_add(&cfg); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | return rt6_get_dflt_router(gwaddr, dev); | 
|  | 1871 | } | 
|  | 1872 |  | 
| Daniel Lezcano | 7b4da53 | 2008-03-04 13:47:14 -0800 | [diff] [blame] | 1873 | void rt6_purge_dflt_routers(struct net *net) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | { | 
|  | 1875 | struct rt6_info *rt; | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1876 | struct fib6_table *table; | 
|  | 1877 |  | 
|  | 1878 | /* NOTE: Keep consistent with rt6_get_dflt_router */ | 
| Daniel Lezcano | 7b4da53 | 2008-03-04 13:47:14 -0800 | [diff] [blame] | 1879 | table = fib6_get_table(net, RT6_TABLE_DFLT); | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1880 | if (table == NULL) | 
|  | 1881 | return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 |  | 
|  | 1883 | restart: | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1884 | read_lock_bh(&table->tb6_lock); | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1885 | for (rt = table->tb6_root.leaf; rt; rt = rt->dst.rt6_next) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | if (rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) { | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1887 | dst_hold(&rt->dst); | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1888 | read_unlock_bh(&table->tb6_lock); | 
| Thomas Graf | e0a1ad73 | 2006-08-22 00:00:21 -0700 | [diff] [blame] | 1889 | ip6_del_rt(rt); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | goto restart; | 
|  | 1891 | } | 
|  | 1892 | } | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 1893 | read_unlock_bh(&table->tb6_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | } | 
|  | 1895 |  | 
| Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1896 | static void rtmsg_to_fib6_config(struct net *net, | 
|  | 1897 | struct in6_rtmsg *rtmsg, | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1898 | struct fib6_config *cfg) | 
|  | 1899 | { | 
|  | 1900 | memset(cfg, 0, sizeof(*cfg)); | 
|  | 1901 |  | 
|  | 1902 | cfg->fc_table = RT6_TABLE_MAIN; | 
|  | 1903 | cfg->fc_ifindex = rtmsg->rtmsg_ifindex; | 
|  | 1904 | cfg->fc_metric = rtmsg->rtmsg_metric; | 
|  | 1905 | cfg->fc_expires = rtmsg->rtmsg_info; | 
|  | 1906 | cfg->fc_dst_len = rtmsg->rtmsg_dst_len; | 
|  | 1907 | cfg->fc_src_len = rtmsg->rtmsg_src_len; | 
|  | 1908 | cfg->fc_flags = rtmsg->rtmsg_flags; | 
|  | 1909 |  | 
| Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1910 | cfg->fc_nlinfo.nl_net = net; | 
| Benjamin Thery | f1243c2 | 2008-02-26 18:10:03 -0800 | [diff] [blame] | 1911 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1912 | ipv6_addr_copy(&cfg->fc_dst, &rtmsg->rtmsg_dst); | 
|  | 1913 | ipv6_addr_copy(&cfg->fc_src, &rtmsg->rtmsg_src); | 
|  | 1914 | ipv6_addr_copy(&cfg->fc_gateway, &rtmsg->rtmsg_gateway); | 
|  | 1915 | } | 
|  | 1916 |  | 
| Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1917 | int ipv6_route_ioctl(struct net *net, unsigned int cmd, void __user *arg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | { | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1919 | struct fib6_config cfg; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | struct in6_rtmsg rtmsg; | 
|  | 1921 | int err; | 
|  | 1922 |  | 
|  | 1923 | switch(cmd) { | 
|  | 1924 | case SIOCADDRT:		/* Add a route */ | 
|  | 1925 | case SIOCDELRT:		/* Delete a route */ | 
|  | 1926 | if (!capable(CAP_NET_ADMIN)) | 
|  | 1927 | return -EPERM; | 
|  | 1928 | err = copy_from_user(&rtmsg, arg, | 
|  | 1929 | sizeof(struct in6_rtmsg)); | 
|  | 1930 | if (err) | 
|  | 1931 | return -EFAULT; | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1932 |  | 
| Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 1933 | rtmsg_to_fib6_config(net, &rtmsg, &cfg); | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1934 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | rtnl_lock(); | 
|  | 1936 | switch (cmd) { | 
|  | 1937 | case SIOCADDRT: | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1938 | err = ip6_route_add(&cfg); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | break; | 
|  | 1940 | case SIOCDELRT: | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 1941 | err = ip6_route_del(&cfg); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | break; | 
|  | 1943 | default: | 
|  | 1944 | err = -EINVAL; | 
|  | 1945 | } | 
|  | 1946 | rtnl_unlock(); | 
|  | 1947 |  | 
|  | 1948 | return err; | 
| Stephen Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 1949 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 |  | 
|  | 1951 | return -EINVAL; | 
|  | 1952 | } | 
|  | 1953 |  | 
|  | 1954 | /* | 
|  | 1955 | *	Drop the packet on the floor | 
|  | 1956 | */ | 
|  | 1957 |  | 
| Brian Haley | d5fdd6b | 2009-06-23 04:31:07 -0700 | [diff] [blame] | 1958 | static int ip6_pkt_drop(struct sk_buff *skb, u8 code, int ipstats_mib_noroutes) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | { | 
| YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 1960 | int type; | 
| Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1961 | struct dst_entry *dst = skb_dst(skb); | 
| YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 1962 | switch (ipstats_mib_noroutes) { | 
|  | 1963 | case IPSTATS_MIB_INNOROUTES: | 
| Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1964 | type = ipv6_addr_type(&ipv6_hdr(skb)->daddr); | 
| Ulrich Weber | 45bb006 | 2010-02-25 23:28:58 +0000 | [diff] [blame] | 1965 | if (type == IPV6_ADDR_ANY) { | 
| Denis V. Lunev | 3bd653c | 2008-10-08 10:54:51 -0700 | [diff] [blame] | 1966 | IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), | 
|  | 1967 | IPSTATS_MIB_INADDRERRORS); | 
| YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 1968 | break; | 
|  | 1969 | } | 
|  | 1970 | /* FALLTHROUGH */ | 
|  | 1971 | case IPSTATS_MIB_OUTNOROUTES: | 
| Denis V. Lunev | 3bd653c | 2008-10-08 10:54:51 -0700 | [diff] [blame] | 1972 | IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), | 
|  | 1973 | ipstats_mib_noroutes); | 
| YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 1974 | break; | 
|  | 1975 | } | 
| Alexey Dobriyan | 3ffe533 | 2010-02-18 08:25:24 +0000 | [diff] [blame] | 1976 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, code, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | kfree_skb(skb); | 
|  | 1978 | return 0; | 
|  | 1979 | } | 
|  | 1980 |  | 
| Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 1981 | static int ip6_pkt_discard(struct sk_buff *skb) | 
|  | 1982 | { | 
| YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 1983 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_INNOROUTES); | 
| Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 1984 | } | 
|  | 1985 |  | 
| Arnaldo Carvalho de Melo | 2038073 | 2005-08-16 02:18:02 -0300 | [diff] [blame] | 1986 | static int ip6_pkt_discard_out(struct sk_buff *skb) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | { | 
| Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1988 | skb->dev = skb_dst(skb)->dev; | 
| YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 1989 | return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | } | 
|  | 1991 |  | 
| David S. Miller | 6723ab5 | 2006-10-18 21:20:57 -0700 | [diff] [blame] | 1992 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | 
|  | 1993 |  | 
| Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 1994 | static int ip6_pkt_prohibit(struct sk_buff *skb) | 
|  | 1995 | { | 
| YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 1996 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); | 
| Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 1997 | } | 
|  | 1998 |  | 
|  | 1999 | static int ip6_pkt_prohibit_out(struct sk_buff *skb) | 
|  | 2000 | { | 
| Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 2001 | skb->dev = skb_dst(skb)->dev; | 
| YOSHIFUJI Hideaki | 612f09e | 2007-04-13 16:18:02 -0700 | [diff] [blame] | 2002 | return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); | 
| Thomas Graf | 9ce8ade | 2006-10-18 20:46:54 -0700 | [diff] [blame] | 2003 | } | 
|  | 2004 |  | 
| David S. Miller | 6723ab5 | 2006-10-18 21:20:57 -0700 | [diff] [blame] | 2005 | #endif | 
|  | 2006 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | /* | 
|  | 2008 | *	Allocate a dst for local (unicast / anycast) address. | 
|  | 2009 | */ | 
|  | 2010 |  | 
|  | 2011 | struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev, | 
|  | 2012 | const struct in6_addr *addr, | 
|  | 2013 | int anycast) | 
|  | 2014 | { | 
| YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2015 | struct net *net = dev_net(idev->dev); | 
| David S. Miller | 5c1e6aa | 2011-04-28 14:13:38 -0700 | [diff] [blame] | 2016 | struct rt6_info *rt = ip6_dst_alloc(&net->ipv6.ip6_dst_ops, | 
| David S. Miller | 957c665 | 2011-06-24 15:25:00 -0700 | [diff] [blame] | 2017 | net->loopback_dev, 0); | 
| David S. Miller | 14deae4 | 2009-01-04 16:04:39 -0800 | [diff] [blame] | 2018 | struct neighbour *neigh; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 |  | 
| Ben Greear | 4038565 | 2010-11-08 12:33:48 +0000 | [diff] [blame] | 2020 | if (rt == NULL) { | 
|  | 2021 | if (net_ratelimit()) | 
|  | 2022 | pr_warning("IPv6:  Maximum number of routes reached," | 
|  | 2023 | " consider increasing route/max_size.\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | return ERR_PTR(-ENOMEM); | 
| Ben Greear | 4038565 | 2010-11-08 12:33:48 +0000 | [diff] [blame] | 2025 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | in6_dev_hold(idev); | 
|  | 2028 |  | 
| David S. Miller | 11d53b4 | 2011-06-24 15:23:34 -0700 | [diff] [blame] | 2029 | rt->dst.flags |= DST_HOST; | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2030 | rt->dst.input = ip6_input; | 
|  | 2031 | rt->dst.output = ip6_output; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | rt->rt6i_idev = idev; | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2033 | rt->dst.obsolete = -1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 |  | 
|  | 2035 | rt->rt6i_flags = RTF_UP | RTF_NONEXTHOP; | 
| YOSHIFUJI Hideaki | 58c4fb8 | 2005-12-21 22:56:42 +0900 | [diff] [blame] | 2036 | if (anycast) | 
|  | 2037 | rt->rt6i_flags |= RTF_ANYCAST; | 
|  | 2038 | else | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | rt->rt6i_flags |= RTF_LOCAL; | 
| David S. Miller | 14deae4 | 2009-01-04 16:04:39 -0800 | [diff] [blame] | 2040 | neigh = ndisc_get_neigh(rt->rt6i_dev, &rt->rt6i_gateway); | 
|  | 2041 | if (IS_ERR(neigh)) { | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2042 | dst_free(&rt->dst); | 
| David S. Miller | 14deae4 | 2009-01-04 16:04:39 -0800 | [diff] [blame] | 2043 |  | 
| David S. Miller | 29546a6 | 2011-03-03 12:10:37 -0800 | [diff] [blame] | 2044 | return ERR_CAST(neigh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | } | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 2046 | dst_set_neighbour(&rt->dst, neigh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 |  | 
|  | 2048 | ipv6_addr_copy(&rt->rt6i_dst.addr, addr); | 
|  | 2049 | rt->rt6i_dst.plen = 128; | 
| Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2050 | rt->rt6i_table = fib6_get_table(net, RT6_TABLE_LOCAL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 |  | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2052 | atomic_set(&rt->dst.__refcnt, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 |  | 
|  | 2054 | return rt; | 
|  | 2055 | } | 
|  | 2056 |  | 
| Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 2057 | int ip6_route_get_saddr(struct net *net, | 
|  | 2058 | struct rt6_info *rt, | 
| Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 2059 | const struct in6_addr *daddr, | 
| Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 2060 | unsigned int prefs, | 
|  | 2061 | struct in6_addr *saddr) | 
|  | 2062 | { | 
|  | 2063 | struct inet6_dev *idev = ip6_dst_idev((struct dst_entry*)rt); | 
|  | 2064 | int err = 0; | 
|  | 2065 | if (rt->rt6i_prefsrc.plen) | 
|  | 2066 | ipv6_addr_copy(saddr, &rt->rt6i_prefsrc.addr); | 
|  | 2067 | else | 
|  | 2068 | err = ipv6_dev_get_saddr(net, idev ? idev->dev : NULL, | 
|  | 2069 | daddr, prefs, saddr); | 
|  | 2070 | return err; | 
|  | 2071 | } | 
|  | 2072 |  | 
|  | 2073 | /* remove deleted ip from prefsrc entries */ | 
|  | 2074 | struct arg_dev_net_ip { | 
|  | 2075 | struct net_device *dev; | 
|  | 2076 | struct net *net; | 
|  | 2077 | struct in6_addr *addr; | 
|  | 2078 | }; | 
|  | 2079 |  | 
|  | 2080 | static int fib6_remove_prefsrc(struct rt6_info *rt, void *arg) | 
|  | 2081 | { | 
|  | 2082 | struct net_device *dev = ((struct arg_dev_net_ip *)arg)->dev; | 
|  | 2083 | struct net *net = ((struct arg_dev_net_ip *)arg)->net; | 
|  | 2084 | struct in6_addr *addr = ((struct arg_dev_net_ip *)arg)->addr; | 
|  | 2085 |  | 
|  | 2086 | if (((void *)rt->rt6i_dev == dev || dev == NULL) && | 
|  | 2087 | rt != net->ipv6.ip6_null_entry && | 
|  | 2088 | ipv6_addr_equal(addr, &rt->rt6i_prefsrc.addr)) { | 
|  | 2089 | /* remove prefsrc entry */ | 
|  | 2090 | rt->rt6i_prefsrc.plen = 0; | 
|  | 2091 | } | 
|  | 2092 | return 0; | 
|  | 2093 | } | 
|  | 2094 |  | 
|  | 2095 | void rt6_remove_prefsrc(struct inet6_ifaddr *ifp) | 
|  | 2096 | { | 
|  | 2097 | struct net *net = dev_net(ifp->idev->dev); | 
|  | 2098 | struct arg_dev_net_ip adni = { | 
|  | 2099 | .dev = ifp->idev->dev, | 
|  | 2100 | .net = net, | 
|  | 2101 | .addr = &ifp->addr, | 
|  | 2102 | }; | 
|  | 2103 | fib6_clean_all(net, fib6_remove_prefsrc, 0, &adni); | 
|  | 2104 | } | 
|  | 2105 |  | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2106 | struct arg_dev_net { | 
|  | 2107 | struct net_device *dev; | 
|  | 2108 | struct net *net; | 
|  | 2109 | }; | 
|  | 2110 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | static int fib6_ifdown(struct rt6_info *rt, void *arg) | 
|  | 2112 | { | 
| stephen hemminger | bc3ef66 | 2010-12-16 17:42:40 +0000 | [diff] [blame] | 2113 | const struct arg_dev_net *adn = arg; | 
|  | 2114 | const struct net_device *dev = adn->dev; | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2115 |  | 
| stephen hemminger | bc3ef66 | 2010-12-16 17:42:40 +0000 | [diff] [blame] | 2116 | if ((rt->rt6i_dev == dev || dev == NULL) && | 
|  | 2117 | rt != adn->net->ipv6.ip6_null_entry) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | RT6_TRACE("deleted by ifdown %p\n", rt); | 
|  | 2119 | return -1; | 
|  | 2120 | } | 
|  | 2121 | return 0; | 
|  | 2122 | } | 
|  | 2123 |  | 
| Daniel Lezcano | f3db485 | 2008-03-03 23:27:06 -0800 | [diff] [blame] | 2124 | void rt6_ifdown(struct net *net, struct net_device *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2125 | { | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2126 | struct arg_dev_net adn = { | 
|  | 2127 | .dev = dev, | 
|  | 2128 | .net = net, | 
|  | 2129 | }; | 
|  | 2130 |  | 
|  | 2131 | fib6_clean_all(net, fib6_ifdown, 0, &adn); | 
| David S. Miller | 1e493d1 | 2008-09-10 17:27:15 -0700 | [diff] [blame] | 2132 | icmp6_clean_all(fib6_ifdown, &adn); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | } | 
|  | 2134 |  | 
|  | 2135 | struct rt6_mtu_change_arg | 
|  | 2136 | { | 
|  | 2137 | struct net_device *dev; | 
|  | 2138 | unsigned mtu; | 
|  | 2139 | }; | 
|  | 2140 |  | 
|  | 2141 | static int rt6_mtu_change_route(struct rt6_info *rt, void *p_arg) | 
|  | 2142 | { | 
|  | 2143 | struct rt6_mtu_change_arg *arg = (struct rt6_mtu_change_arg *) p_arg; | 
|  | 2144 | struct inet6_dev *idev; | 
|  | 2145 |  | 
|  | 2146 | /* In IPv6 pmtu discovery is not optional, | 
|  | 2147 | so that RTAX_MTU lock cannot disable it. | 
|  | 2148 | We still use this lock to block changes | 
|  | 2149 | caused by addrconf/ndisc. | 
|  | 2150 | */ | 
|  | 2151 |  | 
|  | 2152 | idev = __in6_dev_get(arg->dev); | 
|  | 2153 | if (idev == NULL) | 
|  | 2154 | return 0; | 
|  | 2155 |  | 
|  | 2156 | /* For administrative MTU increase, there is no way to discover | 
|  | 2157 | IPv6 PMTU increase, so PMTU increase should be updated here. | 
|  | 2158 | Since RFC 1981 doesn't include administrative MTU increase | 
|  | 2159 | update PMTU increase is a MUST. (i.e. jumbo frame) | 
|  | 2160 | */ | 
|  | 2161 | /* | 
|  | 2162 | If new MTU is less than route PMTU, this new MTU will be the | 
|  | 2163 | lowest MTU in the path, update the route PMTU to reflect PMTU | 
|  | 2164 | decreases; if new MTU is greater than route PMTU, and the | 
|  | 2165 | old MTU is the lowest MTU in the path, update the route PMTU | 
|  | 2166 | to reflect the increase. In this case if the other nodes' MTU | 
|  | 2167 | also have the lowest MTU, TOO BIG MESSAGE will be lead to | 
|  | 2168 | PMTU discouvery. | 
|  | 2169 | */ | 
|  | 2170 | if (rt->rt6i_dev == arg->dev && | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2171 | !dst_metric_locked(&rt->dst, RTAX_MTU) && | 
|  | 2172 | (dst_mtu(&rt->dst) >= arg->mtu || | 
|  | 2173 | (dst_mtu(&rt->dst) < arg->mtu && | 
|  | 2174 | dst_mtu(&rt->dst) == idev->cnf.mtu6))) { | 
| David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 2175 | dst_metric_set(&rt->dst, RTAX_MTU, arg->mtu); | 
| Simon Arlott | 566cfd8 | 2007-07-26 00:09:55 -0700 | [diff] [blame] | 2176 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | return 0; | 
|  | 2178 | } | 
|  | 2179 |  | 
|  | 2180 | void rt6_mtu_change(struct net_device *dev, unsigned mtu) | 
|  | 2181 | { | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2182 | struct rt6_mtu_change_arg arg = { | 
|  | 2183 | .dev = dev, | 
|  | 2184 | .mtu = mtu, | 
|  | 2185 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 |  | 
| YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2187 | fib6_clean_all(dev_net(dev), rt6_mtu_change_route, 0, &arg); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | } | 
|  | 2189 |  | 
| Patrick McHardy | ef7c79e | 2007-06-05 12:38:30 -0700 | [diff] [blame] | 2190 | static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = { | 
| Thomas Graf | 5176f91 | 2006-08-26 20:13:18 -0700 | [diff] [blame] | 2191 | [RTA_GATEWAY]           = { .len = sizeof(struct in6_addr) }, | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2192 | [RTA_OIF]               = { .type = NLA_U32 }, | 
| Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2193 | [RTA_IIF]		= { .type = NLA_U32 }, | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2194 | [RTA_PRIORITY]          = { .type = NLA_U32 }, | 
|  | 2195 | [RTA_METRICS]           = { .type = NLA_NESTED }, | 
|  | 2196 | }; | 
|  | 2197 |  | 
|  | 2198 | static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, | 
|  | 2199 | struct fib6_config *cfg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2200 | { | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2201 | struct rtmsg *rtm; | 
|  | 2202 | struct nlattr *tb[RTA_MAX+1]; | 
|  | 2203 | int err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2205 | err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); | 
|  | 2206 | if (err < 0) | 
|  | 2207 | goto errout; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2209 | err = -EINVAL; | 
|  | 2210 | rtm = nlmsg_data(nlh); | 
|  | 2211 | memset(cfg, 0, sizeof(*cfg)); | 
|  | 2212 |  | 
|  | 2213 | cfg->fc_table = rtm->rtm_table; | 
|  | 2214 | cfg->fc_dst_len = rtm->rtm_dst_len; | 
|  | 2215 | cfg->fc_src_len = rtm->rtm_src_len; | 
|  | 2216 | cfg->fc_flags = RTF_UP; | 
|  | 2217 | cfg->fc_protocol = rtm->rtm_protocol; | 
|  | 2218 |  | 
|  | 2219 | if (rtm->rtm_type == RTN_UNREACHABLE) | 
|  | 2220 | cfg->fc_flags |= RTF_REJECT; | 
|  | 2221 |  | 
| Maciej Żenczykowski | ab79ad1 | 2010-09-27 00:07:02 +0000 | [diff] [blame] | 2222 | if (rtm->rtm_type == RTN_LOCAL) | 
|  | 2223 | cfg->fc_flags |= RTF_LOCAL; | 
|  | 2224 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2225 | cfg->fc_nlinfo.pid = NETLINK_CB(skb).pid; | 
|  | 2226 | cfg->fc_nlinfo.nlh = nlh; | 
| YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2227 | cfg->fc_nlinfo.nl_net = sock_net(skb->sk); | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2228 |  | 
|  | 2229 | if (tb[RTA_GATEWAY]) { | 
|  | 2230 | nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16); | 
|  | 2231 | cfg->fc_flags |= RTF_GATEWAY; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | } | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2233 |  | 
|  | 2234 | if (tb[RTA_DST]) { | 
|  | 2235 | int plen = (rtm->rtm_dst_len + 7) >> 3; | 
|  | 2236 |  | 
|  | 2237 | if (nla_len(tb[RTA_DST]) < plen) | 
|  | 2238 | goto errout; | 
|  | 2239 |  | 
|  | 2240 | nla_memcpy(&cfg->fc_dst, tb[RTA_DST], plen); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | } | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2242 |  | 
|  | 2243 | if (tb[RTA_SRC]) { | 
|  | 2244 | int plen = (rtm->rtm_src_len + 7) >> 3; | 
|  | 2245 |  | 
|  | 2246 | if (nla_len(tb[RTA_SRC]) < plen) | 
|  | 2247 | goto errout; | 
|  | 2248 |  | 
|  | 2249 | nla_memcpy(&cfg->fc_src, tb[RTA_SRC], plen); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | } | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2251 |  | 
| Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 2252 | if (tb[RTA_PREFSRC]) | 
|  | 2253 | nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16); | 
|  | 2254 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2255 | if (tb[RTA_OIF]) | 
|  | 2256 | cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); | 
|  | 2257 |  | 
|  | 2258 | if (tb[RTA_PRIORITY]) | 
|  | 2259 | cfg->fc_metric = nla_get_u32(tb[RTA_PRIORITY]); | 
|  | 2260 |  | 
|  | 2261 | if (tb[RTA_METRICS]) { | 
|  | 2262 | cfg->fc_mx = nla_data(tb[RTA_METRICS]); | 
|  | 2263 | cfg->fc_mx_len = nla_len(tb[RTA_METRICS]); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | } | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2265 |  | 
|  | 2266 | if (tb[RTA_TABLE]) | 
|  | 2267 | cfg->fc_table = nla_get_u32(tb[RTA_TABLE]); | 
|  | 2268 |  | 
|  | 2269 | err = 0; | 
|  | 2270 | errout: | 
|  | 2271 | return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | } | 
|  | 2273 |  | 
| Thomas Graf | c127ea2 | 2007-03-22 11:58:32 -0700 | [diff] [blame] | 2274 | static int inet6_rtm_delroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | { | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2276 | struct fib6_config cfg; | 
|  | 2277 | int err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2279 | err = rtm_to_fib6_config(skb, nlh, &cfg); | 
|  | 2280 | if (err < 0) | 
|  | 2281 | return err; | 
|  | 2282 |  | 
|  | 2283 | return ip6_route_del(&cfg); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | } | 
|  | 2285 |  | 
| Thomas Graf | c127ea2 | 2007-03-22 11:58:32 -0700 | [diff] [blame] | 2286 | static int inet6_rtm_newroute(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | { | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2288 | struct fib6_config cfg; | 
|  | 2289 | int err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2291 | err = rtm_to_fib6_config(skb, nlh, &cfg); | 
|  | 2292 | if (err < 0) | 
|  | 2293 | return err; | 
|  | 2294 |  | 
|  | 2295 | return ip6_route_add(&cfg); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | } | 
|  | 2297 |  | 
| Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 2298 | static inline size_t rt6_nlmsg_size(void) | 
|  | 2299 | { | 
|  | 2300 | return NLMSG_ALIGN(sizeof(struct rtmsg)) | 
|  | 2301 | + nla_total_size(16) /* RTA_SRC */ | 
|  | 2302 | + nla_total_size(16) /* RTA_DST */ | 
|  | 2303 | + nla_total_size(16) /* RTA_GATEWAY */ | 
|  | 2304 | + nla_total_size(16) /* RTA_PREFSRC */ | 
|  | 2305 | + nla_total_size(4) /* RTA_TABLE */ | 
|  | 2306 | + nla_total_size(4) /* RTA_IIF */ | 
|  | 2307 | + nla_total_size(4) /* RTA_OIF */ | 
|  | 2308 | + nla_total_size(4) /* RTA_PRIORITY */ | 
| Noriaki TAKAMIYA | 6a2b9ce | 2007-01-23 22:09:41 -0800 | [diff] [blame] | 2309 | + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ | 
| Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 2310 | + nla_total_size(sizeof(struct rta_cacheinfo)); | 
|  | 2311 | } | 
|  | 2312 |  | 
| Brian Haley | 191cd58 | 2008-08-14 15:33:21 -0700 | [diff] [blame] | 2313 | static int rt6_fill_node(struct net *net, | 
|  | 2314 | struct sk_buff *skb, struct rt6_info *rt, | 
| Jamal Hadi Salim | 0d51aa8 | 2005-06-21 13:51:04 -0700 | [diff] [blame] | 2315 | struct in6_addr *dst, struct in6_addr *src, | 
|  | 2316 | int iif, int type, u32 pid, u32 seq, | 
| YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2317 | int prefix, int nowait, unsigned int flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | { | 
|  | 2319 | struct rtmsg *rtm; | 
| Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2320 | struct nlmsghdr *nlh; | 
| Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 2321 | long expires; | 
| Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 2322 | u32 table; | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 2323 | struct neighbour *n; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 |  | 
|  | 2325 | if (prefix) {	/* user wants prefix routes only */ | 
|  | 2326 | if (!(rt->rt6i_flags & RTF_PREFIX_RT)) { | 
|  | 2327 | /* success since this is not a prefix route */ | 
|  | 2328 | return 1; | 
|  | 2329 | } | 
|  | 2330 | } | 
|  | 2331 |  | 
| Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2332 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*rtm), flags); | 
|  | 2333 | if (nlh == NULL) | 
| Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 2334 | return -EMSGSIZE; | 
| Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2335 |  | 
|  | 2336 | rtm = nlmsg_data(nlh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2337 | rtm->rtm_family = AF_INET6; | 
|  | 2338 | rtm->rtm_dst_len = rt->rt6i_dst.plen; | 
|  | 2339 | rtm->rtm_src_len = rt->rt6i_src.plen; | 
|  | 2340 | rtm->rtm_tos = 0; | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2341 | if (rt->rt6i_table) | 
| Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 2342 | table = rt->rt6i_table->tb6_id; | 
| Thomas Graf | c71099a | 2006-08-04 23:20:06 -0700 | [diff] [blame] | 2343 | else | 
| Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 2344 | table = RT6_TABLE_UNSPEC; | 
|  | 2345 | rtm->rtm_table = table; | 
| Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2346 | NLA_PUT_U32(skb, RTA_TABLE, table); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2347 | if (rt->rt6i_flags&RTF_REJECT) | 
|  | 2348 | rtm->rtm_type = RTN_UNREACHABLE; | 
| Maciej Żenczykowski | ab79ad1 | 2010-09-27 00:07:02 +0000 | [diff] [blame] | 2349 | else if (rt->rt6i_flags&RTF_LOCAL) | 
|  | 2350 | rtm->rtm_type = RTN_LOCAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2351 | else if (rt->rt6i_dev && (rt->rt6i_dev->flags&IFF_LOOPBACK)) | 
|  | 2352 | rtm->rtm_type = RTN_LOCAL; | 
|  | 2353 | else | 
|  | 2354 | rtm->rtm_type = RTN_UNICAST; | 
|  | 2355 | rtm->rtm_flags = 0; | 
|  | 2356 | rtm->rtm_scope = RT_SCOPE_UNIVERSE; | 
|  | 2357 | rtm->rtm_protocol = rt->rt6i_protocol; | 
|  | 2358 | if (rt->rt6i_flags&RTF_DYNAMIC) | 
|  | 2359 | rtm->rtm_protocol = RTPROT_REDIRECT; | 
|  | 2360 | else if (rt->rt6i_flags & RTF_ADDRCONF) | 
|  | 2361 | rtm->rtm_protocol = RTPROT_KERNEL; | 
|  | 2362 | else if (rt->rt6i_flags&RTF_DEFAULT) | 
|  | 2363 | rtm->rtm_protocol = RTPROT_RA; | 
|  | 2364 |  | 
|  | 2365 | if (rt->rt6i_flags&RTF_CACHE) | 
|  | 2366 | rtm->rtm_flags |= RTM_F_CLONED; | 
|  | 2367 |  | 
|  | 2368 | if (dst) { | 
| Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2369 | NLA_PUT(skb, RTA_DST, 16, dst); | 
| YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2370 | rtm->rtm_dst_len = 128; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | } else if (rtm->rtm_dst_len) | 
| Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2372 | NLA_PUT(skb, RTA_DST, 16, &rt->rt6i_dst.addr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | #ifdef CONFIG_IPV6_SUBTREES | 
|  | 2374 | if (src) { | 
| Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2375 | NLA_PUT(skb, RTA_SRC, 16, src); | 
| YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2376 | rtm->rtm_src_len = 128; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | } else if (rtm->rtm_src_len) | 
| Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2378 | NLA_PUT(skb, RTA_SRC, 16, &rt->rt6i_src.addr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | #endif | 
| YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2380 | if (iif) { | 
|  | 2381 | #ifdef CONFIG_IPV6_MROUTE | 
|  | 2382 | if (ipv6_addr_is_multicast(&rt->rt6i_dst.addr)) { | 
| Benjamin Thery | 8229efd | 2008-12-10 16:30:15 -0800 | [diff] [blame] | 2383 | int err = ip6mr_get_route(net, skb, rtm, nowait); | 
| YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2384 | if (err <= 0) { | 
|  | 2385 | if (!nowait) { | 
|  | 2386 | if (err == 0) | 
|  | 2387 | return 0; | 
|  | 2388 | goto nla_put_failure; | 
|  | 2389 | } else { | 
|  | 2390 | if (err == -EMSGSIZE) | 
|  | 2391 | goto nla_put_failure; | 
|  | 2392 | } | 
|  | 2393 | } | 
|  | 2394 | } else | 
|  | 2395 | #endif | 
|  | 2396 | NLA_PUT_U32(skb, RTA_IIF, iif); | 
|  | 2397 | } else if (dst) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | struct in6_addr saddr_buf; | 
| Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 2399 | if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0) | 
| Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2400 | NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2401 | } | 
| Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2402 |  | 
| Daniel Walter | c3968a8 | 2011-04-13 21:10:57 +0000 | [diff] [blame] | 2403 | if (rt->rt6i_prefsrc.plen) { | 
|  | 2404 | struct in6_addr saddr_buf; | 
|  | 2405 | ipv6_addr_copy(&saddr_buf, &rt->rt6i_prefsrc.addr); | 
|  | 2406 | NLA_PUT(skb, RTA_PREFSRC, 16, &saddr_buf); | 
|  | 2407 | } | 
|  | 2408 |  | 
| David S. Miller | defb351 | 2010-12-08 21:16:57 -0800 | [diff] [blame] | 2409 | if (rtnetlink_put_metrics(skb, dst_metrics_ptr(&rt->dst)) < 0) | 
| Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2410 | goto nla_put_failure; | 
|  | 2411 |  | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 2412 | rcu_read_lock(); | 
|  | 2413 | n = dst_get_neighbour(&rt->dst); | 
|  | 2414 | if (n) | 
|  | 2415 | NLA_PUT(skb, RTA_GATEWAY, 16, &n->primary_key); | 
|  | 2416 | rcu_read_unlock(); | 
| Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2417 |  | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2418 | if (rt->dst.dev) | 
| Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2419 | NLA_PUT_U32(skb, RTA_OIF, rt->rt6i_dev->ifindex); | 
|  | 2420 |  | 
|  | 2421 | NLA_PUT_U32(skb, RTA_PRIORITY, rt->rt6i_metric); | 
| Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 2422 |  | 
| YOSHIFUJI Hideaki | 36e3dea | 2008-05-13 02:52:55 +0900 | [diff] [blame] | 2423 | if (!(rt->rt6i_flags & RTF_EXPIRES)) | 
|  | 2424 | expires = 0; | 
|  | 2425 | else if (rt->rt6i_expires - jiffies < INT_MAX) | 
|  | 2426 | expires = rt->rt6i_expires - jiffies; | 
|  | 2427 | else | 
|  | 2428 | expires = INT_MAX; | 
| YOSHIFUJI Hideaki | 69cdf8f | 2008-05-19 16:55:13 -0700 | [diff] [blame] | 2429 |  | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2430 | if (rtnl_put_cacheinfo(skb, &rt->dst, 0, 0, 0, | 
|  | 2431 | expires, rt->dst.error) < 0) | 
| Thomas Graf | e3703b3 | 2006-11-27 09:27:07 -0800 | [diff] [blame] | 2432 | goto nla_put_failure; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 |  | 
| Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2434 | return nlmsg_end(skb, nlh); | 
|  | 2435 |  | 
|  | 2436 | nla_put_failure: | 
| Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 2437 | nlmsg_cancel(skb, nlh); | 
|  | 2438 | return -EMSGSIZE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | } | 
|  | 2440 |  | 
| Patrick McHardy | 1b43af5 | 2006-08-10 23:11:17 -0700 | [diff] [blame] | 2441 | int rt6_dump_route(struct rt6_info *rt, void *p_arg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | { | 
|  | 2443 | struct rt6_rtnl_dump_arg *arg = (struct rt6_rtnl_dump_arg *) p_arg; | 
|  | 2444 | int prefix; | 
|  | 2445 |  | 
| Thomas Graf | 2d7202b | 2006-08-22 00:01:27 -0700 | [diff] [blame] | 2446 | if (nlmsg_len(arg->cb->nlh) >= sizeof(struct rtmsg)) { | 
|  | 2447 | struct rtmsg *rtm = nlmsg_data(arg->cb->nlh); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | prefix = (rtm->rtm_flags & RTM_F_PREFIX) != 0; | 
|  | 2449 | } else | 
|  | 2450 | prefix = 0; | 
|  | 2451 |  | 
| Brian Haley | 191cd58 | 2008-08-14 15:33:21 -0700 | [diff] [blame] | 2452 | return rt6_fill_node(arg->net, | 
|  | 2453 | arg->skb, rt, NULL, NULL, 0, RTM_NEWROUTE, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | NETLINK_CB(arg->cb->skb).pid, arg->cb->nlh->nlmsg_seq, | 
| YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2455 | prefix, 0, NLM_F_MULTI); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | } | 
|  | 2457 |  | 
| Thomas Graf | c127ea2 | 2007-03-22 11:58:32 -0700 | [diff] [blame] | 2458 | static int inet6_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr* nlh, void *arg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | { | 
| YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2460 | struct net *net = sock_net(in_skb->sk); | 
| Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2461 | struct nlattr *tb[RTA_MAX+1]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | struct rt6_info *rt; | 
| Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2463 | struct sk_buff *skb; | 
|  | 2464 | struct rtmsg *rtm; | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2465 | struct flowi6 fl6; | 
| Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2466 | int err, iif = 0; | 
|  | 2467 |  | 
|  | 2468 | err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); | 
|  | 2469 | if (err < 0) | 
|  | 2470 | goto errout; | 
|  | 2471 |  | 
|  | 2472 | err = -EINVAL; | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2473 | memset(&fl6, 0, sizeof(fl6)); | 
| Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2474 |  | 
|  | 2475 | if (tb[RTA_SRC]) { | 
|  | 2476 | if (nla_len(tb[RTA_SRC]) < sizeof(struct in6_addr)) | 
|  | 2477 | goto errout; | 
|  | 2478 |  | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2479 | ipv6_addr_copy(&fl6.saddr, nla_data(tb[RTA_SRC])); | 
| Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2480 | } | 
|  | 2481 |  | 
|  | 2482 | if (tb[RTA_DST]) { | 
|  | 2483 | if (nla_len(tb[RTA_DST]) < sizeof(struct in6_addr)) | 
|  | 2484 | goto errout; | 
|  | 2485 |  | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2486 | ipv6_addr_copy(&fl6.daddr, nla_data(tb[RTA_DST])); | 
| Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2487 | } | 
|  | 2488 |  | 
|  | 2489 | if (tb[RTA_IIF]) | 
|  | 2490 | iif = nla_get_u32(tb[RTA_IIF]); | 
|  | 2491 |  | 
|  | 2492 | if (tb[RTA_OIF]) | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2493 | fl6.flowi6_oif = nla_get_u32(tb[RTA_OIF]); | 
| Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2494 |  | 
|  | 2495 | if (iif) { | 
|  | 2496 | struct net_device *dev; | 
| Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2497 | dev = __dev_get_by_index(net, iif); | 
| Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2498 | if (!dev) { | 
|  | 2499 | err = -ENODEV; | 
|  | 2500 | goto errout; | 
|  | 2501 | } | 
|  | 2502 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 |  | 
|  | 2504 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | 
| Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2505 | if (skb == NULL) { | 
|  | 2506 | err = -ENOBUFS; | 
|  | 2507 | goto errout; | 
|  | 2508 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 |  | 
|  | 2510 | /* Reserve room for dummy headers, this skb can pass | 
|  | 2511 | through good chunk of routing engine. | 
|  | 2512 | */ | 
| Arnaldo Carvalho de Melo | 459a98e | 2007-03-19 15:30:44 -0700 | [diff] [blame] | 2513 | skb_reset_mac_header(skb); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr)); | 
|  | 2515 |  | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2516 | rt = (struct rt6_info*) ip6_route_output(net, NULL, &fl6); | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2517 | skb_dst_set(skb, &rt->dst); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2518 |  | 
| David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2519 | err = rt6_fill_node(net, skb, rt, &fl6.daddr, &fl6.saddr, iif, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | RTM_NEWROUTE, NETLINK_CB(in_skb).pid, | 
| YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2521 | nlh->nlmsg_seq, 0, 0, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2522 | if (err < 0) { | 
| Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2523 | kfree_skb(skb); | 
|  | 2524 | goto errout; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2525 | } | 
|  | 2526 |  | 
| Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2527 | err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid); | 
| Thomas Graf | ab364a6 | 2006-08-22 00:01:47 -0700 | [diff] [blame] | 2528 | errout: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2529 | return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | } | 
|  | 2531 |  | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2532 | void inet6_rt_notify(int event, struct rt6_info *rt, struct nl_info *info) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2533 | { | 
|  | 2534 | struct sk_buff *skb; | 
| Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2535 | struct net *net = info->nl_net; | 
| Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 2536 | u32 seq; | 
|  | 2537 | int err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2538 |  | 
| Denis V. Lunev | 528c4ce | 2007-12-13 09:45:12 -0800 | [diff] [blame] | 2539 | err = -ENOBUFS; | 
|  | 2540 | seq = info->nlh != NULL ? info->nlh->nlmsg_seq : 0; | 
| Thomas Graf | 86872cb | 2006-08-22 00:01:08 -0700 | [diff] [blame] | 2541 |  | 
| Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 2542 | skb = nlmsg_new(rt6_nlmsg_size(), gfp_any()); | 
| Thomas Graf | 21713eb | 2006-08-15 00:35:24 -0700 | [diff] [blame] | 2543 | if (skb == NULL) | 
|  | 2544 | goto errout; | 
|  | 2545 |  | 
| Brian Haley | 191cd58 | 2008-08-14 15:33:21 -0700 | [diff] [blame] | 2546 | err = rt6_fill_node(net, skb, rt, NULL, NULL, 0, | 
| YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2547 | event, info->pid, seq, 0, 0, 0); | 
| Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 2548 | if (err < 0) { | 
|  | 2549 | /* -EMSGSIZE implies BUG in rt6_nlmsg_size() */ | 
|  | 2550 | WARN_ON(err == -EMSGSIZE); | 
|  | 2551 | kfree_skb(skb); | 
|  | 2552 | goto errout; | 
|  | 2553 | } | 
| Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 2554 | rtnl_notify(skb, net, info->pid, RTNLGRP_IPV6_ROUTE, | 
|  | 2555 | info->nlh, gfp_any()); | 
|  | 2556 | return; | 
| Thomas Graf | 21713eb | 2006-08-15 00:35:24 -0700 | [diff] [blame] | 2557 | errout: | 
|  | 2558 | if (err < 0) | 
| Daniel Lezcano | 5578689 | 2008-03-04 13:47:47 -0800 | [diff] [blame] | 2559 | rtnl_set_sk_err(net, RTNLGRP_IPV6_ROUTE, err); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2560 | } | 
|  | 2561 |  | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2562 | static int ip6_route_dev_notify(struct notifier_block *this, | 
|  | 2563 | unsigned long event, void *data) | 
|  | 2564 | { | 
|  | 2565 | struct net_device *dev = (struct net_device *)data; | 
| YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 2566 | struct net *net = dev_net(dev); | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2567 |  | 
|  | 2568 | if (event == NETDEV_REGISTER && (dev->flags & IFF_LOOPBACK)) { | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2569 | net->ipv6.ip6_null_entry->dst.dev = dev; | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2570 | net->ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(dev); | 
|  | 2571 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2572 | net->ipv6.ip6_prohibit_entry->dst.dev = dev; | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2573 | net->ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(dev); | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2574 | net->ipv6.ip6_blk_hole_entry->dst.dev = dev; | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2575 | net->ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(dev); | 
|  | 2576 | #endif | 
|  | 2577 | } | 
|  | 2578 |  | 
|  | 2579 | return NOTIFY_OK; | 
|  | 2580 | } | 
|  | 2581 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2582 | /* | 
|  | 2583 | *	/proc | 
|  | 2584 | */ | 
|  | 2585 |  | 
|  | 2586 | #ifdef CONFIG_PROC_FS | 
|  | 2587 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | struct rt6_proc_arg | 
|  | 2589 | { | 
|  | 2590 | char *buffer; | 
|  | 2591 | int offset; | 
|  | 2592 | int length; | 
|  | 2593 | int skip; | 
|  | 2594 | int len; | 
|  | 2595 | }; | 
|  | 2596 |  | 
|  | 2597 | static int rt6_info_route(struct rt6_info *rt, void *p_arg) | 
|  | 2598 | { | 
| Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 2599 | struct seq_file *m = p_arg; | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 2600 | struct neighbour *n; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 |  | 
| Harvey Harrison | 4b7a427 | 2008-10-29 12:50:24 -0700 | [diff] [blame] | 2602 | seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 |  | 
|  | 2604 | #ifdef CONFIG_IPV6_SUBTREES | 
| Harvey Harrison | 4b7a427 | 2008-10-29 12:50:24 -0700 | [diff] [blame] | 2605 | seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2606 | #else | 
| Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 2607 | seq_puts(m, "00000000000000000000000000000000 00 "); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2608 | #endif | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 2609 | rcu_read_lock(); | 
|  | 2610 | n = dst_get_neighbour(&rt->dst); | 
|  | 2611 | if (n) { | 
|  | 2612 | seq_printf(m, "%pi6", n->primary_key); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | } else { | 
| Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 2614 | seq_puts(m, "00000000000000000000000000000000"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2615 | } | 
| Eric Dumazet | 8a53366 | 2012-02-09 16:13:19 -0500 | [diff] [blame] | 2616 | rcu_read_unlock(); | 
| Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 2617 | seq_printf(m, " %08x %08x %08x %08x %8s\n", | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2618 | rt->rt6i_metric, atomic_read(&rt->dst.__refcnt), | 
|  | 2619 | rt->dst.__use, rt->rt6i_flags, | 
| Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 2620 | rt->rt6i_dev ? rt->rt6i_dev->name : ""); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2621 | return 0; | 
|  | 2622 | } | 
|  | 2623 |  | 
| Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 2624 | static int ipv6_route_show(struct seq_file *m, void *v) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2625 | { | 
| Daniel Lezcano | f3db485 | 2008-03-03 23:27:06 -0800 | [diff] [blame] | 2626 | struct net *net = (struct net *)m->private; | 
|  | 2627 | fib6_clean_all(net, rt6_info_route, 0, m); | 
| Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 2628 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2629 | } | 
|  | 2630 |  | 
| Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 2631 | static int ipv6_route_open(struct inode *inode, struct file *file) | 
|  | 2632 | { | 
| Pavel Emelyanov | de05c55 | 2008-07-18 04:07:21 -0700 | [diff] [blame] | 2633 | return single_open_net(inode, file, ipv6_route_show); | 
| Daniel Lezcano | f3db485 | 2008-03-03 23:27:06 -0800 | [diff] [blame] | 2634 | } | 
|  | 2635 |  | 
| Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 2636 | static const struct file_operations ipv6_route_proc_fops = { | 
|  | 2637 | .owner		= THIS_MODULE, | 
|  | 2638 | .open		= ipv6_route_open, | 
|  | 2639 | .read		= seq_read, | 
|  | 2640 | .llseek		= seq_lseek, | 
| Pavel Emelyanov | b6fcbdb | 2008-07-18 04:07:44 -0700 | [diff] [blame] | 2641 | .release	= single_release_net, | 
| Alexey Dobriyan | 33120b3 | 2007-11-06 05:27:11 -0800 | [diff] [blame] | 2642 | }; | 
|  | 2643 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | static int rt6_stats_seq_show(struct seq_file *seq, void *v) | 
|  | 2645 | { | 
| Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 2646 | struct net *net = (struct net *)seq->private; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | seq_printf(seq, "%04x %04x %04x %04x %04x %04x %04x\n", | 
| Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 2648 | net->ipv6.rt6_stats->fib_nodes, | 
|  | 2649 | net->ipv6.rt6_stats->fib_route_nodes, | 
|  | 2650 | net->ipv6.rt6_stats->fib_rt_alloc, | 
|  | 2651 | net->ipv6.rt6_stats->fib_rt_entries, | 
|  | 2652 | net->ipv6.rt6_stats->fib_rt_cache, | 
| Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2653 | dst_entries_get_slow(&net->ipv6.ip6_dst_ops), | 
| Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 2654 | net->ipv6.rt6_stats->fib_discarded_routes); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2655 |  | 
|  | 2656 | return 0; | 
|  | 2657 | } | 
|  | 2658 |  | 
|  | 2659 | static int rt6_stats_seq_open(struct inode *inode, struct file *file) | 
|  | 2660 | { | 
| Pavel Emelyanov | de05c55 | 2008-07-18 04:07:21 -0700 | [diff] [blame] | 2661 | return single_open_net(inode, file, rt6_stats_seq_show); | 
| Daniel Lezcano | 69ddb80 | 2008-03-04 13:46:23 -0800 | [diff] [blame] | 2662 | } | 
|  | 2663 |  | 
| Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 2664 | static const struct file_operations rt6_stats_seq_fops = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2665 | .owner	 = THIS_MODULE, | 
|  | 2666 | .open	 = rt6_stats_seq_open, | 
|  | 2667 | .read	 = seq_read, | 
|  | 2668 | .llseek	 = seq_lseek, | 
| Pavel Emelyanov | b6fcbdb | 2008-07-18 04:07:44 -0700 | [diff] [blame] | 2669 | .release = single_release_net, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2670 | }; | 
|  | 2671 | #endif	/* CONFIG_PROC_FS */ | 
|  | 2672 |  | 
|  | 2673 | #ifdef CONFIG_SYSCTL | 
|  | 2674 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2675 | static | 
| Alexey Dobriyan | 8d65af7 | 2009-09-23 15:57:19 -0700 | [diff] [blame] | 2676 | int ipv6_sysctl_rtcache_flush(ctl_table *ctl, int write, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | void __user *buffer, size_t *lenp, loff_t *ppos) | 
|  | 2678 | { | 
| Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 2679 | struct net *net; | 
|  | 2680 | int delay; | 
|  | 2681 | if (!write) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2682 | return -EINVAL; | 
| Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 2683 |  | 
|  | 2684 | net = (struct net *)ctl->extra1; | 
|  | 2685 | delay = net->ipv6.sysctl.flush_delay; | 
|  | 2686 | proc_dointvec(ctl, write, buffer, lenp, ppos); | 
|  | 2687 | fib6_run_gc(delay <= 0 ? ~0UL : (unsigned long)delay, net); | 
|  | 2688 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2689 | } | 
|  | 2690 |  | 
| Daniel Lezcano | 760f2d0 | 2008-01-10 02:53:43 -0800 | [diff] [blame] | 2691 | ctl_table ipv6_route_table_template[] = { | 
| YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2692 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2693 | .procname	=	"flush", | 
| Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 2694 | .data		=	&init_net.ipv6.sysctl.flush_delay, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2695 | .maxlen		=	sizeof(int), | 
| Dave Jones | 89c8b3a | 2005-04-28 12:11:49 -0700 | [diff] [blame] | 2696 | .mode		=	0200, | 
| Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 2697 | .proc_handler	=	ipv6_sysctl_rtcache_flush | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2698 | }, | 
|  | 2699 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2700 | .procname	=	"gc_thresh", | 
| Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 2701 | .data		=	&ip6_dst_ops_template.gc_thresh, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | .maxlen		=	sizeof(int), | 
|  | 2703 | .mode		=	0644, | 
| Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 2704 | .proc_handler	=	proc_dointvec, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2705 | }, | 
|  | 2706 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2707 | .procname	=	"max_size", | 
| Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 2708 | .data		=	&init_net.ipv6.sysctl.ip6_rt_max_size, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2709 | .maxlen		=	sizeof(int), | 
|  | 2710 | .mode		=	0644, | 
| Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 2711 | .proc_handler	=	proc_dointvec, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2712 | }, | 
|  | 2713 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2714 | .procname	=	"gc_min_interval", | 
| Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 2715 | .data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | .maxlen		=	sizeof(int), | 
|  | 2717 | .mode		=	0644, | 
| Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 2718 | .proc_handler	=	proc_dointvec_jiffies, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2719 | }, | 
|  | 2720 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | .procname	=	"gc_timeout", | 
| Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 2722 | .data		=	&init_net.ipv6.sysctl.ip6_rt_gc_timeout, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2723 | .maxlen		=	sizeof(int), | 
|  | 2724 | .mode		=	0644, | 
| Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 2725 | .proc_handler	=	proc_dointvec_jiffies, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2726 | }, | 
|  | 2727 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2728 | .procname	=	"gc_interval", | 
| Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 2729 | .data		=	&init_net.ipv6.sysctl.ip6_rt_gc_interval, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | .maxlen		=	sizeof(int), | 
|  | 2731 | .mode		=	0644, | 
| Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 2732 | .proc_handler	=	proc_dointvec_jiffies, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | }, | 
|  | 2734 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2735 | .procname	=	"gc_elasticity", | 
| Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 2736 | .data		=	&init_net.ipv6.sysctl.ip6_rt_gc_elasticity, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | .maxlen		=	sizeof(int), | 
|  | 2738 | .mode		=	0644, | 
| Min Zhang | f3d3f61 | 2010-08-14 22:42:51 -0700 | [diff] [blame] | 2739 | .proc_handler	=	proc_dointvec, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2740 | }, | 
|  | 2741 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2742 | .procname	=	"mtu_expires", | 
| Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 2743 | .data		=	&init_net.ipv6.sysctl.ip6_rt_mtu_expires, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2744 | .maxlen		=	sizeof(int), | 
|  | 2745 | .mode		=	0644, | 
| Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 2746 | .proc_handler	=	proc_dointvec_jiffies, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2747 | }, | 
|  | 2748 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | .procname	=	"min_adv_mss", | 
| Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 2750 | .data		=	&init_net.ipv6.sysctl.ip6_rt_min_advmss, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | .maxlen		=	sizeof(int), | 
|  | 2752 | .mode		=	0644, | 
| Min Zhang | f3d3f61 | 2010-08-14 22:42:51 -0700 | [diff] [blame] | 2753 | .proc_handler	=	proc_dointvec, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | }, | 
|  | 2755 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2756 | .procname	=	"gc_min_interval_ms", | 
| Daniel Lezcano | 4990509 | 2008-01-10 03:01:01 -0800 | [diff] [blame] | 2757 | .data		=	&init_net.ipv6.sysctl.ip6_rt_gc_min_interval, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | .maxlen		=	sizeof(int), | 
|  | 2759 | .mode		=	0644, | 
| Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 2760 | .proc_handler	=	proc_dointvec_ms_jiffies, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2761 | }, | 
| Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 2762 | { } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2763 | }; | 
|  | 2764 |  | 
| Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2765 | struct ctl_table * __net_init ipv6_route_sysctl_init(struct net *net) | 
| Daniel Lezcano | 760f2d0 | 2008-01-10 02:53:43 -0800 | [diff] [blame] | 2766 | { | 
|  | 2767 | struct ctl_table *table; | 
|  | 2768 |  | 
|  | 2769 | table = kmemdup(ipv6_route_table_template, | 
|  | 2770 | sizeof(ipv6_route_table_template), | 
|  | 2771 | GFP_KERNEL); | 
| YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 2772 |  | 
|  | 2773 | if (table) { | 
|  | 2774 | table[0].data = &net->ipv6.sysctl.flush_delay; | 
| Lucian Adrian Grijincu | c486da3 | 2011-02-24 19:48:03 +0000 | [diff] [blame] | 2775 | table[0].extra1 = net; | 
| Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 2776 | table[1].data = &net->ipv6.ip6_dst_ops.gc_thresh; | 
| YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 2777 | table[2].data = &net->ipv6.sysctl.ip6_rt_max_size; | 
|  | 2778 | table[3].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; | 
|  | 2779 | table[4].data = &net->ipv6.sysctl.ip6_rt_gc_timeout; | 
|  | 2780 | table[5].data = &net->ipv6.sysctl.ip6_rt_gc_interval; | 
|  | 2781 | table[6].data = &net->ipv6.sysctl.ip6_rt_gc_elasticity; | 
|  | 2782 | table[7].data = &net->ipv6.sysctl.ip6_rt_mtu_expires; | 
|  | 2783 | table[8].data = &net->ipv6.sysctl.ip6_rt_min_advmss; | 
| Alexey Dobriyan | 9c69fab | 2009-12-18 20:11:03 -0800 | [diff] [blame] | 2784 | table[9].data = &net->ipv6.sysctl.ip6_rt_gc_min_interval; | 
| YOSHIFUJI Hideaki | 5ee0910 | 2008-02-28 00:24:28 +0900 | [diff] [blame] | 2785 | } | 
|  | 2786 |  | 
| Daniel Lezcano | 760f2d0 | 2008-01-10 02:53:43 -0800 | [diff] [blame] | 2787 | return table; | 
|  | 2788 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | #endif | 
|  | 2790 |  | 
| Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2791 | static int __net_init ip6_route_net_init(struct net *net) | 
| Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 2792 | { | 
| Pavel Emelyanov | 633d424 | 2008-04-21 14:25:23 -0700 | [diff] [blame] | 2793 | int ret = -ENOMEM; | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2794 |  | 
| Alexey Dobriyan | 86393e5 | 2009-08-29 01:34:49 +0000 | [diff] [blame] | 2795 | memcpy(&net->ipv6.ip6_dst_ops, &ip6_dst_ops_template, | 
|  | 2796 | sizeof(net->ipv6.ip6_dst_ops)); | 
| Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 2797 |  | 
| Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2798 | if (dst_entries_init(&net->ipv6.ip6_dst_ops) < 0) | 
|  | 2799 | goto out_ip6_dst_ops; | 
|  | 2800 |  | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2801 | net->ipv6.ip6_null_entry = kmemdup(&ip6_null_entry_template, | 
|  | 2802 | sizeof(*net->ipv6.ip6_null_entry), | 
|  | 2803 | GFP_KERNEL); | 
|  | 2804 | if (!net->ipv6.ip6_null_entry) | 
| Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2805 | goto out_ip6_dst_entries; | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2806 | net->ipv6.ip6_null_entry->dst.path = | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2807 | (struct dst_entry *)net->ipv6.ip6_null_entry; | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2808 | net->ipv6.ip6_null_entry->dst.ops = &net->ipv6.ip6_dst_ops; | 
| David S. Miller | 62fa8a8 | 2011-01-26 20:51:05 -0800 | [diff] [blame] | 2809 | dst_init_metrics(&net->ipv6.ip6_null_entry->dst, | 
|  | 2810 | ip6_template_metrics, true); | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2811 |  | 
|  | 2812 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | 
|  | 2813 | net->ipv6.ip6_prohibit_entry = kmemdup(&ip6_prohibit_entry_template, | 
|  | 2814 | sizeof(*net->ipv6.ip6_prohibit_entry), | 
|  | 2815 | GFP_KERNEL); | 
| Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 2816 | if (!net->ipv6.ip6_prohibit_entry) | 
|  | 2817 | goto out_ip6_null_entry; | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2818 | net->ipv6.ip6_prohibit_entry->dst.path = | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2819 | (struct dst_entry *)net->ipv6.ip6_prohibit_entry; | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2820 | net->ipv6.ip6_prohibit_entry->dst.ops = &net->ipv6.ip6_dst_ops; | 
| David S. Miller | 62fa8a8 | 2011-01-26 20:51:05 -0800 | [diff] [blame] | 2821 | dst_init_metrics(&net->ipv6.ip6_prohibit_entry->dst, | 
|  | 2822 | ip6_template_metrics, true); | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2823 |  | 
|  | 2824 | net->ipv6.ip6_blk_hole_entry = kmemdup(&ip6_blk_hole_entry_template, | 
|  | 2825 | sizeof(*net->ipv6.ip6_blk_hole_entry), | 
|  | 2826 | GFP_KERNEL); | 
| Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 2827 | if (!net->ipv6.ip6_blk_hole_entry) | 
|  | 2828 | goto out_ip6_prohibit_entry; | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2829 | net->ipv6.ip6_blk_hole_entry->dst.path = | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2830 | (struct dst_entry *)net->ipv6.ip6_blk_hole_entry; | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2831 | net->ipv6.ip6_blk_hole_entry->dst.ops = &net->ipv6.ip6_dst_ops; | 
| David S. Miller | 62fa8a8 | 2011-01-26 20:51:05 -0800 | [diff] [blame] | 2832 | dst_init_metrics(&net->ipv6.ip6_blk_hole_entry->dst, | 
|  | 2833 | ip6_template_metrics, true); | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2834 | #endif | 
|  | 2835 |  | 
| Peter Zijlstra | b339a47 | 2008-10-07 14:15:00 -0700 | [diff] [blame] | 2836 | net->ipv6.sysctl.flush_delay = 0; | 
|  | 2837 | net->ipv6.sysctl.ip6_rt_max_size = 4096; | 
|  | 2838 | net->ipv6.sysctl.ip6_rt_gc_min_interval = HZ / 2; | 
|  | 2839 | net->ipv6.sysctl.ip6_rt_gc_timeout = 60*HZ; | 
|  | 2840 | net->ipv6.sysctl.ip6_rt_gc_interval = 30*HZ; | 
|  | 2841 | net->ipv6.sysctl.ip6_rt_gc_elasticity = 9; | 
|  | 2842 | net->ipv6.sysctl.ip6_rt_mtu_expires = 10*60*HZ; | 
|  | 2843 | net->ipv6.sysctl.ip6_rt_min_advmss = IPV6_MIN_MTU - 20 - 40; | 
|  | 2844 |  | 
| Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 2845 | #ifdef CONFIG_PROC_FS | 
|  | 2846 | proc_net_fops_create(net, "ipv6_route", 0, &ipv6_route_proc_fops); | 
|  | 2847 | proc_net_fops_create(net, "rt6_stats", S_IRUGO, &rt6_stats_seq_fops); | 
|  | 2848 | #endif | 
| Benjamin Thery | 6891a34 | 2008-03-04 13:49:47 -0800 | [diff] [blame] | 2849 | net->ipv6.ip6_rt_gc_expire = 30*HZ; | 
|  | 2850 |  | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2851 | ret = 0; | 
|  | 2852 | out: | 
|  | 2853 | return ret; | 
| Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 2854 |  | 
| Peter Zijlstra | 68fffc6 | 2008-10-07 14:12:10 -0700 | [diff] [blame] | 2855 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | 
|  | 2856 | out_ip6_prohibit_entry: | 
|  | 2857 | kfree(net->ipv6.ip6_prohibit_entry); | 
|  | 2858 | out_ip6_null_entry: | 
|  | 2859 | kfree(net->ipv6.ip6_null_entry); | 
|  | 2860 | #endif | 
| Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2861 | out_ip6_dst_entries: | 
|  | 2862 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); | 
| Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 2863 | out_ip6_dst_ops: | 
| Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 2864 | goto out; | 
| Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 2865 | } | 
|  | 2866 |  | 
| Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2867 | static void __net_exit ip6_route_net_exit(struct net *net) | 
| Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 2868 | { | 
|  | 2869 | #ifdef CONFIG_PROC_FS | 
|  | 2870 | proc_net_remove(net, "ipv6_route"); | 
|  | 2871 | proc_net_remove(net, "rt6_stats"); | 
|  | 2872 | #endif | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2873 | kfree(net->ipv6.ip6_null_entry); | 
|  | 2874 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | 
|  | 2875 | kfree(net->ipv6.ip6_prohibit_entry); | 
|  | 2876 | kfree(net->ipv6.ip6_blk_hole_entry); | 
|  | 2877 | #endif | 
| Xiaotian Feng | 41bb78b | 2010-11-02 16:11:05 +0000 | [diff] [blame] | 2878 | dst_entries_destroy(&net->ipv6.ip6_dst_ops); | 
| Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 2879 | } | 
|  | 2880 |  | 
|  | 2881 | static struct pernet_operations ip6_route_net_ops = { | 
|  | 2882 | .init = ip6_route_net_init, | 
|  | 2883 | .exit = ip6_route_net_exit, | 
|  | 2884 | }; | 
|  | 2885 |  | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2886 | static struct notifier_block ip6_route_dev_notifier = { | 
|  | 2887 | .notifier_call = ip6_route_dev_notify, | 
|  | 2888 | .priority = 0, | 
|  | 2889 | }; | 
|  | 2890 |  | 
| Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2891 | int __init ip6_route_init(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2892 | { | 
| Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2893 | int ret; | 
|  | 2894 |  | 
| Daniel Lezcano | 9a7ec3a | 2008-03-04 13:48:53 -0800 | [diff] [blame] | 2895 | ret = -ENOMEM; | 
|  | 2896 | ip6_dst_ops_template.kmem_cachep = | 
|  | 2897 | kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0, | 
|  | 2898 | SLAB_HWCACHE_ALIGN, NULL); | 
|  | 2899 | if (!ip6_dst_ops_template.kmem_cachep) | 
| Fernando Carrijo | c19a28e | 2009-01-07 18:09:08 -0800 | [diff] [blame] | 2900 | goto out; | 
| David S. Miller | 14e50e5 | 2007-05-24 18:17:54 -0700 | [diff] [blame] | 2901 |  | 
| Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2902 | ret = dst_entries_init(&ip6_dst_blackhole_ops); | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2903 | if (ret) | 
| Daniel Lezcano | bdb3289 | 2008-03-04 13:48:10 -0800 | [diff] [blame] | 2904 | goto out_kmem_cache; | 
| Daniel Lezcano | bdb3289 | 2008-03-04 13:48:10 -0800 | [diff] [blame] | 2905 |  | 
| Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2906 | ret = register_pernet_subsys(&ip6_route_net_ops); | 
|  | 2907 | if (ret) | 
|  | 2908 | goto out_dst_entries; | 
|  | 2909 |  | 
| Arnaud Ebalard | 5dc121e | 2008-10-01 02:37:56 -0700 | [diff] [blame] | 2910 | ip6_dst_blackhole_ops.kmem_cachep = ip6_dst_ops_template.kmem_cachep; | 
|  | 2911 |  | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2912 | /* Registering of the loopback is done before this portion of code, | 
|  | 2913 | * the loopback reference in rt6_info will not be taken, do it | 
|  | 2914 | * manually for init_net */ | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2915 | init_net.ipv6.ip6_null_entry->dst.dev = init_net.loopback_dev; | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2916 | init_net.ipv6.ip6_null_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); | 
|  | 2917 | #ifdef CONFIG_IPV6_MULTIPLE_TABLES | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2918 | init_net.ipv6.ip6_prohibit_entry->dst.dev = init_net.loopback_dev; | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2919 | init_net.ipv6.ip6_prohibit_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); | 
| Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 2920 | init_net.ipv6.ip6_blk_hole_entry->dst.dev = init_net.loopback_dev; | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2921 | init_net.ipv6.ip6_blk_hole_entry->rt6i_idev = in6_dev_get(init_net.loopback_dev); | 
|  | 2922 | #endif | 
| Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2923 | ret = fib6_init(); | 
|  | 2924 | if (ret) | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2925 | goto out_register_subsys; | 
| Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2926 |  | 
| Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2927 | ret = xfrm6_init(); | 
|  | 2928 | if (ret) | 
| Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 2929 | goto out_fib6_init; | 
| Daniel Lezcano | c35b7e7 | 2007-12-08 00:14:11 -0800 | [diff] [blame] | 2930 |  | 
| Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2931 | ret = fib6_rules_init(); | 
|  | 2932 | if (ret) | 
|  | 2933 | goto xfrm6_init; | 
| Daniel Lezcano | 7e5449c | 2007-12-08 00:14:54 -0800 | [diff] [blame] | 2934 |  | 
| Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2935 | ret = -ENOBUFS; | 
|  | 2936 | if (__rtnl_register(PF_INET6, RTM_NEWROUTE, inet6_rtm_newroute, NULL) || | 
|  | 2937 | __rtnl_register(PF_INET6, RTM_DELROUTE, inet6_rtm_delroute, NULL) || | 
|  | 2938 | __rtnl_register(PF_INET6, RTM_GETROUTE, inet6_rtm_getroute, NULL)) | 
|  | 2939 | goto fib6_rules_init; | 
|  | 2940 |  | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2941 | ret = register_netdevice_notifier(&ip6_route_dev_notifier); | 
| Daniel Lezcano | cdb1876 | 2008-03-04 13:45:33 -0800 | [diff] [blame] | 2942 | if (ret) | 
|  | 2943 | goto fib6_rules_init; | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2944 |  | 
| Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2945 | out: | 
|  | 2946 | return ret; | 
|  | 2947 |  | 
|  | 2948 | fib6_rules_init: | 
| Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2949 | fib6_rules_cleanup(); | 
|  | 2950 | xfrm6_init: | 
| Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2951 | xfrm6_fini(); | 
| Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2952 | out_fib6_init: | 
| Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2953 | fib6_gc_cleanup(); | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2954 | out_register_subsys: | 
|  | 2955 | unregister_pernet_subsys(&ip6_route_net_ops); | 
| Eric Dumazet | fc66f95 | 2010-10-08 06:37:34 +0000 | [diff] [blame] | 2956 | out_dst_entries: | 
|  | 2957 | dst_entries_destroy(&ip6_dst_blackhole_ops); | 
| Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2958 | out_kmem_cache: | 
| Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 2959 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); | 
| Daniel Lezcano | 433d49c | 2007-12-07 00:43:48 -0800 | [diff] [blame] | 2960 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2961 | } | 
|  | 2962 |  | 
|  | 2963 | void ip6_route_cleanup(void) | 
|  | 2964 | { | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2965 | unregister_netdevice_notifier(&ip6_route_dev_notifier); | 
| Thomas Graf | 101367c | 2006-08-04 03:39:02 -0700 | [diff] [blame] | 2966 | fib6_rules_cleanup(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2967 | xfrm6_fini(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2968 | fib6_gc_cleanup(); | 
| Daniel Lezcano | 8ed6778 | 2008-03-04 13:48:30 -0800 | [diff] [blame] | 2969 | unregister_pernet_subsys(&ip6_route_net_ops); | 
| Xiaotian Feng | 41bb78b | 2010-11-02 16:11:05 +0000 | [diff] [blame] | 2970 | dst_entries_destroy(&ip6_dst_blackhole_ops); | 
| Benjamin Thery | f2fc6a5 | 2008-03-04 13:49:23 -0800 | [diff] [blame] | 2971 | kmem_cache_destroy(ip6_dst_ops_template.kmem_cachep); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2972 | } |