1 /**
2  * @file
3  *
4  * ACD IPv4 Address Conflict Detection
5  */
6 
7 /*
8  *
9  * Copyright (c) 2007 Dominik Spies <kontakt@dspies.de>
10  * Copyright (c) 2018 Jasper Verschueren <jasper.verschueren@apart-audio.com>
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without modification,
14  * are permitted provided that the following conditions are met:
15  *
16  * 1. Redistributions of source code must retain the above copyright notice,
17  *    this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  *    this list of conditions and the following disclaimer in the documentation
20  *    and/or other materials provided with the distribution.
21  * 3. The name of the author may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
25  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
29  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33  * OF SUCH DAMAGE.
34  *
35  * Author: Jasper Verschueren <jasper.verschueren@apart-audio.com>
36  * Author: Dominik Spies <kontakt@dspies.de>
37  */
38 
39 #ifndef LWIP_HDR_ACD_H
40 #define LWIP_HDR_ACD_H
41 
42 #include "lwip/opt.h"
43 
44 /* don't build if not configured for use in lwipopts.h */
45 #if LWIP_IPV4 && LWIP_ACD
46 
47 #include "lwip/netif.h"
48 #include "lwip/etharp.h"
49 #include "lwip/prot/acd.h"
50 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 
55 /** ACD Timing
56  *  ACD_TMR_INTERVAL msecs, I recommend a value of 100.
57  *  The value must divide 1000 with a remainder almost 0. Possible values are
58  *  1000, 500, 333, 250, 200, 166, 142, 125, 111, 100 ....
59  */
60 #define ACD_TMR_INTERVAL      100
61 
62 /**
63  * Callback function: Handle conflict information from ACD module
64  *
65  * @param netif   network interface to handle conflict information on
66  * @param state   acd_callback_enum_t
67  */
68 typedef void (*acd_conflict_callback_t)(struct netif *netif, acd_callback_enum_t state);
69 
70 /** ACD state information per netif */
71 struct acd
72 {
73   /** next acd module */
74   struct acd *next;
75   /** the currently selected, probed, announced or used IP-Address */
76   ip4_addr_t ipaddr;
77   /** current ACD state machine state */
78   acd_state_enum_t state;
79   /** sent number of probes or announces, dependent on state */
80   u8_t sent_num;
81   /** ticks to wait, tick is ACD_TMR_INTERVAL long */
82   u16_t ttw;
83   /** ticks until a conflict can again be solved by defending */
84   u8_t lastconflict;
85   /** total number of probed/used IP-Addresses that resulted in a conflict */
86   u8_t num_conflicts;
87   /** callback function -> let's the acd user know if the address is good or
88       if a conflict is detected */
89   acd_conflict_callback_t acd_conflict_callback;
90 };
91 
92 err_t acd_add(struct netif *netif, struct acd *acd,
93               acd_conflict_callback_t acd_conflict_callback);
94 void acd_remove(struct netif *netif, struct acd *acd);
95 err_t acd_start(struct netif *netif, struct acd *acd, ip4_addr_t ipaddr);
96 err_t acd_stop(struct acd *acd);
97 void acd_arp_reply(struct netif *netif, struct etharp_hdr *hdr);
98 void acd_tmr(void);
99 void acd_network_changed_link_down(struct netif *netif);
100 void acd_netif_ip_addr_changed(struct netif *netif, const ip_addr_t *old_addr,
101                                const ip_addr_t *new_addr);
102 
103 #ifdef __cplusplus
104 }
105 #endif
106 
107 #endif /* LWIP_IPV4 && LWIP_ACD */
108 
109 #endif /* LWIP_HDR_ACD_H */
110