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; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 139 | |
| 140 | if (buf[0] != '\t') { |
| 141 | sscanf(buf, "%d%s", &m.index, m.name); |
| 142 | continue; |
| 143 | } |
| 144 | |
| 145 | if (filter.dev && strcmp(filter.dev, m.name)) |
| 146 | continue; |
| 147 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 148 | sscanf(buf, "%08x%d", (__u32 *)&m.addr.data, &m.users); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 149 | |
| 150 | ma = malloc(sizeof(m)); |
| 151 | memcpy(ma, &m, sizeof(m)); |
| 152 | maddr_ins(result_p, ma); |
| 153 | } |
| 154 | fclose(fp); |
| 155 | } |
| 156 | |
| 157 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 158 | static void read_igmp6(struct ma_info **result_p) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 159 | { |
| 160 | char buf[256]; |
| 161 | FILE *fp = fopen("/proc/net/igmp6", "r"); |
| 162 | |
| 163 | if (!fp) |
| 164 | return; |
| 165 | |
| 166 | while (fgets(buf, sizeof(buf), fp)) { |
| 167 | char hexa[256]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 168 | struct ma_info m = { .addr.family = AF_INET6 }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 169 | int len; |
| 170 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 171 | sscanf(buf, "%d%s%s%d", &m.index, m.name, hexa, &m.users); |
| 172 | |
| 173 | if (filter.dev && strcmp(filter.dev, m.name)) |
| 174 | continue; |
| 175 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 176 | 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] | 177 | if (len >= 0) { |
| 178 | struct ma_info *ma = malloc(sizeof(m)); |
| 179 | |
| 180 | memcpy(ma, &m, sizeof(m)); |
| 181 | |
| 182 | ma->addr.bytelen = len; |
| 183 | ma->addr.bitlen = len<<3; |
| 184 | maddr_ins(result_p, ma); |
| 185 | } |
| 186 | } |
| 187 | fclose(fp); |
| 188 | } |
| 189 | |
| 190 | static void print_maddr(FILE *fp, struct ma_info *list) |
| 191 | { |
| 192 | fprintf(fp, "\t"); |
| 193 | |
| 194 | if (list->addr.family == AF_PACKET) { |
| 195 | SPRINT_BUF(b1); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 196 | 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] | 197 | list->addr.bytelen, 0, |
| 198 | b1, sizeof(b1))); |
| 199 | } else { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 200 | switch (list->addr.family) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 201 | case AF_INET: |
| 202 | fprintf(fp, "inet "); |
| 203 | break; |
| 204 | case AF_INET6: |
| 205 | fprintf(fp, "inet6 "); |
| 206 | break; |
| 207 | default: |
| 208 | fprintf(fp, "family %d ", list->addr.family); |
| 209 | break; |
| 210 | } |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 211 | fprintf(fp, "%s", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 212 | format_host(list->addr.family, |
Phil Sutter | a418e45 | 2016-03-22 19:35:15 +0100 | [diff] [blame] | 213 | -1, list->addr.data)); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 214 | } |
| 215 | if (list->users != 1) |
| 216 | fprintf(fp, " users %d", list->users); |
| 217 | if (list->features) |
| 218 | fprintf(fp, " %s", list->features); |
| 219 | fprintf(fp, "\n"); |
| 220 | } |
| 221 | |
| 222 | static void print_mlist(FILE *fp, struct ma_info *list) |
| 223 | { |
| 224 | int cur_index = 0; |
| 225 | |
| 226 | for (; list; list = list->next) { |
| 227 | if (oneline) { |
| 228 | cur_index = list->index; |
| 229 | fprintf(fp, "%d:\t%s%s", cur_index, list->name, _SL_); |
| 230 | } else if (cur_index != list->index) { |
| 231 | cur_index = list->index; |
| 232 | fprintf(fp, "%d:\t%s\n", cur_index, list->name); |
| 233 | } |
| 234 | print_maddr(fp, list); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | static int multiaddr_list(int argc, char **argv) |
| 239 | { |
| 240 | struct ma_info *list = NULL; |
| 241 | |
| 242 | if (!filter.family) |
| 243 | filter.family = preferred_family; |
| 244 | |
| 245 | while (argc > 0) { |
| 246 | if (1) { |
| 247 | if (strcmp(*argv, "dev") == 0) { |
| 248 | NEXT_ARG(); |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 249 | } else if (matches(*argv, "help") == 0) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 250 | usage(); |
| 251 | if (filter.dev) |
| 252 | duparg2("dev", *argv); |
| 253 | filter.dev = *argv; |
| 254 | } |
| 255 | argv++; argc--; |
| 256 | } |
| 257 | |
| 258 | if (!filter.family || filter.family == AF_PACKET) |
| 259 | read_dev_mcast(&list); |
| 260 | if (!filter.family || filter.family == AF_INET) |
| 261 | read_igmp(&list); |
| 262 | if (!filter.family || filter.family == AF_INET6) |
| 263 | read_igmp6(&list); |
| 264 | print_mlist(stdout, list); |
| 265 | return 0; |
| 266 | } |
| 267 | |
Stephen Hemminger | d1f28cf | 2013-02-12 11:09:03 -0800 | [diff] [blame] | 268 | static int multiaddr_modify(int cmd, int argc, char **argv) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 269 | { |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 270 | struct ifreq ifr = {}; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 271 | int fd; |
| 272 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 273 | if (cmd == RTM_NEWADDR) |
| 274 | cmd = SIOCADDMULTI; |
| 275 | else |
| 276 | cmd = SIOCDELMULTI; |
| 277 | |
| 278 | while (argc > 0) { |
| 279 | if (strcmp(*argv, "dev") == 0) { |
| 280 | NEXT_ARG(); |
| 281 | if (ifr.ifr_name[0]) |
| 282 | duparg("dev", *argv); |
| 283 | strncpy(ifr.ifr_name, *argv, IFNAMSIZ); |
| 284 | } else { |
| 285 | if (matches(*argv, "address") == 0) { |
| 286 | NEXT_ARG(); |
| 287 | } |
| 288 | if (matches(*argv, "help") == 0) |
| 289 | usage(); |
| 290 | if (ifr.ifr_hwaddr.sa_data[0]) |
| 291 | duparg("address", *argv); |
Stephen Hemminger | ae665a5 | 2006-12-05 10:10:22 -0800 | [diff] [blame] | 292 | if (ll_addr_a2n(ifr.ifr_hwaddr.sa_data, |
shemminger | f332d16 | 2005-07-05 22:37:15 +0000 | [diff] [blame] | 293 | 14, *argv) < 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 294 | fprintf(stderr, "Error: \"%s\" is not a legal ll address.\n", *argv); |
| 295 | exit(1); |
| 296 | } |
| 297 | } |
| 298 | argc--; argv++; |
| 299 | } |
| 300 | if (ifr.ifr_name[0] == 0) { |
| 301 | fprintf(stderr, "Not enough information: \"dev\" is required.\n"); |
| 302 | exit(-1); |
| 303 | } |
| 304 | |
| 305 | fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 306 | if (fd < 0) { |
| 307 | perror("Cannot create socket"); |
| 308 | exit(1); |
| 309 | } |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 310 | if (ioctl(fd, cmd, (char *)&ifr) != 0) { |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 311 | perror("ioctl"); |
| 312 | exit(1); |
| 313 | } |
| 314 | close(fd); |
| 315 | |
| 316 | exit(0); |
| 317 | } |
| 318 | |
| 319 | |
| 320 | int do_multiaddr(int argc, char **argv) |
| 321 | { |
| 322 | if (argc < 1) |
| 323 | return multiaddr_list(0, NULL); |
| 324 | if (matches(*argv, "add") == 0) |
| 325 | return multiaddr_modify(RTM_NEWADDR, argc-1, argv+1); |
| 326 | if (matches(*argv, "delete") == 0) |
| 327 | return multiaddr_modify(RTM_DELADDR, argc-1, argv+1); |
| 328 | if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 |
| 329 | || matches(*argv, "lst") == 0) |
| 330 | return multiaddr_list(argc-1, argv+1); |
| 331 | if (matches(*argv, "help") == 0) |
| 332 | usage(); |
| 333 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip maddr help\".\n", *argv); |
| 334 | exit(-1); |
| 335 | } |