1550b6e40SSowmini Varadhan /*
2550b6e40SSowmini Varadhan  * CDDL HEADER START
3550b6e40SSowmini Varadhan  *
4550b6e40SSowmini Varadhan  * The contents of this file are subject to the terms of the
5550b6e40SSowmini Varadhan  * Common Development and Distribution License (the "License").
6550b6e40SSowmini Varadhan  * You may not use this file except in compliance with the License.
7550b6e40SSowmini Varadhan  *
8550b6e40SSowmini Varadhan  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9550b6e40SSowmini Varadhan  * or http://www.opensolaris.org/os/licensing.
10550b6e40SSowmini Varadhan  * See the License for the specific language governing permissions
11550b6e40SSowmini Varadhan  * and limitations under the License.
12550b6e40SSowmini Varadhan  *
13550b6e40SSowmini Varadhan  * When distributing Covered Code, include this CDDL HEADER in each
14550b6e40SSowmini Varadhan  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15550b6e40SSowmini Varadhan  * If applicable, add the following below this CDDL HEADER, with the
16550b6e40SSowmini Varadhan  * fields enclosed by brackets "[]" replaced with your own identifying
17550b6e40SSowmini Varadhan  * information: Portions Copyright [yyyy] [name of copyright owner]
18550b6e40SSowmini Varadhan  *
19550b6e40SSowmini Varadhan  * CDDL HEADER END
20550b6e40SSowmini Varadhan  */
21550b6e40SSowmini Varadhan /*
22550b6e40SSowmini Varadhan  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23*ffe245b4SAndy Fiddaman  * Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
24550b6e40SSowmini Varadhan  */
25550b6e40SSowmini Varadhan 
26550b6e40SSowmini Varadhan #include <errno.h>
27550b6e40SSowmini Varadhan #include <fcntl.h>
28550b6e40SSowmini Varadhan #include <priv_utils.h>
29550b6e40SSowmini Varadhan #include <signal.h>
30550b6e40SSowmini Varadhan #include <stdlib.h>
31550b6e40SSowmini Varadhan #include <stdio.h>
32550b6e40SSowmini Varadhan #include <strings.h>
33550b6e40SSowmini Varadhan #include <sys/param.h>
34550b6e40SSowmini Varadhan #include <sys/stat.h>
35550b6e40SSowmini Varadhan #include <unistd.h>
36550b6e40SSowmini Varadhan #include <zone.h>
37550b6e40SSowmini Varadhan #include <libipadm.h>
38550b6e40SSowmini Varadhan #include <libdladm.h>
39550b6e40SSowmini Varadhan #include <libdllink.h>
40550b6e40SSowmini Varadhan #include <net/route.h>
41550b6e40SSowmini Varadhan #include <netinet/in.h>
42550b6e40SSowmini Varadhan #include <net/route.h>
43550b6e40SSowmini Varadhan #include <errno.h>
44550b6e40SSowmini Varadhan #include <inet/ip.h>
45550b6e40SSowmini Varadhan #include <string.h>
46550b6e40SSowmini Varadhan #include <libinetutil.h>
47550b6e40SSowmini Varadhan #include <unistd.h>
48550b6e40SSowmini Varadhan #include <libipadm_impl.h>
49550b6e40SSowmini Varadhan #include <sys/brand.h>
50550b6e40SSowmini Varadhan 
51550b6e40SSowmini Varadhan #define	ROUNDUP_LONG(a) \
52550b6e40SSowmini Varadhan 	((a) > 0 ? (1 + (((a) - 1) | (sizeof (long) - 1))) : sizeof (long))
53550b6e40SSowmini Varadhan #define	HOST_MASK	0xffffffffU
54550b6e40SSowmini Varadhan 
55550b6e40SSowmini Varadhan typedef struct ngz_walk_data_s {
56550b6e40SSowmini Varadhan 	ipadm_handle_t	ngz_iph;
57550b6e40SSowmini Varadhan 	zoneid_t	ngz_zoneid;
58550b6e40SSowmini Varadhan 	char		*ngz_ifname;
59550b6e40SSowmini Varadhan 	boolean_t	ngz_s10c;
60550b6e40SSowmini Varadhan 	ipadm_status_t  ngz_ipstatus;
61550b6e40SSowmini Varadhan 	persist_cb_t	ngz_persist_if;
62550b6e40SSowmini Varadhan } ngz_walk_data_t;
63550b6e40SSowmini Varadhan 
64550b6e40SSowmini Varadhan /*
65550b6e40SSowmini Varadhan  * Tell the kernel to add, delete or change a route
66550b6e40SSowmini Varadhan  */
67550b6e40SSowmini Varadhan static void
i_ipadm_rtioctl4(int rtsock,int action,in_addr_t dst,in_addr_t gate,uint_t masklen,char * ifname,uint8_t metric,int flags)68550b6e40SSowmini Varadhan i_ipadm_rtioctl4(int rtsock,
69550b6e40SSowmini Varadhan     int action,			/* RTM_DELETE, etc */
70550b6e40SSowmini Varadhan     in_addr_t dst,
71550b6e40SSowmini Varadhan     in_addr_t gate,
72550b6e40SSowmini Varadhan     uint_t masklen,
73550b6e40SSowmini Varadhan     char *ifname,
74550b6e40SSowmini Varadhan     uint8_t metric,
75550b6e40SSowmini Varadhan     int flags)
76550b6e40SSowmini Varadhan {
77550b6e40SSowmini Varadhan 	static int rt_sock_seqno = 0;
78550b6e40SSowmini Varadhan 	struct {
79550b6e40SSowmini Varadhan 		struct rt_msghdr w_rtm;
80550b6e40SSowmini Varadhan 		struct sockaddr_in w_dst;
81550b6e40SSowmini Varadhan 		struct sockaddr_in w_gate;
82550b6e40SSowmini Varadhan 		uint8_t w_space[512];
83550b6e40SSowmini Varadhan 	} w;
84550b6e40SSowmini Varadhan 	struct sockaddr_in w_mask;
85550b6e40SSowmini Varadhan 	struct sockaddr_dl w_ifp;
86550b6e40SSowmini Varadhan 	uint8_t *cp;
87550b6e40SSowmini Varadhan 	long cc;
88550b6e40SSowmini Varadhan 
89550b6e40SSowmini Varadhan again:
90550b6e40SSowmini Varadhan 	(void) memset(&w, 0, sizeof (w));
91550b6e40SSowmini Varadhan 	(void) memset(&w_mask, 0, sizeof (w_mask));
92550b6e40SSowmini Varadhan 	(void) memset(&w_ifp, 0, sizeof (w_ifp));
93550b6e40SSowmini Varadhan 	cp = w.w_space;
94550b6e40SSowmini Varadhan 	w.w_rtm.rtm_msglen = sizeof (struct rt_msghdr) +
95550b6e40SSowmini Varadhan 	    2 * ROUNDUP_LONG(sizeof (struct sockaddr_in));
96550b6e40SSowmini Varadhan 	w.w_rtm.rtm_version = RTM_VERSION;
97550b6e40SSowmini Varadhan 	w.w_rtm.rtm_type = action;
98550b6e40SSowmini Varadhan 	w.w_rtm.rtm_flags = (flags | RTF_ZONE);
99550b6e40SSowmini Varadhan 	w.w_rtm.rtm_seq = ++rt_sock_seqno;
100550b6e40SSowmini Varadhan 	w.w_rtm.rtm_addrs = RTA_DST|RTA_GATEWAY;
101550b6e40SSowmini Varadhan 	if (metric != 0 || action == RTM_CHANGE) {
102550b6e40SSowmini Varadhan 		w.w_rtm.rtm_rmx.rmx_hopcount = metric;
103550b6e40SSowmini Varadhan 		w.w_rtm.rtm_inits |= RTV_HOPCOUNT;
104550b6e40SSowmini Varadhan 	}
105550b6e40SSowmini Varadhan 	w.w_dst.sin_family = AF_INET;
106550b6e40SSowmini Varadhan 	w.w_dst.sin_addr.s_addr = dst;
107550b6e40SSowmini Varadhan 	w.w_gate.sin_family = AF_INET;
108550b6e40SSowmini Varadhan 	w.w_gate.sin_addr.s_addr = gate;
109550b6e40SSowmini Varadhan 	if (masklen == HOST_MASK) {
110550b6e40SSowmini Varadhan 		w.w_rtm.rtm_flags |= RTF_HOST;
111550b6e40SSowmini Varadhan 	} else {
112550b6e40SSowmini Varadhan 		struct sockaddr_storage m4;
113550b6e40SSowmini Varadhan 
114550b6e40SSowmini Varadhan 		w.w_rtm.rtm_addrs |= RTA_NETMASK;
115550b6e40SSowmini Varadhan 		w_mask.sin_family = AF_INET;
11664639aafSDarren Reed 		if (plen2mask(masklen, AF_INET, (struct sockaddr *)&m4) != 0) {
117550b6e40SSowmini Varadhan 			return;
118550b6e40SSowmini Varadhan 		}
119550b6e40SSowmini Varadhan 		w_mask.sin_addr = ((struct sockaddr_in *)&m4)->sin_addr;
120550b6e40SSowmini Varadhan 		(void) memmove(cp, &w_mask, sizeof (w_mask));
121550b6e40SSowmini Varadhan 		cp += ROUNDUP_LONG(sizeof (struct sockaddr_in));
122550b6e40SSowmini Varadhan 		w.w_rtm.rtm_msglen += ROUNDUP_LONG(sizeof (struct sockaddr_in));
123550b6e40SSowmini Varadhan 	}
124550b6e40SSowmini Varadhan 	w_ifp.sdl_family = AF_LINK;
125550b6e40SSowmini Varadhan 	w.w_rtm.rtm_addrs |= RTA_IFP;
126550b6e40SSowmini Varadhan 	w_ifp.sdl_index = if_nametoindex(ifname);
127550b6e40SSowmini Varadhan 	(void) memmove(cp, &w_ifp, sizeof (w_ifp));
128550b6e40SSowmini Varadhan 	w.w_rtm.rtm_msglen += ROUNDUP_LONG(sizeof (struct sockaddr_dl));
129550b6e40SSowmini Varadhan 
130550b6e40SSowmini Varadhan 	cc = write(rtsock, &w, w.w_rtm.rtm_msglen);
131550b6e40SSowmini Varadhan 	if (cc < 0) {
132550b6e40SSowmini Varadhan 		if (errno == ESRCH && (action == RTM_CHANGE ||
133550b6e40SSowmini Varadhan 		    action == RTM_DELETE)) {
134550b6e40SSowmini Varadhan 			if (action == RTM_CHANGE) {
135550b6e40SSowmini Varadhan 				action = RTM_ADD;
136550b6e40SSowmini Varadhan 				goto again;
137550b6e40SSowmini Varadhan 			}
138550b6e40SSowmini Varadhan 			return;
139550b6e40SSowmini Varadhan 		}
140550b6e40SSowmini Varadhan 		return;
141550b6e40SSowmini Varadhan 	} else if (cc != w.w_rtm.rtm_msglen) {
142550b6e40SSowmini Varadhan 		return;
143550b6e40SSowmini Varadhan 	}
144550b6e40SSowmini Varadhan }
145550b6e40SSowmini Varadhan 
146550b6e40SSowmini Varadhan static void
i_ipadm_rtioctl6(int rtsock,int action,in6_addr_t dst,in6_addr_t gate,uint_t prefix_length,char * ifname,int flags)147550b6e40SSowmini Varadhan i_ipadm_rtioctl6(int rtsock,
148550b6e40SSowmini Varadhan     int action,			/* RTM_DELETE, etc */
149550b6e40SSowmini Varadhan     in6_addr_t dst,
150550b6e40SSowmini Varadhan     in6_addr_t gate,
151550b6e40SSowmini Varadhan     uint_t prefix_length,
152550b6e40SSowmini Varadhan     char *ifname,
153550b6e40SSowmini Varadhan     int flags)
154550b6e40SSowmini Varadhan {
155550b6e40SSowmini Varadhan 	static int rt_sock_seqno = 0;
156550b6e40SSowmini Varadhan 	struct {
157550b6e40SSowmini Varadhan 		struct rt_msghdr w_rtm;
158550b6e40SSowmini Varadhan 		struct sockaddr_in6 w_dst;
159550b6e40SSowmini Varadhan 		struct sockaddr_in6 w_gate;
160550b6e40SSowmini Varadhan 		uint8_t w_space[512];
161550b6e40SSowmini Varadhan 	} w;
162550b6e40SSowmini Varadhan 	struct sockaddr_in6 w_mask;
163550b6e40SSowmini Varadhan 	struct sockaddr_dl w_ifp;
164550b6e40SSowmini Varadhan 	uint8_t *cp;
165550b6e40SSowmini Varadhan 	long cc;
166550b6e40SSowmini Varadhan 
167550b6e40SSowmini Varadhan again:
168550b6e40SSowmini Varadhan 	(void) memset(&w, 0, sizeof (w));
169550b6e40SSowmini Varadhan 	(void) memset(&w_mask, 0, sizeof (w_mask));
170550b6e40SSowmini Varadhan 	(void) memset(&w_ifp, 0, sizeof (w_ifp));
171550b6e40SSowmini Varadhan 	cp = w.w_space;
172550b6e40SSowmini Varadhan 	w.w_rtm.rtm_msglen = sizeof (struct rt_msghdr) +
173550b6e40SSowmini Varadhan 	    2 * ROUNDUP_LONG(sizeof (struct sockaddr_in6));
174550b6e40SSowmini Varadhan 	w.w_rtm.rtm_version = RTM_VERSION;
175550b6e40SSowmini Varadhan 	w.w_rtm.rtm_type = action;
176550b6e40SSowmini Varadhan 	w.w_rtm.rtm_flags = (flags | RTF_ZONE);
177550b6e40SSowmini Varadhan 	w.w_rtm.rtm_seq = ++rt_sock_seqno;
178550b6e40SSowmini Varadhan 	w.w_rtm.rtm_addrs = RTA_DST|RTA_GATEWAY;
179550b6e40SSowmini Varadhan 	w.w_dst.sin6_family = AF_INET6;
180550b6e40SSowmini Varadhan 	w.w_dst.sin6_addr = dst;
181550b6e40SSowmini Varadhan 	w.w_gate.sin6_family = AF_INET6;
182550b6e40SSowmini Varadhan 	w.w_gate.sin6_addr = gate;
183550b6e40SSowmini Varadhan 	if (prefix_length == IPV6_ABITS) {
184550b6e40SSowmini Varadhan 		w.w_rtm.rtm_flags |= RTF_HOST;
185550b6e40SSowmini Varadhan 	} else {
186550b6e40SSowmini Varadhan 		struct sockaddr_storage m6;
187550b6e40SSowmini Varadhan 
188550b6e40SSowmini Varadhan 		w.w_rtm.rtm_addrs |= RTA_NETMASK;
189550b6e40SSowmini Varadhan 		w_mask.sin6_family = AF_INET6;
19064639aafSDarren Reed 		if (plen2mask(prefix_length, AF_INET6,
19164639aafSDarren Reed 		    (struct sockaddr *)&m6) != 0) {
192550b6e40SSowmini Varadhan 			return;
193550b6e40SSowmini Varadhan 		}
194550b6e40SSowmini Varadhan 		w_mask.sin6_addr = ((struct sockaddr_in6 *)&m6)->sin6_addr;
195550b6e40SSowmini Varadhan 		(void) memmove(cp, &w_mask, sizeof (w_mask));
196550b6e40SSowmini Varadhan 		cp += ROUNDUP_LONG(sizeof (struct sockaddr_in6));
197550b6e40SSowmini Varadhan 		w.w_rtm.rtm_msglen +=
198550b6e40SSowmini Varadhan 		    ROUNDUP_LONG(sizeof (struct sockaddr_in6));
199550b6e40SSowmini Varadhan 	}
200550b6e40SSowmini Varadhan 	w_ifp.sdl_family = AF_LINK;
201550b6e40SSowmini Varadhan 	w.w_rtm.rtm_addrs |= RTA_IFP;
202550b6e40SSowmini Varadhan 	w_ifp.sdl_index = if_nametoindex(ifname);
203550b6e40SSowmini Varadhan 	(void) memmove(cp, &w_ifp, sizeof (w_ifp));
204550b6e40SSowmini Varadhan 	w.w_rtm.rtm_msglen += ROUNDUP_LONG(sizeof (struct sockaddr_dl));
205550b6e40SSowmini Varadhan 
206550b6e40SSowmini Varadhan 	cc = write(rtsock, &w, w.w_rtm.rtm_msglen);
207550b6e40SSowmini Varadhan 	if (cc < 0) {
208550b6e40SSowmini Varadhan 		if (errno == ESRCH && (action == RTM_CHANGE ||
209550b6e40SSowmini Varadhan 		    action == RTM_DELETE)) {
210550b6e40SSowmini Varadhan 			if (action == RTM_CHANGE) {
211550b6e40SSowmini Varadhan 				action = RTM_ADD;
212550b6e40SSowmini Varadhan 				goto again;
213550b6e40SSowmini Varadhan 			}
214550b6e40SSowmini Varadhan 			return;
215550b6e40SSowmini Varadhan 		}
216550b6e40SSowmini Varadhan 		return;
217550b6e40SSowmini Varadhan 	} else if (cc != w.w_rtm.rtm_msglen) {
218550b6e40SSowmini Varadhan 		return;
219550b6e40SSowmini Varadhan 	}
220550b6e40SSowmini Varadhan }
221550b6e40SSowmini Varadhan 
222550b6e40SSowmini Varadhan /*
223550b6e40SSowmini Varadhan  * Return TRUE if running in a Solaris 10 Container.
224550b6e40SSowmini Varadhan  */
225550b6e40SSowmini Varadhan static boolean_t
i_ipadm_zone_is_s10c(zoneid_t zoneid)226550b6e40SSowmini Varadhan i_ipadm_zone_is_s10c(zoneid_t zoneid)
227550b6e40SSowmini Varadhan {
228550b6e40SSowmini Varadhan 	char brand[MAXNAMELEN];
229550b6e40SSowmini Varadhan 
230550b6e40SSowmini Varadhan 	if (zone_getattr(zoneid, ZONE_ATTR_BRAND, brand, sizeof (brand)) < 0)
231550b6e40SSowmini Varadhan 		return (B_FALSE);
232550b6e40SSowmini Varadhan 	return (strcmp(brand, NATIVE_BRAND_NAME) != 0);
233550b6e40SSowmini Varadhan }
234550b6e40SSowmini Varadhan 
235550b6e40SSowmini Varadhan /*
236550b6e40SSowmini Varadhan  * Configure addresses on link. `buf' is a string of comma-separated
237550b6e40SSowmini Varadhan  * IP addresses.
238550b6e40SSowmini Varadhan  */
239550b6e40SSowmini Varadhan static ipadm_status_t
i_ipadm_ngz_addr(ipadm_handle_t iph,char * link,char * buf)240550b6e40SSowmini Varadhan i_ipadm_ngz_addr(ipadm_handle_t iph, char *link, char *buf)
241550b6e40SSowmini Varadhan {
242550b6e40SSowmini Varadhan 	ipadm_status_t ipstatus;
243550b6e40SSowmini Varadhan 	ipadm_addrobj_t ipaddr;
244550b6e40SSowmini Varadhan 	char *cp;
245550b6e40SSowmini Varadhan 
246550b6e40SSowmini Varadhan 	for (cp = strtok(buf, ","); cp != NULL; cp = strtok(NULL, ",")) {
247550b6e40SSowmini Varadhan 		ipstatus = ipadm_create_addrobj(IPADM_ADDR_STATIC, link,
248550b6e40SSowmini Varadhan 		    &ipaddr);
249550b6e40SSowmini Varadhan 		if (ipstatus != IPADM_SUCCESS)
250550b6e40SSowmini Varadhan 			return (ipstatus);
251550b6e40SSowmini Varadhan 		/*
252550b6e40SSowmini Varadhan 		 * ipadm_set_addr does the appropriate name resolution and
253550b6e40SSowmini Varadhan 		 * sets up the ipadm_static_addr field.
254550b6e40SSowmini Varadhan 		 */
255550b6e40SSowmini Varadhan 		ipstatus = ipadm_set_addr(ipaddr, cp, AF_UNSPEC);
256550b6e40SSowmini Varadhan 		if (ipstatus != IPADM_SUCCESS) {
257550b6e40SSowmini Varadhan 			ipadm_destroy_addrobj(ipaddr);
258550b6e40SSowmini Varadhan 			return (ipstatus);
259550b6e40SSowmini Varadhan 		}
260550b6e40SSowmini Varadhan 
261550b6e40SSowmini Varadhan 		ipstatus = ipadm_create_addr(iph, ipaddr,
262550b6e40SSowmini Varadhan 		    (IPADM_OPT_ACTIVE | IPADM_OPT_UP));
263550b6e40SSowmini Varadhan 		if (ipstatus != IPADM_SUCCESS) {
264550b6e40SSowmini Varadhan 			ipadm_destroy_addrobj(ipaddr);
265550b6e40SSowmini Varadhan 			return (ipstatus);
266550b6e40SSowmini Varadhan 		}
267550b6e40SSowmini Varadhan 		ipadm_destroy_addrobj(ipaddr);
268550b6e40SSowmini Varadhan 	}
269550b6e40SSowmini Varadhan 	return (IPADM_SUCCESS);
270550b6e40SSowmini Varadhan }
271550b6e40SSowmini Varadhan 
272550b6e40SSowmini Varadhan /*
273550b6e40SSowmini Varadhan  * The (*persist_if)() will set up persistent information for the interface,
274550b6e40SSowmini Varadhan  * based on what interface families are required, so just resolve the
275550b6e40SSowmini Varadhan  * address and inform the callback about the linkname, and required address
276550b6e40SSowmini Varadhan  * families.
277550b6e40SSowmini Varadhan  */
278550b6e40SSowmini Varadhan static ipadm_status_t
i_ipadm_ngz_persist_if(char * link,char * buf,void (* ngz_persist_if)(char *,boolean_t,boolean_t))279550b6e40SSowmini Varadhan i_ipadm_ngz_persist_if(char *link, char *buf,
280550b6e40SSowmini Varadhan     void (*ngz_persist_if)(char *, boolean_t, boolean_t))
281550b6e40SSowmini Varadhan {
282550b6e40SSowmini Varadhan 	char *cp, *slashp, addr[INET6_ADDRSTRLEN];
283550b6e40SSowmini Varadhan 	ipadm_status_t ipstatus;
284550b6e40SSowmini Varadhan 	struct sockaddr_storage ss;
285550b6e40SSowmini Varadhan 	boolean_t v4 = B_FALSE;
286550b6e40SSowmini Varadhan 	boolean_t v6 = B_FALSE;
287550b6e40SSowmini Varadhan 
288550b6e40SSowmini Varadhan 	for (cp = strtok(buf, ","); cp != NULL; cp = strtok(NULL, ",")) {
289550b6e40SSowmini Varadhan 		/* remove the /<masklen> that's always added by zoneadmd */
290550b6e40SSowmini Varadhan 		slashp = strchr(cp, '/');
291550b6e40SSowmini Varadhan 		(void) strlcpy(addr, cp, (slashp - cp + 1));
292550b6e40SSowmini Varadhan 
293550b6e40SSowmini Varadhan 		/* resolve the address to find the family */
294550b6e40SSowmini Varadhan 		bzero(&ss, sizeof (ss));
295550b6e40SSowmini Varadhan 		ipstatus = i_ipadm_resolve_addr(addr, AF_UNSPEC, &ss);
296550b6e40SSowmini Varadhan 		if (ipstatus != IPADM_SUCCESS)
297550b6e40SSowmini Varadhan 			return (ipstatus);
298550b6e40SSowmini Varadhan 		switch (ss.ss_family) {
299550b6e40SSowmini Varadhan 		case AF_INET:
300550b6e40SSowmini Varadhan 			v4 = B_TRUE;
301550b6e40SSowmini Varadhan 			break;
302550b6e40SSowmini Varadhan 		case AF_INET6:
303550b6e40SSowmini Varadhan 			v6 = B_TRUE;
304550b6e40SSowmini Varadhan 			break;
305550b6e40SSowmini Varadhan 		default:
306550b6e40SSowmini Varadhan 			return (IPADM_BAD_ADDR);
307550b6e40SSowmini Varadhan 		}
308550b6e40SSowmini Varadhan 	}
309550b6e40SSowmini Varadhan 	(*ngz_persist_if)(link, v4, v6);
310550b6e40SSowmini Varadhan 	return (IPADM_SUCCESS);
311550b6e40SSowmini Varadhan }
312550b6e40SSowmini Varadhan 
313550b6e40SSowmini Varadhan static void
i_ipadm_create_ngz_route(int rtsock,char * link,uint8_t * buf,size_t buflen)314550b6e40SSowmini Varadhan i_ipadm_create_ngz_route(int rtsock, char *link, uint8_t *buf, size_t buflen)
315550b6e40SSowmini Varadhan {
316550b6e40SSowmini Varadhan 	struct in6_addr defrouter;
317550b6e40SSowmini Varadhan 	boolean_t isv6;
318550b6e40SSowmini Varadhan 	struct in_addr gw4;
319550b6e40SSowmini Varadhan 	uint8_t *cp;
320550b6e40SSowmini Varadhan 	const in6_addr_t ipv6_all_zeros = { 0, 0, 0, 0 };
321550b6e40SSowmini Varadhan 
322550b6e40SSowmini Varadhan 	if (rtsock == -1)
323550b6e40SSowmini Varadhan 		return;
324550b6e40SSowmini Varadhan 
325550b6e40SSowmini Varadhan 	for (cp = buf; cp < buf + buflen; cp += sizeof (defrouter)) {
326550b6e40SSowmini Varadhan 		bcopy(cp, &defrouter, sizeof (defrouter));
327550b6e40SSowmini Varadhan 		if (IN6_IS_ADDR_UNSPECIFIED(&defrouter))
328550b6e40SSowmini Varadhan 			break;
329550b6e40SSowmini Varadhan 		isv6 = !IN6_IS_ADDR_V4MAPPED(&defrouter);
330550b6e40SSowmini Varadhan 		if (isv6) {
331550b6e40SSowmini Varadhan 			i_ipadm_rtioctl6(rtsock, RTM_ADD, ipv6_all_zeros,
332550b6e40SSowmini Varadhan 			    defrouter, 0, link, RTF_GATEWAY);
333550b6e40SSowmini Varadhan 		} else {
334550b6e40SSowmini Varadhan 			IN6_V4MAPPED_TO_INADDR(&defrouter, &gw4);
335550b6e40SSowmini Varadhan 			i_ipadm_rtioctl4(rtsock, RTM_ADD, INADDR_ANY,
336550b6e40SSowmini Varadhan 			    gw4.s_addr, 0, link, 0, RTF_GATEWAY);
337550b6e40SSowmini Varadhan 		}
338550b6e40SSowmini Varadhan 	}
339550b6e40SSowmini Varadhan }
340550b6e40SSowmini Varadhan 
341550b6e40SSowmini Varadhan /*
342550b6e40SSowmini Varadhan  * Wrapper function to zone_getattr() for retrieving from-gz attributes that
343550b6e40SSowmini Varadhan  * were made availabe for exclusive IP non-global zones by zoneadmd from teh
344550b6e40SSowmini Varadhan  * global zone.
345550b6e40SSowmini Varadhan  */
346550b6e40SSowmini Varadhan static ipadm_status_t
i_ipadm_zone_get_network(zoneid_t zoneid,datalink_id_t linkid,int type,void * buf,size_t * bufsize)347550b6e40SSowmini Varadhan i_ipadm_zone_get_network(zoneid_t zoneid, datalink_id_t linkid, int type,
348550b6e40SSowmini Varadhan     void *buf, size_t *bufsize)
349550b6e40SSowmini Varadhan {
350550b6e40SSowmini Varadhan 	zone_net_data_t *zndata;
351*ffe245b4SAndy Fiddaman 	ipadm_status_t ret = IPADM_SUCCESS;
352550b6e40SSowmini Varadhan 
353550b6e40SSowmini Varadhan 	zndata = calloc(1, sizeof (*zndata) + *bufsize);
354550b6e40SSowmini Varadhan 	if (zndata == NULL)
355550b6e40SSowmini Varadhan 		return (IPADM_NO_MEMORY);
356550b6e40SSowmini Varadhan 	zndata->zn_type = type;
357550b6e40SSowmini Varadhan 	zndata->zn_linkid = linkid;
358550b6e40SSowmini Varadhan 	zndata->zn_len = *bufsize;
359550b6e40SSowmini Varadhan 
360550b6e40SSowmini Varadhan 	if (zone_getattr(zoneid, ZONE_ATTR_NETWORK, zndata,
361550b6e40SSowmini Varadhan 	    sizeof (*zndata) + *bufsize) < 0) {
362*ffe245b4SAndy Fiddaman 		ret = ipadm_errno2status(errno);
363*ffe245b4SAndy Fiddaman 		goto out;
364550b6e40SSowmini Varadhan 	}
365550b6e40SSowmini Varadhan 	*bufsize = zndata->zn_len;
366550b6e40SSowmini Varadhan 	bcopy(zndata->zn_val, buf, *bufsize);
367*ffe245b4SAndy Fiddaman out:
368*ffe245b4SAndy Fiddaman 	free(zndata);
369*ffe245b4SAndy Fiddaman 	return (ret);
370550b6e40SSowmini Varadhan }
371550b6e40SSowmini Varadhan 
372550b6e40SSowmini Varadhan /*
373550b6e40SSowmini Varadhan  * Callback function that configures a single datalink in a non-global zone.
374550b6e40SSowmini Varadhan  */
375550b6e40SSowmini Varadhan static int
i_ipadm_zone_network_attr(dladm_handle_t dh,datalink_id_t linkid,void * arg)376550b6e40SSowmini Varadhan i_ipadm_zone_network_attr(dladm_handle_t dh, datalink_id_t linkid, void *arg)
377550b6e40SSowmini Varadhan {
378550b6e40SSowmini Varadhan 	ngz_walk_data_t *nwd = arg;
379550b6e40SSowmini Varadhan 	zoneid_t zoneid = nwd->ngz_zoneid;
380550b6e40SSowmini Varadhan 	uint8_t buf[PIPE_BUF];
381550b6e40SSowmini Varadhan 	dladm_status_t dlstatus;
382550b6e40SSowmini Varadhan 	ipadm_status_t ipstatus;
383550b6e40SSowmini Varadhan 	char link[MAXLINKNAMELEN];
384550b6e40SSowmini Varadhan 	ipadm_handle_t iph = nwd->ngz_iph;
385550b6e40SSowmini Varadhan 	int rtsock = iph->iph_rtsock;
386550b6e40SSowmini Varadhan 	char *ifname = nwd->ngz_ifname;
387550b6e40SSowmini Varadhan 	boolean_t s10c = nwd->ngz_s10c;
388550b6e40SSowmini Varadhan 	boolean_t is_ipmgmtd = (iph->iph_flags & IPH_IPMGMTD);
389550b6e40SSowmini Varadhan 	size_t bufsize = sizeof (buf);
390550b6e40SSowmini Varadhan 
391550b6e40SSowmini Varadhan 	bzero(buf, bufsize);
392550b6e40SSowmini Varadhan 	ipstatus = i_ipadm_zone_get_network(zoneid, linkid,
393550b6e40SSowmini Varadhan 	    ZONE_NETWORK_ADDRESS, buf, &bufsize);
394550b6e40SSowmini Varadhan 	if (ipstatus != IPADM_SUCCESS)
395550b6e40SSowmini Varadhan 		goto fail;
396550b6e40SSowmini Varadhan 
397550b6e40SSowmini Varadhan 	dlstatus = dladm_datalink_id2info(dh, linkid, NULL, NULL,
398550b6e40SSowmini Varadhan 	    NULL, link, sizeof (link));
399550b6e40SSowmini Varadhan 	if (dlstatus != DLADM_STATUS_OK)
400550b6e40SSowmini Varadhan 		return (DLADM_WALK_CONTINUE);
401550b6e40SSowmini Varadhan 
402550b6e40SSowmini Varadhan 	/*
403550b6e40SSowmini Varadhan 	 * if ifname has been specified, then skip interfaces that don't match
404550b6e40SSowmini Varadhan 	 */
405550b6e40SSowmini Varadhan 	if (ifname != NULL && strcmp(ifname, link) != 0)
406550b6e40SSowmini Varadhan 		return (DLADM_WALK_CONTINUE);
407550b6e40SSowmini Varadhan 
408550b6e40SSowmini Varadhan 	/*
409550b6e40SSowmini Varadhan 	 * Plumb the interface and configure addresses on for S10 Containers.
410550b6e40SSowmini Varadhan 	 * We need to always do this for S10C because ipadm persistent
411550b6e40SSowmini Varadhan 	 * configuration is not available in S10C. For ipkg zones,
412550b6e40SSowmini Varadhan 	 * we skip the actual plumbing/configuration, but will call the
413550b6e40SSowmini Varadhan 	 * (*ngz_persist_if)() callback to create the persistent state for the
414550b6e40SSowmini Varadhan 	 * interface. The interface will be configured in ipkg zones when
415550b6e40SSowmini Varadhan 	 * ipadm_enable_if() is invoked to restore persistent configuration.
416550b6e40SSowmini Varadhan 	 */
417550b6e40SSowmini Varadhan 	if (is_ipmgmtd && !s10c) {
418550b6e40SSowmini Varadhan 		(void) i_ipadm_ngz_persist_if(link, (char *)buf,
419550b6e40SSowmini Varadhan 		    nwd->ngz_persist_if);
420550b6e40SSowmini Varadhan 		return (DLADM_WALK_CONTINUE);
421550b6e40SSowmini Varadhan 	}
422550b6e40SSowmini Varadhan 	ipstatus = i_ipadm_ngz_addr(iph, link, (char *)buf);
423550b6e40SSowmini Varadhan 	if (ipstatus != IPADM_SUCCESS)
424550b6e40SSowmini Varadhan 		goto fail;
425550b6e40SSowmini Varadhan 
426550b6e40SSowmini Varadhan 	/* apply any default router information.  */
427550b6e40SSowmini Varadhan 	bufsize = sizeof (buf);
428550b6e40SSowmini Varadhan 	bzero(buf, bufsize);
429550b6e40SSowmini Varadhan 	ipstatus = i_ipadm_zone_get_network(zoneid, linkid,
430550b6e40SSowmini Varadhan 	    ZONE_NETWORK_DEFROUTER, buf, &bufsize);
431550b6e40SSowmini Varadhan 	if (ipstatus != IPADM_SUCCESS)
432550b6e40SSowmini Varadhan 		goto fail;
433550b6e40SSowmini Varadhan 
434550b6e40SSowmini Varadhan 	i_ipadm_create_ngz_route(rtsock, link, buf, bufsize);
435550b6e40SSowmini Varadhan 
436550b6e40SSowmini Varadhan 	return (DLADM_WALK_CONTINUE);
437550b6e40SSowmini Varadhan fail:
438550b6e40SSowmini Varadhan 	if (ifname != NULL) {
439550b6e40SSowmini Varadhan 		nwd->ngz_ipstatus = ipstatus;
440550b6e40SSowmini Varadhan 		return (DLADM_WALK_TERMINATE);
441550b6e40SSowmini Varadhan 	}
442550b6e40SSowmini Varadhan 	return (DLADM_WALK_CONTINUE);
443550b6e40SSowmini Varadhan }
444550b6e40SSowmini Varadhan 
445550b6e40SSowmini Varadhan /*
446550b6e40SSowmini Varadhan  * ipmgmt_net_from_gz_init() initializes exclusive-IP stack non-global zones by
447550b6e40SSowmini Varadhan  * extracting configuration that has been saved in the kernel and applying
448550b6e40SSowmini Varadhan  * that information to the appropriate datalinks for the zone. If an ifname
449550b6e40SSowmini Varadhan  * argument is passed in, only the selected IP interface corresponding to
450550b6e40SSowmini Varadhan  * datalink will be initialized, otherwise all datalinks will be plumbed for IP
451550b6e40SSowmini Varadhan  * and IP address and route information will be configured.
452550b6e40SSowmini Varadhan  */
453550b6e40SSowmini Varadhan ipadm_status_t
ipadm_init_net_from_gz(ipadm_handle_t iph,char * ifname,void (* persist_if)(char *,boolean_t,boolean_t))454550b6e40SSowmini Varadhan ipadm_init_net_from_gz(ipadm_handle_t iph, char *ifname,
455*ffe245b4SAndy Fiddaman     void (*persist_if)(char *, boolean_t, boolean_t))
456550b6e40SSowmini Varadhan {
457550b6e40SSowmini Varadhan 	ngz_walk_data_t nwd;
458550b6e40SSowmini Varadhan 	uint64_t flags;
459550b6e40SSowmini Varadhan 	dladm_handle_t dlh = iph->iph_dlh;
460550b6e40SSowmini Varadhan 	datalink_id_t linkid;
461550b6e40SSowmini Varadhan 
462550b6e40SSowmini Varadhan 	if (iph->iph_zoneid == GLOBAL_ZONEID)
463550b6e40SSowmini Varadhan 		return (IPADM_NOTSUP);
464550b6e40SSowmini Varadhan 
465550b6e40SSowmini Varadhan 	if (ifname != NULL &&
466550b6e40SSowmini Varadhan 	    i_ipadm_get_flags(iph, ifname, AF_INET, &flags) != IPADM_SUCCESS &&
467550b6e40SSowmini Varadhan 	    i_ipadm_get_flags(iph, ifname, AF_INET6, &flags) != IPADM_SUCCESS)
468550b6e40SSowmini Varadhan 		return (IPADM_ENXIO);
469550b6e40SSowmini Varadhan 
470550b6e40SSowmini Varadhan 	if (ifname != NULL && !(flags & IFF_L3PROTECT))
471550b6e40SSowmini Varadhan 		return (IPADM_SUCCESS); /* nothing to initialize */
472550b6e40SSowmini Varadhan 
473550b6e40SSowmini Varadhan 	nwd.ngz_iph = iph;
474550b6e40SSowmini Varadhan 	nwd.ngz_zoneid = iph->iph_zoneid;
475550b6e40SSowmini Varadhan 	nwd.ngz_ifname = ifname;
476550b6e40SSowmini Varadhan 	nwd.ngz_persist_if = persist_if;
477550b6e40SSowmini Varadhan 	nwd.ngz_s10c = i_ipadm_zone_is_s10c(iph->iph_zoneid);
478550b6e40SSowmini Varadhan 	nwd.ngz_ipstatus = IPADM_SUCCESS;
479550b6e40SSowmini Varadhan 	if (ifname != NULL) {
480550b6e40SSowmini Varadhan 		if (dladm_name2info(dlh, ifname, &linkid, NULL, NULL,
481550b6e40SSowmini Varadhan 		    NULL) != DLADM_STATUS_OK) {
482550b6e40SSowmini Varadhan 			return (IPADM_ENXIO);
483550b6e40SSowmini Varadhan 		}
484550b6e40SSowmini Varadhan 		(void) i_ipadm_zone_network_attr(dlh, linkid, &nwd);
485550b6e40SSowmini Varadhan 	} else {
486550b6e40SSowmini Varadhan 		(void) dladm_walk_datalink_id(i_ipadm_zone_network_attr, dlh,
487550b6e40SSowmini Varadhan 		    &nwd, DATALINK_CLASS_ALL, DATALINK_ANY_MEDIATYPE,
488550b6e40SSowmini Varadhan 		    DLADM_OPT_PERSIST);
489550b6e40SSowmini Varadhan 	}
490550b6e40SSowmini Varadhan 	return (nwd.ngz_ipstatus);
491550b6e40SSowmini Varadhan }