]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
1 | /* |
2 | * ipaddress.c "ip address". | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | |
10 | * | |
11 | * Changes: | |
12 | * Laszlo Valko <valko@linux.karinthy.hu> 990223: address label must be zero terminated | |
13 | */ | |
14 | ||
15 | #include <stdio.h> | |
16 | #include <stdlib.h> | |
17 | #include <unistd.h> | |
18 | #include <syslog.h> | |
19 | #include <fcntl.h> | |
20 | #include <sys/ioctl.h> | |
21 | #include <sys/socket.h> | |
22 | #include <sys/ioctl.h> | |
23 | #include <linux/netdevice.h> | |
24 | #include <linux/if_arp.h> | |
25 | #include <linux/sockios.h> | |
26 | #include <netinet/in.h> | |
27 | #include <arpa/inet.h> | |
28 | #include <string.h> | |
29 | #include <fnmatch.h> | |
30 | ||
31 | #include "rt_names.h" | |
32 | #include "utils.h" | |
33 | #include "ll_map.h" | |
34 | #include "ip_common.h" | |
35 | ||
36 | static struct | |
37 | { | |
38 | int ifindex; | |
39 | int family; | |
40 | int oneline; | |
41 | int showqueue; | |
42 | inet_prefix pfx; | |
43 | int scope, scopemask; | |
44 | int flags, flagmask; | |
45 | int up; | |
46 | char *label; | |
47 | int flushed; | |
48 | char *flushb; | |
49 | int flushp; | |
50 | int flushe; | |
51 | struct rtnl_handle *rth; | |
52 | } filter; | |
53 | ||
54 | static int do_link; | |
55 | ||
56 | static void usage(void) __attribute__((noreturn)); | |
57 | ||
58 | static void usage(void) | |
59 | { | |
60 | if (do_link) { | |
61 | iplink_usage(); | |
62 | } | |
63 | fprintf(stderr, "Usage: ip addr {add|del} IFADDR dev STRING\n"); | |
64 | fprintf(stderr, " ip addr {show|flush} [ dev STRING ] [ scope SCOPE-ID ]\n"); | |
65 | fprintf(stderr, " [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ]\n"); | |
66 | fprintf(stderr, "IFADDR := PREFIX | ADDR peer PREFIX\n"); | |
67 | fprintf(stderr, " [ broadcast ADDR ] [ anycast ADDR ]\n"); | |
68 | fprintf(stderr, " [ label STRING ] [ scope SCOPE-ID ]\n"); | |
69 | fprintf(stderr, "SCOPE-ID := [ host | link | global | NUMBER ]\n"); | |
70 | fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n"); | |
71 | fprintf(stderr, "FLAG := [ permanent | dynamic | secondary | primary |\n"); | |
72 | fprintf(stderr, " tentative | deprecated ]\n"); | |
73 | exit(-1); | |
74 | } | |
75 | ||
76 | void print_link_flags(FILE *fp, unsigned flags, unsigned mdown) | |
77 | { | |
78 | fprintf(fp, "<"); | |
79 | flags &= ~IFF_RUNNING; | |
80 | #define _PF(f) if (flags&IFF_##f) { \ | |
81 | flags &= ~IFF_##f ; \ | |
82 | fprintf(fp, #f "%s", flags ? "," : ""); } | |
83 | _PF(LOOPBACK); | |
84 | _PF(BROADCAST); | |
85 | _PF(POINTOPOINT); | |
86 | _PF(MULTICAST); | |
87 | _PF(NOARP); | |
88 | _PF(ALLMULTI); | |
89 | _PF(PROMISC); | |
90 | _PF(MASTER); | |
91 | _PF(SLAVE); | |
92 | _PF(DEBUG); | |
93 | _PF(DYNAMIC); | |
94 | _PF(AUTOMEDIA); | |
95 | _PF(PORTSEL); | |
96 | _PF(NOTRAILERS); | |
97 | _PF(UP); | |
98 | #undef _PF | |
99 | if (flags) | |
100 | fprintf(fp, "%x", flags); | |
101 | if (mdown) | |
102 | fprintf(fp, ",M-DOWN"); | |
103 | fprintf(fp, "> "); | |
104 | } | |
105 | ||
106 | void print_queuelen(char *name) | |
107 | { | |
108 | struct ifreq ifr; | |
109 | int s; | |
110 | ||
111 | s = socket(AF_INET, SOCK_STREAM, 0); | |
112 | if (s < 0) | |
113 | return; | |
114 | ||
115 | memset(&ifr, 0, sizeof(ifr)); | |
116 | strcpy(ifr.ifr_name, name); | |
117 | if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) { | |
118 | perror("SIOCGIFXQLEN"); | |
119 | close(s); | |
120 | return; | |
121 | } | |
122 | close(s); | |
123 | ||
124 | if (ifr.ifr_qlen) | |
125 | printf("qlen %d", ifr.ifr_qlen); | |
126 | } | |
127 | ||
128 | int print_linkinfo(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) | |
129 | { | |
130 | FILE *fp = (FILE*)arg; | |
131 | struct ifinfomsg *ifi = NLMSG_DATA(n); | |
132 | struct rtattr * tb[IFLA_MAX+1]; | |
133 | int len = n->nlmsg_len; | |
134 | unsigned m_flag = 0; | |
135 | ||
136 | if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK) | |
137 | return 0; | |
138 | ||
139 | len -= NLMSG_LENGTH(sizeof(*ifi)); | |
140 | if (len < 0) | |
141 | return -1; | |
142 | ||
143 | if (filter.ifindex && ifi->ifi_index != filter.ifindex) | |
144 | return 0; | |
145 | if (filter.up && !(ifi->ifi_flags&IFF_UP)) | |
146 | return 0; | |
147 | ||
148 | memset(tb, 0, sizeof(tb)); | |
149 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); | |
150 | if (tb[IFLA_IFNAME] == NULL) { | |
151 | fprintf(stderr, "BUG: nil ifname\n"); | |
152 | return -1; | |
153 | } | |
154 | if (filter.label && | |
155 | (!filter.family || filter.family == AF_PACKET) && | |
156 | fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)) | |
157 | return 0; | |
158 | ||
159 | if (n->nlmsg_type == RTM_DELLINK) | |
160 | fprintf(fp, "Deleted "); | |
161 | ||
162 | fprintf(fp, "%d: %s", ifi->ifi_index, | |
163 | tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>"); | |
164 | ||
165 | if (tb[IFLA_LINK]) { | |
166 | SPRINT_BUF(b1); | |
167 | int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]); | |
168 | if (iflink == 0) | |
169 | fprintf(fp, "@NONE: "); | |
170 | else { | |
171 | fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1)); | |
172 | m_flag = ll_index_to_flags(iflink); | |
173 | m_flag = !(m_flag & IFF_UP); | |
174 | } | |
175 | } else { | |
176 | fprintf(fp, ": "); | |
177 | } | |
178 | print_link_flags(fp, ifi->ifi_flags, m_flag); | |
179 | ||
180 | if (tb[IFLA_MTU]) | |
181 | fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU])); | |
182 | if (tb[IFLA_QDISC]) | |
183 | fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC])); | |
184 | #ifdef IFLA_MASTER | |
185 | if (tb[IFLA_MASTER]) { | |
186 | SPRINT_BUF(b1); | |
187 | fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1)); | |
188 | } | |
189 | #endif | |
190 | if (filter.showqueue) | |
191 | print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME])); | |
192 | ||
193 | if (!filter.family || filter.family == AF_PACKET) { | |
194 | SPRINT_BUF(b1); | |
195 | fprintf(fp, "%s", _SL_); | |
196 | fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1))); | |
197 | ||
198 | if (tb[IFLA_ADDRESS]) { | |
199 | fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]), | |
200 | RTA_PAYLOAD(tb[IFLA_ADDRESS]), | |
201 | ifi->ifi_type, | |
202 | b1, sizeof(b1))); | |
203 | } | |
204 | if (tb[IFLA_BROADCAST]) { | |
205 | if (ifi->ifi_flags&IFF_POINTOPOINT) | |
206 | fprintf(fp, " peer "); | |
207 | else | |
208 | fprintf(fp, " brd "); | |
209 | fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]), | |
210 | RTA_PAYLOAD(tb[IFLA_BROADCAST]), | |
211 | ifi->ifi_type, | |
212 | b1, sizeof(b1))); | |
213 | } | |
214 | } | |
215 | if (do_link && tb[IFLA_STATS] && show_stats) { | |
216 | struct net_device_stats slocal; | |
217 | struct net_device_stats *s = RTA_DATA(tb[IFLA_STATS]); | |
218 | if (((unsigned long)s) & (sizeof(unsigned long)-1)) { | |
219 | memcpy(&slocal, s, sizeof(slocal)); | |
220 | s = &slocal; | |
221 | } | |
222 | fprintf(fp, "%s", _SL_); | |
223 | fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s", | |
224 | s->rx_compressed ? "compressed" : "", _SL_); | |
225 | fprintf(fp, " %-10lu %-8lu %-7lu %-7lu %-7lu %-7lu", | |
226 | s->rx_bytes, s->rx_packets, s->rx_errors, | |
227 | s->rx_dropped, s->rx_over_errors, | |
228 | s->multicast | |
229 | ); | |
230 | if (s->rx_compressed) | |
231 | fprintf(fp, " %-7lu", s->rx_compressed); | |
232 | if (show_stats > 1) { | |
233 | fprintf(fp, "%s", _SL_); | |
234 | fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_); | |
235 | fprintf(fp, " %-7lu %-7lu %-7lu %-7lu %-7lu", | |
236 | s->rx_length_errors, | |
237 | s->rx_crc_errors, | |
238 | s->rx_frame_errors, | |
239 | s->rx_fifo_errors, | |
240 | s->rx_missed_errors | |
241 | ); | |
242 | } | |
243 | fprintf(fp, "%s", _SL_); | |
244 | fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s", | |
245 | s->tx_compressed ? "compressed" : "", _SL_); | |
246 | fprintf(fp, " %-10lu %-8lu %-7lu %-7lu %-7lu %-7lu", | |
247 | s->tx_bytes, s->tx_packets, s->tx_errors, | |
248 | s->tx_dropped, s->tx_carrier_errors, s->collisions); | |
249 | if (s->tx_compressed) | |
250 | fprintf(fp, " %-7lu", s->tx_compressed); | |
251 | if (show_stats > 1) { | |
252 | fprintf(fp, "%s", _SL_); | |
253 | fprintf(fp, " TX errors: aborted fifo window heartbeat%s", _SL_); | |
254 | fprintf(fp, " %-7lu %-7lu %-7lu %-7lu", | |
255 | s->tx_aborted_errors, | |
256 | s->tx_fifo_errors, | |
257 | s->tx_window_errors, | |
258 | s->tx_heartbeat_errors | |
259 | ); | |
260 | } | |
261 | } | |
262 | fprintf(fp, "\n"); | |
263 | fflush(fp); | |
264 | return 0; | |
265 | } | |
266 | ||
267 | static int flush_update(void) | |
268 | { | |
269 | if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) { | |
270 | perror("Failed to send flush request\n"); | |
271 | return -1; | |
272 | } | |
273 | filter.flushp = 0; | |
274 | return 0; | |
275 | } | |
276 | ||
277 | int print_addrinfo(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) | |
278 | { | |
279 | FILE *fp = (FILE*)arg; | |
280 | struct ifaddrmsg *ifa = NLMSG_DATA(n); | |
281 | int len = n->nlmsg_len; | |
282 | struct rtattr * rta_tb[IFA_MAX+1]; | |
283 | char abuf[256]; | |
284 | SPRINT_BUF(b1); | |
285 | ||
286 | if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR) | |
287 | return 0; | |
288 | len -= NLMSG_LENGTH(sizeof(*ifa)); | |
289 | if (len < 0) { | |
290 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
291 | return -1; | |
292 | } | |
293 | ||
294 | if (filter.flushb && n->nlmsg_type != RTM_NEWADDR) | |
295 | return 0; | |
296 | ||
297 | memset(rta_tb, 0, sizeof(rta_tb)); | |
298 | parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa))); | |
299 | ||
300 | if (!rta_tb[IFA_LOCAL]) | |
301 | rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS]; | |
302 | if (!rta_tb[IFA_ADDRESS]) | |
303 | rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL]; | |
304 | ||
305 | if (filter.ifindex && filter.ifindex != ifa->ifa_index) | |
306 | return 0; | |
307 | if ((filter.scope^ifa->ifa_scope)&filter.scopemask) | |
308 | return 0; | |
309 | if ((filter.flags^ifa->ifa_flags)&filter.flagmask) | |
310 | return 0; | |
311 | if (filter.label) { | |
312 | SPRINT_BUF(b1); | |
313 | const char *label; | |
314 | if (rta_tb[IFA_LABEL]) | |
315 | label = RTA_DATA(rta_tb[IFA_LABEL]); | |
316 | else | |
317 | label = ll_idx_n2a(ifa->ifa_index, b1); | |
318 | if (fnmatch(filter.label, label, 0) != 0) | |
319 | return 0; | |
320 | } | |
321 | if (filter.pfx.family) { | |
322 | if (rta_tb[IFA_LOCAL]) { | |
323 | inet_prefix dst; | |
324 | memset(&dst, 0, sizeof(dst)); | |
325 | dst.family = ifa->ifa_family; | |
326 | memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL])); | |
327 | if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen)) | |
328 | return 0; | |
329 | } | |
330 | } | |
331 | ||
332 | if (filter.flushb) { | |
333 | struct nlmsghdr *fn; | |
334 | if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) { | |
335 | if (flush_update()) | |
336 | return -1; | |
337 | } | |
338 | fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp)); | |
339 | memcpy(fn, n, n->nlmsg_len); | |
340 | fn->nlmsg_type = RTM_DELADDR; | |
341 | fn->nlmsg_flags = NLM_F_REQUEST; | |
342 | fn->nlmsg_seq = ++filter.rth->seq; | |
343 | filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb; | |
344 | filter.flushed++; | |
345 | if (show_stats < 2) | |
346 | return 0; | |
347 | } | |
348 | ||
349 | if (n->nlmsg_type == RTM_DELADDR) | |
350 | fprintf(fp, "Deleted "); | |
351 | ||
352 | if (filter.oneline || filter.flushb) | |
353 | fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index)); | |
354 | if (ifa->ifa_family == AF_INET) | |
355 | fprintf(fp, " inet "); | |
356 | else if (ifa->ifa_family == AF_INET6) | |
357 | fprintf(fp, " inet6 "); | |
358 | else if (ifa->ifa_family == AF_DECnet) | |
359 | fprintf(fp, " dnet "); | |
360 | else if (ifa->ifa_family == AF_IPX) | |
361 | fprintf(fp, " ipx "); | |
362 | else | |
363 | fprintf(fp, " family %d ", ifa->ifa_family); | |
364 | ||
365 | if (rta_tb[IFA_LOCAL]) { | |
366 | fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family, | |
367 | RTA_PAYLOAD(rta_tb[IFA_LOCAL]), | |
368 | RTA_DATA(rta_tb[IFA_LOCAL]), | |
369 | abuf, sizeof(abuf))); | |
370 | ||
371 | if (rta_tb[IFA_ADDRESS] == NULL || | |
372 | memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) { | |
373 | fprintf(fp, "/%d ", ifa->ifa_prefixlen); | |
374 | } else { | |
375 | fprintf(fp, " peer %s/%d ", | |
376 | rt_addr_n2a(ifa->ifa_family, | |
377 | RTA_PAYLOAD(rta_tb[IFA_ADDRESS]), | |
378 | RTA_DATA(rta_tb[IFA_ADDRESS]), | |
379 | abuf, sizeof(abuf)), | |
380 | ifa->ifa_prefixlen); | |
381 | } | |
382 | } | |
383 | ||
384 | if (rta_tb[IFA_BROADCAST]) { | |
385 | fprintf(fp, "brd %s ", | |
386 | rt_addr_n2a(ifa->ifa_family, | |
387 | RTA_PAYLOAD(rta_tb[IFA_BROADCAST]), | |
388 | RTA_DATA(rta_tb[IFA_BROADCAST]), | |
389 | abuf, sizeof(abuf))); | |
390 | } | |
391 | if (rta_tb[IFA_ANYCAST]) { | |
392 | fprintf(fp, "any %s ", | |
393 | rt_addr_n2a(ifa->ifa_family, | |
394 | RTA_PAYLOAD(rta_tb[IFA_ANYCAST]), | |
395 | RTA_DATA(rta_tb[IFA_ANYCAST]), | |
396 | abuf, sizeof(abuf))); | |
397 | } | |
398 | fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1))); | |
399 | if (ifa->ifa_flags&IFA_F_SECONDARY) { | |
400 | ifa->ifa_flags &= ~IFA_F_SECONDARY; | |
401 | fprintf(fp, "secondary "); | |
402 | } | |
403 | if (ifa->ifa_flags&IFA_F_TENTATIVE) { | |
404 | ifa->ifa_flags &= ~IFA_F_TENTATIVE; | |
405 | fprintf(fp, "tentative "); | |
406 | } | |
407 | if (ifa->ifa_flags&IFA_F_DEPRECATED) { | |
408 | ifa->ifa_flags &= ~IFA_F_DEPRECATED; | |
409 | fprintf(fp, "deprecated "); | |
410 | } | |
411 | if (!(ifa->ifa_flags&IFA_F_PERMANENT)) { | |
412 | fprintf(fp, "dynamic "); | |
413 | } else | |
414 | ifa->ifa_flags &= ~IFA_F_PERMANENT; | |
415 | if (ifa->ifa_flags) | |
416 | fprintf(fp, "flags %02x ", ifa->ifa_flags); | |
417 | if (rta_tb[IFA_LABEL]) | |
418 | fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL])); | |
419 | if (rta_tb[IFA_CACHEINFO]) { | |
420 | struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]); | |
421 | char buf[128]; | |
422 | fprintf(fp, "%s", _SL_); | |
423 | if (ci->ifa_valid == 0xFFFFFFFFU) | |
424 | sprintf(buf, "valid_lft forever"); | |
425 | else | |
426 | sprintf(buf, "valid_lft %dsec", ci->ifa_valid); | |
427 | if (ci->ifa_prefered == 0xFFFFFFFFU) | |
428 | sprintf(buf+strlen(buf), " preferred_lft forever"); | |
429 | else | |
430 | sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered); | |
431 | fprintf(fp, " %s", buf); | |
432 | } | |
433 | fprintf(fp, "\n"); | |
434 | fflush(fp); | |
435 | return 0; | |
436 | } | |
437 | ||
438 | ||
439 | struct nlmsg_list | |
440 | { | |
441 | struct nlmsg_list *next; | |
442 | struct nlmsghdr h; | |
443 | }; | |
444 | ||
445 | int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp) | |
446 | { | |
447 | for ( ;ainfo ; ainfo = ainfo->next) { | |
448 | struct nlmsghdr *n = &ainfo->h; | |
449 | struct ifaddrmsg *ifa = NLMSG_DATA(n); | |
450 | ||
451 | if (n->nlmsg_type != RTM_NEWADDR) | |
452 | continue; | |
453 | ||
454 | if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa))) | |
455 | return -1; | |
456 | ||
457 | if (ifa->ifa_index != ifindex || | |
458 | (filter.family && filter.family != ifa->ifa_family)) | |
459 | continue; | |
460 | ||
461 | print_addrinfo(NULL, n, fp); | |
462 | } | |
463 | return 0; | |
464 | } | |
465 | ||
466 | ||
467 | int store_nlmsg(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) | |
468 | { | |
469 | struct nlmsg_list **linfo = (struct nlmsg_list**)arg; | |
470 | struct nlmsg_list *h; | |
471 | struct nlmsg_list **lp; | |
472 | ||
473 | h = malloc(n->nlmsg_len+sizeof(void*)); | |
474 | if (h == NULL) | |
475 | return -1; | |
476 | ||
477 | memcpy(&h->h, n, n->nlmsg_len); | |
478 | h->next = NULL; | |
479 | ||
480 | for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */; | |
481 | *lp = h; | |
482 | ||
483 | ll_remember_index(who, n, NULL); | |
484 | return 0; | |
485 | } | |
486 | ||
487 | int ipaddr_list_or_flush(int argc, char **argv, int flush) | |
488 | { | |
489 | struct nlmsg_list *linfo = NULL; | |
490 | struct nlmsg_list *ainfo = NULL; | |
491 | struct nlmsg_list *l; | |
492 | struct rtnl_handle rth; | |
493 | char *filter_dev = NULL; | |
494 | int no_link = 0; | |
495 | ||
496 | ipaddr_reset_filter(oneline); | |
497 | filter.showqueue = 1; | |
498 | ||
499 | if (filter.family == AF_UNSPEC) | |
500 | filter.family = preferred_family; | |
501 | ||
502 | if (flush) { | |
503 | if (argc <= 0) { | |
504 | fprintf(stderr, "Flush requires arguments.\n"); | |
505 | return -1; | |
506 | } | |
507 | if (filter.family == AF_PACKET) { | |
508 | fprintf(stderr, "Cannot flush link addresses.\n"); | |
509 | return -1; | |
510 | } | |
511 | } | |
512 | ||
513 | while (argc > 0) { | |
514 | if (strcmp(*argv, "to") == 0) { | |
515 | NEXT_ARG(); | |
516 | get_prefix(&filter.pfx, *argv, filter.family); | |
517 | if (filter.family == AF_UNSPEC) | |
518 | filter.family = filter.pfx.family; | |
519 | } else if (strcmp(*argv, "scope") == 0) { | |
520 | int scope = 0; | |
521 | NEXT_ARG(); | |
522 | filter.scopemask = -1; | |
523 | if (rtnl_rtscope_a2n(&scope, *argv)) { | |
524 | if (strcmp(*argv, "all") != 0) | |
525 | invarg("invalid \"scope\"\n", *argv); | |
526 | scope = RT_SCOPE_NOWHERE; | |
527 | filter.scopemask = 0; | |
528 | } | |
529 | filter.scope = scope; | |
530 | } else if (strcmp(*argv, "up") == 0) { | |
531 | filter.up = 1; | |
532 | } else if (strcmp(*argv, "dynamic") == 0) { | |
533 | filter.flags &= ~IFA_F_PERMANENT; | |
534 | filter.flagmask |= IFA_F_PERMANENT; | |
535 | } else if (strcmp(*argv, "permanent") == 0) { | |
536 | filter.flags |= IFA_F_PERMANENT; | |
537 | filter.flagmask |= IFA_F_PERMANENT; | |
538 | } else if (strcmp(*argv, "secondary") == 0) { | |
539 | filter.flags |= IFA_F_SECONDARY; | |
540 | filter.flagmask |= IFA_F_SECONDARY; | |
541 | } else if (strcmp(*argv, "primary") == 0) { | |
542 | filter.flags &= ~IFA_F_SECONDARY; | |
543 | filter.flagmask |= IFA_F_SECONDARY; | |
544 | } else if (strcmp(*argv, "tentative") == 0) { | |
545 | filter.flags |= IFA_F_TENTATIVE; | |
546 | filter.flagmask |= IFA_F_TENTATIVE; | |
547 | } else if (strcmp(*argv, "deprecated") == 0) { | |
548 | filter.flags |= IFA_F_DEPRECATED; | |
549 | filter.flagmask |= IFA_F_DEPRECATED; | |
550 | } else if (strcmp(*argv, "label") == 0) { | |
551 | NEXT_ARG(); | |
552 | filter.label = *argv; | |
553 | } else { | |
554 | if (strcmp(*argv, "dev") == 0) { | |
555 | NEXT_ARG(); | |
556 | } | |
557 | if (matches(*argv, "help") == 0) | |
558 | usage(); | |
559 | if (filter_dev) | |
560 | duparg2("dev", *argv); | |
561 | filter_dev = *argv; | |
562 | } | |
563 | argv++; argc--; | |
564 | } | |
565 | ||
566 | if (rtnl_open(&rth, 0) < 0) | |
567 | exit(1); | |
568 | ||
569 | if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) { | |
570 | perror("Cannot send dump request"); | |
571 | exit(1); | |
572 | } | |
573 | ||
574 | if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) { | |
575 | fprintf(stderr, "Dump terminated\n"); | |
576 | exit(1); | |
577 | } | |
578 | ||
579 | if (filter_dev) { | |
580 | filter.ifindex = ll_name_to_index(filter_dev); | |
581 | if (filter.ifindex <= 0) { | |
582 | fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev); | |
583 | return -1; | |
584 | } | |
585 | } | |
586 | ||
587 | if (flush) { | |
588 | int round = 0; | |
589 | char flushb[4096-512]; | |
590 | ||
591 | filter.flushb = flushb; | |
592 | filter.flushp = 0; | |
593 | filter.flushe = sizeof(flushb); | |
594 | filter.rth = &rth; | |
595 | ||
596 | for (;;) { | |
597 | if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) { | |
598 | perror("Cannot send dump request"); | |
599 | exit(1); | |
600 | } | |
601 | filter.flushed = 0; | |
602 | if (rtnl_dump_filter(&rth, print_addrinfo, stdout, NULL, NULL) < 0) { | |
603 | fprintf(stderr, "Flush terminated\n"); | |
604 | exit(1); | |
605 | } | |
606 | if (filter.flushed == 0) { | |
607 | if (round == 0) { | |
608 | fprintf(stderr, "Nothing to flush.\n"); | |
609 | } else if (show_stats) | |
610 | printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":""); | |
611 | fflush(stdout); | |
612 | return 0; | |
613 | } | |
614 | round++; | |
615 | if (flush_update() < 0) | |
616 | exit(1); | |
617 | if (show_stats) { | |
618 | printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed); | |
619 | fflush(stdout); | |
620 | } | |
621 | } | |
622 | } | |
623 | ||
624 | if (filter.family != AF_PACKET) { | |
625 | if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) { | |
626 | perror("Cannot send dump request"); | |
627 | exit(1); | |
628 | } | |
629 | ||
630 | if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) { | |
631 | fprintf(stderr, "Dump terminated\n"); | |
632 | exit(1); | |
633 | } | |
634 | } | |
635 | ||
636 | ||
637 | if (filter.family && filter.family != AF_PACKET) { | |
638 | struct nlmsg_list **lp; | |
639 | lp=&linfo; | |
640 | ||
641 | if (filter.oneline) | |
642 | no_link = 1; | |
643 | ||
644 | while ((l=*lp)!=NULL) { | |
645 | int ok = 0; | |
646 | struct ifinfomsg *ifi = NLMSG_DATA(&l->h); | |
647 | struct nlmsg_list *a; | |
648 | ||
649 | for (a=ainfo; a; a=a->next) { | |
650 | struct nlmsghdr *n = &a->h; | |
651 | struct ifaddrmsg *ifa = NLMSG_DATA(n); | |
652 | ||
653 | if (ifa->ifa_index != ifi->ifi_index || | |
654 | (filter.family && filter.family != ifa->ifa_family)) | |
655 | continue; | |
656 | if ((filter.scope^ifa->ifa_scope)&filter.scopemask) | |
657 | continue; | |
658 | if ((filter.flags^ifa->ifa_flags)&filter.flagmask) | |
659 | continue; | |
660 | if (filter.pfx.family || filter.label) { | |
661 | struct rtattr *tb[IFA_MAX+1]; | |
662 | memset(tb, 0, sizeof(tb)); | |
663 | parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n)); | |
664 | if (!tb[IFA_LOCAL]) | |
665 | tb[IFA_LOCAL] = tb[IFA_ADDRESS]; | |
666 | ||
667 | if (filter.pfx.family && tb[IFA_LOCAL]) { | |
668 | inet_prefix dst; | |
669 | memset(&dst, 0, sizeof(dst)); | |
670 | dst.family = ifa->ifa_family; | |
671 | memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL])); | |
672 | if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen)) | |
673 | continue; | |
674 | } | |
675 | if (filter.label) { | |
676 | SPRINT_BUF(b1); | |
677 | const char *label; | |
678 | if (tb[IFA_LABEL]) | |
679 | label = RTA_DATA(tb[IFA_LABEL]); | |
680 | else | |
681 | label = ll_idx_n2a(ifa->ifa_index, b1); | |
682 | if (fnmatch(filter.label, label, 0) != 0) | |
683 | continue; | |
684 | } | |
685 | } | |
686 | ||
687 | ok = 1; | |
688 | break; | |
689 | } | |
690 | if (!ok) | |
691 | *lp = l->next; | |
692 | else | |
693 | lp = &l->next; | |
694 | } | |
695 | } | |
696 | ||
697 | for (l=linfo; l; l = l->next) { | |
698 | if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) { | |
699 | struct ifinfomsg *ifi = NLMSG_DATA(&l->h); | |
700 | if (filter.family != AF_PACKET) | |
701 | print_selected_addrinfo(ifi->ifi_index, ainfo, stdout); | |
702 | } | |
703 | fflush(stdout); | |
704 | } | |
705 | ||
706 | exit(0); | |
707 | } | |
708 | ||
709 | int ipaddr_list_link(int argc, char **argv) | |
710 | { | |
711 | preferred_family = AF_PACKET; | |
712 | do_link = 1; | |
713 | return ipaddr_list_or_flush(argc, argv, 0); | |
714 | } | |
715 | ||
716 | void ipaddr_reset_filter(int oneline) | |
717 | { | |
718 | memset(&filter, 0, sizeof(filter)); | |
719 | filter.oneline = oneline; | |
720 | } | |
721 | ||
722 | int default_scope(inet_prefix *lcl) | |
723 | { | |
724 | if (lcl->family == AF_INET) { | |
725 | if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127) | |
726 | return RT_SCOPE_HOST; | |
727 | } | |
728 | return 0; | |
729 | } | |
730 | ||
731 | int ipaddr_modify(int cmd, int argc, char **argv) | |
732 | { | |
733 | struct rtnl_handle rth; | |
734 | struct { | |
735 | struct nlmsghdr n; | |
736 | struct ifaddrmsg ifa; | |
737 | char buf[256]; | |
738 | } req; | |
739 | char *d = NULL; | |
740 | char *l = NULL; | |
741 | inet_prefix lcl; | |
742 | inet_prefix peer; | |
743 | int local_len = 0; | |
744 | int peer_len = 0; | |
745 | int brd_len = 0; | |
746 | int any_len = 0; | |
747 | int scoped = 0; | |
748 | ||
749 | memset(&req, 0, sizeof(req)); | |
750 | ||
751 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); | |
752 | req.n.nlmsg_flags = NLM_F_REQUEST; | |
753 | req.n.nlmsg_type = cmd; | |
754 | req.ifa.ifa_family = preferred_family; | |
755 | ||
756 | while (argc > 0) { | |
757 | if (strcmp(*argv, "peer") == 0 || | |
758 | strcmp(*argv, "remote") == 0) { | |
759 | NEXT_ARG(); | |
760 | ||
761 | if (peer_len) | |
762 | duparg("peer", *argv); | |
763 | get_prefix(&peer, *argv, req.ifa.ifa_family); | |
764 | peer_len = peer.bytelen; | |
765 | if (req.ifa.ifa_family == AF_UNSPEC) | |
766 | req.ifa.ifa_family = peer.family; | |
767 | addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen); | |
768 | req.ifa.ifa_prefixlen = peer.bitlen; | |
769 | } else if (matches(*argv, "broadcast") == 0 || | |
770 | strcmp(*argv, "brd") == 0) { | |
771 | inet_prefix addr; | |
772 | NEXT_ARG(); | |
773 | if (brd_len) | |
774 | duparg("broadcast", *argv); | |
775 | if (strcmp(*argv, "+") == 0) | |
776 | brd_len = -1; | |
777 | else if (strcmp(*argv, "-") == 0) | |
778 | brd_len = -2; | |
779 | else { | |
780 | get_addr(&addr, *argv, req.ifa.ifa_family); | |
781 | if (req.ifa.ifa_family == AF_UNSPEC) | |
782 | req.ifa.ifa_family = addr.family; | |
783 | addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen); | |
784 | brd_len = addr.bytelen; | |
785 | } | |
786 | } else if (strcmp(*argv, "anycast") == 0) { | |
787 | inet_prefix addr; | |
788 | NEXT_ARG(); | |
789 | if (any_len) | |
790 | duparg("anycast", *argv); | |
791 | get_addr(&addr, *argv, req.ifa.ifa_family); | |
792 | if (req.ifa.ifa_family == AF_UNSPEC) | |
793 | req.ifa.ifa_family = addr.family; | |
794 | addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen); | |
795 | any_len = addr.bytelen; | |
796 | } else if (strcmp(*argv, "scope") == 0) { | |
797 | int scope = 0; | |
798 | NEXT_ARG(); | |
799 | if (rtnl_rtscope_a2n(&scope, *argv)) | |
800 | invarg(*argv, "invalid scope value."); | |
801 | req.ifa.ifa_scope = scope; | |
802 | scoped = 1; | |
803 | } else if (strcmp(*argv, "dev") == 0) { | |
804 | NEXT_ARG(); | |
805 | d = *argv; | |
806 | } else if (strcmp(*argv, "label") == 0) { | |
807 | NEXT_ARG(); | |
808 | l = *argv; | |
809 | addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1); | |
810 | } else { | |
811 | if (strcmp(*argv, "local") == 0) { | |
812 | NEXT_ARG(); | |
813 | } | |
814 | if (matches(*argv, "help") == 0) | |
815 | usage(); | |
816 | if (local_len) | |
817 | duparg2("local", *argv); | |
818 | get_prefix(&lcl, *argv, req.ifa.ifa_family); | |
819 | if (req.ifa.ifa_family == AF_UNSPEC) | |
820 | req.ifa.ifa_family = lcl.family; | |
821 | addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen); | |
822 | local_len = lcl.bytelen; | |
823 | } | |
824 | argc--; argv++; | |
825 | } | |
826 | if (d == NULL) { | |
827 | fprintf(stderr, "Not enough information: \"dev\" argument is required.\n"); | |
828 | return -1; | |
829 | } | |
830 | if (l && matches(d, l) != 0) { | |
831 | fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l); | |
832 | exit(1); | |
833 | } | |
834 | ||
835 | if (peer_len == 0 && local_len && cmd != RTM_DELADDR) { | |
836 | peer = lcl; | |
837 | addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen); | |
838 | } | |
839 | if (req.ifa.ifa_prefixlen == 0) | |
840 | req.ifa.ifa_prefixlen = lcl.bitlen; | |
841 | ||
842 | if (brd_len < 0 && cmd != RTM_DELADDR) { | |
843 | inet_prefix brd; | |
844 | int i; | |
845 | if (req.ifa.ifa_family != AF_INET) { | |
846 | fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n"); | |
847 | return -1; | |
848 | } | |
849 | brd = peer; | |
850 | if (brd.bitlen <= 30) { | |
851 | for (i=31; i>=brd.bitlen; i--) { | |
852 | if (brd_len == -1) | |
853 | brd.data[0] |= htonl(1<<(31-i)); | |
854 | else | |
855 | brd.data[0] &= ~htonl(1<<(31-i)); | |
856 | } | |
857 | addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen); | |
858 | brd_len = brd.bytelen; | |
859 | } | |
860 | } | |
861 | if (!scoped && cmd != RTM_DELADDR) | |
862 | req.ifa.ifa_scope = default_scope(&lcl); | |
863 | ||
864 | if (rtnl_open(&rth, 0) < 0) | |
865 | exit(1); | |
866 | ||
867 | ll_init_map(&rth); | |
868 | ||
869 | if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) { | |
870 | fprintf(stderr, "Cannot find device \"%s\"\n", d); | |
871 | return -1; | |
872 | } | |
873 | ||
874 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) | |
875 | exit(2); | |
876 | ||
877 | exit(0); | |
878 | } | |
879 | ||
880 | int do_ipaddr(int argc, char **argv) | |
881 | { | |
882 | if (argc < 1) | |
883 | return ipaddr_list_or_flush(0, NULL, 0); | |
884 | if (matches(*argv, "add") == 0) | |
885 | return ipaddr_modify(RTM_NEWADDR, argc-1, argv+1); | |
886 | if (matches(*argv, "delete") == 0) | |
887 | return ipaddr_modify(RTM_DELADDR, argc-1, argv+1); | |
888 | if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 | |
889 | || matches(*argv, "lst") == 0) | |
890 | return ipaddr_list_or_flush(argc-1, argv+1, 0); | |
891 | if (matches(*argv, "flush") == 0) | |
892 | return ipaddr_list_or_flush(argc-1, argv+1, 1); | |
893 | if (matches(*argv, "help") == 0) | |
894 | usage(); | |
895 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip address help\".\n", *argv); | |
896 | exit(-1); | |
897 | } | |
898 |