Пример #1
0
int
netx_send_to(
    struct netxsocket_s *rlpsock,
    const netx_addr_t *destaddr,
    uint8_t *data,
    size_t datalen
)
{
    struct pbuf     *pkt;  /* Outgoing packet */
    struct eth_addr *ethaddr_ret;
    struct ip_addr  *ipaddr_ret;
    int              result;
    int              t_begin;
    struct ip_addr   dest_addr;
    ip4addr_t        addr;
    int              arp_index;

    if (!rlpsock || !rlpsock->nativesock)
        return -1;

    /* struct ip_addr *ipaddr, */
    addr = NETI_INADDR(destaddr);
    dest_addr.addr = addr;

    if (!is_multicast(addr)) {
        /* We have the arp queue turned off, so we need to make sure we have a ARP entry  */
        /* else it will fail on first try */
        result = -1;
        arp_index = etharp_find_addr(netxf_default, &dest_addr, &ethaddr_ret, &ipaddr_ret);
        if (arp_index < 0) {
            result = etharp_query(netxf_default, &dest_addr, NULL);
            if (result == ERR_OK) {
                result = -1;
                t_begin = sys_jiffies();
                /* wait 2 seconds (ARP TIMEOUT)*/
                while ((unsigned int)sys_jiffies() - t_begin < 2000) {
                    arp_index = etharp_find_addr(netxf_default, &dest_addr, &ethaddr_ret, &ipaddr_ret);
                    if (arp_index >= 0) {
                        result = 0;
                        break;
                    }
                }
            } else {
                acnlog(LOG_WARNING | LOG_NETI, "netx_send_to : ARP query failure");
                return -1;
            }
        } else
            result = 0;

        if (result != 0) {
            acnlog(LOG_WARNING | LOG_NETI, "netx_send_to : ARP failure: %d.%d.%d.%d",
                   ntohl(addr) >> 24 & 0x000000ff,
                   ntohl(addr) >> 16 & 0x000000ff,
                   ntohl(addr) >> 8 & 0x000000ff,
                   ntohl(addr) & 0x000000ff);
            return result;
        }
Пример #2
0
/*
 * magic_init - Initialize the magic number generator.
 *
 * Randomize our random seed value.  Here we use the fact that
 * this function is called at *truely random* times by the polling
 * and network functions.  Here we only get 16 bits of new random
 * value but we use the previous value to randomize the other 16
 * bits.
 */
void magic_randomize(void) {
  static u32_t last_jiffies;

  if (!magic_randomized) {
    magic_randomized = !0;
    magic_init();
    /* The initialization function also updates the seed. */
  } else {
    /* magic_randomseed += (magic_randomseed << 16) + TM1; */
    magic_randomseed += (sys_jiffies() - last_jiffies); /* XXX */
  }
  last_jiffies = sys_jiffies();
}
/*
 * Randomize our random seed value.  Here we use the fact that
 * this function is called at *truely random* times by the polling
 * and network functions.  Here we only get 16 bits of new random
 * value but we use the previous value to randomize the other 16
 * bits.
 */
void
avRandomize(void)
{
  static u32_t last_jiffies;

  if (!avRandomized) {
    avRandomized = !0;
    avRandomInit();
    /* The initialization function also updates the seed. */
  } else {
    /* avRandomSeed += (avRandomSeed << 16) + TM1; */
    avRandomSeed += (sys_jiffies() - last_jiffies); /* XXX */
  }
  last_jiffies = sys_jiffies();
}
/*
 * Initialize the random number generator.
 *
 * Here we attempt to compute a random number seed but even if
 * it isn't random, we'll randomize it later.
 *
 * The current method uses the fields from the real time clock,
 * the idle process counter, the millisecond counter, and the
 * hardware timer tick counter.  When this is invoked
 * in startup(), then the idle counter and timer values may
 * repeat after each boot and the real time clock may not be
 * operational.  Thus we call it again on the first random
 * event.
 */
void magic_init(void) {
  magic_randomseed += sys_jiffies();
#ifndef LWIP_RAND
  /* Initialize the Borland random number generator. */
  srand((unsigned)magic_randomseed);
#endif /* LWIP_RAND */
}
/*
 * Churn the randomness pool on a random event.  Call this early and often
 *  on random and semi-random system events to build randomness in time for
 *  usage.  For randomly timed events, pass a null pointer and a zero length
 *  and this will use the system timer and other sources to add randomness.
 *  If new random data is available, pass a pointer to that and it will be
 *  included.
 *
 * Ref: Applied Cryptography 2nd Ed. by Bruce Schneier p. 427
 */
static void magic_churnrand(char *rand_data, u32_t rand_len) {
  md5_context md5_ctx;

  /* LWIP_DEBUGF(LOG_INFO, ("magic_churnrand: %u@%P\n", rand_len, rand_data)); */
  md5_starts(&md5_ctx);
  md5_update(&md5_ctx, (u_char *)magic_randpool, sizeof(magic_randpool));
  if (rand_data) {
    md5_update(&md5_ctx, (u_char *)rand_data, rand_len);
  } else {
    struct {
      /* INCLUDE fields for any system sources of randomness */
      u32_t jiffies;
#ifdef LWIP_RAND
      u32_t rand;
#endif /* LWIP_RAND */
    } sys_data;
    magic_randomseed += sys_jiffies();
    sys_data.jiffies = magic_randomseed;
#ifdef LWIP_RAND
    sys_data.rand = LWIP_RAND();
#endif /* LWIP_RAND */
    /* Load sys_data fields here. */
    md5_update(&md5_ctx, (u_char *)&sys_data, sizeof(sys_data));
  }
  md5_finish(&md5_ctx, (u_char *)magic_randpool);
/*  LWIP_DEBUGF(LOG_INFO, ("magic_churnrand: -> 0\n")); */
}
/*
 * Initialize the random number generator.
 *
 * Here we attempt to compute a random number seed but even if
 * it isn't random, we'll randomize it later.
 *
 * The current method uses the fields from the real time clock,
 * the idle process counter, the millisecond counter, and the
 * hardware timer tick counter.  When this is invoked
 * in startup(), then the idle counter and timer values may
 * repeat after each boot and the real time clock may not be
 * operational.  Thus we call it again on the first random
 * event.
 */
void
avRandomInit()
{
#if 0
  /* Get a pointer into the last 4 bytes of clockBuf. */
  u32_t *lptr1 = (u32_t *)((char *)&clockBuf[3]);

  /*
   * Initialize our seed using the real-time clock, the idle
   * counter, the millisecond timer, and the hardware timer
   * tick counter.  The real-time clock and the hardware
   * tick counter are the best sources of randomness but
   * since the tick counter is only 16 bit (and truncated
   * at that), the idle counter and millisecond timer
   * (which may be small values) are added to help
   * randomize the lower 16 bits of the seed.
   */
  readClk();
  avRandomSeed += *(u32_t *)clockBuf + *lptr1 + OSIdleCtr
           + ppp_mtime() + ((u32_t)TM1 << 16) + TM1;
#else
  avRandomSeed += sys_jiffies(); /* XXX */
#endif

  /* Initialize the Borland random number generator. */
  srand((unsigned)avRandomSeed);
}
/*
 * magic_init - Initialize the magic number generator.
 *
 * Randomize our random seed value.  Here we use the fact that
 * this function is called at *truely random* times by the polling
 * and network functions.  Here we only get 16 bits of new random
 * value but we use the previous value to randomize the other 16
 * bits.
 */
void magic_randomize(void) {
#ifndef LWIP_RAND
  if (!magic_randomized) {
    magic_randomized = !0;
    magic_init();
    /* The initialization function also updates the seed. */
  } else {
#endif /* LWIP_RAND */
    magic_randomseed += sys_jiffies();
#ifndef LWIP_RAND
  }
#endif /* LWIP_RAND */
}
Пример #8
0
u32_t sys_now(void)
{
	static u32_t hz;
	u32_t jiffs;

	if (!hz)
		hz = sys_hz();

	/* use ticks not realtime as sys_now() is used to calculate timers */
	jiffs = sys_jiffies();

	return jiffs * (1000 / hz);
}
Пример #9
0
/*
 * Churn the randomness pool on a random event.  Call this early and often
 *  on random and semi-random system events to build randomness in time for
 *  usage.  For randomly timed events, pass a null pointer and a zero length
 *  and this will use the system timer and other sources to add randomness.
 *  If new random data is available, pass a pointer to that and it will be
 *  included.
 *
 * Ref: Applied Cryptography 2nd Ed. by Bruce Schneier p. 427
 */
void magic_churnrand(char *rand_data, u32_t rand_len) {
  md5_context md5;

  /* LWIP_DEBUGF(LOG_INFO, ("magic_churnrand: %u@%P\n", rand_len, rand_data)); */
  md5_starts(&md5);
  md5_update(&md5, (u_char *)magic_randpool, sizeof(magic_randpool));
  if (rand_data) {
    md5_update(&md5, (u_char *)rand_data, rand_len);
  } else {
    struct {
      /* INCLUDE fields for any system sources of randomness */
      u32_t jiffies;
    } sys_data;
    sys_data.jiffies = sys_jiffies();
    /* Load sys_data fields here. */
    md5_update(&md5, (u_char *)&sys_data, sizeof(sys_data));
  }
  md5_finish(&md5, (u_char *)magic_randpool);
/*  LWIP_DEBUGF(LOG_INFO, ("magic_churnrand: -> 0\n")); */
}
Пример #10
0
/*
 * Initialize the random number generator.
 *
 * Here we attempt to compute a random number seed but even if
 * it isn't random, we'll randomize it later.
 *
 * The current method uses the fields from the real time clock,
 * the idle process counter, the millisecond counter, and the
 * hardware timer tick counter.  When this is invoked
 * in startup(), then the idle counter and timer values may
 * repeat after each boot and the real time clock may not be
 * operational.  Thus we call it again on the first random
 * event.
 */
void magic_init() {
  magic_randomseed += sys_jiffies();

  /* Initialize the Borland random number generator. */
  srand((unsigned)magic_randomseed);
}
Пример #11
0
/* Send a packet on the given connection. */
static err_t pppifOutput(struct netif *netif, struct pbuf *pb, struct ip_addr *ipaddr)
{
    int pd = (int)netif->state;
    u_short protocol = PPP_IP;
    PPPControl *pc = &pppControl[pd];
    u_int fcsOut = PPP_INITFCS;
    struct pbuf *headMB = NULL, *tailMB = NULL, *p;
    u_char c;

    (void)ipaddr;

    /* Validate parameters. */
    /* We let any protocol value go through - it can't hurt us
     * and the peer will just drop it if it's not accepting it. */
	if (pd < 0 || pd >= NUM_PPP || !pc->openFlag || !pb) {
        PPPDEBUG((LOG_WARNING, "pppifOutput[%d]: bad parms prot=%d pb=%p\n",
                    pd, protocol, pb));
#if LINK_STATS
		lwip_stats.link.opterr++;
		lwip_stats.link.drop++;
#endif
		return ERR_ARG;
	}

    /* Check that the link is up. */
	if (lcp_phase[pd] == PHASE_DEAD) {
        PPPDEBUG((LOG_ERR, "pppifOutput[%d]: link not up\n", pd));
#if LINK_STATS
		lwip_stats.link.rterr++;
		lwip_stats.link.drop++;
#endif
		return ERR_RTE;
	}

    /* Grab an output buffer. */
	headMB = pbuf_alloc(PBUF_RAW, 0, PBUF_POOL);
    if (headMB == NULL) {
        PPPDEBUG((LOG_WARNING, "pppifOutput[%d]: first alloc fail\n", pd));
#if LINK_STATS
		lwip_stats.link.memerr++;
		lwip_stats.link.drop++;
#endif /* LINK_STATS */
        return ERR_MEM;
    }
        
#if VJ_SUPPORT > 0
    /* 
     * Attempt Van Jacobson header compression if VJ is configured and
     * this is an IP packet. 
     */
    if (protocol == PPP_IP && pc->vjEnabled) {
        switch (vj_compress_tcp(&pc->vjComp, pb)) {
        case TYPE_IP:
            /* No change...
            protocol = PPP_IP_PROTOCOL;
             */
            break;
        case TYPE_COMPRESSED_TCP:
            protocol = PPP_VJC_COMP;
            break;
        case TYPE_UNCOMPRESSED_TCP:
            protocol = PPP_VJC_UNCOMP;
            break;
        default:
            PPPDEBUG((LOG_WARNING, "pppifOutput[%d]: bad IP packet\n", pd));
#if LINK_STATS
			lwip_stats.link.proterr++;
			lwip_stats.link.drop++;
#endif
        	pbuf_free(headMB);
            return ERR_VAL;
        }
    }
#endif
        
    tailMB = headMB;
        
    /* Build the PPP header. */
    if ((sys_jiffies() - pc->lastXMit) >= PPP_MAXIDLEFLAG)
        tailMB = pppAppend(PPP_FLAG, tailMB, NULL);
    pc->lastXMit = sys_jiffies();
    if (!pc->accomp) {
        fcsOut = PPP_FCS(fcsOut, PPP_ALLSTATIONS);
        tailMB = pppAppend(PPP_ALLSTATIONS, tailMB, &pc->outACCM);
        fcsOut = PPP_FCS(fcsOut, PPP_UI);
        tailMB = pppAppend(PPP_UI, tailMB, &pc->outACCM);
    }
    if (!pc->pcomp || protocol > 0xFF) {
        c = (protocol >> 8) & 0xFF;
        fcsOut = PPP_FCS(fcsOut, c);
        tailMB = pppAppend(c, tailMB, &pc->outACCM);
    }