about summary refs log tree commit diff
path: root/shared/n-acd/src/n-acd-private.h
blob: 3f20791234001e5bf432251251b688b4a4cd5263 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#pragma once

#include <c-list.h>
#include <c-rbtree.h>
#include <errno.h>
#include <inttypes.h>
#include <netinet/if_ether.h>
#include <netinet/in.h>
#include <stdbool.h>
#include <stdlib.h>
#include "util/timer.h"
#include "n-acd.h"

typedef struct NAcdEventNode NAcdEventNode;

#define _cleanup_(_x) __attribute__((__cleanup__(_x)))
#define _public_ __attribute__((__visibility__("default")))

/* This augments the error-codes with internal ones that are never exposed. */
enum {
        _N_ACD_INTERNAL = _N_ACD_E_N,

        N_ACD_E_DROPPED,
};

enum {
        N_ACD_PROBE_STATE_PROBING,
        N_ACD_PROBE_STATE_CONFIGURING,
        N_ACD_PROBE_STATE_ANNOUNCING,
        N_ACD_PROBE_STATE_FAILED,
};

struct NAcdConfig {
        int ifindex;
        unsigned int transport;
        uint8_t mac[ETH_ALEN];
        size_t n_mac;
};

#define N_ACD_CONFIG_NULL(_x) {                                                 \
                .transport = _N_ACD_TRANSPORT_N,                                \
        }

struct NAcdProbeConfig {
        struct in_addr ip;
        uint64_t timeout_msecs;
};

#define N_ACD_PROBE_CONFIG_NULL(_x) {                                           \
                .timeout_msecs = N_ACD_TIMEOUT_RFC5227,                         \
        }

struct NAcdEventNode {
        CList acd_link;
        CList probe_link;
        NAcdEvent event;
        uint8_t sender[ETH_ALEN];
        bool is_public : 1;
};

#define N_ACD_EVENT_NODE_NULL(_x) {                                             \
                .acd_link = C_LIST_INIT((_x).acd_link),                         \
                .probe_link = C_LIST_INIT((_x).probe_link),                     \
        }

struct NAcd {
        unsigned long n_refs;
        unsigned int seed;
        int fd_epoll;
        int fd_socket;
        CRBTree ip_tree;
        CList event_list;
        Timer timer;

        /* BPF map */
        int fd_bpf_map;
        size_t n_bpf_map;
        size_t max_bpf_map;

        /* configuration */
        int ifindex;
        uint8_t mac[ETH_ALEN];

        /* flags */
        bool preempted : 1;
};

#define N_ACD_NULL(_x) {                                                        \
                .n_refs = 1,                                                    \
                .fd_epoll = -1,                                                 \
                .fd_socket = -1,                                                \
                .ip_tree = C_RBTREE_INIT,                                       \
                .event_list = C_LIST_INIT((_x).event_list),                     \
                .timer = TIMER_NULL((_x).timer),                                \
                .fd_bpf_map = -1,                                               \
        }

struct NAcdProbe {
        NAcd *acd;
        CRBNode ip_node;
        CList event_list;
        Timeout timeout;

        /* configuration */
        struct in_addr ip;
        uint64_t timeout_multiplier;
        void *userdata;

        /* state */
        unsigned int state;
        unsigned int n_iteration;
        unsigned int defend;
        uint64_t last_defend;
};

#define N_ACD_PROBE_NULL(_x) {                                                  \
                .ip_node = C_RBNODE_INIT((_x).ip_node),                         \
                .event_list = C_LIST_INIT((_x).event_list),                     \
                .timeout = TIMEOUT_INIT((_x).timeout),                          \
                .state = N_ACD_PROBE_STATE_PROBING,                             \
                .defend = N_ACD_DEFEND_NEVER,                                   \
        }

/* events */

int n_acd_event_node_new(NAcdEventNode **nodep);
NAcdEventNode *n_acd_event_node_free(NAcdEventNode *node);

/* contexts */

void n_acd_remember(NAcd *acd, uint64_t now, bool success);
int n_acd_raise(NAcd *acd, NAcdEventNode **nodep, unsigned int event);
int n_acd_send(NAcd *acd, const struct in_addr *tpa, const struct in_addr *spa);
int n_acd_ensure_bpf_map_space(NAcd *acd);

/* probes */

int n_acd_probe_new(NAcdProbe **probep, NAcd *acd, NAcdProbeConfig *config);
int n_acd_probe_raise(NAcdProbe *probe, NAcdEventNode **nodep, unsigned int event);
int n_acd_probe_handle_timeout(NAcdProbe *probe);
int n_acd_probe_handle_packet(NAcdProbe *probe, struct ether_arp *packet, bool hard_conflict);

/* eBPF */

int n_acd_bpf_map_create(int *mapfdp, size_t max_elements);
int n_acd_bpf_map_add(int mapfd, struct in_addr *addr);
int n_acd_bpf_map_remove(int mapfd, struct in_addr *addr);

int n_acd_bpf_compile(int *progfdp, int mapfd, struct ether_addr *mac);

/* inline helpers */

static inline int n_acd_errno(void) {
        /*
         * Compilers continuously warn about uninitialized variables since they
         * cannot deduce that `return -errno;` will always be negative. This
         * small wrapper makes sure compilers figure that out. Use it as
         * replacement for `errno` read access. Yes, it generates worse code,
         * but only marginally and only affects slow-paths.
         */
        return abs(errno) ? : EIO;
}

static inline void n_acd_event_node_freep(NAcdEventNode **node) {
        if (*node)
                n_acd_event_node_free(*node);
}

static inline void n_acd_closep(int *fdp) {
        if (*fdp >= 0)
                close(*fdp);
}