]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - sys/net/if_llatbl.c
Merge r238989:
[FreeBSD/stable/9.git] / sys / net / if_llatbl.c
1 /*
2  * Copyright (c) 2004 Luigi Rizzo, Alessandro Cerri. All rights reserved.
3  * Copyright (c) 2004-2008 Qing Li. All rights reserved.
4  * Copyright (c) 2008 Kip Macy. All rights reserved.
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
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.
14  * 
15  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_ddb.h"
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.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
46 #ifdef DDB
47 #include <ddb/ddb.h>
48 #endif
49
50 #include <vm/uma.h>
51
52 #include <netinet/in.h>
53 #include <net/if_llatbl.h>
54 #include <net/if.h>
55 #include <net/if_dl.h>
56 #include <net/if_var.h>
57 #include <net/route.h>
58 #include <net/vnet.h>
59 #include <netinet/if_ether.h>
60 #include <netinet6/in6_var.h>
61 #include <netinet6/nd6.h>
62
63 MALLOC_DEFINE(M_LLTABLE, "lltable", "link level address tables");
64
65 static VNET_DEFINE(SLIST_HEAD(, lltable), lltables);
66 #define V_lltables      VNET(lltables)
67
68 extern void arprequest(struct ifnet *, struct in_addr *, struct in_addr *,
69         u_char *);
70
71 static void vnet_lltable_init(void);
72
73 struct rwlock lltable_rwlock;
74 RW_SYSINIT(lltable_rwlock, &lltable_rwlock, "lltable_rwlock");
75
76 /*
77  * Dump arp state for a specific address family.
78  */
79 int
80 lltable_sysctl_dumparp(int af, struct sysctl_req *wr)
81 {
82         struct lltable *llt;
83         int error = 0;
84
85         LLTABLE_RLOCK();
86         SLIST_FOREACH(llt, &V_lltables, llt_link) {
87                 if (llt->llt_af == af) {
88                         error = llt->llt_dump(llt, wr);
89                         if (error != 0)
90                                 goto done;
91                 }
92         }
93 done:
94         LLTABLE_RUNLOCK();
95         return (error);
96 }
97
98 /*
99  * Deletes an address from the address table.
100  * This function is called by the timer functions
101  * such as arptimer() and nd6_llinfo_timer(), and
102  * the caller does the locking.
103  *
104  * Returns the number of held packets, if any, that were dropped.
105  */
106 size_t
107 llentry_free(struct llentry *lle)
108 {
109         size_t pkts_dropped;
110         struct mbuf *next;
111
112         pkts_dropped = 0;
113         LLE_WLOCK_ASSERT(lle);
114         LIST_REMOVE(lle, lle_next);
115
116         while ((lle->la_numheld > 0) && (lle->la_hold != NULL)) {
117                 next = lle->la_hold->m_nextpkt;
118                 m_freem(lle->la_hold);
119                 lle->la_hold = next;
120                 lle->la_numheld--;
121                 pkts_dropped++;
122         }
123
124         KASSERT(lle->la_numheld == 0,
125                 ("%s: la_numheld %d > 0, pkts_droped %zd", __func__,
126                  lle->la_numheld, pkts_dropped));
127
128         lle->la_flags &= ~LLE_VALID;
129         LLE_FREE_LOCKED(lle);
130
131         return (pkts_dropped);
132 }
133
134 /*
135  * (al)locate an llentry for address dst (equivalent to rtalloc for new-arp).
136  *
137  * If found the llentry * is returned referenced and unlocked.
138  */
139 struct llentry *
140 llentry_alloc(struct ifnet *ifp, struct lltable *lt,
141     struct sockaddr_storage *dst)
142 {
143         struct llentry *la;
144
145         IF_AFDATA_RLOCK(ifp);
146         la = lla_lookup(lt, LLE_EXCLUSIVE, (struct sockaddr *)dst);
147         IF_AFDATA_RUNLOCK(ifp);
148         if ((la == NULL) &&
149             (ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) == 0) {
150                 IF_AFDATA_WLOCK(ifp);
151                 la = lla_lookup(lt, (LLE_CREATE | LLE_EXCLUSIVE),
152                     (struct sockaddr *)dst);
153                 IF_AFDATA_WUNLOCK(ifp);
154         }
155
156         if (la != NULL) {
157                 LLE_ADDREF(la);
158                 LLE_WUNLOCK(la);
159         }
160
161         return (la);
162 }
163
164 /*
165  * Free all entries from given table and free itself.
166  */
167 void
168 lltable_free(struct lltable *llt)
169 {
170         struct llentry *lle, *next;
171         int i;
172
173         KASSERT(llt != NULL, ("%s: llt is NULL", __func__));
174
175         LLTABLE_WLOCK();
176         SLIST_REMOVE(&V_lltables, llt, lltable, llt_link);
177         LLTABLE_WUNLOCK();
178
179         for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
180                 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
181                         int canceled;
182
183                         canceled = callout_drain(&lle->la_timer);
184                         LLE_WLOCK(lle);
185                         if (canceled)
186                                 LLE_REMREF(lle);
187                         llentry_free(lle);
188                 }
189         }
190
191         free(llt, M_LLTABLE);
192 }
193
194 #if 0
195 void
196 lltable_drain(int af)
197 {
198         struct lltable  *llt;
199         struct llentry  *lle;
200         register int i;
201
202         LLTABLE_RLOCK();
203         SLIST_FOREACH(llt, &V_lltables, llt_link) {
204                 if (llt->llt_af != af)
205                         continue;
206
207                 for (i=0; i < LLTBL_HASHTBL_SIZE; i++) {
208                         LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
209                                 LLE_WLOCK(lle);
210                                 if (lle->la_hold) {
211                                         m_freem(lle->la_hold);
212                                         lle->la_hold = NULL;
213                                 }
214                                 LLE_WUNLOCK(lle);
215                         }
216                 }
217         }
218         LLTABLE_RUNLOCK();
219 }
220 #endif
221
222 void
223 lltable_prefix_free(int af, struct sockaddr *prefix, struct sockaddr *mask,
224     u_int flags)
225 {
226         struct lltable *llt;
227
228         LLTABLE_RLOCK();
229         SLIST_FOREACH(llt, &V_lltables, llt_link) {
230                 if (llt->llt_af != af)
231                         continue;
232
233                 llt->llt_prefix_free(llt, prefix, mask, flags);
234         }
235         LLTABLE_RUNLOCK();
236 }
237
238
239
240 /*
241  * Create a new lltable.
242  */
243 struct lltable *
244 lltable_init(struct ifnet *ifp, int af)
245 {
246         struct lltable *llt;
247         register int i;
248
249         llt = malloc(sizeof(struct lltable), M_LLTABLE, M_WAITOK);
250
251         llt->llt_af = af;
252         llt->llt_ifp = ifp;
253         for (i = 0; i < LLTBL_HASHTBL_SIZE; i++)
254                 LIST_INIT(&llt->lle_head[i]);
255
256         LLTABLE_WLOCK();
257         SLIST_INSERT_HEAD(&V_lltables, llt, llt_link);
258         LLTABLE_WUNLOCK();
259
260         return (llt);
261 }
262
263 /*
264  * Called in route_output when adding/deleting a route to an interface.
265  */
266 int
267 lla_rt_output(struct rt_msghdr *rtm, struct rt_addrinfo *info)
268 {
269         struct sockaddr_dl *dl =
270             (struct sockaddr_dl *)info->rti_info[RTAX_GATEWAY];
271         struct sockaddr *dst = (struct sockaddr *)info->rti_info[RTAX_DST];
272         struct ifnet *ifp;
273         struct lltable *llt;
274         struct llentry *lle;
275         u_int laflags = 0, flags = 0;
276         int error = 0;
277
278         if (dl == NULL || dl->sdl_family != AF_LINK) {
279                 log(LOG_INFO, "%s: invalid dl\n", __func__);
280                 return EINVAL;
281         }
282         ifp = ifnet_byindex(dl->sdl_index);
283         if (ifp == NULL) {
284                 log(LOG_INFO, "%s: invalid ifp (sdl_index %d)\n",
285                     __func__, dl->sdl_index);
286                 return EINVAL;
287         }
288
289         switch (rtm->rtm_type) {
290         case RTM_ADD:
291                 if (rtm->rtm_flags & RTF_ANNOUNCE) {
292                         flags |= LLE_PUB;
293 #ifdef INET
294                         if (dst->sa_family == AF_INET &&
295                             ((struct sockaddr_inarp *)dst)->sin_other != 0) {
296                                 struct rtentry *rt;
297                                 ((struct sockaddr_inarp *)dst)->sin_other = 0;
298                                 rt = rtalloc1(dst, 0, 0);
299                                 if (rt == NULL || !(rt->rt_flags & RTF_HOST)) {
300                                         log(LOG_INFO, "%s: RTM_ADD publish "
301                                             "(proxy only) is invalid\n",
302                                             __func__);
303                                         if (rt)
304                                                 RTFREE_LOCKED(rt);
305                                         return EINVAL;
306                                 }
307                                 RTFREE_LOCKED(rt);
308
309                                 flags |= LLE_PROXY;
310                         }
311 #endif
312                 }
313                 flags |= LLE_CREATE;
314                 break;
315
316         case RTM_DELETE:
317                 flags |= LLE_DELETE;
318                 break;
319
320         case RTM_CHANGE:
321                 break;
322
323         default:
324                 return EINVAL; /* XXX not implemented yet */
325         }
326
327         /* XXX linked list may be too expensive */
328         LLTABLE_RLOCK();
329         SLIST_FOREACH(llt, &V_lltables, llt_link) {
330                 if (llt->llt_af == dst->sa_family &&
331                     llt->llt_ifp == ifp)
332                         break;
333         }
334         LLTABLE_RUNLOCK();
335         KASSERT(llt != NULL, ("Yep, ugly hacks are bad\n"));
336
337         if (flags & LLE_CREATE)
338                 flags |= LLE_EXCLUSIVE;
339
340         IF_AFDATA_LOCK(ifp);
341         lle = lla_lookup(llt, flags, dst);
342         IF_AFDATA_UNLOCK(ifp);
343         if (LLE_IS_VALID(lle)) {
344                 if (flags & LLE_CREATE) {
345                         /*
346                          * If we delay the delete, then a subsequent
347                          * "arp add" should look up this entry, reset the
348                          * LLE_DELETED flag, and reset the expiration timer
349                          */
350                         bcopy(LLADDR(dl), &lle->ll_addr, ifp->if_addrlen);
351                         lle->la_flags |= (flags & (LLE_PUB | LLE_PROXY));
352                         lle->la_flags |= LLE_VALID;
353                         lle->la_flags &= ~LLE_DELETED;
354 #ifdef INET6
355                         /*
356                          * ND6
357                          */
358                         if (dst->sa_family == AF_INET6)
359                                 lle->ln_state = ND6_LLINFO_REACHABLE;
360 #endif
361                         /*
362                          * NB: arp and ndp always set (RTF_STATIC | RTF_HOST)
363                          */
364
365                         if (rtm->rtm_rmx.rmx_expire == 0) {
366                                 lle->la_flags |= LLE_STATIC;
367                                 lle->la_expire = 0;
368                         } else
369                                 lle->la_expire = rtm->rtm_rmx.rmx_expire;
370                         laflags = lle->la_flags;
371                         LLE_WUNLOCK(lle);
372 #ifdef INET
373                         /*  gratuitous ARP */
374                         if ((laflags & LLE_PUB) && dst->sa_family == AF_INET) {
375                                 arprequest(ifp,
376                                     &((struct sockaddr_in *)dst)->sin_addr,
377                                     &((struct sockaddr_in *)dst)->sin_addr,
378                                     ((laflags & LLE_PROXY) ?
379                                         (u_char *)IF_LLADDR(ifp) :
380                                         (u_char *)LLADDR(dl)));
381                         }
382 #endif
383                 } else {
384                         if (flags & LLE_EXCLUSIVE)
385                                 LLE_WUNLOCK(lle);
386                         else
387                                 LLE_RUNLOCK(lle);
388                 }
389         } else if ((lle == NULL) && (flags & LLE_DELETE))
390                 error = EINVAL;
391
392
393         return (error);
394 }
395
396 static void
397 vnet_lltable_init()
398 {
399
400         SLIST_INIT(&V_lltables);
401 }
402 VNET_SYSINIT(vnet_lltable_init, SI_SUB_PSEUDO, SI_ORDER_FIRST,
403     vnet_lltable_init, NULL);
404
405 #ifdef DDB
406 struct llentry_sa {
407         struct llentry          base;
408         struct sockaddr         l3_addr;
409 };
410
411 static void
412 llatbl_lle_show(struct llentry_sa *la)
413 {
414         struct llentry *lle;
415         uint8_t octet[6];
416
417         lle = &la->base;
418         db_printf("lle=%p\n", lle);
419         db_printf(" lle_next=%p\n", lle->lle_next.le_next);
420         db_printf(" lle_lock=%p\n", &lle->lle_lock);
421         db_printf(" lle_tbl=%p\n", lle->lle_tbl);
422         db_printf(" lle_head=%p\n", lle->lle_head);
423         db_printf(" la_hold=%p\n", lle->la_hold);
424         db_printf(" la_numheld=%d\n", lle->la_numheld);
425         db_printf(" la_expire=%ju\n", (uintmax_t)lle->la_expire);
426         db_printf(" la_flags=0x%04x\n", lle->la_flags);
427         db_printf(" la_asked=%u\n", lle->la_asked);
428         db_printf(" la_preempt=%u\n", lle->la_preempt);
429         db_printf(" ln_byhint=%u\n", lle->ln_byhint);
430         db_printf(" ln_state=%d\n", lle->ln_state);
431         db_printf(" ln_router=%u\n", lle->ln_router);
432         db_printf(" ln_ntick=%ju\n", (uintmax_t)lle->ln_ntick);
433         db_printf(" lle_refcnt=%d\n", lle->lle_refcnt);
434         bcopy(&lle->ll_addr.mac16, octet, sizeof(octet));
435         db_printf(" ll_addr=%02x:%02x:%02x:%02x:%02x:%02x\n",
436             octet[0], octet[1], octet[2], octet[3], octet[4], octet[5]);
437         db_printf(" la_timer=%p\n", &lle->la_timer);
438
439         switch (la->l3_addr.sa_family) {
440 #ifdef INET
441         case AF_INET:
442         {
443                 struct sockaddr_in *sin;
444                 char l3s[INET_ADDRSTRLEN];
445
446                 sin = (struct sockaddr_in *)&la->l3_addr;
447                 inet_ntoa_r(sin->sin_addr, l3s);
448                 db_printf(" l3_addr=%s\n", l3s);
449                 break;
450         }
451 #endif
452 #ifdef INET6
453         case AF_INET6:
454         {
455                 struct sockaddr_in6 *sin6;
456                 char l3s[INET6_ADDRSTRLEN];
457
458                 sin6 = (struct sockaddr_in6 *)&la->l3_addr;
459                 ip6_sprintf(l3s, &sin6->sin6_addr);
460                 db_printf(" l3_addr=%s\n", l3s);
461                 break;
462         }
463 #endif
464         default:
465                 db_printf(" l3_addr=N/A (af=%d)\n", la->l3_addr.sa_family);
466                 break;
467         }
468 }
469
470 DB_SHOW_COMMAND(llentry, db_show_llentry)
471 {
472
473         if (!have_addr) {
474                 db_printf("usage: show llentry <struct llentry *>\n");
475                 return;
476         }
477
478         llatbl_lle_show((struct llentry_sa *)addr);
479 }
480
481 static void
482 llatbl_llt_show(struct lltable *llt)
483 {
484         int i;
485         struct llentry *lle;
486
487         db_printf("llt=%p llt_af=%d llt_ifp=%p\n",
488             llt, llt->llt_af, llt->llt_ifp);
489
490         for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
491                 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
492
493                         llatbl_lle_show((struct llentry_sa *)lle);
494                         if (db_pager_quit)
495                                 return;
496                 }
497         }
498 }
499
500 DB_SHOW_COMMAND(lltable, db_show_lltable)
501 {
502
503         if (!have_addr) {
504                 db_printf("usage: show lltable <struct lltable *>\n");
505                 return;
506         }
507
508         llatbl_llt_show((struct lltable *)addr);
509 }
510
511 DB_SHOW_ALL_COMMAND(lltables, db_show_all_lltables)
512 {
513         VNET_ITERATOR_DECL(vnet_iter);
514         struct lltable *llt;
515
516         VNET_FOREACH(vnet_iter) {
517                 CURVNET_SET_QUIET(vnet_iter);
518 #ifdef VIMAGE
519                 db_printf("vnet=%p\n", curvnet);
520 #endif
521                 SLIST_FOREACH(llt, &V_lltables, llt_link) {
522                         db_printf("llt=%p llt_af=%d llt_ifp=%p(%s)\n",
523                             llt, llt->llt_af, llt->llt_ifp,
524                             (llt->llt_ifp != NULL) ?
525                                 llt->llt_ifp->if_xname : "?");
526                         if (have_addr && addr != 0) /* verbose */
527                                 llatbl_llt_show(llt);
528                         if (db_pager_quit) {
529                                 CURVNET_RESTORE();
530                                 return;
531                         }
532                 }
533                 CURVNET_RESTORE();
534         }
535 }
536 #endif