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