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