<net/if.h>: Do not include <net/if_var.h> for _KERNEL
[dragonfly.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  * 3. 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.3 (Berkeley) 1/4/94
30  * $FreeBSD: src/sys/net/if.c,v 1.185 2004/03/13 02:35:03 brooks Exp $
31  * $DragonFly: src/sys/net/if_clone.c,v 1.1 2008/01/11 11:59:40 sephe Exp $
32  */
33
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37
38 #include <net/if.h>
39 #include <net/if_var.h>
40 #include <net/if_clone.h>
41
42 static LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
43 static int              if_cloners_count;
44
45 MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
46
47 static struct if_clone  *if_clone_lookup(const char *, int *);
48
49 /*
50  * Create a clone network interface.
51  */
52 int
53 if_clone_create(char *name, int len, caddr_t params)
54 {
55         struct if_clone *ifc;
56         char *dp;
57         int wildcard, bytoff, bitoff;
58         int unit;
59         int err;
60
61         ifc = if_clone_lookup(name, &unit);
62         if (ifc == NULL)
63                 return (EINVAL);
64
65         ifnet_lock();
66         if (ifunit(name) != NULL) {
67                 ifnet_unlock();
68                 return (EEXIST);
69         }
70         ifnet_unlock();
71
72         bytoff = bitoff = 0;
73         wildcard = (unit < 0);
74         /*
75          * Find a free unit if none was given.
76          */
77         if (wildcard) {
78                 while (bytoff < ifc->ifc_bmlen &&
79                     ifc->ifc_units[bytoff] == 0xff)
80                         bytoff++;
81                 if (bytoff >= ifc->ifc_bmlen)
82                         return (ENOSPC);
83                 while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
84                         bitoff++;
85                 unit = (bytoff << 3) + bitoff;
86         }
87
88         if (unit > ifc->ifc_maxunit)
89                 return (ENXIO);
90
91         err = (*ifc->ifc_create)(ifc, unit, params);
92         if (err != 0)
93                 return (err);
94
95         if (!wildcard) {
96                 bytoff = unit >> 3;
97                 bitoff = unit - (bytoff << 3);
98         }
99
100         /*
101          * Allocate the unit in the bitmap.
102          */
103         KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
104             ("%s: bit is already set", __func__));
105         ifc->ifc_units[bytoff] |= (1 << bitoff);
106
107         /* In the wildcard case, we need to update the name. */
108         if (wildcard) {
109                 for (dp = name; *dp != '\0'; dp++);
110                 if (ksnprintf(dp, len - (dp-name), "%d", unit) >
111                     len - (dp-name) - 1) {
112                         /*
113                          * This can only be a programmer error and
114                          * there's no straightforward way to recover if
115                          * it happens.
116                          */
117                         panic("if_clone_create(): interface name too long");
118                 }
119
120         }
121
122         EVENTHANDLER_INVOKE(if_clone_event, ifc);
123
124         return (0);
125 }
126
127 /*
128  * Destroy a clone network interface.
129  */
130 int
131 if_clone_destroy(const char *name)
132 {
133         struct if_clone *ifc;
134         struct ifnet *ifp;
135         int bytoff, bitoff;
136         int unit, error;
137
138         ifc = if_clone_lookup(name, &unit);
139         if (ifc == NULL)
140                 return (EINVAL);
141
142         if (unit < ifc->ifc_minifs)
143                 return (EINVAL);
144
145         ifnet_lock();
146
147         ifp = ifunit(name);
148         if (ifp == NULL) {
149                 ifnet_unlock();
150                 return (ENXIO);
151         }
152
153         if (ifc->ifc_destroy == NULL) {
154                 ifnet_unlock();
155                 return (EOPNOTSUPP);
156         }
157
158         error = ifc->ifc_destroy(ifp);
159         if (error) {
160                 ifnet_unlock();
161                 return error;
162         }
163
164         ifnet_unlock();
165
166         /*
167          * Compute offset in the bitmap and deallocate the unit.
168          */
169         bytoff = unit >> 3;
170         bitoff = unit - (bytoff << 3);
171         KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
172             ("%s: bit is already cleared", __func__));
173         ifc->ifc_units[bytoff] &= ~(1 << bitoff);
174         return (0);
175 }
176
177 /*
178  * Register a network interface cloner.
179  */
180 void
181 if_clone_attach(struct if_clone *ifc)
182 {
183         int bytoff, bitoff;
184         int err;
185         int len, maxclone;
186         int unit;
187         struct if_clone *ifct;
188
189         /* Duplicate entries in if_cloners lead
190            to infinite loops in if_clone_create */
191         LIST_FOREACH(ifct, &if_cloners, ifc_list) {
192                 if (ifct == ifc) {
193                         panic("%s: duplicate entry %s\n",
194                               __func__, ifc->ifc_name);
195                 }
196         }
197
198         KASSERT(ifc->ifc_minifs - 1 <= ifc->ifc_maxunit,
199             ("%s: %s requested more units then allowed (%d > %d)",
200             __func__, ifc->ifc_name, ifc->ifc_minifs,
201             ifc->ifc_maxunit + 1));
202         /*
203          * Compute bitmap size and allocate it.
204          */
205         maxclone = ifc->ifc_maxunit + 1;
206         len = maxclone >> 3;
207         if ((len << 3) < maxclone)
208                 len++;
209         ifc->ifc_units = kmalloc(len, M_CLONE, M_WAITOK | M_ZERO);
210         ifc->ifc_bmlen = len;
211
212         LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
213         if_cloners_count++;
214
215         for (unit = 0; unit < ifc->ifc_minifs; unit++) {
216                 err = (*ifc->ifc_create)(ifc, unit, NULL);
217                 KASSERT(err == 0,
218                     ("%s: failed to create required interface %s%d",
219                     __func__, ifc->ifc_name, unit));
220
221                 /* Allocate the unit in the bitmap. */
222                 bytoff = unit >> 3;
223                 bitoff = unit - (bytoff << 3);
224                 ifc->ifc_units[bytoff] |= (1 << bitoff);
225         }
226 }
227
228 /*
229  * Unregister a network interface cloner.
230  */
231 void
232 if_clone_detach(struct if_clone *ifc)
233 {
234
235         LIST_REMOVE(ifc, ifc_list);
236         kfree(ifc->ifc_units, M_CLONE);
237         if_cloners_count--;
238 }
239
240 /*
241  * Provide list of interface cloners to userspace.
242  */
243 int
244 if_clone_list(struct if_clonereq *ifcr)
245 {
246         char outbuf[IFNAMSIZ], *dst;
247         struct if_clone *ifc;
248         int count, error = 0;
249
250         ifcr->ifcr_total = if_cloners_count;
251         if ((dst = ifcr->ifcr_buffer) == NULL) {
252                 /* Just asking how many there are. */
253                 return (0);
254         }
255
256         if (ifcr->ifcr_count < 0)
257                 return (EINVAL);
258
259         count = (if_cloners_count < ifcr->ifcr_count) ?
260             if_cloners_count : ifcr->ifcr_count;
261
262         for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
263              ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
264                 strlcpy(outbuf, ifc->ifc_name, IFNAMSIZ);
265                 error = copyout(outbuf, dst, IFNAMSIZ);
266                 if (error)
267                         break;
268         }
269
270         return (error);
271 }
272
273 /*
274  * Look up a network interface cloner.
275  */
276 static struct if_clone *
277 if_clone_lookup(const char *name, int *unitp)
278 {
279         struct if_clone *ifc;
280         const char *cp;
281         int i;
282
283         for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
284                 for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
285                         if (ifc->ifc_name[i] != *cp)
286                                 goto next_ifc;
287                 }
288                 goto found_name;
289  next_ifc:
290                 ifc = LIST_NEXT(ifc, ifc_list);
291         }
292
293         /* No match. */
294         return (NULL);
295
296  found_name:
297         if (*cp == '\0') {
298                 i = -1;
299         } else {
300                 for (i = 0; *cp != '\0'; cp++) {
301                         if (*cp < '0' || *cp > '9') {
302                                 /* Bogus unit number. */
303                                 return (NULL);
304                         }
305                         i = (i * 10) + (*cp - '0');
306                 }
307         }
308
309         if (unitp != NULL)
310                 *unitp = i;
311         return (ifc);
312 }