mip6.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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/module.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/time.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. if (!pskb_may_pull(skb, (skb->h.raw - skb->data) + 8) ||
  85. !pskb_may_pull(skb, (skb->h.raw - skb->data) + ((skb->h.raw[1] + 1) << 3)))
  86. return -1;
  87. mh = (struct ip6_mh *)skb->h.raw;
  88. if (mh->ip6mh_hdrlen < mip6_mh_len(mh->ip6mh_type)) {
  89. LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH message too short: %d vs >=%d\n",
  90. mh->ip6mh_hdrlen, mip6_mh_len(mh->ip6mh_type));
  91. mip6_param_prob(skb, 0, (&mh->ip6mh_hdrlen) - skb->nh.raw);
  92. return -1;
  93. }
  94. if (mh->ip6mh_proto != IPPROTO_NONE) {
  95. LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH invalid payload proto = %d\n",
  96. mh->ip6mh_proto);
  97. mip6_param_prob(skb, 0, (&mh->ip6mh_proto) - skb->nh.raw);
  98. return -1;
  99. }
  100. return 0;
  101. }
  102. struct mip6_report_rate_limiter {
  103. spinlock_t lock;
  104. struct timeval stamp;
  105. int iif;
  106. struct in6_addr src;
  107. struct in6_addr dst;
  108. };
  109. static struct mip6_report_rate_limiter mip6_report_rl = {
  110. .lock = SPIN_LOCK_UNLOCKED
  111. };
  112. static int mip6_destopt_input(struct xfrm_state *x, struct sk_buff *skb)
  113. {
  114. struct ipv6hdr *iph = skb->nh.ipv6h;
  115. struct ipv6_destopt_hdr *destopt = (struct ipv6_destopt_hdr *)skb->data;
  116. if (!ipv6_addr_equal(&iph->saddr, (struct in6_addr *)x->coaddr) &&
  117. !ipv6_addr_any((struct in6_addr *)x->coaddr))
  118. return -ENOENT;
  119. return destopt->nexthdr;
  120. }
  121. /* Destination Option Header is inserted.
  122. * IP Header's src address is replaced with Home Address Option in
  123. * Destination Option Header.
  124. */
  125. static int mip6_destopt_output(struct xfrm_state *x, struct sk_buff *skb)
  126. {
  127. struct ipv6hdr *iph;
  128. struct ipv6_destopt_hdr *dstopt;
  129. struct ipv6_destopt_hao *hao;
  130. u8 nexthdr;
  131. int len;
  132. iph = (struct ipv6hdr *)skb->data;
  133. iph->payload_len = htons(skb->len - sizeof(*iph));
  134. nexthdr = *skb->nh.raw;
  135. *skb->nh.raw = IPPROTO_DSTOPTS;
  136. dstopt = (struct ipv6_destopt_hdr *)skb->h.raw;
  137. dstopt->nexthdr = nexthdr;
  138. hao = mip6_padn((char *)(dstopt + 1),
  139. calc_padlen(sizeof(*dstopt), 6));
  140. hao->type = IPV6_TLV_HAO;
  141. hao->length = sizeof(*hao) - 2;
  142. BUG_TRAP(hao->length == 16);
  143. len = ((char *)hao - (char *)dstopt) + sizeof(*hao);
  144. memcpy(&hao->addr, &iph->saddr, sizeof(hao->addr));
  145. memcpy(&iph->saddr, x->coaddr, sizeof(iph->saddr));
  146. BUG_TRAP(len == x->props.header_len);
  147. dstopt->hdrlen = (x->props.header_len >> 3) - 1;
  148. return 0;
  149. }
  150. static inline int mip6_report_rl_allow(struct timeval *stamp,
  151. struct in6_addr *dst,
  152. struct in6_addr *src, int iif)
  153. {
  154. int allow = 0;
  155. spin_lock_bh(&mip6_report_rl.lock);
  156. if (mip6_report_rl.stamp.tv_sec != stamp->tv_sec ||
  157. mip6_report_rl.stamp.tv_usec != stamp->tv_usec ||
  158. mip6_report_rl.iif != iif ||
  159. !ipv6_addr_equal(&mip6_report_rl.src, src) ||
  160. !ipv6_addr_equal(&mip6_report_rl.dst, dst)) {
  161. mip6_report_rl.stamp.tv_sec = stamp->tv_sec;
  162. mip6_report_rl.stamp.tv_usec = stamp->tv_usec;
  163. mip6_report_rl.iif = iif;
  164. ipv6_addr_copy(&mip6_report_rl.src, src);
  165. ipv6_addr_copy(&mip6_report_rl.dst, dst);
  166. allow = 1;
  167. }
  168. spin_unlock_bh(&mip6_report_rl.lock);
  169. return allow;
  170. }
  171. static int mip6_destopt_reject(struct xfrm_state *x, struct sk_buff *skb, struct flowi *fl)
  172. {
  173. struct inet6_skb_parm *opt = (struct inet6_skb_parm *)skb->cb;
  174. struct ipv6_destopt_hao *hao = NULL;
  175. struct xfrm_selector sel;
  176. int offset;
  177. struct timeval stamp;
  178. int err = 0;
  179. if (unlikely(fl->proto == IPPROTO_MH &&
  180. fl->fl_mh_type <= IP6_MH_TYPE_MAX))
  181. goto out;
  182. if (likely(opt->dsthao)) {
  183. offset = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO);
  184. if (likely(offset >= 0))
  185. hao = (struct ipv6_destopt_hao *)(skb->nh.raw + offset);
  186. }
  187. skb_get_timestamp(skb, &stamp);
  188. if (!mip6_report_rl_allow(&stamp, &skb->nh.ipv6h->daddr,
  189. hao ? &hao->addr : &skb->nh.ipv6h->saddr,
  190. opt->iif))
  191. goto out;
  192. memset(&sel, 0, sizeof(sel));
  193. memcpy(&sel.daddr, (xfrm_address_t *)&skb->nh.ipv6h->daddr,
  194. sizeof(sel.daddr));
  195. sel.prefixlen_d = 128;
  196. memcpy(&sel.saddr, (xfrm_address_t *)&skb->nh.ipv6h->saddr,
  197. sizeof(sel.saddr));
  198. sel.prefixlen_s = 128;
  199. sel.family = AF_INET6;
  200. sel.proto = fl->proto;
  201. sel.dport = xfrm_flowi_dport(fl);
  202. if (sel.dport)
  203. sel.dport_mask = htons(~0);
  204. sel.sport = xfrm_flowi_sport(fl);
  205. if (sel.sport)
  206. sel.sport_mask = htons(~0);
  207. sel.ifindex = fl->oif;
  208. err = km_report(IPPROTO_DSTOPTS, &sel,
  209. (hao ? (xfrm_address_t *)&hao->addr : NULL));
  210. out:
  211. return err;
  212. }
  213. static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
  214. u8 **nexthdr)
  215. {
  216. u16 offset = sizeof(struct ipv6hdr);
  217. struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.ipv6h + 1);
  218. unsigned int packet_len = skb->tail - skb->nh.raw;
  219. int found_rhdr = 0;
  220. *nexthdr = &skb->nh.ipv6h->nexthdr;
  221. while (offset + 1 <= packet_len) {
  222. switch (**nexthdr) {
  223. case NEXTHDR_HOP:
  224. break;
  225. case NEXTHDR_ROUTING:
  226. found_rhdr = 1;
  227. break;
  228. case NEXTHDR_DEST:
  229. /*
  230. * HAO MUST NOT appear more than once.
  231. * XXX: It is better to try to find by the end of
  232. * XXX: packet if HAO exists.
  233. */
  234. if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0) {
  235. LIMIT_NETDEBUG(KERN_WARNING "mip6: hao exists already, override\n");
  236. return offset;
  237. }
  238. if (found_rhdr)
  239. return offset;
  240. break;
  241. default:
  242. return offset;
  243. }
  244. offset += ipv6_optlen(exthdr);
  245. *nexthdr = &exthdr->nexthdr;
  246. exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
  247. }
  248. return offset;
  249. }
  250. static int mip6_destopt_init_state(struct xfrm_state *x)
  251. {
  252. if (x->id.spi) {
  253. printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
  254. x->id.spi);
  255. return -EINVAL;
  256. }
  257. if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
  258. printk(KERN_INFO "%s: state's mode is not %u: %u\n",
  259. __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
  260. return -EINVAL;
  261. }
  262. x->props.header_len = sizeof(struct ipv6_destopt_hdr) +
  263. calc_padlen(sizeof(struct ipv6_destopt_hdr), 6) +
  264. sizeof(struct ipv6_destopt_hao);
  265. BUG_TRAP(x->props.header_len == 24);
  266. return 0;
  267. }
  268. /*
  269. * Do nothing about destroying since it has no specific operation for
  270. * destination options header unlike IPsec protocols.
  271. */
  272. static void mip6_destopt_destroy(struct xfrm_state *x)
  273. {
  274. }
  275. static struct xfrm_type mip6_destopt_type =
  276. {
  277. .description = "MIP6DESTOPT",
  278. .owner = THIS_MODULE,
  279. .proto = IPPROTO_DSTOPTS,
  280. .flags = XFRM_TYPE_NON_FRAGMENT,
  281. .init_state = mip6_destopt_init_state,
  282. .destructor = mip6_destopt_destroy,
  283. .input = mip6_destopt_input,
  284. .output = mip6_destopt_output,
  285. .reject = mip6_destopt_reject,
  286. .hdr_offset = mip6_destopt_offset,
  287. .local_addr = mip6_xfrm_addr,
  288. };
  289. static int mip6_rthdr_input(struct xfrm_state *x, struct sk_buff *skb)
  290. {
  291. struct rt2_hdr *rt2 = (struct rt2_hdr *)skb->data;
  292. if (!ipv6_addr_equal(&rt2->addr, (struct in6_addr *)x->coaddr) &&
  293. !ipv6_addr_any((struct in6_addr *)x->coaddr))
  294. return -ENOENT;
  295. return rt2->rt_hdr.nexthdr;
  296. }
  297. /* Routing Header type 2 is inserted.
  298. * IP Header's dst address is replaced with Routing Header's Home Address.
  299. */
  300. static int mip6_rthdr_output(struct xfrm_state *x, struct sk_buff *skb)
  301. {
  302. struct ipv6hdr *iph;
  303. struct rt2_hdr *rt2;
  304. u8 nexthdr;
  305. iph = (struct ipv6hdr *)skb->data;
  306. iph->payload_len = htons(skb->len - sizeof(*iph));
  307. nexthdr = *skb->nh.raw;
  308. *skb->nh.raw = IPPROTO_ROUTING;
  309. rt2 = (struct rt2_hdr *)skb->h.raw;
  310. rt2->rt_hdr.nexthdr = nexthdr;
  311. rt2->rt_hdr.hdrlen = (x->props.header_len >> 3) - 1;
  312. rt2->rt_hdr.type = IPV6_SRCRT_TYPE_2;
  313. rt2->rt_hdr.segments_left = 1;
  314. memset(&rt2->reserved, 0, sizeof(rt2->reserved));
  315. BUG_TRAP(rt2->rt_hdr.hdrlen == 2);
  316. memcpy(&rt2->addr, &iph->daddr, sizeof(rt2->addr));
  317. memcpy(&iph->daddr, x->coaddr, sizeof(iph->daddr));
  318. return 0;
  319. }
  320. static int mip6_rthdr_offset(struct xfrm_state *x, struct sk_buff *skb,
  321. u8 **nexthdr)
  322. {
  323. u16 offset = sizeof(struct ipv6hdr);
  324. struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.ipv6h + 1);
  325. unsigned int packet_len = skb->tail - skb->nh.raw;
  326. int found_rhdr = 0;
  327. *nexthdr = &skb->nh.ipv6h->nexthdr;
  328. while (offset + 1 <= packet_len) {
  329. switch (**nexthdr) {
  330. case NEXTHDR_HOP:
  331. break;
  332. case NEXTHDR_ROUTING:
  333. if (offset + 3 <= packet_len) {
  334. struct ipv6_rt_hdr *rt;
  335. rt = (struct ipv6_rt_hdr *)(skb->nh.raw + offset);
  336. if (rt->type != 0)
  337. return offset;
  338. }
  339. found_rhdr = 1;
  340. break;
  341. case NEXTHDR_DEST:
  342. if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
  343. return offset;
  344. if (found_rhdr)
  345. return offset;
  346. break;
  347. default:
  348. return offset;
  349. }
  350. offset += ipv6_optlen(exthdr);
  351. *nexthdr = &exthdr->nexthdr;
  352. exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
  353. }
  354. return offset;
  355. }
  356. static int mip6_rthdr_init_state(struct xfrm_state *x)
  357. {
  358. if (x->id.spi) {
  359. printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
  360. x->id.spi);
  361. return -EINVAL;
  362. }
  363. if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
  364. printk(KERN_INFO "%s: state's mode is not %u: %u\n",
  365. __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
  366. return -EINVAL;
  367. }
  368. x->props.header_len = sizeof(struct rt2_hdr);
  369. return 0;
  370. }
  371. /*
  372. * Do nothing about destroying since it has no specific operation for routing
  373. * header type 2 unlike IPsec protocols.
  374. */
  375. static void mip6_rthdr_destroy(struct xfrm_state *x)
  376. {
  377. }
  378. static struct xfrm_type mip6_rthdr_type =
  379. {
  380. .description = "MIP6RT",
  381. .owner = THIS_MODULE,
  382. .proto = IPPROTO_ROUTING,
  383. .flags = XFRM_TYPE_NON_FRAGMENT,
  384. .init_state = mip6_rthdr_init_state,
  385. .destructor = mip6_rthdr_destroy,
  386. .input = mip6_rthdr_input,
  387. .output = mip6_rthdr_output,
  388. .hdr_offset = mip6_rthdr_offset,
  389. .remote_addr = mip6_xfrm_addr,
  390. };
  391. int __init mip6_init(void)
  392. {
  393. printk(KERN_INFO "Mobile IPv6\n");
  394. if (xfrm_register_type(&mip6_destopt_type, AF_INET6) < 0) {
  395. printk(KERN_INFO "%s: can't add xfrm type(destopt)\n", __FUNCTION__);
  396. goto mip6_destopt_xfrm_fail;
  397. }
  398. if (xfrm_register_type(&mip6_rthdr_type, AF_INET6) < 0) {
  399. printk(KERN_INFO "%s: can't add xfrm type(rthdr)\n", __FUNCTION__);
  400. goto mip6_rthdr_xfrm_fail;
  401. }
  402. return 0;
  403. mip6_rthdr_xfrm_fail:
  404. xfrm_unregister_type(&mip6_destopt_type, AF_INET6);
  405. mip6_destopt_xfrm_fail:
  406. return -EAGAIN;
  407. }
  408. void __exit mip6_fini(void)
  409. {
  410. if (xfrm_unregister_type(&mip6_rthdr_type, AF_INET6) < 0)
  411. printk(KERN_INFO "%s: can't remove xfrm type(rthdr)\n", __FUNCTION__);
  412. if (xfrm_unregister_type(&mip6_destopt_type, AF_INET6) < 0)
  413. printk(KERN_INFO "%s: can't remove xfrm type(destopt)\n", __FUNCTION__);
  414. }