Print this page
XXXX adding PID information to netstat output
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/inet/snmpcom.c
+++ new/usr/src/uts/common/inet/snmpcom.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 */
24 24 /* Copyright (c) 1990 Mentat Inc. */
25 25
26 26 /*
27 27 * This file contains common code for handling Options Management requests
28 28 * for SNMP/MIB.
29 29 */
30 30
31 31 #include <sys/types.h>
32 32 #include <sys/stream.h>
33 33 #include <sys/stropts.h>
34 34 #include <sys/errno.h>
35 35 #define _SUN_TPI_VERSION 2
36 36 #include <sys/tihdr.h>
37 37 #include <sys/ddi.h>
38 38 #include <sys/cmn_err.h>
39 39 #include <sys/policy.h>
40 40
41 41 #include <sys/socket.h>
42 42 #include <netinet/in.h>
43 43
44 44 #include <inet/common.h>
45 45 #include <inet/mi.h>
46 46 #include <inet/mib2.h>
47 47 #include <inet/optcom.h>
48 48 #include <inet/snmpcom.h>
49 49
50 50 #include <inet/ip.h>
51 51 #include <sys/brand.h>
52 52
53 53 #define DEFAULT_LENGTH sizeof (long)
54 54 #define DATA_MBLK_SIZE 1024
55 55 #define TOAHDR_SIZE (sizeof (struct T_optmgmt_ack) +\
56 56 sizeof (struct opthdr))
57 57
58 58 /* SNMP Option Request Structure */
59 59 typedef struct sor_s {
60 60 int sor_group;
61 61 int sor_code; /* MIB2 index value */
62 62 int sor_size;
63 63 } sor_t;
64 64
65 65 /*
66 66 * Validation Table for set requests.
67 67 */
68 68 static sor_t req_arr[] = {
69 69 { MIB2_IP, 1, sizeof (int) },
70 70 { MIB2_IP, 2, sizeof (int) },
71 71 { MIB2_IP, 21, sizeof (mib2_ipRouteEntry_t) },
72 72 { MIB2_IP, 22, sizeof (mib2_ipNetToMediaEntry_t)},
73 73 { MIB2_TCP, 13, sizeof (mib2_tcpConnEntry_t) }
74 74 };
75 75
76 76 /*
77 77 * Binary compatibility to what used to be T_CURRENT in older releases.
78 78 * Unfortunately, the binary chosen for it was different and used by
79 79 * T_PARTSUCCESS in the new name space. However T_PARTSUCESS is only
80 80 * anticiapted in new T_OPTMGM_REQ (and not O_T_OPTMGMT_REQ messages).
81 81 * Only a test for TBADFLAG which uses one of the MIB option levels
82 82 * may have trouble with this provision for binary compatibility.
83 83 */
84 84 #define OLD_T_CURRENT 0x100 /* same value as T_PARTSUCCESS */
85 85
86 86 /*
87 87 * MIB info returned in data part of M_PROTO msg. All info for a single
88 88 * request is appended in a chain of mblk's off of the M_PROTO T_OPTMGMT_ACK
89 89 * ctl buffer.
90 90 */
91 91 int
92 92 snmp_append_data(mblk_t *mpdata, char *blob, int len)
93 93 {
94 94
95 95 if (!mpdata)
96 96 return (0);
97 97 while (mpdata->b_cont)
98 98 mpdata = mpdata->b_cont;
99 99 if (mpdata->b_wptr + len >= mpdata->b_datap->db_lim) {
↓ open down ↓ |
99 lines elided |
↑ open up ↑ |
100 100 mpdata->b_cont = allocb(DATA_MBLK_SIZE, BPRI_HI);
101 101 mpdata = mpdata->b_cont;
102 102 if (!mpdata)
103 103 return (0);
104 104 }
105 105 bcopy(blob, (char *)mpdata->b_wptr, len);
106 106 mpdata->b_wptr += len;
107 107 return (1);
108 108 }
109 109
110 +int
111 +snmp_append_mblk(mblk_t *mpdata, mblk_t *mblk)
112 +{
113 + if (!mpdata || !mblk)
114 + return (0);
115 + while (mpdata->b_cont)
116 + mpdata = mpdata->b_cont;
117 + mpdata->b_cont = mblk;
118 + return (1);
119 +}
120 +
110 121 /*
111 122 * Need a form which avoids O(n^2) behavior locating the end of the
112 123 * chain every time. This is it.
113 124 */
114 125 int
115 126 snmp_append_data2(mblk_t *mpdata, mblk_t **last_mpp, char *blob, int len)
116 127 {
117 128
118 129 if (!mpdata)
119 130 return (0);
120 131 if (*last_mpp == NULL) {
121 132 while (mpdata->b_cont)
122 133 mpdata = mpdata->b_cont;
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
123 134 *last_mpp = mpdata;
124 135 }
125 136 if ((*last_mpp)->b_wptr + len >= (*last_mpp)->b_datap->db_lim) {
126 137 (*last_mpp)->b_cont = allocb(DATA_MBLK_SIZE, BPRI_HI);
127 138 *last_mpp = (*last_mpp)->b_cont;
128 139 if (!*last_mpp)
129 140 return (0);
130 141 }
131 142 bcopy(blob, (char *)(*last_mpp)->b_wptr, len);
132 143 (*last_mpp)->b_wptr += len;
144 + return (1);
145 +}
146 +
147 +int
148 +snmp_append_mblk2(mblk_t *mpdata, mblk_t **last_mpp, mblk_t *mblk)
149 +{
150 + if (!mpdata || !mblk)
151 + return (0);
152 + if (*last_mpp == NULL) {
153 + while (mpdata->b_cont)
154 + mpdata = mpdata->b_cont;
155 + *last_mpp = mpdata;
156 + }
157 + (*last_mpp)->b_cont = mblk;
158 + *last_mpp = (*last_mpp)->b_cont;
133 159 return (1);
134 160 }
135 161
136 162 /*
137 163 * SNMP requests are issued using putmsg() on a stream containing all
138 164 * relevant modules. The ctl part contains a O_T_OPTMGMT_REQ message,
139 165 * and the data part is NULL
140 166 * to process this msg. If snmpcom_req() returns FALSE, then the module
141 167 * will try optcom_req to see if its some sort of SOCKET or IP option.
142 168 * snmpcom_req returns TRUE whenever the first option is recognized as
143 169 * an SNMP request, even if a bad one.
144 170 *
145 171 * "get" is done by a single O_T_OPTMGMT_REQ with MGMT_flags set to T_CURRENT.
146 172 * All modules respond with one or msg's about what they know. Responses
147 173 * are in T_OPTMGMT_ACK format. The opthdr level/name fields identify what
148 174 * is begin returned, the len field how big it is (in bytes). The info
149 175 * itself is in the data portion of the msg. Fixed length info returned
150 176 * in one msg; each table in a separate msg.
151 177 *
152 178 * setfn() returns 1 if things ok, 0 if set request invalid or otherwise
153 179 * messed up.
154 180 *
155 181 * If the passed q is at the bottom of the module chain (q_next == NULL,
156 182 * a ctl msg with req->name, level, len all zero is sent upstream. This
157 183 * is and EOD flag to the caller.
158 184 *
159 185 * IMPORTANT:
160 186 * - The msg type is M_PROTO, not M_PCPROTO!!! This is by design,
161 187 * since multiple messages will be sent to stream head and we want
162 188 * them queued for reading, not discarded.
163 189 * - All requests which match a table entry are sent to all get/set functions
164 190 * of each module. The functions must simply ignore requests not meant
165 191 * for them: getfn() returns 0, setfn() returns 1.
166 192 */
167 193 boolean_t
168 194 snmpcom_req(queue_t *q, mblk_t *mp, pfi_t setfn, pfi_t getfn, cred_t *credp)
169 195 {
170 196 mblk_t *mpctl;
171 197 struct opthdr *req;
172 198 struct opthdr *next_req;
173 199 struct opthdr *req_end;
174 200 struct opthdr *req_start;
175 201 sor_t *sreq;
176 202 struct T_optmgmt_req *tor = (struct T_optmgmt_req *)mp->b_rptr;
177 203 struct T_optmgmt_ack *toa;
178 204 boolean_t legacy_req;
179 205
180 206 if (mp->b_cont) { /* don't deal with multiple mblk's */
181 207 freemsg(mp->b_cont);
182 208 mp->b_cont = (mblk_t *)0;
183 209 optcom_err_ack(q, mp, TSYSERR, EBADMSG);
184 210 return (B_TRUE);
185 211 }
186 212 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_optmgmt_req) ||
187 213 !(req_start = (struct opthdr *)mi_offset_param(mp,
188 214 tor->OPT_offset, tor->OPT_length)))
189 215 goto bad_req1;
190 216 if (! __TPI_OPT_ISALIGNED(req_start))
191 217 goto bad_req1;
192 218
193 219 /*
194 220 * if first option not in the MIB2 or EXPER range, return false so
195 221 * optcom_req can scope things out. Otherwise it's passed to each
196 222 * calling module to process or ignore as it sees fit.
197 223 */
198 224 if ((!(req_start->level >= MIB2_RANGE_START &&
199 225 req_start->level <= MIB2_RANGE_END)) &&
200 226 (!(req_start->level >= EXPER_RANGE_START &&
201 227 req_start->level <= EXPER_RANGE_END)))
202 228 return (B_FALSE);
203 229
204 230 switch (tor->MGMT_flags) {
205 231
206 232 case T_NEGOTIATE:
207 233 if (secpolicy_ip_config(credp, B_FALSE) != 0) {
208 234 optcom_err_ack(q, mp, TACCES, 0);
209 235 return (B_TRUE);
210 236 }
211 237 req_end = (struct opthdr *)((uchar_t *)req_start +
212 238 tor->OPT_length);
213 239 for (req = req_start; req < req_end; req = next_req) {
214 240 next_req =
215 241 (struct opthdr *)((uchar_t *)&req[1] +
216 242 _TPI_ALIGN_OPT(req->len));
217 243 if (next_req > req_end)
218 244 goto bad_req2;
219 245 for (sreq = req_arr; sreq < A_END(req_arr); sreq++) {
220 246 if (req->level == sreq->sor_group &&
221 247 req->name == sreq->sor_code)
222 248 break;
223 249 }
224 250 if (sreq >= A_END(req_arr))
225 251 goto bad_req3;
226 252 if (!(*setfn)(q, req->level, req->name,
227 253 (uchar_t *)&req[1], req->len))
228 254 goto bad_req4;
229 255 }
230 256 if (q->q_next != NULL)
231 257 putnext(q, mp);
232 258 else
233 259 freemsg(mp);
234 260 return (B_TRUE);
235 261
236 262 case OLD_T_CURRENT:
237 263 case T_CURRENT:
238 264 mpctl = allocb(TOAHDR_SIZE, BPRI_MED);
239 265 if (!mpctl) {
240 266 optcom_err_ack(q, mp, TSYSERR, ENOMEM);
241 267 return (B_TRUE);
242 268 }
243 269 mpctl->b_cont = allocb(DATA_MBLK_SIZE, BPRI_MED);
244 270 if (!mpctl->b_cont) {
245 271 freemsg(mpctl);
246 272 optcom_err_ack(q, mp, TSYSERR, ENOMEM);
247 273 return (B_TRUE);
248 274 }
249 275 mpctl->b_datap->db_type = M_PROTO;
250 276 mpctl->b_wptr += TOAHDR_SIZE;
251 277 toa = (struct T_optmgmt_ack *)mpctl->b_rptr;
252 278 toa->PRIM_type = T_OPTMGMT_ACK;
253 279 toa->OPT_offset = sizeof (struct T_optmgmt_ack);
254 280 toa->OPT_length = sizeof (struct opthdr);
255 281 toa->MGMT_flags = T_SUCCESS;
256 282 /*
257 283 * If the current process is running inside a solaris10-
258 284 * branded zone and len is 0 then it's a request for
259 285 * legacy data.
260 286 */
261 287 if (PROC_IS_BRANDED(curproc) &&
262 288 (strcmp(curproc->p_brand->b_name, "solaris10") == 0) &&
263 289 (req_start->len == 0))
264 290 legacy_req = B_TRUE;
265 291 else
266 292 legacy_req = B_FALSE;
267 293 if (!(*getfn)(q, mpctl, req_start->level, legacy_req))
268 294 freemsg(mpctl);
269 295 /*
270 296 * all data for this module has now been sent upstream. If
271 297 * this is bottom module of stream, send up an EOD ctl msg,
272 298 * otherwise pass onto the next guy for processing.
273 299 */
274 300 if (q->q_next != NULL) {
275 301 putnext(q, mp);
276 302 return (B_TRUE);
277 303 }
278 304 if (mp->b_cont) {
279 305 freemsg(mp->b_cont);
280 306 mp->b_cont = NULL;
281 307 }
282 308 mpctl = reallocb(mp, TOAHDR_SIZE, 1);
283 309 if (!mpctl) {
284 310 optcom_err_ack(q, mp, TSYSERR, ENOMEM);
285 311 return (B_TRUE);
286 312 }
287 313 mpctl->b_datap->db_type = M_PROTO;
288 314 mpctl->b_wptr = mpctl->b_rptr + TOAHDR_SIZE;
289 315 toa = (struct T_optmgmt_ack *)mpctl->b_rptr;
290 316 toa->PRIM_type = T_OPTMGMT_ACK;
291 317 toa->OPT_offset = sizeof (struct T_optmgmt_ack);
292 318 toa->OPT_length = sizeof (struct opthdr);
293 319 toa->MGMT_flags = T_SUCCESS;
294 320 req = (struct opthdr *)&toa[1];
295 321 req->level = 0;
296 322 req->name = 0;
297 323 req->len = 0;
298 324 qreply(q, mpctl);
299 325 return (B_TRUE);
300 326
301 327 default:
302 328 optcom_err_ack(q, mp, TBADFLAG, 0);
303 329 return (B_TRUE);
304 330 }
305 331
306 332 bad_req1:;
307 333 printf("snmpcom bad_req1\n");
308 334 goto bad_req;
309 335 bad_req2:;
310 336 printf("snmpcom bad_req2\n");
311 337 goto bad_req;
312 338 bad_req3:;
313 339 printf("snmpcom bad_req3\n");
314 340 goto bad_req;
315 341 bad_req4:;
316 342 printf("snmpcom bad_req4\n");
317 343 /* FALLTHRU */
318 344 bad_req:;
319 345 optcom_err_ack(q, mp, TBADOPT, 0);
320 346 return (B_TRUE);
321 347
322 348 }
↓ open down ↓ |
180 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX