dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / cmd-inet / usr.sbin / ifconfig / ifconfig.c
blob1a2e3eb0a9a662098f719d2379f3914b06734ce0
1 /*
2 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
3 * Copyright 2012, Daniil Lunev. All rights reserved.
4 * Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
5 */
6 /*
7 * Copyright (c) 1983 Regents of the University of California.
8 * All rights reserved. The Berkeley software License Agreement
9 * specifies the terms and conditions for redistribution.
12 #include "defs.h"
13 #include "strings.h"
14 #include "ifconfig.h"
15 #include <compat.h>
16 #include <libdlpi.h>
17 #include <libdllink.h>
18 #include <libdliptun.h>
19 #include <libdllink.h>
20 #include <inet/ip.h>
21 #include <inet/ipsec_impl.h>
22 #include <libipadm.h>
23 #include <ifaddrs.h>
24 #include <libsocket_priv.h>
26 #define LOOPBACK_IF "lo0"
27 #define NONE_STR "none"
28 #define ARP_MOD_NAME "arp"
29 #define LIFC_DEFAULT (LIFC_NOXMIT | LIFC_TEMPORARY | LIFC_ALLZONES |\
30 LIFC_UNDER_IPMP)
32 typedef struct if_flags {
33 uint64_t iff_value;
34 char *iff_name;
35 } if_flags_t;
37 static if_flags_t if_flags_tbl[] = {
38 { IFF_UP, "UP" },
39 { IFF_BROADCAST, "BROADCAST" },
40 { IFF_DEBUG, "DEBUG" },
41 { IFF_LOOPBACK, "LOOPBACK" },
42 { IFF_POINTOPOINT, "POINTOPOINT" },
43 { IFF_NOTRAILERS, "NOTRAILERS" },
44 { IFF_RUNNING, "RUNNING" },
45 { IFF_NOARP, "NOARP" },
46 { IFF_PROMISC, "PROMISC" },
47 { IFF_ALLMULTI, "ALLMULTI" },
48 { IFF_INTELLIGENT, "INTELLIGENT" },
49 { IFF_MULTICAST, "MULTICAST" },
50 { IFF_MULTI_BCAST, "MULTI_BCAST" },
51 { IFF_UNNUMBERED, "UNNUMBERED" },
52 { IFF_DHCPRUNNING, "DHCP" },
53 { IFF_PRIVATE, "PRIVATE" },
54 { IFF_NOXMIT, "NOXMIT" },
55 { IFF_NOLOCAL, "NOLOCAL" },
56 { IFF_DEPRECATED, "DEPRECATED" },
57 { IFF_ADDRCONF, "ADDRCONF" },
58 { IFF_ROUTER, "ROUTER" },
59 { IFF_NONUD, "NONUD" },
60 { IFF_ANYCAST, "ANYCAST" },
61 { IFF_NORTEXCH, "NORTEXCH" },
62 { IFF_IPV4, "IPv4" },
63 { IFF_IPV6, "IPv6" },
64 { IFF_NOFAILOVER, "NOFAILOVER" },
65 { IFF_FAILED, "FAILED" },
66 { IFF_STANDBY, "STANDBY" },
67 { IFF_INACTIVE, "INACTIVE" },
68 { IFF_OFFLINE, "OFFLINE" },
69 { IFF_XRESOLV, "XRESOLV" },
70 { IFF_COS_ENABLED, "CoS" },
71 { IFF_PREFERRED, "PREFERRED" },
72 { IFF_TEMPORARY, "TEMPORARY" },
73 { IFF_FIXEDMTU, "FIXEDMTU" },
74 { IFF_VIRTUAL, "VIRTUAL" },
75 { IFF_DUPLICATE, "DUPLICATE" },
76 { IFF_IPMP, "IPMP"},
77 { IFF_VRRP, "VRRP"},
78 { IFF_NOACCEPT, "NOACCEPT"},
79 { IFF_L3PROTECT, "L3PROTECT"}
82 typedef struct {
83 const char *ia_app;
84 uint64_t ia_flag;
85 uint_t ia_tries;
86 } if_appflags_t;
88 static const if_appflags_t if_appflags_tbl[] = {
89 { "dhcpagent(1M)", IFF_DHCPRUNNING, 1 },
90 { "in.ndpd(1M)", IFF_ADDRCONF, 3 },
91 { NULL, 0, 0 }
94 static dladm_handle_t dlh;
95 boolean_t dlh_opened;
96 static struct lifreq lifr;
97 /* current interface name a particular function is accessing */
98 static char name[LIFNAMSIZ];
99 /* foreach interface saved name */
100 static char origname[LIFNAMSIZ];
101 static int setaddr;
102 static boolean_t setaddr_done = _B_FALSE;
103 static boolean_t ipsec_policy_set;
104 static boolean_t ipsec_auth_covered;
105 static ipadm_handle_t iph;
106 static ipadm_addrobj_t ipaddr;
109 * Make sure the algorithm variables hold more than the sizeof an algorithm
110 * in PF_KEY. (For now, more than a uint8_t.) The NO_***_?ALG indicates that
111 * there was no algorithm requested, and in the ipsec_req that service should
112 * be disabled. (E.g. if ah_aalg remains NO_AH_AALG, then AH will be
113 * disabled on that tunnel.)
115 #define NO_AH_AALG 256
116 #define NO_ESP_AALG 256
117 #define NO_ESP_EALG 256
119 int s, s4, s6;
120 int af = AF_INET; /* default address family */
121 int debug = 0;
122 int all = 0; /* setifdhcp() needs to know this */
123 int verbose = 0;
124 int v4compat = 0; /* Compatible printing format */
127 * Function prototypes for command functions.
129 static int addif(char *arg, int64_t param);
130 static int inetipmp(char *arg, int64_t param);
131 static int inetplumb(char *arg, int64_t param);
132 static int inetunplumb(char *arg, int64_t param);
133 static int removeif(char *arg, int64_t param);
134 static int setdebugflag(char *arg, int64_t param);
135 static int setifaddr(char *arg, int64_t param);
136 static int setifbroadaddr(char *arg, int64_t param);
137 static int setifdstaddr(char *arg, int64_t param);
138 static int setifether(char *arg, int64_t param);
139 static int setifflags(char *arg, int64_t param);
140 static int setifindex(char *arg, int64_t param);
141 static int setifmetric(char *arg, int64_t param);
142 static int setifmtu(char *arg, int64_t param);
143 static int setifnetmask(char *arg, int64_t param);
144 static int setifprefixlen(char *arg, int64_t param);
145 static int setifrevarp(char *arg, int64_t param);
146 static int setifsubnet(char *arg, int64_t param);
147 static int setiftdst(char *arg, int64_t param);
148 static int setiftoken(char *arg, int64_t param);
149 static int setiftsrc(char *arg, int64_t param);
150 static int setverboseflag(char *arg, int64_t param);
151 static int set_tun_ah_alg(char *arg, int64_t param);
152 static int set_tun_esp_auth_alg(char *arg, int64_t param);
153 static int set_tun_esp_encr_alg(char *arg, int64_t param);
154 static int modlist(char *arg, int64_t param);
155 static int modinsert(char *arg, int64_t param);
156 static int modremove(char *arg, int64_t param);
157 static int setifgroupname(char *arg, int64_t param);
158 static int configinfo(char *arg, int64_t param);
159 static void print_config_flags(int af, uint64_t flags);
160 static void print_flags(uint64_t flags);
161 static void print_ifether(const char *ifname);
162 static int set_tun_encap_limit(char *arg, int64_t param);
163 static int clr_tun_encap_limit(char *arg, int64_t param);
164 static int set_tun_hop_limit(char *arg, int64_t param);
165 static int setzone(char *arg, int64_t param);
166 static int setifsrc(char *arg, int64_t param);
167 static int lifnum(const char *ifname);
168 static void plumball(int, char **, int64_t, int64_t, int64_t);
171 * Address family specific function prototypes.
173 static void in_getaddr(char *s, struct sockaddr *saddr, int *plenp);
174 static void in_status(int force, uint64_t flags);
175 static void in_configinfo(int force, uint64_t flags);
176 static void in6_getaddr(char *s, struct sockaddr *saddr, int *plenp);
177 static void in6_status(int force, uint64_t flags);
178 static void in6_configinfo(int force, uint64_t flags);
181 * Misc support functions
183 static boolean_t ni_entry(const char *, void *);
184 static void foreachinterface(int argc, char *argv[],
185 int af, int64_t onflags, int64_t offflags,
186 int64_t lifc_flags);
187 static void ifconfig(int argc, char *argv[], int af,
188 struct ifaddrs *ifa);
189 static boolean_t in_getmask(struct sockaddr_in *saddr,
190 boolean_t addr_set);
191 static int in_getprefixlen(char *addr, boolean_t slash, int plen);
192 static boolean_t in_prefixlentomask(int prefixlen, int maxlen,
193 uchar_t *mask);
194 static void status(void);
195 static void ifstatus(const char *ifname);
196 static void tun_status(datalink_id_t);
197 static void usage(void);
198 static int setifdhcp(const char *caller, const char *ifname,
199 int argc, char *argv[]);
200 static int ip_domux2fd(int *, int *, int *, int *, int *);
201 static int ip_plink(int, int, int, int, int);
202 static int modop(char *arg, char op);
203 static int find_all_interfaces(struct lifconf *lifcp, char **buf,
204 int64_t lifc_flags);
205 static int create_ipmp(const char *grname, int af,
206 const char *ifname, boolean_t implicit);
207 static void start_ipmp_daemon(void);
208 static boolean_t ifaddr_up(ifaddrlistx_t *ifaddrp);
209 static boolean_t ifaddr_down(ifaddrlistx_t *ifaddrp);
210 static dladm_status_t ifconfig_dladm_open(const char *, datalink_class_t,
211 datalink_id_t *);
212 static void dladmerr_exit(dladm_status_t status, const char *str);
213 static void ipadmerr_exit(ipadm_status_t status, const char *str);
214 static boolean_t ifconfig_use_libipadm(int, const char *);
216 #define max(a, b) ((a) < (b) ? (b) : (a))
219 * DHCP_EXIT_IF_FAILURE indicates that the operation failed, but if there
220 * are more interfaces to act on (i.e., ifconfig was invoked with -a), keep
221 * on going rather than exit with an error.
224 #define DHCP_EXIT_IF_FAILURE -1
226 #define NEXTARG 0xffffff /* command takes an argument */
227 #define OPTARG 0xfffffe /* command takes an optional argument */
228 #define AF_ANY (-1)
230 /* Refer to the comments in ifconfig() on the netmask "hack" */
231 #define NETMASK_CMD "netmask"
232 struct sockaddr_storage g_netmask;
233 enum { G_NETMASK_NIL, G_NETMASK_PENDING, G_NETMASK_SET }
234 g_netmask_set = G_NETMASK_NIL;
236 struct cmd {
237 char *c_name;
238 int64_t c_parameter; /* NEXTARG means next argv */
239 int (*c_func)(char *, int64_t);
240 int c_abortonfail; /* don't continue parsing args */
241 /* for the current interface */
242 int c_af; /* address family restrictions */
243 } cmds[] = {
244 { "up", IFF_UP, setifflags, 0, AF_ANY },
245 { "down", -IFF_UP, setifflags, 0, AF_ANY },
246 { "trailers", -IFF_NOTRAILERS, setifflags, 0, AF_ANY },
247 { "-trailers", IFF_NOTRAILERS, setifflags, 0, AF_ANY },
248 { "arp", -IFF_NOARP, setifflags, 0, AF_INET },
249 { "-arp", IFF_NOARP, setifflags, 0, AF_INET },
250 { "router", IFF_ROUTER, setifflags, 0, AF_ANY },
251 { "-router", -IFF_ROUTER, setifflags, 0, AF_ANY },
252 { "private", IFF_PRIVATE, setifflags, 0, AF_ANY },
253 { "-private", -IFF_PRIVATE, setifflags, 0, AF_ANY },
254 { "xmit", -IFF_NOXMIT, setifflags, 0, AF_ANY },
255 { "-xmit", IFF_NOXMIT, setifflags, 0, AF_ANY },
256 { "-nud", IFF_NONUD, setifflags, 0, AF_INET6 },
257 { "nud", -IFF_NONUD, setifflags, 0, AF_INET6 },
258 { "anycast", IFF_ANYCAST, setifflags, 0, AF_ANY },
259 { "-anycast", -IFF_ANYCAST, setifflags, 0, AF_ANY },
260 { "local", -IFF_NOLOCAL, setifflags, 0, AF_ANY },
261 { "-local", IFF_NOLOCAL, setifflags, 0, AF_ANY },
262 { "deprecated", IFF_DEPRECATED, setifflags, 0, AF_ANY },
263 { "-deprecated", -IFF_DEPRECATED, setifflags, 0, AF_ANY },
264 { "preferred", IFF_PREFERRED, setifflags, 0, AF_INET6 },
265 { "-preferred", -IFF_PREFERRED, setifflags, 0, AF_INET6 },
266 { "debug", 0, setdebugflag, 0, AF_ANY },
267 { "verbose", 0, setverboseflag, 0, AF_ANY },
268 { NETMASK_CMD, NEXTARG, setifnetmask, 0, AF_INET },
269 { "metric", NEXTARG, setifmetric, 0, AF_ANY },
270 { "mtu", NEXTARG, setifmtu, 0, AF_ANY },
271 { "index", NEXTARG, setifindex, 0, AF_ANY },
272 { "broadcast", NEXTARG, setifbroadaddr, 0, AF_INET },
273 { "auto-revarp", 0, setifrevarp, 1, AF_INET },
274 { "ipmp", 0, inetipmp, 1, AF_ANY },
275 { "plumb", 0, inetplumb, 1, AF_ANY },
276 { "unplumb", 0, inetunplumb, 0, AF_ANY },
277 { "subnet", NEXTARG, setifsubnet, 0, AF_ANY },
278 { "token", NEXTARG, setiftoken, 0, AF_INET6 },
279 { "tsrc", NEXTARG, setiftsrc, 0, AF_ANY },
280 { "tdst", NEXTARG, setiftdst, 0, AF_ANY },
281 { "encr_auth_algs", NEXTARG, set_tun_esp_auth_alg, 0, AF_ANY },
282 { "encr_algs", NEXTARG, set_tun_esp_encr_alg, 0, AF_ANY },
283 { "auth_algs", NEXTARG, set_tun_ah_alg, 0, AF_ANY },
284 { "addif", NEXTARG, addif, 1, AF_ANY },
285 { "removeif", NEXTARG, removeif, 1, AF_ANY },
286 { "modlist", 0, modlist, 1, AF_ANY },
287 { "modinsert", NEXTARG, modinsert, 1, AF_ANY },
288 { "modremove", NEXTARG, modremove, 1, AF_ANY },
289 { "failover", -IFF_NOFAILOVER, setifflags, 1, AF_ANY },
290 { "-failover", IFF_NOFAILOVER, setifflags, 1, AF_ANY },
291 { "standby", IFF_STANDBY, setifflags, 1, AF_ANY },
292 { "-standby", -IFF_STANDBY, setifflags, 1, AF_ANY },
293 { "failed", IFF_FAILED, setifflags, 1, AF_ANY },
294 { "-failed", -IFF_FAILED, setifflags, 1, AF_ANY },
295 { "group", NEXTARG, setifgroupname, 1, AF_ANY },
296 { "configinfo", 0, configinfo, 1, AF_ANY },
297 { "encaplimit", NEXTARG, set_tun_encap_limit, 0, AF_ANY },
298 { "-encaplimit", 0, clr_tun_encap_limit, 0, AF_ANY },
299 { "thoplimit", NEXTARG, set_tun_hop_limit, 0, AF_ANY },
300 { "set", NEXTARG, setifaddr, 0, AF_ANY },
301 { "destination", NEXTARG, setifdstaddr, 0, AF_ANY },
302 { "zone", NEXTARG, setzone, 0, AF_ANY },
303 { "-zone", 0, setzone, 0, AF_ANY },
304 { "ether", OPTARG, setifether, 0, AF_ANY },
305 { "usesrc", NEXTARG, setifsrc, 0, AF_ANY },
308 * NOTE: any additions to this table must also be applied to ifparse
309 * (usr/src/cmd/cmd-inet/sbin/ifparse/ifparse.c)
312 { 0, 0, setifaddr, 0, AF_ANY },
313 { 0, 0, setifdstaddr, 0, AF_ANY },
314 { 0, 0, 0, 0, 0 },
318 typedef struct if_config_cmd {
319 uint64_t iff_flag;
320 int iff_af;
321 char *iff_name;
322 } if_config_cmd_t;
325 * NOTE: print_config_flags() processes this table in order, so we put "up"
326 * last so that we can be sure "-failover" will take effect first. Otherwise,
327 * IPMP test addresses will erroneously migrate to the IPMP interface.
329 static if_config_cmd_t if_config_cmd_tbl[] = {
330 { IFF_NOTRAILERS, AF_UNSPEC, "-trailers" },
331 { IFF_PRIVATE, AF_UNSPEC, "private" },
332 { IFF_NOXMIT, AF_UNSPEC, "-xmit" },
333 { IFF_ANYCAST, AF_INET6, "anycast" },
334 { IFF_NOLOCAL, AF_UNSPEC, "-local" },
335 { IFF_DEPRECATED, AF_UNSPEC, "deprecated" },
336 { IFF_NOFAILOVER, AF_UNSPEC, "-failover" },
337 { IFF_STANDBY, AF_UNSPEC, "standby" },
338 { IFF_FAILED, AF_UNSPEC, "failed" },
339 { IFF_PREFERRED, AF_UNSPEC, "preferred" },
340 { IFF_NONUD, AF_INET6, "-nud" },
341 { IFF_NOARP, AF_INET, "-arp" },
342 { IFF_UP, AF_UNSPEC, "up" },
343 { 0, 0, NULL },
346 typedef struct ni {
347 char ni_name[LIFNAMSIZ];
348 struct ni *ni_next;
349 } ni_t;
351 static ni_t *ni_list = NULL;
352 static int num_ni = 0;
354 /* End defines and structure definitions for ifconfig -a plumb */
356 /* Known address families */
357 struct afswtch {
358 char *af_name;
359 short af_af;
360 void (*af_status)();
361 void (*af_getaddr)();
362 void (*af_configinfo)();
363 } afs[] = {
364 { "inet", AF_INET, in_status, in_getaddr, in_configinfo },
365 { "inet6", AF_INET6, in6_status, in6_getaddr, in6_configinfo },
366 { 0, 0, 0, 0, 0 }
369 #define SOCKET_AF(af) (((af) == AF_UNSPEC) ? AF_INET : (af))
371 struct afswtch *afp; /* the address family being set or asked about */
374 main(int argc, char *argv[])
376 int64_t lifc_flags;
377 char *default_ip_str;
378 ipadm_status_t istatus;
380 lifc_flags = LIFC_DEFAULT;
382 if (argc < 2) {
383 (void) strncpy(name, "-a", sizeof (name));
384 } else {
385 argc--, argv++;
386 if (strlen(*argv) > sizeof (name) - 1) {
387 (void) fprintf(stderr, "%s: interface name too long\n",
388 *argv);
389 exit(1);
391 (void) strncpy(name, *argv, sizeof (name));
393 name[sizeof (name) - 1] = '\0';
394 (void) strncpy(origname, name, sizeof (origname)); /* For addif */
395 default_ip_str = NULL;
396 v4compat = get_compat_flag(&default_ip_str);
397 if (v4compat == DEFAULT_PROT_BAD_VALUE) {
398 (void) fprintf(stderr,
399 "ifconfig: %s: Bad value for %s in %s\n", default_ip_str,
400 DEFAULT_IP, INET_DEFAULT_FILE);
401 free(default_ip_str);
402 exit(2);
404 free(default_ip_str);
405 argc--, argv++;
406 if (argc > 0) {
407 struct afswtch *myafp;
409 for (myafp = afp = afs; myafp->af_name; myafp++) {
410 if (strcmp(myafp->af_name, *argv) == 0) {
411 afp = myafp; argc--; argv++;
412 break;
415 af = lifr.lifr_addr.ss_family = afp->af_af;
416 if (af == AF_INET6) {
417 v4compat = 0;
421 s = socket(SOCKET_AF(af), SOCK_DGRAM, 0);
422 s4 = socket(AF_INET, SOCK_DGRAM, 0);
423 s6 = socket(AF_INET6, SOCK_DGRAM, 0);
424 if (s == -1 || s4 == -1 || s6 == -1)
425 Perror0_exit("socket");
427 * Open the global libipadm handle. The flag IPH_LEGACY has to
428 * be specified to indicate that logical interface names will
429 * be used during interface creation and address creation.
431 if ((istatus = ipadm_open(&iph, IPH_LEGACY)) != IPADM_SUCCESS)
432 ipadmerr_exit(istatus, "unable to open handle to libipadm");
435 * Special interface names is any combination of these flags.
436 * Note that due to the ifconfig syntax they have to be combined
437 * as a single '-' option.
438 * -a All interfaces
439 * -u "up" interfaces
440 * -d "down" interfaces
441 * -D Interfaces not controlled by DHCP
442 * -4 IPv4 interfaces
443 * -6 IPv6 interfaces
444 * -X Turn on debug (not documented)
445 * -v Turn on verbose
446 * -Z Only interfaces in caller's zone
449 if (name[0] == '-') {
450 /* One or more options */
451 int64_t onflags = 0;
452 int64_t offflags = 0;
453 int c;
454 char *av[2] = { "ifconfig", name };
456 while ((c = getopt(2, av, "audhDXZ46v")) != -1) {
457 switch ((char)c) {
458 case 'a':
459 all = 1;
460 break;
461 case 'u':
462 onflags |= IFF_UP;
463 break;
464 case 'd':
465 offflags |= IFF_UP;
466 break;
467 case 'D':
468 offflags |= IFF_DHCPRUNNING;
469 break;
470 case 'X':
471 debug += 3;
472 break;
473 case 'Z':
474 lifc_flags &= ~LIFC_ALLZONES;
475 break;
476 case '4':
478 * -4 is not a compatable flag, therefore
479 * we assume they want v4compat turned off
481 v4compat = 0;
482 onflags |= IFF_IPV4;
483 break;
484 case '6':
486 * If they want IPv6, well then we'll assume
487 * they don't want IPv4 compat
489 v4compat = 0;
490 onflags |= IFF_IPV6;
491 break;
492 case 'v':
493 verbose = 1;
494 break;
495 case 'h':
496 case '?':
497 usage();
498 exit(1);
501 if (!all) {
502 (void) fprintf(stderr,
503 "ifconfig: %s: no such interface\n", name);
504 exit(1);
506 foreachinterface(argc, argv, af, onflags, offflags,
507 lifc_flags);
508 } else {
509 ifconfig(argc, argv, af, NULL);
511 ipadm_close(iph);
512 return (0);
516 * For each interface, call ifconfig(argc, argv, af, ifa).
517 * Only call function if onflags and offflags are set or clear, respectively,
518 * in the interfaces flags field.
520 static void
521 foreachinterface(int argc, char *argv[], int af,
522 int64_t onflags, int64_t offflags, int64_t lifc_flags)
524 ipadm_addr_info_t *ainfo, *ainfop;
525 struct ifaddrs *ifa;
526 ipadm_status_t istatus;
529 * Special case:
530 * ifconfig -a plumb should find all network interfaces in the current
531 * zone.
533 if (argc > 0 && (strcmp(*argv, "plumb") == 0)) {
534 plumball(argc, argv, onflags, offflags, lifc_flags);
535 return;
537 /* Get all addresses in kernel including addresses that are zero. */
538 istatus = ipadm_addr_info(iph, NULL, &ainfo, IPADM_OPT_ZEROADDR,
539 lifc_flags);
540 if (istatus != IPADM_SUCCESS)
541 ipadmerr_exit(istatus, "could not get addresses from kernel");
544 * For each logical interface, call ifconfig() with the
545 * given arguments.
547 for (ainfop = ainfo; ainfop != NULL; ainfop = IA_NEXT(ainfop)) {
548 if (ainfop->ia_state == IFA_DISABLED)
549 continue;
550 ifa = &ainfop->ia_ifa;
551 if (onflags || offflags) {
552 if ((ifa->ifa_flags & onflags) != onflags)
553 continue;
554 if ((~ifa->ifa_flags & offflags) != offflags)
555 continue;
557 s = (ifa->ifa_addr->sa_family == AF_INET ? s4 : s6);
558 (void) strncpy(name, ifa->ifa_name, sizeof (name));
559 (void) strncpy(origname, name, sizeof (origname));
560 ifconfig(argc, argv, af, ifa);
562 ipadm_free_addr_info(ainfo);
566 * Used for `ifconfig -a plumb'. Finds all datalinks and plumbs the interface.
568 static void
569 plumball(int argc, char *argv[], int64_t onflags, int64_t offflags,
570 int64_t lifc_flags)
572 int n;
573 struct lifreq *lifrp;
574 struct lifconf lifc;
575 char *buf;
577 if (onflags != 0 || offflags != 0) {
578 (void) fprintf(stderr, "ifconfig: invalid syntax used to "
579 "plumb all interfaces.\n");
580 exit(1);
583 if (find_all_interfaces(&lifc, &buf, lifc_flags) != 0 ||
584 lifc.lifc_len == 0)
585 return;
587 lifrp = lifc.lifc_req;
588 for (n = lifc.lifc_len / sizeof (struct lifreq); n > 0; n--, lifrp++) {
590 * Reset global state
591 * setaddr: Used by parser to tear apart source and dest
592 * name and origname contain the name of the 'current'
593 * interface.
595 setaddr = 0;
596 (void) strncpy(name, lifrp->lifr_name, sizeof (name));
597 (void) strncpy(origname, name, sizeof (origname));
598 ifconfig(argc, argv, af, NULL);
603 * Parses the interface name and the command in argv[]. Calls the
604 * appropriate callback function for the given command from `cmds[]'
605 * table.
606 * If there is no command specified, it prints all addresses.
608 static void
609 ifconfig(int argc, char *argv[], int af, struct ifaddrs *ifa)
611 static boolean_t scan_netmask = _B_FALSE;
612 int ret;
613 ipadm_status_t istatus;
614 struct lifreq lifr;
616 if (argc == 0) {
617 status();
618 return;
621 if (strcmp(*argv, "auto-dhcp") == 0 || strcmp(*argv, "dhcp") == 0) {
623 * Some errors are ignored in the case where more than one
624 * interface is being operated on.
626 ret = setifdhcp("ifconfig", name, argc, argv);
627 if (ret == DHCP_EXIT_IF_FAILURE) {
628 if (!all)
629 exit(DHCP_EXIT_FAILURE);
630 } else if (ret != DHCP_EXIT_SUCCESS) {
631 exit(ret);
633 return;
637 * The following is a "hack" to get around the existing interface
638 * setting mechanism. Currently, each interface attribute,
639 * such as address, netmask, broadcast, ... is set separately. But
640 * sometimes two or more attributes must be set together. For
641 * example, setting an address without a netmask does not make sense.
642 * Yet they can be set separately for IPv4 address using the current
643 * ifconfig(1M) syntax. The kernel then "infers" the correct netmask
644 * using the deprecated "IP address classes." This is simply not
645 * correct.
647 * The "hack" below is to go thru the whole command list looking for
648 * the netmask command first. Then use this netmask to set the
649 * address. This does not provide an extensible way to accommodate
650 * future need for setting more than one attributes together.
652 * Note that if the "netmask" command argument is a "+", we need
653 * to save this info and do the query after we know the address to
654 * be set. The reason is that if "addif" is used, the working
655 * interface name will be changed later when the logical interface
656 * is created. In in_getmask(), if an address is not provided,
657 * it will use the working interface's address to do the query.
658 * It will be wrong now as we don't know the logical interface's name.
660 * ifconfig(1M) is too overloaded and the code is so convoluted
661 * that it is "safer" not to re-architect the code to fix the above
662 * issue, hence this "hack." We may be better off to have a new
663 * command with better syntax for configuring network interface
664 * parameters...
666 if (!scan_netmask && afp->af_af == AF_INET) {
667 int largc;
668 char **largv;
670 /* Only go thru the command list once to find the netmask. */
671 scan_netmask = _B_TRUE;
674 * Currently, if multiple netmask commands are specified, the
675 * last one will be used as the final netmask. So we need
676 * to scan the whole list to preserve this behavior.
678 for (largc = argc, largv = argv; largc > 0; largc--, largv++) {
679 if (strcmp(*largv, NETMASK_CMD) == 0) {
680 if (--largc == 0)
681 break;
682 largv++;
683 if (strcmp(*largv, "+") == 0) {
684 g_netmask_set = G_NETMASK_PENDING;
685 } else {
686 in_getaddr(*largv, (struct sockaddr *)
687 &g_netmask, NULL);
688 g_netmask_set = G_NETMASK_SET;
690 /* Continue the scan. */
695 while (argc > 0) {
696 struct cmd *p;
697 boolean_t found_cmd;
699 if (debug)
700 (void) printf("ifconfig: argv %s\n", *argv);
702 found_cmd = _B_FALSE;
703 for (p = cmds; p->c_func; p++) {
704 if (p->c_name) {
705 if (strcmp(*argv, p->c_name) == 0) {
707 * indicate that the command was
708 * found and check to see if
709 * the address family is valid
711 found_cmd = _B_TRUE;
712 if (p->c_af == AF_ANY ||
713 af == p->c_af)
714 break;
716 } else {
717 if (p->c_af == AF_ANY ||
718 af == p->c_af)
719 break;
723 * If we found the keyword, but the address family
724 * did not match spit out an error
726 if (found_cmd && p->c_name == 0) {
727 (void) fprintf(stderr, "ifconfig: Operation %s not"
728 " supported for %s\n", *argv, afp->af_name);
729 exit(1);
732 * else (no keyword found), we assume it's an address
733 * of some sort
735 if (setaddr && ipaddr != NULL) {
737 * We must have already filled in a source address in
738 * `ipaddr' and we now got a destination address.
739 * Fill it in `ipaddr' and call libipadm to create
740 * the static address.
742 if (p->c_name == 0) {
743 istatus = ipadm_set_dst_addr(ipaddr, *argv,
744 (p->c_af == AF_ANY ? AF_UNSPEC : af));
745 if (istatus != IPADM_SUCCESS) {
746 ipadmerr_exit(istatus, "could not "
747 "set destination address");
750 * finished processing dstaddr, so reset setaddr
752 setaddr = 0;
755 * Both source and destination address are in `ipaddr'.
756 * Add the address by calling libipadm.
758 istatus = ipadm_create_addr(iph, ipaddr,
759 IPADM_OPT_ACTIVE);
760 if (istatus != IPADM_SUCCESS)
761 goto createfailed;
762 ipadm_destroy_addrobj(ipaddr);
763 ipaddr = NULL;
764 setaddr_done = _B_TRUE;
765 if (p->c_name == 0) {
766 /* move parser along */
767 argc--, argv++;
768 continue;
771 if (p->c_name == 0 && setaddr_done) {
773 * catch odd commands like
774 * "ifconfig <intf> addr1 addr2 addr3 addr4 up"
776 (void) fprintf(stderr, "%s",
777 "ifconfig: cannot configure more than two "
778 "addresses in one command\n");
779 exit(1);
781 if (p->c_func) {
782 if (p->c_af == AF_INET6) {
783 v4compat = 0;
785 if (p->c_parameter == NEXTARG ||
786 p->c_parameter == OPTARG) {
787 argc--, argv++;
788 if (argc == 0 && p->c_parameter == NEXTARG) {
789 (void) fprintf(stderr,
790 "ifconfig: no argument for %s\n",
791 p->c_name);
792 exit(1);
796 * Call the function if:
798 * there's no address family
799 * restriction
800 * OR
801 * we don't know the address yet
802 * (because we were called from
803 * main)
804 * OR
805 * there is a restriction AND
806 * the address families match
808 if ((p->c_af == AF_ANY) ||
809 (ifa == NULL) ||
810 (ifa->ifa_addr->sa_family == p->c_af)) {
811 ret = (*p->c_func)(*argv, p->c_parameter);
813 * If c_func failed and we should
814 * abort processing for this
815 * interface on failure, return
816 * now rather than going on to
817 * process other commands for
818 * the same interface.
820 if (ret != 0 && p->c_abortonfail)
821 return;
824 argc--, argv++;
827 if (setaddr && ipaddr != NULL) {
829 * Only the source address was provided, which was already
830 * set in `ipaddr'. Add the address by calling libipadm.
832 istatus = ipadm_create_addr(iph, ipaddr, IPADM_OPT_ACTIVE);
833 if (istatus != IPADM_SUCCESS)
834 goto createfailed;
835 ipadm_destroy_addrobj(ipaddr);
836 ipaddr = NULL;
837 setaddr_done = _B_TRUE;
840 /* Check to see if there's a security hole in the tunnel setup. */
841 if (ipsec_policy_set && !ipsec_auth_covered) {
842 (void) fprintf(stderr, "ifconfig: WARNING: tunnel with only "
843 "ESP and no authentication.\n");
845 return;
847 createfailed:
848 (void) fprintf(stderr, "ifconfig: could not create address:% s\n",
849 ipadm_status2str(istatus));
850 /* Remove the newly created logical interface. */
851 if (strcmp(name, origname) != 0) {
852 assert(strchr(name, ':') != NULL);
853 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
854 (void) ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr);
856 exit(1);
859 /* ARGSUSED */
860 static int
861 setdebugflag(char *val, int64_t arg)
863 debug++;
864 return (0);
867 /* ARGSUSED */
868 static int
869 setverboseflag(char *val, int64_t arg)
871 verbose++;
872 return (0);
876 * This function fills in the given lifreq's lifr_addr field based on
877 * g_netmask_set.
879 static void
880 set_mask_lifreq(struct lifreq *lifr, struct sockaddr_storage *addr,
881 struct sockaddr_storage *mask)
883 assert(addr != NULL);
884 assert(mask != NULL);
886 switch (g_netmask_set) {
887 case G_NETMASK_SET:
888 lifr->lifr_addr = g_netmask;
889 break;
891 case G_NETMASK_PENDING:
893 * "+" is used as the argument to "netmask" command. Query
894 * the database on the correct netmask based on the address to
895 * be set.
897 assert(afp->af_af == AF_INET);
898 g_netmask = *addr;
899 if (!in_getmask((struct sockaddr_in *)&g_netmask, _B_TRUE)) {
900 lifr->lifr_addr = *mask;
901 g_netmask_set = G_NETMASK_NIL;
902 } else {
903 lifr->lifr_addr = g_netmask;
904 g_netmask_set = G_NETMASK_SET;
906 break;
908 case G_NETMASK_NIL:
909 default:
910 lifr->lifr_addr = *mask;
911 break;
916 * Set the interface address. Handles <addr>, <addr>/<n> as well as /<n>
917 * syntax for setting the address, the address plus netmask, and just
918 * the netmask respectively.
920 /* ARGSUSED */
921 static int
922 setifaddr(char *addr, int64_t param)
924 ipadm_status_t istatus;
925 int prefixlen = 0;
926 struct lifreq lifr1;
927 struct sockaddr_storage laddr;
928 struct sockaddr_storage netmask;
929 struct sockaddr_in6 *sin6;
930 struct sockaddr_in *sin;
931 struct sockaddr_storage sav_netmask;
932 char cidraddr[BUFSIZ];
934 if (addr[0] == '/')
935 return (setifprefixlen(addr, 0));
937 (*afp->af_getaddr)(addr, (struct sockaddr *)&laddr, &prefixlen);
939 (void) memset(&netmask, 0, sizeof (netmask));
940 netmask.ss_family = afp->af_af;
941 switch (prefixlen) {
942 case NO_PREFIX:
943 /* Nothing there - ok */
944 break;
945 case BAD_ADDR:
946 (void) fprintf(stderr, "ifconfig: Bad prefix length in %s\n",
947 addr);
948 exit(1);
949 default:
950 if (afp->af_af == AF_INET6) {
951 sin6 = (struct sockaddr_in6 *)&netmask;
952 if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
953 (uchar_t *)&sin6->sin6_addr)) {
954 (void) fprintf(stderr, "ifconfig: "
955 "Bad prefix length: %d\n",
956 prefixlen);
957 exit(1);
959 } else {
960 sin = (struct sockaddr_in *)&netmask;
961 if (!in_prefixlentomask(prefixlen, IP_ABITS,
962 (uchar_t *)&sin->sin_addr)) {
963 (void) fprintf(stderr, "ifconfig: "
964 "Bad prefix length: %d\n",
965 prefixlen);
966 exit(1);
970 * Just in case of funny setting of both prefix and netmask,
971 * prefix should override the netmask command.
973 g_netmask_set = G_NETMASK_NIL;
974 break;
978 * Check and see if any "netmask" command is used and perform the
979 * necessary operation.
981 set_mask_lifreq(&lifr, &laddr, &netmask);
983 /* This check is temporary until libipadm supports IPMP interfaces. */
984 if (ifconfig_use_libipadm(s, name)) {
985 char addrstr[INET6_ADDRSTRLEN];
987 if (af == AF_INET) {
988 sin = (struct sockaddr_in *)&laddr;
989 (void) inet_ntop(AF_INET, &sin->sin_addr, addrstr,
990 sizeof (addrstr));
991 } else {
992 sin6 = (struct sockaddr_in6 *)&laddr;
993 (void) inet_ntop(AF_INET6, &sin6->sin6_addr, addrstr,
994 sizeof (addrstr));
996 istatus = ipadm_create_addrobj(IPADM_ADDR_STATIC, name,
997 &ipaddr);
998 if (istatus != IPADM_SUCCESS)
999 ipadmerr_exit(istatus, "setifaddr");
1002 * lifr.lifr_addr, which is updated by set_mask_lifreq()
1003 * will contain the right mask to use.
1005 prefixlen = mask2plen((struct sockaddr *)&lifr.lifr_addr);
1006 (void) snprintf(cidraddr, sizeof (cidraddr), "%s/%d",
1007 addrstr, prefixlen);
1009 istatus = ipadm_set_addr(ipaddr, cidraddr, af);
1010 if (istatus != IPADM_SUCCESS)
1011 ipadmerr_exit(istatus, "could not set address");
1013 * let parser know we got a source.
1014 * Next address, if given, should be dest
1016 setaddr++;
1019 * address will be set by the parser after nextarg has
1020 * been scanned
1022 return (0);
1025 /* Tell parser that an address was set */
1026 setaddr++;
1027 /* save copy of netmask to restore in case of error */
1028 (void) strncpy(lifr1.lifr_name, name, sizeof (lifr1.lifr_name));
1029 if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr1) < 0)
1030 Perror0_exit("SIOCGLIFNETMASK");
1031 sav_netmask = lifr1.lifr_addr;
1034 * If setting the address and not the mask, clear any existing mask
1035 * and the kernel will then assign the default (netmask has been set
1036 * to 0 in this case). If setting both (either by using a prefix or
1037 * using the netmask command), set the mask first, so the address will
1038 * be interpreted correctly.
1040 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1041 /* lifr.lifr_addr already contains netmask from set_mask_lifreq() */
1042 if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
1043 Perror0_exit("SIOCSLIFNETMASK");
1045 if (debug) {
1046 char abuf[INET6_ADDRSTRLEN];
1047 void *addr = (afp->af_af == AF_INET) ?
1048 (void *)&((struct sockaddr_in *)&laddr)->sin_addr :
1049 (void *)&((struct sockaddr_in6 *)&laddr)->sin6_addr;
1051 (void) printf("Setting %s af %d addr %s\n",
1052 lifr.lifr_name, afp->af_af,
1053 inet_ntop(afp->af_af, addr, abuf, sizeof (abuf)));
1055 lifr.lifr_addr = laddr;
1056 lifr.lifr_addr.ss_family = afp->af_af;
1057 if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0) {
1059 * Restore the netmask
1061 int saverr = errno;
1063 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1064 lifr.lifr_addr = sav_netmask;
1065 (void) ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr);
1066 errno = saverr;
1067 Perror0_exit("SIOCSLIFADDR");
1070 return (0);
1074 * The following functions are stolen from the ipseckey(1m) program.
1075 * Perhaps they should be somewhere common, but for now, we just maintain
1076 * two versions. We do this because of the different semantics for which
1077 * algorithms we select ("requested" for ifconfig vs. "actual" for key).
1080 static ulong_t
1081 parsenum(char *num)
1083 ulong_t rc;
1084 char *end = NULL;
1086 errno = 0;
1087 rc = strtoul(num, &end, 0);
1088 if (errno != 0 || end == num || *end != '\0') {
1089 rc = (ulong_t)-1;
1092 return (rc);
1096 * Parse and reverse parse possible algorithm values, include numbers.
1097 * Mostly stolen from ipseckey.c. See the comments above parsenum() for why
1098 * this isn't common to ipseckey.c.
1100 * NOTE: Static buffer in this function for the return value. Since ifconfig
1101 * isn't multithreaded, this isn't a huge problem.
1104 #define NBUF_SIZE 20 /* Enough to print a large integer. */
1106 static char *
1107 rparsealg(uint8_t alg_value, int proto_num)
1109 struct ipsecalgent *alg;
1110 static char numprint[128]; /* Enough to hold an algorithm name. */
1113 * Special cases for "any" and "none"
1114 * The kernel needs to be able to distinguish between "any"
1115 * and "none" and the APIs are underdefined in this area for auth.
1117 if (proto_num == IPSEC_PROTO_AH) {
1118 if (alg_value == SADB_AALG_NONE)
1119 return ("none");
1120 if (alg_value == SADB_AALG_ANY)
1121 return ("any");
1124 alg = getipsecalgbynum(alg_value, proto_num, NULL);
1125 if (alg != NULL) {
1126 (void) strlcpy(numprint, alg->a_names[0], sizeof (numprint));
1127 freeipsecalgent(alg);
1128 } else {
1129 (void) snprintf(numprint, sizeof (numprint), "%d", alg_value);
1132 return (numprint);
1135 static uint_t
1136 parsealg(char *algname, int proto_num)
1138 struct ipsecalgent *alg;
1139 ulong_t invalue;
1141 if (algname == NULL) {
1142 (void) fprintf(stderr, "ifconfig: Unexpected end of command "
1143 "line.\n");
1144 exit(1);
1148 * Special-case "none" and "any".
1149 * Use strcasecmp because its length is bounded.
1151 if (strcasecmp("none", algname) == 0) {
1152 return ((proto_num == IPSEC_PROTO_ESP) ?
1153 NO_ESP_EALG : NO_ESP_AALG);
1155 if ((strcasecmp("any", algname) == 0) && (proto_num == IPSEC_PROTO_AH))
1156 return (SADB_AALG_ANY);
1158 alg = getipsecalgbyname(algname, proto_num, NULL);
1159 if (alg != NULL) {
1160 invalue = alg->a_alg_num;
1161 freeipsecalgent(alg);
1162 return ((uint_t)invalue);
1166 * Since algorithms can be loaded during kernel run-time, check for
1167 * numeric algorithm values too.
1169 invalue = parsenum(algname);
1170 if ((invalue & (ulong_t)0xff) == invalue)
1171 return ((uint_t)invalue);
1173 (void) fprintf(stderr, "ifconfig: %s algorithm type %s unknown.\n",
1174 (proto_num == IPSEC_PROTO_ESP) ?
1175 "Encryption" : "Authentication", algname);
1176 exit(1);
1177 /* NOTREACHED */
1181 * Actual ifconfig functions to set tunnel security properties.
1184 enum ipsec_alg_type { ESP_ENCR_ALG = 1, ESP_AUTH_ALG, AH_AUTH_ALG };
1186 static int
1187 set_tun_algs(int which_alg, int alg)
1189 boolean_t encr_alg_set = _B_FALSE;
1190 iptun_params_t params;
1191 dladm_status_t status;
1192 ipsec_req_t *ipsr;
1194 if ((status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN,
1195 &params.iptun_param_linkid)) != DLADM_STATUS_OK)
1196 goto done;
1198 status = dladm_iptun_getparams(dlh, &params, DLADM_OPT_ACTIVE);
1199 if (status != DLADM_STATUS_OK)
1200 goto done;
1202 ipsr = &params.iptun_param_secinfo;
1205 * If I'm just starting off this ifconfig, I want a clean slate,
1206 * otherwise, I've captured the current tunnel security settings.
1207 * In the case of continuation, I merely add to the settings.
1209 if (!(params.iptun_param_flags & IPTUN_PARAM_SECINFO))
1210 (void) memset(ipsr, 0, sizeof (*ipsr));
1212 /* We're only modifying the IPsec information */
1213 params.iptun_param_flags = IPTUN_PARAM_SECINFO;
1215 switch (which_alg) {
1216 case ESP_ENCR_ALG:
1217 if (alg == NO_ESP_EALG) {
1218 if (ipsr->ipsr_esp_auth_alg == SADB_AALG_NONE)
1219 ipsr->ipsr_esp_req = 0;
1220 ipsr->ipsr_esp_alg = SADB_EALG_NONE;
1222 /* Let the user specify NULL encryption implicitly. */
1223 if (ipsr->ipsr_esp_auth_alg != SADB_AALG_NONE) {
1224 encr_alg_set = _B_TRUE;
1225 ipsr->ipsr_esp_alg = SADB_EALG_NULL;
1227 } else {
1228 encr_alg_set = _B_TRUE;
1229 ipsr->ipsr_esp_req =
1230 IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
1231 ipsr->ipsr_esp_alg = alg;
1233 break;
1234 case ESP_AUTH_ALG:
1235 if (alg == NO_ESP_AALG) {
1236 if ((ipsr->ipsr_esp_alg == SADB_EALG_NONE ||
1237 ipsr->ipsr_esp_alg == SADB_EALG_NULL) &&
1238 !encr_alg_set)
1239 ipsr->ipsr_esp_req = 0;
1240 ipsr->ipsr_esp_auth_alg = SADB_AALG_NONE;
1241 } else {
1242 ipsr->ipsr_esp_req =
1243 IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
1244 ipsr->ipsr_esp_auth_alg = alg;
1246 /* Let the user specify NULL encryption implicitly. */
1247 if (ipsr->ipsr_esp_alg == SADB_EALG_NONE &&
1248 !encr_alg_set)
1249 ipsr->ipsr_esp_alg = SADB_EALG_NULL;
1251 break;
1252 case AH_AUTH_ALG:
1253 if (alg == NO_AH_AALG) {
1254 ipsr->ipsr_ah_req = 0;
1255 ipsr->ipsr_auth_alg = SADB_AALG_NONE;
1256 } else {
1257 ipsr->ipsr_ah_req =
1258 IPSEC_PREF_REQUIRED | IPSEC_PREF_UNIQUE;
1259 ipsr->ipsr_auth_alg = alg;
1261 break;
1262 /* Will never hit DEFAULT */
1265 status = dladm_iptun_modify(dlh, &params, DLADM_OPT_ACTIVE);
1267 done:
1268 if (status != DLADM_STATUS_OK)
1269 dladmerr_exit(status, name);
1270 else {
1271 ipsec_policy_set = _B_TRUE;
1272 if ((ipsr->ipsr_esp_req != 0 &&
1273 ipsr->ipsr_esp_auth_alg != SADB_AALG_NONE) ||
1274 (ipsr->ipsr_ah_req != 0 &&
1275 ipsr->ipsr_auth_alg != SADB_AALG_NONE))
1276 ipsec_auth_covered = _B_TRUE;
1278 return (0);
1281 /* ARGSUSED */
1282 static int
1283 set_tun_esp_encr_alg(char *addr, int64_t param)
1285 return (set_tun_algs(ESP_ENCR_ALG,
1286 parsealg(addr, IPSEC_PROTO_ESP)));
1289 /* ARGSUSED */
1290 static int
1291 set_tun_esp_auth_alg(char *addr, int64_t param)
1293 return (set_tun_algs(ESP_AUTH_ALG,
1294 parsealg(addr, IPSEC_PROTO_AH)));
1297 /* ARGSUSED */
1298 static int
1299 set_tun_ah_alg(char *addr, int64_t param)
1301 return (set_tun_algs(AH_AUTH_ALG,
1302 parsealg(addr, IPSEC_PROTO_AH)));
1305 /* ARGSUSED */
1306 static int
1307 setifrevarp(char *arg, int64_t param)
1309 struct sockaddr_in laddr;
1311 if (afp->af_af == AF_INET6) {
1312 (void) fprintf(stderr,
1313 "ifconfig: revarp not possible on IPv6 interface %s\n",
1314 name);
1315 exit(1);
1317 if (doifrevarp(name, &laddr)) {
1318 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1319 laddr.sin_family = AF_INET;
1320 (void) memcpy(&lifr.lifr_addr, &laddr, sizeof (laddr));
1321 if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
1322 Perror0_exit("SIOCSLIFADDR");
1324 return (0);
1327 /* ARGSUSED */
1328 static int
1329 setifsubnet(char *addr, int64_t param)
1331 int prefixlen = 0;
1332 struct sockaddr_storage subnet;
1334 (*afp->af_getaddr)(addr, &subnet, &prefixlen);
1336 switch (prefixlen) {
1337 case NO_PREFIX:
1338 (void) fprintf(stderr,
1339 "ifconfig: Missing prefix length in subnet %s\n", addr);
1340 exit(1);
1341 /* NOTREACHED */
1342 case BAD_ADDR:
1343 (void) fprintf(stderr,
1344 "ifconfig: Bad prefix length in %s\n", addr);
1345 exit(1);
1346 default:
1347 break;
1350 lifr.lifr_addr = subnet;
1351 lifr.lifr_addrlen = prefixlen;
1352 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1353 if (ioctl(s, SIOCSLIFSUBNET, (caddr_t)&lifr) < 0)
1354 Perror0_exit("SIOCSLIFSUBNET");
1356 return (0);
1359 /* ARGSUSED */
1360 static int
1361 setifnetmask(char *addr, int64_t param)
1363 struct sockaddr_in netmask;
1365 assert(afp->af_af != AF_INET6);
1367 if (strcmp(addr, "+") == 0) {
1368 if (!in_getmask(&netmask, _B_FALSE))
1369 return (0);
1370 (void) printf("Setting netmask of %s to %s\n", name,
1371 inet_ntoa(netmask.sin_addr));
1372 } else {
1373 in_getaddr(addr, (struct sockaddr *)&netmask, NULL);
1375 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1376 (void) memcpy(&lifr.lifr_addr, &netmask, sizeof (netmask));
1377 if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
1378 Perror0_exit("SIOCSLIFNETMASK");
1379 return (0);
1383 * Parse '/<n>' as a netmask.
1385 /* ARGSUSED */
1386 static int
1387 setifprefixlen(char *addr, int64_t param)
1389 int prefixlen;
1390 int af = afp->af_af;
1392 prefixlen = in_getprefixlen(addr, _B_TRUE,
1393 (af == AF_INET) ? IP_ABITS : IPV6_ABITS);
1394 if (prefixlen < 0) {
1395 (void) fprintf(stderr,
1396 "ifconfig: Bad prefix length in %s\n", addr);
1397 exit(1);
1399 (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
1400 lifr.lifr_addr.ss_family = af;
1401 if (af == AF_INET6) {
1402 struct sockaddr_in6 *sin6;
1404 sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
1405 if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
1406 (uchar_t *)&sin6->sin6_addr)) {
1407 (void) fprintf(stderr, "ifconfig: "
1408 "Bad prefix length: %d\n",
1409 prefixlen);
1410 exit(1);
1412 } else if (af == AF_INET) {
1413 struct sockaddr_in *sin;
1415 sin = (struct sockaddr_in *)&lifr.lifr_addr;
1416 if (!in_prefixlentomask(prefixlen, IP_ABITS,
1417 (uchar_t *)&sin->sin_addr)) {
1418 (void) fprintf(stderr, "ifconfig: "
1419 "Bad prefix length: %d\n",
1420 prefixlen);
1421 exit(1);
1423 } else {
1424 (void) fprintf(stderr, "ifconfig: setting prefix only supported"
1425 " for address family inet or inet6\n");
1426 exit(1);
1428 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1429 if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
1430 Perror0_exit("SIOCSLIFNETMASK");
1431 return (0);
1434 /* ARGSUSED */
1435 static int
1436 setifbroadaddr(char *addr, int64_t param)
1438 struct sockaddr_in broadaddr;
1440 assert(afp->af_af != AF_INET6);
1442 if (strcmp(addr, "+") == 0) {
1444 * This doesn't set the broadcast address at all. Rather, it
1445 * gets, then sets the interface's address, relying on the fact
1446 * that resetting the address will reset the broadcast address.
1448 (void) strncpy(lifr.lifr_name, name,
1449 sizeof (lifr.lifr_name));
1450 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
1451 if (errno != EADDRNOTAVAIL)
1452 Perror0_exit("SIOCGLIFADDR");
1453 return (0);
1455 if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
1456 Perror0_exit("SIOCGLIFADDR");
1458 return (0);
1460 in_getaddr(addr, (struct sockaddr *)&broadaddr, NULL);
1462 (void) memcpy(&lifr.lifr_addr, &broadaddr, sizeof (broadaddr));
1463 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1464 if (ioctl(s, SIOCSLIFBRDADDR, (caddr_t)&lifr) < 0)
1465 Perror0_exit("SIOCSLIFBRDADDR");
1466 return (0);
1470 * set interface destination address
1472 /* ARGSUSED */
1473 static int
1474 setifdstaddr(char *addr, int64_t param)
1476 (*afp->af_getaddr)(addr, (struct sockaddr *)&lifr.lifr_addr, NULL);
1477 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1478 if (ioctl(s, SIOCSLIFDSTADDR, (caddr_t)&lifr) < 0)
1479 Perror0_exit("setifdstaddr: SIOCSLIFDSTADDR");
1480 return (0);
1483 /* ARGSUSED */
1484 static int
1485 setifflags(char *val, int64_t value)
1487 struct lifreq lifrl; /* local lifreq struct */
1488 boolean_t bringup = _B_FALSE;
1490 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1491 if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0)
1492 Perror0_exit("setifflags: SIOCGLIFFLAGS");
1494 if (value < 0) {
1495 value = -value;
1497 if ((value & IFF_NOFAILOVER) && (lifr.lifr_flags & IFF_UP)) {
1499 * The kernel does not allow administratively up test
1500 * addresses to be converted to data addresses. Bring
1501 * the address down first, then bring it up after it's
1502 * been converted to a data address.
1504 lifr.lifr_flags &= ~IFF_UP;
1505 (void) ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr);
1506 bringup = _B_TRUE;
1509 lifr.lifr_flags &= ~value;
1510 if ((value & (IFF_UP | IFF_NOFAILOVER)) &&
1511 (lifr.lifr_flags & IFF_DUPLICATE)) {
1513 * If the user is trying to mark an interface with a
1514 * duplicate address as "down," or convert a duplicate
1515 * test address to a data address, then fetch the
1516 * address and set it. This will cause IP to clear
1517 * the IFF_DUPLICATE flag and stop the automatic
1518 * recovery timer.
1520 value = lifr.lifr_flags;
1521 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) >= 0)
1522 (void) ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr);
1523 lifr.lifr_flags = value;
1525 } else {
1526 lifr.lifr_flags |= value;
1530 * If we're about to bring up an underlying physical IPv6 interface in
1531 * an IPMP group, ensure the IPv6 IPMP interface is also up. This is
1532 * for backward compatibility with legacy configurations in which
1533 * there are no explicit hostname files for IPMP interfaces. (For
1534 * IPv4, this is automatically handled by the kernel when migrating
1535 * the underlying interface's data address to the IPMP interface.)
1537 (void) strlcpy(lifrl.lifr_name, name, LIFNAMSIZ);
1539 if (lifnum(lifr.lifr_name) == 0 &&
1540 (lifr.lifr_flags & (IFF_UP|IFF_IPV6)) == (IFF_UP|IFF_IPV6) &&
1541 ioctl(s, SIOCGLIFGROUPNAME, &lifrl) == 0 &&
1542 lifrl.lifr_groupname[0] != '\0') {
1543 lifgroupinfo_t lifgr;
1545 (void) strlcpy(lifgr.gi_grname, lifrl.lifr_groupname,
1546 LIFGRNAMSIZ);
1547 if (ioctl(s, SIOCGLIFGROUPINFO, &lifgr) == -1)
1548 Perror0_exit("setifflags: SIOCGLIFGROUPINFO");
1550 (void) strlcpy(lifrl.lifr_name, lifgr.gi_grifname, LIFNAMSIZ);
1551 if (ioctl(s, SIOCGLIFFLAGS, &lifrl) == -1)
1552 Perror0_exit("setifflags: SIOCGLIFFLAGS");
1553 if (!(lifrl.lifr_flags & IFF_UP)) {
1554 lifrl.lifr_flags |= IFF_UP;
1555 if (ioctl(s, SIOCSLIFFLAGS, &lifrl) == -1)
1556 Perror0_exit("setifflags: SIOCSLIFFLAGS");
1560 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1561 if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0)
1562 Perror0_exit("setifflags: SIOCSLIFFLAGS");
1564 if (bringup) {
1565 lifr.lifr_flags |= IFF_UP;
1566 if (ioctl(s, SIOCSLIFFLAGS, (caddr_t)&lifr) < 0)
1567 Perror0_exit("setifflags: SIOCSLIFFLAGS IFF_UP");
1570 return (0);
1573 /* ARGSUSED */
1574 static int
1575 setifmetric(char *val, int64_t param)
1577 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1578 lifr.lifr_metric = atoi(val);
1579 if (ioctl(s, SIOCSLIFMETRIC, (caddr_t)&lifr) < 0)
1580 Perror0_exit("setifmetric: SIOCSLIFMETRIC");
1581 return (0);
1584 /* ARGSUSED */
1585 static int
1586 setifmtu(char *val, int64_t param)
1588 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1589 lifr.lifr_mtu = atoi(val);
1590 if (ioctl(s, SIOCSLIFMTU, (caddr_t)&lifr) < 0)
1591 Perror0_exit("setifmtu: SIOCSLIFMTU");
1592 return (0);
1595 /* ARGSUSED */
1596 static int
1597 setifindex(char *val, int64_t param)
1599 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1600 lifr.lifr_index = atoi(val);
1601 if (ioctl(s, SIOCSLIFINDEX, (caddr_t)&lifr) < 0)
1602 Perror0_exit("setifindex: SIOCSLIFINDEX");
1603 return (0);
1606 /* ARGSUSED */
1607 static void
1608 notifycb(dlpi_handle_t dh, dlpi_notifyinfo_t *dnip, void *arg)
1612 /* ARGSUSED */
1613 static int
1614 setifether(char *addr, int64_t param)
1616 uchar_t *hwaddr;
1617 int hwaddrlen;
1618 int retval;
1619 ifaddrlistx_t *ifaddrp, *ifaddrs = NULL;
1620 dlpi_handle_t dh;
1621 dlpi_notifyid_t id;
1623 if (addr == NULL) {
1624 ifstatus(name);
1625 print_ifether(name);
1626 return (0);
1630 * if the IP interface in the arguments is a logical
1631 * interface, exit with an error now.
1633 if (strchr(name, ':') != NULL) {
1634 (void) fprintf(stderr, "ifconfig: cannot change"
1635 " ethernet address of a logical interface\n");
1636 exit(1);
1639 if ((hwaddr = _link_aton(addr, &hwaddrlen)) == NULL) {
1640 if (hwaddrlen == -1)
1641 (void) fprintf(stderr,
1642 "ifconfig: bad ethernet address\n");
1643 else
1644 (void) fprintf(stderr, "ifconfig: malloc() failed\n");
1645 exit(1);
1648 if ((retval = dlpi_open(name, &dh, 0)) != DLPI_SUCCESS)
1649 Perrdlpi_exit("cannot dlpi_open() link", name, retval);
1651 retval = dlpi_enabnotify(dh, DL_NOTE_PHYS_ADDR, notifycb, NULL, &id);
1652 if (retval == DLPI_SUCCESS) {
1653 (void) dlpi_disabnotify(dh, id, NULL);
1654 } else {
1656 * This link does not support DL_NOTE_PHYS_ADDR: bring down
1657 * all of the addresses to flush the old hardware address
1658 * information out of IP.
1660 * NOTE: Skipping this when DL_NOTE_PHYS_ADDR is supported is
1661 * more than an optimization: in.mpathd will set IFF_OFFLINE
1662 * if it's notified and the new address is a duplicate of
1663 * another in the group -- but the flags manipulation in
1664 * ifaddr_{down,up}() cannot be atomic and thus might clobber
1665 * IFF_OFFLINE, confusing in.mpathd.
1667 if (ifaddrlistx(name, IFF_UP, 0, &ifaddrs) == -1)
1668 Perror2_exit(name, "cannot get address list");
1670 ifaddrp = ifaddrs;
1671 for (; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
1672 if (!ifaddr_down(ifaddrp)) {
1673 Perror2_exit(ifaddrp->ia_name,
1674 "cannot bring down");
1680 * Change the hardware address.
1682 retval = dlpi_set_physaddr(dh, DL_CURR_PHYS_ADDR, hwaddr, hwaddrlen);
1683 if (retval != DLPI_SUCCESS) {
1684 (void) fprintf(stderr,
1685 "ifconfig: failed setting mac address on %s\n", name);
1687 dlpi_close(dh);
1690 * If any addresses were brought down before changing the hardware
1691 * address, bring them up again.
1693 for (ifaddrp = ifaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
1694 if (!ifaddr_up(ifaddrp))
1695 Perror2_exit(ifaddrp->ia_name, "cannot bring up");
1697 ifaddrlistx_free(ifaddrs);
1699 return (0);
1703 * Print an interface's Ethernet address, if it has one.
1705 static void
1706 print_ifether(const char *ifname)
1708 int fd;
1710 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1712 fd = socket(AF_INET, SOCK_DGRAM, 0);
1713 if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
1715 * It's possible the interface is only configured for
1716 * IPv6; check again with AF_INET6.
1718 (void) close(fd);
1719 fd = socket(AF_INET6, SOCK_DGRAM, 0);
1720 if (fd == -1 || ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) {
1721 (void) close(fd);
1722 return;
1725 (void) close(fd);
1727 /* VNI and IPMP interfaces don't have MAC addresses */
1728 if (lifr.lifr_flags & (IFF_VIRTUAL|IFF_IPMP))
1729 return;
1731 /* IP tunnels also don't have Ethernet-like MAC addresses */
1732 if (ifconfig_dladm_open(ifname, DATALINK_CLASS_IPTUN, NULL) ==
1733 DLADM_STATUS_OK)
1734 return;
1736 dlpi_print_address(ifname);
1740 * static int find_all_interfaces(struct lifconf *lifcp, char **buf,
1741 * int64_t lifc_flags)
1743 * It finds all active data links.
1745 * It takes in input a pointer to struct lifconf to receive interfaces
1746 * informations, a **char to hold allocated buffer, and a lifc_flags.
1748 * Return values:
1749 * 0 = everything OK
1750 * -1 = problem
1752 static int
1753 find_all_interfaces(struct lifconf *lifcp, char **buf, int64_t lifc_flags)
1755 unsigned bufsize;
1756 int n;
1757 ni_t *nip;
1758 struct lifreq *lifrp;
1759 dladm_status_t status;
1761 if (!dlh_opened) {
1762 status = ifconfig_dladm_open(NULL, 0, NULL);
1763 if (status != DLADM_STATUS_OK)
1764 dladmerr_exit(status, "unable to open dladm handle");
1767 (void) dlpi_walk(ni_entry, dlh, 0);
1769 /* Now, translate the linked list into a struct lifreq buffer */
1770 if (num_ni == 0) {
1771 lifcp->lifc_family = AF_UNSPEC;
1772 lifcp->lifc_flags = lifc_flags;
1773 lifcp->lifc_len = 0;
1774 lifcp->lifc_buf = NULL;
1775 return (0);
1778 bufsize = num_ni * sizeof (struct lifreq);
1779 if ((*buf = malloc(bufsize)) == NULL)
1780 Perror0_exit("find_all_interfaces: malloc failed");
1782 lifcp->lifc_family = AF_UNSPEC;
1783 lifcp->lifc_flags = lifc_flags;
1784 lifcp->lifc_len = bufsize;
1785 lifcp->lifc_buf = *buf;
1787 for (n = 0, lifrp = lifcp->lifc_req; n < num_ni; n++, lifrp++) {
1788 nip = ni_list;
1789 (void) strncpy(lifrp->lifr_name, nip->ni_name,
1790 sizeof (lifr.lifr_name));
1791 ni_list = nip->ni_next;
1792 free(nip);
1794 return (0);
1798 * Create the next unused logical interface using the original name
1799 * and assign the address (and mask if '/<n>' is part of the address).
1800 * Use the new logical interface for subsequent subcommands by updating
1801 * the name variable.
1803 * This allows syntax like:
1804 * ifconfig le0 addif 109.106.86.130 netmask + up \
1805 * addif 109.106.86.131 netmask + up
1807 /* ARGSUSED */
1808 static int
1809 addif(char *str, int64_t param)
1811 int prefixlen = 0;
1812 struct sockaddr_storage laddr;
1813 struct sockaddr_storage mask;
1814 struct sockaddr_in6 *sin6;
1815 struct sockaddr_in *sin;
1816 ipadm_status_t istatus;
1817 char cidraddr[BUFSIZ];
1818 char addrstr[INET6_ADDRSTRLEN];
1820 (void) strncpy(name, origname, sizeof (name));
1822 if (strchr(name, ':') != NULL) {
1823 (void) fprintf(stderr,
1824 "ifconfig: addif: bad physical interface name %s\n",
1825 name);
1826 exit(1);
1830 * clear so parser will interpret next address as source followed
1831 * by possible dest
1833 setaddr = 0;
1834 (*afp->af_getaddr)(str, (struct sockaddr *)&laddr, &prefixlen);
1836 (void) memset(&mask, 0, sizeof (mask));
1837 mask.ss_family = afp->af_af;
1839 switch (prefixlen) {
1840 case NO_PREFIX:
1841 /* Nothing there - ok */
1842 break;
1843 case BAD_ADDR:
1844 (void) fprintf(stderr,
1845 "ifconfig: Bad prefix length in %s\n", str);
1846 exit(1);
1847 default:
1848 (void) memset(&mask, 0, sizeof (mask));
1849 mask.ss_family = afp->af_af;
1850 if (afp->af_af == AF_INET6) {
1851 sin6 = (struct sockaddr_in6 *)&mask;
1852 if (!in_prefixlentomask(prefixlen, IPV6_ABITS,
1853 (uchar_t *)&sin6->sin6_addr)) {
1854 (void) fprintf(stderr, "ifconfig: "
1855 "Bad prefix length: %d\n",
1856 prefixlen);
1857 exit(1);
1859 } else {
1860 sin = (struct sockaddr_in *)&mask;
1861 if (!in_prefixlentomask(prefixlen, IP_ABITS,
1862 (uchar_t *)&sin->sin_addr)) {
1863 (void) fprintf(stderr, "ifconfig: "
1864 "Bad prefix length: %d\n",
1865 prefixlen);
1866 exit(1);
1869 g_netmask_set = G_NETMASK_NIL;
1870 break;
1874 * This is a "hack" to get around the problem of SIOCLIFADDIF. The
1875 * problem is that this ioctl does not include the netmask when
1876 * adding a logical interface. This is the same problem described
1877 * in the ifconfig() comments. To get around this problem, we first
1878 * add the logical interface with a 0 address. After that, we set
1879 * the netmask if provided. Finally we set the interface address.
1881 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
1882 (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
1884 /* Note: no need to do DAD here since the interface isn't up yet. */
1886 if (ioctl(s, SIOCLIFADDIF, (caddr_t)&lifr) < 0)
1887 Perror0_exit("addif: SIOCLIFADDIF");
1889 (void) printf("Created new logical interface %s\n",
1890 lifr.lifr_name);
1891 (void) strncpy(name, lifr.lifr_name, sizeof (name));
1894 * Check and see if any "netmask" command is used and perform the
1895 * necessary operation.
1897 set_mask_lifreq(&lifr, &laddr, &mask);
1899 /* This check is temporary until libipadm supports IPMP interfaces. */
1900 if (ifconfig_use_libipadm(s, name)) {
1902 * We added the logical interface above before calling
1903 * ipadm_create_addr(), because, with IPH_LEGACY, we need
1904 * to do an addif for `ifconfig ce0 addif <addr>' but not for
1905 * `ifconfig ce0 <addr>'. libipadm does not have a flag to
1906 * to differentiate between these two cases. To keep it simple,
1907 * we always create the logical interface and pass it to
1908 * libipadm instead of requiring libipadm to addif for some
1909 * cases and not do addif for other cases.
1911 istatus = ipadm_create_addrobj(IPADM_ADDR_STATIC, name,
1912 &ipaddr);
1913 if (istatus != IPADM_SUCCESS)
1914 ipadmerr_exit(istatus, "addif");
1916 if (af == AF_INET) {
1917 sin = (struct sockaddr_in *)&laddr;
1918 (void) inet_ntop(AF_INET, &sin->sin_addr, addrstr,
1919 sizeof (addrstr));
1920 } else {
1921 sin6 = (struct sockaddr_in6 *)&laddr;
1922 (void) inet_ntop(AF_INET6, &sin6->sin6_addr, addrstr,
1923 sizeof (addrstr));
1926 * lifr.lifr_addr, which is updated by set_mask_lifreq()
1927 * will contain the right mask to use.
1929 prefixlen = mask2plen((struct sockaddr *)&lifr.lifr_addr);
1931 (void) snprintf(cidraddr, sizeof (cidraddr), "%s/%d",
1932 addrstr, prefixlen);
1934 istatus = ipadm_set_addr(ipaddr, cidraddr, af);
1936 if (istatus != IPADM_SUCCESS)
1937 ipadmerr_exit(istatus, "could not set address");
1938 setaddr++;
1940 * address will be set by the parser after nextarg
1941 * has been scanned
1943 return (0);
1947 * Only set the netmask if "netmask" command is used or a prefix is
1948 * provided.
1950 if (g_netmask_set == G_NETMASK_SET || prefixlen >= 0) {
1952 * lifr.lifr_addr already contains netmask from
1953 * set_mask_lifreq().
1955 if (ioctl(s, SIOCSLIFNETMASK, (caddr_t)&lifr) < 0)
1956 Perror0_exit("addif: SIOCSLIFNETMASK");
1959 /* Finally, we set the interface address. */
1960 lifr.lifr_addr = laddr;
1961 if (ioctl(s, SIOCSLIFADDR, (caddr_t)&lifr) < 0)
1962 Perror0_exit("SIOCSLIFADDR");
1965 * let parser know we got a source.
1966 * Next address, if given, should be dest
1968 setaddr++;
1969 return (0);
1973 * Remove a logical interface based on its IP address. Unlike addif
1974 * there is no '/<n>' here.
1975 * Verifies that the interface is down before it is removed.
1977 /* ARGSUSED */
1978 static int
1979 removeif(char *str, int64_t param)
1981 struct sockaddr_storage laddr;
1982 ipadm_status_t istatus;
1983 ipadm_addr_info_t *ainfo, *ainfop;
1985 if (strchr(name, ':') != NULL) {
1986 (void) fprintf(stderr,
1987 "ifconfig: removeif: bad physical interface name %s\n",
1988 name);
1989 exit(1);
1992 (*afp->af_getaddr)(str, &laddr, NULL);
1995 * Following check is temporary until libipadm supports
1996 * IPMP interfaces.
1998 if (!ifconfig_use_libipadm(s, name))
1999 goto delete;
2002 * Get all addresses and search this address among the active
2003 * addresses. If an address object was found, delete using
2004 * ipadm_delete_addr().
2006 istatus = ipadm_addr_info(iph, name, &ainfo, 0, LIFC_DEFAULT);
2007 if (istatus != IPADM_SUCCESS)
2008 ipadmerr_exit(istatus, "removeif");
2010 for (ainfop = ainfo; ainfop != NULL; ainfop = IA_NEXT(ainfop))
2011 if (sockaddrcmp(
2012 (struct sockaddr_storage *)ainfop->ia_ifa.ifa_addr, &laddr))
2013 break;
2015 if (ainfop != NULL) {
2016 if (strchr(ainfop->ia_ifa.ifa_name, ':') == NULL) {
2017 (void) fprintf(stderr,
2018 "ifconfig: removeif: cannot remove interface: %s\n",
2019 name);
2020 exit(1);
2022 if (ainfop->ia_aobjname[0] != '\0') {
2023 istatus = ipadm_delete_addr(iph, ainfop->ia_aobjname,
2024 IPADM_OPT_ACTIVE);
2025 if (istatus != IPADM_SUCCESS) {
2026 ipadmerr_exit(istatus,
2027 "could not delete address");
2029 ipadm_free_addr_info(ainfo);
2030 return (0);
2033 ipadm_free_addr_info(ainfo);
2035 delete:
2037 * An address object for this address was not found in ipadm.
2038 * Delete with SIOCLIFREMOVEIF.
2040 lifr.lifr_addr = laddr;
2041 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2042 if (ioctl(s, SIOCLIFREMOVEIF, (caddr_t)&lifr) < 0) {
2043 if (errno == EBUSY) {
2044 /* This can only happen if ipif_id = 0 */
2045 (void) fprintf(stderr,
2046 "ifconfig: removeif: cannot remove interface: %s\n",
2047 name);
2048 exit(1);
2050 Perror0_exit("removeif: SIOCLIFREMOVEIF");
2052 return (0);
2056 * Set the address token for IPv6.
2058 /* ARGSUSED */
2059 static int
2060 setiftoken(char *addr, int64_t param)
2062 int prefixlen = 0;
2063 struct sockaddr_in6 token;
2065 in6_getaddr(addr, (struct sockaddr *)&token, &prefixlen);
2066 switch (prefixlen) {
2067 case NO_PREFIX:
2068 (void) fprintf(stderr,
2069 "ifconfig: Missing prefix length in subnet %s\n", addr);
2070 exit(1);
2071 /* NOTREACHED */
2072 case BAD_ADDR:
2073 (void) fprintf(stderr,
2074 "ifconfig: Bad prefix length in %s\n", addr);
2075 exit(1);
2076 default:
2077 break;
2079 (void) memcpy(&lifr.lifr_addr, &token, sizeof (token));
2080 lifr.lifr_addrlen = prefixlen;
2081 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2082 if (ioctl(s, SIOCSLIFTOKEN, (caddr_t)&lifr) < 0) {
2083 Perror0_exit("setiftoken: SIOCSLIFTOKEN");
2085 return (0);
2088 /* ARGSUSED */
2089 static int
2090 setifgroupname(char *grname, int64_t param)
2092 lifgroupinfo_t lifgr;
2093 struct lifreq lifrl;
2094 ifaddrlistx_t *ifaddrp, *nextifaddrp;
2095 ifaddrlistx_t *ifaddrs = NULL, *downaddrs = NULL;
2096 int af;
2098 if (debug) {
2099 (void) printf("Setting groupname %s on interface %s\n",
2100 grname, name);
2103 (void) strlcpy(lifrl.lifr_name, name, LIFNAMSIZ);
2104 (void) strlcpy(lifrl.lifr_groupname, grname, LIFGRNAMSIZ);
2106 while (ioctl(s, SIOCSLIFGROUPNAME, &lifrl) == -1) {
2107 switch (errno) {
2108 case ENOENT:
2110 * The group doesn't yet exist; create it and repeat.
2112 af = afp->af_af;
2113 if (create_ipmp(grname, af, NULL, _B_TRUE) == -1) {
2114 if (errno == EEXIST)
2115 continue;
2117 Perror2(grname, "cannot create IPMP group");
2118 goto fail;
2120 continue;
2122 case EALREADY:
2124 * The interface is already in another group; must
2125 * remove existing membership first.
2127 lifrl.lifr_groupname[0] = '\0';
2128 if (ioctl(s, SIOCSLIFGROUPNAME, &lifrl) == -1) {
2129 Perror2(name, "cannot remove existing "
2130 "IPMP group membership");
2131 goto fail;
2133 (void) strlcpy(lifrl.lifr_groupname, grname,
2134 LIFGRNAMSIZ);
2135 continue;
2137 case EAFNOSUPPORT:
2139 * The group exists, but it's not configured with the
2140 * address families the interface needs. Since only
2141 * two address families are currently supported, just
2142 * configure the "other" address family. Note that we
2143 * may race with group deletion or creation by another
2144 * process (ENOENT or EEXIST); in such cases we repeat
2145 * our original SIOCSLIFGROUPNAME.
2147 (void) strlcpy(lifgr.gi_grname, grname, LIFGRNAMSIZ);
2148 if (ioctl(s, SIOCGLIFGROUPINFO, &lifgr) == -1) {
2149 if (errno == ENOENT)
2150 continue;
2152 Perror2(grname, "SIOCGLIFGROUPINFO");
2153 goto fail;
2156 af = lifgr.gi_v4 ? AF_INET6 : AF_INET;
2157 if (create_ipmp(grname, af, lifgr.gi_grifname,
2158 _B_TRUE) == -1) {
2159 if (errno == EEXIST)
2160 continue;
2162 Perror2(grname, "cannot configure IPMP group");
2163 goto fail;
2165 continue;
2167 case EADDRINUSE:
2169 * Some addresses are in-use (or under control of DAD).
2170 * Bring them down and retry the group join operation.
2171 * We will bring them back up after the interface has
2172 * been placed in the group.
2174 if (ifaddrlistx(lifrl.lifr_name, IFF_UP|IFF_DUPLICATE,
2175 0, &ifaddrs) == -1) {
2176 Perror2(grname, "cannot get address list");
2177 goto fail;
2180 ifaddrp = ifaddrs;
2181 for (; ifaddrp != NULL; ifaddrp = nextifaddrp) {
2182 if (!ifaddr_down(ifaddrp)) {
2183 ifaddrs = ifaddrp;
2184 goto fail;
2186 nextifaddrp = ifaddrp->ia_next;
2187 ifaddrp->ia_next = downaddrs;
2188 downaddrs = ifaddrp;
2190 ifaddrs = NULL;
2191 continue;
2193 case EADDRNOTAVAIL: {
2195 * Some data addresses are under application control.
2196 * For some of these (e.g., ADDRCONF), the application
2197 * should remove the address, in which case we retry a
2198 * few times (since the application's action is not
2199 * atomic with respect to us) before bailing out and
2200 * informing the user.
2202 int ntries, nappaddr = 0;
2203 const if_appflags_t *iap = if_appflags_tbl;
2205 for (; iap->ia_app != NULL; iap++) {
2206 ntries = 0;
2207 again:
2208 if (ifaddrlistx(lifrl.lifr_name, iap->ia_flag,
2209 IFF_NOFAILOVER, &ifaddrs) == -1) {
2210 (void) fprintf(stderr, "ifconfig: %s: "
2211 "cannot get data addresses managed "
2212 "by %s\n", lifrl.lifr_name,
2213 iap->ia_app);
2214 goto fail;
2217 if (ifaddrs == NULL)
2218 continue;
2220 ifaddrlistx_free(ifaddrs);
2221 ifaddrs = NULL;
2223 if (++ntries < iap->ia_tries) {
2224 (void) poll(NULL, 0, 100);
2225 goto again;
2228 (void) fprintf(stderr, "ifconfig: cannot join "
2229 "IPMP group: %s has data addresses managed "
2230 "by %s\n", lifrl.lifr_name, iap->ia_app);
2231 nappaddr++;
2233 if (nappaddr > 0)
2234 goto fail;
2235 continue;
2237 default:
2238 Perror2(name, "SIOCSLIFGROUPNAME");
2239 goto fail;
2244 * If the interface being moved is under the control of `ipmgmtd(1M)'
2245 * dameon then we should inform the daemon about this move, so that
2246 * the daemon can delete the state associated with this interface.
2248 * This workaround is needed until the IPMP support in ipadm(1M).
2250 ipadm_if_move(iph, name);
2253 * If there were addresses that we had to bring down, it's time to
2254 * bring them up again. As part of bringing them up, the kernel will
2255 * automatically move them to the new IPMP interface.
2257 for (ifaddrp = downaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
2258 if (!ifaddr_up(ifaddrp) && errno != ENXIO) {
2259 (void) fprintf(stderr, "ifconfig: cannot bring back up "
2260 "%s: %s\n", ifaddrp->ia_name, strerror(errno));
2263 ifaddrlistx_free(downaddrs);
2264 return (0);
2265 fail:
2267 * Attempt to bring back up any interfaces that we downed.
2269 for (ifaddrp = downaddrs; ifaddrp != NULL; ifaddrp = ifaddrp->ia_next) {
2270 if (!ifaddr_up(ifaddrp) && errno != ENXIO) {
2271 (void) fprintf(stderr, "ifconfig: cannot bring back up "
2272 "%s: %s\n", ifaddrp->ia_name, strerror(errno));
2275 ifaddrlistx_free(downaddrs);
2276 ifaddrlistx_free(ifaddrs);
2279 * We'd return -1, but foreachinterface() doesn't propagate the error
2280 * into the exit status, so we're forced to explicitly exit().
2282 exit(1);
2283 /* NOTREACHED */
2286 static boolean_t
2287 modcheck(const char *ifname)
2289 (void) strlcpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2291 if (ioctl(s, SIOCGLIFFLAGS, &lifr) < 0) {
2292 Perror0("SIOCGLIFFLAGS");
2293 return (_B_FALSE);
2296 if (lifr.lifr_flags & IFF_IPMP) {
2297 (void) fprintf(stderr, "ifconfig: %s: module operations not"
2298 " supported on IPMP interfaces\n", ifname);
2299 return (_B_FALSE);
2301 if (lifr.lifr_flags & IFF_VIRTUAL) {
2302 (void) fprintf(stderr, "ifconfig: %s: module operations not"
2303 " supported on virtual IP interfaces\n", ifname);
2304 return (_B_FALSE);
2306 return (_B_TRUE);
2310 * To list all the modules above a given network interface.
2312 /* ARGSUSED */
2313 static int
2314 modlist(char *null, int64_t param)
2316 int muxid_fd;
2317 int muxfd;
2318 int ipfd_lowstr;
2319 int arpfd_lowstr;
2320 int num_mods;
2321 int i;
2322 struct str_list strlist;
2323 int orig_arpid;
2326 * We'd return -1, but foreachinterface() doesn't propagate the error
2327 * into the exit status, so we're forced to explicitly exit().
2329 if (!modcheck(name))
2330 exit(1);
2332 if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr,
2333 &orig_arpid) < 0) {
2334 return (-1);
2336 if ((num_mods = ioctl(ipfd_lowstr, I_LIST, NULL)) < 0) {
2337 Perror0("cannot I_LIST to get the number of modules");
2338 } else {
2339 if (debug > 0) {
2340 (void) printf("Listing (%d) modules above %s\n",
2341 num_mods, name);
2344 strlist.sl_nmods = num_mods;
2345 strlist.sl_modlist = malloc(sizeof (struct str_mlist) *
2346 num_mods);
2347 if (strlist.sl_modlist == NULL) {
2348 Perror0("cannot malloc");
2349 } else {
2350 if (ioctl(ipfd_lowstr, I_LIST, (caddr_t)&strlist) < 0) {
2351 Perror0("cannot I_LIST for module names");
2352 } else {
2353 for (i = 0; i < strlist.sl_nmods; i++) {
2354 (void) printf("%d %s\n", i,
2355 strlist.sl_modlist[i].l_name);
2358 free(strlist.sl_modlist);
2361 return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr,
2362 orig_arpid));
2365 #define MODINSERT_OP 'i'
2366 #define MODREMOVE_OP 'r'
2369 * To insert a module to the stream of the interface. It is just a
2370 * wrapper. The real function is modop().
2372 /* ARGSUSED */
2373 static int
2374 modinsert(char *arg, int64_t param)
2376 return (modop(arg, MODINSERT_OP));
2380 * To remove a module from the stream of the interface. It is just a
2381 * wrapper. The real function is modop().
2383 /* ARGSUSED */
2384 static int
2385 modremove(char *arg, int64_t param)
2387 return (modop(arg, MODREMOVE_OP));
2391 * Helper function for mod*() functions. It gets a fd to the lower IP
2392 * stream and I_PUNLINK's the lower stream. It also initializes the
2393 * global variable lifr.
2395 * Param:
2396 * int *muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK
2397 * int *muxid_fd: fd to /dev/udp{,6} for LIFMUXID
2398 * int *ipfd_lowstr: fd to the lower IP stream.
2399 * int *arpfd_lowstr: fd to the lower ARP stream.
2401 * Return:
2402 * -1 if operation fails, 0 otherwise.
2404 * Please see the big block comment above ifplumb() for the logic of the
2405 * PLINK/PUNLINK
2407 static int
2408 ip_domux2fd(int *muxfd, int *muxid_fd, int *ipfd_lowstr, int *arpfd_lowstr,
2409 int *orig_arpid)
2411 uint64_t flags;
2412 char *udp_dev_name;
2414 *orig_arpid = 0;
2415 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2416 if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2417 Perror0_exit("status: SIOCGLIFFLAGS");
2419 flags = lifr.lifr_flags;
2420 if (flags & IFF_IPV4) {
2421 udp_dev_name = UDP_DEV_NAME;
2422 } else if (flags & IFF_IPV6) {
2423 udp_dev_name = UDP6_DEV_NAME;
2424 } else {
2425 return (-1);
2428 if ((*muxid_fd = open(udp_dev_name, O_RDWR)) < 0) {
2429 Perror2("open", udp_dev_name);
2430 return (-1);
2432 if (ioctl(*muxid_fd, SIOCGLIFMUXID, (caddr_t)&lifr) < 0) {
2433 Perror2("SIOCGLIFMUXID", udp_dev_name);
2434 return (-1);
2436 if (debug > 0) {
2437 (void) printf("ARP_muxid %d IP_muxid %d\n",
2438 lifr.lifr_arp_muxid, lifr.lifr_ip_muxid);
2442 * Use /dev/udp{,6} as the mux to avoid linkcycles.
2444 if (ipadm_open_arp_on_udp(udp_dev_name, muxfd) != IPADM_SUCCESS)
2445 return (-1);
2447 if (lifr.lifr_arp_muxid != 0) {
2448 if ((*arpfd_lowstr = ioctl(*muxfd, _I_MUXID2FD,
2449 lifr.lifr_arp_muxid)) < 0) {
2450 if ((errno == EINVAL) &&
2451 (flags & (IFF_NOARP | IFF_IPV6))) {
2453 * Some plumbing utilities set the muxid to
2454 * -1 or some invalid value to signify that
2455 * there is no arp stream. Set the muxid to 0
2456 * before trying to unplumb the IP stream.
2457 * IP does not allow the IP stream to be
2458 * unplumbed if it sees a non-null arp muxid,
2459 * for consistency of IP-ARP streams.
2461 *orig_arpid = lifr.lifr_arp_muxid;
2462 lifr.lifr_arp_muxid = 0;
2463 (void) ioctl(*muxid_fd, SIOCSLIFMUXID,
2464 (caddr_t)&lifr);
2465 *arpfd_lowstr = -1;
2466 } else {
2467 Perror0("_I_MUXID2FD");
2468 return (-1);
2470 } else if (ioctl(*muxfd, I_PUNLINK,
2471 lifr.lifr_arp_muxid) < 0) {
2472 Perror2("I_PUNLINK", udp_dev_name);
2473 return (-1);
2475 } else {
2476 *arpfd_lowstr = -1;
2479 if ((*ipfd_lowstr = ioctl(*muxfd, _I_MUXID2FD,
2480 lifr.lifr_ip_muxid)) < 0) {
2481 Perror0("_I_MUXID2FD");
2482 /* Undo any changes we made */
2483 if (*orig_arpid != 0) {
2484 lifr.lifr_arp_muxid = *orig_arpid;
2485 (void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
2487 return (-1);
2489 if (ioctl(*muxfd, I_PUNLINK, lifr.lifr_ip_muxid) < 0) {
2490 Perror2("I_PUNLINK", udp_dev_name);
2491 /* Undo any changes we made */
2492 if (*orig_arpid != 0) {
2493 lifr.lifr_arp_muxid = *orig_arpid;
2494 (void) ioctl(*muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
2496 return (-1);
2498 return (0);
2502 * Helper function for mod*() functions. It I_PLINK's back the upper and
2503 * lower IP streams. Note that this function must be called after
2504 * ip_domux2fd(). In ip_domux2fd(), the global variable lifr is initialized
2505 * and ip_plink() needs information in lifr. So ip_domux2fd() and ip_plink()
2506 * must be called in pairs.
2508 * Param:
2509 * int muxfd: fd to /dev/udp{,6} for I_PLINK/I_PUNLINK
2510 * int muxid_fd: fd to /dev/udp{,6} for LIFMUXID
2511 * int ipfd_lowstr: fd to the lower IP stream.
2512 * int arpfd_lowstr: fd to the lower ARP stream.
2514 * Return:
2515 * -1 if operation fails, 0 otherwise.
2517 * Please see the big block comment above ifplumb() for the logic of the
2518 * PLINK/PUNLINK
2520 static int
2521 ip_plink(int muxfd, int muxid_fd, int ipfd_lowstr, int arpfd_lowstr,
2522 int orig_arpid)
2524 int ip_muxid;
2526 ip_muxid = ioctl(muxfd, I_PLINK, ipfd_lowstr);
2527 if (ip_muxid < 0) {
2528 Perror2("I_PLINK", UDP_DEV_NAME);
2529 return (-1);
2533 * If there is an arp stream, plink it. If there is no
2534 * arp stream, then it is possible that the plumbing
2535 * utility could have stored any value in the arp_muxid.
2536 * If so, restore it from orig_arpid.
2538 if (arpfd_lowstr != -1) {
2539 if (ioctl(muxfd, I_PLINK, arpfd_lowstr) < 0) {
2540 Perror2("I_PLINK", UDP_DEV_NAME);
2541 return (-1);
2543 } else if (orig_arpid != 0) {
2544 /* Undo the changes we did in ip_domux2fd */
2545 lifr.lifr_arp_muxid = orig_arpid;
2546 lifr.lifr_ip_muxid = ip_muxid;
2547 (void) ioctl(muxid_fd, SIOCSLIFMUXID, (caddr_t)&lifr);
2550 (void) close(muxfd);
2551 (void) close(muxid_fd);
2552 return (0);
2556 * The real function to perform module insertion/removal.
2558 * Param:
2559 * char *arg: the argument string module_name@position
2560 * char op: operation, either MODINSERT_OP or MODREMOVE_OP.
2562 * Return:
2563 * Before doing ip_domux2fd(), this function calls exit(1) in case of
2564 * error. After ip_domux2fd() is done, it returns -1 for error, 0
2565 * otherwise.
2567 static int
2568 modop(char *arg, char op)
2570 char *pos_p;
2571 int muxfd;
2572 int muxid_fd;
2573 int ipfd_lowstr; /* IP stream (lower stream of mux) to be plinked */
2574 int arpfd_lowstr; /* ARP stream (lower stream of mux) to be plinked */
2575 struct strmodconf mod;
2576 char *at_char = "@";
2577 char *arg_str;
2578 int orig_arpid;
2581 * We'd return -1, but foreachinterface() doesn't propagate the error
2582 * into the exit status, so we're forced to explicitly exit().
2584 if (!modcheck(name))
2585 exit(1);
2587 /* Need to save the original string for -a option. */
2588 if ((arg_str = malloc(strlen(arg) + 1)) == NULL) {
2589 Perror0("cannot malloc");
2590 return (-1);
2592 (void) strcpy(arg_str, arg);
2594 if (*arg_str == *at_char) {
2595 (void) fprintf(stderr,
2596 "ifconfig: must supply a module name\n");
2597 exit(1);
2599 mod.mod_name = strtok(arg_str, at_char);
2600 if (strlen(mod.mod_name) > FMNAMESZ) {
2601 (void) fprintf(stderr, "ifconfig: module name too long: %s\n",
2602 mod.mod_name);
2603 exit(1);
2607 * Need to make sure that the core TCP/IP stack modules are not
2608 * removed. Otherwise, "bad" things can happen. If a module
2609 * is removed and inserted back, it loses its old state. But
2610 * the modules above it still have the old state. E.g. IP assumes
2611 * fast data path while tunnel after re-inserted assumes that it can
2612 * receive M_DATA only in fast data path for which it does not have
2613 * any state. This is a general caveat of _I_REMOVE/_I_INSERT.
2615 if (op == MODREMOVE_OP &&
2616 (strcmp(mod.mod_name, ARP_MOD_NAME) == 0 ||
2617 strcmp(mod.mod_name, IP_MOD_NAME) == 0)) {
2618 (void) fprintf(stderr, "ifconfig: cannot remove %s\n",
2619 mod.mod_name);
2620 exit(1);
2623 if ((pos_p = strtok(NULL, at_char)) == NULL) {
2624 (void) fprintf(stderr, "ifconfig: must supply a position\n");
2625 exit(1);
2627 mod.pos = atoi(pos_p);
2629 if (ip_domux2fd(&muxfd, &muxid_fd, &ipfd_lowstr, &arpfd_lowstr,
2630 &orig_arpid) < 0) {
2631 free(arg_str);
2632 return (-1);
2634 switch (op) {
2635 case MODINSERT_OP:
2636 if (debug > 0) {
2637 (void) printf("Inserting module %s at %d\n",
2638 mod.mod_name, mod.pos);
2640 if (ioctl(ipfd_lowstr, _I_INSERT, (caddr_t)&mod) < 0) {
2641 Perror2("fail to insert module", mod.mod_name);
2643 break;
2644 case MODREMOVE_OP:
2645 if (debug > 0) {
2646 (void) printf("Removing module %s at %d\n",
2647 mod.mod_name, mod.pos);
2649 if (ioctl(ipfd_lowstr, _I_REMOVE, (caddr_t)&mod) < 0) {
2650 Perror2("fail to remove module", mod.mod_name);
2652 break;
2653 default:
2654 /* Should never get to here. */
2655 (void) fprintf(stderr, "Unknown operation\n");
2656 break;
2658 free(arg_str);
2659 return (ip_plink(muxfd, muxid_fd, ipfd_lowstr, arpfd_lowstr,
2660 orig_arpid));
2663 static int
2664 modify_tun(iptun_params_t *params)
2666 dladm_status_t status;
2668 if ((status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN,
2669 &params->iptun_param_linkid)) == DLADM_STATUS_OK)
2670 status = dladm_iptun_modify(dlh, params, DLADM_OPT_ACTIVE);
2671 if (status != DLADM_STATUS_OK)
2672 dladmerr_exit(status, name);
2673 return (0);
2677 * Set tunnel source address
2679 /* ARGSUSED */
2680 static int
2681 setiftsrc(char *addr, int64_t param)
2683 iptun_params_t params;
2685 params.iptun_param_flags = IPTUN_PARAM_LADDR;
2686 (void) strlcpy(params.iptun_param_laddr, addr,
2687 sizeof (params.iptun_param_laddr));
2688 return (modify_tun(&params));
2692 * Set tunnel destination address
2694 /* ARGSUSED */
2695 static int
2696 setiftdst(char *addr, int64_t param)
2698 iptun_params_t params;
2700 params.iptun_param_flags = IPTUN_PARAM_RADDR;
2701 (void) strlcpy(params.iptun_param_raddr, addr,
2702 sizeof (params.iptun_param_raddr));
2703 return (modify_tun(&params));
2706 static int
2707 set_tun_prop(const char *propname, char *value)
2709 dladm_status_t status;
2710 datalink_id_t linkid;
2712 status = ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, &linkid);
2713 if (status == DLADM_STATUS_OK) {
2714 status = dladm_set_linkprop(dlh, linkid, propname, &value, 1,
2715 DLADM_OPT_ACTIVE);
2717 if (status != DLADM_STATUS_OK)
2718 dladmerr_exit(status, name);
2719 return (0);
2722 /* Set tunnel encapsulation limit. */
2723 /* ARGSUSED */
2724 static int
2725 set_tun_encap_limit(char *arg, int64_t param)
2727 return (set_tun_prop("encaplimit", arg));
2730 /* Disable encapsulation limit. */
2731 /* ARGSUSED */
2732 static int
2733 clr_tun_encap_limit(char *arg, int64_t param)
2735 return (set_tun_encap_limit("-1", 0));
2738 /* Set tunnel hop limit. */
2739 /* ARGSUSED */
2740 static int
2741 set_tun_hop_limit(char *arg, int64_t param)
2743 return (set_tun_prop("hoplimit", arg));
2746 /* Set zone ID */
2747 static int
2748 setzone(char *arg, int64_t param)
2750 zoneid_t zoneid = GLOBAL_ZONEID;
2752 if (param == NEXTARG) {
2753 /* zone must be active */
2754 if ((zoneid = getzoneidbyname(arg)) == -1) {
2755 (void) fprintf(stderr,
2756 "ifconfig: unknown zone '%s'\n", arg);
2757 exit(1);
2760 (void) strlcpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2761 lifr.lifr_zoneid = zoneid;
2762 if (ioctl(s, SIOCSLIFZONE, (caddr_t)&lifr) == -1)
2763 Perror0_exit("SIOCSLIFZONE");
2764 return (0);
2767 /* Set source address to use */
2768 /* ARGSUSED */
2769 static int
2770 setifsrc(char *arg, int64_t param)
2772 uint_t ifindex = 0;
2773 int rval;
2775 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2778 * Argument can be either an interface name or "none". The latter means
2779 * that any previous selection is cleared.
2782 if (strchr(arg, ':') != NULL) {
2783 (void) fprintf(stderr,
2784 "ifconfig: Cannot specify logical interface for usesrc \n");
2785 exit(1);
2788 rval = strcmp(arg, NONE_STR);
2789 if (rval != 0) {
2790 if ((ifindex = if_nametoindex(arg)) == 0) {
2791 (void) strncpy(lifr.lifr_name, arg, LIFNAMSIZ);
2792 Perror0_exit("Could not get interface index");
2794 lifr.lifr_index = ifindex;
2795 } else {
2796 if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) != 0)
2797 Perror0_exit("Not a valid usesrc consumer");
2798 lifr.lifr_index = 0;
2801 if (debug)
2802 (void) printf("setifsrc: lifr_name %s, lifr_index %d\n",
2803 lifr.lifr_name, lifr.lifr_index);
2805 if (ioctl(s, SIOCSLIFUSESRC, (caddr_t)&lifr) == -1) {
2806 if (rval == 0)
2807 Perror0_exit("Cannot reset usesrc group");
2808 else
2809 Perror0_exit("Could not set source interface");
2812 return (0);
2816 * Print the interface status line associated with `ifname'
2818 static void
2819 ifstatus(const char *ifname)
2821 uint64_t flags;
2822 char if_usesrc_name[LIFNAMSIZ];
2823 char *newbuf;
2824 int n, numifs, rval = 0;
2825 struct lifreq *lifrp;
2826 struct lifsrcof lifs;
2828 (void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2829 if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2830 Perror0_exit("status: SIOCGLIFFLAGS");
2832 flags = lifr.lifr_flags;
2835 * In V4 compatibility mode, we don't print the IFF_IPV4 flag or
2836 * interfaces with IFF_IPV6 set.
2838 if (v4compat) {
2839 flags &= ~IFF_IPV4;
2840 if (flags & IFF_IPV6)
2841 return;
2844 (void) printf("%s: ", ifname);
2845 print_flags(flags);
2847 (void) strncpy(lifr.lifr_name, ifname, sizeof (lifr.lifr_name));
2848 if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) {
2849 Perror0_exit("status: SIOCGLIFMETRIC");
2850 } else {
2851 if (lifr.lifr_metric)
2852 (void) printf(" metric %d", lifr.lifr_metric);
2854 if (ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0)
2855 (void) printf(" mtu %u", lifr.lifr_mtu);
2857 /* don't print index or zone when in compatibility mode */
2858 if (!v4compat) {
2859 if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0)
2860 (void) printf(" index %d", lifr.lifr_index);
2862 * Stack instances use GLOBAL_ZONEID for IP data structures
2863 * even in the non-global zone.
2865 if (ioctl(s, SIOCGLIFZONE, (caddr_t)&lifr) >= 0 &&
2866 lifr.lifr_zoneid != getzoneid() &&
2867 lifr.lifr_zoneid != GLOBAL_ZONEID) {
2868 char zone_name[ZONENAME_MAX];
2870 if (getzonenamebyid(lifr.lifr_zoneid, zone_name,
2871 sizeof (zone_name)) < 0) {
2872 (void) printf("\n\tzone %d", lifr.lifr_zoneid);
2873 } else {
2874 (void) printf("\n\tzone %s", zone_name);
2879 if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0) {
2880 lifs.lifs_ifindex = lifr.lifr_index;
2883 * Find the number of interfaces that use this interfaces'
2884 * address as a source address
2886 lifs.lifs_buf = NULL;
2887 lifs.lifs_maxlen = 0;
2888 for (;;) {
2889 /* The first pass will give the bufsize we need */
2890 rval = ioctl(s, SIOCGLIFSRCOF, (char *)&lifs);
2891 if (rval < 0) {
2892 if (lifs.lifs_buf != NULL) {
2893 free(lifs.lifs_buf);
2894 lifs.lifs_buf = NULL;
2896 lifs.lifs_len = 0;
2897 break;
2899 if (lifs.lifs_len <= lifs.lifs_maxlen)
2900 break;
2901 /* Use kernel's size + a small margin to avoid loops */
2902 lifs.lifs_maxlen = lifs.lifs_len +
2903 5 * sizeof (struct lifreq);
2904 /* For the first pass, realloc acts like malloc */
2905 newbuf = realloc(lifs.lifs_buf, lifs.lifs_maxlen);
2906 if (newbuf == NULL) {
2907 if (lifs.lifs_buf != NULL) {
2908 free(lifs.lifs_buf);
2909 lifs.lifs_buf = NULL;
2911 lifs.lifs_len = 0;
2912 break;
2914 lifs.lifs_buf = newbuf;
2918 numifs = lifs.lifs_len / sizeof (struct lifreq);
2919 if (numifs > 0) {
2920 lifrp = lifs.lifs_req;
2921 (void) printf("\n\tsrcof");
2922 for (n = numifs; n > 0; n--, lifrp++) {
2923 (void) printf(" %s", lifrp->lifr_name);
2927 free(lifs.lifs_buf);
2930 /* Find the interface whose source address this interface uses */
2931 if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) {
2932 if (lifr.lifr_index != 0) {
2933 if (if_indextoname(lifr.lifr_index,
2934 if_usesrc_name) == NULL) {
2935 (void) printf("\n\tusesrc ifIndex %d",
2936 lifr.lifr_index);
2937 } else {
2938 (void) printf("\n\tusesrc %s", if_usesrc_name);
2943 (void) putchar('\n');
2947 * Print the status of the interface. If an address family was
2948 * specified, show it and it only; otherwise, show them all.
2950 static void
2951 status(void)
2953 struct afswtch *p = afp;
2954 uint64_t flags;
2955 datalink_id_t linkid;
2957 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
2958 if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
2959 Perror0_exit("status: SIOCGLIFFLAGS");
2962 flags = lifr.lifr_flags;
2965 * Only print the interface status if the address family matches
2966 * the interface family flag.
2968 if (p != NULL) {
2969 if (((p->af_af == AF_INET6) && (flags & IFF_IPV4)) ||
2970 ((p->af_af == AF_INET) && (flags & IFF_IPV6)))
2971 return;
2975 * In V4 compatibility mode, don't print IFF_IPV6 interfaces.
2977 if (v4compat && (flags & IFF_IPV6))
2978 return;
2980 ifstatus(name);
2982 if (ifconfig_dladm_open(name, DATALINK_CLASS_IPTUN, &linkid) ==
2983 DLADM_STATUS_OK)
2984 tun_status(linkid);
2986 if (p != NULL) {
2987 (*p->af_status)(1, flags);
2988 } else {
2989 for (p = afs; p->af_name; p++) {
2990 /* set global af for use in p->af_status */
2991 af = p->af_af;
2992 (*p->af_status)(0, flags);
2996 * Historically, 'ether' has been an address family,
2997 * so print it here.
2999 print_ifether(name);
3004 * Print the status of the interface in a format that can be used to
3005 * reconfigure the interface later. Code stolen from status() above.
3007 /* ARGSUSED */
3008 static int
3009 configinfo(char *null, int64_t param)
3011 char *cp;
3012 struct afswtch *p = afp;
3013 uint64_t flags;
3014 char lifname[LIFNAMSIZ];
3015 char if_usesrc_name[LIFNAMSIZ];
3017 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3019 if (ioctl(s, SIOCGLIFFLAGS, (caddr_t)&lifr) < 0) {
3020 Perror0_exit("status: SIOCGLIFFLAGS");
3022 flags = lifr.lifr_flags;
3024 if (debug) {
3025 (void) printf("configinfo: name %s flags 0x%llx af_af %d\n",
3026 name, flags, p != NULL ? p->af_af : -1);
3030 * Build the interface name to print (we cannot directly use `name'
3031 * because one cannot "plumb" ":0" interfaces).
3033 (void) strlcpy(lifname, name, LIFNAMSIZ);
3034 if ((cp = strchr(lifname, ':')) != NULL && atoi(cp + 1) == 0)
3035 *cp = '\0';
3038 * if the interface is IPv4
3039 * if we have a IPv6 address family restriction return
3040 * so it won't print
3041 * if we are in IPv4 compatibility mode, clear out IFF_IPV4
3042 * so we don't print it.
3044 if (flags & IFF_IPV4) {
3045 if (p && p->af_af == AF_INET6)
3046 return (-1);
3047 if (v4compat)
3048 flags &= ~IFF_IPV4;
3050 (void) printf("%s inet plumb", lifname);
3051 } else if (flags & IFF_IPV6) {
3053 * else if the interface is IPv6
3054 * if we have a IPv4 address family restriction return
3055 * or we are in IPv4 compatibiltiy mode, return.
3057 if (p && p->af_af == AF_INET)
3058 return (-1);
3059 if (v4compat)
3060 return (-1);
3062 (void) printf("%s inet6 plumb", lifname);
3065 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3066 if (ioctl(s, SIOCGLIFMETRIC, (caddr_t)&lifr) < 0) {
3067 Perror0_exit("configinfo: SIOCGLIFMETRIC");
3068 } else {
3069 if (lifr.lifr_metric)
3070 (void) printf(" metric %d ", lifr.lifr_metric);
3072 if (((flags & (IFF_VIRTUAL|IFF_LOOPBACK)) != IFF_VIRTUAL) &&
3073 ioctl(s, SIOCGLIFMTU, (caddr_t)&lifr) >= 0)
3074 (void) printf(" mtu %u", lifr.lifr_mtu);
3076 /* Index only applies to the zeroth interface */
3077 if (lifnum(name) == 0) {
3078 if (ioctl(s, SIOCGLIFINDEX, (caddr_t)&lifr) >= 0)
3079 (void) printf(" index %d", lifr.lifr_index);
3082 if (ioctl(s, SIOCGLIFUSESRC, (caddr_t)&lifr) == 0) {
3083 if (lifr.lifr_index != 0) {
3084 if (if_indextoname(lifr.lifr_index,
3085 if_usesrc_name) != NULL) {
3086 (void) printf(" usesrc %s", if_usesrc_name);
3091 if (p != NULL) {
3092 (*p->af_configinfo)(1, flags);
3093 } else {
3094 for (p = afs; p->af_name; p++) {
3095 (void) close(s);
3096 s = socket(SOCKET_AF(p->af_af), SOCK_DGRAM, 0);
3097 /* set global af for use in p->af_configinfo */
3098 af = p->af_af;
3099 if (s == -1) {
3100 Perror0_exit("socket");
3102 (*p->af_configinfo)(0, flags);
3106 (void) putchar('\n');
3107 return (0);
3110 static void
3111 print_tsec(iptun_params_t *params)
3113 ipsec_req_t *ipsr;
3115 (void) printf("\ttunnel security settings ");
3116 if (!(params->iptun_param_flags & IPTUN_PARAM_SECINFO)) {
3117 (void) printf("--> use 'ipsecconf -ln -i %s'", name);
3118 } else {
3119 ipsr = &params->iptun_param_secinfo;
3120 if (ipsr->ipsr_ah_req & IPSEC_PREF_REQUIRED) {
3121 (void) printf("ah (%s) ",
3122 rparsealg(ipsr->ipsr_auth_alg, IPSEC_PROTO_AH));
3124 if (ipsr->ipsr_esp_req & IPSEC_PREF_REQUIRED) {
3125 (void) printf("esp (%s",
3126 rparsealg(ipsr->ipsr_esp_alg, IPSEC_PROTO_ESP));
3127 (void) printf("/%s)",
3128 rparsealg(ipsr->ipsr_esp_auth_alg, IPSEC_PROTO_AH));
3131 (void) printf("\n");
3134 static void
3135 tun_status(datalink_id_t linkid)
3137 iptun_params_t params;
3138 char propval[DLADM_PROP_VAL_MAX];
3139 char *valptr[1];
3140 uint_t valcnt = 1;
3141 boolean_t tabbed = _B_FALSE;
3143 params.iptun_param_linkid = linkid;
3145 /* If dladm_iptun_getparams() fails, assume we are not a tunnel. */
3146 assert(dlh_opened);
3147 if (dladm_iptun_getparams(dlh, &params, DLADM_OPT_ACTIVE) !=
3148 DLADM_STATUS_OK)
3149 return;
3151 switch (params.iptun_param_type) {
3152 case IPTUN_TYPE_IPV4:
3153 case IPTUN_TYPE_6TO4:
3154 (void) printf("\tinet");
3155 break;
3156 case IPTUN_TYPE_IPV6:
3157 (void) printf("\tinet6");
3158 break;
3159 default:
3160 dladmerr_exit(DLADM_STATUS_IPTUNTYPE, name);
3161 break;
3165 * There is always a source address. If it hasn't been explicitly
3166 * set, the API will pass back a buffer containing the unspecified
3167 * address.
3169 (void) printf(" tunnel src %s ", params.iptun_param_laddr);
3171 if (params.iptun_param_flags & IPTUN_PARAM_RADDR)
3172 (void) printf("tunnel dst %s\n", params.iptun_param_raddr);
3173 else
3174 (void) putchar('\n');
3176 if (params.iptun_param_flags & IPTUN_PARAM_IPSECPOL)
3177 print_tsec(&params);
3179 valptr[0] = propval;
3180 if (dladm_get_linkprop(dlh, linkid, DLADM_PROP_VAL_CURRENT, "hoplimit",
3181 (char **)valptr, &valcnt) == DLADM_STATUS_OK) {
3182 (void) printf("\ttunnel hop limit %s ", propval);
3183 tabbed = _B_TRUE;
3186 if (dladm_get_linkprop(dlh, linkid, DLADM_PROP_VAL_CURRENT,
3187 "encaplimit", (char **)valptr, &valcnt) == DLADM_STATUS_OK) {
3188 uint32_t elim;
3190 if (!tabbed) {
3191 (void) putchar('\t');
3192 tabbed = _B_TRUE;
3194 elim = strtol(propval, NULL, 10);
3195 if (elim > 0)
3196 (void) printf("tunnel encapsulation limit %s", propval);
3197 else
3198 (void) printf("tunnel encapsulation limit disabled");
3201 if (tabbed)
3202 (void) putchar('\n');
3205 static void
3206 in_status(int force, uint64_t flags)
3208 struct sockaddr_in *sin, *laddr;
3209 struct sockaddr_in netmask = { AF_INET };
3211 if (debug)
3212 (void) printf("in_status(%s) flags 0x%llx\n", name, flags);
3214 /* only print status for IPv4 interfaces */
3215 if (!(flags & IFF_IPV4))
3216 return;
3218 if (!(flags & IFF_NOLOCAL)) {
3219 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3220 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
3221 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3222 errno == ENXIO) {
3223 if (!force)
3224 return;
3225 (void) memset(&lifr.lifr_addr, 0,
3226 sizeof (lifr.lifr_addr));
3227 } else
3228 Perror0_exit("in_status: SIOCGLIFADDR");
3230 sin = (struct sockaddr_in *)&lifr.lifr_addr;
3231 (void) printf("\tinet %s ", inet_ntoa(sin->sin_addr));
3232 laddr = sin;
3233 } else {
3234 (void) printf("\tinet ");
3237 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3238 if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
3239 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3240 errno == ENXIO) {
3241 if (!force)
3242 return;
3243 (void) memset(&lifr.lifr_addr, 0,
3244 sizeof (lifr.lifr_addr));
3245 } else {
3246 Perror0_exit("in_status: SIOCGLIFSUBNET");
3249 sin = (struct sockaddr_in *)&lifr.lifr_addr;
3250 if ((flags & IFF_NOLOCAL) ||
3251 sin->sin_addr.s_addr != laddr->sin_addr.s_addr) {
3252 (void) printf("subnet %s/%d ", inet_ntoa(sin->sin_addr),
3253 lifr.lifr_addrlen);
3255 if (sin->sin_family != AF_INET) {
3256 (void) printf("Wrong family: %d\n", sin->sin_family);
3259 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3260 if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) {
3261 if (errno != EADDRNOTAVAIL)
3262 Perror0_exit("in_status: SIOCGLIFNETMASK");
3263 (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
3264 } else
3265 netmask.sin_addr =
3266 ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr;
3267 if (flags & IFF_POINTOPOINT) {
3268 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3269 if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
3270 if (errno == EADDRNOTAVAIL)
3271 (void) memset(&lifr.lifr_addr, 0,
3272 sizeof (lifr.lifr_addr));
3273 else
3274 Perror0_exit("in_status: SIOCGLIFDSTADDR");
3276 sin = (struct sockaddr_in *)&lifr.lifr_dstaddr;
3277 (void) printf("--> %s ", inet_ntoa(sin->sin_addr));
3279 (void) printf("netmask %x ", ntohl(netmask.sin_addr.s_addr));
3280 if (flags & IFF_BROADCAST) {
3281 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3282 if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) {
3283 if (errno == EADDRNOTAVAIL)
3284 (void) memset(&lifr.lifr_addr, 0,
3285 sizeof (lifr.lifr_addr));
3286 else
3287 Perror0_exit("in_status: SIOCGLIFBRDADDR");
3289 sin = (struct sockaddr_in *)&lifr.lifr_addr;
3290 if (sin->sin_addr.s_addr != 0) {
3291 (void) printf("broadcast %s",
3292 inet_ntoa(sin->sin_addr));
3295 /* If there is a groupname, print it for only the physical interface */
3296 if (strchr(name, ':') == NULL) {
3297 if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3298 lifr.lifr_groupname[0] != '\0') {
3299 (void) printf("\n\tgroupname %s", lifr.lifr_groupname);
3302 (void) putchar('\n');
3305 static void
3306 in6_status(int force, uint64_t flags)
3308 char abuf[INET6_ADDRSTRLEN];
3309 struct sockaddr_in6 *sin6, *laddr6;
3311 if (debug)
3312 (void) printf("in6_status(%s) flags 0x%llx\n", name, flags);
3314 if (!(flags & IFF_IPV6))
3315 return;
3317 if (!(flags & IFF_NOLOCAL)) {
3318 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3319 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
3320 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3321 errno == ENXIO) {
3322 if (!force)
3323 return;
3324 (void) memset(&lifr.lifr_addr, 0,
3325 sizeof (lifr.lifr_addr));
3326 } else
3327 Perror0_exit("in_status6: SIOCGLIFADDR");
3329 sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3330 (void) printf("\tinet6 %s/%d ",
3331 inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3332 abuf, sizeof (abuf)),
3333 lifr.lifr_addrlen);
3334 laddr6 = sin6;
3335 } else {
3336 (void) printf("\tinet6 ");
3338 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3339 if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
3340 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3341 errno == ENXIO) {
3342 if (!force)
3343 return;
3344 (void) memset(&lifr.lifr_addr, 0,
3345 sizeof (lifr.lifr_addr));
3346 } else
3347 Perror0_exit("in_status6: SIOCGLIFSUBNET");
3349 sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3350 if ((flags & IFF_NOLOCAL) ||
3351 !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) {
3352 (void) printf("subnet %s/%d ",
3353 inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3354 abuf, sizeof (abuf)),
3355 lifr.lifr_addrlen);
3357 if (sin6->sin6_family != AF_INET6) {
3358 (void) printf("Wrong family: %d\n", sin6->sin6_family);
3360 if (flags & IFF_POINTOPOINT) {
3361 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3362 if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
3363 if (errno == EADDRNOTAVAIL)
3364 (void) memset(&lifr.lifr_addr, 0,
3365 sizeof (lifr.lifr_addr));
3366 else
3367 Perror0_exit("in_status6: SIOCGLIFDSTADDR");
3369 sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr;
3370 (void) printf("--> %s ",
3371 inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3372 abuf, sizeof (abuf)));
3374 if (verbose) {
3375 (void) putchar('\n');
3376 (void) putchar('\t');
3377 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3378 if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) {
3379 if (errno == EADDRNOTAVAIL || errno == EINVAL)
3380 (void) memset(&lifr.lifr_addr, 0,
3381 sizeof (lifr.lifr_addr));
3382 else
3383 Perror0_exit("in_status6: SIOCGLIFTOKEN");
3384 } else {
3385 sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3386 (void) printf("token %s/%d ",
3387 inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3388 abuf, sizeof (abuf)),
3389 lifr.lifr_addrlen);
3391 if (ioctl(s, SIOCGLIFLNKINFO, (caddr_t)&lifr) < 0) {
3392 if (errno != EINVAL) {
3393 Perror0_exit("in_status6: SIOCGLIFLNKINFO");
3395 } else {
3396 (void) printf("maxhops %u, reachtime %u ms, "
3397 "reachretrans %u ms, maxmtu %u ",
3398 lifr.lifr_ifinfo.lir_maxhops,
3399 lifr.lifr_ifinfo.lir_reachtime,
3400 lifr.lifr_ifinfo.lir_reachretrans,
3401 lifr.lifr_ifinfo.lir_maxmtu);
3404 /* If there is a groupname, print it for only the physical interface */
3405 if (strchr(name, ':') == NULL) {
3406 if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3407 lifr.lifr_groupname[0] != '\0') {
3408 (void) printf("\n\tgroupname %s", lifr.lifr_groupname);
3411 (void) putchar('\n');
3414 static void
3415 in_configinfo(int force, uint64_t flags)
3417 struct sockaddr_in *sin, *laddr;
3418 struct sockaddr_in netmask = { AF_INET };
3420 if (debug)
3421 (void) printf("in_configinfo(%s) flags 0x%llx\n", name, flags);
3423 /* only configinfo info for IPv4 interfaces */
3424 if (!(flags & IFF_IPV4))
3425 return;
3427 if (!(flags & IFF_NOLOCAL)) {
3428 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3429 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
3430 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3431 errno == ENXIO) {
3432 if (!force)
3433 return;
3434 (void) memset(&lifr.lifr_addr, 0,
3435 sizeof (lifr.lifr_addr));
3436 } else
3437 Perror0_exit("in_configinfo: SIOCGLIFADDR");
3439 sin = (struct sockaddr_in *)&lifr.lifr_addr;
3440 (void) printf(" set %s ", inet_ntoa(sin->sin_addr));
3441 laddr = sin;
3444 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3445 if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
3446 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3447 errno == ENXIO) {
3448 if (!force)
3449 return;
3450 (void) memset(&lifr.lifr_addr, 0,
3451 sizeof (lifr.lifr_addr));
3452 } else {
3453 Perror0_exit("in_configinfo: SIOCGLIFSUBNET");
3456 sin = (struct sockaddr_in *)&lifr.lifr_addr;
3458 if ((flags & IFF_NOLOCAL) ||
3459 sin->sin_addr.s_addr != laddr->sin_addr.s_addr) {
3460 (void) printf(" subnet %s/%d ", inet_ntoa(sin->sin_addr),
3461 lifr.lifr_addrlen);
3463 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3464 if (ioctl(s, SIOCGLIFNETMASK, (caddr_t)&lifr) < 0) {
3465 if (errno != EADDRNOTAVAIL)
3466 Perror0_exit("in_configinfo: SIOCGLIFNETMASK");
3467 (void) memset(&lifr.lifr_addr, 0, sizeof (lifr.lifr_addr));
3468 } else
3469 netmask.sin_addr =
3470 ((struct sockaddr_in *)&lifr.lifr_addr)->sin_addr;
3471 if (flags & IFF_POINTOPOINT) {
3472 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3473 if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
3474 if (errno == EADDRNOTAVAIL)
3475 (void) memset(&lifr.lifr_addr, 0,
3476 sizeof (lifr.lifr_addr));
3477 else
3478 Perror0_exit("in_configinfo: SIOCGLIFDSTADDR");
3480 sin = (struct sockaddr_in *)&lifr.lifr_dstaddr;
3481 (void) printf(" destination %s ", inet_ntoa(sin->sin_addr));
3483 (void) printf(" netmask 0x%x ", ntohl(netmask.sin_addr.s_addr));
3484 if (flags & IFF_BROADCAST) {
3485 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3486 if (ioctl(s, SIOCGLIFBRDADDR, (caddr_t)&lifr) < 0) {
3487 if (errno == EADDRNOTAVAIL)
3488 (void) memset(&lifr.lifr_addr, 0,
3489 sizeof (lifr.lifr_addr));
3490 else
3491 Perror0_exit("in_configinfo: SIOCGLIFBRDADDR");
3493 sin = (struct sockaddr_in *)&lifr.lifr_addr;
3494 if (sin->sin_addr.s_addr != 0) {
3495 (void) printf(" broadcast %s ",
3496 inet_ntoa(sin->sin_addr));
3500 /* If there is a groupname, print it for only the zeroth interface */
3501 if (lifnum(name) == 0) {
3502 if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3503 lifr.lifr_groupname[0] != '\0') {
3504 (void) printf(" group %s ", lifr.lifr_groupname);
3508 /* Print flags to configure */
3509 print_config_flags(AF_INET, flags);
3512 static void
3513 in6_configinfo(int force, uint64_t flags)
3515 char abuf[INET6_ADDRSTRLEN];
3516 struct sockaddr_in6 *sin6, *laddr6;
3518 if (debug)
3519 (void) printf("in6_configinfo(%s) flags 0x%llx\n", name,
3520 flags);
3522 if (!(flags & IFF_IPV6))
3523 return;
3525 if (!(flags & IFF_NOLOCAL)) {
3526 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3527 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
3528 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3529 errno == ENXIO) {
3530 if (!force)
3531 return;
3532 (void) memset(&lifr.lifr_addr, 0,
3533 sizeof (lifr.lifr_addr));
3534 } else
3535 Perror0_exit("in6_configinfo: SIOCGLIFADDR");
3537 sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3538 (void) printf(" set %s/%d ",
3539 inet_ntop(AF_INET6, &sin6->sin6_addr, abuf, sizeof (abuf)),
3540 lifr.lifr_addrlen);
3541 laddr6 = sin6;
3543 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3544 if (ioctl(s, SIOCGLIFSUBNET, (caddr_t)&lifr) < 0) {
3545 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT ||
3546 errno == ENXIO) {
3547 if (!force)
3548 return;
3549 (void) memset(&lifr.lifr_addr, 0,
3550 sizeof (lifr.lifr_addr));
3551 } else
3552 Perror0_exit("in6_configinfo: SIOCGLIFSUBNET");
3554 sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3555 if ((flags & IFF_NOLOCAL) ||
3556 !IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &laddr6->sin6_addr)) {
3557 (void) printf(" subnet %s/%d ",
3558 inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3559 abuf, sizeof (abuf)),
3560 lifr.lifr_addrlen);
3563 if (flags & IFF_POINTOPOINT) {
3564 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3565 if (ioctl(s, SIOCGLIFDSTADDR, (caddr_t)&lifr) < 0) {
3566 if (errno == EADDRNOTAVAIL)
3567 (void) memset(&lifr.lifr_addr, 0,
3568 sizeof (lifr.lifr_addr));
3569 else
3570 Perror0_exit("in6_configinfo: SIOCGLIFDSTADDR");
3572 sin6 = (struct sockaddr_in6 *)&lifr.lifr_dstaddr;
3573 (void) printf(" destination %s ",
3574 inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3575 abuf, sizeof (abuf)));
3578 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
3579 if (ioctl(s, SIOCGLIFTOKEN, (caddr_t)&lifr) < 0) {
3580 if (errno == EADDRNOTAVAIL || errno == EINVAL)
3581 (void) memset(&lifr.lifr_addr, 0,
3582 sizeof (lifr.lifr_addr));
3583 else
3584 Perror0_exit("in6_configinfo: SIOCGLIFTOKEN");
3585 } else {
3586 sin6 = (struct sockaddr_in6 *)&lifr.lifr_addr;
3587 (void) printf(" token %s/%d ",
3588 inet_ntop(AF_INET6, (void *)&sin6->sin6_addr,
3589 abuf, sizeof (abuf)),
3590 lifr.lifr_addrlen);
3593 /* If there is a groupname, print it for only the zeroth interface */
3594 if (lifnum(name) == 0) {
3595 if (ioctl(s, SIOCGLIFGROUPNAME, &lifr) >= 0 &&
3596 lifr.lifr_groupname[0] != '\0') {
3597 (void) printf(" group %s ", lifr.lifr_groupname);
3601 /* Print flags to configure */
3602 print_config_flags(AF_INET6, flags);
3606 * If this is a physical interface then remove it.
3607 * If it is a logical interface name use SIOCLIFREMOVEIF to
3608 * remove it. In both cases fail if it doesn't exist.
3610 /* ARGSUSED */
3611 static int
3612 inetunplumb(char *arg, int64_t param)
3614 ipadm_status_t istatus;
3616 istatus = ipadm_delete_if(iph, name, afp->af_af, IPADM_OPT_ACTIVE);
3617 if (istatus != IPADM_SUCCESS) {
3618 (void) fprintf(stderr, "ifconfig: cannot unplumb %s: %s\n",
3619 name, ipadm_status2str(istatus));
3620 exit(1);
3623 return (0);
3627 * Create the interface in `name', using ipadm_create_if(). If `name' is a
3628 * logical interface or loopback interface, ipadm_create_if() uses
3629 * SIOCLIFADDIF to create it.
3631 /* ARGSUSED */
3632 static int
3633 inetplumb(char *arg, int64_t param)
3635 ipadm_status_t istatus;
3637 istatus = ipadm_create_if(iph, name, afp->af_af, IPADM_OPT_ACTIVE);
3638 if (istatus != IPADM_SUCCESS) {
3639 (void) fprintf(stderr, "ifconfig: cannot plumb %s: %s\n",
3640 name, ipadm_status2str(istatus));
3641 if (istatus != IPADM_IF_EXISTS)
3642 exit(1);
3644 return (0);
3647 /* ARGSUSED */
3648 static int
3649 inetipmp(char *arg, int64_t param)
3651 int retval;
3654 * Treat e.g. "ifconfig ipmp0:2 ipmp" as "ifconfig ipmp0:2 plumb".
3655 * Otherwise, try to create the requested IPMP interface.
3657 if (strchr(name, ':') != NULL)
3658 retval = inetplumb(arg, param);
3659 else
3660 retval = create_ipmp(name, afp->af_af, name, _B_FALSE);
3663 * We'd return -1, but foreachinterface() doesn't propagate the error
3664 * into the exit status, so we're forced to explicitly exit().
3666 if (retval == -1)
3667 exit(1);
3668 return (0);
3672 * Create an IPMP group `grname' with address family `af'. If `ifname' is
3673 * non-NULL, it specifies the interface name to use. Otherwise, use the name
3674 * ipmpN, where N corresponds to the lowest available integer. If `implicit'
3675 * is set, then the group is being created as a side-effect of placing an
3676 * underlying interface in a group. Also start in.mpathd if necessary.
3678 static int
3679 create_ipmp(const char *grname, int af, const char *ifname, boolean_t implicit)
3681 static int ipmp_daemon_started;
3682 uint32_t flags = IPADM_OPT_IPMP|IPADM_OPT_ACTIVE;
3683 ipadm_status_t istatus;
3685 if (debug) {
3686 (void) printf("create_ipmp: ifname %s grname %s af %d\n",
3687 ifname != NULL ? ifname : "NULL", grname, af);
3691 * ipadm_create_if() creates the IPMP interface and fills in the
3692 * ppa in lifr.lifr_name, if `ifname'="ipmp".
3694 (void) strlcpy(lifr.lifr_name, (ifname ? ifname : "ipmp"),
3695 sizeof (lifr.lifr_name));
3696 if (ifname == NULL)
3697 flags |= IPADM_OPT_GENPPA;
3698 istatus = ipadm_create_if(iph, lifr.lifr_name, af, flags);
3699 if (istatus != IPADM_SUCCESS) {
3700 (void) fprintf(stderr, "ifconfig: cannot create IPMP interface "
3701 "%s: %s\n", grname, ipadm_status2str(istatus));
3702 return (-1);
3706 * To preserve backward-compatibility, always bring up the link-local
3707 * address for implicitly-created IPv6 IPMP interfaces.
3709 if (implicit && af == AF_INET6) {
3710 if (ioctl(s6, SIOCGLIFFLAGS, &lifr) == 0) {
3711 lifr.lifr_flags |= IFF_UP;
3712 (void) ioctl(s6, SIOCSLIFFLAGS, &lifr);
3717 * If the caller requested a different group name, issue a
3718 * SIOCSLIFGROUPNAME on the new IPMP interface.
3720 if (strcmp(lifr.lifr_name, grname) != 0) {
3721 (void) strlcpy(lifr.lifr_groupname, grname, LIFGRNAMSIZ);
3722 if (ioctl(s, SIOCSLIFGROUPNAME, &lifr) == -1) {
3723 Perror0("SIOCSLIFGROUPNAME");
3724 return (-1);
3729 * If we haven't done so yet, ensure in.mpathd is started.
3731 if (ipmp_daemon_started++ == 0)
3732 start_ipmp_daemon();
3734 return (0);
3738 * Start in.mpathd if it's not already running.
3740 static void
3741 start_ipmp_daemon(void)
3743 int retval;
3744 ipmp_handle_t ipmp_handle;
3747 * Ping in.mpathd to see if it's running already.
3749 if ((retval = ipmp_open(&ipmp_handle)) != IPMP_SUCCESS) {
3750 (void) fprintf(stderr, "ifconfig: cannot create IPMP handle: "
3751 "%s\n", ipmp_errmsg(retval));
3752 return;
3755 retval = ipmp_ping_daemon(ipmp_handle);
3756 ipmp_close(ipmp_handle);
3758 switch (retval) {
3759 case IPMP_ENOMPATHD:
3760 break;
3761 case IPMP_SUCCESS:
3762 return;
3763 default:
3764 (void) fprintf(stderr, "ifconfig: cannot ping in.mpathd: %s\n",
3765 ipmp_errmsg(retval));
3766 break;
3770 * Start in.mpathd. Note that in.mpathd will handle multiple
3771 * incarnations (ipmp_ping_daemon() is just an optimization) so we
3772 * don't need to worry about racing with another ifconfig process.
3774 switch (fork()) {
3775 case -1:
3776 Perror0_exit("start_ipmp_daemon: fork");
3777 /* NOTREACHED */
3778 case 0:
3779 (void) execl(MPATHD_PATH, MPATHD_PATH, NULL);
3780 _exit(1);
3781 /* NOTREACHED */
3782 default:
3783 break;
3788 * Bring the address named by `ifaddrp' up or down. Doesn't trust any mutable
3789 * values in ia_flags since they may be stale.
3791 static boolean_t
3792 ifaddr_op(ifaddrlistx_t *ifaddrp, boolean_t up)
3794 struct lifreq lifrl; /* Local lifreq struct */
3795 int fd = (ifaddrp->ia_flags & IFF_IPV4) ? s4 : s6;
3797 (void) memset(&lifrl, 0, sizeof (lifrl));
3798 (void) strlcpy(lifrl.lifr_name, ifaddrp->ia_name, LIFNAMSIZ);
3799 if (ioctl(fd, SIOCGLIFFLAGS, &lifrl) == -1)
3800 return (_B_FALSE);
3802 if (up)
3803 lifrl.lifr_flags |= IFF_UP;
3804 else
3805 lifrl.lifr_flags &= ~IFF_UP;
3807 if (ioctl(fd, SIOCSLIFFLAGS, &lifrl) == -1)
3808 return (_B_FALSE);
3811 * If we're trying to bring the address down, ensure that DAD activity
3812 * (observable by IFF_DUPLICATE) has also been stopped.
3814 if (!up && ioctl(fd, SIOCGLIFFLAGS, &lifrl) != -1 &&
3815 lifrl.lifr_flags & IFF_DUPLICATE) {
3816 if (ioctl(fd, SIOCGLIFADDR, &lifrl) == -1 ||
3817 ioctl(fd, SIOCSLIFADDR, &lifrl) == -1) {
3818 return (_B_FALSE);
3821 return (_B_TRUE);
3824 static boolean_t
3825 ifaddr_up(ifaddrlistx_t *ifaddrp)
3827 return (ifaddr_op(ifaddrp, _B_TRUE));
3830 static boolean_t
3831 ifaddr_down(ifaddrlistx_t *ifaddrp)
3833 return (ifaddr_op(ifaddrp, _B_FALSE));
3837 * Open the global libdladm handle "dlh" if it isn't already opened. The
3838 * caller may optionally supply a link name to obtain its linkid. If a link
3839 * of a specific class or classes is required, reqclass specifies the class
3840 * mask.
3842 static dladm_status_t
3843 ifconfig_dladm_open(const char *name, datalink_class_t reqclass,
3844 datalink_id_t *linkid)
3846 dladm_status_t status = DLADM_STATUS_OK;
3847 datalink_class_t class;
3849 if (!dlh_opened) {
3850 if ((status = dladm_open(&dlh)) != DLADM_STATUS_OK)
3851 return (status);
3852 dlh_opened = _B_TRUE;
3854 if (name != NULL) {
3855 status = dladm_name2info(dlh, name, linkid, NULL, &class, NULL);
3856 if (status == DLADM_STATUS_OK) {
3857 if (!(class & reqclass))
3858 status = DLADM_STATUS_LINKINVAL;
3861 return (status);
3865 * This function checks if we can use libipadm API's. We will only
3866 * call libipadm functions for non-IPMP interfaces. This check is
3867 * temporary until libipadm supports IPMP interfaces.
3869 static boolean_t
3870 ifconfig_use_libipadm(int s, const char *lifname)
3872 struct lifreq lifr1;
3874 (void) strlcpy(lifr1.lifr_name, lifname, sizeof (lifr1.lifr_name));
3875 if (ioctl(s, SIOCGLIFGROUPNAME, (caddr_t)&lifr1) < 0) {
3876 (void) strncpy(lifr.lifr_name, lifname,
3877 sizeof (lifr.lifr_name));
3878 Perror0_exit("error");
3881 return (lifr1.lifr_groupname[0] == '\0');
3884 static void
3885 ipadmerr_exit(ipadm_status_t status, const char *str)
3887 (void) fprintf(stderr, "ifconfig: %s: %s\n", str,
3888 ipadm_status2str(status));
3889 exit(1);
3892 static void
3893 dladmerr_exit(dladm_status_t status, const char *str)
3895 char errstr[DLADM_STRSIZE];
3897 (void) fprintf(stderr, "%s: %s\n", str,
3898 dladm_status2str(status, errstr));
3899 exit(1);
3902 void
3903 Perror0(const char *cmd)
3905 Perror2(cmd, lifr.lifr_name);
3908 void
3909 Perror0_exit(const char *cmd)
3911 Perror0(cmd);
3912 exit(1);
3915 void
3916 Perror2(const char *cmd, const char *str)
3918 int error = errno;
3920 (void) fprintf(stderr, "ifconfig: %s: ", cmd);
3922 switch (error) {
3923 case ENXIO:
3924 (void) fprintf(stderr, "%s: no such interface\n", str);
3925 break;
3926 case EPERM:
3927 (void) fprintf(stderr, "%s: permission denied\n", str);
3928 break;
3929 case EEXIST:
3930 (void) fprintf(stderr, "%s: already exists\n", str);
3931 break;
3932 case ENAMETOOLONG:
3933 (void) fprintf(stderr, "%s: interface name too long\n", str);
3934 break;
3935 case ERANGE:
3936 (void) fprintf(stderr, "%s: logical interface id is outside "
3937 "allowed range\n", str);
3938 break;
3939 default:
3940 errno = error;
3941 perror(str);
3946 * Print out error message (Perror2()) and exit
3948 void
3949 Perror2_exit(const char *cmd, const char *str)
3951 Perror2(cmd, str);
3952 exit(1);
3953 /* NOTREACHED */
3956 void
3957 Perrdlpi(const char *cmd, const char *linkname, int err)
3959 (void) fprintf(stderr, "ifconfig: %s \"%s\": %s\n", cmd,
3960 linkname, dlpi_strerror(err));
3964 * Print out error message (Perrdlpi()) and exit
3966 void
3967 Perrdlpi_exit(const char *cmd, const char *linkname, int err)
3969 Perrdlpi(cmd, linkname, err);
3970 exit(1);
3974 * If the last argument is non-NULL allow a <addr>/<n> syntax and
3975 * pass out <n> in *plenp.
3976 * If <n> doesn't parse return BAD_ADDR as *plenp.
3977 * If no /<n> is present return NO_PREFIX as *plenp.
3979 static void
3980 in_getaddr(char *s, struct sockaddr *saddr, int *plenp)
3982 /* LINTED: alignment */
3983 struct sockaddr_in *sin = (struct sockaddr_in *)saddr;
3984 struct hostent *hp;
3985 struct netent *np;
3986 char str[BUFSIZ];
3987 int error_num;
3989 (void) strncpy(str, s, sizeof (str));
3992 * Look for '/'<n> is plenp
3994 if (plenp != NULL) {
3995 char *cp;
3997 *plenp = in_getprefixlen(str, _B_TRUE, IP_ABITS);
3998 if (*plenp == BAD_ADDR)
3999 return;
4000 cp = strchr(str, '/');
4001 if (cp != NULL)
4002 *cp = '\0';
4003 } else if (strchr(str, '/') != NULL) {
4004 (void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str);
4005 exit(1);
4008 (void) memset(sin, 0, sizeof (*sin));
4011 * Try to catch attempts to set the broadcast address to all 1's.
4013 if (strcmp(str, "255.255.255.255") == 0 ||
4014 (strtoul(str, (char **)NULL, 0) == 0xffffffffUL)) {
4015 sin->sin_family = AF_INET;
4016 sin->sin_addr.s_addr = 0xffffffff;
4017 return;
4020 hp = getipnodebyname(str, AF_INET, 0, &error_num);
4021 if (hp) {
4022 sin->sin_family = hp->h_addrtype;
4023 (void) memcpy(&sin->sin_addr, hp->h_addr, hp->h_length);
4024 freehostent(hp);
4025 return;
4027 np = getnetbyname(str);
4028 if (np) {
4029 sin->sin_family = np->n_addrtype;
4030 sin->sin_addr = inet_makeaddr(np->n_net, INADDR_ANY);
4031 return;
4033 if (error_num == TRY_AGAIN) {
4034 (void) fprintf(stderr, "ifconfig: %s: bad address "
4035 "(try again later)\n", s);
4036 } else {
4037 (void) fprintf(stderr, "ifconfig: %s: bad address\n", s);
4039 exit(1);
4043 * If the last argument is non-NULL allow a <addr>/<n> syntax and
4044 * pass out <n> in *plenp.
4045 * If <n> doesn't parse return BAD_ADDR as *plenp.
4046 * If no /<n> is present return NO_PREFIX as *plenp.
4048 static void
4049 in6_getaddr(char *s, struct sockaddr *saddr, int *plenp)
4051 /* LINTED: alignment */
4052 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)saddr;
4053 struct hostent *hp;
4054 char str[BUFSIZ];
4055 int error_num;
4057 (void) strncpy(str, s, sizeof (str));
4060 * Look for '/'<n> is plenp
4062 if (plenp != NULL) {
4063 char *cp;
4065 *plenp = in_getprefixlen(str, _B_TRUE, IPV6_ABITS);
4066 if (*plenp == BAD_ADDR)
4067 return;
4068 cp = strchr(str, '/');
4069 if (cp != NULL)
4070 *cp = '\0';
4071 } else if (strchr(str, '/') != NULL) {
4072 (void) fprintf(stderr, "ifconfig: %s: unexpected '/'\n", str);
4073 exit(1);
4076 (void) memset(sin6, 0, sizeof (*sin6));
4078 hp = getipnodebyname(str, AF_INET6, 0, &error_num);
4079 if (hp) {
4080 sin6->sin6_family = hp->h_addrtype;
4081 (void) memcpy(&sin6->sin6_addr, hp->h_addr, hp->h_length);
4082 freehostent(hp);
4083 return;
4085 if (error_num == TRY_AGAIN) {
4086 (void) fprintf(stderr, "ifconfig: %s: bad address "
4087 "(try again later)\n", s);
4088 } else {
4089 (void) fprintf(stderr, "ifconfig: %s: bad address\n", s);
4091 exit(1);
4095 * If "slash" is zero this parses the whole string as
4096 * an integer. With "slash" non zero it parses the tail part as an integer.
4098 * If it is not a valid integer this returns BAD_ADDR.
4099 * If there is /<n> present this returns NO_PREFIX.
4101 static int
4102 in_getprefixlen(char *addr, boolean_t slash, int max_plen)
4104 int prefixlen;
4105 char *str, *end;
4107 if (slash) {
4108 str = strchr(addr, '/');
4109 if (str == NULL)
4110 return (NO_PREFIX);
4111 str++;
4112 } else
4113 str = addr;
4115 prefixlen = strtol(str, &end, 10);
4116 if (prefixlen < 0)
4117 return (BAD_ADDR);
4118 if (str == end)
4119 return (BAD_ADDR);
4120 if (max_plen != 0 && max_plen < prefixlen)
4121 return (BAD_ADDR);
4122 return (prefixlen);
4126 * Convert a prefix length to a mask.
4127 * Returns 1 if ok. 0 otherwise.
4128 * Assumes the mask array is zero'ed by the caller.
4130 static boolean_t
4131 in_prefixlentomask(int prefixlen, int maxlen, uchar_t *mask)
4133 if (prefixlen < 0 || prefixlen > maxlen)
4134 return (0);
4136 while (prefixlen > 0) {
4137 if (prefixlen >= 8) {
4138 *mask++ = 0xFF;
4139 prefixlen -= 8;
4140 continue;
4142 *mask |= 1 << (8 - prefixlen);
4143 prefixlen--;
4145 return (1);
4148 static void
4149 print_flags(uint64_t flags)
4151 boolean_t first = _B_TRUE;
4152 int cnt, i;
4154 (void) printf("flags=%llx", flags);
4155 cnt = sizeof (if_flags_tbl) / sizeof (if_flags_t);
4156 for (i = 0; i < cnt; i++) {
4157 if (flags & if_flags_tbl[i].iff_value) {
4158 if (first) {
4159 (void) printf("<");
4160 first = _B_FALSE;
4161 } else {
4163 * It has to be here and not with the
4164 * printf below because for the last one,
4165 * we don't want a comma before the ">".
4167 (void) printf(",");
4169 (void) printf("%s", if_flags_tbl[i].iff_name);
4172 if (!first)
4173 (void) printf(">");
4176 static void
4177 print_config_flags(int af, uint64_t flags)
4179 if_config_cmd_t *cmdp;
4181 for (cmdp = if_config_cmd_tbl; cmdp->iff_flag != 0; cmdp++) {
4182 if ((flags & cmdp->iff_flag) &&
4183 (cmdp->iff_af == AF_UNSPEC || cmdp->iff_af == af)) {
4184 (void) printf("%s ", cmdp->iff_name);
4190 * Use the configured directory lookup mechanism (e.g. files/NIS/...)
4191 * to find the network mask. Returns true if we found one to set.
4193 * The parameter addr_set controls whether we should get the address of
4194 * the working interface for the netmask query. If addr_set is true,
4195 * we will use the address provided. Otherwise, we will find the working
4196 * interface's address and use it instead.
4198 static boolean_t
4199 in_getmask(struct sockaddr_in *saddr, boolean_t addr_set)
4201 struct sockaddr_in ifaddr;
4204 * Read the address from the interface if it is not passed in.
4206 if (!addr_set) {
4207 (void) strncpy(lifr.lifr_name, name, sizeof (lifr.lifr_name));
4208 if (ioctl(s, SIOCGLIFADDR, (caddr_t)&lifr) < 0) {
4209 if (errno != EADDRNOTAVAIL) {
4210 (void) fprintf(stderr, "Need net number for "
4211 "mask\n");
4213 return (_B_FALSE);
4215 ifaddr = *((struct sockaddr_in *)&lifr.lifr_addr);
4216 } else {
4217 ifaddr.sin_addr = saddr->sin_addr;
4219 if (getnetmaskbyaddr(ifaddr.sin_addr, &saddr->sin_addr) == 0) {
4220 saddr->sin_family = AF_INET;
4221 return (_B_TRUE);
4223 return (_B_FALSE);
4226 static int
4227 lifnum(const char *ifname)
4229 const char *cp;
4231 if ((cp = strchr(ifname, ':')) == NULL)
4232 return (0);
4233 else
4234 return (atoi(cp + 1));
4237 static void
4238 add_ni(const char *name)
4240 ni_t **pp;
4241 ni_t *p;
4243 for (pp = &ni_list; (p = *pp) != NULL; pp = &(p->ni_next)) {
4244 if (strcmp(p->ni_name, name) == 0) {
4245 if (debug > 2)
4246 (void) fprintf(stderr, "'%s' is a duplicate\n",
4247 name);
4248 return;
4252 if (debug > 2)
4253 (void) fprintf(stderr, "adding '%s'\n",
4254 name);
4256 if ((p = malloc(sizeof (ni_t))) == NULL)
4257 return;
4259 (void) strlcpy(p->ni_name, name, sizeof (p->ni_name));
4260 p->ni_next = NULL;
4262 *pp = p;
4263 num_ni++;
4266 static boolean_t
4267 ni_entry(const char *linkname, void *arg)
4269 dlpi_handle_t dh;
4270 datalink_class_t class;
4272 (void) dladm_name2info(arg, linkname, NULL, NULL, &class, NULL);
4274 if (class == DATALINK_CLASS_ETHERSTUB)
4275 return (_B_FALSE);
4276 if (dlpi_open(linkname, &dh, 0) != DLPI_SUCCESS)
4277 return (_B_FALSE);
4279 add_ni(linkname);
4281 dlpi_close(dh);
4282 return (_B_FALSE);
4286 * dhcp-related routines
4289 static int
4290 setifdhcp(const char *caller, const char *ifname, int argc, char *argv[])
4292 dhcp_ipc_request_t *request;
4293 dhcp_ipc_reply_t *reply = NULL;
4294 int timeout = DHCP_IPC_WAIT_DEFAULT;
4295 dhcp_ipc_type_t type = DHCP_START;
4296 int error;
4297 boolean_t is_primary = _B_FALSE;
4298 boolean_t started = _B_FALSE;
4300 for (argv++; --argc > 0; argv++) {
4302 if (strcmp(*argv, "primary") == 0) {
4303 is_primary = _B_TRUE;
4304 continue;
4307 if (strcmp(*argv, "wait") == 0) {
4308 if (--argc <= 0) {
4309 usage();
4310 return (DHCP_EXIT_BADARGS);
4312 argv++;
4314 if (strcmp(*argv, "forever") == 0) {
4315 timeout = DHCP_IPC_WAIT_FOREVER;
4316 continue;
4319 if (sscanf(*argv, "%d", &timeout) != 1) {
4320 usage();
4321 return (DHCP_EXIT_BADARGS);
4324 if (timeout < 0) {
4325 usage();
4326 return (DHCP_EXIT_BADARGS);
4328 continue;
4331 type = dhcp_string_to_request(*argv);
4332 if (type == -1) {
4333 usage();
4334 return (DHCP_EXIT_BADARGS);
4339 * Only try to start agent on start or inform; in all other cases it
4340 * has to already be running for anything to make sense.
4342 if (type == DHCP_START || type == DHCP_INFORM) {
4343 if (dhcp_start_agent(DHCP_IPC_MAX_WAIT) == -1) {
4344 (void) fprintf(stderr, "%s: unable to start %s\n",
4345 caller, DHCP_AGENT_PATH);
4346 return (DHCP_EXIT_FAILURE);
4348 started = _B_TRUE;
4351 if (is_primary)
4352 type |= DHCP_PRIMARY;
4354 if (af != AF_INET)
4355 type |= DHCP_V6;
4357 request = dhcp_ipc_alloc_request(type, ifname, NULL, 0, DHCP_TYPE_NONE);
4358 if (request == NULL) {
4359 (void) fprintf(stderr, "%s: out of memory\n", caller);
4360 return (DHCP_EXIT_SYSTEM);
4363 error = dhcp_ipc_make_request(request, &reply, timeout);
4364 if (error != 0) {
4365 free(request);
4367 * Re-map connect error to not under control if we didn't try a
4368 * start operation, as this has to be true and results in a
4369 * clearer message, not to mention preserving compatibility
4370 * with the days when we always started dhcpagent for every
4371 * request.
4373 if (error == DHCP_IPC_E_CONNECT && !started)
4374 error = DHCP_IPC_E_UNKIF;
4375 (void) fprintf(stderr, "%s: %s: %s\n", caller, ifname,
4376 dhcp_ipc_strerror(error));
4377 return (DHCP_EXIT_FAILURE);
4380 error = reply->return_code;
4381 if (error != 0) {
4382 free(request);
4383 free(reply);
4385 if (error == DHCP_IPC_E_TIMEOUT && timeout == 0)
4386 return (DHCP_EXIT_SUCCESS);
4388 (void) fprintf(stderr, "%s: %s: %s\n", caller, ifname,
4389 dhcp_ipc_strerror(error));
4391 if (error == DHCP_IPC_E_TIMEOUT)
4392 return (DHCP_EXIT_TIMEOUT);
4393 else
4394 return (DHCP_EXIT_IF_FAILURE);
4397 if (DHCP_IPC_CMD(type) == DHCP_STATUS) {
4398 (void) printf("%s", dhcp_status_hdr_string());
4399 (void) printf("%s", dhcp_status_reply_to_string(reply));
4402 free(request);
4403 free(reply);
4404 return (DHCP_EXIT_SUCCESS);
4407 static void
4408 usage(void)
4410 (void) fprintf(stderr,
4411 "usage: ifconfig <interface> | -a[ 4 | 6 | D ][ u | d ][ Z ]\n");
4413 (void) fprintf(stderr, "%s",
4414 "\t[ <addr_family> ]\n"
4415 "\t[ <address>[/<prefix_length>] [ <dest_address> ] ]\n"
4416 "\t[ set [ <address>][/<prefix_length>] ]"
4417 " [ <address>/<prefix_length>] ]\n"
4418 "\t[ destination <dest_address> ]\n"
4419 "\t[ addif <address>[/<prefix_length>]"
4420 " [ <dest_address> ] ]\n"
4421 "\t[ removeif <address>[/<prefix_length>] ]\n"
4422 "\t[ arp | -arp ]\n"
4423 "\t[ auto-revarp ]\n"
4424 "\t[ broadcast <broad_addr> ]\n"
4425 "\t[ index <if_index> ]\n"
4426 "\t[ metric <n> ] [ mtu <n> ]\n"
4427 "\t[ netmask <mask> ]\n"
4428 "\t[ plumb ] [ unplumb ]\n"
4429 "\t[ preferred | -preferred ]\n"
4430 "\t[ private | -private ]\n"
4431 "\t[ local | -local ]\n"
4432 "\t[ router | -router ]\n"
4433 "\t[ subnet <subnet_address>]\n"
4434 "\t[ trailers | -trailers ]\n"
4435 "\t[ token <address>/<prefix_length> ]\n"
4436 "\t[ tsrc <tunnel_src_address> ]\n"
4437 "\t[ tdst <tunnel_dest_address> ]\n"
4438 "\t[ auth_algs <tunnel_AH_authentication_algorithm> ]\n"
4439 "\t[ encr_algs <tunnel_ESP_encryption_algorithm> ]\n"
4440 "\t[ encr_auth_algs <tunnel_ESP_authentication_algorithm> ]\n"
4441 "\t[ up ] [ down ]\n"
4442 "\t[ xmit | -xmit ]\n"
4443 "\t[ modlist ]\n"
4444 "\t[ modinsert <module_name@position> ]\n"
4445 "\t[ modremove <module_name@position> ]\n"
4446 "\t[ ipmp ]\n"
4447 "\t[ group <groupname>] | [ group \"\"]\n"
4448 "\t[ deprecated | -deprecated ]\n"
4449 "\t[ standby | -standby ]\n"
4450 "\t[ failover | -failover ]\n"
4451 "\t[ zone <zonename> | -zone ]\n"
4452 "\t[ usesrc <interface> ]\n");
4454 (void) fprintf(stderr, "or\n");
4455 (void) fprintf(stderr,
4456 "\tifconfig <interface> | -a[ 4 | 6 | D ] [ u | d ]\n");
4458 (void) fprintf(stderr, "%s", "\tauto-dhcp | dhcp\n"
4459 "\t[ wait <time> | forever ]\n\t[ primary ]\n"
4460 "\tstart | drop | ping | release | status | inform\n");