mip6.c 13 KB

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