├── LICENSE ├── Makefile ├── README.md ├── build.sh ├── cli.c ├── debian ├── changelog ├── compat ├── control ├── copyright ├── rules └── source │ └── format ├── swlib.c └── swlib.h /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Library General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License 307 | along with this program; if not, write to the Free Software 308 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 309 | 310 | 311 | Also add information on how to contact you by electronic and paper mail. 312 | 313 | If the program is interactive, make it output a short notice like this 314 | when it starts in an interactive mode: 315 | 316 | Gnomovision version 69, Copyright (C) year name of author 317 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 318 | This is free software, and you are welcome to redistribute it 319 | under certain conditions; type `show c' for details. 320 | 321 | The hypothetical commands `show w' and `show c' should show the appropriate 322 | parts of the General Public License. Of course, the commands you use may 323 | be called something other than `show w' and `show c'; they could even be 324 | mouse-clicks or menu items--whatever suits your program. 325 | 326 | You should also get your employer (if you work as a programmer) or your 327 | school, if any, to sign a "copyright disclaimer" for the program, if 328 | necessary. Here is a sample; alter the names: 329 | 330 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 331 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 332 | 333 | , 1 April 1989 334 | Ty Coon, President of Vice 335 | 336 | This General Public License does not permit incorporating your program into 337 | proprietary programs. If your program is a subroutine library, you may 338 | consider it more useful to permit linking proprietary applications with the 339 | library. If this is what you want to do, use the GNU Library General 340 | Public License instead of this License. 341 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ifndef CFLAGS 2 | CFLAGS = -O2 -g -I /usr/include/libnl3/ 3 | endif 4 | LIBS=-lnl-3 -lnl-genl-3 5 | all: swconfig 6 | 7 | %.o: %.c 8 | $(CC) $(CFLAGS) -c -o $@ $^ 9 | 10 | swconfig: cli.o swlib.o 11 | $(CC) $(LDFLAGS) -o $@ $^ $(LIBS) 12 | 13 | clean: 14 | rm -f *~ *.o swconfig 15 | 16 | install: 17 | install -D swconfig $(DESTDIR)$(PREFIX)/sbin/swconfig 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | swconfig binary packaged for Debian and derivatives 2 | --------------------------------------------------- 3 | 4 | This repo contains the bare minimum needed to build 5 | the swconfig binary. All code is taken from OpenWRT 6 | project so their copyrights apply. 7 | 8 | BUILD: 9 | ------ 10 | 11 | Install build dependencies 12 | 13 | apt-get install git build-essential fakeroot devscripts debhelper libnl-3-dev libnl-genl-3-dev 14 | 15 | As this utility talks to the switch driver in the kernel, kernel headers need to be in 16 | place and "switch.h" must be present otherwise the build will fail. 17 | 18 | If this file is missing, copy it from here and save to /usr/include/linux/ on the build machine: 19 | https://github.com/openwrt-mirror/openwrt/blob/master/target/linux/generic/files/include/uapi/linux/switch.h 20 | 21 | After all the above is done, clone the repo: 22 | 23 | git clone https://github.com/jekader/swconfig.git 24 | cd swconfig 25 | bash build.sh 26 | dpkg -i ../swconfig_15.04-1_armhf.deb 27 | 28 | USE: 29 | ---- 30 | 31 | Please read the upstream documentation on how to use this utility: 32 | http://wiki.openwrt.org/doc/techref/swconfig 33 | 34 | AUTHORS: 35 | -------- 36 | 37 | The code is owned by OpenWRT Project and taken from here: 38 | https://github.com/openwrt-mirror/openwrt/tree/master/package/network/config/swconfig/src 39 | 40 | Main difference from upstream: removed dependency of UCI and libnl-3 user instead of libnl-tiny 41 | -------------------------------------------------------------------------------- /build.sh: -------------------------------------------------------------------------------- 1 | echo "will build swconfig now" 2 | if [ ! -f /usr/include/linux/switch.h ]; then 3 | echo "switch.h not found in kernel headers! Aborting" 4 | exit 1 5 | fi 6 | make clean && cd .. && tar -cvzf swconfig_15.04.orig.tar.gz swconfig && cd swconfig 7 | debuild -us -uc -------------------------------------------------------------------------------- /cli.c: -------------------------------------------------------------------------------- 1 | /* 2 | * swconfig.c: Switch configuration utility 3 | * 4 | * Copyright (C) 2008 Felix Fietkau 5 | * Copyright (C) 2010 Martin Mares 6 | * 7 | * This program is free software; you can redistribute it and/or 8 | * modify it under the terms of the GNU General Public License 9 | * version 2 as published by the Free Software Foundatio. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | */ 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | //#include 27 | #include 28 | 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include "swlib.h" 37 | 38 | enum { 39 | CMD_NONE, 40 | CMD_GET, 41 | CMD_SET, 42 | CMD_LOAD, 43 | CMD_HELP, 44 | CMD_SHOW, 45 | CMD_PORTMAP, 46 | }; 47 | 48 | static void 49 | print_attrs(const struct switch_attr *attr) 50 | { 51 | int i = 0; 52 | while (attr) { 53 | const char *type; 54 | switch(attr->type) { 55 | case SWITCH_TYPE_INT: 56 | type = "int"; 57 | break; 58 | case SWITCH_TYPE_STRING: 59 | type = "string"; 60 | break; 61 | case SWITCH_TYPE_PORTS: 62 | type = "ports"; 63 | break; 64 | case SWITCH_TYPE_NOVAL: 65 | type = "none"; 66 | break; 67 | default: 68 | type = "unknown"; 69 | break; 70 | } 71 | printf("\tAttribute %d (%s): %s (%s)\n", ++i, type, attr->name, attr->description); 72 | attr = attr->next; 73 | } 74 | } 75 | 76 | static void 77 | list_attributes(struct switch_dev *dev) 78 | { 79 | printf("%s: %s(%s), ports: %d (cpu @ %d), vlans: %d\n", dev->dev_name, dev->alias, dev->name, dev->ports, dev->cpu_port, dev->vlans); 80 | printf(" --switch\n"); 81 | print_attrs(dev->ops); 82 | printf(" --vlan\n"); 83 | print_attrs(dev->vlan_ops); 84 | printf(" --port\n"); 85 | print_attrs(dev->port_ops); 86 | } 87 | 88 | static void 89 | print_attr_val(const struct switch_attr *attr, const struct switch_val *val) 90 | { 91 | int i; 92 | 93 | switch (attr->type) { 94 | case SWITCH_TYPE_INT: 95 | printf("%d", val->value.i); 96 | break; 97 | case SWITCH_TYPE_STRING: 98 | printf("%s", val->value.s); 99 | break; 100 | case SWITCH_TYPE_PORTS: 101 | for(i = 0; i < val->len; i++) { 102 | printf("%d%s ", 103 | val->value.ports[i].id, 104 | (val->value.ports[i].flags & 105 | SWLIB_PORT_FLAG_TAGGED) ? "t" : ""); 106 | } 107 | break; 108 | default: 109 | printf("?unknown-type?"); 110 | } 111 | } 112 | 113 | static void 114 | show_attrs(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val) 115 | { 116 | while (attr) { 117 | if (attr->type != SWITCH_TYPE_NOVAL) { 118 | printf("\t%s: ", attr->name); 119 | if (swlib_get_attr(dev, attr, val) < 0) 120 | printf("???"); 121 | else 122 | print_attr_val(attr, val); 123 | putchar('\n'); 124 | } 125 | attr = attr->next; 126 | } 127 | } 128 | 129 | static void 130 | show_global(struct switch_dev *dev) 131 | { 132 | struct switch_val val; 133 | 134 | printf("Global attributes:\n"); 135 | show_attrs(dev, dev->ops, &val); 136 | } 137 | 138 | static void 139 | show_port(struct switch_dev *dev, int port) 140 | { 141 | struct switch_val val; 142 | 143 | printf("Port %d:\n", port); 144 | val.port_vlan = port; 145 | show_attrs(dev, dev->port_ops, &val); 146 | } 147 | 148 | static void 149 | show_vlan(struct switch_dev *dev, int vlan, bool all) 150 | { 151 | struct switch_val val; 152 | struct switch_attr *attr; 153 | 154 | val.port_vlan = vlan; 155 | 156 | if (all) { 157 | attr = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_VLAN, "ports"); 158 | if (swlib_get_attr(dev, attr, &val) < 0) 159 | return; 160 | 161 | if (!val.len) 162 | return; 163 | } 164 | 165 | printf("VLAN %d:\n", vlan); 166 | show_attrs(dev, dev->vlan_ops, &val); 167 | } 168 | 169 | static void 170 | print_usage(void) 171 | { 172 | printf("swconfig list\n"); 173 | printf("swconfig dev [port |vlan ] (help|set |get |load |show)\n"); 174 | exit(1); 175 | } 176 | 177 | /*static void 178 | swconfig_load_uci(struct switch_dev *dev, const char *name) 179 | { 180 | struct uci_context *ctx; 181 | struct uci_package *p = NULL; 182 | int ret = -1; 183 | 184 | ctx = uci_alloc_context(); 185 | if (!ctx) 186 | return; 187 | 188 | uci_load(ctx, name, &p); 189 | if (!p) { 190 | uci_perror(ctx, "Failed to load config file: "); 191 | goto out; 192 | } 193 | 194 | ret = swlib_apply_from_uci(dev, p); 195 | if (ret < 0) 196 | fprintf(stderr, "Failed to apply configuration for switch '%s'\n", dev->dev_name); 197 | 198 | out: 199 | uci_free_context(ctx); 200 | exit(ret); 201 | } 202 | */ 203 | 204 | int main(int argc, char **argv) 205 | { 206 | int retval = 0; 207 | struct switch_dev *dev; 208 | struct switch_attr *a; 209 | struct switch_val val; 210 | int i; 211 | 212 | int cmd = CMD_NONE; 213 | char *cdev = NULL; 214 | int cport = -1; 215 | int cvlan = -1; 216 | char *ckey = NULL; 217 | char *cvalue = NULL; 218 | char *csegment = NULL; 219 | 220 | if((argc == 2) && !strcmp(argv[1], "list")) { 221 | swlib_list(); 222 | return 0; 223 | } 224 | 225 | if(argc < 4) 226 | print_usage(); 227 | 228 | if(strcmp(argv[1], "dev")) 229 | print_usage(); 230 | 231 | cdev = argv[2]; 232 | 233 | for(i = 3; i < argc; i++) 234 | { 235 | char *arg = argv[i]; 236 | if (cmd != CMD_NONE) { 237 | print_usage(); 238 | } else if (!strcmp(arg, "port") && i+1 < argc) { 239 | cport = atoi(argv[++i]); 240 | } else if (!strcmp(arg, "vlan") && i+1 < argc) { 241 | cvlan = atoi(argv[++i]); 242 | } else if (!strcmp(arg, "help")) { 243 | cmd = CMD_HELP; 244 | } else if (!strcmp(arg, "set") && i+1 < argc) { 245 | cmd = CMD_SET; 246 | ckey = argv[++i]; 247 | if (i+1 < argc) 248 | cvalue = argv[++i]; 249 | } else if (!strcmp(arg, "get") && i+1 < argc) { 250 | cmd = CMD_GET; 251 | ckey = argv[++i]; 252 | } else if (!strcmp(arg, "load") && i+1 < argc) { 253 | if ((cport >= 0) || (cvlan >= 0)) 254 | print_usage(); 255 | cmd = CMD_LOAD; 256 | ckey = argv[++i]; 257 | } else if (!strcmp(arg, "portmap")) { 258 | if (i + 1 < argc) 259 | csegment = argv[++i]; 260 | cmd = CMD_PORTMAP; 261 | } else if (!strcmp(arg, "show")) { 262 | cmd = CMD_SHOW; 263 | } else { 264 | print_usage(); 265 | } 266 | } 267 | 268 | if (cmd == CMD_NONE) 269 | print_usage(); 270 | if (cport > -1 && cvlan > -1) 271 | print_usage(); 272 | 273 | dev = swlib_connect(cdev); 274 | if (!dev) { 275 | fprintf(stderr, "Failed to connect to the switch. Use the \"list\" command to see which switches are available.\n"); 276 | return 1; 277 | } 278 | 279 | swlib_scan(dev); 280 | 281 | if (cmd == CMD_GET || cmd == CMD_SET) { 282 | if(cport > -1) 283 | a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_PORT, ckey); 284 | else if(cvlan > -1) 285 | a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_VLAN, ckey); 286 | else 287 | a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_GLOBAL, ckey); 288 | 289 | if(!a) 290 | { 291 | fprintf(stderr, "Unknown attribute \"%s\"\n", ckey); 292 | retval = -1; 293 | goto out; 294 | } 295 | } 296 | 297 | switch(cmd) 298 | { 299 | case CMD_SET: 300 | if ((a->type != SWITCH_TYPE_NOVAL) && 301 | (cvalue == NULL)) 302 | print_usage(); 303 | 304 | if(cvlan > -1) 305 | cport = cvlan; 306 | 307 | if(swlib_set_attr_string(dev, a, cport, cvalue) < 0) 308 | { 309 | fprintf(stderr, "failed\n"); 310 | retval = -1; 311 | goto out; 312 | } 313 | break; 314 | case CMD_GET: 315 | if(cvlan > -1) 316 | val.port_vlan = cvlan; 317 | if(cport > -1) 318 | val.port_vlan = cport; 319 | if(swlib_get_attr(dev, a, &val) < 0) 320 | { 321 | fprintf(stderr, "failed\n"); 322 | retval = -1; 323 | goto out; 324 | } 325 | print_attr_val(a, &val); 326 | putchar('\n'); 327 | break; 328 | /* case CMD_LOAD: 329 | swconfig_load_uci(dev, ckey); 330 | break; */ 331 | case CMD_HELP: 332 | list_attributes(dev); 333 | break; 334 | case CMD_PORTMAP: 335 | swlib_print_portmap(dev, csegment); 336 | break; 337 | case CMD_SHOW: 338 | if (cport >= 0 || cvlan >= 0) { 339 | if (cport >= 0) 340 | show_port(dev, cport); 341 | else 342 | show_vlan(dev, cvlan, false); 343 | } else { 344 | show_global(dev); 345 | for (i=0; i < dev->ports; i++) 346 | show_port(dev, i); 347 | for (i=0; i < dev->vlans; i++) 348 | show_vlan(dev, i, true); 349 | } 350 | break; 351 | } 352 | 353 | out: 354 | swlib_free_all(dev); 355 | return retval; 356 | } 357 | -------------------------------------------------------------------------------- /debian/changelog: -------------------------------------------------------------------------------- 1 | swconfig (15.04-1) UNRELEASED; urgency=medium 2 | 3 | * Initial release. 4 | 5 | -- Jeka Der Sat, 04 Apr 2015 20:49:55 +0100 6 | -------------------------------------------------------------------------------- /debian/compat: -------------------------------------------------------------------------------- 1 | 9 -------------------------------------------------------------------------------- /debian/control: -------------------------------------------------------------------------------- 1 | Source: swconfig 2 | Maintainer: Evgheni Dereveanchin 3 | Section: network 4 | Priority: optional 5 | Standards-Version: 3.9.6 6 | Build-Depends: debhelper (>= 9), libnl-3-dev, libnl-genl-3-dev 7 | 8 | Package: swconfig 9 | Architecture: any 10 | Depends: ${shlibs:Depends}, ${misc:Depends} 11 | Description: Utility to configure configurable Ethernet network switches -------------------------------------------------------------------------------- /debian/copyright: -------------------------------------------------------------------------------- 1 | Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ 2 | Upstream-Name: swconfig 3 | Upstream-Contact: Jouni Malinen 4 | Source: http://wiki.openwrt.org/doc/techref/swconfig 5 | 6 | Files: * 7 | Copyright: 2015 OpenWRT Project 8 | License: GPL-2 9 | On Debian systems, the full text of the GNU General Public 10 | License version 2 can be found in the file 11 | `/usr/share/common-licenses/GPL-2'. 12 | -------------------------------------------------------------------------------- /debian/rules: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | export DEB_CFLAGS_MAINT_APPEND = -I /usr/include/libnl3/ 3 | %: 4 | dh $@ 5 | -------------------------------------------------------------------------------- /debian/source/format: -------------------------------------------------------------------------------- 1 | 3.0 (quilt) -------------------------------------------------------------------------------- /swlib.c: -------------------------------------------------------------------------------- 1 | /* 2 | * swlib.c: Switch configuration API (user space part) 3 | * 4 | * Copyright (C) 2008 Felix Fietkau 5 | * 6 | * This program is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public License 8 | * version 2.1 as published by the Free Software Foundation. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | */ 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include "swlib.h" 27 | #include 28 | #include 29 | #include 30 | 31 | //#define DEBUG 1 32 | #ifdef DEBUG 33 | #define DPRINTF(fmt, ...) fprintf(stderr, "%s(%d): " fmt, __func__, __LINE__, ##__VA_ARGS__) 34 | #else 35 | #define DPRINTF(fmt, ...) do {} while (0) 36 | #endif 37 | 38 | static struct nl_sock *handle; 39 | static struct nl_cache *cache; 40 | static struct genl_family *family; 41 | static struct nlattr *tb[SWITCH_ATTR_MAX + 1]; 42 | static int refcount = 0; 43 | 44 | static struct nla_policy port_policy[SWITCH_ATTR_MAX] = { 45 | [SWITCH_PORT_ID] = { .type = NLA_U32 }, 46 | [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG }, 47 | }; 48 | 49 | static struct nla_policy portmap_policy[SWITCH_PORTMAP_MAX] = { 50 | [SWITCH_PORTMAP_SEGMENT] = { .type = NLA_STRING }, 51 | [SWITCH_PORTMAP_VIRT] = { .type = NLA_U32 }, 52 | }; 53 | 54 | static inline void * 55 | swlib_alloc(size_t size) 56 | { 57 | void *ptr; 58 | 59 | ptr = malloc(size); 60 | if (!ptr) 61 | goto done; 62 | memset(ptr, 0, size); 63 | 64 | done: 65 | return ptr; 66 | } 67 | 68 | static int 69 | wait_handler(struct nl_msg *msg, void *arg) 70 | { 71 | int *finished = arg; 72 | 73 | *finished = 1; 74 | return NL_STOP; 75 | } 76 | 77 | /* helper function for performing netlink requests */ 78 | static int 79 | swlib_call(int cmd, int (*call)(struct nl_msg *, void *), 80 | int (*data)(struct nl_msg *, void *), void *arg) 81 | { 82 | struct nl_msg *msg; 83 | struct nl_cb *cb = NULL; 84 | int finished; 85 | int flags = 0; 86 | int err; 87 | 88 | msg = nlmsg_alloc(); 89 | if (!msg) { 90 | fprintf(stderr, "Out of memory!\n"); 91 | exit(1); 92 | } 93 | 94 | if (!data) 95 | flags |= NLM_F_DUMP; 96 | 97 | genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, genl_family_get_id(family), 0, flags, cmd, 0); 98 | if (data) { 99 | if (data(msg, arg) < 0) 100 | goto nla_put_failure; 101 | } 102 | 103 | cb = nl_cb_alloc(NL_CB_CUSTOM); 104 | if (!cb) { 105 | fprintf(stderr, "nl_cb_alloc failed.\n"); 106 | exit(1); 107 | } 108 | 109 | err = nl_send_auto_complete(handle, msg); 110 | if (err < 0) { 111 | fprintf(stderr, "nl_send_auto_complete failed: %d\n", err); 112 | goto out; 113 | } 114 | 115 | finished = 0; 116 | 117 | if (call) 118 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, call, arg); 119 | 120 | if (data) 121 | nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, wait_handler, &finished); 122 | else 123 | nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, wait_handler, &finished); 124 | 125 | err = nl_recvmsgs(handle, cb); 126 | if (err < 0) { 127 | goto out; 128 | } 129 | 130 | if (!finished) 131 | err = nl_wait_for_ack(handle); 132 | 133 | out: 134 | if (cb) 135 | nl_cb_put(cb); 136 | nla_put_failure: 137 | nlmsg_free(msg); 138 | return err; 139 | } 140 | 141 | static int 142 | send_attr(struct nl_msg *msg, void *arg) 143 | { 144 | struct switch_val *val = arg; 145 | struct switch_attr *attr = val->attr; 146 | 147 | NLA_PUT_U32(msg, SWITCH_ATTR_ID, attr->dev->id); 148 | NLA_PUT_U32(msg, SWITCH_ATTR_OP_ID, attr->id); 149 | switch(attr->atype) { 150 | case SWLIB_ATTR_GROUP_PORT: 151 | NLA_PUT_U32(msg, SWITCH_ATTR_OP_PORT, val->port_vlan); 152 | break; 153 | case SWLIB_ATTR_GROUP_VLAN: 154 | NLA_PUT_U32(msg, SWITCH_ATTR_OP_VLAN, val->port_vlan); 155 | break; 156 | default: 157 | break; 158 | } 159 | 160 | return 0; 161 | 162 | nla_put_failure: 163 | return -1; 164 | } 165 | 166 | static int 167 | store_port_val(struct nl_msg *msg, struct nlattr *nla, struct switch_val *val) 168 | { 169 | struct nlattr *p; 170 | int ports = val->attr->dev->ports; 171 | int err = 0; 172 | int remaining; 173 | 174 | if (!val->value.ports) 175 | val->value.ports = malloc(sizeof(struct switch_port) * ports); 176 | 177 | nla_for_each_nested(p, nla, remaining) { 178 | struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1]; 179 | struct switch_port *port; 180 | 181 | if (val->len >= ports) 182 | break; 183 | 184 | err = nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, p, port_policy); 185 | if (err < 0) 186 | goto out; 187 | 188 | if (!tb[SWITCH_PORT_ID]) 189 | continue; 190 | 191 | port = &val->value.ports[val->len]; 192 | port->id = nla_get_u32(tb[SWITCH_PORT_ID]); 193 | port->flags = 0; 194 | if (tb[SWITCH_PORT_FLAG_TAGGED]) 195 | port->flags |= SWLIB_PORT_FLAG_TAGGED; 196 | 197 | val->len++; 198 | } 199 | 200 | out: 201 | return err; 202 | } 203 | 204 | static int 205 | store_val(struct nl_msg *msg, void *arg) 206 | { 207 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 208 | struct switch_val *val = arg; 209 | 210 | if (!val) 211 | goto error; 212 | 213 | if (nla_parse(tb, SWITCH_ATTR_MAX - 1, genlmsg_attrdata(gnlh, 0), 214 | genlmsg_attrlen(gnlh, 0), NULL) < 0) { 215 | goto error; 216 | } 217 | 218 | if (tb[SWITCH_ATTR_OP_VALUE_INT]) 219 | val->value.i = nla_get_u32(tb[SWITCH_ATTR_OP_VALUE_INT]); 220 | else if (tb[SWITCH_ATTR_OP_VALUE_STR]) 221 | val->value.s = strdup(nla_get_string(tb[SWITCH_ATTR_OP_VALUE_STR])); 222 | else if (tb[SWITCH_ATTR_OP_VALUE_PORTS]) 223 | val->err = store_port_val(msg, tb[SWITCH_ATTR_OP_VALUE_PORTS], val); 224 | 225 | val->err = 0; 226 | return 0; 227 | 228 | error: 229 | return NL_SKIP; 230 | } 231 | 232 | int 233 | swlib_get_attr(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val) 234 | { 235 | int cmd; 236 | int err; 237 | 238 | switch(attr->atype) { 239 | case SWLIB_ATTR_GROUP_GLOBAL: 240 | cmd = SWITCH_CMD_GET_GLOBAL; 241 | break; 242 | case SWLIB_ATTR_GROUP_PORT: 243 | cmd = SWITCH_CMD_GET_PORT; 244 | break; 245 | case SWLIB_ATTR_GROUP_VLAN: 246 | cmd = SWITCH_CMD_GET_VLAN; 247 | break; 248 | default: 249 | return -EINVAL; 250 | } 251 | 252 | memset(&val->value, 0, sizeof(val->value)); 253 | val->len = 0; 254 | val->attr = attr; 255 | val->err = -EINVAL; 256 | err = swlib_call(cmd, store_val, send_attr, val); 257 | if (!err) 258 | err = val->err; 259 | 260 | return err; 261 | } 262 | 263 | static int 264 | send_attr_ports(struct nl_msg *msg, struct switch_val *val) 265 | { 266 | struct nlattr *n; 267 | int i; 268 | 269 | /* TODO implement multipart? */ 270 | if (val->len == 0) 271 | goto done; 272 | n = nla_nest_start(msg, SWITCH_ATTR_OP_VALUE_PORTS); 273 | if (!n) 274 | goto nla_put_failure; 275 | for (i = 0; i < val->len; i++) { 276 | struct switch_port *port = &val->value.ports[i]; 277 | struct nlattr *np; 278 | 279 | np = nla_nest_start(msg, SWITCH_ATTR_PORT); 280 | if (!np) 281 | goto nla_put_failure; 282 | 283 | NLA_PUT_U32(msg, SWITCH_PORT_ID, port->id); 284 | if (port->flags & SWLIB_PORT_FLAG_TAGGED) 285 | NLA_PUT_FLAG(msg, SWITCH_PORT_FLAG_TAGGED); 286 | 287 | nla_nest_end(msg, np); 288 | } 289 | nla_nest_end(msg, n); 290 | done: 291 | return 0; 292 | 293 | nla_put_failure: 294 | return -1; 295 | } 296 | 297 | static int 298 | send_attr_val(struct nl_msg *msg, void *arg) 299 | { 300 | struct switch_val *val = arg; 301 | struct switch_attr *attr = val->attr; 302 | 303 | if (send_attr(msg, arg)) 304 | goto nla_put_failure; 305 | 306 | switch(attr->type) { 307 | case SWITCH_TYPE_NOVAL: 308 | break; 309 | case SWITCH_TYPE_INT: 310 | NLA_PUT_U32(msg, SWITCH_ATTR_OP_VALUE_INT, val->value.i); 311 | break; 312 | case SWITCH_TYPE_STRING: 313 | if (!val->value.s) 314 | goto nla_put_failure; 315 | NLA_PUT_STRING(msg, SWITCH_ATTR_OP_VALUE_STR, val->value.s); 316 | break; 317 | case SWITCH_TYPE_PORTS: 318 | if (send_attr_ports(msg, val) < 0) 319 | goto nla_put_failure; 320 | break; 321 | default: 322 | goto nla_put_failure; 323 | } 324 | return 0; 325 | 326 | nla_put_failure: 327 | return -1; 328 | } 329 | 330 | int 331 | swlib_set_attr(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val) 332 | { 333 | int cmd; 334 | 335 | switch(attr->atype) { 336 | case SWLIB_ATTR_GROUP_GLOBAL: 337 | cmd = SWITCH_CMD_SET_GLOBAL; 338 | break; 339 | case SWLIB_ATTR_GROUP_PORT: 340 | cmd = SWITCH_CMD_SET_PORT; 341 | break; 342 | case SWLIB_ATTR_GROUP_VLAN: 343 | cmd = SWITCH_CMD_SET_VLAN; 344 | break; 345 | default: 346 | return -EINVAL; 347 | } 348 | 349 | val->attr = attr; 350 | return swlib_call(cmd, NULL, send_attr_val, val); 351 | } 352 | 353 | int swlib_set_attr_string(struct switch_dev *dev, struct switch_attr *a, int port_vlan, const char *str) 354 | { 355 | struct switch_port *ports; 356 | struct switch_val val; 357 | char *ptr; 358 | 359 | memset(&val, 0, sizeof(val)); 360 | val.port_vlan = port_vlan; 361 | switch(a->type) { 362 | case SWITCH_TYPE_INT: 363 | val.value.i = atoi(str); 364 | break; 365 | case SWITCH_TYPE_STRING: 366 | val.value.s = str; 367 | break; 368 | case SWITCH_TYPE_PORTS: 369 | ports = alloca(sizeof(struct switch_port) * dev->ports); 370 | memset(ports, 0, sizeof(struct switch_port) * dev->ports); 371 | val.len = 0; 372 | ptr = (char *)str; 373 | while(ptr && *ptr) 374 | { 375 | while(*ptr && isspace(*ptr)) 376 | ptr++; 377 | 378 | if (!*ptr) 379 | break; 380 | 381 | if (!isdigit(*ptr)) 382 | return -1; 383 | 384 | if (val.len >= dev->ports) 385 | return -1; 386 | 387 | ports[val.len].flags = 0; 388 | ports[val.len].id = strtoul(ptr, &ptr, 10); 389 | while(*ptr && !isspace(*ptr)) { 390 | if (*ptr == 't') 391 | ports[val.len].flags |= SWLIB_PORT_FLAG_TAGGED; 392 | else 393 | return -1; 394 | 395 | ptr++; 396 | } 397 | if (*ptr) 398 | ptr++; 399 | val.len++; 400 | } 401 | val.value.ports = ports; 402 | break; 403 | case SWITCH_TYPE_NOVAL: 404 | if (str && !strcmp(str, "0")) 405 | return 0; 406 | 407 | break; 408 | default: 409 | return -1; 410 | } 411 | return swlib_set_attr(dev, a, &val); 412 | } 413 | 414 | 415 | struct attrlist_arg { 416 | int id; 417 | int atype; 418 | struct switch_dev *dev; 419 | struct switch_attr *prev; 420 | struct switch_attr **head; 421 | }; 422 | 423 | static int 424 | add_id(struct nl_msg *msg, void *arg) 425 | { 426 | struct attrlist_arg *l = arg; 427 | 428 | NLA_PUT_U32(msg, SWITCH_ATTR_ID, l->id); 429 | 430 | return 0; 431 | nla_put_failure: 432 | return -1; 433 | } 434 | 435 | static int 436 | add_attr(struct nl_msg *msg, void *ptr) 437 | { 438 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 439 | struct attrlist_arg *arg = ptr; 440 | struct switch_attr *new; 441 | 442 | if (nla_parse(tb, SWITCH_ATTR_MAX - 1, genlmsg_attrdata(gnlh, 0), 443 | genlmsg_attrlen(gnlh, 0), NULL) < 0) 444 | goto done; 445 | 446 | new = swlib_alloc(sizeof(struct switch_attr)); 447 | if (!new) 448 | goto done; 449 | 450 | new->dev = arg->dev; 451 | new->atype = arg->atype; 452 | if (arg->prev) { 453 | arg->prev->next = new; 454 | } else { 455 | arg->prev = *arg->head; 456 | } 457 | *arg->head = new; 458 | arg->head = &new->next; 459 | 460 | if (tb[SWITCH_ATTR_OP_ID]) 461 | new->id = nla_get_u32(tb[SWITCH_ATTR_OP_ID]); 462 | if (tb[SWITCH_ATTR_OP_TYPE]) 463 | new->type = nla_get_u32(tb[SWITCH_ATTR_OP_TYPE]); 464 | if (tb[SWITCH_ATTR_OP_NAME]) 465 | new->name = strdup(nla_get_string(tb[SWITCH_ATTR_OP_NAME])); 466 | if (tb[SWITCH_ATTR_OP_DESCRIPTION]) 467 | new->description = strdup(nla_get_string(tb[SWITCH_ATTR_OP_DESCRIPTION])); 468 | 469 | done: 470 | return NL_SKIP; 471 | } 472 | 473 | int 474 | swlib_scan(struct switch_dev *dev) 475 | { 476 | struct attrlist_arg arg; 477 | 478 | if (dev->ops || dev->port_ops || dev->vlan_ops) 479 | return 0; 480 | 481 | arg.atype = SWLIB_ATTR_GROUP_GLOBAL; 482 | arg.dev = dev; 483 | arg.id = dev->id; 484 | arg.prev = NULL; 485 | arg.head = &dev->ops; 486 | swlib_call(SWITCH_CMD_LIST_GLOBAL, add_attr, add_id, &arg); 487 | 488 | arg.atype = SWLIB_ATTR_GROUP_PORT; 489 | arg.prev = NULL; 490 | arg.head = &dev->port_ops; 491 | swlib_call(SWITCH_CMD_LIST_PORT, add_attr, add_id, &arg); 492 | 493 | arg.atype = SWLIB_ATTR_GROUP_VLAN; 494 | arg.prev = NULL; 495 | arg.head = &dev->vlan_ops; 496 | swlib_call(SWITCH_CMD_LIST_VLAN, add_attr, add_id, &arg); 497 | 498 | return 0; 499 | } 500 | 501 | struct switch_attr *swlib_lookup_attr(struct switch_dev *dev, 502 | enum swlib_attr_group atype, const char *name) 503 | { 504 | struct switch_attr *head; 505 | 506 | if (!name || !dev) 507 | return NULL; 508 | 509 | switch(atype) { 510 | case SWLIB_ATTR_GROUP_GLOBAL: 511 | head = dev->ops; 512 | break; 513 | case SWLIB_ATTR_GROUP_PORT: 514 | head = dev->port_ops; 515 | break; 516 | case SWLIB_ATTR_GROUP_VLAN: 517 | head = dev->vlan_ops; 518 | break; 519 | } 520 | while(head) { 521 | if (!strcmp(name, head->name)) 522 | return head; 523 | head = head->next; 524 | } 525 | 526 | return NULL; 527 | } 528 | 529 | static void 530 | swlib_priv_free(void) 531 | { 532 | if (cache) 533 | nl_cache_free(cache); 534 | if (handle) 535 | nl_socket_free(handle); 536 | handle = NULL; 537 | cache = NULL; 538 | } 539 | 540 | static int 541 | swlib_priv_init(void) 542 | { 543 | int ret; 544 | 545 | handle = nl_socket_alloc(); 546 | if (!handle) { 547 | DPRINTF("Failed to create handle\n"); 548 | goto err; 549 | } 550 | 551 | if (genl_connect(handle)) { 552 | DPRINTF("Failed to connect to generic netlink\n"); 553 | goto err; 554 | } 555 | 556 | ret = genl_ctrl_alloc_cache(handle, &cache); 557 | if (ret < 0) { 558 | DPRINTF("Failed to allocate netlink cache\n"); 559 | goto err; 560 | } 561 | 562 | family = genl_ctrl_search_by_name(cache, "switch"); 563 | if (!family) { 564 | DPRINTF("Switch API not present\n"); 565 | goto err; 566 | } 567 | return 0; 568 | 569 | err: 570 | swlib_priv_free(); 571 | return -EINVAL; 572 | } 573 | 574 | struct swlib_scan_arg { 575 | const char *name; 576 | struct switch_dev *head; 577 | struct switch_dev *ptr; 578 | }; 579 | 580 | static int 581 | add_port_map(struct switch_dev *dev, struct nlattr *nla) 582 | { 583 | struct nlattr *p; 584 | int err = 0, idx = 0; 585 | int remaining; 586 | 587 | dev->maps = malloc(sizeof(struct switch_portmap) * dev->ports); 588 | if (!dev->maps) 589 | return -1; 590 | memset(dev->maps, 0, sizeof(struct switch_portmap) * dev->ports); 591 | 592 | nla_for_each_nested(p, nla, remaining) { 593 | struct nlattr *tb[SWITCH_PORTMAP_MAX+1]; 594 | 595 | if (idx >= dev->ports) 596 | continue; 597 | 598 | err = nla_parse_nested(tb, SWITCH_PORTMAP_MAX, p, portmap_policy); 599 | if (err < 0) 600 | continue; 601 | 602 | 603 | if (tb[SWITCH_PORTMAP_SEGMENT] && tb[SWITCH_PORTMAP_VIRT]) { 604 | dev->maps[idx].segment = strdup(nla_get_string(tb[SWITCH_PORTMAP_SEGMENT])); 605 | dev->maps[idx].virt = nla_get_u32(tb[SWITCH_PORTMAP_VIRT]); 606 | } 607 | idx++; 608 | } 609 | 610 | out: 611 | return err; 612 | } 613 | 614 | 615 | static int 616 | add_switch(struct nl_msg *msg, void *arg) 617 | { 618 | struct swlib_scan_arg *sa = arg; 619 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 620 | struct switch_dev *dev; 621 | const char *name; 622 | const char *alias; 623 | 624 | if (nla_parse(tb, SWITCH_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0) 625 | goto done; 626 | 627 | if (!tb[SWITCH_ATTR_DEV_NAME]) 628 | goto done; 629 | 630 | name = nla_get_string(tb[SWITCH_ATTR_DEV_NAME]); 631 | alias = nla_get_string(tb[SWITCH_ATTR_ALIAS]); 632 | 633 | if (sa->name && (strcmp(name, sa->name) != 0) && (strcmp(alias, sa->name) != 0)) 634 | goto done; 635 | 636 | dev = swlib_alloc(sizeof(struct switch_dev)); 637 | if (!dev) 638 | goto done; 639 | 640 | strncpy(dev->dev_name, name, IFNAMSIZ - 1); 641 | dev->alias = strdup(alias); 642 | if (tb[SWITCH_ATTR_ID]) 643 | dev->id = nla_get_u32(tb[SWITCH_ATTR_ID]); 644 | if (tb[SWITCH_ATTR_NAME]) 645 | dev->name = strdup(nla_get_string(tb[SWITCH_ATTR_NAME])); 646 | if (tb[SWITCH_ATTR_PORTS]) 647 | dev->ports = nla_get_u32(tb[SWITCH_ATTR_PORTS]); 648 | if (tb[SWITCH_ATTR_VLANS]) 649 | dev->vlans = nla_get_u32(tb[SWITCH_ATTR_VLANS]); 650 | if (tb[SWITCH_ATTR_CPU_PORT]) 651 | dev->cpu_port = nla_get_u32(tb[SWITCH_ATTR_CPU_PORT]); 652 | if (tb[SWITCH_ATTR_PORTMAP]) 653 | add_port_map(dev, tb[SWITCH_ATTR_PORTMAP]); 654 | 655 | if (!sa->head) { 656 | sa->head = dev; 657 | sa->ptr = dev; 658 | } else { 659 | sa->ptr->next = dev; 660 | sa->ptr = dev; 661 | } 662 | 663 | refcount++; 664 | done: 665 | return NL_SKIP; 666 | } 667 | 668 | static int 669 | list_switch(struct nl_msg *msg, void *arg) 670 | { 671 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); 672 | 673 | if (nla_parse(tb, SWITCH_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL) < 0) 674 | goto done; 675 | 676 | if (!tb[SWITCH_ATTR_DEV_NAME] || !tb[SWITCH_ATTR_NAME]) 677 | goto done; 678 | 679 | printf("Found: %s - %s\n", nla_get_string(tb[SWITCH_ATTR_DEV_NAME]), 680 | nla_get_string(tb[SWITCH_ATTR_ALIAS])); 681 | 682 | done: 683 | return NL_SKIP; 684 | } 685 | 686 | void 687 | swlib_list(void) 688 | { 689 | if (swlib_priv_init() < 0) 690 | return; 691 | swlib_call(SWITCH_CMD_GET_SWITCH, list_switch, NULL, NULL); 692 | swlib_priv_free(); 693 | } 694 | 695 | void 696 | swlib_print_portmap(struct switch_dev *dev, char *segment) 697 | { 698 | int i; 699 | 700 | if (segment) { 701 | if (!strcmp(segment, "cpu")) { 702 | printf("%d ", dev->cpu_port); 703 | } else if (!strcmp(segment, "disabled")) { 704 | for (i = 0; i < dev->ports; i++) 705 | if (!dev->maps[i].segment) 706 | printf("%d ", i); 707 | } else for (i = 0; i < dev->ports; i++) { 708 | if (dev->maps[i].segment && !strcmp(dev->maps[i].segment, segment)) 709 | printf("%d ", i); 710 | } 711 | } else { 712 | printf("%s - %s\n", dev->dev_name, dev->name); 713 | for (i = 0; i < dev->ports; i++) 714 | if (i == dev->cpu_port) 715 | printf("port%d:\tcpu\n", i); 716 | else if (dev->maps[i].segment) 717 | printf("port%d:\t%s.%d\n", i, dev->maps[i].segment, dev->maps[i].virt); 718 | else 719 | printf("port%d:\tdisabled\n", i); 720 | } 721 | } 722 | 723 | struct switch_dev * 724 | swlib_connect(const char *name) 725 | { 726 | struct swlib_scan_arg arg; 727 | 728 | if (!refcount) { 729 | if (swlib_priv_init() < 0) 730 | return NULL; 731 | }; 732 | 733 | arg.head = NULL; 734 | arg.ptr = NULL; 735 | arg.name = name; 736 | swlib_call(SWITCH_CMD_GET_SWITCH, add_switch, NULL, &arg); 737 | 738 | if (!refcount) 739 | swlib_priv_free(); 740 | 741 | return arg.head; 742 | } 743 | 744 | static void 745 | swlib_free_attributes(struct switch_attr **head) 746 | { 747 | struct switch_attr *a = *head; 748 | struct switch_attr *next; 749 | 750 | while (a) { 751 | next = a->next; 752 | free(a); 753 | a = next; 754 | } 755 | *head = NULL; 756 | } 757 | 758 | void 759 | swlib_free(struct switch_dev *dev) 760 | { 761 | swlib_free_attributes(&dev->ops); 762 | swlib_free_attributes(&dev->port_ops); 763 | swlib_free_attributes(&dev->vlan_ops); 764 | free(dev); 765 | 766 | if (--refcount == 0) 767 | swlib_priv_free(); 768 | } 769 | 770 | void 771 | swlib_free_all(struct switch_dev *dev) 772 | { 773 | struct switch_dev *p; 774 | 775 | while (dev) { 776 | p = dev->next; 777 | swlib_free(dev); 778 | dev = p; 779 | } 780 | } 781 | -------------------------------------------------------------------------------- /swlib.h: -------------------------------------------------------------------------------- 1 | /* 2 | * swlib.h: Switch configuration API (user space part) 3 | * 4 | * Copyright (C) 2008-2009 Felix Fietkau 5 | * 6 | * This program is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public License 8 | * version 2.1 as published by the Free Software Foundation. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | 16 | Usage of the library functions: 17 | 18 | The main datastructure for a switch is the struct switch_device 19 | To get started, you first need to use switch_connect() to probe 20 | for switches and allocate an instance of this struct. 21 | 22 | There are two possible usage modes: 23 | dev = switch_connect("eth0"); 24 | - this call will look for a switch registered for the linux device 25 | "eth0" and only allocate a switch_device for this particular switch. 26 | 27 | dev = switch_connect(NULL) 28 | - this will return one switch_device struct for each available 29 | switch. The switch_device structs are chained with by ->next pointer 30 | 31 | Then to query a switch for all available attributes, use: 32 | swlib_scan(dev); 33 | 34 | All allocated datastructures for the switch_device struct can be freed with 35 | swlib_free(dev); 36 | or 37 | swlib_free_all(dev); 38 | 39 | The latter traverses a whole chain of switch_device structs and frees them all 40 | 41 | Switch attributes (struct switch_attr) are divided into three groups: 42 | dev->ops: 43 | - global settings 44 | dev->port_ops: 45 | - per-port settings 46 | dev->vlan_ops: 47 | - per-vlan settings 48 | 49 | switch_lookup_attr() is a small helper function to locate attributes 50 | by name. 51 | 52 | switch_set_attr() and switch_get_attr() can alter or request the values 53 | of attributes. 54 | 55 | Usage of the switch_attr struct: 56 | 57 | ->atype: attribute group, one of: 58 | - SWLIB_ATTR_GROUP_GLOBAL 59 | - SWLIB_ATTR_GROUP_VLAN 60 | - SWLIB_ATTR_GROUP_PORT 61 | 62 | ->id: identifier for the attribute 63 | 64 | ->type: data type, one of: 65 | - SWITCH_TYPE_INT 66 | - SWITCH_TYPE_STRING 67 | - SWITCH_TYPE_PORT 68 | 69 | ->name: short name of the attribute 70 | ->description: longer description 71 | ->next: pointer to the next attribute of the current group 72 | 73 | 74 | Usage of the switch_val struct: 75 | 76 | When setting attributes, following members of the struct switch_val need 77 | to be set up: 78 | 79 | ->len (for attr->type == SWITCH_TYPE_PORT) 80 | ->port_vlan: 81 | - port number (for attr->atype == SWLIB_ATTR_GROUP_PORT), or: 82 | - vlan number (for attr->atype == SWLIB_ATTR_GROUP_VLAN) 83 | ->value.i (for attr->type == SWITCH_TYPE_INT) 84 | ->value.s (for attr->type == SWITCH_TYPE_STRING) 85 | - owned by the caller, not stored in the library internally 86 | ->value.ports (for attr->type == SWITCH_TYPE_PORT) 87 | - must point to an array of at lest val->len * sizeof(struct switch_port) 88 | 89 | When getting string attributes, val->value.s must be freed by the caller 90 | When getting port list attributes, an internal static buffer is used, 91 | which changes from call to call. 92 | 93 | */ 94 | 95 | #ifndef __SWLIB_H 96 | #define __SWLIB_H 97 | 98 | enum swlib_attr_group { 99 | SWLIB_ATTR_GROUP_GLOBAL, 100 | SWLIB_ATTR_GROUP_VLAN, 101 | SWLIB_ATTR_GROUP_PORT, 102 | }; 103 | 104 | enum swlib_port_flags { 105 | SWLIB_PORT_FLAG_TAGGED = (1 << 0), 106 | }; 107 | 108 | 109 | struct switch_dev; 110 | struct switch_attr; 111 | struct switch_port; 112 | struct switch_port_map; 113 | struct switch_val; 114 | struct uci_package; 115 | 116 | struct switch_dev { 117 | int id; 118 | char dev_name[IFNAMSIZ]; 119 | const char *name; 120 | const char *alias; 121 | int ports; 122 | int vlans; 123 | int cpu_port; 124 | struct switch_attr *ops; 125 | struct switch_attr *port_ops; 126 | struct switch_attr *vlan_ops; 127 | struct switch_portmap *maps; 128 | struct switch_dev *next; 129 | void *priv; 130 | }; 131 | 132 | struct switch_val { 133 | struct switch_attr *attr; 134 | int len; 135 | int err; 136 | int port_vlan; 137 | union { 138 | const char *s; 139 | int i; 140 | struct switch_port *ports; 141 | } value; 142 | }; 143 | 144 | struct switch_attr { 145 | struct switch_dev *dev; 146 | int atype; 147 | int id; 148 | int type; 149 | const char *name; 150 | const char *description; 151 | struct switch_attr *next; 152 | }; 153 | 154 | struct switch_port { 155 | unsigned int id; 156 | unsigned int flags; 157 | }; 158 | 159 | struct switch_portmap { 160 | unsigned int virt; 161 | const char *segment; 162 | }; 163 | 164 | /** 165 | * swlib_list: list all switches 166 | */ 167 | void swlib_list(void); 168 | 169 | /** 170 | * swlib_print_portmap: get portmap 171 | * @dev: switch device struct 172 | */ 173 | void swlib_print_portmap(struct switch_dev *dev, char *segment); 174 | 175 | /** 176 | * swlib_connect: connect to the switch through netlink 177 | * @name: name of the ethernet interface, 178 | * 179 | * if name is NULL, it connect and builds a chain of all switches 180 | */ 181 | struct switch_dev *swlib_connect(const char *name); 182 | 183 | /** 184 | * swlib_free: free all dynamically allocated data for the switch connection 185 | * @dev: switch device struct 186 | * 187 | * all members of a switch device chain (generated by swlib_connect(NULL)) 188 | * must be freed individually 189 | */ 190 | void swlib_free(struct switch_dev *dev); 191 | 192 | /** 193 | * swlib_free_all: run swlib_free on all devices in the chain 194 | * @dev: switch device struct 195 | */ 196 | void swlib_free_all(struct switch_dev *dev); 197 | 198 | /** 199 | * swlib_scan: probe the switch driver for available commands/attributes 200 | * @dev: switch device struct 201 | */ 202 | int swlib_scan(struct switch_dev *dev); 203 | 204 | /** 205 | * swlib_lookup_attr: look up a switch attribute 206 | * @dev: switch device struct 207 | * @type: global, port or vlan 208 | * @name: name of the attribute 209 | */ 210 | struct switch_attr *swlib_lookup_attr(struct switch_dev *dev, 211 | enum swlib_attr_group atype, const char *name); 212 | 213 | /** 214 | * swlib_set_attr: set the value for an attribute 215 | * @dev: switch device struct 216 | * @attr: switch attribute struct 217 | * @val: attribute value pointer 218 | * returns 0 on success 219 | */ 220 | int swlib_set_attr(struct switch_dev *dev, struct switch_attr *attr, 221 | struct switch_val *val); 222 | 223 | /** 224 | * swlib_set_attr_string: set the value for an attribute with type conversion 225 | * @dev: switch device struct 226 | * @attr: switch attribute struct 227 | * @port_vlan: port or vlan (if applicable) 228 | * @str: string value 229 | * returns 0 on success 230 | */ 231 | int swlib_set_attr_string(struct switch_dev *dev, struct switch_attr *attr, 232 | int port_vlan, const char *str); 233 | 234 | /** 235 | * swlib_get_attr: get the value for an attribute 236 | * @dev: switch device struct 237 | * @attr: switch attribute struct 238 | * @val: attribute value pointer 239 | * returns 0 on success 240 | * for string attributes, the result string must be freed by the caller 241 | */ 242 | int swlib_get_attr(struct switch_dev *dev, struct switch_attr *attr, 243 | struct switch_val *val); 244 | 245 | /** 246 | * swlib_apply_from_uci: set up the switch from a uci configuration 247 | * @dev: switch device struct 248 | * @p: uci package which contains the desired global config 249 | */ 250 | int swlib_apply_from_uci(struct switch_dev *dev, struct uci_package *p); 251 | 252 | #endif 253 | --------------------------------------------------------------------------------