3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23 */
24
25 /*
26 * This file contains functions for address management such as creating
27 * an address, deleting an address, enabling an address, disabling an
28 * address, bringing an address down or up, setting/getting properties
29 * on an address object and listing address information
30 * for all addresses in active as well as persistent configuration.
31 */
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netdb.h>
35 #include <inet/ip.h>
36 #include <string.h>
37 #include <strings.h>
38 #include <assert.h>
39 #include <sys/sockio.h>
40 #include <errno.h>
41 #include <unistd.h>
42 #include <stropts.h>
79
80 /*
81 * Callback functions to retrieve property values from the kernel. These
82 * functions, when required, translate the values from the kernel to a format
83 * suitable for printing. They also retrieve DEFAULT, PERM and POSSIBLE values
84 * for a given property.
85 */
86 static ipadm_pd_getf_t i_ipadm_get_prefixlen, i_ipadm_get_addr_flag,
87 i_ipadm_get_zone, i_ipadm_get_broadcast;
88
89 /*
90 * Callback functions to set property values. These functions translate the
91 * values to a format suitable for kernel consumption, allocate the necessary
92 * ioctl buffers and then invoke ioctl().
93 */
94 static ipadm_pd_setf_t i_ipadm_set_prefixlen, i_ipadm_set_addr_flag,
95 i_ipadm_set_zone;
96
97 /* address properties description table */
98 ipadm_prop_desc_t ipadm_addrprop_table[] = {
99 { "broadcast", IPADMPROP_CLASS_ADDR, MOD_PROTO_NONE, 0,
100 NULL, NULL, i_ipadm_get_broadcast },
101
102 { "deprecated", IPADMPROP_CLASS_ADDR, MOD_PROTO_NONE, 0,
103 i_ipadm_set_addr_flag, i_ipadm_get_onoff,
104 i_ipadm_get_addr_flag },
105
106 { "prefixlen", IPADMPROP_CLASS_ADDR, MOD_PROTO_NONE, 0,
107 i_ipadm_set_prefixlen, i_ipadm_get_prefixlen,
108 i_ipadm_get_prefixlen },
109
110 { "private", IPADMPROP_CLASS_ADDR, MOD_PROTO_NONE, 0,
111 i_ipadm_set_addr_flag, i_ipadm_get_onoff, i_ipadm_get_addr_flag },
112
113 { "transmit", IPADMPROP_CLASS_ADDR, MOD_PROTO_NONE, 0,
114 i_ipadm_set_addr_flag, i_ipadm_get_onoff, i_ipadm_get_addr_flag },
115
116 { "zone", IPADMPROP_CLASS_ADDR, MOD_PROTO_NONE, 0,
117 i_ipadm_set_zone, NULL, i_ipadm_get_zone },
118
119 { NULL, 0, 0, 0, NULL, NULL, NULL }
120 };
121
122 static ipadm_prop_desc_t up_addrprop = { "up", IPADMPROP_CLASS_ADDR,
123 MOD_PROTO_NONE, 0, NULL, NULL, NULL };
124
125 /*
126 * Helper function that initializes the `ipadm_ifname', `ipadm_aobjname', and
127 * `ipadm_atype' fields of the given `ipaddr'.
128 */
129 void
130 i_ipadm_init_addr(ipadm_addrobj_t ipaddr, const char *ifname,
131 const char *aobjname, ipadm_addr_type_t atype)
132 {
133 bzero(ipaddr, sizeof (struct ipadm_addrobj_s));
134 (void) strlcpy(ipaddr->ipadm_ifname, ifname,
135 sizeof (ipaddr->ipadm_ifname));
136 (void) strlcpy(ipaddr->ipadm_aobjname, aobjname,
137 sizeof (ipaddr->ipadm_aobjname));
138 ipaddr->ipadm_atype = atype;
139 }
140
141 /*
142 * Determine the permission of the property depending on whether it has a
1359 }
1360 break;
1361 default:
1362 return (IPADM_INVALID_ARG);
1363 }
1364 if (nbytes >= *bufsize) {
1365 /* insufficient buffer space */
1366 *bufsize = nbytes + 1;
1367 return (IPADM_NO_BUFS);
1368 }
1369
1370 return (IPADM_SUCCESS);
1371 }
1372
1373 static ipadm_prop_desc_t *
1374 i_ipadm_get_addrprop_desc(const char *pname)
1375 {
1376 int i;
1377
1378 for (i = 0; ipadm_addrprop_table[i].ipd_name != NULL; i++) {
1379 if (strcmp(pname, ipadm_addrprop_table[i].ipd_name) == 0)
1380 return (&ipadm_addrprop_table[i]);
1381 }
1382 return (NULL);
1383 }
1384
1385 /*
1386 * Gets the value of the given address property `pname' for the address
1387 * object with name `aobjname'.
1388 */
1389 ipadm_status_t
1390 ipadm_get_addrprop(ipadm_handle_t iph, const char *pname, char *buf,
1391 uint_t *bufsize, const char *aobjname, uint_t valtype)
1392 {
1393 struct ipadm_addrobj_s ipaddr;
1394 ipadm_status_t status = IPADM_SUCCESS;
1395 sa_family_t af;
1396 ipadm_prop_desc_t *pdp = NULL;
1397
1398 if (iph == NULL || pname == NULL || buf == NULL ||
1399 bufsize == NULL || *bufsize == 0 || aobjname == NULL) {
|
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2013 by Delphix. All rights reserved.
24 */
25
26 /*
27 * This file contains functions for address management such as creating
28 * an address, deleting an address, enabling an address, disabling an
29 * address, bringing an address down or up, setting/getting properties
30 * on an address object and listing address information
31 * for all addresses in active as well as persistent configuration.
32 */
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netdb.h>
36 #include <inet/ip.h>
37 #include <string.h>
38 #include <strings.h>
39 #include <assert.h>
40 #include <sys/sockio.h>
41 #include <errno.h>
42 #include <unistd.h>
43 #include <stropts.h>
80
81 /*
82 * Callback functions to retrieve property values from the kernel. These
83 * functions, when required, translate the values from the kernel to a format
84 * suitable for printing. They also retrieve DEFAULT, PERM and POSSIBLE values
85 * for a given property.
86 */
87 static ipadm_pd_getf_t i_ipadm_get_prefixlen, i_ipadm_get_addr_flag,
88 i_ipadm_get_zone, i_ipadm_get_broadcast;
89
90 /*
91 * Callback functions to set property values. These functions translate the
92 * values to a format suitable for kernel consumption, allocate the necessary
93 * ioctl buffers and then invoke ioctl().
94 */
95 static ipadm_pd_setf_t i_ipadm_set_prefixlen, i_ipadm_set_addr_flag,
96 i_ipadm_set_zone;
97
98 /* address properties description table */
99 ipadm_prop_desc_t ipadm_addrprop_table[] = {
100 { "broadcast", NULL, IPADMPROP_CLASS_ADDR, MOD_PROTO_NONE, 0,
101 NULL, NULL, i_ipadm_get_broadcast },
102
103 { "deprecated", NULL, IPADMPROP_CLASS_ADDR, MOD_PROTO_NONE, 0,
104 i_ipadm_set_addr_flag, i_ipadm_get_onoff,
105 i_ipadm_get_addr_flag },
106
107 { "prefixlen", NULL, IPADMPROP_CLASS_ADDR, MOD_PROTO_NONE, 0,
108 i_ipadm_set_prefixlen, i_ipadm_get_prefixlen,
109 i_ipadm_get_prefixlen },
110
111 { "private", NULL, IPADMPROP_CLASS_ADDR, MOD_PROTO_NONE, 0,
112 i_ipadm_set_addr_flag, i_ipadm_get_onoff, i_ipadm_get_addr_flag },
113
114 { "transmit", NULL, IPADMPROP_CLASS_ADDR, MOD_PROTO_NONE, 0,
115 i_ipadm_set_addr_flag, i_ipadm_get_onoff, i_ipadm_get_addr_flag },
116
117 { "zone", NULL, IPADMPROP_CLASS_ADDR, MOD_PROTO_NONE, 0,
118 i_ipadm_set_zone, NULL, i_ipadm_get_zone },
119
120 { NULL, NULL, 0, 0, 0, NULL, NULL, NULL }
121 };
122
123 static ipadm_prop_desc_t up_addrprop = { "up", NULL, IPADMPROP_CLASS_ADDR,
124 MOD_PROTO_NONE, 0, NULL, NULL, NULL };
125
126 /*
127 * Helper function that initializes the `ipadm_ifname', `ipadm_aobjname', and
128 * `ipadm_atype' fields of the given `ipaddr'.
129 */
130 void
131 i_ipadm_init_addr(ipadm_addrobj_t ipaddr, const char *ifname,
132 const char *aobjname, ipadm_addr_type_t atype)
133 {
134 bzero(ipaddr, sizeof (struct ipadm_addrobj_s));
135 (void) strlcpy(ipaddr->ipadm_ifname, ifname,
136 sizeof (ipaddr->ipadm_ifname));
137 (void) strlcpy(ipaddr->ipadm_aobjname, aobjname,
138 sizeof (ipaddr->ipadm_aobjname));
139 ipaddr->ipadm_atype = atype;
140 }
141
142 /*
143 * Determine the permission of the property depending on whether it has a
1360 }
1361 break;
1362 default:
1363 return (IPADM_INVALID_ARG);
1364 }
1365 if (nbytes >= *bufsize) {
1366 /* insufficient buffer space */
1367 *bufsize = nbytes + 1;
1368 return (IPADM_NO_BUFS);
1369 }
1370
1371 return (IPADM_SUCCESS);
1372 }
1373
1374 static ipadm_prop_desc_t *
1375 i_ipadm_get_addrprop_desc(const char *pname)
1376 {
1377 int i;
1378
1379 for (i = 0; ipadm_addrprop_table[i].ipd_name != NULL; i++) {
1380 if (strcmp(pname, ipadm_addrprop_table[i].ipd_name) == 0 ||
1381 (ipadm_addrprop_table[i].ipd_old_name != NULL &&
1382 strcmp(pname, ipadm_addrprop_table[i].ipd_old_name) == 0))
1383 return (&ipadm_addrprop_table[i]);
1384 }
1385 return (NULL);
1386 }
1387
1388 /*
1389 * Gets the value of the given address property `pname' for the address
1390 * object with name `aobjname'.
1391 */
1392 ipadm_status_t
1393 ipadm_get_addrprop(ipadm_handle_t iph, const char *pname, char *buf,
1394 uint_t *bufsize, const char *aobjname, uint_t valtype)
1395 {
1396 struct ipadm_addrobj_s ipaddr;
1397 ipadm_status_t status = IPADM_SUCCESS;
1398 sa_family_t af;
1399 ipadm_prop_desc_t *pdp = NULL;
1400
1401 if (iph == NULL || pname == NULL || buf == NULL ||
1402 bufsize == NULL || *bufsize == 0 || aobjname == NULL) {
|