6e6b643e68aa7b8ca47f7b2b13c1604559e9917d
[akaros.git] / kern / src / net / udp.c
1 /* Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
2  * Portions Copyright © 1997-1999 Vita Nuova Limited
3  * Portions Copyright © 2000-2007 Vita Nuova Holdings Limited
4  *                                (www.vitanuova.com)
5  * Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
6  *
7  * Modified for the Akaros operating system:
8  * Copyright (c) 2013-2014 The Regents of the University of California
9  * Copyright (c) 2013-2015 Google Inc.
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a copy
12  * of this software and associated documentation files (the "Software"), to deal
13  * in the Software without restriction, including without limitation the rights
14  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15  * copies of the Software, and to permit persons to whom the Software is
16  * furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
24  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE. */
28
29 #define DEBUG
30 #include <vfs.h>
31 #include <kfs.h>
32 #include <slab.h>
33 #include <kmalloc.h>
34 #include <kref.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <assert.h>
38 #include <error.h>
39 #include <cpio.h>
40 #include <pmap.h>
41 #include <smp.h>
42 #include <ip.h>
43
44 #include <vfs.h>
45 #include <kfs.h>
46 #include <slab.h>
47 #include <kmalloc.h>
48 #include <kref.h>
49 #include <string.h>
50 #include <stdio.h>
51 #include <assert.h>
52 #include <error.h>
53 #include <cpio.h>
54 #include <pmap.h>
55 #include <smp.h>
56 #include <ip.h>
57
58 #define DPRINT if(0)print
59
60 enum {
61         UDP_UDPHDR_SZ = 8,
62
63         UDP4_PHDR_OFF = 8,
64         UDP4_PHDR_SZ = 12,
65         UDP4_IPHDR_SZ = 20,
66         UDP6_IPHDR_SZ = 40,
67         UDP6_PHDR_SZ = 40,
68         UDP6_PHDR_OFF = 0,
69
70         IP_UDPPROTO = 17,
71         UDP_USEAD7 = 52,
72         UDP_USEAD6 = 36,
73
74         Udprxms = 200,
75         Udptickms = 100,
76         Udpmaxxmit = 10,
77 };
78
79 typedef struct Udp4hdr Udp4hdr;
80 struct Udp4hdr {
81         /* ip header */
82         uint8_t vihl;                           /* Version and header length */
83         uint8_t tos;                            /* Type of service */
84         uint8_t length[2];                      /* packet length */
85         uint8_t id[2];                          /* Identification */
86         uint8_t frag[2];                        /* Fragment information */
87         uint8_t Unused;
88         uint8_t udpproto;                       /* Protocol */
89         uint8_t udpplen[2];                     /* Header plus data length */
90         uint8_t udpsrc[IPv4addrlen];    /* Ip source */
91         uint8_t udpdst[IPv4addrlen];    /* Ip destination */
92
93         /* udp header */
94         uint8_t udpsport[2];            /* Source port */
95         uint8_t udpdport[2];            /* Destination port */
96         uint8_t udplen[2];                      /* data length */
97         uint8_t udpcksum[2];            /* Checksum */
98 };
99
100 typedef struct Udp6hdr Udp6hdr;
101 struct Udp6hdr {
102         uint8_t viclfl[4];
103         uint8_t len[2];
104         uint8_t nextheader;
105         uint8_t hoplimit;
106         uint8_t udpsrc[IPaddrlen];
107         uint8_t udpdst[IPaddrlen];
108
109         /* udp header */
110         uint8_t udpsport[2];            /* Source port */
111         uint8_t udpdport[2];            /* Destination port */
112         uint8_t udplen[2];                      /* data length */
113         uint8_t udpcksum[2];            /* Checksum */
114 };
115
116 /* MIB II counters */
117 typedef struct Udpstats Udpstats;
118 struct Udpstats {
119         uint32_t udpInDatagrams;
120         uint32_t udpNoPorts;
121         uint32_t udpInErrors;
122         uint32_t udpOutDatagrams;
123 };
124
125 typedef struct Udppriv Udppriv;
126 struct Udppriv {
127         struct Ipht ht;
128
129         /* MIB counters */
130         Udpstats ustats;
131
132         /* non-MIB stats */
133         uint32_t csumerr;                       /* checksum errors */
134         uint32_t lenerr;                        /* short packet */
135 };
136
137 void (*etherprofiler) (char *name, int qlen);
138 void udpkick(void *x, struct block *bp);
139
140 /*
141  *  protocol specific part of Conv
142  */
143 typedef struct Udpcb Udpcb;
144 struct Udpcb {
145         uint8_t headers;
146 };
147
148 static void udpconnect(struct conv *c, char **argv, int argc)
149 {
150         Udppriv *upriv;
151
152         upriv = c->p->priv;
153         Fsstdconnect(c, argv, argc);
154         Fsconnected(c, 0);
155
156         iphtadd(&upriv->ht, c);
157 }
158
159 static void udpbind(struct conv *c, char **argv, int argc)
160 {
161         Udppriv *upriv;
162
163         upriv = c->p->priv;
164         Fsstdbind(c, argv, argc);
165         iphtadd(&upriv->ht, c);
166 }
167
168 static int udpstate(struct conv *c, char *state, int n)
169 {
170         return snprintf(state, n, "%s qin %d qout %d\n",
171                                         c->inuse ? "Open" : "Closed",
172                                         c->rq ? qlen(c->rq) : 0, c->wq ? qlen(c->wq) : 0);
173 }
174
175 static void udpannounce(struct conv *c, char **argv, int argc)
176 {
177         Udppriv *upriv;
178
179         upriv = c->p->priv;
180         Fsstdannounce(c, argv, argc);
181         Fsconnected(c, NULL);
182         iphtadd(&upriv->ht, c);
183 }
184
185 static void udpbypass(struct conv *cv, char **argv, int argc)
186 {
187         Udppriv *upriv = cv->p->priv;
188
189         Fsstdbypass(cv, argv, argc);
190         iphtadd(&upriv->ht, cv);
191 }
192
193 static void udpcreate(struct conv *c)
194 {
195         c->rq = qopen(128 * 1024, Qmsg, 0, 0);
196         c->wq = qbypass(udpkick, c);
197 }
198
199 static void udpclose(struct conv *c)
200 {
201         Udpcb *ucb;
202         Udppriv *upriv;
203
204         upriv = c->p->priv;
205         iphtrem(&upriv->ht, c);
206
207         c->state = 0;
208         qclose(c->rq);
209         qclose(c->wq);
210         qclose(c->eq);
211         ipmove(c->laddr, IPnoaddr);
212         ipmove(c->raddr, IPnoaddr);
213         c->lport = 0;
214         c->rport = 0;
215
216         ucb = (Udpcb *) c->ptcl;
217         ucb->headers = 0;
218
219         qunlock(&c->qlock);
220 }
221
222 void udpkick(void *x, struct block *bp)
223 {
224         struct conv *c = x;
225         Udp4hdr *uh4;
226         Udp6hdr *uh6;
227         uint16_t rport;
228         uint8_t laddr[IPaddrlen], raddr[IPaddrlen];
229         Udpcb *ucb;
230         int dlen, ptcllen;
231         Udppriv *upriv;
232         struct Fs *f;
233         int version;
234         struct conv *rc;
235
236         upriv = c->p->priv;
237         assert(upriv);
238         f = c->p->f;
239
240         netlog(c->p->f, Logudp, "udp: kick\n");
241         if (bp == NULL)
242                 return;
243
244         ucb = (Udpcb *) c->ptcl;
245         switch (ucb->headers) {
246                 case 7:
247                         /* get user specified addresses */
248                         bp = pullupblock(bp, UDP_USEAD7);
249                         if (bp == NULL)
250                                 return;
251                         ipmove(raddr, bp->rp);
252                         bp->rp += IPaddrlen;
253                         ipmove(laddr, bp->rp);
254                         bp->rp += IPaddrlen;
255                         /* pick interface closest to dest */
256                         if (ipforme(f, laddr) != Runi)
257                                 findlocalip(f, laddr, raddr);
258                         bp->rp += IPaddrlen;    /* Ignore ifc address */
259                         rport = nhgets(bp->rp);
260                         bp->rp += 2 + 2;        /* Ignore local port */
261                         break;
262                 case 6:
263                         /* get user specified addresses */
264                         bp = pullupblock(bp, UDP_USEAD6);
265                         if (bp == NULL)
266                                 return;
267                         ipmove(raddr, bp->rp);
268                         bp->rp += IPaddrlen;
269                         ipmove(laddr, bp->rp);
270                         bp->rp += IPaddrlen;
271                         /* pick interface closest to dest */
272                         if (ipforme(f, laddr) != Runi)
273                                 findlocalip(f, laddr, raddr);
274                         rport = nhgets(bp->rp);
275                         bp->rp += 2 + 2;        /* Ignore local port */
276                         break;
277                 default:
278                         rport = 0;
279                         break;
280         }
281
282         if (ucb->headers) {
283                 if (memcmp(laddr, v4prefix, IPv4off) == 0 ||
284                         ipcmp(laddr, IPnoaddr) == 0)
285                         version = V4;
286                 else
287                         version = V6;
288         } else {
289                 if ((memcmp(c->raddr, v4prefix, IPv4off) == 0 &&
290                          memcmp(c->laddr, v4prefix, IPv4off) == 0)
291                         || ipcmp(c->raddr, IPnoaddr) == 0)
292                         version = V4;
293                 else
294                         version = V6;
295         }
296
297         dlen = blocklen(bp);
298
299         /* fill in pseudo header and compute checksum */
300         switch (version) {
301                 case V4:
302                         bp = padblock(bp, UDP4_IPHDR_SZ + UDP_UDPHDR_SZ);
303                         if (bp == NULL)
304                                 return;
305
306                         uh4 = (Udp4hdr *) (bp->rp);
307                         ptcllen = dlen + UDP_UDPHDR_SZ;
308                         uh4->Unused = 0;
309                         uh4->udpproto = IP_UDPPROTO;
310                         uh4->frag[0] = 0;
311                         uh4->frag[1] = 0;
312                         hnputs(uh4->udpplen, ptcllen);
313                         if (ucb->headers) {
314                                 v6tov4(uh4->udpdst, raddr);
315                                 hnputs(uh4->udpdport, rport);
316                                 v6tov4(uh4->udpsrc, laddr);
317                                 rc = NULL;
318                         } else {
319                                 v6tov4(uh4->udpdst, c->raddr);
320                                 hnputs(uh4->udpdport, c->rport);
321                                 if (ipcmp(c->laddr, IPnoaddr) == 0)
322                                         findlocalip(f, c->laddr, c->raddr);
323                                 v6tov4(uh4->udpsrc, c->laddr);
324                                 rc = c;
325                         }
326                         hnputs(uh4->udpsport, c->lport);
327                         hnputs(uh4->udplen, ptcllen);
328                         uh4->udpcksum[0] = 0;
329                         uh4->udpcksum[1] = 0;
330                         hnputs(uh4->udpcksum,
331                                    ~ptclcsum(bp, UDP4_PHDR_OFF, UDP4_PHDR_SZ));
332                         bp->checksum_start = UDP4_IPHDR_SZ;
333                         bp->checksum_offset = uh4->udpcksum - uh4->udpsport;
334                         bp->flag |= Budpck;
335                         uh4->vihl = IP_VER4;
336                         ipoput4(f, bp, 0, c->ttl, c->tos, rc);
337                         break;
338
339                 case V6:
340                         bp = padblock(bp, UDP6_IPHDR_SZ + UDP_UDPHDR_SZ);
341                         if (bp == NULL)
342                                 return;
343
344                         // using the v6 ip header to create pseudo header
345                         // first then reset it to the normal ip header
346                         uh6 = (Udp6hdr *) (bp->rp);
347                         memset(uh6, 0, 8);
348                         ptcllen = dlen + UDP_UDPHDR_SZ;
349                         hnputl(uh6->viclfl, ptcllen);
350                         uh6->hoplimit = IP_UDPPROTO;
351                         if (ucb->headers) {
352                                 ipmove(uh6->udpdst, raddr);
353                                 hnputs(uh6->udpdport, rport);
354                                 ipmove(uh6->udpsrc, laddr);
355                                 rc = NULL;
356                         } else {
357                                 ipmove(uh6->udpdst, c->raddr);
358                                 hnputs(uh6->udpdport, c->rport);
359                                 if (ipcmp(c->laddr, IPnoaddr) == 0)
360                                         findlocalip(f, c->laddr, c->raddr);
361                                 ipmove(uh6->udpsrc, c->laddr);
362                                 rc = c;
363                         }
364                         hnputs(uh6->udpsport, c->lport);
365                         hnputs(uh6->udplen, ptcllen);
366                         uh6->udpcksum[0] = 0;
367                         uh6->udpcksum[1] = 0;
368                         hnputs(uh6->udpcksum,
369                                    ptclcsum(bp, UDP6_PHDR_OFF,
370                                                         dlen + UDP_UDPHDR_SZ + UDP6_PHDR_SZ));
371                         memset(uh6, 0, 8);
372                         uh6->viclfl[0] = IP_VER6;
373                         hnputs(uh6->len, ptcllen);
374                         uh6->nextheader = IP_UDPPROTO;
375                         ipoput6(f, bp, 0, c->ttl, c->tos, rc);
376                         break;
377
378                 default:
379                         panic("udpkick: version %d", version);
380         }
381         upriv->ustats.udpOutDatagrams++;
382 }
383
384 void udpiput(struct Proto *udp, struct Ipifc *ifc, struct block *bp)
385 {
386         int len;
387         Udp4hdr *uh4;
388         Udp6hdr *uh6;
389         struct conv *c;
390         Udpcb *ucb;
391         uint8_t raddr[IPaddrlen], laddr[IPaddrlen];
392         uint16_t rport, lport;
393         Udppriv *upriv;
394         struct Fs *f;
395         int version;
396         int ottl, oviclfl, olen;
397         uint8_t *p;
398
399         upriv = udp->priv;
400         f = udp->f;
401         upriv->ustats.udpInDatagrams++;
402
403         uh4 = (Udp4hdr *) (bp->rp);
404         version = ((uh4->vihl & 0xF0) == IP_VER6) ? V6 : V4;
405
406         /*
407          * Put back pseudo header for checksum
408          * (remember old values for icmpnoconv())
409          */
410         switch (version) {
411                 case V4:
412                         ottl = uh4->Unused;
413                         uh4->Unused = 0;
414                         len = nhgets(uh4->udplen);
415                         olen = nhgets(uh4->udpplen);
416                         hnputs(uh4->udpplen, len);
417
418                         v4tov6(raddr, uh4->udpsrc);
419                         v4tov6(laddr, uh4->udpdst);
420                         lport = nhgets(uh4->udpdport);
421                         rport = nhgets(uh4->udpsport);
422
423                         if (!(bp->flag & Budpck) &&
424                             (uh4->udpcksum[0] || uh4->udpcksum[1]) &&
425                             ptclcsum(bp, UDP4_PHDR_OFF, len + UDP4_PHDR_SZ)) {
426                                 upriv->ustats.udpInErrors++;
427                                 netlog(f, Logudp, "udp: checksum error %I\n",
428                                        raddr);
429                                 printd("udp: checksum error %I\n", raddr);
430                                 freeblist(bp);
431                                 return;
432                         }
433                         uh4->Unused = ottl;
434                         hnputs(uh4->udpplen, olen);
435                         break;
436                 case V6:
437                         uh6 = (Udp6hdr *) (bp->rp);
438                         len = nhgets(uh6->udplen);
439                         oviclfl = nhgetl(uh6->viclfl);
440                         olen = nhgets(uh6->len);
441                         ottl = uh6->hoplimit;
442                         ipmove(raddr, uh6->udpsrc);
443                         ipmove(laddr, uh6->udpdst);
444                         lport = nhgets(uh6->udpdport);
445                         rport = nhgets(uh6->udpsport);
446                         memset(uh6, 0, 8);
447                         hnputl(uh6->viclfl, len);
448                         uh6->hoplimit = IP_UDPPROTO;
449                         if (ptclcsum(bp, UDP6_PHDR_OFF, len + UDP6_PHDR_SZ)) {
450                                 upriv->ustats.udpInErrors++;
451                                 netlog(f, Logudp, "udp: checksum error %I\n", raddr);
452                                 printd("udp: checksum error %I\n", raddr);
453                                 freeblist(bp);
454                                 return;
455                         }
456                         hnputl(uh6->viclfl, oviclfl);
457                         hnputs(uh6->len, olen);
458                         uh6->nextheader = IP_UDPPROTO;
459                         uh6->hoplimit = ottl;
460                         break;
461                 default:
462                         panic("udpiput: version %d", version);
463                         return; /* to avoid a warning */
464         }
465
466         c = iphtlook(&upriv->ht, raddr, rport, laddr, lport);
467         if (c == NULL) {
468                 /* no converstation found */
469                 upriv->ustats.udpNoPorts++;
470                 netlog(f, Logudp, "udp: no conv %I!%d -> %I!%d\n", raddr, rport,
471                            laddr, lport);
472
473                 switch (version) {
474                         case V4:
475                                 icmpnoconv(f, bp);
476                                 break;
477                         case V6:
478                                 icmphostunr(f, ifc, bp, icmp6_port_unreach, 0);
479                                 break;
480                         default:
481                                 panic("udpiput2: version %d", version);
482                 }
483
484                 freeblist(bp);
485                 return;
486         }
487
488         if (c->state == Bypass) {
489                 bypass_or_drop(c, bp);
490                 return;
491         }
492
493         ucb = (Udpcb *) c->ptcl;
494
495         if (c->state == Announced) {
496                 if (ucb->headers == 0) {
497                         /* create a new conversation */
498                         if (ipforme(f, laddr) != Runi) {
499                                 switch (version) {
500                                         case V4:
501                                                 v4tov6(laddr, ifc->lifc->local);
502                                                 break;
503                                         case V6:
504                                                 ipmove(laddr, ifc->lifc->local);
505                                                 break;
506                                         default:
507                                                 panic("udpiput3: version %d", version);
508                                 }
509                         }
510                         qlock(&udp->qlock);
511                         c = Fsnewcall(c, raddr, rport, laddr, lport, version);
512                         qunlock(&udp->qlock);
513                         if (c == NULL) {
514                                 freeblist(bp);
515                                 return;
516                         }
517                         iphtadd(&upriv->ht, c);
518                         ucb = (Udpcb *) c->ptcl;
519                 }
520         }
521
522         qlock(&c->qlock);
523
524         /*
525          * Trim the packet down to data size
526          */
527         len -= UDP_UDPHDR_SZ;
528         switch (version) {
529                 case V4:
530                         bp = trimblock(bp, UDP4_IPHDR_SZ + UDP_UDPHDR_SZ, len);
531                         break;
532                 case V6:
533                         bp = trimblock(bp, UDP6_IPHDR_SZ + UDP_UDPHDR_SZ, len);
534                         break;
535                 default:
536                         bp = NULL;
537                         panic("udpiput4: version %d", version);
538         }
539         if (bp == NULL) {
540                 qunlock(&c->qlock);
541                 netlog(f, Logudp, "udp: len err %I.%d -> %I.%d\n", raddr, rport,
542                            laddr, lport);
543                 upriv->lenerr++;
544                 return;
545         }
546
547         netlog(f, Logudpmsg, "udp: %I.%d -> %I.%d l %d\n", raddr, rport,
548                    laddr, lport, len);
549
550         switch (ucb->headers) {
551                 case 7:
552                         /* pass the src address */
553                         bp = padblock(bp, UDP_USEAD7);
554                         p = bp->rp;
555                         ipmove(p, raddr);
556                         p += IPaddrlen;
557                         ipmove(p, laddr);
558                         p += IPaddrlen;
559                         ipmove(p, ifc->lifc->local);
560                         p += IPaddrlen;
561                         hnputs(p, rport);
562                         p += 2;
563                         hnputs(p, lport);
564                         break;
565                 case 6:
566                         /* pass the src address */
567                         bp = padblock(bp, UDP_USEAD6);
568                         p = bp->rp;
569                         ipmove(p, raddr);
570                         p += IPaddrlen;
571                         ipmove(p, ipforme(f, laddr) == Runi ? laddr : ifc->lifc->local);
572                         p += IPaddrlen;
573                         hnputs(p, rport);
574                         p += 2;
575                         hnputs(p, lport);
576                         break;
577         }
578
579         if (bp->next)
580                 bp = concatblock(bp);
581
582         if (qfull(c->rq)) {
583                 qunlock(&c->qlock);
584                 netlog(f, Logudp, "udp: qfull %I.%d -> %I.%d\n", raddr, rport,
585                            laddr, lport);
586                 freeblist(bp);
587                 return;
588         }
589
590         qpass(c->rq, bp);
591         qunlock(&c->qlock);
592
593 }
594
595 static void udpctl(struct conv *c, char **f, int n)
596 {
597         Udpcb *ucb = (Udpcb*)c->ptcl;
598
599         if ((n == 1) && strcmp(f[0], "oldheaders") == 0)
600                 ucb->headers = 6;
601         else if ((n == 1) && strcmp(f[0], "headers") == 0)
602                 ucb->headers = 7;
603         else
604                 error(EINVAL, "unknown command to %s", __func__);
605 }
606
607 void udpadvise(struct Proto *udp, struct block *bp, char *msg)
608 {
609         Udp4hdr *h4;
610         Udp6hdr *h6;
611         uint8_t source[IPaddrlen], dest[IPaddrlen];
612         uint16_t psource, pdest;
613         struct conv *s, **p;
614         int version;
615
616         h4 = (Udp4hdr *) (bp->rp);
617         version = ((h4->vihl & 0xF0) == IP_VER6) ? V6 : V4;
618
619         switch (version) {
620                 case V4:
621                         v4tov6(dest, h4->udpdst);
622                         v4tov6(source, h4->udpsrc);
623                         psource = nhgets(h4->udpsport);
624                         pdest = nhgets(h4->udpdport);
625                         break;
626                 case V6:
627                         h6 = (Udp6hdr *) (bp->rp);
628                         ipmove(dest, h6->udpdst);
629                         ipmove(source, h6->udpsrc);
630                         psource = nhgets(h6->udpsport);
631                         pdest = nhgets(h6->udpdport);
632                         break;
633                 default:
634                         panic("udpadvise: version %d", version);
635                         return; /* to avoid a warning */
636         }
637
638         /* Look for a connection */
639         for (p = udp->conv; *p; p++) {
640                 s = *p;
641                 if (s->rport == pdest)
642                         if (s->lport == psource)
643                                 if (ipcmp(s->raddr, dest) == 0)
644                                         if (ipcmp(s->laddr, source) == 0) {
645                                                 if (s->ignoreadvice)
646                                                         break;
647                                                 qlock(&s->qlock);
648                                                 qhangup(s->rq, msg);
649                                                 qhangup(s->wq, msg);
650                                                 qunlock(&s->qlock);
651                                                 freeblist(bp);
652                                                 return;
653                                         }
654         }
655         freeblist(bp);
656 }
657
658 int udpstats(struct Proto *udp, char *buf, int len)
659 {
660         Udppriv *upriv;
661         char *p, *e;
662
663         upriv = udp->priv;
664         p = buf;
665         e = p + len;
666         p = seprintf(p, e, "InDatagrams: %u\n", upriv->ustats.udpInDatagrams);
667         p = seprintf(p, e, "NoPorts: %u\n", upriv->ustats.udpNoPorts);
668         p = seprintf(p, e, "InErrors: %u\n", upriv->ustats.udpInErrors);
669         p = seprintf(p, e, "OutDatagrams: %u\n", upriv->ustats.udpOutDatagrams);
670         return p - buf;
671 }
672
673 void udpinit(struct Fs *fs)
674 {
675         struct Proto *udp;
676
677         udp = kzmalloc(sizeof(struct Proto), 0);
678         udp->priv = kzmalloc(sizeof(Udppriv), 0);
679         udp->name = "udp";
680         udp->connect = udpconnect;
681         udp->bind = udpbind;
682         udp->announce = udpannounce;
683         udp->bypass = udpbypass;
684         udp->ctl = udpctl;
685         udp->state = udpstate;
686         udp->create = udpcreate;
687         udp->close = udpclose;
688         udp->rcv = udpiput;
689         udp->advise = udpadvise;
690         udp->stats = udpstats;
691         udp->ipproto = IP_UDPPROTO;
692         udp->nc = 4096;
693         udp->ptclsize = sizeof(Udpcb);
694
695         Fsproto(fs, udp);
696 }