X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=networking%2Fvconfig.c;h=3f12e7609c6af6d33d4912c08cdf05b68ac6280f;hb=08ea11ab0749a7977e6d47cd0fa7b3c9cc10af32;hp=3bd9c3040e6f12c7ccbc2e19f6fd9c71b3f18aa3;hpb=b4f3d7f5948f94203687031a3cb0b123104a6516;p=oweals%2Fbusybox.git diff --git a/networking/vconfig.c b/networking/vconfig.c index 3bd9c3040..3f12e7609 100644 --- a/networking/vconfig.c +++ b/networking/vconfig.c @@ -1,45 +1,69 @@ -#include -#include -#include -#include +/* vi: set sw=4 ts=4: */ +/* + * vconfig implementation for busybox + * + * Copyright (C) 2001 Manuel Novoa III + * + * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. + */ + +/* BB_AUDIT SUSv3 N/A */ + +#include "libbb.h" #include -#include -#include -#include -#include "busybox.h" -#define VLAN_GROUP_ARRAY_LEN 4096 -#define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */ +/* Stuff from linux/if_vlan.h, kernel version 2.4.23 */ +enum vlan_ioctl_cmds { + ADD_VLAN_CMD, + DEL_VLAN_CMD, + SET_VLAN_INGRESS_PRIORITY_CMD, + SET_VLAN_EGRESS_PRIORITY_CMD, + GET_VLAN_INGRESS_PRIORITY_CMD, + GET_VLAN_EGRESS_PRIORITY_CMD, + SET_VLAN_NAME_TYPE_CMD, + SET_VLAN_FLAG_CMD +}; +enum vlan_name_types { + VLAN_NAME_TYPE_PLUS_VID, /* Name will look like: vlan0005 */ + VLAN_NAME_TYPE_RAW_PLUS_VID, /* name will look like: eth1.0005 */ + VLAN_NAME_TYPE_PLUS_VID_NO_PAD, /* Name will look like: vlan5 */ + VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD, /* Name will look like: eth0.5 */ + VLAN_NAME_TYPE_HIGHEST +}; -/* This is rather specialized in that we're passing a 'char **' in - * order to avoid the pointer dereference multiple times in the - * actual calls below. */ -static unsigned long xstrtoul10(char **str, unsigned long max_val) -{ - char *endptr; - unsigned long r; +struct vlan_ioctl_args { + int cmd; /* Should be one of the vlan_ioctl_cmds enum above. */ + char device1[24]; - r = strtoul(str[2], &endptr, 10); - if ((r > max_val) || (*endptr != 0)) { - show_usage(); - } - return r; -} + union { + char device2[24]; + int VID; + unsigned int skb_priority; + unsigned int name_type; + unsigned int bind_type; + unsigned int flag; /* Matches vlan_dev_info flags */ + } u; + + short vlan_qos; +}; + +#define VLAN_GROUP_ARRAY_LEN 4096 +#define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */ /* On entry, table points to the length of the current string plus - * nul terminator plus data lenght for the subsequent entry. The + * nul terminator plus data length for the subsequent entry. The * return value is the last data entry for the matching string. */ static const char *xfind_str(const char *table, const char *str) { while (strcasecmp(str, table+1) != 0) { if (!*(table += table[0])) { - show_usage(); + bb_show_usage(); } } return table - 1; } -static const char cmds[] = { +static const char cmds[] ALIGN1 = { 4, ADD_VLAN_CMD, 7, 'a', 'd', 'd', 0, 3, DEL_VLAN_CMD, 7, @@ -48,7 +72,7 @@ static const char cmds[] = { 's', 'e', 't', '_', 'n', 'a', 'm', 'e', '_', 't', 'y', 'p', 'e', 0, - 4, SET_VLAN_FLAG_CMD, 12, + 5, SET_VLAN_FLAG_CMD, 12, 's', 'e', 't', '_', 'f', 'l', 'a', 'g', 0, 5, SET_VLAN_EGRESS_PRIORITY_CMD, 18, @@ -61,13 +85,13 @@ static const char cmds[] = { 'm', 'a', 'p', 0, }; -static const char name_types[] = { +static const char name_types[] ALIGN1 = { VLAN_NAME_TYPE_PLUS_VID, 16, 'V', 'L', 'A', 'N', '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D', 0, VLAN_NAME_TYPE_PLUS_VID_NO_PAD, 22, - 'V', 'L', 'A', 'N', + 'V', 'L', 'A', 'N', '_', 'P', 'L', 'U', 'S', '_', 'V', 'I', 'D', '_', 'N', 'O', '_', 'P', 'A', 'D', 0, VLAN_NAME_TYPE_RAW_PLUS_VID, 15, @@ -80,8 +104,9 @@ static const char name_types[] = { '_', 'N', 'O', '_', 'P', 'A', 'D', 0, }; -static const char conf_file_name[] = "/proc/net/vlan/config"; +static const char conf_file_name[] ALIGN1 = "/proc/net/vlan/config"; +int vconfig_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int vconfig_main(int argc, char **argv) { struct vlan_ioctl_args ifr; @@ -89,13 +114,12 @@ int vconfig_main(int argc, char **argv) int fd; if (argc < 3) { - show_usage(); + bb_show_usage(); } /* Don't bother closing the filedes. It will be closed on cleanup. */ - if (open(conf_file_name, O_RDONLY) < 0) { /* Is 802.1q is present? */ - perror_msg_and_die("open %s", conf_file_name); - } + /* Will die if 802.1q is not present */ + xopen(conf_file_name, O_RDONLY); memset(&ifr, 0, sizeof(struct vlan_ioctl_args)); @@ -103,16 +127,14 @@ int vconfig_main(int argc, char **argv) p = xfind_str(cmds+2, *argv); ifr.cmd = *p; if (argc != p[-1]) { - show_usage(); + bb_show_usage(); } if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */ ifr.u.name_type = *xfind_str(name_types+1, argv[1]); } else { - if (strlen(argv[1]) >= IF_NAMESIZE) { - error_msg_and_die("if_name >= %d chars\n", IF_NAMESIZE); - } - strcpy(ifr.device1, argv[1]); + strncpy(ifr.device1, argv[1], IFNAMSIZ); + p = argv[2]; /* I suppose one could try to combine some of the function calls below, * since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized @@ -120,21 +142,20 @@ int vconfig_main(int argc, char **argv) * doing so wouldn't save that much space and would also make maintainence * more of a pain. */ if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */ - ifr.u.flag = xstrtoul10(argv, 1); + ifr.u.flag = xatoul_range(p, 0, 1); + /* DM: in order to set reorder header, qos must be set */ + ifr.vlan_qos = xatoul_range(argv[3], 0, 7); } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */ - ifr.u.VID = xstrtoul10(argv, VLAN_GROUP_ARRAY_LEN-1); + ifr.u.VID = xatoul_range(p, 0, VLAN_GROUP_ARRAY_LEN-1); } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */ - ifr.u.skb_priority = xstrtoul10(argv, ULONG_MAX); - ifr.vlan_qos = xstrtoul10(argv+1, 7); + ifr.u.skb_priority = xatou(p); + ifr.vlan_qos = xatoul_range(argv[3], 0, 7); } } - if (((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) - || (ioctl(fd, SIOCSIFVLAN, &ifr) < 0) - ) { - perror_msg_and_die("socket or ioctl error for %s", *argv); - } + fd = xsocket(AF_INET, SOCK_STREAM, 0); + ioctl_or_perror_and_die(fd, SIOCSIFVLAN, &ifr, + "ioctl error for %s", *argv); return 0; } -