mip6.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. #include <linux/config.h>
  25. #include <linux/module.h>
  26. #include <linux/skbuff.h>
  27. #include <linux/ipv6.h>
  28. #include <net/ipv6.h>
  29. #include <net/xfrm.h>
  30. #include <net/mip6.h>
  31. static xfrm_address_t *mip6_xfrm_addr(struct xfrm_state *x, xfrm_address_t *addr)
  32. {
  33. return x->coaddr;
  34. }
  35. static inline unsigned int calc_padlen(unsigned int len, unsigned int n)
  36. {
  37. return (n - len + 16) & 0x7;
  38. }
  39. static inline void *mip6_padn(__u8 *data, __u8 padlen)
  40. {
  41. if (!data)
  42. return NULL;
  43. if (padlen == 1) {
  44. data[0] = MIP6_OPT_PAD_1;
  45. } else if (padlen > 1) {
  46. data[0] = MIP6_OPT_PAD_N;
  47. data[1] = padlen - 2;
  48. if (padlen > 2)
  49. memset(data+2, 0, data[1]);
  50. }
  51. return data + padlen;
  52. }
  53. static int mip6_destopt_input(struct xfrm_state *x, struct sk_buff *skb)
  54. {
  55. struct ipv6hdr *iph = skb->nh.ipv6h;
  56. struct ipv6_destopt_hdr *destopt = (struct ipv6_destopt_hdr *)skb->data;
  57. if (!ipv6_addr_equal(&iph->saddr, (struct in6_addr *)x->coaddr) &&
  58. !ipv6_addr_any((struct in6_addr *)x->coaddr))
  59. return -ENOENT;
  60. return destopt->nexthdr;
  61. }
  62. /* Destination Option Header is inserted.
  63. * IP Header's src address is replaced with Home Address Option in
  64. * Destination Option Header.
  65. */
  66. static int mip6_destopt_output(struct xfrm_state *x, struct sk_buff *skb)
  67. {
  68. struct ipv6hdr *iph;
  69. struct ipv6_destopt_hdr *dstopt;
  70. struct ipv6_destopt_hao *hao;
  71. u8 nexthdr;
  72. int len;
  73. iph = (struct ipv6hdr *)skb->data;
  74. iph->payload_len = htons(skb->len - sizeof(*iph));
  75. nexthdr = *skb->nh.raw;
  76. *skb->nh.raw = IPPROTO_DSTOPTS;
  77. dstopt = (struct ipv6_destopt_hdr *)skb->h.raw;
  78. dstopt->nexthdr = nexthdr;
  79. hao = mip6_padn((char *)(dstopt + 1),
  80. calc_padlen(sizeof(*dstopt), 6));
  81. hao->type = IPV6_TLV_HAO;
  82. hao->length = sizeof(*hao) - 2;
  83. BUG_TRAP(hao->length == 16);
  84. len = ((char *)hao - (char *)dstopt) + sizeof(*hao);
  85. memcpy(&hao->addr, &iph->saddr, sizeof(hao->addr));
  86. memcpy(&iph->saddr, x->coaddr, sizeof(iph->saddr));
  87. BUG_TRAP(len == x->props.header_len);
  88. dstopt->hdrlen = (x->props.header_len >> 3) - 1;
  89. return 0;
  90. }
  91. static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
  92. u8 **nexthdr)
  93. {
  94. u16 offset = sizeof(struct ipv6hdr);
  95. struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.ipv6h + 1);
  96. unsigned int packet_len = skb->tail - skb->nh.raw;
  97. int found_rhdr = 0;
  98. *nexthdr = &skb->nh.ipv6h->nexthdr;
  99. while (offset + 1 <= packet_len) {
  100. switch (**nexthdr) {
  101. case NEXTHDR_HOP:
  102. break;
  103. case NEXTHDR_ROUTING:
  104. found_rhdr = 1;
  105. break;
  106. case NEXTHDR_DEST:
  107. /*
  108. * HAO MUST NOT appear more than once.
  109. * XXX: It is better to try to find by the end of
  110. * XXX: packet if HAO exists.
  111. */
  112. if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0) {
  113. LIMIT_NETDEBUG(KERN_WARNING "mip6: hao exists already, override\n");
  114. return offset;
  115. }
  116. if (found_rhdr)
  117. return offset;
  118. break;
  119. default:
  120. return offset;
  121. }
  122. offset += ipv6_optlen(exthdr);
  123. *nexthdr = &exthdr->nexthdr;
  124. exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
  125. }
  126. return offset;
  127. }
  128. static int mip6_destopt_init_state(struct xfrm_state *x)
  129. {
  130. if (x->id.spi) {
  131. printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
  132. x->id.spi);
  133. return -EINVAL;
  134. }
  135. if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
  136. printk(KERN_INFO "%s: state's mode is not %u: %u\n",
  137. __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
  138. return -EINVAL;
  139. }
  140. x->props.header_len = sizeof(struct ipv6_destopt_hdr) +
  141. calc_padlen(sizeof(struct ipv6_destopt_hdr), 6) +
  142. sizeof(struct ipv6_destopt_hao);
  143. BUG_TRAP(x->props.header_len == 24);
  144. return 0;
  145. }
  146. /*
  147. * Do nothing about destroying since it has no specific operation for
  148. * destination options header unlike IPsec protocols.
  149. */
  150. static void mip6_destopt_destroy(struct xfrm_state *x)
  151. {
  152. }
  153. static struct xfrm_type mip6_destopt_type =
  154. {
  155. .description = "MIP6DESTOPT",
  156. .owner = THIS_MODULE,
  157. .proto = IPPROTO_DSTOPTS,
  158. .flags = XFRM_TYPE_NON_FRAGMENT,
  159. .init_state = mip6_destopt_init_state,
  160. .destructor = mip6_destopt_destroy,
  161. .input = mip6_destopt_input,
  162. .output = mip6_destopt_output,
  163. .hdr_offset = mip6_destopt_offset,
  164. .local_addr = mip6_xfrm_addr,
  165. };
  166. static int mip6_rthdr_input(struct xfrm_state *x, struct sk_buff *skb)
  167. {
  168. struct rt2_hdr *rt2 = (struct rt2_hdr *)skb->data;
  169. if (!ipv6_addr_equal(&rt2->addr, (struct in6_addr *)x->coaddr) &&
  170. !ipv6_addr_any((struct in6_addr *)x->coaddr))
  171. return -ENOENT;
  172. return rt2->rt_hdr.nexthdr;
  173. }
  174. /* Routing Header type 2 is inserted.
  175. * IP Header's dst address is replaced with Routing Header's Home Address.
  176. */
  177. static int mip6_rthdr_output(struct xfrm_state *x, struct sk_buff *skb)
  178. {
  179. struct ipv6hdr *iph;
  180. struct rt2_hdr *rt2;
  181. u8 nexthdr;
  182. iph = (struct ipv6hdr *)skb->data;
  183. iph->payload_len = htons(skb->len - sizeof(*iph));
  184. nexthdr = *skb->nh.raw;
  185. *skb->nh.raw = IPPROTO_ROUTING;
  186. rt2 = (struct rt2_hdr *)skb->h.raw;
  187. rt2->rt_hdr.nexthdr = nexthdr;
  188. rt2->rt_hdr.hdrlen = (x->props.header_len >> 3) - 1;
  189. rt2->rt_hdr.type = IPV6_SRCRT_TYPE_2;
  190. rt2->rt_hdr.segments_left = 1;
  191. memset(&rt2->reserved, 0, sizeof(rt2->reserved));
  192. BUG_TRAP(rt2->rt_hdr.hdrlen == 2);
  193. memcpy(&rt2->addr, &iph->daddr, sizeof(rt2->addr));
  194. memcpy(&iph->daddr, x->coaddr, sizeof(iph->daddr));
  195. return 0;
  196. }
  197. static int mip6_rthdr_offset(struct xfrm_state *x, struct sk_buff *skb,
  198. u8 **nexthdr)
  199. {
  200. u16 offset = sizeof(struct ipv6hdr);
  201. struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.ipv6h + 1);
  202. unsigned int packet_len = skb->tail - skb->nh.raw;
  203. int found_rhdr = 0;
  204. *nexthdr = &skb->nh.ipv6h->nexthdr;
  205. while (offset + 1 <= packet_len) {
  206. switch (**nexthdr) {
  207. case NEXTHDR_HOP:
  208. break;
  209. case NEXTHDR_ROUTING:
  210. if (offset + 3 <= packet_len) {
  211. struct ipv6_rt_hdr *rt;
  212. rt = (struct ipv6_rt_hdr *)(skb->nh.raw + offset);
  213. if (rt->type != 0)
  214. return offset;
  215. }
  216. found_rhdr = 1;
  217. break;
  218. case NEXTHDR_DEST:
  219. if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
  220. return offset;
  221. if (found_rhdr)
  222. return offset;
  223. break;
  224. default:
  225. return offset;
  226. }
  227. offset += ipv6_optlen(exthdr);
  228. *nexthdr = &exthdr->nexthdr;
  229. exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
  230. }
  231. return offset;
  232. }
  233. static int mip6_rthdr_init_state(struct xfrm_state *x)
  234. {
  235. if (x->id.spi) {
  236. printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
  237. x->id.spi);
  238. return -EINVAL;
  239. }
  240. if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
  241. printk(KERN_INFO "%s: state's mode is not %u: %u\n",
  242. __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
  243. return -EINVAL;
  244. }
  245. x->props.header_len = sizeof(struct rt2_hdr);
  246. return 0;
  247. }
  248. /*
  249. * Do nothing about destroying since it has no specific operation for routing
  250. * header type 2 unlike IPsec protocols.
  251. */
  252. static void mip6_rthdr_destroy(struct xfrm_state *x)
  253. {
  254. }
  255. static struct xfrm_type mip6_rthdr_type =
  256. {
  257. .description = "MIP6RT",
  258. .owner = THIS_MODULE,
  259. .proto = IPPROTO_ROUTING,
  260. .flags = XFRM_TYPE_NON_FRAGMENT,
  261. .init_state = mip6_rthdr_init_state,
  262. .destructor = mip6_rthdr_destroy,
  263. .input = mip6_rthdr_input,
  264. .output = mip6_rthdr_output,
  265. .hdr_offset = mip6_rthdr_offset,
  266. .remote_addr = mip6_xfrm_addr,
  267. };
  268. int __init mip6_init(void)
  269. {
  270. printk(KERN_INFO "Mobile IPv6\n");
  271. if (xfrm_register_type(&mip6_destopt_type, AF_INET6) < 0) {
  272. printk(KERN_INFO "%s: can't add xfrm type(destopt)\n", __FUNCTION__);
  273. goto mip6_destopt_xfrm_fail;
  274. }
  275. if (xfrm_register_type(&mip6_rthdr_type, AF_INET6) < 0) {
  276. printk(KERN_INFO "%s: can't add xfrm type(rthdr)\n", __FUNCTION__);
  277. goto mip6_rthdr_xfrm_fail;
  278. }
  279. return 0;
  280. mip6_rthdr_xfrm_fail:
  281. xfrm_unregister_type(&mip6_destopt_type, AF_INET6);
  282. mip6_destopt_xfrm_fail:
  283. return -EAGAIN;
  284. }
  285. void __exit mip6_fini(void)
  286. {
  287. if (xfrm_unregister_type(&mip6_rthdr_type, AF_INET6) < 0)
  288. printk(KERN_INFO "%s: can't remove xfrm type(rthdr)\n", __FUNCTION__);
  289. if (xfrm_unregister_type(&mip6_destopt_type, AF_INET6) < 0)
  290. printk(KERN_INFO "%s: can't remove xfrm type(destopt)\n", __FUNCTION__);
  291. }