if_llatbl.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  3. *
  4. * Copyright (c) 2004 Luigi Rizzo, Alessandro Cerri. All rights reserved.
  5. * Copyright (c) 2004-2008 Qing Li. All rights reserved.
  6. * Copyright (c) 2008 Kip Macy. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <sys/cdefs.h>
  30. __FBSDID("$FreeBSD$");
  31. #ifndef _NET_IF_LLATBL_H_
  32. #define _NET_IF_LLATBL_H_
  33. #include <sys/_eventhandler.h>
  34. #include <sys/_rwlock.h>
  35. #include <netinet/in.h>
  36. #include <sys/epoch.h>
  37. #include <sys/ck.h>
  38. struct ifnet;
  39. struct sysctl_req;
  40. struct rt_msghdr;
  41. struct rt_addrinfo;
  42. struct llentry;
  43. CK_LIST_HEAD(llentries, llentry);
  44. #define LLE_MAX_LINKHDR 24 /* Full IB header */
  45. /*
  46. * Code referencing llentry must at least hold
  47. * a shared lock
  48. */
  49. struct llentry {
  50. CK_LIST_ENTRY(llentry) lle_next;
  51. union {
  52. struct in_addr addr4;
  53. struct in6_addr addr6;
  54. } r_l3addr;
  55. char r_linkdata[LLE_MAX_LINKHDR]; /* L2 data */
  56. uint8_t r_hdrlen; /* length for LL header */
  57. uint8_t spare0[3];
  58. uint16_t r_flags; /* LLE runtime flags */
  59. uint16_t r_skip_req; /* feedback from fast path */
  60. struct lltable *lle_tbl;
  61. struct llentries *lle_head;
  62. void (*lle_free)(struct llentry *);
  63. struct mbuf *la_hold;
  64. int la_numheld; /* # of packets currently held */
  65. time_t la_expire;
  66. uint16_t la_flags;
  67. uint16_t la_asked;
  68. uint16_t la_preempt;
  69. int16_t ln_state; /* IPv6 has ND6_LLINFO_NOSTATE == -2 */
  70. uint16_t ln_router;
  71. time_t ln_ntick;
  72. time_t lle_remtime; /* Real time remaining */
  73. time_t lle_hittime; /* Time when r_skip_req was unset */
  74. int lle_refcnt;
  75. char *ll_addr; /* link-layer address */
  76. CK_LIST_ENTRY(llentry) lle_chain; /* chain of deleted items */
  77. struct callout lle_timer;
  78. struct rwlock lle_lock;
  79. struct mtx req_mtx;
  80. struct epoch_context lle_epoch_ctx;
  81. };
  82. #define LLE_WLOCK(lle) rw_wlock(&(lle)->lle_lock)
  83. #define LLE_RLOCK(lle) rw_rlock(&(lle)->lle_lock)
  84. #define LLE_WUNLOCK(lle) rw_wunlock(&(lle)->lle_lock)
  85. #define LLE_RUNLOCK(lle) rw_runlock(&(lle)->lle_lock)
  86. #define LLE_DOWNGRADE(lle) rw_downgrade(&(lle)->lle_lock)
  87. #define LLE_TRY_UPGRADE(lle) rw_try_upgrade(&(lle)->lle_lock)
  88. #define LLE_LOCK_INIT(lle) rw_init_flags(&(lle)->lle_lock, "lle", RW_DUPOK)
  89. #define LLE_LOCK_DESTROY(lle) rw_destroy(&(lle)->lle_lock)
  90. #define LLE_WLOCK_ASSERT(lle) rw_assert(&(lle)->lle_lock, RA_WLOCKED)
  91. #define LLE_REQ_INIT(lle) mtx_init(&(lle)->req_mtx, "lle req", \
  92. NULL, MTX_DEF)
  93. #define LLE_REQ_DESTROY(lle) mtx_destroy(&(lle)->req_mtx)
  94. #define LLE_REQ_LOCK(lle) mtx_lock(&(lle)->req_mtx)
  95. #define LLE_REQ_UNLOCK(lle) mtx_unlock(&(lle)->req_mtx)
  96. #define LLE_IS_VALID(lle) (((lle) != NULL) && ((lle) != (void *)-1))
  97. #define LLE_ADDREF(lle) do { \
  98. LLE_WLOCK_ASSERT(lle); \
  99. KASSERT((lle)->lle_refcnt >= 0, \
  100. ("negative refcnt %d on lle %p", \
  101. (lle)->lle_refcnt, (lle))); \
  102. (lle)->lle_refcnt++; \
  103. } while (0)
  104. #define LLE_REMREF(lle) do { \
  105. LLE_WLOCK_ASSERT(lle); \
  106. KASSERT((lle)->lle_refcnt > 0, \
  107. ("bogus refcnt %d on lle %p", \
  108. (lle)->lle_refcnt, (lle))); \
  109. (lle)->lle_refcnt--; \
  110. } while (0)
  111. #define LLE_FREE_LOCKED(lle) do { \
  112. if ((lle)->lle_refcnt == 1) \
  113. (lle)->lle_free(lle); \
  114. else { \
  115. LLE_REMREF(lle); \
  116. LLE_WUNLOCK(lle); \
  117. } \
  118. /* guard against invalid refs */ \
  119. (lle) = NULL; \
  120. } while (0)
  121. #define LLE_FREE(lle) do { \
  122. LLE_WLOCK(lle); \
  123. LLE_FREE_LOCKED(lle); \
  124. } while (0)
  125. typedef struct llentry *(llt_lookup_t)(struct lltable *, u_int flags,
  126. const struct sockaddr *l3addr);
  127. typedef struct llentry *(llt_alloc_t)(struct lltable *, u_int flags,
  128. const struct sockaddr *l3addr);
  129. typedef void (llt_delete_t)(struct lltable *, struct llentry *);
  130. typedef void (llt_prefix_free_t)(struct lltable *,
  131. const struct sockaddr *addr, const struct sockaddr *mask, u_int flags);
  132. typedef int (llt_dump_entry_t)(struct lltable *, struct llentry *,
  133. struct sysctl_req *);
  134. typedef uint32_t (llt_hash_t)(const struct llentry *, uint32_t);
  135. typedef int (llt_match_prefix_t)(const struct sockaddr *,
  136. const struct sockaddr *, u_int, struct llentry *);
  137. typedef void (llt_free_entry_t)(struct lltable *, struct llentry *);
  138. typedef void (llt_fill_sa_entry_t)(const struct llentry *, struct sockaddr *);
  139. typedef void (llt_free_tbl_t)(struct lltable *);
  140. typedef int (llt_link_entry_t)(struct lltable *, struct llentry *);
  141. typedef int (llt_unlink_entry_t)(struct llentry *);
  142. typedef void (llt_mark_used_t)(struct llentry *);
  143. typedef int (llt_foreach_cb_t)(struct lltable *, struct llentry *, void *);
  144. typedef int (llt_foreach_entry_t)(struct lltable *, llt_foreach_cb_t *, void *);
  145. struct lltable {
  146. SLIST_ENTRY(lltable) llt_link;
  147. int llt_af;
  148. int llt_hsize;
  149. int llt_entries;
  150. int llt_maxentries;
  151. struct llentries *lle_head;
  152. struct ifnet *llt_ifp;
  153. llt_lookup_t *llt_lookup;
  154. llt_alloc_t *llt_alloc_entry;
  155. llt_delete_t *llt_delete_entry;
  156. llt_prefix_free_t *llt_prefix_free;
  157. llt_dump_entry_t *llt_dump_entry;
  158. llt_hash_t *llt_hash;
  159. llt_match_prefix_t *llt_match_prefix;
  160. llt_free_entry_t *llt_free_entry;
  161. llt_foreach_entry_t *llt_foreach_entry;
  162. llt_link_entry_t *llt_link_entry;
  163. llt_unlink_entry_t *llt_unlink_entry;
  164. llt_fill_sa_entry_t *llt_fill_sa_entry;
  165. llt_free_tbl_t *llt_free_tbl;
  166. llt_mark_used_t *llt_mark_used;
  167. };
  168. MALLOC_DECLARE(M_LLTABLE);
  169. /*
  170. * LLentry flags
  171. */
  172. #define LLE_DELETED 0x0001 /* entry must be deleted */
  173. #define LLE_STATIC 0x0002 /* entry is static */
  174. #define LLE_IFADDR 0x0004 /* entry is interface addr */
  175. #define LLE_VALID 0x0008 /* ll_addr is valid */
  176. #define LLE_REDIRECT 0x0010 /* installed by redirect; has host rtentry */
  177. #define LLE_PUB 0x0020 /* publish entry ??? */
  178. #define LLE_LINKED 0x0040 /* linked to lookup structure */
  179. /* LLE request flags */
  180. #define LLE_EXCLUSIVE 0x2000 /* return lle xlocked */
  181. #define LLE_UNLOCKED 0x4000 /* return lle unlocked */
  182. #define LLE_ADDRONLY 0x4000 /* return lladdr instead of full header */
  183. #define LLE_CREATE 0x8000 /* hint to avoid lle lookup */
  184. /* LLE flags used by fastpath code */
  185. #define RLLE_VALID 0x0001 /* entry is valid */
  186. #define RLLE_IFADDR LLE_IFADDR /* entry is ifaddr */
  187. #define LLATBL_HASH(key, mask) \
  188. (((((((key >> 8) ^ key) >> 8) ^ key) >> 8) ^ key) & mask)
  189. struct lltable *lltable_allocate_htbl(uint32_t hsize);
  190. void lltable_free(struct lltable *);
  191. void lltable_link(struct lltable *llt);
  192. void lltable_prefix_free(int, struct sockaddr *,
  193. struct sockaddr *, u_int);
  194. int lltable_sysctl_dumparp(int, struct sysctl_req *);
  195. size_t llentry_free(struct llentry *);
  196. /* helper functions */
  197. size_t lltable_drop_entry_queue(struct llentry *);
  198. void lltable_set_entry_addr(struct ifnet *ifp, struct llentry *lle,
  199. const char *linkhdr, size_t linkhdrsize, int lladdr_off);
  200. int lltable_try_set_entry_addr(struct ifnet *ifp, struct llentry *lle,
  201. const char *linkhdr, size_t linkhdrsize, int lladdr_off);
  202. int lltable_calc_llheader(struct ifnet *ifp, int family, char *lladdr,
  203. char *buf, size_t *bufsize, int *lladdr_off);
  204. void lltable_update_ifaddr(struct lltable *llt);
  205. struct llentry *lltable_alloc_entry(struct lltable *llt, u_int flags,
  206. const struct sockaddr *l4addr);
  207. void lltable_free_entry(struct lltable *llt, struct llentry *lle);
  208. int lltable_delete_addr(struct lltable *llt, u_int flags,
  209. const struct sockaddr *l3addr);
  210. int lltable_link_entry(struct lltable *llt, struct llentry *lle);
  211. int lltable_unlink_entry(struct lltable *llt, struct llentry *lle);
  212. void lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa);
  213. struct ifnet *lltable_get_ifp(const struct lltable *llt);
  214. int lltable_get_af(const struct lltable *llt);
  215. int lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f,
  216. void *farg);
  217. /*
  218. * Generic link layer address lookup function.
  219. */
  220. static __inline struct llentry *
  221. lla_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
  222. {
  223. return (llt->llt_lookup(llt, flags, l3addr));
  224. }
  225. /*
  226. * Notify the LLE code that the entry was used by datapath.
  227. */
  228. static __inline void
  229. llentry_mark_used(struct llentry *lle)
  230. {
  231. if (lle->r_skip_req == 0)
  232. return;
  233. if ((lle->r_flags & RLLE_VALID) != 0)
  234. lle->lle_tbl->llt_mark_used(lle);
  235. }
  236. int lla_rt_output(struct rt_msghdr *, struct rt_addrinfo *);
  237. enum {
  238. LLENTRY_RESOLVED,
  239. LLENTRY_TIMEDOUT,
  240. LLENTRY_DELETED,
  241. LLENTRY_EXPIRED,
  242. };
  243. typedef void (*lle_event_fn)(void *, struct llentry *, int);
  244. EVENTHANDLER_DECLARE(lle_event, lle_event_fn);
  245. #endif /* _NET_IF_LLATBL_H_ */