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