summaryrefslogtreecommitdiff
path: root/lwip/port/netif/hurdtunif.c
blob: 728db9ac6082c64948f0c5e86b7a39d689dea73f (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/*
   Copyright (C) 1995,96,98,99,2000,02,17 Free Software Foundation, Inc.
   Written by Michael I. Bushnell, p/BSG.

   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/>.
*/

/* Tunnel devices module */

#include <netif/hurdtunif.h>

#include <hurd/trivfs.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <error.h>
#include <sys/mman.h>
#include <errno.h>

#include <lwip-hurd.h>

/* Add to the end of the queue */
static void
enqueue (struct pbufqueue *q, struct pbuf *p)
{
  *(q->tail) = p;
  p->next = 0;
  q->tail = &p->next;

  q->len++;
}

/* Get from the head of the queue */
static struct pbuf *
dequeue (struct pbufqueue *q)
{
  struct pbuf *ret;

  if (!q->head)
    return 0;

  ret = q->head;
  q->head = q->head->next;
  ret->next = 0;
  q->len--;

  if (!q->head)
    q->tail = &q->head;

  return ret;
}

/*
 * Update the interface's MTU
 */
static error_t
hurdtunif_device_update_mtu (struct netif *netif, uint32_t mtu)
{
  error_t err = 0;

  netif->mtu = mtu;

  return err;
}

/* Set the device flags */
static error_t
hurdtunif_device_set_flags (struct netif *netif, uint16_t flags)
{
  error_t err = 0;
  struct ifcommon *tunif;

  tunif = netif_get_state (netif);
  tunif->flags = flags;

  return err;
}

/*
 * Release all resources of this netif.
 *
 * Returns 0 on success.
 */
static error_t
hurdtunif_device_terminate (struct netif *netif)
{
  struct pbuf *p;
  struct hurdtunif *tunif = (struct hurdtunif *) netif_get_state (netif);

  /* Clear the queue */
  while ((p = dequeue (&tunif->queue)) != 0)
    pbuf_free (p);
  pthread_cond_destroy (&tunif->read);
  pthread_cond_destroy (&tunif->select);
  pthread_mutex_destroy (&tunif->lock);

  /* Free the hook */
  free (netif_get_state (netif)->devname);
  free (netif_get_state (netif));

  return 0;
}

/*
 * Called from lwip.
 *
 * Just enqueue the data.
 */
static err_t
hurdtunif_output (struct netif *netif, struct pbuf *p,
		  const ip4_addr_t * ipaddr)
{
  struct hurdtunif *tunif;
  struct pbuf *pcopy, *oldest;

  tunif = (struct hurdtunif *) netif_get_state (netif);

  /*
   * The stack is responsible for allocating and freeing the pbuf p.
   * Sometimes it keeps the pbuf for the case it needs to be retransmitted,
   * but at other times it frees the pbuf while it's still in our queue,
   * that's why we need a copy.
   */
  pcopy = pbuf_alloc (PBUF_IP, p->tot_len, PBUF_RAM);
  if (pcopy != NULL)
    if (pbuf_copy (pcopy, p) != ERR_OK)
      {
	pbuf_free (pcopy);
	pcopy = NULL;
      }

  pthread_mutex_lock (&tunif->lock);

  /* Avoid unlimited growth. */
  if (tunif->queue.len > 128)
    {
      oldest = dequeue (&tunif->queue);
      pbuf_free (oldest);
    }

  enqueue (&tunif->queue, pcopy);

  if (tunif->read_blocked)
    {
      tunif->read_blocked = 0;
      pthread_cond_broadcast (&tunif->read);
      pthread_cond_broadcast (&tunif->select);
    }

  pthread_mutex_unlock (&tunif->lock);

  return ERR_OK;
}

/*
 * Set up the tunnel a new tunnel device
 */
err_t
hurdtunif_device_init (struct netif *netif)
{
  error_t err = 0;
  struct hurdtunif *tunif;
  char *base_name, *name = netif_get_state (netif)->devname;

  /*
   * Replace the hook by a new one with the proper size.
   * The old one is in the stack and will be removed soon.
   */
  tunif = calloc (1, sizeof (struct hurdtunif));
  if (tunif == NULL)
    {
      LWIP_DEBUGF (NETIF_DEBUG, ("hurdtunif_init: out of memory\n"));
      return ERR_MEM;
    }
  memcpy (tunif, netif_get_state (netif), sizeof (struct ifcommon));
  netif->state = tunif;

  base_name = strrchr (name, '/');
  if (base_name)
    /* The user provided a path */
    base_name++;
  else
    /* The user provided a name for the tunnel. We'll create it at /dev */
    base_name = name;

  if (base_name != name)
    tunif->comm.devname = strdup (name);
  else
    /* Setting up the translator at /dev/tunX.  */
    asprintf (&tunif->comm.devname, "/dev/%s", base_name);

  /* Set the device type */
  tunif->comm.type = ARPHRD_TUNNEL;

  /* MTU = MSS + IP header + TCP header */
  netif->mtu = TCP_MSS + 20 + 20;

  /* Set flags */
  hurdtunif_device_set_flags (netif,
			      IFF_UP | IFF_RUNNING | IFF_POINTOPOINT |
			      IFF_NOARP);

  netif->flags = NETIF_FLAG_LINK_UP;

  /* Set the callbacks */
  netif->output = hurdtunif_output;
  tunif->comm.open = 0;
  tunif->comm.close = 0;
  tunif->comm.terminate = hurdtunif_device_terminate;
  tunif->comm.update_mtu = hurdtunif_device_update_mtu;
  tunif->comm.change_flags = hurdtunif_device_set_flags;

  /* Bind the translator to tunif->comm.devname */
  tunif->underlying = file_name_lookup (tunif->comm.devname,
					O_CREAT | O_NOTRANS, 0664);

  if (tunif->underlying == MACH_PORT_NULL)
    {
      error (0, 0, "%s", tunif->comm.devname);
      return ERR_IF;
    }

  err = trivfs_create_control (tunif->underlying, tunnel_cntlclass,
			       lwip_bucket, tunnel_class, lwip_bucket,
			       &tunif->cntl);

  if (!err)
    {
      mach_port_t right = ports_get_send_right (tunif->cntl);
      err = file_set_translator (tunif->underlying, 0,
				 FS_TRANS_SET | FS_TRANS_ORPHAN, 0, 0, 0,
				 right, MACH_MSG_TYPE_COPY_SEND);
      mach_port_deallocate (mach_task_self (), right);
    }

  if (err)
    {
      error (0, err, "%s", tunif->comm.devname);
      return ERR_IF;
    }

  /* We'll need to get the netif from trivfs operations */
  tunif->cntl->hook = netif;

  /* Output queue initialization */
  tunif->queue.head = 0;
  tunif->queue.tail = &tunif->queue.head;
  tunif->queue.len = 0;
  pthread_mutex_init (&tunif->lock, NULL);
  pthread_cond_init (&tunif->read, NULL);
  pthread_cond_init (&tunif->select, NULL);
  tunif->read_blocked = 0;

  return ERR_OK;
}

/*
 * Set libports classes
 *
 * This function should be called once.
 */
error_t
hurdtunif_module_init ()
{
  error_t err = 0;

  trivfs_add_control_port_class (&tunnel_cntlclass);
  trivfs_add_protid_port_class (&tunnel_class);

  return err;
}

/* If a new open with read and/or write permissions is requested,
   restrict to exclusive usage.  */
static error_t
check_open_hook (struct trivfs_control *cntl, struct iouser *user, int flags)
{
  struct netif *netif;
  struct hurdtunif *tunif;

  for (netif = netif_list; netif; netif = netif->next)
    {
      tunif = (struct hurdtunif *) netif_get_state (netif);
      if (tunif->cntl == cntl)
	break;
    }

  if (netif && flags != O_NORW)
    {
      if (tunif->user)
	return EBUSY;
      else
	tunif->user = user;
    }

  return 0;
}

/* When a protid is destroyed, check if it is the current user.
   If yes, release the interface for other users.  */
static void
pi_destroy_hook (struct trivfs_protid *cred)
{
  struct netif *netif;
  struct hurdtunif *tunif;

  if (cred->pi.class != tunnel_class)
    return;

  netif = (struct netif *) cred->po->cntl->hook;
  tunif = (struct hurdtunif *) netif_get_state (netif);

  if (tunif->user == cred->user)
    tunif->user = 0;
}

/* If this variable is set, it is called every time a new peropen
   structure is created and initialized. */
error_t (*trivfs_check_open_hook) (struct trivfs_control *,
				   struct iouser *, int) = check_open_hook;

/* If this variable is set, it is called every time a protid structure
   is about to be destroyed. */
void (*trivfs_protid_destroy_hook) (struct trivfs_protid *) = pi_destroy_hook;

/* Read data from an IO object.  If offset is -1, read from the object
   maintained file pointer.  If the object is not seekable, offset is
   ignored.  The amount desired to be read is in AMOUNT.  */
error_t
trivfs_S_io_read (struct trivfs_protid *cred,
		  mach_port_t reply, mach_msg_type_name_t reply_type,
		  char **data, mach_msg_type_number_t * data_len,
		  loff_t offs, size_t amount)
{
  struct hurdtunif *tunif;
  struct pbuf *p;

  if (!cred)
    return EOPNOTSUPP;

  if (cred->pi.class != tunnel_class)
    return EOPNOTSUPP;

  tunif =
    (struct hurdtunif *)
    netif_get_state (((struct netif *) cred->po->cntl->hook));

  pthread_mutex_lock (&tunif->lock);

  while (tunif->queue.len == 0)
    {
      if (cred->po->openmodes & O_NONBLOCK)
	{
	  pthread_mutex_unlock (&tunif->lock);
	  return EWOULDBLOCK;
	}

      tunif->read_blocked = 1;
      if (pthread_hurd_cond_wait_np (&tunif->read, &tunif->lock))
	{
	  pthread_mutex_unlock (&tunif->lock);
	  return EINTR;
	}
    }

  p = dequeue (&tunif->queue);

  if (p->tot_len < amount)
    amount = p->tot_len;
  if (amount > 0)
    {
      /* Possibly allocate a new buffer. */
      if (*data_len < amount)
	{
	  *data = mmap (0, amount, PROT_READ | PROT_WRITE, MAP_ANON, 0, 0);
	  if (*data == MAP_FAILED)
	    {
	      pbuf_free (p);
	      pthread_mutex_unlock (&tunif->lock);
	      return ENOMEM;
	    }
	}

      /* Copy the constant data into the buffer. */
      memcpy ((char *) *data, p->payload, amount);
    }
  *data_len = amount;
  pbuf_free (p);

  pthread_mutex_unlock (&tunif->lock);

  return 0;
}

/* Write data to an IO object.  If offset is -1, write at the object
   maintained file pointer.  If the object is not seekable, offset is
   ignored.  The amount successfully written is returned in amount.  A
   given user should not have more than one outstanding io_write on an
   object at a time; servers implement congestion control by delaying
   responses to io_write.  Servers may drop data (returning ENOBUFS)
   if they receive more than one write when not prepared for it.  */
error_t
trivfs_S_io_write (struct trivfs_protid * cred,
		   mach_port_t reply,
		   mach_msg_type_name_t replytype,
		   char *data,
		   mach_msg_type_number_t datalen,
		   off_t offset, mach_msg_type_number_t * amount)
{
  struct netif *netif;
  struct pbuf *p, *q;
  uint16_t off;

  /* Deny access if they have bad credentials. */
  if (!cred)
    return EOPNOTSUPP;

  else if (!(cred->po->openmodes & O_WRITE))
    return EBADF;

  if (cred->pi.class != tunnel_class)
    return EOPNOTSUPP;

  netif = (struct netif *) cred->po->cntl->hook;

  /* Allocate an empty pbuf chain for the data */
  p = pbuf_alloc (PBUF_RAW, datalen, PBUF_POOL);

  if (p)
    {
      /* Iterate to fill the pbuf chain. */
      q = p;
      off = 0;
      do
	{
	  memcpy (q->payload, data, q->len);

	  off += q->len;

	  if (q->tot_len == q->len)
	    break;
	  else
	    q = q->next;
	}
      while (1);

      /* pass it to the stack */
      if (netif->input (p, netif) != ERR_OK)
	{
	  LWIP_DEBUGF (NETIF_DEBUG, ("trivfs_S_io_write: IP input error\n"));
	  pbuf_free (p);
	  p = NULL;
	}

      *amount = datalen;
    }

  return 0;
}

/* Tell how much data can be read from the object without blocking for
   a "long time" (this should be the same meaning of "long time" used
   by the nonblocking flag.  */
kern_return_t
trivfs_S_io_readable (struct trivfs_protid * cred,
		      mach_port_t reply, mach_msg_type_name_t replytype,
		      mach_msg_type_number_t * amount)
{
  struct hurdtunif *tunif;

  if (!cred)
    return EOPNOTSUPP;

  if (cred->pi.class != tunnel_class)
    return EOPNOTSUPP;

  tunif =
    (struct hurdtunif *)
    netif_get_state (((struct netif *) cred->po->cntl->hook));

  pthread_mutex_lock (&tunif->lock);

  if (tunif->queue.head)
    *amount = tunif->queue.head->tot_len;
  else
    *amount = 0;

  pthread_mutex_unlock (&tunif->lock);

  return 0;
}

/* SELECT_TYPE is the bitwise OR of SELECT_READ, SELECT_WRITE, and SELECT_URG.
   Block until one of the indicated types of i/o can be done "quickly", and
   return the types that are then available.  ID_TAG is returned as passed; it
   is just for the convenience of the user in matching up reply messages with
   specific requests sent.  */
static error_t
io_select_common (struct trivfs_protid *cred,
		  mach_port_t reply,
		  mach_msg_type_name_t reply_type,
		  struct timespec *tsp, int *type)
{
  error_t err;
  struct hurdtunif *tunif;

  if (!cred)
    return EOPNOTSUPP;

  if (cred->pi.class != tunnel_class)
    return EOPNOTSUPP;

  /* Make this thread cancellable */
  ports_interrupt_self_on_port_death (cred, reply);

  /* We only deal with SELECT_READ and SELECT_WRITE here.  */
  *type &= SELECT_READ | SELECT_WRITE;

  if (*type == 0)
    return 0;

  tunif =
    (struct hurdtunif *)
    netif_get_state (((struct netif *) cred->po->cntl->hook));

  pthread_mutex_lock (&tunif->lock);

  if (*type & SELECT_WRITE)
    {
      /* We are always writable.  */
      if (tunif->queue.len == 0)
	*type &= ~SELECT_READ;
      pthread_mutex_unlock (&tunif->lock);
      return 0;
    }

  while (1)
    {
      /* There's data on the queue */
      if (tunif->queue.len != 0)
	{
	  *type = SELECT_READ;
	  pthread_mutex_unlock (&tunif->lock);
	  return 0;
	}

      /* The queue is empty, we must wait */
      tunif->read_blocked = 1;
      err =
	pthread_hurd_cond_timedwait_np (&tunif->select, &tunif->lock, tsp);
      if (err)
	{
	  *type = 0;
	  pthread_mutex_unlock (&tunif->lock);

	  if (err == ETIMEDOUT)
	    err = 0;

	  return err;
	}
    }
}

error_t
trivfs_S_io_select (struct trivfs_protid * cred,
		    mach_port_t reply,
		    mach_msg_type_name_t reply_type, int *type)
{
  return io_select_common (cred, reply, reply_type, NULL, type);
}

error_t
trivfs_S_io_select_timeout (struct trivfs_protid * cred,
			    mach_port_t reply,
			    mach_msg_type_name_t reply_type,
			    struct timespec ts, int *type)
{
  return io_select_common (cred, reply, reply_type, &ts, type);
}

/* Change current read/write offset */
error_t
trivfs_S_io_seek (struct trivfs_protid * cred,
		  mach_port_t reply, mach_msg_type_name_t reply_type,
		  off_t offs, int whence, off_t * new_offs)
{
  if (!cred)
    return EOPNOTSUPP;

  if (cred->pi.class != tunnel_class)
    return EOPNOTSUPP;

  return ESPIPE;
}

/* Change the size of the file.  If the size increases, new blocks are
   zero-filled.  After successful return, it is safe to reference mapped
   areas of the file up to NEW_SIZE.  */
error_t
trivfs_S_file_set_size (struct trivfs_protid * cred,
			mach_port_t reply, mach_msg_type_name_t reply_type,
			off_t size)
{
  if (!cred)
    return EOPNOTSUPP;

  if (cred->pi.class != tunnel_class)
    return EOPNOTSUPP;

  return size == 0 ? 0 : EINVAL;
}

/* These four routines modify the O_APPEND, O_ASYNC, O_FSYNC, and
   O_NONBLOCK bits for the IO object. In addition, io_get_openmodes
   will tell you which of O_READ, O_WRITE, and O_EXEC the object can
   be used for.  The O_ASYNC bit affects icky async I/O; good async
   I/O is done through io_async which is orthogonal to these calls. */
error_t
trivfs_S_io_set_all_openmodes (struct trivfs_protid * cred,
			       mach_port_t reply,
			       mach_msg_type_name_t reply_type, int mode)
{
  if (!cred)
    return EOPNOTSUPP;

  if (cred->pi.class != tunnel_class)
    return EOPNOTSUPP;

  return 0;
}

error_t
trivfs_S_io_set_some_openmodes (struct trivfs_protid * cred,
				mach_port_t reply,
				mach_msg_type_name_t reply_type, int bits)
{
  if (!cred)
    return EOPNOTSUPP;

  if (cred->pi.class != tunnel_class)
    return EOPNOTSUPP;

  return 0;
}

error_t
trivfs_S_io_clear_some_openmodes (struct trivfs_protid * cred,
				  mach_port_t reply,
				  mach_msg_type_name_t reply_type, int bits)
{
  if (!cred)
    return EOPNOTSUPP;

  if (cred->pi.class != tunnel_class)
    return EOPNOTSUPP;

  return 0;
}

error_t
trivfs_S_io_get_owner (struct trivfs_protid * cred,
		       mach_port_t reply,
		       mach_msg_type_name_t reply_type, pid_t * owner)
{
  if (!cred)
    return EOPNOTSUPP;

  if (cred->pi.class != tunnel_class)
    return EOPNOTSUPP;

  *owner = 0;
  return 0;
}

error_t
trivfs_S_io_mod_owner (struct trivfs_protid * cred,
		       mach_port_t reply, mach_msg_type_name_t reply_type,
		       pid_t owner)
{
  if (!cred)
    return EOPNOTSUPP;

  if (cred->pi.class != tunnel_class)
    return EOPNOTSUPP;

  return EINVAL;
}

/* Return objects mapping the data underlying this memory object.  If
   the object can be read then memobjrd will be provided; if the
   object can be written then memobjwr will be provided.  For objects
   where read data and write data are the same, these objects will be
   equal, otherwise they will be disjoint.  Servers are permitted to
   implement io_map but not io_map_cntl.  Some objects do not provide
   mapping; they will set none of the ports and return an error.  Such
   objects can still be accessed by io_read and io_write.  */
error_t
trivfs_S_io_map (struct trivfs_protid * cred,
		 mach_port_t reply,
		 mach_msg_type_name_t replyPoly,
		 memory_object_t * rdobj,
		 mach_msg_type_name_t * rdtype,
		 memory_object_t * wrobj, mach_msg_type_name_t * wrtype)
{
  if (!cred)
    return EOPNOTSUPP;

  if (cred->pi.class != tunnel_class)
    return EOPNOTSUPP;

  return EINVAL;
}