diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-06-07 22:13:04 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-06-07 22:13:04 +0000 |
commit | 0876822ded1b1c7ad633c76f6c878ce678bbe42e (patch) | |
tree | 7d313702c07ee5ce30e54f95aa1b1b6ef003e730 /networking/ifenslave.c | |
parent | dd6f570fa3978bdc056ffae4c2ac509250f97177 (diff) | |
download | busybox-w32-0876822ded1b1c7ad633c76f6c878ce678bbe42e.tar.gz busybox-w32-0876822ded1b1c7ad633c76f6c878ce678bbe42e.tar.bz2 busybox-w32-0876822ded1b1c7ad633c76f6c878ce678bbe42e.zip |
ifenslave: fix improper longopts definition
ifenslave: shrink
function old new delta
set_hwaddr - 45 +45
set_ifrname_and_do_ioctl - 26 +26
set_mtu - 25 +25
ioctl_on_skfd - 13 +13
static.ifenslave_longopts 31 34 +3
set_if_up 39 36 -3
set_if_down 39 36 -3
set_if_flags 38 26 -12
static.ifra 32 16 -16
get_slave_flags 40 24 -16
get_if_settings 98 67 -31
set_slave_mtu 37 - -37
set_slave_hwaddr 52 - -52
set_master_hwaddr 52 - -52
ifenslave_main 1566 1411 -155
------------------------------------------------------------------------------
(add/remove: 4/3 grow/shrink: 1/7 up/down: 112/-377) Total: -265 bytes
text data bss dec hex filename
810002 624 7060 817686 c7a16 busybox_old
809737 624 7060 817421 c790d busybox_unstripped
Diffstat (limited to 'networking/ifenslave.c')
-rw-r--r-- | networking/ifenslave.c | 183 |
1 files changed, 82 insertions, 101 deletions
diff --git a/networking/ifenslave.c b/networking/ifenslave.c index f3b4d7466..071cac722 100644 --- a/networking/ifenslave.c +++ b/networking/ifenslave.c | |||
@@ -116,7 +116,7 @@ struct dev_data { | |||
116 | }; | 116 | }; |
117 | 117 | ||
118 | 118 | ||
119 | enum { skfd = 3 }; /* AF_INET socket for ioctl() calls.*/ | 119 | enum { skfd = 3 }; /* AF_INET socket for ioctl() calls. */ |
120 | struct globals { | 120 | struct globals { |
121 | unsigned abi_ver; /* userland - kernel ABI version */ | 121 | unsigned abi_ver; /* userland - kernel ABI version */ |
122 | smallint hwaddr_set; /* Master's hwaddr is set */ | 122 | smallint hwaddr_set; /* Master's hwaddr is set */ |
@@ -136,12 +136,11 @@ struct globals { | |||
136 | static void get_drv_info(char *master_ifname); | 136 | static void get_drv_info(char *master_ifname); |
137 | static int get_if_settings(char *ifname, struct dev_data *dd); | 137 | static int get_if_settings(char *ifname, struct dev_data *dd); |
138 | static int get_slave_flags(char *slave_ifname); | 138 | static int get_slave_flags(char *slave_ifname); |
139 | static int set_master_hwaddr(char *master_ifname, struct sockaddr *hwaddr); | 139 | static int set_hwaddr(char *ifname, struct sockaddr *hwaddr); |
140 | static int set_slave_hwaddr(char *slave_ifname, struct sockaddr *hwaddr); | 140 | static int set_mtu(char *ifname, int mtu); |
141 | static int set_slave_mtu(char *slave_ifname, int mtu); | 141 | static int set_if_flags(char *ifname, int flags); |
142 | static int set_if_flags(char *ifname, short flags); | 142 | static int set_if_up(char *ifname, int flags); |
143 | static int set_if_up(char *ifname, short flags); | 143 | static int set_if_down(char *ifname, int flags); |
144 | static int set_if_down(char *ifname, short flags); | ||
145 | static int clear_if_addr(char *ifname); | 144 | static int clear_if_addr(char *ifname); |
146 | static int set_if_addr(char *master_ifname, char *slave_ifname); | 145 | static int set_if_addr(char *master_ifname, char *slave_ifname); |
147 | static void change_active(char *master_ifname, char *slave_ifname); | 146 | static void change_active(char *master_ifname, char *slave_ifname); |
@@ -153,6 +152,17 @@ static void strncpy_IFNAMSIZ(char *dst, const char *src) | |||
153 | strncpy(dst, src, IFNAMSIZ); | 152 | strncpy(dst, src, IFNAMSIZ); |
154 | } | 153 | } |
155 | 154 | ||
155 | static int ioctl_on_skfd(unsigned request, struct ifreq *ifr) | ||
156 | { | ||
157 | return ioctl(skfd, request, ifr); | ||
158 | } | ||
159 | |||
160 | static int set_ifrname_and_do_ioctl(unsigned request, struct ifreq *ifr, const char *ifname) | ||
161 | { | ||
162 | strncpy_IFNAMSIZ(ifr->ifr_name, ifname); | ||
163 | return ioctl_on_skfd(request, ifr); | ||
164 | } | ||
165 | |||
156 | int ifenslave_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 166 | int ifenslave_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
157 | int ifenslave_main(int argc ATTRIBUTE_UNUSED, char **argv) | 167 | int ifenslave_main(int argc ATTRIBUTE_UNUSED, char **argv) |
158 | { | 168 | { |
@@ -167,10 +177,10 @@ int ifenslave_main(int argc ATTRIBUTE_UNUSED, char **argv) | |||
167 | }; | 177 | }; |
168 | #if ENABLE_GETOPT_LONG | 178 | #if ENABLE_GETOPT_LONG |
169 | static const char ifenslave_longopts[] ALIGN1 = | 179 | static const char ifenslave_longopts[] ALIGN1 = |
170 | "change-active" No_argument "c" | 180 | "change-active\0" No_argument "c" |
171 | "detach" No_argument "d" | 181 | "detach\0" No_argument "d" |
172 | "force" No_argument "f" | 182 | "force\0" No_argument "f" |
173 | ; | 183 | ; |
174 | 184 | ||
175 | applet_long_options = ifenslave_longopts; | 185 | applet_long_options = ifenslave_longopts; |
176 | #endif | 186 | #endif |
@@ -181,10 +191,9 @@ int ifenslave_main(int argc ATTRIBUTE_UNUSED, char **argv) | |||
181 | if (opt & (opt-1)) /* options check */ | 191 | if (opt & (opt-1)) /* options check */ |
182 | bb_show_usage(); | 192 | bb_show_usage(); |
183 | 193 | ||
184 | /* Copy the interface name */ | ||
185 | master_ifname = *argv++; | 194 | master_ifname = *argv++; |
186 | 195 | ||
187 | /* No remaining args means show all interfaces. */ | 196 | /* No interface names - show all interfaces. */ |
188 | if (!master_ifname) { | 197 | if (!master_ifname) { |
189 | display_interfaces(NULL); | 198 | display_interfaces(NULL); |
190 | return EXIT_SUCCESS; | 199 | return EXIT_SUCCESS; |
@@ -193,14 +202,16 @@ int ifenslave_main(int argc ATTRIBUTE_UNUSED, char **argv) | |||
193 | /* Open a basic socket */ | 202 | /* Open a basic socket */ |
194 | xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), skfd); | 203 | xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), skfd); |
195 | 204 | ||
196 | /* exchange abi version with bonding module */ | 205 | /* Exchange abi version with bonding module */ |
197 | get_drv_info(master_ifname); | 206 | get_drv_info(master_ifname); |
198 | 207 | ||
199 | slave_ifname = *argv++; | 208 | slave_ifname = *argv++; |
200 | if (!slave_ifname) { | 209 | if (!slave_ifname) { |
201 | if (opt & (OPT_d|OPT_c)) { | 210 | if (opt & (OPT_d|OPT_c)) { |
202 | display_interfaces(slave_ifname); | 211 | /* --change or --detach, and no slaves given - |
203 | return 2; /* why? */ | 212 | * show all interfaces. */ |
213 | display_interfaces(slave_ifname /* == NULL */); | ||
214 | return 2; /* why 2? */ | ||
204 | } | 215 | } |
205 | /* A single arg means show the | 216 | /* A single arg means show the |
206 | * configuration for this interface | 217 | * configuration for this interface |
@@ -225,12 +236,11 @@ int ifenslave_main(int argc ATTRIBUTE_UNUSED, char **argv) | |||
225 | if (!(master.flags.ifr_flags & IFF_UP)) | 236 | if (!(master.flags.ifr_flags & IFF_UP)) |
226 | bb_error_msg_and_die("%s is not up", master_ifname); | 237 | bb_error_msg_and_die("%s is not up", master_ifname); |
227 | 238 | ||
228 | /* Only for enslaving */ | 239 | /* No opts: neither -c[hange] nor -d[etach] -> it's "enslave" then; |
229 | if (!(opt & (OPT_c|OPT_d))) { | 240 | * and -f[orce] is not there too */ |
230 | sa_family_t master_family = master.hwaddr.ifr_hwaddr.sa_family; | 241 | if (!opt) { |
231 | |||
232 | /* The family '1' is ARPHRD_ETHER for ethernet. */ | 242 | /* The family '1' is ARPHRD_ETHER for ethernet. */ |
233 | if (master_family != 1 && !(opt & OPT_f)) { | 243 | if (master.hwaddr.ifr_hwaddr.sa_family != 1) { |
234 | bb_error_msg_and_die( | 244 | bb_error_msg_and_die( |
235 | "%s is not ethernet-like (-f overrides)", | 245 | "%s is not ethernet-like (-f overrides)", |
236 | master_ifname); | 246 | master_ifname); |
@@ -240,8 +250,7 @@ int ifenslave_main(int argc ATTRIBUTE_UNUSED, char **argv) | |||
240 | /* Accepts only one slave */ | 250 | /* Accepts only one slave */ |
241 | if (opt & OPT_c) { | 251 | if (opt & OPT_c) { |
242 | /* change active slave */ | 252 | /* change active slave */ |
243 | res = get_slave_flags(slave_ifname); | 253 | if (get_slave_flags(slave_ifname)) { |
244 | if (res) { | ||
245 | bb_perror_msg_and_die( | 254 | bb_perror_msg_and_die( |
246 | "%s: can't get flags", slave_ifname); | 255 | "%s: can't get flags", slave_ifname); |
247 | } | 256 | } |
@@ -256,8 +265,8 @@ int ifenslave_main(int argc ATTRIBUTE_UNUSED, char **argv) | |||
256 | /* detach a slave interface from the master */ | 265 | /* detach a slave interface from the master */ |
257 | rv = get_slave_flags(slave_ifname); | 266 | rv = get_slave_flags(slave_ifname); |
258 | if (rv) { | 267 | if (rv) { |
259 | /* Can't work with this slave. */ | 268 | /* Can't work with this slave, */ |
260 | /* remember the error and skip it*/ | 269 | /* remember the error and skip it */ |
261 | bb_perror_msg( | 270 | bb_perror_msg( |
262 | "skipping %s: can't get flags", | 271 | "skipping %s: can't get flags", |
263 | slave_ifname); | 272 | slave_ifname); |
@@ -276,8 +285,8 @@ int ifenslave_main(int argc ATTRIBUTE_UNUSED, char **argv) | |||
276 | /* attach a slave interface to the master */ | 285 | /* attach a slave interface to the master */ |
277 | rv = get_if_settings(slave_ifname, &slave); | 286 | rv = get_if_settings(slave_ifname, &slave); |
278 | if (rv) { | 287 | if (rv) { |
279 | /* Can't work with this slave. */ | 288 | /* Can't work with this slave, */ |
280 | /* remember the error and skip it*/ | 289 | /* remember the error and skip it */ |
281 | bb_perror_msg( | 290 | bb_perror_msg( |
282 | "skipping %s: can't get settings", | 291 | "skipping %s: can't get settings", |
283 | slave_ifname); | 292 | slave_ifname); |
@@ -308,14 +317,11 @@ static void get_drv_info(char *master_ifname) | |||
308 | struct ethtool_drvinfo info; | 317 | struct ethtool_drvinfo info; |
309 | 318 | ||
310 | memset(&ifr, 0, sizeof(ifr)); | 319 | memset(&ifr, 0, sizeof(ifr)); |
311 | strncpy_IFNAMSIZ(ifr.ifr_name, master_ifname); | ||
312 | ifr.ifr_data = (caddr_t)&info; | 320 | ifr.ifr_data = (caddr_t)&info; |
313 | |||
314 | info.cmd = ETHTOOL_GDRVINFO; | 321 | info.cmd = ETHTOOL_GDRVINFO; |
315 | strncpy(info.driver, "ifenslave", 32); | 322 | strncpy(info.driver, "ifenslave", 32); |
316 | snprintf(info.fw_version, 32, "%d", BOND_ABI_VERSION); | 323 | snprintf(info.fw_version, 32, "%d", BOND_ABI_VERSION); |
317 | 324 | if (set_ifrname_and_do_ioctl(SIOCETHTOOL, &ifr, master_ifname) < 0) { | |
318 | if (ioctl(skfd, SIOCETHTOOL, &ifr) < 0) { | ||
319 | if (errno == EOPNOTSUPP) | 325 | if (errno == EOPNOTSUPP) |
320 | return; | 326 | return; |
321 | bb_perror_msg_and_die("%s: SIOCETHTOOL error", master_ifname); | 327 | bb_perror_msg_and_die("%s: SIOCETHTOOL error", master_ifname); |
@@ -324,8 +330,6 @@ static void get_drv_info(char *master_ifname) | |||
324 | abi_ver = bb_strtou(info.fw_version, NULL, 0); | 330 | abi_ver = bb_strtou(info.fw_version, NULL, 0); |
325 | if (errno) | 331 | if (errno) |
326 | bb_error_msg_and_die("%s: SIOCETHTOOL error", master_ifname); | 332 | bb_error_msg_and_die("%s: SIOCETHTOOL error", master_ifname); |
327 | |||
328 | return; | ||
329 | } | 333 | } |
330 | 334 | ||
331 | static void change_active(char *master_ifname, char *slave_ifname) | 335 | static void change_active(char *master_ifname, char *slave_ifname) |
@@ -338,10 +342,9 @@ static void change_active(char *master_ifname, char *slave_ifname) | |||
338 | slave_ifname); | 342 | slave_ifname); |
339 | } | 343 | } |
340 | 344 | ||
341 | strncpy_IFNAMSIZ(ifr.ifr_name, master_ifname); | ||
342 | strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname); | 345 | strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname); |
343 | if (ioctl(skfd, SIOCBONDCHANGEACTIVE, &ifr) < 0 | 346 | if (set_ifrname_and_do_ioctl(SIOCBONDCHANGEACTIVE, &ifr, master_ifname) |
344 | && ioctl(skfd, BOND_CHANGE_ACTIVE_OLD, &ifr) < 0 | 347 | && ioctl_on_skfd(BOND_CHANGE_ACTIVE_OLD, &ifr) |
345 | ) { | 348 | ) { |
346 | bb_perror_msg_and_die( | 349 | bb_perror_msg_and_die( |
347 | "master %s, slave %s: can't " | 350 | "master %s, slave %s: can't " |
@@ -384,7 +387,7 @@ static int enslave(char *master_ifname, char *slave_ifname) | |||
384 | } | 387 | } |
385 | 388 | ||
386 | if (master.mtu.ifr_mtu != slave.mtu.ifr_mtu) { | 389 | if (master.mtu.ifr_mtu != slave.mtu.ifr_mtu) { |
387 | res = set_slave_mtu(slave_ifname, master.mtu.ifr_mtu); | 390 | res = set_mtu(slave_ifname, master.mtu.ifr_mtu); |
388 | if (res) { | 391 | if (res) { |
389 | bb_perror_msg("%s: can't set MTU", slave_ifname); | 392 | bb_perror_msg("%s: can't set MTU", slave_ifname); |
390 | return res; | 393 | return res; |
@@ -400,9 +403,7 @@ static int enslave(char *master_ifname, char *slave_ifname) | |||
400 | * the application sets the slave's | 403 | * the application sets the slave's |
401 | * hwaddr | 404 | * hwaddr |
402 | */ | 405 | */ |
403 | res = set_slave_hwaddr(slave_ifname, | 406 | if (set_hwaddr(slave_ifname, &(master.hwaddr.ifr_hwaddr))) { |
404 | &(master.hwaddr.ifr_hwaddr)); | ||
405 | if (res) { | ||
406 | bb_perror_msg("%s: can't set hw address", | 407 | bb_perror_msg("%s: can't set hw address", |
407 | slave_ifname); | 408 | slave_ifname); |
408 | goto undo_mtu; | 409 | goto undo_mtu; |
@@ -411,8 +412,7 @@ static int enslave(char *master_ifname, char *slave_ifname) | |||
411 | /* For old ABI the application needs to bring the | 412 | /* For old ABI the application needs to bring the |
412 | * slave back up | 413 | * slave back up |
413 | */ | 414 | */ |
414 | res = set_if_up(slave_ifname, slave.flags.ifr_flags); | 415 | if (set_if_up(slave_ifname, slave.flags.ifr_flags)) |
415 | if (res) | ||
416 | goto undo_slave_mac; | 416 | goto undo_slave_mac; |
417 | } | 417 | } |
418 | /* The driver is using a new ABI, | 418 | /* The driver is using a new ABI, |
@@ -428,14 +428,11 @@ static int enslave(char *master_ifname, char *slave_ifname) | |||
428 | /* For old ABI, the master needs to be | 428 | /* For old ABI, the master needs to be |
429 | * down before setting it's hwaddr | 429 | * down before setting it's hwaddr |
430 | */ | 430 | */ |
431 | res = set_if_down(master_ifname, master.flags.ifr_flags); | 431 | if (set_if_down(master_ifname, master.flags.ifr_flags)) |
432 | if (res) | ||
433 | goto undo_mtu; | 432 | goto undo_mtu; |
434 | } | 433 | } |
435 | 434 | ||
436 | res = set_master_hwaddr(master_ifname, | 435 | if (set_hwaddr(master_ifname, &(slave.hwaddr.ifr_hwaddr))) { |
437 | &(slave.hwaddr.ifr_hwaddr)); | ||
438 | if (res) { | ||
439 | bb_error_msg("%s: can't set hw address", | 436 | bb_error_msg("%s: can't set hw address", |
440 | master_ifname); | 437 | master_ifname); |
441 | goto undo_mtu; | 438 | goto undo_mtu; |
@@ -445,8 +442,7 @@ static int enslave(char *master_ifname, char *slave_ifname) | |||
445 | /* For old ABI, bring the master | 442 | /* For old ABI, bring the master |
446 | * back up | 443 | * back up |
447 | */ | 444 | */ |
448 | res = set_if_up(master_ifname, master.flags.ifr_flags); | 445 | if (set_if_up(master_ifname, master.flags.ifr_flags)) |
449 | if (res) | ||
450 | goto undo_master_mac; | 446 | goto undo_master_mac; |
451 | } | 447 | } |
452 | 448 | ||
@@ -454,29 +450,26 @@ static int enslave(char *master_ifname, char *slave_ifname) | |||
454 | } | 450 | } |
455 | 451 | ||
456 | /* Do the real thing */ | 452 | /* Do the real thing */ |
457 | strncpy_IFNAMSIZ(ifr.ifr_name, master_ifname); | ||
458 | strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname); | 453 | strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname); |
459 | if (ioctl(skfd, SIOCBONDENSLAVE, &ifr) < 0 | 454 | if (set_ifrname_and_do_ioctl(SIOCBONDENSLAVE, &ifr, master_ifname) |
460 | && ioctl(skfd, BOND_ENSLAVE_OLD, &ifr) < 0 | 455 | && ioctl_on_skfd(BOND_ENSLAVE_OLD, &ifr) |
461 | ) { | 456 | ) { |
462 | res = 1; | ||
463 | } | ||
464 | |||
465 | if (res) | ||
466 | goto undo_master_mac; | 457 | goto undo_master_mac; |
458 | } | ||
467 | 459 | ||
468 | return 0; | 460 | return 0; |
469 | 461 | ||
470 | /* rollback (best effort) */ | 462 | /* rollback (best effort) */ |
471 | undo_master_mac: | 463 | undo_master_mac: |
472 | set_master_hwaddr(master_ifname, &(master.hwaddr.ifr_hwaddr)); | 464 | set_hwaddr(master_ifname, &(master.hwaddr.ifr_hwaddr)); |
473 | hwaddr_set = 0; | 465 | hwaddr_set = 0; |
474 | goto undo_mtu; | 466 | goto undo_mtu; |
467 | |||
475 | undo_slave_mac: | 468 | undo_slave_mac: |
476 | set_slave_hwaddr(slave_ifname, &(slave.hwaddr.ifr_hwaddr)); | 469 | set_hwaddr(slave_ifname, &(slave.hwaddr.ifr_hwaddr)); |
477 | undo_mtu: | 470 | undo_mtu: |
478 | set_slave_mtu(slave_ifname, slave.mtu.ifr_mtu); | 471 | set_mtu(slave_ifname, slave.mtu.ifr_mtu); |
479 | return res; | 472 | return 1; |
480 | } | 473 | } |
481 | 474 | ||
482 | static int release(char *master_ifname, char *slave_ifname) | 475 | static int release(char *master_ifname, char *slave_ifname) |
@@ -490,10 +483,9 @@ static int release(char *master_ifname, char *slave_ifname) | |||
490 | return 1; | 483 | return 1; |
491 | } | 484 | } |
492 | 485 | ||
493 | strncpy_IFNAMSIZ(ifr.ifr_name, master_ifname); | ||
494 | strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname); | 486 | strncpy_IFNAMSIZ(ifr.ifr_slave, slave_ifname); |
495 | if (ioctl(skfd, SIOCBONDRELEASE, &ifr) < 0 | 487 | if (set_ifrname_and_do_ioctl(SIOCBONDRELEASE, &ifr, master_ifname) < 0 |
496 | && ioctl(skfd, BOND_RELEASE_OLD, &ifr) < 0 | 488 | && ioctl_on_skfd(BOND_RELEASE_OLD, &ifr) < 0 |
497 | ) { | 489 | ) { |
498 | return 1; | 490 | return 1; |
499 | } | 491 | } |
@@ -505,7 +497,7 @@ static int release(char *master_ifname, char *slave_ifname) | |||
505 | } | 497 | } |
506 | 498 | ||
507 | /* set to default mtu */ | 499 | /* set to default mtu */ |
508 | set_slave_mtu(slave_ifname, 1500); | 500 | set_mtu(slave_ifname, 1500); |
509 | 501 | ||
510 | return res; | 502 | return res; |
511 | } | 503 | } |
@@ -514,59 +506,43 @@ static int get_if_settings(char *ifname, struct dev_data *dd) | |||
514 | { | 506 | { |
515 | int res; | 507 | int res; |
516 | 508 | ||
517 | strncpy_IFNAMSIZ(dd->mtu.ifr_name, ifname); | 509 | res = set_ifrname_and_do_ioctl(SIOCGIFMTU, &dd->mtu, ifname); |
518 | res = ioctl(skfd, SIOCGIFMTU, &dd->mtu); | 510 | res |= set_ifrname_and_do_ioctl(SIOCGIFFLAGS, &dd->flags, ifname); |
519 | strncpy_IFNAMSIZ(dd->flags.ifr_name, ifname); | 511 | res |= set_ifrname_and_do_ioctl(SIOCGIFHWADDR, &dd->hwaddr, ifname); |
520 | res |= ioctl(skfd, SIOCGIFFLAGS, &dd->flags); | ||
521 | strncpy_IFNAMSIZ(dd->hwaddr.ifr_name, ifname); | ||
522 | res |= ioctl(skfd, SIOCGIFHWADDR, &dd->hwaddr); | ||
523 | 512 | ||
524 | return res; | 513 | return res; |
525 | } | 514 | } |
526 | 515 | ||
527 | static int get_slave_flags(char *slave_ifname) | 516 | static int get_slave_flags(char *slave_ifname) |
528 | { | 517 | { |
529 | strncpy_IFNAMSIZ(slave.flags.ifr_name, slave_ifname); | 518 | return set_ifrname_and_do_ioctl(SIOCGIFFLAGS, &slave.flags, slave_ifname); |
530 | return ioctl(skfd, SIOCGIFFLAGS, &slave.flags); | ||
531 | } | 519 | } |
532 | 520 | ||
533 | static int set_master_hwaddr(char *master_ifname, struct sockaddr *hwaddr) | 521 | static int set_hwaddr(char *ifname, struct sockaddr *hwaddr) |
534 | { | 522 | { |
535 | struct ifreq ifr; | 523 | struct ifreq ifr; |
536 | 524 | ||
537 | strncpy_IFNAMSIZ(ifr.ifr_name, master_ifname); | 525 | memcpy(&(ifr.ifr_hwaddr), hwaddr, sizeof(*hwaddr)); |
538 | memcpy(&(ifr.ifr_hwaddr), hwaddr, sizeof(struct sockaddr)); | 526 | return set_ifrname_and_do_ioctl(SIOCSIFHWADDR, &ifr, ifname); |
539 | return ioctl(skfd, SIOCSIFHWADDR, &ifr); | ||
540 | } | 527 | } |
541 | 528 | ||
542 | static int set_slave_hwaddr(char *slave_ifname, struct sockaddr *hwaddr) | 529 | static int set_mtu(char *ifname, int mtu) |
543 | { | ||
544 | struct ifreq ifr; | ||
545 | |||
546 | strncpy_IFNAMSIZ(ifr.ifr_name, slave_ifname); | ||
547 | memcpy(&(ifr.ifr_hwaddr), hwaddr, sizeof(struct sockaddr)); | ||
548 | return ioctl(skfd, SIOCSIFHWADDR, &ifr); | ||
549 | } | ||
550 | |||
551 | static int set_slave_mtu(char *slave_ifname, int mtu) | ||
552 | { | 530 | { |
553 | struct ifreq ifr; | 531 | struct ifreq ifr; |
554 | 532 | ||
555 | ifr.ifr_mtu = mtu; | 533 | ifr.ifr_mtu = mtu; |
556 | strncpy_IFNAMSIZ(ifr.ifr_name, slave_ifname); | 534 | return set_ifrname_and_do_ioctl(SIOCSIFMTU, &ifr, ifname); |
557 | return ioctl(skfd, SIOCSIFMTU, &ifr); | ||
558 | } | 535 | } |
559 | 536 | ||
560 | static int set_if_flags(char *ifname, short flags) | 537 | static int set_if_flags(char *ifname, int flags) |
561 | { | 538 | { |
562 | struct ifreq ifr; | 539 | struct ifreq ifr; |
563 | 540 | ||
564 | ifr.ifr_flags = flags; | 541 | ifr.ifr_flags = flags; |
565 | strncpy_IFNAMSIZ(ifr.ifr_name, ifname); | 542 | return set_ifrname_and_do_ioctl(SIOCSIFFLAGS, &ifr, ifname); |
566 | return ioctl(skfd, SIOCSIFFLAGS, &ifr); | ||
567 | } | 543 | } |
568 | 544 | ||
569 | static int set_if_up(char *ifname, short flags) | 545 | static int set_if_up(char *ifname, int flags) |
570 | { | 546 | { |
571 | int res = set_if_flags(ifname, flags | IFF_UP); | 547 | int res = set_if_flags(ifname, flags | IFF_UP); |
572 | if (res) | 548 | if (res) |
@@ -574,7 +550,7 @@ static int set_if_up(char *ifname, short flags) | |||
574 | return res; | 550 | return res; |
575 | } | 551 | } |
576 | 552 | ||
577 | static int set_if_down(char *ifname, short flags) | 553 | static int set_if_down(char *ifname, int flags) |
578 | { | 554 | { |
579 | int res = set_if_flags(ifname, flags & ~IFF_UP); | 555 | int res = set_if_flags(ifname, flags & ~IFF_UP); |
580 | if (res) | 556 | if (res) |
@@ -586,17 +562,24 @@ static int clear_if_addr(char *ifname) | |||
586 | { | 562 | { |
587 | struct ifreq ifr; | 563 | struct ifreq ifr; |
588 | 564 | ||
589 | strncpy_IFNAMSIZ(ifr.ifr_name, ifname); | ||
590 | ifr.ifr_addr.sa_family = AF_INET; | 565 | ifr.ifr_addr.sa_family = AF_INET; |
591 | memset(ifr.ifr_addr.sa_data, 0, sizeof(ifr.ifr_addr.sa_data)); | 566 | memset(ifr.ifr_addr.sa_data, 0, sizeof(ifr.ifr_addr.sa_data)); |
592 | return ioctl(skfd, SIOCSIFADDR, &ifr); | 567 | return set_ifrname_and_do_ioctl(SIOCSIFADDR, &ifr, ifname); |
593 | } | 568 | } |
594 | 569 | ||
595 | static int set_if_addr(char *master_ifname, char *slave_ifname) | 570 | static int set_if_addr(char *master_ifname, char *slave_ifname) |
596 | { | 571 | { |
572 | #if (SIOCGIFADDR | SIOCSIFADDR \ | ||
573 | | SIOCGIFDSTADDR | SIOCSIFDSTADDR \ | ||
574 | | SIOCGIFBRDADDR | SIOCSIFBRDADDR \ | ||
575 | | SIOCGIFNETMASK | SIOCSIFNETMASK) <= 0xffff | ||
576 | #define INT uint16_t | ||
577 | #else | ||
578 | #define INT int | ||
579 | #endif | ||
597 | static const struct { | 580 | static const struct { |
598 | int g_ioctl; | 581 | INT g_ioctl; |
599 | int s_ioctl; | 582 | INT s_ioctl; |
600 | } ifra[] = { | 583 | } ifra[] = { |
601 | { SIOCGIFADDR, SIOCSIFADDR }, | 584 | { SIOCGIFADDR, SIOCSIFADDR }, |
602 | { SIOCGIFDSTADDR, SIOCSIFDSTADDR }, | 585 | { SIOCGIFDSTADDR, SIOCSIFDSTADDR }, |
@@ -609,16 +592,14 @@ static int set_if_addr(char *master_ifname, char *slave_ifname) | |||
609 | unsigned i; | 592 | unsigned i; |
610 | 593 | ||
611 | for (i = 0; i < ARRAY_SIZE(ifra); i++) { | 594 | for (i = 0; i < ARRAY_SIZE(ifra); i++) { |
612 | strncpy_IFNAMSIZ(ifr.ifr_name, master_ifname); | 595 | res = set_ifrname_and_do_ioctl(ifra[i].g_ioctl, &ifr, master_ifname); |
613 | res = ioctl(skfd, ifra[i].g_ioctl, &ifr); | ||
614 | if (res < 0) { | 596 | if (res < 0) { |
615 | ifr.ifr_addr.sa_family = AF_INET; | 597 | ifr.ifr_addr.sa_family = AF_INET; |
616 | memset(ifr.ifr_addr.sa_data, 0, | 598 | memset(ifr.ifr_addr.sa_data, 0, |
617 | sizeof(ifr.ifr_addr.sa_data)); | 599 | sizeof(ifr.ifr_addr.sa_data)); |
618 | } | 600 | } |
619 | 601 | ||
620 | strncpy_IFNAMSIZ(ifr.ifr_name, slave_ifname); | 602 | res = set_ifrname_and_do_ioctl(ifra[i].s_ioctl, &ifr, slave_ifname); |
621 | res = ioctl(skfd, ifra[i].s_ioctl, &ifr); | ||
622 | if (res < 0) | 603 | if (res < 0) |
623 | return res; | 604 | return res; |
624 | } | 605 | } |