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