mip6.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 <linux/icmpv6.h>
  29. #include <net/sock.h>
  30. #include <net/ipv6.h>
  31. #include <net/ip6_checksum.h>
  32. #include <net/xfrm.h>
  33. #include <net/mip6.h>
  34. static xfrm_address_t *mip6_xfrm_addr(struct xfrm_state *x, xfrm_address_t *addr)
  35. {
  36. return x->coaddr;
  37. }
  38. static inline unsigned int calc_padlen(unsigned int len, unsigned int n)
  39. {
  40. return (n - len + 16) & 0x7;
  41. }
  42. static inline void *mip6_padn(__u8 *data, __u8 padlen)
  43. {
  44. if (!data)
  45. return NULL;
  46. if (padlen == 1) {
  47. data[0] = MIP6_OPT_PAD_1;
  48. } else if (padlen > 1) {
  49. data[0] = MIP6_OPT_PAD_N;
  50. data[1] = padlen - 2;
  51. if (padlen > 2)
  52. memset(data+2, 0, data[1]);
  53. }
  54. return data + padlen;
  55. }
  56. static inline void mip6_param_prob(struct sk_buff *skb, int code, int pos)
  57. {
  58. icmpv6_send(skb, ICMPV6_PARAMPROB, code, pos, skb->dev);
  59. }
  60. static int mip6_mh_len(int type)
  61. {
  62. int len = 0;
  63. switch (type) {
  64. case IP6_MH_TYPE_BRR:
  65. len = 0;
  66. break;
  67. case IP6_MH_TYPE_HOTI:
  68. case IP6_MH_TYPE_COTI:
  69. case IP6_MH_TYPE_BU:
  70. case IP6_MH_TYPE_BACK:
  71. len = 1;
  72. break;
  73. case IP6_MH_TYPE_HOT:
  74. case IP6_MH_TYPE_COT:
  75. case IP6_MH_TYPE_BERROR:
  76. len = 2;
  77. break;
  78. }
  79. return len;
  80. }
  81. int mip6_mh_filter(struct sock *sk, struct sk_buff *skb)
  82. {
  83. struct ip6_mh *mh;
  84. int mhlen;
  85. if (!pskb_may_pull(skb, (skb->h.raw - skb->data) + 8) ||
  86. !pskb_may_pull(skb, (skb->h.raw - skb->data) + ((skb->h.raw[1] + 1) << 3)))
  87. return -1;
  88. mh = (struct ip6_mh *)skb->h.raw;
  89. if (mh->ip6mh_hdrlen < mip6_mh_len(mh->ip6mh_type)) {
  90. LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH message too short: %d vs >=%d\n",
  91. mh->ip6mh_hdrlen, mip6_mh_len(mh->ip6mh_type));
  92. mip6_param_prob(skb, 0, (&mh->ip6mh_hdrlen) - skb->nh.raw);
  93. return -1;
  94. }
  95. mhlen = (mh->ip6mh_hdrlen + 1) << 3;
  96. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  97. skb->ip_summed = CHECKSUM_UNNECESSARY;
  98. if (csum_ipv6_magic(&skb->nh.ipv6h->saddr,
  99. &skb->nh.ipv6h->daddr,
  100. mhlen, IPPROTO_MH,
  101. skb->csum)) {
  102. LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH hw checksum failed\n");
  103. skb->ip_summed = CHECKSUM_NONE;
  104. }
  105. }
  106. if (skb->ip_summed == CHECKSUM_NONE) {
  107. if (csum_ipv6_magic(&skb->nh.ipv6h->saddr,
  108. &skb->nh.ipv6h->daddr,
  109. mhlen, IPPROTO_MH,
  110. skb_checksum(skb, 0, mhlen, 0))) {
  111. LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH checksum failed [%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x > %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x]\n",
  112. NIP6(skb->nh.ipv6h->saddr),
  113. NIP6(skb->nh.ipv6h->daddr));
  114. return -1;
  115. }
  116. skb->ip_summed = CHECKSUM_UNNECESSARY;
  117. }
  118. if (mh->ip6mh_proto != IPPROTO_NONE) {
  119. LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH invalid payload proto = %d\n",
  120. mh->ip6mh_proto);
  121. mip6_param_prob(skb, 0, (&mh->ip6mh_proto) - skb->nh.raw);
  122. return -1;
  123. }
  124. return 0;
  125. }
  126. static int mip6_destopt_input(struct xfrm_state *x, struct sk_buff *skb)
  127. {
  128. struct ipv6hdr *iph = skb->nh.ipv6h;
  129. struct ipv6_destopt_hdr *destopt = (struct ipv6_destopt_hdr *)skb->data;
  130. if (!ipv6_addr_equal(&iph->saddr, (struct in6_addr *)x->coaddr) &&
  131. !ipv6_addr_any((struct in6_addr *)x->coaddr))
  132. return -ENOENT;
  133. return destopt->nexthdr;
  134. }
  135. /* Destination Option Header is inserted.
  136. * IP Header's src address is replaced with Home Address Option in
  137. * Destination Option Header.
  138. */
  139. static int mip6_destopt_output(struct xfrm_state *x, struct sk_buff *skb)
  140. {
  141. struct ipv6hdr *iph;
  142. struct ipv6_destopt_hdr *dstopt;
  143. struct ipv6_destopt_hao *hao;
  144. u8 nexthdr;
  145. int len;
  146. iph = (struct ipv6hdr *)skb->data;
  147. iph->payload_len = htons(skb->len - sizeof(*iph));
  148. nexthdr = *skb->nh.raw;
  149. *skb->nh.raw = IPPROTO_DSTOPTS;
  150. dstopt = (struct ipv6_destopt_hdr *)skb->h.raw;
  151. dstopt->nexthdr = nexthdr;
  152. hao = mip6_padn((char *)(dstopt + 1),
  153. calc_padlen(sizeof(*dstopt), 6));
  154. hao->type = IPV6_TLV_HAO;
  155. hao->length = sizeof(*hao) - 2;
  156. BUG_TRAP(hao->length == 16);
  157. len = ((char *)hao - (char *)dstopt) + sizeof(*hao);
  158. memcpy(&hao->addr, &iph->saddr, sizeof(hao->addr));
  159. memcpy(&iph->saddr, x->coaddr, sizeof(iph->saddr));
  160. BUG_TRAP(len == x->props.header_len);
  161. dstopt->hdrlen = (x->props.header_len >> 3) - 1;
  162. return 0;
  163. }
  164. static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
  165. u8 **nexthdr)
  166. {
  167. u16 offset = sizeof(struct ipv6hdr);
  168. struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.ipv6h + 1);
  169. unsigned int packet_len = skb->tail - skb->nh.raw;
  170. int found_rhdr = 0;
  171. *nexthdr = &skb->nh.ipv6h->nexthdr;
  172. while (offset + 1 <= packet_len) {
  173. switch (**nexthdr) {
  174. case NEXTHDR_HOP:
  175. break;
  176. case NEXTHDR_ROUTING:
  177. found_rhdr = 1;
  178. break;
  179. case NEXTHDR_DEST:
  180. /*
  181. * HAO MUST NOT appear more than once.
  182. * XXX: It is better to try to find by the end of
  183. * XXX: packet if HAO exists.
  184. */
  185. if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0) {
  186. LIMIT_NETDEBUG(KERN_WARNING "mip6: hao exists already, override\n");
  187. return offset;
  188. }
  189. if (found_rhdr)
  190. return offset;
  191. break;
  192. default:
  193. return offset;
  194. }
  195. offset += ipv6_optlen(exthdr);
  196. *nexthdr = &exthdr->nexthdr;
  197. exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
  198. }
  199. return offset;
  200. }
  201. static int mip6_destopt_init_state(struct xfrm_state *x)
  202. {
  203. if (x->id.spi) {
  204. printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
  205. x->id.spi);
  206. return -EINVAL;
  207. }
  208. if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
  209. printk(KERN_INFO "%s: state's mode is not %u: %u\n",
  210. __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
  211. return -EINVAL;
  212. }
  213. x->props.header_len = sizeof(struct ipv6_destopt_hdr) +
  214. calc_padlen(sizeof(struct ipv6_destopt_hdr), 6) +
  215. sizeof(struct ipv6_destopt_hao);
  216. BUG_TRAP(x->props.header_len == 24);
  217. return 0;
  218. }
  219. /*
  220. * Do nothing about destroying since it has no specific operation for
  221. * destination options header unlike IPsec protocols.
  222. */
  223. static void mip6_destopt_destroy(struct xfrm_state *x)
  224. {
  225. }
  226. static struct xfrm_type mip6_destopt_type =
  227. {
  228. .description = "MIP6DESTOPT",
  229. .owner = THIS_MODULE,
  230. .proto = IPPROTO_DSTOPTS,
  231. .flags = XFRM_TYPE_NON_FRAGMENT,
  232. .init_state = mip6_destopt_init_state,
  233. .destructor = mip6_destopt_destroy,
  234. .input = mip6_destopt_input,
  235. .output = mip6_destopt_output,
  236. .hdr_offset = mip6_destopt_offset,
  237. .local_addr = mip6_xfrm_addr,
  238. };
  239. static int mip6_rthdr_input(struct xfrm_state *x, struct sk_buff *skb)
  240. {
  241. struct rt2_hdr *rt2 = (struct rt2_hdr *)skb->data;
  242. if (!ipv6_addr_equal(&rt2->addr, (struct in6_addr *)x->coaddr) &&
  243. !ipv6_addr_any((struct in6_addr *)x->coaddr))
  244. return -ENOENT;
  245. return rt2->rt_hdr.nexthdr;
  246. }
  247. /* Routing Header type 2 is inserted.
  248. * IP Header's dst address is replaced with Routing Header's Home Address.
  249. */
  250. static int mip6_rthdr_output(struct xfrm_state *x, struct sk_buff *skb)
  251. {
  252. struct ipv6hdr *iph;
  253. struct rt2_hdr *rt2;
  254. u8 nexthdr;
  255. iph = (struct ipv6hdr *)skb->data;
  256. iph->payload_len = htons(skb->len - sizeof(*iph));
  257. nexthdr = *skb->nh.raw;
  258. *skb->nh.raw = IPPROTO_ROUTING;
  259. rt2 = (struct rt2_hdr *)skb->h.raw;
  260. rt2->rt_hdr.nexthdr = nexthdr;
  261. rt2->rt_hdr.hdrlen = (x->props.header_len >> 3) - 1;
  262. rt2->rt_hdr.type = IPV6_SRCRT_TYPE_2;
  263. rt2->rt_hdr.segments_left = 1;
  264. memset(&rt2->reserved, 0, sizeof(rt2->reserved));
  265. BUG_TRAP(rt2->rt_hdr.hdrlen == 2);
  266. memcpy(&rt2->addr, &iph->daddr, sizeof(rt2->addr));
  267. memcpy(&iph->daddr, x->coaddr, sizeof(iph->daddr));
  268. return 0;
  269. }
  270. static int mip6_rthdr_offset(struct xfrm_state *x, struct sk_buff *skb,
  271. u8 **nexthdr)
  272. {
  273. u16 offset = sizeof(struct ipv6hdr);
  274. struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.ipv6h + 1);
  275. unsigned int packet_len = skb->tail - skb->nh.raw;
  276. int found_rhdr = 0;
  277. *nexthdr = &skb->nh.ipv6h->nexthdr;
  278. while (offset + 1 <= packet_len) {
  279. switch (**nexthdr) {
  280. case NEXTHDR_HOP:
  281. break;
  282. case NEXTHDR_ROUTING:
  283. if (offset + 3 <= packet_len) {
  284. struct ipv6_rt_hdr *rt;
  285. rt = (struct ipv6_rt_hdr *)(skb->nh.raw + offset);
  286. if (rt->type != 0)
  287. return offset;
  288. }
  289. found_rhdr = 1;
  290. break;
  291. case NEXTHDR_DEST:
  292. if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
  293. return offset;
  294. if (found_rhdr)
  295. return offset;
  296. break;
  297. default:
  298. return offset;
  299. }
  300. offset += ipv6_optlen(exthdr);
  301. *nexthdr = &exthdr->nexthdr;
  302. exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
  303. }
  304. return offset;
  305. }
  306. static int mip6_rthdr_init_state(struct xfrm_state *x)
  307. {
  308. if (x->id.spi) {
  309. printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
  310. x->id.spi);
  311. return -EINVAL;
  312. }
  313. if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
  314. printk(KERN_INFO "%s: state's mode is not %u: %u\n",
  315. __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
  316. return -EINVAL;
  317. }
  318. x->props.header_len = sizeof(struct rt2_hdr);
  319. return 0;
  320. }
  321. /*
  322. * Do nothing about destroying since it has no specific operation for routing
  323. * header type 2 unlike IPsec protocols.
  324. */
  325. static void mip6_rthdr_destroy(struct xfrm_state *x)
  326. {
  327. }
  328. static struct xfrm_type mip6_rthdr_type =
  329. {
  330. .description = "MIP6RT",
  331. .owner = THIS_MODULE,
  332. .proto = IPPROTO_ROUTING,
  333. .flags = XFRM_TYPE_NON_FRAGMENT,
  334. .init_state = mip6_rthdr_init_state,
  335. .destructor = mip6_rthdr_destroy,
  336. .input = mip6_rthdr_input,
  337. .output = mip6_rthdr_output,
  338. .hdr_offset = mip6_rthdr_offset,
  339. .remote_addr = mip6_xfrm_addr,
  340. };
  341. int __init mip6_init(void)
  342. {
  343. printk(KERN_INFO "Mobile IPv6\n");
  344. if (xfrm_register_type(&mip6_destopt_type, AF_INET6) < 0) {
  345. printk(KERN_INFO "%s: can't add xfrm type(destopt)\n", __FUNCTION__);
  346. goto mip6_destopt_xfrm_fail;
  347. }
  348. if (xfrm_register_type(&mip6_rthdr_type, AF_INET6) < 0) {
  349. printk(KERN_INFO "%s: can't add xfrm type(rthdr)\n", __FUNCTION__);
  350. goto mip6_rthdr_xfrm_fail;
  351. }
  352. return 0;
  353. mip6_rthdr_xfrm_fail:
  354. xfrm_unregister_type(&mip6_destopt_type, AF_INET6);
  355. mip6_destopt_xfrm_fail:
  356. return -EAGAIN;
  357. }
  358. void __exit mip6_fini(void)
  359. {
  360. if (xfrm_unregister_type(&mip6_rthdr_type, AF_INET6) < 0)
  361. printk(KERN_INFO "%s: can't remove xfrm type(rthdr)\n", __FUNCTION__);
  362. if (xfrm_unregister_type(&mip6_destopt_type, AF_INET6) < 0)
  363. printk(KERN_INFO "%s: can't remove xfrm type(destopt)\n", __FUNCTION__);
  364. }