[d48955b] | 1 | #include <machine/rtems-bsd-user-space.h> |
---|
| 2 | |
---|
[ebbe3cc] | 3 | /* |
---|
| 4 | * Copyright (c) 1983, 1993 |
---|
| 5 | * The Regents of the University of California. All rights reserved. |
---|
| 6 | * |
---|
| 7 | * Redistribution and use in source and binary forms, with or without |
---|
| 8 | * modification, are permitted provided that the following conditions |
---|
| 9 | * are met: |
---|
| 10 | * 1. Redistributions of source code must retain the above copyright |
---|
| 11 | * notice, this list of conditions and the following disclaimer. |
---|
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
| 13 | * notice, this list of conditions and the following disclaimer in the |
---|
| 14 | * documentation and/or other materials provided with the distribution. |
---|
| 15 | * 4. Neither the name of the University nor the names of its contributors |
---|
| 16 | * may be used to endorse or promote products derived from this software |
---|
| 17 | * without specific prior written permission. |
---|
| 18 | * |
---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
| 20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
| 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
| 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
| 25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
| 27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
| 28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
| 29 | * SUCH DAMAGE. |
---|
| 30 | */ |
---|
| 31 | |
---|
| 32 | #ifndef lint |
---|
| 33 | static const char rcsid[] = |
---|
| 34 | "$FreeBSD$"; |
---|
| 35 | #endif /* not lint */ |
---|
| 36 | |
---|
[56e9de9] | 37 | #ifdef __rtems__ |
---|
[fef6dd1] | 38 | #define RTEMS_BSD_PROGRAM_NO_OPEN_WRAP |
---|
| 39 | #define RTEMS_BSD_PROGRAM_NO_SOCKET_WRAP |
---|
| 40 | #define RTEMS_BSD_PROGRAM_NO_CLOSE_WRAP |
---|
| 41 | #define RTEMS_BSD_PROGRAM_NO_FOPEN_WRAP |
---|
| 42 | #define RTEMS_BSD_PROGRAM_NO_FCLOSE_WRAP |
---|
| 43 | #define RTEMS_BSD_PROGRAM_NO_MALLOC_WRAP |
---|
| 44 | #define RTEMS_BSD_PROGRAM_NO_CALLOC_WRAP |
---|
| 45 | #define RTEMS_BSD_PROGRAM_NO_REALLOC_WRAP |
---|
| 46 | #define RTEMS_BSD_PROGRAM_NO_FREE_WRAP |
---|
[56e9de9] | 47 | #include <machine/rtems-bsd-program.h> |
---|
| 48 | #endif /* __rtems__ */ |
---|
[ebbe3cc] | 49 | #include <sys/queue.h> |
---|
[e599318] | 50 | #include <rtems/bsd/sys/types.h> |
---|
[ebbe3cc] | 51 | #include <sys/ioctl.h> |
---|
| 52 | #include <sys/socket.h> |
---|
| 53 | #include <net/if.h> |
---|
| 54 | |
---|
| 55 | #include <err.h> |
---|
| 56 | #include <stdio.h> |
---|
| 57 | #include <stdlib.h> |
---|
| 58 | #include <string.h> |
---|
| 59 | #include <unistd.h> |
---|
| 60 | |
---|
| 61 | #include "ifconfig.h" |
---|
| 62 | |
---|
| 63 | static void |
---|
| 64 | list_cloners(void) |
---|
| 65 | { |
---|
| 66 | struct if_clonereq ifcr; |
---|
| 67 | char *cp, *buf; |
---|
| 68 | int idx; |
---|
| 69 | int s; |
---|
| 70 | |
---|
| 71 | s = socket(AF_LOCAL, SOCK_DGRAM, 0); |
---|
| 72 | if (s == -1) |
---|
| 73 | err(1, "socket(AF_LOCAL,SOCK_DGRAM)"); |
---|
| 74 | |
---|
| 75 | memset(&ifcr, 0, sizeof(ifcr)); |
---|
| 76 | |
---|
| 77 | if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0) |
---|
| 78 | err(1, "SIOCIFGCLONERS for count"); |
---|
| 79 | |
---|
| 80 | buf = malloc(ifcr.ifcr_total * IFNAMSIZ); |
---|
| 81 | if (buf == NULL) |
---|
| 82 | err(1, "unable to allocate cloner name buffer"); |
---|
| 83 | |
---|
| 84 | ifcr.ifcr_count = ifcr.ifcr_total; |
---|
| 85 | ifcr.ifcr_buffer = buf; |
---|
| 86 | |
---|
[b833cc4] | 87 | if (ioctl(s, SIOCIFGCLONERS, &ifcr) < 0) { |
---|
| 88 | free(buf); |
---|
[ebbe3cc] | 89 | err(1, "SIOCIFGCLONERS for names"); |
---|
[b833cc4] | 90 | } |
---|
[ebbe3cc] | 91 | |
---|
| 92 | /* |
---|
| 93 | * In case some disappeared in the mean time, clamp it down. |
---|
| 94 | */ |
---|
| 95 | if (ifcr.ifcr_count > ifcr.ifcr_total) |
---|
| 96 | ifcr.ifcr_count = ifcr.ifcr_total; |
---|
| 97 | |
---|
| 98 | for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) { |
---|
| 99 | if (idx > 0) |
---|
| 100 | putchar(' '); |
---|
| 101 | printf("%s", cp); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | putchar('\n'); |
---|
| 105 | free(buf); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | struct clone_defcb { |
---|
| 109 | char ifprefix[IFNAMSIZ]; |
---|
| 110 | clone_callback_func *clone_cb; |
---|
| 111 | SLIST_ENTRY(clone_defcb) next; |
---|
| 112 | }; |
---|
| 113 | |
---|
| 114 | static SLIST_HEAD(, clone_defcb) clone_defcbh = |
---|
| 115 | SLIST_HEAD_INITIALIZER(clone_defcbh); |
---|
| 116 | |
---|
| 117 | void |
---|
| 118 | clone_setdefcallback(const char *ifprefix, clone_callback_func *p) |
---|
| 119 | { |
---|
| 120 | struct clone_defcb *dcp; |
---|
| 121 | |
---|
| 122 | dcp = malloc(sizeof(*dcp)); |
---|
[b833cc4] | 123 | #ifndef __rtems__ |
---|
| 124 | if (dcp == NULL) { |
---|
| 125 | errx(1, "unable to allocate clone"); |
---|
| 126 | } |
---|
| 127 | #endif /* __rtems__ */ |
---|
[ebbe3cc] | 128 | strlcpy(dcp->ifprefix, ifprefix, IFNAMSIZ-1); |
---|
| 129 | dcp->clone_cb = p; |
---|
| 130 | SLIST_INSERT_HEAD(&clone_defcbh, dcp, next); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | /* |
---|
| 134 | * Do the actual clone operation. Any parameters must have been |
---|
| 135 | * setup by now. If a callback has been setup to do the work |
---|
| 136 | * then defer to it; otherwise do a simple create operation with |
---|
| 137 | * no parameters. |
---|
| 138 | */ |
---|
| 139 | static void |
---|
| 140 | ifclonecreate(int s, void *arg) |
---|
| 141 | { |
---|
| 142 | struct ifreq ifr; |
---|
| 143 | struct clone_defcb *dcp; |
---|
| 144 | clone_callback_func *clone_cb = NULL; |
---|
| 145 | |
---|
| 146 | memset(&ifr, 0, sizeof(ifr)); |
---|
| 147 | (void) strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); |
---|
| 148 | |
---|
| 149 | if (clone_cb == NULL) { |
---|
| 150 | /* Try to find a default callback */ |
---|
| 151 | SLIST_FOREACH(dcp, &clone_defcbh, next) { |
---|
| 152 | if (strncmp(dcp->ifprefix, ifr.ifr_name, |
---|
| 153 | strlen(dcp->ifprefix)) == 0) { |
---|
| 154 | clone_cb = dcp->clone_cb; |
---|
| 155 | break; |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | if (clone_cb == NULL) { |
---|
| 160 | /* NB: no parameters */ |
---|
| 161 | if (ioctl(s, SIOCIFCREATE2, &ifr) < 0) |
---|
| 162 | err(1, "SIOCIFCREATE2"); |
---|
| 163 | } else { |
---|
| 164 | clone_cb(s, &ifr); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | /* |
---|
| 168 | * If we get a different name back than we put in, print it. |
---|
| 169 | */ |
---|
| 170 | if (strncmp(name, ifr.ifr_name, sizeof(name)) != 0) { |
---|
| 171 | strlcpy(name, ifr.ifr_name, sizeof(name)); |
---|
| 172 | printf("%s\n", name); |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | static |
---|
| 177 | DECL_CMD_FUNC(clone_create, arg, d) |
---|
| 178 | { |
---|
| 179 | callback_register(ifclonecreate, NULL); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | static |
---|
| 183 | DECL_CMD_FUNC(clone_destroy, arg, d) |
---|
| 184 | { |
---|
| 185 | (void) strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name)); |
---|
| 186 | if (ioctl(s, SIOCIFDESTROY, &ifr) < 0) |
---|
| 187 | err(1, "SIOCIFDESTROY"); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | static struct cmd clone_cmds[] = { |
---|
| 191 | DEF_CLONE_CMD("create", 0, clone_create), |
---|
| 192 | DEF_CMD("destroy", 0, clone_destroy), |
---|
| 193 | DEF_CLONE_CMD("plumb", 0, clone_create), |
---|
| 194 | DEF_CMD("unplumb", 0, clone_destroy), |
---|
| 195 | }; |
---|
| 196 | |
---|
| 197 | static void |
---|
| 198 | clone_Copt_cb(const char *optarg __unused) |
---|
| 199 | { |
---|
| 200 | list_cloners(); |
---|
| 201 | exit(0); |
---|
| 202 | } |
---|
| 203 | static struct option clone_Copt = { .opt = "C", .opt_usage = "[-C]", .cb = clone_Copt_cb }; |
---|
| 204 | |
---|
[60618d5] | 205 | #ifndef __rtems__ |
---|
[ebbe3cc] | 206 | static __constructor void |
---|
[60618d5] | 207 | #else /* __rtems__ */ |
---|
| 208 | void |
---|
| 209 | #endif /* __rtems__ */ |
---|
[ebbe3cc] | 210 | clone_ctor(void) |
---|
| 211 | { |
---|
[60618d5] | 212 | #ifdef __rtems__ |
---|
| 213 | SLIST_INIT(&clone_defcbh); |
---|
| 214 | #endif /* __rtems__ */ |
---|
[ebbe3cc] | 215 | #define N(a) (sizeof(a) / sizeof(a[0])) |
---|
| 216 | size_t i; |
---|
| 217 | |
---|
| 218 | for (i = 0; i < N(clone_cmds); i++) |
---|
| 219 | cmd_register(&clone_cmds[i]); |
---|
| 220 | opt_register(&clone_Copt); |
---|
| 221 | #undef N |
---|
| 222 | } |
---|
[b833cc4] | 223 | #ifdef __rtems__ |
---|
| 224 | void |
---|
| 225 | clone_dtor(void) |
---|
| 226 | { |
---|
| 227 | struct clone_defcb *dcp; |
---|
| 228 | struct clone_defcb *dcp_tmp; |
---|
| 229 | |
---|
| 230 | SLIST_FOREACH_SAFE(dcp, &clone_defcbh, next, dcp_tmp) { |
---|
| 231 | free(dcp); |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | #endif /* __rtems__ */ |
---|