#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <string.h>
#include <stdio.h>
static int ip6optlen(u_int8_t *opt, u_int8_t *lim);
static void inet6_insert_padopt(u_char *p, int len);
#ifndef IPV6_2292HOPOPTS
#define IPV6_2292HOPOPTS 22
#endif
#ifndef IPV6_2292DSTOPTS
#define IPV6_2292DSTOPTS 23
#endif
#define is_ipv6_hopopts(x) \
((x) == IPV6_HOPOPTS || (x) == IPV6_2292HOPOPTS)
#define is_ipv6_dstopts(x) \
((x) == IPV6_DSTOPTS || (x) == IPV6_2292DSTOPTS)
int
inet6_option_space(int nbytes)
{
nbytes += 2;
return(CMSG_SPACE((nbytes + 7) & ~7));
}
int
inet6_option_init(void *bp, struct cmsghdr **cmsgp, int type)
{
struct cmsghdr *ch = (struct cmsghdr *)bp;
if (!is_ipv6_hopopts(type) && !is_ipv6_dstopts(type))
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 u_int8_t *typep, int multx,
int plusy)
{
int padlen, optlen, off;
u_char *bp = (u_char *)cmsg + cmsg->cmsg_len;
struct ip6_ext *eh = (struct ip6_ext *)CMSG_DATA(cmsg);
if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
return(-1);
if (plusy < 0 || plusy > 7)
return(-1);
if (bp == (u_char *)eh) {
bp += 2;
cmsg->cmsg_len += 2;
}
off = bp - (u_char *)eh;
padlen = roundup2(off % multx, multx) - (off % multx);
padlen += plusy;
padlen %= multx;
inet6_insert_padopt(bp, padlen);
cmsg->cmsg_len += padlen;
bp += padlen;
if (typep[0] == IP6OPT_PAD1)
optlen = 1;
else
optlen = typep[1] + 2;
memcpy(bp, typep, optlen);
bp += optlen;
cmsg->cmsg_len += optlen;
off = bp - (u_char *)eh;
padlen = ((off + 7) & ~7) - off;
inet6_insert_padopt(bp, padlen);
bp += padlen;
cmsg->cmsg_len += padlen;
eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1;
return(0);
}
u_int8_t *
inet6_option_alloc(struct cmsghdr *cmsg, int datalen, int multx, int plusy)
{
int padlen, off;
u_int8_t *bp = (u_char *)cmsg + cmsg->cmsg_len;
u_int8_t *retval;
struct ip6_ext *eh = (struct ip6_ext *)CMSG_DATA(cmsg);
if (multx != 1 && multx != 2 && multx != 4 && multx != 8)
return(NULL);
if (plusy < 0 || plusy > 7)
return(NULL);
if (bp == (u_char *)eh) {
bp += 2;
cmsg->cmsg_len += 2;
}
off = bp - (u_char *)eh;
padlen = roundup2(off % multx, multx) - (off % multx);
padlen += plusy;
padlen %= multx;
inet6_insert_padopt(bp, padlen);
cmsg->cmsg_len += padlen;
bp += padlen;
retval = bp;
bp += datalen;
cmsg->cmsg_len += datalen;
off = bp - (u_char *)eh;
padlen = ((off + 7) & ~7) - off;
inet6_insert_padopt(bp, padlen);
bp += padlen;
cmsg->cmsg_len += padlen;
eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1;
return(retval);
}
int
inet6_option_next(const struct cmsghdr *cmsg, u_int8_t **tptrp)
{
struct ip6_ext *ip6e;
int hdrlen, optlen;
u_int8_t *lim;
if (cmsg->cmsg_level != IPPROTO_IPV6 ||
(!is_ipv6_hopopts(cmsg->cmsg_type) &&
!is_ipv6_dstopts(cmsg->cmsg_type)))
return(-1);
if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
return(-1);
ip6e = (struct ip6_ext *)CMSG_DATA(cmsg);
hdrlen = (ip6e->ip6e_len + 1) << 3;
if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
return(-1);
lim = (u_int8_t *)ip6e + hdrlen;
if (*tptrp == NULL)
*tptrp = (u_int8_t *)(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, u_int8_t **tptrp, int type)
{
struct ip6_ext *ip6e;
int hdrlen, optlen;
u_int8_t *optp, *lim;
if (cmsg->cmsg_level != IPPROTO_IPV6 ||
(!is_ipv6_hopopts(cmsg->cmsg_type) &&
!is_ipv6_dstopts(cmsg->cmsg_type)))
return(-1);
if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext)))
return(-1);
ip6e = (struct ip6_ext *)CMSG_DATA(cmsg);
hdrlen = (ip6e->ip6e_len + 1) << 3;
if (cmsg->cmsg_len < CMSG_SPACE(hdrlen))
return(-1);
lim = (u_int8_t *)ip6e + hdrlen;
if (*tptrp == NULL)
*tptrp = (u_int8_t *)(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(u_int8_t *opt, u_int8_t *lim)
{
int optlen;
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(u_char *p, int len)
{
switch(len) {
case 0:
return;
case 1:
p[0] = IP6OPT_PAD1;
return;
default:
p[0] = IP6OPT_PADN;
p[1] = 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, u_int8_t type,
socklen_t len, u_int8_t align, void **databufp)
{
int currentlen = offset, padlen = 0;
if (type < 2)
return(-1);
if (len > 255 || len < 0 )
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);
currentlen += padlen;
if (extlen &&
currentlen > extlen)
return(-1);
if (extbuf) {
u_int8_t *optp = (u_int8_t *)extbuf + offset;
if (padlen == 1) {
*optp = IP6OPT_PAD1;
optp++;
}
else if (padlen > 0) {
*optp++ = IP6OPT_PADN;
*optp++ = 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) {
u_int8_t *padp;
int padlen = updatelen - offset;
if (updatelen > extlen)
return(-1);
padp = (u_int8_t *)extbuf + offset;
if (padlen == 1)
*padp = IP6OPT_PAD1;
else if (padlen > 0) {
*padp++ = IP6OPT_PADN;
*padp++ = (padlen - 2);
memset(padp, 0, padlen - 2);
}
}
return(updatelen);
}
int
inet6_opt_set_val(void *databuf, int offset, void *val, socklen_t vallen)
{
memcpy((u_int8_t *)databuf + offset, val, vallen);
return(offset + vallen);
}
int
inet6_opt_next(void *extbuf, socklen_t extlen, int offset, u_int8_t *typep,
socklen_t *lenp, void **databufp)
{
u_int8_t *optp, *lim;
int optlen;
if (extlen == 0 || (extlen % 8))
return(-1);
lim = (u_int8_t *)extbuf + extlen;
if (offset == 0) {
optp = (u_int8_t *)((struct ip6_hbh *)extbuf + 1);
}
else
optp = (u_int8_t *)extbuf + offset;
while(optp < lim) {
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;
return(optp + optlen - (u_int8_t *)extbuf);
}
}
optend:
*databufp = NULL;
return(-1);
}
int
inet6_opt_find(void *extbuf, socklen_t extlen, int offset, u_int8_t type,
socklen_t *lenp, void **databufp)
{
u_int8_t *optp, *lim;
int optlen;
if (extlen == 0 || (extlen % 8))
return(-1);
lim = (u_int8_t *)extbuf + extlen;
if (offset == 0) {
optp = (u_int8_t *)((struct ip6_hbh *)extbuf + 1);
}
else
optp = (u_int8_t *)extbuf + offset;
while(optp < lim) {
if ((optlen = ip6optlen(optp, lim)) == 0)
goto optend;
if (*optp == type) {
*lenp = optlen - 2;
*databufp = optp + 2;
return(optp + optlen - (u_int8_t *)extbuf);
}
optp += optlen;
}
optend:
*databufp = NULL;
return(-1);
}
int
inet6_opt_get_val(void *databuf, int offset, void *val, socklen_t vallen)
{
memcpy(val, (u_int8_t *)databuf + offset, vallen);
return(offset + vallen);
}