Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 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 | * Changes: |
| 12 | * Laszlo Valko <valko@linux.karinthy.hu> 990223: address label must be zero terminated |
| 13 | */ |
| 14 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 15 | #include <sys/socket.h> |
| 16 | #include <sys/ioctl.h> |
Glenn L McGrath | 275be87 | 2002-12-16 07:37:21 +0000 | [diff] [blame] | 17 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 18 | #include <fnmatch.h> |
Glenn L McGrath | 275be87 | 2002-12-16 07:37:21 +0000 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | #include <arpa/inet.h> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 24 | |
Glenn L McGrath | 84cc4e7 | 2002-12-11 03:55:52 +0000 | [diff] [blame] | 25 | #define sysinfo kernel_sysinfo |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 26 | #include <linux/if_arp.h> |
Glenn L McGrath | 84cc4e7 | 2002-12-11 03:55:52 +0000 | [diff] [blame] | 27 | #undef sysinfo |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 28 | |
| 29 | #include "rt_names.h" |
| 30 | #include "utils.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 31 | |
Glenn L McGrath | 275be87 | 2002-12-16 07:37:21 +0000 | [diff] [blame] | 32 | #include "libbb.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 33 | |
| 34 | static struct |
| 35 | { |
| 36 | int ifindex; |
| 37 | int family; |
| 38 | int oneline; |
| 39 | int showqueue; |
| 40 | inet_prefix pfx; |
| 41 | int scope, scopemask; |
| 42 | int flags, flagmask; |
| 43 | int up; |
| 44 | char *label; |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame^] | 45 | int flushed; |
| 46 | char *flushb; |
| 47 | int flushp; |
| 48 | int flushe; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 49 | struct rtnl_handle *rth; |
| 50 | } filter; |
| 51 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 52 | void print_link_flags(FILE *fp, unsigned flags, unsigned mdown) |
| 53 | { |
| 54 | fprintf(fp, "<"); |
| 55 | flags &= ~IFF_RUNNING; |
| 56 | #define _PF(f) if (flags&IFF_##f) { \ |
| 57 | flags &= ~IFF_##f ; \ |
| 58 | fprintf(fp, #f "%s", flags ? "," : ""); } |
| 59 | _PF(LOOPBACK); |
| 60 | _PF(BROADCAST); |
| 61 | _PF(POINTOPOINT); |
| 62 | _PF(MULTICAST); |
| 63 | _PF(NOARP); |
| 64 | #if 0 |
| 65 | _PF(ALLMULTI); |
| 66 | _PF(PROMISC); |
| 67 | _PF(MASTER); |
| 68 | _PF(SLAVE); |
| 69 | _PF(DEBUG); |
| 70 | _PF(DYNAMIC); |
| 71 | _PF(AUTOMEDIA); |
| 72 | _PF(PORTSEL); |
| 73 | _PF(NOTRAILERS); |
| 74 | #endif |
| 75 | _PF(UP); |
| 76 | #undef _PF |
| 77 | if (flags) |
| 78 | fprintf(fp, "%x", flags); |
| 79 | if (mdown) |
| 80 | fprintf(fp, ",M-DOWN"); |
| 81 | fprintf(fp, "> "); |
| 82 | } |
| 83 | |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 84 | static void print_queuelen(char *name) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 85 | { |
| 86 | struct ifreq ifr; |
| 87 | int s; |
| 88 | |
| 89 | s = socket(AF_INET, SOCK_STREAM, 0); |
| 90 | if (s < 0) |
| 91 | return; |
| 92 | |
| 93 | memset(&ifr, 0, sizeof(ifr)); |
| 94 | strcpy(ifr.ifr_name, name); |
| 95 | if (ioctl(s, SIOCGIFTXQLEN, &ifr) < 0) { |
| 96 | perror("SIOCGIFXQLEN"); |
| 97 | close(s); |
| 98 | return; |
| 99 | } |
| 100 | close(s); |
| 101 | |
| 102 | if (ifr.ifr_qlen) |
| 103 | printf("qlen %d", ifr.ifr_qlen); |
| 104 | } |
| 105 | |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 106 | static int print_linkinfo(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 107 | { |
| 108 | FILE *fp = (FILE*)arg; |
| 109 | struct ifinfomsg *ifi = NLMSG_DATA(n); |
| 110 | struct rtattr * tb[IFLA_MAX+1]; |
| 111 | int len = n->nlmsg_len; |
| 112 | unsigned m_flag = 0; |
| 113 | |
| 114 | if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK) |
| 115 | return 0; |
| 116 | |
| 117 | len -= NLMSG_LENGTH(sizeof(*ifi)); |
| 118 | if (len < 0) |
| 119 | return -1; |
| 120 | |
| 121 | if (filter.ifindex && ifi->ifi_index != filter.ifindex) |
| 122 | return 0; |
| 123 | if (filter.up && !(ifi->ifi_flags&IFF_UP)) |
| 124 | return 0; |
| 125 | |
| 126 | memset(tb, 0, sizeof(tb)); |
| 127 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); |
| 128 | if (tb[IFLA_IFNAME] == NULL) { |
Glenn L McGrath | df72536 | 2002-11-28 10:49:14 +0000 | [diff] [blame] | 129 | error_msg("nil ifname"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 130 | return -1; |
| 131 | } |
| 132 | if (filter.label && |
| 133 | (!filter.family || filter.family == AF_PACKET) && |
| 134 | fnmatch(filter.label, RTA_DATA(tb[IFLA_IFNAME]), 0)) |
| 135 | return 0; |
| 136 | |
| 137 | if (n->nlmsg_type == RTM_DELLINK) |
| 138 | fprintf(fp, "Deleted "); |
| 139 | |
| 140 | fprintf(fp, "%d: %s", ifi->ifi_index, |
| 141 | tb[IFLA_IFNAME] ? (char*)RTA_DATA(tb[IFLA_IFNAME]) : "<nil>"); |
| 142 | |
| 143 | if (tb[IFLA_LINK]) { |
| 144 | SPRINT_BUF(b1); |
| 145 | int iflink = *(int*)RTA_DATA(tb[IFLA_LINK]); |
| 146 | if (iflink == 0) |
| 147 | fprintf(fp, "@NONE: "); |
| 148 | else { |
| 149 | fprintf(fp, "@%s: ", ll_idx_n2a(iflink, b1)); |
| 150 | m_flag = ll_index_to_flags(iflink); |
| 151 | m_flag = !(m_flag & IFF_UP); |
| 152 | } |
| 153 | } else { |
| 154 | fprintf(fp, ": "); |
| 155 | } |
| 156 | print_link_flags(fp, ifi->ifi_flags, m_flag); |
| 157 | |
| 158 | if (tb[IFLA_MTU]) |
| 159 | fprintf(fp, "mtu %u ", *(int*)RTA_DATA(tb[IFLA_MTU])); |
| 160 | if (tb[IFLA_QDISC]) |
| 161 | fprintf(fp, "qdisc %s ", (char*)RTA_DATA(tb[IFLA_QDISC])); |
| 162 | #ifdef IFLA_MASTER |
| 163 | if (tb[IFLA_MASTER]) { |
| 164 | SPRINT_BUF(b1); |
| 165 | fprintf(fp, "master %s ", ll_idx_n2a(*(int*)RTA_DATA(tb[IFLA_MASTER]), b1)); |
| 166 | } |
| 167 | #endif |
| 168 | if (filter.showqueue) |
| 169 | print_queuelen((char*)RTA_DATA(tb[IFLA_IFNAME])); |
| 170 | |
| 171 | if (!filter.family || filter.family == AF_PACKET) { |
| 172 | SPRINT_BUF(b1); |
| 173 | fprintf(fp, "%s", _SL_); |
| 174 | fprintf(fp, " link/%s ", ll_type_n2a(ifi->ifi_type, b1, sizeof(b1))); |
| 175 | |
| 176 | if (tb[IFLA_ADDRESS]) { |
| 177 | fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]), |
| 178 | RTA_PAYLOAD(tb[IFLA_ADDRESS]), |
| 179 | ifi->ifi_type, |
| 180 | b1, sizeof(b1))); |
| 181 | } |
| 182 | if (tb[IFLA_BROADCAST]) { |
| 183 | if (ifi->ifi_flags&IFF_POINTOPOINT) |
| 184 | fprintf(fp, " peer "); |
| 185 | else |
| 186 | fprintf(fp, " brd "); |
| 187 | fprintf(fp, "%s", ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]), |
| 188 | RTA_PAYLOAD(tb[IFLA_BROADCAST]), |
| 189 | ifi->ifi_type, |
| 190 | b1, sizeof(b1))); |
| 191 | } |
| 192 | } |
| 193 | fprintf(fp, "\n"); |
| 194 | fflush(fp); |
| 195 | return 0; |
| 196 | } |
| 197 | |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame^] | 198 | static int flush_update(void) |
| 199 | { |
| 200 | if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) { |
| 201 | perror("Failed to send flush request\n"); |
| 202 | return -1; |
| 203 | } |
| 204 | filter.flushp = 0; |
| 205 | return 0; |
| 206 | } |
| 207 | |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 208 | static int print_addrinfo(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 209 | { |
| 210 | FILE *fp = (FILE*)arg; |
| 211 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 212 | int len = n->nlmsg_len; |
| 213 | struct rtattr * rta_tb[IFA_MAX+1]; |
| 214 | char abuf[256]; |
| 215 | SPRINT_BUF(b1); |
| 216 | |
| 217 | if (n->nlmsg_type != RTM_NEWADDR && n->nlmsg_type != RTM_DELADDR) |
| 218 | return 0; |
| 219 | len -= NLMSG_LENGTH(sizeof(*ifa)); |
| 220 | if (len < 0) { |
Glenn L McGrath | df72536 | 2002-11-28 10:49:14 +0000 | [diff] [blame] | 221 | error_msg("wrong nlmsg len %d", len); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 222 | return -1; |
| 223 | } |
| 224 | |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame^] | 225 | if (filter.flushb && n->nlmsg_type != RTM_NEWADDR) |
| 226 | return 0; |
| 227 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 228 | memset(rta_tb, 0, sizeof(rta_tb)); |
| 229 | parse_rtattr(rta_tb, IFA_MAX, IFA_RTA(ifa), n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa))); |
| 230 | |
| 231 | if (!rta_tb[IFA_LOCAL]) |
| 232 | rta_tb[IFA_LOCAL] = rta_tb[IFA_ADDRESS]; |
| 233 | if (!rta_tb[IFA_ADDRESS]) |
| 234 | rta_tb[IFA_ADDRESS] = rta_tb[IFA_LOCAL]; |
| 235 | |
| 236 | if (filter.ifindex && filter.ifindex != ifa->ifa_index) |
| 237 | return 0; |
| 238 | if ((filter.scope^ifa->ifa_scope)&filter.scopemask) |
| 239 | return 0; |
| 240 | if ((filter.flags^ifa->ifa_flags)&filter.flagmask) |
| 241 | return 0; |
| 242 | if (filter.label) { |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 243 | const char *label; |
| 244 | if (rta_tb[IFA_LABEL]) |
| 245 | label = RTA_DATA(rta_tb[IFA_LABEL]); |
| 246 | else |
| 247 | label = ll_idx_n2a(ifa->ifa_index, b1); |
| 248 | if (fnmatch(filter.label, label, 0) != 0) |
| 249 | return 0; |
| 250 | } |
| 251 | if (filter.pfx.family) { |
| 252 | if (rta_tb[IFA_LOCAL]) { |
| 253 | inet_prefix dst; |
| 254 | memset(&dst, 0, sizeof(dst)); |
| 255 | dst.family = ifa->ifa_family; |
| 256 | memcpy(&dst.data, RTA_DATA(rta_tb[IFA_LOCAL]), RTA_PAYLOAD(rta_tb[IFA_LOCAL])); |
| 257 | if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen)) |
| 258 | return 0; |
| 259 | } |
| 260 | } |
| 261 | |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame^] | 262 | if (filter.flushb) { |
| 263 | struct nlmsghdr *fn; |
| 264 | if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) { |
| 265 | if (flush_update()) |
| 266 | return -1; |
| 267 | } |
| 268 | fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp)); |
| 269 | memcpy(fn, n, n->nlmsg_len); |
| 270 | fn->nlmsg_type = RTM_DELADDR; |
| 271 | fn->nlmsg_flags = NLM_F_REQUEST; |
| 272 | fn->nlmsg_seq = ++filter.rth->seq; |
| 273 | filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb; |
| 274 | filter.flushed++; |
| 275 | return 0; |
| 276 | } |
| 277 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 278 | if (n->nlmsg_type == RTM_DELADDR) |
| 279 | fprintf(fp, "Deleted "); |
| 280 | |
| 281 | if (filter.oneline) |
| 282 | fprintf(fp, "%u: %s", ifa->ifa_index, ll_index_to_name(ifa->ifa_index)); |
| 283 | if (ifa->ifa_family == AF_INET) |
| 284 | fprintf(fp, " inet "); |
| 285 | else if (ifa->ifa_family == AF_INET6) |
| 286 | fprintf(fp, " inet6 "); |
| 287 | else |
| 288 | fprintf(fp, " family %d ", ifa->ifa_family); |
| 289 | |
| 290 | if (rta_tb[IFA_LOCAL]) { |
| 291 | fprintf(fp, "%s", rt_addr_n2a(ifa->ifa_family, |
| 292 | RTA_PAYLOAD(rta_tb[IFA_LOCAL]), |
| 293 | RTA_DATA(rta_tb[IFA_LOCAL]), |
| 294 | abuf, sizeof(abuf))); |
| 295 | |
| 296 | if (rta_tb[IFA_ADDRESS] == NULL || |
| 297 | memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0) { |
| 298 | fprintf(fp, "/%d ", ifa->ifa_prefixlen); |
| 299 | } else { |
| 300 | fprintf(fp, " peer %s/%d ", |
| 301 | rt_addr_n2a(ifa->ifa_family, |
| 302 | RTA_PAYLOAD(rta_tb[IFA_ADDRESS]), |
| 303 | RTA_DATA(rta_tb[IFA_ADDRESS]), |
| 304 | abuf, sizeof(abuf)), |
| 305 | ifa->ifa_prefixlen); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | if (rta_tb[IFA_BROADCAST]) { |
| 310 | fprintf(fp, "brd %s ", |
| 311 | rt_addr_n2a(ifa->ifa_family, |
| 312 | RTA_PAYLOAD(rta_tb[IFA_BROADCAST]), |
| 313 | RTA_DATA(rta_tb[IFA_BROADCAST]), |
| 314 | abuf, sizeof(abuf))); |
| 315 | } |
| 316 | if (rta_tb[IFA_ANYCAST]) { |
| 317 | fprintf(fp, "any %s ", |
| 318 | rt_addr_n2a(ifa->ifa_family, |
| 319 | RTA_PAYLOAD(rta_tb[IFA_ANYCAST]), |
| 320 | RTA_DATA(rta_tb[IFA_ANYCAST]), |
| 321 | abuf, sizeof(abuf))); |
| 322 | } |
| 323 | fprintf(fp, "scope %s ", rtnl_rtscope_n2a(ifa->ifa_scope, b1, sizeof(b1))); |
| 324 | if (ifa->ifa_flags&IFA_F_SECONDARY) { |
| 325 | ifa->ifa_flags &= ~IFA_F_SECONDARY; |
| 326 | fprintf(fp, "secondary "); |
| 327 | } |
| 328 | if (ifa->ifa_flags&IFA_F_TENTATIVE) { |
| 329 | ifa->ifa_flags &= ~IFA_F_TENTATIVE; |
| 330 | fprintf(fp, "tentative "); |
| 331 | } |
| 332 | if (ifa->ifa_flags&IFA_F_DEPRECATED) { |
| 333 | ifa->ifa_flags &= ~IFA_F_DEPRECATED; |
| 334 | fprintf(fp, "deprecated "); |
| 335 | } |
| 336 | if (!(ifa->ifa_flags&IFA_F_PERMANENT)) { |
| 337 | fprintf(fp, "dynamic "); |
| 338 | } else |
| 339 | ifa->ifa_flags &= ~IFA_F_PERMANENT; |
| 340 | if (ifa->ifa_flags) |
| 341 | fprintf(fp, "flags %02x ", ifa->ifa_flags); |
| 342 | if (rta_tb[IFA_LABEL]) |
| 343 | fprintf(fp, "%s", (char*)RTA_DATA(rta_tb[IFA_LABEL])); |
| 344 | if (rta_tb[IFA_CACHEINFO]) { |
| 345 | struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]); |
| 346 | char buf[128]; |
| 347 | fprintf(fp, "%s", _SL_); |
| 348 | if (ci->ifa_valid == 0xFFFFFFFFU) |
| 349 | sprintf(buf, "valid_lft forever"); |
| 350 | else |
| 351 | sprintf(buf, "valid_lft %dsec", ci->ifa_valid); |
| 352 | if (ci->ifa_prefered == 0xFFFFFFFFU) |
| 353 | sprintf(buf+strlen(buf), " preferred_lft forever"); |
| 354 | else |
| 355 | sprintf(buf+strlen(buf), " preferred_lft %dsec", ci->ifa_prefered); |
| 356 | fprintf(fp, " %s", buf); |
| 357 | } |
| 358 | fprintf(fp, "\n"); |
| 359 | fflush(fp); |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | |
| 364 | struct nlmsg_list |
| 365 | { |
| 366 | struct nlmsg_list *next; |
| 367 | struct nlmsghdr h; |
| 368 | }; |
| 369 | |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 370 | static int print_selected_addrinfo(int ifindex, struct nlmsg_list *ainfo, FILE *fp) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 371 | { |
| 372 | for ( ;ainfo ; ainfo = ainfo->next) { |
| 373 | struct nlmsghdr *n = &ainfo->h; |
| 374 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 375 | |
| 376 | if (n->nlmsg_type != RTM_NEWADDR) |
| 377 | continue; |
| 378 | |
| 379 | if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifa))) |
| 380 | return -1; |
| 381 | |
| 382 | if (ifa->ifa_index != ifindex || |
| 383 | (filter.family && filter.family != ifa->ifa_family)) |
| 384 | continue; |
| 385 | |
| 386 | print_addrinfo(NULL, n, fp); |
| 387 | } |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 392 | static int store_nlmsg(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 393 | { |
| 394 | struct nlmsg_list **linfo = (struct nlmsg_list**)arg; |
| 395 | struct nlmsg_list *h; |
| 396 | struct nlmsg_list **lp; |
| 397 | |
| 398 | h = malloc(n->nlmsg_len+sizeof(void*)); |
| 399 | if (h == NULL) |
| 400 | return -1; |
| 401 | |
| 402 | memcpy(&h->h, n, n->nlmsg_len); |
| 403 | h->next = NULL; |
| 404 | |
| 405 | for (lp = linfo; *lp; lp = &(*lp)->next) /* NOTHING */; |
| 406 | *lp = h; |
| 407 | |
| 408 | ll_remember_index(who, n, NULL); |
| 409 | return 0; |
| 410 | } |
| 411 | |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 412 | static void ipaddr_reset_filter(int _oneline) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 413 | { |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 414 | memset(&filter, 0, sizeof(filter)); |
| 415 | filter.oneline = _oneline; |
| 416 | } |
| 417 | |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame^] | 418 | extern int ipaddr_list_or_flush(int argc, char **argv, int flush) |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 419 | { |
| 420 | const char *option[] = { "to", "scope", "up", "label", "dev", 0 }; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 421 | struct nlmsg_list *linfo = NULL; |
| 422 | struct nlmsg_list *ainfo = NULL; |
| 423 | struct nlmsg_list *l; |
| 424 | struct rtnl_handle rth; |
| 425 | char *filter_dev = NULL; |
| 426 | int no_link = 0; |
| 427 | |
| 428 | ipaddr_reset_filter(oneline); |
| 429 | filter.showqueue = 1; |
| 430 | |
| 431 | if (filter.family == AF_UNSPEC) |
| 432 | filter.family = preferred_family; |
| 433 | |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame^] | 434 | if (flush) { |
| 435 | if (argc <= 0) { |
| 436 | fprintf(stderr, "Flush requires arguments.\n"); |
| 437 | return -1; |
| 438 | } |
| 439 | if (filter.family == AF_PACKET) { |
| 440 | fprintf(stderr, "Cannot flush link addresses.\n"); |
| 441 | return -1; |
| 442 | } |
| 443 | } |
| 444 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 445 | while (argc > 0) { |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 446 | const unsigned short option_num = compare_string_array(option, *argv); |
| 447 | switch (option_num) { |
| 448 | case 0: /* to */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 449 | NEXT_ARG(); |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 450 | get_prefix(&filter.pfx, *argv, filter.family); |
| 451 | if (filter.family == AF_UNSPEC) { |
| 452 | filter.family = filter.pfx.family; |
| 453 | } |
| 454 | break; |
| 455 | case 1: /* scope */ |
| 456 | { |
| 457 | int scope = 0; |
| 458 | NEXT_ARG(); |
| 459 | filter.scopemask = -1; |
| 460 | if (rtnl_rtscope_a2n(&scope, *argv)) { |
| 461 | if (strcmp(*argv, "all") != 0) { |
| 462 | invarg("invalid \"scope\"\n", *argv); |
| 463 | } |
| 464 | scope = RT_SCOPE_NOWHERE; |
| 465 | filter.scopemask = 0; |
| 466 | } |
| 467 | filter.scope = scope; |
| 468 | break; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 469 | } |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 470 | case 2: /* up */ |
| 471 | filter.up = 1; |
| 472 | break; |
| 473 | case 3: /* label */ |
| 474 | NEXT_ARG(); |
| 475 | filter.label = *argv; |
| 476 | break; |
| 477 | case 4: /* dev */ |
| 478 | NEXT_ARG(); |
| 479 | default: |
| 480 | if (filter_dev) { |
| 481 | duparg2("dev", *argv); |
| 482 | } |
| 483 | filter_dev = *argv; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 484 | } |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 485 | argv++; |
| 486 | argc--; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | if (rtnl_open(&rth, 0) < 0) |
| 490 | exit(1); |
| 491 | |
| 492 | if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) { |
Glenn L McGrath | df72536 | 2002-11-28 10:49:14 +0000 | [diff] [blame] | 493 | perror_msg_and_die("Cannot send dump request"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) { |
Glenn L McGrath | df72536 | 2002-11-28 10:49:14 +0000 | [diff] [blame] | 497 | error_msg_and_die("Dump terminated"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | if (filter_dev) { |
| 501 | filter.ifindex = ll_name_to_index(filter_dev); |
| 502 | if (filter.ifindex <= 0) { |
Glenn L McGrath | df72536 | 2002-11-28 10:49:14 +0000 | [diff] [blame] | 503 | error_msg("Device \"%s\" does not exist.", filter_dev); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 504 | return -1; |
| 505 | } |
| 506 | } |
| 507 | |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame^] | 508 | if (flush) { |
| 509 | int round = 0; |
| 510 | char flushb[4096-512]; |
| 511 | |
| 512 | filter.flushb = flushb; |
| 513 | filter.flushp = 0; |
| 514 | filter.flushe = sizeof(flushb); |
| 515 | filter.rth = &rth; |
| 516 | |
| 517 | for (;;) { |
| 518 | if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) { |
| 519 | perror("Cannot send dump request"); |
| 520 | exit(1); |
| 521 | } |
| 522 | filter.flushed = 0; |
| 523 | if (rtnl_dump_filter(&rth, print_addrinfo, stdout, NULL, NULL) < 0) { |
| 524 | fprintf(stderr, "Flush terminated\n"); |
| 525 | exit(1); |
| 526 | } |
| 527 | if (filter.flushed == 0) { |
| 528 | if (round == 0) |
| 529 | fprintf(stderr, "Nothing to flush.\n"); |
| 530 | fflush(stdout); |
| 531 | return 0; |
| 532 | } |
| 533 | round++; |
| 534 | if (flush_update() < 0) |
| 535 | exit(1); |
| 536 | } |
| 537 | } |
| 538 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 539 | if (filter.family != AF_PACKET) { |
| 540 | if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) { |
Glenn L McGrath | df72536 | 2002-11-28 10:49:14 +0000 | [diff] [blame] | 541 | perror_msg_and_die("Cannot send dump request"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) { |
Glenn L McGrath | df72536 | 2002-11-28 10:49:14 +0000 | [diff] [blame] | 545 | error_msg_and_die("Dump terminated"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
| 549 | |
| 550 | if (filter.family && filter.family != AF_PACKET) { |
| 551 | struct nlmsg_list **lp; |
| 552 | lp=&linfo; |
| 553 | |
| 554 | if (filter.oneline) |
| 555 | no_link = 1; |
| 556 | |
| 557 | while ((l=*lp)!=NULL) { |
| 558 | int ok = 0; |
| 559 | struct ifinfomsg *ifi = NLMSG_DATA(&l->h); |
| 560 | struct nlmsg_list *a; |
| 561 | |
| 562 | for (a=ainfo; a; a=a->next) { |
| 563 | struct nlmsghdr *n = &a->h; |
| 564 | struct ifaddrmsg *ifa = NLMSG_DATA(n); |
| 565 | |
| 566 | if (ifa->ifa_index != ifi->ifi_index || |
| 567 | (filter.family && filter.family != ifa->ifa_family)) |
| 568 | continue; |
| 569 | if ((filter.scope^ifa->ifa_scope)&filter.scopemask) |
| 570 | continue; |
| 571 | if ((filter.flags^ifa->ifa_flags)&filter.flagmask) |
| 572 | continue; |
| 573 | if (filter.pfx.family || filter.label) { |
| 574 | struct rtattr *tb[IFA_MAX+1]; |
| 575 | memset(tb, 0, sizeof(tb)); |
| 576 | parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), IFA_PAYLOAD(n)); |
| 577 | if (!tb[IFA_LOCAL]) |
| 578 | tb[IFA_LOCAL] = tb[IFA_ADDRESS]; |
| 579 | |
| 580 | if (filter.pfx.family && tb[IFA_LOCAL]) { |
| 581 | inet_prefix dst; |
| 582 | memset(&dst, 0, sizeof(dst)); |
| 583 | dst.family = ifa->ifa_family; |
| 584 | memcpy(&dst.data, RTA_DATA(tb[IFA_LOCAL]), RTA_PAYLOAD(tb[IFA_LOCAL])); |
| 585 | if (inet_addr_match(&dst, &filter.pfx, filter.pfx.bitlen)) |
| 586 | continue; |
| 587 | } |
| 588 | if (filter.label) { |
| 589 | SPRINT_BUF(b1); |
| 590 | const char *label; |
| 591 | if (tb[IFA_LABEL]) |
| 592 | label = RTA_DATA(tb[IFA_LABEL]); |
| 593 | else |
| 594 | label = ll_idx_n2a(ifa->ifa_index, b1); |
| 595 | if (fnmatch(filter.label, label, 0) != 0) |
| 596 | continue; |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | ok = 1; |
| 601 | break; |
| 602 | } |
| 603 | if (!ok) |
| 604 | *lp = l->next; |
| 605 | else |
| 606 | lp = &l->next; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | for (l=linfo; l; l = l->next) { |
| 611 | if (no_link || print_linkinfo(NULL, &l->h, stdout) == 0) { |
| 612 | struct ifinfomsg *ifi = NLMSG_DATA(&l->h); |
| 613 | if (filter.family != AF_PACKET) |
| 614 | print_selected_addrinfo(ifi->ifi_index, ainfo, stdout); |
| 615 | } |
| 616 | fflush(stdout); |
| 617 | } |
| 618 | |
| 619 | exit(0); |
| 620 | } |
| 621 | |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 622 | static int default_scope(inet_prefix *lcl) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 623 | { |
| 624 | if (lcl->family == AF_INET) { |
| 625 | if (lcl->bytelen >= 1 && *(__u8*)&lcl->data == 127) |
| 626 | return RT_SCOPE_HOST; |
| 627 | } |
| 628 | return 0; |
| 629 | } |
| 630 | |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 631 | static int ipaddr_modify(int cmd, int argc, char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 632 | { |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 633 | const char *option[] = { "peer", "remote", "broadcast", "brd", |
| 634 | "anycast", "scope", "dev", "label", "local", 0 }; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 635 | struct rtnl_handle rth; |
| 636 | struct { |
| 637 | struct nlmsghdr n; |
| 638 | struct ifaddrmsg ifa; |
| 639 | char buf[256]; |
| 640 | } req; |
| 641 | char *d = NULL; |
| 642 | char *l = NULL; |
| 643 | inet_prefix lcl; |
| 644 | inet_prefix peer; |
| 645 | int local_len = 0; |
| 646 | int peer_len = 0; |
| 647 | int brd_len = 0; |
| 648 | int any_len = 0; |
| 649 | int scoped = 0; |
| 650 | |
| 651 | memset(&req, 0, sizeof(req)); |
| 652 | |
| 653 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); |
| 654 | req.n.nlmsg_flags = NLM_F_REQUEST; |
| 655 | req.n.nlmsg_type = cmd; |
| 656 | req.ifa.ifa_family = preferred_family; |
| 657 | |
| 658 | while (argc > 0) { |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 659 | const unsigned short option_num = compare_string_array(option, *argv); |
| 660 | switch (option_num) { |
| 661 | case 0: /* peer */ |
| 662 | case 1: /* remote */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 663 | NEXT_ARG(); |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 664 | |
| 665 | if (peer_len) { |
| 666 | duparg("peer", *argv); |
| 667 | } |
| 668 | get_prefix(&peer, *argv, req.ifa.ifa_family); |
| 669 | peer_len = peer.bytelen; |
| 670 | if (req.ifa.ifa_family == AF_UNSPEC) { |
| 671 | req.ifa.ifa_family = peer.family; |
| 672 | } |
| 673 | addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &peer.data, peer.bytelen); |
| 674 | req.ifa.ifa_prefixlen = peer.bitlen; |
| 675 | break; |
| 676 | case 2: /* broadcast */ |
| 677 | case 3: /* brd */ |
| 678 | { |
| 679 | inet_prefix addr; |
| 680 | NEXT_ARG(); |
| 681 | if (brd_len) { |
| 682 | duparg("broadcast", *argv); |
| 683 | } |
| 684 | if (strcmp(*argv, "+") == 0) { |
| 685 | brd_len = -1; |
| 686 | } |
| 687 | else if (strcmp(*argv, "-") == 0) { |
| 688 | brd_len = -2; |
| 689 | } else { |
| 690 | get_addr(&addr, *argv, req.ifa.ifa_family); |
| 691 | if (req.ifa.ifa_family == AF_UNSPEC) |
| 692 | req.ifa.ifa_family = addr.family; |
| 693 | addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &addr.data, addr.bytelen); |
| 694 | brd_len = addr.bytelen; |
| 695 | } |
| 696 | break; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 697 | } |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 698 | case 4: /* anycast */ |
| 699 | { |
| 700 | inet_prefix addr; |
| 701 | NEXT_ARG(); |
| 702 | if (any_len) { |
| 703 | duparg("anycast", *argv); |
| 704 | } |
| 705 | get_addr(&addr, *argv, req.ifa.ifa_family); |
| 706 | if (req.ifa.ifa_family == AF_UNSPEC) { |
| 707 | req.ifa.ifa_family = addr.family; |
| 708 | } |
| 709 | addattr_l(&req.n, sizeof(req), IFA_ANYCAST, &addr.data, addr.bytelen); |
| 710 | any_len = addr.bytelen; |
| 711 | break; |
| 712 | } |
| 713 | case 5: /* scope */ |
| 714 | { |
| 715 | int scope = 0; |
| 716 | NEXT_ARG(); |
| 717 | if (rtnl_rtscope_a2n(&scope, *argv)) { |
| 718 | invarg(*argv, "invalid scope value."); |
| 719 | } |
| 720 | req.ifa.ifa_scope = scope; |
| 721 | scoped = 1; |
| 722 | break; |
| 723 | } |
| 724 | case 6: /* dev */ |
| 725 | NEXT_ARG(); |
| 726 | d = *argv; |
| 727 | break; |
| 728 | case 7: /* label */ |
| 729 | NEXT_ARG(); |
| 730 | l = *argv; |
| 731 | addattr_l(&req.n, sizeof(req), IFA_LABEL, l, strlen(l)+1); |
| 732 | break; |
| 733 | case 8: /* local */ |
| 734 | NEXT_ARG(); |
| 735 | default: |
| 736 | if (local_len) { |
| 737 | duparg2("local", *argv); |
| 738 | } |
| 739 | get_prefix(&lcl, *argv, req.ifa.ifa_family); |
| 740 | if (req.ifa.ifa_family == AF_UNSPEC) { |
| 741 | req.ifa.ifa_family = lcl.family; |
| 742 | } |
| 743 | addattr_l(&req.n, sizeof(req), IFA_LOCAL, &lcl.data, lcl.bytelen); |
| 744 | local_len = lcl.bytelen; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 745 | } |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 746 | argc--; |
| 747 | argv++; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 748 | } |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 749 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 750 | if (d == NULL) { |
Glenn L McGrath | df72536 | 2002-11-28 10:49:14 +0000 | [diff] [blame] | 751 | error_msg("Not enough information: \"dev\" argument is required."); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 752 | return -1; |
| 753 | } |
| 754 | if (l && matches(d, l) != 0) { |
Glenn L McGrath | df72536 | 2002-11-28 10:49:14 +0000 | [diff] [blame] | 755 | error_msg_and_die("\"dev\" (%s) must match \"label\" (%s).", d, l); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | if (peer_len == 0 && local_len && cmd != RTM_DELADDR) { |
| 759 | peer = lcl; |
| 760 | addattr_l(&req.n, sizeof(req), IFA_ADDRESS, &lcl.data, lcl.bytelen); |
| 761 | } |
| 762 | if (req.ifa.ifa_prefixlen == 0) |
| 763 | req.ifa.ifa_prefixlen = lcl.bitlen; |
| 764 | |
| 765 | if (brd_len < 0 && cmd != RTM_DELADDR) { |
| 766 | inet_prefix brd; |
| 767 | int i; |
| 768 | if (req.ifa.ifa_family != AF_INET) { |
Glenn L McGrath | df72536 | 2002-11-28 10:49:14 +0000 | [diff] [blame] | 769 | error_msg("Broadcast can be set only for IPv4 addresses"); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 770 | return -1; |
| 771 | } |
| 772 | brd = peer; |
| 773 | if (brd.bitlen <= 30) { |
| 774 | for (i=31; i>=brd.bitlen; i--) { |
| 775 | if (brd_len == -1) |
| 776 | brd.data[0] |= htonl(1<<(31-i)); |
| 777 | else |
| 778 | brd.data[0] &= ~htonl(1<<(31-i)); |
| 779 | } |
| 780 | addattr_l(&req.n, sizeof(req), IFA_BROADCAST, &brd.data, brd.bytelen); |
| 781 | brd_len = brd.bytelen; |
| 782 | } |
| 783 | } |
| 784 | if (!scoped && cmd != RTM_DELADDR) |
| 785 | req.ifa.ifa_scope = default_scope(&lcl); |
| 786 | |
| 787 | if (rtnl_open(&rth, 0) < 0) |
| 788 | exit(1); |
| 789 | |
| 790 | ll_init_map(&rth); |
| 791 | |
| 792 | if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) { |
Glenn L McGrath | df72536 | 2002-11-28 10:49:14 +0000 | [diff] [blame] | 793 | error_msg("Cannot find device \"%s\"", d); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 794 | return -1; |
| 795 | } |
| 796 | |
| 797 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) |
| 798 | exit(2); |
| 799 | |
| 800 | exit(0); |
| 801 | } |
| 802 | |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 803 | extern int do_ipaddr(int argc, char **argv) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 804 | { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame^] | 805 | const char *commands[] = { "add", "delete", "list", "show", "lst", "flush", 0 }; |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 806 | unsigned short command_num = 2; |
| 807 | |
| 808 | if (*argv) { |
| 809 | command_num = compare_string_array(commands, *argv); |
| 810 | } |
| 811 | switch (command_num) { |
| 812 | case 0: /* add */ |
| 813 | return ipaddr_modify(RTM_NEWADDR, argc-1, argv+1); |
| 814 | case 1: /* delete */ |
| 815 | return ipaddr_modify(RTM_DELADDR, argc-1, argv+1); |
| 816 | case 2: /* list */ |
| 817 | case 3: /* show */ |
| 818 | case 4: /* lst */ |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame^] | 819 | return ipaddr_list_or_flush(argc-1, argv+1, 0); |
| 820 | case 5: /* flush */ |
| 821 | return ipaddr_list_or_flush(argc-1, argv+1, 1); |
Glenn L McGrath | 2626ef6 | 2002-12-02 01:40:05 +0000 | [diff] [blame] | 822 | } |
| 823 | error_msg_and_die("Unknown command %s", *argv); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 824 | } |