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
20 #include <sys/ioctl.h>
21 #include <sys/socket.h>
22 #include <sys/ioctl.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
28 #define sysinfo kernel_sysinfo
29 #include <linux/netdevice.h>
30 #include <linux/if_arp.h>
31 #include <linux/sockios.h>
37 #include "ip_common.h"
52 struct rtnl_handle *rth;
55 void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
58 flags &= ~IFF_RUNNING;
59 #define _PF(f) if (flags&IFF_##f) { \
61 fprintf(fp, #f "%s", flags ? "," : ""); }
81 fprintf(fp, "%x", flags);
83 fprintf(fp, ",M-DOWN");
87 static void print_queuelen(char *name)
92 s = socket(AF_INET, SOCK_STREAM, 0);
96 memset(&ifr, 0, sizeof(ifr));
97 strcpy(ifr.ifr_name, name);
98 if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
99 perror("SIOCGIFXQLEN");
106 printf("qlen %d", ifr.ifr_qlen);
109 static int print_linkinfo(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
111 FILE *fp = (FILE*)arg;
112 struct ifinfomsg *ifi = NLMSG_DATA(n);
113 struct rtattr * tb[IFLA_MAX+1];
114 int len = n->nlmsg_len;
117 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
120 len -= NLMSG_LENGTH(sizeof(*ifi));
124 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
126 if (filter.up && !(ifi->ifi_flags&IFF_UP))
129 memset(tb, 0, sizeof(tb));
130 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
131 if (tb[IFLA_IFNAME] == NULL) {
132 error_msg("nil ifname");
136 (!filter.family || filter.family == AF_PACKET) &&
137 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
140 if (n->nlmsg_type == RTM_DELLINK)
141 fprintf(fp, "Deleted ");
143 fprintf(fp, "%d: %s", ifi->ifi_index,
144 tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>");
148 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
150 fprintf(fp, "@NONE: ");
152 fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
153 m_flag = ll_index_to_flags(iflink);
154 m_flag = !(m_flag & IFF_UP);
159 print_link_flags(fp, ifi->ifi_flags, m_flag);
162 fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
164 fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
166 if (tb[IFLA_MASTER]) {
168 fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
171 if (filter.showqueue)
172 print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
174 if (!filter.family || filter.family == AF_PACKET) {
176 fprintf(fp, "%s", _SL_);
177 fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
179 if (tb[IFLA_ADDRESS]) {
180 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
181 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
185 if (tb[IFLA_BROADCAST]) {
186 if (ifi->ifi_flags&IFF_POINTOPOINT)
187 fprintf(fp, " peer ");
189 fprintf(fp, " brd ");
190 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
191 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
201 static int print_addrinfo(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
203 FILE *fp = (FILE*)arg;
204 struct ifaddrmsg *ifa = NLMSG_DATA(n);
205 int len = n->nlmsg_len;
206 struct rtattr * rta_tb[IFA_MAX+1];
210 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
212 len -= NLMSG_LENGTH(sizeof(*ifa));
214 error_msg("wrong nlmsg len %d", len);
218 memset(rta_tb, 0, sizeof(rta_tb));
219 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
221 if (!rta_tb[IFA_LOCAL])
222 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
223 if (!rta_tb[IFA_ADDRESS])
224 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
226 if (filter.ifindex && filter.ifindex != ifa->ifa_index)
228 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
230 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
234 if (rta_tb[IFA_LABEL])
235 label = RTA_DATA(rta_tb[IFA_LABEL]);
237 label = ll_idx_n2a(ifa->ifa_index, b1);
238 if (fnmatch(filter.label, label, 0) != 0)
241 if (filter.pfx.family) {
242 if (rta_tb[IFA_LOCAL]) {
244 memset(&dst, 0, sizeof(dst));
245 dst.family = ifa->ifa_family;
246 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
247 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
252 if (n->nlmsg_type == RTM_DELADDR)
253 fprintf(fp, "Deleted ");
256 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
257 if (ifa->ifa_family == AF_INET)
258 fprintf(fp, " inet ");
259 else if (ifa->ifa_family == AF_INET6)
260 fprintf(fp, " inet6 ");
262 fprintf(fp, " family %d ", ifa->ifa_family);
264 if (rta_tb[IFA_LOCAL]) {
265 fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family,
266 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
267 RTA_DATA(rta_tb[IFA_LOCAL]),
268 abuf, sizeof(abuf)));
270 if (rta_tb[IFA_ADDRESS] == NULL ||
271 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
272 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
274 fprintf(fp, " peer %s/%d ",
275 rt_addr_n2a(ifa->ifa_family,
276 RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
277 RTA_DATA(rta_tb[IFA_ADDRESS]),
283 if (rta_tb[IFA_BROADCAST]) {
284 fprintf(fp, "brd %s ",
285 rt_addr_n2a(ifa->ifa_family,
286 RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
287 RTA_DATA(rta_tb[IFA_BROADCAST]),
288 abuf, sizeof(abuf)));
290 if (rta_tb[IFA_ANYCAST]) {
291 fprintf(fp, "any %s ",
292 rt_addr_n2a(ifa->ifa_family,
293 RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
294 RTA_DATA(rta_tb[IFA_ANYCAST]),
295 abuf, sizeof(abuf)));
297 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
298 if (ifa->ifa_flags&IFA_F_SECONDARY) {
299 ifa->ifa_flags &= ~IFA_F_SECONDARY;
300 fprintf(fp, "secondary ");
302 if (ifa->ifa_flags&IFA_F_TENTATIVE) {
303 ifa->ifa_flags &= ~IFA_F_TENTATIVE;
304 fprintf(fp, "tentative ");
306 if (ifa->ifa_flags&IFA_F_DEPRECATED) {
307 ifa->ifa_flags &= ~IFA_F_DEPRECATED;
308 fprintf(fp, "deprecated ");
310 if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
311 fprintf(fp, "dynamic ");
313 ifa->ifa_flags &= ~IFA_F_PERMANENT;
315 fprintf(fp, "flags %02x ", ifa->ifa_flags);
316 if (rta_tb[IFA_LABEL])
317 fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL]));
318 if (rta_tb[IFA_CACHEINFO]) {
319 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
321 fprintf(fp, "%s", _SL_);
322 if (ci->ifa_valid == 0xFFFFFFFFU)
323 sprintf(buf, "valid_lft forever");
325 sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
326 if (ci->ifa_prefered == 0xFFFFFFFFU)
327 sprintf(buf+strlen(buf), " preferred_lft forever");
329 sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
330 fprintf(fp, " %s", buf);
340 struct nlmsg_list *next;
344 static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
346 for ( ;ainfo ; ainfo = ainfo->next) {
347 struct nlmsghdr *n = &ainfo->h;
348 struct ifaddrmsg *ifa = NLMSG_DATA(n);
350 if (n->nlmsg_type != RTM_NEWADDR)
353 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
356 if (ifa->ifa_index != ifindex ||
357 (filter.family && filter.family != ifa->ifa_family))
360 print_addrinfo(NULL, n, fp);
366 static int store_nlmsg(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
368 struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
369 struct nlmsg_list *h;
370 struct nlmsg_list **lp;
372 h = malloc(n->nlmsg_len+sizeof(void*));
376 memcpy(&h->h, n, n->nlmsg_len);
379 for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
382 ll_remember_index(who, n, NULL);
386 static void ipaddr_reset_filter(int _oneline)
388 memset(&filter, 0, sizeof(filter));
389 filter.oneline = _oneline;
392 extern int ipaddr_list(int argc, char **argv)
394 const char *option[] = { "to", "scope", "up", "label", "dev", 0 };
395 struct nlmsg_list *linfo = NULL;
396 struct nlmsg_list *ainfo = NULL;
397 struct nlmsg_list *l;
398 struct rtnl_handle rth;
399 char *filter_dev = NULL;
402 ipaddr_reset_filter(oneline);
403 filter.showqueue = 1;
405 if (filter.family == AF_UNSPEC)
406 filter.family = preferred_family;
409 const unsigned short option_num = compare_string_array(option, *argv);
410 switch (option_num) {
413 get_prefix(&filter.pfx, *argv, filter.family);
414 if (filter.family == AF_UNSPEC) {
415 filter.family = filter.pfx.family;
422 filter.scopemask = -1;
423 if (rtnl_rtscope_a2n(&scope, *argv)) {
424 if (strcmp(*argv, "all") != 0) {
425 invarg("invalid \"scope\"\n", *argv);
427 scope = RT_SCOPE_NOWHERE;
428 filter.scopemask = 0;
430 filter.scope = scope;
438 filter.label = *argv;
444 duparg2("dev", *argv);
452 if (rtnl_open(&rth, 0) < 0)
455 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
456 perror_msg_and_die("Cannot send dump request");
459 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) {
460 error_msg_and_die("Dump terminated");
464 filter.ifindex = ll_name_to_index(filter_dev);
465 if (filter.ifindex <= 0) {
466 error_msg("Device \"%s\" does not exist.", filter_dev);
471 if (filter.family != AF_PACKET) {
472 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
473 perror_msg_and_die("Cannot send dump request");
476 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) {
477 error_msg_and_die("Dump terminated");
482 if (filter.family && filter.family != AF_PACKET) {
483 struct nlmsg_list **lp;
489 while ((l=*lp)!=NULL) {
491 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
492 struct nlmsg_list *a;
494 for (a=ainfo; a; a=a->next) {
495 struct nlmsghdr *n = &a->h;
496 struct ifaddrmsg *ifa = NLMSG_DATA(n);
498 if (ifa->ifa_index != ifi->ifi_index ||
499 (filter.family && filter.family != ifa->ifa_family))
501 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
503 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
505 if (filter.pfx.family || filter.label) {
506 struct rtattr *tb[IFA_MAX+1];
507 memset(tb, 0, sizeof(tb));
508 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
510 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
512 if (filter.pfx.family && tb[IFA_LOCAL]) {
514 memset(&dst, 0, sizeof(dst));
515 dst.family = ifa->ifa_family;
516 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
517 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
524 label = RTA_DATA(tb[IFA_LABEL]);
526 label = ll_idx_n2a(ifa->ifa_index, b1);
527 if (fnmatch(filter.label, label, 0) != 0)
542 for (l=linfo; l; l = l->next) {
543 if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
544 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
545 if (filter.family != AF_PACKET)
546 print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
554 static int default_scope(inet_prefix *lcl)
556 if (lcl->family == AF_INET) {
557 if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127)
558 return RT_SCOPE_HOST;
563 static int ipaddr_modify(int cmd, int argc, char **argv)
565 const char *option[] = { "peer", "remote", "broadcast", "brd",
566 "anycast", "scope", "dev", "label", "local", 0 };
567 struct rtnl_handle rth;
570 struct ifaddrmsg ifa;
583 memset(&req, 0, sizeof(req));
585 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
586 req.n.nlmsg_flags = NLM_F_REQUEST;
587 req.n.nlmsg_type = cmd;
588 req.ifa.ifa_family = preferred_family;
591 const unsigned short option_num = compare_string_array(option, *argv);
592 switch (option_num) {
598 duparg("peer", *argv);
600 get_prefix(&peer, *argv, req.ifa.ifa_family);
601 peer_len = peer.bytelen;
602 if (req.ifa.ifa_family == AF_UNSPEC) {
603 req.ifa.ifa_family = peer.family;
605 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
606 req.ifa.ifa_prefixlen = peer.bitlen;
608 case 2: /* broadcast */
614 duparg("broadcast", *argv);
616 if (strcmp(*argv, "+") == 0) {
619 else if (strcmp(*argv, "-") == 0) {
622 get_addr(&addr, *argv, req.ifa.ifa_family);
623 if (req.ifa.ifa_family == AF_UNSPEC)
624 req.ifa.ifa_family = addr.family;
625 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
626 brd_len = addr.bytelen;
630 case 4: /* anycast */
635 duparg("anycast", *argv);
637 get_addr(&addr, *argv, req.ifa.ifa_family);
638 if (req.ifa.ifa_family == AF_UNSPEC) {
639 req.ifa.ifa_family = addr.family;
641 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
642 any_len = addr.bytelen;
649 if (rtnl_rtscope_a2n(&scope, *argv)) {
650 invarg(*argv, "invalid scope value.");
652 req.ifa.ifa_scope = scope;
663 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
669 duparg2("local", *argv);
671 get_prefix(&lcl, *argv, req.ifa.ifa_family);
672 if (req.ifa.ifa_family == AF_UNSPEC) {
673 req.ifa.ifa_family = lcl.family;
675 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
676 local_len = lcl.bytelen;
683 error_msg("Not enough information: \"dev\" argument is required.");
686 if (l && matches(d, l) != 0) {
687 error_msg_and_die("\"dev\" (%s) must match \"label\" (%s).", d, l);
690 if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
692 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
694 if (req.ifa.ifa_prefixlen == 0)
695 req.ifa.ifa_prefixlen = lcl.bitlen;
697 if (brd_len < 0 && cmd != RTM_DELADDR) {
700 if (req.ifa.ifa_family != AF_INET) {
701 error_msg("Broadcast can be set only for IPv4 addresses");
705 if (brd.bitlen <= 30) {
706 for (i=31; i>=brd.bitlen; i--) {
708 brd.data[0] |= htonl(1<<(31-i));
710 brd.data[0] &= ~htonl(1<<(31-i));
712 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
713 brd_len = brd.bytelen;
716 if (!scoped && cmd != RTM_DELADDR)
717 req.ifa.ifa_scope = default_scope(&lcl);
719 if (rtnl_open(&rth, 0) < 0)
724 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
725 error_msg("Cannot find device \"%s\"", d);
729 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
735 extern int do_ipaddr(int argc, char **argv)
737 const char *commands[] = { "add", "delete", "list", "show", "lst", 0 };
738 unsigned short command_num = 2;
741 command_num = compare_string_array(commands, *argv);
743 switch (command_num) {
745 return ipaddr_modify(RTM_NEWADDR, argc-1, argv+1);
747 return ipaddr_modify(RTM_DELADDR, argc-1, argv+1);
751 return ipaddr_list(argc-1, argv+1);
753 error_msg_and_die("Unknown command %s", *argv);