1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (c) 1997 Peter Wemm. 5 * 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 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed for the FreeBSD Project 18 * by Peter Wemm. 19 * 4. The name of the author may not be used to endorse or promote products 20 * derived from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * so there! 35 * 36 * $FreeBSD$ 37 */ 38 39 #pragma once 40 41 #include <libifconfig.h> 42 #include <stdbool.h> 43 44 #define __constructor __attribute__((constructor)) 45 46 struct afswtch; 47 struct cmd; 48 49 typedef void c_func(const char *cmd, int arg, int s, const struct afswtch *afp); 50 typedef void c_func2(const char *arg1, const char *arg2, int s, 51 const struct afswtch *afp); 52 typedef void c_func3(const char *cmd, const char *arg, int s, 53 const struct afswtch *afp); 54 55 struct cmd { 56 const char *c_name; 57 int c_parameter; 58 #define NEXTARG 0xffffff /* has following arg */ 59 #define NEXTARG2 0xfffffe /* has 2 following args */ 60 #define OPTARG 0xfffffd /* has optional following arg */ 61 #define SPARAM 0xfffffc /* parameter is string c_sparameter */ 62 const char *c_sparameter; 63 union { 64 c_func *c_func; 65 c_func2 *c_func2; 66 c_func3 *c_func3; 67 } c_u; 68 int c_iscloneop; 69 struct cmd *c_next; 70 }; 71 void cmd_register(struct cmd *); 72 73 typedef void callback_func(int s, void *); 74 void callback_register(callback_func *, void *); 75 76 /* 77 * Macros for declaring command functions and initializing entries. 78 */ 79 #define DECL_CMD_FUNC(name, cmd, arg) \ 80 void name(const char *cmd, int arg, int s, const struct afswtch *afp) 81 #define DECL_CMD_FUNC2(name, arg1, arg2) \ 82 void name(const char *arg1, const char *arg2, int s, \ 83 const struct afswtch *afp) 84 85 #define DEF_CMD(name, param, func) { \ 86 .c_name = (name), \ 87 .c_parameter = (param), \ 88 .c_u = { .c_func = (func) }, \ 89 .c_iscloneop = 0, \ 90 .c_next = NULL, \ 91 } 92 #define DEF_CMD_ARG(name, func) { \ 93 .c_name = (name), \ 94 .c_parameter = NEXTARG, \ 95 .c_u = { .c_func = (func) }, \ 96 .c_iscloneop = 0, \ 97 .c_next = NULL, \ 98 } 99 #define DEF_CMD_OPTARG(name, func) { \ 100 .c_name = (name), \ 101 .c_parameter = OPTARG, \ 102 .c_u = { .c_func = (func) }, \ 103 .c_iscloneop = 0, \ 104 .c_next = NULL, \ 105 } 106 #define DEF_CMD_ARG2(name, func) { \ 107 .c_name = (name), \ 108 .c_parameter = NEXTARG2, \ 109 .c_u = { .c_func2 = (func) }, \ 110 .c_iscloneop = 0, \ 111 .c_next = NULL, \ 112 } 113 #define DEF_CMD_SARG(name, sparam, func) { \ 114 .c_name = (name), \ 115 .c_parameter = SPARAM, \ 116 .c_sparameter = (sparam), \ 117 .c_u = { .c_func3 = (func) }, \ 118 .c_iscloneop = 0, \ 119 .c_next = NULL, \ 120 } 121 #define DEF_CLONE_CMD(name, param, func) { \ 122 .c_name = (name), \ 123 .c_parameter = (param), \ 124 .c_u = { .c_func = (func) }, \ 125 .c_iscloneop = 1, \ 126 .c_next = NULL, \ 127 } 128 #define DEF_CLONE_CMD_ARG(name, func) { \ 129 .c_name = (name), \ 130 .c_parameter = NEXTARG, \ 131 .c_u = { .c_func = (func) }, \ 132 .c_iscloneop = 1, \ 133 .c_next = NULL, \ 134 } 135 #define DEF_CLONE_CMD_ARG2(name, func) { \ 136 .c_name = (name), \ 137 .c_parameter = NEXTARG2, \ 138 .c_u = { .c_func2 = (func) }, \ 139 .c_iscloneop = 1, \ 140 .c_next = NULL, \ 141 } 142 143 struct ifaddrs; 144 struct addrinfo; 145 146 enum { 147 RIDADDR, 148 ADDR, 149 MASK, 150 DSTADDR, 151 }; 152 153 struct afswtch { 154 const char *af_name; /* as given on cmd line, e.g. "inet" */ 155 short af_af; /* AF_* */ 156 /* 157 * Status is handled one of two ways; if there is an 158 * address associated with the interface then the 159 * associated address family af_status method is invoked 160 * with the appropriate addressin info. Otherwise, if 161 * all possible info is to be displayed and af_other_status 162 * is defined then it is invoked after all address status 163 * is presented. 164 */ 165 void (*af_status)(int, const struct ifaddrs *); 166 void (*af_other_status)(int); 167 /* parse address method */ 168 void (*af_getaddr)(const char *, int); 169 /* parse prefix method (IPv6) */ 170 void (*af_getprefix)(const char *, int); 171 void (*af_postproc)(int s, const struct afswtch *, 172 int newaddr, int ifflags); 173 u_long af_difaddr; /* set dst if address ioctl */ 174 u_long af_aifaddr; /* set if address ioctl */ 175 void *af_ridreq; /* */ 176 void *af_addreq; /* */ 177 struct afswtch *af_next; 178 179 /* XXX doesn't fit model */ 180 void (*af_status_tunnel)(int); 181 void (*af_settunnel)(int s, struct addrinfo *srcres, 182 struct addrinfo *dstres); 183 }; 184 void af_register(struct afswtch *); 185 186 struct ifconfig_args { 187 bool all; /* Match everything */ 188 bool downonly; /* Down-only items */ 189 bool uponly; /* Up-only items */ 190 bool namesonly; /* Output only names */ 191 bool noload; /* Do not load relevant kernel modules */ 192 bool supmedia; /* Supported media */ 193 bool printkeys; /* Print security keys */ 194 int verbose; /* verbosity level */ 195 int argc; 196 char **argv; 197 const char *ifname; /* Requested interface name */ 198 const char *matchgroup; /* Group name to match */ 199 const char *nogroup; /* Group name to exclude */ 200 const struct afswtch *afp; /* AF we're operating on */ 201 }; 202 203 struct option { 204 const char *opt; 205 const char *opt_usage; 206 void (*cb)(const char *arg); 207 struct option *next; 208 }; 209 void opt_register(struct option *); 210 211 extern ifconfig_handle_t *lifh; 212 extern struct ifreq ifr; 213 extern char name[IFNAMSIZ]; /* name of interface */ 214 extern int allmedia; 215 extern int printkeys; 216 extern int newaddr; 217 extern int verbose; 218 extern int printifname; 219 extern int exit_code; 220 extern struct ifconfig_args args; 221 222 void setifcap(const char *, int value, int s, const struct afswtch *); 223 void setifcapnv(const char *vname, const char *arg, int s, 224 const struct afswtch *afp); 225 226 void Perror(const char *cmd); 227 void printb(const char *s, unsigned value, const char *bits); 228 229 void ifmaybeload(struct ifconfig_args *args, const char *name); 230 231 typedef int clone_match_func(const char *); 232 typedef void clone_callback_func(int, struct ifreq *); 233 void clone_setdefcallback_prefix(const char *, clone_callback_func *); 234 void clone_setdefcallback_filter(clone_match_func *, clone_callback_func *); 235 236 void sfp_status(int s, struct ifreq *ifr, int verbose); 237 238 /* 239 * XXX expose this so modules that neeed to know of any pending 240 * operations on ifmedia can avoid cmd line ordering confusion. 241 */ 242 struct ifmediareq *ifmedia_getstate(void); 243 244 void print_vhid(const struct ifaddrs *, const char *); 245 246 void ioctl_ifcreate(int s, struct ifreq *); 247