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