Lines Matching full:pdu

199 snmp_parse_pdus_hdr(struct asn_buf *b, struct snmp_pdu *pdu, asn_len_t *lenp)  in snmp_parse_pdus_hdr()  argument
201 if (pdu->type == SNMP_PDU_TRAP) { in snmp_parse_pdus_hdr()
202 if (asn_get_objid(b, &pdu->enterprise) != ASN_ERR_OK) { in snmp_parse_pdus_hdr()
206 if (asn_get_ipaddress(b, pdu->agent_addr) != ASN_ERR_OK) { in snmp_parse_pdus_hdr()
210 if (asn_get_integer(b, &pdu->generic_trap) != ASN_ERR_OK) { in snmp_parse_pdus_hdr()
214 if (asn_get_integer(b, &pdu->specific_trap) != ASN_ERR_OK) { in snmp_parse_pdus_hdr()
218 if (asn_get_timeticks(b, &pdu->time_stamp) != ASN_ERR_OK) { in snmp_parse_pdus_hdr()
223 if (asn_get_integer(b, &pdu->request_id) != ASN_ERR_OK) { in snmp_parse_pdus_hdr()
227 if (asn_get_integer(b, &pdu->error_status) != ASN_ERR_OK) { in snmp_parse_pdus_hdr()
231 if (asn_get_integer(b, &pdu->error_index) != ASN_ERR_OK) { in snmp_parse_pdus_hdr()
246 parse_pdus(struct asn_buf *b, struct snmp_pdu *pdu, int32_t *ip) in parse_pdus() argument
252 err = snmp_parse_pdus_hdr(b, pdu, &len); in parse_pdus()
258 v = pdu->bindings; in parse_pdus()
261 if (pdu->nbindings == SNMP_MAX_BINDINGS) { in parse_pdus()
262 snmp_error("too many bindings (> %u) in PDU", in parse_pdus()
271 *ip = pdu->nbindings + 1; in parse_pdus()
273 pdu->nbindings++; in parse_pdus()
284 parse_secparams(struct asn_buf *b, struct snmp_pdu *pdu) in parse_secparams() argument
307 if (asn_get_octetstring(&tb, (u_char *)&pdu->engine.engine_id, in parse_secparams()
312 pdu->engine.engine_len = octs_len; in parse_secparams()
314 if (asn_get_integer(&tb, &pdu->engine.engine_boots) != ASN_ERR_OK) { in parse_secparams()
319 if (asn_get_integer(&tb, &pdu->engine.engine_time) != ASN_ERR_OK) { in parse_secparams()
325 if (asn_get_octetstring(&tb, (u_char *)&pdu->user.sec_name, &octs_len) in parse_secparams()
330 pdu->user.sec_name[octs_len] = '\0'; in parse_secparams()
332 octs_len = sizeof(pdu->msg_digest); in parse_secparams()
333 if (asn_get_octetstring(&tb, (u_char *)&pdu->msg_digest, &octs_len) != in parse_secparams()
334 ASN_ERR_OK || ((pdu->flags & SNMP_MSG_AUTH_FLAG) != 0 && in parse_secparams()
335 octs_len != sizeof(pdu->msg_digest))) { in parse_secparams()
340 octs_len = sizeof(pdu->msg_salt); in parse_secparams()
341 if (asn_get_octetstring(&tb, (u_char *)&pdu->msg_salt, &octs_len) != in parse_secparams()
342 ASN_ERR_OK ||((pdu->flags & SNMP_MSG_PRIV_FLAG) != 0 && in parse_secparams()
343 octs_len != sizeof(pdu->msg_salt))) { in parse_secparams()
348 if ((pdu->flags & SNMP_MSG_AUTH_FLAG) != 0) { in parse_secparams()
349 pdu->digest_ptr = b->asn_ptr - SNMP_USM_AUTH_SIZE; in parse_secparams()
350 pdu->digest_ptr -= octs_len + ASN_MAXLENLEN; in parse_secparams()
357 pdu_encode_secparams(struct asn_buf *b, struct snmp_pdu *pdu) in pdu_encode_secparams() argument
372 if (asn_put_octetstring(&tb, (u_char *)pdu->engine.engine_id, in pdu_encode_secparams()
373 pdu->engine.engine_len) != ASN_ERR_OK) in pdu_encode_secparams()
376 if (asn_put_integer(&tb, pdu->engine.engine_boots) != ASN_ERR_OK) in pdu_encode_secparams()
379 if (asn_put_integer(&tb, pdu->engine.engine_time) != ASN_ERR_OK) in pdu_encode_secparams()
382 if (asn_put_octetstring(&tb, (u_char *)pdu->user.sec_name, in pdu_encode_secparams()
383 strlen(pdu->user.sec_name)) != ASN_ERR_OK) in pdu_encode_secparams()
386 if ((pdu->flags & SNMP_MSG_AUTH_FLAG) != 0) { in pdu_encode_secparams()
388 if (asn_put_octetstring(&tb, (u_char *)pdu->msg_digest, in pdu_encode_secparams()
389 sizeof(pdu->msg_digest)) != ASN_ERR_OK) in pdu_encode_secparams()
392 if (asn_put_octetstring(&tb, (u_char *)pdu->msg_digest, 0) in pdu_encode_secparams()
397 if ((pdu->flags & SNMP_MSG_PRIV_FLAG) != 0) { in pdu_encode_secparams()
398 if (asn_put_octetstring(&tb, (u_char *)pdu->msg_salt, in pdu_encode_secparams()
399 sizeof(pdu->msg_salt)) != ASN_ERR_OK) in pdu_encode_secparams()
402 if (asn_put_octetstring(&tb, (u_char *)pdu->msg_salt, 0) in pdu_encode_secparams()
410 if ((pdu->flags & SNMP_MSG_AUTH_FLAG) != 0) in pdu_encode_secparams()
411 pdu->digest_ptr = b->asn_ptr + auth_off - moved; in pdu_encode_secparams()
415 pdu->digest_ptr += ASN_MAXLENLEN; in pdu_encode_secparams()
417 if ((pdu->flags & SNMP_MSG_PRIV_FLAG) != 0 && asn_put_temp_header(b, in pdu_encode_secparams()
418 ASN_TYPE_OCTETSTRING, &pdu->encrypted_ptr) != ASN_ERR_OK) in pdu_encode_secparams()
425 * Decode the PDU except for the variable bindings itself.
431 snmp_pdu_decode(struct asn_buf *b, struct snmp_pdu *pdu, int32_t *ip) in snmp_pdu_decode() argument
435 if ((code = snmp_pdu_decode_header(b, pdu)) != SNMP_CODE_OK) in snmp_pdu_decode()
438 if (pdu->version == SNMP_V3) { in snmp_pdu_decode()
439 if (pdu->security_model != SNMP_SECMODEL_USM) in snmp_pdu_decode()
441 if ((code = snmp_pdu_decode_secmode(b, pdu)) != SNMP_CODE_OK) in snmp_pdu_decode()
445 code = snmp_pdu_decode_scoped(b, pdu, ip); in snmp_pdu_decode()
449 snmp_pdu_free(pdu); in snmp_pdu_decode()
453 if (pdu->version == SNMP_Verr) in snmp_pdu_decode()
464 snmp_pdu_decode_header(struct asn_buf *b, struct snmp_pdu *pdu) in snmp_pdu_decode_header() argument
470 pdu->outer_ptr = b->asn_ptr; in snmp_pdu_decode_header()
471 pdu->outer_len = b->asn_len; in snmp_pdu_decode_header()
474 snmp_error("cannot decode pdu header"); in snmp_pdu_decode_header()
492 pdu->version = SNMP_V1; in snmp_pdu_decode_header()
494 pdu->version = SNMP_V2c; in snmp_pdu_decode_header()
496 pdu->version = SNMP_V3; in snmp_pdu_decode_header()
498 pdu->version = SNMP_Verr; in snmp_pdu_decode_header()
503 if (pdu->version == SNMP_V3) { in snmp_pdu_decode_header()
505 snmp_error("cannot decode pdu global data header"); in snmp_pdu_decode_header()
509 if (asn_get_integer(b, &pdu->identifier) != ASN_ERR_OK) { in snmp_pdu_decode_header()
514 if (asn_get_integer(b, &pdu->engine.max_msg_size) in snmp_pdu_decode_header()
521 if (asn_get_octetstring(b, (u_char *)&pdu->flags, in snmp_pdu_decode_header()
527 if (asn_get_integer(b, &pdu->security_model) != ASN_ERR_OK) { in snmp_pdu_decode_header()
532 if (pdu->security_model != SNMP_SECMODEL_USM) in snmp_pdu_decode_header()
535 if (parse_secparams(b, pdu) != ASN_ERR_OK) in snmp_pdu_decode_header()
539 if (asn_get_octetstring(b, (u_char *)pdu->community, in snmp_pdu_decode_header()
544 pdu->community[octs_len] = '\0'; in snmp_pdu_decode_header()
551 snmp_pdu_decode_scoped(struct asn_buf *b, struct snmp_pdu *pdu, int32_t *ip) in snmp_pdu_decode_scoped() argument
557 if (pdu->version == SNMP_V3) { in snmp_pdu_decode_scoped()
559 snmp_error("cannot decode scoped pdu header"); in snmp_pdu_decode_scoped()
564 if (asn_get_octetstring(b, (u_char *)&pdu->context_engine, in snmp_pdu_decode_scoped()
569 pdu->context_engine_len = len; in snmp_pdu_decode_scoped()
572 if (asn_get_octetstring(b, (u_char *)&pdu->context_name, in snmp_pdu_decode_scoped()
577 pdu->context_name[len] = '\0'; in snmp_pdu_decode_scoped()
581 snmp_error("cannot get pdu header"); in snmp_pdu_decode_scoped()
586 snmp_error("bad pdu header tag"); in snmp_pdu_decode_scoped()
589 pdu->type = type & ASN_TYPE_MASK; in snmp_pdu_decode_scoped()
591 switch (pdu->type) { in snmp_pdu_decode_scoped()
600 if (pdu->version != SNMP_V1) { in snmp_pdu_decode_scoped()
601 snmp_error("bad pdu type %u", pdu->type); in snmp_pdu_decode_scoped()
610 if (pdu->version == SNMP_V1) { in snmp_pdu_decode_scoped()
611 snmp_error("bad pdu type %u", pdu->type); in snmp_pdu_decode_scoped()
617 snmp_error("bad pdu type %u", pdu->type); in snmp_pdu_decode_scoped()
624 err = parse_pdus(b, pdu, ip); in snmp_pdu_decode_scoped()
629 snmp_error("ignoring trailing junk after pdu"); in snmp_pdu_decode_scoped()
637 snmp_pdu_decode_secmode(struct asn_buf *b, struct snmp_pdu *pdu) in snmp_pdu_decode_secmode() argument
643 if (pdu->user.auth_proto != SNMP_AUTH_NOAUTH && in snmp_pdu_decode_secmode()
644 (pdu->flags & SNMP_MSG_AUTH_FLAG) == 0) in snmp_pdu_decode_secmode()
647 if ((code = snmp_pdu_calc_digest(pdu, digest)) != in snmp_pdu_decode_secmode()
651 if (pdu->user.auth_proto != SNMP_AUTH_NOAUTH && in snmp_pdu_decode_secmode()
652 memcmp(digest, pdu->msg_digest, sizeof(pdu->msg_digest)) != 0) in snmp_pdu_decode_secmode()
655 if (pdu->user.priv_proto != SNMP_PRIV_NOPRIV && (asn_get_header(b, &type, in snmp_pdu_decode_secmode()
656 &pdu->scoped_len) != ASN_ERR_OK || type != ASN_TYPE_OCTETSTRING)) { in snmp_pdu_decode_secmode()
657 snmp_error("cannot decode encrypted pdu"); in snmp_pdu_decode_secmode()
660 pdu->scoped_ptr = b->asn_ptr; in snmp_pdu_decode_secmode()
662 if (pdu->user.priv_proto != SNMP_PRIV_NOPRIV && in snmp_pdu_decode_secmode()
663 (pdu->flags & SNMP_MSG_PRIV_FLAG) == 0) in snmp_pdu_decode_secmode()
666 if ((code = snmp_pdu_decrypt(pdu)) != SNMP_CODE_OK) in snmp_pdu_decode_secmode()
673 * Check whether what we have is the complete PDU by snooping at the
677 * > 0 the length of this PDU
731 * Encode the SNMP PDU without the variable bindings field.
738 snmp_pdu_encode_header(struct asn_buf *b, struct snmp_pdu *pdu) in snmp_pdu_encode_header() argument
744 &pdu->outer_ptr) != ASN_ERR_OK) in snmp_pdu_encode_header()
747 if (pdu->version == SNMP_V1) in snmp_pdu_encode_header()
749 else if (pdu->version == SNMP_V2c) in snmp_pdu_encode_header()
751 else if (pdu->version == SNMP_V3) in snmp_pdu_encode_header()
758 if (pdu->version == SNMP_V3) { in snmp_pdu_encode_header()
763 if (asn_put_integer(b, pdu->identifier) != ASN_ERR_OK) in snmp_pdu_encode_header()
766 if (asn_put_integer(b, pdu->engine.max_msg_size) != ASN_ERR_OK) in snmp_pdu_encode_header()
769 if (pdu->type != SNMP_PDU_RESPONSE && in snmp_pdu_encode_header()
770 pdu->type != SNMP_PDU_TRAP && in snmp_pdu_encode_header()
771 pdu->type != SNMP_PDU_TRAP2 && in snmp_pdu_encode_header()
772 pdu->type != SNMP_PDU_REPORT) in snmp_pdu_encode_header()
773 pdu->flags |= SNMP_MSG_REPORT_FLAG; in snmp_pdu_encode_header()
775 if (asn_put_octetstring(b, (u_char *)&pdu->flags, 1) in snmp_pdu_encode_header()
779 if (asn_put_integer(b, pdu->security_model) != ASN_ERR_OK) in snmp_pdu_encode_header()
785 if (pdu->security_model != SNMP_SECMODEL_USM) in snmp_pdu_encode_header()
788 if (pdu_encode_secparams(b, pdu) != SNMP_CODE_OK) in snmp_pdu_encode_header()
793 ASN_TYPE_CONSTRUCTED), &pdu->scoped_ptr) != ASN_ERR_OK) in snmp_pdu_encode_header()
796 if (asn_put_octetstring(b, (u_char *)pdu->context_engine, in snmp_pdu_encode_header()
797 pdu->context_engine_len) != ASN_ERR_OK) in snmp_pdu_encode_header()
800 if (asn_put_octetstring(b, (u_char *)pdu->context_name, in snmp_pdu_encode_header()
801 strlen(pdu->context_name)) != ASN_ERR_OK) in snmp_pdu_encode_header()
804 if (asn_put_octetstring(b, (u_char *)pdu->community, in snmp_pdu_encode_header()
805 strlen(pdu->community)) != ASN_ERR_OK) in snmp_pdu_encode_header()
810 pdu->type), &pdu->pdu_ptr) != ASN_ERR_OK) in snmp_pdu_encode_header()
813 if (pdu->type == SNMP_PDU_TRAP) { in snmp_pdu_encode_header()
814 if (pdu->version != SNMP_V1 || in snmp_pdu_encode_header()
815 asn_put_objid(b, &pdu->enterprise) != ASN_ERR_OK || in snmp_pdu_encode_header()
816 asn_put_ipaddress(b, pdu->agent_addr) != ASN_ERR_OK || in snmp_pdu_encode_header()
817 asn_put_integer(b, pdu->generic_trap) != ASN_ERR_OK || in snmp_pdu_encode_header()
818 asn_put_integer(b, pdu->specific_trap) != ASN_ERR_OK || in snmp_pdu_encode_header()
819 asn_put_timeticks(b, pdu->time_stamp) != ASN_ERR_OK) in snmp_pdu_encode_header()
822 if (pdu->version == SNMP_V1 && (pdu->type == SNMP_PDU_GETBULK || in snmp_pdu_encode_header()
823 pdu->type == SNMP_PDU_INFORM || in snmp_pdu_encode_header()
824 pdu->type == SNMP_PDU_TRAP2 || in snmp_pdu_encode_header()
825 pdu->type == SNMP_PDU_REPORT)) in snmp_pdu_encode_header()
828 if (asn_put_integer(b, pdu->request_id) != ASN_ERR_OK || in snmp_pdu_encode_header()
829 asn_put_integer(b, pdu->error_status) != ASN_ERR_OK || in snmp_pdu_encode_header()
830 asn_put_integer(b, pdu->error_index) != ASN_ERR_OK) in snmp_pdu_encode_header()
835 &pdu->vars_ptr) != ASN_ERR_OK) in snmp_pdu_encode_header()
842 snmp_pdu_fix_padd(struct asn_buf *b, struct snmp_pdu *pdu) in snmp_pdu_fix_padd() argument
846 if (pdu->user.priv_proto == SNMP_PRIV_DES && pdu->scoped_len % 8 != 0) { in snmp_pdu_fix_padd()
847 padlen = 8 - (pdu->scoped_len % 8); in snmp_pdu_fix_padd()
850 pdu->scoped_len += padlen; in snmp_pdu_fix_padd()
857 snmp_fix_encoding(struct asn_buf *b, struct snmp_pdu *pdu) in snmp_fix_encoding() argument
862 if (asn_commit_header(b, pdu->vars_ptr, NULL) != ASN_ERR_OK || in snmp_fix_encoding()
863 asn_commit_header(b, pdu->pdu_ptr, NULL) != ASN_ERR_OK) in snmp_fix_encoding()
866 if (pdu->version == SNMP_V3) { in snmp_fix_encoding()
867 if (asn_commit_header(b, pdu->scoped_ptr, NULL) != ASN_ERR_OK) in snmp_fix_encoding()
870 pdu->scoped_len = b->asn_ptr - pdu->scoped_ptr; in snmp_fix_encoding()
871 if (snmp_pdu_fix_padd(b, pdu) != ASN_ERR_OK) in snmp_fix_encoding()
874 if (pdu->security_model != SNMP_SECMODEL_USM) in snmp_fix_encoding()
877 if (snmp_pdu_encrypt(pdu) != SNMP_CODE_OK) in snmp_fix_encoding()
880 if (pdu->user.priv_proto != SNMP_PRIV_NOPRIV && in snmp_fix_encoding()
881 asn_commit_header(b, pdu->encrypted_ptr, NULL) != ASN_ERR_OK) in snmp_fix_encoding()
885 if (asn_commit_header(b, pdu->outer_ptr, &moved) != ASN_ERR_OK) in snmp_fix_encoding()
888 pdu->outer_len = b->asn_ptr - pdu->outer_ptr; in snmp_fix_encoding()
889 pdu->digest_ptr -= moved; in snmp_fix_encoding()
891 if (pdu->version == SNMP_V3) { in snmp_fix_encoding()
892 if ((code = snmp_pdu_calc_digest(pdu, pdu->msg_digest)) != in snmp_fix_encoding()
896 if ((pdu->flags & SNMP_MSG_AUTH_FLAG) != 0) in snmp_fix_encoding()
897 memcpy(pdu->digest_ptr, pdu->msg_digest, in snmp_fix_encoding()
898 sizeof(pdu->msg_digest)); in snmp_fix_encoding()
993 * Encode an PDU.
996 snmp_pdu_encode(struct snmp_pdu *pdu, struct asn_buf *resp_b) in snmp_pdu_encode() argument
1001 if ((err = snmp_pdu_encode_header(resp_b, pdu)) != SNMP_CODE_OK) in snmp_pdu_encode()
1003 for (idx = 0; idx < pdu->nbindings; idx++) in snmp_pdu_encode()
1004 if (snmp_binding_encode(resp_b, &pdu->bindings[idx]) in snmp_pdu_encode()
1008 return (snmp_fix_encoding(resp_b, pdu)); in snmp_pdu_encode()
1078 dump_bindings(const struct snmp_pdu *pdu) in dump_bindings() argument
1082 for (i = 0; i < pdu->nbindings; i++) { in dump_bindings()
1084 dump_binding(&pdu->bindings[i]); in dump_bindings()
1090 dump_notrap(const struct snmp_pdu *pdu) in dump_notrap() argument
1092 snmp_printf(" request_id=%d", pdu->request_id); in dump_notrap()
1093 snmp_printf(" error_status=%d", pdu->error_status); in dump_notrap()
1094 snmp_printf(" error_index=%d\n", pdu->error_index); in dump_notrap()
1095 dump_bindings(pdu); in dump_notrap()
1099 snmp_pdu_dump(const struct snmp_pdu *pdu) in snmp_pdu_dump() argument
1115 if (pdu->version == SNMP_V1) in snmp_pdu_dump()
1117 else if (pdu->version == SNMP_V2c) in snmp_pdu_dump()
1119 else if (pdu->version == SNMP_V3) in snmp_pdu_dump()
1124 switch (pdu->type) { in snmp_pdu_dump()
1126 snmp_printf("%s %s '%s'", types[pdu->type], vers, pdu->community); in snmp_pdu_dump()
1127 snmp_printf(" enterprise=%s", asn_oid2str_r(&pdu->enterprise, buf)); in snmp_pdu_dump()
1128 snmp_printf(" agent_addr=%u.%u.%u.%u", pdu->agent_addr[0], in snmp_pdu_dump()
1129 pdu->agent_addr[1], pdu->agent_addr[2], pdu->agent_addr[3]); in snmp_pdu_dump()
1130 snmp_printf(" generic_trap=%d", pdu->generic_trap); in snmp_pdu_dump()
1131 snmp_printf(" specific_trap=%d", pdu->specific_trap); in snmp_pdu_dump()
1132 snmp_printf(" time-stamp=%u\n", pdu->time_stamp); in snmp_pdu_dump()
1133 dump_bindings(pdu); in snmp_pdu_dump()
1144 snmp_printf("%s %s '%s'", types[pdu->type], vers, pdu->community); in snmp_pdu_dump()
1145 dump_notrap(pdu); in snmp_pdu_dump()
1149 snmp_printf("bad pdu type %u\n", pdu->type); in snmp_pdu_dump()
1187 snmp_pdu_init_secparams(struct snmp_pdu *pdu) in snmp_pdu_init_secparams() argument
1191 if (pdu->user.auth_proto != SNMP_AUTH_NOAUTH) in snmp_pdu_init_secparams()
1192 pdu->flags |= SNMP_MSG_AUTH_FLAG; in snmp_pdu_init_secparams()
1194 switch (pdu->user.priv_proto) { in snmp_pdu_init_secparams()
1196 memcpy(pdu->msg_salt, &pdu->engine.engine_boots, in snmp_pdu_init_secparams()
1197 sizeof(pdu->engine.engine_boots)); in snmp_pdu_init_secparams()
1199 memcpy(pdu->msg_salt + sizeof(pdu->engine.engine_boots), &rval, in snmp_pdu_init_secparams()
1201 pdu->flags |= SNMP_MSG_PRIV_FLAG; in snmp_pdu_init_secparams()
1205 memcpy(pdu->msg_salt, &rval, sizeof(int32_t)); in snmp_pdu_init_secparams()
1207 memcpy(pdu->msg_salt + sizeof(int32_t), &rval, sizeof(int32_t)); in snmp_pdu_init_secparams()
1208 pdu->flags |= SNMP_MSG_PRIV_FLAG; in snmp_pdu_init_secparams()
1216 snmp_pdu_free(struct snmp_pdu *pdu) in snmp_pdu_free() argument
1220 for (i = 0; i < pdu->nbindings; i++) in snmp_pdu_free()
1221 snmp_value_free(&pdu->bindings[i]); in snmp_pdu_free()
1222 pdu->nbindings = 0; in snmp_pdu_free()