1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
|
/*====================================================================*
*
* Copyright (c) 2013 Qualcomm Atheros, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted (subject to the limitations
* in the disclaimer below) provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* * Neither the name of Qualcomm Atheros nor the names of
* its contributors may be used to endorse or promote products
* derived from this software without specific prior written
* permission.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
* GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
* COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*--------------------------------------------------------------------*/
/*====================================================================*
*
* plcfwd.c - Atheros PLC Forward Configuration Manager;
*
*
*--------------------------------------------------------------------*/
/*====================================================================*"
* system header files;
*--------------------------------------------------------------------*/
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <ctype.h>
/*====================================================================*
* custom header files;
*--------------------------------------------------------------------*/
#include "../tools/getoptv.h"
#include "../tools/putoptv.h"
#include "../tools/memory.h"
#include "../tools/number.h"
#include "../tools/symbol.h"
#include "../tools/types.h"
#include "../tools/flags.h"
#include "../tools/files.h"
#include "../tools/error.h"
#include "../tools/permissions.h"
#include "../plc/plc.h"
/*====================================================================*
* custom source files;
*--------------------------------------------------------------------*/
#ifndef MAKEFILE
#include "../plc/Confirm.c"
#include "../plc/Display.c"
#include "../plc/Failure.c"
#include "../plc/Request.c"
#include "../plc/ReadMME.c"
#include "../plc/SendMME.c"
#include "../plc/Devices.c"
#endif
#ifndef MAKEFILE
#include "../tools/getoptv.c"
#include "../tools/putoptv.c"
#include "../tools/version.c"
#include "../tools/uintspec.c"
#include "../tools/basespec.c"
#include "../tools/hexdump.c"
#include "../tools/hexview.c"
#include "../tools/hexencode.c"
#include "../tools/hexdecode.c"
#include "../tools/hexout.c"
#include "../tools/todigit.c"
#include "../tools/synonym.c"
#include "../tools/binout.c"
#include "../tools/error.c"
#include "../tools/desuid.c"
#endif
#ifndef MAKEFILE
#include "../ether/initchannel.c"
#include "../ether/openchannel.c"
#include "../ether/closechannel.c"
#include "../ether/readpacket.c"
#include "../ether/sendpacket.c"
#include "../ether/channel.c"
#endif
#ifndef MAKEFILE
#include "../mme/MMECode.c"
#include "../mme/EthernetHeader.c"
#include "../mme/QualcommHeader.c"
#include "../mme/UnwantedMessage.c"
#endif
/*====================================================================*
* program constants;
*--------------------------------------------------------------------*/
#define PLCFWD_VERBOSE (1 << 0)
#define PLCFWD_SILENCE (1 << 1)
#define PLCFWD_LENGTH 0
#define PLCFWD_OFFSET 0
#define PLCFWD_GET 0
#define PLCFWD_ADD 1
#define PLCFWD_REM 2
#define PLCFWD_STO 3
#define PLCFWD_CTL 4
#define PLCFWD_SET 5
#define PLCFWD_FWD 6
#define PLCFWD_VER 0
/*====================================================================*
* program variables;
*--------------------------------------------------------------------*/
/*
* this structure is only used in the VS_FORWARD_CONFIG message but
* it is common to several variations of the message and is used in
* arrays;
*/
#ifndef __GNUC__
#pragma pack (push,1)
#endif
typedef struct item
{
uint8_t MAC_ADDR [ETHER_ADDR_LEN];
uint16_t NUM_VLANIDS;
uint16_t VLANID [10];
}
item;
#ifndef __GNUC__
#pragma pack (pop)
#endif
/*
* synonym table for options -M and -S;
*/
#define STATES (sizeof (states) / sizeof (struct _term_))
static const struct _term_ states [] =
{
{
"disable",
"0"
},
{
"enable",
"1"
},
{
"off",
"0"
},
{
"on",
"1"
}
};
/*====================================================================*
*
* void readitem (struct item * item, char const * string);
*
* encode a slave structure with infomation specified by a string
* specification has the following production:
*
* <spec> := <mac_addr>
* <spec> := <spec>,<vlan_id>
*
* basically, encode slave->MAC_ADDR then encode slave->VLANID[]
* with hexadecimal VLANID values; we allow 10 VLANID values but
* only 8 are legal;
*
* the idea is to read multiple input strings and call this function
* to initialize one or more slave structures; it is possible to fit
* up to 128 slave structures in one message frame;
*
*
*--------------------------------------------------------------------*/
static void readitem (struct item * item, char const * string)
{
register uint8_t * origin = (uint8_t *)(item->MAC_ADDR);
register uint8_t * offset = (uint8_t *)(item->MAC_ADDR);
size_t extent = sizeof (item->MAC_ADDR);
memset (item, 0, sizeof (* item));
while ((extent) && (*string))
{
unsigned radix = RADIX_HEX;
unsigned field = sizeof (uint8_t) + sizeof (uint8_t);
unsigned value = 0;
unsigned digit = 0;
if ((offset != origin) && (*string == HEX_EXTENDER))
{
string++;
}
while (field--)
{
if ((digit = todigit (*string)) < radix)
{
value *= radix;
value += digit;
string++;
continue;
}
error (1, EINVAL, "bad MAC address: ...[%s] (1)", string);
}
*offset = value;
offset++;
extent--;
}
if (extent)
{
error (1, EINVAL, "bad MAC address: ...[%s] (2)", string);
}
while (isspace (*string))
{
string++;
}
if ((*string) && (*string != ','))
{
error (1, EINVAL, "bad MAC address: ...[%s] (3)", string);
}
while (*string == ',')
{
unsigned radix = RADIX_DEC;
unsigned digit = 0;
unsigned value = 0;
do
{
string++;
}
while (isspace (*string));
while ((digit = todigit (*string)) < radix)
{
value *= radix;
value += digit;
string++;
}
while (isspace (*string))
{
string++;
}
if (item->NUM_VLANIDS < (sizeof (item->VLANID) / sizeof (uint16_t)))
{
item->VLANID [item->NUM_VLANIDS++] = value;
}
}
while (isspace (*string))
{
string++;
}
if (*string)
{
error (1, EINVAL, "bad VLAN ID: ...[%s]", string);
}
return;
}
/*====================================================================*
*
* unsigned readlist (struct item list [], unsigned size);
*
* read one or more items from stdin; discard comments; assume one
* item per line; permit multiple items on one line when separated
* by semicolon; items cannot straddle lines; readitem () controls
* what consitutes one item;
*
*--------------------------------------------------------------------*/
static unsigned readlist (struct item list [], unsigned size)
{
struct item * item = list;
char string [1024];
char * sp = string;
signed c;
for (c = getc (stdin); c != EOF; c = getc (stdin))
{
if (isspace (c))
{
continue;
}
if (c == '#')
{
while ((c != '\n') && (c != EOF))
{
c = getc (stdin);
}
continue;
}
sp = string;
while ((c != ';') && (c != '\n') && (c != EOF))
{
*sp++ = (char)(c);
c = getc (stdin);
}
*sp = (char)(0);
if (size)
{
readitem (item++, string);
size--;
}
}
return ((unsigned)(item - list));
}
/*====================================================================*
*
* void showlist (struct item list [], unsigned items)
*
* print item list on stdout in a format suitable for input using
* readlist (); this function may be commented out if it not used;
*
*
*--------------------------------------------------------------------*/
#if 0
static void showlist (struct item list [], unsigned items)
{
while (items--)
{
uint16_t fields = list->NUM_VLANIDS;
uint16_t * field = list->VLANID;
hexout (list->MAC_ADDR, sizeof (list->MAC_ADDR), 0, 0, stdout);
while (fields--)
{
printf (", %d", *field);
field++;
}
printf ("\n");
list++;
}
return;
}
#endif
/*====================================================================*
*
* signed ReadVLANIDs (struct plc * plc, uint32_t offset, uint32_t length);
*
*
*--------------------------------------------------------------------*/
static signed ReadVLANIDs (struct plc * plc, uint32_t offset, uint32_t length)
{
struct channel * channel = (struct channel *)(plc->channel);
struct message * message = (struct message *)(plc->message);
#ifndef __GNUC__
#pragma pack (push,1)
#endif
struct __packed vs_forward_config_request
{
struct ethernet_hdr ethernet;
struct qualcomm_hdr qualcomm;
uint8_t RESERVED1;
uint8_t MREQUEST;
uint8_t MVERSION;
uint32_t RESERVED2;
uint32_t DATA_LENGTH;
uint32_t DATA_OFFSET;
uint16_t RESERVED3;
}
* request = (struct vs_forward_config_request *) (message);
struct __packed vs_forward_config_confirm
{
struct ethernet_hdr ethernet;
struct qualcomm_hdr qualcomm;
uint8_t RESERVED1;
uint8_t RESULTCODE;
uint8_t OPERATION;
uint8_t MVERSION;
uint32_t RESERVED2;
uint32_t DATA_LENGTH;
uint32_t DATA_OFFSET;
uint8_t DATA [PLC_MODULE_SIZE];
}
* confirm = (struct vs_forward_config_confirm *) (message);
#ifndef __GNUC__
#pragma pack (pop)
#endif
memset (message, 0, sizeof (* message));
EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
QualcommHeader (&request->qualcomm, 0, (VS_FORWARD_CONFIG | MMTYPE_REQ));
request->MREQUEST = PLCFWD_GET;
request->MVERSION = PLCFWD_VER;
request->DATA_OFFSET = HTOLE32 (offset);
request->DATA_LENGTH = HTOLE32 (length);
plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
if (SendMME (plc) <= 0)
{
error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
return (-1);
}
while (ReadMME (plc, 0, (VS_FORWARD_CONFIG | MMTYPE_CNF)) > 0)
{
if (confirm->RESULTCODE)
{
Failure (plc, PLC_WONTDOIT);
continue;
}
hexview (confirm->DATA, LE32TOH (confirm->DATA_OFFSET), LE32TOH (confirm->DATA_LENGTH), stdout);
}
return (0);
}
/*====================================================================*
*
* signed AddVLANIDs (struct plc * plc, struct item list [], unsigned items);
*
*
*--------------------------------------------------------------------*/
static signed AddVLANIDs (struct plc * plc, struct item list [], unsigned items)
{
struct channel * channel = (struct channel *)(plc->channel);
struct message * message = (struct message *)(plc->message);
#ifndef __GNUC__
#pragma pack (push,1)
#endif
struct __packed vs_forward_config_request
{
struct ethernet_hdr ethernet;
struct qualcomm_hdr qualcomm;
uint8_t RESERVED1;
uint8_t MREQUEST;
uint8_t MVERSION;
uint32_t RESERVED2;
uint16_t ITEMS;
struct item LIST [1];
}
* request = (struct vs_forward_config_request *) (message);
struct __packed vs_forward_config_confirm
{
struct ethernet_hdr ethernet;
struct qualcomm_hdr qualcomm;
uint8_t RESERVED1;
uint8_t RESULTCODE;
uint8_t OPERATION;
uint8_t MVERSION;
uint32_t RESERVED2;
}
* confirm = (struct vs_forward_config_confirm *) (message);
#ifndef __GNUC__
#pragma pack (pop)
#endif
struct item * item = request->LIST;
memset (message, 0, sizeof (* message));
EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
QualcommHeader (&request->qualcomm, 0, (VS_FORWARD_CONFIG | MMTYPE_REQ));
request->MREQUEST = PLCFWD_ADD;
request->MVERSION = PLCFWD_VER;
request->ITEMS = HTOLE16 (items);
while (items--)
{
unsigned count;
memcpy (item->MAC_ADDR, list->MAC_ADDR, sizeof (item->MAC_ADDR));
item->NUM_VLANIDS = HTOLE16 (list->NUM_VLANIDS);
for (count = 0; count < list->NUM_VLANIDS; count++)
{
item->VLANID [count] = HTOLE16 (list->VLANID [count]);
}
// item++;
item = (struct item *)(&item->VLANID [count]);
list++;
}
plc->packetsize = (signed)((uint8_t *)(item) - (uint8_t *)(request));
if (SendMME (plc) <= 0)
{
error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
return (-1);
}
while (ReadMME (plc, 0, (VS_FORWARD_CONFIG | MMTYPE_CNF)) > 0)
{
if (confirm->RESULTCODE)
{
Failure (plc, PLC_WONTDOIT);
continue;
}
}
return (0);
}
/*====================================================================*
*
* signed RemoveVLANIDs (struct plc * plc, struct item list [], unsigned items);
*
*
*--------------------------------------------------------------------*/
static signed RemoveVLANIDs (struct plc * plc, struct item list [], unsigned items)
{
struct channel * channel = (struct channel *)(plc->channel);
struct message * message = (struct message *)(plc->message);
#ifndef __GNUC__
#pragma pack (push,1)
#endif
struct __packed vs_forward_config_request
{
struct ethernet_hdr ethernet;
struct qualcomm_hdr qualcomm;
uint8_t RESERVED1;
uint8_t MREQUEST;
uint8_t MVERSION;
uint32_t RESERVED2;
uint16_t ITEMS;
struct item LIST [1];
}
* request = (struct vs_forward_config_request *) (message);
struct __packed vs_forward_config_confirm
{
struct ethernet_hdr ethernet;
struct qualcomm_hdr qualcomm;
uint8_t RESERVED1;
uint8_t RESULTCODE;
uint8_t OPERATION;
uint8_t MVERSION;
uint32_t RESERVED2;
}
* confirm = (struct vs_forward_config_confirm *) (message);
#ifndef __GNUC__
#pragma pack (pop)
#endif
struct item * item = request->LIST;
memset (message, 0, sizeof (* message));
EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
QualcommHeader (&request->qualcomm, 0, (VS_FORWARD_CONFIG | MMTYPE_REQ));
request->MREQUEST = PLCFWD_REM;
request->MVERSION = PLCFWD_VER;
request->ITEMS = HTOLE16 (items);
while (items--)
{
unsigned count;
memcpy (item->MAC_ADDR, list->MAC_ADDR, sizeof (item->MAC_ADDR));
item->NUM_VLANIDS = HTOLE16 (list->NUM_VLANIDS);
for (count = 0; count < list->NUM_VLANIDS; count++)
{
item->VLANID [count] = HTOLE16 (list->VLANID [count]);
}
// item++;
item = (struct item *)(&item->VLANID [count]);
list++;
}
plc->packetsize = (signed)((uint8_t *)(item) - (uint8_t *)(request));
if (SendMME (plc) <= 0)
{
error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
return (-1);
}
while (ReadMME (plc, 0, (VS_FORWARD_CONFIG | MMTYPE_CNF)) > 0)
{
if (confirm->RESULTCODE)
{
Failure (plc, PLC_WONTDOIT);
continue;
}
}
return (0);
}
/*====================================================================*
*
* signed CommitVLANIDs (struct plc * plc);
*
*
*--------------------------------------------------------------------*/
static signed CommitVLANIDs (struct plc * plc)
{
struct channel * channel = (struct channel *)(plc->channel);
struct message * message = (struct message *)(plc->message);
#ifndef __GNUC__
#pragma pack (push,1)
#endif
struct __packed vs_forward_config_request
{
struct ethernet_hdr ethernet;
struct qualcomm_hdr qualcomm;
uint8_t RESERVED1;
uint8_t MREQUEST;
uint8_t MVERSION;
uint32_t RESERVED2;
}
* request = (struct vs_forward_config_request *) (message);
struct __packed vs_forward_config_confirm
{
struct ethernet_hdr ethernet;
struct qualcomm_hdr qualcomm;
uint8_t RESERVED1;
uint8_t RESULTCODE;
uint8_t OPERATION;
uint8_t MVERSION;
uint32_t RESERVED2;
}
* confirm = (struct vs_forward_config_confirm *) (message);
#ifndef __GNUC__
#pragma pack (pop)
#endif
memset (message, 0, sizeof (* message));
EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
QualcommHeader (&request->qualcomm, 0, (VS_FORWARD_CONFIG | MMTYPE_REQ));
request->MREQUEST = PLCFWD_STO;
request->MVERSION = PLCFWD_VER;
plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
if (SendMME (plc) <= 0)
{
error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
return (-1);
}
while (ReadMME (plc, 0, (VS_FORWARD_CONFIG | MMTYPE_CNF)) > 0)
{
if (confirm->RESULTCODE)
{
Failure (plc, PLC_WONTDOIT);
continue;
}
}
return (0);
}
/*====================================================================*
*
* signed ControlVLANIDs (struct plc * plc);
*
*
*--------------------------------------------------------------------*/
static signed ControlVLANIDs (struct plc * plc)
{
struct channel * channel = (struct channel *)(plc->channel);
struct message * message = (struct message *)(plc->message);
#ifndef __GNUC__
#pragma pack (push,1)
#endif
struct __packed vs_forward_config_request
{
struct ethernet_hdr ethernet;
struct qualcomm_hdr qualcomm;
uint8_t RESERVED1;
uint8_t MREQUEST;
uint8_t MVERSION;
uint32_t RESERVED2;
uint8_t ENABLE;
uint8_t UPSTREAMCHECK;
uint8_t RESERVED3;
}
* request = (struct vs_forward_config_request *) (message);
struct __packed vs_forward_config_confirm
{
struct ethernet_hdr ethernet;
struct qualcomm_hdr qualcomm;
uint8_t RESERVED1;
uint8_t RESULTCODE;
uint8_t OPERATION;
uint8_t MVERSION;
uint32_t RESERVED2;
}
* confirm = (struct vs_forward_config_confirm *) (message);
#ifndef __GNUC__
#pragma pack (pop)
#endif
memset (message, 0, sizeof (* message));
EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
QualcommHeader (&request->qualcomm, 0, (VS_FORWARD_CONFIG | MMTYPE_REQ));
request->MREQUEST = PLCFWD_CTL;
request->MVERSION = PLCFWD_VER;
request->ENABLE = plc->module;
request->UPSTREAMCHECK = plc->pushbutton;
plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
if (SendMME (plc) <= 0)
{
error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
return (-1);
}
while (ReadMME (plc, 0, (VS_FORWARD_CONFIG | MMTYPE_CNF)) > 0)
{
if (confirm->RESULTCODE)
{
Failure (plc, PLC_WONTDOIT);
continue;
}
}
return (0);
}
/*====================================================================*
*
* signed DefaultVLANIDs (struct plc * plc);
*
*
*--------------------------------------------------------------------*/
static signed DefaultVLANIDs (struct plc * plc, struct item list [], unsigned items)
{
struct channel * channel = (struct channel *)(plc->channel);
struct message * message = (struct message *)(plc->message);
#ifndef __GNUC__
#pragma pack (push,1)
#endif
struct __packed vs_forward_config_request
{
struct ethernet_hdr ethernet;
struct qualcomm_hdr qualcomm;
uint8_t RESERVED1;
uint8_t MREQUEST;
uint8_t MVERSION;
uint32_t RESERVED2;
uint16_t VLANID;
uint16_t RESERVED3;
}
* request = (struct vs_forward_config_request *) (message);
struct __packed vs_forward_config_confirm
{
struct ethernet_hdr ethernet;
struct qualcomm_hdr qualcomm;
uint8_t RESERVED1;
uint8_t RESULTCODE;
uint8_t OPERATION;
uint8_t MVERSION;
uint32_t RESERVED2;
}
* confirm = (struct vs_forward_config_confirm *) (message);
#ifndef __GNUC__
#pragma pack (pop)
#endif
memset (message, 0, sizeof (* message));
EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
QualcommHeader (&request->qualcomm, 0, (VS_FORWARD_CONFIG | MMTYPE_REQ));
request->MREQUEST = PLCFWD_SET;
request->MVERSION = PLCFWD_VER;
request->VLANID = HTOLE16 (list [0].VLANID [0]);
plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
if (SendMME (plc) <= 0)
{
error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
return (-1);
}
while (ReadMME (plc, 0, (VS_FORWARD_CONFIG | MMTYPE_CNF)) > 0)
{
if (confirm->RESULTCODE)
{
Failure (plc, PLC_WONTDOIT);
continue;
}
}
return (0);
}
/*====================================================================*
*
* signed ForwardVLANIDs (struct plc * plc);
*
*
*--------------------------------------------------------------------*/
static signed ForwardVLANIDs (struct plc * plc)
{
struct channel * channel = (struct channel *)(plc->channel);
struct message * message = (struct message *)(plc->message);
#ifndef __GNUC__
#pragma pack (push,1)
#endif
struct __packed vs_forward_config_request
{
struct ethernet_hdr ethernet;
struct qualcomm_hdr qualcomm;
uint8_t RESERVED1;
uint8_t MREQUEST;
uint8_t MVERSION;
uint32_t RESERVED2;
uint8_t ENABLED;
uint16_t VLANID;
uint16_t RESERVED3;
struct item ITEM;
}
* request = (struct vs_forward_config_request *) (message);
struct __packed vs_forward_config_confirm
{
struct ethernet_hdr ethernet;
struct qualcomm_hdr qualcomm;
uint8_t RESERVED1;
uint8_t RESULTCODE;
uint8_t OPERATION;
uint8_t MVERSION;
uint32_t RESERVED2;
}
* confirm = (struct vs_forward_config_confirm *) (message);
#ifndef __GNUC__
#pragma pack (pop)
#endif
memset (message, 0, sizeof (* message));
EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
QualcommHeader (&request->qualcomm, 0, (VS_FORWARD_CONFIG | MMTYPE_REQ));
request->MREQUEST = PLCFWD_FWD;
request->MVERSION = PLCFWD_VER;
plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
if (SendMME (plc) <= 0)
{
error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
return (-1);
}
while (ReadMME (plc, 0, (VS_FORWARD_CONFIG | MMTYPE_CNF)) > 0)
{
if (confirm->RESULTCODE)
{
Failure (plc, PLC_WONTDOIT);
continue;
}
}
return (0);
}
/*====================================================================*
*
* void function (struct plc * plc, struct item list [], unsigned items);
*
* perform the VLANID action specified by the action member
* in struct plc as set in the main program; only one action
* is performed;
*
*
*--------------------------------------------------------------------*/
static void function (struct plc * plc, uint32_t offset, uint32_t length, struct item * list, unsigned items)
{
if (plc->action == PLCFWD_GET)
{
ReadVLANIDs (plc, offset, length);
return;
}
if (plc->action == PLCFWD_ADD)
{
AddVLANIDs (plc, list, items);
return;
}
if (plc->action == PLCFWD_REM)
{
RemoveVLANIDs (plc, list, items);
return;
}
if (plc->action == PLCFWD_STO)
{
CommitVLANIDs (plc);
return;
}
if (plc->action == PLCFWD_CTL)
{
ControlVLANIDs (plc);
return;
}
if (plc->action == PLCFWD_SET)
{
DefaultVLANIDs (plc, list, items);
return;
}
if (plc->action == PLCFWD_FWD)
{
ForwardVLANIDs (plc);
return;
}
return;
}
/*====================================================================*
*
* int main (int argc, char const * argv[]);
*
*
*--------------------------------------------------------------------*/
int main (int argc, char const * argv [])
{
extern struct channel channel;
static char const * optv [] =
{
"ACD:ef:i:l:M:o:qRS:t:vxz:",
"device [device] [...] [> stdout]",
"Qualcomm Atheros VLANID Forward Configuration Manager",
"A\tadd VLAN ID of multiple slaves to memory",
"C\tcommit configuration to flash memory",
"D x\tset default VLAN ID",
"e\tredirect stderr to stdout",
"f s\tread VLANIDS from file (s)",
#if defined (WINPCAP) || defined (LIBPCAP)
"i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
#else
"i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
#endif
"l n\tdata length in bytes [" LITERAL (PLCFWD_LENGTH) "]",
"M n\tenable VLANID forwarding on the master",
"o x\tdata offset in bytes [" LITERAL (PLCFWD_OFFSET) "]",
"q\tquiet mode",
"R\tremove VLAN ID of multiple slaves from memory",
"S n\tenable VLANID forwarding on all slaves",
"t n\ttimeout is (n) millisecond [" LITERAL (CHANNEL_TIMEOUT) "]",
"v\tverbose mode",
"x\texit on error",
"z s\tslavespec",
(char const *) (0)
};
#include "../plc/plc.c"
struct item list [128];
unsigned size = sizeof (list) / sizeof (struct item);
unsigned items = 0;
uint32_t offset = 0;
uint32_t length = 0;
signed c;
initchannel (&channel);
desuid ();
memset (&list, 0, sizeof (list));
if (getenv (PLCDEVICE))
{
channel.ifname = strdup (getenv (PLCDEVICE));
}
optind = 1;
while ((c = getoptv (argc, argv, optv)) != -1)
{
switch (c)
{
case 'A':
plc.action = PLCFWD_ADD;
break;
case 'C':
plc.action = PLCFWD_STO;
break;
case 'D':
plc.action = PLCFWD_SET;
list [0].VLANID [0] = (uint16_t)(basespec (optarg, 10, sizeof (uint16_t)));
break;
case 'e':
dup2 (STDOUT_FILENO, STDERR_FILENO);
break;
case 'f':
if (!freopen (optarg, "rb", stdin))
{
error (1, errno, "%s", optarg);
}
items += readlist (&list [items], size - items);
break;
case 'i':
#if defined (WINPCAP) || defined (LIBPCAP)
channel.ifindex = atoi (optarg);
#else
channel.ifname = optarg;
#endif
break;
case 'M':
plc.action = PLCFWD_CTL;
plc.module = (uint8_t)(uintspec (synonym (optarg, states, STATES), 0, UCHAR_MAX));
break;
case 'l':
length = (uint32_t) (basespec (optarg, 10, sizeof (length)));
break;
case 'o':
offset = (uint32_t) (basespec (optarg, 10, sizeof (offset)));
break;
case 'q':
_setbits (channel.flags, CHANNEL_SILENCE);
_setbits (plc.flags, PLC_SILENCE);
break;
case 'R':
plc.action = PLCFWD_REM;
break;
case 'S':
plc.action = PLCFWD_CTL;
plc.pushbutton = (uint8_t)(uintspec (synonym (optarg, states, STATES), 0, UCHAR_MAX));
break;
case 't':
channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
break;
case 'v':
_setbits (channel.flags, CHANNEL_VERBOSE);
_setbits (plc.flags, PLC_VERBOSE);
break;
case 'x':
_setbits (plc.flags, PLC_BAILOUT);
break;
case 'z':
readitem (&list [items++], optarg);
break;
default:
break;
}
}
argc -= optind;
argv += optind;
#if 0
showlist (list, items);
#endif
openchannel (&channel);
if (!(plc.message = malloc (sizeof (* plc.message))))
{
error (1, errno, PLC_NOMEMORY);
}
if (!argc)
{
function (&plc, offset, length, list, items);
}
while ((argc) && (* argv))
{
if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
{
error (1, errno, PLC_BAD_MAC, * argv);
}
function (&plc, offset, length, list, items);
argv++;
argc--;
}
free (plc.message);
closechannel (&channel);
return (0);
}
|