]>
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 | * | |
aba5acdf SH |
11 | */ |
12 | ||
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
15 | #include <unistd.h> | |
16 | #include <syslog.h> | |
e6e6fb5c | 17 | #include <inttypes.h> |
aba5acdf SH |
18 | #include <fcntl.h> |
19 | #include <sys/ioctl.h> | |
20 | #include <sys/socket.h> | |
21 | #include <sys/ioctl.h> | |
5bd9dd49 | 22 | #include <errno.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 | ||
81824ac2 PE |
37 | enum { |
38 | IPADD_LIST, | |
39 | IPADD_FLUSH, | |
40 | IPADD_SAVE, | |
41 | }; | |
7b3d366e | 42 | |
aba5acdf SH |
43 | static struct |
44 | { | |
45 | int ifindex; | |
46 | int family; | |
47 | int oneline; | |
48 | int showqueue; | |
49 | inet_prefix pfx; | |
50 | int scope, scopemask; | |
51 | int flags, flagmask; | |
52 | int up; | |
53 | char *label; | |
54 | int flushed; | |
55 | char *flushb; | |
56 | int flushp; | |
57 | int flushe; | |
f960c92a | 58 | int group; |
aba5acdf SH |
59 | } filter; |
60 | ||
61 | static int do_link; | |
62 | ||
63 | static void usage(void) __attribute__((noreturn)); | |
64 | ||
65 | static void usage(void) | |
66 | { | |
67 | if (do_link) { | |
68 | iplink_usage(); | |
69 | } | |
0aef366b | 70 | fprintf(stderr, "Usage: ip addr {add|change|replace} IFADDR dev STRING [ LIFETIME ]\n"); |
a1f27794 | 71 | fprintf(stderr, " [ CONFFLAG-LIST ]\n"); |
35546df7 | 72 | fprintf(stderr, " ip addr del IFADDR dev STRING\n"); |
81824ac2 | 73 | fprintf(stderr, " ip addr {show|save|flush} [ dev STRING ] [ scope SCOPE-ID ]\n"); |
aba5acdf | 74 | fprintf(stderr, " [ to PREFIX ] [ FLAG-LIST ] [ label PATTERN ]\n"); |
81824ac2 | 75 | fprintf(stderr, " ip addr {showdump|restore}\n"); |
aba5acdf SH |
76 | fprintf(stderr, "IFADDR := PREFIX | ADDR peer PREFIX\n"); |
77 | fprintf(stderr, " [ broadcast ADDR ] [ anycast ADDR ]\n"); | |
78 | fprintf(stderr, " [ label STRING ] [ scope SCOPE-ID ]\n"); | |
79 | fprintf(stderr, "SCOPE-ID := [ host | link | global | NUMBER ]\n"); | |
80 | fprintf(stderr, "FLAG-LIST := [ FLAG-LIST ] FLAG\n"); | |
81 | fprintf(stderr, "FLAG := [ permanent | dynamic | secondary | primary |\n"); | |
a1b9ffcc | 82 | fprintf(stderr, " tentative | deprecated | dadfailed | temporary |\n"); |
a1f27794 | 83 | fprintf(stderr, " CONFFLAG-LIST ]\n"); |
bac735c5 NT |
84 | fprintf(stderr, "CONFFLAG-LIST := [ CONFFLAG-LIST ] CONFFLAG\n"); |
85 | fprintf(stderr, "CONFFLAG := [ home | nodad ]\n"); | |
35546df7 MN |
86 | fprintf(stderr, "LIFETIME := [ valid_lft LFT ] [ preferred_lft LFT ]\n"); |
87 | fprintf(stderr, "LFT := forever | SECONDS\n"); | |
88 | ||
aba5acdf SH |
89 | exit(-1); |
90 | } | |
91 | ||
92 | void print_link_flags(FILE *fp, unsigned flags, unsigned mdown) | |
93 | { | |
94 | fprintf(fp, "<"); | |
73b49e9f | 95 | if (flags & IFF_UP && !(flags & IFF_RUNNING)) |
96 | fprintf(fp, "NO-CARRIER%s", flags ? "," : ""); | |
aba5acdf SH |
97 | flags &= ~IFF_RUNNING; |
98 | #define _PF(f) if (flags&IFF_##f) { \ | |
99 | flags &= ~IFF_##f ; \ | |
100 | fprintf(fp, #f "%s", flags ? "," : ""); } | |
101 | _PF(LOOPBACK); | |
102 | _PF(BROADCAST); | |
103 | _PF(POINTOPOINT); | |
104 | _PF(MULTICAST); | |
105 | _PF(NOARP); | |
106 | _PF(ALLMULTI); | |
107 | _PF(PROMISC); | |
108 | _PF(MASTER); | |
109 | _PF(SLAVE); | |
110 | _PF(DEBUG); | |
111 | _PF(DYNAMIC); | |
112 | _PF(AUTOMEDIA); | |
113 | _PF(PORTSEL); | |
114 | _PF(NOTRAILERS); | |
115 | _PF(UP); | |
dcb283c3 TG |
116 | _PF(LOWER_UP); |
117 | _PF(DORMANT); | |
98f9a1d2 | 118 | _PF(ECHO); |
aba5acdf SH |
119 | #undef _PF |
120 | if (flags) | |
121 | fprintf(fp, "%x", flags); | |
122 | if (mdown) | |
123 | fprintf(fp, ",M-DOWN"); | |
124 | fprintf(fp, "> "); | |
125 | } | |
126 | ||
3d866ba2 SH |
127 | static const char *oper_states[] = { |
128 | "UNKNOWN", "NOTPRESENT", "DOWN", "LOWERLAYERDOWN", | |
129 | "TESTING", "DORMANT", "UP" | |
130 | }; | |
131 | ||
132 | static void print_operstate(FILE *f, __u8 state) | |
133 | { | |
134 | if (state >= sizeof(oper_states)/sizeof(oper_states[0])) | |
135 | fprintf(f, "state %#x ", state); | |
136 | else | |
137 | fprintf(f, "state %s ", oper_states[state]); | |
138 | } | |
139 | ||
4f2fdd44 SH |
140 | int get_operstate(const char *name) |
141 | { | |
142 | int i; | |
143 | ||
144 | for (i = 0; i < sizeof(oper_states)/sizeof(oper_states[0]); i++) | |
145 | if (strcasecmp(name, oper_states[i]) == 0) | |
146 | return i; | |
147 | return -1; | |
148 | } | |
149 | ||
f78e316f | 150 | static void print_queuelen(FILE *f, struct rtattr *tb[IFLA_MAX + 1]) |
aba5acdf | 151 | { |
f78e316f ED |
152 | int qlen; |
153 | ||
154 | if (tb[IFLA_TXQLEN]) | |
155 | qlen = *(int *)RTA_DATA(tb[IFLA_TXQLEN]); | |
156 | else { | |
157 | struct ifreq ifr; | |
158 | int s = socket(AF_INET, SOCK_STREAM, 0); | |
159 | ||
160 | if (s < 0) | |
161 | return; | |
162 | ||
163 | memset(&ifr, 0, sizeof(ifr)); | |
ff24746c | 164 | strcpy(ifr.ifr_name, rta_getattr_str(tb[IFLA_IFNAME])); |
f78e316f ED |
165 | if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) { |
166 | fprintf(f, "ioctl(SIOCGIFXQLEN) failed: %s\n", strerror(errno)); | |
167 | close(s); | |
168 | return; | |
169 | } | |
aba5acdf | 170 | close(s); |
f78e316f | 171 | qlen = ifr.ifr_qlen; |
aba5acdf | 172 | } |
f78e316f ED |
173 | if (qlen) |
174 | fprintf(f, "qlen %d", qlen); | |
aba5acdf SH |
175 | } |
176 | ||
82499282 SH |
177 | static const char *link_modes[] = { |
178 | "DEFAULT", "DORMANT" | |
179 | }; | |
180 | ||
181 | static void print_linkmode(FILE *f, struct rtattr *tb) | |
182 | { | |
183 | unsigned int mode = rta_getattr_u8(tb); | |
184 | ||
185 | if (mode >= sizeof(link_modes) / sizeof(link_modes[0])) | |
186 | fprintf(f, "mode %d ", mode); | |
187 | else | |
188 | fprintf(f, "mode %s ", link_modes[mode]); | |
189 | } | |
190 | ||
1d934839 PM |
191 | static void print_linktype(FILE *fp, struct rtattr *tb) |
192 | { | |
193 | struct rtattr *linkinfo[IFLA_INFO_MAX+1]; | |
194 | struct link_util *lu; | |
195 | char *kind; | |
196 | ||
197 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb); | |
198 | ||
199 | if (!linkinfo[IFLA_INFO_KIND]) | |
200 | return; | |
201 | kind = RTA_DATA(linkinfo[IFLA_INFO_KIND]); | |
202 | ||
203 | fprintf(fp, "%s", _SL_); | |
204 | fprintf(fp, " %s ", kind); | |
205 | ||
206 | lu = get_link_kind(kind); | |
207 | if (!lu || !lu->print_opt) | |
208 | return; | |
209 | ||
210 | if (1) { | |
211 | struct rtattr *attr[lu->maxattr+1], **data = NULL; | |
212 | ||
213 | if (linkinfo[IFLA_INFO_DATA]) { | |
214 | parse_rtattr_nested(attr, lu->maxattr, | |
215 | linkinfo[IFLA_INFO_DATA]); | |
216 | data = attr; | |
217 | } | |
218 | lu->print_opt(lu, fp, data); | |
219 | ||
220 | if (linkinfo[IFLA_INFO_XSTATS] && show_stats && | |
221 | lu->print_xstats) | |
222 | lu->print_xstats(lu, fp, linkinfo[IFLA_INFO_XSTATS]); | |
223 | } | |
224 | } | |
225 | ||
3fd86630 CW |
226 | static void print_vfinfo(FILE *fp, struct rtattr *vfinfo) |
227 | { | |
228 | struct ifla_vf_mac *vf_mac; | |
229 | struct ifla_vf_vlan *vf_vlan; | |
230 | struct ifla_vf_tx_rate *vf_tx_rate; | |
7b8179c7 | 231 | struct ifla_vf_spoofchk *vf_spoofchk; |
3fd86630 | 232 | struct rtattr *vf[IFLA_VF_MAX+1]; |
7b8179c7 | 233 | struct rtattr *tmp; |
3fd86630 CW |
234 | SPRINT_BUF(b1); |
235 | ||
236 | if (vfinfo->rta_type != IFLA_VF_INFO) { | |
237 | fprintf(stderr, "BUG: rta type is %d\n", vfinfo->rta_type); | |
238 | return; | |
239 | } | |
240 | ||
241 | parse_rtattr_nested(vf, IFLA_VF_MAX, vfinfo); | |
242 | ||
243 | vf_mac = RTA_DATA(vf[IFLA_VF_MAC]); | |
244 | vf_vlan = RTA_DATA(vf[IFLA_VF_VLAN]); | |
245 | vf_tx_rate = RTA_DATA(vf[IFLA_VF_TX_RATE]); | |
246 | ||
7b8179c7 GR |
247 | /* Check if the spoof checking vf info type is supported by |
248 | * this kernel. | |
249 | */ | |
250 | tmp = (struct rtattr *)((char *)vf[IFLA_VF_TX_RATE] + | |
251 | vf[IFLA_VF_TX_RATE]->rta_len); | |
252 | ||
253 | if (tmp->rta_type != IFLA_VF_SPOOFCHK) | |
254 | vf_spoofchk = NULL; | |
255 | else | |
256 | vf_spoofchk = RTA_DATA(vf[IFLA_VF_SPOOFCHK]); | |
257 | ||
3fd86630 CW |
258 | fprintf(fp, "\n vf %d MAC %s", vf_mac->vf, |
259 | ll_addr_n2a((unsigned char *)&vf_mac->mac, | |
260 | ETH_ALEN, 0, b1, sizeof(b1))); | |
261 | if (vf_vlan->vlan) | |
262 | fprintf(fp, ", vlan %d", vf_vlan->vlan); | |
263 | if (vf_vlan->qos) | |
264 | fprintf(fp, ", qos %d", vf_vlan->qos); | |
265 | if (vf_tx_rate->rate) | |
266 | fprintf(fp, ", tx rate %d (Mbps)", vf_tx_rate->rate); | |
7b8179c7 GR |
267 | if (vf_spoofchk && vf_spoofchk->setting != -1) { |
268 | if (vf_spoofchk->setting) | |
269 | fprintf(fp, ", spoof checking on"); | |
270 | else | |
271 | fprintf(fp, ", spoof checking off"); | |
272 | } | |
3fd86630 CW |
273 | } |
274 | ||
e6e6fb5c SH |
275 | static void print_link_stats64(FILE *fp, const struct rtnl_link_stats64 *s) { |
276 | fprintf(fp, "%s", _SL_); | |
277 | fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s", | |
278 | s->rx_compressed ? "compressed" : "", _SL_); | |
279 | fprintf(fp, " %-10"PRIu64" %-8"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"", | |
280 | (uint64_t)s->rx_bytes, | |
281 | (uint64_t)s->rx_packets, | |
282 | (uint64_t)s->rx_errors, | |
283 | (uint64_t)s->rx_dropped, | |
284 | (uint64_t)s->rx_over_errors, | |
285 | (uint64_t)s->multicast); | |
286 | if (s->rx_compressed) | |
287 | fprintf(fp, " %-7"PRIu64"", | |
288 | (uint64_t)s->rx_compressed); | |
289 | if (show_stats > 1) { | |
290 | fprintf(fp, "%s", _SL_); | |
291 | fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_); | |
292 | fprintf(fp, " %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"", | |
293 | (uint64_t)s->rx_length_errors, | |
294 | (uint64_t)s->rx_crc_errors, | |
295 | (uint64_t)s->rx_frame_errors, | |
296 | (uint64_t)s->rx_fifo_errors, | |
297 | (uint64_t)s->rx_missed_errors); | |
298 | } | |
299 | fprintf(fp, "%s", _SL_); | |
300 | fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s", | |
301 | (uint64_t)s->tx_compressed ? "compressed" : "", _SL_); | |
302 | fprintf(fp, " %-10"PRIu64" %-8"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"", | |
303 | (uint64_t)s->tx_bytes, | |
304 | (uint64_t)s->tx_packets, | |
305 | (uint64_t)s->tx_errors, | |
306 | (uint64_t)s->tx_dropped, | |
307 | (uint64_t)s->tx_carrier_errors, | |
308 | (uint64_t)s->collisions); | |
309 | if (s->tx_compressed) | |
310 | fprintf(fp, " %-7"PRIu64"", | |
311 | (uint64_t)s->tx_compressed); | |
312 | if (show_stats > 1) { | |
313 | fprintf(fp, "%s", _SL_); | |
314 | fprintf(fp, " TX errors: aborted fifo window heartbeat%s", _SL_); | |
315 | fprintf(fp, " %-7"PRIu64" %-7"PRIu64" %-7"PRIu64" %-7"PRIu64"", | |
316 | (uint64_t)s->tx_aborted_errors, | |
317 | (uint64_t)s->tx_fifo_errors, | |
318 | (uint64_t)s->tx_window_errors, | |
319 | (uint64_t)s->tx_heartbeat_errors); | |
320 | } | |
321 | } | |
322 | ||
323 | static void print_link_stats(FILE *fp, const struct rtnl_link_stats *s) | |
324 | { | |
325 | fprintf(fp, "%s", _SL_); | |
326 | fprintf(fp, " RX: bytes packets errors dropped overrun mcast %s%s", | |
327 | s->rx_compressed ? "compressed" : "", _SL_); | |
328 | fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u", | |
329 | s->rx_bytes, s->rx_packets, s->rx_errors, | |
330 | s->rx_dropped, s->rx_over_errors, | |
331 | s->multicast | |
332 | ); | |
333 | if (s->rx_compressed) | |
334 | fprintf(fp, " %-7u", s->rx_compressed); | |
335 | if (show_stats > 1) { | |
336 | fprintf(fp, "%s", _SL_); | |
337 | fprintf(fp, " RX errors: length crc frame fifo missed%s", _SL_); | |
338 | fprintf(fp, " %-7u %-7u %-7u %-7u %-7u", | |
339 | s->rx_length_errors, | |
340 | s->rx_crc_errors, | |
341 | s->rx_frame_errors, | |
342 | s->rx_fifo_errors, | |
343 | s->rx_missed_errors | |
344 | ); | |
345 | } | |
346 | fprintf(fp, "%s", _SL_); | |
347 | fprintf(fp, " TX: bytes packets errors dropped carrier collsns %s%s", | |
348 | s->tx_compressed ? "compressed" : "", _SL_); | |
349 | fprintf(fp, " %-10u %-8u %-7u %-7u %-7u %-7u", | |
350 | s->tx_bytes, s->tx_packets, s->tx_errors, | |
351 | s->tx_dropped, s->tx_carrier_errors, s->collisions); | |
352 | if (s->tx_compressed) | |
353 | fprintf(fp, " %-7u", s->tx_compressed); | |
354 | if (show_stats > 1) { | |
355 | fprintf(fp, "%s", _SL_); | |
356 | fprintf(fp, " TX errors: aborted fifo window heartbeat%s", _SL_); | |
357 | fprintf(fp, " %-7u %-7u %-7u %-7u", | |
358 | s->tx_aborted_errors, | |
359 | s->tx_fifo_errors, | |
360 | s->tx_window_errors, | |
361 | s->tx_heartbeat_errors | |
362 | ); | |
363 | } | |
364 | } | |
365 | ||
ae665a52 | 366 | int print_linkinfo(const struct sockaddr_nl *who, |
50772dc5 | 367 | struct nlmsghdr *n, void *arg) |
aba5acdf SH |
368 | { |
369 | FILE *fp = (FILE*)arg; | |
370 | struct ifinfomsg *ifi = NLMSG_DATA(n); | |
371 | struct rtattr * tb[IFLA_MAX+1]; | |
372 | int len = n->nlmsg_len; | |
373 | unsigned m_flag = 0; | |
374 | ||
375 | if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK) | |
376 | return 0; | |
377 | ||
378 | len -= NLMSG_LENGTH(sizeof(*ifi)); | |
379 | if (len < 0) | |
380 | return -1; | |
381 | ||
382 | if (filter.ifindex && ifi->ifi_index != filter.ifindex) | |
383 | return 0; | |
384 | if (filter.up && !(ifi->ifi_flags&IFF_UP)) | |
385 | return 0; | |
386 | ||
aba5acdf SH |
387 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); |
388 | if (tb[IFLA_IFNAME] == NULL) { | |
4cd23bdd | 389 | fprintf(stderr, "BUG: device with ifindex %d has nil ifname\n", ifi->ifi_index); |
aba5acdf SH |
390 | } |
391 | if (filter.label && | |
392 | (!filter.family || filter.family == AF_PACKET) && | |
393 | fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)) | |
394 | return 0; | |
395 | ||
f960c92a VD |
396 | if (tb[IFLA_GROUP]) { |
397 | int group = *(int*)RTA_DATA(tb[IFLA_GROUP]); | |
398 | if (group != filter.group) | |
399 | return -1; | |
400 | } | |
401 | ||
aba5acdf SH |
402 | if (n->nlmsg_type == RTM_DELLINK) |
403 | fprintf(fp, "Deleted "); | |
404 | ||
405 | fprintf(fp, "%d: %s", ifi->ifi_index, | |
ff24746c | 406 | tb[IFLA_IFNAME] ? rta_getattr_str(tb[IFLA_IFNAME]) : "<nil>"); |
aba5acdf SH |
407 | |
408 | if (tb[IFLA_LINK]) { | |
409 | SPRINT_BUF(b1); | |
410 | int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]); | |
411 | if (iflink == 0) | |
412 | fprintf(fp, "@NONE: "); | |
413 | else { | |
414 | fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1)); | |
415 | m_flag = ll_index_to_flags(iflink); | |
416 | m_flag = !(m_flag & IFF_UP); | |
417 | } | |
418 | } else { | |
419 | fprintf(fp, ": "); | |
420 | } | |
421 | print_link_flags(fp, ifi->ifi_flags, m_flag); | |
422 | ||
423 | if (tb[IFLA_MTU]) | |
424 | fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU])); | |
425 | if (tb[IFLA_QDISC]) | |
ff24746c | 426 | fprintf(fp, "qdisc %s ", rta_getattr_str(tb[IFLA_QDISC])); |
aba5acdf SH |
427 | if (tb[IFLA_MASTER]) { |
428 | SPRINT_BUF(b1); | |
429 | fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1)); | |
430 | } | |
82499282 | 431 | |
3d866ba2 | 432 | if (tb[IFLA_OPERSTATE]) |
ff24746c | 433 | print_operstate(fp, rta_getattr_u8(tb[IFLA_OPERSTATE])); |
82499282 SH |
434 | |
435 | if (do_link && tb[IFLA_LINKMODE]) | |
436 | print_linkmode(fp, tb[IFLA_LINKMODE]); | |
437 | ||
aba5acdf | 438 | if (filter.showqueue) |
f78e316f | 439 | print_queuelen(fp, tb); |
ae665a52 | 440 | |
aba5acdf SH |
441 | if (!filter.family || filter.family == AF_PACKET) { |
442 | SPRINT_BUF(b1); | |
443 | fprintf(fp, "%s", _SL_); | |
444 | fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1))); | |
445 | ||
446 | if (tb[IFLA_ADDRESS]) { | |
447 | fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]), | |
448 | RTA_PAYLOAD(tb[IFLA_ADDRESS]), | |
449 | ifi->ifi_type, | |
450 | b1, sizeof(b1))); | |
451 | } | |
452 | if (tb[IFLA_BROADCAST]) { | |
453 | if (ifi->ifi_flags&IFF_POINTOPOINT) | |
454 | fprintf(fp, " peer "); | |
455 | else | |
456 | fprintf(fp, " brd "); | |
457 | fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]), | |
458 | RTA_PAYLOAD(tb[IFLA_BROADCAST]), | |
459 | ifi->ifi_type, | |
460 | b1, sizeof(b1))); | |
461 | } | |
462 | } | |
1d934839 PM |
463 | |
464 | if (do_link && tb[IFLA_LINKINFO] && show_details) | |
465 | print_linktype(fp, tb[IFLA_LINKINFO]); | |
466 | ||
ace9c961 SH |
467 | if (do_link && tb[IFLA_IFALIAS]) |
468 | fprintf(fp,"\n alias %s", | |
ff24746c | 469 | rta_getattr_str(tb[IFLA_IFALIAS])); |
ace9c961 | 470 | |
e6e6fb5c SH |
471 | if (do_link && show_stats) { |
472 | if (tb[IFLA_STATS64]) | |
473 | print_link_stats64(fp, RTA_DATA(tb[IFLA_STATS64])); | |
474 | else if (tb[IFLA_STATS]) | |
475 | print_link_stats(fp, RTA_DATA(tb[IFLA_STATS])); | |
ae7229d5 | 476 | } |
e6e6fb5c | 477 | |
3fd86630 CW |
478 | if (do_link && tb[IFLA_VFINFO_LIST] && tb[IFLA_NUM_VF]) { |
479 | struct rtattr *i, *vflist = tb[IFLA_VFINFO_LIST]; | |
480 | int rem = RTA_PAYLOAD(vflist); | |
481 | for (i = RTA_DATA(vflist); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) | |
482 | print_vfinfo(fp, i); | |
aba5acdf | 483 | } |
3fd86630 | 484 | |
aba5acdf SH |
485 | fprintf(fp, "\n"); |
486 | fflush(fp); | |
487 | return 0; | |
488 | } | |
489 | ||
490 | static int flush_update(void) | |
491 | { | |
f31a37f7 | 492 | if (rtnl_send_check(&rth, filter.flushb, filter.flushp) < 0) { |
1fb0a998 | 493 | perror("Failed to send flush request"); |
aba5acdf SH |
494 | return -1; |
495 | } | |
496 | filter.flushp = 0; | |
497 | return 0; | |
498 | } | |
499 | ||
35546df7 MN |
500 | static int set_lifetime(unsigned int *lifetime, char *argv) |
501 | { | |
502 | if (strcmp(argv, "forever") == 0) | |
141bb606 | 503 | *lifetime = INFINITY_LIFE_TIME; |
35546df7 MN |
504 | else if (get_u32(lifetime, argv, 0)) |
505 | return -1; | |
506 | ||
507 | return 0; | |
508 | } | |
509 | ||
ae665a52 | 510 | int print_addrinfo(const struct sockaddr_nl *who, struct nlmsghdr *n, |
6dc9f016 | 511 | void *arg) |
aba5acdf SH |
512 | { |
513 | FILE *fp = (FILE*)arg; | |
514 | struct ifaddrmsg *ifa = NLMSG_DATA(n); | |
515 | int len = n->nlmsg_len; | |
037d950b | 516 | int deprecated = 0; |
3bc1c4f2 BG |
517 | /* Use local copy of ifa_flags to not interfere with filtering code */ |
518 | unsigned int ifa_flags; | |
aba5acdf SH |
519 | struct rtattr * rta_tb[IFA_MAX+1]; |
520 | char abuf[256]; | |
521 | SPRINT_BUF(b1); | |
522 | ||
523 | if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR) | |
524 | return 0; | |
525 | len -= NLMSG_LENGTH(sizeof(*ifa)); | |
526 | if (len < 0) { | |
527 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
528 | return -1; | |
529 | } | |
530 | ||
531 | if (filter.flushb && n->nlmsg_type != RTM_NEWADDR) | |
532 | return 0; | |
533 | ||
aba5acdf SH |
534 | parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa))); |
535 | ||
536 | if (!rta_tb[IFA_LOCAL]) | |
537 | rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS]; | |
538 | if (!rta_tb[IFA_ADDRESS]) | |
539 | rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL]; | |
540 | ||
541 | if (filter.ifindex && filter.ifindex != ifa->ifa_index) | |
542 | return 0; | |
543 | if ((filter.scope^ifa->ifa_scope)&filter.scopemask) | |
544 | return 0; | |
545 | if ((filter.flags^ifa->ifa_flags)&filter.flagmask) | |
546 | return 0; | |
547 | if (filter.label) { | |
548 | SPRINT_BUF(b1); | |
549 | const char *label; | |
550 | if (rta_tb[IFA_LABEL]) | |
551 | label = RTA_DATA(rta_tb[IFA_LABEL]); | |
552 | else | |
553 | label = ll_idx_n2a(ifa->ifa_index, b1); | |
554 | if (fnmatch(filter.label, label, 0) != 0) | |
555 | return 0; | |
556 | } | |
557 | if (filter.pfx.family) { | |
558 | if (rta_tb[IFA_LOCAL]) { | |
559 | inet_prefix dst; | |
560 | memset(&dst, 0, sizeof(dst)); | |
561 | dst.family = ifa->ifa_family; | |
562 | memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL])); | |
563 | if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen)) | |
564 | return 0; | |
565 | } | |
566 | } | |
567 | ||
3eb1731b | 568 | if (filter.family && filter.family != ifa->ifa_family) |
569 | return 0; | |
570 | ||
aba5acdf SH |
571 | if (filter.flushb) { |
572 | struct nlmsghdr *fn; | |
573 | if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) { | |
574 | if (flush_update()) | |
575 | return -1; | |
576 | } | |
577 | fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp)); | |
578 | memcpy(fn, n, n->nlmsg_len); | |
579 | fn->nlmsg_type = RTM_DELADDR; | |
580 | fn->nlmsg_flags = NLM_F_REQUEST; | |
351efcde | 581 | fn->nlmsg_seq = ++rth.seq; |
aba5acdf SH |
582 | filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb; |
583 | filter.flushed++; | |
584 | if (show_stats < 2) | |
585 | return 0; | |
586 | } | |
587 | ||
588 | if (n->nlmsg_type == RTM_DELADDR) | |
589 | fprintf(fp, "Deleted "); | |
590 | ||
591 | if (filter.oneline || filter.flushb) | |
592 | fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index)); | |
593 | if (ifa->ifa_family == AF_INET) | |
594 | fprintf(fp, " inet "); | |
595 | else if (ifa->ifa_family == AF_INET6) | |
596 | fprintf(fp, " inet6 "); | |
597 | else if (ifa->ifa_family == AF_DECnet) | |
598 | fprintf(fp, " dnet "); | |
599 | else if (ifa->ifa_family == AF_IPX) | |
600 | fprintf(fp, " ipx "); | |
601 | else | |
602 | fprintf(fp, " family %d ", ifa->ifa_family); | |
603 | ||
604 | if (rta_tb[IFA_LOCAL]) { | |
605 | fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family, | |
606 | RTA_PAYLOAD(rta_tb[IFA_LOCAL]), | |
607 | RTA_DATA(rta_tb[IFA_LOCAL]), | |
608 | abuf, sizeof(abuf))); | |
609 | ||
610 | if (rta_tb[IFA_ADDRESS] == NULL || | |
611 | memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) { | |
612 | fprintf(fp, "/%d ", ifa->ifa_prefixlen); | |
613 | } else { | |
614 | fprintf(fp, " peer %s/%d ", | |
615 | rt_addr_n2a(ifa->ifa_family, | |
616 | RTA_PAYLOAD(rta_tb[IFA_ADDRESS]), | |
617 | RTA_DATA(rta_tb[IFA_ADDRESS]), | |
618 | abuf, sizeof(abuf)), | |
619 | ifa->ifa_prefixlen); | |
620 | } | |
621 | } | |
622 | ||
623 | if (rta_tb[IFA_BROADCAST]) { | |
624 | fprintf(fp, "brd %s ", | |
625 | rt_addr_n2a(ifa->ifa_family, | |
626 | RTA_PAYLOAD(rta_tb[IFA_BROADCAST]), | |
627 | RTA_DATA(rta_tb[IFA_BROADCAST]), | |
628 | abuf, sizeof(abuf))); | |
629 | } | |
630 | if (rta_tb[IFA_ANYCAST]) { | |
631 | fprintf(fp, "any %s ", | |
632 | rt_addr_n2a(ifa->ifa_family, | |
633 | RTA_PAYLOAD(rta_tb[IFA_ANYCAST]), | |
634 | RTA_DATA(rta_tb[IFA_ANYCAST]), | |
635 | abuf, sizeof(abuf))); | |
636 | } | |
637 | fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1))); | |
3bc1c4f2 | 638 | ifa_flags = ifa->ifa_flags; |
aba5acdf | 639 | if (ifa->ifa_flags&IFA_F_SECONDARY) { |
3bc1c4f2 | 640 | ifa_flags &= ~IFA_F_SECONDARY; |
a1b9ffcc BH |
641 | if (ifa->ifa_family == AF_INET6) |
642 | fprintf(fp, "temporary "); | |
643 | else | |
644 | fprintf(fp, "secondary "); | |
aba5acdf SH |
645 | } |
646 | if (ifa->ifa_flags&IFA_F_TENTATIVE) { | |
3bc1c4f2 | 647 | ifa_flags &= ~IFA_F_TENTATIVE; |
aba5acdf SH |
648 | fprintf(fp, "tentative "); |
649 | } | |
650 | if (ifa->ifa_flags&IFA_F_DEPRECATED) { | |
3bc1c4f2 | 651 | ifa_flags &= ~IFA_F_DEPRECATED; |
037d950b | 652 | deprecated = 1; |
aba5acdf SH |
653 | fprintf(fp, "deprecated "); |
654 | } | |
bac735c5 | 655 | if (ifa->ifa_flags&IFA_F_HOMEADDRESS) { |
3bc1c4f2 | 656 | ifa_flags &= ~IFA_F_HOMEADDRESS; |
bac735c5 NT |
657 | fprintf(fp, "home "); |
658 | } | |
659 | if (ifa->ifa_flags&IFA_F_NODAD) { | |
3bc1c4f2 | 660 | ifa_flags &= ~IFA_F_NODAD; |
bac735c5 NT |
661 | fprintf(fp, "nodad "); |
662 | } | |
aba5acdf SH |
663 | if (!(ifa->ifa_flags&IFA_F_PERMANENT)) { |
664 | fprintf(fp, "dynamic "); | |
665 | } else | |
3bc1c4f2 | 666 | ifa_flags &= ~IFA_F_PERMANENT; |
f4af851b | 667 | if (ifa->ifa_flags&IFA_F_DADFAILED) { |
3bc1c4f2 | 668 | ifa_flags &= ~IFA_F_DADFAILED; |
f4af851b BH |
669 | fprintf(fp, "dadfailed "); |
670 | } | |
3bc1c4f2 BG |
671 | if (ifa_flags) |
672 | fprintf(fp, "flags %02x ", ifa_flags); | |
aba5acdf | 673 | if (rta_tb[IFA_LABEL]) |
ff24746c | 674 | fprintf(fp, "%s", rta_getattr_str(rta_tb[IFA_LABEL])); |
aba5acdf SH |
675 | if (rta_tb[IFA_CACHEINFO]) { |
676 | struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]); | |
aba5acdf | 677 | fprintf(fp, "%s", _SL_); |
f66efadd | 678 | fprintf(fp, " valid_lft "); |
141bb606 | 679 | if (ci->ifa_valid == INFINITY_LIFE_TIME) |
f66efadd | 680 | fprintf(fp, "forever"); |
aba5acdf | 681 | else |
f66efadd AS |
682 | fprintf(fp, "%usec", ci->ifa_valid); |
683 | fprintf(fp, " preferred_lft "); | |
141bb606 | 684 | if (ci->ifa_prefered == INFINITY_LIFE_TIME) |
f66efadd | 685 | fprintf(fp, "forever"); |
037d950b BG |
686 | else { |
687 | if (deprecated) | |
f66efadd | 688 | fprintf(fp, "%dsec", ci->ifa_prefered); |
037d950b | 689 | else |
f66efadd | 690 | fprintf(fp, "%usec", ci->ifa_prefered); |
037d950b | 691 | } |
aba5acdf SH |
692 | } |
693 | fprintf(fp, "\n"); | |
694 | fflush(fp); | |
695 | return 0; | |
696 | } | |
697 | ||
b49240ec SH |
698 | int print_addrinfo_primary(const struct sockaddr_nl *who, struct nlmsghdr *n, |
699 | void *arg) | |
700 | { | |
701 | struct ifaddrmsg *ifa = NLMSG_DATA(n); | |
702 | ||
3bc1c4f2 | 703 | if (ifa->ifa_flags & IFA_F_SECONDARY) |
b49240ec SH |
704 | return 0; |
705 | ||
706 | return print_addrinfo(who, n, arg); | |
707 | } | |
708 | ||
709 | int print_addrinfo_secondary(const struct sockaddr_nl *who, struct nlmsghdr *n, | |
710 | void *arg) | |
711 | { | |
712 | struct ifaddrmsg *ifa = NLMSG_DATA(n); | |
713 | ||
3bc1c4f2 | 714 | if (!(ifa->ifa_flags & IFA_F_SECONDARY)) |
b49240ec SH |
715 | return 0; |
716 | ||
717 | return print_addrinfo(who, n, arg); | |
718 | } | |
aba5acdf SH |
719 | |
720 | struct nlmsg_list | |
721 | { | |
722 | struct nlmsg_list *next; | |
723 | struct nlmsghdr h; | |
724 | }; | |
725 | ||
62e2e540 ED |
726 | struct nlmsg_chain |
727 | { | |
728 | struct nlmsg_list *head; | |
729 | struct nlmsg_list *tail; | |
730 | }; | |
731 | ||
3d866ba2 | 732 | static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp) |
aba5acdf SH |
733 | { |
734 | for ( ;ainfo ; ainfo = ainfo->next) { | |
735 | struct nlmsghdr *n = &ainfo->h; | |
736 | struct ifaddrmsg *ifa = NLMSG_DATA(n); | |
737 | ||
738 | if (n->nlmsg_type != RTM_NEWADDR) | |
739 | continue; | |
740 | ||
741 | if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa))) | |
742 | return -1; | |
743 | ||
ae665a52 | 744 | if (ifa->ifa_index != ifindex || |
aba5acdf SH |
745 | (filter.family && filter.family != ifa->ifa_family)) |
746 | continue; | |
747 | ||
748 | print_addrinfo(NULL, n, fp); | |
749 | } | |
750 | return 0; | |
751 | } | |
752 | ||
753 | ||
ae665a52 | 754 | static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, |
6dc9f016 | 755 | void *arg) |
aba5acdf | 756 | { |
62e2e540 | 757 | struct nlmsg_chain *lchain = (struct nlmsg_chain *)arg; |
aba5acdf | 758 | struct nlmsg_list *h; |
aba5acdf SH |
759 | |
760 | h = malloc(n->nlmsg_len+sizeof(void*)); | |
761 | if (h == NULL) | |
762 | return -1; | |
763 | ||
764 | memcpy(&h->h, n, n->nlmsg_len); | |
765 | h->next = NULL; | |
766 | ||
62e2e540 ED |
767 | if (lchain->tail) |
768 | lchain->tail->next = h; | |
769 | else | |
770 | lchain->head = h; | |
771 | lchain->tail = h; | |
aba5acdf SH |
772 | |
773 | ll_remember_index(who, n, NULL); | |
774 | return 0; | |
775 | } | |
776 | ||
81824ac2 PE |
777 | static __u32 ipadd_dump_magic = 0x47361222; |
778 | ||
779 | static int ipadd_save_prep(void) | |
780 | { | |
781 | int ret; | |
782 | ||
783 | if (isatty(STDOUT_FILENO)) { | |
784 | fprintf(stderr, "Not sending binary stream to stdout\n"); | |
785 | return -1; | |
786 | } | |
787 | ||
788 | ret = write(STDOUT_FILENO, &ipadd_dump_magic, sizeof(ipadd_dump_magic)); | |
789 | if (ret != sizeof(ipadd_dump_magic)) { | |
790 | fprintf(stderr, "Can't write magic to dump file\n"); | |
791 | return -1; | |
792 | } | |
793 | ||
794 | return 0; | |
795 | } | |
796 | ||
797 | static int ipadd_dump_check_magic(void) | |
798 | { | |
799 | int ret; | |
800 | __u32 magic = 0; | |
801 | ||
802 | if (isatty(STDIN_FILENO)) { | |
803 | fprintf(stderr, "Can't restore addr dump from a terminal\n"); | |
804 | return -1; | |
805 | } | |
806 | ||
807 | ret = fread(&magic, sizeof(magic), 1, stdin); | |
808 | if (magic != ipadd_dump_magic) { | |
809 | fprintf(stderr, "Magic mismatch (%d elems, %x magic)\n", ret, magic); | |
810 | return -1; | |
811 | } | |
812 | ||
813 | return 0; | |
814 | } | |
815 | ||
816 | static int save_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, | |
817 | void *arg) | |
818 | { | |
819 | int ret; | |
820 | ||
821 | ret = write(STDOUT_FILENO, n, n->nlmsg_len); | |
822 | if ((ret > 0) && (ret != n->nlmsg_len)) { | |
823 | fprintf(stderr, "Short write while saving nlmsg\n"); | |
824 | ret = -EIO; | |
825 | } | |
826 | ||
827 | return ret == n->nlmsg_len ? 0 : ret; | |
828 | } | |
829 | ||
830 | static int show_handler(const struct sockaddr_nl *nl, struct nlmsghdr *n, void *arg) | |
831 | { | |
832 | struct ifaddrmsg *ifa = NLMSG_DATA(n); | |
833 | ||
834 | printf("if%d:\n", ifa->ifa_index); | |
835 | print_addrinfo(NULL, n, stdout); | |
836 | return 0; | |
837 | } | |
838 | ||
839 | static int ipaddr_showdump(void) | |
840 | { | |
841 | if (ipadd_dump_check_magic()) | |
842 | exit(-1); | |
843 | ||
844 | exit(rtnl_from_file(stdin, &show_handler, NULL)); | |
845 | } | |
846 | ||
847 | static int restore_handler(const struct sockaddr_nl *nl, struct nlmsghdr *n, void *arg) | |
848 | { | |
849 | int ret; | |
850 | ||
851 | n->nlmsg_flags |= NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK; | |
852 | ||
853 | ll_init_map(&rth); | |
854 | ||
855 | ret = rtnl_talk(&rth, n, 0, 0, n); | |
856 | if ((ret < 0) && (errno == EEXIST)) | |
857 | ret = 0; | |
858 | ||
859 | return ret; | |
860 | } | |
861 | ||
862 | static int ipaddr_restore(void) | |
863 | { | |
864 | if (ipadd_dump_check_magic()) | |
865 | exit(-1); | |
866 | ||
867 | exit(rtnl_from_file(stdin, &restore_handler, NULL)); | |
868 | } | |
869 | ||
8d07e5f7 SH |
870 | static void free_nlmsg_chain(struct nlmsg_chain *info) |
871 | { | |
872 | struct nlmsg_list *l, *n; | |
873 | ||
874 | for (l = info->head; l; l = n) { | |
875 | n = l->next; | |
876 | free(l); | |
877 | } | |
878 | } | |
879 | ||
880 | static void ipaddr_filter(struct nlmsg_chain *linfo, struct nlmsg_chain *ainfo) | |
881 | { | |
882 | struct nlmsg_list *l, **lp; | |
883 | ||
884 | lp = &linfo->head; | |
885 | while ( (l = *lp) != NULL) { | |
886 | int ok = 0; | |
7f747fd9 | 887 | int missing_net_address = 1; |
8d07e5f7 SH |
888 | struct ifinfomsg *ifi = NLMSG_DATA(&l->h); |
889 | struct nlmsg_list *a; | |
890 | ||
891 | for (a = ainfo->head; a; a = a->next) { | |
892 | struct nlmsghdr *n = &a->h; | |
893 | struct ifaddrmsg *ifa = NLMSG_DATA(n); | |
894 | ||
7f747fd9 PP |
895 | if (ifa->ifa_index != ifi->ifi_index) |
896 | continue; | |
897 | missing_net_address = 0; | |
898 | if (filter.family && filter.family != ifa->ifa_family) | |
8d07e5f7 SH |
899 | continue; |
900 | if ((filter.scope^ifa->ifa_scope)&filter.scopemask) | |
901 | continue; | |
902 | if ((filter.flags^ifa->ifa_flags)&filter.flagmask) | |
903 | continue; | |
904 | if (filter.pfx.family || filter.label) { | |
905 | struct rtattr *tb[IFA_MAX+1]; | |
906 | parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n)); | |
907 | if (!tb[IFA_LOCAL]) | |
908 | tb[IFA_LOCAL] = tb[IFA_ADDRESS]; | |
909 | ||
910 | if (filter.pfx.family && tb[IFA_LOCAL]) { | |
911 | inet_prefix dst; | |
912 | memset(&dst, 0, sizeof(dst)); | |
913 | dst.family = ifa->ifa_family; | |
914 | memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL])); | |
915 | if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen)) | |
916 | continue; | |
917 | } | |
918 | if (filter.label) { | |
919 | SPRINT_BUF(b1); | |
920 | const char *label; | |
921 | if (tb[IFA_LABEL]) | |
922 | label = RTA_DATA(tb[IFA_LABEL]); | |
923 | else | |
924 | label = ll_idx_n2a(ifa->ifa_index, b1); | |
925 | if (fnmatch(filter.label, label, 0) != 0) | |
926 | continue; | |
927 | } | |
928 | } | |
929 | ||
930 | ok = 1; | |
931 | break; | |
932 | } | |
7f747fd9 PP |
933 | if (missing_net_address && |
934 | (filter.family == AF_UNSPEC || filter.family == AF_PACKET)) | |
935 | ok = 1; | |
8d07e5f7 SH |
936 | if (!ok) { |
937 | *lp = l->next; | |
938 | free(l); | |
939 | } else | |
940 | lp = &l->next; | |
941 | } | |
942 | } | |
943 | ||
944 | static int ipaddr_flush(void) | |
945 | { | |
946 | int round = 0; | |
947 | char flushb[4096-512]; | |
948 | ||
949 | filter.flushb = flushb; | |
950 | filter.flushp = 0; | |
951 | filter.flushe = sizeof(flushb); | |
952 | ||
953 | while ((max_flush_loops == 0) || (round < max_flush_loops)) { | |
954 | const struct rtnl_dump_filter_arg a[3] = { | |
955 | { | |
956 | .filter = print_addrinfo_secondary, | |
957 | .arg1 = stdout, | |
958 | }, | |
959 | { | |
960 | .filter = print_addrinfo_primary, | |
961 | .arg1 = stdout, | |
962 | }, | |
963 | { | |
964 | .filter = NULL, | |
965 | .arg1 = NULL, | |
966 | }, | |
967 | }; | |
968 | if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) { | |
969 | perror("Cannot send dump request"); | |
970 | exit(1); | |
971 | } | |
972 | filter.flushed = 0; | |
973 | if (rtnl_dump_filter_l(&rth, a) < 0) { | |
974 | fprintf(stderr, "Flush terminated\n"); | |
975 | exit(1); | |
976 | } | |
977 | if (filter.flushed == 0) { | |
978 | flush_done: | |
979 | if (show_stats) { | |
980 | if (round == 0) | |
981 | printf("Nothing to flush.\n"); | |
982 | else | |
983 | printf("*** Flush is complete after %d round%s ***\n", round, round>1?"s":""); | |
984 | } | |
985 | fflush(stdout); | |
986 | return 0; | |
987 | } | |
988 | round++; | |
989 | if (flush_update() < 0) | |
990 | return 1; | |
991 | ||
992 | if (show_stats) { | |
993 | printf("\n*** Round %d, deleting %d addresses ***\n", round, filter.flushed); | |
994 | fflush(stdout); | |
995 | } | |
996 | ||
997 | /* If we are flushing, and specifying primary, then we | |
998 | * want to flush only a single round. Otherwise, we'll | |
999 | * start flushing secondaries that were promoted to | |
1000 | * primaries. | |
1001 | */ | |
1002 | if (!(filter.flags & IFA_F_SECONDARY) && (filter.flagmask & IFA_F_SECONDARY)) | |
1003 | goto flush_done; | |
1004 | } | |
1005 | fprintf(stderr, "*** Flush remains incomplete after %d rounds. ***\n", max_flush_loops); | |
1006 | fflush(stderr); | |
1007 | return 1; | |
1008 | } | |
1009 | ||
81824ac2 | 1010 | static int ipaddr_list_flush_or_save(int argc, char **argv, int action) |
aba5acdf | 1011 | { |
62e2e540 ED |
1012 | struct nlmsg_chain linfo = { NULL, NULL}; |
1013 | struct nlmsg_chain ainfo = { NULL, NULL}; | |
8d07e5f7 | 1014 | struct nlmsg_list *l; |
aba5acdf SH |
1015 | char *filter_dev = NULL; |
1016 | int no_link = 0; | |
1017 | ||
1018 | ipaddr_reset_filter(oneline); | |
1019 | filter.showqueue = 1; | |
1020 | ||
1021 | if (filter.family == AF_UNSPEC) | |
1022 | filter.family = preferred_family; | |
1023 | ||
242b8da7 | 1024 | filter.group = INIT_NETDEV_GROUP; |
f960c92a | 1025 | |
81824ac2 | 1026 | if (action == IPADD_FLUSH) { |
aba5acdf SH |
1027 | if (argc <= 0) { |
1028 | fprintf(stderr, "Flush requires arguments.\n"); | |
f960c92a | 1029 | |
aba5acdf SH |
1030 | return -1; |
1031 | } | |
1032 | if (filter.family == AF_PACKET) { | |
1033 | fprintf(stderr, "Cannot flush link addresses.\n"); | |
1034 | return -1; | |
1035 | } | |
1036 | } | |
1037 | ||
1038 | while (argc > 0) { | |
1039 | if (strcmp(*argv, "to") == 0) { | |
1040 | NEXT_ARG(); | |
1041 | get_prefix(&filter.pfx, *argv, filter.family); | |
1042 | if (filter.family == AF_UNSPEC) | |
1043 | filter.family = filter.pfx.family; | |
1044 | } else if (strcmp(*argv, "scope") == 0) { | |
f332d169 | 1045 | unsigned scope = 0; |
aba5acdf SH |
1046 | NEXT_ARG(); |
1047 | filter.scopemask = -1; | |
1048 | if (rtnl_rtscope_a2n(&scope, *argv)) { | |
1049 | if (strcmp(*argv, "all") != 0) | |
1050 | invarg("invalid \"scope\"\n", *argv); | |
1051 | scope = RT_SCOPE_NOWHERE; | |
1052 | filter.scopemask = 0; | |
1053 | } | |
1054 | filter.scope = scope; | |
1055 | } else if (strcmp(*argv, "up") == 0) { | |
1056 | filter.up = 1; | |
1057 | } else if (strcmp(*argv, "dynamic") == 0) { | |
1058 | filter.flags &= ~IFA_F_PERMANENT; | |
1059 | filter.flagmask |= IFA_F_PERMANENT; | |
1060 | } else if (strcmp(*argv, "permanent") == 0) { | |
1061 | filter.flags |= IFA_F_PERMANENT; | |
1062 | filter.flagmask |= IFA_F_PERMANENT; | |
a1b9ffcc BH |
1063 | } else if (strcmp(*argv, "secondary") == 0 || |
1064 | strcmp(*argv, "temporary") == 0) { | |
aba5acdf SH |
1065 | filter.flags |= IFA_F_SECONDARY; |
1066 | filter.flagmask |= IFA_F_SECONDARY; | |
1067 | } else if (strcmp(*argv, "primary") == 0) { | |
1068 | filter.flags &= ~IFA_F_SECONDARY; | |
1069 | filter.flagmask |= IFA_F_SECONDARY; | |
1070 | } else if (strcmp(*argv, "tentative") == 0) { | |
1071 | filter.flags |= IFA_F_TENTATIVE; | |
1072 | filter.flagmask |= IFA_F_TENTATIVE; | |
1073 | } else if (strcmp(*argv, "deprecated") == 0) { | |
1074 | filter.flags |= IFA_F_DEPRECATED; | |
1075 | filter.flagmask |= IFA_F_DEPRECATED; | |
bac735c5 NT |
1076 | } else if (strcmp(*argv, "home") == 0) { |
1077 | filter.flags |= IFA_F_HOMEADDRESS; | |
1078 | filter.flagmask |= IFA_F_HOMEADDRESS; | |
1079 | } else if (strcmp(*argv, "nodad") == 0) { | |
1080 | filter.flags |= IFA_F_NODAD; | |
1081 | filter.flagmask |= IFA_F_NODAD; | |
a1f27794 BH |
1082 | } else if (strcmp(*argv, "dadfailed") == 0) { |
1083 | filter.flags |= IFA_F_DADFAILED; | |
1084 | filter.flagmask |= IFA_F_DADFAILED; | |
aba5acdf SH |
1085 | } else if (strcmp(*argv, "label") == 0) { |
1086 | NEXT_ARG(); | |
1087 | filter.label = *argv; | |
f960c92a VD |
1088 | } else if (strcmp(*argv, "group") == 0) { |
1089 | NEXT_ARG(); | |
1090 | if (rtnl_group_a2n(&filter.group, *argv)) | |
1091 | invarg("Invalid \"group\" value\n", *argv); | |
aba5acdf SH |
1092 | } else { |
1093 | if (strcmp(*argv, "dev") == 0) { | |
1094 | NEXT_ARG(); | |
1095 | } | |
1096 | if (matches(*argv, "help") == 0) | |
1097 | usage(); | |
1098 | if (filter_dev) | |
1099 | duparg2("dev", *argv); | |
1100 | filter_dev = *argv; | |
1101 | } | |
1102 | argv++; argc--; | |
1103 | } | |
1104 | ||
aba5acdf SH |
1105 | if (filter_dev) { |
1106 | filter.ifindex = ll_name_to_index(filter_dev); | |
1107 | if (filter.ifindex <= 0) { | |
1108 | fprintf(stderr, "Device \"%s\" does not exist.\n", filter_dev); | |
1109 | return -1; | |
1110 | } | |
1111 | } | |
1112 | ||
81824ac2 | 1113 | if (action == IPADD_FLUSH) |
8d07e5f7 | 1114 | return ipaddr_flush(); |
351efcde | 1115 | |
81824ac2 PE |
1116 | if (action == IPADD_SAVE) { |
1117 | if (ipadd_save_prep()) | |
1118 | exit(1); | |
1119 | ||
1120 | if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETADDR) < 0) { | |
1121 | perror("Cannot send dump request"); | |
1122 | exit(1); | |
1123 | } | |
1124 | ||
1125 | if (rtnl_dump_filter(&rth, save_nlmsg, stdout) < 0) { | |
1126 | fprintf(stderr, "Save terminated\n"); | |
1127 | exit(1); | |
1128 | } | |
1129 | ||
1130 | exit(0); | |
1131 | } | |
1132 | ||
8d07e5f7 SH |
1133 | if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) { |
1134 | perror("Cannot send dump request"); | |
1135 | exit(1); | |
1136 | } | |
3bc1c4f2 | 1137 | |
8d07e5f7 SH |
1138 | if (rtnl_dump_filter(&rth, store_nlmsg, &linfo) < 0) { |
1139 | fprintf(stderr, "Dump terminated\n"); | |
1140 | exit(1); | |
aba5acdf SH |
1141 | } |
1142 | ||
af9d406f | 1143 | if (filter.family != AF_PACKET) { |
8d07e5f7 SH |
1144 | if (filter.oneline) |
1145 | no_link = 1; | |
1146 | ||
aba5acdf SH |
1147 | if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) { |
1148 | perror("Cannot send dump request"); | |
1149 | exit(1); | |
1150 | } | |
1151 | ||
cd70f3f5 | 1152 | if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo) < 0) { |
aba5acdf SH |
1153 | fprintf(stderr, "Dump terminated\n"); |
1154 | exit(1); | |
1155 | } | |
aba5acdf | 1156 | |
8d07e5f7 | 1157 | ipaddr_filter(&linfo, &ainfo); |
aba5acdf SH |
1158 | } |
1159 | ||
8d07e5f7 | 1160 | for (l = linfo.head; l; l = l->next) { |
aba5acdf SH |
1161 | if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) { |
1162 | struct ifinfomsg *ifi = NLMSG_DATA(&l->h); | |
1163 | if (filter.family != AF_PACKET) | |
8d07e5f7 SH |
1164 | print_selected_addrinfo(ifi->ifi_index, |
1165 | ainfo.head, stdout); | |
aba5acdf | 1166 | } |
aba5acdf | 1167 | } |
8d07e5f7 SH |
1168 | fflush(stdout); |
1169 | ||
1170 | free_nlmsg_chain(&ainfo); | |
1171 | free_nlmsg_chain(&linfo); | |
aba5acdf | 1172 | |
351efcde | 1173 | return 0; |
aba5acdf SH |
1174 | } |
1175 | ||
1176 | int ipaddr_list_link(int argc, char **argv) | |
1177 | { | |
1178 | preferred_family = AF_PACKET; | |
1179 | do_link = 1; | |
81824ac2 | 1180 | return ipaddr_list_flush_or_save(argc, argv, IPADD_LIST); |
aba5acdf SH |
1181 | } |
1182 | ||
1183 | void ipaddr_reset_filter(int oneline) | |
1184 | { | |
1185 | memset(&filter, 0, sizeof(filter)); | |
1186 | filter.oneline = oneline; | |
1187 | } | |
1188 | ||
3d866ba2 | 1189 | static int default_scope(inet_prefix *lcl) |
aba5acdf SH |
1190 | { |
1191 | if (lcl->family == AF_INET) { | |
1192 | if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127) | |
1193 | return RT_SCOPE_HOST; | |
1194 | } | |
1195 | return 0; | |
1196 | } | |
1197 | ||
3d866ba2 | 1198 | static int ipaddr_modify(int cmd, int flags, int argc, char **argv) |
aba5acdf | 1199 | { |
aba5acdf SH |
1200 | struct { |
1201 | struct nlmsghdr n; | |
1202 | struct ifaddrmsg ifa; | |
1203 | char buf[256]; | |
1204 | } req; | |
1205 | char *d = NULL; | |
1206 | char *l = NULL; | |
f082b64f | 1207 | char *lcl_arg = NULL; |
35546df7 MN |
1208 | char *valid_lftp = NULL; |
1209 | char *preferred_lftp = NULL; | |
aba5acdf SH |
1210 | inet_prefix lcl; |
1211 | inet_prefix peer; | |
1212 | int local_len = 0; | |
1213 | int peer_len = 0; | |
1214 | int brd_len = 0; | |
1215 | int any_len = 0; | |
1216 | int scoped = 0; | |
141bb606 MN |
1217 | __u32 preferred_lft = INFINITY_LIFE_TIME; |
1218 | __u32 valid_lft = INFINITY_LIFE_TIME; | |
35546df7 | 1219 | struct ifa_cacheinfo cinfo; |
aba5acdf SH |
1220 | |
1221 | memset(&req, 0, sizeof(req)); | |
1222 | ||
1223 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); | |
0aef366b | 1224 | req.n.nlmsg_flags = NLM_F_REQUEST | flags; |
aba5acdf SH |
1225 | req.n.nlmsg_type = cmd; |
1226 | req.ifa.ifa_family = preferred_family; | |
1227 | ||
1228 | while (argc > 0) { | |
1229 | if (strcmp(*argv, "peer") == 0 || | |
1230 | strcmp(*argv, "remote") == 0) { | |
1231 | NEXT_ARG(); | |
1232 | ||
1233 | if (peer_len) | |
1234 | duparg("peer", *argv); | |
1235 | get_prefix(&peer, *argv, req.ifa.ifa_family); | |
1236 | peer_len = peer.bytelen; | |
1237 | if (req.ifa.ifa_family == AF_UNSPEC) | |
1238 | req.ifa.ifa_family = peer.family; | |
1239 | addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen); | |
1240 | req.ifa.ifa_prefixlen = peer.bitlen; | |
1241 | } else if (matches(*argv, "broadcast") == 0 || | |
1242 | strcmp(*argv, "brd") == 0) { | |
1243 | inet_prefix addr; | |
1244 | NEXT_ARG(); | |
1245 | if (brd_len) | |
1246 | duparg("broadcast", *argv); | |
1247 | if (strcmp(*argv, "+") == 0) | |
1248 | brd_len = -1; | |
1249 | else if (strcmp(*argv, "-") == 0) | |
1250 | brd_len = -2; | |
1251 | else { | |
1252 | get_addr(&addr, *argv, req.ifa.ifa_family); | |
1253 | if (req.ifa.ifa_family == AF_UNSPEC) | |
1254 | req.ifa.ifa_family = addr.family; | |
1255 | addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen); | |
1256 | brd_len = addr.bytelen; | |
1257 | } | |
1258 | } else if (strcmp(*argv, "anycast") == 0) { | |
1259 | inet_prefix addr; | |
1260 | NEXT_ARG(); | |
1261 | if (any_len) | |
1262 | duparg("anycast", *argv); | |
1263 | get_addr(&addr, *argv, req.ifa.ifa_family); | |
1264 | if (req.ifa.ifa_family == AF_UNSPEC) | |
1265 | req.ifa.ifa_family = addr.family; | |
1266 | addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen); | |
1267 | any_len = addr.bytelen; | |
1268 | } else if (strcmp(*argv, "scope") == 0) { | |
f332d169 | 1269 | unsigned scope = 0; |
aba5acdf SH |
1270 | NEXT_ARG(); |
1271 | if (rtnl_rtscope_a2n(&scope, *argv)) | |
f1675d61 | 1272 | invarg("invalid scope value.", *argv); |
aba5acdf SH |
1273 | req.ifa.ifa_scope = scope; |
1274 | scoped = 1; | |
1275 | } else if (strcmp(*argv, "dev") == 0) { | |
1276 | NEXT_ARG(); | |
1277 | d = *argv; | |
1278 | } else if (strcmp(*argv, "label") == 0) { | |
1279 | NEXT_ARG(); | |
1280 | l = *argv; | |
1281 | addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1); | |
35546df7 MN |
1282 | } else if (matches(*argv, "valid_lft") == 0) { |
1283 | if (valid_lftp) | |
1284 | duparg("valid_lft", *argv); | |
1285 | NEXT_ARG(); | |
1286 | valid_lftp = *argv; | |
1287 | if (set_lifetime(&valid_lft, *argv)) | |
1288 | invarg("valid_lft value", *argv); | |
1289 | } else if (matches(*argv, "preferred_lft") == 0) { | |
1290 | if (preferred_lftp) | |
1291 | duparg("preferred_lft", *argv); | |
1292 | NEXT_ARG(); | |
1293 | preferred_lftp = *argv; | |
1294 | if (set_lifetime(&preferred_lft, *argv)) | |
1295 | invarg("preferred_lft value", *argv); | |
bac735c5 NT |
1296 | } else if (strcmp(*argv, "home") == 0) { |
1297 | req.ifa.ifa_flags |= IFA_F_HOMEADDRESS; | |
1298 | } else if (strcmp(*argv, "nodad") == 0) { | |
1299 | req.ifa.ifa_flags |= IFA_F_NODAD; | |
aba5acdf SH |
1300 | } else { |
1301 | if (strcmp(*argv, "local") == 0) { | |
1302 | NEXT_ARG(); | |
1303 | } | |
1304 | if (matches(*argv, "help") == 0) | |
1305 | usage(); | |
1306 | if (local_len) | |
1307 | duparg2("local", *argv); | |
f082b64f | 1308 | lcl_arg = *argv; |
aba5acdf SH |
1309 | get_prefix(&lcl, *argv, req.ifa.ifa_family); |
1310 | if (req.ifa.ifa_family == AF_UNSPEC) | |
1311 | req.ifa.ifa_family = lcl.family; | |
1312 | addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen); | |
1313 | local_len = lcl.bytelen; | |
1314 | } | |
1315 | argc--; argv++; | |
1316 | } | |
1317 | if (d == NULL) { | |
1318 | fprintf(stderr, "Not enough information: \"dev\" argument is required.\n"); | |
1319 | return -1; | |
1320 | } | |
1321 | if (l && matches(d, l) != 0) { | |
1322 | fprintf(stderr, "\"dev\" (%s) must match \"label\" (%s).\n", d, l); | |
1db61e02 | 1323 | return -1; |
aba5acdf SH |
1324 | } |
1325 | ||
f082b64f | 1326 | if (peer_len == 0 && local_len) { |
1327 | if (cmd == RTM_DELADDR && lcl.family == AF_INET && !(lcl.flags & PREFIXLEN_SPECIFIED)) { | |
1328 | fprintf(stderr, | |
1329 | "Warning: Executing wildcard deletion to stay compatible with old scripts.\n" \ | |
1330 | " Explicitly specify the prefix length (%s/%d) to avoid this warning.\n" \ | |
1331 | " This special behaviour is likely to disappear in further releases,\n" \ | |
1332 | " fix your scripts!\n", lcl_arg, local_len*8); | |
1333 | } else { | |
1334 | peer = lcl; | |
1335 | addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen); | |
1336 | } | |
aba5acdf SH |
1337 | } |
1338 | if (req.ifa.ifa_prefixlen == 0) | |
1339 | req.ifa.ifa_prefixlen = lcl.bitlen; | |
1340 | ||
1341 | if (brd_len < 0 && cmd != RTM_DELADDR) { | |
1342 | inet_prefix brd; | |
1343 | int i; | |
1344 | if (req.ifa.ifa_family != AF_INET) { | |
1345 | fprintf(stderr, "Broadcast can be set only for IPv4 addresses\n"); | |
1346 | return -1; | |
1347 | } | |
1348 | brd = peer; | |
1349 | if (brd.bitlen <= 30) { | |
1350 | for (i=31; i>=brd.bitlen; i--) { | |
1351 | if (brd_len == -1) | |
1352 | brd.data[0] |= htonl(1<<(31-i)); | |
1353 | else | |
1354 | brd.data[0] &= ~htonl(1<<(31-i)); | |
1355 | } | |
1356 | addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen); | |
1357 | brd_len = brd.bytelen; | |
1358 | } | |
1359 | } | |
1360 | if (!scoped && cmd != RTM_DELADDR) | |
1361 | req.ifa.ifa_scope = default_scope(&lcl); | |
1362 | ||
aba5acdf SH |
1363 | ll_init_map(&rth); |
1364 | ||
1365 | if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) { | |
1366 | fprintf(stderr, "Cannot find device \"%s\"\n", d); | |
1367 | return -1; | |
1368 | } | |
1369 | ||
35546df7 MN |
1370 | if (valid_lftp || preferred_lftp) { |
1371 | if (!valid_lft) { | |
1372 | fprintf(stderr, "valid_lft is zero\n"); | |
1373 | return -1; | |
1374 | } | |
1375 | if (valid_lft < preferred_lft) { | |
1376 | fprintf(stderr, "preferred_lft is greater than valid_lft\n"); | |
1377 | return -1; | |
1378 | } | |
1379 | ||
1380 | memset(&cinfo, 0, sizeof(cinfo)); | |
1381 | cinfo.ifa_prefered = preferred_lft; | |
1382 | cinfo.ifa_valid = valid_lft; | |
1383 | addattr_l(&req.n, sizeof(req), IFA_CACHEINFO, &cinfo, | |
1384 | sizeof(cinfo)); | |
1385 | } | |
1386 | ||
cd70f3f5 | 1387 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0) |
1db61e02 | 1388 | return -2; |
aba5acdf | 1389 | |
351efcde | 1390 | return 0; |
aba5acdf SH |
1391 | } |
1392 | ||
1393 | int do_ipaddr(int argc, char **argv) | |
1394 | { | |
1395 | if (argc < 1) | |
81824ac2 | 1396 | return ipaddr_list_flush_or_save(0, NULL, IPADD_LIST); |
aba5acdf | 1397 | if (matches(*argv, "add") == 0) |
0aef366b NT |
1398 | return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_EXCL, argc-1, argv+1); |
1399 | if (matches(*argv, "change") == 0 || | |
1400 | strcmp(*argv, "chg") == 0) | |
1401 | return ipaddr_modify(RTM_NEWADDR, NLM_F_REPLACE, argc-1, argv+1); | |
1402 | if (matches(*argv, "replace") == 0) | |
1403 | return ipaddr_modify(RTM_NEWADDR, NLM_F_CREATE|NLM_F_REPLACE, argc-1, argv+1); | |
aba5acdf | 1404 | if (matches(*argv, "delete") == 0) |
0aef366b | 1405 | return ipaddr_modify(RTM_DELADDR, 0, argc-1, argv+1); |
aba5acdf SH |
1406 | if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 |
1407 | || matches(*argv, "lst") == 0) | |
81824ac2 | 1408 | return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_LIST); |
aba5acdf | 1409 | if (matches(*argv, "flush") == 0) |
81824ac2 PE |
1410 | return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_FLUSH); |
1411 | if (matches(*argv, "save") == 0) | |
1412 | return ipaddr_list_flush_or_save(argc-1, argv+1, IPADD_SAVE); | |
1413 | if (matches(*argv, "showdump") == 0) | |
1414 | return ipaddr_showdump(); | |
1415 | if (matches(*argv, "restore") == 0) | |
1416 | return ipaddr_restore(); | |
aba5acdf SH |
1417 | if (matches(*argv, "help") == 0) |
1418 | usage(); | |
b096fa5f | 1419 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip addr help\".\n", *argv); |
aba5acdf SH |
1420 | exit(-1); |
1421 | } | |
1422 |