#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: ip6opt.c,v 1.15 2014/02/07 02:36:06 christos Exp $");
#endif
#include "namespace.h"
#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <assert.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#ifdef __weak_alias
__weak_alias(inet6_option_alloc,_inet6_option_alloc)
__weak_alias(inet6_option_append,_inet6_option_append)
__weak_alias(inet6_option_find,_inet6_option_find)
__weak_alias(inet6_option_init,_inet6_option_init)
__weak_alias(inet6_option_next,_inet6_option_next)
__weak_alias(inet6_option_space,_inet6_option_space)
__weak_alias(inet6_opt_init, _inet6_opt_init)
__weak_alias(inet6_opt_append, _inet6_opt_append)
__weak_alias(inet6_opt_finish, _inet6_opt_finish)
__weak_alias(inet6_opt_set_val, _inet6_opt_set_val)
__weak_alias(inet6_opt_next, _inet6_opt_next)
__weak_alias(inet6_opt_find, _inet6_opt_find)
__weak_alias(inet6_opt_get_val, _inet6_opt_get_val)
#endif
static int ip6optlen(uint8_t *opt, uint8_t *lim);
static void inet6_insert_padopt(uint8_t *p, size_t len);
int
inet6_option_space(int nbytes)
{
size_t sp;
nbytes += 2;
sp = CMSG_SPACE((nbytes + 7) & ~7);
_DIAGASSERT(__type_fit(int, sp));
return (int)sp;
}
int
inet6_option_init(void *bp, struct cmsghdr **cmsgp, int type)
{
register struct cmsghdr *ch;
_DIAGASSERT(bp != NULL);
_DIAGASSERT(cmsgp != NULL);
ch = (struct cmsghdr *)bp;
if (type != IPV6_HOPOPTS && type != IPV6_DSTOPTS)
return(-1);
ch->cmsg_level = IPPROTO_IPV6;
ch->cmsg_type = type;
ch->cmsg_len = CMSG_LEN(0);
*cmsgp = ch;
return(0);
}
int
inet6_option_append(struct cmsghdr *cmsg, const uint8_t *typep, int multx,
int plusy)
{
size_t padlen, optlen, off;
register uint8_t *bp;
struct ip6_ext *eh;
_DIAGASSERT(cmsg != NULL);
_DIAGASSERT(typep != NULL);
bp = (uint8_t *)(void *)cmsg + cmsg->cmsg_len;
eh = (struct ip6_ext *)(void *)CMSG_DATA(cmsg);
if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
return(-1);
if (plusy < 0 || plusy > 7)
return(-1);
if (bp == (uint8_t *)(void *)eh) {
bp += 2;
cmsg->cmsg_len += 2;
}
off = bp - (uint8_t *)(void *)eh;
padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
(off % multx);
padlen += plusy;
padlen %= multx;
inet6_insert_padopt(bp, padlen);
_DIAGASSERT(__type_fit(socklen_t, padlen + cmsg->cmsg_len));
cmsg->cmsg_len += (socklen_t)padlen;
bp += padlen;
if (typep[0] == IP6OPT_PAD1)
optlen = 1;
else
optlen = typep[1] + 2;
memcpy(bp, typep, (size_t)optlen);
bp += optlen;
_DIAGASSERT(__type_fit(socklen_t, optlen + cmsg->cmsg_len));
cmsg->cmsg_len += (socklen_t)optlen;
off = bp - (uint8_t *)(void *)eh;
padlen = ((off + 7) & ~7) - off;
inet6_insert_padopt(bp, padlen);
bp += padlen;
_DIAGASSERT(__type_fit(socklen_t, padlen + cmsg->cmsg_len));
cmsg->cmsg_len += (socklen_t)padlen;
off = bp - (uint8_t *)(void *)eh;
_DIAGASSERT(__type_fit(uint8_t, (off >> 3) - 1));
eh->ip6e_len = (uint8_t)((off >> 3) - 1);
return(0);
}
uint8_t *
inet6_option_alloc(struct cmsghdr *cmsg, int datalen, int multx, int plusy)
{
size_t padlen, off;
register uint8_t *bp;
uint8_t *retval;
struct ip6_ext *eh;
_DIAGASSERT(cmsg != NULL);
bp = (uint8_t *)(void *)cmsg + cmsg->cmsg_len;
eh = (struct ip6_ext *)(void *)CMSG_DATA(cmsg);
if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
return(NULL);
if (plusy < 0 || plusy > 7)
return(NULL);
if (bp == (uint8_t *)(void *)eh) {
bp += 2;
cmsg->cmsg_len += 2;
}
off = bp - (uint8_t *)(void *)eh;
padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) -
(off % multx);
padlen += plusy;
padlen %= multx;
inet6_insert_padopt(bp, padlen);
cmsg->cmsg_len += (socklen_t)padlen;
bp += padlen;
retval = bp;
bp += datalen;
cmsg->cmsg_len += datalen;
off = bp - (uint8_t *)(void *)eh;
padlen = ((off + 7) & ~7) - off;
inet6_insert_padopt(bp, padlen);
bp += padlen;
_DIAGASSERT(__type_fit(socklen_t, padlen + cmsg->cmsg_len));
cmsg->cmsg_len += (socklen_t)padlen;
off = bp - (uint8_t *)(void *)eh;
_DIAGASSERT(__type_fit(uint8_t, (off >> 3) - 1));
eh->ip6e_len = (uint8_t)((off >> 3) - 1);
return(retval);
}
int
inet6_option_next(const struct cmsghdr *cmsg, uint8_t **tptrp)
{
struct ip6_ext *ip6e;
int hdrlen, optlen;
uint8_t *lim;
_DIAGASSERT(cmsg != NULL);
_DIAGASSERT(tptrp != NULL);
if (cmsg->cmsg_level != IPPROTO_IPV6 ||
(cmsg->cmsg_type != IPV6_HOPOPTS &&
cmsg->cmsg_type != IPV6_DSTOPTS))
return(-1);
if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
return(-1);
ip6e = __UNCONST(CCMSG_DATA(cmsg));
hdrlen = (ip6e->ip6e_len + 1) << 3;
if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
return(-1);
lim = (uint8_t *)(void *)ip6e + hdrlen;
if (*tptrp == NULL)
*tptrp = (uint8_t *)(void *)(ip6e + 1);
else {
if ((optlen = ip6optlen(*tptrp, lim)) == 0)
return(-1);
*tptrp = *tptrp + optlen;
}
if (*tptrp >= lim) {
*tptrp = NULL;
return(-1);
}
if (ip6optlen(*tptrp, lim) == 0)
return(-1);
else
return(0);
}
int
inet6_option_find(const struct cmsghdr *cmsg, uint8_t **tptrp, int type)
{
struct ip6_ext *ip6e;
int hdrlen, optlen;
uint8_t *optp, *lim;
_DIAGASSERT(cmsg != NULL);
_DIAGASSERT(tptrp != NULL);
if (cmsg->cmsg_level != IPPROTO_IPV6 ||
(cmsg->cmsg_type != IPV6_HOPOPTS &&
cmsg->cmsg_type != IPV6_DSTOPTS))
return(-1);
if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
return(-1);
ip6e = __UNCONST(CCMSG_DATA(cmsg));
hdrlen = (ip6e->ip6e_len + 1) << 3;
if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
return(-1);
lim = (uint8_t *)(void *)ip6e + hdrlen;
if (*tptrp == NULL)
*tptrp = (uint8_t *)(void *)(ip6e + 1);
else {
if ((optlen = ip6optlen(*tptrp, lim)) == 0)
return(-1);
*tptrp = *tptrp + optlen;
}
for (optp = *tptrp; optp < lim; optp += optlen) {
if (*optp == type) {
*tptrp = optp;
return(0);
}
if ((optlen = ip6optlen(optp, lim)) == 0)
return(-1);
}
*tptrp = NULL;
return(-1);
}
static int
ip6optlen(uint8_t *opt, uint8_t *lim)
{
int optlen;
_DIAGASSERT(opt != NULL);
_DIAGASSERT(lim != NULL);
if (*opt == IP6OPT_PAD1)
optlen = 1;
else {
if (opt + 2 > lim)
return(0);
optlen = *(opt + 1) + 2;
}
if (opt + optlen <= lim)
return(optlen);
return(0);
}
static void
inet6_insert_padopt(uint8_t *p, size_t len)
{
_DIAGASSERT(p != NULL);
switch(len) {
case 0:
return;
case 1:
p[0] = IP6OPT_PAD1;
return;
default:
p[0] = IP6OPT_PADN;
_DIAGASSERT(__type_fit(uint8_t, len - 2));
p[1] = (uint8_t)(len - 2);
memset(&p[2], 0, len - 2);
return;
}
}
int
inet6_opt_init(void *extbuf, socklen_t extlen)
{
struct ip6_ext *ext = (struct ip6_ext *)extbuf;
if (ext) {
if (extlen == 0 || (extlen % 8))
return (-1);
ext->ip6e_len = (extlen >> 3) - 1;
}
return (2);
}
int
inet6_opt_append(void *extbuf, socklen_t extlen, int offset, uint8_t type,
socklen_t len, uint8_t align, void **databufp)
{
int currentlen = offset;
size_t padlen = 0;
if (type < 2)
return (-1);
if (len > 255)
return (-1);
if (align != 1 && align != 2 && align != 4 && align != 8)
return (-1);
if (align > len)
return (-1);
currentlen += 2 + len;
if (currentlen % align)
padlen = align - (currentlen % align);
_DIAGASSERT(__type_fit(int, currentlen + padlen));
currentlen += (int)padlen;
if (extlen &&
(socklen_t)currentlen > extlen)
return (-1);
if (extbuf) {
uint8_t *optp = (uint8_t *)extbuf + offset;
if (padlen == 1) {
*optp = IP6OPT_PAD1;
optp++;
} else if (padlen > 0) {
*optp++ = IP6OPT_PADN;
_DIAGASSERT(__type_fit(uint8_t, padlen - 2));
*optp++ = (uint8_t)(padlen - 2);
memset(optp, 0, padlen - 2);
optp += (padlen - 2);
}
*optp++ = type;
*optp++ = len;
*databufp = optp;
}
return (currentlen);
}
int
inet6_opt_finish(void *extbuf, socklen_t extlen, int offset)
{
int updatelen = offset > 0 ? (1 + ((offset - 1) | 7)) : 0;
if (extbuf) {
uint8_t *padp;
size_t padlen = updatelen - offset;
if ((socklen_t)updatelen > extlen || padlen >= 256 + 2)
return (-1);
padp = (uint8_t *)extbuf + offset;
if (padlen == 1)
*padp = IP6OPT_PAD1;
else if (padlen > 0) {
*padp++ = IP6OPT_PADN;
*padp++ = (uint8_t)(padlen - 2);
memset(padp, 0, padlen - 2);
}
}
return (updatelen);
}
int
inet6_opt_set_val(void *databuf, int offset, void *val, socklen_t vallen)
{
memcpy((uint8_t *)databuf + offset, val, vallen);
return (offset + vallen);
}
int
inet6_opt_next(void *extbuf, socklen_t extlen, int offset, uint8_t *typep,
socklen_t *lenp, void **databufp)
{
uint8_t *optp, *lim;
int optlen;
if (extlen == 0 || (extlen % 8))
return (-1);
lim = (uint8_t *)extbuf + extlen;
if (offset == 0)
optp = (uint8_t *)(void *)((struct ip6_hbh *)extbuf + 1);
else
optp = (uint8_t *)extbuf + offset;
while (optp < lim) {
ptrdiff_t rv;
switch(*optp) {
case IP6OPT_PAD1:
optp++;
break;
case IP6OPT_PADN:
if ((optlen = ip6optlen(optp, lim)) == 0)
goto optend;
optp += optlen;
break;
default:
if ((optlen = ip6optlen(optp, lim)) == 0)
goto optend;
*typep = *optp;
*lenp = optlen - 2;
*databufp = optp + 2;
rv = optp + optlen - (uint8_t *)extbuf;
_DIAGASSERT(__type_fit(int, rv));
return (int)rv;
}
}
optend:
*databufp = NULL;
return (-1);
}
int
inet6_opt_find(void *extbuf, socklen_t extlen, int offset, uint8_t type,
socklen_t *lenp, void **databufp)
{
uint8_t *optp, *lim;
int optlen;
if (extlen == 0 || (extlen % 8))
return (-1);
lim = (uint8_t *)extbuf + extlen;
if (offset == 0)
optp = (uint8_t *)(void *)((struct ip6_hbh *)extbuf + 1);
else
optp = (uint8_t *)extbuf + offset;
while (optp < lim) {
if ((optlen = ip6optlen(optp, lim)) == 0)
goto optend;
if (*optp == type) {
ptrdiff_t td;
*lenp = optlen - 2;
*databufp = optp + 2;
td = optp + optlen - (uint8_t *)extbuf;
_DIAGASSERT(__type_fit(int, td));
return (int)td;
}
optp += optlen;
}
optend:
*databufp = NULL;
return (-1);
}
int
inet6_opt_get_val(void *databuf, int offset, void *val, socklen_t vallen)
{
memcpy(val, (uint8_t *)databuf + offset, vallen);
return (offset + vallen);
}