Use a helper for tracing an interface
[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         qlock_t qlock;
146         uint8_t headers;
147 };
148
149 static void udpconnect(struct conv *c, char **argv, int argc)
150 {
151         Udppriv *upriv;
152
153         upriv = c->p->priv;
154         Fsstdconnect(c, argv, argc);
155         Fsconnected(c, 0);
156
157         iphtadd(&upriv->ht, c);
158 }
159
160 static int udpstate(struct conv *c, char *state, int n)
161 {
162         return snprintf(state, n, "%s qin %d qout %d\n",
163                                         c->inuse ? "Open" : "Closed",
164                                         c->rq ? qlen(c->rq) : 0, c->wq ? qlen(c->wq) : 0);
165 }
166
167 static void udpannounce(struct conv *c, char **argv, int argc)
168 {
169         Udppriv *upriv;
170
171         upriv = c->p->priv;
172         Fsstdannounce(c, argv, argc);
173         Fsconnected(c, NULL);
174         iphtadd(&upriv->ht, c);
175 }
176
177 static void udpcreate(struct conv *c)
178 {
179         c->rq = qopen(128 * 1024, Qmsg, 0, 0);
180         c->wq = qbypass(udpkick, c);
181 }
182
183 static void udpclose(struct conv *c)
184 {
185         Udpcb *ucb;
186         Udppriv *upriv;
187
188         upriv = c->p->priv;
189         iphtrem(&upriv->ht, c);
190
191         c->state = 0;
192         qclose(c->rq);
193         qclose(c->wq);
194         qclose(c->eq);
195         ipmove(c->laddr, IPnoaddr);
196         ipmove(c->raddr, IPnoaddr);
197         c->lport = 0;
198         c->rport = 0;
199
200         ucb = (Udpcb *) c->ptcl;
201         ucb->headers = 0;
202
203         qunlock(&c->qlock);
204 }
205
206 void udpkick(void *x, struct block *bp)
207 {
208         struct conv *c = x;
209         Udp4hdr *uh4;
210         Udp6hdr *uh6;
211         uint16_t rport;
212         uint8_t laddr[IPaddrlen], raddr[IPaddrlen];
213         Udpcb *ucb;
214         int dlen, ptcllen;
215         Udppriv *upriv;
216         struct Fs *f;
217         int version;
218         struct conv *rc;
219
220         upriv = c->p->priv;
221         assert(upriv);
222         f = c->p->f;
223
224         netlog(c->p->f, Logudp, "udp: kick\n");
225         if (bp == NULL)
226                 return;
227
228         ucb = (Udpcb *) c->ptcl;
229         switch (ucb->headers) {
230                 case 7:
231                         /* get user specified addresses */
232                         bp = pullupblock(bp, UDP_USEAD7);
233                         if (bp == NULL)
234                                 return;
235                         ipmove(raddr, bp->rp);
236                         bp->rp += IPaddrlen;
237                         ipmove(laddr, bp->rp);
238                         bp->rp += IPaddrlen;
239                         /* pick interface closest to dest */
240                         if (ipforme(f, laddr) != Runi)
241                                 findlocalip(f, laddr, raddr);
242                         bp->rp += IPaddrlen;    /* Ignore ifc address */
243                         rport = nhgets(bp->rp);
244                         bp->rp += 2 + 2;        /* Ignore local port */
245                         break;
246                 case 6:
247                         /* get user specified addresses */
248                         bp = pullupblock(bp, UDP_USEAD6);
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                         rport = nhgets(bp->rp);
259                         bp->rp += 2 + 2;        /* Ignore local port */
260                         break;
261                 default:
262                         rport = 0;
263                         break;
264         }
265
266         if (ucb->headers) {
267                 if (memcmp(laddr, v4prefix, IPv4off) == 0 ||
268                         ipcmp(laddr, IPnoaddr) == 0)
269                         version = V4;
270                 else
271                         version = V6;
272         } else {
273                 if ((memcmp(c->raddr, v4prefix, IPv4off) == 0 &&
274                          memcmp(c->laddr, v4prefix, IPv4off) == 0)
275                         || ipcmp(c->raddr, IPnoaddr) == 0)
276                         version = V4;
277                 else
278                         version = V6;
279         }
280
281         dlen = blocklen(bp);
282
283         /* fill in pseudo header and compute checksum */
284         switch (version) {
285                 case V4:
286                         bp = padblock(bp, UDP4_IPHDR_SZ + UDP_UDPHDR_SZ);
287                         if (bp == NULL)
288                                 return;
289
290                         uh4 = (Udp4hdr *) (bp->rp);
291                         ptcllen = dlen + UDP_UDPHDR_SZ;
292                         uh4->Unused = 0;
293                         uh4->udpproto = IP_UDPPROTO;
294                         uh4->frag[0] = 0;
295                         uh4->frag[1] = 0;
296                         hnputs(uh4->udpplen, ptcllen);
297                         if (ucb->headers) {
298                                 v6tov4(uh4->udpdst, raddr);
299                                 hnputs(uh4->udpdport, rport);
300                                 v6tov4(uh4->udpsrc, laddr);
301                                 rc = NULL;
302                         } else {
303                                 v6tov4(uh4->udpdst, c->raddr);
304                                 hnputs(uh4->udpdport, c->rport);
305                                 if (ipcmp(c->laddr, IPnoaddr) == 0)
306                                         findlocalip(f, c->laddr, c->raddr);
307                                 v6tov4(uh4->udpsrc, c->laddr);
308                                 rc = c;
309                         }
310                         hnputs(uh4->udpsport, c->lport);
311                         hnputs(uh4->udplen, ptcllen);
312                         uh4->udpcksum[0] = 0;
313                         uh4->udpcksum[1] = 0;
314                         hnputs(uh4->udpcksum,
315                                    ~ptclcsum(bp, UDP4_PHDR_OFF, UDP4_PHDR_SZ));
316                         bp->checksum_start = UDP4_IPHDR_SZ;
317                         bp->checksum_offset = uh4->udpcksum - uh4->udpsport;
318                         bp->flag |= Budpck;
319                         uh4->vihl = IP_VER4;
320                         ipoput4(f, bp, 0, c->ttl, c->tos, rc);
321                         break;
322
323                 case V6:
324                         bp = padblock(bp, UDP6_IPHDR_SZ + UDP_UDPHDR_SZ);
325                         if (bp == NULL)
326                                 return;
327
328                         // using the v6 ip header to create pseudo header
329                         // first then reset it to the normal ip header
330                         uh6 = (Udp6hdr *) (bp->rp);
331                         memset(uh6, 0, 8);
332                         ptcllen = dlen + UDP_UDPHDR_SZ;
333                         hnputl(uh6->viclfl, ptcllen);
334                         uh6->hoplimit = IP_UDPPROTO;
335                         if (ucb->headers) {
336                                 ipmove(uh6->udpdst, raddr);
337                                 hnputs(uh6->udpdport, rport);
338                                 ipmove(uh6->udpsrc, laddr);
339                                 rc = NULL;
340                         } else {
341                                 ipmove(uh6->udpdst, c->raddr);
342                                 hnputs(uh6->udpdport, c->rport);
343                                 if (ipcmp(c->laddr, IPnoaddr) == 0)
344                                         findlocalip(f, c->laddr, c->raddr);
345                                 ipmove(uh6->udpsrc, c->laddr);
346                                 rc = c;
347                         }
348                         hnputs(uh6->udpsport, c->lport);
349                         hnputs(uh6->udplen, ptcllen);
350                         uh6->udpcksum[0] = 0;
351                         uh6->udpcksum[1] = 0;
352                         hnputs(uh6->udpcksum,
353                                    ptclcsum(bp, UDP6_PHDR_OFF,
354                                                         dlen + UDP_UDPHDR_SZ + UDP6_PHDR_SZ));
355                         memset(uh6, 0, 8);
356                         uh6->viclfl[0] = IP_VER6;
357                         hnputs(uh6->len, ptcllen);
358                         uh6->nextheader = IP_UDPPROTO;
359                         ipoput6(f, bp, 0, c->ttl, c->tos, rc);
360                         break;
361
362                 default:
363                         panic("udpkick: version %d", version);
364         }
365         upriv->ustats.udpOutDatagrams++;
366 }
367
368 void udpiput(struct Proto *udp, struct Ipifc *ifc, struct block *bp)
369 {
370         int len;
371         Udp4hdr *uh4;
372         Udp6hdr *uh6;
373         struct conv *c;
374         Udpcb *ucb;
375         uint8_t raddr[IPaddrlen], laddr[IPaddrlen];
376         uint16_t rport, lport;
377         Udppriv *upriv;
378         struct Fs *f;
379         int version;
380         int ottl, oviclfl, olen;
381         uint8_t *p;
382
383         upriv = udp->priv;
384         f = udp->f;
385         upriv->ustats.udpInDatagrams++;
386
387         uh4 = (Udp4hdr *) (bp->rp);
388         version = ((uh4->vihl & 0xF0) == IP_VER6) ? V6 : V4;
389
390         /*
391          * Put back pseudo header for checksum
392          * (remember old values for icmpnoconv())
393          */
394         switch (version) {
395                 case V4:
396                         ottl = uh4->Unused;
397                         uh4->Unused = 0;
398                         len = nhgets(uh4->udplen);
399                         olen = nhgets(uh4->udpplen);
400                         hnputs(uh4->udpplen, len);
401
402                         v4tov6(raddr, uh4->udpsrc);
403                         v4tov6(laddr, uh4->udpdst);
404                         lport = nhgets(uh4->udpdport);
405                         rport = nhgets(uh4->udpsport);
406
407                         if (!(bp->flag & Budpck) &&
408                             (uh4->udpcksum[0] || uh4->udpcksum[1]) &&
409                             ptclcsum(bp, UDP4_PHDR_OFF, len + UDP4_PHDR_SZ)) {
410                                 upriv->ustats.udpInErrors++;
411                                 netlog(f, Logudp, "udp: checksum error %I\n",
412                                        raddr);
413                                 printd("udp: checksum error %I\n", raddr);
414                                 freeblist(bp);
415                                 return;
416                         }
417                         uh4->Unused = ottl;
418                         hnputs(uh4->udpplen, olen);
419                         break;
420                 case V6:
421                         uh6 = (Udp6hdr *) (bp->rp);
422                         len = nhgets(uh6->udplen);
423                         oviclfl = nhgetl(uh6->viclfl);
424                         olen = nhgets(uh6->len);
425                         ottl = uh6->hoplimit;
426                         ipmove(raddr, uh6->udpsrc);
427                         ipmove(laddr, uh6->udpdst);
428                         lport = nhgets(uh6->udpdport);
429                         rport = nhgets(uh6->udpsport);
430                         memset(uh6, 0, 8);
431                         hnputl(uh6->viclfl, len);
432                         uh6->hoplimit = IP_UDPPROTO;
433                         if (ptclcsum(bp, UDP6_PHDR_OFF, len + UDP6_PHDR_SZ)) {
434                                 upriv->ustats.udpInErrors++;
435                                 netlog(f, Logudp, "udp: checksum error %I\n", raddr);
436                                 printd("udp: checksum error %I\n", raddr);
437                                 freeblist(bp);
438                                 return;
439                         }
440                         hnputl(uh6->viclfl, oviclfl);
441                         hnputs(uh6->len, olen);
442                         uh6->nextheader = IP_UDPPROTO;
443                         uh6->hoplimit = ottl;
444                         break;
445                 default:
446                         panic("udpiput: version %d", version);
447                         return; /* to avoid a warning */
448         }
449
450         qlock(&udp->qlock);
451
452         c = iphtlook(&upriv->ht, raddr, rport, laddr, lport);
453         if (c == NULL) {
454                 /* no converstation found */
455                 upriv->ustats.udpNoPorts++;
456                 qunlock(&udp->qlock);
457                 netlog(f, Logudp, "udp: no conv %I!%d -> %I!%d\n", raddr, rport,
458                            laddr, lport);
459
460                 switch (version) {
461                         case V4:
462                                 icmpnoconv(f, bp);
463                                 break;
464                         case V6:
465                                 icmphostunr(f, ifc, bp, icmp6_port_unreach, 0);
466                                 break;
467                         default:
468                                 panic("udpiput2: version %d", version);
469                 }
470
471                 freeblist(bp);
472                 return;
473         }
474         ucb = (Udpcb *) c->ptcl;
475
476         if (c->state == Announced) {
477                 if (ucb->headers == 0) {
478                         /* create a new conversation */
479                         if (ipforme(f, laddr) != Runi) {
480                                 switch (version) {
481                                         case V4:
482                                                 v4tov6(laddr, ifc->lifc->local);
483                                                 break;
484                                         case V6:
485                                                 ipmove(laddr, ifc->lifc->local);
486                                                 break;
487                                         default:
488                                                 panic("udpiput3: version %d", version);
489                                 }
490                         }
491                         c = Fsnewcall(c, raddr, rport, laddr, lport, version);
492                         if (c == NULL) {
493                                 qunlock(&udp->qlock);
494                                 freeblist(bp);
495                                 return;
496                         }
497                         iphtadd(&upriv->ht, c);
498                         ucb = (Udpcb *) c->ptcl;
499                 }
500         }
501
502         qlock(&c->qlock);
503         qunlock(&udp->qlock);
504
505         /*
506          * Trim the packet down to data size
507          */
508         len -= UDP_UDPHDR_SZ;
509         switch (version) {
510                 case V4:
511                         bp = trimblock(bp, UDP4_IPHDR_SZ + UDP_UDPHDR_SZ, len);
512                         break;
513                 case V6:
514                         bp = trimblock(bp, UDP6_IPHDR_SZ + UDP_UDPHDR_SZ, len);
515                         break;
516                 default:
517                         bp = NULL;
518                         panic("udpiput4: version %d", version);
519         }
520         if (bp == NULL) {
521                 qunlock(&c->qlock);
522                 netlog(f, Logudp, "udp: len err %I.%d -> %I.%d\n", raddr, rport,
523                            laddr, lport);
524                 upriv->lenerr++;
525                 return;
526         }
527
528         netlog(f, Logudpmsg, "udp: %I.%d -> %I.%d l %d\n", raddr, rport,
529                    laddr, lport, len);
530
531         switch (ucb->headers) {
532                 case 7:
533                         /* pass the src address */
534                         bp = padblock(bp, UDP_USEAD7);
535                         p = bp->rp;
536                         ipmove(p, raddr);
537                         p += IPaddrlen;
538                         ipmove(p, laddr);
539                         p += IPaddrlen;
540                         ipmove(p, ifc->lifc->local);
541                         p += IPaddrlen;
542                         hnputs(p, rport);
543                         p += 2;
544                         hnputs(p, lport);
545                         break;
546                 case 6:
547                         /* pass the src address */
548                         bp = padblock(bp, UDP_USEAD6);
549                         p = bp->rp;
550                         ipmove(p, raddr);
551                         p += IPaddrlen;
552                         ipmove(p, ipforme(f, laddr) == Runi ? laddr : ifc->lifc->local);
553                         p += IPaddrlen;
554                         hnputs(p, rport);
555                         p += 2;
556                         hnputs(p, lport);
557                         break;
558         }
559
560         if (bp->next)
561                 bp = concatblock(bp);
562
563         if (qfull(c->rq)) {
564                 qunlock(&c->qlock);
565                 netlog(f, Logudp, "udp: qfull %I.%d -> %I.%d\n", raddr, rport,
566                            laddr, lport);
567                 freeblist(bp);
568                 return;
569         }
570
571         qpass(c->rq, bp);
572         qunlock(&c->qlock);
573
574 }
575
576 static void udpctl(struct conv *c, char **f, int n)
577 {
578         Udpcb *ucb = (Udpcb*)c->ptcl;
579
580         if ((n == 1) && strcmp(f[0], "oldheaders") == 0)
581                 ucb->headers = 6;
582         else if ((n == 1) && strcmp(f[0], "headers") == 0)
583                 ucb->headers = 7;
584         else
585                 error(EINVAL, "unknown command to %s", __func__);
586 }
587
588 void udpadvise(struct Proto *udp, struct block *bp, char *msg)
589 {
590         Udp4hdr *h4;
591         Udp6hdr *h6;
592         uint8_t source[IPaddrlen], dest[IPaddrlen];
593         uint16_t psource, pdest;
594         struct conv *s, **p;
595         int version;
596
597         h4 = (Udp4hdr *) (bp->rp);
598         version = ((h4->vihl & 0xF0) == IP_VER6) ? V6 : V4;
599
600         switch (version) {
601                 case V4:
602                         v4tov6(dest, h4->udpdst);
603                         v4tov6(source, h4->udpsrc);
604                         psource = nhgets(h4->udpsport);
605                         pdest = nhgets(h4->udpdport);
606                         break;
607                 case V6:
608                         h6 = (Udp6hdr *) (bp->rp);
609                         ipmove(dest, h6->udpdst);
610                         ipmove(source, h6->udpsrc);
611                         psource = nhgets(h6->udpsport);
612                         pdest = nhgets(h6->udpdport);
613                         break;
614                 default:
615                         panic("udpadvise: version %d", version);
616                         return; /* to avoid a warning */
617         }
618
619         /* Look for a connection */
620         qlock(&udp->qlock);
621         for (p = udp->conv; *p; p++) {
622                 s = *p;
623                 if (s->rport == pdest)
624                         if (s->lport == psource)
625                                 if (ipcmp(s->raddr, dest) == 0)
626                                         if (ipcmp(s->laddr, source) == 0) {
627                                                 if (s->ignoreadvice)
628                                                         break;
629                                                 qlock(&s->qlock);
630                                                 qunlock(&udp->qlock);
631                                                 qhangup(s->rq, msg);
632                                                 qhangup(s->wq, msg);
633                                                 qunlock(&s->qlock);
634                                                 freeblist(bp);
635                                                 return;
636                                         }
637         }
638         qunlock(&udp->qlock);
639         freeblist(bp);
640 }
641
642 int udpstats(struct Proto *udp, char *buf, int len)
643 {
644         Udppriv *upriv;
645         char *p, *e;
646
647         upriv = udp->priv;
648         p = buf;
649         e = p + len;
650         p = seprintf(p, e, "InDatagrams: %u\n", upriv->ustats.udpInDatagrams);
651         p = seprintf(p, e, "NoPorts: %u\n", upriv->ustats.udpNoPorts);
652         p = seprintf(p, e, "InErrors: %u\n", upriv->ustats.udpInErrors);
653         p = seprintf(p, e, "OutDatagrams: %u\n", upriv->ustats.udpOutDatagrams);
654         return p - buf;
655 }
656
657 void udpnewconv(struct Proto *udp, struct conv *conv)
658 {
659         /* Fsprotoclone alloc'd our priv struct and attached it to conv already.
660          * Now we need to init it */
661         struct Udpcb *ucb = (struct Udpcb *)conv->ptcl;
662         qlock_init(&ucb->qlock);
663 }
664
665 void udpinit(struct Fs *fs)
666 {
667         struct Proto *udp;
668
669         udp = kzmalloc(sizeof(struct Proto), 0);
670         udp->priv = kzmalloc(sizeof(Udppriv), 0);
671         udp->name = "udp";
672         udp->connect = udpconnect;
673         udp->announce = udpannounce;
674         udp->ctl = udpctl;
675         udp->state = udpstate;
676         udp->create = udpcreate;
677         udp->close = udpclose;
678         udp->rcv = udpiput;
679         udp->advise = udpadvise;
680         udp->stats = udpstats;
681         udp->ipproto = IP_UDPPROTO;
682         udp->nc = Nchans;
683         udp->newconv = udpnewconv;
684         udp->ptclsize = sizeof(Udpcb);
685
686         Fsproto(fs, udp);
687 }