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