xref: /freebsd/contrib/bsnmp/snmp_mibII/mibII_nettomedia.c (revision d37ea99837e6ad50837fd9fe1771ddf1c3ba6002)
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 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_nettomedia.c,v 1.7 2003/12/03 10:01:19 hbb Exp $
34  *
35  * Read-only implementation of the Arp table (ipNetToMediaTable)
36  *
37  * The problem with the table is, that we don't receive routing message
38  * when a) an arp table entry is resolved and b) when an arp table entry is
39  * deleted automatically. Therefor we need to poll the table from time to
40  * time.
41  */
42 #include "mibII.h"
43 #include "mibII_oid.h"
44 
45 #define ARPREFRESH	30
46 
47 struct mibarp *
48 mib_find_arp(const struct mibif *ifp, struct in_addr in)
49 {
50 	struct mibarp *at;
51 	u_int32_t a = ntohl(in.s_addr);
52 
53 	if (get_ticks() >= mibarpticks + ARPREFRESH)
54 		mib_arp_update();
55 
56 	TAILQ_FOREACH(at, &mibarp_list, link)
57 		if (at->index.subs[0] == ifp->index &&
58 		    (at->index.subs[1] == ((a >> 24) & 0xff)) &&
59 		    (at->index.subs[2] == ((a >> 16) & 0xff)) &&
60 		    (at->index.subs[3] == ((a >>  8) & 0xff)) &&
61 		    (at->index.subs[4] == ((a >>  0) & 0xff)))
62 			return (at);
63 	return (NULL);
64 }
65 
66 struct mibarp *
67 mib_arp_create(const struct mibif *ifp, struct in_addr in, const u_char *phys,
68     size_t physlen)
69 {
70 	struct mibarp *at;
71 	u_int32_t a = ntohl(in.s_addr);
72 
73 	if ((at = malloc(sizeof(*at))) == NULL)
74 		return (NULL);
75 	at->flags = 0;
76 
77 	at->index.len = 5;
78 	at->index.subs[0] = ifp->index;
79 	at->index.subs[1] = (a >> 24) & 0xff;
80 	at->index.subs[2] = (a >> 16) & 0xff;
81 	at->index.subs[3] = (a >>  8) & 0xff;
82 	at->index.subs[4] = (a >>  0) & 0xff;
83 	if ((at->physlen = physlen) > sizeof(at->phys))
84 		at->physlen = sizeof(at->phys);
85 	memcpy(at->phys, phys, at->physlen);
86 
87 	INSERT_OBJECT_OID(at, &mibarp_list);
88 
89 	return (at);
90 }
91 
92 void
93 mib_arp_delete(struct mibarp *at)
94 {
95 	TAILQ_REMOVE(&mibarp_list, at, link);
96 	free(at);
97 }
98 
99 int
100 op_nettomedia(struct snmp_context *ctx __unused, struct snmp_value *value,
101     u_int sub, u_int iidx __unused, enum snmp_op op)
102 {
103 	struct mibarp *at;
104 
105 	at = NULL;	/* gcc */
106 
107 	if (get_ticks() >= mibarpticks + ARPREFRESH)
108 		mib_arp_update();
109 
110 	switch (op) {
111 
112 	  case SNMP_OP_GETNEXT:
113 		if ((at = NEXT_OBJECT_OID(&mibarp_list, &value->var, sub)) == NULL)
114 			return (SNMP_ERR_NOSUCHNAME);
115 		index_append(&value->var, sub, &at->index);
116 		break;
117 
118 	  case SNMP_OP_GET:
119 		if ((at = FIND_OBJECT_OID(&mibarp_list, &value->var, sub)) == NULL)
120 			return (SNMP_ERR_NOSUCHNAME);
121 		break;
122 
123 	  case SNMP_OP_SET:
124 		if ((at = FIND_OBJECT_OID(&mibarp_list, &value->var, sub)) == NULL)
125 			return (SNMP_ERR_NO_CREATION);
126 		return (SNMP_ERR_NOT_WRITEABLE);
127 
128 	  case SNMP_OP_ROLLBACK:
129 	  case SNMP_OP_COMMIT:
130 		abort();
131 	}
132 
133 	switch (value->var.subs[sub - 1]) {
134 
135 	  case LEAF_ipNetToMediaIfIndex:
136 		value->v.integer = at->index.subs[0];
137 		break;
138 
139 	  case LEAF_ipNetToMediaPhysAddress:
140 		return (string_get(value, at->phys, at->physlen));
141 
142 	  case LEAF_ipNetToMediaNetAddress:
143 		value->v.ipaddress[0] = at->index.subs[1];
144 		value->v.ipaddress[1] = at->index.subs[2];
145 		value->v.ipaddress[2] = at->index.subs[3];
146 		value->v.ipaddress[3] = at->index.subs[4];
147 		break;
148 
149 	  case LEAF_ipNetToMediaType:
150 		value->v.integer = (at->flags & MIBARP_PERM) ? 4 : 3;
151 		break;
152 	}
153 	return (SNMP_ERR_NOERROR);
154 }
155