mip6.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. int mhlen;
  85. if (!pskb_may_pull(skb, (skb->h.raw - skb->data) + 8) ||
  86. !pskb_may_pull(skb, (skb->h.raw - skb->data) + ((skb->h.raw[1] + 1) << 3)))
  87. return -1;
  88. mh = (struct ip6_mh *)skb->h.raw;
  89. if (mh->ip6mh_hdrlen < mip6_mh_len(mh->ip6mh_type)) {
  90. LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH message too short: %d vs >=%d\n",
  91. mh->ip6mh_hdrlen, mip6_mh_len(mh->ip6mh_type));
  92. mip6_param_prob(skb, 0, (&mh->ip6mh_hdrlen) - skb->nh.raw);
  93. return -1;
  94. }
  95. mhlen = (mh->ip6mh_hdrlen + 1) << 3;
  96. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  97. skb->ip_summed = CHECKSUM_UNNECESSARY;
  98. if (csum_ipv6_magic(&skb->nh.ipv6h->saddr,
  99. &skb->nh.ipv6h->daddr,
  100. mhlen, IPPROTO_MH,
  101. skb->csum)) {
  102. LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH hw checksum failed\n");
  103. skb->ip_summed = CHECKSUM_NONE;
  104. }
  105. }
  106. if (skb->ip_summed == CHECKSUM_NONE) {
  107. if (csum_ipv6_magic(&skb->nh.ipv6h->saddr,
  108. &skb->nh.ipv6h->daddr,
  109. mhlen, IPPROTO_MH,
  110. skb_checksum(skb, 0, mhlen, 0))) {
  111. LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH checksum failed "
  112. "[" NIP6_FMT " > " NIP6_FMT "]\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 (unlikely(fl->proto == IPPROTO_MH &&
  205. fl->fl_mh_type <= IP6_MH_TYPE_MAX))
  206. goto out;
  207. if (likely(opt->dsthao)) {
  208. offset = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO);
  209. if (likely(offset >= 0))
  210. hao = (struct ipv6_destopt_hao *)(skb->nh.raw + offset);
  211. }
  212. skb_get_timestamp(skb, &stamp);
  213. if (!mip6_report_rl_allow(&stamp, &skb->nh.ipv6h->daddr,
  214. hao ? &hao->addr : &skb->nh.ipv6h->saddr,
  215. opt->iif))
  216. goto out;
  217. memset(&sel, 0, sizeof(sel));
  218. memcpy(&sel.daddr, (xfrm_address_t *)&skb->nh.ipv6h->daddr,
  219. sizeof(sel.daddr));
  220. sel.prefixlen_d = 128;
  221. memcpy(&sel.saddr, (xfrm_address_t *)&skb->nh.ipv6h->saddr,
  222. sizeof(sel.saddr));
  223. sel.prefixlen_s = 128;
  224. sel.family = AF_INET6;
  225. sel.proto = fl->proto;
  226. sel.dport = xfrm_flowi_dport(fl);
  227. if (sel.dport)
  228. sel.dport_mask = ~((__u16)0);
  229. sel.sport = xfrm_flowi_sport(fl);
  230. if (sel.sport)
  231. sel.sport_mask = ~((__u16)0);
  232. sel.ifindex = fl->oif;
  233. err = km_report(IPPROTO_DSTOPTS, &sel,
  234. (hao ? (xfrm_address_t *)&hao->addr : NULL));
  235. out:
  236. return err;
  237. }
  238. static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
  239. u8 **nexthdr)
  240. {
  241. u16 offset = sizeof(struct ipv6hdr);
  242. struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.ipv6h + 1);
  243. unsigned int packet_len = skb->tail - skb->nh.raw;
  244. int found_rhdr = 0;
  245. *nexthdr = &skb->nh.ipv6h->nexthdr;
  246. while (offset + 1 <= packet_len) {
  247. switch (**nexthdr) {
  248. case NEXTHDR_HOP:
  249. break;
  250. case NEXTHDR_ROUTING:
  251. found_rhdr = 1;
  252. break;
  253. case NEXTHDR_DEST:
  254. /*
  255. * HAO MUST NOT appear more than once.
  256. * XXX: It is better to try to find by the end of
  257. * XXX: packet if HAO exists.
  258. */
  259. if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0) {
  260. LIMIT_NETDEBUG(KERN_WARNING "mip6: hao exists already, override\n");
  261. return offset;
  262. }
  263. if (found_rhdr)
  264. return offset;
  265. break;
  266. default:
  267. return offset;
  268. }
  269. offset += ipv6_optlen(exthdr);
  270. *nexthdr = &exthdr->nexthdr;
  271. exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
  272. }
  273. return offset;
  274. }
  275. static int mip6_destopt_init_state(struct xfrm_state *x)
  276. {
  277. if (x->id.spi) {
  278. printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
  279. x->id.spi);
  280. return -EINVAL;
  281. }
  282. if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
  283. printk(KERN_INFO "%s: state's mode is not %u: %u\n",
  284. __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
  285. return -EINVAL;
  286. }
  287. x->props.header_len = sizeof(struct ipv6_destopt_hdr) +
  288. calc_padlen(sizeof(struct ipv6_destopt_hdr), 6) +
  289. sizeof(struct ipv6_destopt_hao);
  290. BUG_TRAP(x->props.header_len == 24);
  291. return 0;
  292. }
  293. /*
  294. * Do nothing about destroying since it has no specific operation for
  295. * destination options header unlike IPsec protocols.
  296. */
  297. static void mip6_destopt_destroy(struct xfrm_state *x)
  298. {
  299. }
  300. static struct xfrm_type mip6_destopt_type =
  301. {
  302. .description = "MIP6DESTOPT",
  303. .owner = THIS_MODULE,
  304. .proto = IPPROTO_DSTOPTS,
  305. .flags = XFRM_TYPE_NON_FRAGMENT,
  306. .init_state = mip6_destopt_init_state,
  307. .destructor = mip6_destopt_destroy,
  308. .input = mip6_destopt_input,
  309. .output = mip6_destopt_output,
  310. .reject = mip6_destopt_reject,
  311. .hdr_offset = mip6_destopt_offset,
  312. .local_addr = mip6_xfrm_addr,
  313. };
  314. static int mip6_rthdr_input(struct xfrm_state *x, struct sk_buff *skb)
  315. {
  316. struct rt2_hdr *rt2 = (struct rt2_hdr *)skb->data;
  317. if (!ipv6_addr_equal(&rt2->addr, (struct in6_addr *)x->coaddr) &&
  318. !ipv6_addr_any((struct in6_addr *)x->coaddr))
  319. return -ENOENT;
  320. return rt2->rt_hdr.nexthdr;
  321. }
  322. /* Routing Header type 2 is inserted.
  323. * IP Header's dst address is replaced with Routing Header's Home Address.
  324. */
  325. static int mip6_rthdr_output(struct xfrm_state *x, struct sk_buff *skb)
  326. {
  327. struct ipv6hdr *iph;
  328. struct rt2_hdr *rt2;
  329. u8 nexthdr;
  330. iph = (struct ipv6hdr *)skb->data;
  331. iph->payload_len = htons(skb->len - sizeof(*iph));
  332. nexthdr = *skb->nh.raw;
  333. *skb->nh.raw = IPPROTO_ROUTING;
  334. rt2 = (struct rt2_hdr *)skb->h.raw;
  335. rt2->rt_hdr.nexthdr = nexthdr;
  336. rt2->rt_hdr.hdrlen = (x->props.header_len >> 3) - 1;
  337. rt2->rt_hdr.type = IPV6_SRCRT_TYPE_2;
  338. rt2->rt_hdr.segments_left = 1;
  339. memset(&rt2->reserved, 0, sizeof(rt2->reserved));
  340. BUG_TRAP(rt2->rt_hdr.hdrlen == 2);
  341. memcpy(&rt2->addr, &iph->daddr, sizeof(rt2->addr));
  342. memcpy(&iph->daddr, x->coaddr, sizeof(iph->daddr));
  343. return 0;
  344. }
  345. static int mip6_rthdr_offset(struct xfrm_state *x, struct sk_buff *skb,
  346. u8 **nexthdr)
  347. {
  348. u16 offset = sizeof(struct ipv6hdr);
  349. struct ipv6_opt_hdr *exthdr = (struct ipv6_opt_hdr*)(skb->nh.ipv6h + 1);
  350. unsigned int packet_len = skb->tail - skb->nh.raw;
  351. int found_rhdr = 0;
  352. *nexthdr = &skb->nh.ipv6h->nexthdr;
  353. while (offset + 1 <= packet_len) {
  354. switch (**nexthdr) {
  355. case NEXTHDR_HOP:
  356. break;
  357. case NEXTHDR_ROUTING:
  358. if (offset + 3 <= packet_len) {
  359. struct ipv6_rt_hdr *rt;
  360. rt = (struct ipv6_rt_hdr *)(skb->nh.raw + offset);
  361. if (rt->type != 0)
  362. return offset;
  363. }
  364. found_rhdr = 1;
  365. break;
  366. case NEXTHDR_DEST:
  367. if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
  368. return offset;
  369. if (found_rhdr)
  370. return offset;
  371. break;
  372. default:
  373. return offset;
  374. }
  375. offset += ipv6_optlen(exthdr);
  376. *nexthdr = &exthdr->nexthdr;
  377. exthdr = (struct ipv6_opt_hdr*)(skb->nh.raw + offset);
  378. }
  379. return offset;
  380. }
  381. static int mip6_rthdr_init_state(struct xfrm_state *x)
  382. {
  383. if (x->id.spi) {
  384. printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
  385. x->id.spi);
  386. return -EINVAL;
  387. }
  388. if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
  389. printk(KERN_INFO "%s: state's mode is not %u: %u\n",
  390. __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
  391. return -EINVAL;
  392. }
  393. x->props.header_len = sizeof(struct rt2_hdr);
  394. return 0;
  395. }
  396. /*
  397. * Do nothing about destroying since it has no specific operation for routing
  398. * header type 2 unlike IPsec protocols.
  399. */
  400. static void mip6_rthdr_destroy(struct xfrm_state *x)
  401. {
  402. }
  403. static struct xfrm_type mip6_rthdr_type =
  404. {
  405. .description = "MIP6RT",
  406. .owner = THIS_MODULE,
  407. .proto = IPPROTO_ROUTING,
  408. .flags = XFRM_TYPE_NON_FRAGMENT,
  409. .init_state = mip6_rthdr_init_state,
  410. .destructor = mip6_rthdr_destroy,
  411. .input = mip6_rthdr_input,
  412. .output = mip6_rthdr_output,
  413. .hdr_offset = mip6_rthdr_offset,
  414. .remote_addr = mip6_xfrm_addr,
  415. };
  416. int __init mip6_init(void)
  417. {
  418. printk(KERN_INFO "Mobile IPv6\n");
  419. if (xfrm_register_type(&mip6_destopt_type, AF_INET6) < 0) {
  420. printk(KERN_INFO "%s: can't add xfrm type(destopt)\n", __FUNCTION__);
  421. goto mip6_destopt_xfrm_fail;
  422. }
  423. if (xfrm_register_type(&mip6_rthdr_type, AF_INET6) < 0) {
  424. printk(KERN_INFO "%s: can't add xfrm type(rthdr)\n", __FUNCTION__);
  425. goto mip6_rthdr_xfrm_fail;
  426. }
  427. return 0;
  428. mip6_rthdr_xfrm_fail:
  429. xfrm_unregister_type(&mip6_destopt_type, AF_INET6);
  430. mip6_destopt_xfrm_fail:
  431. return -EAGAIN;
  432. }
  433. void __exit mip6_fini(void)
  434. {
  435. if (xfrm_unregister_type(&mip6_rthdr_type, AF_INET6) < 0)
  436. printk(KERN_INFO "%s: can't remove xfrm type(rthdr)\n", __FUNCTION__);
  437. if (xfrm_unregister_type(&mip6_destopt_type, AF_INET6) < 0)
  438. printk(KERN_INFO "%s: can't remove xfrm type(destopt)\n", __FUNCTION__);
  439. }