aboutsummaryrefslogtreecommitdiff
path: root/lwip/port/netif/ifcommon.c
blob: 8a18f1d6b66a195942e548195de4e5bfe6380f27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
   Copyright (C) 2017 Free Software Foundation, Inc.
   Written by Joan Lledó.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the GNU Hurd.  If not, see <http://www.gnu.org/licenses/>.
*/

/* Common interface for all kinds of devices */

#include <netif/ifcommon.h>

#include <net/if.h>
#include <errno.h>

#include <lwip/tcpip.h>

/* Open the device and set the interface up */
static error_t
if_open (struct netif *netif)
{
  error_t err = 0;
  struct ifcommon *ifc = netif_get_state (netif);

  if (ifc->open)
    err = ifc->open (netif);
  if (!err)
    {
      /* Up the inerface */
      ifc->flags |= IFF_UP | IFF_RUNNING;
      netif_set_up (netif);
    }

  return err;
}

/* Close the device and set the interface down */
static error_t
if_close (struct netif *netif)
{
  error_t err = 0;
  struct ifcommon *ifc = netif_get_state (netif);

  if (ifc->close)
    err = ifc->close (netif);
  if (!err)
    {
      /* Down the inerface */
      ifc->flags &= ~(IFF_UP | IFF_RUNNING);
      netif_set_down (netif);
    }

  return err;
}

/*
 * Common initialization callback for all kinds of devices.
 *
 * This function doesn't assume there's a device nor tries to open it.
 * If a device is present, it must be opened from the ifc->init() callback.
 */
err_t
if_init (struct netif *netif)
{
  struct ifcommon *ifc = netif_get_state (netif);

  if (ifc == NULL)
    /* The user provided no interface */
    return -1;

  return ifc->init (netif);
}

/* Tries to close the device and frees allocated resources */
error_t
if_terminate (struct netif * netif)
{
  error_t err;
  struct ifcommon *ifc = netif_get_state (netif);

  if (ifc == NULL)
    /* The user provided no interface */
    return -1;

  err = if_close (netif);
  if (err)
    return err;

  return ifc->terminate (netif);
}

/* Args for _if_change_flags() */
struct if_change_flags_args
{
  struct netif *netif;
  uint16_t flags;
  error_t err;
};

/*
 * Implementation of if_change_flags(), called inside a thread-safe context
 */
static void
_if_change_flags (void *arg)
{
  error_t err;
  struct ifcommon *ifc;
  uint16_t oldflags;
  struct if_change_flags_args *args = arg;

  ifc = netif_get_state (args->netif);

  if (ifc == NULL)
    {
      /* The user provided no interface */
      errno = EINVAL;
      return;
    }

  oldflags = ifc->flags;

  err = ifc->change_flags (args->netif, args->flags);

  if (!err && ((oldflags ^ args->flags) & IFF_UP))	/* Bit is different  ? */
    err = ((oldflags & IFF_UP) ? if_close : if_open) (args->netif);

  args->err = err;

  return;
}

/*
 * Change device flags.
 *
 * If IFF_UP changes, it opens/closes the device accordingly.
 */
error_t
if_change_flags (struct netif * netif, uint16_t flags)
{
  error_t err;

  /*
   * Call _if_change_flags() inside the tcpip_thread and wait for it to finish.
   */
  struct if_change_flags_args *args =
    calloc (1, sizeof (struct if_change_flags_args));
  args->netif = netif;
  args->flags = flags;
  err = tcpip_callback_wait(_if_change_flags, args);

  if(!err)
    /* Get the return value */
    err = args->err;

  free (args);

  return err;
}