2 * Copyright (c) 2014, Bryan Venteicher <bryanv@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "opt_inet6.h"
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/eventhandler.h>
35 #include <sys/kernel.h>
38 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/refcount.h>
42 #include <sys/rmlock.h>
45 #include <sys/queue.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/sockio.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
54 #include <net/ethernet.h>
56 #include <net/if_var.h>
57 #include <net/if_clone.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
60 #include <net/if_vxlan.h>
61 #include <net/netisr.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/in_var.h>
66 #include <netinet/in_pcb.h>
67 #include <netinet/ip.h>
68 #include <netinet/ip6.h>
69 #include <netinet/ip_var.h>
70 #include <netinet6/ip6_var.h>
71 #include <netinet/udp.h>
72 #include <netinet/udp_var.h>
75 LIST_HEAD(vxlan_softc_head, vxlan_softc);
77 struct vxlan_socket_mc_info {
78 union vxlan_sockaddr vxlsomc_saddr;
79 union vxlan_sockaddr vxlsomc_gaddr;
84 #define VXLAN_SO_MC_MAX_GROUPS 32
86 #define VXLAN_SO_VNI_HASH_SHIFT 6
87 #define VXLAN_SO_VNI_HASH_SIZE (1 << VXLAN_SO_VNI_HASH_SHIFT)
88 #define VXLAN_SO_VNI_HASH(_vni) ((_vni) % VXLAN_SO_VNI_HASH_SIZE)
91 struct socket *vxlso_sock;
92 struct rmlock vxlso_lock;
94 union vxlan_sockaddr vxlso_laddr;
95 LIST_ENTRY(vxlan_socket) vxlso_entry;
96 struct vxlan_softc_head vxlso_vni_hash[VXLAN_SO_VNI_HASH_SIZE];
97 struct vxlan_socket_mc_info vxlso_mc[VXLAN_SO_MC_MAX_GROUPS];
100 #define VXLAN_SO_RLOCK(_vso, _p) rm_rlock(&(_vso)->vxlso_lock, (_p))
101 #define VXLAN_SO_RUNLOCK(_vso, _p) rm_runlock(&(_vso)->vxlso_lock, (_p))
102 #define VXLAN_SO_WLOCK(_vso) rm_wlock(&(_vso)->vxlso_lock)
103 #define VXLAN_SO_WUNLOCK(_vso) rm_wunlock(&(_vso)->vxlso_lock)
104 #define VXLAN_SO_LOCK_ASSERT(_vso) \
105 rm_assert(&(_vso)->vxlso_lock, RA_LOCKED)
106 #define VXLAN_SO_LOCK_WASSERT(_vso) \
107 rm_assert(&(_vso)->vxlso_lock, RA_WLOCKED)
109 #define VXLAN_SO_ACQUIRE(_vso) refcount_acquire(&(_vso)->vxlso_refcnt)
110 #define VXLAN_SO_RELEASE(_vso) refcount_release(&(_vso)->vxlso_refcnt)
112 struct vxlan_ftable_entry {
113 LIST_ENTRY(vxlan_ftable_entry) vxlfe_hash;
114 uint16_t vxlfe_flags;
115 uint8_t vxlfe_mac[ETHER_ADDR_LEN];
116 union vxlan_sockaddr vxlfe_raddr;
120 #define VXLAN_FE_FLAG_DYNAMIC 0x01
121 #define VXLAN_FE_FLAG_STATIC 0x02
123 #define VXLAN_FE_IS_DYNAMIC(_fe) \
124 ((_fe)->vxlfe_flags & VXLAN_FE_FLAG_DYNAMIC)
126 #define VXLAN_SC_FTABLE_SHIFT 9
127 #define VXLAN_SC_FTABLE_SIZE (1 << VXLAN_SC_FTABLE_SHIFT)
128 #define VXLAN_SC_FTABLE_MASK (VXLAN_SC_FTABLE_SIZE - 1)
129 #define VXLAN_SC_FTABLE_HASH(_sc, _mac) \
130 (vxlan_mac_hash(_sc, _mac) % VXLAN_SC_FTABLE_SIZE)
132 LIST_HEAD(vxlan_ftable_head, vxlan_ftable_entry);
134 struct vxlan_statistics {
135 uint32_t ftable_nospace;
136 uint32_t ftable_lock_upgrade_failed;
140 struct ifnet *vxl_ifp;
141 struct vxlan_socket *vxl_sock;
143 union vxlan_sockaddr vxl_src_addr;
144 union vxlan_sockaddr vxl_dst_addr;
146 #define VXLAN_FLAG_INIT 0x0001
147 #define VXLAN_FLAG_TEARDOWN 0x0002
148 #define VXLAN_FLAG_LEARN 0x0004
150 uint32_t vxl_port_hash_key;
151 uint16_t vxl_min_port;
152 uint16_t vxl_max_port;
155 /* Lookup table from MAC address to forwarding entry. */
156 uint32_t vxl_ftable_cnt;
157 uint32_t vxl_ftable_max;
158 uint32_t vxl_ftable_timeout;
159 uint32_t vxl_ftable_hash_key;
160 struct vxlan_ftable_head *vxl_ftable;
162 /* Derived from vxl_dst_addr. */
163 struct vxlan_ftable_entry vxl_default_fe;
165 struct ip_moptions *vxl_im4o;
166 struct ip6_moptions *vxl_im6o;
168 struct rmlock vxl_lock;
169 volatile u_int vxl_refcnt;
172 int vxl_vso_mc_index;
173 struct vxlan_statistics vxl_stats;
174 struct sysctl_oid *vxl_sysctl_node;
175 struct sysctl_ctx_list vxl_sysctl_ctx;
176 struct callout vxl_callout;
177 uint8_t vxl_hwaddr[ETHER_ADDR_LEN];
179 struct ifnet *vxl_mc_ifp;
180 char vxl_mc_ifname[IFNAMSIZ];
181 LIST_ENTRY(vxlan_softc) vxl_entry;
182 LIST_ENTRY(vxlan_softc) vxl_ifdetach_list;
185 #define VXLAN_RLOCK(_sc, _p) rm_rlock(&(_sc)->vxl_lock, (_p))
186 #define VXLAN_RUNLOCK(_sc, _p) rm_runlock(&(_sc)->vxl_lock, (_p))
187 #define VXLAN_WLOCK(_sc) rm_wlock(&(_sc)->vxl_lock)
188 #define VXLAN_WUNLOCK(_sc) rm_wunlock(&(_sc)->vxl_lock)
189 #define VXLAN_LOCK_WOWNED(_sc) rm_wowned(&(_sc)->vxl_lock)
190 #define VXLAN_LOCK_ASSERT(_sc) rm_assert(&(_sc)->vxl_lock, RA_LOCKED)
191 #define VXLAN_LOCK_WASSERT(_sc) rm_assert(&(_sc)->vxl_lock, RA_WLOCKED)
192 #define VXLAN_UNLOCK(_sc, _p) do { \
193 if (VXLAN_LOCK_WOWNED(_sc)) \
194 VXLAN_WUNLOCK(_sc); \
196 VXLAN_RUNLOCK(_sc, _p); \
199 #define VXLAN_ACQUIRE(_sc) refcount_acquire(&(_sc)->vxl_refcnt)
200 #define VXLAN_RELEASE(_sc) refcount_release(&(_sc)->vxl_refcnt)
202 #define satoconstsin(sa) ((const struct sockaddr_in *)(sa))
203 #define satoconstsin6(sa) ((const struct sockaddr_in6 *)(sa))
206 struct udphdr vxlh_udp;
207 struct vxlan_header vxlh_hdr;
210 static int vxlan_ftable_addr_cmp(const uint8_t *, const uint8_t *);
211 static void vxlan_ftable_init(struct vxlan_softc *);
212 static void vxlan_ftable_fini(struct vxlan_softc *);
213 static void vxlan_ftable_flush(struct vxlan_softc *, int);
214 static void vxlan_ftable_expire(struct vxlan_softc *);
215 static int vxlan_ftable_update_locked(struct vxlan_softc *,
216 const struct sockaddr *, const uint8_t *,
217 struct rm_priotracker *);
218 static int vxlan_ftable_update(struct vxlan_softc *,
219 const struct sockaddr *, const uint8_t *);
220 static int vxlan_ftable_sysctl_dump(SYSCTL_HANDLER_ARGS);
222 static struct vxlan_ftable_entry *
223 vxlan_ftable_entry_alloc(void);
224 static void vxlan_ftable_entry_free(struct vxlan_ftable_entry *);
225 static void vxlan_ftable_entry_init(struct vxlan_softc *,
226 struct vxlan_ftable_entry *, const uint8_t *,
227 const struct sockaddr *, uint32_t);
228 static void vxlan_ftable_entry_destroy(struct vxlan_softc *,
229 struct vxlan_ftable_entry *);
230 static int vxlan_ftable_entry_insert(struct vxlan_softc *,
231 struct vxlan_ftable_entry *);
232 static struct vxlan_ftable_entry *
233 vxlan_ftable_entry_lookup(struct vxlan_softc *,
235 static void vxlan_ftable_entry_dump(struct vxlan_ftable_entry *,
238 static struct vxlan_socket *
239 vxlan_socket_alloc(const union vxlan_sockaddr *);
240 static void vxlan_socket_destroy(struct vxlan_socket *);
241 static void vxlan_socket_release(struct vxlan_socket *);
242 static struct vxlan_socket *
243 vxlan_socket_lookup(union vxlan_sockaddr *vxlsa);
244 static void vxlan_socket_insert(struct vxlan_socket *);
245 static int vxlan_socket_init(struct vxlan_socket *, struct ifnet *);
246 static int vxlan_socket_bind(struct vxlan_socket *, struct ifnet *);
247 static int vxlan_socket_create(struct ifnet *, int,
248 const union vxlan_sockaddr *, struct vxlan_socket **);
249 static void vxlan_socket_ifdetach(struct vxlan_socket *,
250 struct ifnet *, struct vxlan_softc_head *);
252 static struct vxlan_socket *
253 vxlan_socket_mc_lookup(const union vxlan_sockaddr *);
254 static int vxlan_sockaddr_mc_info_match(
255 const struct vxlan_socket_mc_info *,
256 const union vxlan_sockaddr *,
257 const union vxlan_sockaddr *, int);
258 static int vxlan_socket_mc_join_group(struct vxlan_socket *,
259 const union vxlan_sockaddr *, const union vxlan_sockaddr *,
260 int *, union vxlan_sockaddr *);
261 static int vxlan_socket_mc_leave_group(struct vxlan_socket *,
262 const union vxlan_sockaddr *,
263 const union vxlan_sockaddr *, int);
264 static int vxlan_socket_mc_add_group(struct vxlan_socket *,
265 const union vxlan_sockaddr *, const union vxlan_sockaddr *,
267 static void vxlan_socket_mc_release_group_by_idx(struct vxlan_socket *,
270 static struct vxlan_softc *
271 vxlan_socket_lookup_softc_locked(struct vxlan_socket *,
273 static struct vxlan_softc *
274 vxlan_socket_lookup_softc(struct vxlan_socket *, uint32_t);
275 static int vxlan_socket_insert_softc(struct vxlan_socket *,
276 struct vxlan_softc *);
277 static void vxlan_socket_remove_softc(struct vxlan_socket *,
278 struct vxlan_softc *);
280 static struct ifnet *
281 vxlan_multicast_if_ref(struct vxlan_softc *, int);
282 static void vxlan_free_multicast(struct vxlan_softc *);
283 static int vxlan_setup_multicast_interface(struct vxlan_softc *);
285 static int vxlan_setup_multicast(struct vxlan_softc *);
286 static int vxlan_setup_socket(struct vxlan_softc *);
287 static void vxlan_setup_interface(struct vxlan_softc *);
288 static int vxlan_valid_init_config(struct vxlan_softc *);
289 static void vxlan_init_wait(struct vxlan_softc *);
290 static void vxlan_init_complete(struct vxlan_softc *);
291 static void vxlan_init(void *);
292 static void vxlan_release(struct vxlan_softc *);
293 static void vxlan_teardown_wait(struct vxlan_softc *);
294 static void vxlan_teardown_complete(struct vxlan_softc *);
295 static void vxlan_teardown_locked(struct vxlan_softc *);
296 static void vxlan_teardown(struct vxlan_softc *);
297 static void vxlan_ifdetach(struct vxlan_softc *, struct ifnet *,
298 struct vxlan_softc_head *);
299 static void vxlan_timer(void *);
301 static int vxlan_ctrl_get_config(struct vxlan_softc *, void *);
302 static int vxlan_ctrl_set_vni(struct vxlan_softc *, void *);
303 static int vxlan_ctrl_set_local_addr(struct vxlan_softc *, void *);
304 static int vxlan_ctrl_set_remote_addr(struct vxlan_softc *, void *);
305 static int vxlan_ctrl_set_local_port(struct vxlan_softc *, void *);
306 static int vxlan_ctrl_set_remote_port(struct vxlan_softc *, void *);
307 static int vxlan_ctrl_set_port_range(struct vxlan_softc *, void *);
308 static int vxlan_ctrl_set_ftable_timeout(struct vxlan_softc *, void *);
309 static int vxlan_ctrl_set_ftable_max(struct vxlan_softc *, void *);
310 static int vxlan_ctrl_set_multicast_if(struct vxlan_softc * , void *);
311 static int vxlan_ctrl_set_ttl(struct vxlan_softc *, void *);
312 static int vxlan_ctrl_set_learn(struct vxlan_softc *, void *);
313 static int vxlan_ctrl_ftable_entry_add(struct vxlan_softc *, void *);
314 static int vxlan_ctrl_ftable_entry_rem(struct vxlan_softc *, void *);
315 static int vxlan_ctrl_flush(struct vxlan_softc *, void *);
316 static int vxlan_ioctl_drvspec(struct vxlan_softc *,
317 struct ifdrv *, int);
318 static int vxlan_ioctl_ifflags(struct vxlan_softc *);
319 static int vxlan_ioctl(struct ifnet *, u_long, caddr_t);
321 #if defined(INET) || defined(INET6)
322 static uint16_t vxlan_pick_source_port(struct vxlan_softc *, struct mbuf *);
323 static void vxlan_encap_header(struct vxlan_softc *, struct mbuf *,
324 int, uint16_t, uint16_t);
326 static int vxlan_encap4(struct vxlan_softc *,
327 const union vxlan_sockaddr *, struct mbuf *);
328 static int vxlan_encap6(struct vxlan_softc *,
329 const union vxlan_sockaddr *, struct mbuf *);
330 static int vxlan_transmit(struct ifnet *, struct mbuf *);
331 static void vxlan_qflush(struct ifnet *);
332 static void vxlan_rcv_udp_packet(struct mbuf *, int, struct inpcb *,
333 const struct sockaddr *, void *);
334 static int vxlan_input(struct vxlan_socket *, uint32_t, struct mbuf **,
335 const struct sockaddr *);
337 static void vxlan_set_default_config(struct vxlan_softc *);
338 static int vxlan_set_user_config(struct vxlan_softc *,
339 struct ifvxlanparam *);
340 static int vxlan_clone_create(struct if_clone *, int, caddr_t);
341 static void vxlan_clone_destroy(struct ifnet *);
343 static uint32_t vxlan_mac_hash(struct vxlan_softc *, const uint8_t *);
344 static void vxlan_fakeaddr(struct vxlan_softc *);
346 static int vxlan_sockaddr_cmp(const union vxlan_sockaddr *,
347 const struct sockaddr *);
348 static void vxlan_sockaddr_copy(union vxlan_sockaddr *,
349 const struct sockaddr *);
350 static int vxlan_sockaddr_in_equal(const union vxlan_sockaddr *,
351 const struct sockaddr *);
352 static void vxlan_sockaddr_in_copy(union vxlan_sockaddr *,
353 const struct sockaddr *);
354 static int vxlan_sockaddr_supported(const union vxlan_sockaddr *, int);
355 static int vxlan_sockaddr_in_any(const union vxlan_sockaddr *);
356 static int vxlan_sockaddr_in_multicast(const union vxlan_sockaddr *);
358 static int vxlan_can_change_config(struct vxlan_softc *);
359 static int vxlan_check_vni(uint32_t);
360 static int vxlan_check_ttl(int);
361 static int vxlan_check_ftable_timeout(uint32_t);
362 static int vxlan_check_ftable_max(uint32_t);
364 static void vxlan_sysctl_setup(struct vxlan_softc *);
365 static void vxlan_sysctl_destroy(struct vxlan_softc *);
366 static int vxlan_tunable_int(struct vxlan_softc *, const char *, int);
368 static void vxlan_ifdetach_event(void *, struct ifnet *);
369 static void vxlan_load(void);
370 static void vxlan_unload(void);
371 static int vxlan_modevent(module_t, int, void *);
373 static const char vxlan_name[] = "vxlan";
374 static MALLOC_DEFINE(M_VXLAN, vxlan_name,
375 "Virtual eXtensible LAN Interface");
376 static struct if_clone *vxlan_cloner;
377 static struct mtx vxlan_list_mtx;
378 static LIST_HEAD(, vxlan_socket) vxlan_socket_list;
380 static eventhandler_tag vxlan_ifdetach_event_tag;
382 SYSCTL_DECL(_net_link);
383 SYSCTL_NODE(_net_link, OID_AUTO, vxlan, CTLFLAG_RW, 0,
384 "Virtual eXtensible Local Area Network");
386 static int vxlan_legacy_port = 0;
387 TUNABLE_INT("net.link.vxlan.legacy_port", &vxlan_legacy_port);
388 static int vxlan_reuse_port = 0;
389 TUNABLE_INT("net.link.vxlan.reuse_port", &vxlan_reuse_port);
391 /* Default maximum number of addresses in the forwarding table. */
392 #ifndef VXLAN_FTABLE_MAX
393 #define VXLAN_FTABLE_MAX 2000
396 /* Timeout (in seconds) of addresses learned in the forwarding table. */
397 #ifndef VXLAN_FTABLE_TIMEOUT
398 #define VXLAN_FTABLE_TIMEOUT (20 * 60)
402 * Maximum timeout (in seconds) of addresses learned in the forwarding
405 #ifndef VXLAN_FTABLE_MAX_TIMEOUT
406 #define VXLAN_FTABLE_MAX_TIMEOUT (60 * 60 * 24)
409 /* Number of seconds between pruning attempts of the forwarding table. */
410 #ifndef VXLAN_FTABLE_PRUNE
411 #define VXLAN_FTABLE_PRUNE (5 * 60)
414 static int vxlan_ftable_prune_period = VXLAN_FTABLE_PRUNE;
416 struct vxlan_control {
417 int (*vxlc_func)(struct vxlan_softc *, void *);
420 #define VXLAN_CTRL_FLAG_COPYIN 0x01
421 #define VXLAN_CTRL_FLAG_COPYOUT 0x02
422 #define VXLAN_CTRL_FLAG_SUSER 0x04
425 static const struct vxlan_control vxlan_control_table[] = {
426 [VXLAN_CMD_GET_CONFIG] =
427 { vxlan_ctrl_get_config, sizeof(struct ifvxlancfg),
428 VXLAN_CTRL_FLAG_COPYOUT
431 [VXLAN_CMD_SET_VNI] =
432 { vxlan_ctrl_set_vni, sizeof(struct ifvxlancmd),
433 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
436 [VXLAN_CMD_SET_LOCAL_ADDR] =
437 { vxlan_ctrl_set_local_addr, sizeof(struct ifvxlancmd),
438 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
441 [VXLAN_CMD_SET_REMOTE_ADDR] =
442 { vxlan_ctrl_set_remote_addr, sizeof(struct ifvxlancmd),
443 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
446 [VXLAN_CMD_SET_LOCAL_PORT] =
447 { vxlan_ctrl_set_local_port, sizeof(struct ifvxlancmd),
448 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
451 [VXLAN_CMD_SET_REMOTE_PORT] =
452 { vxlan_ctrl_set_remote_port, sizeof(struct ifvxlancmd),
453 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
456 [VXLAN_CMD_SET_PORT_RANGE] =
457 { vxlan_ctrl_set_port_range, sizeof(struct ifvxlancmd),
458 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
461 [VXLAN_CMD_SET_FTABLE_TIMEOUT] =
462 { vxlan_ctrl_set_ftable_timeout, sizeof(struct ifvxlancmd),
463 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
466 [VXLAN_CMD_SET_FTABLE_MAX] =
467 { vxlan_ctrl_set_ftable_max, sizeof(struct ifvxlancmd),
468 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
471 [VXLAN_CMD_SET_MULTICAST_IF] =
472 { vxlan_ctrl_set_multicast_if, sizeof(struct ifvxlancmd),
473 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
476 [VXLAN_CMD_SET_TTL] =
477 { vxlan_ctrl_set_ttl, sizeof(struct ifvxlancmd),
478 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
481 [VXLAN_CMD_SET_LEARN] =
482 { vxlan_ctrl_set_learn, sizeof(struct ifvxlancmd),
483 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
486 [VXLAN_CMD_FTABLE_ENTRY_ADD] =
487 { vxlan_ctrl_ftable_entry_add, sizeof(struct ifvxlancmd),
488 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
491 [VXLAN_CMD_FTABLE_ENTRY_REM] =
492 { vxlan_ctrl_ftable_entry_rem, sizeof(struct ifvxlancmd),
493 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
497 { vxlan_ctrl_flush, sizeof(struct ifvxlancmd),
498 VXLAN_CTRL_FLAG_COPYIN | VXLAN_CTRL_FLAG_SUSER,
502 static const int vxlan_control_table_size = nitems(vxlan_control_table);
505 vxlan_ftable_addr_cmp(const uint8_t *a, const uint8_t *b)
509 for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++)
510 d = ((int)a[i]) - ((int)b[i]);
516 vxlan_ftable_init(struct vxlan_softc *sc)
520 sc->vxl_ftable = malloc(sizeof(struct vxlan_ftable_head) *
521 VXLAN_SC_FTABLE_SIZE, M_VXLAN, M_ZERO | M_WAITOK);
523 for (i = 0; i < VXLAN_SC_FTABLE_SIZE; i++)
524 LIST_INIT(&sc->vxl_ftable[i]);
525 sc->vxl_ftable_hash_key = arc4random();
529 vxlan_ftable_fini(struct vxlan_softc *sc)
533 for (i = 0; i < VXLAN_SC_FTABLE_SIZE; i++) {
534 KASSERT(LIST_EMPTY(&sc->vxl_ftable[i]),
535 ("%s: vxlan %p ftable[%d] not empty", __func__, sc, i));
537 MPASS(sc->vxl_ftable_cnt == 0);
539 free(sc->vxl_ftable, M_VXLAN);
540 sc->vxl_ftable = NULL;
544 vxlan_ftable_flush(struct vxlan_softc *sc, int all)
546 struct vxlan_ftable_entry *fe, *tfe;
549 for (i = 0; i < VXLAN_SC_FTABLE_SIZE; i++) {
550 LIST_FOREACH_SAFE(fe, &sc->vxl_ftable[i], vxlfe_hash, tfe) {
551 if (all || VXLAN_FE_IS_DYNAMIC(fe))
552 vxlan_ftable_entry_destroy(sc, fe);
558 vxlan_ftable_expire(struct vxlan_softc *sc)
560 struct vxlan_ftable_entry *fe, *tfe;
563 VXLAN_LOCK_WASSERT(sc);
565 for (i = 0; i < VXLAN_SC_FTABLE_SIZE; i++) {
566 LIST_FOREACH_SAFE(fe, &sc->vxl_ftable[i], vxlfe_hash, tfe) {
567 if (VXLAN_FE_IS_DYNAMIC(fe) &&
568 time_uptime >= fe->vxlfe_expire)
569 vxlan_ftable_entry_destroy(sc, fe);
575 vxlan_ftable_update_locked(struct vxlan_softc *sc, const struct sockaddr *sa,
576 const uint8_t *mac, struct rm_priotracker *tracker)
578 union vxlan_sockaddr vxlsa;
579 struct vxlan_ftable_entry *fe;
582 VXLAN_LOCK_ASSERT(sc);
586 * A forwarding entry for this MAC address might already exist. If
587 * so, update it, otherwise create a new one. We may have to upgrade
588 * the lock if we have to change or create an entry.
590 fe = vxlan_ftable_entry_lookup(sc, mac);
592 fe->vxlfe_expire = time_uptime + sc->vxl_ftable_timeout;
594 if (!VXLAN_FE_IS_DYNAMIC(fe) ||
595 vxlan_sockaddr_in_equal(&fe->vxlfe_raddr, sa))
597 if (!VXLAN_LOCK_WOWNED(sc)) {
598 VXLAN_RUNLOCK(sc, tracker);
600 sc->vxl_stats.ftable_lock_upgrade_failed++;
603 vxlan_sockaddr_in_copy(&fe->vxlfe_raddr, sa);
607 if (!VXLAN_LOCK_WOWNED(sc)) {
608 VXLAN_RUNLOCK(sc, tracker);
610 sc->vxl_stats.ftable_lock_upgrade_failed++;
614 if (sc->vxl_ftable_cnt >= sc->vxl_ftable_max) {
615 sc->vxl_stats.ftable_nospace++;
619 fe = vxlan_ftable_entry_alloc();
624 * The source port may be randomly select by the remove host, so
625 * use the port of the default destination address.
627 vxlan_sockaddr_copy(&vxlsa, sa);
628 vxlsa.in4.sin_port = sc->vxl_dst_addr.in4.sin_port;
630 vxlan_ftable_entry_init(sc, fe, mac, &vxlsa.sa,
631 VXLAN_FE_FLAG_DYNAMIC);
633 /* The prior lookup failed, so the insert should not. */
634 error = vxlan_ftable_entry_insert(sc, fe);
641 vxlan_ftable_update(struct vxlan_softc *sc, const struct sockaddr *sa,
644 struct rm_priotracker tracker;
647 VXLAN_RLOCK(sc, &tracker);
648 error = vxlan_ftable_update_locked(sc, sa, mac, &tracker);
649 VXLAN_UNLOCK(sc, &tracker);
655 vxlan_ftable_sysctl_dump(SYSCTL_HANDLER_ARGS)
657 struct rm_priotracker tracker;
659 struct vxlan_softc *sc;
660 struct vxlan_ftable_entry *fe;
665 * This is mostly intended for debugging during development. It is
666 * not practical to dump an entire large table this way.
670 size = PAGE_SIZE; /* Calculate later. */
672 sbuf_new(&sb, NULL, size, SBUF_FIXEDLEN);
673 sbuf_putc(&sb, '\n');
675 VXLAN_RLOCK(sc, &tracker);
676 for (i = 0; i < VXLAN_SC_FTABLE_SIZE; i++) {
677 LIST_FOREACH(fe, &sc->vxl_ftable[i], vxlfe_hash) {
678 if (sbuf_error(&sb) != 0)
680 vxlan_ftable_entry_dump(fe, &sb);
683 VXLAN_RUNLOCK(sc, &tracker);
685 if (sbuf_len(&sb) == 1)
689 error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
695 static struct vxlan_ftable_entry *
696 vxlan_ftable_entry_alloc(void)
698 struct vxlan_ftable_entry *fe;
700 fe = malloc(sizeof(*fe), M_VXLAN, M_ZERO | M_NOWAIT);
706 vxlan_ftable_entry_free(struct vxlan_ftable_entry *fe)
713 vxlan_ftable_entry_init(struct vxlan_softc *sc, struct vxlan_ftable_entry *fe,
714 const uint8_t *mac, const struct sockaddr *sa, uint32_t flags)
717 fe->vxlfe_flags = flags;
718 fe->vxlfe_expire = time_uptime + sc->vxl_ftable_timeout;
719 memcpy(fe->vxlfe_mac, mac, ETHER_ADDR_LEN);
720 vxlan_sockaddr_copy(&fe->vxlfe_raddr, sa);
724 vxlan_ftable_entry_destroy(struct vxlan_softc *sc,
725 struct vxlan_ftable_entry *fe)
728 sc->vxl_ftable_cnt--;
729 LIST_REMOVE(fe, vxlfe_hash);
730 vxlan_ftable_entry_free(fe);
734 vxlan_ftable_entry_insert(struct vxlan_softc *sc,
735 struct vxlan_ftable_entry *fe)
737 struct vxlan_ftable_entry *lfe;
741 VXLAN_LOCK_WASSERT(sc);
742 hash = VXLAN_SC_FTABLE_HASH(sc, fe->vxlfe_mac);
744 lfe = LIST_FIRST(&sc->vxl_ftable[hash]);
746 LIST_INSERT_HEAD(&sc->vxl_ftable[hash], fe, vxlfe_hash);
751 dir = vxlan_ftable_addr_cmp(fe->vxlfe_mac, lfe->vxlfe_mac);
755 LIST_INSERT_BEFORE(lfe, fe, vxlfe_hash);
757 } else if (LIST_NEXT(lfe, vxlfe_hash) == NULL) {
758 LIST_INSERT_AFTER(lfe, fe, vxlfe_hash);
761 lfe = LIST_NEXT(lfe, vxlfe_hash);
762 } while (lfe != NULL);
765 sc->vxl_ftable_cnt++;
770 static struct vxlan_ftable_entry *
771 vxlan_ftable_entry_lookup(struct vxlan_softc *sc, const uint8_t *mac)
773 struct vxlan_ftable_entry *fe;
777 VXLAN_LOCK_ASSERT(sc);
778 hash = VXLAN_SC_FTABLE_HASH(sc, mac);
780 LIST_FOREACH(fe, &sc->vxl_ftable[hash], vxlfe_hash) {
781 dir = vxlan_ftable_addr_cmp(fe->vxlfe_mac, mac);
792 vxlan_ftable_entry_dump(struct vxlan_ftable_entry *fe, struct sbuf *sb)
795 const union vxlan_sockaddr *sa;
797 int i, len, af, width;
799 sa = &fe->vxlfe_raddr;
800 af = sa->sa.sa_family;
803 sbuf_printf(sb, "%c 0x%02X ", VXLAN_FE_IS_DYNAMIC(fe) ? 'D' : 'S',
806 for (i = 0; i < ETHER_ADDR_LEN - 1; i++)
807 sbuf_printf(sb, "%02X:", fe->vxlfe_mac[i]);
808 sbuf_printf(sb, "%02X ", fe->vxlfe_mac[i]);
811 addr = &sa->in4.sin_addr;
812 width = INET_ADDRSTRLEN - 1;
814 addr = &sa->in6.sin6_addr;
815 width = INET6_ADDRSTRLEN - 1;
817 inet_ntop(af, addr, buf, sizeof(buf));
818 sbuf_printf(sb, "%*s ", width, buf);
820 sbuf_printf(sb, "%08jd", (intmax_t)fe->vxlfe_expire);
824 /* Truncate a partial line. */
825 if (sbuf_error(sb) != 0)
826 sbuf_setpos(sb, len);
829 static struct vxlan_socket *
830 vxlan_socket_alloc(const union vxlan_sockaddr *sa)
832 struct vxlan_socket *vso;
835 vso = malloc(sizeof(*vso), M_VXLAN, M_WAITOK | M_ZERO);
836 rm_init(&vso->vxlso_lock, "vxlansorm");
837 refcount_init(&vso->vxlso_refcnt, 0);
838 for (i = 0; i < VXLAN_SO_VNI_HASH_SIZE; i++)
839 LIST_INIT(&vso->vxlso_vni_hash[i]);
840 vso->vxlso_laddr = *sa;
846 vxlan_socket_destroy(struct vxlan_socket *vso)
849 struct vxlan_socket_mc_info *mc;
852 for (i = 0; i < VXLAN_SO_MC_MAX_GROUPS; i++) {
853 mc = &vso->vxlso_mc[i];
854 KASSERT(mc->vxlsomc_gaddr.sa.sa_family == AF_UNSPEC,
855 ("%s: socket %p mc[%d] still has address",
859 for (i = 0; i < VXLAN_SO_VNI_HASH_SIZE; i++) {
860 KASSERT(LIST_EMPTY(&vso->vxlso_vni_hash[i]),
861 ("%s: socket %p vni_hash[%d] not empty",
865 so = vso->vxlso_sock;
867 vso->vxlso_sock = NULL;
871 rm_destroy(&vso->vxlso_lock);
876 vxlan_socket_release(struct vxlan_socket *vso)
880 mtx_lock(&vxlan_list_mtx);
881 destroy = VXLAN_SO_RELEASE(vso);
883 LIST_REMOVE(vso, vxlso_entry);
884 mtx_unlock(&vxlan_list_mtx);
887 vxlan_socket_destroy(vso);
890 static struct vxlan_socket *
891 vxlan_socket_lookup(union vxlan_sockaddr *vxlsa)
893 struct vxlan_socket *vso;
895 mtx_lock(&vxlan_list_mtx);
896 LIST_FOREACH(vso, &vxlan_socket_list, vxlso_entry) {
897 if (vxlan_sockaddr_cmp(&vso->vxlso_laddr, &vxlsa->sa) == 0) {
898 VXLAN_SO_ACQUIRE(vso);
902 mtx_unlock(&vxlan_list_mtx);
908 vxlan_socket_insert(struct vxlan_socket *vso)
911 mtx_lock(&vxlan_list_mtx);
912 VXLAN_SO_ACQUIRE(vso);
913 LIST_INSERT_HEAD(&vxlan_socket_list, vso, vxlso_entry);
914 mtx_unlock(&vxlan_list_mtx);
918 vxlan_socket_init(struct vxlan_socket *vso, struct ifnet *ifp)
925 error = socreate(vso->vxlso_laddr.sa.sa_family, &vso->vxlso_sock,
926 SOCK_DGRAM, IPPROTO_UDP, td->td_ucred, td);
928 if_printf(ifp, "cannot create socket: %d\n", error);
932 error = udp_set_kernel_tunneling(vso->vxlso_sock,
933 vxlan_rcv_udp_packet, vso);
935 if_printf(ifp, "cannot set tunneling function: %d\n", error);
939 if (vxlan_reuse_port != 0) {
943 bzero(&sopt, sizeof(sopt));
944 sopt.sopt_dir = SOPT_SET;
945 sopt.sopt_level = IPPROTO_IP;
946 sopt.sopt_name = SO_REUSEPORT;
947 sopt.sopt_val = &val;
948 sopt.sopt_valsize = sizeof(val);
949 error = sosetopt(vso->vxlso_sock, &sopt);
952 "cannot set REUSEADDR socket opt: %d\n", error);
961 vxlan_socket_bind(struct vxlan_socket *vso, struct ifnet *ifp)
963 union vxlan_sockaddr laddr;
968 laddr = vso->vxlso_laddr;
970 error = sobind(vso->vxlso_sock, &laddr.sa, td);
972 if (error != EADDRINUSE)
973 if_printf(ifp, "cannot bind socket: %d\n", error);
981 vxlan_socket_create(struct ifnet *ifp, int multicast,
982 const union vxlan_sockaddr *saddr, struct vxlan_socket **vsop)
984 union vxlan_sockaddr laddr;
985 struct vxlan_socket *vso;
991 * If this socket will be multicast, then only the local port
992 * must be specified when binding.
994 if (multicast != 0) {
995 if (VXLAN_SOCKADDR_IS_IPV4(&laddr))
996 laddr.in4.sin_addr.s_addr = INADDR_ANY;
999 laddr.in6.sin6_addr = in6addr_any;
1003 vso = vxlan_socket_alloc(&laddr);
1007 error = vxlan_socket_init(vso, ifp);
1011 error = vxlan_socket_bind(vso, ifp);
1016 * There is a small window between the bind completing and
1017 * inserting the socket, so that a concurrent create may fail.
1018 * Let's not worry about that for now.
1020 vxlan_socket_insert(vso);
1026 vxlan_socket_destroy(vso);
1032 vxlan_socket_ifdetach(struct vxlan_socket *vso, struct ifnet *ifp,
1033 struct vxlan_softc_head *list)
1035 struct rm_priotracker tracker;
1036 struct vxlan_softc *sc;
1039 VXLAN_SO_RLOCK(vso, &tracker);
1040 for (i = 0; i < VXLAN_SO_VNI_HASH_SIZE; i++) {
1041 LIST_FOREACH(sc, &vso->vxlso_vni_hash[i], vxl_entry)
1042 vxlan_ifdetach(sc, ifp, list);
1044 VXLAN_SO_RUNLOCK(vso, &tracker);
1047 static struct vxlan_socket *
1048 vxlan_socket_mc_lookup(const union vxlan_sockaddr *vxlsa)
1050 struct vxlan_socket *vso;
1051 union vxlan_sockaddr laddr;
1055 if (VXLAN_SOCKADDR_IS_IPV4(&laddr))
1056 laddr.in4.sin_addr.s_addr = INADDR_ANY;
1059 laddr.in6.sin6_addr = in6addr_any;
1062 vso = vxlan_socket_lookup(&laddr);
1068 vxlan_sockaddr_mc_info_match(const struct vxlan_socket_mc_info *mc,
1069 const union vxlan_sockaddr *group, const union vxlan_sockaddr *local,
1073 if (!vxlan_sockaddr_in_any(local) &&
1074 !vxlan_sockaddr_in_equal(&mc->vxlsomc_saddr, &local->sa))
1076 if (!vxlan_sockaddr_in_equal(&mc->vxlsomc_gaddr, &group->sa))
1078 if (ifidx != 0 && ifidx != mc->vxlsomc_ifidx)
1085 vxlan_socket_mc_join_group(struct vxlan_socket *vso,
1086 const union vxlan_sockaddr *group, const union vxlan_sockaddr *local,
1087 int *ifidx, union vxlan_sockaddr *source)
1089 struct sockopt sopt;
1094 if (VXLAN_SOCKADDR_IS_IPV4(group)) {
1095 struct ip_mreq mreq;
1097 mreq.imr_multiaddr = group->in4.sin_addr;
1098 mreq.imr_interface = local->in4.sin_addr;
1100 bzero(&sopt, sizeof(sopt));
1101 sopt.sopt_dir = SOPT_SET;
1102 sopt.sopt_level = IPPROTO_IP;
1103 sopt.sopt_name = IP_ADD_MEMBERSHIP;
1104 sopt.sopt_val = &mreq;
1105 sopt.sopt_valsize = sizeof(mreq);
1106 error = sosetopt(vso->vxlso_sock, &sopt);
1111 * BMV: Ideally, there would be a formal way for us to get
1112 * the local interface that was selected based on the
1113 * imr_interface address. We could then update *ifidx so
1114 * vxlan_sockaddr_mc_info_match() would return a match for
1115 * later creates that explicitly set the multicast interface.
1117 * If we really need to, we can of course look in the INP's
1119 * sotoinpcb(vso->vxlso_sock)->inp_moptions->
1120 * imo_membership[]->inm_ifp
1121 * similarly to imo_match_group().
1123 source->in4.sin_addr = local->in4.sin_addr;
1125 } else if (VXLAN_SOCKADDR_IS_IPV6(group)) {
1126 struct ipv6_mreq mreq;
1128 mreq.ipv6mr_multiaddr = group->in6.sin6_addr;
1129 mreq.ipv6mr_interface = *ifidx;
1131 bzero(&sopt, sizeof(sopt));
1132 sopt.sopt_dir = SOPT_SET;
1133 sopt.sopt_level = IPPROTO_IPV6;
1134 sopt.sopt_name = IPV6_JOIN_GROUP;
1135 sopt.sopt_val = &mreq;
1136 sopt.sopt_valsize = sizeof(mreq);
1137 error = sosetopt(vso->vxlso_sock, &sopt);
1142 * BMV: As with IPv4, we would really like to know what
1143 * interface in6p_lookup_mcast_ifp() selected.
1146 error = EAFNOSUPPORT;
1152 vxlan_socket_mc_leave_group(struct vxlan_socket *vso,
1153 const union vxlan_sockaddr *group, const union vxlan_sockaddr *source,
1156 struct sockopt sopt;
1159 bzero(&sopt, sizeof(sopt));
1160 sopt.sopt_dir = SOPT_SET;
1162 if (VXLAN_SOCKADDR_IS_IPV4(group)) {
1163 struct ip_mreq mreq;
1165 mreq.imr_multiaddr = group->in4.sin_addr;
1166 mreq.imr_interface = source->in4.sin_addr;
1168 sopt.sopt_level = IPPROTO_IP;
1169 sopt.sopt_name = IP_DROP_MEMBERSHIP;
1170 sopt.sopt_val = &mreq;
1171 sopt.sopt_valsize = sizeof(mreq);
1172 error = sosetopt(vso->vxlso_sock, &sopt);
1174 } else if (VXLAN_SOCKADDR_IS_IPV6(group)) {
1175 struct ipv6_mreq mreq;
1177 mreq.ipv6mr_multiaddr = group->in6.sin6_addr;
1178 mreq.ipv6mr_interface = ifidx;
1180 sopt.sopt_level = IPPROTO_IPV6;
1181 sopt.sopt_name = IPV6_LEAVE_GROUP;
1182 sopt.sopt_val = &mreq;
1183 sopt.sopt_valsize = sizeof(mreq);
1184 error = sosetopt(vso->vxlso_sock, &sopt);
1187 error = EAFNOSUPPORT;
1193 vxlan_socket_mc_add_group(struct vxlan_socket *vso,
1194 const union vxlan_sockaddr *group, const union vxlan_sockaddr *local,
1195 int ifidx, int *idx)
1197 union vxlan_sockaddr source;
1198 struct vxlan_socket_mc_info *mc;
1199 int i, empty, error;
1202 * Within a socket, the same multicast group may be used by multiple
1203 * interfaces, each with a different network identifier. But a socket
1204 * may only join a multicast group once, so keep track of the users
1208 VXLAN_SO_WLOCK(vso);
1209 for (empty = 0, i = 0; i < VXLAN_SO_MC_MAX_GROUPS; i++) {
1210 mc = &vso->vxlso_mc[i];
1212 if (mc->vxlsomc_gaddr.sa.sa_family == AF_UNSPEC) {
1217 if (vxlan_sockaddr_mc_info_match(mc, group, local, ifidx))
1220 VXLAN_SO_WUNLOCK(vso);
1225 error = vxlan_socket_mc_join_group(vso, group, local, &ifidx, &source);
1229 VXLAN_SO_WLOCK(vso);
1230 for (i = 0; i < VXLAN_SO_MC_MAX_GROUPS; i++) {
1231 mc = &vso->vxlso_mc[i];
1233 if (mc->vxlsomc_gaddr.sa.sa_family == AF_UNSPEC) {
1234 vxlan_sockaddr_copy(&mc->vxlsomc_gaddr, &group->sa);
1235 vxlan_sockaddr_copy(&mc->vxlsomc_saddr, &source.sa);
1236 mc->vxlsomc_ifidx = ifidx;
1240 VXLAN_SO_WUNLOCK(vso);
1242 error = vxlan_socket_mc_leave_group(vso, group, &source, ifidx);
1248 mc->vxlsomc_users++;
1249 VXLAN_SO_WUNLOCK(vso);
1257 vxlan_socket_mc_release_group_by_idx(struct vxlan_socket *vso, int idx)
1259 union vxlan_sockaddr group, source;
1260 struct vxlan_socket_mc_info *mc;
1263 KASSERT(idx >= 0 && idx < VXLAN_SO_MC_MAX_GROUPS,
1264 ("%s: vso %p idx %d out of bounds", __func__, vso, idx));
1267 mc = &vso->vxlso_mc[idx];
1269 VXLAN_SO_WLOCK(vso);
1270 mc->vxlsomc_users--;
1271 if (mc->vxlsomc_users == 0) {
1272 group = mc->vxlsomc_gaddr;
1273 source = mc->vxlsomc_saddr;
1274 ifidx = mc->vxlsomc_ifidx;
1275 bzero(mc, sizeof(*mc));
1278 VXLAN_SO_WUNLOCK(vso);
1282 * Our socket's membership in this group may have already
1283 * been removed if we joined through an interface that's
1286 vxlan_socket_mc_leave_group(vso, &group, &source, ifidx);
1290 static struct vxlan_softc *
1291 vxlan_socket_lookup_softc_locked(struct vxlan_socket *vso, uint32_t vni)
1293 struct vxlan_softc *sc;
1296 VXLAN_SO_LOCK_ASSERT(vso);
1297 hash = VXLAN_SO_VNI_HASH(vni);
1299 LIST_FOREACH(sc, &vso->vxlso_vni_hash[hash], vxl_entry) {
1300 if (sc->vxl_vni == vni) {
1309 static struct vxlan_softc *
1310 vxlan_socket_lookup_softc(struct vxlan_socket *vso, uint32_t vni)
1312 struct rm_priotracker tracker;
1313 struct vxlan_softc *sc;
1315 VXLAN_SO_RLOCK(vso, &tracker);
1316 sc = vxlan_socket_lookup_softc_locked(vso, vni);
1317 VXLAN_SO_RUNLOCK(vso, &tracker);
1323 vxlan_socket_insert_softc(struct vxlan_socket *vso, struct vxlan_softc *sc)
1325 struct vxlan_softc *tsc;
1329 hash = VXLAN_SO_VNI_HASH(vni);
1331 VXLAN_SO_WLOCK(vso);
1332 tsc = vxlan_socket_lookup_softc_locked(vso, vni);
1334 VXLAN_SO_WUNLOCK(vso);
1340 LIST_INSERT_HEAD(&vso->vxlso_vni_hash[hash], sc, vxl_entry);
1341 VXLAN_SO_WUNLOCK(vso);
1347 vxlan_socket_remove_softc(struct vxlan_socket *vso, struct vxlan_softc *sc)
1350 VXLAN_SO_WLOCK(vso);
1351 LIST_REMOVE(sc, vxl_entry);
1352 VXLAN_SO_WUNLOCK(vso);
1357 static struct ifnet *
1358 vxlan_multicast_if_ref(struct vxlan_softc *sc, int ipv4)
1362 VXLAN_LOCK_ASSERT(sc);
1364 if (ipv4 && sc->vxl_im4o != NULL)
1365 ifp = sc->vxl_im4o->imo_multicast_ifp;
1366 else if (!ipv4 && sc->vxl_im6o != NULL)
1367 ifp = sc->vxl_im6o->im6o_multicast_ifp;
1378 vxlan_free_multicast(struct vxlan_softc *sc)
1381 if (sc->vxl_mc_ifp != NULL) {
1382 if_rele(sc->vxl_mc_ifp);
1383 sc->vxl_mc_ifp = NULL;
1384 sc->vxl_mc_ifindex = 0;
1387 if (sc->vxl_im4o != NULL) {
1388 free(sc->vxl_im4o, M_VXLAN);
1389 sc->vxl_im4o = NULL;
1392 if (sc->vxl_im6o != NULL) {
1393 free(sc->vxl_im6o, M_VXLAN);
1394 sc->vxl_im6o = NULL;
1399 vxlan_setup_multicast_interface(struct vxlan_softc *sc)
1403 ifp = ifunit_ref(sc->vxl_mc_ifname);
1405 if_printf(sc->vxl_ifp, "multicast interfaces %s does "
1406 "not exist\n", sc->vxl_mc_ifname);
1410 if ((ifp->if_flags & IFF_MULTICAST) == 0) {
1411 if_printf(sc->vxl_ifp, "interface %s does not support "
1412 "multicast\n", sc->vxl_mc_ifname);
1417 sc->vxl_mc_ifp = ifp;
1418 sc->vxl_mc_ifindex = ifp->if_index;
1424 vxlan_setup_multicast(struct vxlan_softc *sc)
1426 const union vxlan_sockaddr *group;
1429 group = &sc->vxl_dst_addr;
1432 if (sc->vxl_mc_ifname[0] != '\0') {
1433 error = vxlan_setup_multicast_interface(sc);
1439 * Initialize an multicast options structure that is sufficiently
1440 * populated for use in the respective IP output routine. This
1441 * structure is typically stored in the socket, but our sockets
1442 * may be shared among multiple interfaces.
1444 if (VXLAN_SOCKADDR_IS_IPV4(group)) {
1445 sc->vxl_im4o = malloc(sizeof(struct ip_moptions), M_VXLAN,
1447 sc->vxl_im4o->imo_multicast_ifp = sc->vxl_mc_ifp;
1448 sc->vxl_im4o->imo_multicast_ttl = sc->vxl_ttl;
1449 sc->vxl_im4o->imo_multicast_vif = -1;
1450 } else if (VXLAN_SOCKADDR_IS_IPV6(group)) {
1451 sc->vxl_im6o = malloc(sizeof(struct ip6_moptions), M_VXLAN,
1453 sc->vxl_im6o->im6o_multicast_ifp = sc->vxl_mc_ifp;
1454 sc->vxl_im6o->im6o_multicast_hlim = sc->vxl_ttl;
1461 vxlan_setup_socket(struct vxlan_softc *sc)
1463 struct vxlan_socket *vso;
1465 union vxlan_sockaddr *saddr, *daddr;
1466 int multicast, error;
1470 saddr = &sc->vxl_src_addr;
1471 daddr = &sc->vxl_dst_addr;
1473 multicast = vxlan_sockaddr_in_multicast(daddr);
1474 MPASS(multicast != -1);
1475 sc->vxl_vso_mc_index = -1;
1478 * Try to create the socket. If that fails, attempt to use an
1481 error = vxlan_socket_create(ifp, multicast, saddr, &vso);
1484 vso = vxlan_socket_mc_lookup(saddr);
1486 vso = vxlan_socket_lookup(saddr);
1489 if_printf(ifp, "cannot create socket (error: %d), "
1490 "and no existing socket found\n", error);
1495 if (multicast != 0) {
1496 error = vxlan_setup_multicast(sc);
1500 error = vxlan_socket_mc_add_group(vso, daddr, saddr,
1501 sc->vxl_mc_ifindex, &sc->vxl_vso_mc_index);
1507 error = vxlan_socket_insert_softc(vso, sc);
1509 sc->vxl_sock = NULL;
1510 if_printf(ifp, "network identifier %d already exists in "
1511 "this socket\n", sc->vxl_vni);
1519 if (sc->vxl_vso_mc_index != -1) {
1520 vxlan_socket_mc_release_group_by_idx(vso,
1521 sc->vxl_vso_mc_index);
1522 sc->vxl_vso_mc_index = -1;
1525 vxlan_free_multicast(sc);
1526 vxlan_socket_release(vso);
1533 vxlan_setup_interface(struct vxlan_softc *sc)
1538 ifp->if_hdrlen = ETHER_HDR_LEN + sizeof(struct vxlanudphdr);
1540 if (VXLAN_SOCKADDR_IS_IPV4(&sc->vxl_dst_addr) != 0)
1541 ifp->if_hdrlen += sizeof(struct ip);
1542 else if (VXLAN_SOCKADDR_IS_IPV6(&sc->vxl_dst_addr) != 0)
1543 ifp->if_hdrlen += sizeof(struct ip6_hdr);
1547 vxlan_valid_init_config(struct vxlan_softc *sc)
1551 if (vxlan_check_vni(sc->vxl_vni) != 0) {
1552 reason = "invalid virtual network identifier specified";
1556 if (vxlan_sockaddr_supported(&sc->vxl_src_addr, 1) == 0) {
1557 reason = "source address type is not supported";
1561 if (vxlan_sockaddr_supported(&sc->vxl_dst_addr, 0) == 0) {
1562 reason = "destination address type is not supported";
1566 if (vxlan_sockaddr_in_any(&sc->vxl_dst_addr) != 0) {
1567 reason = "no valid destination address specified";
1571 if (vxlan_sockaddr_in_multicast(&sc->vxl_dst_addr) == 0 &&
1572 sc->vxl_mc_ifname[0] != '\0') {
1573 reason = "can only specify interface with a group address";
1577 if (vxlan_sockaddr_in_any(&sc->vxl_src_addr) == 0) {
1578 if (VXLAN_SOCKADDR_IS_IPV4(&sc->vxl_src_addr) ^
1579 VXLAN_SOCKADDR_IS_IPV4(&sc->vxl_dst_addr)) {
1580 reason = "source and destination address must both "
1581 "be either IPv4 or IPv6";
1586 if (sc->vxl_src_addr.in4.sin_port == 0) {
1587 reason = "local port not specified";
1591 if (sc->vxl_dst_addr.in4.sin_port == 0) {
1592 reason = "remote port not specified";
1599 if_printf(sc->vxl_ifp, "cannot initialize interface: %s\n", reason);
1604 vxlan_init_wait(struct vxlan_softc *sc)
1607 VXLAN_LOCK_WASSERT(sc);
1608 while (sc->vxl_flags & VXLAN_FLAG_INIT)
1609 rm_sleep(sc, &sc->vxl_lock, 0, "vxlint", hz);
1613 vxlan_init_complete(struct vxlan_softc *sc)
1617 sc->vxl_flags &= ~VXLAN_FLAG_INIT;
1623 vxlan_init(void *xsc)
1625 static const uint8_t empty_mac[ETHER_ADDR_LEN];
1626 struct vxlan_softc *sc;
1633 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1637 sc->vxl_flags |= VXLAN_FLAG_INIT;
1640 if (vxlan_valid_init_config(sc) != 0)
1643 vxlan_setup_interface(sc);
1645 if (vxlan_setup_socket(sc) != 0)
1648 /* Initialize the default forwarding entry. */
1649 vxlan_ftable_entry_init(sc, &sc->vxl_default_fe, empty_mac,
1650 &sc->vxl_dst_addr.sa, VXLAN_FE_FLAG_STATIC);
1653 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1654 callout_reset(&sc->vxl_callout, vxlan_ftable_prune_period * hz,
1659 vxlan_init_complete(sc);
1663 vxlan_release(struct vxlan_softc *sc)
1667 * The softc may be destroyed as soon as we release our reference,
1668 * so we cannot serialize the wakeup with the softc lock. We use a
1669 * timeout in our sleeps so a missed wakeup is unfortunate but not
1672 if (VXLAN_RELEASE(sc) != 0)
1677 vxlan_teardown_wait(struct vxlan_softc *sc)
1680 VXLAN_LOCK_WASSERT(sc);
1681 while (sc->vxl_flags & VXLAN_FLAG_TEARDOWN)
1682 rm_sleep(sc, &sc->vxl_lock, 0, "vxltrn", hz);
1686 vxlan_teardown_complete(struct vxlan_softc *sc)
1690 sc->vxl_flags &= ~VXLAN_FLAG_TEARDOWN;
1696 vxlan_teardown_locked(struct vxlan_softc *sc)
1699 struct vxlan_socket *vso;
1703 VXLAN_LOCK_WASSERT(sc);
1704 MPASS(sc->vxl_flags & VXLAN_FLAG_TEARDOWN);
1706 ifp->if_flags &= ~IFF_UP;
1707 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1708 callout_stop(&sc->vxl_callout);
1710 sc->vxl_sock = NULL;
1715 vxlan_socket_remove_softc(vso, sc);
1717 if (sc->vxl_vso_mc_index != -1) {
1718 vxlan_socket_mc_release_group_by_idx(vso,
1719 sc->vxl_vso_mc_index);
1720 sc->vxl_vso_mc_index = -1;
1725 while (sc->vxl_refcnt != 0)
1726 rm_sleep(sc, &sc->vxl_lock, 0, "vxldrn", hz);
1729 callout_drain(&sc->vxl_callout);
1731 vxlan_free_multicast(sc);
1733 vxlan_socket_release(vso);
1735 vxlan_teardown_complete(sc);
1739 vxlan_teardown(struct vxlan_softc *sc)
1743 if (sc->vxl_flags & VXLAN_FLAG_TEARDOWN) {
1744 vxlan_teardown_wait(sc);
1749 sc->vxl_flags |= VXLAN_FLAG_TEARDOWN;
1750 vxlan_teardown_locked(sc);
1754 vxlan_ifdetach(struct vxlan_softc *sc, struct ifnet *ifp,
1755 struct vxlan_softc_head *list)
1760 if (sc->vxl_mc_ifp != ifp)
1762 if (sc->vxl_flags & VXLAN_FLAG_TEARDOWN)
1765 sc->vxl_flags |= VXLAN_FLAG_TEARDOWN;
1766 LIST_INSERT_HEAD(list, sc, vxl_ifdetach_list);
1773 vxlan_timer(void *xsc)
1775 struct vxlan_softc *sc;
1778 VXLAN_LOCK_WASSERT(sc);
1780 vxlan_ftable_expire(sc);
1781 callout_schedule(&sc->vxl_callout, vxlan_ftable_prune_period * hz);
1785 vxlan_ioctl_ifflags(struct vxlan_softc *sc)
1791 if (ifp->if_flags & IFF_UP) {
1792 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1795 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1803 vxlan_ctrl_get_config(struct vxlan_softc *sc, void *arg)
1805 struct rm_priotracker tracker;
1806 struct ifvxlancfg *cfg;
1809 bzero(cfg, sizeof(*cfg));
1811 VXLAN_RLOCK(sc, &tracker);
1812 cfg->vxlc_vni = sc->vxl_vni;
1813 memcpy(&cfg->vxlc_local_sa, &sc->vxl_src_addr,
1814 sizeof(union vxlan_sockaddr));
1815 memcpy(&cfg->vxlc_remote_sa, &sc->vxl_dst_addr,
1816 sizeof(union vxlan_sockaddr));
1817 cfg->vxlc_mc_ifindex = sc->vxl_mc_ifindex;
1818 cfg->vxlc_ftable_cnt = sc->vxl_ftable_cnt;
1819 cfg->vxlc_ftable_max = sc->vxl_ftable_max;
1820 cfg->vxlc_ftable_timeout = sc->vxl_ftable_timeout;
1821 cfg->vxlc_port_min = sc->vxl_min_port;
1822 cfg->vxlc_port_max = sc->vxl_max_port;
1823 cfg->vxlc_learn = (sc->vxl_flags & VXLAN_FLAG_LEARN) != 0;
1824 cfg->vxlc_ttl = sc->vxl_ttl;
1825 VXLAN_RUNLOCK(sc, &tracker);
1831 vxlan_ctrl_set_vni(struct vxlan_softc *sc, void *arg)
1833 struct ifvxlancmd *cmd;
1838 if (vxlan_check_vni(cmd->vxlcmd_vni) != 0)
1842 if (vxlan_can_change_config(sc)) {
1843 sc->vxl_vni = cmd->vxlcmd_vni;
1853 vxlan_ctrl_set_local_addr(struct vxlan_softc *sc, void *arg)
1855 struct ifvxlancmd *cmd;
1856 union vxlan_sockaddr *vxlsa;
1860 vxlsa = &cmd->vxlcmd_sa;
1862 if (!VXLAN_SOCKADDR_IS_IPV46(vxlsa))
1864 if (vxlan_sockaddr_in_multicast(vxlsa) != 0)
1868 if (vxlan_can_change_config(sc)) {
1869 vxlan_sockaddr_in_copy(&sc->vxl_src_addr, &vxlsa->sa);
1879 vxlan_ctrl_set_remote_addr(struct vxlan_softc *sc, void *arg)
1881 struct ifvxlancmd *cmd;
1882 union vxlan_sockaddr *vxlsa;
1886 vxlsa = &cmd->vxlcmd_sa;
1888 if (!VXLAN_SOCKADDR_IS_IPV46(vxlsa))
1892 if (vxlan_can_change_config(sc)) {
1893 vxlan_sockaddr_in_copy(&sc->vxl_dst_addr, &vxlsa->sa);
1903 vxlan_ctrl_set_local_port(struct vxlan_softc *sc, void *arg)
1905 struct ifvxlancmd *cmd;
1910 if (cmd->vxlcmd_port == 0)
1914 if (vxlan_can_change_config(sc)) {
1915 sc->vxl_src_addr.in4.sin_port = htons(cmd->vxlcmd_port);
1925 vxlan_ctrl_set_remote_port(struct vxlan_softc *sc, void *arg)
1927 struct ifvxlancmd *cmd;
1932 if (cmd->vxlcmd_port == 0)
1936 if (vxlan_can_change_config(sc)) {
1937 sc->vxl_dst_addr.in4.sin_port = htons(cmd->vxlcmd_port);
1947 vxlan_ctrl_set_port_range(struct vxlan_softc *sc, void *arg)
1949 struct ifvxlancmd *cmd;
1954 min = cmd->vxlcmd_port_min;
1955 max = cmd->vxlcmd_port_max;
1961 if (vxlan_can_change_config(sc)) {
1962 sc->vxl_min_port = min;
1963 sc->vxl_max_port = max;
1973 vxlan_ctrl_set_ftable_timeout(struct vxlan_softc *sc, void *arg)
1975 struct ifvxlancmd *cmd;
1981 if (vxlan_check_ftable_timeout(cmd->vxlcmd_ftable_timeout) == 0) {
1982 sc->vxl_ftable_timeout = cmd->vxlcmd_ftable_timeout;
1992 vxlan_ctrl_set_ftable_max(struct vxlan_softc *sc, void *arg)
1994 struct ifvxlancmd *cmd;
2000 if (vxlan_check_ftable_max(cmd->vxlcmd_ftable_max) == 0) {
2001 sc->vxl_ftable_max = cmd->vxlcmd_ftable_max;
2011 vxlan_ctrl_set_multicast_if(struct vxlan_softc * sc, void *arg)
2013 struct ifvxlancmd *cmd;
2019 if (vxlan_can_change_config(sc)) {
2020 strlcpy(sc->vxl_mc_ifname, cmd->vxlcmd_ifname, IFNAMSIZ);
2030 vxlan_ctrl_set_ttl(struct vxlan_softc *sc, void *arg)
2032 struct ifvxlancmd *cmd;
2038 if (vxlan_check_ttl(cmd->vxlcmd_ttl) == 0) {
2039 sc->vxl_ttl = cmd->vxlcmd_ttl;
2040 if (sc->vxl_im4o != NULL)
2041 sc->vxl_im4o->imo_multicast_ttl = sc->vxl_ttl;
2042 if (sc->vxl_im6o != NULL)
2043 sc->vxl_im6o->im6o_multicast_hlim = sc->vxl_ttl;
2053 vxlan_ctrl_set_learn(struct vxlan_softc *sc, void *arg)
2055 struct ifvxlancmd *cmd;
2060 if (cmd->vxlcmd_flags & VXLAN_CMD_FLAG_LEARN)
2061 sc->vxl_flags |= VXLAN_FLAG_LEARN;
2063 sc->vxl_flags &= ~VXLAN_FLAG_LEARN;
2070 vxlan_ctrl_ftable_entry_add(struct vxlan_softc *sc, void *arg)
2072 union vxlan_sockaddr vxlsa;
2073 struct ifvxlancmd *cmd;
2074 struct vxlan_ftable_entry *fe;
2078 vxlsa = cmd->vxlcmd_sa;
2080 if (!VXLAN_SOCKADDR_IS_IPV46(&vxlsa))
2082 if (vxlan_sockaddr_in_any(&vxlsa) != 0)
2084 if (vxlan_sockaddr_in_multicast(&vxlsa) != 0)
2086 /* BMV: We could support both IPv4 and IPv6 later. */
2087 if (vxlsa.sa.sa_family != sc->vxl_dst_addr.sa.sa_family)
2088 return (EAFNOSUPPORT);
2090 fe = vxlan_ftable_entry_alloc();
2094 if (vxlsa.in4.sin_port == 0)
2095 vxlsa.in4.sin_port = sc->vxl_dst_addr.in4.sin_port;
2097 vxlan_ftable_entry_init(sc, fe, cmd->vxlcmd_mac, &vxlsa.sa,
2098 VXLAN_FE_FLAG_STATIC);
2101 error = vxlan_ftable_entry_insert(sc, fe);
2105 vxlan_ftable_entry_free(fe);
2111 vxlan_ctrl_ftable_entry_rem(struct vxlan_softc *sc, void *arg)
2113 struct ifvxlancmd *cmd;
2114 struct vxlan_ftable_entry *fe;
2120 fe = vxlan_ftable_entry_lookup(sc, cmd->vxlcmd_mac);
2122 vxlan_ftable_entry_destroy(sc, fe);
2132 vxlan_ctrl_flush(struct vxlan_softc *sc, void *arg)
2134 struct ifvxlancmd *cmd;
2138 all = cmd->vxlcmd_flags & VXLAN_CMD_FLAG_FLUSH_ALL;
2141 vxlan_ftable_flush(sc, all);
2148 vxlan_ioctl_drvspec(struct vxlan_softc *sc, struct ifdrv *ifd, int get)
2150 const struct vxlan_control *vc;
2152 struct ifvxlancfg cfg;
2153 struct ifvxlancmd cmd;
2157 if (ifd->ifd_cmd >= vxlan_control_table_size)
2160 bzero(&args, sizeof(args));
2161 vc = &vxlan_control_table[ifd->ifd_cmd];
2162 out = (vc->vxlc_flags & VXLAN_CTRL_FLAG_COPYOUT) != 0;
2164 if ((get != 0 && out == 0) || (get == 0 && out != 0))
2167 if (vc->vxlc_flags & VXLAN_CTRL_FLAG_SUSER) {
2168 error = priv_check(curthread, PRIV_NET_VXLAN);
2173 if (ifd->ifd_len != vc->vxlc_argsize ||
2174 ifd->ifd_len > sizeof(args))
2177 if (vc->vxlc_flags & VXLAN_CTRL_FLAG_COPYIN) {
2178 error = copyin(ifd->ifd_data, &args, ifd->ifd_len);
2183 error = vc->vxlc_func(sc, &args);
2187 if (vc->vxlc_flags & VXLAN_CTRL_FLAG_COPYOUT) {
2188 error = copyout(&args, ifd->ifd_data, ifd->ifd_len);
2197 vxlan_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2199 struct vxlan_softc *sc;
2205 ifr = (struct ifreq *) data;
2206 ifd = (struct ifdrv *) data;
2216 error = vxlan_ioctl_drvspec(sc, ifd, cmd == SIOCGDRVSPEC);
2220 error = vxlan_ioctl_ifflags(sc);
2223 error = ether_ioctl(ifp, cmd, data);
2230 #if defined(INET) || defined(INET6)
2232 vxlan_pick_source_port(struct vxlan_softc *sc, struct mbuf *m)
2237 range = sc->vxl_max_port - sc->vxl_min_port + 1;
2239 if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE &&
2240 M_HASHTYPE_GET(m) != M_HASHTYPE_OPAQUE)
2241 hash = m->m_pkthdr.flowid;
2243 hash = jenkins_hash(m->m_data, ETHER_HDR_LEN,
2244 sc->vxl_port_hash_key);
2246 return (sc->vxl_min_port + (hash % range));
2250 vxlan_encap_header(struct vxlan_softc *sc, struct mbuf *m, int ipoff,
2251 uint16_t srcport, uint16_t dstport)
2253 struct vxlanudphdr *hdr;
2254 struct udphdr *udph;
2255 struct vxlan_header *vxh;
2258 len = m->m_pkthdr.len - ipoff;
2259 MPASS(len >= sizeof(struct vxlanudphdr));
2260 hdr = mtodo(m, ipoff);
2262 udph = &hdr->vxlh_udp;
2263 udph->uh_sport = srcport;
2264 udph->uh_dport = dstport;
2265 udph->uh_ulen = htons(len);
2268 vxh = &hdr->vxlh_hdr;
2269 vxh->vxlh_flags = htonl(VXLAN_HDR_FLAGS_VALID_VNI);
2270 vxh->vxlh_vni = htonl(sc->vxl_vni << VXLAN_HDR_VNI_SHIFT);
2275 vxlan_encap4(struct vxlan_softc *sc, const union vxlan_sockaddr *fvxlsa,
2281 struct in_addr srcaddr, dstaddr;
2282 uint16_t srcport, dstport;
2283 int len, mcast, error;
2286 srcaddr = sc->vxl_src_addr.in4.sin_addr;
2287 srcport = vxlan_pick_source_port(sc, m);
2288 dstaddr = fvxlsa->in4.sin_addr;
2289 dstport = fvxlsa->in4.sin_port;
2291 M_PREPEND(m, sizeof(struct ip) + sizeof(struct vxlanudphdr),
2294 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2298 len = m->m_pkthdr.len;
2300 ip = mtod(m, struct ip *);
2302 ip->ip_len = htons(len);
2304 ip->ip_ttl = sc->vxl_ttl;
2305 ip->ip_p = IPPROTO_UDP;
2307 ip->ip_src = srcaddr;
2308 ip->ip_dst = dstaddr;
2310 vxlan_encap_header(sc, m, sizeof(struct ip), srcport, dstport);
2312 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
2313 m->m_flags &= ~(M_MCAST | M_BCAST);
2315 error = ip_output(m, NULL, NULL, 0, sc->vxl_im4o, NULL);
2317 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
2318 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
2320 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
2322 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2332 vxlan_encap6(struct vxlan_softc *sc, const union vxlan_sockaddr *fvxlsa,
2337 struct ip6_hdr *ip6;
2338 const struct in6_addr *srcaddr, *dstaddr;
2339 uint16_t srcport, dstport;
2340 int len, mcast, error;
2343 srcaddr = &sc->vxl_src_addr.in6.sin6_addr;
2344 srcport = vxlan_pick_source_port(sc, m);
2345 dstaddr = &fvxlsa->in6.sin6_addr;
2346 dstport = fvxlsa->in6.sin6_port;
2348 M_PREPEND(m, sizeof(struct ip6_hdr) + sizeof(struct vxlanudphdr),
2351 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2355 len = m->m_pkthdr.len;
2357 ip6 = mtod(m, struct ip6_hdr *);
2358 ip6->ip6_flow = 0; /* BMV: Keep in forwarding entry? */
2359 ip6->ip6_vfc = IPV6_VERSION;
2361 ip6->ip6_nxt = IPPROTO_UDP;
2362 ip6->ip6_hlim = sc->vxl_ttl;
2363 ip6->ip6_src = *srcaddr;
2364 ip6->ip6_dst = *dstaddr;
2366 vxlan_encap_header(sc, m, sizeof(struct ip6_hdr), srcport, dstport);
2369 * XXX BMV We need support for RFC6935 before we can send and
2370 * receive IPv6 UDP packets with a zero checksum.
2373 struct udphdr *hdr = mtodo(m, sizeof(struct ip6_hdr));
2374 hdr->uh_sum = in6_cksum_pseudo(ip6,
2375 m->m_pkthdr.len - sizeof(struct ip6_hdr), IPPROTO_UDP, 0);
2376 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
2377 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
2380 mcast = (m->m_flags & (M_MCAST | M_BCAST)) ? 1 : 0;
2381 m->m_flags &= ~(M_MCAST | M_BCAST);
2383 error = ip6_output(m, NULL, NULL, 0, sc->vxl_im6o, NULL, NULL);
2385 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
2386 if_inc_counter(ifp, IFCOUNTER_OBYTES, len);
2388 if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
2390 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2400 vxlan_transmit(struct ifnet *ifp, struct mbuf *m)
2402 struct rm_priotracker tracker;
2403 union vxlan_sockaddr vxlsa;
2404 struct vxlan_softc *sc;
2405 struct vxlan_ftable_entry *fe;
2406 struct ifnet *mcifp;
2407 struct ether_header *eh;
2411 eh = mtod(m, struct ether_header *);
2415 ETHER_BPF_MTAP(ifp, m);
2417 VXLAN_RLOCK(sc, &tracker);
2418 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2419 VXLAN_RUNLOCK(sc, &tracker);
2424 if ((m->m_flags & (M_BCAST | M_MCAST)) == 0)
2425 fe = vxlan_ftable_entry_lookup(sc, eh->ether_dhost);
2427 fe = &sc->vxl_default_fe;
2428 vxlan_sockaddr_copy(&vxlsa, &fe->vxlfe_raddr.sa);
2430 ipv4 = VXLAN_SOCKADDR_IS_IPV4(&vxlsa) != 0;
2431 if (vxlan_sockaddr_in_multicast(&vxlsa) != 0)
2432 mcifp = vxlan_multicast_if_ref(sc, ipv4);
2435 VXLAN_RUNLOCK(sc, &tracker);
2438 error = vxlan_encap4(sc, &vxlsa, m);
2440 error = vxlan_encap6(sc, &vxlsa, m);
2450 vxlan_qflush(struct ifnet *ifp __unused)
2455 vxlan_rcv_udp_packet(struct mbuf *m, int offset, struct inpcb *inpcb,
2456 const struct sockaddr *srcsa, void *xvso)
2458 struct vxlan_socket *vso;
2459 struct vxlan_header *vxh, vxlanhdr;
2465 offset += sizeof(struct udphdr);
2467 if (m->m_pkthdr.len < offset + sizeof(struct vxlan_header))
2470 if (__predict_false(m->m_len < offset + sizeof(struct vxlan_header))) {
2471 m_copydata(m, offset, sizeof(struct vxlan_header),
2472 (caddr_t) &vxlanhdr);
2475 vxh = mtodo(m, offset);
2478 * Drop if there is a reserved bit set in either the flags or VNI
2479 * fields of the header. This goes against the specification, but
2480 * a bit set may indicate an unsupported new feature. This matches
2481 * the behavior of the Linux implementation.
2483 if (vxh->vxlh_flags != htonl(VXLAN_HDR_FLAGS_VALID_VNI) ||
2484 vxh->vxlh_vni & ~htonl(VXLAN_VNI_MASK))
2487 vni = ntohl(vxh->vxlh_vni) >> VXLAN_HDR_VNI_SHIFT;
2488 /* Adjust to the start of the inner Ethernet frame. */
2489 m_adj(m, offset + sizeof(struct vxlan_header));
2491 error = vxlan_input(vso, vni, &m, srcsa);
2492 MPASS(error != 0 || m == NULL);
2500 vxlan_input(struct vxlan_socket *vso, uint32_t vni, struct mbuf **m0,
2501 const struct sockaddr *sa)
2503 struct vxlan_softc *sc;
2506 struct ether_header *eh;
2509 sc = vxlan_socket_lookup_softc(vso, vni);
2515 eh = mtod(m, struct ether_header *);
2517 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2520 } else if (ifp == m->m_pkthdr.rcvif) {
2521 /* XXX Does not catch more complex loops. */
2526 if (sc->vxl_flags & VXLAN_FLAG_LEARN)
2527 vxlan_ftable_update(sc, sa, eh->ether_shost);
2530 m->m_pkthdr.rcvif = ifp;
2531 M_SETFIB(m, ifp->if_fib);
2533 error = netisr_queue_src(NETISR_ETHER, 0, m);
2542 vxlan_set_default_config(struct vxlan_softc *sc)
2545 sc->vxl_flags |= VXLAN_FLAG_LEARN;
2547 sc->vxl_vni = VXLAN_VNI_MAX;
2548 sc->vxl_ttl = IPDEFTTL;
2550 if (!vxlan_tunable_int(sc, "legacy_port", vxlan_legacy_port)) {
2551 sc->vxl_src_addr.in4.sin_port = htons(VXLAN_PORT);
2552 sc->vxl_dst_addr.in4.sin_port = htons(VXLAN_PORT);
2554 sc->vxl_src_addr.in4.sin_port = htons(VXLAN_LEGACY_PORT);
2555 sc->vxl_dst_addr.in4.sin_port = htons(VXLAN_LEGACY_PORT);
2558 sc->vxl_min_port = V_ipport_firstauto;
2559 sc->vxl_max_port = V_ipport_lastauto;
2561 sc->vxl_ftable_max = VXLAN_FTABLE_MAX;
2562 sc->vxl_ftable_timeout = VXLAN_FTABLE_TIMEOUT;
2566 vxlan_set_user_config(struct vxlan_softc *sc, struct ifvxlanparam *vxlp)
2570 if (vxlp->vxlp_with & (VXLAN_PARAM_WITH_LOCAL_ADDR4 |
2571 VXLAN_PARAM_WITH_REMOTE_ADDR4))
2572 return (EAFNOSUPPORT);
2576 if (vxlp->vxlp_with & (VXLAN_PARAM_WITH_LOCAL_ADDR6 |
2577 VXLAN_PARAM_WITH_REMOTE_ADDR6))
2578 return (EAFNOSUPPORT);
2581 if (vxlp->vxlp_with & VXLAN_PARAM_WITH_VNI) {
2582 if (vxlan_check_vni(vxlp->vxlp_vni) == 0)
2583 sc->vxl_vni = vxlp->vxlp_vni;
2586 if (vxlp->vxlp_with & VXLAN_PARAM_WITH_LOCAL_ADDR4) {
2587 sc->vxl_src_addr.in4.sin_len = sizeof(struct sockaddr_in);
2588 sc->vxl_src_addr.in4.sin_family = AF_INET;
2589 sc->vxl_src_addr.in4.sin_addr = vxlp->vxlp_local_in4;
2590 } else if (vxlp->vxlp_with & VXLAN_PARAM_WITH_LOCAL_ADDR6) {
2591 sc->vxl_src_addr.in6.sin6_len = sizeof(struct sockaddr_in6);
2592 sc->vxl_src_addr.in6.sin6_family = AF_INET6;
2593 sc->vxl_src_addr.in6.sin6_addr = vxlp->vxlp_local_in6;
2596 if (vxlp->vxlp_with & VXLAN_PARAM_WITH_REMOTE_ADDR4) {
2597 sc->vxl_dst_addr.in4.sin_len = sizeof(struct sockaddr_in);
2598 sc->vxl_dst_addr.in4.sin_family = AF_INET;
2599 sc->vxl_dst_addr.in4.sin_addr = vxlp->vxlp_remote_in4;
2600 } else if (vxlp->vxlp_with & VXLAN_PARAM_WITH_REMOTE_ADDR6) {
2601 sc->vxl_dst_addr.in6.sin6_len = sizeof(struct sockaddr_in6);
2602 sc->vxl_dst_addr.in6.sin6_family = AF_INET6;
2603 sc->vxl_dst_addr.in6.sin6_addr = vxlp->vxlp_remote_in6;
2606 if (vxlp->vxlp_with & VXLAN_PARAM_WITH_LOCAL_PORT)
2607 sc->vxl_src_addr.in4.sin_port = htons(vxlp->vxlp_local_port);
2608 if (vxlp->vxlp_with & VXLAN_PARAM_WITH_REMOTE_PORT)
2609 sc->vxl_dst_addr.in4.sin_port = htons(vxlp->vxlp_remote_port);
2611 if (vxlp->vxlp_with & VXLAN_PARAM_WITH_PORT_RANGE) {
2612 if (vxlp->vxlp_min_port <= vxlp->vxlp_max_port) {
2613 sc->vxl_min_port = vxlp->vxlp_min_port;
2614 sc->vxl_max_port = vxlp->vxlp_max_port;
2618 if (vxlp->vxlp_with & VXLAN_PARAM_WITH_MULTICAST_IF)
2619 strlcpy(sc->vxl_mc_ifname, vxlp->vxlp_mc_ifname, IFNAMSIZ);
2621 if (vxlp->vxlp_with & VXLAN_PARAM_WITH_FTABLE_TIMEOUT) {
2622 if (vxlan_check_ftable_timeout(vxlp->vxlp_ftable_timeout) == 0)
2623 sc->vxl_ftable_timeout = vxlp->vxlp_ftable_timeout;
2626 if (vxlp->vxlp_with & VXLAN_PARAM_WITH_FTABLE_MAX) {
2627 if (vxlan_check_ftable_max(vxlp->vxlp_ftable_max) == 0)
2628 sc->vxl_ftable_max = vxlp->vxlp_ftable_max;
2631 if (vxlp->vxlp_with & VXLAN_PARAM_WITH_TTL) {
2632 if (vxlan_check_ttl(vxlp->vxlp_ttl) == 0)
2633 sc->vxl_ttl = vxlp->vxlp_ttl;
2636 if (vxlp->vxlp_with & VXLAN_PARAM_WITH_LEARN) {
2637 if (vxlp->vxlp_learn == 0)
2638 sc->vxl_flags &= ~VXLAN_FLAG_LEARN;
2645 vxlan_clone_create(struct if_clone *ifc, int unit, caddr_t params)
2647 struct vxlan_softc *sc;
2649 struct ifvxlanparam vxlp;
2652 sc = malloc(sizeof(struct vxlan_softc), M_VXLAN, M_WAITOK | M_ZERO);
2653 sc->vxl_unit = unit;
2654 vxlan_set_default_config(sc);
2657 error = copyin(params, &vxlp, sizeof(vxlp));
2661 error = vxlan_set_user_config(sc, &vxlp);
2666 ifp = if_alloc(IFT_ETHER);
2673 rm_init(&sc->vxl_lock, "vxlanrm");
2674 callout_init_rw(&sc->vxl_callout, &sc->vxl_lock, 0);
2675 sc->vxl_port_hash_key = arc4random();
2676 vxlan_ftable_init(sc);
2678 vxlan_sysctl_setup(sc);
2681 if_initname(ifp, vxlan_name, unit);
2682 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2683 ifp->if_init = vxlan_init;
2684 ifp->if_ioctl = vxlan_ioctl;
2685 ifp->if_transmit = vxlan_transmit;
2686 ifp->if_qflush = vxlan_qflush;
2689 ether_ifattach(ifp, sc->vxl_hwaddr);
2691 ifp->if_baudrate = 0;
2702 vxlan_clone_destroy(struct ifnet *ifp)
2704 struct vxlan_softc *sc;
2710 vxlan_ftable_flush(sc, 1);
2712 ether_ifdetach(ifp);
2715 vxlan_ftable_fini(sc);
2717 vxlan_sysctl_destroy(sc);
2718 rm_destroy(&sc->vxl_lock);
2722 /* BMV: Taken from if_bridge. */
2724 vxlan_mac_hash(struct vxlan_softc *sc, const uint8_t *addr)
2726 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = sc->vxl_ftable_hash_key;
2736 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2737 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2739 #define mix(a, b, c) \
2741 a -= b; a -= c; a ^= (c >> 13); \
2742 b -= c; b -= a; b ^= (a << 8); \
2743 c -= a; c -= b; c ^= (b >> 13); \
2744 a -= b; a -= c; a ^= (c >> 12); \
2745 b -= c; b -= a; b ^= (a << 16); \
2746 c -= a; c -= b; c ^= (b >> 5); \
2747 a -= b; a -= c; a ^= (c >> 3); \
2748 b -= c; b -= a; b ^= (a << 10); \
2749 c -= a; c -= b; c ^= (b >> 15); \
2760 vxlan_fakeaddr(struct vxlan_softc *sc)
2764 * Generate a non-multicast, locally administered address.
2766 * BMV: Should we use the FreeBSD OUI range instead?
2768 arc4rand(sc->vxl_hwaddr, ETHER_ADDR_LEN, 1);
2769 sc->vxl_hwaddr[0] &= ~1;
2770 sc->vxl_hwaddr[0] |= 2;
2774 vxlan_sockaddr_cmp(const union vxlan_sockaddr *vxladdr,
2775 const struct sockaddr *sa)
2778 return (bcmp(&vxladdr->sa, sa, vxladdr->sa.sa_len));
2782 vxlan_sockaddr_copy(union vxlan_sockaddr *vxladdr,
2783 const struct sockaddr *sa)
2786 MPASS(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
2787 bzero(vxladdr, sizeof(*vxladdr));
2789 if (sa->sa_family == AF_INET) {
2790 vxladdr->in4 = *satoconstsin(sa);
2791 vxladdr->in4.sin_len = sizeof(struct sockaddr_in);
2792 } else if (sa->sa_family == AF_INET6) {
2793 vxladdr->in6 = *satoconstsin6(sa);
2794 vxladdr->in6.sin6_len = sizeof(struct sockaddr_in6);
2799 vxlan_sockaddr_in_equal(const union vxlan_sockaddr *vxladdr,
2800 const struct sockaddr *sa)
2804 if (sa->sa_family == AF_INET) {
2805 const struct in_addr *in4 = &satoconstsin(sa)->sin_addr;
2806 equal = in4->s_addr == vxladdr->in4.sin_addr.s_addr;
2807 } else if (sa->sa_family == AF_INET6) {
2808 const struct in6_addr *in6 = &satoconstsin6(sa)->sin6_addr;
2809 equal = IN6_ARE_ADDR_EQUAL(in6, &vxladdr->in6.sin6_addr);
2817 vxlan_sockaddr_in_copy(union vxlan_sockaddr *vxladdr,
2818 const struct sockaddr *sa)
2821 MPASS(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
2823 if (sa->sa_family == AF_INET) {
2824 const struct in_addr *in4 = &satoconstsin(sa)->sin_addr;
2825 vxladdr->in4.sin_family = AF_INET;
2826 vxladdr->in4.sin_len = sizeof(struct sockaddr_in);
2827 vxladdr->in4.sin_addr = *in4;
2828 } else if (sa->sa_family == AF_INET6) {
2829 const struct in6_addr *in6 = &satoconstsin6(sa)->sin6_addr;
2830 vxladdr->in6.sin6_family = AF_INET6;
2831 vxladdr->in6.sin6_len = sizeof(struct sockaddr_in6);
2832 vxladdr->in6.sin6_addr = *in6;
2837 vxlan_sockaddr_supported(const union vxlan_sockaddr *vxladdr, int unspec)
2839 const struct sockaddr *sa;
2845 if (sa->sa_family == AF_UNSPEC && unspec != 0) {
2847 } else if (sa->sa_family == AF_INET) {
2851 } else if (sa->sa_family == AF_INET6) {
2861 vxlan_sockaddr_in_any(const union vxlan_sockaddr *vxladdr)
2863 const struct sockaddr *sa;
2868 if (sa->sa_family == AF_INET) {
2869 const struct in_addr *in4 = &satoconstsin(sa)->sin_addr;
2870 any = in4->s_addr == INADDR_ANY;
2871 } else if (sa->sa_family == AF_INET6) {
2872 const struct in6_addr *in6 = &satoconstsin6(sa)->sin6_addr;
2873 any = IN6_IS_ADDR_UNSPECIFIED(in6);
2881 vxlan_sockaddr_in_multicast(const union vxlan_sockaddr *vxladdr)
2883 const struct sockaddr *sa;
2888 if (sa->sa_family == AF_INET) {
2889 const struct in_addr *in4 = &satoconstsin(sa)->sin_addr;
2890 mc = IN_MULTICAST(ntohl(in4->s_addr));
2891 } else if (sa->sa_family == AF_INET6) {
2892 const struct in6_addr *in6 = &satoconstsin6(sa)->sin6_addr;
2893 mc = IN6_IS_ADDR_MULTICAST(in6);
2901 vxlan_can_change_config(struct vxlan_softc *sc)
2906 VXLAN_LOCK_ASSERT(sc);
2908 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2910 if (sc->vxl_flags & (VXLAN_FLAG_INIT | VXLAN_FLAG_TEARDOWN))
2917 vxlan_check_vni(uint32_t vni)
2920 return (vni >= VXLAN_VNI_MAX);
2924 vxlan_check_ttl(int ttl)
2927 return (ttl > MAXTTL);
2931 vxlan_check_ftable_timeout(uint32_t timeout)
2934 return (timeout > VXLAN_FTABLE_MAX_TIMEOUT);
2938 vxlan_check_ftable_max(uint32_t max)
2941 return (max > VXLAN_FTABLE_MAX);
2945 vxlan_sysctl_setup(struct vxlan_softc *sc)
2947 struct sysctl_ctx_list *ctx;
2948 struct sysctl_oid *node;
2949 struct vxlan_statistics *stats;
2952 ctx = &sc->vxl_sysctl_ctx;
2953 stats = &sc->vxl_stats;
2954 snprintf(namebuf, sizeof(namebuf), "%d", sc->vxl_unit);
2956 sysctl_ctx_init(ctx);
2957 sc->vxl_sysctl_node = SYSCTL_ADD_NODE(ctx,
2958 SYSCTL_STATIC_CHILDREN(_net_link_vxlan), OID_AUTO, namebuf,
2959 CTLFLAG_RD, NULL, "");
2961 node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sc->vxl_sysctl_node),
2962 OID_AUTO, "ftable", CTLFLAG_RD, NULL, "");
2963 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(node), OID_AUTO, "count",
2964 CTLFLAG_RD, &sc->vxl_ftable_cnt, 0,
2965 "Number of entries in fowarding table");
2966 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(node), OID_AUTO, "max",
2967 CTLFLAG_RD, &sc->vxl_ftable_max, 0,
2968 "Maximum number of entries allowed in fowarding table");
2969 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(node), OID_AUTO, "timeout",
2970 CTLFLAG_RD, &sc->vxl_ftable_timeout, 0,
2971 "Number of seconds between prunes of the forwarding table");
2972 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(node), OID_AUTO, "dump",
2973 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_SKIP,
2974 sc, 0, vxlan_ftable_sysctl_dump, "A",
2975 "Dump the forwarding table entries");
2977 node = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(sc->vxl_sysctl_node),
2978 OID_AUTO, "stats", CTLFLAG_RD, NULL, "");
2979 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(node), OID_AUTO,
2980 "ftable_nospace", CTLFLAG_RD, &stats->ftable_nospace, 0,
2981 "Fowarding table reached maximum entries");
2982 SYSCTL_ADD_UINT(ctx, SYSCTL_CHILDREN(node), OID_AUTO,
2983 "ftable_lock_upgrade_failed", CTLFLAG_RD,
2984 &stats->ftable_lock_upgrade_failed, 0,
2985 "Forwarding table update required lock upgrade");
2989 vxlan_sysctl_destroy(struct vxlan_softc *sc)
2992 sysctl_ctx_free(&sc->vxl_sysctl_ctx);
2993 sc->vxl_sysctl_node = NULL;
2997 vxlan_tunable_int(struct vxlan_softc *sc, const char *knob, int def)
3001 snprintf(path, sizeof(path), "net.link.vxlan.%d.%s",
3002 sc->vxl_unit, knob);
3003 TUNABLE_INT_FETCH(path, &def);
3009 vxlan_ifdetach_event(void *arg __unused, struct ifnet *ifp)
3011 struct vxlan_softc_head list;
3012 struct vxlan_socket *vso;
3013 struct vxlan_softc *sc, *tsc;
3017 if (ifp->if_flags & IFF_RENAMING)
3019 if ((ifp->if_flags & IFF_MULTICAST) == 0)
3022 mtx_lock(&vxlan_list_mtx);
3023 LIST_FOREACH(vso, &vxlan_socket_list, vxlso_entry)
3024 vxlan_socket_ifdetach(vso, ifp, &list);
3025 mtx_unlock(&vxlan_list_mtx);
3027 LIST_FOREACH_SAFE(sc, &list, vxl_ifdetach_list, tsc) {
3028 LIST_REMOVE(sc, vxl_ifdetach_list);
3031 if (sc->vxl_flags & VXLAN_FLAG_INIT)
3032 vxlan_init_wait(sc);
3033 vxlan_teardown_locked(sc);
3041 mtx_init(&vxlan_list_mtx, "vxlan list", NULL, MTX_DEF);
3042 LIST_INIT(&vxlan_socket_list);
3043 vxlan_ifdetach_event_tag = EVENTHANDLER_REGISTER(ifnet_departure_event,
3044 vxlan_ifdetach_event, NULL, EVENTHANDLER_PRI_ANY);
3045 vxlan_cloner = if_clone_simple(vxlan_name, vxlan_clone_create,
3046 vxlan_clone_destroy, 0);
3053 EVENTHANDLER_DEREGISTER(ifnet_departure_event,
3054 vxlan_ifdetach_event_tag);
3055 if_clone_detach(vxlan_cloner);
3056 mtx_destroy(&vxlan_list_mtx);
3057 MPASS(LIST_EMPTY(&vxlan_socket_list));
3061 vxlan_modevent(module_t mod, int type, void *unused)
3082 static moduledata_t vxlan_mod = {
3088 DECLARE_MODULE(if_vxlan, vxlan_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);
3089 MODULE_VERSION(if_vxlan, 1);