smux.c 36 KB

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