71#include "lwip/ip_addr.h"
80#define AUTOIP_NET 0xA9FE0000
82#define AUTOIP_RANGE_START (AUTOIP_NET | 0x0100)
84#define AUTOIP_RANGE_END (AUTOIP_NET | 0xFEFF)
89#ifndef LWIP_AUTOIP_RAND
90#define LWIP_AUTOIP_RAND(netif) ( (((u32_t)((netif->hwaddr[5]) & 0xff) << 24) | \
91 ((u32_t)((netif->hwaddr[3]) & 0xff) << 16) | \
92 ((u32_t)((netif->hwaddr[2]) & 0xff) << 8) | \
93 ((u32_t)((netif->hwaddr[4]) & 0xff))) + \
94 (netif->autoip?netif->autoip->tried_llipaddr:0))
101#ifndef LWIP_AUTOIP_CREATE_SEED_ADDR
102#define LWIP_AUTOIP_CREATE_SEED_ADDR(netif) \
103 htonl(AUTOIP_RANGE_START + ((u32_t)(((u8_t)(netif->hwaddr[4])) | \
104 ((u32_t)((u8_t)(netif->hwaddr[5]))) << 8)))
108static void autoip_handle_arp_conflict(
struct netif *
netif);
123static void autoip_start_probing(
struct netif *
netif);
133autoip_set_struct(
struct netif *
netif,
struct autoip *autoip)
140 memset(autoip, 0,
sizeof(
struct autoip));
142 netif->autoip = autoip;
152 netif->autoip->tried_llipaddr++;
160autoip_handle_arp_conflict(
struct netif *
netif)
163 unsigned char defend = 1;
166 if (
netif->autoip->lastconflict > 0) {
171 (
"autoip_handle_arp_conflict(): we are defending, but in DEFEND_INTERVAL, retreating\n"));
174 autoip_restart(
netif);
177 (
"autoip_handle_arp_conflict(): we are defend, send ARP Announce\n"));
178 autoip_arp_announce(
netif);
179 netif->autoip->lastconflict = DEFEND_INTERVAL * AUTOIP_TICKS_PER_SECOND;
183 (
"autoip_handle_arp_conflict(): we do not defend, retreating\n"));
185 autoip_restart(
netif);
203 addr +=
netif->autoip->tried_llipaddr;
204 addr = AUTOIP_NET | (addr & 0xffff);
207 if (addr < AUTOIP_RANGE_START) {
208 addr += AUTOIP_RANGE_END - AUTOIP_RANGE_START + 1;
210 if (addr > AUTOIP_RANGE_END) {
211 addr -= AUTOIP_RANGE_END - AUTOIP_RANGE_START + 1;
213 LWIP_ASSERT(
"AUTOIP address not in range", (addr >= AUTOIP_RANGE_START) &&
214 (addr <= AUTOIP_RANGE_END));
233 &
netif->autoip->llipaddr, ARP_REQUEST);
246 &
netif->autoip->llipaddr, ARP_REQUEST);
257 struct autoip *autoip =
netif->autoip;
287 struct autoip *autoip =
netif->autoip;
304 if (autoip ==
NULL) {
307 (
"autoip_start(): starting new AUTOIP client\n"));
308 autoip = (
struct autoip *)
mem_malloc(
sizeof(
struct autoip));
309 if (autoip ==
NULL) {
311 (
"autoip_start(): could not allocate autoip\n"));
314 memset(autoip, 0,
sizeof(
struct autoip));
316 netif->autoip = autoip;
319 autoip->state = AUTOIP_STATE_OFF;
321 autoip->sent_num = 0;
323 autoip->lastconflict = 0;
326 autoip_create_addr(
netif, &(autoip->llipaddr));
327 autoip_start_probing(
netif);
335 struct autoip *autoip =
netif->autoip;
337 autoip->
state = AUTOIP_STATE_PROBING;
338 autoip->sent_num = 0;
348 autoip->ttw = (
u16_t)(LWIP_AUTOIP_RAND(
netif) % (PROBE_WAIT * AUTOIP_TICKS_PER_SECOND));
355 if (autoip->tried_llipaddr > MAX_CONFLICTS) {
356 autoip->ttw = RATE_LIMIT_INTERVAL * AUTOIP_TICKS_PER_SECOND;
371 autoip_start_probing(
netif);
399 if (
netif->autoip->lastconflict > 0) {
400 netif->autoip->lastconflict--;
404 (
"autoip_tmr() AutoIP-State: %"U16_F", ttw=%"U16_F"\n",
408 case AUTOIP_STATE_PROBING:
409 if (
netif->autoip->ttw > 0) {
410 netif->autoip->ttw--;
412 if (
netif->autoip->sent_num >= PROBE_NUM) {
413 netif->autoip->
state = AUTOIP_STATE_ANNOUNCING;
414 netif->autoip->sent_num = 0;
415 netif->autoip->ttw = ANNOUNCE_WAIT * AUTOIP_TICKS_PER_SECOND;
421 autoip_arp_probe(
netif);
423 (
"autoip_tmr() PROBING Sent Probe\n"));
424 netif->autoip->sent_num++;
427 ((PROBE_MAX - PROBE_MIN) * AUTOIP_TICKS_PER_SECOND) ) +
428 PROBE_MIN * AUTOIP_TICKS_PER_SECOND);
433 case AUTOIP_STATE_ANNOUNCING:
434 if (
netif->autoip->ttw > 0) {
435 netif->autoip->ttw--;
437 if (
netif->autoip->sent_num == 0) {
446 autoip_arp_announce(
netif);
448 (
"autoip_tmr() ANNOUNCING Sent Announce\n"));
450 netif->autoip->ttw = ANNOUNCE_INTERVAL * AUTOIP_TICKS_PER_SECOND;
451 netif->autoip->sent_num++;
453 if (
netif->autoip->sent_num >= ANNOUNCE_NUM) {
455 netif->autoip->sent_num = 0;
456 netif->autoip->ttw = 0;
478autoip_arp_reply(
struct netif *
netif,
struct etharp_hdr *hdr)
488 struct eth_addr netifaddr;
497 if ((
netif->autoip->
state == AUTOIP_STATE_PROBING) ||
498 ((
netif->autoip->
state == AUTOIP_STATE_ANNOUNCING) &&
499 (
netif->autoip->sent_num == 0))) {
508 !eth_addr_cmp(&netifaddr, &hdr->shwaddr))) {
510 (
"autoip_arp_reply(): Probe Conflict detected\n"));
511 autoip_restart(
netif);
519 !eth_addr_cmp(&netifaddr, &hdr->shwaddr)) {
521 (
"autoip_arp_reply(): Conflicting ARP-Packet detected\n"));
522 autoip_handle_arp_conflict(
netif);
#define LWIP_DEBUGF(debug, message)
#define LWIP_DBG_LEVEL_WARNING
#define LWIP_ASSERT(message, assertion)
#define ip_addr_cmp(addr1, addr2)
#define ip4_addr1_16(ipaddr)
#define ip4_addr2_16(ipaddr)
#define IP4_ADDR(ipaddr, a, b, c, d)
typedefPACK_STRUCT_END struct ip_addr ip_addr_t
#define ip4_addr3_16(ipaddr)
#define ip_addr_set_zero(ipaddr)
#define IPADDR2_COPY(dest, src)
#define ip4_addr4_16(ipaddr)
#define ip4_addr_set_u32(dest_ipaddr, src_u32)
void * mem_malloc(mem_size_t size)
void netif_set_netmask(struct netif *netif, ip_addr_t *netmask)
struct netif * netif_list
void netif_set_down(struct netif *netif)
void netif_set_gw(struct netif *netif, ip_addr_t *gw)
void netif_set_ipaddr(struct netif *netif, ip_addr_t *ipaddr)
void netif_set_up(struct netif *netif)
#define netif_is_up(netif)
u8_t hwaddr[NETIF_MAX_HWADDR_LEN]