summaryrefslogtreecommitdiff
path: root/eth-multiplexer/device_impl.c
blob: 459d9c4cfb020efe6dfdc35c24aee48fb5dbcd13 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* 
   Copyright (C) 2008 Free Software Foundation, Inc.
   Written by Zheng Da.

   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; see the file COPYING.  If not, write to
   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include <fcntl.h>
#include <mach.h>
#include <hurd.h>
#include <hurd/ports.h>
#include <hurd/netfs.h>
#include <device/device.h>

#include "ethernet.h"
#include "vdev.h"
#include "ourdevice_S.h"
#include "notify_S.h"
#include "bpf_impl.h"
#include "netfs_impl.h"
#include "util.h"

extern struct port_bucket *port_bucket;
extern struct port_class *vdev_portclass;
extern struct port_class *other_portclass;
extern struct port_info *notify_pi;

/* Implementation of device interface */
kern_return_t
ds_xxx_device_set_status (device_t device, dev_flavor_t flavor,
			  dev_status_t status, size_t statu_cnt)
{
  struct vether_device *vdev = ports_lookup_port (port_bucket, device,
						  vdev_portclass);
  if (vdev == NULL)
    return D_NO_SUCH_DEVICE;
  ports_port_deref (vdev);
  return D_INVALID_OPERATION;
}

kern_return_t
ds_xxx_device_get_status (device_t device, dev_flavor_t flavor,
			  dev_status_t status, size_t *statuscnt)
{
  struct vether_device *vdev = ports_lookup_port (port_bucket, device,
						  vdev_portclass);
  if (vdev == NULL)
    return D_NO_SUCH_DEVICE;
  ports_port_deref (vdev);
  return D_INVALID_OPERATION;
}

kern_return_t
ds_xxx_device_set_filter (device_t device, mach_port_t rec,
			  int pri, filter_array_t filt, size_t len)
{
  struct vether_device *vdev = ports_lookup_port (port_bucket, device,
						  vdev_portclass);
  if (vdev == NULL)
    return D_NO_SUCH_DEVICE;
  ports_port_deref (vdev);
  return D_INVALID_OPERATION;
}

/*
 * This function is currently running in the multithread environment,
 * it should be protected by locks.
 */
kern_return_t
ds_device_open (mach_port_t master_port, mach_port_t reply_port,
		mach_msg_type_name_t reply_portPoly,
		dev_mode_t mode, dev_name_t name, mach_port_t *device, 
	 	mach_msg_type_name_t *devicetype)
{
  struct vether_device *dev;
  int openstat; 
  int right_mode = 1;
  struct protid *pi = ports_lookup_port (netfs_port_bucket, master_port, 0);
  if (pi == NULL)
    return D_NO_SUCH_DEVICE;

  /* If the virtual device hasn't been created yet,
   * create it now. */
  if (pi->po->np->nn->ln == NULL)
    {
      extern struct port_bucket *port_bucket;
      extern struct port_class *vdev_portclass;
      extern struct stat underlying_node_stat;
      static int ino_count = 0;
      /* Create a new light node (virtual device). */
      struct lnode *ln = (struct lnode *) add_vdev (pi->po->np->nn->name,
						    sizeof (*ln),
						    vdev_portclass,
						    port_bucket);
      if (ln == NULL)
	{
	  ports_port_deref (pi);
	  return D_NO_MEMORY;
	}
      memset (&ln->st, 0, sizeof (ln->st));
      ln->st.st_ino = ++ino_count;
      ln->st.st_mode = S_IFCHR | (underlying_node_stat.st_mode & ~S_IFMT);
      ln->st.st_ctime = ln->st.st_mtime = ln->st.st_atime = time (NULL);
      fshelp_touch (&ln->st, TOUCH_ATIME|TOUCH_MTIME|TOUCH_CTIME,
		    multiplexer_maptime);
      pi->po->np->nn->ln = ln;
    }

  dev = (struct vether_device *) pi->po->np->nn->ln;
  /* check the mode */
  openstat = pi->po->openstat;
  if (mode & D_READ && !(openstat & O_READ))
    right_mode = 0;
  if (mode & D_WRITE && !(openstat & O_WRITE))
    right_mode = 0;
  ports_port_deref (pi);

  if (dev)
    {
      if (!right_mode)
	return EBADF;
      *device = dev->dev_port;
      *devicetype = MACH_MSG_TYPE_MAKE_SEND;
      return 0;
    }
  return D_NO_SUCH_DEVICE;
}

kern_return_t
ds_device_close (device_t device)
{
  return 0;
}

kern_return_t
ds_device_write (device_t device, mach_port_t reply_port,
		 mach_msg_type_name_t reply_type, dev_mode_t mode,
		 recnum_t recnum, io_buf_ptr_t data, size_t datalen,
		 int *bytes_written)
{
  kern_return_t ret = 0;
  struct vether_device *vdev = ports_lookup_port (port_bucket, device,
						  vdev_portclass);
  if (vdev == NULL)
    {
      vm_deallocate (mach_task_self (), (vm_address_t) data, datalen);
      return D_NO_SUCH_DEVICE;
    }
  /* The packet is forwarded to all virtual interfaces and
   * the interface which the multiplexer connects to. */
  broadcast_pack (data, datalen, vdev);
  *bytes_written = datalen;
  if(ether_port != MACH_PORT_NULL)
    ret = device_write (ether_port, mode , recnum ,
			data, datalen, bytes_written);
  /* The data in device_write() is transmifered out of line,
   * so the server-side function has to deallocate it. */
  vm_deallocate (mach_task_self (), (vm_address_t) data, datalen);
  ports_port_deref (vdev);
  return ret;
}

kern_return_t
ds_device_write_inband (device_t device, mach_port_t reply_port,
			mach_msg_type_name_t reply_type, dev_mode_t mode,
			recnum_t recnum, io_buf_ptr_inband_t data,
			size_t datalen, int *bytes_written)
{
  struct vether_device *vdev = ports_lookup_port (port_bucket, device,
						  vdev_portclass);
  if (vdev == NULL)
    return D_NO_SUCH_DEVICE;
  ports_port_deref (vdev);
  return D_INVALID_OPERATION;
}

kern_return_t
ds_device_read (device_t device, mach_port_t reply_port,
		mach_msg_type_name_t reply_type, dev_mode_t mode,
		recnum_t recnum, int bytes_wanted,
		io_buf_ptr_t *data, size_t *datalen)
{
  struct vether_device *vdev = ports_lookup_port (port_bucket, device,
						  vdev_portclass);
  if (vdev == NULL)
    return D_NO_SUCH_DEVICE;
  ports_port_deref (vdev);
  return D_INVALID_OPERATION;
}

kern_return_t
ds_device_read_inband (device_t device, mach_port_t reply_port,
		       mach_msg_type_name_t reply_type, dev_mode_t mode,
		       recnum_t recnum, int bytes_wanted,
		       io_buf_ptr_inband_t data, size_t *datalen)
{
  struct vether_device *vdev = ports_lookup_port (port_bucket, device,
						  vdev_portclass);
  if (vdev == NULL)
    return D_NO_SUCH_DEVICE;
  ports_port_deref (vdev);
  return D_INVALID_OPERATION;
}

kern_return_t
ds_device_map (device_t device, vm_prot_t prot, vm_offset_t offset,
	       vm_size_t size, memory_object_t *pager, int unmap)
{
  struct vether_device *vdev = ports_lookup_port (port_bucket, device,
						  vdev_portclass);
  if (vdev == NULL)
    return D_NO_SUCH_DEVICE;
  ports_port_deref (vdev);
  return D_INVALID_OPERATION;
}

kern_return_t
ds_device_set_status (device_t device, dev_flavor_t flavor,
		      dev_status_t status, size_t statuslen)
{
  struct vether_device *vdev = ports_lookup_port (port_bucket, device,
						  vdev_portclass);
  if (vdev == NULL)
    return D_NO_SUCH_DEVICE;
  ports_port_deref (vdev);
  return D_INVALID_OPERATION;
}

kern_return_t
ds_device_get_status (device_t device, dev_flavor_t flavor,
		      dev_status_t status, size_t *statuslen)
{
  extern io_return_t dev_getstat (struct vether_device *, dev_flavor_t,
				  dev_status_t, natural_t *);
  kern_return_t ret = 0;
  struct vether_device *vdev = ports_lookup_port (port_bucket, device,
						  vdev_portclass);
  if (vdev == NULL)
    return D_NO_SUCH_DEVICE;
  if(ether_port != MACH_PORT_NULL)
    ret = device_get_status (ether_port, flavor, status, statuslen);
  else 
    ret = dev_getstat (vdev, flavor, status, statuslen);
  ports_port_deref (vdev);
  return ret;
}

kern_return_t
ds_device_set_filter (device_t device, mach_port_t receive_port,
		      int priority, filter_array_t filter, size_t filterlen)
{
  mach_port_t tmp;
  kern_return_t err;
  struct vether_device *vdev = ports_lookup_port (port_bucket, device,
						  vdev_portclass);
  if (vdev == NULL)
    return D_NO_SUCH_DEVICE;
  err = mach_port_request_notification (mach_task_self (), receive_port, 
					MACH_NOTIFY_DEAD_NAME, 0,
					ports_get_right (notify_pi), 
					MACH_MSG_TYPE_MAKE_SEND_ONCE, &tmp);
  if (err != KERN_SUCCESS)
    goto out;
  if (tmp != MACH_PORT_NULL)
    mach_port_deallocate (mach_task_self (), tmp);
  err = net_set_filter (&vdev->port_list, receive_port,
			priority, filter, filterlen);
out:
  ports_port_deref (vdev);
  return err;
}