]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net/if_clone.c
This commit was generated by cvs2svn to compensate for changes in r138296,
[FreeBSD/FreeBSD.git] / sys / net / if_clone.c
1 /*
2  * Copyright (c) 1980, 1986, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 THE REGENTS 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  *      @(#)if.c        8.5 (Berkeley) 1/9/95
30  * $FreeBSD$
31  */
32
33 #include <sys/param.h>
34 #include <sys/malloc.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/kernel.h>
38 #include <sys/systm.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41
42 #include <net/if.h>
43 #include <net/if_clone.h>
44 #if 0
45 #include <net/if_dl.h>
46 #endif
47 #include <net/if_types.h>
48 #include <net/if_var.h>
49 #include <net/radix.h>
50 #include <net/route.h>
51
52 static void             if_clone_free(struct if_clone *ifc);
53
54 static struct mtx       if_cloners_mtx;
55 static int              if_cloners_count;
56 LIST_HEAD(, if_clone)   if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
57
58 #define IF_CLONERS_LOCK_INIT()          \
59     mtx_init(&if_cloners_mtx, "if_cloners lock", NULL, MTX_DEF)
60 #define IF_CLONERS_LOCK_ASSERT()        mtx_assert(&if_cloners_mtx, MA_OWNED)
61 #define IF_CLONERS_LOCK()               mtx_lock(&if_cloners_mtx)
62 #define IF_CLONERS_UNLOCK()             mtx_unlock(&if_cloners_mtx)
63
64 #define IF_CLONE_LOCK_INIT(ifc)         \
65     mtx_init(&(ifc)->ifc_mtx, "if_clone lock", NULL, MTX_DEF)
66 #define IF_CLONE_LOCK_DESTROY(ifc)      mtx_destroy(&(ifc)->ifc_mtx)
67 #define IF_CLONE_LOCK_ASSERT(ifc)       mtx_assert(&(ifc)->ifc_mtx, MA_OWNED)
68 #define IF_CLONE_LOCK(ifc)              mtx_lock(&(ifc)->ifc_mtx)
69 #define IF_CLONE_UNLOCK(ifc)            mtx_unlock(&(ifc)->ifc_mtx)
70
71 #define IF_CLONE_ADDREF(ifc)                                            \
72         do {                                                            \
73                 IF_CLONE_LOCK(ifc);                                     \
74                 IF_CLONE_ADDREF_LOCKED(ifc);                            \
75                 IF_CLONE_UNLOCK(ifc);                                   \
76         } while (0)
77 #define IF_CLONE_ADDREF_LOCKED(ifc)                                     \
78         do {                                                            \
79                 IF_CLONE_LOCK_ASSERT(ifc);                              \
80                 KASSERT((ifc)->ifc_refcnt >= 0,                         \
81                     ("negative refcnt %ld", (ifc)->ifc_refcnt));        \
82                 (ifc)->ifc_refcnt++;                                    \
83         } while (0)
84 #define IF_CLONE_REMREF(ifc)                                            \
85         do {                                                            \
86                 IF_CLONE_LOCK(ifc);                                     \
87                 IF_CLONE_REMREF_LOCKED(ifc);                            \
88         } while (0)
89 #define IF_CLONE_REMREF_LOCKED(ifc)                                     \
90         do {                                                            \
91                 IF_CLONE_LOCK_ASSERT(ifc);                              \
92                 KASSERT((ifc)->ifc_refcnt > 0,                          \
93                     ("bogus refcnt %ld", (ifc)->ifc_refcnt));           \
94                 if (--(ifc)->ifc_refcnt == 0) {                         \
95                         IF_CLONE_UNLOCK(ifc);                           \
96                         if_clone_free(ifc);                             \
97                 } else {                                                \
98                         /* silently free the lock */                    \
99                         IF_CLONE_UNLOCK(ifc);                           \
100                 }                                                       \
101         } while (0)
102
103 MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
104
105 void
106 if_clone_init(void)
107 {
108         IF_CLONERS_LOCK_INIT();
109 }
110
111 /*
112  * Create a clone network interface.
113  */
114 int
115 if_clone_create(char *name, size_t len)
116 {
117         int err;
118         struct if_clone *ifc;
119
120         if (ifunit(name) != NULL)
121                 return (EEXIST);
122
123         /* Try to find an applicable cloner for this request */
124         IF_CLONERS_LOCK();
125         LIST_FOREACH(ifc, &if_cloners, ifc_list) {
126                 if (ifc->ifc_match(ifc, name)) {
127                         IF_CLONE_ADDREF(ifc);
128                         break;
129                 }
130         }
131         IF_CLONERS_UNLOCK();
132
133         if (ifc == NULL)
134                 return (EINVAL);
135
136         err = (*ifc->ifc_create)(ifc, name, len);
137         IF_CLONE_REMREF(ifc);
138         return (err);
139 }
140
141 /*
142  * Destroy a clone network interface.
143  */
144 int
145 if_clone_destroy(const char *name)
146 {
147         int err;
148         struct if_clone *ifc;
149         struct ifnet *ifp;
150
151         ifp = ifunit(name);
152         if (ifp == NULL)
153                 return (ENXIO);
154
155         /* Find the cloner for this interface */
156         IF_CLONERS_LOCK();
157         LIST_FOREACH(ifc, &if_cloners, ifc_list) {
158                 if (strcmp(ifc->ifc_name, ifp->if_dname) == 0) {
159                         IF_CLONE_ADDREF(ifc);
160                         break;
161                 }
162         }
163         IF_CLONERS_UNLOCK();
164         if (ifc == NULL)
165                 return (EINVAL);
166
167         if (ifc->ifc_destroy == NULL) {
168                 err = EOPNOTSUPP;
169                 goto done;
170         }
171
172         err =  (*ifc->ifc_destroy)(ifc, ifp);
173
174 done:
175         IF_CLONE_REMREF(ifc);
176         return (err);
177 }
178
179 /*
180  * Register a network interface cloner.
181  */
182 void
183 if_clone_attach(struct if_clone *ifc)
184 {
185         int len, maxclone;
186
187         /*
188          * Compute bitmap size and allocate it.
189          */
190         maxclone = ifc->ifc_maxunit + 1;
191         len = maxclone >> 3;
192         if ((len << 3) < maxclone)
193                 len++;
194         ifc->ifc_units = malloc(len, M_CLONE, M_WAITOK | M_ZERO);
195         ifc->ifc_bmlen = len;
196         IF_CLONE_LOCK_INIT(ifc);
197         IF_CLONE_ADDREF(ifc);
198
199         IF_CLONERS_LOCK();
200         LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
201         if_cloners_count++;
202         IF_CLONERS_UNLOCK();
203
204         if (ifc->ifc_attach != NULL)
205                 (*ifc->ifc_attach)(ifc);
206         EVENTHANDLER_INVOKE(if_clone_event, ifc);
207 }
208
209 /*
210  * Unregister a network interface cloner.
211  */
212 void
213 if_clone_detach(struct if_clone *ifc)
214 {
215
216         IF_CLONERS_LOCK();
217         LIST_REMOVE(ifc, ifc_list);
218         if_cloners_count--;
219         IF_CLONERS_UNLOCK();
220
221         IF_CLONE_REMREF(ifc);
222 }
223
224 static void
225 if_clone_free(struct if_clone *ifc)
226 {
227
228         IF_CLONE_LOCK_DESTROY(ifc);
229         free(ifc->ifc_units, M_CLONE);
230 }
231
232 /*
233  * Provide list of interface cloners to userspace.
234  */
235 int
236 if_clone_list(struct if_clonereq *ifcr)
237 {
238         char *buf, *dst, *outbuf = NULL;
239         struct if_clone *ifc;
240         int buf_count, count, err = 0;
241
242         IF_CLONERS_LOCK();
243         /*
244          * Set our internal output buffer size.  We could end up not
245          * reporting a cloner that is added between the unlock and lock
246          * below, but that's not a major problem.  Not caping our
247          * allocation to the number of cloners actually in the system
248          * could be because that would let arbitrary users cause us to
249          * allocate abritrary amounts of kernel memory.
250          */
251         buf_count = (if_cloners_count < ifcr->ifcr_count) ?
252             if_cloners_count : ifcr->ifcr_count;
253         IF_CLONERS_UNLOCK();
254
255         outbuf = malloc(IFNAMSIZ*buf_count, M_CLONE, M_WAITOK | M_ZERO);
256
257         IF_CLONERS_LOCK();
258
259         ifcr->ifcr_total = if_cloners_count;
260         if ((dst = ifcr->ifcr_buffer) == NULL) {
261                 /* Just asking how many there are. */
262                 goto done;
263         }
264
265         if (ifcr->ifcr_count < 0) {
266                 err = EINVAL;
267                 goto done;
268         }
269
270         count = (if_cloners_count < buf_count) ?
271             if_cloners_count : buf_count;
272
273         for (ifc = LIST_FIRST(&if_cloners), buf = outbuf;
274             ifc != NULL && count != 0;
275             ifc = LIST_NEXT(ifc, ifc_list), count--, buf += IFNAMSIZ) {
276                 strlcpy(buf, ifc->ifc_name, IFNAMSIZ);
277         }
278
279 done:
280         IF_CLONERS_UNLOCK();
281         if (err == 0)
282                 err = copyout(outbuf, dst, buf_count*IFNAMSIZ);
283         if (outbuf != NULL)
284                 free(outbuf, M_CLONE);
285         return (err);
286 }
287
288 /*
289  * A utility function to extract unit numbers from interface names of
290  * the form name###.
291  *
292  * Returns 0 on success and an error on failure.
293  */
294 int
295 ifc_name2unit(const char *name, int *unit)
296 {
297         const char      *cp;
298
299         for (cp = name; *cp != '\0' && (*cp < '0' || *cp > '9'); cp++);
300         if (*cp == '\0') {
301                 *unit = -1;
302         } else {
303                 for (*unit = 0; *cp != '\0'; cp++) {
304                         if (*cp < '0' || *cp > '9') {
305                                 /* Bogus unit number. */
306                                 return (EINVAL);
307                         }
308                         *unit = (*unit * 10) + (*cp - '0');
309                 }
310         }
311
312         return (0);
313 }
314
315 int
316 ifc_alloc_unit(struct if_clone *ifc, int *unit)
317 {
318         int wildcard, bytoff, bitoff;
319         int err = 0;
320
321         IF_CLONE_LOCK(ifc);
322
323         bytoff = bitoff = 0;
324         wildcard = (*unit < 0);
325         /*
326          * Find a free unit if none was given.
327          */
328         if (wildcard) {
329                 while ((bytoff < ifc->ifc_bmlen)
330                     && (ifc->ifc_units[bytoff] == 0xff))
331                         bytoff++;
332                 if (bytoff >= ifc->ifc_bmlen) {
333                         err = ENOSPC;
334                         goto done;
335                 }
336                 while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
337                         bitoff++;
338                 *unit = (bytoff << 3) + bitoff;
339         }
340
341         if (*unit > ifc->ifc_maxunit) {
342                 err = ENOSPC;
343                 goto done;
344         }
345
346         if (!wildcard) {
347                 bytoff = *unit >> 3;
348                 bitoff = *unit - (bytoff << 3);
349         }
350
351         if((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0) {
352                 err = EEXIST;
353                 goto done;
354         }
355         /*
356          * Allocate the unit in the bitmap.
357          */
358         ifc->ifc_units[bytoff] |= (1 << bitoff);
359
360 done:
361         IF_CLONE_UNLOCK(ifc);
362         return (err);
363 }
364
365 void
366 ifc_free_unit(struct if_clone *ifc, int unit)
367 {
368         int bytoff, bitoff;
369
370
371         /*
372          * Compute offset in the bitmap and deallocate the unit.
373          */
374         bytoff = unit >> 3;
375         bitoff = unit - (bytoff << 3);
376
377         IF_CLONE_LOCK(ifc);
378         KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
379             ("%s: bit is already cleared", __func__));
380         ifc->ifc_units[bytoff] &= ~(1 << bitoff);
381         IF_CLONE_UNLOCK(ifc);
382 }
383
384 void
385 ifc_simple_attach(struct if_clone *ifc)
386 {
387         int err;
388         int unit;
389         char name[IFNAMSIZ];
390         struct ifc_simple_data *ifcs = ifc->ifc_data;
391
392         KASSERT(ifcs->ifcs_minifs - 1 <= ifc->ifc_maxunit,
393             ("%s: %s requested more units then allowed (%d > %d)",
394             __func__, ifc->ifc_name, ifcs->ifcs_minifs,
395             ifc->ifc_maxunit + 1));
396
397         for (unit = 0; unit < ifcs->ifcs_minifs; unit++) {
398                 snprintf(name, IFNAMSIZ, "%s%d", ifc->ifc_name, unit);
399                 err = (*ifc->ifc_create)(ifc, name, IFNAMSIZ);
400                 KASSERT(err == 0,
401                     ("%s: failed to create required interface %s",
402                     __func__, name));
403         }
404 }
405
406 int
407 ifc_simple_match(struct if_clone *ifc, const char *name)
408 {
409         const char *cp;
410         int i;
411         
412         /* Match the name */
413         for (cp = name, i = 0; i < strlen(ifc->ifc_name); i++, cp++) {
414                 if (ifc->ifc_name[i] != *cp)
415                         return (0);
416         }
417
418         /* Make sure there's a unit number or nothing after the name */
419         for (; *cp != '\0'; cp++) {
420                 if (*cp < '0' || *cp > '9')
421                         return (0);
422         }
423
424         return (1);
425 }
426
427 int
428 ifc_simple_create(struct if_clone *ifc, char *name, size_t len)
429 {
430         char *dp;
431         int wildcard;
432         int unit;
433         int err;
434         struct ifc_simple_data *ifcs = ifc->ifc_data;
435
436         err = ifc_name2unit(name, &unit);
437         if (err != 0)
438                 return (err);
439
440         wildcard = (unit < 0);
441
442         err = ifc_alloc_unit(ifc, &unit);
443         if (err != 0)
444                 return (err);
445
446         err = ifcs->ifcs_create(ifc, unit);
447         if (err != 0) {
448                 ifc_free_unit(ifc, unit);
449                 return (err);
450         }
451
452         /* In the wildcard case, we need to update the name. */
453         if (wildcard) {
454                 for (dp = name; *dp != '\0'; dp++);
455                 if (snprintf(dp, len - (dp-name), "%d", unit) >
456                     len - (dp-name) - 1) {
457                         /*
458                          * This can only be a programmer error and
459                          * there's no straightforward way to recover if
460                          * it happens.
461                          */
462                         panic("if_clone_create(): interface name too long");
463                 }
464
465         }
466
467         return (0);
468 }
469
470 int
471 ifc_simple_destroy(struct if_clone *ifc, struct ifnet *ifp)
472 {
473         int unit;
474         struct ifc_simple_data *ifcs = ifc->ifc_data;
475
476         unit = ifp->if_dunit;
477
478         if (unit < ifcs->ifcs_minifs) 
479                 return (EINVAL);
480
481         ifcs->ifcs_destroy(ifp);
482
483         ifc_free_unit(ifc, unit);
484
485         return (0);
486 }