]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * IPv6 over IPv6 tunnel device | |
3 | * Linux INET6 implementation | |
4 | * | |
5 | * Authors: | |
6 | * Ville Nuorvala <vnuorval@tcs.hut.fi> | |
7 | * | |
8 | * $Id$ | |
9 | * | |
10 | * Based on: | |
11 | * linux/net/ipv6/sit.c | |
12 | * | |
13 | * RFC 2473 | |
14 | * | |
15 | * This program is free software; you can redistribute it and/or | |
16 | * modify it under the terms of the GNU General Public License | |
17 | * as published by the Free Software Foundation; either version | |
18 | * 2 of the License, or (at your option) any later version. | |
19 | * | |
20 | */ | |
21 | ||
22 | #include <linux/config.h> | |
23 | #include <linux/module.h> | |
4fc268d2 | 24 | #include <linux/capability.h> |
1da177e4 LT |
25 | #include <linux/errno.h> |
26 | #include <linux/types.h> | |
27 | #include <linux/sockios.h> | |
28 | #include <linux/if.h> | |
29 | #include <linux/in.h> | |
30 | #include <linux/ip.h> | |
31 | #include <linux/if_tunnel.h> | |
32 | #include <linux/net.h> | |
33 | #include <linux/in6.h> | |
34 | #include <linux/netdevice.h> | |
35 | #include <linux/if_arp.h> | |
36 | #include <linux/icmpv6.h> | |
37 | #include <linux/init.h> | |
38 | #include <linux/route.h> | |
39 | #include <linux/rtnetlink.h> | |
40 | #include <linux/netfilter_ipv6.h> | |
41 | ||
42 | #include <asm/uaccess.h> | |
43 | #include <asm/atomic.h> | |
44 | ||
45 | #include <net/ip.h> | |
46 | #include <net/ipv6.h> | |
47 | #include <net/protocol.h> | |
48 | #include <net/ip6_route.h> | |
49 | #include <net/addrconf.h> | |
50 | #include <net/ip6_tunnel.h> | |
51 | #include <net/xfrm.h> | |
52 | #include <net/dsfield.h> | |
53 | #include <net/inet_ecn.h> | |
54 | ||
55 | MODULE_AUTHOR("Ville Nuorvala"); | |
56 | MODULE_DESCRIPTION("IPv6-in-IPv6 tunnel"); | |
57 | MODULE_LICENSE("GPL"); | |
58 | ||
59 | #define IPV6_TLV_TEL_DST_SIZE 8 | |
60 | ||
61 | #ifdef IP6_TNL_DEBUG | |
62 | #define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __FUNCTION__) | |
63 | #else | |
64 | #define IP6_TNL_TRACE(x...) do {;} while(0) | |
65 | #endif | |
66 | ||
67 | #define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK) | |
68 | ||
69 | #define HASH_SIZE 32 | |
70 | ||
71 | #define HASH(addr) (((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \ | |
72 | (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \ | |
73 | (HASH_SIZE - 1)) | |
74 | ||
75 | static int ip6ip6_fb_tnl_dev_init(struct net_device *dev); | |
76 | static int ip6ip6_tnl_dev_init(struct net_device *dev); | |
77 | static void ip6ip6_tnl_dev_setup(struct net_device *dev); | |
78 | ||
79 | /* the IPv6 tunnel fallback device */ | |
80 | static struct net_device *ip6ip6_fb_tnl_dev; | |
81 | ||
82 | ||
83 | /* lists for storing tunnels in use */ | |
84 | static struct ip6_tnl *tnls_r_l[HASH_SIZE]; | |
85 | static struct ip6_tnl *tnls_wc[1]; | |
86 | static struct ip6_tnl **tnls[2] = { tnls_wc, tnls_r_l }; | |
87 | ||
88 | /* lock for the tunnel lists */ | |
89 | static DEFINE_RWLOCK(ip6ip6_lock); | |
90 | ||
91 | static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t) | |
92 | { | |
93 | struct dst_entry *dst = t->dst_cache; | |
94 | ||
95 | if (dst && dst->obsolete && | |
96 | dst->ops->check(dst, t->dst_cookie) == NULL) { | |
97 | t->dst_cache = NULL; | |
98 | dst_release(dst); | |
99 | return NULL; | |
100 | } | |
101 | ||
102 | return dst; | |
103 | } | |
104 | ||
105 | static inline void ip6_tnl_dst_reset(struct ip6_tnl *t) | |
106 | { | |
107 | dst_release(t->dst_cache); | |
108 | t->dst_cache = NULL; | |
109 | } | |
110 | ||
111 | static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst) | |
112 | { | |
113 | struct rt6_info *rt = (struct rt6_info *) dst; | |
114 | t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0; | |
115 | dst_release(t->dst_cache); | |
116 | t->dst_cache = dst; | |
117 | } | |
118 | ||
119 | /** | |
120 | * ip6ip6_tnl_lookup - fetch tunnel matching the end-point addresses | |
121 | * @remote: the address of the tunnel exit-point | |
122 | * @local: the address of the tunnel entry-point | |
123 | * | |
124 | * Return: | |
125 | * tunnel matching given end-points if found, | |
126 | * else fallback tunnel if its device is up, | |
127 | * else %NULL | |
128 | **/ | |
129 | ||
130 | static struct ip6_tnl * | |
131 | ip6ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local) | |
132 | { | |
133 | unsigned h0 = HASH(remote); | |
134 | unsigned h1 = HASH(local); | |
135 | struct ip6_tnl *t; | |
136 | ||
137 | for (t = tnls_r_l[h0 ^ h1]; t; t = t->next) { | |
138 | if (ipv6_addr_equal(local, &t->parms.laddr) && | |
139 | ipv6_addr_equal(remote, &t->parms.raddr) && | |
140 | (t->dev->flags & IFF_UP)) | |
141 | return t; | |
142 | } | |
143 | if ((t = tnls_wc[0]) != NULL && (t->dev->flags & IFF_UP)) | |
144 | return t; | |
145 | ||
146 | return NULL; | |
147 | } | |
148 | ||
149 | /** | |
150 | * ip6ip6_bucket - get head of list matching given tunnel parameters | |
151 | * @p: parameters containing tunnel end-points | |
152 | * | |
153 | * Description: | |
154 | * ip6ip6_bucket() returns the head of the list matching the | |
155 | * &struct in6_addr entries laddr and raddr in @p. | |
156 | * | |
157 | * Return: head of IPv6 tunnel list | |
158 | **/ | |
159 | ||
160 | static struct ip6_tnl ** | |
161 | ip6ip6_bucket(struct ip6_tnl_parm *p) | |
162 | { | |
163 | struct in6_addr *remote = &p->raddr; | |
164 | struct in6_addr *local = &p->laddr; | |
165 | unsigned h = 0; | |
166 | int prio = 0; | |
167 | ||
168 | if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) { | |
169 | prio = 1; | |
170 | h = HASH(remote) ^ HASH(local); | |
171 | } | |
172 | return &tnls[prio][h]; | |
173 | } | |
174 | ||
175 | /** | |
176 | * ip6ip6_tnl_link - add tunnel to hash table | |
177 | * @t: tunnel to be added | |
178 | **/ | |
179 | ||
180 | static void | |
181 | ip6ip6_tnl_link(struct ip6_tnl *t) | |
182 | { | |
183 | struct ip6_tnl **tp = ip6ip6_bucket(&t->parms); | |
184 | ||
185 | t->next = *tp; | |
186 | write_lock_bh(&ip6ip6_lock); | |
187 | *tp = t; | |
188 | write_unlock_bh(&ip6ip6_lock); | |
189 | } | |
190 | ||
191 | /** | |
192 | * ip6ip6_tnl_unlink - remove tunnel from hash table | |
193 | * @t: tunnel to be removed | |
194 | **/ | |
195 | ||
196 | static void | |
197 | ip6ip6_tnl_unlink(struct ip6_tnl *t) | |
198 | { | |
199 | struct ip6_tnl **tp; | |
200 | ||
201 | for (tp = ip6ip6_bucket(&t->parms); *tp; tp = &(*tp)->next) { | |
202 | if (t == *tp) { | |
203 | write_lock_bh(&ip6ip6_lock); | |
204 | *tp = t->next; | |
205 | write_unlock_bh(&ip6ip6_lock); | |
206 | break; | |
207 | } | |
208 | } | |
209 | } | |
210 | ||
211 | /** | |
212 | * ip6_tnl_create() - create a new tunnel | |
213 | * @p: tunnel parameters | |
214 | * @pt: pointer to new tunnel | |
215 | * | |
216 | * Description: | |
217 | * Create tunnel matching given parameters. | |
218 | * | |
219 | * Return: | |
220 | * 0 on success | |
221 | **/ | |
222 | ||
223 | static int | |
224 | ip6_tnl_create(struct ip6_tnl_parm *p, struct ip6_tnl **pt) | |
225 | { | |
226 | struct net_device *dev; | |
227 | struct ip6_tnl *t; | |
228 | char name[IFNAMSIZ]; | |
229 | int err; | |
230 | ||
231 | if (p->name[0]) { | |
232 | strlcpy(name, p->name, IFNAMSIZ); | |
233 | } else { | |
234 | int i; | |
235 | for (i = 1; i < IP6_TNL_MAX; i++) { | |
236 | sprintf(name, "ip6tnl%d", i); | |
237 | if (__dev_get_by_name(name) == NULL) | |
238 | break; | |
239 | } | |
240 | if (i == IP6_TNL_MAX) | |
241 | return -ENOBUFS; | |
242 | } | |
243 | dev = alloc_netdev(sizeof (*t), name, ip6ip6_tnl_dev_setup); | |
244 | if (dev == NULL) | |
245 | return -ENOMEM; | |
246 | ||
2941a486 | 247 | t = netdev_priv(dev); |
1da177e4 LT |
248 | dev->init = ip6ip6_tnl_dev_init; |
249 | t->parms = *p; | |
250 | ||
251 | if ((err = register_netdevice(dev)) < 0) { | |
252 | free_netdev(dev); | |
253 | return err; | |
254 | } | |
255 | dev_hold(dev); | |
256 | ||
257 | ip6ip6_tnl_link(t); | |
258 | *pt = t; | |
259 | return 0; | |
260 | } | |
261 | ||
262 | /** | |
263 | * ip6ip6_tnl_locate - find or create tunnel matching given parameters | |
264 | * @p: tunnel parameters | |
265 | * @create: != 0 if allowed to create new tunnel if no match found | |
266 | * | |
267 | * Description: | |
268 | * ip6ip6_tnl_locate() first tries to locate an existing tunnel | |
269 | * based on @parms. If this is unsuccessful, but @create is set a new | |
270 | * tunnel device is created and registered for use. | |
271 | * | |
272 | * Return: | |
273 | * 0 if tunnel located or created, | |
274 | * -EINVAL if parameters incorrect, | |
275 | * -ENODEV if no matching tunnel available | |
276 | **/ | |
277 | ||
278 | static int | |
279 | ip6ip6_tnl_locate(struct ip6_tnl_parm *p, struct ip6_tnl **pt, int create) | |
280 | { | |
281 | struct in6_addr *remote = &p->raddr; | |
282 | struct in6_addr *local = &p->laddr; | |
283 | struct ip6_tnl *t; | |
284 | ||
285 | if (p->proto != IPPROTO_IPV6) | |
286 | return -EINVAL; | |
287 | ||
288 | for (t = *ip6ip6_bucket(p); t; t = t->next) { | |
289 | if (ipv6_addr_equal(local, &t->parms.laddr) && | |
290 | ipv6_addr_equal(remote, &t->parms.raddr)) { | |
291 | *pt = t; | |
292 | return (create ? -EEXIST : 0); | |
293 | } | |
294 | } | |
295 | if (!create) | |
296 | return -ENODEV; | |
297 | ||
298 | return ip6_tnl_create(p, pt); | |
299 | } | |
300 | ||
301 | /** | |
302 | * ip6ip6_tnl_dev_uninit - tunnel device uninitializer | |
303 | * @dev: the device to be destroyed | |
304 | * | |
305 | * Description: | |
306 | * ip6ip6_tnl_dev_uninit() removes tunnel from its list | |
307 | **/ | |
308 | ||
309 | static void | |
310 | ip6ip6_tnl_dev_uninit(struct net_device *dev) | |
311 | { | |
2941a486 | 312 | struct ip6_tnl *t = netdev_priv(dev); |
1da177e4 LT |
313 | |
314 | if (dev == ip6ip6_fb_tnl_dev) { | |
315 | write_lock_bh(&ip6ip6_lock); | |
316 | tnls_wc[0] = NULL; | |
317 | write_unlock_bh(&ip6ip6_lock); | |
318 | } else { | |
319 | ip6ip6_tnl_unlink(t); | |
320 | } | |
321 | ip6_tnl_dst_reset(t); | |
322 | dev_put(dev); | |
323 | } | |
324 | ||
325 | /** | |
326 | * parse_tvl_tnl_enc_lim - handle encapsulation limit option | |
327 | * @skb: received socket buffer | |
328 | * | |
329 | * Return: | |
330 | * 0 if none was found, | |
331 | * else index to encapsulation limit | |
332 | **/ | |
333 | ||
334 | static __u16 | |
335 | parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw) | |
336 | { | |
337 | struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw; | |
338 | __u8 nexthdr = ipv6h->nexthdr; | |
339 | __u16 off = sizeof (*ipv6h); | |
340 | ||
341 | while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) { | |
342 | __u16 optlen = 0; | |
343 | struct ipv6_opt_hdr *hdr; | |
344 | if (raw + off + sizeof (*hdr) > skb->data && | |
345 | !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr))) | |
346 | break; | |
347 | ||
348 | hdr = (struct ipv6_opt_hdr *) (raw + off); | |
349 | if (nexthdr == NEXTHDR_FRAGMENT) { | |
350 | struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr; | |
351 | if (frag_hdr->frag_off) | |
352 | break; | |
353 | optlen = 8; | |
354 | } else if (nexthdr == NEXTHDR_AUTH) { | |
355 | optlen = (hdr->hdrlen + 2) << 2; | |
356 | } else { | |
357 | optlen = ipv6_optlen(hdr); | |
358 | } | |
359 | if (nexthdr == NEXTHDR_DEST) { | |
360 | __u16 i = off + 2; | |
361 | while (1) { | |
362 | struct ipv6_tlv_tnl_enc_lim *tel; | |
363 | ||
364 | /* No more room for encapsulation limit */ | |
365 | if (i + sizeof (*tel) > off + optlen) | |
366 | break; | |
367 | ||
368 | tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i]; | |
369 | /* return index of option if found and valid */ | |
370 | if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT && | |
371 | tel->length == 1) | |
372 | return i; | |
373 | /* else jump to next option */ | |
374 | if (tel->type) | |
375 | i += tel->length + 2; | |
376 | else | |
377 | i++; | |
378 | } | |
379 | } | |
380 | nexthdr = hdr->nexthdr; | |
381 | off += optlen; | |
382 | } | |
383 | return 0; | |
384 | } | |
385 | ||
386 | /** | |
387 | * ip6ip6_err - tunnel error handler | |
388 | * | |
389 | * Description: | |
390 | * ip6ip6_err() should handle errors in the tunnel according | |
391 | * to the specifications in RFC 2473. | |
392 | **/ | |
393 | ||
394 | static void | |
395 | ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, | |
396 | int type, int code, int offset, __u32 info) | |
397 | { | |
398 | struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data; | |
399 | struct ip6_tnl *t; | |
400 | int rel_msg = 0; | |
401 | int rel_type = ICMPV6_DEST_UNREACH; | |
402 | int rel_code = ICMPV6_ADDR_UNREACH; | |
403 | __u32 rel_info = 0; | |
404 | __u16 len; | |
405 | ||
406 | /* If the packet doesn't contain the original IPv6 header we are | |
407 | in trouble since we might need the source address for further | |
408 | processing of the error. */ | |
409 | ||
410 | read_lock(&ip6ip6_lock); | |
411 | if ((t = ip6ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL) | |
412 | goto out; | |
413 | ||
414 | switch (type) { | |
415 | __u32 teli; | |
416 | struct ipv6_tlv_tnl_enc_lim *tel; | |
417 | __u32 mtu; | |
418 | case ICMPV6_DEST_UNREACH: | |
419 | if (net_ratelimit()) | |
420 | printk(KERN_WARNING | |
421 | "%s: Path to destination invalid " | |
422 | "or inactive!\n", t->parms.name); | |
423 | rel_msg = 1; | |
424 | break; | |
425 | case ICMPV6_TIME_EXCEED: | |
426 | if (code == ICMPV6_EXC_HOPLIMIT) { | |
427 | if (net_ratelimit()) | |
428 | printk(KERN_WARNING | |
429 | "%s: Too small hop limit or " | |
430 | "routing loop in tunnel!\n", | |
431 | t->parms.name); | |
432 | rel_msg = 1; | |
433 | } | |
434 | break; | |
435 | case ICMPV6_PARAMPROB: | |
436 | /* ignore if parameter problem not caused by a tunnel | |
437 | encapsulation limit sub-option */ | |
438 | if (code != ICMPV6_HDR_FIELD) { | |
439 | break; | |
440 | } | |
441 | teli = parse_tlv_tnl_enc_lim(skb, skb->data); | |
442 | ||
443 | if (teli && teli == ntohl(info) - 2) { | |
444 | tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli]; | |
445 | if (tel->encap_limit == 0) { | |
446 | if (net_ratelimit()) | |
447 | printk(KERN_WARNING | |
448 | "%s: Too small encapsulation " | |
449 | "limit or routing loop in " | |
450 | "tunnel!\n", t->parms.name); | |
451 | rel_msg = 1; | |
452 | } | |
453 | } | |
454 | break; | |
455 | case ICMPV6_PKT_TOOBIG: | |
456 | mtu = ntohl(info) - offset; | |
457 | if (mtu < IPV6_MIN_MTU) | |
458 | mtu = IPV6_MIN_MTU; | |
459 | t->dev->mtu = mtu; | |
460 | ||
461 | if ((len = sizeof (*ipv6h) + ipv6h->payload_len) > mtu) { | |
462 | rel_type = ICMPV6_PKT_TOOBIG; | |
463 | rel_code = 0; | |
464 | rel_info = mtu; | |
465 | rel_msg = 1; | |
466 | } | |
467 | break; | |
468 | } | |
469 | if (rel_msg && pskb_may_pull(skb, offset + sizeof (*ipv6h))) { | |
470 | struct rt6_info *rt; | |
471 | struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); | |
472 | if (!skb2) | |
473 | goto out; | |
474 | ||
475 | dst_release(skb2->dst); | |
476 | skb2->dst = NULL; | |
477 | skb_pull(skb2, offset); | |
478 | skb2->nh.raw = skb2->data; | |
479 | ||
480 | /* Try to guess incoming interface */ | |
481 | rt = rt6_lookup(&skb2->nh.ipv6h->saddr, NULL, 0, 0); | |
482 | ||
483 | if (rt && rt->rt6i_dev) | |
484 | skb2->dev = rt->rt6i_dev; | |
485 | ||
486 | icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev); | |
487 | ||
488 | if (rt) | |
489 | dst_release(&rt->u.dst); | |
490 | ||
491 | kfree_skb(skb2); | |
492 | } | |
493 | out: | |
494 | read_unlock(&ip6ip6_lock); | |
495 | } | |
496 | ||
497 | static inline void ip6ip6_ecn_decapsulate(struct ipv6hdr *outer_iph, | |
498 | struct sk_buff *skb) | |
499 | { | |
500 | struct ipv6hdr *inner_iph = skb->nh.ipv6h; | |
501 | ||
502 | if (INET_ECN_is_ce(ipv6_get_dsfield(outer_iph))) | |
503 | IP6_ECN_set_ce(inner_iph); | |
504 | } | |
505 | ||
506 | /** | |
507 | * ip6ip6_rcv - decapsulate IPv6 packet and retransmit it locally | |
508 | * @skb: received socket buffer | |
509 | * | |
510 | * Return: 0 | |
511 | **/ | |
512 | ||
513 | static int | |
951dbc8a | 514 | ip6ip6_rcv(struct sk_buff **pskb) |
1da177e4 LT |
515 | { |
516 | struct sk_buff *skb = *pskb; | |
517 | struct ipv6hdr *ipv6h; | |
518 | struct ip6_tnl *t; | |
519 | ||
520 | if (!pskb_may_pull(skb, sizeof (*ipv6h))) | |
521 | goto discard; | |
522 | ||
523 | ipv6h = skb->nh.ipv6h; | |
524 | ||
525 | read_lock(&ip6ip6_lock); | |
526 | ||
527 | if ((t = ip6ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) { | |
528 | if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) { | |
9f0ede52 | 529 | read_unlock(&ip6ip6_lock); |
1da177e4 LT |
530 | kfree_skb(skb); |
531 | return 0; | |
532 | } | |
533 | ||
534 | if (!(t->parms.flags & IP6_TNL_F_CAP_RCV)) { | |
535 | t->stat.rx_dropped++; | |
536 | read_unlock(&ip6ip6_lock); | |
537 | goto discard; | |
538 | } | |
539 | secpath_reset(skb); | |
540 | skb->mac.raw = skb->nh.raw; | |
541 | skb->nh.raw = skb->data; | |
542 | skb->protocol = htons(ETH_P_IPV6); | |
543 | skb->pkt_type = PACKET_HOST; | |
544 | memset(skb->cb, 0, sizeof(struct inet6_skb_parm)); | |
545 | skb->dev = t->dev; | |
546 | dst_release(skb->dst); | |
547 | skb->dst = NULL; | |
548 | if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY) | |
549 | ipv6_copy_dscp(ipv6h, skb->nh.ipv6h); | |
550 | ip6ip6_ecn_decapsulate(ipv6h, skb); | |
551 | t->stat.rx_packets++; | |
552 | t->stat.rx_bytes += skb->len; | |
553 | netif_rx(skb); | |
554 | read_unlock(&ip6ip6_lock); | |
555 | return 0; | |
556 | } | |
557 | read_unlock(&ip6ip6_lock); | |
558 | icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0, skb->dev); | |
559 | discard: | |
560 | return 1; | |
561 | } | |
562 | ||
563 | static inline struct ipv6_txoptions *create_tel(__u8 encap_limit) | |
564 | { | |
565 | struct ipv6_tlv_tnl_enc_lim *tel; | |
566 | struct ipv6_txoptions *opt; | |
567 | __u8 *raw; | |
568 | ||
569 | int opt_len = sizeof(*opt) + 8; | |
570 | ||
571 | if (!(opt = kmalloc(opt_len, GFP_ATOMIC))) { | |
572 | return NULL; | |
573 | } | |
574 | memset(opt, 0, opt_len); | |
575 | opt->tot_len = opt_len; | |
576 | opt->dst0opt = (struct ipv6_opt_hdr *) (opt + 1); | |
577 | opt->opt_nflen = 8; | |
578 | ||
579 | tel = (struct ipv6_tlv_tnl_enc_lim *) (opt->dst0opt + 1); | |
580 | tel->type = IPV6_TLV_TNL_ENCAP_LIMIT; | |
581 | tel->length = 1; | |
582 | tel->encap_limit = encap_limit; | |
583 | ||
584 | raw = (__u8 *) opt->dst0opt; | |
585 | raw[5] = IPV6_TLV_PADN; | |
586 | raw[6] = 1; | |
587 | ||
588 | return opt; | |
589 | } | |
590 | ||
591 | /** | |
592 | * ip6ip6_tnl_addr_conflict - compare packet addresses to tunnel's own | |
593 | * @t: the outgoing tunnel device | |
594 | * @hdr: IPv6 header from the incoming packet | |
595 | * | |
596 | * Description: | |
597 | * Avoid trivial tunneling loop by checking that tunnel exit-point | |
598 | * doesn't match source of incoming packet. | |
599 | * | |
600 | * Return: | |
601 | * 1 if conflict, | |
602 | * 0 else | |
603 | **/ | |
604 | ||
605 | static inline int | |
606 | ip6ip6_tnl_addr_conflict(struct ip6_tnl *t, struct ipv6hdr *hdr) | |
607 | { | |
608 | return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr); | |
609 | } | |
610 | ||
611 | /** | |
612 | * ip6ip6_tnl_xmit - encapsulate packet and send | |
613 | * @skb: the outgoing socket buffer | |
614 | * @dev: the outgoing tunnel device | |
615 | * | |
616 | * Description: | |
617 | * Build new header and do some sanity checks on the packet before sending | |
618 | * it. | |
619 | * | |
620 | * Return: | |
621 | * 0 | |
622 | **/ | |
623 | ||
624 | static int | |
625 | ip6ip6_tnl_xmit(struct sk_buff *skb, struct net_device *dev) | |
626 | { | |
2941a486 | 627 | struct ip6_tnl *t = netdev_priv(dev); |
1da177e4 LT |
628 | struct net_device_stats *stats = &t->stat; |
629 | struct ipv6hdr *ipv6h = skb->nh.ipv6h; | |
630 | struct ipv6_txoptions *opt = NULL; | |
631 | int encap_limit = -1; | |
632 | __u16 offset; | |
633 | struct flowi fl; | |
634 | struct dst_entry *dst; | |
635 | struct net_device *tdev; | |
636 | int mtu; | |
637 | int max_headroom = sizeof(struct ipv6hdr); | |
638 | u8 proto; | |
639 | int err; | |
640 | int pkt_len; | |
641 | int dsfield; | |
642 | ||
643 | if (t->recursion++) { | |
644 | stats->collisions++; | |
645 | goto tx_err; | |
646 | } | |
647 | if (skb->protocol != htons(ETH_P_IPV6) || | |
648 | !(t->parms.flags & IP6_TNL_F_CAP_XMIT) || | |
649 | ip6ip6_tnl_addr_conflict(t, ipv6h)) { | |
650 | goto tx_err; | |
651 | } | |
652 | if ((offset = parse_tlv_tnl_enc_lim(skb, skb->nh.raw)) > 0) { | |
653 | struct ipv6_tlv_tnl_enc_lim *tel; | |
654 | tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->nh.raw[offset]; | |
655 | if (tel->encap_limit == 0) { | |
656 | icmpv6_send(skb, ICMPV6_PARAMPROB, | |
657 | ICMPV6_HDR_FIELD, offset + 2, skb->dev); | |
658 | goto tx_err; | |
659 | } | |
660 | encap_limit = tel->encap_limit - 1; | |
661 | } else if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) { | |
662 | encap_limit = t->parms.encap_limit; | |
663 | } | |
664 | memcpy(&fl, &t->fl, sizeof (fl)); | |
665 | proto = fl.proto; | |
666 | ||
667 | dsfield = ipv6_get_dsfield(ipv6h); | |
668 | if ((t->parms.flags & IP6_TNL_F_USE_ORIG_TCLASS)) | |
669 | fl.fl6_flowlabel |= (*(__u32 *) ipv6h & IPV6_TCLASS_MASK); | |
670 | if ((t->parms.flags & IP6_TNL_F_USE_ORIG_FLOWLABEL)) | |
671 | fl.fl6_flowlabel |= (*(__u32 *) ipv6h & IPV6_FLOWLABEL_MASK); | |
672 | ||
673 | if (encap_limit >= 0 && (opt = create_tel(encap_limit)) == NULL) | |
674 | goto tx_err; | |
675 | ||
676 | if ((dst = ip6_tnl_dst_check(t)) != NULL) | |
677 | dst_hold(dst); | |
a57ebc90 | 678 | else { |
1da177e4 LT |
679 | dst = ip6_route_output(NULL, &fl); |
680 | ||
a57ebc90 PM |
681 | if (dst->error || xfrm_lookup(&dst, &fl, NULL, 0) < 0) |
682 | goto tx_err_link_failure; | |
683 | } | |
1da177e4 LT |
684 | |
685 | tdev = dst->dev; | |
686 | ||
687 | if (tdev == dev) { | |
688 | stats->collisions++; | |
689 | if (net_ratelimit()) | |
690 | printk(KERN_WARNING | |
691 | "%s: Local routing loop detected!\n", | |
692 | t->parms.name); | |
693 | goto tx_err_dst_release; | |
694 | } | |
695 | mtu = dst_mtu(dst) - sizeof (*ipv6h); | |
696 | if (opt) { | |
697 | max_headroom += 8; | |
698 | mtu -= 8; | |
699 | } | |
700 | if (mtu < IPV6_MIN_MTU) | |
701 | mtu = IPV6_MIN_MTU; | |
702 | if (skb->dst && mtu < dst_mtu(skb->dst)) { | |
703 | struct rt6_info *rt = (struct rt6_info *) skb->dst; | |
704 | rt->rt6i_flags |= RTF_MODIFIED; | |
705 | rt->u.dst.metrics[RTAX_MTU-1] = mtu; | |
706 | } | |
707 | if (skb->len > mtu) { | |
708 | icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu, dev); | |
709 | goto tx_err_dst_release; | |
710 | } | |
711 | ||
712 | /* | |
713 | * Okay, now see if we can stuff it in the buffer as-is. | |
714 | */ | |
715 | max_headroom += LL_RESERVED_SPACE(tdev); | |
716 | ||
717 | if (skb_headroom(skb) < max_headroom || | |
718 | skb_cloned(skb) || skb_shared(skb)) { | |
719 | struct sk_buff *new_skb; | |
720 | ||
721 | if (!(new_skb = skb_realloc_headroom(skb, max_headroom))) | |
722 | goto tx_err_dst_release; | |
723 | ||
724 | if (skb->sk) | |
725 | skb_set_owner_w(new_skb, skb->sk); | |
726 | kfree_skb(skb); | |
727 | skb = new_skb; | |
728 | } | |
729 | dst_release(skb->dst); | |
730 | skb->dst = dst_clone(dst); | |
731 | ||
732 | skb->h.raw = skb->nh.raw; | |
733 | ||
734 | if (opt) | |
735 | ipv6_push_nfrag_opts(skb, opt, &proto, NULL); | |
736 | ||
737 | skb->nh.raw = skb_push(skb, sizeof(struct ipv6hdr)); | |
738 | ipv6h = skb->nh.ipv6h; | |
739 | *(u32*)ipv6h = fl.fl6_flowlabel | htonl(0x60000000); | |
740 | dsfield = INET_ECN_encapsulate(0, dsfield); | |
741 | ipv6_change_dsfield(ipv6h, ~INET_ECN_MASK, dsfield); | |
742 | ipv6h->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); | |
743 | ipv6h->hop_limit = t->parms.hop_limit; | |
744 | ipv6h->nexthdr = proto; | |
745 | ipv6_addr_copy(&ipv6h->saddr, &fl.fl6_src); | |
746 | ipv6_addr_copy(&ipv6h->daddr, &fl.fl6_dst); | |
747 | nf_reset(skb); | |
748 | pkt_len = skb->len; | |
749 | err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, | |
750 | skb->dst->dev, dst_output); | |
751 | ||
752 | if (err == NET_XMIT_SUCCESS || err == NET_XMIT_CN) { | |
753 | stats->tx_bytes += pkt_len; | |
754 | stats->tx_packets++; | |
755 | } else { | |
756 | stats->tx_errors++; | |
757 | stats->tx_aborted_errors++; | |
758 | } | |
759 | ip6_tnl_dst_store(t, dst); | |
760 | ||
a51482bd | 761 | kfree(opt); |
1da177e4 LT |
762 | |
763 | t->recursion--; | |
764 | return 0; | |
765 | tx_err_link_failure: | |
766 | stats->tx_carrier_errors++; | |
767 | dst_link_failure(skb); | |
768 | tx_err_dst_release: | |
769 | dst_release(dst); | |
a51482bd | 770 | kfree(opt); |
1da177e4 LT |
771 | tx_err: |
772 | stats->tx_errors++; | |
773 | stats->tx_dropped++; | |
774 | kfree_skb(skb); | |
775 | t->recursion--; | |
776 | return 0; | |
777 | } | |
778 | ||
779 | static void ip6_tnl_set_cap(struct ip6_tnl *t) | |
780 | { | |
781 | struct ip6_tnl_parm *p = &t->parms; | |
782 | struct in6_addr *laddr = &p->laddr; | |
783 | struct in6_addr *raddr = &p->raddr; | |
784 | int ltype = ipv6_addr_type(laddr); | |
785 | int rtype = ipv6_addr_type(raddr); | |
786 | ||
787 | p->flags &= ~(IP6_TNL_F_CAP_XMIT|IP6_TNL_F_CAP_RCV); | |
788 | ||
789 | if (ltype != IPV6_ADDR_ANY && rtype != IPV6_ADDR_ANY && | |
790 | ((ltype|rtype) & | |
791 | (IPV6_ADDR_UNICAST| | |
792 | IPV6_ADDR_LOOPBACK|IPV6_ADDR_LINKLOCAL| | |
793 | IPV6_ADDR_MAPPED|IPV6_ADDR_RESERVED)) == IPV6_ADDR_UNICAST) { | |
794 | struct net_device *ldev = NULL; | |
795 | int l_ok = 1; | |
796 | int r_ok = 1; | |
797 | ||
798 | if (p->link) | |
799 | ldev = dev_get_by_index(p->link); | |
800 | ||
801 | if (ltype&IPV6_ADDR_UNICAST && !ipv6_chk_addr(laddr, ldev, 0)) | |
802 | l_ok = 0; | |
803 | ||
804 | if (rtype&IPV6_ADDR_UNICAST && ipv6_chk_addr(raddr, NULL, 0)) | |
805 | r_ok = 0; | |
806 | ||
807 | if (l_ok && r_ok) { | |
808 | if (ltype&IPV6_ADDR_UNICAST) | |
809 | p->flags |= IP6_TNL_F_CAP_XMIT; | |
810 | if (rtype&IPV6_ADDR_UNICAST) | |
811 | p->flags |= IP6_TNL_F_CAP_RCV; | |
812 | } | |
813 | if (ldev) | |
814 | dev_put(ldev); | |
815 | } | |
816 | } | |
817 | ||
818 | static void ip6ip6_tnl_link_config(struct ip6_tnl *t) | |
819 | { | |
820 | struct net_device *dev = t->dev; | |
821 | struct ip6_tnl_parm *p = &t->parms; | |
822 | struct flowi *fl = &t->fl; | |
823 | ||
824 | memcpy(&dev->dev_addr, &p->laddr, sizeof(struct in6_addr)); | |
825 | memcpy(&dev->broadcast, &p->raddr, sizeof(struct in6_addr)); | |
826 | ||
827 | /* Set up flowi template */ | |
828 | ipv6_addr_copy(&fl->fl6_src, &p->laddr); | |
829 | ipv6_addr_copy(&fl->fl6_dst, &p->raddr); | |
830 | fl->oif = p->link; | |
831 | fl->fl6_flowlabel = 0; | |
832 | ||
833 | if (!(p->flags&IP6_TNL_F_USE_ORIG_TCLASS)) | |
834 | fl->fl6_flowlabel |= IPV6_TCLASS_MASK & p->flowinfo; | |
835 | if (!(p->flags&IP6_TNL_F_USE_ORIG_FLOWLABEL)) | |
836 | fl->fl6_flowlabel |= IPV6_FLOWLABEL_MASK & p->flowinfo; | |
837 | ||
838 | ip6_tnl_set_cap(t); | |
839 | ||
840 | if (p->flags&IP6_TNL_F_CAP_XMIT && p->flags&IP6_TNL_F_CAP_RCV) | |
841 | dev->flags |= IFF_POINTOPOINT; | |
842 | else | |
843 | dev->flags &= ~IFF_POINTOPOINT; | |
844 | ||
845 | dev->iflink = p->link; | |
846 | ||
847 | if (p->flags & IP6_TNL_F_CAP_XMIT) { | |
848 | struct rt6_info *rt = rt6_lookup(&p->raddr, &p->laddr, | |
849 | p->link, 0); | |
850 | ||
851 | if (rt == NULL) | |
852 | return; | |
853 | ||
854 | if (rt->rt6i_dev) { | |
855 | dev->hard_header_len = rt->rt6i_dev->hard_header_len + | |
856 | sizeof (struct ipv6hdr); | |
857 | ||
858 | dev->mtu = rt->rt6i_dev->mtu - sizeof (struct ipv6hdr); | |
859 | ||
860 | if (dev->mtu < IPV6_MIN_MTU) | |
861 | dev->mtu = IPV6_MIN_MTU; | |
862 | } | |
863 | dst_release(&rt->u.dst); | |
864 | } | |
865 | } | |
866 | ||
867 | /** | |
868 | * ip6ip6_tnl_change - update the tunnel parameters | |
869 | * @t: tunnel to be changed | |
870 | * @p: tunnel configuration parameters | |
871 | * @active: != 0 if tunnel is ready for use | |
872 | * | |
873 | * Description: | |
874 | * ip6ip6_tnl_change() updates the tunnel parameters | |
875 | **/ | |
876 | ||
877 | static int | |
878 | ip6ip6_tnl_change(struct ip6_tnl *t, struct ip6_tnl_parm *p) | |
879 | { | |
880 | ipv6_addr_copy(&t->parms.laddr, &p->laddr); | |
881 | ipv6_addr_copy(&t->parms.raddr, &p->raddr); | |
882 | t->parms.flags = p->flags; | |
883 | t->parms.hop_limit = p->hop_limit; | |
884 | t->parms.encap_limit = p->encap_limit; | |
885 | t->parms.flowinfo = p->flowinfo; | |
8181b8c1 | 886 | t->parms.link = p->link; |
1da177e4 LT |
887 | ip6ip6_tnl_link_config(t); |
888 | return 0; | |
889 | } | |
890 | ||
891 | /** | |
892 | * ip6ip6_tnl_ioctl - configure ipv6 tunnels from userspace | |
893 | * @dev: virtual device associated with tunnel | |
894 | * @ifr: parameters passed from userspace | |
895 | * @cmd: command to be performed | |
896 | * | |
897 | * Description: | |
898 | * ip6ip6_tnl_ioctl() is used for managing IPv6 tunnels | |
899 | * from userspace. | |
900 | * | |
901 | * The possible commands are the following: | |
902 | * %SIOCGETTUNNEL: get tunnel parameters for device | |
903 | * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters | |
904 | * %SIOCCHGTUNNEL: change tunnel parameters to those given | |
905 | * %SIOCDELTUNNEL: delete tunnel | |
906 | * | |
907 | * The fallback device "ip6tnl0", created during module | |
908 | * initialization, can be used for creating other tunnel devices. | |
909 | * | |
910 | * Return: | |
911 | * 0 on success, | |
912 | * %-EFAULT if unable to copy data to or from userspace, | |
913 | * %-EPERM if current process hasn't %CAP_NET_ADMIN set | |
914 | * %-EINVAL if passed tunnel parameters are invalid, | |
915 | * %-EEXIST if changing a tunnel's parameters would cause a conflict | |
916 | * %-ENODEV if attempting to change or delete a nonexisting device | |
917 | **/ | |
918 | ||
919 | static int | |
920 | ip6ip6_tnl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) | |
921 | { | |
922 | int err = 0; | |
923 | int create; | |
924 | struct ip6_tnl_parm p; | |
925 | struct ip6_tnl *t = NULL; | |
926 | ||
927 | switch (cmd) { | |
928 | case SIOCGETTUNNEL: | |
929 | if (dev == ip6ip6_fb_tnl_dev) { | |
930 | if (copy_from_user(&p, | |
931 | ifr->ifr_ifru.ifru_data, | |
932 | sizeof (p))) { | |
933 | err = -EFAULT; | |
934 | break; | |
935 | } | |
936 | if ((err = ip6ip6_tnl_locate(&p, &t, 0)) == -ENODEV) | |
2941a486 | 937 | t = netdev_priv(dev); |
1da177e4 LT |
938 | else if (err) |
939 | break; | |
940 | } else | |
2941a486 | 941 | t = netdev_priv(dev); |
1da177e4 LT |
942 | |
943 | memcpy(&p, &t->parms, sizeof (p)); | |
944 | if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof (p))) { | |
945 | err = -EFAULT; | |
946 | } | |
947 | break; | |
948 | case SIOCADDTUNNEL: | |
949 | case SIOCCHGTUNNEL: | |
950 | err = -EPERM; | |
951 | create = (cmd == SIOCADDTUNNEL); | |
952 | if (!capable(CAP_NET_ADMIN)) | |
953 | break; | |
954 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof (p))) { | |
955 | err = -EFAULT; | |
956 | break; | |
957 | } | |
958 | if (!create && dev != ip6ip6_fb_tnl_dev) { | |
2941a486 | 959 | t = netdev_priv(dev); |
1da177e4 LT |
960 | } |
961 | if (!t && (err = ip6ip6_tnl_locate(&p, &t, create))) { | |
962 | break; | |
963 | } | |
964 | if (cmd == SIOCCHGTUNNEL) { | |
965 | if (t->dev != dev) { | |
966 | err = -EEXIST; | |
967 | break; | |
968 | } | |
969 | ip6ip6_tnl_unlink(t); | |
970 | err = ip6ip6_tnl_change(t, &p); | |
971 | ip6ip6_tnl_link(t); | |
972 | netdev_state_change(dev); | |
973 | } | |
974 | if (copy_to_user(ifr->ifr_ifru.ifru_data, | |
975 | &t->parms, sizeof (p))) { | |
976 | err = -EFAULT; | |
977 | } else { | |
978 | err = 0; | |
979 | } | |
980 | break; | |
981 | case SIOCDELTUNNEL: | |
982 | err = -EPERM; | |
983 | if (!capable(CAP_NET_ADMIN)) | |
984 | break; | |
985 | ||
986 | if (dev == ip6ip6_fb_tnl_dev) { | |
987 | if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, | |
988 | sizeof (p))) { | |
989 | err = -EFAULT; | |
990 | break; | |
991 | } | |
992 | err = ip6ip6_tnl_locate(&p, &t, 0); | |
993 | if (err) | |
994 | break; | |
2941a486 | 995 | if (t == netdev_priv(ip6ip6_fb_tnl_dev)) { |
1da177e4 LT |
996 | err = -EPERM; |
997 | break; | |
998 | } | |
999 | } else { | |
2941a486 | 1000 | t = netdev_priv(dev); |
1da177e4 LT |
1001 | } |
1002 | err = unregister_netdevice(t->dev); | |
1003 | break; | |
1004 | default: | |
1005 | err = -EINVAL; | |
1006 | } | |
1007 | return err; | |
1008 | } | |
1009 | ||
1010 | /** | |
1011 | * ip6ip6_tnl_get_stats - return the stats for tunnel device | |
1012 | * @dev: virtual device associated with tunnel | |
1013 | * | |
1014 | * Return: stats for device | |
1015 | **/ | |
1016 | ||
1017 | static struct net_device_stats * | |
1018 | ip6ip6_tnl_get_stats(struct net_device *dev) | |
1019 | { | |
2941a486 | 1020 | return &(((struct ip6_tnl *)netdev_priv(dev))->stat); |
1da177e4 LT |
1021 | } |
1022 | ||
1023 | /** | |
1024 | * ip6ip6_tnl_change_mtu - change mtu manually for tunnel device | |
1025 | * @dev: virtual device associated with tunnel | |
1026 | * @new_mtu: the new mtu | |
1027 | * | |
1028 | * Return: | |
1029 | * 0 on success, | |
1030 | * %-EINVAL if mtu too small | |
1031 | **/ | |
1032 | ||
1033 | static int | |
1034 | ip6ip6_tnl_change_mtu(struct net_device *dev, int new_mtu) | |
1035 | { | |
1036 | if (new_mtu < IPV6_MIN_MTU) { | |
1037 | return -EINVAL; | |
1038 | } | |
1039 | dev->mtu = new_mtu; | |
1040 | return 0; | |
1041 | } | |
1042 | ||
1043 | /** | |
1044 | * ip6ip6_tnl_dev_setup - setup virtual tunnel device | |
1045 | * @dev: virtual device associated with tunnel | |
1046 | * | |
1047 | * Description: | |
1048 | * Initialize function pointers and device parameters | |
1049 | **/ | |
1050 | ||
1051 | static void ip6ip6_tnl_dev_setup(struct net_device *dev) | |
1052 | { | |
1053 | SET_MODULE_OWNER(dev); | |
1054 | dev->uninit = ip6ip6_tnl_dev_uninit; | |
1055 | dev->destructor = free_netdev; | |
1056 | dev->hard_start_xmit = ip6ip6_tnl_xmit; | |
1057 | dev->get_stats = ip6ip6_tnl_get_stats; | |
1058 | dev->do_ioctl = ip6ip6_tnl_ioctl; | |
1059 | dev->change_mtu = ip6ip6_tnl_change_mtu; | |
1060 | ||
1061 | dev->type = ARPHRD_TUNNEL6; | |
1062 | dev->hard_header_len = LL_MAX_HEADER + sizeof (struct ipv6hdr); | |
1063 | dev->mtu = ETH_DATA_LEN - sizeof (struct ipv6hdr); | |
1064 | dev->flags |= IFF_NOARP; | |
1065 | dev->addr_len = sizeof(struct in6_addr); | |
1066 | } | |
1067 | ||
1068 | ||
1069 | /** | |
1070 | * ip6ip6_tnl_dev_init_gen - general initializer for all tunnel devices | |
1071 | * @dev: virtual device associated with tunnel | |
1072 | **/ | |
1073 | ||
1074 | static inline void | |
1075 | ip6ip6_tnl_dev_init_gen(struct net_device *dev) | |
1076 | { | |
2941a486 | 1077 | struct ip6_tnl *t = netdev_priv(dev); |
1da177e4 LT |
1078 | t->fl.proto = IPPROTO_IPV6; |
1079 | t->dev = dev; | |
1080 | strcpy(t->parms.name, dev->name); | |
1081 | } | |
1082 | ||
1083 | /** | |
1084 | * ip6ip6_tnl_dev_init - initializer for all non fallback tunnel devices | |
1085 | * @dev: virtual device associated with tunnel | |
1086 | **/ | |
1087 | ||
1088 | static int | |
1089 | ip6ip6_tnl_dev_init(struct net_device *dev) | |
1090 | { | |
2941a486 | 1091 | struct ip6_tnl *t = netdev_priv(dev); |
1da177e4 LT |
1092 | ip6ip6_tnl_dev_init_gen(dev); |
1093 | ip6ip6_tnl_link_config(t); | |
1094 | return 0; | |
1095 | } | |
1096 | ||
1097 | /** | |
1098 | * ip6ip6_fb_tnl_dev_init - initializer for fallback tunnel device | |
1099 | * @dev: fallback device | |
1100 | * | |
1101 | * Return: 0 | |
1102 | **/ | |
1103 | ||
1104 | static int | |
1105 | ip6ip6_fb_tnl_dev_init(struct net_device *dev) | |
1106 | { | |
2941a486 | 1107 | struct ip6_tnl *t = netdev_priv(dev); |
1da177e4 LT |
1108 | ip6ip6_tnl_dev_init_gen(dev); |
1109 | dev_hold(dev); | |
1110 | tnls_wc[0] = t; | |
1111 | return 0; | |
1112 | } | |
1113 | ||
0303770d | 1114 | #ifdef CONFIG_INET6_TUNNEL |
1da177e4 | 1115 | static struct xfrm6_tunnel ip6ip6_handler = { |
0303770d PM |
1116 | .handler = ip6ip6_rcv, |
1117 | .err_handler = ip6ip6_err, | |
1da177e4 LT |
1118 | }; |
1119 | ||
0303770d PM |
1120 | static inline int ip6ip6_register(void) |
1121 | { | |
1122 | return xfrm6_tunnel_register(&ip6ip6_handler); | |
1123 | } | |
1124 | ||
1125 | static inline int ip6ip6_unregister(void) | |
1126 | { | |
227510c7 | 1127 | return xfrm6_tunnel_deregister(&ip6ip6_handler); |
0303770d PM |
1128 | } |
1129 | #else | |
1130 | static struct inet6_protocol xfrm6_tunnel_protocol = { | |
1131 | .handler = ip6ip6_rcv, | |
1132 | .err_handler = ip6ip6_err, | |
1133 | .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, | |
1134 | }; | |
1135 | ||
1136 | static inline int ip6ip6_register(void) | |
1137 | { | |
1138 | return inet6_add_protocol(&xfrm6_tunnel_protocol, IPPROTO_IPV6); | |
1139 | } | |
1140 | ||
1141 | static inline int ip6ip6_unregister(void) | |
1142 | { | |
1143 | return inet6_del_protocol(&xfrm6_tunnel_protocol, IPPROTO_IPV6); | |
1144 | } | |
1145 | #endif | |
1146 | ||
1da177e4 LT |
1147 | /** |
1148 | * ip6_tunnel_init - register protocol and reserve needed resources | |
1149 | * | |
1150 | * Return: 0 on success | |
1151 | **/ | |
1152 | ||
1153 | static int __init ip6_tunnel_init(void) | |
1154 | { | |
1155 | int err; | |
1156 | ||
0303770d | 1157 | if (ip6ip6_register() < 0) { |
1da177e4 LT |
1158 | printk(KERN_ERR "ip6ip6 init: can't register tunnel\n"); |
1159 | return -EAGAIN; | |
1160 | } | |
1161 | ip6ip6_fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6tnl0", | |
1162 | ip6ip6_tnl_dev_setup); | |
1163 | ||
1164 | if (!ip6ip6_fb_tnl_dev) { | |
1165 | err = -ENOMEM; | |
1166 | goto fail; | |
1167 | } | |
1168 | ip6ip6_fb_tnl_dev->init = ip6ip6_fb_tnl_dev_init; | |
1169 | ||
1170 | if ((err = register_netdev(ip6ip6_fb_tnl_dev))) { | |
1171 | free_netdev(ip6ip6_fb_tnl_dev); | |
1172 | goto fail; | |
1173 | } | |
1174 | return 0; | |
1175 | fail: | |
0303770d | 1176 | ip6ip6_unregister(); |
1da177e4 LT |
1177 | return err; |
1178 | } | |
1179 | ||
1180 | /** | |
1181 | * ip6_tunnel_cleanup - free resources and unregister protocol | |
1182 | **/ | |
1183 | ||
1184 | static void __exit ip6_tunnel_cleanup(void) | |
1185 | { | |
0303770d | 1186 | if (ip6ip6_unregister() < 0) |
1da177e4 LT |
1187 | printk(KERN_INFO "ip6ip6 close: can't deregister tunnel\n"); |
1188 | ||
1189 | unregister_netdev(ip6ip6_fb_tnl_dev); | |
1190 | } | |
1191 | ||
1192 | module_init(ip6_tunnel_init); | |
1193 | module_exit(ip6_tunnel_cleanup); |