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