ip6_tunnel.c
来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 1,163 行 · 第 1/2 页
C
1,163 行
/* * IPv6 over IPv6 tunnel device * Linux INET6 implementation * * Authors: * Ville Nuorvala <vnuorval@tcs.hut.fi> * * $Id$ * * Based on: * linux/net/ipv6/sit.c * * RFC 2473 * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. * */#include <linux/config.h>#include <linux/module.h>#include <linux/errno.h>#include <linux/types.h>#include <linux/sockios.h>#include <linux/if.h>#include <linux/in.h>#include <linux/ip.h>#include <linux/if_tunnel.h>#include <linux/net.h>#include <linux/in6.h>#include <linux/netdevice.h>#include <linux/if_arp.h>#include <linux/icmpv6.h>#include <linux/init.h>#include <linux/route.h>#include <linux/rtnetlink.h>#include <linux/netfilter_ipv6.h>#include <asm/uaccess.h>#include <asm/atomic.h>#include <net/ip.h>#include <net/ipv6.h>#include <net/protocol.h>#include <net/ip6_route.h>#include <net/addrconf.h>#include <net/ip6_tunnel.h>#include <net/xfrm.h>#include <net/dsfield.h>#include <net/inet_ecn.h>MODULE_AUTHOR("Ville Nuorvala");MODULE_DESCRIPTION("IPv6-in-IPv6 tunnel");MODULE_LICENSE("GPL");#define IPV6_TLV_TEL_DST_SIZE 8#ifdef IP6_TNL_DEBUG#define IP6_TNL_TRACE(x...) printk(KERN_DEBUG "%s:" x "\n", __FUNCTION__)#else#define IP6_TNL_TRACE(x...) do {;} while(0)#endif#define IPV6_TCLASS_MASK (IPV6_FLOWINFO_MASK & ~IPV6_FLOWLABEL_MASK)#define HASH_SIZE 32#define HASH(addr) (((addr)->s6_addr32[0] ^ (addr)->s6_addr32[1] ^ \ (addr)->s6_addr32[2] ^ (addr)->s6_addr32[3]) & \ (HASH_SIZE - 1))static int ip6ip6_fb_tnl_dev_init(struct net_device *dev);static int ip6ip6_tnl_dev_init(struct net_device *dev);static void ip6ip6_tnl_dev_setup(struct net_device *dev);/* the IPv6 tunnel fallback device */static struct net_device *ip6ip6_fb_tnl_dev;/* lists for storing tunnels in use */static struct ip6_tnl *tnls_r_l[HASH_SIZE];static struct ip6_tnl *tnls_wc[1];static struct ip6_tnl **tnls[2] = { tnls_wc, tnls_r_l };/* lock for the tunnel lists */static rwlock_t ip6ip6_lock = RW_LOCK_UNLOCKED;static inline struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t){ struct dst_entry *dst = t->dst_cache; if (dst && dst->obsolete && dst->ops->check(dst, t->dst_cookie) == NULL) { t->dst_cache = NULL; return NULL; } return dst;}static inline void ip6_tnl_dst_reset(struct ip6_tnl *t){ dst_release(t->dst_cache); t->dst_cache = NULL;}static inline void ip6_tnl_dst_store(struct ip6_tnl *t, struct dst_entry *dst){ struct rt6_info *rt = (struct rt6_info *) dst; t->dst_cookie = rt->rt6i_node ? rt->rt6i_node->fn_sernum : 0; dst_release(t->dst_cache); t->dst_cache = dst;}/** * ip6ip6_tnl_lookup - fetch tunnel matching the end-point addresses * @remote: the address of the tunnel exit-point * @local: the address of the tunnel entry-point * * Return: * tunnel matching given end-points if found, * else fallback tunnel if its device is up, * else %NULL **/static struct ip6_tnl *ip6ip6_tnl_lookup(struct in6_addr *remote, struct in6_addr *local){ unsigned h0 = HASH(remote); unsigned h1 = HASH(local); struct ip6_tnl *t; for (t = tnls_r_l[h0 ^ h1]; t; t = t->next) { if (!ipv6_addr_cmp(local, &t->parms.laddr) && !ipv6_addr_cmp(remote, &t->parms.raddr) && (t->dev->flags & IFF_UP)) return t; } if ((t = tnls_wc[0]) != NULL && (t->dev->flags & IFF_UP)) return t; return NULL;}/** * ip6ip6_bucket - get head of list matching given tunnel parameters * @p: parameters containing tunnel end-points * * Description: * ip6ip6_bucket() returns the head of the list matching the * &struct in6_addr entries laddr and raddr in @p. * * Return: head of IPv6 tunnel list **/static struct ip6_tnl **ip6ip6_bucket(struct ip6_tnl_parm *p){ struct in6_addr *remote = &p->raddr; struct in6_addr *local = &p->laddr; unsigned h = 0; int prio = 0; if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) { prio = 1; h = HASH(remote) ^ HASH(local); } return &tnls[prio][h];}/** * ip6ip6_tnl_link - add tunnel to hash table * @t: tunnel to be added **/static voidip6ip6_tnl_link(struct ip6_tnl *t){ struct ip6_tnl **tp = ip6ip6_bucket(&t->parms); write_lock_bh(&ip6ip6_lock); t->next = *tp; write_unlock_bh(&ip6ip6_lock); *tp = t;}/** * ip6ip6_tnl_unlink - remove tunnel from hash table * @t: tunnel to be removed **/static voidip6ip6_tnl_unlink(struct ip6_tnl *t){ struct ip6_tnl **tp; for (tp = ip6ip6_bucket(&t->parms); *tp; tp = &(*tp)->next) { if (t == *tp) { write_lock_bh(&ip6ip6_lock); *tp = t->next; write_unlock_bh(&ip6ip6_lock); break; } }}/** * ip6_tnl_create() - create a new tunnel * @p: tunnel parameters * @pt: pointer to new tunnel * * Description: * Create tunnel matching given parameters. * * Return: * 0 on success **/static intip6_tnl_create(struct ip6_tnl_parm *p, struct ip6_tnl **pt){ struct net_device *dev; struct ip6_tnl *t; char name[IFNAMSIZ]; int err; if (p->name[0]) { strlcpy(name, p->name, IFNAMSIZ); } else { int i; for (i = 1; i < IP6_TNL_MAX; i++) { sprintf(name, "ip6tnl%d", i); if (__dev_get_by_name(name) == NULL) break; } if (i == IP6_TNL_MAX) return -ENOBUFS; } dev = alloc_netdev(sizeof (*t), name, ip6ip6_tnl_dev_setup); if (dev == NULL) return -ENOMEM; t = dev->priv; dev->init = ip6ip6_tnl_dev_init; t->parms = *p; if ((err = register_netdevice(dev)) < 0) { free_netdev(dev); return err; } dev_hold(dev); ip6ip6_tnl_link(t); *pt = t; return 0;}/** * ip6ip6_tnl_locate - find or create tunnel matching given parameters * @p: tunnel parameters * @create: != 0 if allowed to create new tunnel if no match found * * Description: * ip6ip6_tnl_locate() first tries to locate an existing tunnel * based on @parms. If this is unsuccessful, but @create is set a new * tunnel device is created and registered for use. * * Return: * 0 if tunnel located or created, * -EINVAL if parameters incorrect, * -ENODEV if no matching tunnel available **/static intip6ip6_tnl_locate(struct ip6_tnl_parm *p, struct ip6_tnl **pt, int create){ struct in6_addr *remote = &p->raddr; struct in6_addr *local = &p->laddr; struct ip6_tnl *t; if (p->proto != IPPROTO_IPV6) return -EINVAL; for (t = *ip6ip6_bucket(p); t; t = t->next) { if (!ipv6_addr_cmp(local, &t->parms.laddr) && !ipv6_addr_cmp(remote, &t->parms.raddr)) { *pt = t; return (create ? -EEXIST : 0); } } if (!create) return -ENODEV; return ip6_tnl_create(p, pt);}/** * ip6ip6_tnl_dev_uninit - tunnel device uninitializer * @dev: the device to be destroyed * * Description: * ip6ip6_tnl_dev_uninit() removes tunnel from its list **/static voidip6ip6_tnl_dev_uninit(struct net_device *dev){ struct ip6_tnl *t = dev->priv; if (dev == ip6ip6_fb_tnl_dev) { write_lock_bh(&ip6ip6_lock); tnls_wc[0] = NULL; write_unlock_bh(&ip6ip6_lock); } else { ip6ip6_tnl_unlink(t); } ip6_tnl_dst_reset(t); dev_put(dev);}/** * parse_tvl_tnl_enc_lim - handle encapsulation limit option * @skb: received socket buffer * * Return: * 0 if none was found, * else index to encapsulation limit **/static __u16parse_tlv_tnl_enc_lim(struct sk_buff *skb, __u8 * raw){ struct ipv6hdr *ipv6h = (struct ipv6hdr *) raw; __u8 nexthdr = ipv6h->nexthdr; __u16 off = sizeof (*ipv6h); while (ipv6_ext_hdr(nexthdr) && nexthdr != NEXTHDR_NONE) { __u16 optlen = 0; struct ipv6_opt_hdr *hdr; if (raw + off + sizeof (*hdr) > skb->data && !pskb_may_pull(skb, raw - skb->data + off + sizeof (*hdr))) break; hdr = (struct ipv6_opt_hdr *) (raw + off); if (nexthdr == NEXTHDR_FRAGMENT) { struct frag_hdr *frag_hdr = (struct frag_hdr *) hdr; if (frag_hdr->frag_off) break; optlen = 8; } else if (nexthdr == NEXTHDR_AUTH) { optlen = (hdr->hdrlen + 2) << 2; } else { optlen = ipv6_optlen(hdr); } if (nexthdr == NEXTHDR_DEST) { __u16 i = off + 2; while (1) { struct ipv6_tlv_tnl_enc_lim *tel; /* No more room for encapsulation limit */ if (i + sizeof (*tel) > off + optlen) break; tel = (struct ipv6_tlv_tnl_enc_lim *) &raw[i]; /* return index of option if found and valid */ if (tel->type == IPV6_TLV_TNL_ENCAP_LIMIT && tel->length == 1) return i; /* else jump to next option */ if (tel->type) i += tel->length + 2; else i++; } } nexthdr = hdr->nexthdr; off += optlen; } return 0;}/** * ip6ip6_err - tunnel error handler * * Description: * ip6ip6_err() should handle errors in the tunnel according * to the specifications in RFC 2473. **/static void ip6ip6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, int type, int code, int offset, __u32 info){ struct ipv6hdr *ipv6h = (struct ipv6hdr *) skb->data; struct ip6_tnl *t; int rel_msg = 0; int rel_type = ICMPV6_DEST_UNREACH; int rel_code = ICMPV6_ADDR_UNREACH; __u32 rel_info = 0; __u16 len; /* If the packet doesn't contain the original IPv6 header we are in trouble since we might need the source address for further processing of the error. */ read_lock(&ip6ip6_lock); if ((t = ip6ip6_tnl_lookup(&ipv6h->daddr, &ipv6h->saddr)) == NULL) goto out; switch (type) { __u32 teli; struct ipv6_tlv_tnl_enc_lim *tel; __u32 mtu; case ICMPV6_DEST_UNREACH: if (net_ratelimit()) printk(KERN_WARNING "%s: Path to destination invalid " "or inactive!\n", t->parms.name); rel_msg = 1; break; case ICMPV6_TIME_EXCEED: if (code == ICMPV6_EXC_HOPLIMIT) { if (net_ratelimit()) printk(KERN_WARNING "%s: Too small hop limit or " "routing loop in tunnel!\n", t->parms.name); rel_msg = 1; } break; case ICMPV6_PARAMPROB: /* ignore if parameter problem not caused by a tunnel encapsulation limit sub-option */ if (code != ICMPV6_HDR_FIELD) { break; } teli = parse_tlv_tnl_enc_lim(skb, skb->data); if (teli && teli == ntohl(info) - 2) { tel = (struct ipv6_tlv_tnl_enc_lim *) &skb->data[teli]; if (tel->encap_limit == 0) { if (net_ratelimit()) printk(KERN_WARNING "%s: Too small encapsulation " "limit or routing loop in " "tunnel!\n", t->parms.name); rel_msg = 1; } } break; case ICMPV6_PKT_TOOBIG: mtu = ntohl(info) - offset; if (mtu < IPV6_MIN_MTU) mtu = IPV6_MIN_MTU; t->dev->mtu = mtu; if ((len = sizeof (*ipv6h) + ipv6h->payload_len) > mtu) { rel_type = ICMPV6_PKT_TOOBIG; rel_code = 0; rel_info = mtu; rel_msg = 1; } break; } if (rel_msg && pskb_may_pull(skb, offset + sizeof (*ipv6h))) { struct rt6_info *rt; struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); if (!skb2) goto out; dst_release(skb2->dst); skb2->dst = NULL; skb_pull(skb2, offset); skb2->nh.raw = skb2->data; /* Try to guess incoming interface */ rt = rt6_lookup(&skb2->nh.ipv6h->saddr, NULL, 0, 0); if (rt && rt->rt6i_dev) skb2->dev = rt->rt6i_dev; icmpv6_send(skb2, rel_type, rel_code, rel_info, skb2->dev); if (rt) dst_release(&rt->u.dst); kfree_skb(skb2); }out: read_unlock(&ip6ip6_lock);}static inline void ip6ip6_ecn_decapsulate(struct ipv6hdr *outer_iph, struct sk_buff *skb){ struct ipv6hdr *inner_iph = skb->nh.ipv6h; if (INET_ECN_is_ce(ipv6_get_dsfield(outer_iph))) IP6_ECN_set_ce(inner_iph);}/** * ip6ip6_rcv - decapsulate IPv6 packet and retransmit it locally * @skb: received socket buffer * * Return: 0 **/static int ip6ip6_rcv(struct sk_buff **pskb, unsigned int *nhoffp){ struct sk_buff *skb = *pskb; struct ipv6hdr *ipv6h; struct ip6_tnl *t; if (!pskb_may_pull(skb, sizeof (*ipv6h))) goto discard; ipv6h = skb->nh.ipv6h; read_lock(&ip6ip6_lock); if ((t = ip6ip6_tnl_lookup(&ipv6h->saddr, &ipv6h->daddr)) != NULL) { if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) { kfree_skb(skb); return 0; } if (!(t->parms.flags & IP6_TNL_F_CAP_RCV)) { t->stat.rx_dropped++; read_unlock(&ip6ip6_lock); goto discard; } secpath_reset(skb); skb->mac.raw = skb->nh.raw; skb->nh.raw = skb->data; skb->protocol = htons(ETH_P_IPV6); skb->pkt_type = PACKET_HOST; memset(skb->cb, 0, sizeof(struct inet6_skb_parm)); skb->dev = t->dev; dst_release(skb->dst); skb->dst = NULL; if (t->parms.flags & IP6_TNL_F_RCV_DSCP_COPY) ipv6_copy_dscp(ipv6h, skb->nh.ipv6h); ip6ip6_ecn_decapsulate(ipv6h, skb); t->stat.rx_packets++; t->stat.rx_bytes += skb->len; netif_rx(skb); read_unlock(&ip6ip6_lock); return 0; } read_unlock(&ip6ip6_lock); icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0, skb->dev);discard: return 1;}static inline struct ipv6_txoptions *create_tel(__u8 encap_limit){ struct ipv6_tlv_tnl_enc_lim *tel; struct ipv6_txoptions *opt; __u8 *raw; int opt_len = sizeof(*opt) + 8; if (!(opt = kmalloc(opt_len, GFP_ATOMIC))) { return NULL; } memset(opt, 0, opt_len); opt->tot_len = opt_len; opt->dst0opt = (struct ipv6_opt_hdr *) (opt + 1); opt->opt_nflen = 8; tel = (struct ipv6_tlv_tnl_enc_lim *) (opt->dst0opt + 1); tel->type = IPV6_TLV_TNL_ENCAP_LIMIT; tel->length = 1; tel->encap_limit = encap_limit; raw = (__u8 *) opt->dst0opt; raw[5] = IPV6_TLV_PADN;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?