Annotation of /trunk/mkinitrd-magellan/busybox/networking/libiproute/iplink.c
Parent Directory | Revision Log
Revision 1179 -
(hide annotations)
(download)
Wed Dec 15 21:33:41 2010 UTC (13 years, 10 months ago) by niro
File MIME type: text/plain
File size: 8927 byte(s)
Wed Dec 15 21:33:41 2010 UTC (13 years, 10 months ago) by niro
File MIME type: text/plain
File size: 8927 byte(s)
-updated to busybox-1.17.4
1 | niro | 532 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||
3 | * iplink.c "ip link". | ||
4 | * | ||
5 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | ||
6 | * | ||
7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
8 | */ | ||
9 | #include <net/if.h> | ||
10 | #include <net/if_packet.h> | ||
11 | #include <netpacket/packet.h> | ||
12 | #include <net/ethernet.h> | ||
13 | |||
14 | niro | 816 | #include "ip_common.h" /* #include "libbb.h" is inside */ |
15 | niro | 532 | #include "rt_names.h" |
16 | #include "utils.h" | ||
17 | |||
18 | niro | 1179 | #ifndef IFLA_LINKINFO |
19 | # define IFLA_LINKINFO 18 | ||
20 | # define IFLA_INFO_KIND 1 | ||
21 | #endif | ||
22 | |||
23 | niro | 816 | /* taken from linux/sockios.h */ |
24 | niro | 532 | #define SIOCSIFNAME 0x8923 /* set interface name */ |
25 | |||
26 | niro | 816 | /* Exits on error */ |
27 | niro | 532 | static int get_ctl_fd(void) |
28 | { | ||
29 | int fd; | ||
30 | |||
31 | fd = socket(PF_INET, SOCK_DGRAM, 0); | ||
32 | if (fd >= 0) | ||
33 | return fd; | ||
34 | fd = socket(PF_PACKET, SOCK_DGRAM, 0); | ||
35 | if (fd >= 0) | ||
36 | return fd; | ||
37 | niro | 816 | return xsocket(PF_INET6, SOCK_DGRAM, 0); |
38 | niro | 532 | } |
39 | |||
40 | niro | 816 | /* Exits on error */ |
41 | static void do_chflags(char *dev, uint32_t flags, uint32_t mask) | ||
42 | niro | 532 | { |
43 | struct ifreq ifr; | ||
44 | int fd; | ||
45 | |||
46 | niro | 984 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
47 | niro | 532 | fd = get_ctl_fd(); |
48 | niro | 816 | xioctl(fd, SIOCGIFFLAGS, &ifr); |
49 | if ((ifr.ifr_flags ^ flags) & mask) { | ||
50 | niro | 532 | ifr.ifr_flags &= ~mask; |
51 | niro | 816 | ifr.ifr_flags |= mask & flags; |
52 | xioctl(fd, SIOCSIFFLAGS, &ifr); | ||
53 | niro | 532 | } |
54 | close(fd); | ||
55 | } | ||
56 | |||
57 | niro | 816 | /* Exits on error */ |
58 | static void do_changename(char *dev, char *newdev) | ||
59 | niro | 532 | { |
60 | struct ifreq ifr; | ||
61 | int fd; | ||
62 | |||
63 | niro | 984 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
64 | strncpy_IFNAMSIZ(ifr.ifr_newname, newdev); | ||
65 | niro | 532 | fd = get_ctl_fd(); |
66 | niro | 816 | xioctl(fd, SIOCSIFNAME, &ifr); |
67 | niro | 532 | close(fd); |
68 | } | ||
69 | |||
70 | niro | 816 | /* Exits on error */ |
71 | static void set_qlen(char *dev, int qlen) | ||
72 | niro | 532 | { |
73 | struct ifreq ifr; | ||
74 | int s; | ||
75 | |||
76 | s = get_ctl_fd(); | ||
77 | memset(&ifr, 0, sizeof(ifr)); | ||
78 | niro | 984 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
79 | niro | 532 | ifr.ifr_qlen = qlen; |
80 | niro | 816 | xioctl(s, SIOCSIFTXQLEN, &ifr); |
81 | niro | 532 | close(s); |
82 | } | ||
83 | |||
84 | niro | 816 | /* Exits on error */ |
85 | static void set_mtu(char *dev, int mtu) | ||
86 | niro | 532 | { |
87 | struct ifreq ifr; | ||
88 | int s; | ||
89 | |||
90 | s = get_ctl_fd(); | ||
91 | memset(&ifr, 0, sizeof(ifr)); | ||
92 | niro | 984 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
93 | niro | 532 | ifr.ifr_mtu = mtu; |
94 | niro | 816 | xioctl(s, SIOCSIFMTU, &ifr); |
95 | niro | 532 | close(s); |
96 | } | ||
97 | |||
98 | niro | 816 | /* Exits on error */ |
99 | niro | 532 | static int get_address(char *dev, int *htype) |
100 | { | ||
101 | struct ifreq ifr; | ||
102 | struct sockaddr_ll me; | ||
103 | socklen_t alen; | ||
104 | int s; | ||
105 | |||
106 | niro | 816 | s = xsocket(PF_PACKET, SOCK_DGRAM, 0); |
107 | niro | 532 | |
108 | memset(&ifr, 0, sizeof(ifr)); | ||
109 | niro | 984 | strncpy_IFNAMSIZ(ifr.ifr_name, dev); |
110 | niro | 816 | xioctl(s, SIOCGIFINDEX, &ifr); |
111 | niro | 532 | |
112 | memset(&me, 0, sizeof(me)); | ||
113 | me.sll_family = AF_PACKET; | ||
114 | me.sll_ifindex = ifr.ifr_ifindex; | ||
115 | me.sll_protocol = htons(ETH_P_LOOP); | ||
116 | niro | 816 | xbind(s, (struct sockaddr*)&me, sizeof(me)); |
117 | niro | 532 | alen = sizeof(me); |
118 | niro | 984 | getsockname(s, (struct sockaddr*)&me, &alen); |
119 | //never happens: | ||
120 | //if (getsockname(s, (struct sockaddr*)&me, &alen) == -1) | ||
121 | // bb_perror_msg_and_die("getsockname"); | ||
122 | niro | 532 | close(s); |
123 | *htype = me.sll_hatype; | ||
124 | return me.sll_halen; | ||
125 | } | ||
126 | |||
127 | niro | 816 | /* Exits on error */ |
128 | static void parse_address(char *dev, int hatype, int halen, char *lla, struct ifreq *ifr) | ||
129 | niro | 532 | { |
130 | int alen; | ||
131 | |||
132 | memset(ifr, 0, sizeof(*ifr)); | ||
133 | niro | 984 | strncpy_IFNAMSIZ(ifr->ifr_name, dev); |
134 | niro | 532 | ifr->ifr_hwaddr.sa_family = hatype; |
135 | niro | 816 | |
136 | alen = hatype == 1/*ARPHRD_ETHER*/ ? 14/*ETH_HLEN*/ : 19/*INFINIBAND_HLEN*/; | ||
137 | alen = ll_addr_a2n((unsigned char *)(ifr->ifr_hwaddr.sa_data), alen, lla); | ||
138 | niro | 532 | if (alen < 0) |
139 | niro | 816 | exit(EXIT_FAILURE); |
140 | niro | 532 | if (alen != halen) { |
141 | niro | 816 | bb_error_msg_and_die("wrong address (%s) length: expected %d bytes", lla, halen); |
142 | niro | 532 | } |
143 | } | ||
144 | |||
145 | niro | 816 | /* Exits on error */ |
146 | static void set_address(struct ifreq *ifr, int brd) | ||
147 | niro | 532 | { |
148 | int s; | ||
149 | |||
150 | s = get_ctl_fd(); | ||
151 | niro | 816 | if (brd) |
152 | xioctl(s, SIOCSIFHWBROADCAST, ifr); | ||
153 | else | ||
154 | xioctl(s, SIOCSIFHWADDR, ifr); | ||
155 | niro | 532 | close(s); |
156 | } | ||
157 | |||
158 | |||
159 | niro | 816 | static void die_must_be_on_off(const char *msg) NORETURN; |
160 | static void die_must_be_on_off(const char *msg) | ||
161 | niro | 532 | { |
162 | niro | 816 | bb_error_msg_and_die("argument of \"%s\" must be \"on\" or \"off\"", msg); |
163 | } | ||
164 | |||
165 | /* Return value becomes exitcode. It's okay to not return at all */ | ||
166 | static int do_set(char **argv) | ||
167 | { | ||
168 | niro | 532 | char *dev = NULL; |
169 | uint32_t mask = 0; | ||
170 | uint32_t flags = 0; | ||
171 | int qlen = -1; | ||
172 | int mtu = -1; | ||
173 | char *newaddr = NULL; | ||
174 | char *newbrd = NULL; | ||
175 | struct ifreq ifr0, ifr1; | ||
176 | char *newname = NULL; | ||
177 | int htype, halen; | ||
178 | niro | 816 | static const char keywords[] ALIGN1 = |
179 | niro | 1123 | "up\0""down\0""name\0""mtu\0""qlen\0""multicast\0" |
180 | niro | 816 | "arp\0""address\0""dev\0"; |
181 | niro | 1123 | enum { ARG_up = 0, ARG_down, ARG_name, ARG_mtu, ARG_qlen, ARG_multicast, |
182 | niro | 816 | ARG_arp, ARG_addr, ARG_dev }; |
183 | static const char str_on_off[] ALIGN1 = "on\0""off\0"; | ||
184 | enum { PARM_on = 0, PARM_off }; | ||
185 | smalluint key; | ||
186 | niro | 532 | |
187 | niro | 816 | while (*argv) { |
188 | /* substring search ensures that e.g. "addr" and "address" | ||
189 | * are both accepted */ | ||
190 | key = index_in_substrings(keywords, *argv); | ||
191 | if (key == ARG_up) { | ||
192 | niro | 532 | mask |= IFF_UP; |
193 | flags |= IFF_UP; | ||
194 | niro | 1123 | } else if (key == ARG_down) { |
195 | niro | 532 | mask |= IFF_UP; |
196 | flags &= ~IFF_UP; | ||
197 | niro | 1123 | } else if (key == ARG_name) { |
198 | niro | 532 | NEXT_ARG(); |
199 | newname = *argv; | ||
200 | niro | 1123 | } else if (key == ARG_mtu) { |
201 | niro | 532 | NEXT_ARG(); |
202 | if (mtu != -1) | ||
203 | duparg("mtu", *argv); | ||
204 | niro | 984 | mtu = get_unsigned(*argv, "mtu"); |
205 | niro | 1123 | } else if (key == ARG_qlen) { |
206 | niro | 532 | NEXT_ARG(); |
207 | niro | 1123 | if (qlen != -1) |
208 | duparg("qlen", *argv); | ||
209 | qlen = get_unsigned(*argv, "qlen"); | ||
210 | } else if (key == ARG_addr) { | ||
211 | niro | 532 | NEXT_ARG(); |
212 | newaddr = *argv; | ||
213 | niro | 1123 | } else if (key >= ARG_dev) { |
214 | niro | 816 | if (key == ARG_dev) { |
215 | niro | 532 | NEXT_ARG(); |
216 | } | ||
217 | if (dev) | ||
218 | duparg2("dev", *argv); | ||
219 | dev = *argv; | ||
220 | niro | 1123 | } else { |
221 | int param; | ||
222 | NEXT_ARG(); | ||
223 | param = index_in_strings(str_on_off, *argv); | ||
224 | if (key == ARG_multicast) { | ||
225 | if (param < 0) | ||
226 | die_must_be_on_off("multicast"); | ||
227 | mask |= IFF_MULTICAST; | ||
228 | if (param == PARM_on) | ||
229 | flags |= IFF_MULTICAST; | ||
230 | else | ||
231 | flags &= ~IFF_MULTICAST; | ||
232 | } else if (key == ARG_arp) { | ||
233 | if (param < 0) | ||
234 | die_must_be_on_off("arp"); | ||
235 | mask |= IFF_NOARP; | ||
236 | if (param == PARM_on) | ||
237 | flags &= ~IFF_NOARP; | ||
238 | else | ||
239 | flags |= IFF_NOARP; | ||
240 | } | ||
241 | niro | 532 | } |
242 | niro | 816 | argv++; |
243 | niro | 532 | } |
244 | |||
245 | if (!dev) { | ||
246 | niro | 816 | bb_error_msg_and_die(bb_msg_requires_arg, "\"dev\""); |
247 | niro | 532 | } |
248 | |||
249 | if (newaddr || newbrd) { | ||
250 | halen = get_address(dev, &htype); | ||
251 | if (newaddr) { | ||
252 | niro | 816 | parse_address(dev, htype, halen, newaddr, &ifr0); |
253 | niro | 1123 | set_address(&ifr0, 0); |
254 | niro | 532 | } |
255 | if (newbrd) { | ||
256 | niro | 816 | parse_address(dev, htype, halen, newbrd, &ifr1); |
257 | niro | 1123 | set_address(&ifr1, 1); |
258 | niro | 532 | } |
259 | } | ||
260 | |||
261 | if (newname && strcmp(dev, newname)) { | ||
262 | niro | 816 | do_changename(dev, newname); |
263 | niro | 532 | dev = newname; |
264 | } | ||
265 | if (qlen != -1) { | ||
266 | niro | 816 | set_qlen(dev, qlen); |
267 | niro | 532 | } |
268 | if (mtu != -1) { | ||
269 | niro | 816 | set_mtu(dev, mtu); |
270 | niro | 532 | } |
271 | if (mask) | ||
272 | niro | 816 | do_chflags(dev, flags, mask); |
273 | niro | 532 | return 0; |
274 | } | ||
275 | |||
276 | niro | 816 | static int ipaddr_list_link(char **argv) |
277 | niro | 532 | { |
278 | preferred_family = AF_PACKET; | ||
279 | niro | 816 | return ipaddr_list_or_flush(argv, 0); |
280 | niro | 532 | } |
281 | |||
282 | niro | 1123 | #ifndef NLMSG_TAIL |
283 | #define NLMSG_TAIL(nmsg) \ | ||
284 | ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len))) | ||
285 | #endif | ||
286 | niro | 816 | /* Return value becomes exitcode. It's okay to not return at all */ |
287 | niro | 1123 | static int do_change(char **argv, const unsigned rtm) |
288 | { | ||
289 | static const char keywords[] ALIGN1 = | ||
290 | "link\0""name\0""type\0""dev\0"; | ||
291 | enum { | ||
292 | ARG_link, | ||
293 | ARG_name, | ||
294 | ARG_type, | ||
295 | ARG_dev, | ||
296 | }; | ||
297 | struct rtnl_handle rth; | ||
298 | struct { | ||
299 | struct nlmsghdr n; | ||
300 | struct ifinfomsg i; | ||
301 | char buf[1024]; | ||
302 | } req; | ||
303 | smalluint arg; | ||
304 | char *name_str = NULL, *link_str = NULL, *type_str = NULL, *dev_str = NULL; | ||
305 | |||
306 | memset(&req, 0, sizeof(req)); | ||
307 | |||
308 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); | ||
309 | req.n.nlmsg_flags = NLM_F_REQUEST; | ||
310 | req.n.nlmsg_type = rtm; | ||
311 | req.i.ifi_family = preferred_family; | ||
312 | if (rtm == RTM_NEWLINK) | ||
313 | req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL; | ||
314 | |||
315 | while (*argv) { | ||
316 | arg = index_in_substrings(keywords, *argv); | ||
317 | if (arg == ARG_link) { | ||
318 | NEXT_ARG(); | ||
319 | link_str = *argv; | ||
320 | } else if (arg == ARG_name) { | ||
321 | NEXT_ARG(); | ||
322 | name_str = *argv; | ||
323 | } else if (arg == ARG_type) { | ||
324 | NEXT_ARG(); | ||
325 | type_str = *argv; | ||
326 | } else { | ||
327 | if (arg == ARG_dev) { | ||
328 | if (dev_str) | ||
329 | duparg(*argv, "dev"); | ||
330 | NEXT_ARG(); | ||
331 | } | ||
332 | dev_str = *argv; | ||
333 | } | ||
334 | argv++; | ||
335 | } | ||
336 | xrtnl_open(&rth); | ||
337 | ll_init_map(&rth); | ||
338 | if (type_str) { | ||
339 | struct rtattr *linkinfo = NLMSG_TAIL(&req.n); | ||
340 | |||
341 | addattr_l(&req.n, sizeof(req), IFLA_LINKINFO, NULL, 0); | ||
342 | addattr_l(&req.n, sizeof(req), IFLA_INFO_KIND, type_str, | ||
343 | strlen(type_str)); | ||
344 | linkinfo->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)linkinfo; | ||
345 | } | ||
346 | if (rtm != RTM_NEWLINK) { | ||
347 | if (!dev_str) | ||
348 | return 1; /* Need a device to delete */ | ||
349 | req.i.ifi_index = xll_name_to_index(dev_str); | ||
350 | } else { | ||
351 | if (!name_str) | ||
352 | name_str = dev_str; | ||
353 | if (link_str) { | ||
354 | int idx = xll_name_to_index(link_str); | ||
355 | addattr_l(&req.n, sizeof(req), IFLA_LINK, &idx, 4); | ||
356 | } | ||
357 | } | ||
358 | if (name_str) { | ||
359 | const size_t name_len = strlen(name_str) + 1; | ||
360 | if (name_len < 2 || name_len > IFNAMSIZ) | ||
361 | invarg(name_str, "name"); | ||
362 | addattr_l(&req.n, sizeof(req), IFLA_IFNAME, name_str, name_len); | ||
363 | } | ||
364 | if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) | ||
365 | return 2; | ||
366 | return 0; | ||
367 | } | ||
368 | |||
369 | /* Return value becomes exitcode. It's okay to not return at all */ | ||
370 | niro | 816 | int do_iplink(char **argv) |
371 | niro | 532 | { |
372 | niro | 816 | static const char keywords[] ALIGN1 = |
373 | niro | 1123 | "add\0""delete\0""set\0""show\0""lst\0""list\0"; |
374 | if (*argv) { | ||
375 | smalluint key = index_in_substrings(keywords, *argv); | ||
376 | if (key > 5) /* invalid argument */ | ||
377 | bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name); | ||
378 | argv++; | ||
379 | if (key <= 1) /* add/delete */ | ||
380 | return do_change(argv, key ? RTM_DELLINK : RTM_NEWLINK); | ||
381 | else if (key == 2) /* set */ | ||
382 | return do_set(argv); | ||
383 | } | ||
384 | niro | 816 | /* show, lst, list */ |
385 | return ipaddr_list_link(argv); | ||
386 | niro | 532 | } |