mip6.c 12 KB

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