mip6.c 13 KB

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