1 /* |
|
2 * CDDL HEADER START |
|
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 /* |
|
23 * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 #include <sys/types.h> |
|
27 #include <stropts.h> |
|
28 #include <sys/conf.h> |
|
29 #include <net/if.h> |
|
30 #include <sys/sockio.h> |
|
31 #include <string.h> |
|
32 #include <stdio.h> |
|
33 #include <stdlib.h> |
|
34 #include <unistd.h> |
|
35 #include <netdb.h> |
|
36 #include <arpa/inet.h> |
|
37 |
|
38 #include <rad/adr.h> |
|
39 #include <rad/rad_modapi.h> |
|
40 |
|
41 #include "api_network.h" |
|
42 |
|
43 #include <sys/socket.h> |
|
44 #include <sys/ioctl.h> |
|
45 #include <netinet/in.h> |
|
46 #include <net/if.h> |
|
47 #include <ifaddrs.h> |
|
48 |
|
49 static int |
|
50 if_count(int fd, int *count) |
|
51 { |
|
52 struct lifnum ifnum; |
|
53 |
|
54 ifnum.lifn_family = AF_INET; |
|
55 ifnum.lifn_flags = 0; |
|
56 if (ioctl(fd, SIOCGLIFNUM, &ifnum) == -1) { |
|
57 (void) close(fd); |
|
58 return (-1); |
|
59 } |
|
60 |
|
61 *count = ifnum.lifn_count; |
|
62 return (0); |
|
63 } |
|
64 |
|
65 /* ARGSUSED */ |
|
66 conerr_t |
|
67 interface_Network_read_networkInterfaces(rad_instance_t *inst, |
|
68 adr_attribute_t *attr, adr_data_t **data, adr_data_t **error) |
|
69 { |
|
70 int fd; |
|
71 int count, rcount; |
|
72 int reqsize; |
|
73 struct lifreq *reqs = NULL; |
|
74 struct lifconf ifconf; |
|
75 adr_data_t *result; |
|
76 |
|
77 if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) |
|
78 return (CE_OBJECT); |
|
79 |
|
80 do { |
|
81 if (reqs != NULL) |
|
82 free(reqs); |
|
83 |
|
84 if (if_count(fd, &count) == -1) |
|
85 return (CE_OBJECT); |
|
86 |
|
87 if (count == 0) { |
|
88 (void) close(fd); |
|
89 *data = adr_data_new_array(&adr_t_array_string, 0); |
|
90 return (CE_OK); |
|
91 } |
|
92 |
|
93 count += 5; |
|
94 reqsize = count * sizeof (struct lifreq); |
|
95 if ((reqs = malloc(reqsize)) == NULL) { |
|
96 (void) close(fd); |
|
97 return (CE_OBJECT); |
|
98 } |
|
99 |
|
100 ifconf.lifc_family = AF_INET; |
|
101 ifconf.lifc_len = reqsize; |
|
102 ifconf.lifc_buf = (char *)reqs; |
|
103 ifconf.lifc_flags = 0; |
|
104 if (ioctl(fd, SIOCGLIFCONF, &ifconf) == -1) { |
|
105 (void) close(fd); |
|
106 free(reqs); |
|
107 return (CE_OBJECT); |
|
108 } |
|
109 rcount = ifconf.lifc_len / sizeof (struct lifreq); |
|
110 } while (rcount >= count); |
|
111 |
|
112 result = adr_data_new_array(&adr_t_array_string, rcount); |
|
113 for (int i = 0; i < rcount; i++) { |
|
114 struct lifreq *r = &ifconf.lifc_req[i]; |
|
115 if (strchr(r->lifr_name, ':') != NULL) |
|
116 continue; |
|
117 if (ioctl(fd, SIOCGLIFFLAGS, r) == -1) |
|
118 continue; |
|
119 if ((r->lifr_flags & (IFF_LOOPBACK | IFF_VIRTUAL)) != 0) |
|
120 continue; |
|
121 (void) adr_array_add(result, adr_data_new_string(r->lifr_name, |
|
122 LT_COPY)); |
|
123 } |
|
124 (void) close(fd); |
|
125 free(reqs); |
|
126 |
|
127 *data = adr_data_purify_deep(result); |
|
128 return (CE_OK); |
|
129 } |
|
130 |
|
131 /* ARGSUSED */ |
|
132 conerr_t |
|
133 interface_Network_invoke_getHostNameForIP(rad_instance_t *inst, |
|
134 adr_method_t *meth, adr_data_t **ret, adr_data_t **args, int count, |
|
135 adr_data_t **error) |
|
136 { |
|
137 int errnum; |
|
138 struct hostent *he; |
|
139 ipaddr_t addr; |
|
140 |
|
141 if (inet_pton(AF_INET, adr_data_to_string(args[0]), &addr) != 1) |
|
142 return (CE_OBJECT); |
|
143 |
|
144 if ((he = getipnodebyaddr(&addr, sizeof (addr), AF_INET, &errnum)) |
|
145 == NULL) { |
|
146 /* Not found? Bounce it back. */ |
|
147 (void) adr_data_ref(args[0]); |
|
148 *ret = args[0]; |
|
149 return (CE_OK); |
|
150 } |
|
151 |
|
152 adr_data_t *result = adr_data_new_string(he->h_name, LT_COPY); |
|
153 freehostent(he); |
|
154 *ret = result; |
|
155 |
|
156 return (CE_OK); |
|
157 } |
|
158 |
|
159 /* ARGSUSED */ |
|
160 conerr_t |
|
161 interface_Network_invoke_hostGetIPs(rad_instance_t *inst, adr_method_t *meth, |
|
162 adr_data_t **ret, adr_data_t **args, int count, adr_data_t **error) |
|
163 { |
|
164 int errnum; |
|
165 struct hostent *he; |
|
166 |
|
167 if ((he = getipnodebyname(adr_data_to_string(args[0]), AF_INET, |
|
168 AI_DEFAULT, &errnum)) == NULL) { |
|
169 *ret = NULL; |
|
170 return (CE_OBJECT); |
|
171 } |
|
172 |
|
173 int n = 0; |
|
174 for (char **aptr = he->h_addr_list; *aptr != NULL; aptr++) |
|
175 n++; |
|
176 adr_data_t *result = adr_data_new_array(&adr_t_array_string, n); |
|
177 for (int i = 0; i < n; i++) { |
|
178 char abuf[INET_ADDRSTRLEN]; |
|
179 if (inet_ntop(AF_INET, he->h_addr_list[i], abuf, |
|
180 sizeof (abuf)) != NULL) |
|
181 (void) adr_array_add(result, adr_data_new_string(abuf, |
|
182 LT_COPY)); |
|
183 } |
|
184 |
|
185 *ret = adr_data_purify_deep(result); |
|
186 freehostent(he); |
|
187 |
|
188 return (CE_OK); |
|
189 } |
|
190 |
|
191 /* ARGSUSED */ |
|
192 conerr_t |
|
193 interface_Network_invoke_isLocalAddress(rad_instance_t *inst, |
|
194 adr_method_t *meth, adr_data_t **ret, adr_data_t **args, int count, |
|
195 adr_data_t **error) |
|
196 { |
|
197 struct ifaddrs *ifaddr; |
|
198 struct ifaddrs *ifa; |
|
199 struct sockaddr_in *sin; |
|
200 struct sockaddr_in6 *sin6; |
|
201 |
|
202 char buf[1024]; |
|
203 (void) memset(buf, 0, sizeof (buf)); |
|
204 |
|
205 /* Set default return value */ |
|
206 *ret = adr_data_new_boolean(B_FALSE); |
|
207 |
|
208 // Get IP addresses of each network interface. |
|
209 // |
|
210 // ifa_name ifa_addr->sin_addr.s_addr |
|
211 // |
|
212 // lo0 0.0.0.0 |
|
213 // lo0 ::1 (AF_INET6) |
|
214 // lo0 127.0.0.1 (AF_INET) |
|
215 // e1000g0 129.XXX.XXX.XXX (AF_INET) |
|
216 // |
|
217 // See: man -s3 socket getifaddrs |
|
218 if (getifaddrs(&ifaddr) == -1) { |
|
219 return (CE_OBJECT); |
|
220 } |
|
221 |
|
222 /* Iterate over linked list of IP addresses */ |
|
223 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { |
|
224 if (ifa->ifa_flags & IFF_IPV4) { |
|
225 /* LINTED */ |
|
226 sin = (struct sockaddr_in *)ifa->ifa_addr; |
|
227 |
|
228 /* Match given IP address */ |
|
229 if (strcmp(adr_data_to_string(args[0]), |
|
230 inet_ntop(AF_INET, |
|
231 &sin->sin_addr.s_addr, buf, 1024)) == 0) { |
|
232 *ret = adr_data_new_boolean(B_TRUE); |
|
233 break; |
|
234 } |
|
235 } else { |
|
236 /* LINTED */ |
|
237 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; |
|
238 |
|
239 /* Match given IP address */ |
|
240 if (strcmp(adr_data_to_string(args[0]), |
|
241 inet_ntop(AF_INET6, |
|
242 &sin6->sin6_addr.s6_addr, buf, 1024)) == 0) { |
|
243 *ret = adr_data_new_boolean(B_TRUE); |
|
244 break; |
|
245 } |
|
246 } |
|
247 } |
|
248 freeifaddrs(ifaddr); |
|
249 return (CE_OK); |
|
250 } |
|
251 |
|
252 int |
|
253 _rad_init(void) |
|
254 { |
|
255 adr_name_t *aname = adr_name_vcreate(MOD_DOMAIN, 1, "type", "Network"); |
|
256 conerr_t cerr = rad_cont_insert_singleton(rad_container, aname, |
|
257 &modinfo, &interface_Network_svr); |
|
258 adr_name_rele(aname); |
|
259 if (cerr != CE_OK) { |
|
260 rad_log(RL_ERROR, "(mod_network) failed to insert Network"); |
|
261 return (-1); |
|
262 } |
|
263 |
|
264 return (0); |
|
265 } |
|
266 |
|
267 /* |
|
268 * _rad_fini is called by the RAD daemon when the module is unloaded. Any |
|
269 * module finalisation is completed here. |
|
270 */ |
|
271 /*ARGSUSED*/ |
|
272 void |
|
273 _rad_fini(void *unused) |
|
274 { |
|
275 } |
|