Esempio n. 1
0
ng_pktsnip_t *ng_ipv6_hdr_build(ng_pktsnip_t *payload,
                                uint8_t *src, uint8_t src_len,
                                uint8_t *dst, uint8_t dst_len)
{
    ng_pktsnip_t *ipv6;
    ng_ipv6_hdr_t *hdr;

    if (((src_len != 0) && (src_len != sizeof(ng_ipv6_addr_t))) ||
        ((dst_len != 0) && (dst_len != sizeof(ng_ipv6_addr_t)))) {
        DEBUG("ipv6_hdr: Address length was not 0 or %zu byte.\n",
              sizeof(ng_ipv6_addr_t));
        return NULL;
    }

    ipv6 = ng_pktbuf_add(payload, NULL, sizeof(ng_ipv6_hdr_t), HDR_NETTYPE);

    if (ipv6 == NULL) {
        DEBUG("ipv6_hdr: no space left in packet buffer\n");
        return NULL;
    }

    hdr = (ng_ipv6_hdr_t *)ipv6->data;

    if ((src != NULL) && (src_len != 0)) {
#ifdef MODULE_NG_IPV6_ADDR
        DEBUG("ipv6_hdr: set packet source to %s\n",
              ng_ipv6_addr_to_str(addr_str, (ng_ipv6_addr_t *)src,
                                  sizeof(addr_str)));
#endif
        memcpy(&hdr->src, src, src_len);
    }
    else {
        DEBUG("ipv6_hdr: set packet source to ::\n");
        ng_ipv6_addr_set_unspecified(&hdr->src);
    }

    memset(&hdr->dst + dst_len, 0, sizeof(ng_ipv6_addr_t) - dst_len);

    if ((dst != NULL) && (dst_len != 0)) {
#ifdef MODULE_NG_IPV6_ADDR
        DEBUG("ipv6_hdr: set packet destination to %s\n",
              ng_ipv6_addr_to_str(addr_str, (ng_ipv6_addr_t *)dst,
                                  sizeof(addr_str)));
#endif
        memcpy(&hdr->dst, dst, dst_len);
    }
    else {
        DEBUG("ipv6_hdr: set packet destination to ::1\n");
        ng_ipv6_addr_set_loopback(&hdr->dst);
    }

    hdr->v_tc_fl = byteorder_htonl(0x60000000); /* set version, tc and fl in one go*/
    hdr->nh = PROTNUM_RESERVED;
    hdr->hl = 0;

    return ipv6;
}
Esempio n. 2
0
static void send(int argc, char **argv) {
    if (argc < 2) {
        puts("Send n bytes to fe80::1\n");
        puts("Usage: send <n>");
        return;
    }

    ipv6_hdr_t ipv6_hdr;
    netdev_hlist_t ulh = { NULL, NULL, &ipv6_hdr, sizeof(ipv6_hdr_t) };
    long long n = atoll(argv[1]);
    uint8_t bytes[n];
    uint16_t dest = 1;

    if (n < 0 || n >= (1 << 16)) {
        puts("n must be a 16-bit unsigned integer");
        return;
    }

    for (int i = 0; i < n; i++) {
        bytes[i] = data_value;
    }

    data_value++;

    ulh.next = &ulh;
    ulh.prev = &ulh;
    ipv6_hdr_set_version(&ipv6_hdr);
    ipv6_hdr_set_trafficclass(&ipv6_hdr, 0);
    ipv6_hdr_set_flowlabel(&ipv6_hdr, 0);
    ipv6_hdr.length = byteorder_htons((uint16_t)n);
    ipv6_hdr.nextheader = IPV6_PROTO_NUM_NONE;
    ipv6_hdr.hoplimit = 64;
    ipv6_hdr.srcaddr.u64[0] = byteorder_htonll(0xfe80000000000000);
    ipv6_hdr.srcaddr.u32[2] = byteorder_htonl(0x000000ff);
    ipv6_hdr.srcaddr.u16[6] = byteorder_htons(0xfe00);
    ipv6_hdr.srcaddr.u16[7] = byteorder_htons(src);
    ipv6_hdr.destaddr.u64[0] = byteorder_htonll(0xfe80000000000000);
    ipv6_hdr.destaddr.u32[2] = byteorder_htonl(0x000000ff);
    ipv6_hdr.destaddr.u16[6] = byteorder_htons(0xfe00);
    ipv6_hdr.destaddr.u16[7] = byteorder_htons(dest);

    netapi_send_data2(sixlowpan, &ulh, &dest, sizeof(uint16_t), bytes, n);
}
Esempio n. 3
0
void rpl_send_DIO(rpl_dodag_t *mydodag, ipv6_addr_t *destination)
{
#if ENABLE_DEBUG

    if (destination) {
        DEBUGF("Send DIO to %s\n", ipv6_addr_to_str(addr_str, IPV6_MAX_ADDR_STR_LEN, destination));
    }

#endif
    icmp_send_buf = get_rpl_send_icmpv6_buf(ipv6_ext_hdr_len);

    if (mydodag == NULL) {
        DEBUGF("Error - trying to send DIO without being part of a dodag.\n");
        return;
    }

    icmp_send_buf->type = ICMPV6_TYPE_RPL_CONTROL;
    icmp_send_buf->code = ICMP_CODE_DIO;

    rpl_send_dio_buf = get_rpl_send_dio_buf();
    memset(rpl_send_dio_buf, 0, sizeof(*rpl_send_dio_buf));

    DEBUGF("Sending DIO with ");
    rpl_send_dio_buf->rpl_instanceid = mydodag->instance->id;
    DEBUG("instance %02X ", rpl_send_dio_buf->rpl_instanceid);
    rpl_send_dio_buf->version_number = mydodag->version;
    rpl_send_dio_buf->rank = byteorder_htons(mydodag->my_rank);
    DEBUG("rank %04X\n", byteorder_ntohs(rpl_send_dio_buf->rank));
    rpl_send_dio_buf->g_mop_prf = (mydodag->grounded << RPL_GROUNDED_SHIFT) |
                                  (mydodag->mop << RPL_MOP_SHIFT) | mydodag->prf;
    rpl_send_dio_buf->dtsn = mydodag->dtsn;
    rpl_send_dio_buf->flags = 0;
    rpl_send_dio_buf->reserved = 0;
    rpl_send_dio_buf->dodagid = mydodag->dodag_id;

    int opt_hdr_len = 0;
    /* DODAG configuration option */
    rpl_send_opt_dodag_conf_buf = get_rpl_send_opt_dodag_conf_buf(DIO_BASE_LEN);
    rpl_send_opt_dodag_conf_buf->type = RPL_OPT_DODAG_CONF;
    rpl_send_opt_dodag_conf_buf->length = RPL_OPT_DODAG_CONF_LEN;
    rpl_send_opt_dodag_conf_buf->flags_a_pcs = 0;
    rpl_send_opt_dodag_conf_buf->DIOIntDoubl = mydodag->dio_interval_doubling;
    rpl_send_opt_dodag_conf_buf->DIOIntMin = mydodag->dio_min;
    rpl_send_opt_dodag_conf_buf->DIORedun = mydodag->dio_redundancy;
    rpl_send_opt_dodag_conf_buf->MaxRankIncrease = byteorder_htons(mydodag->maxrankincrease);
    rpl_send_opt_dodag_conf_buf->MinHopRankIncrease = byteorder_htons(mydodag->minhoprankincrease);
    rpl_send_opt_dodag_conf_buf->ocp = byteorder_htons(mydodag->of->ocp);
    rpl_send_opt_dodag_conf_buf->reserved = 0;
    rpl_send_opt_dodag_conf_buf->default_lifetime = mydodag->default_lifetime;
    rpl_send_opt_dodag_conf_buf->lifetime_unit = byteorder_htons(mydodag->lifetime_unit);

    opt_hdr_len += RPL_OPT_DODAG_CONF_LEN_WITH_OPT_LEN;

    if (!ipv6_addr_is_unspecified(&mydodag->prefix)) {
        rpl_send_opt_prefix_information_buf = get_rpl_send_opt_prefix_information_buf(DIO_BASE_LEN + opt_hdr_len);
        rpl_send_opt_prefix_information_buf->type = RPL_OPT_PREFIX_INFO;
        rpl_send_opt_prefix_information_buf->length = RPL_OPT_PREFIX_INFO_LEN;
        rpl_send_opt_prefix_information_buf->flags = mydodag->prefix_flags;
        rpl_send_opt_prefix_information_buf->prefix = mydodag->prefix;
        rpl_send_opt_prefix_information_buf->prefix_length = mydodag->prefix_length;
        rpl_send_opt_prefix_information_buf->preferred_lifetime = byteorder_htonl(mydodag->prefix_preferred_lifetime);
        rpl_send_opt_prefix_information_buf->valid_lifetime = byteorder_htonl(mydodag->prefix_valid_lifetime);

        opt_hdr_len += RPL_OPT_PREFIX_INFO_LEN_WITH_OPT_LEN;
    }

    uint16_t plen = ICMPV6_HDR_LEN + DIO_BASE_LEN + opt_hdr_len;
    rpl_send(destination, (uint8_t *)icmp_send_buf, plen, IPV6_PROTO_NUM_ICMPV6);
}
Esempio n. 4
0
            0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, \
            0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f \
        } \
    }

#define DEFAULT_TEST_V_TC_FL    TEST_UINT32
#define DEFAULT_TEST_LEN        TEST_UINT16
#define DEFAULT_TEST_NH         TEST_UINT8
#define DEFAULT_TEST_HL         TEST_UINT8

static gnrc_pktsnip_t *_pkt_w_ip_hdr, *_pkt_no_ip_hdr;

static void set_up(void)
{
    ipv6_hdr_t ip = (ipv6_hdr_t) {
        .v_tc_fl    = byteorder_htonl(DEFAULT_TEST_V_TC_FL),
        .len        = byteorder_htons(DEFAULT_TEST_LEN),
        .nh         = DEFAULT_TEST_NH,
        .hl         = DEFAULT_TEST_HL,
        .src        = DEFAULT_TEST_SRC,
        .dst        = DEFAULT_TEST_DST
    };

    ipv6_hdr_set_version(&ip);

    gnrc_pktbuf_init();
    _pkt_w_ip_hdr = gnrc_pktbuf_add(NULL, NULL, 1, GNRC_NETTYPE_NETIF);
    assert(_pkt_w_ip_hdr);
    _pkt_w_ip_hdr = gnrc_pktbuf_add(_pkt_w_ip_hdr, &ip, sizeof(ipv6_hdr_t), GNRC_NETTYPE_IPV6);
    assert(_pkt_w_ip_hdr);
    _pkt_w_ip_hdr = gnrc_pktbuf_add(_pkt_w_ip_hdr, NULL, 1, GNRC_NETTYPE_UNDEF);