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