mip6.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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/rawv6.h>
  33. #include <net/xfrm.h>
  34. #include <net/mip6.h>
  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] = IPV6_TLV_PAD0;
  45. } else if (padlen > 1) {
  46. data[0] = IPV6_TLV_PADN;
  47. data[1] = padlen - 2;
  48. if (padlen > 2)
  49. memset(data+2, 0, data[1]);
  50. }
  51. return data + padlen;
  52. }
  53. static inline void mip6_param_prob(struct sk_buff *skb, u8 code, int pos)
  54. {
  55. icmpv6_send(skb, ICMPV6_PARAMPROB, code, pos);
  56. }
  57. static int mip6_mh_len(int type)
  58. {
  59. int len = 0;
  60. switch (type) {
  61. case IP6_MH_TYPE_BRR:
  62. len = 0;
  63. break;
  64. case IP6_MH_TYPE_HOTI:
  65. case IP6_MH_TYPE_COTI:
  66. case IP6_MH_TYPE_BU:
  67. case IP6_MH_TYPE_BACK:
  68. len = 1;
  69. break;
  70. case IP6_MH_TYPE_HOT:
  71. case IP6_MH_TYPE_COT:
  72. case IP6_MH_TYPE_BERROR:
  73. len = 2;
  74. break;
  75. }
  76. return len;
  77. }
  78. static int mip6_mh_filter(struct sock *sk, struct sk_buff *skb)
  79. {
  80. struct ip6_mh *mh;
  81. if (!pskb_may_pull(skb, (skb_transport_offset(skb)) + 8) ||
  82. !pskb_may_pull(skb, (skb_transport_offset(skb) +
  83. ((skb_transport_header(skb)[1] + 1) << 3))))
  84. return -1;
  85. mh = (struct ip6_mh *)skb_transport_header(skb);
  86. if (mh->ip6mh_hdrlen < mip6_mh_len(mh->ip6mh_type)) {
  87. LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH message too short: %d vs >=%d\n",
  88. mh->ip6mh_hdrlen, mip6_mh_len(mh->ip6mh_type));
  89. mip6_param_prob(skb, 0, ((&mh->ip6mh_hdrlen) -
  90. skb_network_header(skb)));
  91. return -1;
  92. }
  93. if (mh->ip6mh_proto != IPPROTO_NONE) {
  94. LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH invalid payload proto = %d\n",
  95. mh->ip6mh_proto);
  96. mip6_param_prob(skb, 0, ((&mh->ip6mh_proto) -
  97. skb_network_header(skb)));
  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(mip6_report_rl.lock)
  111. };
  112. static int mip6_destopt_input(struct xfrm_state *x, struct sk_buff *skb)
  113. {
  114. const struct ipv6hdr *iph = ipv6_hdr(skb);
  115. struct ipv6_destopt_hdr *destopt = (struct ipv6_destopt_hdr *)skb->data;
  116. int err = destopt->nexthdr;
  117. spin_lock(&x->lock);
  118. if (!ipv6_addr_equal(&iph->saddr, (struct in6_addr *)x->coaddr) &&
  119. !ipv6_addr_any((struct in6_addr *)x->coaddr))
  120. err = -ENOENT;
  121. spin_unlock(&x->lock);
  122. return err;
  123. }
  124. /* Destination Option Header is inserted.
  125. * IP Header's src address is replaced with Home Address Option in
  126. * Destination Option Header.
  127. */
  128. static int mip6_destopt_output(struct xfrm_state *x, struct sk_buff *skb)
  129. {
  130. struct ipv6hdr *iph;
  131. struct ipv6_destopt_hdr *dstopt;
  132. struct ipv6_destopt_hao *hao;
  133. u8 nexthdr;
  134. int len;
  135. skb_push(skb, -skb_network_offset(skb));
  136. iph = ipv6_hdr(skb);
  137. nexthdr = *skb_mac_header(skb);
  138. *skb_mac_header(skb) = IPPROTO_DSTOPTS;
  139. dstopt = (struct ipv6_destopt_hdr *)skb_transport_header(skb);
  140. dstopt->nexthdr = nexthdr;
  141. hao = mip6_padn((char *)(dstopt + 1),
  142. calc_padlen(sizeof(*dstopt), 6));
  143. hao->type = IPV6_TLV_HAO;
  144. BUILD_BUG_ON(sizeof(*hao) != 18);
  145. hao->length = sizeof(*hao) - 2;
  146. len = ((char *)hao - (char *)dstopt) + sizeof(*hao);
  147. memcpy(&hao->addr, &iph->saddr, sizeof(hao->addr));
  148. spin_lock_bh(&x->lock);
  149. memcpy(&iph->saddr, x->coaddr, sizeof(iph->saddr));
  150. spin_unlock_bh(&x->lock);
  151. WARN_ON(len != x->props.header_len);
  152. dstopt->hdrlen = (x->props.header_len >> 3) - 1;
  153. return 0;
  154. }
  155. static inline int mip6_report_rl_allow(struct timeval *stamp,
  156. const struct in6_addr *dst,
  157. const struct in6_addr *src, int iif)
  158. {
  159. int allow = 0;
  160. spin_lock_bh(&mip6_report_rl.lock);
  161. if (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_equal(&mip6_report_rl.src, src) ||
  165. !ipv6_addr_equal(&mip6_report_rl.dst, dst)) {
  166. mip6_report_rl.stamp.tv_sec = stamp->tv_sec;
  167. mip6_report_rl.stamp.tv_usec = stamp->tv_usec;
  168. mip6_report_rl.iif = iif;
  169. ipv6_addr_copy(&mip6_report_rl.src, src);
  170. ipv6_addr_copy(&mip6_report_rl.dst, dst);
  171. allow = 1;
  172. }
  173. spin_unlock_bh(&mip6_report_rl.lock);
  174. return allow;
  175. }
  176. static int mip6_destopt_reject(struct xfrm_state *x, struct sk_buff *skb,
  177. const struct flowi *fl)
  178. {
  179. struct net *net = xs_net(x);
  180. struct inet6_skb_parm *opt = (struct inet6_skb_parm *)skb->cb;
  181. const struct flowi6 *fl6 = &fl->u.ip6;
  182. struct ipv6_destopt_hao *hao = NULL;
  183. struct xfrm_selector sel;
  184. int offset;
  185. struct timeval stamp;
  186. int err = 0;
  187. if (unlikely(fl6->flowi6_proto == IPPROTO_MH &&
  188. fl6->fl6_mh_type <= IP6_MH_TYPE_MAX))
  189. goto out;
  190. if (likely(opt->dsthao)) {
  191. offset = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO);
  192. if (likely(offset >= 0))
  193. hao = (struct ipv6_destopt_hao *)
  194. (skb_network_header(skb) + offset);
  195. }
  196. skb_get_timestamp(skb, &stamp);
  197. if (!mip6_report_rl_allow(&stamp, &ipv6_hdr(skb)->daddr,
  198. hao ? &hao->addr : &ipv6_hdr(skb)->saddr,
  199. opt->iif))
  200. goto out;
  201. memset(&sel, 0, sizeof(sel));
  202. memcpy(&sel.daddr, (xfrm_address_t *)&ipv6_hdr(skb)->daddr,
  203. sizeof(sel.daddr));
  204. sel.prefixlen_d = 128;
  205. memcpy(&sel.saddr, (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
  206. sizeof(sel.saddr));
  207. sel.prefixlen_s = 128;
  208. sel.family = AF_INET6;
  209. sel.proto = fl6->flowi6_proto;
  210. sel.dport = xfrm_flowi_dport(fl, &fl6->uli);
  211. if (sel.dport)
  212. sel.dport_mask = htons(~0);
  213. sel.sport = xfrm_flowi_sport(fl, &fl6->uli);
  214. if (sel.sport)
  215. sel.sport_mask = htons(~0);
  216. sel.ifindex = fl6->flowi6_oif;
  217. err = km_report(net, IPPROTO_DSTOPTS, &sel,
  218. (hao ? (xfrm_address_t *)&hao->addr : NULL));
  219. out:
  220. return err;
  221. }
  222. static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
  223. u8 **nexthdr)
  224. {
  225. u16 offset = sizeof(struct ipv6hdr);
  226. struct ipv6_opt_hdr *exthdr =
  227. (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
  228. const unsigned char *nh = skb_network_header(skb);
  229. unsigned int packet_len = skb->tail - skb->network_header;
  230. int found_rhdr = 0;
  231. *nexthdr = &ipv6_hdr(skb)->nexthdr;
  232. while (offset + 1 <= packet_len) {
  233. switch (**nexthdr) {
  234. case NEXTHDR_HOP:
  235. break;
  236. case NEXTHDR_ROUTING:
  237. found_rhdr = 1;
  238. break;
  239. case NEXTHDR_DEST:
  240. /*
  241. * HAO MUST NOT appear more than once.
  242. * XXX: It is better to try to find by the end of
  243. * XXX: packet if HAO exists.
  244. */
  245. if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0) {
  246. LIMIT_NETDEBUG(KERN_WARNING "mip6: hao exists already, override\n");
  247. return offset;
  248. }
  249. if (found_rhdr)
  250. return offset;
  251. break;
  252. default:
  253. return offset;
  254. }
  255. offset += ipv6_optlen(exthdr);
  256. *nexthdr = &exthdr->nexthdr;
  257. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  258. }
  259. return offset;
  260. }
  261. static int mip6_destopt_init_state(struct xfrm_state *x)
  262. {
  263. if (x->id.spi) {
  264. printk(KERN_INFO "%s: spi is not 0: %u\n", __func__,
  265. x->id.spi);
  266. return -EINVAL;
  267. }
  268. if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
  269. printk(KERN_INFO "%s: state's mode is not %u: %u\n",
  270. __func__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
  271. return -EINVAL;
  272. }
  273. x->props.header_len = sizeof(struct ipv6_destopt_hdr) +
  274. calc_padlen(sizeof(struct ipv6_destopt_hdr), 6) +
  275. sizeof(struct ipv6_destopt_hao);
  276. WARN_ON(x->props.header_len != 24);
  277. return 0;
  278. }
  279. /*
  280. * Do nothing about destroying since it has no specific operation for
  281. * destination options header unlike IPsec protocols.
  282. */
  283. static void mip6_destopt_destroy(struct xfrm_state *x)
  284. {
  285. }
  286. static const struct xfrm_type mip6_destopt_type =
  287. {
  288. .description = "MIP6DESTOPT",
  289. .owner = THIS_MODULE,
  290. .proto = IPPROTO_DSTOPTS,
  291. .flags = XFRM_TYPE_NON_FRAGMENT | XFRM_TYPE_LOCAL_COADDR,
  292. .init_state = mip6_destopt_init_state,
  293. .destructor = mip6_destopt_destroy,
  294. .input = mip6_destopt_input,
  295. .output = mip6_destopt_output,
  296. .reject = mip6_destopt_reject,
  297. .hdr_offset = mip6_destopt_offset,
  298. };
  299. static int mip6_rthdr_input(struct xfrm_state *x, struct sk_buff *skb)
  300. {
  301. const struct ipv6hdr *iph = ipv6_hdr(skb);
  302. struct rt2_hdr *rt2 = (struct rt2_hdr *)skb->data;
  303. int err = rt2->rt_hdr.nexthdr;
  304. spin_lock(&x->lock);
  305. if (!ipv6_addr_equal(&iph->daddr, (struct in6_addr *)x->coaddr) &&
  306. !ipv6_addr_any((struct in6_addr *)x->coaddr))
  307. err = -ENOENT;
  308. spin_unlock(&x->lock);
  309. return err;
  310. }
  311. /* Routing Header type 2 is inserted.
  312. * IP Header's dst address is replaced with Routing Header's Home Address.
  313. */
  314. static int mip6_rthdr_output(struct xfrm_state *x, struct sk_buff *skb)
  315. {
  316. struct ipv6hdr *iph;
  317. struct rt2_hdr *rt2;
  318. u8 nexthdr;
  319. skb_push(skb, -skb_network_offset(skb));
  320. iph = ipv6_hdr(skb);
  321. nexthdr = *skb_mac_header(skb);
  322. *skb_mac_header(skb) = IPPROTO_ROUTING;
  323. rt2 = (struct rt2_hdr *)skb_transport_header(skb);
  324. rt2->rt_hdr.nexthdr = nexthdr;
  325. rt2->rt_hdr.hdrlen = (x->props.header_len >> 3) - 1;
  326. rt2->rt_hdr.type = IPV6_SRCRT_TYPE_2;
  327. rt2->rt_hdr.segments_left = 1;
  328. memset(&rt2->reserved, 0, sizeof(rt2->reserved));
  329. WARN_ON(rt2->rt_hdr.hdrlen != 2);
  330. memcpy(&rt2->addr, &iph->daddr, sizeof(rt2->addr));
  331. spin_lock_bh(&x->lock);
  332. memcpy(&iph->daddr, x->coaddr, sizeof(iph->daddr));
  333. spin_unlock_bh(&x->lock);
  334. return 0;
  335. }
  336. static int mip6_rthdr_offset(struct xfrm_state *x, struct sk_buff *skb,
  337. u8 **nexthdr)
  338. {
  339. u16 offset = sizeof(struct ipv6hdr);
  340. struct ipv6_opt_hdr *exthdr =
  341. (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
  342. const unsigned char *nh = skb_network_header(skb);
  343. unsigned int packet_len = skb->tail - skb->network_header;
  344. int found_rhdr = 0;
  345. *nexthdr = &ipv6_hdr(skb)->nexthdr;
  346. while (offset + 1 <= packet_len) {
  347. switch (**nexthdr) {
  348. case NEXTHDR_HOP:
  349. break;
  350. case NEXTHDR_ROUTING:
  351. if (offset + 3 <= packet_len) {
  352. struct ipv6_rt_hdr *rt;
  353. rt = (struct ipv6_rt_hdr *)(nh + offset);
  354. if (rt->type != 0)
  355. return offset;
  356. }
  357. found_rhdr = 1;
  358. break;
  359. case NEXTHDR_DEST:
  360. if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
  361. return offset;
  362. if (found_rhdr)
  363. return offset;
  364. break;
  365. default:
  366. return offset;
  367. }
  368. offset += ipv6_optlen(exthdr);
  369. *nexthdr = &exthdr->nexthdr;
  370. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  371. }
  372. return offset;
  373. }
  374. static int mip6_rthdr_init_state(struct xfrm_state *x)
  375. {
  376. if (x->id.spi) {
  377. printk(KERN_INFO "%s: spi is not 0: %u\n", __func__,
  378. x->id.spi);
  379. return -EINVAL;
  380. }
  381. if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
  382. printk(KERN_INFO "%s: state's mode is not %u: %u\n",
  383. __func__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
  384. return -EINVAL;
  385. }
  386. x->props.header_len = sizeof(struct rt2_hdr);
  387. return 0;
  388. }
  389. /*
  390. * Do nothing about destroying since it has no specific operation for routing
  391. * header type 2 unlike IPsec protocols.
  392. */
  393. static void mip6_rthdr_destroy(struct xfrm_state *x)
  394. {
  395. }
  396. static const struct xfrm_type mip6_rthdr_type =
  397. {
  398. .description = "MIP6RT",
  399. .owner = THIS_MODULE,
  400. .proto = IPPROTO_ROUTING,
  401. .flags = XFRM_TYPE_NON_FRAGMENT | XFRM_TYPE_REMOTE_COADDR,
  402. .init_state = mip6_rthdr_init_state,
  403. .destructor = mip6_rthdr_destroy,
  404. .input = mip6_rthdr_input,
  405. .output = mip6_rthdr_output,
  406. .hdr_offset = mip6_rthdr_offset,
  407. };
  408. static int __init mip6_init(void)
  409. {
  410. printk(KERN_INFO "Mobile IPv6\n");
  411. if (xfrm_register_type(&mip6_destopt_type, AF_INET6) < 0) {
  412. printk(KERN_INFO "%s: can't add xfrm type(destopt)\n", __func__);
  413. goto mip6_destopt_xfrm_fail;
  414. }
  415. if (xfrm_register_type(&mip6_rthdr_type, AF_INET6) < 0) {
  416. printk(KERN_INFO "%s: can't add xfrm type(rthdr)\n", __func__);
  417. goto mip6_rthdr_xfrm_fail;
  418. }
  419. if (rawv6_mh_filter_register(mip6_mh_filter) < 0) {
  420. printk(KERN_INFO "%s: can't add rawv6 mh filter\n", __func__);
  421. goto mip6_rawv6_mh_fail;
  422. }
  423. return 0;
  424. mip6_rawv6_mh_fail:
  425. xfrm_unregister_type(&mip6_rthdr_type, AF_INET6);
  426. mip6_rthdr_xfrm_fail:
  427. xfrm_unregister_type(&mip6_destopt_type, AF_INET6);
  428. mip6_destopt_xfrm_fail:
  429. return -EAGAIN;
  430. }
  431. static void __exit mip6_fini(void)
  432. {
  433. if (rawv6_mh_filter_unregister(mip6_mh_filter) < 0)
  434. printk(KERN_INFO "%s: can't remove rawv6 mh filter\n", __func__);
  435. if (xfrm_unregister_type(&mip6_rthdr_type, AF_INET6) < 0)
  436. printk(KERN_INFO "%s: can't remove xfrm type(rthdr)\n", __func__);
  437. if (xfrm_unregister_type(&mip6_destopt_type, AF_INET6) < 0)
  438. printk(KERN_INFO "%s: can't remove xfrm type(destopt)\n", __func__);
  439. }
  440. module_init(mip6_init);
  441. module_exit(mip6_fini);
  442. MODULE_LICENSE("GPL");
  443. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_DSTOPTS);
  444. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_ROUTING);