xref: /freebsd/contrib/bsnmp/snmp_mibII/mibII_rcvaddr.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*
2  * Copyright (c) 2001-2003
3  *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4  *	All rights reserved.
5  *
6  * Author: Harti Brandt <harti@freebsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $Begemot: bsnmp/snmp_mibII/mibII_rcvaddr.c,v 1.9 2004/08/06 08:47:03 brandt Exp $
30  *
31  * Interface receive address table.
32  */
33 #include "mibII.h"
34 #include "mibII_oid.h"
35 
36 /*
37  * find receive address
38  */
39 struct mibrcvaddr *
40 mib_find_rcvaddr(u_int ifindex, const u_char *addr, size_t addrlen)
41 {
42 	struct mibrcvaddr *rcv;
43 
44 	TAILQ_FOREACH(rcv, &mibrcvaddr_list, link)
45 		if (rcv->ifindex == ifindex &&
46 		    rcv->addrlen == addrlen &&
47 		    memcmp(rcv->addr, addr, addrlen) == 0)
48 			return (rcv);
49 	return (NULL);
50 }
51 
52 /*
53  * Create receive address
54  */
55 struct mibrcvaddr *
56 mib_rcvaddr_create(struct mibif *ifp, const u_char *addr, size_t addrlen)
57 {
58 	struct mibrcvaddr *rcv;
59 	u_int i;
60 
61 	if (addrlen + OIDLEN_ifRcvAddressEntry + 1 > ASN_MAXOIDLEN)
62 		return (NULL);
63 
64 	if ((rcv = malloc(sizeof(*rcv))) == NULL)
65 		return (NULL);
66 	rcv->ifindex = ifp->index;
67 	rcv->addrlen = addrlen;
68 	memcpy(rcv->addr, addr, addrlen);
69 	rcv->flags = 0;
70 
71 	rcv->index.len = addrlen + 2;
72 	rcv->index.subs[0] = ifp->index;
73 	rcv->index.subs[1] = addrlen;
74 	for (i = 0; i < addrlen; i++)
75 		rcv->index.subs[i + 2] = addr[i];
76 
77 	INSERT_OBJECT_OID(rcv, &mibrcvaddr_list);
78 
79 	return (rcv);
80 }
81 
82 /*
83  * Delete a receive address
84  */
85 void
86 mib_rcvaddr_delete(struct mibrcvaddr *rcv)
87 {
88 	TAILQ_REMOVE(&mibrcvaddr_list, rcv, link);
89 	free(rcv);
90 }
91 
92 int
93 op_rcvaddr(struct snmp_context *ctx __unused, struct snmp_value *value,
94     u_int sub, u_int iidx __unused, enum snmp_op op)
95 {
96 	struct mibrcvaddr *rcv;
97 
98 	rcv = NULL;	/* make compiler happy */
99 
100 	switch (op) {
101 
102 	  case SNMP_OP_GETNEXT:
103 		if ((rcv = NEXT_OBJECT_OID(&mibrcvaddr_list, &value->var, sub)) == NULL)
104 			return (SNMP_ERR_NOSUCHNAME);
105 		index_append(&value->var, sub, &rcv->index);
106 		break;
107 
108 	  case SNMP_OP_GET:
109 		if ((rcv = FIND_OBJECT_OID(&mibrcvaddr_list, &value->var, sub)) == NULL)
110 			return (SNMP_ERR_NOSUCHNAME);
111 		break;
112 
113 	  case SNMP_OP_SET:
114 		if ((rcv = FIND_OBJECT_OID(&mibrcvaddr_list, &value->var, sub)) == NULL)
115 			return (SNMP_ERR_NO_CREATION);
116 		return (SNMP_ERR_NOT_WRITEABLE);
117 
118 	  case SNMP_OP_ROLLBACK:
119 	  case SNMP_OP_COMMIT:
120 		abort();
121 	}
122 
123 	switch (value->var.subs[sub - 1]) {
124 
125 	  case LEAF_ifRcvAddressStatus:
126 		value->v.integer = 1;
127 		break;
128 
129 	  case LEAF_ifRcvAddressType:
130 		value->v.integer = (rcv->flags & MIBRCVADDR_VOLATILE) ? 2 : 3;
131 		break;
132 	}
133 	return (SNMP_ERR_NOERROR);
134 }
135