Esempio n. 1
0
File: csp_io.c Progetto: janbre/NUTS
int csp_send(csp_conn_t * conn, csp_packet_t * packet, uint32_t timeout) {

    int ret;

    if ((conn == NULL) || (packet == NULL) || (conn->state != CONN_OPEN)) {
        csp_log_error("Invalid call to csp_send\r\n");
        return 0;
    }

#ifdef CSP_USE_RDP
    if (conn->idout.flags & CSP_FRDP) {
        if (csp_rdp_send(conn, packet, timeout) != CSP_ERR_NONE) {
            csp_route_t * ifout = csp_route_if(conn->idout.dst);
            if (ifout != NULL && ifout->interface != NULL)
                ifout->interface->tx_error++;
            csp_log_warn("RDP send failed\r\n!");
            return 0;
        }
    }
#endif

    ret = csp_send_direct(conn->idout, packet, timeout);

    return (ret == CSP_ERR_NONE) ? 1 : 0;

}
Esempio n. 2
0
uint8_t csp_route_get_nexthop_mac(uint8_t node) {

    csp_route_t * route = csp_route_if(node);
    return route->nexthop_mac_addr;

}
Esempio n. 3
0
File: csp_io.c Progetto: nsat/libcsp
int csp_send_direct(csp_id_t idout, csp_packet_t * packet, uint32_t timeout) {

	if (packet == NULL) {
		csp_log_error("csp_send_direct called with NULL packet\r\n");
		goto err;
	}

	csp_route_t * ifout = csp_route_if(idout.dst);

	if ((ifout == NULL) || (ifout->interface == NULL) || (ifout->interface->nexthop == NULL)) {
		csp_log_error("No route to host: %#08x\r\n", idout.ext);
		goto err;
	}

	csp_log_packet("Output: Src %u, Dst %u, Dport %u, Sport %u, Pri %u, Flags 0x%02X, Size %u VIA: %s\r\n",
		idout.src, idout.dst, idout.dport, idout.sport, idout.pri, idout.flags, packet->length, ifout->interface->name);

	/*
	 * Copy identifier to packet.  This originally happened below (after
	 * encryption), but doing it here makes logging simpler and doesn't
	 * affect the encryption stuff at all.
	 */
	packet->id.ext = idout.ext;

	/* Log the packet as sent by the application (before encryption) */
	if (csp_packet_callback)
		csp_packet_callback(CSP_OUTPUT, ifout->interface->name, packet);

#if 0
#ifdef __linux__
        struct timespec ts;
        clock_gettime(CLOCK_REALTIME, &ts);
        double sec = ts.tv_sec + ts.tv_nsec / 1e9;
#else
        double sec = (float)xTaskGetTickCount() / configTICK_RATE_HZ;
#endif

        printf("%.3f: packet contents (%d bytes):", sec,
            packet->length);

        for (int i = 0; i < packet->length; i++) {
            if (i % 16 == 0) printf("\n");
            printf("%02x ", packet->data[i]);
        }
        printf("\n\n");
#endif

#ifdef CSP_USE_PROMISC
	/* Loopback traffic is added to promisc queue by the router */
	if (idout.dst != my_address && idout.src == my_address) {
		packet->id.ext = idout.ext;
		csp_promisc_add(packet, csp_promisc_queue);
	}
#endif

	/* Only encrypt packets from the current node */
	if (idout.src == my_address) {
		/* Append HMAC */
		if (idout.flags & CSP_FHMAC) {
#ifdef CSP_USE_HMAC
			/* Calculate and add HMAC */
			if (csp_hmac_append(packet) != 0) {
				/* HMAC append failed */
				csp_log_warn("HMAC append failed!\r\n");
				goto tx_err;
			}
#else
			csp_log_warn("Attempt to send packet with HMAC, but CSP was compiled without HMAC support. Discarding packet\r\n");
			goto tx_err;
#endif
		}

		/* Append CRC32 */
		if (idout.flags & CSP_FCRC32) {
#ifdef CSP_USE_CRC32
			/* Calculate and add CRC32 */
			if (csp_crc32_append(packet) != 0) {
				/* CRC32 append failed */
				csp_log_warn("CRC32 append failed!\r\n");
				goto tx_err;
			}
#else
			csp_log_warn("Attempt to send packet with CRC32, but CSP was compiled without CRC32 support. Sending without CRC32r\n");
			idout.flags &= ~(CSP_FCRC32);
#endif
		}

		if (idout.flags & CSP_FXTEA) {
#ifdef CSP_USE_XTEA
			/* Create nonce */
			uint32_t nonce, nonce_n;
			nonce = (uint32_t)rand();
			nonce_n = csp_hton32(nonce);
			memcpy(&packet->data[packet->length], &nonce_n, sizeof(nonce_n));

			/* Create initialization vector */
			uint32_t iv[2] = {nonce, 1};

			/* Encrypt data */
			if (csp_xtea_encrypt(packet->data, packet->length, iv) != 0) {
				/* Encryption failed */
				csp_log_warn("Encryption failed! Discarding packet\r\n");
				goto tx_err;
			}

			packet->length += sizeof(nonce_n);
#else
			csp_log_warn("Attempt to send XTEA encrypted packet, but CSP was compiled without XTEA support. Discarding packet\r\n");
			goto tx_err;
#endif
		}
	}

	/* Store length before passing to interface */
	uint16_t bytes = packet->length;
	uint16_t mtu = ifout->interface->mtu;

	if (mtu > 0 && bytes > mtu) {
		csp_log_warn("Attempt to send a packet larger than the interface's mtu.\r\n");
		goto tx_err;
	}

	/* Log the packet as sent over the interface */
	if (csp_packet_callback)
		csp_packet_callback(CSP_OUTPUT_RAW, ifout->interface->name, packet);

	if ((*ifout->interface->nexthop)(ifout->interface, packet, timeout) != CSP_ERR_NONE)
		goto tx_err;

	/* Update our transmit-time estimates, if the interface supports it */
	if (ifout->interface->tx_ms_per_byte) {
		/*
		 * If the completion time is in the past, that means
		 * the interface is not currently transmitting.
		 */
		uint32_t time_now = csp_get_ms();
		if (csp_time_after(time_now, ifout->interface->tx_done_time))
			ifout->interface->tx_done_time = time_now;

		ifout->interface->tx_done_time +=
			ifout->interface->tx_ms_per_packet
			+ bytes * ifout->interface->tx_ms_per_byte;

#if 0
		printf("DEBUG: now %u, expected completion at %u\n",
			time_now, ifout->interface->tx_done_time);
#endif
	}

	ifout->interface->tx++;
	ifout->interface->txbytes += bytes;
	return CSP_ERR_NONE;

tx_err:
	ifout->interface->tx_error++;
err:
	return CSP_ERR_TX;

}
Esempio n. 4
0
File: csp_io.c Progetto: janbre/NUTS
int csp_send_direct(csp_id_t idout, csp_packet_t * packet, uint32_t timeout) {

    if (packet == NULL) {
        csp_log_error("csp_send_direct called with NULL packet\r\n");
        goto err;
    }

    csp_route_t * ifout = csp_route_if(idout.dst);

    if ((ifout == NULL) || (ifout->interface == NULL) || (ifout->interface->nexthop == NULL)) {
        csp_log_error("No route to host: %#08x\r\n", idout.ext);
        goto err;
    }

    csp_log_packet("Sending packet size %u from %u to %u port %u via interface %s\r\n", packet->length, idout.src, idout.dst, idout.dport, ifout->interface->name);

#ifdef CSP_USE_PROMISC
    /* Loopback traffic is added to promisc queue by the router */
    if (idout.dst != my_address) {
        packet->id.ext = idout.ext;
        csp_promisc_add(packet, csp_promisc_queue);
    }
#endif

    if (idout.flags & CSP_SEQNR) {
#ifdef CSP_USE_SEQNR
        /*Append the gloal seqnr. to the packet	*/
        if(csp_seqnr_append(packet) != 0) {
            /* SEQNR append failed */
            csp_log_warn("SEQNR append failed !\r\n");
            goto tx_err;
        }
#else
        csp_log_warn("Attempt to send packet with SEQNR, but csp was compiled without SEQNR support. Discarding packet\r\n");
        goto tx_err;
#endif
    }



    /* Only encrypt packets from the current node */
    if (idout.src == my_address) {
        /* Append HMAC */
        if (idout.flags & CSP_FHMAC) {
#ifdef CSP_USE_HMAC
            /* Calculate and add HMAC */
            if (csp_hmac_append(packet) != 0) {
                /* HMAC append failed */
                csp_log_warn("HMAC append failed!\r\n");
                goto tx_err;
            }
#else
            csp_log_warn("Attempt to send packet with HMAC, but CSP was compiled without HMAC support. Discarding packet\r\n");
            goto tx_err;
#endif
        }

        /* Append CRC32 */
        if (idout.flags & CSP_FCRC32) {
#ifdef CSP_USE_CRC32
            /* Calculate and add CRC32 */
            if (csp_crc32_append(packet) != 0) {
                /* CRC32 append failed */
                csp_log_warn("CRC32 append failed!\r\n");
                goto tx_err;
            }
#else
            csp_log_warn("Attempt to send packet with CRC32, but CSP was compiled without CRC32 support. Sending without CRC32r\n");
            idout.flags &= ~(CSP_FCRC32);
#endif
        }

        if (idout.flags & CSP_FXTEA) {
#ifdef CSP_USE_XTEA
            /* Create nonce */
            uint32_t nonce, nonce_n;
            nonce = (uint32_t)rand();
            nonce_n = csp_hton32(nonce);
            memcpy(&packet->data[packet->length], &nonce_n, sizeof(nonce_n));

            /* Create initialization vector */
            uint32_t iv[2] = {nonce, 1};

            /* Encrypt data */
            if (csp_xtea_encrypt(packet->data, packet->length, iv) != 0) {
                /* Encryption failed */
                csp_log_warn("Encryption failed! Discarding packet\r\n");
                goto tx_err;
            }

            packet->length += sizeof(nonce_n);
#else
            csp_log_warn("Attempt to send XTEA encrypted packet, but CSP was compiled without XTEA support. Discarding packet\r\n");
            goto tx_err;
#endif
        }
    }

    /* Copy identifier to packet */
    packet->id.ext = idout.ext;

    /* Store length before passing to interface */
    uint16_t bytes = packet->length;
    uint16_t mtu = ifout->interface->mtu;

    if (mtu > 0 && bytes > mtu)
        goto tx_err;

    if ((*ifout->interface->nexthop)(packet, timeout) != CSP_ERR_NONE)
        goto tx_err;

    ifout->interface->tx++;
    ifout->interface->txbytes += bytes;
    return CSP_ERR_NONE;

tx_err:
    ifout->interface->tx_error++;
err:
    return CSP_ERR_TX;

}
Esempio n. 5
0
/**
 * Function to transmit a frame without an existing connection structure.
 * This function is used for stateless transmissions
 * @param idout 32bit CSP identifier
 * @param packet pointer to packet,
 * @param timeout a timeout to wait for TX to complete. NOTE: not all underlying drivers supports flow-control.
 * @return returns 1 if successful and 0 otherwise. you MUST free the frame yourself if the transmission was not successful.
 */
int csp_send_direct(csp_id_t idout, csp_packet_t * packet, unsigned int timeout) {

	if (packet == NULL) {
		csp_debug(CSP_ERROR, "csp_send_direct: packet == NULL\r\n");
		return 0;
	}

	csp_iface_t * ifout = csp_route_if(idout.dst);

	if ((ifout == NULL) || (*ifout->nexthop == NULL)) {
		csp_debug(CSP_ERROR, "No route to host: %#08x\r\n", idout.ext);
		return 0;
	}

	csp_debug(CSP_PACKET, "Sending packet from %u to %u port %u via interface %s\r\n", idout.src, idout.dst, idout.dport, ifout->name);
	ifout->count++;
	
#if CSP_USE_PROMISC
    /* Loopback traffic is added to promisc queue by the router */
    if (idout.dst != my_address) {
        packet->id.ext = idout.ext;
        csp_promisc_add(packet, csp_promisc_queue);
    }
#endif

    /* Only encrypt packets from the current node */
    if (idout.src == my_address && (idout.flags & CSP_FXTEA)) {
#if CSP_ENABLE_XTEA
    	/* Create nonce */
    	uint32_t nonce, nonce_n;
    	nonce = (uint32_t)rand();
    	nonce_n = htonl(nonce);
    	memcpy(&packet->data[packet->length], &nonce_n, sizeof(nonce_n));

    	/* Create initialization vector */
    	uint32_t iv[2] = {nonce, 1};

    	/* Encrypt data */
		if (xtea_encrypt(packet->data, packet->length, (uint32_t *)CSP_CRYPTO_KEY, iv) != 0) {
			/* Encryption failed */
			csp_debug(CSP_WARN, "Encryption failed! Discarding packet\r\n");
			csp_buffer_free(packet);
			return 0;
		}

		packet->length += sizeof(nonce_n);
#else
		csp_debug(CSP_WARN, "Attempt to send XTEA encrypted packet, but CSP was compiled without XTEA support. Discarding packet\r\n");
		return 0;
#endif
    }

    /* Only append HMAC to packets from the current node */
    if (idout.src == my_address && (idout.flags & CSP_FHMAC)) {
#if CSP_ENABLE_HMAC
		/* Calculate and add HMAC */
		if (hmac_append(packet, (uint8_t *)CSP_CRYPTO_KEY, CSP_CRYPTO_KEY_LENGTH) != 0) {
			/* HMAC append failed */
			csp_debug(CSP_WARN, "HMAC append failed!\r\n");
			csp_buffer_free(packet);
			return 0;
		}
#else
		csp_debug(CSP_WARN, "Attempt to send packet with HMAC, but CSP was compiled without HMAC support. Discarding packet\r\n");
		return 0;
#endif
    }

	return (*ifout->nexthop)(idout, packet, timeout);

}