aboutsummaryrefslogtreecommitdiffstats
path: root/net/ipv6/sit.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/ipv6/sit.c')
-rw-r--r--net/ipv6/sit.c456
1 files changed, 346 insertions, 110 deletions
diff --git a/net/ipv6/sit.c b/net/ipv6/sit.c
index 1656c003b989..4b2f1033994e 100644
--- a/net/ipv6/sit.c
+++ b/net/ipv6/sit.c
@@ -16,7 +16,7 @@
16 * Changes: 16 * Changes:
17 * Roger Venning <r.venning@telstra.com>: 6to4 support 17 * Roger Venning <r.venning@telstra.com>: 6to4 support
18 * Nate Thompson <nate@thebog.net>: 6to4 support 18 * Nate Thompson <nate@thebog.net>: 6to4 support
19 * Fred L. Templin <fltemplin@acm.org>: isatap support 19 * Fred Templin <fred.l.templin@boeing.com>: isatap support
20 */ 20 */
21 21
22#include <linux/module.h> 22#include <linux/module.h>
@@ -52,6 +52,8 @@
52#include <net/inet_ecn.h> 52#include <net/inet_ecn.h>
53#include <net/xfrm.h> 53#include <net/xfrm.h>
54#include <net/dsfield.h> 54#include <net/dsfield.h>
55#include <net/net_namespace.h>
56#include <net/netns/generic.h>
55 57
56/* 58/*
57 This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c 59 This version of net/ipv6/sit.c is cloned of net/ipv4/ip_gre.c
@@ -66,41 +68,47 @@ static int ipip6_fb_tunnel_init(struct net_device *dev);
66static int ipip6_tunnel_init(struct net_device *dev); 68static int ipip6_tunnel_init(struct net_device *dev);
67static void ipip6_tunnel_setup(struct net_device *dev); 69static void ipip6_tunnel_setup(struct net_device *dev);
68 70
69static struct net_device *ipip6_fb_tunnel_dev; 71static int sit_net_id;
72struct sit_net {
73 struct ip_tunnel *tunnels_r_l[HASH_SIZE];
74 struct ip_tunnel *tunnels_r[HASH_SIZE];
75 struct ip_tunnel *tunnels_l[HASH_SIZE];
76 struct ip_tunnel *tunnels_wc[1];
77 struct ip_tunnel **tunnels[4];
70 78
71static struct ip_tunnel *tunnels_r_l[HASH_SIZE]; 79 struct net_device *fb_tunnel_dev;
72static struct ip_tunnel *tunnels_r[HASH_SIZE]; 80};
73static struct ip_tunnel *tunnels_l[HASH_SIZE];
74static struct ip_tunnel *tunnels_wc[1];
75static struct ip_tunnel **tunnels[4] = { tunnels_wc, tunnels_l, tunnels_r, tunnels_r_l };
76 81
77static DEFINE_RWLOCK(ipip6_lock); 82static DEFINE_RWLOCK(ipip6_lock);
78 83
79static struct ip_tunnel * ipip6_tunnel_lookup(__be32 remote, __be32 local) 84static struct ip_tunnel * ipip6_tunnel_lookup(struct net *net,
85 __be32 remote, __be32 local)
80{ 86{
81 unsigned h0 = HASH(remote); 87 unsigned h0 = HASH(remote);
82 unsigned h1 = HASH(local); 88 unsigned h1 = HASH(local);
83 struct ip_tunnel *t; 89 struct ip_tunnel *t;
90 struct sit_net *sitn = net_generic(net, sit_net_id);
84 91
85 for (t = tunnels_r_l[h0^h1]; t; t = t->next) { 92 for (t = sitn->tunnels_r_l[h0^h1]; t; t = t->next) {
86 if (local == t->parms.iph.saddr && 93 if (local == t->parms.iph.saddr &&
87 remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP)) 94 remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
88 return t; 95 return t;
89 } 96 }
90 for (t = tunnels_r[h0]; t; t = t->next) { 97 for (t = sitn->tunnels_r[h0]; t; t = t->next) {
91 if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP)) 98 if (remote == t->parms.iph.daddr && (t->dev->flags&IFF_UP))
92 return t; 99 return t;
93 } 100 }
94 for (t = tunnels_l[h1]; t; t = t->next) { 101 for (t = sitn->tunnels_l[h1]; t; t = t->next) {
95 if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP)) 102 if (local == t->parms.iph.saddr && (t->dev->flags&IFF_UP))
96 return t; 103 return t;
97 } 104 }
98 if ((t = tunnels_wc[0]) != NULL && (t->dev->flags&IFF_UP)) 105 if ((t = sitn->tunnels_wc[0]) != NULL && (t->dev->flags&IFF_UP))
99 return t; 106 return t;
100 return NULL; 107 return NULL;
101} 108}
102 109
103static struct ip_tunnel **__ipip6_bucket(struct ip_tunnel_parm *parms) 110static struct ip_tunnel **__ipip6_bucket(struct sit_net *sitn,
111 struct ip_tunnel_parm *parms)
104{ 112{
105 __be32 remote = parms->iph.daddr; 113 __be32 remote = parms->iph.daddr;
106 __be32 local = parms->iph.saddr; 114 __be32 local = parms->iph.saddr;
@@ -115,19 +123,20 @@ static struct ip_tunnel **__ipip6_bucket(struct ip_tunnel_parm *parms)
115 prio |= 1; 123 prio |= 1;
116 h ^= HASH(local); 124 h ^= HASH(local);
117 } 125 }
118 return &tunnels[prio][h]; 126 return &sitn->tunnels[prio][h];
119} 127}
120 128
121static inline struct ip_tunnel **ipip6_bucket(struct ip_tunnel *t) 129static inline struct ip_tunnel **ipip6_bucket(struct sit_net *sitn,
130 struct ip_tunnel *t)
122{ 131{
123 return __ipip6_bucket(&t->parms); 132 return __ipip6_bucket(sitn, &t->parms);
124} 133}
125 134
126static void ipip6_tunnel_unlink(struct ip_tunnel *t) 135static void ipip6_tunnel_unlink(struct sit_net *sitn, struct ip_tunnel *t)
127{ 136{
128 struct ip_tunnel **tp; 137 struct ip_tunnel **tp;
129 138
130 for (tp = ipip6_bucket(t); *tp; tp = &(*tp)->next) { 139 for (tp = ipip6_bucket(sitn, t); *tp; tp = &(*tp)->next) {
131 if (t == *tp) { 140 if (t == *tp) {
132 write_lock_bh(&ipip6_lock); 141 write_lock_bh(&ipip6_lock);
133 *tp = t->next; 142 *tp = t->next;
@@ -137,9 +146,9 @@ static void ipip6_tunnel_unlink(struct ip_tunnel *t)
137 } 146 }
138} 147}
139 148
140static void ipip6_tunnel_link(struct ip_tunnel *t) 149static void ipip6_tunnel_link(struct sit_net *sitn, struct ip_tunnel *t)
141{ 150{
142 struct ip_tunnel **tp = ipip6_bucket(t); 151 struct ip_tunnel **tp = ipip6_bucket(sitn, t);
143 152
144 t->next = *tp; 153 t->next = *tp;
145 write_lock_bh(&ipip6_lock); 154 write_lock_bh(&ipip6_lock);
@@ -147,15 +156,17 @@ static void ipip6_tunnel_link(struct ip_tunnel *t)
147 write_unlock_bh(&ipip6_lock); 156 write_unlock_bh(&ipip6_lock);
148} 157}
149 158
150static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int create) 159static struct ip_tunnel * ipip6_tunnel_locate(struct net *net,
160 struct ip_tunnel_parm *parms, int create)
151{ 161{
152 __be32 remote = parms->iph.daddr; 162 __be32 remote = parms->iph.daddr;
153 __be32 local = parms->iph.saddr; 163 __be32 local = parms->iph.saddr;
154 struct ip_tunnel *t, **tp, *nt; 164 struct ip_tunnel *t, **tp, *nt;
155 struct net_device *dev; 165 struct net_device *dev;
156 char name[IFNAMSIZ]; 166 char name[IFNAMSIZ];
167 struct sit_net *sitn = net_generic(net, sit_net_id);
157 168
158 for (tp = __ipip6_bucket(parms); (t = *tp) != NULL; tp = &t->next) { 169 for (tp = __ipip6_bucket(sitn, parms); (t = *tp) != NULL; tp = &t->next) {
159 if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr) 170 if (local == t->parms.iph.saddr && remote == t->parms.iph.daddr)
160 return t; 171 return t;
161 } 172 }
@@ -171,6 +182,8 @@ static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int
171 if (dev == NULL) 182 if (dev == NULL)
172 return NULL; 183 return NULL;
173 184
185 dev_net_set(dev, net);
186
174 if (strchr(name, '%')) { 187 if (strchr(name, '%')) {
175 if (dev_alloc_name(dev, name) < 0) 188 if (dev_alloc_name(dev, name) < 0)
176 goto failed_free; 189 goto failed_free;
@@ -188,7 +201,7 @@ static struct ip_tunnel * ipip6_tunnel_locate(struct ip_tunnel_parm *parms, int
188 201
189 dev_hold(dev); 202 dev_hold(dev);
190 203
191 ipip6_tunnel_link(nt); 204 ipip6_tunnel_link(sitn, nt);
192 return nt; 205 return nt;
193 206
194failed_free: 207failed_free:
@@ -197,15 +210,192 @@ failed:
197 return NULL; 210 return NULL;
198} 211}
199 212
213static struct ip_tunnel_prl_entry *
214__ipip6_tunnel_locate_prl(struct ip_tunnel *t, __be32 addr)
215{
216 struct ip_tunnel_prl_entry *p = (struct ip_tunnel_prl_entry *)NULL;
217
218 for (p = t->prl; p; p = p->next)
219 if (p->addr == addr)
220 break;
221 return p;
222
223}
224
225static int ipip6_tunnel_get_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
226{
227 struct ip_tunnel_prl *kp;
228 struct ip_tunnel_prl_entry *prl;
229 unsigned int cmax, c = 0, ca, len;
230 int ret = 0;
231
232 cmax = a->datalen / sizeof(*a);
233 if (cmax > 1 && a->addr != htonl(INADDR_ANY))
234 cmax = 1;
235
236 /* For simple GET or for root users,
237 * we try harder to allocate.
238 */
239 kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
240 kcalloc(cmax, sizeof(*kp), GFP_KERNEL) :
241 NULL;
242
243 read_lock(&ipip6_lock);
244
245 ca = t->prl_count < cmax ? t->prl_count : cmax;
246
247 if (!kp) {
248 /* We don't try hard to allocate much memory for
249 * non-root users.
250 * For root users, retry allocating enough memory for
251 * the answer.
252 */
253 kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
254 if (!kp) {
255 ret = -ENOMEM;
256 goto out;
257 }
258 }
259
260 c = 0;
261 for (prl = t->prl; prl; prl = prl->next) {
262 if (c > cmax)
263 break;
264 if (a->addr != htonl(INADDR_ANY) && prl->addr != a->addr)
265 continue;
266 kp[c].addr = prl->addr;
267 kp[c].flags = prl->flags;
268 c++;
269 if (a->addr != htonl(INADDR_ANY))
270 break;
271 }
272out:
273 read_unlock(&ipip6_lock);
274
275 len = sizeof(*kp) * c;
276 ret = len ? copy_to_user(a->data, kp, len) : 0;
277
278 kfree(kp);
279 if (ret)
280 return -EFAULT;
281
282 a->datalen = len;
283 return 0;
284}
285
286static int
287ipip6_tunnel_add_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a, int chg)
288{
289 struct ip_tunnel_prl_entry *p;
290 int err = 0;
291
292 if (a->addr == htonl(INADDR_ANY))
293 return -EINVAL;
294
295 write_lock(&ipip6_lock);
296
297 for (p = t->prl; p; p = p->next) {
298 if (p->addr == a->addr) {
299 if (chg)
300 goto update;
301 err = -EEXIST;
302 goto out;
303 }
304 }
305
306 if (chg) {
307 err = -ENXIO;
308 goto out;
309 }
310
311 p = kzalloc(sizeof(struct ip_tunnel_prl_entry), GFP_KERNEL);
312 if (!p) {
313 err = -ENOBUFS;
314 goto out;
315 }
316
317 p->next = t->prl;
318 t->prl = p;
319 t->prl_count++;
320update:
321 p->addr = a->addr;
322 p->flags = a->flags;
323out:
324 write_unlock(&ipip6_lock);
325 return err;
326}
327
328static int
329ipip6_tunnel_del_prl(struct ip_tunnel *t, struct ip_tunnel_prl *a)
330{
331 struct ip_tunnel_prl_entry *x, **p;
332 int err = 0;
333
334 write_lock(&ipip6_lock);
335
336 if (a && a->addr != htonl(INADDR_ANY)) {
337 for (p = &t->prl; *p; p = &(*p)->next) {
338 if ((*p)->addr == a->addr) {
339 x = *p;
340 *p = x->next;
341 kfree(x);
342 t->prl_count--;
343 goto out;
344 }
345 }
346 err = -ENXIO;
347 } else {
348 while (t->prl) {
349 x = t->prl;
350 t->prl = t->prl->next;
351 kfree(x);
352 t->prl_count--;
353 }
354 }
355out:
356 write_unlock(&ipip6_lock);
357 return 0;
358}
359
360static int
361isatap_chksrc(struct sk_buff *skb, struct iphdr *iph, struct ip_tunnel *t)
362{
363 struct ip_tunnel_prl_entry *p;
364 int ok = 1;
365
366 read_lock(&ipip6_lock);
367 p = __ipip6_tunnel_locate_prl(t, iph->saddr);
368 if (p) {
369 if (p->flags & PRL_DEFAULT)
370 skb->ndisc_nodetype = NDISC_NODETYPE_DEFAULT;
371 else
372 skb->ndisc_nodetype = NDISC_NODETYPE_NODEFAULT;
373 } else {
374 struct in6_addr *addr6 = &ipv6_hdr(skb)->saddr;
375 if (ipv6_addr_is_isatap(addr6) &&
376 (addr6->s6_addr32[3] == iph->saddr) &&
377 ipv6_chk_prefix(addr6, t->dev))
378 skb->ndisc_nodetype = NDISC_NODETYPE_HOST;
379 else
380 ok = 0;
381 }
382 read_unlock(&ipip6_lock);
383 return ok;
384}
385
200static void ipip6_tunnel_uninit(struct net_device *dev) 386static void ipip6_tunnel_uninit(struct net_device *dev)
201{ 387{
202 if (dev == ipip6_fb_tunnel_dev) { 388 struct net *net = dev_net(dev);
389 struct sit_net *sitn = net_generic(net, sit_net_id);
390
391 if (dev == sitn->fb_tunnel_dev) {
203 write_lock_bh(&ipip6_lock); 392 write_lock_bh(&ipip6_lock);
204 tunnels_wc[0] = NULL; 393 sitn->tunnels_wc[0] = NULL;
205 write_unlock_bh(&ipip6_lock); 394 write_unlock_bh(&ipip6_lock);
206 dev_put(dev); 395 dev_put(dev);
207 } else { 396 } else {
208 ipip6_tunnel_unlink(netdev_priv(dev)); 397 ipip6_tunnel_unlink(sitn, netdev_priv(dev));
398 ipip6_tunnel_del_prl(netdev_priv(dev), NULL);
209 dev_put(dev); 399 dev_put(dev);
210 } 400 }
211} 401}
@@ -256,7 +446,7 @@ static int ipip6_err(struct sk_buff *skb, u32 info)
256 err = -ENOENT; 446 err = -ENOENT;
257 447
258 read_lock(&ipip6_lock); 448 read_lock(&ipip6_lock);
259 t = ipip6_tunnel_lookup(iph->daddr, iph->saddr); 449 t = ipip6_tunnel_lookup(dev_net(skb->dev), iph->daddr, iph->saddr);
260 if (t == NULL || t->parms.iph.daddr == 0) 450 if (t == NULL || t->parms.iph.daddr == 0)
261 goto out; 451 goto out;
262 452
@@ -339,11 +529,12 @@ out:
339 skb_reset_network_header(skb2); 529 skb_reset_network_header(skb2);
340 530
341 /* Try to guess incoming interface */ 531 /* Try to guess incoming interface */
342 rt6i = rt6_lookup(&iph6->saddr, NULL, NULL, 0); 532 rt6i = rt6_lookup(dev_net(skb->dev), &iph6->saddr, NULL, NULL, 0);
343 if (rt6i && rt6i->rt6i_dev) { 533 if (rt6i && rt6i->rt6i_dev) {
344 skb2->dev = rt6i->rt6i_dev; 534 skb2->dev = rt6i->rt6i_dev;
345 535
346 rt6i = rt6_lookup(&iph6->daddr, &iph6->saddr, NULL, 0); 536 rt6i = rt6_lookup(dev_net(skb->dev),
537 &iph6->daddr, &iph6->saddr, NULL, 0);
347 538
348 if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) { 539 if (rt6i && rt6i->rt6i_dev && rt6i->rt6i_dev->type == ARPHRD_SIT) {
349 struct ip_tunnel *t = netdev_priv(rt6i->rt6i_dev); 540 struct ip_tunnel *t = netdev_priv(rt6i->rt6i_dev);
@@ -365,48 +556,6 @@ static inline void ipip6_ecn_decapsulate(struct iphdr *iph, struct sk_buff *skb)
365 IP6_ECN_set_ce(ipv6_hdr(skb)); 556 IP6_ECN_set_ce(ipv6_hdr(skb));
366} 557}
367 558
368/* ISATAP (RFC4214) - check source address */
369static int
370isatap_srcok(struct sk_buff *skb, struct iphdr *iph, struct net_device *dev)
371{
372 struct neighbour *neigh;
373 struct dst_entry *dst;
374 struct rt6_info *rt;
375 struct flowi fl;
376 struct in6_addr *addr6;
377 struct in6_addr rtr;
378 struct ipv6hdr *iph6;
379 int ok = 0;
380
381 /* from onlink default router */
382 ipv6_addr_set(&rtr, htonl(0xFE800000), 0, 0, 0);
383 ipv6_isatap_eui64(rtr.s6_addr + 8, iph->saddr);
384 if ((rt = rt6_get_dflt_router(&rtr, dev))) {
385 dst_release(&rt->u.dst);
386 return 1;
387 }
388
389 iph6 = ipv6_hdr(skb);
390 memset(&fl, 0, sizeof(fl));
391 fl.proto = iph6->nexthdr;
392 ipv6_addr_copy(&fl.fl6_dst, &iph6->saddr);
393 fl.oif = dev->ifindex;
394 security_skb_classify_flow(skb, &fl);
395
396 dst = ip6_route_output(NULL, &fl);
397 if (!dst->error && (dst->dev == dev) && (neigh = dst->neighbour)) {
398
399 addr6 = (struct in6_addr*)&neigh->primary_key;
400
401 /* from correct previous hop */
402 if (ipv6_addr_is_isatap(addr6) &&
403 (addr6->s6_addr32[3] == iph->saddr))
404 ok = 1;
405 }
406 dst_release(dst);
407 return ok;
408}
409
410static int ipip6_rcv(struct sk_buff *skb) 559static int ipip6_rcv(struct sk_buff *skb)
411{ 560{
412 struct iphdr *iph; 561 struct iphdr *iph;
@@ -418,7 +567,8 @@ static int ipip6_rcv(struct sk_buff *skb)
418 iph = ip_hdr(skb); 567 iph = ip_hdr(skb);
419 568
420 read_lock(&ipip6_lock); 569 read_lock(&ipip6_lock);
421 if ((tunnel = ipip6_tunnel_lookup(iph->saddr, iph->daddr)) != NULL) { 570 if ((tunnel = ipip6_tunnel_lookup(dev_net(skb->dev),
571 iph->saddr, iph->daddr)) != NULL) {
422 secpath_reset(skb); 572 secpath_reset(skb);
423 skb->mac_header = skb->network_header; 573 skb->mac_header = skb->network_header;
424 skb_reset_network_header(skb); 574 skb_reset_network_header(skb);
@@ -427,7 +577,7 @@ static int ipip6_rcv(struct sk_buff *skb)
427 skb->pkt_type = PACKET_HOST; 577 skb->pkt_type = PACKET_HOST;
428 578
429 if ((tunnel->dev->priv_flags & IFF_ISATAP) && 579 if ((tunnel->dev->priv_flags & IFF_ISATAP) &&
430 !isatap_srcok(skb, iph, tunnel->dev)) { 580 !isatap_chksrc(skb, iph, tunnel)) {
431 tunnel->stat.rx_errors++; 581 tunnel->stat.rx_errors++;
432 read_unlock(&ipip6_lock); 582 read_unlock(&ipip6_lock);
433 kfree_skb(skb); 583 kfree_skb(skb);
@@ -554,7 +704,7 @@ static int ipip6_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
554 .tos = RT_TOS(tos) } }, 704 .tos = RT_TOS(tos) } },
555 .oif = tunnel->parms.link, 705 .oif = tunnel->parms.link,
556 .proto = IPPROTO_IPV6 }; 706 .proto = IPPROTO_IPV6 };
557 if (ip_route_output_key(&init_net, &rt, &fl)) { 707 if (ip_route_output_key(dev_net(dev), &rt, &fl)) {
558 tunnel->stat.tx_carrier_errors++; 708 tunnel->stat.tx_carrier_errors++;
559 goto tx_error_icmp; 709 goto tx_error_icmp;
560 } 710 }
@@ -683,7 +833,7 @@ static void ipip6_tunnel_bind_dev(struct net_device *dev)
683 .oif = tunnel->parms.link, 833 .oif = tunnel->parms.link,
684 .proto = IPPROTO_IPV6 }; 834 .proto = IPPROTO_IPV6 };
685 struct rtable *rt; 835 struct rtable *rt;
686 if (!ip_route_output_key(&init_net, &rt, &fl)) { 836 if (!ip_route_output_key(dev_net(dev), &rt, &fl)) {
687 tdev = rt->u.dst.dev; 837 tdev = rt->u.dst.dev;
688 ip_rt_put(rt); 838 ip_rt_put(rt);
689 } 839 }
@@ -691,7 +841,7 @@ static void ipip6_tunnel_bind_dev(struct net_device *dev)
691 } 841 }
692 842
693 if (!tdev && tunnel->parms.link) 843 if (!tdev && tunnel->parms.link)
694 tdev = __dev_get_by_index(&init_net, tunnel->parms.link); 844 tdev = __dev_get_by_index(dev_net(dev), tunnel->parms.link);
695 845
696 if (tdev) { 846 if (tdev) {
697 dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr); 847 dev->hard_header_len = tdev->hard_header_len + sizeof(struct iphdr);
@@ -707,17 +857,20 @@ ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
707{ 857{
708 int err = 0; 858 int err = 0;
709 struct ip_tunnel_parm p; 859 struct ip_tunnel_parm p;
860 struct ip_tunnel_prl prl;
710 struct ip_tunnel *t; 861 struct ip_tunnel *t;
862 struct net *net = dev_net(dev);
863 struct sit_net *sitn = net_generic(net, sit_net_id);
711 864
712 switch (cmd) { 865 switch (cmd) {
713 case SIOCGETTUNNEL: 866 case SIOCGETTUNNEL:
714 t = NULL; 867 t = NULL;
715 if (dev == ipip6_fb_tunnel_dev) { 868 if (dev == sitn->fb_tunnel_dev) {
716 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) { 869 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
717 err = -EFAULT; 870 err = -EFAULT;
718 break; 871 break;
719 } 872 }
720 t = ipip6_tunnel_locate(&p, 0); 873 t = ipip6_tunnel_locate(net, &p, 0);
721 } 874 }
722 if (t == NULL) 875 if (t == NULL)
723 t = netdev_priv(dev); 876 t = netdev_priv(dev);
@@ -743,9 +896,9 @@ ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
743 if (p.iph.ttl) 896 if (p.iph.ttl)
744 p.iph.frag_off |= htons(IP_DF); 897 p.iph.frag_off |= htons(IP_DF);
745 898
746 t = ipip6_tunnel_locate(&p, cmd == SIOCADDTUNNEL); 899 t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
747 900
748 if (dev != ipip6_fb_tunnel_dev && cmd == SIOCCHGTUNNEL) { 901 if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
749 if (t != NULL) { 902 if (t != NULL) {
750 if (t->dev != dev) { 903 if (t->dev != dev) {
751 err = -EEXIST; 904 err = -EEXIST;
@@ -758,12 +911,12 @@ ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
758 break; 911 break;
759 } 912 }
760 t = netdev_priv(dev); 913 t = netdev_priv(dev);
761 ipip6_tunnel_unlink(t); 914 ipip6_tunnel_unlink(sitn, t);
762 t->parms.iph.saddr = p.iph.saddr; 915 t->parms.iph.saddr = p.iph.saddr;
763 t->parms.iph.daddr = p.iph.daddr; 916 t->parms.iph.daddr = p.iph.daddr;
764 memcpy(dev->dev_addr, &p.iph.saddr, 4); 917 memcpy(dev->dev_addr, &p.iph.saddr, 4);
765 memcpy(dev->broadcast, &p.iph.daddr, 4); 918 memcpy(dev->broadcast, &p.iph.daddr, 4);
766 ipip6_tunnel_link(t); 919 ipip6_tunnel_link(sitn, t);
767 netdev_state_change(dev); 920 netdev_state_change(dev);
768 } 921 }
769 } 922 }
@@ -790,15 +943,15 @@ ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
790 if (!capable(CAP_NET_ADMIN)) 943 if (!capable(CAP_NET_ADMIN))
791 goto done; 944 goto done;
792 945
793 if (dev == ipip6_fb_tunnel_dev) { 946 if (dev == sitn->fb_tunnel_dev) {
794 err = -EFAULT; 947 err = -EFAULT;
795 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) 948 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
796 goto done; 949 goto done;
797 err = -ENOENT; 950 err = -ENOENT;
798 if ((t = ipip6_tunnel_locate(&p, 0)) == NULL) 951 if ((t = ipip6_tunnel_locate(net, &p, 0)) == NULL)
799 goto done; 952 goto done;
800 err = -EPERM; 953 err = -EPERM;
801 if (t == netdev_priv(ipip6_fb_tunnel_dev)) 954 if (t == netdev_priv(sitn->fb_tunnel_dev))
802 goto done; 955 goto done;
803 dev = t->dev; 956 dev = t->dev;
804 } 957 }
@@ -806,6 +959,42 @@ ipip6_tunnel_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd)
806 err = 0; 959 err = 0;
807 break; 960 break;
808 961
962 case SIOCGETPRL:
963 case SIOCADDPRL:
964 case SIOCDELPRL:
965 case SIOCCHGPRL:
966 err = -EPERM;
967 if (cmd != SIOCGETPRL && !capable(CAP_NET_ADMIN))
968 goto done;
969 err = -EINVAL;
970 if (dev == sitn->fb_tunnel_dev)
971 goto done;
972 err = -EFAULT;
973 if (copy_from_user(&prl, ifr->ifr_ifru.ifru_data, sizeof(prl)))
974 goto done;
975 err = -ENOENT;
976 if (!(t = netdev_priv(dev)))
977 goto done;
978
979 switch (cmd) {
980 case SIOCGETPRL:
981 err = ipip6_tunnel_get_prl(t, &prl);
982 if (!err && copy_to_user(ifr->ifr_ifru.ifru_data,
983 &prl, sizeof(prl)))
984 err = -EFAULT;
985 break;
986 case SIOCDELPRL:
987 err = ipip6_tunnel_del_prl(t, &prl);
988 break;
989 case SIOCADDPRL:
990 case SIOCCHGPRL:
991 err = ipip6_tunnel_add_prl(t, &prl, cmd == SIOCCHGPRL);
992 break;
993 }
994 if (cmd != SIOCGETPRL)
995 netdev_state_change(dev);
996 break;
997
809 default: 998 default:
810 err = -EINVAL; 999 err = -EINVAL;
811 } 1000 }
@@ -842,6 +1031,7 @@ static void ipip6_tunnel_setup(struct net_device *dev)
842 dev->flags = IFF_NOARP; 1031 dev->flags = IFF_NOARP;
843 dev->iflink = 0; 1032 dev->iflink = 0;
844 dev->addr_len = 4; 1033 dev->addr_len = 4;
1034 dev->features |= NETIF_F_NETNS_LOCAL;
845} 1035}
846 1036
847static int ipip6_tunnel_init(struct net_device *dev) 1037static int ipip6_tunnel_init(struct net_device *dev)
@@ -861,10 +1051,12 @@ static int ipip6_tunnel_init(struct net_device *dev)
861 return 0; 1051 return 0;
862} 1052}
863 1053
864static int __init ipip6_fb_tunnel_init(struct net_device *dev) 1054static int ipip6_fb_tunnel_init(struct net_device *dev)
865{ 1055{
866 struct ip_tunnel *tunnel = netdev_priv(dev); 1056 struct ip_tunnel *tunnel = netdev_priv(dev);
867 struct iphdr *iph = &tunnel->parms.iph; 1057 struct iphdr *iph = &tunnel->parms.iph;
1058 struct net *net = dev_net(dev);
1059 struct sit_net *sitn = net_generic(net, sit_net_id);
868 1060
869 tunnel->dev = dev; 1061 tunnel->dev = dev;
870 strcpy(tunnel->parms.name, dev->name); 1062 strcpy(tunnel->parms.name, dev->name);
@@ -875,7 +1067,7 @@ static int __init ipip6_fb_tunnel_init(struct net_device *dev)
875 iph->ttl = 64; 1067 iph->ttl = 64;
876 1068
877 dev_hold(dev); 1069 dev_hold(dev);
878 tunnels_wc[0] = tunnel; 1070 sitn->tunnels_wc[0] = tunnel;
879 return 0; 1071 return 0;
880} 1072}
881 1073
@@ -885,7 +1077,7 @@ static struct xfrm_tunnel sit_handler = {
885 .priority = 1, 1077 .priority = 1,
886}; 1078};
887 1079
888static void __exit sit_destroy_tunnels(void) 1080static void sit_destroy_tunnels(struct sit_net *sitn)
889{ 1081{
890 int prio; 1082 int prio;
891 1083
@@ -893,20 +1085,78 @@ static void __exit sit_destroy_tunnels(void)
893 int h; 1085 int h;
894 for (h = 0; h < HASH_SIZE; h++) { 1086 for (h = 0; h < HASH_SIZE; h++) {
895 struct ip_tunnel *t; 1087 struct ip_tunnel *t;
896 while ((t = tunnels[prio][h]) != NULL) 1088 while ((t = sitn->tunnels[prio][h]) != NULL)
897 unregister_netdevice(t->dev); 1089 unregister_netdevice(t->dev);
898 } 1090 }
899 } 1091 }
900} 1092}
901 1093
902static void __exit sit_cleanup(void) 1094static int sit_init_net(struct net *net)
903{ 1095{
904 xfrm4_tunnel_deregister(&sit_handler, AF_INET6); 1096 int err;
1097 struct sit_net *sitn;
1098
1099 err = -ENOMEM;
1100 sitn = kzalloc(sizeof(struct sit_net), GFP_KERNEL);
1101 if (sitn == NULL)
1102 goto err_alloc;
1103
1104 err = net_assign_generic(net, sit_net_id, sitn);
1105 if (err < 0)
1106 goto err_assign;
1107
1108 sitn->tunnels[0] = sitn->tunnels_wc;
1109 sitn->tunnels[1] = sitn->tunnels_l;
1110 sitn->tunnels[2] = sitn->tunnels_r;
1111 sitn->tunnels[3] = sitn->tunnels_r_l;
1112
1113 sitn->fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0",
1114 ipip6_tunnel_setup);
1115 if (!sitn->fb_tunnel_dev) {
1116 err = -ENOMEM;
1117 goto err_alloc_dev;
1118 }
1119
1120 sitn->fb_tunnel_dev->init = ipip6_fb_tunnel_init;
1121 dev_net_set(sitn->fb_tunnel_dev, net);
1122
1123 if ((err = register_netdev(sitn->fb_tunnel_dev)))
1124 goto err_reg_dev;
905 1125
1126 return 0;
1127
1128err_reg_dev:
1129 free_netdev(sitn->fb_tunnel_dev);
1130err_alloc_dev:
1131 /* nothing */
1132err_assign:
1133 kfree(sitn);
1134err_alloc:
1135 return err;
1136}
1137
1138static void sit_exit_net(struct net *net)
1139{
1140 struct sit_net *sitn;
1141
1142 sitn = net_generic(net, sit_net_id);
906 rtnl_lock(); 1143 rtnl_lock();
907 sit_destroy_tunnels(); 1144 sit_destroy_tunnels(sitn);
908 unregister_netdevice(ipip6_fb_tunnel_dev); 1145 unregister_netdevice(sitn->fb_tunnel_dev);
909 rtnl_unlock(); 1146 rtnl_unlock();
1147 kfree(sitn);
1148}
1149
1150static struct pernet_operations sit_net_ops = {
1151 .init = sit_init_net,
1152 .exit = sit_exit_net,
1153};
1154
1155static void __exit sit_cleanup(void)
1156{
1157 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
1158
1159 unregister_pernet_gen_device(sit_net_id, &sit_net_ops);
910} 1160}
911 1161
912static int __init sit_init(void) 1162static int __init sit_init(void)
@@ -920,25 +1170,11 @@ static int __init sit_init(void)
920 return -EAGAIN; 1170 return -EAGAIN;
921 } 1171 }
922 1172
923 ipip6_fb_tunnel_dev = alloc_netdev(sizeof(struct ip_tunnel), "sit0", 1173 err = register_pernet_gen_device(&sit_net_id, &sit_net_ops);
924 ipip6_tunnel_setup); 1174 if (err < 0)
925 if (!ipip6_fb_tunnel_dev) { 1175 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
926 err = -ENOMEM;
927 goto err1;
928 }
929
930 ipip6_fb_tunnel_dev->init = ipip6_fb_tunnel_init;
931 1176
932 if ((err = register_netdev(ipip6_fb_tunnel_dev)))
933 goto err2;
934
935 out:
936 return err; 1177 return err;
937 err2:
938 free_netdev(ipip6_fb_tunnel_dev);
939 err1:
940 xfrm4_tunnel_deregister(&sit_handler, AF_INET6);
941 goto out;
942} 1178}
943 1179
944module_init(sit_init); 1180module_init(sit_init);