print-rrcp.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2007 - Andrey "nording" Chernyak <andrew@nording.ru>
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that: (1) source code distributions
  6. * retain the above copyright notice and this paragraph in its entirety, (2)
  7. * distributions including binary code include the above copyright notice and
  8. * this paragraph in its entirety in the documentation or other materials
  9. * provided with the distribution, and (3) all advertising materials mentioning
  10. * features or use of this software display the following acknowledgement:
  11. * ``This product includes software developed by the University of California,
  12. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  13. * the University nor the names of its contributors may be used to endorse
  14. * or promote products derived from this software without specific prior
  15. * written permission.
  16. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19. *
  20. * Format and print Realtek Remote Control Protocol (RRCP)
  21. * and Realtek Echo Protocol (RRCP-REP) packets.
  22. */
  23. /* \summary: Realtek Remote Control Protocol (RRCP) printer */
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include <netdissect-stdinc.h>
  28. #include "netdissect.h"
  29. #include "addrtoname.h"
  30. #include "extract.h"
  31. #include "ether.h"
  32. #define RRCP_OPCODE_MASK 0x7F /* 0x00 = hello, 0x01 = get, 0x02 = set */
  33. #define RRCP_ISREPLY 0x80 /* 0 = request to switch, 0x80 = reply from switch */
  34. #define RRCP_PROTO_OFFSET 0 /* proto - 1 byte, must be 1 */
  35. #define RRCP_OPCODE_ISREPLY_OFFSET 1 /* opcode and isreply flag - 1 byte */
  36. #define RRCP_AUTHKEY_OFFSET 2 /* authorization key - 2 bytes, 0x2379 by default */
  37. /* most packets */
  38. #define RRCP_REG_ADDR_OFFSET 4 /* register address - 2 bytes */
  39. #define RRCP_REG_DATA_OFFSET 6 /* register data - 4 bytes */
  40. #define RRCP_COOKIE1_OFFSET 10 /* 4 bytes */
  41. #define RRCP_COOKIE2_OFFSET 14 /* 4 bytes */
  42. /* hello reply packets */
  43. #define RRCP_DOWNLINK_PORT_OFFSET 4 /* 1 byte */
  44. #define RRCP_UPLINK_PORT_OFFSET 5 /* 1 byte */
  45. #define RRCP_UPLINK_MAC_OFFSET 6 /* 6 byte MAC address */
  46. #define RRCP_CHIP_ID_OFFSET 12 /* 2 bytes */
  47. #define RRCP_VENDOR_ID_OFFSET 14 /* 4 bytes */
  48. static const struct tok proto_values[] = {
  49. { 1, "RRCP" },
  50. { 2, "RRCP-REP" },
  51. { 0, NULL }
  52. };
  53. static const struct tok opcode_values[] = {
  54. { 0, "hello" },
  55. { 1, "get" },
  56. { 2, "set" },
  57. { 0, NULL }
  58. };
  59. /*
  60. * Print RRCP requests
  61. */
  62. void
  63. rrcp_print(netdissect_options *ndo,
  64. register const u_char *cp,
  65. u_int length _U_,
  66. const struct lladdr_info *src,
  67. const struct lladdr_info *dst)
  68. {
  69. uint8_t rrcp_proto;
  70. uint8_t rrcp_opcode;
  71. ND_TCHECK(*(cp + RRCP_PROTO_OFFSET));
  72. rrcp_proto = *(cp + RRCP_PROTO_OFFSET);
  73. ND_TCHECK(*(cp + RRCP_OPCODE_ISREPLY_OFFSET));
  74. rrcp_opcode = (*(cp + RRCP_OPCODE_ISREPLY_OFFSET)) & RRCP_OPCODE_MASK;
  75. if (src != NULL && dst != NULL) {
  76. ND_PRINT((ndo, "%s > %s, ",
  77. (src->addr_string)(ndo, src->addr),
  78. (dst->addr_string)(ndo, dst->addr)));
  79. }
  80. ND_PRINT((ndo, "%s %s",
  81. tok2str(proto_values,"RRCP-0x%02x",rrcp_proto),
  82. ((*(cp + RRCP_OPCODE_ISREPLY_OFFSET)) & RRCP_ISREPLY) ? "reply" : "query"));
  83. if (rrcp_proto==1){
  84. ND_PRINT((ndo, ": %s",
  85. tok2str(opcode_values,"unknown opcode (0x%02x)",rrcp_opcode)));
  86. }
  87. if (rrcp_opcode==1 || rrcp_opcode==2){
  88. ND_TCHECK2(*(cp + RRCP_REG_ADDR_OFFSET), 6);
  89. ND_PRINT((ndo, " addr=0x%04x, data=0x%08x",
  90. EXTRACT_LE_16BITS(cp + RRCP_REG_ADDR_OFFSET),
  91. EXTRACT_LE_32BITS(cp + RRCP_REG_DATA_OFFSET)));
  92. }
  93. if (rrcp_proto==1){
  94. ND_TCHECK2(*(cp + RRCP_AUTHKEY_OFFSET), 2);
  95. ND_PRINT((ndo, ", auth=0x%04x",
  96. EXTRACT_16BITS(cp + RRCP_AUTHKEY_OFFSET)));
  97. }
  98. if (rrcp_proto==1 && rrcp_opcode==0 &&
  99. ((*(cp + RRCP_OPCODE_ISREPLY_OFFSET)) & RRCP_ISREPLY)){
  100. ND_TCHECK2(*(cp + RRCP_VENDOR_ID_OFFSET), 4);
  101. ND_PRINT((ndo, " downlink_port=%d, uplink_port=%d, uplink_mac=%s, vendor_id=%08x ,chip_id=%04x ",
  102. *(cp + RRCP_DOWNLINK_PORT_OFFSET),
  103. *(cp + RRCP_UPLINK_PORT_OFFSET),
  104. etheraddr_string(ndo, cp + RRCP_UPLINK_MAC_OFFSET),
  105. EXTRACT_32BITS(cp + RRCP_VENDOR_ID_OFFSET),
  106. EXTRACT_16BITS(cp + RRCP_CHIP_ID_OFFSET)));
  107. }else if (rrcp_opcode==1 || rrcp_opcode==2 || rrcp_proto==2){
  108. ND_TCHECK2(*(cp + RRCP_COOKIE2_OFFSET), 4);
  109. ND_PRINT((ndo, ", cookie=0x%08x%08x ",
  110. EXTRACT_32BITS(cp + RRCP_COOKIE2_OFFSET),
  111. EXTRACT_32BITS(cp + RRCP_COOKIE1_OFFSET)));
  112. }
  113. return;
  114. trunc:
  115. ND_PRINT((ndo, "[|rrcp]"));
  116. }