|
| 1 | +/* |
| 2 | + * Copyright (C)2003-2006 Helsinki University of Technology |
| 3 | + * Copyright (C)2003-2006 USAGI/WIDE Project |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation; either version 2 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | + */ |
| 19 | +/* |
| 20 | + * Authors: |
| 21 | + * Noriaki TAKAMIYA @USAGI |
| 22 | + * Masahide NAKAMURA @USAGI |
| 23 | + */ |
| 24 | + |
| 25 | +#include <linux/config.h> |
| 26 | +#include <linux/module.h> |
| 27 | +#include <linux/skbuff.h> |
| 28 | +#include <linux/ipv6.h> |
| 29 | +#include <net/ipv6.h> |
| 30 | +#include <net/xfrm.h> |
| 31 | +#include <net/mip6.h> |
| 32 | + |
| 33 | +static xfrm_address_t *mip6_xfrm_addr(struct xfrm_state *x, xfrm_address_t *addr) |
| 34 | +{ |
| 35 | + return x->coaddr; |
| 36 | +} |
| 37 | + |
| 38 | +static int mip6_rthdr_input(struct xfrm_state *x, struct sk_buff *skb) |
| 39 | +{ |
| 40 | + struct rt2_hdr *rt2 = (struct rt2_hdr *)skb->data; |
| 41 | + |
| 42 | + if (!ipv6_addr_equal(&rt2->addr, (struct in6_addr *)x->coaddr) && |
| 43 | + !ipv6_addr_any((struct in6_addr *)x->coaddr)) |
| 44 | + return -ENOENT; |
| 45 | + |
| 46 | + return rt2->rt_hdr.nexthdr; |
| 47 | +} |
| 48 | + |
| 49 | +/* Routing Header type 2 is inserted. |
| 50 | + * IP Header's dst address is replaced with Routing Header's Home Address. |
| 51 | + */ |
| 52 | +static int mip6_rthdr_output(struct xfrm_state *x, struct sk_buff *skb) |
| 53 | +{ |
| 54 | + struct ipv6hdr *iph; |
| 55 | + struct rt2_hdr *rt2; |
| 56 | + u8 nexthdr; |
| 57 | + |
| 58 | + iph = (struct ipv6hdr *)skb->data; |
| 59 | + iph->payload_len = htons(skb->len - sizeof(*iph)); |
| 60 | + |
| 61 | + nexthdr = *skb->nh.raw; |
| 62 | + *skb->nh.raw = IPPROTO_ROUTING; |
| 63 | + |
| 64 | + rt2 = (struct rt2_hdr *)skb->h.raw; |
| 65 | + rt2->rt_hdr.nexthdr = nexthdr; |
| 66 | + rt2->rt_hdr.hdrlen = (x->props.header_len >> 3) - 1; |
| 67 | + rt2->rt_hdr.type = IPV6_SRCRT_TYPE_2; |
| 68 | + rt2->rt_hdr.segments_left = 1; |
| 69 | + memset(&rt2->reserved, 0, sizeof(rt2->reserved)); |
| 70 | + |
| 71 | + BUG_TRAP(rt2->rt_hdr.hdrlen == 2); |
| 72 | + |
| 73 | + memcpy(&rt2->addr, &iph->daddr, sizeof(rt2->addr)); |
| 74 | + memcpy(&iph->daddr, x->coaddr, sizeof(iph->daddr)); |
| 75 | + |
| 76 | + return 0; |
| 77 | +} |
| 78 | + |
| 79 | +static int mip6_rthdr_offset(struct xfrm_state *x, struct sk_buff *skb, |
| 80 | + u8 **nexthdr) |
| 81 | +{ |
| 82 | + u16 offset = sizeof(struct ipv6hdr); |
| 83 | + struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.ipv6h + 1); |
| 84 | + unsigned int packet_len = skb->tail - skb->nh.raw; |
| 85 | + int found_rhdr = 0; |
| 86 | + |
| 87 | + *nexthdr = &skb->nh.ipv6h->nexthdr; |
| 88 | + |
| 89 | + while (offset + 1 <= packet_len) { |
| 90 | + |
| 91 | + switch (**nexthdr) { |
| 92 | + case NEXTHDR_HOP: |
| 93 | + break; |
| 94 | + case NEXTHDR_ROUTING: |
| 95 | + if (offset + 3 <= packet_len) { |
| 96 | + struct ipv6_rt_hdr *rt; |
| 97 | + rt = (struct ipv6_rt_hdr *)(skb->nh.raw + offset); |
| 98 | + if (rt->type != 0) |
| 99 | + return offset; |
| 100 | + } |
| 101 | + found_rhdr = 1; |
| 102 | + break; |
| 103 | + case NEXTHDR_DEST: |
| 104 | + if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0) |
| 105 | + return offset; |
| 106 | + |
| 107 | + if (found_rhdr) |
| 108 | + return offset; |
| 109 | + |
| 110 | + break; |
| 111 | + default: |
| 112 | + return offset; |
| 113 | + } |
| 114 | + |
| 115 | + offset += ipv6_optlen(exthdr); |
| 116 | + *nexthdr = &exthdr->nexthdr; |
| 117 | + exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset); |
| 118 | + } |
| 119 | + |
| 120 | + return offset; |
| 121 | +} |
| 122 | + |
| 123 | +static int mip6_rthdr_init_state(struct xfrm_state *x) |
| 124 | +{ |
| 125 | + if (x->id.spi) { |
| 126 | + printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__, |
| 127 | + x->id.spi); |
| 128 | + return -EINVAL; |
| 129 | + } |
| 130 | + if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) { |
| 131 | + printk(KERN_INFO "%s: state's mode is not %u: %u\n", |
| 132 | + __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode); |
| 133 | + return -EINVAL; |
| 134 | + } |
| 135 | + |
| 136 | + x->props.header_len = sizeof(struct rt2_hdr); |
| 137 | + |
| 138 | + return 0; |
| 139 | +} |
| 140 | + |
| 141 | +/* |
| 142 | + * Do nothing about destroying since it has no specific operation for routing |
| 143 | + * header type 2 unlike IPsec protocols. |
| 144 | + */ |
| 145 | +static void mip6_rthdr_destroy(struct xfrm_state *x) |
| 146 | +{ |
| 147 | +} |
| 148 | + |
| 149 | +static struct xfrm_type mip6_rthdr_type = |
| 150 | +{ |
| 151 | + .description = "MIP6RT", |
| 152 | + .owner = THIS_MODULE, |
| 153 | + .proto = IPPROTO_ROUTING, |
| 154 | + .flags = XFRM_TYPE_NON_FRAGMENT, |
| 155 | + .init_state = mip6_rthdr_init_state, |
| 156 | + .destructor = mip6_rthdr_destroy, |
| 157 | + .input = mip6_rthdr_input, |
| 158 | + .output = mip6_rthdr_output, |
| 159 | + .hdr_offset = mip6_rthdr_offset, |
| 160 | + .remote_addr = mip6_xfrm_addr, |
| 161 | +}; |
| 162 | + |
| 163 | +int __init mip6_init(void) |
| 164 | +{ |
| 165 | + printk(KERN_INFO "Mobile IPv6\n"); |
| 166 | + |
| 167 | + if (xfrm_register_type(&mip6_rthdr_type, AF_INET6) < 0) { |
| 168 | + printk(KERN_INFO "%s: can't add xfrm type(rthdr)\n", __FUNCTION__); |
| 169 | + goto mip6_rthdr_xfrm_fail; |
| 170 | + } |
| 171 | + return 0; |
| 172 | + |
| 173 | + mip6_rthdr_xfrm_fail: |
| 174 | + return -EAGAIN; |
| 175 | +} |
| 176 | + |
| 177 | +void __exit mip6_fini(void) |
| 178 | +{ |
| 179 | + if (xfrm_unregister_type(&mip6_rthdr_type, AF_INET6) < 0) |
| 180 | + printk(KERN_INFO "%s: can't remove xfrm type(rthdr)\n", __FUNCTION__); |
| 181 | +} |
0 commit comments