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 snl_state; 154 struct snl_parsed_addr; 155 struct snl_parsed_link; 156 typedef struct snl_parsed_link if_link_t; 157 typedef struct snl_parsed_addr if_addr_t; 158 struct ifconfig_args; 159 struct io_handler { 160 int s; /* socket to use for ioctls */ 161 struct snl_state *ss; /* NETLINK_ROUTE snl(3) socket */ 162 }; 163 164 typedef void af_setvhid_f(int vhid); 165 typedef void af_status_nl_f(struct ifconfig_args *args, struct io_handler *h, 166 if_link_t *link, if_addr_t *ifa); 167 168 struct afswtch { 169 const char *af_name; /* as given on cmd line, e.g. "inet" */ 170 short af_af; /* AF_* */ 171 /* 172 * Status is handled one of two ways; if there is an 173 * address associated with the interface then the 174 * associated address family af_status method is invoked 175 * with the appropriate addressin info. Otherwise, if 176 * all possible info is to be displayed and af_other_status 177 * is defined then it is invoked after all address status 178 * is presented. 179 */ 180 #ifndef WITHOUT_NETLINK 181 af_status_nl_f *af_status_nl; 182 #else 183 void (*af_status)(int, const struct ifaddrs *); 184 #endif 185 void (*af_other_status)(int); 186 /* parse address method */ 187 void (*af_getaddr)(const char *, int); 188 /* parse prefix method (IPv6) */ 189 void (*af_getprefix)(const char *, int); 190 void (*af_postproc)(int s, const struct afswtch *, 191 int newaddr, int ifflags); 192 af_setvhid_f *af_setvhid; /* Set CARP vhid for an address */ 193 u_long af_difaddr; /* set dst if address ioctl */ 194 u_long af_aifaddr; /* set if address ioctl */ 195 void *af_ridreq; /* */ 196 void *af_addreq; /* */ 197 struct afswtch *af_next; 198 199 /* XXX doesn't fit model */ 200 void (*af_status_tunnel)(int); 201 void (*af_settunnel)(int s, struct addrinfo *srcres, 202 struct addrinfo *dstres); 203 }; 204 void af_register(struct afswtch *); 205 206 struct ifconfig_args { 207 bool all; /* Match everything */ 208 bool downonly; /* Down-only items */ 209 bool uponly; /* Up-only items */ 210 bool namesonly; /* Output only names */ 211 bool noload; /* Do not load relevant kernel modules */ 212 bool supmedia; /* Supported media */ 213 bool printkeys; /* Print security keys */ 214 bool allfamilies; /* Print all families */ 215 int verbose; /* verbosity level */ 216 int argc; 217 char **argv; 218 const char *ifname; /* Requested interface name */ 219 const char *matchgroup; /* Group name to match */ 220 const char *nogroup; /* Group name to exclude */ 221 const struct afswtch *afp; /* AF we're operating on */ 222 }; 223 224 struct option { 225 const char *opt; 226 const char *opt_usage; 227 void (*cb)(const char *arg); 228 struct option *next; 229 }; 230 void opt_register(struct option *); 231 232 extern ifconfig_handle_t *lifh; 233 extern struct ifreq ifr; 234 extern char name[IFNAMSIZ]; /* name of interface */ 235 extern int allmedia; 236 extern int printkeys; 237 extern int newaddr; 238 extern int verbose; 239 extern int printifname; 240 extern int exit_code; 241 extern struct ifconfig_args args; 242 243 void setifcap(const char *, int value, int s, const struct afswtch *); 244 void setifcapnv(const char *vname, const char *arg, int s, 245 const struct afswtch *afp); 246 247 void Perror(const char *cmd); 248 void printb(const char *s, unsigned value, const char *bits); 249 250 void ifmaybeload(struct ifconfig_args *args, const char *name); 251 252 typedef int clone_match_func(const char *); 253 typedef void clone_callback_func(int, struct ifreq *); 254 void clone_setdefcallback_prefix(const char *, clone_callback_func *); 255 void clone_setdefcallback_filter(clone_match_func *, clone_callback_func *); 256 257 void sfp_status(int s, struct ifreq *ifr, int verbose); 258 259 struct sockaddr_dl; 260 bool match_ether(const struct sockaddr_dl *sdl); 261 bool match_if_flags(struct ifconfig_args *args, int if_flags); 262 int ifconfig(int argc, char *const *argv, int iscreate, const struct afswtch *uafp); 263 bool group_member(const char *ifname, const char *match, const char *nomatch); 264 void print_ifcap(struct ifconfig_args *args, int s); 265 void tunnel_status(int s); 266 struct afswtch *af_getbyfamily(int af); 267 void af_other_status(int s); 268 void print_ifstatus(int s); 269 void print_metric(int s); 270 271 /* Netlink-related functions */ 272 void list_interfaces_nl(struct ifconfig_args *args); 273 274 /* 275 * XXX expose this so modules that neeed to know of any pending 276 * operations on ifmedia can avoid cmd line ordering confusion. 277 */ 278 struct ifmediareq *ifmedia_getstate(void); 279 280 void print_vhid(const struct ifaddrs *, const char *); 281 282 void ioctl_ifcreate(int s, struct ifreq *); 283