LCOV - code coverage report
Current view: top level - src/ap - vlan_init.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1388338050 Lines: 87 454 19.2 %
Date: 2013-12-29 Functions: 10 23 43.5 %
Branches: 33 239 13.8 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * hostapd / VLAN initialization
       3                 :            :  * Copyright 2003, Instant802 Networks, Inc.
       4                 :            :  * Copyright 2005-2006, Devicescape Software, Inc.
       5                 :            :  * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
       6                 :            :  *
       7                 :            :  * This software may be distributed under the terms of the BSD license.
       8                 :            :  * See README for more details.
       9                 :            :  */
      10                 :            : 
      11                 :            : #include "utils/includes.h"
      12                 :            : 
      13                 :            : #include "utils/common.h"
      14                 :            : #include "hostapd.h"
      15                 :            : #include "ap_config.h"
      16                 :            : #include "ap_drv_ops.h"
      17                 :            : #include "vlan_init.h"
      18                 :            : #include "vlan_util.h"
      19                 :            : 
      20                 :            : 
      21                 :            : #ifdef CONFIG_FULL_DYNAMIC_VLAN
      22                 :            : 
      23                 :            : #include <net/if.h>
      24                 :            : #include <sys/ioctl.h>
      25                 :            : #include <linux/sockios.h>
      26                 :            : #include <linux/if_vlan.h>
      27                 :            : #include <linux/if_bridge.h>
      28                 :            : 
      29                 :            : #include "drivers/priv_netlink.h"
      30                 :            : #include "utils/eloop.h"
      31                 :            : 
      32                 :            : 
      33                 :            : struct full_dynamic_vlan {
      34                 :            :         int s; /* socket on which to listen for new/removed interfaces. */
      35                 :            : };
      36                 :            : 
      37                 :            : 
      38                 :          0 : static int ifconfig_helper(const char *if_name, int up)
      39                 :            : {
      40                 :            :         int fd;
      41                 :            :         struct ifreq ifr;
      42                 :            : 
      43         [ #  # ]:          0 :         if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
      44                 :          0 :                 wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
      45                 :          0 :                            "failed: %s", __func__, strerror(errno));
      46                 :          0 :                 return -1;
      47                 :            :         }
      48                 :            : 
      49                 :          0 :         os_memset(&ifr, 0, sizeof(ifr));
      50                 :          0 :         os_strlcpy(ifr.ifr_name, if_name, IFNAMSIZ);
      51                 :            : 
      52         [ #  # ]:          0 :         if (ioctl(fd, SIOCGIFFLAGS, &ifr) != 0) {
      53                 :          0 :                 wpa_printf(MSG_ERROR, "VLAN: %s: ioctl(SIOCGIFFLAGS) failed "
      54                 :            :                            "for interface %s: %s",
      55                 :          0 :                            __func__, if_name, strerror(errno));
      56                 :          0 :                 close(fd);
      57                 :          0 :                 return -1;
      58                 :            :         }
      59                 :            : 
      60         [ #  # ]:          0 :         if (up)
      61                 :          0 :                 ifr.ifr_flags |= IFF_UP;
      62                 :            :         else
      63                 :          0 :                 ifr.ifr_flags &= ~IFF_UP;
      64                 :            : 
      65         [ #  # ]:          0 :         if (ioctl(fd, SIOCSIFFLAGS, &ifr) != 0) {
      66                 :          0 :                 wpa_printf(MSG_ERROR, "VLAN: %s: ioctl(SIOCSIFFLAGS) failed "
      67                 :            :                            "for interface %s (up=%d): %s",
      68                 :          0 :                            __func__, if_name, up, strerror(errno));
      69                 :          0 :                 close(fd);
      70                 :          0 :                 return -1;
      71                 :            :         }
      72                 :            : 
      73                 :          0 :         close(fd);
      74                 :          0 :         return 0;
      75                 :            : }
      76                 :            : 
      77                 :            : 
      78                 :          0 : static int ifconfig_up(const char *if_name)
      79                 :            : {
      80                 :          0 :         wpa_printf(MSG_DEBUG, "VLAN: Set interface %s up", if_name);
      81                 :          0 :         return ifconfig_helper(if_name, 1);
      82                 :            : }
      83                 :            : 
      84                 :            : 
      85                 :          0 : static int ifconfig_down(const char *if_name)
      86                 :            : {
      87                 :          0 :         wpa_printf(MSG_DEBUG, "VLAN: Set interface %s down", if_name);
      88                 :          0 :         return ifconfig_helper(if_name, 0);
      89                 :            : }
      90                 :            : 
      91                 :            : 
      92                 :            : /*
      93                 :            :  * These are only available in recent linux headers (without the leading
      94                 :            :  * underscore).
      95                 :            :  */
      96                 :            : #define _GET_VLAN_REALDEV_NAME_CMD      8
      97                 :            : #define _GET_VLAN_VID_CMD               9
      98                 :            : 
      99                 :            : /* This value should be 256 ONLY. If it is something else, then hostapd
     100                 :            :  * might crash!, as this value has been hard-coded in 2.4.x kernel
     101                 :            :  * bridging code.
     102                 :            :  */
     103                 :            : #define MAX_BR_PORTS                    256
     104                 :            : 
     105                 :          0 : static int br_delif(const char *br_name, const char *if_name)
     106                 :            : {
     107                 :            :         int fd;
     108                 :            :         struct ifreq ifr;
     109                 :            :         unsigned long args[2];
     110                 :            :         int if_index;
     111                 :            : 
     112                 :          0 :         wpa_printf(MSG_DEBUG, "VLAN: br_delif(%s, %s)", br_name, if_name);
     113         [ #  # ]:          0 :         if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
     114                 :          0 :                 wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
     115                 :          0 :                            "failed: %s", __func__, strerror(errno));
     116                 :          0 :                 return -1;
     117                 :            :         }
     118                 :            : 
     119                 :          0 :         if_index = if_nametoindex(if_name);
     120                 :            : 
     121         [ #  # ]:          0 :         if (if_index == 0) {
     122                 :          0 :                 wpa_printf(MSG_ERROR, "VLAN: %s: Failure determining "
     123                 :            :                            "interface index for '%s'",
     124                 :            :                            __func__, if_name);
     125                 :          0 :                 close(fd);
     126                 :          0 :                 return -1;
     127                 :            :         }
     128                 :            : 
     129                 :          0 :         args[0] = BRCTL_DEL_IF;
     130                 :          0 :         args[1] = if_index;
     131                 :            : 
     132                 :          0 :         os_strlcpy(ifr.ifr_name, br_name, sizeof(ifr.ifr_name));
     133                 :          0 :         ifr.ifr_data = (__caddr_t) args;
     134                 :            : 
     135 [ #  # ][ #  # ]:          0 :         if (ioctl(fd, SIOCDEVPRIVATE, &ifr) < 0 && errno != EINVAL) {
     136                 :            :                 /* No error if interface already removed. */
     137                 :          0 :                 wpa_printf(MSG_ERROR, "VLAN: %s: ioctl[SIOCDEVPRIVATE,"
     138                 :            :                            "BRCTL_DEL_IF] failed for br_name=%s if_name=%s: "
     139                 :          0 :                            "%s", __func__, br_name, if_name, strerror(errno));
     140                 :          0 :                 close(fd);
     141                 :          0 :                 return -1;
     142                 :            :         }
     143                 :            : 
     144                 :          0 :         close(fd);
     145                 :          0 :         return 0;
     146                 :            : }
     147                 :            : 
     148                 :            : 
     149                 :            : /*
     150                 :            :         Add interface 'if_name' to the bridge 'br_name'
     151                 :            : 
     152                 :            :         returns -1 on error
     153                 :            :         returns 1 if the interface is already part of the bridge
     154                 :            :         returns 0 otherwise
     155                 :            : */
     156                 :          0 : static int br_addif(const char *br_name, const char *if_name)
     157                 :            : {
     158                 :            :         int fd;
     159                 :            :         struct ifreq ifr;
     160                 :            :         unsigned long args[2];
     161                 :            :         int if_index;
     162                 :            : 
     163                 :          0 :         wpa_printf(MSG_DEBUG, "VLAN: br_addif(%s, %s)", br_name, if_name);
     164         [ #  # ]:          0 :         if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
     165                 :          0 :                 wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
     166                 :          0 :                            "failed: %s", __func__, strerror(errno));
     167                 :          0 :                 return -1;
     168                 :            :         }
     169                 :            : 
     170                 :          0 :         if_index = if_nametoindex(if_name);
     171                 :            : 
     172         [ #  # ]:          0 :         if (if_index == 0) {
     173                 :          0 :                 wpa_printf(MSG_ERROR, "VLAN: %s: Failure determining "
     174                 :            :                            "interface index for '%s'",
     175                 :            :                            __func__, if_name);
     176                 :          0 :                 close(fd);
     177                 :          0 :                 return -1;
     178                 :            :         }
     179                 :            : 
     180                 :          0 :         args[0] = BRCTL_ADD_IF;
     181                 :          0 :         args[1] = if_index;
     182                 :            : 
     183                 :          0 :         os_strlcpy(ifr.ifr_name, br_name, sizeof(ifr.ifr_name));
     184                 :          0 :         ifr.ifr_data = (__caddr_t) args;
     185                 :            : 
     186         [ #  # ]:          0 :         if (ioctl(fd, SIOCDEVPRIVATE, &ifr) < 0) {
     187         [ #  # ]:          0 :                 if (errno == EBUSY) {
     188                 :            :                         /* The interface is already added. */
     189                 :          0 :                         close(fd);
     190                 :          0 :                         return 1;
     191                 :            :                 }
     192                 :            : 
     193                 :          0 :                 wpa_printf(MSG_ERROR, "VLAN: %s: ioctl[SIOCDEVPRIVATE,"
     194                 :            :                            "BRCTL_ADD_IF] failed for br_name=%s if_name=%s: "
     195                 :          0 :                            "%s", __func__, br_name, if_name, strerror(errno));
     196                 :          0 :                 close(fd);
     197                 :          0 :                 return -1;
     198                 :            :         }
     199                 :            : 
     200                 :          0 :         close(fd);
     201                 :          0 :         return 0;
     202                 :            : }
     203                 :            : 
     204                 :            : 
     205                 :          0 : static int br_delbr(const char *br_name)
     206                 :            : {
     207                 :            :         int fd;
     208                 :            :         unsigned long arg[2];
     209                 :            : 
     210                 :          0 :         wpa_printf(MSG_DEBUG, "VLAN: br_delbr(%s)", br_name);
     211         [ #  # ]:          0 :         if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
     212                 :          0 :                 wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
     213                 :          0 :                            "failed: %s", __func__, strerror(errno));
     214                 :          0 :                 return -1;
     215                 :            :         }
     216                 :            : 
     217                 :          0 :         arg[0] = BRCTL_DEL_BRIDGE;
     218                 :          0 :         arg[1] = (unsigned long) br_name;
     219                 :            : 
     220 [ #  # ][ #  # ]:          0 :         if (ioctl(fd, SIOCGIFBR, arg) < 0 && errno != ENXIO) {
     221                 :            :                 /* No error if bridge already removed. */
     222                 :          0 :                 wpa_printf(MSG_ERROR, "VLAN: %s: BRCTL_DEL_BRIDGE failed for "
     223                 :          0 :                            "%s: %s", __func__, br_name, strerror(errno));
     224                 :          0 :                 close(fd);
     225                 :          0 :                 return -1;
     226                 :            :         }
     227                 :            : 
     228                 :          0 :         close(fd);
     229                 :          0 :         return 0;
     230                 :            : }
     231                 :            : 
     232                 :            : 
     233                 :            : /*
     234                 :            :         Add a bridge with the name 'br_name'.
     235                 :            : 
     236                 :            :         returns -1 on error
     237                 :            :         returns 1 if the bridge already exists
     238                 :            :         returns 0 otherwise
     239                 :            : */
     240                 :          0 : static int br_addbr(const char *br_name)
     241                 :            : {
     242                 :            :         int fd;
     243                 :            :         unsigned long arg[4];
     244                 :            :         struct ifreq ifr;
     245                 :            : 
     246                 :          0 :         wpa_printf(MSG_DEBUG, "VLAN: br_addbr(%s)", br_name);
     247         [ #  # ]:          0 :         if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
     248                 :          0 :                 wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
     249                 :          0 :                            "failed: %s", __func__, strerror(errno));
     250                 :          0 :                 return -1;
     251                 :            :         }
     252                 :            : 
     253                 :          0 :         arg[0] = BRCTL_ADD_BRIDGE;
     254                 :          0 :         arg[1] = (unsigned long) br_name;
     255                 :            : 
     256         [ #  # ]:          0 :         if (ioctl(fd, SIOCGIFBR, arg) < 0) {
     257         [ #  # ]:          0 :                 if (errno == EEXIST) {
     258                 :            :                         /* The bridge is already added. */
     259                 :          0 :                         close(fd);
     260                 :          0 :                         return 1;
     261                 :            :                 } else {
     262                 :          0 :                         wpa_printf(MSG_ERROR, "VLAN: %s: BRCTL_ADD_BRIDGE "
     263                 :            :                                    "failed for %s: %s",
     264                 :          0 :                                    __func__, br_name, strerror(errno));
     265                 :          0 :                         close(fd);
     266                 :          0 :                         return -1;
     267                 :            :                 }
     268                 :            :         }
     269                 :            : 
     270                 :            :         /* Decrease forwarding delay to avoid EAPOL timeouts. */
     271                 :          0 :         os_memset(&ifr, 0, sizeof(ifr));
     272                 :          0 :         os_strlcpy(ifr.ifr_name, br_name, IFNAMSIZ);
     273                 :          0 :         arg[0] = BRCTL_SET_BRIDGE_FORWARD_DELAY;
     274                 :          0 :         arg[1] = 1;
     275                 :          0 :         arg[2] = 0;
     276                 :          0 :         arg[3] = 0;
     277                 :          0 :         ifr.ifr_data = (char *) &arg;
     278         [ #  # ]:          0 :         if (ioctl(fd, SIOCDEVPRIVATE, &ifr) < 0) {
     279                 :          0 :                 wpa_printf(MSG_ERROR, "VLAN: %s: "
     280                 :            :                            "BRCTL_SET_BRIDGE_FORWARD_DELAY (1 sec) failed for "
     281                 :          0 :                            "%s: %s", __func__, br_name, strerror(errno));
     282                 :            :                 /* Continue anyway */
     283                 :            :         }
     284                 :            : 
     285                 :          0 :         close(fd);
     286                 :          0 :         return 0;
     287                 :            : }
     288                 :            : 
     289                 :            : 
     290                 :          0 : static int br_getnumports(const char *br_name)
     291                 :            : {
     292                 :            :         int fd;
     293                 :            :         int i;
     294                 :          0 :         int port_cnt = 0;
     295                 :            :         unsigned long arg[4];
     296                 :            :         int ifindices[MAX_BR_PORTS];
     297                 :            :         struct ifreq ifr;
     298                 :            : 
     299         [ #  # ]:          0 :         if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
     300                 :          0 :                 wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
     301                 :          0 :                            "failed: %s", __func__, strerror(errno));
     302                 :          0 :                 return -1;
     303                 :            :         }
     304                 :            : 
     305                 :          0 :         arg[0] = BRCTL_GET_PORT_LIST;
     306                 :          0 :         arg[1] = (unsigned long) ifindices;
     307                 :          0 :         arg[2] = MAX_BR_PORTS;
     308                 :          0 :         arg[3] = 0;
     309                 :            : 
     310                 :          0 :         os_memset(ifindices, 0, sizeof(ifindices));
     311                 :          0 :         os_strlcpy(ifr.ifr_name, br_name, sizeof(ifr.ifr_name));
     312                 :          0 :         ifr.ifr_data = (__caddr_t) arg;
     313                 :            : 
     314         [ #  # ]:          0 :         if (ioctl(fd, SIOCDEVPRIVATE, &ifr) < 0) {
     315                 :          0 :                 wpa_printf(MSG_ERROR, "VLAN: %s: BRCTL_GET_PORT_LIST "
     316                 :            :                            "failed for %s: %s",
     317                 :          0 :                            __func__, br_name, strerror(errno));
     318                 :          0 :                 close(fd);
     319                 :          0 :                 return -1;
     320                 :            :         }
     321                 :            : 
     322         [ #  # ]:          0 :         for (i = 1; i < MAX_BR_PORTS; i++) {
     323         [ #  # ]:          0 :                 if (ifindices[i] > 0) {
     324                 :          0 :                         port_cnt++;
     325                 :            :                 }
     326                 :            :         }
     327                 :            : 
     328                 :          0 :         close(fd);
     329                 :          0 :         return port_cnt;
     330                 :            : }
     331                 :            : 
     332                 :            : 
     333                 :            : #ifndef CONFIG_VLAN_NETLINK
     334                 :            : 
     335                 :            : int vlan_rem(const char *if_name)
     336                 :            : {
     337                 :            :         int fd;
     338                 :            :         struct vlan_ioctl_args if_request;
     339                 :            : 
     340                 :            :         wpa_printf(MSG_DEBUG, "VLAN: vlan_rem(%s)", if_name);
     341                 :            :         if ((os_strlen(if_name) + 1) > sizeof(if_request.device1)) {
     342                 :            :                 wpa_printf(MSG_ERROR, "VLAN: Interface name too long: '%s'",
     343                 :            :                            if_name);
     344                 :            :                 return -1;
     345                 :            :         }
     346                 :            : 
     347                 :            :         if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
     348                 :            :                 wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
     349                 :            :                            "failed: %s", __func__, strerror(errno));
     350                 :            :                 return -1;
     351                 :            :         }
     352                 :            : 
     353                 :            :         os_memset(&if_request, 0, sizeof(if_request));
     354                 :            : 
     355                 :            :         os_strlcpy(if_request.device1, if_name, sizeof(if_request.device1));
     356                 :            :         if_request.cmd = DEL_VLAN_CMD;
     357                 :            : 
     358                 :            :         if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
     359                 :            :                 wpa_printf(MSG_ERROR, "VLAN: %s: DEL_VLAN_CMD failed for %s: "
     360                 :            :                            "%s", __func__, if_name, strerror(errno));
     361                 :            :                 close(fd);
     362                 :            :                 return -1;
     363                 :            :         }
     364                 :            : 
     365                 :            :         close(fd);
     366                 :            :         return 0;
     367                 :            : }
     368                 :            : 
     369                 :            : 
     370                 :            : /*
     371                 :            :         Add a vlan interface with VLAN ID 'vid' and tagged interface
     372                 :            :         'if_name'.
     373                 :            : 
     374                 :            :         returns -1 on error
     375                 :            :         returns 1 if the interface already exists
     376                 :            :         returns 0 otherwise
     377                 :            : */
     378                 :            : int vlan_add(const char *if_name, int vid, const char *vlan_if_name)
     379                 :            : {
     380                 :            :         int fd;
     381                 :            :         struct vlan_ioctl_args if_request;
     382                 :            : 
     383                 :            :         wpa_printf(MSG_DEBUG, "VLAN: vlan_add(if_name=%s, vid=%d)",
     384                 :            :                    if_name, vid);
     385                 :            :         ifconfig_up(if_name);
     386                 :            : 
     387                 :            :         if ((os_strlen(if_name) + 1) > sizeof(if_request.device1)) {
     388                 :            :                 wpa_printf(MSG_ERROR, "VLAN: Interface name too long: '%s'",
     389                 :            :                            if_name);
     390                 :            :                 return -1;
     391                 :            :         }
     392                 :            : 
     393                 :            :         if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
     394                 :            :                 wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
     395                 :            :                            "failed: %s", __func__, strerror(errno));
     396                 :            :                 return -1;
     397                 :            :         }
     398                 :            : 
     399                 :            :         os_memset(&if_request, 0, sizeof(if_request));
     400                 :            : 
     401                 :            :         /* Determine if a suitable vlan device already exists. */
     402                 :            : 
     403                 :            :         os_snprintf(if_request.device1, sizeof(if_request.device1), "vlan%d",
     404                 :            :                     vid);
     405                 :            : 
     406                 :            :         if_request.cmd = _GET_VLAN_VID_CMD;
     407                 :            : 
     408                 :            :         if (ioctl(fd, SIOCSIFVLAN, &if_request) == 0) {
     409                 :            : 
     410                 :            :                 if (if_request.u.VID == vid) {
     411                 :            :                         if_request.cmd = _GET_VLAN_REALDEV_NAME_CMD;
     412                 :            : 
     413                 :            :                         if (ioctl(fd, SIOCSIFVLAN, &if_request) == 0 &&
     414                 :            :                             os_strncmp(if_request.u.device2, if_name,
     415                 :            :                                        sizeof(if_request.u.device2)) == 0) {
     416                 :            :                                 close(fd);
     417                 :            :                                 wpa_printf(MSG_DEBUG, "VLAN: vlan_add: "
     418                 :            :                                            "if_name %s exists already",
     419                 :            :                                            if_request.device1);
     420                 :            :                                 return 1;
     421                 :            :                         }
     422                 :            :                 }
     423                 :            :         }
     424                 :            : 
     425                 :            :         /* A suitable vlan device does not already exist, add one. */
     426                 :            : 
     427                 :            :         os_memset(&if_request, 0, sizeof(if_request));
     428                 :            :         os_strlcpy(if_request.device1, if_name, sizeof(if_request.device1));
     429                 :            :         if_request.u.VID = vid;
     430                 :            :         if_request.cmd = ADD_VLAN_CMD;
     431                 :            : 
     432                 :            :         if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
     433                 :            :                 wpa_printf(MSG_ERROR, "VLAN: %s: ADD_VLAN_CMD failed for %s: "
     434                 :            :                            "%s",
     435                 :            :                            __func__, if_request.device1, strerror(errno));
     436                 :            :                 close(fd);
     437                 :            :                 return -1;
     438                 :            :         }
     439                 :            : 
     440                 :            :         close(fd);
     441                 :            :         return 0;
     442                 :            : }
     443                 :            : 
     444                 :            : 
     445                 :            : static int vlan_set_name_type(unsigned int name_type)
     446                 :            : {
     447                 :            :         int fd;
     448                 :            :         struct vlan_ioctl_args if_request;
     449                 :            : 
     450                 :            :         wpa_printf(MSG_DEBUG, "VLAN: vlan_set_name_type(name_type=%u)",
     451                 :            :                    name_type);
     452                 :            :         if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
     453                 :            :                 wpa_printf(MSG_ERROR, "VLAN: %s: socket(AF_INET,SOCK_STREAM) "
     454                 :            :                            "failed: %s", __func__, strerror(errno));
     455                 :            :                 return -1;
     456                 :            :         }
     457                 :            : 
     458                 :            :         os_memset(&if_request, 0, sizeof(if_request));
     459                 :            : 
     460                 :            :         if_request.u.name_type = name_type;
     461                 :            :         if_request.cmd = SET_VLAN_NAME_TYPE_CMD;
     462                 :            :         if (ioctl(fd, SIOCSIFVLAN, &if_request) < 0) {
     463                 :            :                 wpa_printf(MSG_ERROR, "VLAN: %s: SET_VLAN_NAME_TYPE_CMD "
     464                 :            :                            "name_type=%u failed: %s",
     465                 :            :                            __func__, name_type, strerror(errno));
     466                 :            :                 close(fd);
     467                 :            :                 return -1;
     468                 :            :         }
     469                 :            : 
     470                 :            :         close(fd);
     471                 :            :         return 0;
     472                 :            : }
     473                 :            : 
     474                 :            : #endif /* CONFIG_VLAN_NETLINK */
     475                 :            : 
     476                 :            : 
     477                 :            : /**
     478                 :            :  * Increase the usage counter for given parent/ifname combination.
     479                 :            :  * If create is set, then this iface is added to the global list.
     480                 :            :  * Returns
     481                 :            :  *      -1 on error
     482                 :            :  *      0 if iface is not in list
     483                 :            :  *      1 if iface is in list (was there or has been added)
     484                 :            :  */
     485                 :          0 : static int hapd_get_dynamic_iface(const char *parent, const char *ifname,
     486                 :            :                                   int create, struct hostapd_data *hapd)
     487                 :            : {
     488                 :            :         size_t i;
     489                 :          0 :         struct hostapd_dynamic_iface *j = NULL, **tmp;
     490                 :          0 :         struct hapd_interfaces *hapd_global = hapd->iface->interfaces;
     491                 :            : 
     492         [ #  # ]:          0 :         if (!parent)
     493                 :          0 :                 parent = "";
     494                 :            : 
     495         [ #  # ]:          0 :         for (i = 0; i < hapd_global->count_dynamic; i++) {
     496                 :          0 :                 j = hapd_global->dynamic_iface[i];
     497 [ #  # ][ #  # ]:          0 :                 if (os_strncmp(j->iface, ifname, sizeof(j->iface)) == 0 &&
     498                 :          0 :                     os_strncmp(j->parent, parent, sizeof(j->parent)) == 0)
     499                 :          0 :                         break;
     500                 :            :         }
     501         [ #  # ]:          0 :         if (i < hapd_global->count_dynamic) {
     502                 :          0 :                 j->usage++;
     503                 :          0 :                 return 1;
     504                 :            :         }
     505                 :            : 
     506                 :            :         /* new entry required */
     507         [ #  # ]:          0 :         if (!create)
     508                 :          0 :                 return 0;
     509                 :            : 
     510                 :          0 :         j = os_zalloc(sizeof(*j));
     511         [ #  # ]:          0 :         if (!j)
     512                 :          0 :                 return -1;
     513                 :          0 :         os_strlcpy(j->iface, ifname, sizeof(j->iface));
     514                 :          0 :         os_strlcpy(j->parent, parent, sizeof(j->parent));
     515                 :            : 
     516                 :          0 :         tmp = os_realloc_array(hapd_global->dynamic_iface, i + 1,
     517                 :            :                                sizeof(*hapd_global->dynamic_iface));
     518         [ #  # ]:          0 :         if (!tmp) {
     519                 :          0 :                 wpa_printf(MSG_ERROR, "VLAN: Failed to allocate memory in %s",
     520                 :            :                            __func__);
     521                 :          0 :                 return -1;
     522                 :            :         }
     523                 :          0 :         hapd_global->count_dynamic++;
     524                 :          0 :         hapd_global->dynamic_iface = tmp;
     525                 :          0 :         hapd_global->dynamic_iface[i] = j;
     526                 :            : 
     527                 :          0 :         return 1;
     528                 :            : }
     529                 :            : 
     530                 :            : 
     531                 :            : /**
     532                 :            :  * Decrease the usage counter for given ifname.
     533                 :            :  * Returns
     534                 :            :  *     -1 on error or if iface was not found
     535                 :            :  *     0 if iface was found and is still present
     536                 :            :  *     1 if iface was removed from global list
     537                 :            :  */
     538                 :          0 : static int hapd_put_dynamic_iface(const char *parent, const char *ifname,
     539                 :            :                                   struct hostapd_data *hapd)
     540                 :            : {
     541                 :            :         size_t i;
     542                 :          0 :         struct hostapd_dynamic_iface *j = NULL, **tmp;
     543                 :          0 :         struct hapd_interfaces *hapd_glob = hapd->iface->interfaces;
     544                 :            : 
     545         [ #  # ]:          0 :         if (!parent)
     546                 :          0 :                 parent = "";
     547                 :            : 
     548         [ #  # ]:          0 :         for (i = 0; i < hapd_glob->count_dynamic; i++) {
     549                 :          0 :                 j = hapd_glob->dynamic_iface[i];
     550 [ #  # ][ #  # ]:          0 :                 if (os_strncmp(j->iface, ifname, sizeof(j->iface)) == 0 &&
     551                 :          0 :                     os_strncmp(j->parent, parent, sizeof(j->parent)) == 0)
     552                 :          0 :                         break;
     553                 :            :         }
     554                 :            : 
     555         [ #  # ]:          0 :         if (i == hapd_glob->count_dynamic) {
     556                 :            :                 /*
     557                 :            :                  * Interface not in global list. This can happen if alloc in
     558                 :            :                  * _get_ failed.
     559                 :            :                  */
     560                 :          0 :                 return -1;
     561                 :            :         }
     562                 :            : 
     563         [ #  # ]:          0 :         if (j->usage > 0) {
     564                 :          0 :                 j->usage--;
     565                 :          0 :                 return 0;
     566                 :            :         }
     567                 :            : 
     568                 :          0 :         os_free(j);
     569         [ #  # ]:          0 :         for (; i < hapd_glob->count_dynamic - 1; i++)
     570                 :          0 :                 hapd_glob->dynamic_iface[i] = hapd_glob->dynamic_iface[i + 1];
     571                 :          0 :         hapd_glob->dynamic_iface[hapd_glob->count_dynamic - 1] = NULL;
     572                 :          0 :         hapd_glob->count_dynamic--;
     573                 :            : 
     574         [ #  # ]:          0 :         if (hapd_glob->count_dynamic == 0) {
     575                 :          0 :                 os_free(hapd_glob->dynamic_iface);
     576                 :          0 :                 hapd_glob->dynamic_iface = NULL;
     577                 :          0 :                 return 1;
     578                 :            :         }
     579                 :            : 
     580                 :          0 :         tmp = os_realloc_array(hapd_glob->dynamic_iface,
     581                 :            :                                hapd_glob->count_dynamic,
     582                 :            :                                sizeof(*hapd_glob->dynamic_iface));
     583         [ #  # ]:          0 :         if (!tmp) {
     584                 :          0 :                 wpa_printf(MSG_ERROR, "VLAN: Failed to release memory in %s",
     585                 :            :                            __func__);
     586                 :          0 :                 return -1;
     587                 :            :         }
     588                 :          0 :         hapd_glob->dynamic_iface = tmp;
     589                 :            : 
     590                 :          0 :         return 1;
     591                 :            : }
     592                 :            : 
     593                 :            : 
     594                 :       1649 : static void vlan_newlink(char *ifname, struct hostapd_data *hapd)
     595                 :            : {
     596                 :            :         char vlan_ifname[IFNAMSIZ];
     597                 :            :         char br_name[IFNAMSIZ];
     598                 :       1649 :         struct hostapd_vlan *vlan = hapd->conf->vlan;
     599                 :       1649 :         char *tagged_interface = hapd->conf->ssid.vlan_tagged_interface;
     600                 :       1649 :         int vlan_naming = hapd->conf->ssid.vlan_naming;
     601                 :            :         int ret;
     602                 :            : 
     603                 :       1649 :         wpa_printf(MSG_DEBUG, "VLAN: vlan_newlink(%s)", ifname);
     604                 :            : 
     605         [ -  + ]:       1649 :         while (vlan) {
     606         [ #  # ]:          0 :                 if (os_strcmp(ifname, vlan->ifname) == 0) {
     607                 :            : 
     608         [ #  # ]:          0 :                         if (hapd->conf->vlan_bridge[0]) {
     609                 :          0 :                                 os_snprintf(br_name, sizeof(br_name), "%s%d",
     610                 :          0 :                                             hapd->conf->vlan_bridge,
     611                 :            :                                             vlan->vlan_id);
     612         [ #  # ]:          0 :                         } else if (tagged_interface) {
     613                 :          0 :                                 os_snprintf(br_name, sizeof(br_name),
     614                 :            :                                             "br%s.%d", tagged_interface,
     615                 :            :                                             vlan->vlan_id);
     616                 :            :                         } else {
     617                 :          0 :                                 os_snprintf(br_name, sizeof(br_name),
     618                 :            :                                             "brvlan%d", vlan->vlan_id);
     619                 :            :                         }
     620                 :            : 
     621                 :          0 :                         ret = br_addbr(br_name);
     622         [ #  # ]:          0 :                         if (hapd_get_dynamic_iface(NULL, br_name, ret == 0,
     623                 :            :                                                    hapd))
     624                 :          0 :                                 vlan->clean |= DVLAN_CLEAN_BR;
     625                 :            : 
     626                 :          0 :                         ifconfig_up(br_name);
     627                 :            : 
     628         [ #  # ]:          0 :                         if (tagged_interface) {
     629         [ #  # ]:          0 :                                 if (vlan_naming ==
     630                 :            :                                     DYNAMIC_VLAN_NAMING_WITH_DEVICE)
     631                 :          0 :                                         os_snprintf(vlan_ifname,
     632                 :            :                                                     sizeof(vlan_ifname),
     633                 :            :                                                     "%s.%d", tagged_interface,
     634                 :            :                                                     vlan->vlan_id);
     635                 :            :                                 else
     636                 :          0 :                                         os_snprintf(vlan_ifname,
     637                 :            :                                                     sizeof(vlan_ifname),
     638                 :            :                                                     "vlan%d", vlan->vlan_id);
     639                 :            : 
     640                 :          0 :                                 ifconfig_up(tagged_interface);
     641                 :          0 :                                 ret = vlan_add(tagged_interface, vlan->vlan_id,
     642                 :            :                                               vlan_ifname);
     643         [ #  # ]:          0 :                                 if (hapd_get_dynamic_iface(NULL, vlan_ifname,
     644                 :            :                                                            ret == 0, hapd))
     645                 :          0 :                                         vlan->clean |= DVLAN_CLEAN_VLAN;
     646                 :            : 
     647                 :          0 :                                 ret = br_addif(br_name, vlan_ifname);
     648         [ #  # ]:          0 :                                 if (hapd_get_dynamic_iface(br_name,
     649                 :            :                                                            vlan_ifname,
     650                 :            :                                                            ret == 0, hapd))
     651                 :          0 :                                         vlan->clean |= DVLAN_CLEAN_VLAN_PORT;
     652                 :            : 
     653                 :          0 :                                 ifconfig_up(vlan_ifname);
     654                 :            :                         }
     655                 :            : 
     656                 :          0 :                         ret = br_addif(br_name, ifname);
     657         [ #  # ]:          0 :                         if (hapd_get_dynamic_iface(br_name, ifname, ret == 0,
     658                 :            :                                                    hapd))
     659                 :          0 :                                 vlan->clean |= DVLAN_CLEAN_WLAN_PORT;
     660                 :            : 
     661                 :          0 :                         ifconfig_up(ifname);
     662                 :            : 
     663                 :          0 :                         break;
     664                 :            :                 }
     665                 :          0 :                 vlan = vlan->next;
     666                 :            :         }
     667                 :       1649 : }
     668                 :            : 
     669                 :            : 
     670                 :         23 : static void vlan_dellink(char *ifname, struct hostapd_data *hapd)
     671                 :            : {
     672                 :            :         char vlan_ifname[IFNAMSIZ];
     673                 :            :         char br_name[IFNAMSIZ];
     674                 :         23 :         struct hostapd_vlan *first, *prev, *vlan = hapd->conf->vlan;
     675                 :         23 :         char *tagged_interface = hapd->conf->ssid.vlan_tagged_interface;
     676                 :         23 :         int vlan_naming = hapd->conf->ssid.vlan_naming;
     677                 :            : 
     678                 :         23 :         wpa_printf(MSG_DEBUG, "VLAN: vlan_dellink(%s)", ifname);
     679                 :            : 
     680                 :         23 :         first = prev = vlan;
     681                 :            : 
     682         [ -  + ]:         23 :         while (vlan) {
     683         [ #  # ]:          0 :                 if (os_strcmp(ifname, vlan->ifname) == 0) {
     684         [ #  # ]:          0 :                         if (hapd->conf->vlan_bridge[0]) {
     685                 :          0 :                                 os_snprintf(br_name, sizeof(br_name), "%s%d",
     686                 :          0 :                                             hapd->conf->vlan_bridge,
     687                 :            :                                             vlan->vlan_id);
     688         [ #  # ]:          0 :                         } else if (tagged_interface) {
     689                 :          0 :                                 os_snprintf(br_name, sizeof(br_name),
     690                 :            :                                             "br%s.%d", tagged_interface,
     691                 :            :                                             vlan->vlan_id);
     692                 :            :                         } else {
     693                 :          0 :                                 os_snprintf(br_name, sizeof(br_name),
     694                 :            :                                             "brvlan%d", vlan->vlan_id);
     695                 :            :                         }
     696                 :            : 
     697   [ #  #  #  # ]:          0 :                         if ((vlan->clean & DVLAN_CLEAN_WLAN_PORT) &&
     698                 :          0 :                             hapd_put_dynamic_iface(br_name, vlan->ifname, hapd))
     699                 :          0 :                                 br_delif(br_name, vlan->ifname);
     700                 :            : 
     701         [ #  # ]:          0 :                         if (tagged_interface) {
     702         [ #  # ]:          0 :                                 if (vlan_naming ==
     703                 :            :                                     DYNAMIC_VLAN_NAMING_WITH_DEVICE)
     704                 :          0 :                                         os_snprintf(vlan_ifname,
     705                 :            :                                                     sizeof(vlan_ifname),
     706                 :            :                                                     "%s.%d", tagged_interface,
     707                 :            :                                                     vlan->vlan_id);
     708                 :            :                                 else
     709                 :          0 :                                         os_snprintf(vlan_ifname,
     710                 :            :                                                     sizeof(vlan_ifname),
     711                 :            :                                                     "vlan%d", vlan->vlan_id);
     712   [ #  #  #  # ]:          0 :                                 if ((vlan->clean & DVLAN_CLEAN_VLAN_PORT) &&
     713                 :          0 :                                     hapd_put_dynamic_iface(br_name, vlan_ifname,
     714                 :            :                                                            hapd))
     715                 :          0 :                                         br_delif(br_name, vlan_ifname);
     716                 :          0 :                                 ifconfig_down(vlan_ifname);
     717                 :            : 
     718   [ #  #  #  # ]:          0 :                                 if ((vlan->clean & DVLAN_CLEAN_VLAN) &&
     719                 :          0 :                                     hapd_put_dynamic_iface(NULL, vlan_ifname,
     720                 :            :                                                            hapd))
     721                 :          0 :                                         vlan_rem(vlan_ifname);
     722                 :            :                         }
     723                 :            : 
     724   [ #  #  #  # ]:          0 :                         if ((vlan->clean & DVLAN_CLEAN_BR) &&
     725         [ #  # ]:          0 :                             hapd_put_dynamic_iface(NULL, br_name, hapd) &&
     726                 :          0 :                             br_getnumports(br_name) == 0) {
     727                 :          0 :                                 ifconfig_down(br_name);
     728                 :          0 :                                 br_delbr(br_name);
     729                 :            :                         }
     730                 :            : 
     731         [ #  # ]:          0 :                         if (vlan == first) {
     732                 :          0 :                                 hapd->conf->vlan = vlan->next;
     733                 :            :                         } else {
     734                 :          0 :                                 prev->next = vlan->next;
     735                 :            :                         }
     736                 :          0 :                         os_free(vlan);
     737                 :            : 
     738                 :          0 :                         break;
     739                 :            :                 }
     740                 :          0 :                 prev = vlan;
     741                 :          0 :                 vlan = vlan->next;
     742                 :            :         }
     743                 :         23 : }
     744                 :            : 
     745                 :            : 
     746                 :            : static void
     747                 :       1672 : vlan_read_ifnames(struct nlmsghdr *h, size_t len, int del,
     748                 :            :                   struct hostapd_data *hapd)
     749                 :            : {
     750                 :            :         struct ifinfomsg *ifi;
     751                 :            :         int attrlen, nlmsg_len, rta_len;
     752                 :            :         struct rtattr *attr;
     753                 :            : 
     754         [ -  + ]:       1672 :         if (len < sizeof(*ifi))
     755                 :          0 :                 return;
     756                 :            : 
     757                 :       1672 :         ifi = NLMSG_DATA(h);
     758                 :            : 
     759                 :       1672 :         nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
     760                 :            : 
     761                 :       1672 :         attrlen = h->nlmsg_len - nlmsg_len;
     762         [ -  + ]:       1672 :         if (attrlen < 0)
     763                 :          0 :                 return;
     764                 :            : 
     765                 :       1672 :         attr = (struct rtattr *) (((char *) ifi) + nlmsg_len);
     766                 :            : 
     767                 :       1672 :         rta_len = RTA_ALIGN(sizeof(struct rtattr));
     768 [ +  - ][ +  + ]:      30096 :         while (RTA_OK(attr, attrlen)) {
                 [ +  + ]
     769                 :            :                 char ifname[IFNAMSIZ + 1];
     770                 :            : 
     771         [ +  + ]:      28424 :                 if (attr->rta_type == IFLA_IFNAME) {
     772                 :       1672 :                         int n = attr->rta_len - rta_len;
     773         [ -  + ]:       1672 :                         if (n < 0)
     774                 :          0 :                                 break;
     775                 :            : 
     776                 :       1672 :                         os_memset(ifname, 0, sizeof(ifname));
     777                 :            : 
     778         [ -  + ]:       1672 :                         if ((size_t) n > sizeof(ifname))
     779                 :          0 :                                 n = sizeof(ifname);
     780                 :       1672 :                         os_memcpy(ifname, ((char *) attr) + rta_len, n);
     781                 :            : 
     782         [ +  + ]:       1672 :                         if (del)
     783                 :         23 :                                 vlan_dellink(ifname, hapd);
     784                 :            :                         else
     785                 :       1649 :                                 vlan_newlink(ifname, hapd);
     786                 :            :                 }
     787                 :            : 
     788                 :      28424 :                 attr = RTA_NEXT(attr, attrlen);
     789                 :            :         }
     790                 :            : }
     791                 :            : 
     792                 :            : 
     793                 :       1672 : static void vlan_event_receive(int sock, void *eloop_ctx, void *sock_ctx)
     794                 :            : {
     795                 :            :         char buf[8192];
     796                 :            :         int left;
     797                 :            :         struct sockaddr_nl from;
     798                 :            :         socklen_t fromlen;
     799                 :            :         struct nlmsghdr *h;
     800                 :       1672 :         struct hostapd_data *hapd = eloop_ctx;
     801                 :            : 
     802                 :       1672 :         fromlen = sizeof(from);
     803                 :       1672 :         left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
     804                 :            :                         (struct sockaddr *) &from, &fromlen);
     805         [ -  + ]:       1672 :         if (left < 0) {
     806 [ #  # ][ #  # ]:          0 :                 if (errno != EINTR && errno != EAGAIN)
     807                 :          0 :                         wpa_printf(MSG_ERROR, "VLAN: %s: recvfrom failed: %s",
     808                 :          0 :                                    __func__, strerror(errno));
     809                 :       1672 :                 return;
     810                 :            :         }
     811                 :            : 
     812                 :       1672 :         h = (struct nlmsghdr *) buf;
     813         [ +  + ]:       3344 :         while (left >= (int) sizeof(*h)) {
     814                 :            :                 int len, plen;
     815                 :            : 
     816                 :       1672 :                 len = h->nlmsg_len;
     817                 :       1672 :                 plen = len - sizeof(*h);
     818 [ +  - ][ -  + ]:       1672 :                 if (len > left || plen < 0) {
     819                 :          0 :                         wpa_printf(MSG_DEBUG, "VLAN: Malformed netlink "
     820                 :            :                                    "message: len=%d left=%d plen=%d",
     821                 :            :                                    len, left, plen);
     822                 :          0 :                         break;
     823                 :            :                 }
     824                 :            : 
     825      [ +  +  - ]:       1672 :                 switch (h->nlmsg_type) {
     826                 :            :                 case RTM_NEWLINK:
     827                 :       1649 :                         vlan_read_ifnames(h, plen, 0, hapd);
     828                 :       1649 :                         break;
     829                 :            :                 case RTM_DELLINK:
     830                 :         23 :                         vlan_read_ifnames(h, plen, 1, hapd);
     831                 :         23 :                         break;
     832                 :            :                 }
     833                 :            : 
     834                 :       1672 :                 len = NLMSG_ALIGN(len);
     835                 :       1672 :                 left -= len;
     836                 :       1672 :                 h = (struct nlmsghdr *) ((char *) h + len);
     837                 :            :         }
     838                 :            : 
     839         [ -  + ]:       1672 :         if (left > 0) {
     840                 :          0 :                 wpa_printf(MSG_DEBUG, "VLAN: %s: %d extra bytes in the end of "
     841                 :            :                            "netlink message", __func__, left);
     842                 :            :         }
     843                 :            : }
     844                 :            : 
     845                 :            : 
     846                 :            : static struct full_dynamic_vlan *
     847                 :        200 : full_dynamic_vlan_init(struct hostapd_data *hapd)
     848                 :            : {
     849                 :            :         struct sockaddr_nl local;
     850                 :            :         struct full_dynamic_vlan *priv;
     851                 :            : 
     852                 :        200 :         priv = os_zalloc(sizeof(*priv));
     853         [ -  + ]:        200 :         if (priv == NULL)
     854                 :          0 :                 return NULL;
     855                 :            : 
     856                 :            : #ifndef CONFIG_VLAN_NETLINK
     857                 :            :         vlan_set_name_type(hapd->conf->ssid.vlan_naming ==
     858                 :            :                            DYNAMIC_VLAN_NAMING_WITH_DEVICE ?
     859                 :            :                            VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD :
     860                 :            :                            VLAN_NAME_TYPE_PLUS_VID_NO_PAD);
     861                 :            : #endif /* CONFIG_VLAN_NETLINK */
     862                 :            : 
     863                 :        200 :         priv->s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
     864         [ -  + ]:        200 :         if (priv->s < 0) {
     865                 :          0 :                 wpa_printf(MSG_ERROR, "VLAN: %s: socket(PF_NETLINK,SOCK_RAW,"
     866                 :            :                            "NETLINK_ROUTE) failed: %s",
     867                 :          0 :                            __func__, strerror(errno));
     868                 :          0 :                 os_free(priv);
     869                 :          0 :                 return NULL;
     870                 :            :         }
     871                 :            : 
     872                 :        200 :         os_memset(&local, 0, sizeof(local));
     873                 :        200 :         local.nl_family = AF_NETLINK;
     874                 :        200 :         local.nl_groups = RTMGRP_LINK;
     875         [ -  + ]:        200 :         if (bind(priv->s, (struct sockaddr *) &local, sizeof(local)) < 0) {
     876                 :          0 :                 wpa_printf(MSG_ERROR, "VLAN: %s: bind(netlink) failed: %s",
     877                 :          0 :                            __func__, strerror(errno));
     878                 :          0 :                 close(priv->s);
     879                 :          0 :                 os_free(priv);
     880                 :          0 :                 return NULL;
     881                 :            :         }
     882                 :            : 
     883         [ -  + ]:        200 :         if (eloop_register_read_sock(priv->s, vlan_event_receive, hapd, NULL))
     884                 :            :         {
     885                 :          0 :                 close(priv->s);
     886                 :          0 :                 os_free(priv);
     887                 :          0 :                 return NULL;
     888                 :            :         }
     889                 :            : 
     890                 :        200 :         return priv;
     891                 :            : }
     892                 :            : 
     893                 :            : 
     894                 :        201 : static void full_dynamic_vlan_deinit(struct full_dynamic_vlan *priv)
     895                 :            : {
     896         [ +  + ]:        201 :         if (priv == NULL)
     897                 :        201 :                 return;
     898                 :        200 :         eloop_unregister_read_sock(priv->s);
     899                 :        200 :         close(priv->s);
     900                 :        200 :         os_free(priv);
     901                 :            : }
     902                 :            : #endif /* CONFIG_FULL_DYNAMIC_VLAN */
     903                 :            : 
     904                 :            : 
     905                 :          0 : int vlan_setup_encryption_dyn(struct hostapd_data *hapd,
     906                 :            :                               struct hostapd_ssid *mssid, const char *dyn_vlan)
     907                 :            : {
     908                 :            :         int i;
     909                 :            : 
     910         [ #  # ]:          0 :         if (dyn_vlan == NULL)
     911                 :          0 :                 return 0;
     912                 :            : 
     913                 :            :         /* Static WEP keys are set here; IEEE 802.1X and WPA uses their own
     914                 :            :          * functions for setting up dynamic broadcast keys. */
     915         [ #  # ]:          0 :         for (i = 0; i < 4; i++) {
     916   [ #  #  #  # ]:          0 :                 if (mssid->wep.key[i] &&
     917                 :          0 :                     hostapd_drv_set_key(dyn_vlan, hapd, WPA_ALG_WEP, NULL, i,
     918                 :          0 :                                         i == mssid->wep.idx, NULL, 0,
     919                 :          0 :                                         mssid->wep.key[i], mssid->wep.len[i]))
     920                 :            :                 {
     921                 :          0 :                         wpa_printf(MSG_ERROR, "VLAN: Could not set WEP "
     922                 :            :                                    "encryption for dynamic VLAN");
     923                 :          0 :                         return -1;
     924                 :            :                 }
     925                 :            :         }
     926                 :            : 
     927                 :          0 :         return 0;
     928                 :            : }
     929                 :            : 
     930                 :            : 
     931                 :        200 : static int vlan_dynamic_add(struct hostapd_data *hapd,
     932                 :            :                             struct hostapd_vlan *vlan)
     933                 :            : {
     934         [ -  + ]:        200 :         while (vlan) {
     935         [ #  # ]:          0 :                 if (vlan->vlan_id != VLAN_ID_WILDCARD) {
     936         [ #  # ]:          0 :                         if (hostapd_vlan_if_add(hapd, vlan->ifname)) {
     937         [ #  # ]:          0 :                                 if (errno != EEXIST) {
     938                 :          0 :                                         wpa_printf(MSG_ERROR, "VLAN: Could "
     939                 :            :                                                    "not add VLAN %s: %s",
     940                 :          0 :                                                    vlan->ifname,
     941                 :          0 :                                                    strerror(errno));
     942                 :          0 :                                         return -1;
     943                 :            :                                 }
     944                 :            :                         }
     945                 :            : #ifdef CONFIG_FULL_DYNAMIC_VLAN
     946                 :          0 :                         ifconfig_up(vlan->ifname);
     947                 :            : #endif /* CONFIG_FULL_DYNAMIC_VLAN */
     948                 :            :                 }
     949                 :            : 
     950                 :          0 :                 vlan = vlan->next;
     951                 :            :         }
     952                 :            : 
     953                 :        200 :         return 0;
     954                 :            : }
     955                 :            : 
     956                 :            : 
     957                 :        201 : static void vlan_dynamic_remove(struct hostapd_data *hapd,
     958                 :            :                                 struct hostapd_vlan *vlan)
     959                 :            : {
     960                 :            :         struct hostapd_vlan *next;
     961                 :            : 
     962         [ -  + ]:        201 :         while (vlan) {
     963                 :          0 :                 next = vlan->next;
     964                 :            : 
     965   [ #  #  #  # ]:          0 :                 if (vlan->vlan_id != VLAN_ID_WILDCARD &&
     966                 :          0 :                     hostapd_vlan_if_remove(hapd, vlan->ifname)) {
     967                 :          0 :                         wpa_printf(MSG_ERROR, "VLAN: Could not remove VLAN "
     968                 :            :                                    "iface: %s: %s",
     969                 :          0 :                                    vlan->ifname, strerror(errno));
     970                 :            :                 }
     971                 :            : #ifdef CONFIG_FULL_DYNAMIC_VLAN
     972         [ #  # ]:          0 :                 if (vlan->clean)
     973                 :          0 :                         vlan_dellink(vlan->ifname, hapd);
     974                 :            : #endif /* CONFIG_FULL_DYNAMIC_VLAN */
     975                 :            : 
     976                 :          0 :                 vlan = next;
     977                 :            :         }
     978                 :        201 : }
     979                 :            : 
     980                 :            : 
     981                 :        200 : int vlan_init(struct hostapd_data *hapd)
     982                 :            : {
     983                 :            : #ifdef CONFIG_FULL_DYNAMIC_VLAN
     984                 :        200 :         hapd->full_dynamic_vlan = full_dynamic_vlan_init(hapd);
     985                 :            : #endif /* CONFIG_FULL_DYNAMIC_VLAN */
     986                 :            : 
     987 [ #  # ][ -  + ]:        200 :         if (hapd->conf->ssid.dynamic_vlan != DYNAMIC_VLAN_DISABLED &&
     988                 :          0 :             !hapd->conf->vlan) {
     989                 :            :                 /* dynamic vlans enabled but no (or empty) vlan_file given */
     990                 :            :                 struct hostapd_vlan *vlan;
     991                 :          0 :                 vlan = os_zalloc(sizeof(*vlan));
     992         [ #  # ]:          0 :                 if (vlan == NULL) {
     993                 :          0 :                         wpa_printf(MSG_ERROR, "Out of memory while assigning "
     994                 :            :                                    "VLAN interfaces");
     995                 :          0 :                         return -1;
     996                 :            :                 }
     997                 :            : 
     998                 :          0 :                 vlan->vlan_id = VLAN_ID_WILDCARD;
     999                 :          0 :                 os_snprintf(vlan->ifname, sizeof(vlan->ifname), "%s.#",
    1000                 :          0 :                             hapd->conf->iface);
    1001                 :          0 :                 vlan->next = hapd->conf->vlan;
    1002                 :          0 :                 hapd->conf->vlan = vlan;
    1003                 :            :         }
    1004                 :            : 
    1005         [ -  + ]:        200 :         if (vlan_dynamic_add(hapd, hapd->conf->vlan))
    1006                 :          0 :                 return -1;
    1007                 :            : 
    1008                 :        200 :         return 0;
    1009                 :            : }
    1010                 :            : 
    1011                 :            : 
    1012                 :        201 : void vlan_deinit(struct hostapd_data *hapd)
    1013                 :            : {
    1014                 :        201 :         vlan_dynamic_remove(hapd, hapd->conf->vlan);
    1015                 :            : 
    1016                 :            : #ifdef CONFIG_FULL_DYNAMIC_VLAN
    1017                 :        201 :         full_dynamic_vlan_deinit(hapd->full_dynamic_vlan);
    1018                 :        201 :         hapd->full_dynamic_vlan = NULL;
    1019                 :            : #endif /* CONFIG_FULL_DYNAMIC_VLAN */
    1020                 :        201 : }
    1021                 :            : 
    1022                 :            : 
    1023                 :          0 : struct hostapd_vlan * vlan_add_dynamic(struct hostapd_data *hapd,
    1024                 :            :                                        struct hostapd_vlan *vlan,
    1025                 :            :                                        int vlan_id)
    1026                 :            : {
    1027                 :            :         struct hostapd_vlan *n;
    1028                 :            :         char *ifname, *pos;
    1029                 :            : 
    1030 [ #  # ][ #  # ]:          0 :         if (vlan == NULL || vlan_id <= 0 || vlan_id > MAX_VLAN_ID ||
         [ #  # ][ #  # ]
    1031                 :          0 :             vlan->vlan_id != VLAN_ID_WILDCARD)
    1032                 :          0 :                 return NULL;
    1033                 :            : 
    1034                 :          0 :         wpa_printf(MSG_DEBUG, "VLAN: %s(vlan_id=%d ifname=%s)",
    1035                 :          0 :                    __func__, vlan_id, vlan->ifname);
    1036                 :          0 :         ifname = os_strdup(vlan->ifname);
    1037         [ #  # ]:          0 :         if (ifname == NULL)
    1038                 :          0 :                 return NULL;
    1039                 :          0 :         pos = os_strchr(ifname, '#');
    1040         [ #  # ]:          0 :         if (pos == NULL) {
    1041                 :          0 :                 os_free(ifname);
    1042                 :          0 :                 return NULL;
    1043                 :            :         }
    1044                 :          0 :         *pos++ = '\0';
    1045                 :            : 
    1046                 :          0 :         n = os_zalloc(sizeof(*n));
    1047         [ #  # ]:          0 :         if (n == NULL) {
    1048                 :          0 :                 os_free(ifname);
    1049                 :          0 :                 return NULL;
    1050                 :            :         }
    1051                 :            : 
    1052                 :          0 :         n->vlan_id = vlan_id;
    1053                 :          0 :         n->dynamic_vlan = 1;
    1054                 :            : 
    1055                 :          0 :         os_snprintf(n->ifname, sizeof(n->ifname), "%s%d%s", ifname, vlan_id,
    1056                 :            :                     pos);
    1057                 :          0 :         os_free(ifname);
    1058                 :            : 
    1059         [ #  # ]:          0 :         if (hostapd_vlan_if_add(hapd, n->ifname)) {
    1060                 :          0 :                 os_free(n);
    1061                 :          0 :                 return NULL;
    1062                 :            :         }
    1063                 :            : 
    1064                 :          0 :         n->next = hapd->conf->vlan;
    1065                 :          0 :         hapd->conf->vlan = n;
    1066                 :            : 
    1067                 :            : #ifdef CONFIG_FULL_DYNAMIC_VLAN
    1068                 :          0 :         ifconfig_up(n->ifname);
    1069                 :            : #endif /* CONFIG_FULL_DYNAMIC_VLAN */
    1070                 :            : 
    1071                 :          0 :         return n;
    1072                 :            : }
    1073                 :            : 
    1074                 :            : 
    1075                 :          0 : int vlan_remove_dynamic(struct hostapd_data *hapd, int vlan_id)
    1076                 :            : {
    1077                 :            :         struct hostapd_vlan *vlan;
    1078                 :            : 
    1079 [ #  # ][ #  # ]:          0 :         if (vlan_id <= 0 || vlan_id > MAX_VLAN_ID)
    1080                 :          0 :                 return 1;
    1081                 :            : 
    1082                 :          0 :         wpa_printf(MSG_DEBUG, "VLAN: %s(vlan_id=%d)", __func__, vlan_id);
    1083                 :            : 
    1084                 :          0 :         vlan = hapd->conf->vlan;
    1085         [ #  # ]:          0 :         while (vlan) {
    1086 [ #  # ][ #  # ]:          0 :                 if (vlan->vlan_id == vlan_id && vlan->dynamic_vlan > 0) {
    1087                 :          0 :                         vlan->dynamic_vlan--;
    1088                 :          0 :                         break;
    1089                 :            :                 }
    1090                 :          0 :                 vlan = vlan->next;
    1091                 :            :         }
    1092                 :            : 
    1093         [ #  # ]:          0 :         if (vlan == NULL)
    1094                 :          0 :                 return 1;
    1095                 :            : 
    1096         [ #  # ]:          0 :         if (vlan->dynamic_vlan == 0)
    1097                 :          0 :                 hostapd_vlan_if_remove(hapd, vlan->ifname);
    1098                 :            : 
    1099                 :          0 :         return 0;
    1100                 :            : }

Generated by: LCOV version 1.9