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 #include <linux/netdevice.h>
29 #include <linux/if_arp.h>
30 #include <linux/sockios.h>
35 #include "ip_common.h"
50 struct rtnl_handle *rth;
53 void print_link_flags(FILE *fp, unsigned flags, unsigned mdown)
56 flags &= ~IFF_RUNNING;
57 #define _PF(f) if (flags&IFF_##f) { \
59 fprintf(fp, #f "%s", flags ? "," : ""); }
79 fprintf(fp, "%x", flags);
81 fprintf(fp, ",M-DOWN");
85 static void print_queuelen(char *name)
90 s = socket(AF_INET, SOCK_STREAM, 0);
94 memset(&ifr, 0, sizeof(ifr));
95 strcpy(ifr.ifr_name, name);
96 if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) {
97 perror("SIOCGIFXQLEN");
104 printf("qlen %d", ifr.ifr_qlen);
107 static int print_linkinfo(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
109 FILE *fp = (FILE*)arg;
110 struct ifinfomsg *ifi = NLMSG_DATA(n);
111 struct rtattr * tb[IFLA_MAX+1];
112 int len = n->nlmsg_len;
115 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
118 len -= NLMSG_LENGTH(sizeof(*ifi));
122 if (filter.ifindex && ifi->ifi_index != filter.ifindex)
124 if (filter.up && !(ifi->ifi_flags&IFF_UP))
127 memset(tb, 0, sizeof(tb));
128 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
129 if (tb[IFLA_IFNAME] == NULL) {
130 error_msg("nil ifname");
134 (!filter.family || filter.family == AF_PACKET) &&
135 fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0))
138 if (n->nlmsg_type == RTM_DELLINK)
139 fprintf(fp, "Deleted ");
141 fprintf(fp, "%d: %s", ifi->ifi_index,
142 tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>");
146 int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]);
148 fprintf(fp, "@NONE: ");
150 fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1));
151 m_flag = ll_index_to_flags(iflink);
152 m_flag = !(m_flag & IFF_UP);
157 print_link_flags(fp, ifi->ifi_flags, m_flag);
160 fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU]));
162 fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC]));
164 if (tb[IFLA_MASTER]) {
166 fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1));
169 if (filter.showqueue)
170 print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME]));
172 if (!filter.family || filter.family == AF_PACKET) {
174 fprintf(fp, "%s", _SL_);
175 fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1)));
177 if (tb[IFLA_ADDRESS]) {
178 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]),
179 RTA_PAYLOAD(tb[IFLA_ADDRESS]),
183 if (tb[IFLA_BROADCAST]) {
184 if (ifi->ifi_flags&IFF_POINTOPOINT)
185 fprintf(fp, " peer ");
187 fprintf(fp, " brd ");
188 fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]),
189 RTA_PAYLOAD(tb[IFLA_BROADCAST]),
199 static int print_addrinfo(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
201 FILE *fp = (FILE*)arg;
202 struct ifaddrmsg *ifa = NLMSG_DATA(n);
203 int len = n->nlmsg_len;
204 struct rtattr * rta_tb[IFA_MAX+1];
208 if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR)
210 len -= NLMSG_LENGTH(sizeof(*ifa));
212 error_msg("wrong nlmsg len %d", len);
216 memset(rta_tb, 0, sizeof(rta_tb));
217 parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)));
219 if (!rta_tb[IFA_LOCAL])
220 rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS];
221 if (!rta_tb[IFA_ADDRESS])
222 rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL];
224 if (filter.ifindex && filter.ifindex != ifa->ifa_index)
226 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
228 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
232 if (rta_tb[IFA_LABEL])
233 label = RTA_DATA(rta_tb[IFA_LABEL]);
235 label = ll_idx_n2a(ifa->ifa_index, b1);
236 if (fnmatch(filter.label, label, 0) != 0)
239 if (filter.pfx.family) {
240 if (rta_tb[IFA_LOCAL]) {
242 memset(&dst, 0, sizeof(dst));
243 dst.family = ifa->ifa_family;
244 memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL]));
245 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
250 if (n->nlmsg_type == RTM_DELADDR)
251 fprintf(fp, "Deleted ");
254 fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index));
255 if (ifa->ifa_family == AF_INET)
256 fprintf(fp, " inet ");
257 else if (ifa->ifa_family == AF_INET6)
258 fprintf(fp, " inet6 ");
260 fprintf(fp, " family %d ", ifa->ifa_family);
262 if (rta_tb[IFA_LOCAL]) {
263 fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family,
264 RTA_PAYLOAD(rta_tb[IFA_LOCAL]),
265 RTA_DATA(rta_tb[IFA_LOCAL]),
266 abuf, sizeof(abuf)));
268 if (rta_tb[IFA_ADDRESS] == NULL ||
269 memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) {
270 fprintf(fp, "/%d ", ifa->ifa_prefixlen);
272 fprintf(fp, " peer %s/%d ",
273 rt_addr_n2a(ifa->ifa_family,
274 RTA_PAYLOAD(rta_tb[IFA_ADDRESS]),
275 RTA_DATA(rta_tb[IFA_ADDRESS]),
281 if (rta_tb[IFA_BROADCAST]) {
282 fprintf(fp, "brd %s ",
283 rt_addr_n2a(ifa->ifa_family,
284 RTA_PAYLOAD(rta_tb[IFA_BROADCAST]),
285 RTA_DATA(rta_tb[IFA_BROADCAST]),
286 abuf, sizeof(abuf)));
288 if (rta_tb[IFA_ANYCAST]) {
289 fprintf(fp, "any %s ",
290 rt_addr_n2a(ifa->ifa_family,
291 RTA_PAYLOAD(rta_tb[IFA_ANYCAST]),
292 RTA_DATA(rta_tb[IFA_ANYCAST]),
293 abuf, sizeof(abuf)));
295 fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1)));
296 if (ifa->ifa_flags&IFA_F_SECONDARY) {
297 ifa->ifa_flags &= ~IFA_F_SECONDARY;
298 fprintf(fp, "secondary ");
300 if (ifa->ifa_flags&IFA_F_TENTATIVE) {
301 ifa->ifa_flags &= ~IFA_F_TENTATIVE;
302 fprintf(fp, "tentative ");
304 if (ifa->ifa_flags&IFA_F_DEPRECATED) {
305 ifa->ifa_flags &= ~IFA_F_DEPRECATED;
306 fprintf(fp, "deprecated ");
308 if (!(ifa->ifa_flags&IFA_F_PERMANENT)) {
309 fprintf(fp, "dynamic ");
311 ifa->ifa_flags &= ~IFA_F_PERMANENT;
313 fprintf(fp, "flags %02x ", ifa->ifa_flags);
314 if (rta_tb[IFA_LABEL])
315 fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL]));
316 if (rta_tb[IFA_CACHEINFO]) {
317 struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]);
319 fprintf(fp, "%s", _SL_);
320 if (ci->ifa_valid == 0xFFFFFFFFU)
321 sprintf(buf, "valid_lft forever");
323 sprintf(buf, "valid_lft %dsec", ci->ifa_valid);
324 if (ci->ifa_prefered == 0xFFFFFFFFU)
325 sprintf(buf+strlen(buf), " preferred_lft forever");
327 sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered);
328 fprintf(fp, " %s", buf);
338 struct nlmsg_list *next;
342 static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp)
344 for ( ;ainfo ; ainfo = ainfo->next) {
345 struct nlmsghdr *n = &ainfo->h;
346 struct ifaddrmsg *ifa = NLMSG_DATA(n);
348 if (n->nlmsg_type != RTM_NEWADDR)
351 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa)))
354 if (ifa->ifa_index != ifindex ||
355 (filter.family && filter.family != ifa->ifa_family))
358 print_addrinfo(NULL, n, fp);
364 static int store_nlmsg(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
366 struct nlmsg_list **linfo = (struct nlmsg_list**)arg;
367 struct nlmsg_list *h;
368 struct nlmsg_list **lp;
370 h = malloc(n->nlmsg_len+sizeof(void*));
374 memcpy(&h->h, n, n->nlmsg_len);
377 for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */;
380 ll_remember_index(who, n, NULL);
384 static void ipaddr_reset_filter(int _oneline)
386 memset(&filter, 0, sizeof(filter));
387 filter.oneline = _oneline;
390 extern int ipaddr_list(int argc, char **argv)
392 const char *option[] = { "to", "scope", "up", "label", "dev", 0 };
393 struct nlmsg_list *linfo = NULL;
394 struct nlmsg_list *ainfo = NULL;
395 struct nlmsg_list *l;
396 struct rtnl_handle rth;
397 char *filter_dev = NULL;
400 ipaddr_reset_filter(oneline);
401 filter.showqueue = 1;
403 if (filter.family == AF_UNSPEC)
404 filter.family = preferred_family;
407 const unsigned short option_num = compare_string_array(option, *argv);
408 switch (option_num) {
411 get_prefix(&filter.pfx, *argv, filter.family);
412 if (filter.family == AF_UNSPEC) {
413 filter.family = filter.pfx.family;
420 filter.scopemask = -1;
421 if (rtnl_rtscope_a2n(&scope, *argv)) {
422 if (strcmp(*argv, "all") != 0) {
423 invarg("invalid \"scope\"\n", *argv);
425 scope = RT_SCOPE_NOWHERE;
426 filter.scopemask = 0;
428 filter.scope = scope;
436 filter.label = *argv;
442 duparg2("dev", *argv);
450 if (rtnl_open(&rth, 0) < 0)
453 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
454 perror_msg_and_die("Cannot send dump request");
457 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) {
458 error_msg_and_die("Dump terminated");
462 filter.ifindex = ll_name_to_index(filter_dev);
463 if (filter.ifindex <= 0) {
464 error_msg("Device \"%s\" does not exist.", filter_dev);
469 if (filter.family != AF_PACKET) {
470 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
471 perror_msg_and_die("Cannot send dump request");
474 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) {
475 error_msg_and_die("Dump terminated");
480 if (filter.family && filter.family != AF_PACKET) {
481 struct nlmsg_list **lp;
487 while ((l=*lp)!=NULL) {
489 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
490 struct nlmsg_list *a;
492 for (a=ainfo; a; a=a->next) {
493 struct nlmsghdr *n = &a->h;
494 struct ifaddrmsg *ifa = NLMSG_DATA(n);
496 if (ifa->ifa_index != ifi->ifi_index ||
497 (filter.family && filter.family != ifa->ifa_family))
499 if ((filter.scope^ifa->ifa_scope)&filter.scopemask)
501 if ((filter.flags^ifa->ifa_flags)&filter.flagmask)
503 if (filter.pfx.family || filter.label) {
504 struct rtattr *tb[IFA_MAX+1];
505 memset(tb, 0, sizeof(tb));
506 parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n));
508 tb[IFA_LOCAL] = tb[IFA_ADDRESS];
510 if (filter.pfx.family && tb[IFA_LOCAL]) {
512 memset(&dst, 0, sizeof(dst));
513 dst.family = ifa->ifa_family;
514 memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL]));
515 if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen))
522 label = RTA_DATA(tb[IFA_LABEL]);
524 label = ll_idx_n2a(ifa->ifa_index, b1);
525 if (fnmatch(filter.label, label, 0) != 0)
540 for (l=linfo; l; l = l->next) {
541 if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) {
542 struct ifinfomsg *ifi = NLMSG_DATA(&l->h);
543 if (filter.family != AF_PACKET)
544 print_selected_addrinfo(ifi->ifi_index, ainfo, stdout);
552 static int default_scope(inet_prefix *lcl)
554 if (lcl->family == AF_INET) {
555 if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127)
556 return RT_SCOPE_HOST;
561 static int ipaddr_modify(int cmd, int argc, char **argv)
563 const char *option[] = { "peer", "remote", "broadcast", "brd",
564 "anycast", "scope", "dev", "label", "local", 0 };
565 struct rtnl_handle rth;
568 struct ifaddrmsg ifa;
581 memset(&req, 0, sizeof(req));
583 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg));
584 req.n.nlmsg_flags = NLM_F_REQUEST;
585 req.n.nlmsg_type = cmd;
586 req.ifa.ifa_family = preferred_family;
589 const unsigned short option_num = compare_string_array(option, *argv);
590 switch (option_num) {
596 duparg("peer", *argv);
598 get_prefix(&peer, *argv, req.ifa.ifa_family);
599 peer_len = peer.bytelen;
600 if (req.ifa.ifa_family == AF_UNSPEC) {
601 req.ifa.ifa_family = peer.family;
603 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen);
604 req.ifa.ifa_prefixlen = peer.bitlen;
606 case 2: /* broadcast */
612 duparg("broadcast", *argv);
614 if (strcmp(*argv, "+") == 0) {
617 else if (strcmp(*argv, "-") == 0) {
620 get_addr(&addr, *argv, req.ifa.ifa_family);
621 if (req.ifa.ifa_family == AF_UNSPEC)
622 req.ifa.ifa_family = addr.family;
623 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen);
624 brd_len = addr.bytelen;
628 case 4: /* anycast */
633 duparg("anycast", *argv);
635 get_addr(&addr, *argv, req.ifa.ifa_family);
636 if (req.ifa.ifa_family == AF_UNSPEC) {
637 req.ifa.ifa_family = addr.family;
639 addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen);
640 any_len = addr.bytelen;
647 if (rtnl_rtscope_a2n(&scope, *argv)) {
648 invarg(*argv, "invalid scope value.");
650 req.ifa.ifa_scope = scope;
661 addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1);
667 duparg2("local", *argv);
669 get_prefix(&lcl, *argv, req.ifa.ifa_family);
670 if (req.ifa.ifa_family == AF_UNSPEC) {
671 req.ifa.ifa_family = lcl.family;
673 addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen);
674 local_len = lcl.bytelen;
681 error_msg("Not enough information: \"dev\" argument is required.");
684 if (l && matches(d, l) != 0) {
685 error_msg_and_die("\"dev\" (%s) must match \"label\" (%s).", d, l);
688 if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
690 addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen);
692 if (req.ifa.ifa_prefixlen == 0)
693 req.ifa.ifa_prefixlen = lcl.bitlen;
695 if (brd_len < 0 && cmd != RTM_DELADDR) {
698 if (req.ifa.ifa_family != AF_INET) {
699 error_msg("Broadcast can be set only for IPv4 addresses");
703 if (brd.bitlen <= 30) {
704 for (i=31; i>=brd.bitlen; i--) {
706 brd.data[0] |= htonl(1<<(31-i));
708 brd.data[0] &= ~htonl(1<<(31-i));
710 addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen);
711 brd_len = brd.bytelen;
714 if (!scoped && cmd != RTM_DELADDR)
715 req.ifa.ifa_scope = default_scope(&lcl);
717 if (rtnl_open(&rth, 0) < 0)
722 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
723 error_msg("Cannot find device \"%s\"", d);
727 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
733 extern int do_ipaddr(int argc, char **argv)
735 const char *commands[] = { "add", "delete", "list", "show", "lst", 0 };
736 unsigned short command_num = 2;
739 command_num = compare_string_array(commands, *argv);
741 switch (command_num) {
743 return ipaddr_modify(RTM_NEWADDR, argc-1, argv+1);
745 return ipaddr_modify(RTM_DELADDR, argc-1, argv+1);
749 return ipaddr_list(argc-1, argv+1);
751 error_msg_and_die("Unknown command %s", *argv);