├── .gitattributes ├── .github └── workflows │ ├── c-cpp.yml │ ├── codacy.yml │ └── codeql.yml ├── .gitignore ├── .travis.yml ├── AUTHORS ├── COPYING ├── ChangeLog ├── Makefile.am ├── NEWS ├── README ├── README.md ├── autoinstall.sh ├── configure.ac ├── debian ├── README ├── README.Debian ├── README.source ├── changelog ├── compat ├── control ├── copyright ├── docs ├── mvrpd-fem.default ├── mvrpd-fem.service ├── rules └── source │ └── format ├── expand_id ├── m4 └── gcc_stack_protect.m4 ├── src ├── bridge.c ├── bridge.h ├── cmdline.c ├── cmdline.h ├── debug.c ├── debug.h ├── ether.c ├── ether.h ├── event.c ├── event.h ├── main.c ├── mvrp.c ├── mvrp.h ├── port.c ├── port.h ├── random.c ├── random.h ├── receive-nflog.c ├── test-vlan0.c ├── timer.c ├── timer.h ├── vlan.c └── vlan.h └── test-and-coverage.sh /.gitattributes: -------------------------------------------------------------------------------- 1 | src/main.c filter=id 2 | -------------------------------------------------------------------------------- /.github/workflows/c-cpp.yml: -------------------------------------------------------------------------------- 1 | name: C/C++ CI 2 | 3 | on: 4 | push: 5 | branches: [ "master" ] 6 | pull_request: 7 | branches: [ "master" ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v3 16 | - name: dependencies 17 | run: sudo apt-get install -y autotools-dev libpq-dev libnet-dev libnl-3-dev libnl-cli-3-dev libnl-genl-3-dev libnl-nf-3-dev libnl-route-3-dev 18 | - name: autoinstall 19 | run: ./autoinstall.sh 20 | - name: test 21 | run: make check 22 | -------------------------------------------------------------------------------- /.github/workflows/codacy.yml: -------------------------------------------------------------------------------- 1 | # This workflow uses actions that are not certified by GitHub. 2 | # They are provided by a third-party and are governed by 3 | # separate terms of service, privacy policy, and support 4 | # documentation. 5 | 6 | # This workflow checks out code, performs a Codacy security scan 7 | # and integrates the results with the 8 | # GitHub Advanced Security code scanning feature. For more information on 9 | # the Codacy security scan action usage and parameters, see 10 | # https://github.com/codacy/codacy-analysis-cli-action. 11 | # For more information on Codacy Analysis CLI in general, see 12 | # https://github.com/codacy/codacy-analysis-cli. 13 | 14 | name: Codacy Security Scan 15 | 16 | on: 17 | push: 18 | branches: [ "master" ] 19 | pull_request: 20 | # The branches below must be a subset of the branches above 21 | branches: [ "master" ] 22 | schedule: 23 | - cron: '22 5 * * 5' 24 | 25 | permissions: 26 | contents: read 27 | 28 | jobs: 29 | codacy-security-scan: 30 | permissions: 31 | contents: read # for actions/checkout to fetch code 32 | security-events: write # for github/codeql-action/upload-sarif to upload SARIF results 33 | actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status 34 | name: Codacy Security Scan 35 | runs-on: ubuntu-latest 36 | steps: 37 | # Checkout the repository to the GitHub Actions runner 38 | - name: Checkout code 39 | uses: actions/checkout@v3 40 | 41 | # Execute Codacy Analysis CLI and generate a SARIF output with the security issues identified during the analysis 42 | - name: Run Codacy Analysis CLI 43 | uses: codacy/codacy-analysis-cli-action@d840f886c4bd4edc059706d09c6a1586111c540b 44 | with: 45 | # Check https://github.com/codacy/codacy-analysis-cli#project-token to get your project token from your Codacy repository 46 | # You can also omit the token and run the tools that support default configurations 47 | project-token: ${{ secrets.CODACY_PROJECT_TOKEN }} 48 | verbose: true 49 | output: results.sarif 50 | format: sarif 51 | # Adjust severity of non-security issues 52 | gh-code-scanning-compat: true 53 | # Force 0 exit code to allow SARIF file generation 54 | # This will handover control about PR rejection to the GitHub side 55 | max-allowed-issues: 2147483647 56 | 57 | # Upload the SARIF file generated in the previous step 58 | - name: Upload SARIF results file 59 | uses: github/codeql-action/upload-sarif@v2 60 | with: 61 | sarif_file: results.sarif 62 | -------------------------------------------------------------------------------- /.github/workflows/codeql.yml: -------------------------------------------------------------------------------- 1 | name: "CodeQL" 2 | 3 | on: 4 | push: 5 | branches: [ 'master' ] 6 | pull_request: 7 | # The branches below must be a subset of the branches above 8 | branches: [ 'master' ] 9 | schedule: 10 | - cron: '0 18 * * 2' 11 | 12 | jobs: 13 | analyze: 14 | name: Analyze 15 | runs-on: ubuntu-latest 16 | permissions: 17 | actions: read 18 | contents: read 19 | security-events: write 20 | 21 | strategy: 22 | fail-fast: false 23 | matrix: 24 | language: [ 'cpp' ] 25 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] 26 | # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support 27 | 28 | steps: 29 | - name: Checkout repository 30 | uses: actions/checkout@v3 31 | 32 | - name: Install dependencies 33 | run: sudo apt-get install -y autotools-dev libpq-dev libnet-dev libnl-3-dev libnl-cli-3-dev libnl-genl-3-dev libnl-nf-3-dev libnl-route-3-dev 34 | 35 | # Initializes the CodeQL tools for scanning. 36 | - name: Initialize CodeQL 37 | uses: github/codeql-action/init@v2 38 | with: 39 | languages: ${{ matrix.language }} 40 | # If you wish to specify custom queries, you can do so here or in a config file. 41 | # By default, queries listed here will override any specified in a config file. 42 | # Prefix the list here with "+" to use these queries and those in the config file. 43 | 44 | # Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs 45 | queries: +security-and-quality 46 | 47 | - name: Build 48 | run: ./autoinstall.sh 49 | 50 | - name: Perform CodeQL Analysis 51 | uses: github/codeql-action/analyze@v2 52 | with: 53 | category: "/language:${{matrix.language}}" 54 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.debhelper 3 | debian/mvrpd-fem 4 | debian/files 5 | .deps 6 | .dirstamp 7 | INSTALL 8 | Makefile 9 | Makefile.in 10 | aclocal.m4 11 | compile 12 | config.h 13 | config.h.in 14 | config.h.in~ 15 | config.log 16 | config.status 17 | configure 18 | debian/mvrpd-fem.debhelper.log 19 | debian/mvrpd-fem.substvars 20 | depcomp 21 | mvrpd 22 | install-sh 23 | missing 24 | stamp-h1 25 | autom4te.cache 26 | *~ 27 | coverage.info 28 | *.gcda 29 | *.gcno 30 | test-vlan0 31 | out-test-coverage 32 | debian/autoreconf.after 33 | debian/autoreconf.before 34 | debian/debhelper-build-stamp 35 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: c 2 | compiler: 3 | - gcc 4 | 5 | env: 6 | global: 7 | # The next declaration is the encrypted COVERITY_SCAN_TOKEN, created 8 | # via the "travis encrypt" command using the project repo's public key 9 | - secure: "mO3eRvklhSbvGU/xS+SgeBnBNofU0+iDS/d4sdXlB7JyAMrPo5nd3Ulq/CtYvEUx9m/fpcJmOIozrybkPoYHhYFqswBOe0jOlWJKt6LPw6DRCIfmjIKNCBRWslxe8PEciOj2+yjcV6e4dM0ZMW3PQ7KX/eOWvE0zv1umjGOYeq8X3v11bYAVUDsdn4GzMrgkFvP5T8rcuqsF4Egj72pydjF+JlEkasYrI8toN2R5GkPnCl5A+0rbptEYuONgidBnfg/u1K5EJk/PrUnsb3QFoYAH6DOVeWFmepGLr+cvdeUsIEZzkfusv8cyJViWh18BK+xkYPKeCfxrCJAz24T9vZoNzFYgLNs/nEg0L4CsoZdlPGKDvhwT2+cYFU0Vu+vTlLYfVXXAJyJ7OEvDybAywt6xQm+3gynZ0B3cqUyK8x4/HB5m4mKjChf1OmfWx424+tpOYHfU9SSn1+6s0Ncn7OLF0Iub1qB4Mn2gytpkgtpXwpzZHlUrQKFY5ohsA9NoJ19Oe52KBpkrtcTIud6+Fi9lAcT5cyIaWtbROvemlZU4r6NSxIA2ElLRUeFqRPpEiKV1LanWWjhirEenZbWO+O/5JgbjEGiLYO9JfDaE8lqrMUCYSbNTOeHEs/q6pIlduEjz0mIM8IBuMB0KCYb+1MqvgMCX6I2KfjbhphvMM6A=" 10 | 11 | before_install: 12 | - echo -n | openssl s_client -connect https://scan.coverity.com:443 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' | sudo tee -a /etc/ssl/certs/ca- 13 | 14 | addons: 15 | coverity_scan: 16 | project: 17 | name: "michael-dev/mvrpd" 18 | description: "Linux bridge based MVRP daemon" 19 | notification_email: michael-dev@fami-braun.de 20 | #build_command_prepend: "" 21 | build_command: "./autoinstall.sh" 22 | branch_pattern: coverity_scan 23 | apt: 24 | update: true 25 | packages: 26 | - autotools-dev 27 | - libnl-3-dev 28 | - libnl-cli-3-dev 29 | - libnl-genl-3-dev 30 | - libnl-nf-3-dev 31 | - libnl-route-3-dev 32 | 33 | script: 34 | - ./autoinstall.sh 35 | 36 | dist: bionic 37 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | (C) 2019 Michael Braun 2 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/michael-dev/mvrpd/23ec4d6ff861b032c13e091d72e37235c9ff151e/ChangeLog -------------------------------------------------------------------------------- /Makefile.am: -------------------------------------------------------------------------------- 1 | #include $(top_srcdir)/aminclude_static.am 2 | 3 | sbin_PROGRAMS = mvrpd 4 | check_PROGRAMS = test-vlan0 5 | AUTOMAKE_OPTIONS = subdir-objects 6 | ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4 7 | AM_CFLAGS = $(LIBNL_CFLAGS) $(LIBNLGENL_CFLAGS) $(LIBNLNF_CFLAGS) $(LIBNLROUTE_CFLAGS) -Wall -O2 -g -fPIE -std=gnu99 -Wall -Werror -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 8 | LIBS += $(LIBNL_LIBS) $(LIBNLGENL_LIBS) $(LIBNLNF_LIBS) $(LIBNLROUTE_LIBS) 9 | 10 | mvrpd_SOURCES = src/debug.c src/cmdline.c src/event.c src/main.c src/port.c src/bridge.c src/timer.c src/receive-nflog.c src/ether.c src/mvrp.c src/vlan.c src/random.c 11 | test_vlan0_SOURCES = src/debug.c src/cmdline.c src/vlan.c src/test-vlan0.c 12 | test_vlan0_CFLAGS = -fprofile-arcs -ftest-coverage 13 | test_vlan0_LIBS = -fprofile-arcs -ftest-coverage 14 | 15 | -------------------------------------------------------------------------------- /NEWS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/michael-dev/mvrpd/23ec4d6ff861b032c13e091d72e37235c9ff151e/NEWS -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | See README.md 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Travis CI: 2 | [![Build Status](https://secure.travis-ci.org/michael-dev/mvrpd.png?branch=master)](http://travis-ci.org/michael-dev/mvrpd) 3 | 4 | Coverity Scan: 5 | [![Coverity Scan Build Status](https://scan.coverity.com/projects/19020/badge.svg)](https://scan.coverity.com/projects/19020) 6 | 7 | mvrpd 8 | ===== 9 | 10 | This daemon implements MVRP (supersedes GRVP) for linux bridges. It aims at automatically configuring vlans on links, especially inter-bridge/inter-switch. 11 | 12 | overview 13 | -------- 14 | 15 | naive approach: 16 | * Given a tree of network devices like an ethernet spanning tree. 17 | * The information of any device that is interested in a VLAN is then propagated along all edges. 18 | * Any switch that has two or more ports that are interested in the same VLAN adds the VLAN to these ports. 19 | 20 | mvrp approach: 21 | * The switch records which ports are interested in a VLAN, either because they declared interest for a VLAN using MVRP or because it is configured manually. 22 | * The switch declares interest in a VLAN on each dynamic (MVRP-enabled) port, that indicates whether there are other ports on itself that also want this VLAN by registering for that VLAN. 23 | * If the bridge has two or more ports that are interested in a VLAN, it will activate (aka configure or register) the VLAN on all interested ports. 24 | 25 | mvrpd 26 | ----- 27 | 28 | This daemon scans a set of bridge ports (defaults to all non-dynamic and the bridge itself) for VLANs it is statically configured to be interested it. 29 | Additionally, it listens on all dynamic (MVRP-enabled) ports for other devices or bridges interested in a VLAN. 30 | 31 | If a bridge is interested in a VLAN, it registers for that VLAN on all dynamic ports. If a port is the only one interested in a VLAN, it is not registering for that VLAN (no loopback). 32 | 33 | If a bridge has two or more ports that are interested in a VLAN, it will configure that VLAN on all interested ports. 34 | 35 | Optionally, some VLANs may be skipped. 36 | 37 | This daemon operates on vlan\_filtering enabled linux bridges. 38 | 39 | cmdline 40 | ------- 41 | 42 | * --bridge (exactly once) 43 | * --uplinkif : matches interfaces that are configured using MVRP (maybe repeated, takes precedence over --epif) 44 | * --epif : matches interfaces that are statically configured (maybe repeated, ususally includes the bridge itself) 45 | * --ptpif : matches interfaces. If those are configured by MVRP, it is assumed that at most one MVRP-enabled applicant (peer) is connected to this bridge port (e.g. another MVRP enabled bridge) 46 | * --ignore-vlan : ignore this vlan id 47 | * --restrict-to-ep: only declare (announce) and thus register (configure) VLANs that are already added to the (statically configured) ports 48 | 49 | Where pattern is matched using fnmatch, e.g. eth\* would match eth0, eth1, etc. 50 | 51 | extra cmdline 52 | ------------- 53 | 54 | * --bridge-dump-netlink: dump netlink messages send/received in bridge module 55 | * --debug 56 | * --debug-all 57 | * --debug-bridge 58 | * --debug-ether 59 | * --debug-mvrp 60 | * --debug-nflog 61 | * --debug-port 62 | * --nflog-group: nflog group for MVRP snooping 63 | * --verbose 64 | 65 | test-setup 66 | ---------- 67 | 68 | ``` 69 | ip link add dev mvrp-bridge type bridge 70 | ip link set dev mvrp-bridge type bridge vlan_filtering 1 71 | ip link set dev mvrp-bridge type bridge vlan_default_pvid 0 72 | ip link set dev mvrp-bridge up 73 | bridge vlan add vid 300 dev mvrp-bridge self 74 | 75 | for i in $(seq 0 10); do 76 | ip link add dev mvrp-p$i type veth peer name mvrp-c$i 77 | ip link set dev mvrp-p$i up 78 | ip link set dev mvrp-c$i up 79 | ip link add link mvrp-c$i name mvrp-c$i.100 type vlan id 100 80 | ip link set dev mvrp-c$i.100 type vlan mvrp on 81 | ip link set dev mvrp-c$i.100 up 82 | ip link set dev mvrp-p$i master mvrp-bridge 83 | done 84 | 85 | nft add table bridge nat 86 | nft add chain bridge nat PREROUTING { type filter hook prerouting priority dstnat\; policy accept\; } 87 | nft add rule bridge nat PREROUTING meta ibrname "mvrp-bridge" ether daddr 01:80:c2:00:00:21 log group 3 drop 88 | ``` 89 | 90 | ``` 91 | for i in $(seq 0 10); do 92 | ip link set dev mvrp-p$i down 93 | ip link del dev mvrp-p$i 94 | done 95 | ip link set dev mvrp-bridge down 96 | ip link del dev mvrp-bridge 97 | ``` 98 | 99 | -------------------------------------------------------------------------------- /autoinstall.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash -e 2 | 3 | if [ "$1" = "--enable-sanitizer" ]; then 4 | export CFLAGS 5 | export LDFLAGS 6 | 7 | export PATH=/usr/lib/ccache:$PATH 8 | 9 | CFLAGS="$CFLAGS -fsanitize=address -O1 -fno-omit-frame-pointer -g" 10 | LDFLAGS="$LDFLAGS -fsanitize=address -fno-omit-frame-pointer -g" 11 | 12 | CFLAGS="$CFLAGS -Wno-format-nonliteral" 13 | CFLAGS="$CFLAGS -fsanitize=undefined" 14 | ##CFLAGS="$CFLAGS -fno-sanitize-recover" 15 | LDFLAGS="$LDFLAGS -fsanitize=undefined" 16 | #LDFLAGS="$LDFLAGS -fno-sanitize-recover" 17 | fi 18 | 19 | autoreconf -fvi 20 | ./configure --prefix=/tmp/usr --enable-debug 21 | make clean 22 | make 23 | make check 24 | -------------------------------------------------------------------------------- /configure.ac: -------------------------------------------------------------------------------- 1 | # -*- Autoconf -*- 2 | # Process this file with autoconf to produce a configure script. 3 | 4 | AC_PREREQ([2.69]) 5 | AC_INIT([dhcpsnoopingd],[0.2],[michael-dev@fami-braun.de]) 6 | AC_CONFIG_SRCDIR([src]) 7 | AC_CONFIG_MACRO_DIR([m4]) 8 | AM_INIT_AUTOMAKE 9 | AC_CONFIG_HEADER([config.h]) 10 | 11 | # Checks for programs. 12 | AC_PROG_CC 13 | AM_PROG_CC_C_O 14 | AC_LANG(C) 15 | GCC_STACK_PROTECTOR 16 | 17 | PKG_CHECK_MODULES(LIBNL, libnl-3.0) 18 | AC_SUBST(LIBNL_CFLAGS) 19 | AC_SUBST(LIBNL_LIBS) 20 | 21 | PKG_CHECK_MODULES(LIBNLGENL, libnl-genl-3.0) 22 | AC_SUBST(LIBNLGENL_CFLAGS) 23 | AC_SUBST(LIBNLGENL_LIBS) 24 | 25 | PKG_CHECK_MODULES(LIBNLNF, libnl-nf-3.0) 26 | AC_SUBST(LIBNLNF_CFLAGS) 27 | AC_SUBST(LIBNLNF_LIBS) 28 | 29 | PKG_CHECK_MODULES(LIBNLROUTE, libnl-route-3.0) 30 | AC_SUBST(LIBNLROUTE_CFLAGS) 31 | AC_SUBST(LIBNLROUTE_LIBS) 32 | 33 | #check if we want a debug build 34 | AC_ARG_ENABLE(debug, [ --enable-debug enable debug output], [ 35 | if test "x$enableval" != "xno"; then 36 | AC_DEFINE([DEBUG], , [Enable debug output]) 37 | fi 38 | ]) 39 | 40 | #check if version is given 41 | AC_ARG_WITH(rev, [ --with-rev enable rev output], [ 42 | if test "x$withval" != "x"; then 43 | AC_DEFINE_UNQUOTED([REV],"$withval",[revision]) 44 | fi 45 | ]) 46 | 47 | AC_ARG_WITH([nflog-group], 48 | [AS_HELP_STRING([--with-nflog-group], [nflog group id, defaults to 3])], 49 | AC_DEFINE_UNQUOTED([NFLOG_GROUP], $withval , [nflog group]), 50 | AC_DEFINE([NFLOG_GROUP], 3 , [nflog group])) 51 | 52 | # Checks for header files. 53 | AC_HEADER_STDC 54 | 55 | # Checks for library functions. 56 | AC_FUNC_MEMCMP 57 | AC_SEARCH_LIBS([floor], [m]) 58 | 59 | AC_CONFIG_FILES([Makefile]) 60 | AC_OUTPUT 61 | 62 | -------------------------------------------------------------------------------- /debian/README: -------------------------------------------------------------------------------- 1 | The Debian Package mvrpd-fem 2 | ---------------------------- 3 | 4 | Comments regarding the Package 5 | 6 | -- Michael Braun Mon, 30 Mar 2015 17:04:48 +0200 7 | -------------------------------------------------------------------------------- /debian/README.Debian: -------------------------------------------------------------------------------- 1 | mvrpd-fem for Debian 2 | ---------------------------- 3 | 4 | 5 | 6 | -- Michael Braun Mon, 30 Mar 2015 17:04:48 +0200 7 | -------------------------------------------------------------------------------- /debian/README.source: -------------------------------------------------------------------------------- 1 | mvrpd-fem for Debian 2 | ---------------------------- 3 | 4 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /debian/changelog: -------------------------------------------------------------------------------- 1 | mvrpd-fem (0.12) unstable; urgency=medium 2 | 3 | * fix handling inserting new meta 4 | 5 | -- Michael Braun Mon, 26 Aug 2024 14:16:06 +0200 6 | 7 | mvrpd-fem (0.11) unstabled; urgency=medium 8 | 9 | * fix handling big VLAN id 10 | 11 | -- Michael Braun Mon, 08 May 2023 13:31:47 +0200 12 | 13 | mvrpd-fem (0.10) unstable; urgency=medium 14 | 15 | * increase buffer size 16 | 17 | -- Michael Braun Sun, 28 Feb 2021 16:19:11 +0100 18 | 19 | mvrpd-fem (0.9) unstable; urgency=medium 20 | 21 | * rebuild 22 | 23 | -- Michael Braun Wed, 06 May 2020 14:31:58 +0200 24 | 25 | mvrpd-fem (0.8) unstable; urgency=medium 26 | 27 | * handle packet loss more gracefully 28 | 29 | -- Michael Braun Sat, 25 Apr 2020 16:52:29 +0200 30 | 31 | mvrpd-fem (0.7) unstable; urgency=medium 32 | 33 | * fix leaveAll 34 | 35 | -- Michael Braun Fri, 24 Apr 2020 14:40:09 +0200 36 | 37 | mvrpd-fem (0.6) unstable; urgency=medium 38 | 39 | * debug more in verbose 40 | 41 | -- Michael Braun Thu, 23 Apr 2020 22:33:22 +0200 42 | 43 | mvrpd-fem (0.5) unstable; urgency=medium 44 | 45 | * Initial Release. 46 | 47 | -- Michael Braun Mon, 19 Aug 2019 19:04:33 +0200 48 | -------------------------------------------------------------------------------- /debian/compat: -------------------------------------------------------------------------------- 1 | 11 2 | -------------------------------------------------------------------------------- /debian/control: -------------------------------------------------------------------------------- 1 | Source: mvrpd-fem 2 | Section: unknown 3 | Priority: extra 4 | Maintainer: Michael Braun 5 | Build-Depends: debhelper (>= 8.0.0), autotools-dev, libnl-3-dev, libnl-cli-3-dev, libnl-genl-3-dev, libnl-nf-3-dev, libnl-route-3-dev 6 | Standards-Version: 3.9.3 7 | Homepage: https://github.com/michael-dev/mvrpd 8 | #Vcs-Git: git://git.debian.org/collab-maint/mvrpd-fem.git 9 | #Vcs-Browser: http://git.debian.org/?p=collab-maint/mvrpd-fem.git;a=summary 10 | 11 | Package: mvrpd-fem 12 | Architecture: any 13 | Depends: ${shlibs:Depends}, ${misc:Depends} 14 | Description: 15 | 16 | -------------------------------------------------------------------------------- /debian/copyright: -------------------------------------------------------------------------------- 1 | Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ 2 | Upstream-Name: mvrpd-fem 3 | Source: 4 | 5 | Files: * 6 | Copyright: 7 | 8 | License: GPL-3.0+ 9 | 10 | Files: debian/* 11 | Copyright: 2015 Michael Braun 12 | License: GPL-3.0+ 13 | 14 | License: GPL-3.0+ 15 | This program is free software: you can redistribute it and/or modify 16 | it under the terms of the GNU General Public License as published by 17 | the Free Software Foundation, either version 3 of the License, or 18 | (at your option) any later version. 19 | . 20 | This package is distributed in the hope that it will be useful, 21 | but WITHOUT ANY WARRANTY; without even the implied warranty of 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 | GNU General Public License for more details. 24 | . 25 | You should have received a copy of the GNU General Public License 26 | along with this program. If not, see . 27 | . 28 | On Debian systems, the complete text of the GNU General 29 | Public License version 3 can be found in "/usr/share/common-licenses/GPL-3". 30 | 31 | # Please also look if there are files or directories which have a 32 | # different copyright/license attached and list them here. 33 | # Please avoid to pick license terms that are more restrictive than the 34 | # packaged work, as it may make Debian's contributions unacceptable upstream. 35 | -------------------------------------------------------------------------------- /debian/docs: -------------------------------------------------------------------------------- 1 | NEWS 2 | README 3 | README.md 4 | -------------------------------------------------------------------------------- /debian/mvrpd-fem.default: -------------------------------------------------------------------------------- 1 | OPTIONS=--bridge brvlan --uplinkif femap\* --ptpif femap\* --restrict-to-ep --epif brvlan --epif tinctap\* --verbose 2 | -------------------------------------------------------------------------------- /debian/mvrpd-fem.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=MVRPd 3 | After=network.target 4 | 5 | [Service] 6 | EnvironmentFile=/etc/default/mvrpd-fem 7 | ExecStart=/usr/sbin/mvrpd $OPTIONS 8 | KillMode=process 9 | Restart=on-failure 10 | Type=simple 11 | 12 | [Install] 13 | WantedBy=multi-user.target 14 | 15 | 16 | -------------------------------------------------------------------------------- /debian/rules: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | # -*- makefile -*- 3 | # Sample debian/rules that uses debhelper. 4 | # This file was originally written by Joey Hess and Craig Small. 5 | # As a special exception, when this file is copied by dh-make into a 6 | # dh-make output file, you may use that output file without restriction. 7 | # This special exception was added by Craig Small in version 0.37 of dh-make. 8 | 9 | # Uncomment this to turn on verbose mode. 10 | #export DH_VERBOSE=1 11 | 12 | override_dh_auto_configure: 13 | dh_auto_configure -- --enable-debug 14 | 15 | %: 16 | dh $@ --with autotools-dev 17 | -------------------------------------------------------------------------------- /debian/source/format: -------------------------------------------------------------------------------- 1 | 3.0 (native) 2 | -------------------------------------------------------------------------------- /expand_id: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby 2 | data = STDIN.read 3 | last_date = `git log --pretty=format:"%h %ad" -1` 4 | puts data.gsub('$Id$', '$Id: ' + last_date.to_s + '$') 5 | -------------------------------------------------------------------------------- /m4/gcc_stack_protect.m4: -------------------------------------------------------------------------------- 1 | dnl 2 | dnl Useful macros for autoconf to check for ssp-patched gcc 3 | dnl 1.0 - September 2003 - Tiago Sousa 4 | dnl 1.1 - August 2006 - Ted Percival 5 | dnl * Stricter language checking (C or C++) 6 | dnl * Adds GCC_STACK_PROTECT_LIB to add -lssp to LDFLAGS as necessary 7 | dnl * Caches all results 8 | dnl * Uses macros to ensure correct ouput in quiet/silent mode 9 | dnl 1.2 - April 2007 - Ted Percival 10 | dnl * Added GCC_STACK_PROTECTOR macro for simpler (one-line) invocation 11 | dnl * GCC_STACK_PROTECT_LIB now adds -lssp to LIBS rather than LDFLAGS 12 | dnl 13 | dnl About ssp: 14 | dnl GCC extension for protecting applications from stack-smashing attacks 15 | dnl http://www.research.ibm.com/trl/projects/security/ssp/ 16 | dnl 17 | dnl Usage: 18 | dnl Most people will simply call GCC_STACK_PROTECTOR. 19 | dnl If you only use one of C or C++, you can save time by only calling the 20 | dnl macro appropriate for that language. In that case you should also call 21 | dnl GCC_STACK_PROTECT_LIB first. 22 | dnl 23 | dnl GCC_STACK_PROTECTOR 24 | dnl Tries to turn on stack protection for C and C++ by calling the following 25 | dnl three macros with the right languages. 26 | dnl 27 | dnl GCC_STACK_PROTECT_CC 28 | dnl checks -fstack-protector with the C compiler, if it exists then updates 29 | dnl CFLAGS and defines ENABLE_SSP_CC 30 | dnl 31 | dnl GCC_STACK_PROTECT_CXX 32 | dnl checks -fstack-protector with the C++ compiler, if it exists then updates 33 | dnl CXXFLAGS and defines ENABLE_SSP_CXX 34 | dnl 35 | dnl GCC_STACK_PROTECT_LIB 36 | dnl adds -lssp to LIBS if it is available 37 | dnl ssp is usually provided as part of libc, but was previously a separate lib 38 | dnl It does not hurt to add -lssp even if libc provides SSP - in that case 39 | dnl libssp will simply be ignored. 40 | dnl 41 | 42 | AC_DEFUN([GCC_STACK_PROTECT_LIB],[ 43 | AC_CACHE_CHECK([whether libssp exists], ssp_cv_lib, 44 | [ssp_old_libs="$LIBS" 45 | LIBS="$LIBS -lssp" 46 | AC_TRY_LINK(,, ssp_cv_lib=yes, ssp_cv_lib=no) 47 | LIBS="$ssp_old_libs" 48 | ]) 49 | if test $ssp_cv_lib = yes; then 50 | LIBS="$LIBS -lssp" 51 | fi 52 | ]) 53 | 54 | AC_DEFUN([GCC_STACK_PROTECT_CC],[ 55 | AC_LANG_ASSERT(C) 56 | if test "X$CC" != "X"; then 57 | AC_CACHE_CHECK([whether ${CC} accepts -fstack-protector], 58 | ssp_cv_cc, 59 | [ssp_old_cflags="$CFLAGS" 60 | CFLAGS="$CFLAGS -fstack-protector" 61 | AC_TRY_COMPILE(,, ssp_cv_cc=yes, ssp_cv_cc=no) 62 | CFLAGS="$ssp_old_cflags" 63 | ]) 64 | if test $ssp_cv_cc = yes; then 65 | CFLAGS="$CFLAGS -fstack-protector" 66 | AC_DEFINE([ENABLE_SSP_CC], 1, [Define if SSP C support is enabled.]) 67 | fi 68 | fi 69 | ]) 70 | 71 | AC_DEFUN([GCC_STACK_PROTECT_CXX],[ 72 | AC_LANG_ASSERT(C++) 73 | if test "X$CXX" != "X"; then 74 | AC_CACHE_CHECK([whether ${CXX} accepts -fstack-protector], 75 | ssp_cv_cxx, 76 | [ssp_old_cxxflags="$CXXFLAGS" 77 | CXXFLAGS="$CXXFLAGS -fstack-protector" 78 | AC_TRY_COMPILE(,, ssp_cv_cxx=yes, ssp_cv_cxx=no) 79 | CXXFLAGS="$ssp_old_cxxflags" 80 | ]) 81 | if test $ssp_cv_cxx = yes; then 82 | CXXFLAGS="$CXXFLAGS -fstack-protector" 83 | AC_DEFINE([ENABLE_SSP_CXX], 1, [Define if SSP C++ support is enabled.]) 84 | fi 85 | fi 86 | ]) 87 | 88 | AC_DEFUN([GCC_STACK_PROTECTOR],[ 89 | GCC_STACK_PROTECT_LIB 90 | 91 | AC_LANG_PUSH([C]) 92 | GCC_STACK_PROTECT_CC 93 | AC_LANG_POP([C]) 94 | 95 | AC_LANG_PUSH([C++]) 96 | GCC_STACK_PROTECT_CXX 97 | AC_LANG_POP([C++]) 98 | ]) 99 | 100 | -------------------------------------------------------------------------------- /src/bridge.c: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of mvrpd. 3 | * 4 | * mvrpd is free software: you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * mvrpd is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with mvrpd. 16 | * If not, see . 17 | * 18 | * (C) 2019, Michael Braun 19 | */ 20 | 21 | #include "config.h" 22 | 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | #include "debug.h" 30 | #include "port.h" 31 | #include "event.h" 32 | #include "cmdline.h" 33 | #include "timer.h" 34 | #include "vlan.h" 35 | 36 | #define ETH_ALEN 6 37 | #ifndef VLAN_VID_MASK 38 | #define VLAN_VID_MASK 0x0fff /* VLAN Identifier */ 39 | #endif 40 | 41 | struct my_array { 42 | int num; 43 | char** item; 44 | }; 45 | 46 | static struct my_array epIfPattern = { 0, NULL }; 47 | static struct my_array uplinkIfPattern = { 0, NULL }; 48 | static struct my_array ptpIfPattern = { 0, NULL }; 49 | static char *bridge = NULL; 50 | static int bridgeIfIdx = 0; 51 | static struct nl_sock *nf_sock_bcast = NULL; 52 | static struct nl_sock *nf_sock_dump = NULL; 53 | static struct nl_sock *nf_sock_vlan = NULL; 54 | static int dumpNetlink = 0; 55 | 56 | struct nf_obj_cb { 57 | struct nl_msg *msg; 58 | int fromDump; 59 | }; 60 | 61 | static void bridge_dump_links(); 62 | 63 | static int 64 | _br_vlan(int ifidx, int add, struct vlan_arr *vlan) 65 | { 66 | assert(nf_sock_vlan); 67 | struct nl_msg *nlmsg = NULL; 68 | struct nlattr *af_spec = NULL; 69 | int err = -1; 70 | struct ifinfomsg ifi = { 0 }; 71 | 72 | nlmsg = nlmsg_alloc_simple(add ? RTM_SETLINK : RTM_DELLINK, 0); 73 | if (!nlmsg) 74 | goto err; 75 | 76 | ifi.ifi_index = ifidx; 77 | ifi.ifi_family = AF_BRIDGE; 78 | if (nlmsg_append(nlmsg, &ifi, sizeof(ifi), NLMSG_ALIGNTO) < 0) 79 | goto err; 80 | 81 | af_spec = nla_nest_start(nlmsg, IFLA_AF_SPEC); 82 | if (!af_spec) 83 | goto err; 84 | 85 | int it = 0; 86 | uint16_t vid = 0; 87 | while (vlan_next(vlan, &it, &vid) == 0) { 88 | struct bridge_vlan_info vinfo = {}; 89 | vinfo.vid = vid; 90 | if (nla_put(nlmsg, IFLA_BRIDGE_VLAN_INFO, sizeof(vinfo), &vinfo) < 0) 91 | goto err; 92 | } 93 | 94 | nla_nest_end(nlmsg, af_spec); 95 | 96 | err = nl_send_sync(nf_sock_vlan, nlmsg); 97 | nlmsg = NULL; 98 | 99 | err: 100 | if (nlmsg) 101 | nlmsg_free(nlmsg); 102 | 103 | return err; 104 | } 105 | 106 | int br_vlan_add(int ifidx, struct vlan_arr *vlan) 107 | { 108 | return _br_vlan(ifidx, 1, vlan); 109 | } 110 | 111 | int br_vlan_del(int ifidx, struct vlan_arr *vlan) 112 | { 113 | return _br_vlan(ifidx, 0, vlan); 114 | } 115 | 116 | static int 117 | in_array(const char *ifname, const struct my_array *arr) 118 | { 119 | int i; 120 | 121 | assert(ifname); 122 | 123 | for (i = 0; i < arr->num; i++) { 124 | if (fnmatch(arr->item[i], ifname, 0) == 0) { 125 | return 1; 126 | } 127 | } 128 | 129 | return 0; 130 | } 131 | 132 | 133 | /* classify ifname 134 | * returns 135 | * 0: unclassified 136 | * 1: uplink 137 | * 2: ep 138 | */ 139 | static int 140 | classify_ifname(const char *ifname) 141 | { 142 | assert(ifname); 143 | 144 | if (in_array(ifname, &uplinkIfPattern)) 145 | return 1; 146 | 147 | if (in_array(ifname, &epIfPattern)) 148 | return 2; 149 | 150 | return 0; 151 | } 152 | 153 | static int 154 | is_ptp(const char *ifname) 155 | { 156 | return in_array(ifname, &ptpIfPattern); 157 | } 158 | 159 | static void 160 | obj_input_newlink(struct rtnl_link *link, struct nl_msg *msg, int fromDump) 161 | { 162 | const int ifidx = rtnl_link_get_ifindex(link); 163 | const char *ifname = rtnl_link_get_name(link); 164 | 165 | if (bridgeIfIdx == -1 && strncmp(ifname, bridge, IFNAMSIZ) == 0) { 166 | bridgeIfIdx = ifidx; 167 | eprintf(DEBUG_VERBOSE, "bridge newly created %s(%d)", ifname, ifidx); 168 | } 169 | 170 | if (rtnl_link_get_master(link) != bridgeIfIdx && 171 | ifidx != bridgeIfIdx) { 172 | port_del(ifidx); 173 | return; 174 | } 175 | 176 | int type = classify_ifname(ifname); 177 | 178 | eprintf(DEBUG_BRIDGE, "NEWLINK: %s(%d) type %d", ifname, ifidx, type); 179 | 180 | if (type == IF_UNDEF || (type == IF_MVRP && ifidx == bridgeIfIdx)) { 181 | port_del(ifidx); 182 | return; 183 | } 184 | 185 | struct ifinfomsg *ifi = nlmsg_data(nlmsg_hdr(msg)); 186 | struct nlattr *a_af_spec = NULL; 187 | if (ifi->ifi_family != AF_BRIDGE) { 188 | eprintf(DEBUG_BRIDGE, "msg is not of family bridge, so discard IFLA_AF_SPEC"); 189 | /* it might have IFLA_AF_SPEC, but this has a different content */ 190 | if (!fromDump) 191 | bridge_dump_links(); // pass ifidx once the kernel supports it ;) 192 | } else { 193 | a_af_spec = nlmsg_find_attr(nlmsg_hdr(msg), sizeof(struct ifinfomsg), IFLA_AF_SPEC); 194 | } 195 | 196 | struct vlan_arr *vlan = vlan_alloc("br-newlink"); 197 | uint16_t vid_begin = 0; 198 | 199 | if (a_af_spec) { 200 | eprintf(DEBUG_BRIDGE, "got IFLA_AF_SPEC type %d len %d, expecting type %d, fromDump %d", (int) nla_type(a_af_spec), (int) nla_len(a_af_spec), (int) IFLA_AF_SPEC, fromDump); 201 | 202 | int remaining; 203 | struct nlattr *attr; 204 | 205 | nla_for_each_nested(attr, a_af_spec, remaining) { 206 | eprintf(DEBUG_BRIDGE, "got anoter IFLA_AF_SPEC entry type %d len %d, expecting type %d and len %zd", (int) nla_type(attr), (int) nla_len(attr), (int) IFLA_BRIDGE_VLAN_INFO, sizeof(struct bridge_vlan_info)); 207 | if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO) 208 | continue; 209 | if (nla_len(attr) != sizeof(struct bridge_vlan_info)) 210 | continue; 211 | struct bridge_vlan_info *vinfo = nla_data(attr); 212 | if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK) 213 | continue; 214 | if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) { 215 | vid_begin = vinfo->vid; 216 | continue; 217 | } 218 | if ((vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END) && vid_begin) { 219 | eprintf(DEBUG_BRIDGE, "found vlans %d-%d on %s(%d)", vid_begin, vinfo->vid, ifname, ifidx); 220 | for (int vid = vid_begin; vid <= vinfo->vid; vid++) { 221 | vlan_set(vlan, vid); 222 | } 223 | vid_begin = 0; 224 | } else { 225 | eprintf(DEBUG_BRIDGE, "found vlan %d on %s(%d)", vinfo->vid, ifname, ifidx); 226 | vlan_set(vlan, vinfo->vid); 227 | } 228 | } 229 | } else { 230 | eprintf(DEBUG_BRIDGE, "bridge received no VLAN information"); 231 | } 232 | 233 | if (isdebug(DEBUG_BRIDGE)) { 234 | char vlans[4096]; 235 | int trunc = (sizeof(vlans) == vlan_dump(vlan, vlans, sizeof(vlans))); 236 | eprintf(DEBUG_BRIDGE, "port: ifidx: %d name: %s type:%d vlans: %s%s", ifidx, ifname, type, vlans, (trunc ? "...":"")); 237 | } 238 | 239 | struct nl_addr *addr; 240 | addr = rtnl_link_get_addr(link); 241 | if (nl_addr_get_len(addr) != ETH_ALEN) 242 | goto out; 243 | int ptp = is_ptp(ifname); 244 | const char *mac = nl_addr_get_binary_addr(addr); 245 | 246 | port_add(type, ifidx, ifname, ptp, vlan, mac); 247 | out: 248 | vlan_free(vlan); 249 | } 250 | 251 | static void 252 | obj_input_dellink(struct rtnl_link *link, struct nl_msg *msg) 253 | { 254 | const int ifidx = rtnl_link_get_ifindex(link); 255 | if (ifidx == bridgeIfIdx) { 256 | eprintf(DEBUG_ERROR, "my bridge %s removed", rtnl_link_get_name(link)); 257 | bridgeIfIdx = -1; 258 | port_del_all(); 259 | return; 260 | } 261 | port_del(ifidx); 262 | } 263 | 264 | static void 265 | obj_input_route(struct nl_object *obj, void *arg) 266 | { 267 | struct nf_obj_cb *ctx = arg; 268 | struct nl_msg *msg = ctx->msg; 269 | if (isdebug(DEBUG_BRIDGE)) { 270 | char buf[4096]; 271 | nl_object_dump_buf(obj, buf, sizeof(buf)); 272 | eprintf(DEBUG_BRIDGE, "received fromDump=%d %s", ctx->fromDump, buf); 273 | } 274 | 275 | int type = nl_object_get_msgtype(obj); 276 | switch (type) { 277 | case RTM_NEWLINK: 278 | obj_input_newlink((struct rtnl_link *) obj, msg, ctx->fromDump); 279 | break; 280 | case RTM_DELLINK: 281 | obj_input_dellink((struct rtnl_link *) obj, msg); 282 | break; 283 | } 284 | } 285 | 286 | static int 287 | event_input_route(struct nl_msg *msg, void *arg) 288 | { 289 | if (isdebug(DEBUG_BRIDGE)) { 290 | char buf[256] = {0}; 291 | FILE *ofd; 292 | 293 | ofd = fmemopen(buf, sizeof(buf), "w"); 294 | if (ofd && dumpNetlink) { 295 | nl_msg_dump(msg, ofd); 296 | eprintf(DEBUG_BRIDGE, "received message: %s", buf); 297 | nl_msg_dump(msg, stderr); 298 | } else { 299 | eprintf(DEBUG_BRIDGE, "received message"); 300 | } 301 | if (ofd) 302 | fclose(ofd); 303 | } 304 | 305 | struct nf_obj_cb ctx; 306 | ctx.msg = msg; 307 | ctx.fromDump = (arg == nf_sock_dump); 308 | 309 | if (nl_msg_parse(msg, &obj_input_route, &ctx) < 0) 310 | eprintf(DEBUG_BRIDGE, "<> Unknown message type"); 311 | return NL_OK; 312 | } 313 | 314 | static void 315 | bridge_receive(int s, void* ctx) 316 | { 317 | struct nl_sock *nf_sock_route = (struct nl_sock *) ctx; 318 | int ret; 319 | ret = nl_recvmsgs_default(nf_sock_route); 320 | if (ret < 0) { 321 | eprintf(DEBUG_ERROR, "receiving ROUTE->NEIGH failed on %d error %s", s, strerror(errno)); 322 | } 323 | } 324 | 325 | static void 326 | array_append(struct my_array *arr, char* ifname) 327 | { 328 | char** tmp = realloc(arr->item, (arr->num+1) * sizeof(*arr->item)); 329 | if (!tmp) { 330 | eprintf(DEBUG_ERROR, "%s:%d %s error parsing command line", __FILE__, __LINE__, __PRETTY_FUNCTION__); 331 | exit(1); 332 | } 333 | 334 | tmp[arr->num] = calloc(strnlen(ifname,IFNAMSIZ-1)+1, sizeof(char)); 335 | if (!tmp[arr->num]) { 336 | eprintf(DEBUG_ERROR, "%s:%d %s error parsing command line", __FILE__, __LINE__, __PRETTY_FUNCTION__); 337 | exit(1); 338 | } 339 | strcpy(tmp[arr->num], ifname); 340 | arr->item = tmp; 341 | arr->num++; 342 | } 343 | 344 | static void 345 | add_if(int c, void *if_pattern) 346 | { 347 | 348 | if (!optarg) 349 | return; 350 | 351 | eprintf(DEBUG_BRIDGE, "add if prefix %s\n", optarg); 352 | array_append(if_pattern, optarg); 353 | } 354 | 355 | static void 356 | set_if(int c, void *arg) 357 | { 358 | char **ifname = arg; 359 | 360 | if (!optarg) 361 | return; 362 | 363 | eprintf(DEBUG_BRIDGE, "set if %s\n", optarg); 364 | if (*ifname) { 365 | free(*ifname); 366 | *ifname = NULL; 367 | } 368 | *ifname = calloc(strlen(optarg)+1, sizeof(char)); 369 | if (!*ifname) { 370 | eprintf(DEBUG_ERROR, "%s:%d %s error parsing command line", __FILE__, __LINE__, __PRETTY_FUNCTION__); 371 | exit(1); 372 | } 373 | strcpy(*ifname, optarg); 374 | } 375 | 376 | static void 377 | bridge_start_listen() 378 | { 379 | assert(nf_sock_bcast == NULL); 380 | nf_sock_bcast = nl_socket_alloc(); 381 | if (!nf_sock_bcast) { 382 | eprintf(DEBUG_ERROR, "cannot alloc socket (I): %s", strerror(errno)); 383 | exit(254); 384 | } 385 | nl_socket_disable_seq_check(nf_sock_bcast); 386 | nl_socket_modify_cb(nf_sock_bcast, NL_CB_VALID, NL_CB_CUSTOM, event_input_route, nf_sock_bcast); 387 | 388 | if (nl_connect(nf_sock_bcast, NETLINK_ROUTE) < 0) { 389 | eprintf(DEBUG_ERROR, "cannot connect I: %s", strerror(errno)); 390 | exit(254); 391 | } 392 | 393 | if (nl_socket_add_membership(nf_sock_bcast, RTNLGRP_LINK)) { 394 | eprintf(DEBUG_ERROR, "cannot bind to GRPLINK: %s", strerror(errno)); 395 | exit(254); 396 | } 397 | 398 | int rffd = nl_socket_get_fd(nf_sock_bcast); 399 | cb_add_handle(rffd, nf_sock_bcast, bridge_receive); 400 | } 401 | 402 | static void 403 | bridge_dump_init() 404 | { 405 | assert(nf_sock_dump == NULL); 406 | nf_sock_dump = nl_socket_alloc(); 407 | if (!nf_sock_dump) { 408 | eprintf(DEBUG_ERROR, "cannot alloc socket (II): %s", strerror(errno)); 409 | exit(254); 410 | } 411 | 412 | nl_socket_disable_seq_check(nf_sock_dump); 413 | nl_socket_modify_cb(nf_sock_dump, NL_CB_VALID, NL_CB_CUSTOM, event_input_route, nf_sock_dump); 414 | nl_socket_disable_auto_ack(nf_sock_dump); 415 | 416 | if (nl_connect(nf_sock_dump, NETLINK_ROUTE) < 0) { 417 | eprintf(DEBUG_ERROR, "cannot connect II: %s", strerror(errno)); 418 | exit(254); 419 | } 420 | 421 | int rffd = nl_socket_get_fd(nf_sock_dump); 422 | cb_add_handle(rffd, nf_sock_dump, bridge_receive); 423 | } 424 | 425 | static void 426 | bridge_vlan_init() 427 | { 428 | assert(nf_sock_vlan == NULL); 429 | nf_sock_vlan = nl_socket_alloc(); 430 | if (!nf_sock_vlan) { 431 | eprintf(DEBUG_ERROR, "cannot alloc socket (III): %s", strerror(errno)); 432 | exit(254); 433 | } 434 | 435 | if (nl_connect(nf_sock_vlan, NETLINK_ROUTE) < 0) { 436 | eprintf(DEBUG_ERROR, "cannot connect III: %s", strerror(errno)); 437 | exit(254); 438 | } 439 | } 440 | 441 | static void 442 | bridge_dump_links() 443 | { 444 | /* nl_rtgen_request(nf_sock_dump, RTM_GETNEIGH, AF_BRIDGE, NLM_F_DUMP) 445 | * produces an undersized payload and thus gets discarded by the kernel. 446 | */ 447 | /* 448 | * getting vlan information is only supported for AF_BRIDGE w NLM_F_DUMP RTM_GETLINK requests. 449 | * All others do not have it. 450 | * Sadly, AF_BRIGE+NLM_F_DUMP->kernel:rtnl_bridge_getlink does not allow to filter for master device or ifidx. 451 | * If it could, w'd use: 452 | * 1. msg.ifi_index = ifidx; 453 | * 2. nlmsg = nlmsg_alloc_simple(RTM_GETLINK, NLM_F_REQUEST | (ifidx ? 0 : NLM_F_DUMP)); 454 | * 3. if (!ifidx && 455 | * nla_put_u32(nlmsg, IFLA_MASTER, bridgeIfIdx) < 0) ... 456 | } 457 | */ 458 | struct ifinfomsg msg = { 0 }; 459 | struct nl_msg *nlmsg = NULL; 460 | 461 | msg.ifi_family = AF_BRIDGE; 462 | 463 | nlmsg = nlmsg_alloc_simple(RTM_GETLINK, NLM_F_REQUEST | NLM_F_DUMP); 464 | if (!nlmsg) { 465 | eprintf(DEBUG_ERROR, "out of memory"); 466 | exit(254); 467 | } 468 | if (nlmsg_append(nlmsg, &msg, sizeof(msg), NLMSG_ALIGNTO) < 0) { 469 | eprintf(DEBUG_ERROR, "out of memory"); 470 | exit(254); 471 | } 472 | if (nla_put_u32(nlmsg, IFLA_EXT_MASK, RTEXT_FILTER_BRVLAN) < 0) { 473 | eprintf(DEBUG_ERROR, "out of memory"); 474 | exit(254); 475 | } 476 | 477 | if (isdebug(DEBUG_BRIDGE)) { 478 | char buf[1024] = {0}; 479 | FILE *ofd; 480 | 481 | ofd = fmemopen(buf, sizeof(buf), "w"); 482 | if (ofd && dumpNetlink) { 483 | nl_msg_dump(nlmsg, ofd); 484 | eprintf(DEBUG_BRIDGE, "send message: %s", buf); 485 | } else { 486 | eprintf(DEBUG_BRIDGE, "send message"); 487 | } 488 | if (ofd) 489 | fclose(ofd); 490 | } 491 | 492 | if (nl_send_auto(nf_sock_dump, nlmsg) < 0) { /* ACK was disabled above */ 493 | eprintf(DEBUG_ERROR, "netlink error"); 494 | exit(254); 495 | } 496 | 497 | nlmsg_free(nlmsg); 498 | } 499 | 500 | static void 501 | bridge_start(void *ctx) 502 | { 503 | eprintf(DEBUG_BRIDGE, "Listen to ROUTE->LINK notifications"); 504 | 505 | if (!bridge) { 506 | eprintf(DEBUG_ERROR, "no bridge set"); 507 | exit(254); 508 | } 509 | 510 | bridgeIfIdx = if_nametoindex(bridge); 511 | 512 | if (!bridgeIfIdx) { 513 | eprintf(DEBUG_ERROR, "bridge does not exist"); 514 | bridgeIfIdx = -1; 515 | } 516 | 517 | /* connect to netlink route to get notified of new bridge ports */ 518 | bridge_start_listen(); 519 | 520 | /* connect to netlink route to dump all known bridge ports */ 521 | bridge_dump_init(); 522 | if (bridgeIfIdx != -1) 523 | /* cannot pass bridgeIfIdx for filtering as not supported by kernel */ 524 | bridge_dump_links(); 525 | 526 | /* socket or vlan_add or vlan_del */ 527 | bridge_vlan_init(); 528 | } 529 | 530 | static void 531 | setDumpNetlink(int c, void *arg) 532 | { 533 | dumpNetlink = 1; 534 | } 535 | 536 | static __attribute__((constructor)) void 537 | bridge_init() 538 | { 539 | { 540 | struct option long_option = {"epif", required_argument, 0, 0}; 541 | add_option_cb(long_option, add_if, &epIfPattern); 542 | } 543 | { 544 | struct option long_option = {"uplinkif", required_argument, 0, 0}; 545 | add_option_cb(long_option, add_if, &uplinkIfPattern); 546 | } 547 | { 548 | struct option long_option = {"ptpif", required_argument, 0, 0}; 549 | add_option_cb(long_option, add_if, &ptpIfPattern); 550 | } 551 | { 552 | struct option long_option = {"bridge", required_argument, 0, 0}; 553 | add_option_cb(long_option, set_if, &bridge); 554 | } 555 | { 556 | struct option long_option = {"bridge-dump-netlink", no_argument, 0, 0}; 557 | add_option_cb(long_option, setDumpNetlink, NULL); 558 | } 559 | cb_add_timer(0, 0, NULL, bridge_start); 560 | } 561 | 562 | -------------------------------------------------------------------------------- /src/bridge.h: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of mvrpd. 3 | * 4 | * mvrpd is free software: you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * mvrpd is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with mvrpd. 16 | * If not, see . 17 | * 18 | * (C) 2019, Michael Braun 19 | */ 20 | 21 | struct vlan_arr; 22 | 23 | int br_vlan_add(int ifidx, struct vlan_arr *vlan); 24 | int br_vlan_del(int ifidx, struct vlan_arr *vlan); 25 | 26 | -------------------------------------------------------------------------------- /src/cmdline.c: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of mvrpd. 3 | * 4 | * mvrpd is free software: you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * mvrpd is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with mvrpd. 16 | * If not, see . 17 | * 18 | * (C) 2019, Michael Braun 19 | */ 20 | 21 | #include "config.h" 22 | #include "cmdline.h" 23 | #include "debug.h" 24 | #include 25 | #include 26 | #include 27 | 28 | struct option_cb_entry { 29 | struct option option; 30 | option_cb cb; 31 | void *cbarg; 32 | struct option_cb_entry* next; 33 | }; 34 | 35 | static struct option_cb_entry* globalOptionCb = NULL; 36 | static int globalOptionCbSize = 0; 37 | 38 | void add_option_cb(struct option opt, option_cb cb, void *cbarg) { 39 | struct option_cb_entry* entry = malloc(sizeof(struct option_cb_entry)); 40 | if (!entry) { 41 | eprintf(DEBUG_ERROR, "out of memory at %s:%d in %s", __FILE__, __LINE__, __PRETTY_FUNCTION__); 42 | exit(1); 43 | } 44 | memcpy(&entry->option, &opt, sizeof(opt)); 45 | entry->cb = cb; 46 | entry->cbarg = cbarg; 47 | entry->next = globalOptionCb; 48 | globalOptionCb = entry; 49 | globalOptionCbSize++; 50 | } 51 | 52 | void parse_cmdline(int argc, char *argv[]) 53 | { 54 | struct option *long_options = calloc(globalOptionCbSize + 1, sizeof(struct option)); 55 | struct option_cb_entry **option_cbs = calloc(globalOptionCbSize, sizeof(*option_cbs)); 56 | int i=0; 57 | for(struct option_cb_entry *entry = globalOptionCb; entry; entry = entry->next, i++) { 58 | memcpy(&long_options[i], &entry->option, sizeof(entry->option)); 59 | option_cbs[i] = entry; 60 | } 61 | 62 | int option_index = 0; 63 | int c; 64 | while ((c = getopt_long (argc, argv, "", long_options, &option_index)) != -1) { 65 | if (c == '?') { 66 | eprintf(DEBUG_ERROR, "%s:%d %s error parsing command line", __FILE__, __LINE__, __PRETTY_FUNCTION__); 67 | exit(1); 68 | } 69 | if (option_index < 0 || option_index >= globalOptionCbSize) { 70 | eprintf(DEBUG_ERROR, "%s:%d %s error parsing command line - invalid index returned", __FILE__, __LINE__, __PRETTY_FUNCTION__); 71 | exit(1); 72 | } 73 | struct option_cb_entry *entry = option_cbs[option_index]; 74 | entry->cb(c, entry->cbarg); 75 | } 76 | free (option_cbs); 77 | free (long_options); 78 | } 79 | -------------------------------------------------------------------------------- /src/cmdline.h: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of mvrpd. 3 | * 4 | * mvrpd is free software: you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * mvrpd is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with mvrpd. 16 | * If not, see . 17 | * 18 | * (C) 2019, Michael Braun 19 | */ 20 | 21 | #ifndef MVRPD_CMDLINE 22 | #define MVRPD_CMDLINE 23 | 24 | #include 25 | 26 | typedef void (*option_cb)(int c, void *arg); 27 | void add_option_cb(struct option opt, option_cb cb, void *cbarg); 28 | void parse_cmdline(); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /src/debug.c: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of mvrpd. 3 | * 4 | * mvrpd is free software: you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * mvrpd is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with mvrpd. 16 | * If not, see . 17 | * 18 | * (C) 2019, Michael Braun 19 | */ 20 | 21 | #include "config.h" 22 | #include "debug.h" 23 | #include "cmdline.h" 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | static int debug = DEBUG_ERROR; 30 | 31 | void set_debug_flag(int c, void *ctx) { 32 | debug |= c; 33 | } 34 | 35 | int isdebug(const int level) { 36 | return !!(level & debug); 37 | } 38 | 39 | void edprint(const int level, const char* msg, const char* file, const int line, const char* fnc) 40 | { 41 | char syslogbuf[4096]; 42 | const char *bname; 43 | if (level & debug) { 44 | bname = (strrchr(file, '/') ? strrchr(file, '/') + 1 : file); 45 | snprintf(syslogbuf, sizeof(syslogbuf), "%s (%s:%d): %s", fnc, bname, line, msg); 46 | #ifdef DEBUG 47 | openlog ("mvrpd", LOG_CONS | LOG_PID | LOG_NDELAY | LOG_PERROR, LOG_DAEMON); 48 | syslog(LOG_INFO, "%s", syslogbuf); 49 | closelog(); 50 | #else 51 | fprintf(stderr, "%s\n", syslogbuf); 52 | #endif 53 | }; 54 | } 55 | 56 | static __attribute__((constructor)) void debug_init() 57 | { 58 | { 59 | struct option long_option = {"debug", no_argument, 0, DEBUG_GENERAL}; 60 | add_option_cb(long_option, set_debug_flag, NULL); 61 | } 62 | { 63 | struct option long_option = {"debug-nflog", no_argument, 0, DEBUG_NFLOG}; 64 | add_option_cb(long_option, set_debug_flag, NULL); 65 | } 66 | { 67 | struct option long_option = {"debug-ether", no_argument, 0, DEBUG_ETHER}; 68 | add_option_cb(long_option, set_debug_flag, NULL); 69 | } 70 | { 71 | struct option long_option = {"debug-bridge", no_argument, 0, DEBUG_BRIDGE}; 72 | add_option_cb(long_option, set_debug_flag, NULL); 73 | } 74 | { 75 | struct option long_option = {"debug-mvrp", no_argument, 0, DEBUG_MVRP}; 76 | add_option_cb(long_option, set_debug_flag, NULL); 77 | } 78 | { 79 | struct option long_option = {"debug-port", no_argument, 0, DEBUG_PORT}; 80 | add_option_cb(long_option, set_debug_flag, NULL); 81 | } 82 | { 83 | struct option long_option = {"debug-all", no_argument, 0, DEBUG_ALL}; 84 | add_option_cb(long_option, set_debug_flag, NULL); 85 | } 86 | { 87 | struct option long_option = {"verbose", no_argument, 0, DEBUG_VERBOSE}; 88 | add_option_cb(long_option, set_debug_flag, NULL); 89 | } 90 | } 91 | 92 | -------------------------------------------------------------------------------- /src/debug.h: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of mvrpd. 3 | * 4 | * mvrpd is free software: you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * mvrpd is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with mvrpd. 16 | * If not, see . 17 | * 18 | * (C) 2019, Michael Braun 19 | */ 20 | 21 | #ifndef MVRPD_DEBUG 22 | #define MVRPD_DEBUG 23 | 24 | #define DEBUG_ERROR 1 25 | #define DEBUG_GENERAL 2 26 | #define DEBUG_NFLOG 4 27 | #define DEBUG_ETHER 8 28 | #define DEBUG_BRIDGE 16 29 | #define DEBUG_MVRP 32 30 | #define DEBUG_VERBOSE 64 31 | #define DEBUG_PORT 128 32 | #define DEBUG_ALL 255 33 | 34 | #include 35 | 36 | int isdebug(const int level); 37 | void edprint(const int level, const char* msg, const char* file, const int line, const char* fnc); 38 | #define eprintf(level, ...) { \ 39 | if (isdebug(level)) { \ 40 | char syslogbuf[81920];\ 41 | snprintf(syslogbuf, sizeof(syslogbuf), __VA_ARGS__);\ 42 | edprint(level, syslogbuf, __FILE__, __LINE__, __PRETTY_FUNCTION__);\ 43 | };\ 44 | }; 45 | 46 | #endif 47 | 48 | -------------------------------------------------------------------------------- /src/ether.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include "debug.h" 16 | #include "event.h" 17 | 18 | struct ether_socket { 19 | int if_index; 20 | char if_name[IFNAMSIZ]; 21 | char if_mac[ETH_ALEN]; 22 | char mcast_mac[ETH_ALEN]; 23 | int hwproto; 24 | int fd; 25 | }; 26 | 27 | int ether_send(struct ether_socket *sock, const char* dst /* may be NULL */, const unsigned char *msg, size_t msglen) { 28 | uint8_t *buf; 29 | size_t buf_len; 30 | int err; 31 | struct ether_header *eh; 32 | struct sockaddr_ll sock_addr; 33 | 34 | eprintf(DEBUG_ETHER, "sending packet on %d len %zd", sock->if_index, msglen); 35 | 36 | if (!dst) 37 | dst = sock->mcast_mac; 38 | 39 | buf_len = sizeof(*eh) + msglen; 40 | if (buf_len > ETH_FRAME_LEN) 41 | return -EMSGSIZE; 42 | 43 | buf = malloc(buf_len); 44 | if (!buf) 45 | return -EMSGSIZE; 46 | memset (buf, 0, buf_len); 47 | 48 | eh = (struct ether_header *) buf; 49 | memcpy (eh->ether_shost, sock->if_mac, ETH_ALEN); 50 | memcpy (eh->ether_dhost, dst, ETH_ALEN); 51 | eh->ether_type = htons (sock->hwproto); 52 | 53 | memcpy(buf + sizeof(*eh), msg, msglen); 54 | 55 | memset (&sock_addr, 0, sizeof(sock_addr)); 56 | sock_addr.sll_ifindex = sock->if_index; 57 | sock_addr.sll_halen = ETH_ALEN; 58 | memcpy (sock_addr.sll_addr, dst, ETH_ALEN); 59 | 60 | err = sendto(sock->fd, buf, buf_len, 0, 61 | (struct sockaddr *) &sock_addr, sizeof (sock_addr)); 62 | if (err < 0) { 63 | eprintf(DEBUG_ERROR, "sending packet failed: %s(%d)", strerror(errno), errno); 64 | goto out; 65 | } 66 | 67 | err = 0; 68 | 69 | out: 70 | free(buf); 71 | 72 | return err; 73 | } 74 | 75 | static void ether_receive(int s, void *ctx) 76 | { 77 | struct ether_socket *sock = ctx; 78 | struct sockaddr_ll client_addr; 79 | struct ether_header *eh; 80 | struct msghdr msg; 81 | struct iovec iov; 82 | uint8_t buf[ETH_FRAME_LEN], *payload; 83 | int hwproto; 84 | size_t bytes = 0, payloadlen = 0; 85 | 86 | memset(&msg, 0, sizeof(msg)); 87 | memset(&client_addr, 0, sizeof(client_addr)); 88 | memset(buf, 0, sizeof(buf)); 89 | 90 | eprintf(DEBUG_ETHER, "ether receive on %s(%d)", sock->if_name, sock->if_index); 91 | 92 | iov.iov_len = sizeof(buf); 93 | iov.iov_base = buf; 94 | msg.msg_name = &client_addr; 95 | msg.msg_namelen = sizeof(client_addr); 96 | msg.msg_iov = &iov; 97 | msg.msg_iovlen = 1; 98 | bytes = recvmsg(sock->fd, &msg, 0); 99 | 100 | if (bytes < sizeof(*eh)) 101 | return; 102 | 103 | eh = (struct ether_header*) buf; 104 | hwproto = htons(eh->ether_type); 105 | if (hwproto != sock->hwproto) { 106 | eprintf(DEBUG_ETHER, "ether...packet drop received proto=%x!=%x on %s(%d)", hwproto, sock->hwproto, sock->if_name, sock->if_index); 107 | return; 108 | } 109 | if (memcmp(eh->ether_dhost, sock->if_mac, ETH_ALEN) != 0 && 110 | memcmp(eh->ether_dhost, sock->mcast_mac, ETH_ALEN) != 0) { 111 | eprintf(DEBUG_ETHER, "ether...packet drop received proto=%x on %s(%d) daddr %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", hwproto, sock->if_name, sock->if_index, eh->ether_dhost[0], eh->ether_dhost[1], eh->ether_dhost[2], eh->ether_dhost[3], eh->ether_dhost[4], eh->ether_dhost[5]); 112 | return; 113 | } 114 | 115 | payload = buf + sizeof(*eh); 116 | payloadlen = bytes - sizeof(*eh); 117 | 118 | if (isdebug(DEBUG_ETHER)) { 119 | eprintf(DEBUG_ETHER, "ether...packet received proto=%x on %s(%d)", hwproto, sock->if_name, sock->if_index); 120 | fprintf(stderr, "payload = "); 121 | for (int i = 0; i < payloadlen; i++) 122 | { 123 | fprintf(stderr, "%s%02x", (i > 0 ? ":" : ""), payload[i]); 124 | } 125 | fprintf(stderr, "\n"); 126 | } 127 | 128 | cb_call_packet_cb(hwproto, payload, payloadlen, sock->if_name, sock->if_index); 129 | } 130 | 131 | void ether_close(struct ether_socket *sock) { 132 | if (!sock) 133 | return; 134 | cb_del_handle(sock->fd, sock, ether_receive); 135 | close(sock->fd); 136 | free(sock); 137 | } 138 | 139 | struct ether_socket * 140 | ether_listen(int if_index, const char *if_name, const char *if_mac, int hwproto, const char* mcast_mac) 141 | { 142 | struct packet_mreq multicast_req; 143 | struct sockaddr_ll addr; 144 | int err, fd; 145 | struct ether_socket *sock = NULL; 146 | 147 | eprintf(DEBUG_ETHER, "listening for packets on %s(%d) type %04x mcast %02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", if_name, if_index, hwproto, 148 | mcast_mac[0],mcast_mac[1],mcast_mac[2],mcast_mac[3],mcast_mac[4],mcast_mac[5]); 149 | 150 | /* filtering for hwproto did not work for me, hwptoto=ETH_P_ALL works but will result in a load problem as tagged packets are received as well */ 151 | fd = socket (PF_PACKET, SOCK_RAW, htons(hwproto)); 152 | if (fd < 0) 153 | goto errout; 154 | memset(&addr, 0, sizeof(addr)); 155 | addr.sll_ifindex = if_index; 156 | addr.sll_family = AF_PACKET; 157 | addr.sll_protocol = htons(hwproto); 158 | 159 | err = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); 160 | if (err < 0) 161 | goto errout; 162 | 163 | memset(&multicast_req, 0, sizeof(multicast_req));; 164 | multicast_req.mr_ifindex = if_index; 165 | multicast_req.mr_type = PACKET_MR_MULTICAST; 166 | multicast_req.mr_alen = ETH_ALEN; 167 | memcpy(multicast_req.mr_address, mcast_mac, ETH_ALEN); 168 | err = setsockopt(fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, 169 | &multicast_req, sizeof(multicast_req)); 170 | if (err < 0) 171 | goto errout; 172 | 173 | sock = malloc(sizeof(*sock)); 174 | assert(sock); 175 | sock->if_index = if_index; 176 | strncpy(sock->if_name, if_name, sizeof(sock->if_name) - 1); 177 | memcpy(sock->if_mac, if_mac, ETH_ALEN); 178 | memcpy(sock->mcast_mac, mcast_mac, ETH_ALEN); 179 | sock->hwproto = hwproto; 180 | sock->fd = fd; 181 | 182 | /* for unkown reasons this is not called ... */ 183 | cb_add_handle(fd, sock, ether_receive); 184 | 185 | return sock; 186 | 187 | errout: 188 | eprintf(DEBUG_ERROR, "ether socket error: %s(%d)", strerror(errno), errno ); 189 | if (fd >= 0) 190 | close(fd); 191 | return NULL; 192 | } 193 | -------------------------------------------------------------------------------- /src/ether.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct ether_socket; 4 | 5 | int ether_send(struct ether_socket *sock, const char* dst /* may be NULL */, const unsigned char *msg, size_t msglen); 6 | void ether_close(struct ether_socket *sock); 7 | struct ether_socket * 8 | ether_listen(int if_index, const char *if_name, const char *if_mac, int hwproto, const char* mcast_addr); 9 | 10 | -------------------------------------------------------------------------------- /src/event.c: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of mvrpd. 3 | * 4 | * mvrpd is free software: you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * mvrpd is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with mvrpd. 16 | * If not, see . 17 | * 18 | * (C) 2019, Michael Braun 19 | */ 20 | 21 | #include "config.h" 22 | #include "event.h" 23 | #include "debug.h" 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | struct packet_cb_list_entry { 33 | packet_cb cb; 34 | struct packet_cb_list_entry* next; 35 | }; 36 | struct packet_cb_list_entry* packet_cb_list = NULL; 37 | 38 | struct handle_cb_list_entry { 39 | handle_cb cb; 40 | int h; 41 | void *ctx; 42 | int delete; 43 | struct handle_cb_list_entry* next; 44 | }; 45 | struct handle_cb_list_entry* handle_cb_list = NULL; 46 | 47 | struct signal_cb_list_entry { 48 | signal_cb cb; 49 | int s; 50 | int called; 51 | struct signal_cb_list_entry* next; 52 | }; 53 | struct signal_cb_list_entry* signal_cb_list = NULL; 54 | 55 | int signalCalled = 0; 56 | 57 | void cb_add_packet_cb(packet_cb cb) { 58 | struct packet_cb_list_entry* entry = malloc(sizeof(struct packet_cb_list_entry)); 59 | if (!entry) { 60 | eprintf(DEBUG_ERROR, "out of memory"); 61 | exit(1); 62 | } 63 | memset(entry, 0, sizeof(struct packet_cb_list_entry)); 64 | entry->cb = cb; 65 | entry->next = packet_cb_list; 66 | packet_cb_list = entry; 67 | }; 68 | 69 | void cb_call_packet_cb(const int ptype, const uint8_t *packet, const int len, const char* ifname, const int ifindex) { 70 | for (struct packet_cb_list_entry* entry = packet_cb_list; entry; entry = entry->next) { 71 | entry->cb(ptype, packet, len, ifname, ifindex); 72 | } 73 | }; 74 | 75 | void cb_add_handle(int h, void* ctx, handle_cb cb) { 76 | struct handle_cb_list_entry* entry = malloc(sizeof(struct handle_cb_list_entry)); 77 | if (!entry) { 78 | eprintf(DEBUG_ERROR, "out of memory"); 79 | exit(1); 80 | } 81 | if(!h) { 82 | eprintf(DEBUG_ERROR, "no handle given"); 83 | exit(1); 84 | } 85 | if (!cb) { 86 | eprintf(DEBUG_ERROR, "no cb given"); 87 | exit(1); 88 | } 89 | memset(entry, 0, sizeof(struct handle_cb_list_entry)); 90 | entry->h = h; 91 | entry->cb = cb; 92 | entry->ctx = ctx; 93 | entry->next = handle_cb_list; 94 | handle_cb_list = entry; 95 | }; 96 | 97 | void cb_del_handle(int h, void* ctx, handle_cb cb) { 98 | for (struct handle_cb_list_entry* entry = handle_cb_list; entry; entry = entry->next) { 99 | if (entry->h != h || 100 | entry->cb != cb || 101 | entry->ctx != ctx) 102 | continue; 103 | entry->delete = 1; 104 | } 105 | } 106 | 107 | void signal_cb_int(int s) { 108 | for (struct signal_cb_list_entry* entry = signal_cb_list; entry; entry = entry->next) { 109 | if (entry->s == s) { 110 | entry->called++; 111 | signalCalled = 1; 112 | } 113 | } 114 | }; 115 | 116 | void cb_add_signal(int s, signal_cb cb) { 117 | struct signal_cb_list_entry* entry = malloc(sizeof(struct signal_cb_list_entry)); 118 | if (!entry) { 119 | eprintf(DEBUG_ERROR, "out of memory"); 120 | exit(1); 121 | } 122 | memset(entry, 0, sizeof(struct signal_cb_list_entry)); 123 | if(!s) { 124 | eprintf(DEBUG_ERROR, "no signal given"); 125 | exit(1); 126 | } 127 | if (!cb) { 128 | eprintf(DEBUG_ERROR, "no cb given"); 129 | exit(1); 130 | } 131 | entry->s = s; 132 | entry->cb = cb; 133 | entry->next = signal_cb_list; 134 | signal_cb_list = entry; 135 | signal(s, signal_cb_int); 136 | }; 137 | 138 | void event_runloop() { 139 | fd_set rfds; 140 | int maxfd, retval; 141 | 142 | // Block SIGALRM and SIGUSR1 143 | sigset_t sigset, oldset; 144 | sigemptyset(&sigset); 145 | for (struct signal_cb_list_entry* entry = signal_cb_list; entry; entry = entry->next) { 146 | sigaddset (&sigset, entry->s); 147 | } 148 | sigprocmask(SIG_BLOCK, &sigset, &oldset); 149 | 150 | while (1) { 151 | FD_ZERO(&rfds); 152 | maxfd = -1; 153 | { 154 | struct handle_cb_list_entry *entry = handle_cb_list, *prev = NULL; 155 | while (entry) { 156 | if (!entry->delete) { 157 | FD_SET(entry->h, &rfds); 158 | if (maxfd < entry->h) { 159 | maxfd = entry->h; 160 | } 161 | prev = entry; 162 | entry = prev->next; 163 | } else { 164 | if (prev) 165 | prev->next = entry->next; 166 | else 167 | handle_cb_list = entry->next; 168 | free(entry); 169 | if (prev) 170 | entry = prev->next; 171 | else 172 | entry = handle_cb_list; 173 | } 174 | } 175 | } 176 | 177 | signalCalled = 0; 178 | retval = pselect(maxfd+1, &rfds, NULL, NULL, NULL, &oldset); 179 | if (retval < 0 && errno != EINTR) 180 | break; 181 | if (retval > 0) { 182 | for (struct handle_cb_list_entry* entry = handle_cb_list; entry; entry = entry->next) { 183 | if (FD_ISSET(entry->h, &rfds)) { 184 | entry->cb(entry->h, entry->ctx); 185 | } 186 | } 187 | } 188 | if (signalCalled > 0) { 189 | for (struct signal_cb_list_entry* entry = signal_cb_list; entry; entry = entry->next) { 190 | if (entry->called > 0) { 191 | entry->called = 0; 192 | entry->cb(entry->s); 193 | } 194 | } 195 | } 196 | } 197 | eprintf(DEBUG_ERROR, "exit due to: %s (%d)", strerror(errno), errno); 198 | }; 199 | -------------------------------------------------------------------------------- /src/event.h: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of mvrpd. 3 | * 4 | * mvrpd is free software: you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * mvrpd is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with mvrpd. 16 | * If not, see . 17 | * 18 | * (C) 2019, Michael Braun 19 | */ 20 | #ifndef MVRPD_EVENT 21 | #define MVRPD_EVENT 22 | 23 | #include 24 | 25 | typedef void (*packet_cb) (const int ptype, const uint8_t *packet, const int len, const char* ifname, const int ifindex); 26 | typedef void (*handle_cb) (int h, void* ctx); 27 | typedef void (*signal_cb) (int h); 28 | 29 | void cb_add_packet_cb(packet_cb cb); 30 | void cb_call_packet_cb(const int ptype, const uint8_t *packet, const int len, const char* ifname, const int ifindex); 31 | void cb_add_handle(int h, void* ctx, handle_cb cb); 32 | void cb_del_handle(int h, void* ctx, handle_cb cb); 33 | void cb_add_signal(int s, signal_cb cb); 34 | 35 | void event_runloop(); 36 | 37 | #endif 38 | 39 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of mvrpd. 3 | * 4 | * mvrpd is free software: you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * mvrpd is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with mvrpd. 16 | * If not, see . 17 | * 18 | * (C) 2019, Michael Braun 19 | */ 20 | 21 | #include "config.h" 22 | #include "cmdline.h" 23 | #include "event.h" 24 | #include "debug.h" 25 | #include 26 | 27 | int main(int argc, char *argv[]) 28 | { 29 | #ifdef REV 30 | fprintf(stderr, "mvrpd version svn-%s\n", REV); 31 | #else 32 | fprintf(stderr, "mvrpd version $Id$\n"); 33 | #endif 34 | 35 | parse_cmdline(argc, argv); 36 | event_runloop(); 37 | } 38 | -------------------------------------------------------------------------------- /src/mvrp.c: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of mvrpd. 3 | * 4 | * mvrpd is free software: you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * mvrpd is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with mvrpd. 16 | * If not, see . 17 | * 18 | * (C) 2019, Michael Braun 19 | */ 20 | 21 | #include "config.h" 22 | #include "event.h" 23 | #include "debug.h" 24 | #include "mvrp.h" 25 | #include "timer.h" 26 | #include "ether.h" 27 | #include "port.h" 28 | #include "vlan.h" 29 | #include "random.h" 30 | 31 | #include 32 | #include 33 | #include 34 | 35 | #define MIN2(a,b) ( a <= b ? a : b) 36 | #define MIN(a,b,c,d,e,f) MIN2(MIN2(MIN2(MIN2(MIN2(a,b),c),d),e),f) 37 | 38 | const time_t leaveAllInterval = 60; 39 | const time_t leaveTimeout = 5; // at least, at most twice 40 | const time_t leaveAllLeaveTimeout = 50; // at least, at most twice 41 | const time_t periodicSendInterval = 10; 42 | const time_t gracePeriodForRemoteLeaveAll = 10; 43 | 44 | struct mrpdu_message { 45 | uint8_t AttributeType; 46 | uint8_t AttributeLength; /* length of FirstValue */ 47 | } __attribute__((packed)); 48 | 49 | struct mrpdu { 50 | uint8_t ProtocolVersion; 51 | /* mrpdu could have trailing NULL (0x0000) indicating the ENDMARK */ 52 | } __attribute__((packed)); 53 | 54 | struct mrpdu_vectorattrib { 55 | uint16_t VectorHeader; /* LeaveAllEvent << 13 | NumberOfValues */ 56 | } __attribute__((packed)); 57 | 58 | enum mvrp_event { 59 | MVRP_EV_NEW = 0, 60 | MVRP_EV_JOININ = 1, 61 | MVRP_EV_IN = 2, 62 | MVRP_EV_JOINMT = 3, 63 | MVRP_EV_MT = 4, 64 | MVRP_EV_LV = 5, 65 | _MVRP_EV_MAX 66 | }; 67 | 68 | struct mvrp_build_state { 69 | unsigned int changes:1; 70 | unsigned int notempty:1; 71 | }; 72 | 73 | static const char mvrp_addr[6] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x21 }; 74 | static const char MVRP_PROTO_VERSION = 0x00; 75 | static const char MVRP_VID_ATTR_LEN = 0x02; 76 | static const char MVRP_VID_ATTR_TYPE = 0x01; 77 | 78 | static void _mvrp_send(struct if_entry *port, int leaveAll, int force); 79 | 80 | static void 81 | mvrp_do_leaveall(struct if_entry *port) 82 | { 83 | 84 | vlan_free(port->vlan_declared_remote_leaveAll); 85 | port->vlan_declared_remote_leaveAll = vlan_clone(port->vlan_declared_remote, "port->drl"); 86 | port->needSend = 1; 87 | } 88 | 89 | static void 90 | mvrp_handle_leaveall(struct if_entry *port) 91 | { 92 | if (!port) 93 | return; 94 | 95 | struct timespec tv; 96 | clock_gettime(CLOCK_MONOTONIC, &tv); 97 | port->lastLeaveAll = tv.tv_sec; 98 | port->lastLeaveAllFromMe = 0; 99 | mvrp_do_leaveall(port); 100 | eprintf(DEBUG_MVRP, "received leaveAll on port %s(%d) [type=%d], trigger sending", port->ifname, port->ifidx, port->type); 101 | } 102 | 103 | static const char * 104 | mvrp_event2str(int event) 105 | { 106 | switch (event) { 107 | case MVRP_EV_NEW: 108 | return "NEW"; 109 | case MVRP_EV_JOININ: 110 | return "JOININ"; 111 | case MVRP_EV_IN: 112 | return "IN"; 113 | case MVRP_EV_JOINMT: 114 | return "JOINMT"; 115 | case MVRP_EV_MT: 116 | return "MT"; 117 | case MVRP_EV_LV: 118 | return "LV"; 119 | default: 120 | return "??"; 121 | } 122 | } 123 | 124 | static void 125 | mvrp_handle_vlan_event(struct if_entry *port, int event, int vid) 126 | { 127 | if (!port) 128 | return; 129 | assert(port->type == IF_MVRP); 130 | 131 | switch (event) { 132 | case MVRP_EV_JOININ: 133 | case MVRP_EV_JOINMT: 134 | case MVRP_EV_NEW: 135 | vlan_set(port->vlan_declared_remote, vid); 136 | vlan_unset(port->vlan_declared_remote_leave, vid); 137 | vlan_unset(port->vlan_declared_remote_leave2, vid); 138 | vlan_unset(port->vlan_declared_remote_leaveAll, vid); 139 | vlan_unset(port->vlan_declared_remote_leaveAll2, vid); 140 | break; 141 | case MVRP_EV_LV: 142 | if (!port->ptp) { 143 | vlan_set(port->vlan_declared_remote_leave, vid); 144 | if (vlan_test(port->vlan_declared_local, vid)) 145 | port->needSend = 1; 146 | break; 147 | } 148 | case MVRP_EV_IN: 149 | case MVRP_EV_MT: 150 | if (!port->ptp) 151 | break; 152 | vlan_unset(port->vlan_declared_remote, vid); 153 | /* Leave while timer for leaveAll was running, skip it as already done */ 154 | vlan_unset(port->vlan_declared_remote_leaveAll, vid); 155 | vlan_unset(port->vlan_declared_remote_leaveAll2, vid); 156 | } 157 | 158 | switch (event) { 159 | case MVRP_EV_JOININ: 160 | case MVRP_EV_IN: 161 | vlan_set(port->vlan_registered_remote, vid); 162 | break; 163 | case MVRP_EV_LV: 164 | if (!port->ptp) 165 | break; 166 | case MVRP_EV_JOINMT: 167 | case MVRP_EV_MT: 168 | case MVRP_EV_NEW: 169 | vlan_unset(port->vlan_registered_remote, vid); 170 | break; 171 | } 172 | } 173 | 174 | static void 175 | mvrp_parse_event(struct if_entry *port, const int attrtype, const int attrlen, const unsigned char* firstval, int idx, int event) 176 | { 177 | if (attrtype != MVRP_VID_ATTR_TYPE) { 178 | eprintf(DEBUG_MVRP, "MVRP unknown attribute type %d", attrtype); 179 | return; /* invalid type, see IEE 802.1q-2018 11.2.3.1.6 */ 180 | } 181 | if (attrlen != MVRP_VID_ATTR_LEN) { 182 | eprintf(DEBUG_MVRP, "MVRP bad vid attribute len %d", attrlen); 183 | return; /* invalid length, see IEEE 802.1q-2018 11.2.3.1.8 */ 184 | } 185 | 186 | uint16_t vid; 187 | assert(attrlen == sizeof(vid)); 188 | memcpy(&vid, firstval, sizeof(vid)); 189 | vid = ntohs(vid) + idx; 190 | 191 | eprintf(DEBUG_MVRP, "MVRP: * trigger event %s(%d) vid %d", mvrp_event2str(event), event, vid); 192 | mvrp_handle_vlan_event(port, event, vid); 193 | } 194 | 195 | /* 196 | * returns the number of bytes consumed or 197 | * zero for non-recoverable error 198 | * 199 | * note: endmark has length 2, all others length > 2 200 | */ 201 | static size_t 202 | mvrp_parse_vecattr(struct if_entry *port, const int attrtype, const int attrlen, const unsigned char* msgbuf, size_t bytes, int *leaveAllDone) 203 | { 204 | const struct mrpdu_vectorattrib *mrpdu_vec = NULL; 205 | size_t consumed = 0; 206 | 207 | /* test for endmark */ 208 | if (bytes >= 2 && 209 | msgbuf[0] == 0x00 && 210 | msgbuf[1] == 0x00) { 211 | return 2; 212 | } 213 | 214 | if (bytes < sizeof(*mrpdu_vec)) { 215 | eprintf(DEBUG_MVRP, "MVRP vector header too short"); 216 | return 0; 217 | } 218 | 219 | mrpdu_vec = (struct mrpdu_vectorattrib *) msgbuf; 220 | consumed += sizeof(*mrpdu_vec); 221 | 222 | eprintf(DEBUG_MVRP, "vector header = %04hx at %p", mrpdu_vec->VectorHeader, mrpdu_vec); 223 | const int leaveAllEvent = (ntohs(mrpdu_vec->VectorHeader) / 8192 == 1); 224 | const int numOfValues = ntohs(mrpdu_vec->VectorHeader) % 8192; 225 | 226 | const unsigned char* firstValue = msgbuf + consumed; // attrlen bytes 227 | consumed += attrlen; 228 | 229 | if (isdebug(DEBUG_MVRP)) { 230 | char buf[4096]; 231 | char *ptr = buf; 232 | 233 | for (int i = 0; i < attrlen && ptr < buf + sizeof(buf); i++) 234 | ptr += snprintf(ptr, buf + sizeof(buf) - ptr, "%s%02hhx", (i > 0 ? ":" : ""), firstValue[i]); 235 | ptr[0] = '\0'; 236 | 237 | eprintf(DEBUG_MVRP, "MVRP: * vector leaveAllEvent=%d numOfValues=%d firstValue=%s%s", leaveAllEvent, numOfValues, (attrlen == 1 ? "0x" : ""), buf); 238 | } 239 | 240 | const unsigned char *vector = msgbuf + consumed; 241 | 242 | if (leaveAllEvent == 0x1 && !(*leaveAllDone)) { 243 | mvrp_handle_leaveall(port); 244 | *leaveAllDone = 1; 245 | } 246 | 247 | // vector can be either fourpackedevents or threepackedevents 248 | // MVRP only used threepackedevents 249 | int attridx = 0; 250 | int numOfValuesRemaining = numOfValues; 251 | while (numOfValuesRemaining > 0 && bytes > consumed) { 252 | int event; 253 | uint8_t val = *vector; 254 | consumed++; 255 | vector++; 256 | 257 | event = (val / 36) % 6; 258 | mvrp_parse_event(port, attrtype, attrlen, firstValue, attridx, event); 259 | attridx++; 260 | numOfValuesRemaining--; 261 | 262 | if (numOfValuesRemaining == 0) 263 | break; 264 | 265 | event = (val / 6) % 6; 266 | mvrp_parse_event(port, attrtype, attrlen, firstValue, attridx, event); 267 | attridx++; 268 | numOfValuesRemaining--; 269 | 270 | if (numOfValuesRemaining == 0) 271 | break; 272 | 273 | event = (val / 1) % 6; 274 | mvrp_parse_event(port, attrtype, attrlen, firstValue, attridx, event); 275 | attridx++; 276 | numOfValuesRemaining--; 277 | } 278 | 279 | if (numOfValuesRemaining > 0) { 280 | eprintf(DEBUG_MVRP, "MVRP pdu too short for vector numOfValues=%d remaining=%d vectorBytes=%zd", numOfValues, numOfValuesRemaining, msgbuf + consumed - vector); 281 | return 0; // numOfValues bigger than message length provides 282 | } 283 | 284 | return consumed; 285 | } 286 | 287 | /* 288 | * returns the number of bytes consumed or 289 | * zero for non-recoverable error 290 | * 291 | * note: endmark has length 2, all others length > 2 292 | */ 293 | static size_t 294 | mvrp_parse_msg(struct if_entry *port, const unsigned char *msgbuf, size_t bytes) 295 | { 296 | const struct mrpdu_message *mrpdu_msg = NULL; 297 | size_t consumed = 0; 298 | int leaveAllDone = 0; 299 | 300 | /* test for endmark */ 301 | if (bytes >= 2 && 302 | msgbuf[0] == 0x00 && 303 | msgbuf[1] == 0x00) { 304 | return 2; 305 | } 306 | 307 | if (bytes < sizeof(*mrpdu_msg)) { 308 | eprintf(DEBUG_MVRP, "MVRP message too short"); 309 | return 0; 310 | } 311 | 312 | mrpdu_msg = (struct mrpdu_message *) msgbuf; 313 | consumed += sizeof(*mrpdu_msg); 314 | 315 | const int attrtype = mrpdu_msg->AttributeType; 316 | const int attrlen = mrpdu_msg->AttributeLength; 317 | 318 | eprintf(DEBUG_MVRP, "MVRP: * attrtype=%d attrlen=%d", attrtype, attrlen); 319 | while (consumed < bytes) { 320 | eprintf(DEBUG_MVRP, "MVRP: parse another vector chunk with %zd bytes left", bytes - consumed); 321 | size_t rc = mvrp_parse_vecattr(port, attrtype, attrlen, msgbuf + consumed, bytes - consumed, &leaveAllDone); 322 | consumed += rc; 323 | if (rc == 0) 324 | return 0; 325 | if (rc == 2) 326 | break; // endmark 327 | } 328 | 329 | return consumed; 330 | } 331 | 332 | static int 333 | mvrp_parse_pdu(struct if_entry *port, const unsigned char *msgbuf, size_t bytes) 334 | { 335 | const struct mrpdu *mrpdu; 336 | const unsigned char *mrpdu_msg_ptr; 337 | const unsigned char *mrpdu_msg_eof; 338 | 339 | if (bytes < sizeof(*mrpdu)) 340 | return -1; 341 | 342 | mrpdu = (struct mrpdu *) msgbuf; 343 | 344 | /* 345 | * This is the first version of the protocol. 346 | * Wenn shall parse older versions as well if supported, and can parse 347 | * never versions accoring to our version specification. 348 | */ 349 | 350 | eprintf(DEBUG_MVRP, "MVRP: protocol version %hhd", mrpdu->ProtocolVersion); 351 | if (mrpdu->ProtocolVersion != MVRP_PROTO_VERSION) 352 | eprintf(DEBUG_MVRP, "MVRP: different protocol version %hhd != %hhd", mrpdu->ProtocolVersion, MVRP_PROTO_VERSION); 353 | 354 | mrpdu_msg_ptr = msgbuf + sizeof(*mrpdu); 355 | mrpdu_msg_eof = msgbuf + bytes; 356 | 357 | while (mrpdu_msg_ptr < mrpdu_msg_eof) { 358 | eprintf(DEBUG_MVRP, "MVRP: parse another msg chunk with %zd bytes left", mrpdu_msg_eof - mrpdu_msg_ptr); 359 | size_t rc = mvrp_parse_msg(port, mrpdu_msg_ptr, mrpdu_msg_eof - mrpdu_msg_ptr); 360 | mrpdu_msg_ptr += rc; 361 | if (rc == 0) 362 | return -1; 363 | if (rc == 2) /* end mark encountered */ 364 | break; 365 | } 366 | 367 | assert(mrpdu_msg_ptr <= mrpdu_msg_eof); 368 | 369 | if (mrpdu_msg_ptr < mrpdu_msg_eof) 370 | eprintf(DEBUG_MVRP, "MVRP got junk at the end: %zd bytes left over", (mrpdu_msg_eof - mrpdu_msg_ptr)); 371 | 372 | return 0; 373 | } 374 | 375 | static void 376 | mvrp_got_packet(const int ptype, const unsigned char *packet, const int len, const char* ifname, const int ifindex) 377 | { 378 | if (ptype != ETH_P_MVRP) { 379 | eprintf(DEBUG_MVRP, "packet is not MVRP"); 380 | return; 381 | } 382 | 383 | struct if_entry *port = port_get_by_ifidx(ifindex); 384 | if (!port || port->type != IF_MVRP) { 385 | eprintf(DEBUG_MVRP, "port %s(%d) not listening, maybe due to NFLOG", ifname, ifindex); 386 | return; 387 | } 388 | 389 | eprintf(DEBUG_MVRP, "receive on port %s(%d) [type=%d]", port->ifname, port->ifidx, port->type); 390 | int rc = mvrp_parse_pdu(port, packet, len); 391 | if (rc < 0) 392 | eprintf(DEBUG_ERROR, "MVRP: bad packet ignored"); 393 | eprintf(DEBUG_MVRP, "MVRP ACK processing finished"); 394 | 395 | if (port->needSend) 396 | _mvrp_send(port, 0, 1); 397 | 398 | port_vlan_changed(); 399 | } 400 | 401 | static size_t 402 | mvrp_build_endmark(unsigned char *msgbuf, size_t bytes) 403 | { 404 | if (bytes < 2) 405 | return 0; 406 | memset(msgbuf, 0, 2); 407 | return 2; 408 | } 409 | 410 | static void 411 | mvrp_write_vec_header(struct mrpdu_vectorattrib *mrpdu_vec, int leaveAll, int numOfValues) 412 | { 413 | uint16_t val = (numOfValues % 8192); 414 | if (leaveAll) 415 | val += 8192; 416 | mrpdu_vec->VectorHeader = htons(val); 417 | } 418 | 419 | static size_t 420 | mvrp_build_msg(struct if_entry *port, int leaveAll, unsigned char *msgbuf, size_t bytes, struct mvrp_build_state *ret) 421 | { 422 | struct mrpdu_message *mrpdu_msg = NULL; 423 | struct mrpdu_vectorattrib *mrpdu_vec = NULL; 424 | size_t len = 0; 425 | int numOfValues = 0; 426 | void *firstValue = NULL; 427 | 428 | if (bytes < sizeof(*mrpdu_msg)) 429 | return 0; 430 | 431 | eprintf(DEBUG_MVRP, "add mrpdu_message header at 0"); 432 | mrpdu_msg = (struct mrpdu_message *) msgbuf; 433 | len += sizeof(*mrpdu_msg); 434 | 435 | mrpdu_msg->AttributeType = MVRP_VID_ATTR_TYPE; 436 | mrpdu_msg->AttributeLength = MVRP_VID_ATTR_LEN; 437 | 438 | if (bytes - len < sizeof(*mrpdu_vec)) { 439 | eprintf(DEBUG_MVRP, "MVRP vector header too short"); 440 | return 0; 441 | } 442 | 443 | eprintf(DEBUG_MVRP, "add mrpdu_vector header at %zu", len); 444 | mrpdu_vec = (struct mrpdu_vectorattrib *) (msgbuf + len); 445 | len += sizeof(*mrpdu_vec); 446 | numOfValues = 0; 447 | eprintf(DEBUG_MVRP, " update vector header at %zu with leaveAdd=%d numOfValues=%d", (((size_t) mrpdu_vec) - ((size_t) msgbuf)), leaveAll, numOfValues); 448 | mvrp_write_vec_header(mrpdu_vec, leaveAll, numOfValues); 449 | 450 | eprintf(DEBUG_MVRP, "add firstvalue at %zu", len); 451 | firstValue = (void*) (msgbuf + len); 452 | len += 2; 453 | 454 | ret->changes = ret->changes || leaveAll; 455 | ret->notempty = ret->notempty || leaveAll; 456 | 457 | int itdo = 0, itdn = 0, itro = 0, itrn = 0, itrd = 0, itrr = 0; 458 | /* I) report JOIN / JOININ 459 | * viddn: vlan id declared local new 460 | * vidrn: vlan id registered local new 461 | * II) report LEAVE / MT 462 | * viddo: vlan id declared local old 463 | * vidro: vlan id registered local old 464 | * III) fix inconsistent state for vlan ids remote is still interested in 465 | * virrd: vlan id remote declared 466 | * virrr: vlan id remote registered 467 | */ 468 | uint16_t viddo = 0, viddn = 0, vidro = 0, vidrn = 0, vidrd = 0, vidrr = 0; 469 | uint16_t vid = MIN(viddo, viddn, vidro, vidrn, vidrd, vidrr); 470 | uint16_t lastvid = 0; 471 | uint8_t *vecitem = NULL; // make compiler quiet by initializing to NULL 472 | 473 | while (vid != 0xffff) { 474 | while (viddo <= vid) 475 | vlan_next(port->vlan_declared_local_lastSend, &itdo, &viddo); 476 | while (viddn <= vid) 477 | vlan_next(port->vlan_declared_local, &itdn, &viddn); 478 | while (vidro <= vid) 479 | vlan_next(port->vlan_registered_local_lastSend, &itro, &vidro); 480 | while (vidrn <= vid) 481 | vlan_next(port->vlan_registered_local, &itrn, &vidrn); 482 | while (vidrd <= vid) 483 | vlan_next(port->vlan_declared_remote, &itrd, &vidrd); 484 | while (vidrr <= vid) 485 | vlan_next(port->vlan_registered_remote, &itrr, &vidrr); 486 | 487 | vid = MIN(viddo, viddn, vidro, vidrn, vidrd, vidrr); 488 | if (vid == 0xffff) 489 | break; 490 | eprintf(DEBUG_MVRP, "add vid %hu, declaration: old=%d new=%d, registration: old=%d, new=%d", 491 | vid, (viddo == vid), (viddn == vid), (vidro == vid), (vidrn == vid)); 492 | 493 | if (lastvid == 0) { 494 | uint16_t tmp = htons(vid); 495 | memcpy(firstValue, &tmp, sizeof(tmp)); 496 | lastvid = vid - 1; 497 | } else if (vid - lastvid > 12) { 498 | /* each unneccessarely added VLAN adds 8/3 bits 499 | * an extra vector header costs 16 bit + 16 bit firstval 500 | * so after 32 / (8/3) = 12 unset vlans -> use new vector */ 501 | /* create a new vector header */ 502 | eprintf(DEBUG_MVRP, "add mrpdu_vector header at %zu", len); 503 | mrpdu_vec = (struct mrpdu_vectorattrib *) (msgbuf + len); 504 | len += sizeof(*mrpdu_vec); 505 | if (len > bytes) 506 | return 0; 507 | 508 | numOfValues = 0; 509 | eprintf(DEBUG_MVRP, " update vector header at %zu with leaveAdd=%d numOfValues=%d", (((size_t)mrpdu_vec) - ((size_t)msgbuf)), leaveAll, numOfValues); 510 | mvrp_write_vec_header(mrpdu_vec, leaveAll, numOfValues); 511 | 512 | eprintf(DEBUG_MVRP, "add firstvalue at %zu", len); 513 | firstValue = (void*) (msgbuf + len); 514 | len += 2; 515 | if (len > bytes) 516 | return 0; 517 | uint16_t tmp = htons(vid); 518 | memcpy(firstValue, &tmp, sizeof(tmp)); 519 | lastvid = vid - 1; 520 | } 521 | 522 | ret->notempty = 1; 523 | 524 | while (lastvid < vid) { 525 | lastvid++; 526 | 527 | int event; 528 | if (lastvid == vid) { 529 | if (viddo == vid && viddn != vid) 530 | event = MVRP_EV_LV; // was declared but not longer is 531 | else if (viddn == vid && vidrn == vid) 532 | event = MVRP_EV_JOININ; // declared and registered 533 | else if (viddn == vid && vidrn != vid) 534 | event = MVRP_EV_JOINMT; // declared but not registered 535 | else if (viddn != vid && vidrn == vid) 536 | event = MVRP_EV_IN; // not declared but registered 537 | else if (viddn != vid && vidrn != vid) 538 | event = MVRP_EV_MT; // not declared and not registered 539 | else { 540 | eprintf(DEBUG_ERROR, "ups viddo=%d viddn=%d vidro=%d vidrn=%d vid=%d", viddo,viddn, vidro, vidrn, vid); 541 | event = MVRP_EV_MT; 542 | } 543 | ret->changes = ret->changes || (viddn == vid && viddo != vid); // declaration changed 544 | ret->changes = ret->changes || (viddn != vid && viddo == vid); // declaration changed 545 | ret->changes = ret->changes || (vidrn == vid && vidro != vid); // registration changed 546 | ret->changes = ret->changes || (vidrn != vid && vidro == vid); // registration changed 547 | } else { 548 | // lastvid is in no list, so neither declared nor registered 549 | event = MVRP_EV_MT; 550 | } 551 | 552 | switch (numOfValues % 3) { 553 | case 0: 554 | eprintf(DEBUG_MVRP, " move vecitem to %zu", len); 555 | vecitem = msgbuf + len; 556 | len++; 557 | if (len > bytes) 558 | return 0; 559 | *vecitem += 36 * event; 560 | break; 561 | case 1: 562 | assert(vecitem); 563 | *vecitem += 6 * event; 564 | break; 565 | case 2: 566 | assert(vecitem); 567 | *vecitem += 1 * event; 568 | break; 569 | } 570 | 571 | eprintf(DEBUG_MVRP, " wrote event for %hu at %zu = %zu", lastvid, (vecitem - msgbuf), len-1); 572 | numOfValues++; 573 | } 574 | 575 | eprintf(DEBUG_MVRP, " update vector header at %zu with leaveAdd=%d numOfValues=%d", (((size_t)mrpdu_vec) - ((size_t)msgbuf)), leaveAll, numOfValues); 576 | mvrp_write_vec_header(mrpdu_vec, leaveAll, numOfValues); 577 | } 578 | 579 | size_t rc = mvrp_build_endmark(msgbuf + len, bytes - len); 580 | if (rc == 0) { 581 | eprintf(DEBUG_ERROR, "failed to add endmark to end of MVRP list of vector"); 582 | return 0; 583 | } 584 | len += rc; 585 | 586 | return len; 587 | } 588 | 589 | static size_t 590 | mvrp_build_pdu(struct if_entry *port, int leaveAll, unsigned char *msgbuf, size_t bytes, struct mvrp_build_state *ret) 591 | { 592 | struct mrpdu *mrpdu; 593 | size_t len = 0, rc; 594 | 595 | if (bytes < sizeof(*mrpdu)) 596 | return -1; 597 | 598 | mrpdu = (struct mrpdu *) msgbuf; 599 | len += sizeof(*mrpdu); 600 | 601 | mrpdu->ProtocolVersion = MVRP_PROTO_VERSION; 602 | 603 | rc = mvrp_build_msg(port, leaveAll, msgbuf + len, bytes - len, ret); 604 | if (rc == 0) 605 | return 0; 606 | len += rc; 607 | 608 | rc = mvrp_build_endmark(msgbuf + len, bytes - len); 609 | if (rc == 0) 610 | eprintf(DEBUG_ERROR, "failed to add endmark to end of MVRP message"); 611 | len += rc; 612 | 613 | return len; 614 | } 615 | 616 | void 617 | mvrp_send(struct if_entry *port) 618 | { 619 | _mvrp_send(port, 0, 0); 620 | } 621 | 622 | static void 623 | _mvrp_send(struct if_entry *port, int leaveAll, int force) 624 | { 625 | unsigned char packet[ETH_FRAME_LEN - sizeof(struct ether_header)]; 626 | memset(packet, 0, sizeof(packet)); 627 | 628 | eprintf(DEBUG_MVRP, "send on port %s(%d) [type=%d] leaveAll=%d force=%d", port->ifname, port->ifidx, port->type, leaveAll, force); 629 | 630 | struct mvrp_build_state ret = { }; 631 | size_t len = mvrp_build_pdu(port, leaveAll, packet, sizeof(packet), &ret); 632 | if (len == 0) { 633 | eprintf(DEBUG_ERROR, "MVRP: failed to build packet"); 634 | return; 635 | } 636 | if (!ret.notempty) { 637 | eprintf(DEBUG_VERBOSE, "MVRP: failed to build packet - nothing to send on %s(%d)", port->ifname, port->ifidx); 638 | return; 639 | } 640 | if (vlan_compare(port->vlan_registered_local, port->vlan_registered_remote)) { 641 | char buf[4096]; 642 | int rc; 643 | eprintf(DEBUG_VERBOSE, "inconsistent state for registered vlans on local and remote side %s(%d)", port->ifname, port->ifidx); 644 | rc = (sizeof(buf) == vlan_dump(port->vlan_registered_local, buf, sizeof(buf))); 645 | eprintf(DEBUG_VERBOSE, " * local registered vlans %s%s", buf, rc ? "...":""); 646 | rc = (sizeof(buf) == vlan_dump(port->vlan_registered_remote, buf, sizeof(buf))); 647 | eprintf(DEBUG_VERBOSE, " * remote registered vlans %s%s", buf, rc ? "...":""); 648 | 649 | force = 1; // force sending 650 | } 651 | if (!force && !ret.changes) { 652 | eprintf(DEBUG_MVRP, "skip sending packet as nothing changed on port %s(%d)", port->ifname, port->ifidx); 653 | return; 654 | } 655 | if (isdebug(DEBUG_MVRP)) { 656 | char buf[4096]; 657 | int rc; 658 | rc = (sizeof(buf) == vlan_dump(port->vlan_declared_local, buf, sizeof(buf))); 659 | eprintf(DEBUG_MVRP, "send packet declaring vlans %s%s", buf, rc ? "...":""); 660 | rc = (sizeof(buf) == vlan_dump(port->vlan_registered_local, buf, sizeof(buf))); 661 | eprintf(DEBUG_MVRP, "send packet registered vlans %s%s", buf, rc ? "...":""); 662 | rc = (sizeof(buf) == vlan_dump(port->vlan_declared_local_lastSend, buf, sizeof(buf))); 663 | eprintf(DEBUG_MVRP, "send packet declaring vlans lastSend %s%s", buf, rc ? "...":""); 664 | rc = (sizeof(buf) == vlan_dump(port->vlan_registered_local_lastSend, buf, sizeof(buf))); 665 | eprintf(DEBUG_MVRP, "send packet registered vlans lastSend %s%s", buf, rc ? "...":""); 666 | 667 | eprintf(DEBUG_MVRP, "send packet (start)"); 668 | rc = mvrp_parse_pdu(NULL, packet, len); 669 | eprintf(DEBUG_MVRP, "send packet (end)"); 670 | if (rc < 0) { 671 | eprintf(DEBUG_ERROR, "MVRP: bad packet generated"); 672 | } 673 | } 674 | ether_send(port->sock, NULL, packet, len); 675 | 676 | // record changes 677 | vlan_free(port->vlan_registered_local_lastSend); 678 | port->vlan_registered_local_lastSend = vlan_clone(port->vlan_registered_local, "port->vrlS"); 679 | 680 | vlan_free(port->vlan_declared_local_lastSend); 681 | port->vlan_declared_local_lastSend = vlan_clone(port->vlan_declared_local, "port->vdllS"); 682 | 683 | port->needSend = 0; 684 | struct timespec tv; 685 | clock_gettime(CLOCK_MONOTONIC, &tv); 686 | port->lastSent = tv.tv_sec; 687 | } 688 | 689 | struct ether_socket * 690 | mvrp_listen(int if_index, const char *if_name, const char *if_mac) 691 | { 692 | return ether_listen(if_index, if_name, if_mac, ETH_P_MVRP, mvrp_addr); 693 | } 694 | 695 | void 696 | mvrp_close(struct ether_socket *sock) 697 | { 698 | return ether_close(sock); 699 | } 700 | 701 | static void 702 | mvrp_timer_leave_cb(struct if_entry *port, void *ctx) 703 | { 704 | struct timespec *now = ctx; 705 | if (port->type != IF_MVRP) 706 | return; 707 | if (port->lastLeaveTimer + leaveTimeout > now->tv_sec) 708 | return; 709 | port->lastLeaveTimer = now->tv_sec; 710 | 711 | if (isdebug(DEBUG_MVRP) && vlan_notempty(port->vlan_declared_remote_leave2)) { 712 | char buf[4096]; 713 | int rc; 714 | rc = (sizeof(buf) == vlan_dump(port->vlan_declared_remote_leave2, buf, sizeof(buf))); 715 | eprintf(DEBUG_MVRP, "discard remote vlans due to leaveTimer timing out for vlans %s%s", buf, rc ? "...":""); 716 | } 717 | 718 | int it = 0; 719 | uint16_t vid = 0; 720 | while (vlan_next(port->vlan_declared_remote_leave2, &it, &vid) == 0) { 721 | vlan_unset(port->vlan_declared_remote, vid); 722 | vlan_unset(port->vlan_registered_remote, vid); 723 | /* leave already done, no need to repeat in leaveAll timer */ 724 | vlan_unset(port->vlan_declared_remote_leaveAll, vid); 725 | vlan_unset(port->vlan_declared_remote_leaveAll2, vid); 726 | } 727 | vlan_free(port->vlan_declared_remote_leave2); 728 | port->vlan_declared_remote_leave2 = port->vlan_declared_remote_leave; 729 | port->vlan_declared_remote_leave = vlan_alloc("port->vdrl"); 730 | } 731 | 732 | static void 733 | mvrp_timer_leaveAll_leave_cb(struct if_entry *port, void *ctx) 734 | { 735 | struct timespec *now = ctx; 736 | if (port->type != IF_MVRP) 737 | return; 738 | if (port->lastLeaveAllLeaveTimer + leaveAllLeaveTimeout > now->tv_sec) 739 | return; 740 | port->lastLeaveAllLeaveTimer = now->tv_sec; 741 | 742 | if (isdebug(DEBUG_MVRP) && vlan_notempty(port->vlan_declared_remote_leaveAll2)) { 743 | char buf[4096]; 744 | int rc; 745 | rc = (sizeof(buf) == vlan_dump(port->vlan_declared_remote_leaveAll2, buf, sizeof(buf))); 746 | eprintf(DEBUG_MVRP, "discard remote vlans due to leaveAllLeaveTimer timing out for vlans %s%s", buf, rc ? "...":""); 747 | } 748 | 749 | int it = 0; 750 | uint16_t vid = 0; 751 | while (vlan_next(port->vlan_declared_remote_leaveAll2, &it, &vid) == 0) { 752 | vlan_unset(port->vlan_declared_remote, vid); 753 | vlan_unset(port->vlan_registered_remote, vid); 754 | /* leave already done, no need to repeat in leave timer */ 755 | vlan_unset(port->vlan_declared_remote_leave, vid); 756 | vlan_unset(port->vlan_declared_remote_leave2, vid); 757 | } 758 | vlan_free(port->vlan_declared_remote_leaveAll2); 759 | port->vlan_declared_remote_leaveAll2 = port->vlan_declared_remote_leaveAll; 760 | port->vlan_declared_remote_leaveAll = vlan_alloc("port->vdrla"); 761 | } 762 | 763 | static void 764 | mvrp_timer_leaveAllSend_cb(struct if_entry *port, void *ctx) 765 | { 766 | struct timespec *now = ctx; 767 | int gracePeriod = 0; 768 | 769 | if (port->type != IF_MVRP) 770 | return; 771 | if (!port->lastLeaveAllFromMe) 772 | gracePeriod += (gracePeriodForRemoteLeaveAll / 2); 773 | gracePeriod += getrandom(gracePeriodForRemoteLeaveAll / 2); 774 | 775 | if (port->lastLeaveAll + leaveAllInterval + gracePeriod > now->tv_sec) 776 | return; 777 | port->lastLeaveAll = now->tv_sec; 778 | port->lastLeaveAllFromMe = 1; 779 | eprintf(DEBUG_VERBOSE, "send periodic leaveAll on port %s(%d) [type=%d]", port->ifname, port->ifidx, port->type); 780 | mvrp_do_leaveall(port); 781 | _mvrp_send(port, 1, 1); 782 | } 783 | 784 | static void 785 | mvrp_timer_periodic_send_cb(struct if_entry *port, void *ctx) 786 | { 787 | struct timespec *now = ctx; 788 | if (port->type != IF_MVRP) 789 | return; 790 | if (port->lastSent + periodicSendInterval > now->tv_sec && 791 | !port->needSend) 792 | return; 793 | _mvrp_send(port, 0, 1); 794 | } 795 | 796 | static void 797 | mvrp_timer(void *ctx) 798 | { 799 | struct timespec tv; 800 | clock_gettime(CLOCK_MONOTONIC, &tv); 801 | for_each_port(mvrp_timer_leave_cb, &tv); 802 | 803 | for_each_port(mvrp_timer_leaveAll_leave_cb, &tv); 804 | 805 | for_each_port(mvrp_timer_leaveAllSend_cb, &tv); 806 | 807 | port_vlan_changed(); 808 | 809 | for_each_port(mvrp_timer_periodic_send_cb, &tv); 810 | } 811 | 812 | static __attribute__((constructor)) void mvrp_init() 813 | { 814 | cb_add_packet_cb(mvrp_got_packet); 815 | cb_add_timer(1, 1, NULL, mvrp_timer); 816 | } 817 | 818 | -------------------------------------------------------------------------------- /src/mvrp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of mvrpd. 3 | * 4 | * mvrpd is free software: you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * mvrpd is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with mvrpd. 16 | * If not, see . 17 | * 18 | * (C) 2019, Michael Braun 19 | */ 20 | #ifndef MVRPD_MVRP 21 | #define MVRPD_MVRP 22 | 23 | struct ether_socket; 24 | struct if_entry; 25 | 26 | struct ether_socket *mvrp_listen(int if_index, const char *if_name, const char *if_mac); 27 | void mvrp_close(struct ether_socket *sock); 28 | void mvrp_send(struct if_entry *port); 29 | 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /src/port.c: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of mvrpd. 3 | * 4 | * mvrpd is free software: you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * mvrpd is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with mvrpd. 16 | * If not, see . 17 | * 18 | * (C) 2019, Michael Braun 19 | */ 20 | 21 | #include "config.h" 22 | #include "port.h" 23 | #include "signal.h" 24 | #include "debug.h" 25 | #include "event.h" 26 | #include "timer.h" 27 | #include "mvrp.h" 28 | #include "vlan.h" 29 | #include "bridge.h" 30 | #include "cmdline.h" 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | 45 | 46 | static struct if_entry* ifHead = NULL; 47 | static struct vlan_arr *ignVlan = NULL; 48 | static unsigned char restrictToEp = 0; 49 | 50 | void 51 | for_each_port(void (*cb) (struct if_entry *port, void *ctx), void *ctx) 52 | { 53 | struct if_entry* entry; 54 | for (entry = ifHead; entry; entry = entry->next) { 55 | cb(entry, ctx); 56 | } 57 | } 58 | 59 | static struct if_entry * 60 | get_if(const int ifidx, struct if_entry **prev) 61 | { 62 | struct if_entry* entry; 63 | if (prev) 64 | *prev = NULL; 65 | for (entry = ifHead; entry; entry = entry->next) { 66 | if (entry->ifidx == ifidx) 67 | break; 68 | if (prev) 69 | *prev = entry; 70 | } 71 | return entry; 72 | } 73 | 74 | struct if_entry * 75 | port_get_by_ifidx(int ifidx) 76 | { 77 | return get_if(ifidx, NULL); 78 | } 79 | 80 | static struct if_entry * 81 | add_if(const int ifidx) 82 | { 83 | struct if_entry* entry = malloc(sizeof(*entry)); 84 | if (!entry) { 85 | eprintf(DEBUG_ERROR, "out of memory at %s:%d in %s", __FILE__, __LINE__, __PRETTY_FUNCTION__); 86 | return NULL; 87 | } 88 | memset(entry, 0, sizeof(*entry)); 89 | entry->ifidx = ifidx; 90 | entry->next = ifHead; 91 | entry->vlan_state = vlan_alloc("port->vr"); 92 | 93 | assert(!entry->vlan_to_add_last_print); 94 | entry->vlan_to_add_last_print = vlan_alloc("port->vtalp"); 95 | assert(entry->vlan_to_add_last_print); 96 | 97 | ifHead = entry; 98 | return entry; 99 | } 100 | 101 | static void 102 | conf_uplink(struct if_entry *entry) 103 | { 104 | assert(!entry->sock); 105 | entry->sock = mvrp_listen(entry->ifidx, entry->ifname, entry->mac); 106 | assert(entry->sock); 107 | 108 | assert(!entry->vlan_registered_local); 109 | entry->vlan_registered_local = vlan_alloc("port->vs"); 110 | assert(entry->vlan_registered_local); 111 | 112 | assert(!entry->vlan_registered_local_last_print); 113 | entry->vlan_registered_local_last_print = vlan_alloc("port->vslp"); 114 | assert(entry->vlan_registered_local_last_print); 115 | 116 | assert(!entry->vlan_registered_local_lastSend); 117 | entry->vlan_registered_local_lastSend = vlan_alloc("port->vrlS"); 118 | assert(entry->vlan_registered_local_lastSend); 119 | 120 | assert(!entry->vlan_registered_remote); 121 | entry->vlan_registered_remote = vlan_alloc("port->vrr"); 122 | assert(entry->vlan_registered_remote); 123 | 124 | assert(!entry->vlan_declared_remote); 125 | entry->vlan_declared_remote = vlan_alloc("port->vdr"); 126 | assert(entry->vlan_declared_remote); 127 | 128 | assert(!entry->vlan_declared_remote_leave); 129 | entry->vlan_declared_remote_leave = vlan_alloc("port->vdrl"); 130 | assert(entry->vlan_declared_remote_leave); 131 | 132 | assert(!entry->vlan_declared_remote_leave2); 133 | entry->vlan_declared_remote_leave2 = vlan_alloc("port->vdrl2"); 134 | assert(entry->vlan_declared_remote_leave2); 135 | 136 | assert(!entry->vlan_declared_remote_leaveAll); 137 | entry->vlan_declared_remote_leaveAll = vlan_alloc("port->vdrla"); 138 | assert(entry->vlan_declared_remote_leaveAll); 139 | 140 | assert(!entry->vlan_declared_remote_leaveAll2); 141 | entry->vlan_declared_remote_leaveAll2 = vlan_alloc("port->vdrla2"); 142 | assert(entry->vlan_declared_remote_leaveAll2); 143 | 144 | assert(!entry->vlan_declared_local); 145 | entry->vlan_declared_local = vlan_alloc("port->vdl"); 146 | assert(entry->vlan_declared_local); 147 | 148 | assert(!entry->vlan_declared_local_last_print); 149 | entry->vlan_declared_local_last_print = vlan_alloc("port->vdllp"); 150 | assert(entry->vlan_declared_local_last_print); 151 | 152 | assert(!entry->vlan_declared_local_lastSend); 153 | entry->vlan_declared_local_lastSend = vlan_alloc("port->vdllS"); 154 | assert(entry->vlan_declared_local_lastSend); 155 | } 156 | 157 | static void 158 | deconf_uplink(struct if_entry *entry) 159 | { 160 | if (entry->sock) 161 | mvrp_close(entry->sock); 162 | entry->sock = NULL; 163 | 164 | vlan_free(entry->vlan_registered_local); 165 | entry->vlan_registered_local = NULL; 166 | 167 | vlan_free(entry->vlan_registered_local_last_print); 168 | entry->vlan_registered_local_last_print = NULL; 169 | 170 | vlan_free(entry->vlan_registered_local_lastSend); 171 | entry->vlan_registered_local_lastSend = NULL; 172 | 173 | vlan_free(entry->vlan_registered_remote); 174 | entry->vlan_registered_remote = NULL; 175 | 176 | vlan_free(entry->vlan_declared_remote); 177 | entry->vlan_declared_remote = NULL; 178 | 179 | vlan_free(entry->vlan_declared_remote_leave); 180 | entry->vlan_declared_remote_leave = NULL; 181 | 182 | vlan_free(entry->vlan_declared_remote_leave2); 183 | entry->vlan_declared_remote_leave2 = NULL; 184 | 185 | vlan_free(entry->vlan_declared_remote_leaveAll); 186 | entry->vlan_declared_remote_leaveAll = NULL; 187 | 188 | vlan_free(entry->vlan_declared_remote_leaveAll2); 189 | entry->vlan_declared_remote_leaveAll2 = NULL; 190 | 191 | vlan_free(entry->vlan_declared_local); 192 | entry->vlan_declared_local = NULL; 193 | 194 | vlan_free(entry->vlan_declared_local_last_print); 195 | entry->vlan_declared_local_last_print = NULL; 196 | 197 | vlan_free(entry->vlan_declared_local_lastSend); 198 | entry->vlan_declared_local_lastSend = NULL; 199 | } 200 | 201 | static void 202 | update_if(struct if_entry *entry, int type, const char *ifname, const char *mac, int ptp, struct vlan_arr *vlan) 203 | { 204 | if (mac) 205 | memcpy(entry->mac, mac, ETH_ALEN); 206 | else 207 | memset(entry->mac, 0, ETH_ALEN); 208 | entry->ptp = ptp; 209 | strncpy(entry->ifname, ifname, IFNAMSIZ-1); 210 | 211 | vlan_free(entry->vlan_state); 212 | entry->vlan_state = vlan_clone(vlan, "port->vr"); 213 | if (!entry->vlan_state) { 214 | eprintf(DEBUG_ERROR, "out of memory at %s:%d in %s", __FILE__, __LINE__, __PRETTY_FUNCTION__); 215 | exit(254); 216 | } 217 | 218 | if (isdebug(DEBUG_PORT)) { 219 | char vlans[4096]; 220 | int trunc = (sizeof(vlans) == vlan_dump(entry->vlan_state, vlans, sizeof(vlans))); 221 | eprintf(DEBUG_PORT, "ifidx: %d name: %s type:%d ptp:%d vlans: %s%s", entry->ifidx, entry->ifname, type,entry->ptp, vlans, (trunc ? "...":"")); 222 | } 223 | 224 | if (entry->type == type) 225 | return; 226 | if (entry->type == IF_MVRP) 227 | /* uplink aka mvrp */ 228 | deconf_uplink(entry); 229 | entry->type = type; 230 | if (entry->type == IF_MVRP) 231 | /* uplink aka mvrp */ 232 | conf_uplink(entry); 233 | } 234 | 235 | static void 236 | dump_if(int s) 237 | { 238 | struct if_entry* entry; 239 | char vlans[4096]; 240 | 241 | for (entry = ifHead; entry; entry = entry->next) { 242 | int trunc = (sizeof(vlans) == vlan_dump(entry->vlan_state, vlans, sizeof(vlans))); 243 | eprintf(DEBUG_PORT, "port: ifidx: %d name: %s type:%d ptp:%d vlans: %s%s", entry->ifidx, entry->ifname, entry->type, entry->ptp, vlans, (trunc ? "...":"")); 244 | } 245 | } 246 | 247 | static void 248 | port_configure_br_vlan(struct if_entry *entry, struct vlan_arr *vlan_register) { 249 | int it = 0, hasadd = 0, hasdel = 0; 250 | uint16_t vid = 0; 251 | struct vlan_arr *vlan_del = vlan_alloc("vlan-del"); 252 | struct vlan_arr *vlan_add = vlan_alloc("vlan-add"); 253 | 254 | while (vlan_next(vlan_register, &it, &vid) == 0) { 255 | if (vlan_test(ignVlan, vid)) 256 | continue; 257 | if (!vlan_test(entry->vlan_declared_remote, vid)) 258 | continue; 259 | if (vlan_test(entry->vlan_state, vid) && 260 | vlan_test(entry->vlan_registered_local, vid)) 261 | continue; 262 | vlan_set(entry->vlan_registered_local, vid); 263 | vlan_set(vlan_add, vid); 264 | hasadd = 1; 265 | } 266 | 267 | it = 0; 268 | vid = 0; 269 | while (vlan_next(entry->vlan_registered_local, &it, &vid) == 0) { 270 | if (vlan_test(ignVlan, vid)) 271 | continue; 272 | if (vlan_test(vlan_register, vid) && 273 | vlan_test(entry->vlan_declared_remote, vid)) 274 | continue; 275 | vlan_unset(entry->vlan_registered_local, vid); 276 | vlan_set(vlan_del, vid); 277 | hasdel = 1; 278 | } 279 | 280 | it = 0; 281 | vid = 0; 282 | while (vlan_next(entry->vlan_state, &it, &vid) == 0) { 283 | if (vlan_test(ignVlan, vid)) 284 | continue; 285 | if (vlan_test(vlan_register, vid) && 286 | vlan_test(entry->vlan_declared_remote, vid)) 287 | continue; 288 | vlan_unset(entry->vlan_registered_local, vid); // vlan_state is managed by NEWLINK messages on bridge! 289 | vlan_set(vlan_del, vid); 290 | hasdel = 1; 291 | } 292 | 293 | if (isdebug(DEBUG_PORT | DEBUG_VERBOSE) && hasdel) { 294 | char buf[4096]; 295 | int trunc = (sizeof(buf) == vlan_dump(vlan_del, buf, sizeof(buf))); 296 | eprintf(DEBUG_PORT | DEBUG_VERBOSE, "del vlans from port %s: %s%s", entry->ifname, buf, (trunc ? "..." : "")); 297 | } 298 | if (isdebug(DEBUG_PORT | DEBUG_VERBOSE) && hasadd) { 299 | char buf[4096]; 300 | int trunc = (sizeof(buf) == vlan_dump(vlan_add, buf, sizeof(buf))); 301 | eprintf(DEBUG_PORT | DEBUG_VERBOSE, "add vlans to port %s: %s%s", entry->ifname, buf, (trunc ? "..." : "")); 302 | } 303 | if (isdebug(DEBUG_PORT)) { 304 | char buf[4096]; 305 | int trunc; 306 | 307 | eprintf(DEBUG_PORT, "configure vlans on %s", entry->ifname); 308 | 309 | trunc = (sizeof(buf) == vlan_dump(vlan_add, buf, sizeof(buf))); 310 | eprintf(DEBUG_PORT, " * add vlan %s%s, hasadd=%d", buf, trunc ? "..." : "", hasadd); 311 | 312 | trunc = (sizeof(buf) == vlan_dump(vlan_del, buf, sizeof(buf))); 313 | eprintf(DEBUG_PORT, " * del vlan %s%s, hasdel=%d", buf, trunc ? "..." : "", hasdel); 314 | 315 | trunc = (sizeof(buf) == vlan_dump(entry->vlan_declared_remote, buf, sizeof(buf))); 316 | eprintf(DEBUG_PORT, " * declared_remote: %s%s", buf, trunc ? "..." : ""); 317 | 318 | trunc = (sizeof(buf) == vlan_dump(entry->vlan_registered_remote, buf, sizeof(buf))); 319 | eprintf(DEBUG_PORT, " * register remote: %s%s", buf, trunc ? "..." : ""); 320 | 321 | trunc = (sizeof(buf) == vlan_dump(entry->vlan_declared_local, buf, sizeof(buf))); 322 | eprintf(DEBUG_PORT, " * declared_locally: %s%s", buf, trunc ? "..." : ""); 323 | 324 | trunc = (sizeof(buf) == vlan_dump(entry->vlan_registered_local, buf, sizeof(buf))); 325 | eprintf(DEBUG_PORT, " * register locally: %s%s", buf, trunc ? "..." : ""); 326 | 327 | trunc = (sizeof(buf) == vlan_dump(entry->vlan_state, buf, sizeof(buf))); 328 | eprintf(DEBUG_PORT, " * local netif state: %s%s", buf, trunc ? "..." : ""); 329 | 330 | trunc = (sizeof(buf) == vlan_dump(vlan_register, buf, sizeof(buf))); 331 | eprintf(DEBUG_PORT, " * wanted in this run: %s%s", buf, trunc ? "..." : ""); 332 | 333 | trunc = (sizeof(buf) == vlan_dump(ignVlan, buf, sizeof(buf))); 334 | eprintf(DEBUG_PORT, " * ignore: %s%s", buf, trunc ? "..." : ""); 335 | } 336 | if (hasdel) 337 | br_vlan_del(entry->ifidx, vlan_del); 338 | if (hasadd) 339 | br_vlan_add(entry->ifidx, vlan_add); 340 | 341 | vlan_free(vlan_add); 342 | vlan_free(vlan_del); 343 | } 344 | 345 | static void 346 | port_recompute_timer(void *ctx) 347 | { 348 | struct if_entry* entry; 349 | // a vid is in both vlan_wanted* arrays iff at least two ports request it 350 | // if one port requests it, it is set in vlan_wanted0 351 | struct vlan_arr *vlan_wanted0 = vlan_alloc("vlan_wanted0"); 352 | struct vlan_arr *vlan_wanted1 = vlan_alloc("vlan_wanted1"); 353 | 354 | for (entry = ifHead; entry; entry = entry->next) { 355 | struct vlan_arr *vlan_to_add; 356 | if (entry->type == IF_MVRP && !restrictToEp) 357 | // uplink 358 | vlan_to_add = entry->vlan_declared_remote; 359 | else if (entry->type == IF_STATIC) 360 | // ep 361 | vlan_to_add = entry->vlan_state; 362 | else 363 | continue; 364 | 365 | if (isdebug(DEBUG_PORT) && vlan_to_add) { 366 | char vlans[4096]; 367 | int trunc = (sizeof(vlans) == vlan_dump(vlan_to_add, vlans, sizeof(vlans))); 368 | eprintf(DEBUG_PORT, "ifidx: %d name: %s type:%d ptp:%d vlans-to-add: %s%s", entry->ifidx, entry->ifname, entry->type,entry->ptp, vlans, (trunc ? "...":"")); 369 | } 370 | if (isdebug(DEBUG_VERBOSE) && 371 | vlan_compare(entry->vlan_to_add_last_print, vlan_to_add)) { 372 | char vlans[4096]; 373 | int trunc = (sizeof(vlans) == vlan_dump(vlan_to_add, vlans, sizeof(vlans))); 374 | eprintf(DEBUG_VERBOSE, "ifidx: %d name: %s type:%d ptp:%d vlans-to-add: %s%s", entry->ifidx, entry->ifname, entry->type,entry->ptp, vlans, (trunc ? "...":"")); 375 | vlan_free(entry->vlan_to_add_last_print); 376 | entry->vlan_to_add_last_print = vlan_clone(vlan_to_add,"port->vtalp"); 377 | } 378 | 379 | int it = 0; 380 | uint16_t vid = 0; 381 | while (vlan_next(vlan_to_add, &it, &vid) == 0) { 382 | if (vlan_test(ignVlan, vid)) 383 | continue; 384 | if (restrictToEp || 385 | vlan_set(vlan_wanted0, vid)) 386 | vlan_set(vlan_wanted1, vid); 387 | } 388 | } 389 | 390 | if (isdebug(DEBUG_PORT)) { 391 | char vlans[4096]; 392 | int trunc; 393 | trunc = (sizeof(vlans) == vlan_dump(vlan_wanted0, vlans, sizeof(vlans))); 394 | eprintf(DEBUG_PORT, "wanted0: %s%s", vlans, (trunc ? "...":"")); 395 | trunc = (sizeof(vlans) == vlan_dump(vlan_wanted1, vlans, sizeof(vlans))); 396 | eprintf(DEBUG_PORT, "wanted1: %s%s", vlans, (trunc ? "...":"")); 397 | } 398 | 399 | for (entry = ifHead; entry; entry = entry->next) { 400 | if (entry->type != IF_MVRP) 401 | continue; 402 | 403 | if (isdebug(DEBUG_VERBOSE) && restrictToEp && 404 | vlan_compare(entry->vlan_to_add_last_print, entry->vlan_declared_remote)) { 405 | char vlans[4096]; 406 | int trunc = (sizeof(vlans) == vlan_dump(entry->vlan_declared_remote, vlans, sizeof(vlans))); 407 | eprintf(DEBUG_VERBOSE, "ifidx: %d name: %s type:%d ptp:%d vlans declared remote: %s%s", entry->ifidx, entry->ifname, entry->type,entry->ptp, vlans, (trunc ? "...":"")); 408 | vlan_free(entry->vlan_to_add_last_print); 409 | entry->vlan_to_add_last_print = vlan_clone(entry->vlan_declared_remote,"port->vtalp"); 410 | } 411 | 412 | vlan_free(entry->vlan_declared_local); 413 | entry->vlan_declared_local = vlan_clone(vlan_wanted1, "port->vdl"); 414 | // declare locally: every vid on at least one other port 415 | // that is at least two ports OR at least one port but not this 416 | // iff restrictToEp -> wanted0 is empty 417 | int it = 0; 418 | uint16_t vid = 0; 419 | while (vlan_next(vlan_wanted0, &it, &vid) == 0) { 420 | if (vlan_test(entry->vlan_declared_remote, vid)) 421 | continue; 422 | vlan_set(entry->vlan_declared_local, vid); 423 | } 424 | 425 | port_configure_br_vlan(entry, vlan_wanted1); 426 | mvrp_send(entry); 427 | 428 | if (isdebug(DEBUG_VERBOSE) && 429 | vlan_compare(entry->vlan_declared_local_last_print, entry->vlan_declared_local)) { 430 | char vlans[4096]; 431 | int trunc = (sizeof(vlans) == vlan_dump(entry->vlan_declared_local, vlans, sizeof(vlans))); 432 | eprintf(DEBUG_VERBOSE, "ifidx: %d name: %s type:%d ptp:%d vlans-declared-local: %s%s", entry->ifidx, entry->ifname, entry->type,entry->ptp, vlans, (trunc ? "...":"")); 433 | vlan_free(entry->vlan_declared_local_last_print); 434 | entry->vlan_declared_local_last_print = vlan_clone(entry->vlan_declared_local,"port->vdllp"); 435 | } 436 | if (isdebug(DEBUG_VERBOSE) && 437 | vlan_compare(entry->vlan_registered_local_last_print, entry->vlan_registered_local)) { 438 | char vlans[4096]; 439 | int trunc = (sizeof(vlans) == vlan_dump(entry->vlan_registered_local, vlans, sizeof(vlans))); 440 | eprintf(DEBUG_VERBOSE, "ifidx: %d name: %s type:%d ptp:%d vlans-registered-local: %s%s", entry->ifidx, entry->ifname, entry->type,entry->ptp, vlans, (trunc ? "...":"")); 441 | vlan_free(entry->vlan_registered_local_last_print); 442 | entry->vlan_registered_local_last_print = vlan_clone(entry->vlan_registered_local,"port->vslp"); 443 | } 444 | } 445 | 446 | vlan_free(vlan_wanted0); 447 | vlan_free(vlan_wanted1); 448 | } 449 | 450 | void port_vlan_changed() 451 | { 452 | cb_del_timer(NULL, port_recompute_timer); 453 | cb_add_timer(0, 0, NULL, port_recompute_timer); 454 | } 455 | 456 | static void _port_free(struct if_entry *entry) 457 | { 458 | if (entry->type == IF_MVRP) 459 | /* uplink aka mvrp */ 460 | deconf_uplink(entry); 461 | 462 | vlan_free(entry->vlan_state); 463 | 464 | vlan_free(entry->vlan_to_add_last_print); 465 | 466 | memset(entry, 0, sizeof(*entry)); 467 | 468 | free(entry); 469 | } 470 | 471 | void port_del(int ifidx) 472 | { 473 | struct if_entry *prev; 474 | struct if_entry *entry = get_if(ifidx, &prev); 475 | if (!entry) 476 | return; 477 | if (prev) 478 | prev->next = entry->next; 479 | else 480 | ifHead = entry->next; 481 | 482 | _port_free(entry); 483 | } 484 | 485 | void port_del_all() 486 | { 487 | struct if_entry* entry = ifHead; 488 | while (entry) { 489 | struct if_entry* next = entry->next; 490 | _port_free(entry); 491 | entry = next; 492 | } 493 | ifHead = NULL; 494 | } 495 | 496 | void port_add(int type, int ifidx, const char *ifname, int ptp, struct vlan_arr *vlan, const char *mac) 497 | { 498 | struct if_entry *entry = get_if(ifidx, NULL); 499 | if (!entry) 500 | entry = add_if(ifidx); 501 | update_if(entry, type, ifname, mac, ptp, vlan); 502 | 503 | port_vlan_changed(); 504 | } 505 | 506 | static void 507 | addIgnVLAN(int c, void *arg) 508 | { 509 | if (!optarg) 510 | return; 511 | int vid = atoi(optarg); 512 | if (vid <= 0 || vid >= 4095) 513 | { 514 | eprintf(DEBUG_ERROR, "invalid vlan id given: %d", vid); 515 | exit(254); 516 | } 517 | vlan_set(ignVlan, vid); 518 | } 519 | 520 | static void 521 | setRestrictToEp(int c, void *arg) 522 | { 523 | restrictToEp = 1; 524 | } 525 | 526 | static __attribute__((constructor)) void port_init() 527 | { 528 | ignVlan = vlan_alloc(NULL); 529 | cb_add_signal(SIGUSR1, dump_if); 530 | { 531 | struct option long_option = {"ignore-vlan", required_argument, 0, 0}; 532 | add_option_cb(long_option, addIgnVLAN, NULL); 533 | } 534 | { 535 | struct option long_option = {"restrict-to-ep", no_argument, 0, 0}; 536 | add_option_cb(long_option, setRestrictToEp, NULL); 537 | } 538 | } 539 | 540 | -------------------------------------------------------------------------------- /src/port.h: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of mvrpd. 3 | * 4 | * mvrpd is free software: you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * mvrpd is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with mvrpd. 16 | * If not, see . 17 | * 18 | * (C) 2019, Michael Braun 19 | */ 20 | 21 | #ifndef MVRPD_PORT 22 | #define MVRPD_PORT 23 | 24 | #include 25 | #include 26 | #include 27 | 28 | struct vlan_arr; 29 | 30 | enum { 31 | IF_UNDEF = 0, 32 | IF_MVRP = 1, 33 | IF_STATIC = 2 34 | }; 35 | 36 | struct if_entry 37 | { 38 | int type; 39 | int ifidx; 40 | char ifname[IFNAMSIZ]; 41 | char mac[ETH_ALEN]; 42 | int ptp; 43 | 44 | /* vlans configured locally, managed by bridge NEWLINK monitoring; mainly for IF_STATIC */ 45 | struct vlan_arr *vlan_state; 46 | 47 | /* vlans declared (aka requested) locally on IF_MVRP interfaces */ 48 | struct vlan_arr *vlan_declared_local; 49 | struct vlan_arr *vlan_declared_local_lastSend; 50 | 51 | /* vlans declared (aka requested) remotely on IF_MVRP interfaces */ 52 | struct vlan_arr *vlan_declared_remote; 53 | /* leave: wait for others on link to say "but hey, I still need it" */ 54 | struct vlan_arr *vlan_declared_remote_leave; 55 | struct vlan_arr *vlan_declared_remote_leave2; 56 | /* leaveAll needs a bigger timer as we see too much packet loss so periodic timer can make up */ 57 | struct vlan_arr *vlan_declared_remote_leaveAll; 58 | struct vlan_arr *vlan_declared_remote_leaveAll2; 59 | 60 | /* vlans configured locally managed by mvrpd on IF_MVRP interfaces */ 61 | struct vlan_arr *vlan_registered_local; 62 | struct vlan_arr *vlan_registered_local_lastSend; 63 | 64 | /* vlan registered (aka configured) remotely on IF_MVRP interfaces */ 65 | struct vlan_arr *vlan_registered_remote; 66 | 67 | /* MVRP state tracking */ 68 | unsigned int needSend:1; // indicates a leave message has been received and thus join should be sent 69 | time_t lastLeaveAll; /* sent or received leaveAll at this timestamp */ 70 | time_t lastLeaveAllFromMe; /* was it sent or receive at lastLeaveAll? */ 71 | time_t lastLeaveAllLeaveTimer; /* when did i last purge vlans not refreshed after leaveAll */ 72 | time_t lastLeaveTimer; /* leave, not leaveAll */ 73 | time_t lastSent; /* periodic timer */ 74 | 75 | /* debugging */ 76 | struct vlan_arr *vlan_to_add_last_print; 77 | struct vlan_arr *vlan_registered_local_last_print; 78 | struct vlan_arr *vlan_declared_local_last_print; 79 | 80 | /* else */ 81 | struct ether_socket *sock; 82 | struct if_entry *next; 83 | }; 84 | 85 | 86 | void port_add(int type, int ifidx, const char *ifname, int ptp, struct vlan_arr *vlan, const char *mac); 87 | void port_del(int ifidx); 88 | void port_del_all(); 89 | void port_vlan_changed(); 90 | struct if_entry *port_get_by_ifidx(int ifidx); 91 | void for_each_port(void (*cb) (struct if_entry *port, void *ctx), void *ctx); 92 | 93 | #endif 94 | -------------------------------------------------------------------------------- /src/random.c: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of mvrpd. 3 | * 4 | * mvrpd is free software: you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * mvrpd is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with mvrpd. 16 | * If not, see . 17 | * 18 | * (C) 2019, Michael Braun 19 | */ 20 | 21 | #include "config.h" 22 | #include "random.h" 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | // random uniform in [0; max) 29 | int getrandom(const int max) 30 | { 31 | if (max <= 0) 32 | return 0; 33 | 34 | double r = (double)rand(); 35 | double m = (double)(RAND_MAX); 36 | return floor(r * max / (m+1.0)); 37 | } 38 | 39 | static __attribute__((constructor)) void random_init() 40 | { 41 | srand(time(0)); 42 | } 43 | 44 | -------------------------------------------------------------------------------- /src/random.h: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of mvrpd. 3 | * 4 | * mvrpd is free software: you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * mvrpd is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with mvrpd. 16 | * If not, see . 17 | * 18 | * (C) 2019, Michael Braun 19 | */ 20 | 21 | #ifndef MVRPD_RANDOM 22 | #define MVRPD_RANDOM 23 | 24 | int getrandom(const int max); // random betwen 0 and max 25 | 26 | #endif 27 | 28 | -------------------------------------------------------------------------------- /src/receive-nflog.c: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of mvrpd. 3 | * 4 | * mvrpd is free software: you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * mvrpd is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with mvrpd. 16 | * If not, see . 17 | * 18 | * (C) 2019, Michael Braun 19 | */ 20 | 21 | #include "config.h" 22 | #include "event.h" 23 | #include "debug.h" 24 | #include "cmdline.h" 25 | #include "timer.h" 26 | 27 | #include 28 | #include 29 | #define _LINUX_IF_H 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | static int groupId = NFLOG_GROUP; 40 | 41 | static void obj_input_nflog(struct nl_object *obj, void *arg) 42 | { 43 | struct nfnl_log_msg *msg = (struct nfnl_log_msg *) obj; 44 | char ifname[IF_NAMESIZE]; 45 | 46 | if (isdebug(DEBUG_NFLOG)) { 47 | char buf[4096]; 48 | nl_object_dump_buf(obj, buf, sizeof(buf)); 49 | eprintf(DEBUG_NFLOG, "received #2 %s", buf); 50 | } 51 | 52 | uint32_t indev = nfnl_log_msg_get_physindev(msg); 53 | if (!indev) 54 | indev = nfnl_log_msg_get_indev(msg); 55 | 56 | if (indev == 0) { 57 | eprintf(DEBUG_NFLOG, "obj_input_nflog...err indev %d == 0", indev); 58 | return; 59 | } 60 | memset(ifname,0,sizeof(ifname)); 61 | if (!if_indextoname(indev, ifname)) { 62 | eprintf(DEBUG_ERROR, "obj_input_nlog: failed to fetch interface name of ifidx %d: %s (%d)", indev, strerror(errno), errno); 63 | return; 64 | } 65 | 66 | uint16_t hwproto = ntohs(nfnl_log_msg_get_hwproto(msg)); 67 | if (hwproto != 0x88f5) { 68 | eprintf(DEBUG_ERROR, "obj_input_nlog: not MVRP protocol: hwproto = %x on %s(%d)", hwproto, ifname, indev); 69 | return; 70 | } 71 | 72 | int len = 0; 73 | const u_char* data = (const u_char*) nfnl_log_msg_get_payload(msg, (int*) &len); 74 | 75 | if (isdebug(DEBUG_NFLOG)) { 76 | eprintf(DEBUG_NFLOG, "obj_input...packet received proto=%x on %s(%d)", hwproto, ifname, indev); 77 | fprintf(stderr, "payload = "); 78 | for (int i = 0; i < len; i++) 79 | { 80 | fprintf(stderr, "%s%02x", (i > 0 ? ":" : ""), data[i]); 81 | } 82 | fprintf(stderr, "\n"); 83 | } 84 | 85 | cb_call_packet_cb(hwproto, data, len, ifname, indev); 86 | } 87 | 88 | static int event_input_nflog(struct nl_msg *msg, void *arg) 89 | { 90 | if (isdebug(DEBUG_NFLOG)) { 91 | char buf[4096] = {0}; 92 | FILE *ofd; 93 | 94 | ofd = fmemopen(buf, sizeof(buf), "w"); 95 | if (ofd) { 96 | nl_msg_dump(msg, ofd); 97 | fclose(ofd); 98 | eprintf(DEBUG_NFLOG, "received message #2: %s", buf); 99 | } else { 100 | eprintf(DEBUG_NFLOG, "received message #2"); 101 | } 102 | 103 | /* get hw header: <00 08 aka ip> -> no VLAN */ 104 | struct nlattr *attr = nlmsg_find_attr(nlmsg_hdr(msg), NFNL_HDRLEN, NFULA_HWHEADER); 105 | char *data = nla_data(attr); 106 | int len = nla_len(attr); 107 | memset(buf, 0, sizeof(buf)); 108 | int offset = 0; 109 | for (int i = 0; i < len && offset < sizeof(buf); i++) 110 | offset += snprintf(buf + offset, sizeof(buf) - offset, (i > 0 ? ":%02hhx" : "%02hhx"), data[i]); 111 | eprintf(DEBUG_NFLOG, "HWHEADER %s", buf); 112 | 113 | } 114 | if (nl_msg_parse(msg, &obj_input_nflog, NULL) < 0) 115 | eprintf(DEBUG_NFLOG, "<> Unknown message type"); 116 | return NL_OK; 117 | } 118 | 119 | static void nflog_receive(int s, void* ctx) 120 | { 121 | int ret; 122 | struct nl_sock *nf_sock_nflog = (struct nl_sock *) ctx; 123 | ret = nl_recvmsgs_default(nf_sock_nflog); 124 | if (ret < 0) { 125 | eprintf(DEBUG_ERROR, "receiving nflog socket %d failed %s", s, strerror(errno)); 126 | } 127 | } 128 | 129 | static void set_nflog_group(int c, void *arg) { 130 | if (!optarg) 131 | return; 132 | 133 | groupId = atoi(optarg); 134 | fprintf(stderr, "nf log group %d\n", groupId); 135 | } 136 | 137 | static void nflog_start_listen(void *ctx) { 138 | /* connect to netfilter / NFLOG */ 139 | struct nl_sock *nf_sock_nflog; 140 | struct nfnl_log *log; 141 | int nffd; 142 | int rcvbuf; 143 | 144 | eprintf(DEBUG_NFLOG, "listen to NFLOG packets for group %d", groupId); 145 | 146 | nf_sock_nflog = nl_socket_alloc(); 147 | if (nf_sock_nflog == NULL) { 148 | eprintf(DEBUG_ERROR, "cannot alloc socket: %s", strerror(errno)); 149 | exit(254); 150 | } 151 | nl_socket_disable_seq_check(nf_sock_nflog); 152 | nl_socket_modify_cb(nf_sock_nflog, NL_CB_VALID, NL_CB_CUSTOM, event_input_nflog, NULL); 153 | 154 | if (nl_connect(nf_sock_nflog, NETLINK_NETFILTER) < 0) { 155 | eprintf(DEBUG_ERROR, "cannot connect: %s", strerror(errno)); 156 | exit(254); 157 | } 158 | 159 | if (nfnl_log_pf_bind(nf_sock_nflog, AF_BRIDGE) < 0) { 160 | eprintf(DEBUG_ERROR, "cannot bind: %s", strerror(errno)); 161 | exit(254); 162 | } 163 | 164 | log = nfnl_log_alloc(); 165 | assert(log); 166 | nfnl_log_set_group(log, groupId); 167 | 168 | nfnl_log_set_copy_mode(log, NFNL_LOG_COPY_PACKET); 169 | 170 | nfnl_log_set_copy_range(log, 0xFFFF); 171 | 172 | if (nfnl_log_create(nf_sock_nflog, log) < 0) { 173 | eprintf(DEBUG_ERROR, "cannot create log: %s", strerror(errno)); 174 | exit(254); 175 | } 176 | 177 | nffd = nl_socket_get_fd(nf_sock_nflog); 178 | if (nffd < 0) { 179 | eprintf(DEBUG_ERROR, "nflog socket %d is error", nffd); 180 | exit(254); 181 | } 182 | eprintf(DEBUG_NFLOG, "nflog socket %d", nffd); 183 | 184 | rcvbuf = 1024 * 1024; 185 | if (setsockopt(nffd, SOL_SOCKET, SO_RCVBUFFORCE, 186 | &rcvbuf, sizeof rcvbuf)) { 187 | eprintf(DEBUG_ERROR, "nflog socket %d cannot set buffer size to %d", nffd, rcvbuf); 188 | } 189 | 190 | cb_add_handle(nffd, nf_sock_nflog, nflog_receive); 191 | } 192 | 193 | static __attribute__((constructor)) void nflog_init() 194 | { 195 | { 196 | struct option long_option = {"nflog-group", required_argument, 0, 0}; 197 | add_option_cb(long_option, set_nflog_group, NULL); 198 | } 199 | 200 | cb_add_timer(0, 0, NULL, nflog_start_listen); 201 | } 202 | 203 | -------------------------------------------------------------------------------- /src/test-vlan0.c: -------------------------------------------------------------------------------- 1 | #include "vlan.h" 2 | #include "cmdline.h" 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | void dump_vlan(struct vlan_arr *arr) { 9 | char tmp[4096]; 10 | int trunc = (sizeof(tmp) == vlan_dump(arr, tmp, sizeof(tmp))); 11 | printf("%s%s\n", tmp, trunc ? "..." : ""); 12 | } 13 | 14 | void test0() { 15 | struct vlan_arr *arr; 16 | 17 | arr = vlan_alloc("test0"); 18 | vlan_set(arr, 32); 19 | vlan_set(arr, 1); 20 | vlan_set(arr, 16); 21 | dump_vlan(arr); 22 | vlan_free(arr); 23 | } 24 | 25 | void test1() { 26 | struct vlan_arr *arr; 27 | 28 | arr = vlan_alloc("test1"); 29 | vlan_set(arr, 31); 30 | vlan_set(arr, 1); 31 | vlan_set(arr, 15); 32 | dump_vlan(arr); 33 | vlan_free(arr); 34 | } 35 | 36 | void test2() { 37 | struct vlan_arr *arr; 38 | 39 | arr = vlan_alloc("test2"); 40 | vlan_set(arr, 1); 41 | vlan_set(arr, 31); 42 | vlan_set(arr, 15); 43 | dump_vlan(arr); 44 | vlan_free(arr); 45 | } 46 | 47 | void test3() { 48 | struct vlan_arr *arr; 49 | 50 | arr = vlan_alloc("test3"); 51 | vlan_set(arr, 1024); 52 | vlan_set(arr, 1056); 53 | vlan_set(arr, 1040); 54 | dump_vlan(arr); 55 | vlan_free(arr); 56 | } 57 | 58 | void test4() { 59 | struct vlan_arr *arr; 60 | 61 | arr = vlan_alloc("test4"); 62 | vlan_set(arr, 1024); 63 | vlan_set(arr, 1088); 64 | vlan_set(arr, 1056); 65 | dump_vlan(arr); 66 | vlan_free(arr); 67 | } 68 | 69 | void test5() { 70 | struct vlan_arr *arr; 71 | 72 | arr = vlan_alloc("test5"); 73 | vlan_set(arr, 2048); 74 | vlan_set(arr, 1024); 75 | vlan_set(arr, 1088); 76 | vlan_set(arr, 1056); 77 | dump_vlan(arr); 78 | vlan_free(arr); 79 | } 80 | 81 | void test6() { 82 | struct vlan_arr *arr; 83 | 84 | arr = vlan_alloc("test6"); 85 | vlan_set(arr, 2048); 86 | vlan_set(arr, 1024); 87 | vlan_set(arr, 1); 88 | dump_vlan(arr); 89 | vlan_free(arr); 90 | } 91 | 92 | void test7() { 93 | struct vlan_arr *arr; 94 | 95 | arr = vlan_alloc("test7"); 96 | for (int i = 0; i < 64; i++) 97 | vlan_set(arr, i*32+1); 98 | vlan_test(arr, 30*32); 99 | dump_vlan(arr); 100 | vlan_free(arr); 101 | } 102 | 103 | void test8() { 104 | struct vlan_arr *arr, *arr0; 105 | arr = vlan_alloc("test8a"); 106 | for (int i = 0; i < 64; i++) 107 | vlan_set(arr, i*32+1); 108 | for (int i = 1; i < 4094; i++) { 109 | int wasset = vlan_test(arr, i); 110 | if (i % 32 == 1 && i < 64*32+1) 111 | assert(wasset); 112 | else 113 | assert(!wasset); 114 | } 115 | arr0 = vlan_clone(arr, "test8b"); 116 | for (int i = 1; i < 4094; i++) { 117 | int wasset = vlan_test(arr0, i); 118 | if (i % 32 == 1 && i < 64*32+1) 119 | assert(wasset); 120 | else 121 | assert(!wasset); 122 | } 123 | for (int i = 0; i < 64; i++) { 124 | int wasset = vlan_unset(arr0, i*32+1); 125 | assert(wasset); 126 | } 127 | for (int i = 0; i < 64; i++) { 128 | int wasset = vlan_test(arr0, i*32+1); 129 | assert(!wasset); 130 | } 131 | { 132 | int wasset = vlan_test(arr0, 18*32); 133 | assert(!wasset); 134 | } 135 | dump_vlan(arr); 136 | dump_vlan(arr0); 137 | vlan_free(arr); 138 | vlan_free(arr0); 139 | } 140 | 141 | void test9() { 142 | struct vlan_arr *arr, *arr0; 143 | 144 | int step = 48; 145 | int cnt = 16; 146 | arr = vlan_alloc("test9a"); 147 | for (int i = 0; i < cnt; i++) 148 | vlan_set(arr, i*step+1); 149 | for (int i = 1; i < 4094; i++) { 150 | int wasset = vlan_test(arr, i); 151 | if (i % step == 1 && i < step*cnt+1) 152 | assert(wasset); 153 | else 154 | assert(!wasset); 155 | } 156 | arr0 = vlan_clone(arr, "test9b"); 157 | for (int i = 1; i < 4094; i++) { 158 | int wasset = vlan_test(arr0, i); 159 | if (i % step == 1 && i < step*cnt+1) 160 | assert(wasset); 161 | else 162 | assert(!wasset); 163 | } 164 | for (int i = 0; i < cnt; i++) { 165 | int wasset = vlan_unset(arr0, i*step+1); 166 | assert(wasset); 167 | } 168 | for (int i = 0; i < cnt; i++) { 169 | int wasset = vlan_test(arr0, i*step+1); 170 | assert(!wasset); 171 | } 172 | for (int i = 0; i < cnt; i++) { 173 | int wasset0 = vlan_test(arr, i*step); 174 | assert(!wasset0); 175 | int wasset1 = vlan_test(arr, (cnt - 1)*step+1); 176 | assert(wasset1); 177 | } 178 | dump_vlan(arr); 179 | dump_vlan(arr0); 180 | vlan_free(arr); 181 | vlan_free(arr0); 182 | } 183 | 184 | void test10() { 185 | { 186 | struct vlan_arr *arr; 187 | char tmp[4096]; 188 | 189 | arr = vlan_alloc("test10a"); 190 | vlan_set(arr, 4095); 191 | 192 | dump_vlan(arr); 193 | vlan_dump(arr, tmp, sizeof(tmp)); 194 | assert(strncmp(tmp, "4095", sizeof(tmp)) == 0); 195 | 196 | vlan_free(arr); 197 | } 198 | 199 | { 200 | struct vlan_arr *arr; 201 | char tmp[4096]; 202 | arr = vlan_alloc("test10b"); 203 | vlan_set(arr, 1); 204 | vlan_set(arr, 4095); 205 | 206 | dump_vlan(arr); 207 | vlan_dump(arr, tmp, sizeof(tmp)); 208 | assert(strncmp(tmp, "1,4095", sizeof(tmp)) == 0); 209 | 210 | vlan_free(arr); 211 | } 212 | } 213 | 214 | int main(int argc, char *argv[]) 215 | { 216 | parse_cmdline(argc, argv); 217 | 218 | char *argv2[2]; 219 | argv2[0] = argv[0]; 220 | argv2[1] = "--debug-all"; 221 | parse_cmdline(2, argv2); 222 | 223 | printf("##### TEST: 0 #####\n"); 224 | test0(); 225 | printf("##### TEST: 1 #####\n"); 226 | test1(); 227 | printf("##### TEST: 2 #####\n"); 228 | test2(); 229 | printf("##### TEST: 3 #####\n"); 230 | test3(); 231 | printf("##### TEST: 4 #####\n"); 232 | test4(); 233 | printf("##### TEST: 5 #####\n"); 234 | test5(); 235 | printf("##### TEST: 6 #####\n"); 236 | test6(); 237 | printf("##### TEST: 7 #####\n"); 238 | test7(); 239 | printf("##### TEST: 8 #####\n"); 240 | test8(); 241 | printf("##### TEST: 9 #####\n"); 242 | test9(); 243 | printf("##### TEST: 10 #####\n"); 244 | test10(); 245 | printf("##### TEST: END #####\n"); 246 | } 247 | 248 | -------------------------------------------------------------------------------- /src/timer.c: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of mvrpd. 3 | * 4 | * mvrpd is free software: you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * mvrpd is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with mvrpd. 16 | * If not, see . 17 | * 18 | * (C) 2019, Michael Braun 19 | */ 20 | 21 | #include "config.h" 22 | 23 | #include "timer.h" 24 | #include "event.h" 25 | #include "debug.h" 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | 35 | #define SLOT_INTERVAL 1 36 | 37 | struct timer_cb_list_entry { 38 | int lastcalled; 39 | int timeout; 40 | int repeat; 41 | int deleted; 42 | void* ctx; 43 | timer_cb cb; 44 | struct timer_cb_list_entry* next; 45 | }; 46 | struct timer_cb_list_entry* timer_cb_list = NULL; 47 | 48 | void cb_add_timer(int timeout, int repeat, void* ctx, timer_cb cb) 49 | { 50 | struct timer_cb_list_entry* entry = malloc(sizeof(struct timer_cb_list_entry)); 51 | if (!entry) { 52 | eprintf(DEBUG_ERROR, "out of memory"); 53 | exit(1); 54 | } 55 | memset(entry, 0, sizeof(struct timer_cb_list_entry)); 56 | entry->cb = cb; 57 | entry->ctx = ctx; 58 | entry->timeout = timeout; 59 | entry->repeat = repeat; 60 | entry->lastcalled = reltime(); 61 | entry->next = timer_cb_list; 62 | timer_cb_list = entry; 63 | }; 64 | 65 | void cb_del_timer(void* ctx, timer_cb cb) 66 | { 67 | struct timer_cb_list_entry* entry = NULL; 68 | for (entry = timer_cb_list; entry; entry = entry->next) { 69 | if (entry->cb != cb) 70 | continue; 71 | if (entry->ctx != ctx) 72 | continue; 73 | entry->deleted = 1; 74 | } 75 | } 76 | 77 | void timer(int s) 78 | { 79 | alarm (SLOT_INTERVAL); 80 | 81 | struct timer_cb_list_entry* entry = NULL, *prev = NULL, *next = NULL; 82 | int now = reltime(); 83 | timer_cb cb; 84 | void *ctx = NULL; 85 | 86 | next = timer_cb_list; 87 | while (next) { 88 | prev = entry; 89 | entry = next; 90 | next = entry->next; 91 | 92 | if (entry->deleted) { 93 | /* delete entry */ 94 | if (prev) { 95 | prev->next = next; 96 | } else { 97 | timer_cb_list = next; 98 | } 99 | free(entry); 100 | entry = prev; 101 | continue; 102 | } 103 | 104 | if (entry->lastcalled + entry->timeout > now) 105 | continue; 106 | 107 | cb = entry->cb; 108 | ctx = entry->ctx; 109 | 110 | /* timer needs to fire */ 111 | if (entry->repeat) { 112 | entry->lastcalled = now; 113 | } else { 114 | /* do not free it here, as cb might add a new timer => successive add_timer would break if prev=NULL */ 115 | entry->deleted = 1; 116 | } 117 | 118 | /* call cb */ 119 | cb(ctx); 120 | } 121 | } 122 | 123 | uint32_t reltime() 124 | { 125 | #if defined(CLOCK_BOOTTIME) 126 | static clockid_t clock_id = CLOCK_BOOTTIME; 127 | #elif defined(CLOCK_MONOTONIC) 128 | static clockid_t clock_id = CLOCK_MONOTONIC; 129 | #else 130 | static clockid_t clock_id = CLOCK_REALTIME; 131 | #endif 132 | struct timespec ts; 133 | int res; 134 | 135 | while (1) { 136 | res = clock_gettime(clock_id, &ts); 137 | if (res == 0) 138 | return ts.tv_sec; 139 | switch (clock_id) { 140 | #ifdef CLOCK_BOOTTIME 141 | case CLOCK_BOOTTIME: 142 | clock_id = CLOCK_MONOTONIC; 143 | break; 144 | #endif 145 | #ifdef CLOCK_MONOTONIC 146 | case CLOCK_MONOTONIC: 147 | clock_id = CLOCK_REALTIME; 148 | break; 149 | #endif 150 | case CLOCK_REALTIME: 151 | return time(NULL); 152 | } 153 | } 154 | } 155 | 156 | static __attribute__((constructor)) void timer_init() 157 | { 158 | cb_add_signal(SIGALRM, timer); 159 | alarm (SLOT_INTERVAL); 160 | } 161 | 162 | -------------------------------------------------------------------------------- /src/timer.h: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of mvrpd. 3 | * 4 | * mvrpd is free software: you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * mvrpd is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with mvrpd. 16 | * If not, see . 17 | * 18 | * (C) 2019, Michael Braun 19 | */ 20 | 21 | #ifndef MVRPD_TIMER 22 | #define MVRPD_TIMER 23 | 24 | #include 25 | 26 | #define PRUNE_INTERVAL 300 27 | 28 | typedef void (*timer_cb) (void* ctx); 29 | 30 | void cb_add_timer(int timeout, int repeat, void* ctx, timer_cb cb); 31 | void cb_del_timer(void* ctx, timer_cb cb); 32 | uint32_t reltime(); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /src/vlan.c: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of mvrpd. 3 | * 4 | * mvrpd is free software: you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * mvrpd is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with mvrpd. 16 | * If not, see . 17 | * 18 | * (C) 2019, Michael Braun 19 | */ 20 | 21 | #include "vlan.h" 22 | #include "debug.h" 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #define MIN(a,b) ((a < b) ? a : b) 33 | #define MAX(a,b) ((a > b) ? a : b) 34 | #define VLAN_MAX 4096 35 | 36 | struct vlan_entry { 37 | uint8_t start; // vid / 16 38 | uint8_t offset; // offset in data array 39 | } __attribute__((packed)); 40 | 41 | struct vlan_arr { 42 | uint16_t numentries; 43 | char name[32]; 44 | 45 | uint8_t nummeta; 46 | /* sorted by start, implies ordering of offset 47 | * for successive entries: delta in start * 16 is minimum distance in offset 48 | */ 49 | struct vlan_entry *meta; 50 | 51 | uint8_t numbitmap; 52 | uint16_t *bitmap; 53 | 54 | uint8_t lastidx; // offset in meta array 55 | } __attribute__((packed)); 56 | 57 | static inline uint8_t 58 | vlan_offset(struct vlan_arr *arr, uint16_t metaidx) 59 | { 60 | return (metaidx < arr->nummeta) ? arr->meta[metaidx].offset : arr->numbitmap; 61 | } 62 | 63 | static inline uint16_t 64 | vlan_start_vid(struct vlan_arr *arr, uint16_t metaidx) 65 | { 66 | return (metaidx < arr->nummeta) ? arr->meta[metaidx].start * 16 : VLAN_MAX; 67 | } 68 | 69 | static inline uint16_t 70 | vlan_end_vid(struct vlan_arr *arr, uint16_t metaidx) // first not included in metaidx 71 | { 72 | return (vlan_offset(arr, metaidx + 1) - vlan_offset(arr, metaidx)) * 16 + vlan_start_vid(arr, metaidx); 73 | } 74 | 75 | /* returns 1 if found, else 0; *metaidx is set to match or its successor */ 76 | static int 77 | vlan_find(struct vlan_arr *arr, uint16_t vid, uint16_t *metaidx) 78 | { 79 | uint16_t right = 0; 80 | uint16_t left = arr->nummeta; 81 | 82 | /* exploit the fact that most requests target close-by consecutive vids 83 | * even if this is not a perfect match, still reduce the lookup range */ 84 | for (uint16_t middle = (arr->lastidx > 0) ? arr->lastidx - 1 : 0; 85 | middle < MIN(arr->nummeta, arr->lastidx + 2); 86 | middle++) { 87 | if (vid >= vlan_start_vid(arr, middle)) { 88 | right = MAX(middle, right); 89 | } else { 90 | left = MIN(middle, left); 91 | } 92 | if (vid >= vlan_end_vid(arr, middle)) { 93 | right = MAX(middle + 1, right); 94 | } else { 95 | left = MIN(middle + 1, left); 96 | } 97 | } 98 | 99 | while (right < left) { 100 | uint16_t middle = (left + right) / 2; 101 | assert(middle < arr->nummeta); 102 | 103 | if (vid < vlan_start_vid(arr, middle)) { 104 | left = middle; // vid needs to come before middle 105 | continue; 106 | } 107 | 108 | assert(vlan_start_vid(arr, middle + 1) >= vlan_end_vid(arr, middle)); 109 | 110 | if (vid >= vlan_end_vid(arr, middle)) { 111 | // vid needs come after middle 112 | right = middle + 1; 113 | continue; 114 | } 115 | 116 | if (!(vid >= vlan_start_vid(arr, middle)) || !(vid < vlan_end_vid(arr, middle))) { 117 | eprintf(DEBUG_ERROR, "middle=%hu vid=%hu, start_vid=%hu end_vid=%hu", middle, vid, vlan_start_vid(arr, middle), vlan_end_vid(arr, middle)); 118 | } 119 | 120 | assert(vid >= vlan_start_vid(arr, middle)); 121 | assert(vid < vlan_end_vid(arr, middle)); 122 | 123 | *metaidx = middle; 124 | arr->lastidx = middle; 125 | 126 | return 1; 127 | } 128 | 129 | assert(right == left); 130 | *metaidx = right; // successor idx 131 | arr->lastidx = right; 132 | 133 | return 0; 134 | } 135 | 136 | static uint16_t 137 | vlan_find_or_add_room(struct vlan_arr *arr, uint16_t vid) 138 | { 139 | uint16_t succ; 140 | 141 | if (vlan_find(arr, vid, &succ) == 1) 142 | return succ; 143 | 144 | unsigned char merge = 0; // 1 for prev, 2 for succ 145 | uint8_t extrabitmap = 0; 146 | 147 | assert(vid < vlan_start_vid(arr, succ)); 148 | assert(succ == 0 || vid >= vlan_end_vid(arr, succ-1)); 149 | 150 | if (succ > 0 && 151 | vlan_end_vid(arr, succ-1) / 16 == vid / 16 - 1) 152 | extrabitmap += 1; 153 | if (succ > 0 && 154 | vlan_end_vid(arr, succ-1) / 16 >= vid / 16 - 1) { 155 | merge += 1; 156 | extrabitmap += 1; 157 | } 158 | if (arr->nummeta > succ && 159 | vlan_start_vid(arr, succ) / 16 <= vid / 16 + 2) { 160 | merge += 2; 161 | extrabitmap += 1; 162 | } 163 | if (arr->nummeta > succ && 164 | vlan_start_vid(arr, succ) / 16 == vid / 16 + 2) 165 | extrabitmap += 1; 166 | 167 | if (merge == 0) 168 | extrabitmap = 1; /* plus an extra metaidx entry */ 169 | if (merge == 3) 170 | extrabitmap -= 1; /* the bitmap byte for vid/16 was counted twice */ 171 | 172 | eprintf(DEBUG_GENERAL, "arr %s(%p) nummeta=%hu metabitmap=%hu vid=%hu, idx=%hu vlan_offset=%hu vlan_start_vid=%hu vlan_end_vid=%hu, extrabitmap=%hu merge=%d", 173 | arr->name, arr, arr->nummeta, arr->numbitmap, vid, succ, vlan_offset(arr, succ), vlan_start_vid(arr, succ), vlan_end_vid(arr, succ), extrabitmap, merge); 174 | 175 | uint8_t startoffset = vlan_offset(arr, succ); // fetch before numbitmap is updated 176 | eprintf(DEBUG_GENERAL, "old: arr->bitmap=%p startoffset=%d e=%d numbitmap=%d", arr->bitmap, startoffset, extrabitmap, (int) arr->numbitmap); 177 | assert(extrabitmap); 178 | assert(arr->numbitmap <= UCHAR_MAX - extrabitmap); 179 | assert(startoffset <= arr->numbitmap); 180 | 181 | arr->bitmap = realloc(arr->bitmap, (arr->numbitmap + extrabitmap) * sizeof(*arr->bitmap)); 182 | arr->numbitmap += extrabitmap; 183 | assert(arr->bitmap); 184 | 185 | for (uint8_t i = succ; i < arr->nummeta; i++) { 186 | assert(arr->meta[i].offset >= startoffset); 187 | arr->meta[i].offset += extrabitmap; 188 | } 189 | uint8_t endoffset = startoffset + extrabitmap; 190 | for (uint8_t i = arr->numbitmap - 1; i >= endoffset; i--) { 191 | // eprintf(DEBUG_GENERAL, "arr->bitmap=%p i=%d i-e=%d numbitmap=%d", arr->bitmap, i, i-extrabitmap, (int) arr->numbitmap); 192 | arr->bitmap[i] = arr->bitmap[i - extrabitmap]; 193 | } 194 | for (uint8_t i = startoffset; i < endoffset; i++) { 195 | // eprintf(DEBUG_GENERAL, "arr->bitmap=%p i=%d startoffset=%d e=%d numbitmap=%d", arr->bitmap, i, startoffset, extrabitmap, (int) arr->numbitmap); 196 | arr->bitmap[i] = 0; 197 | } 198 | 199 | switch (merge) { 200 | case 3: // merge prev and succ 201 | eprintf(DEBUG_GENERAL, "end_vid=%hu start_vid=%hu succ=%hu", vlan_end_vid(arr, succ-1), vlan_start_vid(arr, succ), succ); 202 | assert(vlan_end_vid(arr, succ - 1) == vlan_start_vid(arr, succ)); 203 | // drop succ from meta list 204 | for (uint16_t i = succ; i < arr->nummeta - 1; i++) 205 | arr->meta[i] = arr->meta[i+1]; 206 | arr->nummeta--; 207 | arr->meta = realloc(arr->meta, arr->nummeta * sizeof(*arr->meta)); 208 | assert(arr->meta); 209 | return succ - 1; 210 | case 2: // merge succ but not prev 211 | assert(vid >= vlan_start_vid(arr, succ) - extrabitmap * 16); 212 | arr->meta[succ].offset -= extrabitmap; 213 | arr->meta[succ].start -= extrabitmap; 214 | return succ; 215 | case 1: // merge prev but not succ 216 | assert(vid < vlan_end_vid(arr, succ - 1)); 217 | return succ - 1; 218 | case 0: // merge none 219 | // insert into meta list 220 | arr->nummeta++; 221 | arr->meta = realloc(arr->meta, arr->nummeta * sizeof(*arr->meta)); 222 | assert(arr->meta); 223 | for (uint16_t i = arr->nummeta - 1; i > succ; i--) 224 | arr->meta[i] = arr->meta[i - 1]; 225 | arr->meta[succ].offset = startoffset; 226 | arr->meta[succ].start = vid / 16; 227 | return succ; 228 | } 229 | 230 | assert(0); 231 | return 0; 232 | } 233 | 234 | int 235 | vlan_set(struct vlan_arr *arr, uint16_t vid) 236 | { 237 | eprintf(DEBUG_GENERAL, "%s(%p) set %hu", arr->name, arr, vid); 238 | assert(vid < VLAN_MAX); 239 | 240 | if (vid >= VLAN_MAX) 241 | return 0; 242 | 243 | uint16_t metaidx = vlan_find_or_add_room(arr, vid); 244 | assert(vid >= vlan_start_vid(arr, metaidx)); 245 | assert(vid < vlan_end_vid(arr, metaidx)); 246 | 247 | uint16_t offset = vlan_offset(arr, metaidx) + (vid - vlan_start_vid(arr, metaidx)) / 16; 248 | assert(offset < arr->numbitmap); 249 | 250 | uint16_t mask = 1 << (vid % 16); 251 | int wasset = !!( arr->bitmap[offset] & mask); 252 | arr->bitmap[offset] |= mask; 253 | 254 | if (!wasset) 255 | arr->numentries++; 256 | 257 | return wasset; 258 | } 259 | 260 | static void 261 | vlan_rebuild(struct vlan_arr *arr) 262 | { 263 | struct vlan_arr *n = vlan_alloc(NULL); 264 | assert(n); 265 | 266 | int it = 0; 267 | uint16_t vid = 0; 268 | while (vlan_next(arr, &it, &vid) == 0) { 269 | vlan_set(n, vid); 270 | } 271 | 272 | free(arr->meta); 273 | arr->meta = n->meta; 274 | n->meta = NULL; 275 | 276 | free(arr->bitmap); 277 | arr->bitmap = n->bitmap; 278 | n->bitmap = NULL; 279 | 280 | arr->nummeta = n->nummeta; 281 | n->nummeta = 0; 282 | 283 | arr->numbitmap = n->numbitmap; 284 | n->numbitmap = 0; 285 | 286 | assert(arr->numentries == n->numentries); 287 | 288 | vlan_free(n); 289 | } 290 | 291 | int 292 | vlan_unset(struct vlan_arr *arr, uint16_t vid) 293 | { 294 | eprintf(DEBUG_GENERAL, "%s(%p) unset %hu", arr->name, arr, vid); 295 | assert(vid < VLAN_MAX); 296 | uint16_t metaidx; 297 | 298 | if (vid >= VLAN_MAX) 299 | return 0; 300 | 301 | if (vlan_find(arr, vid, &metaidx) == 0) 302 | return 0; 303 | 304 | assert(vid >= vlan_start_vid(arr, metaidx)); 305 | assert(vid < vlan_end_vid(arr, metaidx)); 306 | 307 | uint16_t offset = vlan_offset(arr, metaidx) + (vid - vlan_start_vid(arr, metaidx)) / 16; 308 | assert(offset < arr->numbitmap); 309 | 310 | uint16_t mask = 1 << (vid % 16); 311 | int wasset = !!( arr->bitmap[offset] & mask); 312 | arr->bitmap[offset] &= ~mask; 313 | 314 | if (wasset) 315 | arr->numentries--; 316 | 317 | if (arr->numbitmap > arr->numentries) 318 | vlan_rebuild(arr); 319 | 320 | return wasset; 321 | } 322 | 323 | int 324 | vlan_test(struct vlan_arr *arr, uint16_t vid) 325 | { 326 | eprintf(DEBUG_GENERAL, "%s(%p) test %hu", arr->name, arr, vid); 327 | uint16_t metaidx; 328 | 329 | if (vlan_find(arr, vid, &metaidx) == 0) 330 | return 0; 331 | 332 | assert(vid >= vlan_start_vid(arr, metaidx)); 333 | assert(vid < vlan_end_vid(arr, metaidx)); 334 | 335 | uint16_t offset = vlan_offset(arr, metaidx) + (vid - vlan_start_vid(arr, metaidx)) / 16; 336 | assert(offset < arr->numbitmap); 337 | 338 | uint16_t mask = 1 << (vid % 16); 339 | int wasset = !!( arr->bitmap[offset] & mask); 340 | 341 | return wasset; 342 | } 343 | 344 | // 1 on end, 0 if found 345 | int 346 | vlan_next(struct vlan_arr *arr, int *iterator, uint16_t *vid) 347 | { 348 | if (!*vid) 349 | *iterator = 0; 350 | (*vid)++; 351 | 352 | while (*iterator < arr->nummeta) { 353 | if (*vid >= vlan_end_vid(arr, *iterator)) { 354 | (*iterator)++; 355 | continue; 356 | } 357 | 358 | if (*vid < vlan_start_vid(arr, *iterator)) 359 | *vid = vlan_start_vid(arr, *iterator); 360 | 361 | uint16_t offset = vlan_offset(arr, *iterator) + ((*vid) - vlan_start_vid(arr, *iterator)) / 16; 362 | assert(offset < arr->numbitmap); 363 | 364 | uint16_t mask = 1 << ((*vid) % 16); 365 | int wasset = !!( arr->bitmap[offset] & mask); 366 | 367 | if (wasset) 368 | return 0; 369 | 370 | (*vid)++; 371 | } 372 | 373 | *vid = 0xffff; 374 | 375 | return 1; 376 | } 377 | 378 | size_t 379 | vlan_dump(struct vlan_arr *arr, char *buf, size_t buflen) 380 | { 381 | size_t written = 0; 382 | int it = 0; 383 | uint16_t vid = 0; 384 | 385 | if (buflen > 0) 386 | buf[0] = '\0'; 387 | 388 | while (vlan_next(arr, &it, &vid) == 0 && 389 | written < buflen) { 390 | written += snprintf(buf + written, buflen - written, "%s%d", (written == 0 ? "" : ","), vid); 391 | } 392 | 393 | if (isdebug(DEBUG_GENERAL)) { 394 | char tmp[4096], *ptr; 395 | ptr = tmp; 396 | ptr += snprintf(ptr, tmp + sizeof(tmp) - ptr, "vlan %s(%p) numentries: %hu nummeta: %hu numbitmap: %hu\n", 397 | arr->name, arr, arr->numentries, arr->nummeta, arr->numbitmap); 398 | 399 | if (ptr < tmp + sizeof(tmp)) 400 | ptr += snprintf(ptr, tmp + sizeof(tmp) - ptr, "meta:\n"); 401 | for (uint16_t i = 0; i < arr->nummeta && ptr < tmp + sizeof(tmp); i++) { 402 | ptr += snprintf(ptr, tmp + sizeof(tmp) - ptr, " * %02d: start=%hhu offset=%hhu // startvid=%hu endvid=%hu\n", i, arr->meta[i].start, arr->meta[i].offset, vlan_start_vid(arr, i), vlan_end_vid(arr, i)); 403 | } 404 | 405 | if (ptr < tmp + sizeof(tmp)) 406 | ptr += snprintf(ptr, tmp + sizeof(tmp) - ptr, "bitmap:\n"); 407 | for (uint16_t i = 0; i < arr->numbitmap && ptr < tmp + sizeof(tmp); i++) { 408 | ptr += snprintf(ptr, tmp + sizeof(tmp) - ptr, " * %02d: %04hx\n", i, arr->bitmap[i]); 409 | } 410 | 411 | eprintf(DEBUG_GENERAL, "%s", tmp); 412 | } 413 | 414 | return written; 415 | } 416 | 417 | struct vlan_arr * 418 | vlan_alloc(const char *name) 419 | { 420 | struct vlan_arr *ret = malloc(sizeof(*ret)); 421 | assert(ret); 422 | memset(ret, 0, sizeof(*ret)); 423 | 424 | if (name) 425 | strncpy(ret->name, name, sizeof(ret->name)-1); 426 | 427 | ret->numentries = 0; 428 | 429 | ret->nummeta = 0; 430 | ret->meta = NULL; 431 | 432 | ret->numbitmap = 0; 433 | ret->bitmap = NULL; 434 | 435 | return ret; 436 | } 437 | 438 | struct vlan_arr * 439 | vlan_clone(struct vlan_arr *arr, const char *name) 440 | { 441 | struct vlan_arr *ret = vlan_alloc(name); 442 | assert(ret); 443 | 444 | ret->numentries = arr->numentries; 445 | 446 | ret->nummeta = arr->nummeta; 447 | free(ret->meta); 448 | ret->meta = calloc(ret->nummeta, sizeof(*ret->meta)); 449 | 450 | ret->numbitmap = arr->numbitmap; 451 | free(ret->bitmap); 452 | ret->bitmap = calloc(ret->numbitmap, sizeof(*ret->bitmap)); 453 | 454 | if (ret->nummeta > 0) { 455 | assert(ret->meta); 456 | memcpy(ret->meta, arr->meta, ret->nummeta * sizeof(*ret->meta)); 457 | } 458 | if (ret->numbitmap > 0) { 459 | assert(ret->bitmap); 460 | memcpy(ret->bitmap, arr->bitmap, ret->numbitmap * sizeof(*ret->bitmap)); 461 | } 462 | 463 | return ret; 464 | } 465 | 466 | void 467 | vlan_free(struct vlan_arr *arr) 468 | { 469 | if (!arr) 470 | return; 471 | 472 | free(arr->meta); 473 | free(arr->bitmap); 474 | 475 | free(arr); 476 | } 477 | 478 | int 479 | vlan_compare(struct vlan_arr *arr1, struct vlan_arr *arr2) 480 | { 481 | int it1 = 0, it2 = 0; 482 | uint16_t vid1 = 0, vid2 = 0; 483 | int rc1, rc2; 484 | 485 | while (1) { 486 | rc1 = vlan_next(arr1, &it1, &vid1); 487 | rc2 = vlan_next(arr2, &it2, &vid2); 488 | 489 | if (rc1 != 0 && rc2 != 0) 490 | return 0; // success 491 | 492 | if (rc1 != 0 || rc2 != 0) 493 | return 1; // different number of vlans 494 | 495 | if (vid1 != vid2) 496 | return 1; // different vlan next 497 | } 498 | 499 | return 0; 500 | } 501 | 502 | // 0 if empty, 1 else 503 | int 504 | vlan_notempty(struct vlan_arr *arr) 505 | { 506 | return !!arr->numentries; 507 | } 508 | -------------------------------------------------------------------------------- /src/vlan.h: -------------------------------------------------------------------------------- 1 | /** 2 | * This file is part of mvrpd. 3 | * 4 | * mvrpd is free software: you can redistribute it and/or 5 | * modify it under the terms of the GNU General Public License as published by 6 | * the Free Software Foundation, either version 3 of the License, or 7 | * (at your option) any later version. 8 | * 9 | * mvrpd is distributed in the hope that it will be useful, 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | * GNU General Public License for more details. 13 | * 14 | * You should have received a copy of the GNU General Public License 15 | * along with mvrpd. 16 | * If not, see . 17 | * 18 | * (C) 2019, Michael Braun 19 | */ 20 | 21 | #ifndef MVRPD_VLAN 22 | #define MVRPD_VLAN 23 | 24 | #include 25 | #include 26 | 27 | struct vlan_arr; 28 | 29 | int vlan_set(struct vlan_arr *arr, uint16_t vid); // returns old mask 30 | int vlan_unset(struct vlan_arr *arr, uint16_t vid); // returns old mask 31 | int vlan_test(struct vlan_arr *arr, uint16_t vid); 32 | struct vlan_arr *vlan_alloc(const char *name); 33 | struct vlan_arr *vlan_clone(struct vlan_arr *arr, const char *name); 34 | void vlan_free(struct vlan_arr *arr); 35 | int vlan_next(struct vlan_arr *arr, int *iterator, uint16_t *vid); // 1 on end, 0 if found 36 | size_t vlan_dump(struct vlan_arr *arr, char *buf, size_t buflen); 37 | int vlan_compare(struct vlan_arr *arr1, struct vlan_arr *arr2); // 1 in different, 0 on equal 38 | int vlan_notempty(struct vlan_arr *arr); 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /test-and-coverage.sh: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | set -e 4 | 5 | rm -f src/*.gcno # generated by make 6 | rm -f src/*.gcda # generated during runtime 7 | rm -f coverage.info 8 | rm -rf out-test-coverage 9 | 10 | echo "build" 11 | make clean 12 | make check 13 | 14 | echo "run test"; 15 | 16 | ./test-vlan0 17 | 18 | echo "collect" 19 | #gcov src/test_vlan0-vlan.o 20 | gcov src/test_vlan0-*.o 21 | #lcov --coverage --directory src --output-file coverage.info 22 | lcov --capture --directory src --output-file coverage.info 23 | genhtml coverage.info --output-directory out-test-coverage 24 | 25 | --------------------------------------------------------------------------------