2 * Copyright (c) 2001-2002
3 * Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 * Author: Harti Brandt <harti@freebsd.org>
8 * Redistribution of this software and documentation and use in source and
9 * binary forms, with or without modification, are permitted provided that
10 * the following conditions are met:
12 * 1. Redistributions of source code or documentation must retain the above
13 * copyright notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY FRAUNHOFER FOKUS
22 * AND ITS CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
25 * FRAUNHOFER FOKUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
28 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * $Begemot: bsnmp/snmp_mibII/mibII_ipaddr.c,v 1.6 2003/01/28 13:44:35 hbb Exp $
35 * IP address table. This table is writeable!
37 * Writing to this table will add a new IP address to the interface.
38 * An address can be deleted with writing the interface index 0.
41 #include "mibII_oid.h"
43 static const struct asn_oid
44 oid_ipAddrTable = OIDX_ipAddrTable;
47 * Be careful not to hold any pointers during the SET processing - the
48 * interface and address lists can be relocated at any time.
51 struct snmp_dependency dep;
60 struct in_addr rb_mask;
61 struct in_addr rb_bcast;
63 #define UPD_IFINDEX 0x0001
64 #define UPD_MASK 0x0002
65 #define UPD_BCAST 0x0004
66 #define RB_CREATE 0x0001
67 #define RB_DESTROY 0x0002
68 #define RB_MODIFY 0x0004
71 * Create a new interface address
74 create(struct update *upd)
79 if (!(upd->set & UPD_MASK)) {
80 if (IN_CLASSA(ntohl(upd->addr.s_addr)))
81 upd->mask.s_addr = htonl(IN_CLASSA_NET);
82 else if (IN_CLASSB(ntohl(upd->addr.s_addr)))
83 upd->mask.s_addr = htonl(IN_CLASSB_NET);
84 else if (IN_CLASSC(ntohl(upd->addr.s_addr)))
85 upd->mask.s_addr = htonl(IN_CLASSC_NET);
87 upd->mask.s_addr = 0xffffffff;
90 bcast.s_addr = upd->addr.s_addr & upd->mask.s_addr;
91 if (!(upd->set & UPD_BCAST) || upd->bcast)
92 bcast.s_addr |= htonl(0xffffffff & ~ntohl(upd->mask.s_addr));
94 if ((ifa = mib_create_ifa(upd->ifindex, upd->addr, upd->mask, bcast)) == NULL)
95 return (SNMP_ERR_GENERR);
98 return (SNMP_ERR_NOERROR);
102 * Modify the netmask or broadcast address. The ifindex cannot be
103 * changed (obviously).
106 modify(struct update *upd, struct mibifa *ifa)
110 if ((ifp = mib_find_if(ifa->ifindex)) == NULL)
111 return (SNMP_ERR_WRONG_VALUE);
112 if ((upd->set & UPD_IFINDEX) && upd->ifindex != ifa->ifindex)
113 return (SNMP_ERR_INCONS_VALUE);
115 upd->rb_mask = ifa->inmask;
116 upd->rb_bcast = ifa->inbcast;
117 if (((upd->set & UPD_MASK) && upd->mask.s_addr != ifa->inmask.s_addr) ||
118 (upd->set & UPD_BCAST)) {
119 if (upd->set & UPD_MASK)
120 ifa->inmask = upd->mask;
121 if (upd->set & UPD_BCAST) {
122 ifa->inbcast.s_addr = ifa->inaddr.s_addr
123 & ifa->inmask.s_addr;
125 ifa->inbcast.s_addr |= 0xffffffff
126 & ~ifa->inmask.s_addr;
128 if (mib_modify_ifa(ifa)) {
129 syslog(LOG_ERR, "set netmask/bcast: %m");
130 ifa->inmask = upd->rb_mask;
131 ifa->inbcast = upd->rb_bcast;
132 mib_unmodify_ifa(ifa);
133 return (SNMP_ERR_GENERR);
135 upd->rb |= RB_MODIFY;
137 return (SNMP_ERR_NOERROR);
141 * Remove an IP address from an interface. This is called when
145 destroy_func(struct snmp_context *ctx __unused, int fail __unused, void *arg)
147 struct mibifa *ifa = arg;
149 if (ifa->flags & MIBIFA_DESTROYED) {
150 TAILQ_REMOVE(&mibifa_list, ifa, link);
156 * Destroy the given row in the table. We remove the address from the
157 * system, but keep the structure around for the COMMIT. It's deleted
158 * only in the finish function.
161 destroy(struct snmp_context *ctx, struct update *upd, struct mibifa *ifa)
163 if (mib_destroy_ifa(ifa))
164 return (SNMP_ERR_GENERR);
165 if (snmp_set_atfinish(ctx, destroy_func, ifa)) {
166 syslog(LOG_ERR, "atfinish: %m");
167 mib_undestroy_ifa(ifa);
168 return (SNMP_ERR_GENERR);
170 upd->rb |= RB_DESTROY;
171 return (SNMP_ERR_NOERROR);
175 * This function is called to commit/rollback a SET on an IpAddrEntry
178 update_func(struct snmp_context *ctx, struct snmp_dependency *dep,
181 struct update *upd = (struct update *)dep;
186 case SNMP_DEPOP_COMMIT:
187 if ((ifa = mib_find_ifa(upd->addr)) == NULL) {
188 /* non existing entry - must have ifindex */
189 if (!(upd->set & UPD_IFINDEX))
190 return (SNMP_ERR_INCONS_NAME);
191 return (create(upd));
194 if ((upd->set & UPD_IFINDEX) && upd->ifindex == 0) {
196 return (destroy(ctx, upd, ifa));
199 return (modify(upd, ifa));
201 case SNMP_DEPOP_ROLLBACK:
202 if ((ifa = mib_find_ifa(upd->addr)) == NULL) {
205 return (SNMP_ERR_NOERROR);
207 if (upd->rb & RB_CREATE) {
208 mib_uncreate_ifa(ifa);
209 return (SNMP_ERR_NOERROR);
211 if (upd->rb & RB_DESTROY) {
212 mib_undestroy_ifa(ifa);
213 return (SNMP_ERR_NOERROR);
215 if (upd->rb & RB_MODIFY) {
216 ifa->inmask = upd->rb_mask;
217 ifa->inbcast = upd->rb_bcast;
218 mib_unmodify_ifa(ifa);
219 return (SNMP_ERR_NOERROR);
221 return (SNMP_ERR_NOERROR);
226 /**********************************************************************/
231 op_ipaddr(struct snmp_context *ctx, struct snmp_value *value,
232 u_int sub, u_int iidx, enum snmp_op op)
240 which = value->var.subs[sub - 1];
246 case SNMP_OP_GETNEXT:
247 if ((ifa = NEXT_OBJECT_OID(&mibifa_list, &value->var, sub)) == NULL)
248 return (SNMP_ERR_NOSUCHNAME);
249 index_append(&value->var, sub, &ifa->index);
253 if ((ifa = FIND_OBJECT_OID(&mibifa_list, &value->var, sub)) == NULL)
254 return (SNMP_ERR_NOSUCHNAME);
258 if (index_decode(&value->var, sub, iidx, ipaddr))
259 return (SNMP_ERR_NO_CREATION);
260 ifa = FIND_OBJECT_OID(&mibifa_list, &value->var, sub);
262 idx.subs[0] = ipaddr[0];
263 idx.subs[1] = ipaddr[1];
264 idx.subs[2] = ipaddr[2];
265 idx.subs[3] = ipaddr[3];
267 if ((upd = (struct update *)snmp_dep_lookup(ctx,
268 &oid_ipAddrTable, &idx, sizeof(*upd), update_func)) == NULL)
269 return (SNMP_ERR_RES_UNAVAIL);
271 upd->addr.s_addr = htonl((ipaddr[0] << 24) | (ipaddr[1] << 16) |
272 (ipaddr[2] << 8) | (ipaddr[3] << 0));
276 case LEAF_ipAdEntIfIndex:
277 if (upd->set & UPD_IFINDEX)
278 return (SNMP_ERR_INCONS_VALUE);
279 if (value->v.integer < 0 ||
280 value->v.integer > 0x07fffffff)
281 return (SNMP_ERR_WRONG_VALUE);
283 if (ifa->ifindex != (u_int)value->v.integer &&
284 value->v.integer != 0)
285 return (SNMP_ERR_INCONS_VALUE);
287 upd->set |= UPD_IFINDEX;
288 upd->ifindex = (u_int)value->v.integer;
291 case LEAF_ipAdEntNetMask:
292 if (upd->set & UPD_MASK)
293 return (SNMP_ERR_INCONS_VALUE);
294 upd->mask.s_addr = htonl((value->v.ipaddress[0] << 24)
295 | (value->v.ipaddress[1] << 16)
296 | (value->v.ipaddress[2] << 8)
297 | (value->v.ipaddress[3] << 0));
298 upd->set |= UPD_MASK;
301 case LEAF_ipAdEntBcastAddr:
302 if (upd->set & UPD_BCAST)
303 return (SNMP_ERR_INCONS_VALUE);
304 if (value->v.integer != 0 && value->v.integer != 1)
305 return (SNMP_ERR_WRONG_VALUE);
306 upd->bcast = value->v.integer;
307 upd->set |= UPD_BCAST;
311 return (SNMP_ERR_NOERROR);
313 case SNMP_OP_ROLLBACK:
315 return (SNMP_ERR_NOERROR);
320 case LEAF_ipAdEntAddr:
321 value->v.ipaddress[0] = ifa->index.subs[0];
322 value->v.ipaddress[1] = ifa->index.subs[1];
323 value->v.ipaddress[2] = ifa->index.subs[2];
324 value->v.ipaddress[3] = ifa->index.subs[3];
327 case LEAF_ipAdEntIfIndex:
328 if (ifa->flags & MIBIFA_DESTROYED)
329 value->v.integer = 0;
331 value->v.integer = ifa->ifindex;
334 case LEAF_ipAdEntNetMask:
335 value->v.ipaddress[0] = (ntohl(ifa->inmask.s_addr) >> 24) & 0xff;
336 value->v.ipaddress[1] = (ntohl(ifa->inmask.s_addr) >> 16) & 0xff;
337 value->v.ipaddress[2] = (ntohl(ifa->inmask.s_addr) >> 8) & 0xff;
338 value->v.ipaddress[3] = (ntohl(ifa->inmask.s_addr) >> 0) & 0xff;
341 case LEAF_ipAdEntBcastAddr:
342 value->v.integer = ntohl(ifa->inbcast.s_addr) & 1;
346 case LEAF_ipAdEntReasmMaxSize:
347 value->v.integer = 65535;
350 return (SNMP_ERR_NOERROR);