mip6.c 13 KB

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