1239278Sgonzo/*	$NetBSD: ieee8023_tlv.c,v 1.2 2005/12/11 12:24:54 christos Exp $	*/
2239278Sgonzo
3239278Sgonzo/*-
4239278Sgonzo * Copyright (c)2005 YAMAMOTO Takashi,
5239278Sgonzo * All rights reserved.
6239278Sgonzo *
7239278Sgonzo * Redistribution and use in source and binary forms, with or without
8239278Sgonzo * modification, are permitted provided that the following conditions
9239278Sgonzo * are met:
10239278Sgonzo * 1. Redistributions of source code must retain the above copyright
11239278Sgonzo *    notice, this list of conditions and the following disclaimer.
12239278Sgonzo * 2. Redistributions in binary form must reproduce the above copyright
13239278Sgonzo *    notice, this list of conditions and the following disclaimer in the
14239278Sgonzo *    documentation and/or other materials provided with the distribution.
15239278Sgonzo *
16239278Sgonzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17239278Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18239278Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19239278Sgonzo * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20239278Sgonzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21239278Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22239278Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23239278Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24239278Sgonzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25239278Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26239278Sgonzo * SUCH DAMAGE.
27239278Sgonzo */
28239278Sgonzo
29239278Sgonzo#include <sys/cdefs.h>
30239278Sgonzo__KERNEL_RCSID(0, "$NetBSD: ieee8023_tlv.c,v 1.2 2005/12/11 12:24:54 christos Exp $");
31239278Sgonzo
32239278Sgonzo#include <sys/param.h>
33239278Sgonzo#include <sys/systm.h>
34239278Sgonzo
35239278Sgonzo#include <net/agr/ieee8023_tlv.h>
36239278Sgonzo
37239278Sgonzoint
38239278Sgonzotlv_check(const void *p, size_t size, const struct tlvhdr *tlv,
39239278Sgonzo    const struct tlv_template *tmpl, bool check_type)
40239278Sgonzo{
41239278Sgonzo
42239278Sgonzo	while (/* CONSTCOND */ 1) {
43239278Sgonzo		if ((const char *)tlv - (const char *)p + sizeof(*tlv) > size) {
44239278Sgonzo			return EINVAL;
45239278Sgonzo		}
46239278Sgonzo		if ((check_type && tlv->tlv_type != tmpl->tmpl_type) ||
47239278Sgonzo		    tlv->tlv_length != tmpl->tmpl_length) {
48239278Sgonzo			return EINVAL;
49239278Sgonzo		}
50239278Sgonzo		if (tmpl->tmpl_type == 0) {
51239278Sgonzo			break;
52239278Sgonzo		}
53239278Sgonzo		tlv = (const struct tlvhdr *)
54239278Sgonzo		    ((const char *)tlv + tlv->tlv_length);
55239278Sgonzo		tmpl++;
56239278Sgonzo	}
57239278Sgonzo
58239278Sgonzo	return 0;
59239278Sgonzo}
60239278Sgonzo
61239278Sgonzo