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 (at) 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> 20 #include <netinet/in.h> 21 #include <arpa/inet.h> 22 #include <string.h> 23 24 #include <linux/netdevice.h> 25 #include <linux/if.h> 26 #include <linux/if_arp.h> 27 #include <linux/sockios.h> 28 29 #include "rt_names.h" 30 #include "utils.h" 31 32 static struct { 33 char *dev; 34 int family; 35 } filter; 36 37 static void usage(void) __attribute__((noreturn)); 38 39 static void usage(void) 40 { 41 fprintf(stderr, "Usage: ip maddr [ add | del ] MULTIADDR dev STRING\n"); 42 fprintf(stderr, " ip maddr show [ dev STRING ]\n"); 43 exit(-1); 44 } 45 46 static int parse_hex(char *str, unsigned char *addr, size_t size) 47 { 48 int len=0; 49 50 while (*str && (len < 2 * size)) { 51 int tmp; 52 if (str[1] == 0) 53 return -1; 54 if (sscanf(str, "%02x", &tmp) != 1) 55 return -1; 56 addr[len] = tmp; 57 len++; 58 str += 2; 59 } 60 return len; 61 } 62 63 struct ma_info 64 { 65 struct ma_info *next; 66 int index; 67 int users; 68 char *features; 69 char name[IFNAMSIZ]; 70 inet_prefix addr; 71 }; 72 73 void maddr_ins(struct ma_info **lst, struct ma_info *m) 74 { 75 struct ma_info *mp; 76 77 for (; (mp=*lst) != NULL; lst = &mp->next) { 78 if (mp->index > m->index) 79 break; 80 } 81 m->next = *lst; 82 *lst = m; 83 } 84 85 void read_dev_mcast(struct ma_info **result_p) 86 { 87 char buf[256]; 88 FILE *fp = fopen("/proc/net/dev_mcast", "r"); 89 90 if (!fp) 91 return; 92 93 while (fgets(buf, sizeof(buf), fp)) { 94 char hexa[256]; 95 struct ma_info m; 96 int len; 97 int st; 98 99 memset(&m, 0, sizeof(m)); 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 105 m.addr.family = AF_PACKET; 106 107 len = parse_hex(hexa, (unsigned char*)&m.addr.data, sizeof (m.addr.data)); 108 if (len >= 0) { 109 struct ma_info *ma = malloc(sizeof(m)); 110 111 memcpy(ma, &m, sizeof(m)); 112 ma->addr.bytelen = len; 113 ma->addr.bitlen = len<<3; 114 if (st) 115 ma->features = "static"; 116 maddr_ins(result_p, ma); 117 } 118 } 119 fclose(fp); 120 } 121 122 void read_igmp(struct ma_info **result_p) 123 { 124 struct ma_info m; 125 char buf[256]; 126 FILE *fp = fopen("/proc/net/igmp", "r"); 127 128 if (!fp) 129 return; 130 memset(&m, 0, sizeof(m)); 131 fgets(buf, sizeof(buf), fp); 132 133 m.addr.family = AF_INET; 134 m.addr.bitlen = 32; 135 m.addr.bytelen = 4; 136 137 while (fgets(buf, sizeof(buf), fp)) { 138 struct ma_info *ma = malloc(sizeof(m)); 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 148 sscanf(buf, "%08x%d", (__u32*)&m.addr.data, &m.users); 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 158 void read_igmp6(struct ma_info **result_p) 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]; 168 struct ma_info m; 169 int len; 170 171 memset(&m, 0, sizeof(m)); 172 sscanf(buf, "%d%s%s%d", &m.index, m.name, hexa, &m.users); 173 174 if (filter.dev && strcmp(filter.dev, m.name)) 175 continue; 176 177 m.addr.family = AF_INET6; 178 179 len = parse_hex(hexa, (unsigned char*)&m.addr.data, sizeof (m.addr.data)); 180 if (len >= 0) { 181 struct ma_info *ma = malloc(sizeof(m)); 182 183 memcpy(ma, &m, sizeof(m)); 184 185 ma->addr.bytelen = len; 186 ma->addr.bitlen = len<<3; 187 maddr_ins(result_p, ma); 188 } 189 } 190 fclose(fp); 191 } 192 193 static void print_maddr(FILE *fp, struct ma_info *list) 194 { 195 fprintf(fp, "\t"); 196 197 if (list->addr.family == AF_PACKET) { 198 SPRINT_BUF(b1); 199 fprintf(fp, "link %s", ll_addr_n2a((unsigned char*)list->addr.data, 200 list->addr.bytelen, 0, 201 b1, sizeof(b1))); 202 } else { 203 char abuf[256]; 204 switch(list->addr.family) { 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 } 215 fprintf(fp, "%s", 216 format_host(list->addr.family, 217 -1, 218 list->addr.data, 219 abuf, sizeof(abuf))); 220 } 221 if (list->users != 1) 222 fprintf(fp, " users %d", list->users); 223 if (list->features) 224 fprintf(fp, " %s", list->features); 225 fprintf(fp, "\n"); 226 } 227 228 static void print_mlist(FILE *fp, struct ma_info *list) 229 { 230 int cur_index = 0; 231 232 for (; list; list = list->next) { 233 if (oneline) { 234 cur_index = list->index; 235 fprintf(fp, "%d:\t%s%s", cur_index, list->name, _SL_); 236 } else if (cur_index != list->index) { 237 cur_index = list->index; 238 fprintf(fp, "%d:\t%s\n", cur_index, list->name); 239 } 240 print_maddr(fp, list); 241 } 242 } 243 244 static int multiaddr_list(int argc, char **argv) 245 { 246 struct ma_info *list = NULL; 247 248 if (!filter.family) 249 filter.family = preferred_family; 250 251 while (argc > 0) { 252 if (1) { 253 if (strcmp(*argv, "dev") == 0) { 254 NEXT_ARG(); 255 } 256 if (matches(*argv, "help") == 0) 257 usage(); 258 if (filter.dev) 259 duparg2("dev", *argv); 260 filter.dev = *argv; 261 } 262 argv++; argc--; 263 } 264 265 if (!filter.family || filter.family == AF_PACKET) 266 read_dev_mcast(&list); 267 if (!filter.family || filter.family == AF_INET) 268 read_igmp(&list); 269 if (!filter.family || filter.family == AF_INET6) 270 read_igmp6(&list); 271 print_mlist(stdout, list); 272 return 0; 273 } 274 275 int multiaddr_modify(int cmd, int argc, char **argv) 276 { 277 struct ifreq ifr; 278 int fd; 279 280 memset(&ifr, 0, sizeof(ifr)); 281 282 if (cmd == RTM_NEWADDR) 283 cmd = SIOCADDMULTI; 284 else 285 cmd = SIOCDELMULTI; 286 287 while (argc > 0) { 288 if (strcmp(*argv, "dev") == 0) { 289 NEXT_ARG(); 290 if (ifr.ifr_name[0]) 291 duparg("dev", *argv); 292 strncpy(ifr.ifr_name, *argv, IFNAMSIZ); 293 } else { 294 if (matches(*argv, "address") == 0) { 295 NEXT_ARG(); 296 } 297 if (matches(*argv, "help") == 0) 298 usage(); 299 if (ifr.ifr_hwaddr.sa_data[0]) 300 duparg("address", *argv); 301 if (ll_addr_a2n(ifr.ifr_hwaddr.sa_data, 302 14, *argv) < 0) { 303 fprintf(stderr, "Error: \"%s\" is not a legal ll address.\n", *argv); 304 exit(1); 305 } 306 } 307 argc--; argv++; 308 } 309 if (ifr.ifr_name[0] == 0) { 310 fprintf(stderr, "Not enough information: \"dev\" is required.\n"); 311 exit(-1); 312 } 313 314 fd = socket(AF_INET, SOCK_DGRAM, 0); 315 if (fd < 0) { 316 perror("Cannot create socket"); 317 exit(1); 318 } 319 if (ioctl(fd, cmd, (char*)&ifr) != 0) { 320 perror("ioctl"); 321 exit(1); 322 } 323 close(fd); 324 325 exit(0); 326 } 327 328 329 int do_multiaddr(int argc, char **argv) 330 { 331 if (argc < 1) 332 return multiaddr_list(0, NULL); 333 if (matches(*argv, "add") == 0) 334 return multiaddr_modify(RTM_NEWADDR, argc-1, argv+1); 335 if (matches(*argv, "delete") == 0) 336 return multiaddr_modify(RTM_DELADDR, argc-1, argv+1); 337 if (matches(*argv, "list") == 0 || matches(*argv, "show") == 0 338 || matches(*argv, "lst") == 0) 339 return multiaddr_list(argc-1, argv+1); 340 if (matches(*argv, "help") == 0) 341 usage(); 342 fprintf(stderr, "Command \"%s\" is unknown, try \"ip maddr help\".\n", *argv); 343 exit(-1); 344 } 345