]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/bsnmp/snmp_mibII/mibII_ipaddr.c
Virgin import of bsnmp 1.4
[FreeBSD/FreeBSD.git] / contrib / bsnmp / snmp_mibII / mibII_ipaddr.c
1 /*
2  * Copyright (c) 2001-2002
3  *      Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4  *      All rights reserved.
5  *
6  * Author: Harti Brandt <harti@freebsd.org>
7  *
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:
11  *
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.
20  *
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.
32  *
33  * $Begemot: bsnmp/snmp_mibII/mibII_ipaddr.c,v 1.6 2003/01/28 13:44:35 hbb Exp $
34  *
35  * IP address table. This table is writeable!
36  *
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.
39  */
40 #include "mibII.h"
41 #include "mibII_oid.h"
42
43 static const struct asn_oid
44         oid_ipAddrTable = OIDX_ipAddrTable;
45
46 /*
47  * Be careful not to hold any pointers during the SET processing - the
48  * interface and address lists can be relocated at any time.
49  */
50 struct update {
51         struct snmp_dependency dep;
52
53         u_int32_t       set;
54         struct in_addr  addr;
55         struct in_addr  mask;
56         int             bcast;
57         u_int           ifindex;
58
59         u_int32_t       rb;
60         struct in_addr  rb_mask;
61         struct in_addr  rb_bcast;
62 };
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
69
70 /*
71  * Create a new interface address
72  */
73 static int
74 create(struct update *upd)
75 {
76         struct in_addr bcast;
77         struct mibifa *ifa;
78
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);
86                 else
87                         upd->mask.s_addr = 0xffffffff;
88         }
89
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));
93
94         if ((ifa = mib_create_ifa(upd->ifindex, upd->addr, upd->mask, bcast)) == NULL)
95                 return (SNMP_ERR_GENERR);
96
97         upd->rb |= RB_CREATE;
98         return (SNMP_ERR_NOERROR);
99 }
100
101 /*
102  * Modify the netmask or broadcast address. The ifindex cannot be
103  * changed (obviously).
104  */
105 static int
106 modify(struct update *upd, struct mibifa *ifa)
107 {
108         struct mibif *ifp;
109
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);
114
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;
124                         if (upd->bcast)
125                                 ifa->inbcast.s_addr |= 0xffffffff
126                                     & ~ifa->inmask.s_addr;
127                 }
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);
134                 }
135                 upd->rb |= RB_MODIFY;
136         }
137         return (SNMP_ERR_NOERROR);
138 }
139
140 /*
141  * Remove an IP address from an interface. This is called when
142  * the SET finishes.
143  */
144 static void
145 destroy_func(struct snmp_context *ctx __unused, int fail __unused, void *arg)
146 {
147         struct mibifa *ifa = arg;
148
149         if (ifa->flags & MIBIFA_DESTROYED) {
150                 TAILQ_REMOVE(&mibifa_list, ifa, link);
151                 free(ifa);
152         }
153 }
154
155 /*
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.
159  */
160 static int
161 destroy(struct snmp_context *ctx, struct update *upd, struct mibifa *ifa)
162 {
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);
169         }
170         upd->rb |= RB_DESTROY;
171         return (SNMP_ERR_NOERROR);
172 }
173
174 /*
175  * This function is called to commit/rollback a SET on an IpAddrEntry
176  */
177 static int
178 update_func(struct snmp_context *ctx, struct snmp_dependency *dep,
179     enum snmp_depop op)
180 {
181         struct update *upd = (struct update *)dep;
182         struct mibifa *ifa;
183
184         switch (op) {
185
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));
192                 }
193                 /* existing entry */
194                 if ((upd->set & UPD_IFINDEX) && upd->ifindex == 0) {
195                         /* delete */
196                         return (destroy(ctx, upd, ifa));
197                 }
198                 /* modify entry */
199                 return (modify(upd, ifa));
200
201           case SNMP_DEPOP_ROLLBACK:
202                 if ((ifa = mib_find_ifa(upd->addr)) == NULL) {
203                         /* ups */
204                         mib_iflist_bad = 1;
205                         return (SNMP_ERR_NOERROR);
206                 }
207                 if (upd->rb & RB_CREATE) {
208                         mib_uncreate_ifa(ifa);
209                         return (SNMP_ERR_NOERROR);
210                 }
211                 if (upd->rb & RB_DESTROY) {
212                         mib_undestroy_ifa(ifa);
213                         return (SNMP_ERR_NOERROR);
214                 }
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);
220                 }
221                 return (SNMP_ERR_NOERROR);
222         }
223         abort();
224 }
225
226 /**********************************************************************/
227 /*
228  * ACTION
229  */
230 int
231 op_ipaddr(struct snmp_context *ctx, struct snmp_value *value,
232     u_int sub, u_int iidx, enum snmp_op op)
233 {
234         asn_subid_t which;
235         struct mibifa *ifa;
236         struct update *upd;
237         struct asn_oid idx;
238         u_char ipaddr[4];
239
240         which = value->var.subs[sub - 1];
241
242         ifa = NULL;
243
244         switch (op) {
245
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);
250                 break;
251
252           case SNMP_OP_GET:
253                 if ((ifa = FIND_OBJECT_OID(&mibifa_list, &value->var, sub)) == NULL)
254                         return (SNMP_ERR_NOSUCHNAME);
255                 break;
256
257           case SNMP_OP_SET:
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);
261                 idx.len = 4;
262                 idx.subs[0] = ipaddr[0];
263                 idx.subs[1] = ipaddr[1];
264                 idx.subs[2] = ipaddr[2];
265                 idx.subs[3] = ipaddr[3];
266
267                 if ((upd = (struct update *)snmp_dep_lookup(ctx,
268                     &oid_ipAddrTable, &idx, sizeof(*upd), update_func)) == NULL)
269                         return (SNMP_ERR_RES_UNAVAIL);
270
271                 upd->addr.s_addr = htonl((ipaddr[0] << 24) | (ipaddr[1] << 16) |
272                     (ipaddr[2] << 8) | (ipaddr[3] << 0));
273
274                 switch (which) {
275
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);
282                         if (ifa != NULL) {
283                                 if (ifa->ifindex != (u_int)value->v.integer &&
284                                     value->v.integer != 0)
285                                         return (SNMP_ERR_INCONS_VALUE);
286                         }
287                         upd->set |= UPD_IFINDEX;
288                         upd->ifindex = (u_int)value->v.integer;
289                         break;
290
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;
299                         break;
300
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;
308                         break;
309
310                 }
311                 return (SNMP_ERR_NOERROR);
312
313           case SNMP_OP_ROLLBACK:
314           case SNMP_OP_COMMIT:
315                 return (SNMP_ERR_NOERROR);
316         }
317
318         switch (which) {
319
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];
325                 break;
326
327           case LEAF_ipAdEntIfIndex:
328                 if (ifa->flags & MIBIFA_DESTROYED)
329                         value->v.integer = 0;
330                 else
331                         value->v.integer = ifa->ifindex;
332                 break;
333
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;
339                 break;
340
341           case LEAF_ipAdEntBcastAddr:
342                 value->v.integer = ntohl(ifa->inbcast.s_addr) & 1;
343                 break;
344
345
346           case LEAF_ipAdEntReasmMaxSize:
347                 value->v.integer = 65535;
348                 break;
349         }
350         return (SNMP_ERR_NOERROR);
351 }