+#include "rpl.h"
+
+static const struct tok icmp6_type_values[] = {
+ { ICMP6_DST_UNREACH, "destination unreachable"},
+ { ICMP6_PACKET_TOO_BIG, "packet too big"},
+ { ICMP6_TIME_EXCEEDED, "time exceeded in-transit"},
+ { ICMP6_PARAM_PROB, "parameter problem"},
+ { ICMP6_ECHO_REQUEST, "echo request"},
+ { ICMP6_ECHO_REPLY, "echo reply"},
+ { MLD6_LISTENER_QUERY, "multicast listener query"},
+ { MLD6_LISTENER_REPORT, "multicast listener report"},
+ { MLD6_LISTENER_DONE, "multicast listener done"},
+ { ND_ROUTER_SOLICIT, "router solicitation"},
+ { ND_ROUTER_ADVERT, "router advertisement"},
+ { ND_NEIGHBOR_SOLICIT, "neighbor solicitation"},
+ { ND_NEIGHBOR_ADVERT, "neighbor advertisement"},
+ { ND_REDIRECT, "redirect"},
+ { ICMP6_ROUTER_RENUMBERING, "router renumbering"},
+ { IND_SOLICIT, "inverse neighbor solicitation"},
+ { IND_ADVERT, "inverse neighbor advertisement"},
+ { MLDV2_LISTENER_REPORT, "multicast listener report v2"},
+ { ICMP6_HADISCOV_REQUEST, "ha discovery request"},
+ { ICMP6_HADISCOV_REPLY, "ha discovery reply"},
+ { ICMP6_MOBILEPREFIX_SOLICIT, "mobile router solicitation"},
+ { ICMP6_MOBILEPREFIX_ADVERT, "mobile router advertisement"},
+ { ICMP6_WRUREQUEST, "who-are-you request"},
+ { ICMP6_WRUREPLY, "who-are-you reply"},
+ { ICMP6_NI_QUERY, "node information query"},
+ { ICMP6_NI_REPLY, "node information reply"},
+ { MLD6_MTRACE, "mtrace message"},
+ { MLD6_MTRACE_RESP, "mtrace response"},
+ { ND_RPL_MESSAGE, "RPL"},
+ { 0, NULL }
+};
+
+static const struct tok icmp6_dst_unreach_code_values[] = {
+ { ICMP6_DST_UNREACH_NOROUTE, "unreachable route" },
+ { ICMP6_DST_UNREACH_ADMIN, " unreachable prohibited"},
+ { ICMP6_DST_UNREACH_BEYONDSCOPE, "beyond scope"},
+ { ICMP6_DST_UNREACH_ADDR, "unreachable address"},
+ { ICMP6_DST_UNREACH_NOPORT, "unreachable port"},
+ { 0, NULL }
+};
+
+static const struct tok icmp6_opt_pi_flag_values[] = {
+ { ND_OPT_PI_FLAG_ONLINK, "onlink" },
+ { ND_OPT_PI_FLAG_AUTO, "auto" },
+ { ND_OPT_PI_FLAG_ROUTER, "router" },
+ { 0, NULL }
+};
+
+static const struct tok icmp6_opt_ra_flag_values[] = {
+ { ND_RA_FLAG_MANAGED, "managed" },
+ { ND_RA_FLAG_OTHER, "other stateful"},
+ { ND_RA_FLAG_HOME_AGENT, "home agent"},
+ { 0, NULL }
+};
+
+static const struct tok icmp6_nd_na_flag_values[] = {
+ { ND_NA_FLAG_ROUTER, "router" },
+ { ND_NA_FLAG_SOLICITED, "solicited" },
+ { ND_NA_FLAG_OVERRIDE, "override" },
+ { 0, NULL }
+};
+
+
+static const struct tok icmp6_opt_values[] = {
+ { ND_OPT_SOURCE_LINKADDR, "source link-address"},
+ { ND_OPT_TARGET_LINKADDR, "destination link-address"},
+ { ND_OPT_PREFIX_INFORMATION, "prefix info"},
+ { ND_OPT_REDIRECTED_HEADER, "redirected header"},
+ { ND_OPT_MTU, "mtu"},
+ { ND_OPT_RDNSS, "rdnss"},
+ { ND_OPT_DNSSL, "dnssl"},
+ { ND_OPT_ADVINTERVAL, "advertisement interval"},
+ { ND_OPT_HOMEAGENT_INFO, "homeagent information"},
+ { ND_OPT_ROUTE_INFO, "route info"},
+ { 0, NULL }
+};
+
+/* mldv2 report types */
+static const struct tok mldv2report2str[] = {
+ { 1, "is_in" },
+ { 2, "is_ex" },
+ { 3, "to_in" },
+ { 4, "to_ex" },
+ { 5, "allow" },
+ { 6, "block" },
+ { 0, NULL }
+};
+
+static const char *
+get_rtpref(u_int v)
+{
+ static const char *rtpref_str[] = {
+ "medium", /* 00 */
+ "high", /* 01 */
+ "rsv", /* 10 */
+ "low" /* 11 */
+ };
+
+ return rtpref_str[((v & ND_RA_FLAG_RTPREF_MASK) >> 3) & 0xff];
+}
+
+static const char *
+get_lifetime(uint32_t v)
+{
+ static char buf[20];
+
+ if (v == (uint32_t)~0UL)
+ return "infinity";
+ else {
+ snprintf(buf, sizeof(buf), "%us", v);
+ return buf;
+ }
+}
+
+static void
+print_lladdr(netdissect_options *ndo, const uint8_t *p, size_t l)
+{
+ const uint8_t *ep, *q;
+
+ q = p;
+ ep = p + l;
+ while (l > 0 && q < ep) {
+ if (q > p)
+ ND_PRINT((ndo,":"));
+ ND_PRINT((ndo,"%02x", *q++));
+ l--;
+ }
+}
+
+static int icmp6_cksum(netdissect_options *ndo, const struct ip6_hdr *ip6,
+ const struct icmp6_hdr *icp, u_int len)
+{
+ return nextproto6_cksum(ndo, ip6, (const uint8_t *)(const void *)icp, len, len,
+ IPPROTO_ICMPV6);
+}
+
+static const struct tok rpl_mop_values[] = {
+ { RPL_DIO_NONSTORING, "nonstoring"},
+ { RPL_DIO_STORING, "storing"},
+ { RPL_DIO_NONSTORING_MULTICAST, "nonstoring-multicast"},
+ { RPL_DIO_STORING_MULTICAST, "storing-multicast"},
+ { 0, NULL},
+};
+
+static const struct tok rpl_subopt_values[] = {
+ { RPL_OPT_PAD0, "pad0"},
+ { RPL_OPT_PADN, "padN"},
+ { RPL_DIO_METRICS, "metrics"},
+ { RPL_DIO_ROUTINGINFO, "routinginfo"},
+ { RPL_DIO_CONFIG, "config"},
+ { RPL_DAO_RPLTARGET, "rpltarget"},
+ { RPL_DAO_TRANSITINFO, "transitinfo"},
+ { RPL_DIO_DESTPREFIX, "destprefix"},
+ { RPL_DAO_RPLTARGET_DESC, "rpltargetdesc"},
+ { 0, NULL},
+};
+
+static void
+rpl_dio_printopt(netdissect_options *ndo,
+ const struct rpl_dio_genoption *opt,
+ u_int length)
+{
+ if(length < RPL_DIO_GENOPTION_LEN) return;
+ length -= RPL_DIO_GENOPTION_LEN;
+
+ ND_TCHECK(opt->rpl_dio_len);
+
+ while((opt->rpl_dio_type == RPL_OPT_PAD0 &&
+ (const u_char *)opt < ndo->ndo_snapend) ||
+ ND_TTEST2(*opt,(opt->rpl_dio_len+2))) {
+
+ unsigned int optlen = opt->rpl_dio_len+2;
+ if(opt->rpl_dio_type == RPL_OPT_PAD0) {
+ optlen = 1;
+ ND_PRINT((ndo, " opt:pad0"));
+ } else {
+ ND_PRINT((ndo, " opt:%s len:%u ",
+ tok2str(rpl_subopt_values, "subopt:%u", opt->rpl_dio_type),
+ optlen));
+ if(ndo->ndo_vflag > 2) {
+ unsigned int paylen = opt->rpl_dio_len;
+ if(paylen > length) paylen = length;
+ hex_print(ndo,
+ " ",
+ ((const uint8_t *)opt) + RPL_DIO_GENOPTION_LEN, /* content of DIO option */
+ paylen);
+ }
+ }
+ opt = (const struct rpl_dio_genoption *)(((const char *)opt) + optlen);
+ length -= optlen;
+ }
+ return;
+trunc:
+ ND_PRINT((ndo," [|truncated]"));
+ return;
+}
+
+static void
+rpl_dio_print(netdissect_options *ndo,
+ const u_char *bp, u_int length)
+{
+ const struct nd_rpl_dio *dio = (const struct nd_rpl_dio *)bp;
+ const char *dagid_str;
+
+ ND_TCHECK(*dio);
+ dagid_str = ip6addr_string (ndo, dio->rpl_dagid);
+
+ ND_PRINT((ndo, " [dagid:%s,seq:%u,instance:%u,rank:%u,%smop:%s,prf:%u]",
+ dagid_str,
+ dio->rpl_dtsn,
+ dio->rpl_instanceid,
+ EXTRACT_16BITS(&dio->rpl_dagrank),
+ RPL_DIO_GROUNDED(dio->rpl_mopprf) ? "grounded,":"",
+ tok2str(rpl_mop_values, "mop%u", RPL_DIO_MOP(dio->rpl_mopprf)),
+ RPL_DIO_PRF(dio->rpl_mopprf)));
+
+ if(ndo->ndo_vflag > 1) {
+ const struct rpl_dio_genoption *opt = (const struct rpl_dio_genoption *)&dio[1];
+ rpl_dio_printopt(ndo, opt, length);
+ }
+ return;
+trunc:
+ ND_PRINT((ndo," [|truncated]"));
+ return;
+}
+
+static void
+rpl_dao_print(netdissect_options *ndo,
+ const u_char *bp, u_int length)
+{
+ const struct nd_rpl_dao *dao = (const struct nd_rpl_dao *)bp;
+ const char *dagid_str = "<elided>";
+
+ ND_TCHECK(*dao);
+ if (length < ND_RPL_DAO_MIN_LEN)
+ goto tooshort;
+
+ bp += ND_RPL_DAO_MIN_LEN;
+ length -= ND_RPL_DAO_MIN_LEN;
+ if(RPL_DAO_D(dao->rpl_flags)) {
+ ND_TCHECK2(dao->rpl_dagid, DAGID_LEN);
+ if (length < DAGID_LEN)
+ goto tooshort;
+ dagid_str = ip6addr_string (ndo, dao->rpl_dagid);
+ bp += DAGID_LEN;
+ length -= DAGID_LEN;
+ }
+
+ ND_PRINT((ndo, " [dagid:%s,seq:%u,instance:%u%s%s,%02x]",
+ dagid_str,
+ dao->rpl_daoseq,
+ dao->rpl_instanceid,
+ RPL_DAO_K(dao->rpl_flags) ? ",acK":"",
+ RPL_DAO_D(dao->rpl_flags) ? ",Dagid":"",
+ dao->rpl_flags));
+
+ if(ndo->ndo_vflag > 1) {
+ const struct rpl_dio_genoption *opt = (const struct rpl_dio_genoption *)bp;
+ rpl_dio_printopt(ndo, opt, length);
+ }
+ return;
+
+trunc:
+ ND_PRINT((ndo," [|truncated]"));
+ return;
+
+tooshort:
+ ND_PRINT((ndo," [|length too short]"));
+ return;
+}
+
+static void
+rpl_daoack_print(netdissect_options *ndo,
+ const u_char *bp, u_int length)
+{
+ const struct nd_rpl_daoack *daoack = (const struct nd_rpl_daoack *)bp;
+ const char *dagid_str = "<elided>";
+
+ ND_TCHECK2(*daoack, ND_RPL_DAOACK_MIN_LEN);
+ if (length < ND_RPL_DAOACK_MIN_LEN)
+ goto tooshort;
+
+ bp += ND_RPL_DAOACK_MIN_LEN;
+ length -= ND_RPL_DAOACK_MIN_LEN;
+ if(RPL_DAOACK_D(daoack->rpl_flags)) {
+ ND_TCHECK2(daoack->rpl_dagid, DAGID_LEN);
+ if (length < DAGID_LEN)
+ goto tooshort;
+ dagid_str = ip6addr_string (ndo, daoack->rpl_dagid);
+ bp += DAGID_LEN;
+ length -= DAGID_LEN;
+ }
+
+ ND_PRINT((ndo, " [dagid:%s,seq:%u,instance:%u,status:%u]",
+ dagid_str,
+ daoack->rpl_daoseq,
+ daoack->rpl_instanceid,
+ daoack->rpl_status));
+
+ /* no officially defined options for DAOACK, but print any we find */
+ if(ndo->ndo_vflag > 1) {
+ const struct rpl_dio_genoption *opt = (const struct rpl_dio_genoption *)bp;
+ rpl_dio_printopt(ndo, opt, length);
+ }
+ return;
+
+trunc:
+ ND_PRINT((ndo," [|dao-truncated]"));
+ return;
+
+tooshort:
+ ND_PRINT((ndo," [|dao-length too short]"));
+ return;
+}
+
+static void
+rpl_print(netdissect_options *ndo,
+ const struct icmp6_hdr *hdr,
+ const u_char *bp, u_int length)
+{
+ int secured = hdr->icmp6_code & 0x80;
+ int basecode= hdr->icmp6_code & 0x7f;
+
+ if(secured) {
+ ND_PRINT((ndo, ", (SEC) [worktodo]"));
+ /* XXX
+ * the next header pointer needs to move forward to
+ * skip the secure part.
+ */
+ return;
+ } else {
+ ND_PRINT((ndo, ", (CLR)"));
+ }
+
+ switch(basecode) {
+ case ND_RPL_DAG_IS:
+ ND_PRINT((ndo, "DODAG Information Solicitation"));
+ if(ndo->ndo_vflag) {
+ }
+ break;
+ case ND_RPL_DAG_IO:
+ ND_PRINT((ndo, "DODAG Information Object"));
+ if(ndo->ndo_vflag) {
+ rpl_dio_print(ndo, bp, length);
+ }
+ break;
+ case ND_RPL_DAO:
+ ND_PRINT((ndo, "Destination Advertisement Object"));
+ if(ndo->ndo_vflag) {
+ rpl_dao_print(ndo, bp, length);
+ }
+ break;
+ case ND_RPL_DAO_ACK:
+ ND_PRINT((ndo, "Destination Advertisement Object Ack"));
+ if(ndo->ndo_vflag) {
+ rpl_daoack_print(ndo, bp, length);
+ }
+ break;
+ default:
+ ND_PRINT((ndo, "RPL message, unknown code %u",hdr->icmp6_code));
+ break;
+ }
+ return;
+
+#if 0
+trunc:
+ ND_PRINT((ndo," [|truncated]"));
+ return;