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, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. * Authors:
  20. * Noriaki TAKAMIYA @USAGI
  21. * Masahide NAKAMURA @USAGI
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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_PAD1;
  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. net_dbg_ratelimited("mip6: MH message too short: %d vs >=%d\n",
  90. mh->ip6mh_hdrlen,
  91. mip6_mh_len(mh->ip6mh_type));
  92. mip6_param_prob(skb, 0, offsetof(struct ip6_mh, ip6mh_hdrlen) +
  93. skb_network_header_len(skb));
  94. return -1;
  95. }
  96. if (mh->ip6mh_proto != IPPROTO_NONE) {
  97. net_dbg_ratelimited("mip6: MH invalid payload proto = %d\n",
  98. mh->ip6mh_proto);
  99. mip6_param_prob(skb, 0, offsetof(struct ip6_mh, ip6mh_proto) +
  100. skb_network_header_len(skb));
  101. return -1;
  102. }
  103. return 0;
  104. }
  105. struct mip6_report_rate_limiter {
  106. spinlock_t lock;
  107. struct timeval stamp;
  108. int iif;
  109. struct in6_addr src;
  110. struct in6_addr dst;
  111. };
  112. static struct mip6_report_rate_limiter mip6_report_rl = {
  113. .lock = __SPIN_LOCK_UNLOCKED(mip6_report_rl.lock)
  114. };
  115. static int mip6_destopt_input(struct xfrm_state *x, struct sk_buff *skb)
  116. {
  117. const struct ipv6hdr *iph = ipv6_hdr(skb);
  118. struct ipv6_destopt_hdr *destopt = (struct ipv6_destopt_hdr *)skb->data;
  119. int err = destopt->nexthdr;
  120. spin_lock(&x->lock);
  121. if (!ipv6_addr_equal(&iph->saddr, (struct in6_addr *)x->coaddr) &&
  122. !ipv6_addr_any((struct in6_addr *)x->coaddr))
  123. err = -ENOENT;
  124. spin_unlock(&x->lock);
  125. return err;
  126. }
  127. /* Destination Option Header is inserted.
  128. * IP Header's src address is replaced with Home Address Option in
  129. * Destination Option Header.
  130. */
  131. static int mip6_destopt_output(struct xfrm_state *x, struct sk_buff *skb)
  132. {
  133. struct ipv6hdr *iph;
  134. struct ipv6_destopt_hdr *dstopt;
  135. struct ipv6_destopt_hao *hao;
  136. u8 nexthdr;
  137. int len;
  138. skb_push(skb, -skb_network_offset(skb));
  139. iph = ipv6_hdr(skb);
  140. nexthdr = *skb_mac_header(skb);
  141. *skb_mac_header(skb) = IPPROTO_DSTOPTS;
  142. dstopt = (struct ipv6_destopt_hdr *)skb_transport_header(skb);
  143. dstopt->nexthdr = nexthdr;
  144. hao = mip6_padn((char *)(dstopt + 1),
  145. calc_padlen(sizeof(*dstopt), 6));
  146. hao->type = IPV6_TLV_HAO;
  147. BUILD_BUG_ON(sizeof(*hao) != 18);
  148. hao->length = sizeof(*hao) - 2;
  149. len = ((char *)hao - (char *)dstopt) + sizeof(*hao);
  150. memcpy(&hao->addr, &iph->saddr, sizeof(hao->addr));
  151. spin_lock_bh(&x->lock);
  152. memcpy(&iph->saddr, x->coaddr, sizeof(iph->saddr));
  153. spin_unlock_bh(&x->lock);
  154. WARN_ON(len != x->props.header_len);
  155. dstopt->hdrlen = (x->props.header_len >> 3) - 1;
  156. return 0;
  157. }
  158. static inline int mip6_report_rl_allow(struct timeval *stamp,
  159. const struct in6_addr *dst,
  160. const struct in6_addr *src, int iif)
  161. {
  162. int allow = 0;
  163. spin_lock_bh(&mip6_report_rl.lock);
  164. if (mip6_report_rl.stamp.tv_sec != stamp->tv_sec ||
  165. mip6_report_rl.stamp.tv_usec != stamp->tv_usec ||
  166. mip6_report_rl.iif != iif ||
  167. !ipv6_addr_equal(&mip6_report_rl.src, src) ||
  168. !ipv6_addr_equal(&mip6_report_rl.dst, dst)) {
  169. mip6_report_rl.stamp.tv_sec = stamp->tv_sec;
  170. mip6_report_rl.stamp.tv_usec = stamp->tv_usec;
  171. mip6_report_rl.iif = iif;
  172. mip6_report_rl.src = *src;
  173. mip6_report_rl.dst = *dst;
  174. allow = 1;
  175. }
  176. spin_unlock_bh(&mip6_report_rl.lock);
  177. return allow;
  178. }
  179. static int mip6_destopt_reject(struct xfrm_state *x, struct sk_buff *skb,
  180. const struct flowi *fl)
  181. {
  182. struct net *net = xs_net(x);
  183. struct inet6_skb_parm *opt = (struct inet6_skb_parm *)skb->cb;
  184. const struct flowi6 *fl6 = &fl->u.ip6;
  185. struct ipv6_destopt_hao *hao = NULL;
  186. struct xfrm_selector sel;
  187. int offset;
  188. struct timeval stamp;
  189. int err = 0;
  190. if (unlikely(fl6->flowi6_proto == IPPROTO_MH &&
  191. fl6->fl6_mh_type <= IP6_MH_TYPE_MAX))
  192. goto out;
  193. if (likely(opt->dsthao)) {
  194. offset = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO);
  195. if (likely(offset >= 0))
  196. hao = (struct ipv6_destopt_hao *)
  197. (skb_network_header(skb) + offset);
  198. }
  199. skb_get_timestamp(skb, &stamp);
  200. if (!mip6_report_rl_allow(&stamp, &ipv6_hdr(skb)->daddr,
  201. hao ? &hao->addr : &ipv6_hdr(skb)->saddr,
  202. opt->iif))
  203. goto out;
  204. memset(&sel, 0, sizeof(sel));
  205. memcpy(&sel.daddr, (xfrm_address_t *)&ipv6_hdr(skb)->daddr,
  206. sizeof(sel.daddr));
  207. sel.prefixlen_d = 128;
  208. memcpy(&sel.saddr, (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
  209. sizeof(sel.saddr));
  210. sel.prefixlen_s = 128;
  211. sel.family = AF_INET6;
  212. sel.proto = fl6->flowi6_proto;
  213. sel.dport = xfrm_flowi_dport(fl, &fl6->uli);
  214. if (sel.dport)
  215. sel.dport_mask = htons(~0);
  216. sel.sport = xfrm_flowi_sport(fl, &fl6->uli);
  217. if (sel.sport)
  218. sel.sport_mask = htons(~0);
  219. sel.ifindex = fl6->flowi6_oif;
  220. err = km_report(net, IPPROTO_DSTOPTS, &sel,
  221. (hao ? (xfrm_address_t *)&hao->addr : NULL));
  222. out:
  223. return err;
  224. }
  225. static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
  226. u8 **nexthdr)
  227. {
  228. u16 offset = sizeof(struct ipv6hdr);
  229. struct ipv6_opt_hdr *exthdr =
  230. (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
  231. const unsigned char *nh = skb_network_header(skb);
  232. unsigned int packet_len = skb_tail_pointer(skb) -
  233. skb_network_header(skb);
  234. int found_rhdr = 0;
  235. *nexthdr = &ipv6_hdr(skb)->nexthdr;
  236. while (offset + 1 <= packet_len) {
  237. switch (**nexthdr) {
  238. case NEXTHDR_HOP:
  239. break;
  240. case NEXTHDR_ROUTING:
  241. found_rhdr = 1;
  242. break;
  243. case NEXTHDR_DEST:
  244. /*
  245. * HAO MUST NOT appear more than once.
  246. * XXX: It is better to try to find by the end of
  247. * XXX: packet if HAO exists.
  248. */
  249. if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0) {
  250. net_dbg_ratelimited("mip6: hao exists already, override\n");
  251. return offset;
  252. }
  253. if (found_rhdr)
  254. return offset;
  255. break;
  256. default:
  257. return offset;
  258. }
  259. offset += ipv6_optlen(exthdr);
  260. *nexthdr = &exthdr->nexthdr;
  261. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  262. }
  263. return offset;
  264. }
  265. static int mip6_destopt_init_state(struct xfrm_state *x)
  266. {
  267. if (x->id.spi) {
  268. pr_info("%s: spi is not 0: %u\n", __func__, x->id.spi);
  269. return -EINVAL;
  270. }
  271. if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
  272. pr_info("%s: state's mode is not %u: %u\n",
  273. __func__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
  274. return -EINVAL;
  275. }
  276. x->props.header_len = sizeof(struct ipv6_destopt_hdr) +
  277. calc_padlen(sizeof(struct ipv6_destopt_hdr), 6) +
  278. sizeof(struct ipv6_destopt_hao);
  279. WARN_ON(x->props.header_len != 24);
  280. return 0;
  281. }
  282. /*
  283. * Do nothing about destroying since it has no specific operation for
  284. * destination options header unlike IPsec protocols.
  285. */
  286. static void mip6_destopt_destroy(struct xfrm_state *x)
  287. {
  288. }
  289. static const struct xfrm_type mip6_destopt_type = {
  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_pointer(skb) -
  346. skb_network_header(skb);
  347. int found_rhdr = 0;
  348. *nexthdr = &ipv6_hdr(skb)->nexthdr;
  349. while (offset + 1 <= packet_len) {
  350. switch (**nexthdr) {
  351. case NEXTHDR_HOP:
  352. break;
  353. case NEXTHDR_ROUTING:
  354. if (offset + 3 <= packet_len) {
  355. struct ipv6_rt_hdr *rt;
  356. rt = (struct ipv6_rt_hdr *)(nh + offset);
  357. if (rt->type != 0)
  358. return offset;
  359. }
  360. found_rhdr = 1;
  361. break;
  362. case NEXTHDR_DEST:
  363. if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
  364. return offset;
  365. if (found_rhdr)
  366. return offset;
  367. break;
  368. default:
  369. return offset;
  370. }
  371. offset += ipv6_optlen(exthdr);
  372. *nexthdr = &exthdr->nexthdr;
  373. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  374. }
  375. return offset;
  376. }
  377. static int mip6_rthdr_init_state(struct xfrm_state *x)
  378. {
  379. if (x->id.spi) {
  380. pr_info("%s: spi is not 0: %u\n", __func__, x->id.spi);
  381. return -EINVAL;
  382. }
  383. if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
  384. pr_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. .description = "MIP6RT",
  400. .owner = THIS_MODULE,
  401. .proto = IPPROTO_ROUTING,
  402. .flags = XFRM_TYPE_NON_FRAGMENT | XFRM_TYPE_REMOTE_COADDR,
  403. .init_state = mip6_rthdr_init_state,
  404. .destructor = mip6_rthdr_destroy,
  405. .input = mip6_rthdr_input,
  406. .output = mip6_rthdr_output,
  407. .hdr_offset = mip6_rthdr_offset,
  408. };
  409. static int __init mip6_init(void)
  410. {
  411. pr_info("Mobile IPv6\n");
  412. if (xfrm_register_type(&mip6_destopt_type, AF_INET6) < 0) {
  413. pr_info("%s: can't add xfrm type(destopt)\n", __func__);
  414. goto mip6_destopt_xfrm_fail;
  415. }
  416. if (xfrm_register_type(&mip6_rthdr_type, AF_INET6) < 0) {
  417. pr_info("%s: can't add xfrm type(rthdr)\n", __func__);
  418. goto mip6_rthdr_xfrm_fail;
  419. }
  420. if (rawv6_mh_filter_register(mip6_mh_filter) < 0) {
  421. pr_info("%s: can't add rawv6 mh filter\n", __func__);
  422. goto mip6_rawv6_mh_fail;
  423. }
  424. return 0;
  425. mip6_rawv6_mh_fail:
  426. xfrm_unregister_type(&mip6_rthdr_type, AF_INET6);
  427. mip6_rthdr_xfrm_fail:
  428. xfrm_unregister_type(&mip6_destopt_type, AF_INET6);
  429. mip6_destopt_xfrm_fail:
  430. return -EAGAIN;
  431. }
  432. static void __exit mip6_fini(void)
  433. {
  434. if (rawv6_mh_filter_unregister(mip6_mh_filter) < 0)
  435. pr_info("%s: can't remove rawv6 mh filter\n", __func__);
  436. if (xfrm_unregister_type(&mip6_rthdr_type, AF_INET6) < 0)
  437. pr_info("%s: can't remove xfrm type(rthdr)\n", __func__);
  438. if (xfrm_unregister_type(&mip6_destopt_type, AF_INET6) < 0)
  439. pr_info("%s: can't remove xfrm type(destopt)\n", __func__);
  440. }
  441. module_init(mip6_init);
  442. module_exit(mip6_fini);
  443. MODULE_LICENSE("GPL");
  444. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_DSTOPTS);
  445. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_ROUTING);