1 | /* |
---|
2 | * COPYRIGHT (c) 1989-2007. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.org/license/LICENSE. |
---|
8 | */ |
---|
9 | |
---|
10 | #ifdef HAVE_CONFIG_H |
---|
11 | #include "config.h" |
---|
12 | #endif |
---|
13 | |
---|
14 | #include <rtems.h> |
---|
15 | #include <string.h> |
---|
16 | #include <stdio.h> |
---|
17 | #include <ctype.h> |
---|
18 | #include <errno.h> |
---|
19 | |
---|
20 | #include <rtems/rtems_bsdnet.h> |
---|
21 | #include <sys/socket.h> |
---|
22 | #include <net/if.h> |
---|
23 | #include <arpa/inet.h> |
---|
24 | #include <netinet/in.h> |
---|
25 | #include <sys/sockio.h> |
---|
26 | #include <net/route.h> |
---|
27 | |
---|
28 | #include <rtems/monitor.h> |
---|
29 | |
---|
30 | void mon_ifconfig(int argc, char *argv[], |
---|
31 | uint32_t command_arg RTEMS_UNUSED, |
---|
32 | bool verbose RTEMS_UNUSED) |
---|
33 | { |
---|
34 | struct sockaddr_in ipaddr; |
---|
35 | struct sockaddr_in dstaddr; |
---|
36 | struct sockaddr_in netmask; |
---|
37 | struct sockaddr_in broadcast; |
---|
38 | char *iface; |
---|
39 | int f_ip = 0; |
---|
40 | int f_ptp = 0; |
---|
41 | int f_netmask = 0; |
---|
42 | int f_up = 0; |
---|
43 | int f_down = 0; |
---|
44 | int f_bcast = 0; |
---|
45 | int cur_idx; |
---|
46 | int rc; |
---|
47 | int flags; |
---|
48 | |
---|
49 | memset(&ipaddr, 0, sizeof(ipaddr)); |
---|
50 | memset(&dstaddr, 0, sizeof(dstaddr)); |
---|
51 | memset(&netmask, 0, sizeof(netmask)); |
---|
52 | memset(&broadcast, 0, sizeof(broadcast)); |
---|
53 | |
---|
54 | ipaddr.sin_len = sizeof(ipaddr); |
---|
55 | ipaddr.sin_family = AF_INET; |
---|
56 | |
---|
57 | dstaddr.sin_len = sizeof(dstaddr); |
---|
58 | dstaddr.sin_family = AF_INET; |
---|
59 | |
---|
60 | netmask.sin_len = sizeof(netmask); |
---|
61 | netmask.sin_family = AF_INET; |
---|
62 | |
---|
63 | broadcast.sin_len = sizeof(broadcast); |
---|
64 | broadcast.sin_family = AF_INET; |
---|
65 | |
---|
66 | cur_idx = 0; |
---|
67 | if (argc <= 1) { |
---|
68 | /* display all interfaces */ |
---|
69 | iface = NULL; |
---|
70 | cur_idx += 1; |
---|
71 | } else { |
---|
72 | iface = argv[1]; |
---|
73 | if (isdigit((unsigned char)*argv[2])) { |
---|
74 | if (inet_pton(AF_INET, argv[2], &ipaddr.sin_addr) < 0) { |
---|
75 | printf("bad ip address: %s\n", argv[2]); |
---|
76 | return; |
---|
77 | } |
---|
78 | f_ip = 1; |
---|
79 | cur_idx += 3; |
---|
80 | } else { |
---|
81 | cur_idx += 2; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | if ((f_down !=0) && (f_ip != 0)) { |
---|
86 | f_up = 1; |
---|
87 | } |
---|
88 | |
---|
89 | while(argc > cur_idx) { |
---|
90 | if (strcmp(argv[cur_idx], "up") == 0) { |
---|
91 | f_up = 1; |
---|
92 | if (f_down != 0) { |
---|
93 | printf("Can't make interface up and down\n"); |
---|
94 | } |
---|
95 | } else if(strcmp(argv[cur_idx], "down") == 0) { |
---|
96 | f_down = 1; |
---|
97 | if (f_up != 0) { |
---|
98 | printf("Can't make interface up and down\n"); |
---|
99 | } |
---|
100 | } else if(strcmp(argv[cur_idx], "netmask") == 0) { |
---|
101 | if ((cur_idx + 1) >= argc) { |
---|
102 | printf("No netmask address\n"); |
---|
103 | return; |
---|
104 | } |
---|
105 | if (inet_pton(AF_INET, argv[cur_idx+1], &netmask.sin_addr) < 0) { |
---|
106 | printf("bad netmask: %s\n", argv[cur_idx]); |
---|
107 | return; |
---|
108 | } |
---|
109 | f_netmask = 1; |
---|
110 | cur_idx += 1; |
---|
111 | } else if(strcmp(argv[cur_idx], "broadcast") == 0) { |
---|
112 | if ((cur_idx + 1) >= argc) { |
---|
113 | printf("No broadcast address\n"); |
---|
114 | return; |
---|
115 | } |
---|
116 | if (inet_pton(AF_INET, argv[cur_idx+1], &broadcast.sin_addr) < 0) { |
---|
117 | printf("bad broadcast: %s\n", argv[cur_idx]); |
---|
118 | return; |
---|
119 | } |
---|
120 | f_bcast = 1; |
---|
121 | cur_idx += 1; |
---|
122 | } else if(strcmp(argv[cur_idx], "pointopoint") == 0) { |
---|
123 | if ((cur_idx + 1) >= argc) { |
---|
124 | printf("No pointopoint address\n"); |
---|
125 | return; |
---|
126 | } |
---|
127 | if (inet_pton(AF_INET, argv[cur_idx+1], &dstaddr.sin_addr) < 0) { |
---|
128 | printf("bad pointopoint: %s\n", argv[cur_idx]); |
---|
129 | return; |
---|
130 | } |
---|
131 | |
---|
132 | f_ptp = 1; |
---|
133 | cur_idx += 1; |
---|
134 | } else { |
---|
135 | printf("Bad parameter: %s\n", argv[cur_idx]); |
---|
136 | return; |
---|
137 | } |
---|
138 | |
---|
139 | cur_idx += 1; |
---|
140 | } |
---|
141 | |
---|
142 | printf("ifconfig "); |
---|
143 | if (iface != NULL) { |
---|
144 | printf("%s ", iface); |
---|
145 | if (f_ip != 0) { |
---|
146 | char str[256]; |
---|
147 | inet_ntop(AF_INET, &ipaddr.sin_addr, str, 256); |
---|
148 | printf("%s ", str); |
---|
149 | } |
---|
150 | |
---|
151 | if (f_netmask != 0) { |
---|
152 | char str[256]; |
---|
153 | inet_ntop(AF_INET, &netmask.sin_addr, str, 256); |
---|
154 | printf("netmask %s ", str); |
---|
155 | } |
---|
156 | |
---|
157 | if (f_bcast != 0) { |
---|
158 | char str[256]; |
---|
159 | inet_ntop(AF_INET, &broadcast.sin_addr, str, 256); |
---|
160 | printf("broadcast %s ", str); |
---|
161 | } |
---|
162 | |
---|
163 | if (f_ptp != 0) { |
---|
164 | char str[256]; |
---|
165 | inet_ntop(AF_INET, &dstaddr.sin_addr, str, 256); |
---|
166 | printf("pointopoint %s ", str); |
---|
167 | } |
---|
168 | |
---|
169 | if (f_up != 0) { |
---|
170 | printf("up\n"); |
---|
171 | } else if (f_down != 0) { |
---|
172 | printf("down\n"); |
---|
173 | } else { |
---|
174 | printf("\n"); |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | if ((iface == NULL) || ((f_ip == 0) && (f_down == 0) && (f_up == 0))) { |
---|
179 | rtems_bsdnet_show_if_stats(); |
---|
180 | return; |
---|
181 | } |
---|
182 | |
---|
183 | flags = 0; |
---|
184 | if (f_netmask) { |
---|
185 | rc = rtems_bsdnet_ifconfig(iface, SIOCSIFNETMASK, &netmask); |
---|
186 | if (rc < 0) { |
---|
187 | printf("Could not set netmask: %s\n", strerror(errno)); |
---|
188 | return; |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | if (f_bcast) { |
---|
193 | rc = rtems_bsdnet_ifconfig(iface, SIOCSIFBRDADDR, &broadcast); |
---|
194 | if (rc < 0) { |
---|
195 | printf("Could not set broadcast: %s\n", strerror(errno)); |
---|
196 | return; |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | if (f_ptp) { |
---|
201 | rc = rtems_bsdnet_ifconfig(iface, SIOCSIFDSTADDR, &dstaddr); |
---|
202 | if (rc < 0) { |
---|
203 | printf("Could not set destination address: %s\n", strerror(errno)); |
---|
204 | return; |
---|
205 | } |
---|
206 | flags |= IFF_POINTOPOINT; |
---|
207 | } |
---|
208 | |
---|
209 | /* This must come _after_ setting the netmask, broadcast addresses */ |
---|
210 | if (f_ip) { |
---|
211 | rc = rtems_bsdnet_ifconfig(iface, SIOCSIFADDR, &ipaddr); |
---|
212 | if (rc < 0) { |
---|
213 | printf("Could not set IP address: %s\n", strerror(errno)); |
---|
214 | return; |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | if (f_up != 0) { |
---|
219 | flags |= IFF_UP; |
---|
220 | } |
---|
221 | |
---|
222 | if (f_down != 0) { |
---|
223 | printf("Warning: taking interfaces down is not supported\n"); |
---|
224 | } |
---|
225 | |
---|
226 | rc = rtems_bsdnet_ifconfig(iface, SIOCSIFFLAGS, &flags); |
---|
227 | if (rc < 0) { |
---|
228 | printf("Could not set interface flags: %s\n", strerror(errno)); |
---|
229 | return; |
---|
230 | } |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | |
---|
235 | void mon_route(int argc, char *argv[], |
---|
236 | uint32_t command_arg RTEMS_UNUSED, |
---|
237 | bool verbose RTEMS_UNUSED) |
---|
238 | { |
---|
239 | int cmd; |
---|
240 | struct sockaddr_in dst; |
---|
241 | struct sockaddr_in gw; |
---|
242 | struct sockaddr_in netmask; |
---|
243 | int f_host; |
---|
244 | int f_gw = 0; |
---|
245 | int cur_idx; |
---|
246 | int flags; |
---|
247 | int rc; |
---|
248 | |
---|
249 | memset(&dst, 0, sizeof(dst)); |
---|
250 | memset(&gw, 0, sizeof(gw)); |
---|
251 | memset(&netmask, 0, sizeof(netmask)); |
---|
252 | |
---|
253 | dst.sin_len = sizeof(dst); |
---|
254 | dst.sin_family = AF_INET; |
---|
255 | dst.sin_addr.s_addr = inet_addr("0.0.0.0"); |
---|
256 | |
---|
257 | gw.sin_len = sizeof(gw); |
---|
258 | gw.sin_family = AF_INET; |
---|
259 | gw.sin_addr.s_addr = inet_addr("0.0.0.0"); |
---|
260 | |
---|
261 | netmask.sin_len = sizeof(netmask); |
---|
262 | netmask.sin_family = AF_INET; |
---|
263 | netmask.sin_addr.s_addr = inet_addr("255.255.255.0"); |
---|
264 | |
---|
265 | if (argc < 2) { |
---|
266 | rtems_bsdnet_show_inet_routes(); |
---|
267 | return; |
---|
268 | } |
---|
269 | |
---|
270 | if (strcmp(argv[1], "add") == 0) { |
---|
271 | cmd = RTM_ADD; |
---|
272 | } else if (strcmp(argv[1], "del") == 0) { |
---|
273 | cmd = RTM_DELETE; |
---|
274 | } else { |
---|
275 | printf("invalid command: %s\n", argv[1]); |
---|
276 | printf("\tit should be 'add' or 'del'\n"); |
---|
277 | return; |
---|
278 | } |
---|
279 | |
---|
280 | if (argc < 3) { |
---|
281 | printf("not enough arguments\n"); |
---|
282 | return; |
---|
283 | } |
---|
284 | |
---|
285 | if (strcmp(argv[2], "-host") == 0) { |
---|
286 | f_host = 1; |
---|
287 | } else if (strcmp(argv[2], "-net") == 0) { |
---|
288 | f_host = 0; |
---|
289 | } else { |
---|
290 | printf("Invalid type: %s\n", argv[1]); |
---|
291 | printf("\tit should be '-host' or '-net'\n"); |
---|
292 | return; |
---|
293 | } |
---|
294 | |
---|
295 | if (argc < 4) { |
---|
296 | printf("not enough arguments\n"); |
---|
297 | return; |
---|
298 | } |
---|
299 | |
---|
300 | inet_pton(AF_INET, argv[3], &dst.sin_addr); |
---|
301 | |
---|
302 | cur_idx = 4; |
---|
303 | while(cur_idx < argc) { |
---|
304 | if (strcmp(argv[cur_idx], "gw") == 0) { |
---|
305 | if ((cur_idx +1) >= argc) { |
---|
306 | printf("no gateway address\n"); |
---|
307 | return; |
---|
308 | } |
---|
309 | f_gw = 1; |
---|
310 | inet_pton(AF_INET, argv[cur_idx + 1], &gw.sin_addr); |
---|
311 | cur_idx += 1; |
---|
312 | } else if(strcmp(argv[cur_idx], "netmask") == 0) { |
---|
313 | if ((cur_idx +1) >= argc) { |
---|
314 | printf("no netmask address\n"); |
---|
315 | return; |
---|
316 | } |
---|
317 | f_gw = 1; |
---|
318 | inet_pton(AF_INET, argv[cur_idx + 1], &netmask.sin_addr); |
---|
319 | cur_idx += 1; |
---|
320 | } else { |
---|
321 | printf("Unknown argument: %s\n", argv[cur_idx]); |
---|
322 | return; |
---|
323 | } |
---|
324 | cur_idx += 1; |
---|
325 | } |
---|
326 | |
---|
327 | flags = RTF_STATIC; |
---|
328 | if (f_gw != 0) { |
---|
329 | flags |= RTF_GATEWAY; |
---|
330 | } |
---|
331 | if (f_host != 0) { |
---|
332 | flags |= RTF_HOST; |
---|
333 | } |
---|
334 | |
---|
335 | rc = rtems_bsdnet_rtrequest(cmd, |
---|
336 | (struct sockaddr*) &dst, |
---|
337 | (struct sockaddr*) &gw, |
---|
338 | (struct sockaddr*) &netmask, flags, NULL); |
---|
339 | if (rc < 0) { |
---|
340 | printf("Error adding route\n"); |
---|
341 | } |
---|
342 | } |
---|