blob: 4f726fdd976f1363b4392c3d7008b61d9a68c7ca [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
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!shemmingere5779fb2004-06-09 22:56:28 +000020#include <netinet/in.h>
21#include <arpa/inet.h>
22#include <string.h>
23
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000024#include <linux/netdevice.h>
25#include <linux/if.h>
26#include <linux/if_arp.h>
27#include <linux/sockios.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000028
29#include "rt_names.h"
30#include "utils.h"
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -080031#include "ip_common.h"
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000032
33static struct {
34 char *dev;
35 int family;
36} filter;
37
38static void usage(void) __attribute__((noreturn));
39
40static 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 Fourdan7f71c0c2008-11-25 12:36:22 +000047static int parse_hex(char *str, unsigned char *addr, size_t size)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000048{
Stephen Hemmingerd947b232013-03-14 13:44:25 -070049 int len = 0;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000050
Olivier Fourdan7f71c0c2008-11-25 12:36:22 +000051 while (*str && (len < 2 * size)) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000052 int tmp;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070053
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000054 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 Hemminger56f5daa2016-03-21 11:52:19 -070065struct ma_info {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000066 struct ma_info *next;
67 int index;
68 int users;
69 char *features;
70 char name[IFNAMSIZ];
71 inet_prefix addr;
72};
73
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -080074static void maddr_ins(struct ma_info **lst, struct ma_info *m)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000075{
76 struct ma_info *mp;
77
Stephen Hemmingerd947b232013-03-14 13:44:25 -070078 for (; (mp = *lst) != NULL; lst = &mp->next) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000079 if (mp->index > m->index)
80 break;
81 }
82 m->next = *lst;
83 *lst = m;
84}
85
Stephen Hemmingerd1f28cf2013-02-12 11:09:03 -080086static void read_dev_mcast(struct ma_info **result_p)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000087{
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 Sutterd17b1362016-07-18 16:48:42 +020096 struct ma_info m = { .addr.family = AF_PACKET };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000097 int len;
98 int st;
99
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000100 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 Hemminger56f5daa2016-03-21 11:52:19 -0700105 len = parse_hex(hexa, (unsigned char *)&m.addr.data, sizeof(m.addr.data));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000106 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 Hemmingerd1f28cf2013-02-12 11:09:03 -0800120static void read_igmp(struct ma_info **result_p)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000121{
Phil Sutterd17b1362016-07-18 16:48:42 +0200122 struct ma_info m = {
123 .addr.family = AF_INET,
124 .addr.bitlen = 32,
125 .addr.bytelen = 4,
126 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000127 char buf[256];
128 FILE *fp = fopen("/proc/net/igmp", "r");
129
130 if (!fp)
131 return;
Thomas Jarosch19bcc052011-10-03 05:24:44 +0000132 if (!fgets(buf, sizeof(buf), fp)) {
133 fclose(fp);
Stephen Hemminger38c867d2011-03-09 10:41:09 -0800134 return;
Thomas Jarosch19bcc052011-10-03 05:24:44 +0000135 }
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000136
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000137 while (fgets(buf, sizeof(buf), fp)) {
Brandon Philips1f7190d2011-03-23 12:03:49 -0700138 struct ma_info *ma;
Petr Vorel530903d2017-01-17 00:25:50 +0100139 size_t len;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000140
141 if (buf[0] != '\t') {
142 sscanf(buf, "%d%s", &m.index, m.name);
Petr Vorel530903d2017-01-17 00:25:50 +0100143 len = strlen(m.name);
144 if (m.name[len - 1] == ':')
145 len--;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000146 continue;
147 }
148
Petr Vorel530903d2017-01-17 00:25:50 +0100149 if (filter.dev && strncmp(filter.dev, m.name, len))
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000150 continue;
151
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700152 sscanf(buf, "%08x%d", (__u32 *)&m.addr.data, &m.users);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000153
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 Hemmingerd1f28cf2013-02-12 11:09:03 -0800162static void read_igmp6(struct ma_info **result_p)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000163{
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 Sutterd17b1362016-07-18 16:48:42 +0200172 struct ma_info m = { .addr.family = AF_INET6 };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000173 int len;
174
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000175 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 Hemminger56f5daa2016-03-21 11:52:19 -0700180 len = parse_hex(hexa, (unsigned char *)&m.addr.data, sizeof(m.addr.data));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000181 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
194static 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 Hemminger56f5daa2016-03-21 11:52:19 -0700200 fprintf(fp, "link %s", ll_addr_n2a((unsigned char *)list->addr.data,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000201 list->addr.bytelen, 0,
202 b1, sizeof(b1)));
203 } else {
Stephen Hemminger56f5daa2016-03-21 11:52:19 -0700204 switch (list->addr.family) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000205 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 Hemmingerae665a52006-12-05 10:10:22 -0800215 fprintf(fp, "%s",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000216 format_host(list->addr.family,
Phil Suttera418e452016-03-22 19:35:15 +0100217 -1, list->addr.data));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000218 }
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
226static 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
242static 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 Hemminger56f5daa2016-03-21 11:52:19 -0700253 } else if (matches(*argv, "help") == 0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000254 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 Hemmingerd1f28cf2013-02-12 11:09:03 -0800272static int multiaddr_modify(int cmd, int argc, char **argv)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000273{
Phil Sutterd17b1362016-07-18 16:48:42 +0200274 struct ifreq ifr = {};
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000275 int fd;
276
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000277 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 Hemmingerae665a52006-12-05 10:10:22 -0800296 if (ll_addr_a2n(ifr.ifr_hwaddr.sa_data,
shemmingerf332d162005-07-05 22:37:15 +0000297 14, *argv) < 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000298 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 Hemminger56f5daa2016-03-21 11:52:19 -0700314 if (ioctl(fd, cmd, (char *)&ifr) != 0) {
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000315 perror("ioctl");
316 exit(1);
317 }
318 close(fd);
319
320 exit(0);
321}
322
323
324int 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}