osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ipmaddr.c "ip maddress". |
| 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> |
osdl.org!shemminger | e5779fb | 2004-06-09 22:56:28 +0000 | [diff] [blame] | 20 | #include <netinet/in.h> |
| 21 | #include <arpa/inet.h> |
| 22 | #include <string.h> |
| 23 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 24 | #include <linux/netdevice.h> |
| 25 | #include <linux/if.h> |
| 26 | #include <linux/if_arp.h> |
| 27 | #include <linux/sockios.h> |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 28 | |
| 29 | #include "rt_names.h" |
| 30 | #include "utils.h" |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 31 | #include "ip_common.h" |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 32 | |
| 33 | static struct { |
| 34 | char *dev; |
| 35 | int family; |
| 36 | } filter; |
| 37 | |
| 38 | static void usage(void) __attribute__((noreturn)); |
| 39 | |
| 40 | static void usage(void) |
| 41 | { |
| 42 | fprintf(stderr, "Usage: ip maddr [ add | del ] MULTIADDR dev STRING\n"); |
| 43 | fprintf(stderr, " ip maddr show [ dev STRING ]\n"); |
| 44 | exit(-1); |
| 45 | } |
| 46 | |
Olivier Fourdan | 7f71c0c | 2008-11-25 12:36:22 +0000 | [diff] [blame] | 47 | static int parse_hex(char *str, unsigned char *addr, size_t size) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 48 | { |
Stephen Hemminger | d947b23 | 2013-03-14 13:44:25 -0700 | [diff] [blame] | 49 | int len = 0; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 50 | |
Olivier Fourdan | 7f71c0c | 2008-11-25 12:36:22 +0000 | [diff] [blame] | 51 | while (*str && (len < 2 * size)) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 52 | int tmp; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 53 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 54 | if (str[1] == 0) |
| 55 | return -1; |
| 56 | if (sscanf(str, "%02x", &tmp) != 1) |
| 57 | return -1; |
| 58 | addr[len] = tmp; |
| 59 | len++; |
| 60 | str += 2; |
| 61 | } |
| 62 | return len; |
| 63 | } |
| 64 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 65 | struct ma_info { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 66 | struct ma_info *next; |
| 67 | int index; |
| 68 | int users; |
| 69 | char *features; |
| 70 | char name[IFNAMSIZ]; |
| 71 | inet_prefix addr; |
| 72 | }; |
| 73 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 74 | static void maddr_ins(struct ma_info **lst, struct ma_info *m) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 75 | { |
| 76 | struct ma_info *mp; |
| 77 | |
Stephen Hemminger | d947b23 | 2013-03-14 13:44:25 -0700 | [diff] [blame] | 78 | for (; (mp = *lst) != NULL; lst = &mp->next) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 79 | if (mp->index > m->index) |
| 80 | break; |
| 81 | } |
| 82 | m->next = *lst; |
| 83 | *lst = m; |
| 84 | } |
| 85 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 86 | static void read_dev_mcast(struct ma_info **result_p) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 87 | { |
| 88 | char buf[256]; |
| 89 | FILE *fp = fopen("/proc/net/dev_mcast", "r"); |
| 90 | |
| 91 | if (!fp) |
| 92 | return; |
| 93 | |
| 94 | while (fgets(buf, sizeof(buf), fp)) { |
| 95 | char hexa[256]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 96 | struct ma_info m = { .addr.family = AF_PACKET }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 97 | int len; |
| 98 | int st; |
| 99 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 100 | sscanf(buf, "%d%s%d%d%s", &m.index, m.name, &m.users, &st, |
| 101 | hexa); |
| 102 | if (filter.dev && strcmp(filter.dev, m.name)) |
| 103 | continue; |
| 104 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 105 | len = parse_hex(hexa, (unsigned char *)&m.addr.data, sizeof(m.addr.data)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 106 | if (len >= 0) { |
| 107 | struct ma_info *ma = malloc(sizeof(m)); |
| 108 | |
| 109 | memcpy(ma, &m, sizeof(m)); |
| 110 | ma->addr.bytelen = len; |
| 111 | ma->addr.bitlen = len<<3; |
| 112 | if (st) |
| 113 | ma->features = "static"; |
| 114 | maddr_ins(result_p, ma); |
| 115 | } |
| 116 | } |
| 117 | fclose(fp); |
| 118 | } |
| 119 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 120 | static void read_igmp(struct ma_info **result_p) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 121 | { |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 122 | struct ma_info m = { |
| 123 | .addr.family = AF_INET, |
| 124 | .addr.bitlen = 32, |
| 125 | .addr.bytelen = 4, |
| 126 | }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 127 | char buf[256]; |
| 128 | FILE *fp = fopen("/proc/net/igmp", "r"); |
| 129 | |
| 130 | if (!fp) |
| 131 | return; |
Thomas Jarosch | 19bcc05 | 2011-10-03 05:24:44 +0000 | [diff] [blame] | 132 | if (!fgets(buf, sizeof(buf), fp)) { |
| 133 | fclose(fp); |
Stephen Hemminger | 38c867d | 2011-03-09 10:41:09 -0800 | [diff] [blame] | 134 | return; |
Thomas Jarosch | 19bcc05 | 2011-10-03 05:24:44 +0000 | [diff] [blame] | 135 | } |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 136 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 137 | while (fgets(buf, sizeof(buf), fp)) { |
Brandon Philips | 1f7190d | 2011-03-23 12:03:49 -0700 | [diff] [blame] | 138 | struct ma_info *ma; |
Petr Vorel | 530903d | 2017-01-17 00:25:50 +0100 | [diff] [blame] | 139 | size_t len; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 140 | |
| 141 | if (buf[0] != '\t') { |
| 142 | sscanf(buf, "%d%s", &m.index, m.name); |
Petr Vorel | 530903d | 2017-01-17 00:25:50 +0100 | [diff] [blame] | 143 | len = strlen(m.name); |
| 144 | if (m.name[len - 1] == ':') |
| 145 | len--; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 146 | continue; |
| 147 | } |
| 148 | |
Petr Vorel | 530903d | 2017-01-17 00:25:50 +0100 | [diff] [blame] | 149 | if (filter.dev && strncmp(filter.dev, m.name, len)) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 150 | continue; |
| 151 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 152 | sscanf(buf, "%08x%d", (__u32 *)&m.addr.data, &m.users); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 153 | |
| 154 | ma = malloc(sizeof(m)); |
| 155 | memcpy(ma, &m, sizeof(m)); |
| 156 | maddr_ins(result_p, ma); |
| 157 | } |
| 158 | fclose(fp); |
| 159 | } |
| 160 | |
| 161 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 162 | static void read_igmp6(struct ma_info **result_p) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 163 | { |
| 164 | char buf[256]; |
| 165 | FILE *fp = fopen("/proc/net/igmp6", "r"); |
| 166 | |
| 167 | if (!fp) |
| 168 | return; |
| 169 | |
| 170 | while (fgets(buf, sizeof(buf), fp)) { |
| 171 | char hexa[256]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 172 | struct ma_info m = { .addr.family = AF_INET6 }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 173 | int len; |
| 174 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 175 | sscanf(buf, "%d%s%s%d", &m.index, m.name, hexa, &m.users); |
| 176 | |
| 177 | if (filter.dev && strcmp(filter.dev, m.name)) |
| 178 | continue; |
| 179 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 180 | len = parse_hex(hexa, (unsigned char *)&m.addr.data, sizeof(m.addr.data)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 181 | if (len >= 0) { |
| 182 | struct ma_info *ma = malloc(sizeof(m)); |
| 183 | |
| 184 | memcpy(ma, &m, sizeof(m)); |
| 185 | |
| 186 | ma->addr.bytelen = len; |
| 187 | ma->addr.bitlen = len<<3; |
| 188 | maddr_ins(result_p, ma); |
| 189 | } |
| 190 | } |
| 191 | fclose(fp); |
| 192 | } |
| 193 | |
| 194 | static void print_maddr(FILE *fp, struct ma_info *list) |
| 195 | { |
| 196 | fprintf(fp, "\t"); |
| 197 | |
| 198 | if (list->addr.family == AF_PACKET) { |
| 199 | SPRINT_BUF(b1); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 200 | fprintf(fp, "link %s", ll_addr_n2a((unsigned char *)list->addr.data, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 201 | list->addr.bytelen, 0, |
| 202 | b1, sizeof(b1))); |
| 203 | } else { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 204 | switch (list->addr.family) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 205 | case AF_INET: |
| 206 | fprintf(fp, "inet "); |
| 207 | break; |
| 208 | case AF_INET6: |
| 209 | fprintf(fp, "inet6 "); |
| 210 | break; |
| 211 | default: |
| 212 | fprintf(fp, "family %d ", list->addr.family); |
| 213 | break; |
| 214 | } |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 215 | fprintf(fp, "%s", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 216 | format_host(list->addr.family, |
Phil Sutter | a418e45 | 2016-03-22 19:35:15 +0100 | [diff] [blame] | 217 | -1, list->addr.data)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 218 | } |
| 219 | if (list->users != 1) |
| 220 | fprintf(fp, " users %d", list->users); |
| 221 | if (list->features) |
| 222 | fprintf(fp, " %s", list->features); |
| 223 | fprintf(fp, "\n"); |
| 224 | } |
| 225 | |
| 226 | static void print_mlist(FILE *fp, struct ma_info *list) |
| 227 | { |
| 228 | int cur_index = 0; |
| 229 | |
| 230 | for (; list; list = list->next) { |
| 231 | if (oneline) { |
| 232 | cur_index = list->index; |
| 233 | fprintf(fp, "%d:\t%s%s", cur_index, list->name, _SL_); |
| 234 | } else if (cur_index != list->index) { |
| 235 | cur_index = list->index; |
| 236 | fprintf(fp, "%d:\t%s\n", cur_index, list->name); |
| 237 | } |
| 238 | print_maddr(fp, list); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | static int multiaddr_list(int argc, char **argv) |
| 243 | { |
| 244 | struct ma_info *list = NULL; |
| 245 | |
| 246 | if (!filter.family) |
| 247 | filter.family = preferred_family; |
| 248 | |
| 249 | while (argc > 0) { |
| 250 | if (1) { |
| 251 | if (strcmp(*argv, "dev") == 0) { |
| 252 | NEXT_ARG(); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 253 | } else if (matches(*argv, "help") == 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 254 | usage(); |
| 255 | if (filter.dev) |
| 256 | duparg2("dev", *argv); |
| 257 | filter.dev = *argv; |
| 258 | } |
| 259 | argv++; argc--; |
| 260 | } |
| 261 | |
| 262 | if (!filter.family || filter.family == AF_PACKET) |
| 263 | read_dev_mcast(&list); |
| 264 | if (!filter.family || filter.family == AF_INET) |
| 265 | read_igmp(&list); |
| 266 | if (!filter.family || filter.family == AF_INET6) |
| 267 | read_igmp6(&list); |
| 268 | print_mlist(stdout, list); |
| 269 | return 0; |
| 270 | } |
| 271 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 272 | static int multiaddr_modify(int cmd, int argc, char **argv) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 273 | { |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 274 | struct ifreq ifr = {}; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 275 | int fd; |
| 276 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 277 | if (cmd == RTM_NEWADDR) |
| 278 | cmd = SIOCADDMULTI; |
| 279 | else |
| 280 | cmd = SIOCDELMULTI; |
| 281 | |
| 282 | while (argc > 0) { |
| 283 | if (strcmp(*argv, "dev") == 0) { |
| 284 | NEXT_ARG(); |
| 285 | if (ifr.ifr_name[0]) |
| 286 | duparg("dev", *argv); |
| 287 | strncpy(ifr.ifr_name, *argv, IFNAMSIZ); |
| 288 | } else { |
| 289 | if (matches(*argv, "address") == 0) { |
| 290 | NEXT_ARG(); |
| 291 | } |
| 292 | if (matches(*argv, "help") == 0) |
| 293 | usage(); |
| 294 | if (ifr.ifr_hwaddr.sa_data[0]) |
| 295 | duparg("address", *argv); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 296 | if (ll_addr_a2n(ifr.ifr_hwaddr.sa_data, |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 297 | 14, *argv) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 298 | fprintf(stderr, "Error: \"%s\" is not a legal ll address.\n", *argv); |
| 299 | exit(1); |
| 300 | } |
| 301 | } |
| 302 | argc--; argv++; |
| 303 | } |
| 304 | if (ifr.ifr_name[0] == 0) { |
| 305 | fprintf(stderr, "Not enough information: \"dev\" is required.\n"); |
| 306 | exit(-1); |
| 307 | } |
| 308 | |
| 309 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 310 | if (fd < 0) { |
| 311 | perror("Cannot create socket"); |
| 312 | exit(1); |
| 313 | } |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 314 | if (ioctl(fd, cmd, (char *)&ifr) != 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 315 | perror("ioctl"); |
| 316 | exit(1); |
| 317 | } |
| 318 | close(fd); |
| 319 | |
| 320 | exit(0); |
| 321 | } |
| 322 | |
| 323 | |
| 324 | int do_multiaddr(int argc, char **argv) |
| 325 | { |
| 326 | if (argc < 1) |
| 327 | return multiaddr_list(0, NULL); |
| 328 | if (matches(*argv, "add") == 0) |
| 329 | return multiaddr_modify(RTM_NEWADDR, argc-1, argv+1); |
| 330 | if (matches(*argv, "delete") == 0) |
| 331 | return multiaddr_modify(RTM_DELADDR, argc-1, argv+1); |
| 332 | if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 |
| 333 | || matches(*argv, "lst") == 0) |
| 334 | return multiaddr_list(argc-1, argv+1); |
| 335 | if (matches(*argv, "help") == 0) |
| 336 | usage(); |
| 337 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip maddr help\".\n", *argv); |
| 338 | exit(-1); |
| 339 | } |