summaryrefslogtreecommitdiff
path: root/src/lib/libc/net/ip6opt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/net/ip6opt.c')
-rw-r--r--src/lib/libc/net/ip6opt.c309
1 files changed, 1 insertions, 308 deletions
diff --git a/src/lib/libc/net/ip6opt.c b/src/lib/libc/net/ip6opt.c
index 7b4fdc4c4e..d98e300cb6 100644
--- a/src/lib/libc/net/ip6opt.c
+++ b/src/lib/libc/net/ip6opt.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ip6opt.c,v 1.6 2014/02/17 21:05:39 stsp Exp $ */ 1/* $OpenBSD: ip6opt.c,v 1.7 2014/06/13 15:41:06 chrisz Exp $ */
2/* $KAME: ip6opt.c,v 1.18 2005/06/15 07:11:35 keiichi Exp $ */ 2/* $KAME: ip6opt.c,v 1.18 2005/06/15 07:11:35 keiichi Exp $ */
3 3
4/* 4/*
@@ -41,296 +41,6 @@
41#include <stdio.h> 41#include <stdio.h>
42 42
43static int ip6optlen(u_int8_t *opt, u_int8_t *lim); 43static int ip6optlen(u_int8_t *opt, u_int8_t *lim);
44static void inet6_insert_padopt(u_char *p, int len);
45
46/*
47 * This function returns the number of bytes required to hold an option
48 * when it is stored as ancillary data, including the cmsghdr structure
49 * at the beginning, and any padding at the end (to make its size a
50 * multiple of 8 bytes). The argument is the size of the structure
51 * defining the option, which must include any pad bytes at the
52 * beginning (the value y in the alignment term "xn + y"), the type
53 * byte, the length byte, and the option data.
54 */
55int
56inet6_option_space(int nbytes)
57{
58 nbytes += 2; /* we need space for nxt-hdr and length fields */
59 return (CMSG_SPACE((nbytes + 7) & ~7));
60}
61
62/*
63 * This function is called once per ancillary data object that will
64 * contain either Hop-by-Hop or Destination options. It returns 0 on
65 * success or -1 on an error.
66 */
67int
68inet6_option_init(void *bp, struct cmsghdr **cmsgp, int type)
69{
70 struct cmsghdr *ch = (struct cmsghdr *)bp;
71
72 /* argument validation */
73 if (type != IPV6_HOPOPTS && type != IPV6_DSTOPTS)
74 return (-1);
75
76 ch->cmsg_level = IPPROTO_IPV6;
77 ch->cmsg_type = type;
78 ch->cmsg_len = CMSG_LEN(0);
79
80 *cmsgp = ch;
81 return (0);
82}
83
84/*
85 * This function appends a Hop-by-Hop option or a Destination option
86 * into an ancillary data object that has been initialized by
87 * inet6_option_init(). This function returns 0 if it succeeds or -1 on
88 * an error.
89 * multx is the value x in the alignment term "xn + y" described
90 * earlier. It must have a value of 1, 2, 4, or 8.
91 * plusy is the value y in the alignment term "xn + y" described
92 * earlier. It must have a value between 0 and 7, inclusive.
93 */
94int
95inet6_option_append(struct cmsghdr *cmsg, const u_int8_t *typep, int multx,
96 int plusy)
97{
98 int padlen, optlen, off;
99 u_char *bp = (u_char *)cmsg + cmsg->cmsg_len;
100 struct ip6_ext *eh = (struct ip6_ext *)CMSG_DATA(cmsg);
101
102 /* argument validation */
103 if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
104 return (-1);
105 if (plusy < 0 || plusy > 7)
106 return (-1);
107#if 0
108 if (typep[0] > 255)
109 return (-1);
110#endif
111
112 /*
113 * If this is the first option, allocate space for the
114 * first 2 bytes(for next header and length fields) of
115 * the option header.
116 */
117 if (bp == (u_char *)eh) {
118 bp += 2;
119 cmsg->cmsg_len += 2;
120 }
121
122 /* calculate pad length before the option. */
123 off = bp - (u_char *)eh;
124 padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
125 (off % multx);
126 padlen += plusy;
127 padlen %= multx; /* keep the pad as short as possible */
128 /* insert padding */
129 inet6_insert_padopt(bp, padlen);
130 cmsg->cmsg_len += padlen;
131 bp += padlen;
132
133 /* copy the option */
134 if (typep[0] == IP6OPT_PAD1)
135 optlen = 1;
136 else
137 optlen = typep[1] + 2;
138 memcpy(bp, typep, optlen);
139 bp += optlen;
140 cmsg->cmsg_len += optlen;
141
142 /* calculate pad length after the option and insert the padding */
143 off = bp - (u_char *)eh;
144 padlen = ((off + 7) & ~7) - off;
145 inet6_insert_padopt(bp, padlen);
146 bp += padlen;
147 cmsg->cmsg_len += padlen;
148
149 /* update the length field of the ip6 option header */
150 eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1;
151
152 return (0);
153}
154
155/*
156 * This function appends a Hop-by-Hop option or a Destination option
157 * into an ancillary data object that has been initialized by
158 * inet6_option_init(). This function returns a pointer to the 8-bit
159 * option type field that starts the option on success, or NULL on an
160 * error.
161 * The difference between this function and inet6_option_append() is
162 * that the latter copies the contents of a previously built option into
163 * the ancillary data object while the current function returns a
164 * pointer to the space in the data object where the option's TLV must
165 * then be built by the caller.
166 *
167 */
168u_int8_t *
169inet6_option_alloc(struct cmsghdr *cmsg, int datalen, int multx, int plusy)
170{
171 int padlen, off;
172 u_int8_t *bp = (u_char *)cmsg + cmsg->cmsg_len;
173 u_int8_t *retval;
174 struct ip6_ext *eh = (struct ip6_ext *)CMSG_DATA(cmsg);
175
176 /* argument validation */
177 if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
178 return (NULL);
179 if (plusy < 0 || plusy > 7)
180 return (NULL);
181
182 /*
183 * If this is the first option, allocate space for the
184 * first 2 bytes(for next header and length fields) of
185 * the option header.
186 */
187 if (bp == (u_char *)eh) {
188 bp += 2;
189 cmsg->cmsg_len += 2;
190 }
191
192 /* calculate pad length before the option. */
193 off = bp - (u_char *)eh;
194 padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
195 (off % multx);
196 padlen += plusy;
197 padlen %= multx; /* keep the pad as short as possible */
198 /* insert padding */
199 inet6_insert_padopt(bp, padlen);
200 cmsg->cmsg_len += padlen;
201 bp += padlen;
202
203 /* keep space to store specified length of data */
204 retval = bp;
205 bp += datalen;
206 cmsg->cmsg_len += datalen;
207
208 /* calculate pad length after the option and insert the padding */
209 off = bp - (u_char *)eh;
210 padlen = ((off + 7) & ~7) - off;
211 inet6_insert_padopt(bp, padlen);
212 bp += padlen;
213 cmsg->cmsg_len += padlen;
214
215 /* update the length field of the ip6 option header */
216 eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1;
217
218 return (retval);
219}
220
221/*
222 * This function processes the next Hop-by-Hop option or Destination
223 * option in an ancillary data object. If another option remains to be
224 * processed, the return value of the function is 0 and *tptrp points to
225 * the 8-bit option type field (which is followed by the 8-bit option
226 * data length, followed by the option data). If no more options remain
227 * to be processed, the return value is -1 and *tptrp is NULL. If an
228 * error occurs, the return value is -1 and *tptrp is not NULL.
229 * (RFC 2292, 6.3.5)
230 */
231int
232inet6_option_next(const struct cmsghdr *cmsg, u_int8_t **tptrp)
233{
234 struct ip6_ext *ip6e;
235 int hdrlen, optlen;
236 u_int8_t *lim;
237
238 if (cmsg->cmsg_level != IPPROTO_IPV6 ||
239 (cmsg->cmsg_type != IPV6_HOPOPTS &&
240 cmsg->cmsg_type != IPV6_DSTOPTS))
241 return (-1);
242
243 /* message length validation */
244 if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
245 return (-1);
246 ip6e = (struct ip6_ext *)CMSG_DATA(cmsg);
247 hdrlen = (ip6e->ip6e_len + 1) << 3;
248 if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
249 return (-1);
250
251 /*
252 * If the caller does not specify the starting point,
253 * simply return the 1st option.
254 * Otherwise, search the option list for the next option.
255 */
256 lim = (u_int8_t *)ip6e + hdrlen;
257 if (*tptrp == NULL)
258 *tptrp = (u_int8_t *)(ip6e + 1);
259 else {
260 if ((optlen = ip6optlen(*tptrp, lim)) == 0)
261 return (-1);
262
263 *tptrp = *tptrp + optlen;
264 }
265 if (*tptrp >= lim) { /* there is no option */
266 *tptrp = NULL;
267 return (-1);
268 }
269 /*
270 * Finally, checks if the next option is safely stored in the
271 * cmsg data.
272 */
273 if (ip6optlen(*tptrp, lim) == 0)
274 return (-1);
275 else
276 return (0);
277}
278
279/*
280 * This function is similar to the inet6_option_next() function,
281 * except this function lets the caller specify the option type to be
282 * searched for, instead of always returning the next option in the
283 * ancillary data object.
284 * Note: RFC 2292 says the type of tptrp is u_int8_t *, but we think
285 * it's a typo. The variable should be type of u_int8_t **.
286 */
287int
288inet6_option_find(const struct cmsghdr *cmsg, u_int8_t **tptrp, int type)
289{
290 struct ip6_ext *ip6e;
291 int hdrlen, optlen;
292 u_int8_t *optp, *lim;
293
294 if (cmsg->cmsg_level != IPPROTO_IPV6 ||
295 (cmsg->cmsg_type != IPV6_HOPOPTS &&
296 cmsg->cmsg_type != IPV6_DSTOPTS))
297 return (-1);
298
299 /* message length validation */
300 if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
301 return (-1);
302 ip6e = (struct ip6_ext *)CMSG_DATA(cmsg);
303 hdrlen = (ip6e->ip6e_len + 1) << 3;
304 if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
305 return (-1);
306
307 /*
308 * If the caller does not specify the starting point,
309 * search from the beginning of the option list.
310 * Otherwise, search from *the next option* of the specified point.
311 */
312 lim = (u_int8_t *)ip6e + hdrlen;
313 if (*tptrp == NULL)
314 *tptrp = (u_int8_t *)(ip6e + 1);
315 else {
316 if ((optlen = ip6optlen(*tptrp, lim)) == 0)
317 return (-1);
318
319 *tptrp = *tptrp + optlen;
320 }
321 for (optp = *tptrp; optp < lim; optp += optlen) {
322 if (*optp == type) {
323 *tptrp = optp;
324 return (0);
325 }
326 if ((optlen = ip6optlen(optp, lim)) == 0)
327 return (-1);
328 }
329
330 /* search failed */
331 *tptrp = NULL;
332 return (-1);
333}
334 44
335/* 45/*
336 * Calculate the length of a given IPv6 option. Also checks 46 * Calculate the length of a given IPv6 option. Also checks
@@ -356,23 +66,6 @@ ip6optlen(u_int8_t *opt, u_int8_t *lim)
356 return (0); 66 return (0);
357} 67}
358 68
359static void
360inet6_insert_padopt(u_char *p, int len)
361{
362 switch(len) {
363 case 0:
364 return;
365 case 1:
366 p[0] = IP6OPT_PAD1;
367 return;
368 default:
369 p[0] = IP6OPT_PADN;
370 p[1] = len - 2;
371 memset(&p[2], 0, len - 2);
372 return;
373 }
374}
375
376/* 69/*
377 * The following functions are defined in RFC3542, which is a successor 70 * The following functions are defined in RFC3542, which is a successor
378 * of RFC2292. 71 * of RFC2292.