summary refs log tree commit diff
path: root/inet/inet6_opt.c
blob: 9a6bb0ab715ae317d6d4b9810912c9531ffa090a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* Copyright (C) 2006-2015 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   Contributed by Ulrich Drepper <drepper@redhat.com>, 2006.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <string.h>
#include <netinet/in.h>
#include <netinet/ip6.h>


/* RFC 3542, 10.1

   This function returns the number of bytes needed for the empty
   extension header i.e., without any options.  If EXTBUF is not NULL it
   also initializes the extension header to have the correct length
   field.  In that case if the EXTLEN value is not a positive (i.e.,
   non-zero) multiple of 8 the function fails and returns -1.  */
int
inet6_opt_init (void *extbuf, socklen_t extlen)
{
  if (extbuf != NULL)
    {
      if (extlen <= 0 || (extlen % 8) != 0 || extlen > 256 * 8)
	return -1;

      /* Fill in the length in units of 8 octets.  */
      struct ip6_hbh *extp = (struct ip6_hbh *) extbuf;

      /* RFC 2460 requires that the header extension length is the
	 length of the option header in 8-byte units, not including
	 the first 8 bytes.  Hence we have to subtract one.  */
      extp->ip6h_len = extlen / 8 - 1;
    }

  return sizeof (struct ip6_hbh);
}


static void
add_padding (uint8_t *extbuf, int offset, int npad)
{
  if (npad == 1)
    extbuf[offset] = IP6OPT_PAD1;
  else if (npad > 0)
    {
      struct ip6_opt *pad_opt = (struct ip6_opt *) (extbuf + offset);

      pad_opt->ip6o_type = IP6OPT_PADN;
      pad_opt->ip6o_len = npad - sizeof (struct ip6_opt);
      /* Clear the memory used by the padding.  */
      memset (pad_opt + 1, '\0', pad_opt->ip6o_len);
    }
}



/* RFC 3542, 10.2

   This function returns the updated total length taking into account
   adding an option with length 'len' and alignment 'align'.  If
   EXTBUF is not NULL then, in addition to returning the length, the
   function inserts any needed pad option, initializes the option
   (setting the type and length fields) and returns a pointer to the
   location for the option content in databufp.  If the option does
   not fit in the extension header buffer the function returns -1.  */
int
inet6_opt_append (void *extbuf, socklen_t extlen, int offset, uint8_t type,
		  socklen_t len, uint8_t align, void **databufp)
{
  /* Check minimum offset.  */
  if (offset < sizeof (struct ip6_hbh))
    return -1;

  /* One cannot add padding options.  */
  if (type == IP6OPT_PAD1 || type == IP6OPT_PADN)
    return -1;

  /* The option length must fit in one octet.  */
  if (len > 255)
    return -1;

  /* The alignment can only by 1, 2, 4, or 8 and must not exceed the
     option length.  */
  if (align == 0 || align > 8 || (align & (align - 1)) != 0 || align > len)
    return -1;

  /* Determine the needed padding for alignment.  Following the
     current content of the buffer we have the is the IPv6 option type
     and length, followed immediately by the data.  The data has the
     alignment constraints.  Therefore padding must be inserted in the
     form of padding options before the new option. */
  int data_offset = offset + sizeof (struct ip6_opt);
  int npad = (align - data_offset % align) & (align - 1);

  if (extbuf != NULL)
    {
      /* Now we can check whether the buffer is large enough.  */
      if (data_offset + npad + len > extlen)
	return -1;

      add_padding (extbuf, offset, npad);

      offset += npad;

      /* Now prepare the option itself.  */
      struct ip6_opt *opt = (struct ip6_opt *) ((uint8_t *) extbuf + offset);

      opt->ip6o_type = type;
      opt->ip6o_len = len;

      *databufp = opt + 1;
    }
  else
    offset += npad;

  return offset + sizeof (struct ip6_opt) + len;
}


/* RFC 3542, 10.3

   This function returns the updated total length taking into account
   the final padding of the extension header to make it a multiple of
   8 bytes.  If EXTBUF is not NULL the function also initializes the
   option by inserting a Pad1 or PadN option of the proper length.  */
int
inet6_opt_finish (void *extbuf, socklen_t extlen, int offset)
{
  /* Check minimum offset.  */
  if (offset < sizeof (struct ip6_hbh))
    return -1;

  /* Required padding at the end.  */
  int npad = (8 - (offset & 7)) & 7;

  if (extbuf != NULL)
    {
      /* Make sure the buffer is large enough.  */
      if (offset + npad > extlen)
	return -1;

      add_padding (extbuf, offset, npad);
    }

  return offset + npad;
}


/* RFC 3542, 10.4

   This function inserts data items of various sizes in the data
   portion of the option.  VAL should point to the data to be
   inserted.  OFFSET specifies where in the data portion of the option
   the value should be inserted; the first byte after the option type
   and length is accessed by specifying an offset of zero.  */
int
inet6_opt_set_val (void *databuf, int offset, void *val, socklen_t vallen)
{
  memcpy ((uint8_t *) databuf + offset, val, vallen);

  return offset + vallen;
}


/* RFC 3542, 10.5

   This function parses received option extension headers returning
   the next option.  EXTBUF and EXTLEN specifies the extension header.
   OFFSET should either be zero (for the first option) or the length
   returned by a previous call to 'inet6_opt_next' or
   'inet6_opt_find'.  It specifies the position where to continue
   scanning the extension buffer.  */
int
inet6_opt_next (void *extbuf, socklen_t extlen, int offset, uint8_t *typep,
		socklen_t *lenp, void **databufp)
{
  if (offset == 0)
    offset = sizeof (struct ip6_hbh);
  else if (offset < sizeof (struct ip6_hbh))
    return -1;

  while (offset < extlen)
    {
      struct ip6_opt *opt = (struct ip6_opt *) ((uint8_t *) extbuf + offset);

      if (opt->ip6o_type == IP6OPT_PAD1)
	/* Single byte padding.  */
	++offset;
      else if (opt->ip6o_type == IP6OPT_PADN)
	offset += sizeof (struct ip6_opt) + opt->ip6o_len;
      else
	{
	  /* Check whether the option is valid.  */
	  offset += sizeof (struct ip6_opt) + opt->ip6o_len;
	  if (offset > extlen)
	    return -1;

	  *typep = opt->ip6o_type;
	  *lenp = opt->ip6o_len;
	  *databufp = opt + 1;
	  return offset;
	}
    }

  return -1;
}


/* RFC 3542, 10.6

   This function is similar to the previously described
   'inet6_opt_next' function, except this function lets the caller
   specify the option type to be searched for, instead of always
   returning the next option in the extension header.  */
int
inet6_opt_find (void *extbuf, socklen_t extlen, int offset, uint8_t type,
		socklen_t *lenp, void **databufp)
{
  if (offset == 0)
    offset = sizeof (struct ip6_hbh);
  else if (offset < sizeof (struct ip6_hbh))
    return -1;

  while (offset < extlen)
    {
      struct ip6_opt *opt = (struct ip6_opt *) ((uint8_t *) extbuf + offset);

      if (opt->ip6o_type == IP6OPT_PAD1)
	{
	  /* Single byte padding.  */
	  ++offset;
	  if (type == IP6OPT_PAD1)
	    {
	      *lenp = 0;
	      *databufp = (uint8_t *) extbuf + offset;
	      return offset;
	    }
	}
      else if (opt->ip6o_type != type)
	offset += sizeof (struct ip6_opt) + opt->ip6o_len;
      else
	{
	  /* Check whether the option is valid.  */
	  offset += sizeof (struct ip6_opt) + opt->ip6o_len;
	  if (offset > extlen)
	    return -1;

	  *lenp = opt->ip6o_len;
	  *databufp = opt + 1;
	  return offset;
	}
    }

  return -1;
}


/* RFC 3542, 10.7

   This function extracts data items of various sizes in the data
   portion of the option.  */
int
inet6_opt_get_val (void *databuf, int offset, void *val, socklen_t vallen)
{
  memcpy (val, (uint8_t *) databuf + offset, vallen);

  return offset + vallen;
}