paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* SNMP support |
| 2 | * Copyright (C) 1999 Kunihiro Ishiguro <kunihiro@zebra.org> |
| 3 | * |
| 4 | * This file is part of GNU Zebra. |
| 5 | * |
| 6 | * GNU Zebra is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the |
| 8 | * Free Software Foundation; either version 2, or (at your option) any |
| 9 | * later version. |
| 10 | * |
| 11 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 18 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | * 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <zebra.h> |
| 23 | |
| 24 | #ifdef HAVE_SNMP |
paul | 07661cb | 2003-03-18 00:03:05 +0000 | [diff] [blame] | 25 | #include <net-snmp/net-snmp-config.h> |
Joakim Tjernlund | fb62a3c | 2008-05-13 20:03:32 +0200 | [diff] [blame] | 26 | #include <net-snmp/net-snmp-includes.h> |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 27 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 28 | #include "log.h" |
| 29 | #include "thread.h" |
| 30 | #include "linklist.h" |
| 31 | #include "command.h" |
gdt | 5e4fa16 | 2004-03-16 14:38:36 +0000 | [diff] [blame] | 32 | #include <lib/version.h> |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 33 | #include "memory.h" |
| 34 | #include "sockunion.h" |
paul | dd488a7 | 2003-06-19 01:21:07 +0000 | [diff] [blame] | 35 | #include "smux.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 36 | |
| 37 | #define min(A,B) ((A) < (B) ? (A) : (B)) |
| 38 | |
| 39 | enum smux_event {SMUX_SCHEDULE, SMUX_CONNECT, SMUX_READ}; |
| 40 | |
| 41 | void smux_event (enum smux_event, int); |
| 42 | |
| 43 | |
| 44 | /* SMUX socket. */ |
| 45 | int smux_sock = -1; |
| 46 | |
| 47 | /* SMUX subtree list. */ |
| 48 | struct list *treelist; |
| 49 | |
| 50 | /* SMUX oid. */ |
hasso | c75105a | 2004-10-13 10:33:26 +0000 | [diff] [blame] | 51 | oid *smux_oid = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 52 | size_t smux_oid_len; |
| 53 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 54 | /* SMUX password. */ |
hasso | c75105a | 2004-10-13 10:33:26 +0000 | [diff] [blame] | 55 | char *smux_passwd = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 56 | |
| 57 | /* SMUX read threads. */ |
| 58 | struct thread *smux_read_thread; |
| 59 | |
| 60 | /* SMUX connect thrads. */ |
| 61 | struct thread *smux_connect_thread; |
| 62 | |
| 63 | /* SMUX debug flag. */ |
| 64 | int debug_smux = 0; |
| 65 | |
| 66 | /* SMUX failure count. */ |
| 67 | int fail = 0; |
| 68 | |
| 69 | /* SMUX node. */ |
Stephen Hemminger | 7fc626d | 2008-12-01 11:10:34 -0800 | [diff] [blame] | 70 | static struct cmd_node smux_node = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 71 | { |
| 72 | SMUX_NODE, |
| 73 | "" /* SMUX has no interface. */ |
| 74 | }; |
paul | dd488a7 | 2003-06-19 01:21:07 +0000 | [diff] [blame] | 75 | |
| 76 | /* thread master */ |
| 77 | static struct thread_master *master; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 78 | |
| 79 | void * |
Stephen Hemminger | 65d3fbb | 2009-05-15 09:59:03 -0700 | [diff] [blame] | 80 | oid_copy (void *dest, const void *src, size_t size) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 81 | { |
| 82 | return memcpy (dest, src, size * sizeof (oid)); |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | oid2in_addr (oid oid[], int len, struct in_addr *addr) |
| 87 | { |
| 88 | int i; |
| 89 | u_char *pnt; |
| 90 | |
| 91 | if (len == 0) |
| 92 | return; |
| 93 | |
| 94 | pnt = (u_char *) addr; |
| 95 | |
| 96 | for (i = 0; i < len; i++) |
| 97 | *pnt++ = oid[i]; |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | oid_copy_addr (oid oid[], struct in_addr *addr, int len) |
| 102 | { |
| 103 | int i; |
| 104 | u_char *pnt; |
| 105 | |
| 106 | if (len == 0) |
| 107 | return; |
| 108 | |
| 109 | pnt = (u_char *) addr; |
| 110 | |
| 111 | for (i = 0; i < len; i++) |
| 112 | oid[i] = *pnt++; |
| 113 | } |
| 114 | |
| 115 | int |
| 116 | oid_compare (oid *o1, int o1_len, oid *o2, int o2_len) |
| 117 | { |
| 118 | int i; |
| 119 | |
| 120 | for (i = 0; i < min (o1_len, o2_len); i++) |
| 121 | { |
| 122 | if (o1[i] < o2[i]) |
| 123 | return -1; |
| 124 | else if (o1[i] > o2[i]) |
| 125 | return 1; |
| 126 | } |
| 127 | if (o1_len < o2_len) |
| 128 | return -1; |
| 129 | if (o1_len > o2_len) |
| 130 | return 1; |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 135 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 136 | oid_compare_part (oid *o1, int o1_len, oid *o2, int o2_len) |
| 137 | { |
| 138 | int i; |
| 139 | |
| 140 | for (i = 0; i < min (o1_len, o2_len); i++) |
| 141 | { |
| 142 | if (o1[i] < o2[i]) |
| 143 | return -1; |
| 144 | else if (o1[i] > o2[i]) |
| 145 | return 1; |
| 146 | } |
| 147 | if (o1_len < o2_len) |
| 148 | return -1; |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 153 | static void |
Stephen Hemminger | 65d3fbb | 2009-05-15 09:59:03 -0700 | [diff] [blame] | 154 | smux_oid_dump (const char *prefix, const oid *oid, size_t oid_len) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 155 | { |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 156 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 157 | int first = 1; |
| 158 | char buf[MAX_OID_LEN * 3]; |
| 159 | |
| 160 | buf[0] = '\0'; |
| 161 | |
| 162 | for (i = 0; i < oid_len; i++) |
| 163 | { |
| 164 | sprintf (buf + strlen (buf), "%s%d", first ? "" : ".", (int) oid[i]); |
| 165 | first = 0; |
| 166 | } |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 167 | zlog_debug ("%s: %s", prefix, buf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 170 | static int |
Stephen Hemminger | 65d3fbb | 2009-05-15 09:59:03 -0700 | [diff] [blame] | 171 | smux_socket (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 172 | { |
| 173 | int ret; |
| 174 | #ifdef HAVE_IPV6 |
| 175 | struct addrinfo hints, *res0, *res; |
| 176 | int gai; |
| 177 | #else |
| 178 | struct sockaddr_in serv; |
| 179 | struct servent *sp; |
| 180 | #endif |
| 181 | int sock = 0; |
| 182 | |
| 183 | #ifdef HAVE_IPV6 |
| 184 | memset(&hints, 0, sizeof(hints)); |
| 185 | hints.ai_family = PF_UNSPEC; |
| 186 | hints.ai_socktype = SOCK_STREAM; |
| 187 | gai = getaddrinfo(NULL, "smux", &hints, &res0); |
| 188 | if (gai == EAI_SERVICE) |
| 189 | { |
| 190 | char servbuf[NI_MAXSERV]; |
| 191 | sprintf(servbuf,"%d",SMUX_PORT_DEFAULT); |
| 192 | servbuf[sizeof (servbuf) - 1] = '\0'; |
| 193 | gai = getaddrinfo(NULL, servbuf, &hints, &res0); |
| 194 | } |
| 195 | if (gai) |
| 196 | { |
| 197 | zlog_warn("Cannot locate loopback service smux"); |
| 198 | return -1; |
| 199 | } |
| 200 | for(res=res0; res; res=res->ai_next) |
| 201 | { |
| 202 | if (res->ai_family != AF_INET |
| 203 | #ifdef HAVE_IPV6 |
| 204 | && res->ai_family != AF_INET6 |
| 205 | #endif /* HAVE_IPV6 */ |
| 206 | ) |
| 207 | continue; |
| 208 | |
| 209 | sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); |
| 210 | if (sock < 0) |
| 211 | continue; |
| 212 | sockopt_reuseaddr (sock); |
| 213 | sockopt_reuseport (sock); |
| 214 | ret = connect (sock, res->ai_addr, res->ai_addrlen); |
| 215 | if (ret < 0) |
| 216 | { |
| 217 | close(sock); |
| 218 | sock = -1; |
| 219 | continue; |
| 220 | } |
| 221 | break; |
| 222 | } |
| 223 | freeaddrinfo(res0); |
| 224 | if (sock < 0) |
| 225 | zlog_warn ("Can't connect to SNMP agent with SMUX"); |
| 226 | #else |
| 227 | sock = socket (AF_INET, SOCK_STREAM, 0); |
| 228 | if (sock < 0) |
| 229 | { |
| 230 | zlog_warn ("Can't make socket for SNMP"); |
| 231 | return -1; |
| 232 | } |
| 233 | |
| 234 | memset (&serv, 0, sizeof (struct sockaddr_in)); |
| 235 | serv.sin_family = AF_INET; |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 236 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 237 | serv.sin_len = sizeof (struct sockaddr_in); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 238 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 239 | |
| 240 | sp = getservbyname ("smux", "tcp"); |
| 241 | if (sp != NULL) |
| 242 | serv.sin_port = sp->s_port; |
| 243 | else |
| 244 | serv.sin_port = htons (SMUX_PORT_DEFAULT); |
| 245 | |
| 246 | serv.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
| 247 | |
| 248 | sockopt_reuseaddr (sock); |
| 249 | sockopt_reuseport (sock); |
| 250 | |
| 251 | ret = connect (sock, (struct sockaddr *) &serv, sizeof (struct sockaddr_in)); |
| 252 | if (ret < 0) |
| 253 | { |
| 254 | close (sock); |
| 255 | smux_sock = -1; |
| 256 | zlog_warn ("Can't connect to SNMP agent with SMUX"); |
| 257 | return -1; |
| 258 | } |
| 259 | #endif |
| 260 | return sock; |
| 261 | } |
| 262 | |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 263 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 264 | smux_getresp_send (oid objid[], size_t objid_len, long reqid, long errstat, |
| 265 | long errindex, u_char val_type, void *arg, size_t arg_len) |
| 266 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 267 | u_char buf[BUFSIZ]; |
| 268 | u_char *ptr, *h1, *h1e, *h2, *h2e; |
paul | 70e149e | 2004-10-31 16:29:50 +0000 | [diff] [blame] | 269 | size_t len, length; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 270 | |
| 271 | ptr = buf; |
| 272 | len = BUFSIZ; |
| 273 | length = len; |
| 274 | |
| 275 | if (debug_smux) |
| 276 | { |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 277 | zlog_debug ("SMUX GETRSP send"); |
| 278 | zlog_debug ("SMUX GETRSP reqid: %ld", reqid); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | h1 = ptr; |
| 282 | /* Place holder h1 for complete sequence */ |
| 283 | ptr = asn_build_sequence (ptr, &len, (u_char) SMUX_GETRSP, 0); |
| 284 | h1e = ptr; |
| 285 | |
| 286 | ptr = asn_build_int (ptr, &len, |
| 287 | (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER), |
| 288 | &reqid, sizeof (reqid)); |
| 289 | |
| 290 | if (debug_smux) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 291 | zlog_debug ("SMUX GETRSP errstat: %ld", errstat); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 292 | |
| 293 | ptr = asn_build_int (ptr, &len, |
| 294 | (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER), |
| 295 | &errstat, sizeof (errstat)); |
| 296 | if (debug_smux) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 297 | zlog_debug ("SMUX GETRSP errindex: %ld", errindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 298 | |
| 299 | ptr = asn_build_int (ptr, &len, |
| 300 | (u_char) (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER), |
| 301 | &errindex, sizeof (errindex)); |
| 302 | |
| 303 | h2 = ptr; |
| 304 | /* Place holder h2 for one variable */ |
| 305 | ptr = asn_build_sequence (ptr, &len, |
| 306 | (u_char)(ASN_SEQUENCE | ASN_CONSTRUCTOR), |
| 307 | 0); |
| 308 | h2e = ptr; |
| 309 | |
| 310 | ptr = snmp_build_var_op (ptr, objid, &objid_len, |
| 311 | val_type, arg_len, arg, &len); |
| 312 | |
| 313 | /* Now variable size is known, fill in size */ |
| 314 | asn_build_sequence(h2,&length,(u_char)(ASN_SEQUENCE|ASN_CONSTRUCTOR),ptr-h2e); |
| 315 | |
| 316 | /* Fill in size of whole sequence */ |
| 317 | asn_build_sequence(h1,&length,(u_char)SMUX_GETRSP,ptr-h1e); |
| 318 | |
| 319 | if (debug_smux) |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 320 | zlog_debug ("SMUX getresp send: %td", (ptr - buf)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 321 | |
Stephen Hemminger | 004b123 | 2011-12-07 00:55:15 +0400 | [diff] [blame] | 322 | send (smux_sock, buf, (ptr - buf), 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 325 | static u_char * |
| 326 | smux_var (u_char *ptr, size_t len, oid objid[], size_t *objid_len, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 327 | size_t *var_val_len, |
| 328 | u_char *var_val_type, |
| 329 | void **var_value) |
| 330 | { |
| 331 | u_char type; |
| 332 | u_char val_type; |
| 333 | size_t val_len; |
| 334 | u_char *val; |
| 335 | |
| 336 | if (debug_smux) |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 337 | zlog_debug ("SMUX var parse: len %zd", len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 338 | |
| 339 | /* Parse header. */ |
| 340 | ptr = asn_parse_header (ptr, &len, &type); |
| 341 | |
| 342 | if (debug_smux) |
| 343 | { |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 344 | zlog_debug ("SMUX var parse: type %d len %zd", type, len); |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 345 | zlog_debug ("SMUX var parse: type must be %d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 346 | (ASN_SEQUENCE | ASN_CONSTRUCTOR)); |
| 347 | } |
| 348 | |
| 349 | /* Parse var option. */ |
| 350 | *objid_len = MAX_OID_LEN; |
| 351 | ptr = snmp_parse_var_op(ptr, objid, objid_len, &val_type, |
| 352 | &val_len, &val, &len); |
| 353 | |
| 354 | if (var_val_len) |
| 355 | *var_val_len = val_len; |
| 356 | |
| 357 | if (var_value) |
| 358 | *var_value = (void*) val; |
| 359 | |
| 360 | if (var_val_type) |
| 361 | *var_val_type = val_type; |
| 362 | |
| 363 | /* Requested object id length is objid_len. */ |
| 364 | if (debug_smux) |
| 365 | smux_oid_dump ("Request OID", objid, *objid_len); |
| 366 | |
| 367 | if (debug_smux) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 368 | zlog_debug ("SMUX val_type: %d", val_type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 369 | |
| 370 | /* Check request value type. */ |
| 371 | if (debug_smux) |
| 372 | switch (val_type) |
| 373 | { |
| 374 | case ASN_NULL: |
| 375 | /* In case of SMUX_GET or SMUX_GET_NEXT val_type is set to |
| 376 | ASN_NULL. */ |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 377 | zlog_debug ("ASN_NULL"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 378 | break; |
| 379 | |
| 380 | case ASN_INTEGER: |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 381 | zlog_debug ("ASN_INTEGER"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 382 | break; |
| 383 | case ASN_COUNTER: |
| 384 | case ASN_GAUGE: |
| 385 | case ASN_TIMETICKS: |
| 386 | case ASN_UINTEGER: |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 387 | zlog_debug ("ASN_COUNTER"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 388 | break; |
| 389 | case ASN_COUNTER64: |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 390 | zlog_debug ("ASN_COUNTER64"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 391 | break; |
| 392 | case ASN_IPADDRESS: |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 393 | zlog_debug ("ASN_IPADDRESS"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 394 | break; |
| 395 | case ASN_OCTET_STR: |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 396 | zlog_debug ("ASN_OCTET_STR"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 397 | break; |
| 398 | case ASN_OPAQUE: |
| 399 | case ASN_NSAP: |
| 400 | case ASN_OBJECT_ID: |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 401 | zlog_debug ("ASN_OPAQUE"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 402 | break; |
| 403 | case SNMP_NOSUCHOBJECT: |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 404 | zlog_debug ("SNMP_NOSUCHOBJECT"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 405 | break; |
| 406 | case SNMP_NOSUCHINSTANCE: |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 407 | zlog_debug ("SNMP_NOSUCHINSTANCE"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 408 | break; |
| 409 | case SNMP_ENDOFMIBVIEW: |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 410 | zlog_debug ("SNMP_ENDOFMIBVIEW"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 411 | break; |
| 412 | case ASN_BIT_STR: |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 413 | zlog_debug ("ASN_BIT_STR"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 414 | break; |
| 415 | default: |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 416 | zlog_debug ("Unknown type"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 417 | break; |
| 418 | } |
| 419 | return ptr; |
| 420 | } |
| 421 | |
| 422 | /* NOTE: all 3 functions (smux_set, smux_get & smux_getnext) are based on |
| 423 | ucd-snmp smux and as such suppose, that the peer receives in the message |
| 424 | only one variable. Fortunately, IBM seems to do the same in AIX. */ |
| 425 | |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 426 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 427 | smux_set (oid *reqid, size_t *reqid_len, |
| 428 | u_char val_type, void *val, size_t val_len, int action) |
| 429 | { |
| 430 | int j; |
| 431 | struct subtree *subtree; |
| 432 | struct variable *v; |
| 433 | int subresult; |
| 434 | oid *suffix; |
paul | 70e149e | 2004-10-31 16:29:50 +0000 | [diff] [blame] | 435 | size_t suffix_len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 436 | int result; |
| 437 | u_char *statP = NULL; |
| 438 | WriteMethod *write_method = NULL; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 439 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 440 | |
| 441 | /* Check */ |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 442 | for (ALL_LIST_ELEMENTS (treelist, node, nnode, subtree)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 443 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 444 | subresult = oid_compare_part (reqid, *reqid_len, |
| 445 | subtree->name, subtree->name_len); |
| 446 | |
| 447 | /* Subtree matched. */ |
| 448 | if (subresult == 0) |
| 449 | { |
| 450 | /* Prepare suffix. */ |
| 451 | suffix = reqid + subtree->name_len; |
| 452 | suffix_len = *reqid_len - subtree->name_len; |
| 453 | result = subresult; |
| 454 | |
| 455 | /* Check variables. */ |
| 456 | for (j = 0; j < subtree->variables_num; j++) |
| 457 | { |
| 458 | v = &subtree->variables[j]; |
| 459 | |
| 460 | /* Always check suffix */ |
| 461 | result = oid_compare_part (suffix, suffix_len, |
| 462 | v->name, v->namelen); |
| 463 | |
| 464 | /* This is exact match so result must be zero. */ |
| 465 | if (result == 0) |
| 466 | { |
| 467 | if (debug_smux) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 468 | zlog_debug ("SMUX function call index is %d", v->magic); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 469 | |
| 470 | statP = (*v->findVar) (v, suffix, &suffix_len, 1, |
| 471 | &val_len, &write_method); |
| 472 | |
| 473 | if (write_method) |
| 474 | { |
| 475 | return (*write_method)(action, val, val_type, val_len, |
| 476 | statP, suffix, suffix_len, v); |
| 477 | } |
| 478 | else |
| 479 | { |
| 480 | return SNMP_ERR_READONLY; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /* If above execution is failed or oid is small (so |
| 485 | there is no further match). */ |
| 486 | if (result < 0) |
| 487 | return SNMP_ERR_NOSUCHNAME; |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | return SNMP_ERR_NOSUCHNAME; |
| 492 | } |
| 493 | |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 494 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 495 | smux_get (oid *reqid, size_t *reqid_len, int exact, |
| 496 | u_char *val_type,void **val, size_t *val_len) |
| 497 | { |
| 498 | int j; |
| 499 | struct subtree *subtree; |
| 500 | struct variable *v; |
| 501 | int subresult; |
| 502 | oid *suffix; |
paul | 70e149e | 2004-10-31 16:29:50 +0000 | [diff] [blame] | 503 | size_t suffix_len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 504 | int result; |
| 505 | WriteMethod *write_method=NULL; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 506 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 507 | |
| 508 | /* Check */ |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 509 | for (ALL_LIST_ELEMENTS (treelist, node, nnode,subtree)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 510 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 511 | subresult = oid_compare_part (reqid, *reqid_len, |
| 512 | subtree->name, subtree->name_len); |
| 513 | |
| 514 | /* Subtree matched. */ |
| 515 | if (subresult == 0) |
| 516 | { |
| 517 | /* Prepare suffix. */ |
| 518 | suffix = reqid + subtree->name_len; |
| 519 | suffix_len = *reqid_len - subtree->name_len; |
| 520 | result = subresult; |
| 521 | |
| 522 | /* Check variables. */ |
| 523 | for (j = 0; j < subtree->variables_num; j++) |
| 524 | { |
| 525 | v = &subtree->variables[j]; |
| 526 | |
| 527 | /* Always check suffix */ |
| 528 | result = oid_compare_part (suffix, suffix_len, |
| 529 | v->name, v->namelen); |
| 530 | |
| 531 | /* This is exact match so result must be zero. */ |
| 532 | if (result == 0) |
| 533 | { |
| 534 | if (debug_smux) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 535 | zlog_debug ("SMUX function call index is %d", v->magic); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 536 | |
| 537 | *val = (*v->findVar) (v, suffix, &suffix_len, exact, |
| 538 | val_len, &write_method); |
| 539 | |
| 540 | /* There is no instance. */ |
| 541 | if (*val == NULL) |
| 542 | return SNMP_NOSUCHINSTANCE; |
| 543 | |
| 544 | /* Call is suceed. */ |
| 545 | *val_type = v->type; |
| 546 | |
| 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | /* If above execution is failed or oid is small (so |
| 551 | there is no further match). */ |
| 552 | if (result < 0) |
| 553 | return SNMP_ERR_NOSUCHNAME; |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | return SNMP_ERR_NOSUCHNAME; |
| 558 | } |
| 559 | |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 560 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 561 | smux_getnext (oid *reqid, size_t *reqid_len, int exact, |
| 562 | u_char *val_type,void **val, size_t *val_len) |
| 563 | { |
| 564 | int j; |
| 565 | oid save[MAX_OID_LEN]; |
| 566 | int savelen = 0; |
| 567 | struct subtree *subtree; |
| 568 | struct variable *v; |
| 569 | int subresult; |
| 570 | oid *suffix; |
paul | 70e149e | 2004-10-31 16:29:50 +0000 | [diff] [blame] | 571 | size_t suffix_len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 572 | int result; |
| 573 | WriteMethod *write_method=NULL; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 574 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 575 | |
| 576 | |
| 577 | /* Save incoming request. */ |
| 578 | oid_copy (save, reqid, *reqid_len); |
| 579 | savelen = *reqid_len; |
| 580 | |
| 581 | /* Check */ |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 582 | for (ALL_LIST_ELEMENTS (treelist, node, nnode, subtree)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 583 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 584 | subresult = oid_compare_part (reqid, *reqid_len, |
| 585 | subtree->name, subtree->name_len); |
| 586 | |
| 587 | /* If request is in the tree. The agent has to make sure we |
| 588 | only receive requests we have registered for. */ |
| 589 | /* Unfortunately, that's not true. In fact, a SMUX subagent has to |
| 590 | behave as if it manages the whole SNMP MIB tree itself. It's the |
| 591 | duty of the master agent to collect the best answer and return it |
| 592 | to the manager. See RFC 1227 chapter 3.1.6 for the glory details |
| 593 | :-). ucd-snmp really behaves bad here as it actually might ask |
| 594 | multiple times for the same GETNEXT request as it throws away the |
| 595 | answer when it expects it in a different subtree and might come |
| 596 | back later with the very same request. --jochen */ |
| 597 | |
| 598 | if (subresult <= 0) |
| 599 | { |
| 600 | /* Prepare suffix. */ |
| 601 | suffix = reqid + subtree->name_len; |
| 602 | suffix_len = *reqid_len - subtree->name_len; |
| 603 | if (subresult < 0) |
| 604 | { |
| 605 | oid_copy(reqid, subtree->name, subtree->name_len); |
| 606 | *reqid_len = subtree->name_len; |
| 607 | } |
| 608 | for (j = 0; j < subtree->variables_num; j++) |
| 609 | { |
| 610 | result = subresult; |
| 611 | v = &subtree->variables[j]; |
| 612 | |
| 613 | /* Next then check result >= 0. */ |
| 614 | if (result == 0) |
| 615 | result = oid_compare_part (suffix, suffix_len, |
| 616 | v->name, v->namelen); |
| 617 | |
| 618 | if (result <= 0) |
| 619 | { |
| 620 | if (debug_smux) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 621 | zlog_debug ("SMUX function call index is %d", v->magic); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 622 | if(result<0) |
| 623 | { |
| 624 | oid_copy(suffix, v->name, v->namelen); |
| 625 | suffix_len = v->namelen; |
| 626 | } |
| 627 | *val = (*v->findVar) (v, suffix, &suffix_len, exact, |
| 628 | val_len, &write_method); |
| 629 | *reqid_len = suffix_len + subtree->name_len; |
| 630 | if (*val) |
| 631 | { |
| 632 | *val_type = v->type; |
| 633 | return 0; |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | memcpy (reqid, save, savelen * sizeof(oid)); |
| 640 | *reqid_len = savelen; |
| 641 | |
| 642 | return SNMP_ERR_NOSUCHNAME; |
| 643 | } |
| 644 | |
| 645 | /* GET message header. */ |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 646 | static u_char * |
| 647 | smux_parse_get_header (u_char *ptr, size_t *len, long *reqid) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 648 | { |
| 649 | u_char type; |
| 650 | long errstat; |
| 651 | long errindex; |
| 652 | |
| 653 | /* Request ID. */ |
| 654 | ptr = asn_parse_int (ptr, len, &type, reqid, sizeof (*reqid)); |
| 655 | |
| 656 | if (debug_smux) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 657 | zlog_debug ("SMUX GET reqid: %d len: %d", (int) *reqid, (int) *len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 658 | |
| 659 | /* Error status. */ |
| 660 | ptr = asn_parse_int (ptr, len, &type, &errstat, sizeof (errstat)); |
| 661 | |
| 662 | if (debug_smux) |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 663 | zlog_debug ("SMUX GET errstat %ld len: %zd", errstat, *len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 664 | |
| 665 | /* Error index. */ |
| 666 | ptr = asn_parse_int (ptr, len, &type, &errindex, sizeof (errindex)); |
| 667 | |
| 668 | if (debug_smux) |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 669 | zlog_debug ("SMUX GET errindex %ld len: %zd", errindex, *len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 670 | |
| 671 | return ptr; |
| 672 | } |
| 673 | |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 674 | static void |
| 675 | smux_parse_set (u_char *ptr, size_t len, int action) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 676 | { |
| 677 | long reqid; |
| 678 | oid oid[MAX_OID_LEN]; |
| 679 | size_t oid_len; |
| 680 | u_char val_type; |
| 681 | void *val; |
| 682 | size_t val_len; |
| 683 | int ret; |
| 684 | |
| 685 | if (debug_smux) |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 686 | zlog_debug ("SMUX SET(%s) message parse: len %zd", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 687 | (RESERVE1 == action) ? "RESERVE1" : ((FREE == action) ? "FREE" : "COMMIT"), |
| 688 | len); |
| 689 | |
| 690 | /* Parse SET message header. */ |
| 691 | ptr = smux_parse_get_header (ptr, &len, &reqid); |
| 692 | |
| 693 | /* Parse SET message object ID. */ |
| 694 | ptr = smux_var (ptr, len, oid, &oid_len, &val_len, &val_type, &val); |
| 695 | |
| 696 | ret = smux_set (oid, &oid_len, val_type, val, val_len, action); |
| 697 | if (debug_smux) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 698 | zlog_debug ("SMUX SET ret %d", ret); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 699 | |
| 700 | /* Return result. */ |
| 701 | if (RESERVE1 == action) |
| 702 | smux_getresp_send (oid, oid_len, reqid, ret, 3, ASN_NULL, NULL, 0); |
| 703 | } |
| 704 | |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 705 | static void |
| 706 | smux_parse_get (u_char *ptr, size_t len, int exact) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 707 | { |
| 708 | long reqid; |
| 709 | oid oid[MAX_OID_LEN]; |
| 710 | size_t oid_len; |
| 711 | u_char val_type; |
| 712 | void *val; |
| 713 | size_t val_len; |
| 714 | int ret; |
| 715 | |
| 716 | if (debug_smux) |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 717 | zlog_debug ("SMUX GET message parse: len %zd", len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 718 | |
| 719 | /* Parse GET message header. */ |
| 720 | ptr = smux_parse_get_header (ptr, &len, &reqid); |
| 721 | |
| 722 | /* Parse GET message object ID. We needn't the value come */ |
| 723 | ptr = smux_var (ptr, len, oid, &oid_len, NULL, NULL, NULL); |
| 724 | |
| 725 | /* Traditional getstatptr. */ |
| 726 | if (exact) |
| 727 | ret = smux_get (oid, &oid_len, exact, &val_type, &val, &val_len); |
| 728 | else |
| 729 | ret = smux_getnext (oid, &oid_len, exact, &val_type, &val, &val_len); |
| 730 | |
| 731 | /* Return result. */ |
| 732 | if (ret == 0) |
| 733 | smux_getresp_send (oid, oid_len, reqid, 0, 0, val_type, val, val_len); |
| 734 | else |
| 735 | smux_getresp_send (oid, oid_len, reqid, ret, 3, ASN_NULL, NULL, 0); |
| 736 | } |
| 737 | |
| 738 | /* Parse SMUX_CLOSE message. */ |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 739 | static void |
| 740 | smux_parse_close (u_char *ptr, int len) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 741 | { |
| 742 | long reason = 0; |
| 743 | |
| 744 | while (len--) |
| 745 | { |
| 746 | reason = (reason << 8) | (long) *ptr; |
| 747 | ptr++; |
| 748 | } |
| 749 | zlog_info ("SMUX_CLOSE with reason: %ld", reason); |
| 750 | } |
| 751 | |
| 752 | /* SMUX_RRSP message. */ |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 753 | static void |
| 754 | smux_parse_rrsp (u_char *ptr, size_t len) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 755 | { |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 756 | u_char val; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 757 | long errstat; |
| 758 | |
| 759 | ptr = asn_parse_int (ptr, &len, &val, &errstat, sizeof (errstat)); |
| 760 | |
| 761 | if (debug_smux) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 762 | zlog_debug ("SMUX_RRSP value: %d errstat: %ld", val, errstat); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | /* Parse SMUX message. */ |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 766 | static int |
| 767 | smux_parse (u_char *ptr, size_t len) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 768 | { |
| 769 | /* This buffer we'll use for SOUT message. We could allocate it with |
| 770 | malloc and save only static pointer/lenght, but IMHO static |
| 771 | buffer is a faster solusion. */ |
| 772 | static u_char sout_save_buff[SMUXMAXPKTSIZE]; |
| 773 | static int sout_save_len = 0; |
| 774 | |
| 775 | int len_income = len; /* see note below: YYY */ |
| 776 | u_char type; |
| 777 | u_char rollback; |
| 778 | |
| 779 | rollback = ptr[2]; /* important only for SMUX_SOUT */ |
| 780 | |
| 781 | process_rest: /* see note below: YYY */ |
| 782 | |
| 783 | /* Parse SMUX message type and subsequent length. */ |
| 784 | ptr = asn_parse_header (ptr, &len, &type); |
| 785 | |
| 786 | if (debug_smux) |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 787 | zlog_debug ("SMUX message received type: %d rest len: %zd", type, len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 788 | |
| 789 | switch (type) |
| 790 | { |
| 791 | case SMUX_OPEN: |
| 792 | /* Open must be not send from SNMP agent. */ |
| 793 | zlog_warn ("SMUX_OPEN received: resetting connection."); |
| 794 | return -1; |
| 795 | break; |
| 796 | case SMUX_RREQ: |
| 797 | /* SMUX_RREQ message is invalid for us. */ |
| 798 | zlog_warn ("SMUX_RREQ received: resetting connection."); |
| 799 | return -1; |
| 800 | break; |
| 801 | case SMUX_SOUT: |
| 802 | /* SMUX_SOUT message is now valied for us. */ |
| 803 | if (debug_smux) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 804 | zlog_debug ("SMUX_SOUT(%s)", rollback ? "rollback" : "commit"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 805 | |
| 806 | if (sout_save_len > 0) |
| 807 | { |
| 808 | smux_parse_set (sout_save_buff, sout_save_len, rollback ? FREE : COMMIT); |
| 809 | sout_save_len = 0; |
| 810 | } |
| 811 | else |
| 812 | zlog_warn ("SMUX_SOUT sout_save_len=%d - invalid", (int) sout_save_len); |
| 813 | |
| 814 | if (len_income > 3) |
| 815 | { |
| 816 | /* YYY: this strange code has to solve the "slow peer" |
| 817 | problem: When agent sends SMUX_SOUT message it doesn't |
| 818 | wait any responce and may send some next message to |
| 819 | subagent. Then the peer in 'smux_read()' will recieve |
| 820 | from socket the 'concatenated' buffer, contaning both |
| 821 | SMUX_SOUT message and the next one |
| 822 | (SMUX_GET/SMUX_GETNEXT/SMUX_GET). So we should check: if |
| 823 | the buffer is longer than 3 ( length of SMUX_SOUT ), we |
| 824 | must process the rest of it. This effect may be observed |
| 825 | if 'debug_smux' is set to '1' */ |
| 826 | ptr++; |
| 827 | len = len_income - 3; |
| 828 | goto process_rest; |
| 829 | } |
| 830 | break; |
| 831 | case SMUX_GETRSP: |
| 832 | /* SMUX_GETRSP message is invalid for us. */ |
| 833 | zlog_warn ("SMUX_GETRSP received: resetting connection."); |
| 834 | return -1; |
| 835 | break; |
| 836 | case SMUX_CLOSE: |
| 837 | /* Close SMUX connection. */ |
| 838 | if (debug_smux) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 839 | zlog_debug ("SMUX_CLOSE"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 840 | smux_parse_close (ptr, len); |
| 841 | return -1; |
| 842 | break; |
| 843 | case SMUX_RRSP: |
| 844 | /* This is response for register message. */ |
| 845 | if (debug_smux) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 846 | zlog_debug ("SMUX_RRSP"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 847 | smux_parse_rrsp (ptr, len); |
| 848 | break; |
| 849 | case SMUX_GET: |
| 850 | /* Exact request for object id. */ |
| 851 | if (debug_smux) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 852 | zlog_debug ("SMUX_GET"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 853 | smux_parse_get (ptr, len, 1); |
| 854 | break; |
| 855 | case SMUX_GETNEXT: |
| 856 | /* Next request for object id. */ |
| 857 | if (debug_smux) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 858 | zlog_debug ("SMUX_GETNEXT"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 859 | smux_parse_get (ptr, len, 0); |
| 860 | break; |
| 861 | case SMUX_SET: |
| 862 | /* SMUX_SET is supported with some limitations. */ |
| 863 | if (debug_smux) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 864 | zlog_debug ("SMUX_SET"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 865 | |
| 866 | /* save the data for future SMUX_SOUT */ |
| 867 | memcpy (sout_save_buff, ptr, len); |
| 868 | sout_save_len = len; |
| 869 | smux_parse_set (ptr, len, RESERVE1); |
| 870 | break; |
| 871 | default: |
| 872 | zlog_info ("Unknown type: %d", type); |
| 873 | break; |
| 874 | } |
| 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | /* SMUX message read function. */ |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 879 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 880 | smux_read (struct thread *t) |
| 881 | { |
| 882 | int sock; |
| 883 | int len; |
| 884 | u_char buf[SMUXMAXPKTSIZE]; |
| 885 | int ret; |
| 886 | |
| 887 | /* Clear thread. */ |
| 888 | sock = THREAD_FD (t); |
| 889 | smux_read_thread = NULL; |
| 890 | |
| 891 | if (debug_smux) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 892 | zlog_debug ("SMUX read start"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 893 | |
| 894 | /* Read message from SMUX socket. */ |
| 895 | len = recv (sock, buf, SMUXMAXPKTSIZE, 0); |
| 896 | |
| 897 | if (len < 0) |
| 898 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 899 | zlog_warn ("Can't read all SMUX packet: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 900 | close (sock); |
| 901 | smux_sock = -1; |
| 902 | smux_event (SMUX_CONNECT, 0); |
| 903 | return -1; |
| 904 | } |
| 905 | |
| 906 | if (len == 0) |
| 907 | { |
| 908 | zlog_warn ("SMUX connection closed: %d", sock); |
| 909 | close (sock); |
| 910 | smux_sock = -1; |
| 911 | smux_event (SMUX_CONNECT, 0); |
| 912 | return -1; |
| 913 | } |
| 914 | |
| 915 | if (debug_smux) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 916 | zlog_debug ("SMUX read len: %d", len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 917 | |
| 918 | /* Parse the message. */ |
| 919 | ret = smux_parse (buf, len); |
| 920 | |
| 921 | if (ret < 0) |
| 922 | { |
| 923 | close (sock); |
| 924 | smux_sock = -1; |
| 925 | smux_event (SMUX_CONNECT, 0); |
| 926 | return -1; |
| 927 | } |
| 928 | |
| 929 | /* Regiser read thread. */ |
| 930 | smux_event (SMUX_READ, sock); |
| 931 | |
| 932 | return 0; |
| 933 | } |
| 934 | |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 935 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 936 | smux_open (int sock) |
| 937 | { |
| 938 | u_char buf[BUFSIZ]; |
| 939 | u_char *ptr; |
paul | 70e149e | 2004-10-31 16:29:50 +0000 | [diff] [blame] | 940 | size_t len; |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 941 | long version; |
Stephen Hemminger | 004b123 | 2011-12-07 00:55:15 +0400 | [diff] [blame] | 942 | const char progname[] = QUAGGA_PROGNAME "-" QUAGGA_VERSION; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 943 | |
| 944 | if (debug_smux) |
| 945 | { |
| 946 | smux_oid_dump ("SMUX open oid", smux_oid, smux_oid_len); |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 947 | zlog_debug ("SMUX open progname: %s", progname); |
| 948 | zlog_debug ("SMUX open password: %s", smux_passwd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | ptr = buf; |
| 952 | len = BUFSIZ; |
| 953 | |
| 954 | /* SMUX Header. As placeholder. */ |
| 955 | ptr = asn_build_header (ptr, &len, (u_char) SMUX_OPEN, 0); |
| 956 | |
| 957 | /* SMUX Open. */ |
| 958 | version = 0; |
| 959 | ptr = asn_build_int (ptr, &len, |
| 960 | (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER), |
Andrew J. Schorr | 965b83f | 2006-09-26 15:30:43 +0000 | [diff] [blame] | 961 | &version, sizeof (version)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 962 | |
| 963 | /* SMUX connection oid. */ |
| 964 | ptr = asn_build_objid (ptr, &len, |
| 965 | (u_char) |
| 966 | (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OBJECT_ID), |
| 967 | smux_oid, smux_oid_len); |
| 968 | |
| 969 | /* SMUX connection description. */ |
| 970 | ptr = asn_build_string (ptr, &len, |
| 971 | (u_char) |
| 972 | (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OCTET_STR), |
Stephen Hemminger | 004b123 | 2011-12-07 00:55:15 +0400 | [diff] [blame] | 973 | (const u_char *) progname, strlen (progname)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 974 | |
| 975 | /* SMUX connection password. */ |
| 976 | ptr = asn_build_string (ptr, &len, |
| 977 | (u_char) |
| 978 | (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OCTET_STR), |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 979 | (u_char *)smux_passwd, strlen (smux_passwd)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 980 | |
| 981 | /* Fill in real SMUX header. We exclude ASN header size (2). */ |
| 982 | len = BUFSIZ; |
| 983 | asn_build_header (buf, &len, (u_char) SMUX_OPEN, (ptr - buf) - 2); |
| 984 | |
| 985 | return send (sock, buf, (ptr - buf), 0); |
| 986 | } |
| 987 | |
| 988 | int |
Stephen Hemminger | 65d3fbb | 2009-05-15 09:59:03 -0700 | [diff] [blame] | 989 | smux_trap (const oid *name, size_t namelen, |
| 990 | const oid *iname, size_t inamelen, |
| 991 | const struct trap_object *trapobj, size_t trapobjlen, |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 992 | unsigned int tick, u_char sptrap) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 993 | { |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 994 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 995 | u_char buf[BUFSIZ]; |
| 996 | u_char *ptr; |
paul | 70e149e | 2004-10-31 16:29:50 +0000 | [diff] [blame] | 997 | size_t len, length; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 998 | struct in_addr addr; |
| 999 | unsigned long val; |
| 1000 | u_char *h1, *h1e; |
| 1001 | |
| 1002 | ptr = buf; |
| 1003 | len = BUFSIZ; |
| 1004 | length = len; |
| 1005 | |
| 1006 | /* When SMUX connection is not established. */ |
| 1007 | if (smux_sock < 0) |
| 1008 | return 0; |
| 1009 | |
| 1010 | /* SMUX header. */ |
| 1011 | ptr = asn_build_header (ptr, &len, (u_char) SMUX_TRAP, 0); |
| 1012 | |
| 1013 | /* Sub agent enterprise oid. */ |
| 1014 | ptr = asn_build_objid (ptr, &len, |
| 1015 | (u_char) |
| 1016 | (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OBJECT_ID), |
| 1017 | smux_oid, smux_oid_len); |
| 1018 | |
| 1019 | /* IP address. */ |
| 1020 | addr.s_addr = 0; |
| 1021 | ptr = asn_build_string (ptr, &len, |
| 1022 | (u_char) |
| 1023 | (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_IPADDRESS), |
Andrew J. Schorr | 965b83f | 2006-09-26 15:30:43 +0000 | [diff] [blame] | 1024 | (u_char *)&addr, sizeof (addr)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1025 | |
| 1026 | /* Generic trap integer. */ |
| 1027 | val = SNMP_TRAP_ENTERPRISESPECIFIC; |
| 1028 | ptr = asn_build_int (ptr, &len, |
| 1029 | (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER), |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 1030 | (long *)&val, sizeof (val)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1031 | |
| 1032 | /* Specific trap integer. */ |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 1033 | val = sptrap; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1034 | ptr = asn_build_int (ptr, &len, |
| 1035 | (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER), |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 1036 | (long *)&val, sizeof (val)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1037 | |
| 1038 | /* Timeticks timestamp. */ |
| 1039 | val = 0; |
| 1040 | ptr = asn_build_unsigned_int (ptr, &len, |
| 1041 | (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_TIMETICKS), |
Andrew J. Schorr | 965b83f | 2006-09-26 15:30:43 +0000 | [diff] [blame] | 1042 | &val, sizeof (val)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1043 | |
| 1044 | /* Variables. */ |
| 1045 | h1 = ptr; |
| 1046 | ptr = asn_build_sequence (ptr, &len, |
| 1047 | (u_char) (ASN_SEQUENCE | ASN_CONSTRUCTOR), |
| 1048 | 0); |
| 1049 | |
| 1050 | |
| 1051 | /* Iteration for each objects. */ |
| 1052 | h1e = ptr; |
| 1053 | for (i = 0; i < trapobjlen; i++) |
| 1054 | { |
| 1055 | int ret; |
| 1056 | oid oid[MAX_OID_LEN]; |
| 1057 | size_t oid_len; |
| 1058 | void *val; |
| 1059 | size_t val_len; |
| 1060 | u_char val_type; |
| 1061 | |
| 1062 | /* Make OID. */ |
vincent | 5e4914c | 2005-09-29 16:34:30 +0000 | [diff] [blame] | 1063 | if (trapobj[i].namelen > 0) |
| 1064 | { |
| 1065 | oid_copy (oid, name, namelen); |
| 1066 | oid_copy (oid + namelen, trapobj[i].name, trapobj[i].namelen); |
| 1067 | oid_copy (oid + namelen + trapobj[i].namelen, iname, inamelen); |
| 1068 | oid_len = namelen + trapobj[i].namelen + inamelen; |
| 1069 | } |
| 1070 | else |
| 1071 | { |
| 1072 | oid_copy (oid, name, namelen); |
| 1073 | oid_copy (oid + namelen, trapobj[i].name, trapobj[i].namelen * (-1)); |
| 1074 | oid_len = namelen + trapobj[i].namelen * (-1) ; |
| 1075 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1076 | |
vincent | 5e4914c | 2005-09-29 16:34:30 +0000 | [diff] [blame] | 1077 | if (debug_smux) |
| 1078 | { |
| 1079 | smux_oid_dump ("Trap", name, namelen); |
| 1080 | if (trapobj[i].namelen < 0) |
| 1081 | smux_oid_dump ("Trap", |
| 1082 | trapobj[i].name, (- 1) * (trapobj[i].namelen)); |
| 1083 | else |
| 1084 | { |
| 1085 | smux_oid_dump ("Trap", trapobj[i].name, (trapobj[i].namelen)); |
| 1086 | smux_oid_dump ("Trap", iname, inamelen); |
| 1087 | } |
| 1088 | smux_oid_dump ("Trap", oid, oid_len); |
Andrew J. Schorr | eda9ba7 | 2007-04-27 18:13:15 +0000 | [diff] [blame] | 1089 | zlog_info ("BUFSIZ: %d // oid_len: %lu", BUFSIZ, (u_long)oid_len); |
vincent | 5e4914c | 2005-09-29 16:34:30 +0000 | [diff] [blame] | 1090 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1091 | |
| 1092 | ret = smux_get (oid, &oid_len, 1, &val_type, &val, &val_len); |
| 1093 | |
| 1094 | if (debug_smux) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 1095 | zlog_debug ("smux_get result %d", ret); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1096 | |
| 1097 | if (ret == 0) |
| 1098 | ptr = snmp_build_var_op (ptr, oid, &oid_len, |
| 1099 | val_type, val_len, val, &len); |
| 1100 | } |
| 1101 | |
| 1102 | /* Now variable size is known, fill in size */ |
| 1103 | asn_build_sequence(h1, &length, |
| 1104 | (u_char) (ASN_SEQUENCE | ASN_CONSTRUCTOR), |
| 1105 | ptr - h1e); |
| 1106 | |
| 1107 | /* Fill in size of whole sequence */ |
| 1108 | len = BUFSIZ; |
| 1109 | asn_build_header (buf, &len, (u_char) SMUX_TRAP, (ptr - buf) - 2); |
| 1110 | |
| 1111 | return send (smux_sock, buf, (ptr - buf), 0); |
| 1112 | } |
| 1113 | |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 1114 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1115 | smux_register (int sock) |
| 1116 | { |
| 1117 | u_char buf[BUFSIZ]; |
| 1118 | u_char *ptr; |
paul | 70e149e | 2004-10-31 16:29:50 +0000 | [diff] [blame] | 1119 | int ret; |
| 1120 | size_t len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1121 | long priority; |
| 1122 | long operation; |
| 1123 | struct subtree *subtree; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1124 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1125 | |
| 1126 | ret = 0; |
| 1127 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1128 | for (ALL_LIST_ELEMENTS (treelist, node, nnode, subtree)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1129 | { |
| 1130 | ptr = buf; |
| 1131 | len = BUFSIZ; |
| 1132 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1133 | /* SMUX RReq Header. */ |
| 1134 | ptr = asn_build_header (ptr, &len, (u_char) SMUX_RREQ, 0); |
| 1135 | |
| 1136 | /* Register MIB tree. */ |
| 1137 | ptr = asn_build_objid (ptr, &len, |
| 1138 | (u_char) |
| 1139 | (ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_OBJECT_ID), |
| 1140 | subtree->name, subtree->name_len); |
| 1141 | |
| 1142 | /* Priority. */ |
| 1143 | priority = -1; |
| 1144 | ptr = asn_build_int (ptr, &len, |
| 1145 | (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER), |
Andrew J. Schorr | 965b83f | 2006-09-26 15:30:43 +0000 | [diff] [blame] | 1146 | &priority, sizeof (priority)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1147 | |
| 1148 | /* Operation. */ |
| 1149 | operation = 2; /* Register R/W */ |
| 1150 | ptr = asn_build_int (ptr, &len, |
| 1151 | (u_char)(ASN_UNIVERSAL | ASN_PRIMITIVE | ASN_INTEGER), |
Andrew J. Schorr | 965b83f | 2006-09-26 15:30:43 +0000 | [diff] [blame] | 1152 | &operation, sizeof (operation)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1153 | |
| 1154 | if (debug_smux) |
| 1155 | { |
| 1156 | smux_oid_dump ("SMUX register oid", subtree->name, subtree->name_len); |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 1157 | zlog_debug ("SMUX register priority: %ld", priority); |
| 1158 | zlog_debug ("SMUX register operation: %ld", operation); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | len = BUFSIZ; |
| 1162 | asn_build_header (buf, &len, (u_char) SMUX_RREQ, (ptr - buf) - 2); |
| 1163 | ret = send (sock, buf, (ptr - buf), 0); |
| 1164 | if (ret < 0) |
| 1165 | return ret; |
| 1166 | } |
| 1167 | return ret; |
| 1168 | } |
| 1169 | |
| 1170 | /* Try to connect to SNMP agent. */ |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 1171 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1172 | smux_connect (struct thread *t) |
| 1173 | { |
| 1174 | int ret; |
| 1175 | |
| 1176 | if (debug_smux) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 1177 | zlog_debug ("SMUX connect try %d", fail + 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1178 | |
| 1179 | /* Clear thread poner of myself. */ |
| 1180 | smux_connect_thread = NULL; |
| 1181 | |
| 1182 | /* Make socket. Try to connect. */ |
| 1183 | smux_sock = smux_socket (); |
| 1184 | if (smux_sock < 0) |
| 1185 | { |
| 1186 | if (++fail < SMUX_MAX_FAILURE) |
| 1187 | smux_event (SMUX_CONNECT, 0); |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
| 1191 | /* Send OPEN PDU. */ |
| 1192 | ret = smux_open (smux_sock); |
| 1193 | if (ret < 0) |
| 1194 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 1195 | zlog_warn ("SMUX open message send failed: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1196 | close (smux_sock); |
| 1197 | smux_sock = -1; |
| 1198 | if (++fail < SMUX_MAX_FAILURE) |
| 1199 | smux_event (SMUX_CONNECT, 0); |
| 1200 | return -1; |
| 1201 | } |
| 1202 | |
| 1203 | /* Send any outstanding register PDUs. */ |
| 1204 | ret = smux_register (smux_sock); |
| 1205 | if (ret < 0) |
| 1206 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 1207 | zlog_warn ("SMUX register message send failed: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1208 | close (smux_sock); |
| 1209 | smux_sock = -1; |
| 1210 | if (++fail < SMUX_MAX_FAILURE) |
| 1211 | smux_event (SMUX_CONNECT, 0); |
| 1212 | return -1; |
| 1213 | } |
| 1214 | |
| 1215 | /* Everything goes fine. */ |
| 1216 | smux_event (SMUX_READ, smux_sock); |
| 1217 | |
| 1218 | return 0; |
| 1219 | } |
| 1220 | |
| 1221 | /* Clear all SMUX related resources. */ |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 1222 | static void |
Stephen Hemminger | 65d3fbb | 2009-05-15 09:59:03 -0700 | [diff] [blame] | 1223 | smux_stop (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1224 | { |
| 1225 | if (smux_read_thread) |
Paul Jakma | a56ef88 | 2007-10-22 15:53:17 +0000 | [diff] [blame] | 1226 | { |
| 1227 | thread_cancel (smux_read_thread); |
| 1228 | smux_read_thread = NULL; |
| 1229 | } |
| 1230 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1231 | if (smux_connect_thread) |
Paul Jakma | a56ef88 | 2007-10-22 15:53:17 +0000 | [diff] [blame] | 1232 | { |
| 1233 | thread_cancel (smux_connect_thread); |
| 1234 | smux_connect_thread = NULL; |
| 1235 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1236 | |
| 1237 | if (smux_sock >= 0) |
| 1238 | { |
| 1239 | close (smux_sock); |
| 1240 | smux_sock = -1; |
| 1241 | } |
| 1242 | } |
| 1243 | |
paul | dd488a7 | 2003-06-19 01:21:07 +0000 | [diff] [blame] | 1244 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1245 | |
| 1246 | void |
| 1247 | smux_event (enum smux_event event, int sock) |
| 1248 | { |
| 1249 | switch (event) |
| 1250 | { |
| 1251 | case SMUX_SCHEDULE: |
| 1252 | smux_connect_thread = thread_add_event (master, smux_connect, NULL, 0); |
| 1253 | break; |
| 1254 | case SMUX_CONNECT: |
| 1255 | smux_connect_thread = thread_add_timer (master, smux_connect, NULL, 10); |
| 1256 | break; |
| 1257 | case SMUX_READ: |
| 1258 | smux_read_thread = thread_add_read (master, smux_read, NULL, sock); |
| 1259 | break; |
| 1260 | default: |
| 1261 | break; |
| 1262 | } |
| 1263 | } |
| 1264 | |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 1265 | static int |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 1266 | smux_str2oid (const char *str, oid *oid, size_t *oid_len) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1267 | { |
| 1268 | int len; |
| 1269 | int val; |
| 1270 | |
| 1271 | len = 0; |
| 1272 | val = 0; |
| 1273 | *oid_len = 0; |
| 1274 | |
| 1275 | if (*str == '.') |
| 1276 | str++; |
| 1277 | if (*str == '\0') |
| 1278 | return 0; |
| 1279 | |
| 1280 | while (1) |
| 1281 | { |
| 1282 | if (! isdigit (*str)) |
| 1283 | return -1; |
| 1284 | |
| 1285 | while (isdigit (*str)) |
| 1286 | { |
| 1287 | val *= 10; |
| 1288 | val += (*str - '0'); |
| 1289 | str++; |
| 1290 | } |
| 1291 | |
| 1292 | if (*str == '\0') |
| 1293 | break; |
| 1294 | if (*str != '.') |
| 1295 | return -1; |
| 1296 | |
| 1297 | oid[len++] = val; |
| 1298 | val = 0; |
| 1299 | str++; |
| 1300 | } |
| 1301 | |
| 1302 | oid[len++] = val; |
| 1303 | *oid_len = len; |
| 1304 | |
| 1305 | return 0; |
| 1306 | } |
| 1307 | |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 1308 | static oid * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1309 | smux_oid_dup (oid *objid, size_t objid_len) |
| 1310 | { |
| 1311 | oid *new; |
| 1312 | |
| 1313 | new = XMALLOC (MTYPE_TMP, sizeof (oid) * objid_len); |
| 1314 | oid_copy (new, objid, objid_len); |
| 1315 | |
| 1316 | return new; |
| 1317 | } |
| 1318 | |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 1319 | static int |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 1320 | smux_peer_oid (struct vty *vty, const char *oid_str, const char *passwd_str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1321 | { |
| 1322 | int ret; |
| 1323 | oid oid[MAX_OID_LEN]; |
| 1324 | size_t oid_len; |
| 1325 | |
| 1326 | ret = smux_str2oid (oid_str, oid, &oid_len); |
| 1327 | if (ret != 0) |
| 1328 | { |
| 1329 | vty_out (vty, "object ID malformed%s", VTY_NEWLINE); |
| 1330 | return CMD_WARNING; |
| 1331 | } |
| 1332 | |
hasso | c75105a | 2004-10-13 10:33:26 +0000 | [diff] [blame] | 1333 | if (smux_oid) |
| 1334 | { |
| 1335 | free (smux_oid); |
| 1336 | smux_oid = NULL; |
| 1337 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1338 | |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 1339 | /* careful, smux_passwd might point to string constant */ |
hasso | c75105a | 2004-10-13 10:33:26 +0000 | [diff] [blame] | 1340 | if (smux_passwd) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1341 | { |
| 1342 | free (smux_passwd); |
| 1343 | smux_passwd = NULL; |
| 1344 | } |
| 1345 | |
| 1346 | smux_oid = smux_oid_dup (oid, oid_len); |
| 1347 | smux_oid_len = oid_len; |
| 1348 | |
| 1349 | if (passwd_str) |
| 1350 | smux_passwd = strdup (passwd_str); |
hasso | c75105a | 2004-10-13 10:33:26 +0000 | [diff] [blame] | 1351 | else |
| 1352 | smux_passwd = strdup (""); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1353 | |
hasso | c75105a | 2004-10-13 10:33:26 +0000 | [diff] [blame] | 1354 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | int |
| 1358 | smux_header_generic (struct variable *v, oid *name, size_t *length, int exact, |
| 1359 | size_t *var_len, WriteMethod **write_method) |
| 1360 | { |
| 1361 | oid fulloid[MAX_OID_LEN]; |
| 1362 | int ret; |
| 1363 | |
| 1364 | oid_copy (fulloid, v->name, v->namelen); |
| 1365 | fulloid[v->namelen] = 0; |
| 1366 | /* Check against full instance. */ |
| 1367 | ret = oid_compare (name, *length, fulloid, v->namelen + 1); |
| 1368 | |
| 1369 | /* Check single instance. */ |
| 1370 | if ((exact && (ret != 0)) || (!exact && (ret >= 0))) |
| 1371 | return MATCH_FAILED; |
| 1372 | |
| 1373 | /* In case of getnext, fill in full instance. */ |
| 1374 | memcpy (name, fulloid, (v->namelen + 1) * sizeof (oid)); |
| 1375 | *length = v->namelen + 1; |
| 1376 | |
| 1377 | *write_method = 0; |
| 1378 | *var_len = sizeof(long); /* default to 'long' results */ |
| 1379 | |
| 1380 | return MATCH_SUCCEEDED; |
| 1381 | } |
| 1382 | |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 1383 | static int |
Stephen Hemminger | 65d3fbb | 2009-05-15 09:59:03 -0700 | [diff] [blame] | 1384 | smux_peer_default (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1385 | { |
hasso | c75105a | 2004-10-13 10:33:26 +0000 | [diff] [blame] | 1386 | if (smux_oid) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1387 | { |
| 1388 | free (smux_oid); |
hasso | c75105a | 2004-10-13 10:33:26 +0000 | [diff] [blame] | 1389 | smux_oid = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1390 | } |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 1391 | |
| 1392 | /* careful, smux_passwd might be pointing at string constant */ |
hasso | c75105a | 2004-10-13 10:33:26 +0000 | [diff] [blame] | 1393 | if (smux_passwd) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1394 | { |
| 1395 | free (smux_passwd); |
hasso | c75105a | 2004-10-13 10:33:26 +0000 | [diff] [blame] | 1396 | smux_passwd = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1397 | } |
hasso | c75105a | 2004-10-13 10:33:26 +0000 | [diff] [blame] | 1398 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1399 | return CMD_SUCCESS; |
| 1400 | } |
| 1401 | |
| 1402 | DEFUN (smux_peer, |
| 1403 | smux_peer_cmd, |
| 1404 | "smux peer OID", |
| 1405 | "SNMP MUX protocol settings\n" |
| 1406 | "SNMP MUX peer settings\n" |
| 1407 | "Object ID used in SMUX peering\n") |
| 1408 | { |
hasso | c75105a | 2004-10-13 10:33:26 +0000 | [diff] [blame] | 1409 | if (smux_peer_oid (vty, argv[0], NULL) == 0) |
| 1410 | { |
| 1411 | smux_start(); |
| 1412 | return CMD_SUCCESS; |
| 1413 | } |
| 1414 | else |
| 1415 | return CMD_WARNING; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | DEFUN (smux_peer_password, |
| 1419 | smux_peer_password_cmd, |
| 1420 | "smux peer OID PASSWORD", |
| 1421 | "SNMP MUX protocol settings\n" |
| 1422 | "SNMP MUX peer settings\n" |
| 1423 | "SMUX peering object ID\n" |
| 1424 | "SMUX peering password\n") |
| 1425 | { |
hasso | c75105a | 2004-10-13 10:33:26 +0000 | [diff] [blame] | 1426 | if (smux_peer_oid (vty, argv[0], argv[1]) == 0) |
| 1427 | { |
| 1428 | smux_start(); |
| 1429 | return CMD_SUCCESS; |
| 1430 | } |
| 1431 | else |
| 1432 | return CMD_WARNING; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | DEFUN (no_smux_peer, |
| 1436 | no_smux_peer_cmd, |
hasso | c75105a | 2004-10-13 10:33:26 +0000 | [diff] [blame] | 1437 | "no smux peer", |
| 1438 | NO_STR |
| 1439 | "SNMP MUX protocol settings\n" |
| 1440 | "SNMP MUX peer settings\n") |
| 1441 | { |
| 1442 | smux_stop(); |
| 1443 | return smux_peer_default (); |
| 1444 | } |
| 1445 | |
| 1446 | ALIAS (no_smux_peer, |
| 1447 | no_smux_peer_oid_cmd, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1448 | "no smux peer OID", |
| 1449 | NO_STR |
| 1450 | "SNMP MUX protocol settings\n" |
| 1451 | "SNMP MUX peer settings\n" |
hasso | c75105a | 2004-10-13 10:33:26 +0000 | [diff] [blame] | 1452 | "SMUX peering object ID\n") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1453 | |
hasso | c75105a | 2004-10-13 10:33:26 +0000 | [diff] [blame] | 1454 | ALIAS (no_smux_peer, |
| 1455 | no_smux_peer_oid_password_cmd, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1456 | "no smux peer OID PASSWORD", |
| 1457 | NO_STR |
| 1458 | "SNMP MUX protocol settings\n" |
| 1459 | "SNMP MUX peer settings\n" |
| 1460 | "SMUX peering object ID\n" |
| 1461 | "SMUX peering password\n") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1462 | |
Chris Caputo | 0be8dfb | 2009-06-02 18:40:07 +0100 | [diff] [blame] | 1463 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1464 | config_write_smux (struct vty *vty) |
| 1465 | { |
| 1466 | int first = 1; |
paul | 9035efa | 2004-10-10 11:56:56 +0000 | [diff] [blame] | 1467 | unsigned int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1468 | |
hasso | c75105a | 2004-10-13 10:33:26 +0000 | [diff] [blame] | 1469 | if (smux_oid) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1470 | { |
| 1471 | vty_out (vty, "smux peer "); |
| 1472 | for (i = 0; i < smux_oid_len; i++) |
| 1473 | { |
| 1474 | vty_out (vty, "%s%d", first ? "" : ".", (int) smux_oid[i]); |
| 1475 | first = 0; |
| 1476 | } |
| 1477 | vty_out (vty, " %s%s", smux_passwd, VTY_NEWLINE); |
| 1478 | } |
| 1479 | return 0; |
| 1480 | } |
| 1481 | |
| 1482 | /* Register subtree to smux master tree. */ |
| 1483 | void |
paul | c9eca01 | 2004-10-11 11:28:44 +0000 | [diff] [blame] | 1484 | smux_register_mib (const char *descr, struct variable *var, |
| 1485 | size_t width, int num, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1486 | oid name[], size_t namelen) |
| 1487 | { |
| 1488 | struct subtree *tree; |
| 1489 | |
| 1490 | tree = (struct subtree *)malloc(sizeof(struct subtree)); |
| 1491 | oid_copy (tree->name, name, namelen); |
| 1492 | tree->name_len = namelen; |
| 1493 | tree->variables = var; |
| 1494 | tree->variables_num = num; |
| 1495 | tree->variables_width = width; |
| 1496 | tree->registered = 0; |
| 1497 | listnode_add_sort(treelist, tree); |
| 1498 | } |
| 1499 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1500 | /* Compare function to keep treelist sorted */ |
| 1501 | static int |
| 1502 | smux_tree_cmp(struct subtree *tree1, struct subtree *tree2) |
| 1503 | { |
| 1504 | return oid_compare(tree1->name, tree1->name_len, |
| 1505 | tree2->name, tree2->name_len); |
| 1506 | } |
| 1507 | |
| 1508 | /* Initialize some values then schedule first SMUX connection. */ |
| 1509 | void |
hasso | c75105a | 2004-10-13 10:33:26 +0000 | [diff] [blame] | 1510 | smux_init (struct thread_master *tm) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1511 | { |
paul | dd488a7 | 2003-06-19 01:21:07 +0000 | [diff] [blame] | 1512 | /* copy callers thread master */ |
| 1513 | master = tm; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1514 | |
| 1515 | /* Make MIB tree. */ |
| 1516 | treelist = list_new(); |
| 1517 | treelist->cmp = (int (*)(void *, void *))smux_tree_cmp; |
| 1518 | |
| 1519 | /* Install commands. */ |
| 1520 | install_node (&smux_node, config_write_smux); |
| 1521 | |
| 1522 | install_element (CONFIG_NODE, &smux_peer_cmd); |
| 1523 | install_element (CONFIG_NODE, &smux_peer_password_cmd); |
| 1524 | install_element (CONFIG_NODE, &no_smux_peer_cmd); |
hasso | c75105a | 2004-10-13 10:33:26 +0000 | [diff] [blame] | 1525 | install_element (CONFIG_NODE, &no_smux_peer_oid_cmd); |
| 1526 | install_element (CONFIG_NODE, &no_smux_peer_oid_password_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | void |
| 1530 | smux_start(void) |
| 1531 | { |
Paul Jakma | a56ef88 | 2007-10-22 15:53:17 +0000 | [diff] [blame] | 1532 | /* Close any existing connections. */ |
| 1533 | smux_stop(); |
| 1534 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1535 | /* Schedule first connection. */ |
| 1536 | smux_event (SMUX_SCHEDULE, 0); |
| 1537 | } |
| 1538 | #endif /* HAVE_SNMP */ |