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