mip6.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. ktime_t 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(ktime_t 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 (!ktime_equal(mip6_report_rl.stamp, stamp) ||
  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 = stamp;
  169. mip6_report_rl.iif = iif;
  170. mip6_report_rl.src = *src;
  171. mip6_report_rl.dst = *dst;
  172. allow = 1;
  173. }
  174. spin_unlock_bh(&mip6_report_rl.lock);
  175. return allow;
  176. }
  177. static int mip6_destopt_reject(struct xfrm_state *x, struct sk_buff *skb,
  178. const struct flowi *fl)
  179. {
  180. struct net *net = xs_net(x);
  181. struct inet6_skb_parm *opt = (struct inet6_skb_parm *)skb->cb;
  182. const struct flowi6 *fl6 = &fl->u.ip6;
  183. struct ipv6_destopt_hao *hao = NULL;
  184. struct xfrm_selector sel;
  185. int offset;
  186. ktime_t stamp;
  187. int err = 0;
  188. if (unlikely(fl6->flowi6_proto == IPPROTO_MH &&
  189. fl6->fl6_mh_type <= IP6_MH_TYPE_MAX))
  190. goto out;
  191. if (likely(opt->dsthao)) {
  192. offset = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO);
  193. if (likely(offset >= 0))
  194. hao = (struct ipv6_destopt_hao *)
  195. (skb_network_header(skb) + offset);
  196. }
  197. stamp = skb_get_ktime(skb);
  198. if (!mip6_report_rl_allow(stamp, &ipv6_hdr(skb)->daddr,
  199. hao ? &hao->addr : &ipv6_hdr(skb)->saddr,
  200. opt->iif))
  201. goto out;
  202. memset(&sel, 0, sizeof(sel));
  203. memcpy(&sel.daddr, (xfrm_address_t *)&ipv6_hdr(skb)->daddr,
  204. sizeof(sel.daddr));
  205. sel.prefixlen_d = 128;
  206. memcpy(&sel.saddr, (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
  207. sizeof(sel.saddr));
  208. sel.prefixlen_s = 128;
  209. sel.family = AF_INET6;
  210. sel.proto = fl6->flowi6_proto;
  211. sel.dport = xfrm_flowi_dport(fl, &fl6->uli);
  212. if (sel.dport)
  213. sel.dport_mask = htons(~0);
  214. sel.sport = xfrm_flowi_sport(fl, &fl6->uli);
  215. if (sel.sport)
  216. sel.sport_mask = htons(~0);
  217. sel.ifindex = fl6->flowi6_oif;
  218. err = km_report(net, IPPROTO_DSTOPTS, &sel,
  219. (hao ? (xfrm_address_t *)&hao->addr : NULL));
  220. out:
  221. return err;
  222. }
  223. static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
  224. u8 **nexthdr)
  225. {
  226. u16 offset = sizeof(struct ipv6hdr);
  227. struct ipv6_opt_hdr *exthdr =
  228. (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
  229. const unsigned char *nh = skb_network_header(skb);
  230. unsigned int packet_len = skb_tail_pointer(skb) -
  231. skb_network_header(skb);
  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. net_dbg_ratelimited("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. pr_info("%s: spi is not 0: %u\n", __func__, x->id.spi);
  267. return -EINVAL;
  268. }
  269. if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
  270. pr_info("%s: state's mode is not %u: %u\n",
  271. __func__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
  272. return -EINVAL;
  273. }
  274. x->props.header_len = sizeof(struct ipv6_destopt_hdr) +
  275. calc_padlen(sizeof(struct ipv6_destopt_hdr), 6) +
  276. sizeof(struct ipv6_destopt_hao);
  277. WARN_ON(x->props.header_len != 24);
  278. return 0;
  279. }
  280. /*
  281. * Do nothing about destroying since it has no specific operation for
  282. * destination options header unlike IPsec protocols.
  283. */
  284. static void mip6_destopt_destroy(struct xfrm_state *x)
  285. {
  286. }
  287. static const struct xfrm_type mip6_destopt_type = {
  288. .description = "MIP6DESTOPT",
  289. .owner = THIS_MODULE,
  290. .proto = IPPROTO_DSTOPTS,
  291. .flags = XFRM_TYPE_NON_FRAGMENT | XFRM_TYPE_LOCAL_COADDR,
  292. .init_state = mip6_destopt_init_state,
  293. .destructor = mip6_destopt_destroy,
  294. .input = mip6_destopt_input,
  295. .output = mip6_destopt_output,
  296. .reject = mip6_destopt_reject,
  297. .hdr_offset = mip6_destopt_offset,
  298. };
  299. static int mip6_rthdr_input(struct xfrm_state *x, struct sk_buff *skb)
  300. {
  301. const struct ipv6hdr *iph = ipv6_hdr(skb);
  302. struct rt2_hdr *rt2 = (struct rt2_hdr *)skb->data;
  303. int err = rt2->rt_hdr.nexthdr;
  304. spin_lock(&x->lock);
  305. if (!ipv6_addr_equal(&iph->daddr, (struct in6_addr *)x->coaddr) &&
  306. !ipv6_addr_any((struct in6_addr *)x->coaddr))
  307. err = -ENOENT;
  308. spin_unlock(&x->lock);
  309. return err;
  310. }
  311. /* Routing Header type 2 is inserted.
  312. * IP Header's dst address is replaced with Routing Header's Home Address.
  313. */
  314. static int mip6_rthdr_output(struct xfrm_state *x, struct sk_buff *skb)
  315. {
  316. struct ipv6hdr *iph;
  317. struct rt2_hdr *rt2;
  318. u8 nexthdr;
  319. skb_push(skb, -skb_network_offset(skb));
  320. iph = ipv6_hdr(skb);
  321. nexthdr = *skb_mac_header(skb);
  322. *skb_mac_header(skb) = IPPROTO_ROUTING;
  323. rt2 = (struct rt2_hdr *)skb_transport_header(skb);
  324. rt2->rt_hdr.nexthdr = nexthdr;
  325. rt2->rt_hdr.hdrlen = (x->props.header_len >> 3) - 1;
  326. rt2->rt_hdr.type = IPV6_SRCRT_TYPE_2;
  327. rt2->rt_hdr.segments_left = 1;
  328. memset(&rt2->reserved, 0, sizeof(rt2->reserved));
  329. WARN_ON(rt2->rt_hdr.hdrlen != 2);
  330. memcpy(&rt2->addr, &iph->daddr, sizeof(rt2->addr));
  331. spin_lock_bh(&x->lock);
  332. memcpy(&iph->daddr, x->coaddr, sizeof(iph->daddr));
  333. spin_unlock_bh(&x->lock);
  334. return 0;
  335. }
  336. static int mip6_rthdr_offset(struct xfrm_state *x, struct sk_buff *skb,
  337. u8 **nexthdr)
  338. {
  339. u16 offset = sizeof(struct ipv6hdr);
  340. struct ipv6_opt_hdr *exthdr =
  341. (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
  342. const unsigned char *nh = skb_network_header(skb);
  343. unsigned int packet_len = skb_tail_pointer(skb) -
  344. skb_network_header(skb);
  345. int found_rhdr = 0;
  346. *nexthdr = &ipv6_hdr(skb)->nexthdr;
  347. while (offset + 1 <= packet_len) {
  348. switch (**nexthdr) {
  349. case NEXTHDR_HOP:
  350. break;
  351. case NEXTHDR_ROUTING:
  352. if (offset + 3 <= packet_len) {
  353. struct ipv6_rt_hdr *rt;
  354. rt = (struct ipv6_rt_hdr *)(nh + offset);
  355. if (rt->type != 0)
  356. return offset;
  357. }
  358. found_rhdr = 1;
  359. break;
  360. case NEXTHDR_DEST:
  361. if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
  362. return offset;
  363. if (found_rhdr)
  364. return offset;
  365. break;
  366. default:
  367. return offset;
  368. }
  369. offset += ipv6_optlen(exthdr);
  370. *nexthdr = &exthdr->nexthdr;
  371. exthdr = (struct ipv6_opt_hdr *)(nh + offset);
  372. }
  373. return offset;
  374. }
  375. static int mip6_rthdr_init_state(struct xfrm_state *x)
  376. {
  377. if (x->id.spi) {
  378. pr_info("%s: spi is not 0: %u\n", __func__, x->id.spi);
  379. return -EINVAL;
  380. }
  381. if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
  382. pr_info("%s: state's mode is not %u: %u\n",
  383. __func__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
  384. return -EINVAL;
  385. }
  386. x->props.header_len = sizeof(struct rt2_hdr);
  387. return 0;
  388. }
  389. /*
  390. * Do nothing about destroying since it has no specific operation for routing
  391. * header type 2 unlike IPsec protocols.
  392. */
  393. static void mip6_rthdr_destroy(struct xfrm_state *x)
  394. {
  395. }
  396. static const struct xfrm_type mip6_rthdr_type = {
  397. .description = "MIP6RT",
  398. .owner = THIS_MODULE,
  399. .proto = IPPROTO_ROUTING,
  400. .flags = XFRM_TYPE_NON_FRAGMENT | XFRM_TYPE_REMOTE_COADDR,
  401. .init_state = mip6_rthdr_init_state,
  402. .destructor = mip6_rthdr_destroy,
  403. .input = mip6_rthdr_input,
  404. .output = mip6_rthdr_output,
  405. .hdr_offset = mip6_rthdr_offset,
  406. };
  407. static int __init mip6_init(void)
  408. {
  409. pr_info("Mobile IPv6\n");
  410. if (xfrm_register_type(&mip6_destopt_type, AF_INET6) < 0) {
  411. pr_info("%s: can't add xfrm type(destopt)\n", __func__);
  412. goto mip6_destopt_xfrm_fail;
  413. }
  414. if (xfrm_register_type(&mip6_rthdr_type, AF_INET6) < 0) {
  415. pr_info("%s: can't add xfrm type(rthdr)\n", __func__);
  416. goto mip6_rthdr_xfrm_fail;
  417. }
  418. if (rawv6_mh_filter_register(mip6_mh_filter) < 0) {
  419. pr_info("%s: can't add rawv6 mh filter\n", __func__);
  420. goto mip6_rawv6_mh_fail;
  421. }
  422. return 0;
  423. mip6_rawv6_mh_fail:
  424. xfrm_unregister_type(&mip6_rthdr_type, AF_INET6);
  425. mip6_rthdr_xfrm_fail:
  426. xfrm_unregister_type(&mip6_destopt_type, AF_INET6);
  427. mip6_destopt_xfrm_fail:
  428. return -EAGAIN;
  429. }
  430. static void __exit mip6_fini(void)
  431. {
  432. if (rawv6_mh_filter_unregister(mip6_mh_filter) < 0)
  433. pr_info("%s: can't remove rawv6 mh filter\n", __func__);
  434. if (xfrm_unregister_type(&mip6_rthdr_type, AF_INET6) < 0)
  435. pr_info("%s: can't remove xfrm type(rthdr)\n", __func__);
  436. if (xfrm_unregister_type(&mip6_destopt_type, AF_INET6) < 0)
  437. pr_info("%s: can't remove xfrm type(destopt)\n", __func__);
  438. }
  439. module_init(mip6_init);
  440. module_exit(mip6_fini);
  441. MODULE_LICENSE("GPL");
  442. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_DSTOPTS);
  443. MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_ROUTING);