1*12e5c931Sguenther /* $OpenBSD: attribute.c,v 1.13 2017/02/03 08:23:46 guenther Exp $ */
251ca15aeSniklas /* $EOM: attribute.c,v 1.10 2000/02/20 19:58:36 niklas Exp $ */
32040585eSniklas
42040585eSniklas /*
5eb840acdSniklas * Copyright (c) 1998, 1999 Niklas Hallqvist. All rights reserved.
62040585eSniklas *
72040585eSniklas * Redistribution and use in source and binary forms, with or without
82040585eSniklas * modification, are permitted provided that the following conditions
92040585eSniklas * are met:
102040585eSniklas * 1. Redistributions of source code must retain the above copyright
112040585eSniklas * notice, this list of conditions and the following disclaimer.
122040585eSniklas * 2. Redistributions in binary form must reproduce the above copyright
132040585eSniklas * notice, this list of conditions and the following disclaimer in the
142040585eSniklas * documentation and/or other materials provided with the distribution.
152040585eSniklas *
162040585eSniklas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
172040585eSniklas * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
182040585eSniklas * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
192040585eSniklas * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
202040585eSniklas * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
212040585eSniklas * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
222040585eSniklas * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
232040585eSniklas * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
242040585eSniklas * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
252040585eSniklas * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262040585eSniklas */
272040585eSniklas
282040585eSniklas /*
292040585eSniklas * This code was written under funding by Ericsson Radio Systems.
302040585eSniklas */
312040585eSniklas
322040585eSniklas #include <sys/types.h>
33*12e5c931Sguenther #include <netinet/in.h>
342040585eSniklas #include <string.h>
352040585eSniklas
362040585eSniklas #include "attribute.h"
372040585eSniklas #include "conf.h"
382040585eSniklas #include "log.h"
392040585eSniklas #include "isakmp.h"
402040585eSniklas #include "util.h"
412040585eSniklas
422040585eSniklas u_int8_t *
attribute_set_basic(u_int8_t * buf,u_int16_t type,u_int16_t value)432040585eSniklas attribute_set_basic(u_int8_t *buf, u_int16_t type, u_int16_t value)
442040585eSniklas {
452040585eSniklas SET_ISAKMP_ATTR_TYPE(buf, ISAKMP_ATTR_MAKE(1, type));
462040585eSniklas SET_ISAKMP_ATTR_LENGTH_VALUE(buf, value);
472040585eSniklas return buf + ISAKMP_ATTR_VALUE_OFF;
482040585eSniklas }
492040585eSniklas
502040585eSniklas u_int8_t *
attribute_set_var(u_int8_t * buf,u_int16_t type,u_int8_t * value,u_int16_t len)512040585eSniklas attribute_set_var(u_int8_t *buf, u_int16_t type, u_int8_t *value,
522040585eSniklas u_int16_t len)
532040585eSniklas {
542040585eSniklas SET_ISAKMP_ATTR_TYPE(buf, ISAKMP_ATTR_MAKE(0, type));
552040585eSniklas SET_ISAKMP_ATTR_LENGTH_VALUE(buf, len);
562040585eSniklas memcpy(buf + ISAKMP_ATTR_VALUE_OFF, value, len);
572040585eSniklas return buf + ISAKMP_ATTR_VALUE_OFF + len;
582040585eSniklas }
592040585eSniklas
60baddc67cSniklas /*
61baddc67cSniklas * Execute a function FUNC taking an attribute type, value, length and ARG
62baddc67cSniklas * as arguments for each attribute in the area of ISAKMP attributes located
63baddc67cSniklas * at BUF, sized SZ. If any invocation fails, the processing aborts with a
64baddc67cSniklas * -1 return value. If all goes well return zero.
65baddc67cSniklas */
662040585eSniklas int
attribute_map(u_int8_t * buf,size_t sz,int (* func)(u_int16_t,u_int8_t *,u_int16_t,void *),void * arg)67df915834Shshoexer attribute_map(u_int8_t *buf, size_t sz, int (*func)(u_int16_t, u_int8_t *,
68df915834Shshoexer u_int16_t, void *), void *arg)
692040585eSniklas {
702040585eSniklas u_int8_t *attr;
712040585eSniklas int fmt;
722040585eSniklas u_int16_t type;
732040585eSniklas u_int8_t *value;
742040585eSniklas u_int16_t len;
752040585eSniklas
76fb9475d6Sderaadt for (attr = buf; attr < buf + sz; attr = value + len) {
772040585eSniklas if (attr + ISAKMP_ATTR_VALUE_OFF > buf + sz)
782040585eSniklas return -1;
792040585eSniklas type = GET_ISAKMP_ATTR_TYPE(attr);
802040585eSniklas fmt = ISAKMP_ATTR_FORMAT(type);
812040585eSniklas type = ISAKMP_ATTR_TYPE(type);
82df915834Shshoexer value = attr + (fmt ? ISAKMP_ATTR_LENGTH_VALUE_OFF
83df915834Shshoexer : ISAKMP_ATTR_VALUE_OFF);
842040585eSniklas len = (fmt ? ISAKMP_ATTR_LENGTH_VALUE_LEN
852040585eSniklas : GET_ISAKMP_ATTR_LENGTH_VALUE(attr));
862040585eSniklas if (value + len > buf + sz)
872040585eSniklas return -1;
882040585eSniklas if (func(type, value, len, arg))
892040585eSniklas return -1;
902040585eSniklas }
912040585eSniklas return 0;
922040585eSniklas }
932040585eSniklas
942040585eSniklas int
attribute_set_constant(char * section,char * tag,struct constant_map * map,int attr_class,u_int8_t ** attr)952040585eSniklas attribute_set_constant(char *section, char *tag, struct constant_map *map,
962040585eSniklas int attr_class, u_int8_t **attr)
972040585eSniklas {
982040585eSniklas char *name;
992040585eSniklas int value;
1002040585eSniklas
1012040585eSniklas name = conf_get_str(section, tag);
102fb9475d6Sderaadt if (!name) {
10351ca15aeSniklas LOG_DBG((LOG_MISC, 70,
1042b81057dSniklas "attribute_set_constant: no %s in the %s section", tag,
10551ca15aeSniklas section));
1062040585eSniklas return -1;
1072040585eSniklas }
1082040585eSniklas value = constant_value(map, name);
1092040585eSniklas *attr = attribute_set_basic(*attr, attr_class, value);
1102040585eSniklas return 0;
1112040585eSniklas }
112