mip6.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 int mip6_rthdr_input(struct xfrm_state *x, struct sk_buff *skb)
  36. {
  37. struct rt2_hdr *rt2 = (struct rt2_hdr *)skb->data;
  38. if (!ipv6_addr_equal(&rt2->addr, (struct in6_addr *)x->coaddr) &&
  39. !ipv6_addr_any((struct in6_addr *)x->coaddr))
  40. return -ENOENT;
  41. return rt2->rt_hdr.nexthdr;
  42. }
  43. /* Routing Header type 2 is inserted.
  44. * IP Header's dst address is replaced with Routing Header's Home Address.
  45. */
  46. static int mip6_rthdr_output(struct xfrm_state *x, struct sk_buff *skb)
  47. {
  48. struct ipv6hdr *iph;
  49. struct rt2_hdr *rt2;
  50. u8 nexthdr;
  51. iph = (struct ipv6hdr *)skb->data;
  52. iph->payload_len = htons(skb->len - sizeof(*iph));
  53. nexthdr = *skb->nh.raw;
  54. *skb->nh.raw = IPPROTO_ROUTING;
  55. rt2 = (struct rt2_hdr *)skb->h.raw;
  56. rt2->rt_hdr.nexthdr = nexthdr;
  57. rt2->rt_hdr.hdrlen = (x->props.header_len >> 3) - 1;
  58. rt2->rt_hdr.type = IPV6_SRCRT_TYPE_2;
  59. rt2->rt_hdr.segments_left = 1;
  60. memset(&rt2->reserved, 0, sizeof(rt2->reserved));
  61. BUG_TRAP(rt2->rt_hdr.hdrlen == 2);
  62. memcpy(&rt2->addr, &iph->daddr, sizeof(rt2->addr));
  63. memcpy(&iph->daddr, x->coaddr, sizeof(iph->daddr));
  64. return 0;
  65. }
  66. static int mip6_rthdr_offset(struct xfrm_state *x, struct sk_buff *skb,
  67. u8 **nexthdr)
  68. {
  69. u16 offset = sizeof(struct ipv6hdr);
  70. struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.ipv6h + 1);
  71. unsigned int packet_len = skb->tail - skb->nh.raw;
  72. int found_rhdr = 0;
  73. *nexthdr = &skb->nh.ipv6h->nexthdr;
  74. while (offset + 1 <= packet_len) {
  75. switch (**nexthdr) {
  76. case NEXTHDR_HOP:
  77. break;
  78. case NEXTHDR_ROUTING:
  79. if (offset + 3 <= packet_len) {
  80. struct ipv6_rt_hdr *rt;
  81. rt = (struct ipv6_rt_hdr *)(skb->nh.raw + offset);
  82. if (rt->type != 0)
  83. return offset;
  84. }
  85. found_rhdr = 1;
  86. break;
  87. case NEXTHDR_DEST:
  88. if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
  89. return offset;
  90. if (found_rhdr)
  91. return offset;
  92. break;
  93. default:
  94. return offset;
  95. }
  96. offset += ipv6_optlen(exthdr);
  97. *nexthdr = &exthdr->nexthdr;
  98. exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
  99. }
  100. return offset;
  101. }
  102. static int mip6_rthdr_init_state(struct xfrm_state *x)
  103. {
  104. if (x->id.spi) {
  105. printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
  106. x->id.spi);
  107. return -EINVAL;
  108. }
  109. if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
  110. printk(KERN_INFO "%s: state's mode is not %u: %u\n",
  111. __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
  112. return -EINVAL;
  113. }
  114. x->props.header_len = sizeof(struct rt2_hdr);
  115. return 0;
  116. }
  117. /*
  118. * Do nothing about destroying since it has no specific operation for routing
  119. * header type 2 unlike IPsec protocols.
  120. */
  121. static void mip6_rthdr_destroy(struct xfrm_state *x)
  122. {
  123. }
  124. static struct xfrm_type mip6_rthdr_type =
  125. {
  126. .description = "MIP6RT",
  127. .owner = THIS_MODULE,
  128. .proto = IPPROTO_ROUTING,
  129. .flags = XFRM_TYPE_NON_FRAGMENT,
  130. .init_state = mip6_rthdr_init_state,
  131. .destructor = mip6_rthdr_destroy,
  132. .input = mip6_rthdr_input,
  133. .output = mip6_rthdr_output,
  134. .hdr_offset = mip6_rthdr_offset,
  135. .remote_addr = mip6_xfrm_addr,
  136. };
  137. int __init mip6_init(void)
  138. {
  139. printk(KERN_INFO "Mobile IPv6\n");
  140. if (xfrm_register_type(&mip6_rthdr_type, AF_INET6) < 0) {
  141. printk(KERN_INFO "%s: can't add xfrm type(rthdr)\n", __FUNCTION__);
  142. goto mip6_rthdr_xfrm_fail;
  143. }
  144. return 0;
  145. mip6_rthdr_xfrm_fail:
  146. return -EAGAIN;
  147. }
  148. void __exit mip6_fini(void)
  149. {
  150. if (xfrm_unregister_type(&mip6_rthdr_type, AF_INET6) < 0)
  151. printk(KERN_INFO "%s: can't remove xfrm type(rthdr)\n", __FUNCTION__);
  152. }