if_llatbl.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  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. #include "opt_ddb.h"
  31. #include "opt_inet.h"
  32. #include "opt_inet6.h"
  33. #include <sys/param.h>
  34. #include <sys/systm.h>
  35. #include <sys/eventhandler.h>
  36. #include <sys/malloc.h>
  37. #include <sys/mbuf.h>
  38. #include <sys/syslog.h>
  39. #include <sys/sysctl.h>
  40. #include <sys/socket.h>
  41. #include <sys/kernel.h>
  42. #include <sys/lock.h>
  43. #include <sys/mutex.h>
  44. #include <sys/rwlock.h>
  45. #ifdef DDB
  46. #include <ddb/ddb.h>
  47. #endif
  48. #include <vm/uma.h>
  49. #include <netinet/in.h>
  50. #include <net/if_llatbl.h>
  51. #include <net/if.h>
  52. #include <net/if_dl.h>
  53. #include <net/if_var.h>
  54. #include <net/if_private.h>
  55. #include <net/route.h>
  56. #include <net/route/route_ctl.h>
  57. #include <net/route/route_debug.h>
  58. #include <net/vnet.h>
  59. #include <netinet/if_ether.h>
  60. #include <netinet6/in6_var.h>
  61. #include <netinet6/nd6.h>
  62. MALLOC_DEFINE(M_LLTABLE, "lltable", "link level address tables");
  63. VNET_DEFINE_STATIC(SLIST_HEAD(, lltable), lltables) =
  64. SLIST_HEAD_INITIALIZER(lltables);
  65. #define V_lltables VNET(lltables)
  66. static struct rwlock lltable_list_lock;
  67. RW_SYSINIT(lltable_list_lock, &lltable_list_lock, "lltable_list_lock");
  68. #define LLTABLE_LIST_RLOCK() rw_rlock(&lltable_list_lock)
  69. #define LLTABLE_LIST_RUNLOCK() rw_runlock(&lltable_list_lock)
  70. #define LLTABLE_LIST_WLOCK() rw_wlock(&lltable_list_lock)
  71. #define LLTABLE_LIST_WUNLOCK() rw_wunlock(&lltable_list_lock)
  72. #define LLTABLE_LIST_LOCK_ASSERT() rw_assert(&lltable_list_lock, RA_LOCKED)
  73. static void lltable_unlink(struct lltable *llt);
  74. static void llentries_unlink(struct lltable *llt, struct llentries *head);
  75. /*
  76. * Dump lle state for a specific address family.
  77. */
  78. static int
  79. lltable_dump_af(struct lltable *llt, struct sysctl_req *wr)
  80. {
  81. struct epoch_tracker et;
  82. int error;
  83. LLTABLE_LIST_LOCK_ASSERT();
  84. if (llt->llt_ifp->if_flags & IFF_LOOPBACK)
  85. return (0);
  86. error = 0;
  87. NET_EPOCH_ENTER(et);
  88. error = lltable_foreach_lle(llt,
  89. (llt_foreach_cb_t *)llt->llt_dump_entry, wr);
  90. NET_EPOCH_EXIT(et);
  91. return (error);
  92. }
  93. /*
  94. * Dump arp state for a specific address family.
  95. */
  96. int
  97. lltable_sysctl_dumparp(int af, struct sysctl_req *wr)
  98. {
  99. struct lltable *llt;
  100. int error = 0;
  101. LLTABLE_LIST_RLOCK();
  102. SLIST_FOREACH(llt, &V_lltables, llt_link) {
  103. if (llt->llt_af == af) {
  104. error = lltable_dump_af(llt, wr);
  105. if (error != 0)
  106. goto done;
  107. }
  108. }
  109. done:
  110. LLTABLE_LIST_RUNLOCK();
  111. return (error);
  112. }
  113. /*
  114. * Adds a mbuf to hold queue. Drops old packets if the queue is full.
  115. *
  116. * Returns the number of held packets that were dropped.
  117. */
  118. size_t
  119. lltable_append_entry_queue(struct llentry *lle, struct mbuf *m,
  120. size_t maxheld)
  121. {
  122. size_t pkts_dropped = 0;
  123. LLE_WLOCK_ASSERT(lle);
  124. while (lle->la_numheld >= maxheld && lle->la_hold != NULL) {
  125. struct mbuf *next = lle->la_hold->m_nextpkt;
  126. m_freem(lle->la_hold);
  127. lle->la_hold = next;
  128. lle->la_numheld--;
  129. pkts_dropped++;
  130. }
  131. if (lle->la_hold != NULL) {
  132. struct mbuf *curr = lle->la_hold;
  133. while (curr->m_nextpkt != NULL)
  134. curr = curr->m_nextpkt;
  135. curr->m_nextpkt = m;
  136. } else
  137. lle->la_hold = m;
  138. lle->la_numheld++;
  139. return pkts_dropped;
  140. }
  141. /*
  142. * Common function helpers for chained hash table.
  143. */
  144. /*
  145. * Runs specified callback for each entry in @llt.
  146. * Caller does the locking.
  147. *
  148. */
  149. static int
  150. htable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f, void *farg)
  151. {
  152. struct llentry *lle, *next;
  153. int i, error;
  154. error = 0;
  155. for (i = 0; i < llt->llt_hsize; i++) {
  156. CK_LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
  157. error = f(llt, lle, farg);
  158. if (error != 0)
  159. break;
  160. }
  161. }
  162. return (error);
  163. }
  164. /*
  165. * The htable_[un]link_entry() functions return:
  166. * 0 if the entry was (un)linked already and nothing changed,
  167. * 1 if the entry was added/removed to/from the table, and
  168. * -1 on error (e.g., not being able to add the entry due to limits reached).
  169. * While the "unlink" operation should never error, callers of
  170. * lltable_link_entry() need to check for errors and handle them.
  171. */
  172. static int
  173. htable_link_entry(struct lltable *llt, struct llentry *lle)
  174. {
  175. struct llentries *lleh;
  176. uint32_t hashidx;
  177. if ((lle->la_flags & LLE_LINKED) != 0)
  178. return (0);
  179. IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
  180. if (llt->llt_maxentries > 0 &&
  181. llt->llt_entries >= llt->llt_maxentries)
  182. return (-1);
  183. hashidx = llt->llt_hash(lle, llt->llt_hsize);
  184. lleh = &llt->lle_head[hashidx];
  185. lle->lle_tbl = llt;
  186. lle->lle_head = lleh;
  187. lle->la_flags |= LLE_LINKED;
  188. CK_LIST_INSERT_HEAD(lleh, lle, lle_next);
  189. llt->llt_entries++;
  190. return (1);
  191. }
  192. static int
  193. htable_unlink_entry(struct llentry *lle)
  194. {
  195. struct lltable *llt;
  196. if ((lle->la_flags & LLE_LINKED) == 0)
  197. return (0);
  198. llt = lle->lle_tbl;
  199. IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
  200. KASSERT(llt->llt_entries > 0, ("%s: lltable %p (%s) entries %d <= 0",
  201. __func__, llt, if_name(llt->llt_ifp), llt->llt_entries));
  202. CK_LIST_REMOVE(lle, lle_next);
  203. lle->la_flags &= ~(LLE_VALID | LLE_LINKED);
  204. #if 0
  205. lle->lle_tbl = NULL;
  206. lle->lle_head = NULL;
  207. #endif
  208. llt->llt_entries--;
  209. return (1);
  210. }
  211. struct prefix_match_data {
  212. const struct sockaddr *addr;
  213. const struct sockaddr *mask;
  214. struct llentries dchain;
  215. u_int flags;
  216. };
  217. static int
  218. htable_prefix_free_cb(struct lltable *llt, struct llentry *lle, void *farg)
  219. {
  220. struct prefix_match_data *pmd;
  221. pmd = (struct prefix_match_data *)farg;
  222. if (llt->llt_match_prefix(pmd->addr, pmd->mask, pmd->flags, lle)) {
  223. LLE_WLOCK(lle);
  224. CK_LIST_INSERT_HEAD(&pmd->dchain, lle, lle_chain);
  225. }
  226. return (0);
  227. }
  228. static void
  229. htable_prefix_free(struct lltable *llt, const struct sockaddr *addr,
  230. const struct sockaddr *mask, u_int flags)
  231. {
  232. struct llentry *lle, *next;
  233. struct prefix_match_data pmd;
  234. bzero(&pmd, sizeof(pmd));
  235. pmd.addr = addr;
  236. pmd.mask = mask;
  237. pmd.flags = flags;
  238. CK_LIST_INIT(&pmd.dchain);
  239. IF_AFDATA_WLOCK(llt->llt_ifp);
  240. /* Push matching lles to chain */
  241. lltable_foreach_lle(llt, htable_prefix_free_cb, &pmd);
  242. llentries_unlink(llt, &pmd.dchain);
  243. IF_AFDATA_WUNLOCK(llt->llt_ifp);
  244. CK_LIST_FOREACH_SAFE(lle, &pmd.dchain, lle_chain, next)
  245. lltable_free_entry(llt, lle);
  246. }
  247. static void
  248. htable_free_tbl(struct lltable *llt)
  249. {
  250. free(llt->lle_head, M_LLTABLE);
  251. free(llt, M_LLTABLE);
  252. }
  253. static void
  254. llentries_unlink(struct lltable *llt, struct llentries *head)
  255. {
  256. struct llentry *lle, *next;
  257. CK_LIST_FOREACH_SAFE(lle, head, lle_chain, next)
  258. llt->llt_unlink_entry(lle);
  259. }
  260. /*
  261. * Helper function used to drop all mbufs in hold queue.
  262. *
  263. * Returns the number of held packets, if any, that were dropped.
  264. */
  265. size_t
  266. lltable_drop_entry_queue(struct llentry *lle)
  267. {
  268. size_t pkts_dropped = 0;
  269. LLE_WLOCK_ASSERT(lle);
  270. while (lle->la_hold != NULL) {
  271. struct mbuf *next = lle->la_hold->m_nextpkt;
  272. m_freem(lle->la_hold);
  273. lle->la_hold = next;
  274. lle->la_numheld--;
  275. pkts_dropped++;
  276. }
  277. KASSERT(lle->la_numheld == 0,
  278. ("%s: la_numheld %d > 0, pkts_dropped %zd", __func__,
  279. lle->la_numheld, pkts_dropped));
  280. return (pkts_dropped);
  281. }
  282. void
  283. lltable_set_entry_addr(struct ifnet *ifp, struct llentry *lle,
  284. const char *linkhdr, size_t linkhdrsize, int lladdr_off)
  285. {
  286. memcpy(lle->r_linkdata, linkhdr, linkhdrsize);
  287. lle->r_hdrlen = linkhdrsize;
  288. lle->ll_addr = &lle->r_linkdata[lladdr_off];
  289. lle->la_flags |= LLE_VALID;
  290. lle->r_flags |= RLLE_VALID;
  291. }
  292. /*
  293. * Acquires lltable write lock.
  294. *
  295. * Returns true on success, with both lltable and lle lock held.
  296. * On failure, false is returned and lle wlock is still held.
  297. */
  298. bool
  299. lltable_acquire_wlock(struct ifnet *ifp, struct llentry *lle)
  300. {
  301. NET_EPOCH_ASSERT();
  302. /* Perform real LLE update */
  303. /* use afdata WLOCK to update fields */
  304. LLE_WUNLOCK(lle);
  305. IF_AFDATA_WLOCK(ifp);
  306. LLE_WLOCK(lle);
  307. /*
  308. * Since we droppped LLE lock, other thread might have deleted
  309. * this lle. Check and return
  310. */
  311. if ((lle->la_flags & LLE_DELETED) != 0) {
  312. IF_AFDATA_WUNLOCK(ifp);
  313. return (false);
  314. }
  315. return (true);
  316. }
  317. /*
  318. * Tries to update @lle link-level address.
  319. * Since update requires AFDATA WLOCK, function
  320. * drops @lle lock, acquires AFDATA lock and then acquires
  321. * @lle lock to maintain lock order.
  322. *
  323. * Returns 1 on success.
  324. */
  325. int
  326. lltable_try_set_entry_addr(struct ifnet *ifp, struct llentry *lle,
  327. const char *linkhdr, size_t linkhdrsize, int lladdr_off)
  328. {
  329. if (!lltable_acquire_wlock(ifp, lle))
  330. return (0);
  331. /* Update data */
  332. lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize, lladdr_off);
  333. IF_AFDATA_WUNLOCK(ifp);
  334. return (1);
  335. }
  336. /*
  337. * Helper function used to pre-compute full/partial link-layer
  338. * header data suitable for feeding into if_output().
  339. */
  340. int
  341. lltable_calc_llheader(struct ifnet *ifp, int family, char *lladdr,
  342. char *buf, size_t *bufsize, int *lladdr_off)
  343. {
  344. struct if_encap_req ereq;
  345. int error;
  346. bzero(buf, *bufsize);
  347. bzero(&ereq, sizeof(ereq));
  348. ereq.buf = buf;
  349. ereq.bufsize = *bufsize;
  350. ereq.rtype = IFENCAP_LL;
  351. ereq.family = family;
  352. ereq.lladdr = lladdr;
  353. ereq.lladdr_len = ifp->if_addrlen;
  354. error = ifp->if_requestencap(ifp, &ereq);
  355. if (error == 0) {
  356. *bufsize = ereq.bufsize;
  357. *lladdr_off = ereq.lladdr_off;
  358. }
  359. return (error);
  360. }
  361. /*
  362. * Searches for the child entry matching @family inside @lle.
  363. * Returns the entry or NULL.
  364. */
  365. struct llentry *
  366. llentry_lookup_family(struct llentry *lle, int family)
  367. {
  368. struct llentry *child_lle;
  369. if (lle == NULL)
  370. return (NULL);
  371. CK_SLIST_FOREACH(child_lle, &lle->lle_children, lle_child_next) {
  372. if (child_lle->r_family == family)
  373. return (child_lle);
  374. }
  375. return (NULL);
  376. }
  377. /*
  378. * Retrieves upper protocol family for the llentry.
  379. * By default, all "normal" (e.g. upper_family == transport_family)
  380. * llentries have r_family set to 0.
  381. * Thus, use @default_family in that regard, otherwise use r_family.
  382. *
  383. * Returns upper protocol family
  384. */
  385. int
  386. llentry_get_upper_family(const struct llentry *lle, int default_family)
  387. {
  388. return (lle->r_family == 0 ? default_family : lle->r_family);
  389. }
  390. /*
  391. * Prints llentry @lle data into provided buffer.
  392. * Example: lle/inet/valid/em0/1.2.3.4
  393. *
  394. * Returns @buf.
  395. */
  396. char *
  397. llentry_print_buf(const struct llentry *lle, struct ifnet *ifp, int family,
  398. char *buf, size_t bufsize)
  399. {
  400. #if defined(INET) || defined(INET6)
  401. char abuf[INET6_ADDRSTRLEN];
  402. #endif
  403. const char *valid = (lle->r_flags & RLLE_VALID) ? "valid" : "no_l2";
  404. const char *upper_str = rib_print_family(llentry_get_upper_family(lle, family));
  405. switch (family) {
  406. #ifdef INET
  407. case AF_INET:
  408. inet_ntop(AF_INET, &lle->r_l3addr.addr4, abuf, sizeof(abuf));
  409. snprintf(buf, bufsize, "lle/%s/%s/%s/%s", upper_str,
  410. valid, if_name(ifp), abuf);
  411. break;
  412. #endif
  413. #ifdef INET6
  414. case AF_INET6:
  415. inet_ntop(AF_INET6, &lle->r_l3addr.addr6, abuf, sizeof(abuf));
  416. snprintf(buf, bufsize, "lle/%s/%s/%s/%s", upper_str,
  417. valid, if_name(ifp), abuf);
  418. break;
  419. #endif
  420. default:
  421. snprintf(buf, bufsize, "lle/%s/%s/%s/????", upper_str,
  422. valid, if_name(ifp));
  423. break;
  424. }
  425. return (buf);
  426. }
  427. char *
  428. llentry_print_buf_lltable(const struct llentry *lle, char *buf, size_t bufsize)
  429. {
  430. struct lltable *tbl = lle->lle_tbl;
  431. return (llentry_print_buf(lle, lltable_get_ifp(tbl), lltable_get_af(tbl), buf, bufsize));
  432. }
  433. /*
  434. * Requests feedback from the datapath.
  435. * First packet using @lle should result in
  436. * setting r_skip_req back to 0 and updating
  437. * lle_hittime to the current time_uptime.
  438. */
  439. void
  440. llentry_request_feedback(struct llentry *lle)
  441. {
  442. struct llentry *child_lle;
  443. LLE_REQ_LOCK(lle);
  444. lle->r_skip_req = 1;
  445. LLE_REQ_UNLOCK(lle);
  446. CK_SLIST_FOREACH(child_lle, &lle->lle_children, lle_child_next) {
  447. LLE_REQ_LOCK(child_lle);
  448. child_lle->r_skip_req = 1;
  449. LLE_REQ_UNLOCK(child_lle);
  450. }
  451. }
  452. /*
  453. * Updates the lle state to mark it has been used
  454. * and record the time.
  455. * Used by the llentry_provide_feedback() wrapper.
  456. */
  457. void
  458. llentry_mark_used(struct llentry *lle)
  459. {
  460. LLE_REQ_LOCK(lle);
  461. lle->r_skip_req = 0;
  462. lle->lle_hittime = time_uptime;
  463. LLE_REQ_UNLOCK(lle);
  464. }
  465. /*
  466. * Fetches the time when lle was used.
  467. * Return 0 if the entry was not used, relevant time_uptime
  468. * otherwise.
  469. */
  470. static time_t
  471. llentry_get_hittime_raw(struct llentry *lle)
  472. {
  473. time_t lle_hittime = 0;
  474. LLE_REQ_LOCK(lle);
  475. if ((lle->r_skip_req == 0) && (lle_hittime < lle->lle_hittime))
  476. lle_hittime = lle->lle_hittime;
  477. LLE_REQ_UNLOCK(lle);
  478. return (lle_hittime);
  479. }
  480. time_t
  481. llentry_get_hittime(struct llentry *lle)
  482. {
  483. time_t lle_hittime = 0;
  484. struct llentry *child_lle;
  485. lle_hittime = llentry_get_hittime_raw(lle);
  486. CK_SLIST_FOREACH(child_lle, &lle->lle_children, lle_child_next) {
  487. time_t hittime = llentry_get_hittime_raw(child_lle);
  488. if (hittime > lle_hittime)
  489. lle_hittime = hittime;
  490. }
  491. return (lle_hittime);
  492. }
  493. /*
  494. * Update link-layer header for given @lle after
  495. * interface lladdr was changed.
  496. */
  497. static int
  498. llentry_update_ifaddr(struct lltable *llt, struct llentry *lle, void *farg)
  499. {
  500. struct ifnet *ifp;
  501. u_char linkhdr[LLE_MAX_LINKHDR];
  502. size_t linkhdrsize;
  503. u_char *lladdr;
  504. int lladdr_off;
  505. ifp = (struct ifnet *)farg;
  506. lladdr = lle->ll_addr;
  507. LLE_WLOCK(lle);
  508. if ((lle->la_flags & LLE_VALID) == 0) {
  509. LLE_WUNLOCK(lle);
  510. return (0);
  511. }
  512. if ((lle->la_flags & LLE_IFADDR) != 0)
  513. lladdr = IF_LLADDR(ifp);
  514. linkhdrsize = sizeof(linkhdr);
  515. lltable_calc_llheader(ifp, llt->llt_af, lladdr, linkhdr, &linkhdrsize,
  516. &lladdr_off);
  517. memcpy(lle->r_linkdata, linkhdr, linkhdrsize);
  518. LLE_WUNLOCK(lle);
  519. return (0);
  520. }
  521. /*
  522. * Update all calculated headers for given @llt
  523. */
  524. void
  525. lltable_update_ifaddr(struct lltable *llt)
  526. {
  527. if (llt->llt_ifp->if_flags & IFF_LOOPBACK)
  528. return;
  529. IF_AFDATA_WLOCK(llt->llt_ifp);
  530. lltable_foreach_lle(llt, llentry_update_ifaddr, llt->llt_ifp);
  531. IF_AFDATA_WUNLOCK(llt->llt_ifp);
  532. }
  533. /*
  534. *
  535. * Performs generic cleanup routines and frees lle.
  536. *
  537. * Called for non-linked entries, with callouts and
  538. * other AF-specific cleanups performed.
  539. *
  540. * @lle must be passed WLOCK'ed
  541. *
  542. * Returns the number of held packets, if any, that were dropped.
  543. */
  544. size_t
  545. llentry_free(struct llentry *lle)
  546. {
  547. size_t pkts_dropped;
  548. LLE_WLOCK_ASSERT(lle);
  549. KASSERT((lle->la_flags & LLE_LINKED) == 0, ("freeing linked lle"));
  550. pkts_dropped = lltable_drop_entry_queue(lle);
  551. /* cancel timer */
  552. if (callout_stop(&lle->lle_timer) > 0)
  553. LLE_REMREF(lle);
  554. LLE_FREE_LOCKED(lle);
  555. return (pkts_dropped);
  556. }
  557. /*
  558. * Free all entries from given table and free itself.
  559. */
  560. static int
  561. lltable_free_cb(struct lltable *llt, struct llentry *lle, void *farg)
  562. {
  563. struct llentries *dchain;
  564. dchain = (struct llentries *)farg;
  565. LLE_WLOCK(lle);
  566. CK_LIST_INSERT_HEAD(dchain, lle, lle_chain);
  567. return (0);
  568. }
  569. /*
  570. * Free all entries from given table and free itself.
  571. */
  572. void
  573. lltable_free(struct lltable *llt)
  574. {
  575. struct llentry *lle, *next;
  576. struct llentries dchain;
  577. KASSERT(llt != NULL, ("%s: llt is NULL", __func__));
  578. lltable_unlink(llt);
  579. CK_LIST_INIT(&dchain);
  580. IF_AFDATA_WLOCK(llt->llt_ifp);
  581. /* Push all lles to @dchain */
  582. lltable_foreach_lle(llt, lltable_free_cb, &dchain);
  583. llentries_unlink(llt, &dchain);
  584. IF_AFDATA_WUNLOCK(llt->llt_ifp);
  585. CK_LIST_FOREACH_SAFE(lle, &dchain, lle_chain, next) {
  586. llentry_free(lle);
  587. }
  588. KASSERT(llt->llt_entries == 0, ("%s: lltable %p (%s) entries not 0: %d",
  589. __func__, llt, llt->llt_ifp->if_xname, llt->llt_entries));
  590. llt->llt_free_tbl(llt);
  591. }
  592. /*
  593. * Deletes an address from given lltable.
  594. * Used for userland interaction to remove
  595. * individual entries. Skips entries added by OS.
  596. */
  597. int
  598. lltable_delete_addr(struct lltable *llt, u_int flags,
  599. const struct sockaddr *l3addr)
  600. {
  601. struct llentry *lle;
  602. struct ifnet *ifp;
  603. ifp = llt->llt_ifp;
  604. IF_AFDATA_WLOCK(ifp);
  605. lle = lla_lookup(llt, LLE_SF(l3addr->sa_family, LLE_EXCLUSIVE), l3addr);
  606. if (lle == NULL) {
  607. IF_AFDATA_WUNLOCK(ifp);
  608. return (ENOENT);
  609. }
  610. if ((lle->la_flags & LLE_IFADDR) != 0 && (flags & LLE_IFADDR) == 0) {
  611. IF_AFDATA_WUNLOCK(ifp);
  612. LLE_WUNLOCK(lle);
  613. return (EPERM);
  614. }
  615. lltable_unlink_entry(llt, lle);
  616. IF_AFDATA_WUNLOCK(ifp);
  617. llt->llt_delete_entry(llt, lle);
  618. return (0);
  619. }
  620. void
  621. lltable_prefix_free(int af, struct sockaddr *addr, struct sockaddr *mask,
  622. u_int flags)
  623. {
  624. struct lltable *llt;
  625. LLTABLE_LIST_RLOCK();
  626. SLIST_FOREACH(llt, &V_lltables, llt_link) {
  627. if (llt->llt_af != af)
  628. continue;
  629. llt->llt_prefix_free(llt, addr, mask, flags);
  630. }
  631. LLTABLE_LIST_RUNLOCK();
  632. }
  633. /*
  634. * Delete llentries that func() returns true.
  635. */
  636. struct lle_match_data {
  637. struct llentries dchain;
  638. llt_match_cb_t *func;
  639. void *farg;
  640. };
  641. static int
  642. lltable_delete_conditional_cb(struct lltable *llt, struct llentry *lle,
  643. void *farg)
  644. {
  645. struct lle_match_data *lmd;
  646. lmd = (struct lle_match_data *)farg;
  647. if (lmd->func(llt, lle, lmd->farg)) {
  648. LLE_WLOCK(lle);
  649. CK_LIST_INSERT_HEAD(&lmd->dchain, lle, lle_chain);
  650. }
  651. return (0);
  652. }
  653. void
  654. lltable_delete_conditional(struct lltable *llt, llt_match_cb_t *func,
  655. void *farg)
  656. {
  657. struct llentry *lle, *next;
  658. struct lle_match_data lmd;
  659. bzero(&lmd, sizeof(lmd));
  660. CK_LIST_INIT(&lmd.dchain);
  661. lmd.func = func;
  662. lmd.farg = farg;
  663. IF_AFDATA_WLOCK(llt->llt_ifp);
  664. lltable_foreach_lle(llt, lltable_delete_conditional_cb, &lmd);
  665. llentries_unlink(llt, &lmd.dchain);
  666. IF_AFDATA_WUNLOCK(llt->llt_ifp);
  667. CK_LIST_FOREACH_SAFE(lle, &lmd.dchain, lle_chain, next)
  668. llt->llt_delete_entry(llt, lle);
  669. }
  670. struct lltable *
  671. lltable_allocate_htbl(uint32_t hsize)
  672. {
  673. struct lltable *llt;
  674. int i;
  675. llt = malloc(sizeof(struct lltable), M_LLTABLE, M_WAITOK | M_ZERO);
  676. llt->llt_hsize = hsize;
  677. llt->lle_head = malloc(sizeof(struct llentries) * hsize,
  678. M_LLTABLE, M_WAITOK | M_ZERO);
  679. for (i = 0; i < llt->llt_hsize; i++)
  680. CK_LIST_INIT(&llt->lle_head[i]);
  681. /* Set some default callbacks */
  682. llt->llt_link_entry = htable_link_entry;
  683. llt->llt_unlink_entry = htable_unlink_entry;
  684. llt->llt_prefix_free = htable_prefix_free;
  685. llt->llt_foreach_entry = htable_foreach_lle;
  686. llt->llt_free_tbl = htable_free_tbl;
  687. return (llt);
  688. }
  689. /*
  690. * Links lltable to global llt list.
  691. */
  692. void
  693. lltable_link(struct lltable *llt)
  694. {
  695. LLTABLE_LIST_WLOCK();
  696. SLIST_INSERT_HEAD(&V_lltables, llt, llt_link);
  697. LLTABLE_LIST_WUNLOCK();
  698. }
  699. static void
  700. lltable_unlink(struct lltable *llt)
  701. {
  702. LLTABLE_LIST_WLOCK();
  703. SLIST_REMOVE(&V_lltables, llt, lltable, llt_link);
  704. LLTABLE_LIST_WUNLOCK();
  705. }
  706. /*
  707. * Gets interface @ifp lltable for the specified @family
  708. */
  709. struct lltable *
  710. lltable_get(struct ifnet *ifp, int family)
  711. {
  712. switch (family) {
  713. #ifdef INET
  714. case AF_INET:
  715. return (in_lltable_get(ifp));
  716. #endif
  717. #ifdef INET6
  718. case AF_INET6:
  719. return (in6_lltable_get(ifp));
  720. #endif
  721. }
  722. return (NULL);
  723. }
  724. /*
  725. * External methods used by lltable consumers
  726. */
  727. int
  728. lltable_foreach_lle(struct lltable *llt, llt_foreach_cb_t *f, void *farg)
  729. {
  730. return (llt->llt_foreach_entry(llt, f, farg));
  731. }
  732. struct llentry *
  733. lltable_alloc_entry(struct lltable *llt, u_int flags,
  734. const struct sockaddr *l3addr)
  735. {
  736. return (llt->llt_alloc_entry(llt, flags, l3addr));
  737. }
  738. void
  739. lltable_free_entry(struct lltable *llt, struct llentry *lle)
  740. {
  741. llt->llt_free_entry(llt, lle);
  742. }
  743. int
  744. lltable_link_entry(struct lltable *llt, struct llentry *lle)
  745. {
  746. int error = llt->llt_link_entry(llt, lle);
  747. if (error == 0 && (lle->la_flags & LLE_PUB) != 0)
  748. llt->llt_flags |= LLT_ADDEDPROXY;
  749. return (error);
  750. }
  751. void
  752. lltable_link_child_entry(struct llentry *lle, struct llentry *child_lle)
  753. {
  754. child_lle->lle_parent = lle;
  755. child_lle->lle_tbl = lle->lle_tbl;
  756. child_lle->la_flags |= LLE_LINKED;
  757. CK_SLIST_INSERT_HEAD(&lle->lle_children, child_lle, lle_child_next);
  758. }
  759. void
  760. lltable_unlink_child_entry(struct llentry *child_lle)
  761. {
  762. struct llentry *lle = child_lle->lle_parent;
  763. child_lle->la_flags &= ~LLE_LINKED;
  764. child_lle->lle_parent = NULL;
  765. CK_SLIST_REMOVE(&lle->lle_children, child_lle, llentry, lle_child_next);
  766. }
  767. int
  768. lltable_unlink_entry(struct lltable *llt, struct llentry *lle)
  769. {
  770. return (llt->llt_unlink_entry(lle));
  771. }
  772. void
  773. lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
  774. {
  775. struct lltable *llt;
  776. llt = lle->lle_tbl;
  777. llt->llt_fill_sa_entry(lle, sa);
  778. }
  779. struct ifnet *
  780. lltable_get_ifp(const struct lltable *llt)
  781. {
  782. return (llt->llt_ifp);
  783. }
  784. int
  785. lltable_get_af(const struct lltable *llt)
  786. {
  787. return (llt->llt_af);
  788. }
  789. /*
  790. * Called in route_output when rtm_flags contains RTF_LLDATA.
  791. */
  792. int
  793. lla_rt_output(struct rt_msghdr *rtm, struct rt_addrinfo *info)
  794. {
  795. struct sockaddr_dl *dl =
  796. (struct sockaddr_dl *)info->rti_info[RTAX_GATEWAY];
  797. struct sockaddr *dst = (struct sockaddr *)info->rti_info[RTAX_DST];
  798. struct ifnet *ifp;
  799. struct lltable *llt;
  800. struct llentry *lle, *lle_tmp;
  801. uint8_t linkhdr[LLE_MAX_LINKHDR];
  802. size_t linkhdrsize;
  803. int lladdr_off;
  804. u_int laflags = 0;
  805. int error;
  806. if (dl == NULL || dl->sdl_family != AF_LINK)
  807. return (EINVAL);
  808. /* XXX: should be ntohs() */
  809. ifp = ifnet_byindex(dl->sdl_index);
  810. if (ifp == NULL) {
  811. log(LOG_INFO, "%s: invalid ifp (sdl_index %d)\n",
  812. __func__, dl->sdl_index);
  813. return EINVAL;
  814. }
  815. llt = lltable_get(ifp, dst->sa_family);
  816. if (llt == NULL)
  817. return (ESRCH);
  818. error = 0;
  819. switch (rtm->rtm_type) {
  820. case RTM_ADD:
  821. /* Add static LLE */
  822. laflags = 0;
  823. if (rtm->rtm_rmx.rmx_expire == 0)
  824. laflags = LLE_STATIC;
  825. lle = lltable_alloc_entry(llt, laflags, dst);
  826. if (lle == NULL)
  827. return (ENOMEM);
  828. linkhdrsize = sizeof(linkhdr);
  829. if (lltable_calc_llheader(ifp, dst->sa_family, LLADDR(dl),
  830. linkhdr, &linkhdrsize, &lladdr_off) != 0) {
  831. lltable_free_entry(llt, lle);
  832. return (EINVAL);
  833. }
  834. lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
  835. lladdr_off);
  836. if ((rtm->rtm_flags & RTF_ANNOUNCE))
  837. lle->la_flags |= LLE_PUB;
  838. lle->la_expire = rtm->rtm_rmx.rmx_expire;
  839. laflags = lle->la_flags;
  840. /* Try to link new entry */
  841. lle_tmp = NULL;
  842. IF_AFDATA_WLOCK(ifp);
  843. LLE_WLOCK(lle);
  844. lle_tmp = lla_lookup(llt, LLE_EXCLUSIVE, dst);
  845. if (lle_tmp != NULL) {
  846. /* Check if we are trying to replace immutable entry */
  847. if ((lle_tmp->la_flags & LLE_IFADDR) != 0) {
  848. IF_AFDATA_WUNLOCK(ifp);
  849. LLE_WUNLOCK(lle_tmp);
  850. lltable_free_entry(llt, lle);
  851. return (EPERM);
  852. }
  853. /* Unlink existing entry from table */
  854. lltable_unlink_entry(llt, lle_tmp);
  855. }
  856. lltable_link_entry(llt, lle);
  857. IF_AFDATA_WUNLOCK(ifp);
  858. if (lle_tmp != NULL) {
  859. EVENTHANDLER_INVOKE(lle_event, lle_tmp,LLENTRY_EXPIRED);
  860. lltable_free_entry(llt, lle_tmp);
  861. }
  862. /*
  863. * By invoking LLE handler here we might get
  864. * two events on static LLE entry insertion
  865. * in routing socket. However, since we might have
  866. * other subscribers we need to generate this event.
  867. */
  868. EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_RESOLVED);
  869. LLE_WUNLOCK(lle);
  870. llt->llt_post_resolved(llt, lle);
  871. break;
  872. case RTM_DELETE:
  873. return (lltable_delete_addr(llt, 0, dst));
  874. default:
  875. error = EINVAL;
  876. }
  877. return (error);
  878. }
  879. #ifdef DDB
  880. static void
  881. llatbl_lle_show(struct llentry *lle)
  882. {
  883. uint8_t octet[6];
  884. sa_family_t af = AF_UNSPEC;
  885. char l3_addr_fmt[] = " l3_addr=%s (af=%d)\n";
  886. db_printf("lle=%p\n", lle);
  887. db_printf(" lle_next=%p\n", lle->lle_next.cle_next);
  888. db_printf(" lle_lock=%p\n", &lle->lle_lock);
  889. db_printf(" lle_tbl=%p\n", lle->lle_tbl);
  890. db_printf(" lle_head=%p\n", lle->lle_head);
  891. db_printf(" la_hold=%p\n", lle->la_hold);
  892. db_printf(" la_numheld=%d\n", lle->la_numheld);
  893. db_printf(" la_expire=%ju\n", (uintmax_t)lle->la_expire);
  894. db_printf(" la_flags=0x%04x\n", lle->la_flags);
  895. db_printf(" la_asked=%u\n", lle->la_asked);
  896. db_printf(" la_preempt=%u\n", lle->la_preempt);
  897. db_printf(" ln_state=%d\n", lle->ln_state);
  898. db_printf(" ln_router=%u\n", lle->ln_router);
  899. db_printf(" ln_ntick=%ju\n", (uintmax_t)lle->ln_ntick);
  900. db_printf(" lle_refcnt=%d\n", lle->lle_refcnt);
  901. bcopy(lle->ll_addr, octet, sizeof(octet));
  902. db_printf(" ll_addr=%02x:%02x:%02x:%02x:%02x:%02x\n",
  903. octet[0], octet[1], octet[2], octet[3], octet[4], octet[5]);
  904. db_printf(" lle_timer=%p\n", &lle->lle_timer);
  905. if (lle->lle_tbl) {
  906. af = lle->lle_tbl->llt_af;
  907. }
  908. switch (af) {
  909. #ifdef INET
  910. case AF_INET:
  911. {
  912. struct sockaddr_in sin;
  913. char l3s[INET_ADDRSTRLEN];
  914. lltable_fill_sa_entry(lle, (struct sockaddr *)&sin);
  915. (void) inet_ntop(af, &sin.sin_addr, l3s, sizeof(l3s));
  916. db_printf(l3_addr_fmt, l3s, af);
  917. break;
  918. }
  919. #endif
  920. #ifdef INET6
  921. case AF_INET6:
  922. {
  923. struct sockaddr_in6 sin6;
  924. char l3s[INET6_ADDRSTRLEN];
  925. lltable_fill_sa_entry(lle, (struct sockaddr *)&sin6);
  926. (void) inet_ntop(af, &sin6.sin6_addr, l3s, sizeof(l3s));
  927. db_printf(l3_addr_fmt, l3s, af);
  928. break;
  929. }
  930. #endif
  931. default:
  932. db_printf(l3_addr_fmt, "N/A", af);
  933. break;
  934. }
  935. }
  936. DB_SHOW_COMMAND(llentry, db_show_llentry)
  937. {
  938. if (!have_addr) {
  939. db_printf("usage: show llentry <struct llentry *>\n");
  940. return;
  941. }
  942. llatbl_lle_show((struct llentry *)addr);
  943. }
  944. static void
  945. llatbl_llt_show(struct lltable *llt)
  946. {
  947. int i;
  948. struct llentry *lle;
  949. db_printf("llt=%p llt_af=%d llt_ifp=%p\n",
  950. llt, llt->llt_af, llt->llt_ifp);
  951. for (i = 0; i < llt->llt_hsize; i++) {
  952. CK_LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
  953. llatbl_lle_show(lle);
  954. if (db_pager_quit)
  955. return;
  956. }
  957. }
  958. }
  959. DB_SHOW_COMMAND(lltable, db_show_lltable)
  960. {
  961. if (!have_addr) {
  962. db_printf("usage: show lltable <struct lltable *>\n");
  963. return;
  964. }
  965. llatbl_llt_show((struct lltable *)addr);
  966. }
  967. DB_SHOW_ALL_COMMAND(lltables, db_show_all_lltables)
  968. {
  969. VNET_ITERATOR_DECL(vnet_iter);
  970. struct lltable *llt;
  971. VNET_FOREACH(vnet_iter) {
  972. CURVNET_SET_QUIET(vnet_iter);
  973. #ifdef VIMAGE
  974. db_printf("vnet=%p\n", curvnet);
  975. #endif
  976. SLIST_FOREACH(llt, &V_lltables, llt_link) {
  977. db_printf("llt=%p llt_af=%d llt_ifp=%p(%s)\n",
  978. llt, llt->llt_af, llt->llt_ifp,
  979. (llt->llt_ifp != NULL) ?
  980. llt->llt_ifp->if_xname : "?");
  981. if (have_addr && addr != 0) /* verbose */
  982. llatbl_llt_show(llt);
  983. if (db_pager_quit) {
  984. CURVNET_RESTORE();
  985. return;
  986. }
  987. }
  988. CURVNET_RESTORE();
  989. }
  990. }
  991. #endif