2 * ipaddress.c "ip address".
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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 * Laszlo Valko <valko@linux.karinthy.hu> 990223: address label must be zero terminated
15 #include <sys/socket.h>
16 #include <sys/ioctl.h>
23 #include <arpa/inet.h>
25 #define sysinfo kernel_sysinfo
26 #include <linux/if_arp.h>
45 struct rtnl_handle *rth;
48 void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
51 flags &= ~IFF_RUNNING;
52 #define _PF(f) if (flags&IFF_##f) { \
54 fprintf(fp, #f "%s", flags ? "," : ""); }
74 fprintf(fp, "%x", flags);
76 fprintf(fp, ",M-DOWN");
80 static void print_queuelen(char *name)
85 s = socket(AF_INET, SOCK_STREAM, 0);
89 memset(&ifr, 0, sizeof(ifr));
90 strcpy(ifr.ifr_name, name);
91 if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
92 perror("SIOCGIFXQLEN");
99 printf("qlen %d", ifr.ifr_qlen);
102 static int print_linkinfo(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
104 FILE *fp = (FILE*)arg;
105 struct ifinfomsg *ifi = NLMSG_DATA(n);
106 struct rtattr * tb[IFLA_MAX+1];
107 int len = n->nlmsg_len;
110 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
113 len -= NLMSG_LENGTH(sizeof(*ifi));
117 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
119 if (filter.up && !(ifi->ifi_flags&IFF_UP))
122 memset(tb, 0, sizeof(tb));
123 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
124 if (tb[IFLA_IFNAME] == NULL) {
125 error_msg("nil ifname");
129 (!filter.family || filter.family == AF_PACKET) &&
130 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
133 if (n->nlmsg_type == RTM_DELLINK)
134 fprintf(fp, "Deleted ");
136 fprintf(fp, "%d: %s", ifi->ifi_index,
137 tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>");
141 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
143 fprintf(fp, "@NONE: ");
145 fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
146 m_flag = ll_index_to_flags(iflink);
147 m_flag = !(m_flag & IFF_UP);
152 print_link_flags(fp, ifi->ifi_flags, m_flag);
155 fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
157 fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
159 if (tb[IFLA_MASTER]) {
161 fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
164 if (filter.showqueue)
165 print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
167 if (!filter.family || filter.family == AF_PACKET) {
169 fprintf(fp, "%s", _SL_);
170 fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
172 if (tb[IFLA_ADDRESS]) {
173 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
174 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
178 if (tb[IFLA_BROADCAST]) {
179 if (ifi->ifi_flags&IFF_POINTOPOINT)
180 fprintf(fp, " peer ");
182 fprintf(fp, " brd ");
183 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
184 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
194 static int print_addrinfo(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
196 FILE *fp = (FILE*)arg;
197 struct ifaddrmsg *ifa = NLMSG_DATA(n);
198 int len = n->nlmsg_len;
199 struct rtattr * rta_tb[IFA_MAX+1];
203 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
205 len -= NLMSG_LENGTH(sizeof(*ifa));
207 error_msg("wrong nlmsg len %d", len);
211 memset(rta_tb, 0, sizeof(rta_tb));
212 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
214 if (!rta_tb[IFA_LOCAL])
215 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
216 if (!rta_tb[IFA_ADDRESS])
217 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
219 if (filter.ifindex && filter.ifindex != ifa->ifa_index)
221 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
223 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
227 if (rta_tb[IFA_LABEL])
228 label = RTA_DATA(rta_tb[IFA_LABEL]);
230 label = ll_idx_n2a(ifa->ifa_index, b1);
231 if (fnmatch(filter.label, label, 0) != 0)
234 if (filter.pfx.family) {
235 if (rta_tb[IFA_LOCAL]) {
237 memset(&dst, 0, sizeof(dst));
238 dst.family = ifa->ifa_family;
239 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
240 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
245 if (n->nlmsg_type == RTM_DELADDR)
246 fprintf(fp, "Deleted ");
249 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
250 if (ifa->ifa_family == AF_INET)
251 fprintf(fp, " inet ");
252 else if (ifa->ifa_family == AF_INET6)
253 fprintf(fp, " inet6 ");
255 fprintf(fp, " family %d ", ifa->ifa_family);
257 if (rta_tb[IFA_LOCAL]) {
258 fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family,
259 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
260 RTA_DATA(rta_tb[IFA_LOCAL]),
261 abuf, sizeof(abuf)));
263 if (rta_tb[IFA_ADDRESS] == NULL ||
264 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
265 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
267 fprintf(fp, " peer %s/%d ",
268 rt_addr_n2a(ifa->ifa_family,
269 RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
270 RTA_DATA(rta_tb[IFA_ADDRESS]),
276 if (rta_tb[IFA_BROADCAST]) {
277 fprintf(fp, "brd %s ",
278 rt_addr_n2a(ifa->ifa_family,
279 RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
280 RTA_DATA(rta_tb[IFA_BROADCAST]),
281 abuf, sizeof(abuf)));
283 if (rta_tb[IFA_ANYCAST]) {
284 fprintf(fp, "any %s ",
285 rt_addr_n2a(ifa->ifa_family,
286 RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
287 RTA_DATA(rta_tb[IFA_ANYCAST]),
288 abuf, sizeof(abuf)));
290 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
291 if (ifa->ifa_flags&IFA_F_SECONDARY) {
292 ifa->ifa_flags &= ~IFA_F_SECONDARY;
293 fprintf(fp, "secondary ");
295 if (ifa->ifa_flags&IFA_F_TENTATIVE) {
296 ifa->ifa_flags &= ~IFA_F_TENTATIVE;
297 fprintf(fp, "tentative ");
299 if (ifa->ifa_flags&IFA_F_DEPRECATED) {
300 ifa->ifa_flags &= ~IFA_F_DEPRECATED;
301 fprintf(fp, "deprecated ");
303 if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
304 fprintf(fp, "dynamic ");
306 ifa->ifa_flags &= ~IFA_F_PERMANENT;
308 fprintf(fp, "flags %02x ", ifa->ifa_flags);
309 if (rta_tb[IFA_LABEL])
310 fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL]));
311 if (rta_tb[IFA_CACHEINFO]) {
312 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
314 fprintf(fp, "%s", _SL_);
315 if (ci->ifa_valid == 0xFFFFFFFFU)
316 sprintf(buf, "valid_lft forever");
318 sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
319 if (ci->ifa_prefered == 0xFFFFFFFFU)
320 sprintf(buf+strlen(buf), " preferred_lft forever");
322 sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
323 fprintf(fp, " %s", buf);
333 struct nlmsg_list *next;
337 static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
339 for ( ;ainfo ; ainfo = ainfo->next) {
340 struct nlmsghdr *n = &ainfo->h;
341 struct ifaddrmsg *ifa = NLMSG_DATA(n);
343 if (n->nlmsg_type != RTM_NEWADDR)
346 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
349 if (ifa->ifa_index != ifindex ||
350 (filter.family && filter.family != ifa->ifa_family))
353 print_addrinfo(NULL, n, fp);
359 static int store_nlmsg(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
361 struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
362 struct nlmsg_list *h;
363 struct nlmsg_list **lp;
365 h = malloc(n->nlmsg_len+sizeof(void*));
369 memcpy(&h->h, n, n->nlmsg_len);
372 for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
375 ll_remember_index(who, n, NULL);
379 static void ipaddr_reset_filter(int _oneline)
381 memset(&filter, 0, sizeof(filter));
382 filter.oneline = _oneline;
385 extern int ipaddr_list(int argc, char **argv)
387 const char *option[] = { "to", "scope", "up", "label", "dev", 0 };
388 struct nlmsg_list *linfo = NULL;
389 struct nlmsg_list *ainfo = NULL;
390 struct nlmsg_list *l;
391 struct rtnl_handle rth;
392 char *filter_dev = NULL;
395 ipaddr_reset_filter(oneline);
396 filter.showqueue = 1;
398 if (filter.family == AF_UNSPEC)
399 filter.family = preferred_family;
402 const unsigned short option_num = compare_string_array(option, *argv);
403 switch (option_num) {
406 get_prefix(&filter.pfx, *argv, filter.family);
407 if (filter.family == AF_UNSPEC) {
408 filter.family = filter.pfx.family;
415 filter.scopemask = -1;
416 if (rtnl_rtscope_a2n(&scope, *argv)) {
417 if (strcmp(*argv, "all") != 0) {
418 invarg("invalid \"scope\"\n", *argv);
420 scope = RT_SCOPE_NOWHERE;
421 filter.scopemask = 0;
423 filter.scope = scope;
431 filter.label = *argv;
437 duparg2("dev", *argv);
445 if (rtnl_open(&rth, 0) < 0)
448 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
449 perror_msg_and_die("Cannot send dump request");
452 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) {
453 error_msg_and_die("Dump terminated");
457 filter.ifindex = ll_name_to_index(filter_dev);
458 if (filter.ifindex <= 0) {
459 error_msg("Device \"%s\" does not exist.", filter_dev);
464 if (filter.family != AF_PACKET) {
465 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
466 perror_msg_and_die("Cannot send dump request");
469 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) {
470 error_msg_and_die("Dump terminated");
475 if (filter.family && filter.family != AF_PACKET) {
476 struct nlmsg_list **lp;
482 while ((l=*lp)!=NULL) {
484 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
485 struct nlmsg_list *a;
487 for (a=ainfo; a; a=a->next) {
488 struct nlmsghdr *n = &a->h;
489 struct ifaddrmsg *ifa = NLMSG_DATA(n);
491 if (ifa->ifa_index != ifi->ifi_index ||
492 (filter.family && filter.family != ifa->ifa_family))
494 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
496 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
498 if (filter.pfx.family || filter.label) {
499 struct rtattr *tb[IFA_MAX+1];
500 memset(tb, 0, sizeof(tb));
501 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
503 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
505 if (filter.pfx.family && tb[IFA_LOCAL]) {
507 memset(&dst, 0, sizeof(dst));
508 dst.family = ifa->ifa_family;
509 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
510 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
517 label = RTA_DATA(tb[IFA_LABEL]);
519 label = ll_idx_n2a(ifa->ifa_index, b1);
520 if (fnmatch(filter.label, label, 0) != 0)
535 for (l=linfo; l; l = l->next) {
536 if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
537 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
538 if (filter.family != AF_PACKET)
539 print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
547 static int default_scope(inet_prefix *lcl)
549 if (lcl->family == AF_INET) {
550 if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127)
551 return RT_SCOPE_HOST;
556 static int ipaddr_modify(int cmd, int argc, char **argv)
558 const char *option[] = { "peer", "remote", "broadcast", "brd",
559 "anycast", "scope", "dev", "label", "local", 0 };
560 struct rtnl_handle rth;
563 struct ifaddrmsg ifa;
576 memset(&req, 0, sizeof(req));
578 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
579 req.n.nlmsg_flags = NLM_F_REQUEST;
580 req.n.nlmsg_type = cmd;
581 req.ifa.ifa_family = preferred_family;
584 const unsigned short option_num = compare_string_array(option, *argv);
585 switch (option_num) {
591 duparg("peer", *argv);
593 get_prefix(&peer, *argv, req.ifa.ifa_family);
594 peer_len = peer.bytelen;
595 if (req.ifa.ifa_family == AF_UNSPEC) {
596 req.ifa.ifa_family = peer.family;
598 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
599 req.ifa.ifa_prefixlen = peer.bitlen;
601 case 2: /* broadcast */
607 duparg("broadcast", *argv);
609 if (strcmp(*argv, "+") == 0) {
612 else if (strcmp(*argv, "-") == 0) {
615 get_addr(&addr, *argv, req.ifa.ifa_family);
616 if (req.ifa.ifa_family == AF_UNSPEC)
617 req.ifa.ifa_family = addr.family;
618 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
619 brd_len = addr.bytelen;
623 case 4: /* anycast */
628 duparg("anycast", *argv);
630 get_addr(&addr, *argv, req.ifa.ifa_family);
631 if (req.ifa.ifa_family == AF_UNSPEC) {
632 req.ifa.ifa_family = addr.family;
634 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
635 any_len = addr.bytelen;
642 if (rtnl_rtscope_a2n(&scope, *argv)) {
643 invarg(*argv, "invalid scope value.");
645 req.ifa.ifa_scope = scope;
656 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
662 duparg2("local", *argv);
664 get_prefix(&lcl, *argv, req.ifa.ifa_family);
665 if (req.ifa.ifa_family == AF_UNSPEC) {
666 req.ifa.ifa_family = lcl.family;
668 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
669 local_len = lcl.bytelen;
676 error_msg("Not enough information: \"dev\" argument is required.");
679 if (l && matches(d, l) != 0) {
680 error_msg_and_die("\"dev\" (%s) must match \"label\" (%s).", d, l);
683 if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
685 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
687 if (req.ifa.ifa_prefixlen == 0)
688 req.ifa.ifa_prefixlen = lcl.bitlen;
690 if (brd_len < 0 && cmd != RTM_DELADDR) {
693 if (req.ifa.ifa_family != AF_INET) {
694 error_msg("Broadcast can be set only for IPv4 addresses");
698 if (brd.bitlen <= 30) {
699 for (i=31; i>=brd.bitlen; i--) {
701 brd.data[0] |= htonl(1<<(31-i));
703 brd.data[0] &= ~htonl(1<<(31-i));
705 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
706 brd_len = brd.bytelen;
709 if (!scoped && cmd != RTM_DELADDR)
710 req.ifa.ifa_scope = default_scope(&lcl);
712 if (rtnl_open(&rth, 0) < 0)
717 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
718 error_msg("Cannot find device \"%s\"", d);
722 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
728 extern int do_ipaddr(int argc, char **argv)
730 const char *commands[] = { "add", "delete", "list", "show", "lst", 0 };
731 unsigned short command_num = 2;
734 command_num = compare_string_array(commands, *argv);
736 switch (command_num) {
738 return ipaddr_modify(RTM_NEWADDR, argc-1, argv+1);
740 return ipaddr_modify(RTM_DELADDR, argc-1, argv+1);
744 return ipaddr_list(argc-1, argv+1);
746 error_msg_and_die("Unknown command %s", *argv);