Blame inet/inet6_opt.c

Packit 6c4009
/* Copyright (C) 2006-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@redhat.com>, 2006.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <netinet/in.h>
Packit 6c4009
#include <netinet/ip6.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* RFC 3542, 10.1
Packit 6c4009
Packit 6c4009
   This function returns the number of bytes needed for the empty
Packit 6c4009
   extension header i.e., without any options.  If EXTBUF is not NULL it
Packit 6c4009
   also initializes the extension header to have the correct length
Packit 6c4009
   field.  In that case if the EXTLEN value is not a positive (i.e.,
Packit 6c4009
   non-zero) multiple of 8 the function fails and returns -1.  */
Packit 6c4009
int
Packit 6c4009
inet6_opt_init (void *extbuf, socklen_t extlen)
Packit 6c4009
{
Packit 6c4009
  if (extbuf != NULL)
Packit 6c4009
    {
Packit 6c4009
      if (extlen <= 0 || (extlen % 8) != 0 || extlen > 256 * 8)
Packit 6c4009
	return -1;
Packit 6c4009
Packit 6c4009
      /* Fill in the length in units of 8 octets.  */
Packit 6c4009
      struct ip6_hbh *extp = (struct ip6_hbh *) extbuf;
Packit 6c4009
Packit 6c4009
      /* RFC 2460 requires that the header extension length is the
Packit 6c4009
	 length of the option header in 8-byte units, not including
Packit 6c4009
	 the first 8 bytes.  Hence we have to subtract one.  */
Packit 6c4009
      extp->ip6h_len = extlen / 8 - 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return sizeof (struct ip6_hbh);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
add_padding (uint8_t *extbuf, int offset, int npad)
Packit 6c4009
{
Packit 6c4009
  if (npad == 1)
Packit 6c4009
    extbuf[offset] = IP6OPT_PAD1;
Packit 6c4009
  else if (npad > 0)
Packit 6c4009
    {
Packit 6c4009
      struct ip6_opt *pad_opt = (struct ip6_opt *) (extbuf + offset);
Packit 6c4009
Packit 6c4009
      pad_opt->ip6o_type = IP6OPT_PADN;
Packit 6c4009
      pad_opt->ip6o_len = npad - sizeof (struct ip6_opt);
Packit 6c4009
      /* Clear the memory used by the padding.  */
Packit 6c4009
      memset (pad_opt + 1, '\0', pad_opt->ip6o_len);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* RFC 3542, 10.2
Packit 6c4009
Packit 6c4009
   This function returns the updated total length taking into account
Packit 6c4009
   adding an option with length 'len' and alignment 'align'.  If
Packit 6c4009
   EXTBUF is not NULL then, in addition to returning the length, the
Packit 6c4009
   function inserts any needed pad option, initializes the option
Packit 6c4009
   (setting the type and length fields) and returns a pointer to the
Packit 6c4009
   location for the option content in databufp.  If the option does
Packit 6c4009
   not fit in the extension header buffer the function returns -1.  */
Packit 6c4009
int
Packit 6c4009
inet6_opt_append (void *extbuf, socklen_t extlen, int offset, uint8_t type,
Packit 6c4009
		  socklen_t len, uint8_t align, void **databufp)
Packit 6c4009
{
Packit 6c4009
  /* Check minimum offset.  */
Packit 6c4009
  if (offset < sizeof (struct ip6_hbh))
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  /* One cannot add padding options.  */
Packit 6c4009
  if (type == IP6OPT_PAD1 || type == IP6OPT_PADN)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  /* The option length must fit in one octet.  */
Packit 6c4009
  if (len > 255)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  /* The alignment can only by 1, 2, 4, or 8 and must not exceed the
Packit 6c4009
     option length.  */
Packit 6c4009
  if (align == 0 || align > 8 || (align & (align - 1)) != 0 || align > len)
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  /* Determine the needed padding for alignment.  Following the
Packit 6c4009
     current content of the buffer we have the is the IPv6 option type
Packit 6c4009
     and length, followed immediately by the data.  The data has the
Packit 6c4009
     alignment constraints.  Therefore padding must be inserted in the
Packit 6c4009
     form of padding options before the new option. */
Packit 6c4009
  int data_offset = offset + sizeof (struct ip6_opt);
Packit 6c4009
  int npad = (align - data_offset % align) & (align - 1);
Packit 6c4009
Packit 6c4009
  if (extbuf != NULL)
Packit 6c4009
    {
Packit 6c4009
      /* Now we can check whether the buffer is large enough.  */
Packit 6c4009
      if (data_offset + npad + len > extlen)
Packit 6c4009
	return -1;
Packit 6c4009
Packit 6c4009
      add_padding (extbuf, offset, npad);
Packit 6c4009
Packit 6c4009
      offset += npad;
Packit 6c4009
Packit 6c4009
      /* Now prepare the option itself.  */
Packit 6c4009
      struct ip6_opt *opt = (struct ip6_opt *) ((uint8_t *) extbuf + offset);
Packit 6c4009
Packit 6c4009
      opt->ip6o_type = type;
Packit 6c4009
      opt->ip6o_len = len;
Packit 6c4009
Packit 6c4009
      *databufp = opt + 1;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    offset += npad;
Packit 6c4009
Packit 6c4009
  return offset + sizeof (struct ip6_opt) + len;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* RFC 3542, 10.3
Packit 6c4009
Packit 6c4009
   This function returns the updated total length taking into account
Packit 6c4009
   the final padding of the extension header to make it a multiple of
Packit 6c4009
   8 bytes.  If EXTBUF is not NULL the function also initializes the
Packit 6c4009
   option by inserting a Pad1 or PadN option of the proper length.  */
Packit 6c4009
int
Packit 6c4009
inet6_opt_finish (void *extbuf, socklen_t extlen, int offset)
Packit 6c4009
{
Packit 6c4009
  /* Check minimum offset.  */
Packit 6c4009
  if (offset < sizeof (struct ip6_hbh))
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  /* Required padding at the end.  */
Packit 6c4009
  int npad = (8 - (offset & 7)) & 7;
Packit 6c4009
Packit 6c4009
  if (extbuf != NULL)
Packit 6c4009
    {
Packit 6c4009
      /* Make sure the buffer is large enough.  */
Packit 6c4009
      if (offset + npad > extlen)
Packit 6c4009
	return -1;
Packit 6c4009
Packit 6c4009
      add_padding (extbuf, offset, npad);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return offset + npad;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* RFC 3542, 10.4
Packit 6c4009
Packit 6c4009
   This function inserts data items of various sizes in the data
Packit 6c4009
   portion of the option.  VAL should point to the data to be
Packit 6c4009
   inserted.  OFFSET specifies where in the data portion of the option
Packit 6c4009
   the value should be inserted; the first byte after the option type
Packit 6c4009
   and length is accessed by specifying an offset of zero.  */
Packit 6c4009
int
Packit 6c4009
inet6_opt_set_val (void *databuf, int offset, void *val, socklen_t vallen)
Packit 6c4009
{
Packit 6c4009
  memcpy ((uint8_t *) databuf + offset, val, vallen);
Packit 6c4009
Packit 6c4009
  return offset + vallen;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* RFC 3542, 10.5
Packit 6c4009
Packit 6c4009
   This function parses received option extension headers returning
Packit 6c4009
   the next option.  EXTBUF and EXTLEN specifies the extension header.
Packit 6c4009
   OFFSET should either be zero (for the first option) or the length
Packit 6c4009
   returned by a previous call to 'inet6_opt_next' or
Packit 6c4009
   'inet6_opt_find'.  It specifies the position where to continue
Packit 6c4009
   scanning the extension buffer.  */
Packit 6c4009
int
Packit 6c4009
inet6_opt_next (void *extbuf, socklen_t extlen, int offset, uint8_t *typep,
Packit 6c4009
		socklen_t *lenp, void **databufp)
Packit 6c4009
{
Packit 6c4009
  if (offset == 0)
Packit 6c4009
    offset = sizeof (struct ip6_hbh);
Packit 6c4009
  else if (offset < sizeof (struct ip6_hbh))
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  while (offset < extlen)
Packit 6c4009
    {
Packit 6c4009
      struct ip6_opt *opt = (struct ip6_opt *) ((uint8_t *) extbuf + offset);
Packit 6c4009
Packit 6c4009
      if (opt->ip6o_type == IP6OPT_PAD1)
Packit 6c4009
	/* Single byte padding.  */
Packit 6c4009
	++offset;
Packit 6c4009
      else if (opt->ip6o_type == IP6OPT_PADN)
Packit 6c4009
	offset += sizeof (struct ip6_opt) + opt->ip6o_len;
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* Check whether the option is valid.  */
Packit 6c4009
	  offset += sizeof (struct ip6_opt) + opt->ip6o_len;
Packit 6c4009
	  if (offset > extlen)
Packit 6c4009
	    return -1;
Packit 6c4009
Packit 6c4009
	  *typep = opt->ip6o_type;
Packit 6c4009
	  *lenp = opt->ip6o_len;
Packit 6c4009
	  *databufp = opt + 1;
Packit 6c4009
	  return offset;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return -1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* RFC 3542, 10.6
Packit 6c4009
Packit 6c4009
   This function is similar to the previously described
Packit 6c4009
   'inet6_opt_next' function, except this function lets the caller
Packit 6c4009
   specify the option type to be searched for, instead of always
Packit 6c4009
   returning the next option in the extension header.  */
Packit 6c4009
int
Packit 6c4009
inet6_opt_find (void *extbuf, socklen_t extlen, int offset, uint8_t type,
Packit 6c4009
		socklen_t *lenp, void **databufp)
Packit 6c4009
{
Packit 6c4009
  if (offset == 0)
Packit 6c4009
    offset = sizeof (struct ip6_hbh);
Packit 6c4009
  else if (offset < sizeof (struct ip6_hbh))
Packit 6c4009
    return -1;
Packit 6c4009
Packit 6c4009
  while (offset < extlen)
Packit 6c4009
    {
Packit 6c4009
      struct ip6_opt *opt = (struct ip6_opt *) ((uint8_t *) extbuf + offset);
Packit 6c4009
Packit 6c4009
      if (opt->ip6o_type == IP6OPT_PAD1)
Packit 6c4009
	{
Packit 6c4009
	  /* Single byte padding.  */
Packit 6c4009
	  ++offset;
Packit 6c4009
	  if (type == IP6OPT_PAD1)
Packit 6c4009
	    {
Packit 6c4009
	      *lenp = 0;
Packit 6c4009
	      *databufp = (uint8_t *) extbuf + offset;
Packit 6c4009
	      return offset;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else if (opt->ip6o_type != type)
Packit 6c4009
	offset += sizeof (struct ip6_opt) + opt->ip6o_len;
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* Check whether the option is valid.  */
Packit 6c4009
	  offset += sizeof (struct ip6_opt) + opt->ip6o_len;
Packit 6c4009
	  if (offset > extlen)
Packit 6c4009
	    return -1;
Packit 6c4009
Packit 6c4009
	  *lenp = opt->ip6o_len;
Packit 6c4009
	  *databufp = opt + 1;
Packit 6c4009
	  return offset;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return -1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* RFC 3542, 10.7
Packit 6c4009
Packit 6c4009
   This function extracts data items of various sizes in the data
Packit 6c4009
   portion of the option.  */
Packit 6c4009
int
Packit 6c4009
inet6_opt_get_val (void *databuf, int offset, void *val, socklen_t vallen)
Packit 6c4009
{
Packit 6c4009
  memcpy (val, (uint8_t *) databuf + offset, vallen);
Packit 6c4009
Packit 6c4009
  return offset + vallen;
Packit 6c4009
}