dest6.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* $OpenBSD: dest6.c,v 1.15 2015/03/14 03:38:52 jsg Exp $ */
  2. /* $KAME: dest6.c,v 1.25 2001/02/22 01:39:16 itojun Exp $ */
  3. /*
  4. * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the project nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #include <sys/param.h>
  32. #include <sys/systm.h>
  33. #include <sys/mbuf.h>
  34. #include <sys/domain.h>
  35. #include <sys/protosw.h>
  36. #include <sys/socket.h>
  37. #include <sys/errno.h>
  38. #include <sys/time.h>
  39. #include <sys/kernel.h>
  40. #include <netinet/in.h>
  41. #include <netinet/ip6.h>
  42. #include <netinet6/ip6_var.h>
  43. #include <netinet/icmp6.h>
  44. /*
  45. * Destination options header processing.
  46. */
  47. int
  48. dest6_input(struct mbuf **mp, int *offp, int proto)
  49. {
  50. struct mbuf *m = *mp;
  51. int off = *offp, dstoptlen, optlen;
  52. struct ip6_dest *dstopts;
  53. u_int8_t *opt;
  54. /* validation of the length of the header */
  55. IP6_EXTHDR_GET(dstopts, struct ip6_dest *, m, off, sizeof(*dstopts));
  56. if (dstopts == NULL)
  57. return IPPROTO_DONE;
  58. dstoptlen = (dstopts->ip6d_len + 1) << 3;
  59. IP6_EXTHDR_GET(dstopts, struct ip6_dest *, m, off, dstoptlen);
  60. if (dstopts == NULL)
  61. return IPPROTO_DONE;
  62. off += dstoptlen;
  63. dstoptlen -= sizeof(struct ip6_dest);
  64. opt = (u_int8_t *)dstopts + sizeof(struct ip6_dest);
  65. /* search header for all options. */
  66. for (optlen = 0; dstoptlen > 0; dstoptlen -= optlen, opt += optlen) {
  67. if (*opt != IP6OPT_PAD1 &&
  68. (dstoptlen < IP6OPT_MINLEN || *(opt + 1) + 2 > dstoptlen)) {
  69. ip6stat.ip6s_toosmall++;
  70. goto bad;
  71. }
  72. switch (*opt) {
  73. case IP6OPT_PAD1:
  74. optlen = 1;
  75. break;
  76. case IP6OPT_PADN:
  77. optlen = *(opt + 1) + 2;
  78. break;
  79. default: /* unknown option */
  80. optlen = ip6_unknown_opt(opt, m,
  81. opt - mtod(m, u_int8_t *));
  82. if (optlen == -1)
  83. return (IPPROTO_DONE);
  84. optlen += 2;
  85. break;
  86. }
  87. }
  88. *offp = off;
  89. return (dstopts->ip6d_nxt);
  90. bad:
  91. m_freem(m);
  92. return (IPPROTO_DONE);
  93. }