Fixes network stats printf strings
[akaros.git] / kern / src / net / icmp.c
1 // INFERNO
2 #include <vfs.h>
3 #include <kfs.h>
4 #include <slab.h>
5 #include <kmalloc.h>
6 #include <kref.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <assert.h>
10 #include <error.h>
11 #include <cpio.h>
12 #include <pmap.h>
13 #include <smp.h>
14 #include <ip.h>
15
16 #include <vfs.h>
17 #include <kfs.h>
18 #include <slab.h>
19 #include <kmalloc.h>
20 #include <kref.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <assert.h>
24 #include <error.h>
25 #include <cpio.h>
26 #include <pmap.h>
27 #include <smp.h>
28 #include <ip.h>
29
30 typedef struct Icmp {
31         uint8_t vihl;                           /* Version and header length */
32         uint8_t tos;                            /* Type of service */
33         uint8_t length[2];                      /* packet length */
34         uint8_t id[2];                          /* Identification */
35         uint8_t frag[2];                        /* Fragment information */
36         uint8_t ttl;                            /* Time to live */
37         uint8_t proto;                          /* Protocol */
38         uint8_t ipcksum[2];                     /* Header checksum */
39         uint8_t src[4];                         /* Ip source */
40         uint8_t dst[4];                         /* Ip destination */
41         uint8_t type;
42         uint8_t code;
43         uint8_t cksum[2];
44         uint8_t icmpid[2];
45         uint8_t seq[2];
46         uint8_t data[1];
47 } Icmp;
48
49 enum {                                                  /* Packet Types */
50         EchoReply = 0,
51         Unreachable = 3,
52         SrcQuench = 4,
53         Redirect = 5,
54         EchoRequest = 8,
55         TimeExceed = 11,
56         InParmProblem = 12,
57         Timestamp = 13,
58         TimestampReply = 14,
59         InfoRequest = 15,
60         InfoReply = 16,
61         AddrMaskRequest = 17,
62         AddrMaskReply = 18,
63
64         Maxtype = 18,
65 };
66
67 enum {
68         MinAdvise = 24,                         /* minimum needed for us to advise another protocol */
69 };
70
71 char *icmpnames[Maxtype + 1] = {
72         [EchoReply] "EchoReply",
73         [Unreachable] "Unreachable",
74         [SrcQuench] "SrcQuench",
75         [Redirect] "Redirect",
76         [EchoRequest] "EchoRequest",
77         [TimeExceed] "TimeExceed",
78         [InParmProblem] "InParmProblem",
79         [Timestamp] "Timestamp",
80         [TimestampReply] "TimestampReply",
81         [InfoRequest] "InfoRequest",
82         [InfoReply] "InfoReply",
83         [AddrMaskRequest] "AddrMaskRequest",
84         [AddrMaskReply] "AddrMaskReply  ",
85 };
86
87 enum {
88         IP_ICMPPROTO = 1,
89         ICMP_IPSIZE = 20,
90         ICMP_HDRSIZE = 8,
91 };
92
93 enum {
94         InMsgs,
95         InErrors,
96         OutMsgs,
97         CsumErrs,
98         LenErrs,
99         HlenErrs,
100
101         Nstats,
102 };
103
104 static char *statnames[Nstats] = {
105         [InMsgs] "InMsgs",
106         [InErrors] "InErrors",
107         [OutMsgs] "OutMsgs",
108         [CsumErrs] "CsumErrs",
109         [LenErrs] "LenErrs",
110         [HlenErrs] "HlenErrs",
111 };
112
113 typedef struct Icmppriv Icmppriv;
114 struct Icmppriv {
115         uint32_t stats[Nstats];
116
117         /* message counts */
118         uint32_t in[Maxtype + 1];
119         uint32_t out[Maxtype + 1];
120 };
121
122 static void icmpkick(void *x, struct block *);
123
124 static void icmpcreate(struct conv *c)
125 {
126         c->rq = qopen(64 * 1024, Qmsg, 0, c);
127         c->wq = qbypass(icmpkick, c);
128 }
129
130 extern char *icmpconnect(struct conv *c, char **argv, int argc)
131 {
132         char *e;
133
134         e = Fsstdconnect(c, argv, argc);
135         if (e != NULL)
136                 return e;
137         Fsconnected(c, e);
138
139         return NULL;
140 }
141
142 extern int icmpstate(struct conv *c, char *state, int n)
143 {
144         return snprintf(state, n, "%s qin %d qout %d",
145                                         "Datagram",
146                                         c->rq ? qlen(c->rq) : 0, c->wq ? qlen(c->wq) : 0);
147 }
148
149 extern char *icmpannounce(struct conv *c, char **argv, int argc)
150 {
151         char *e;
152
153         e = Fsstdannounce(c, argv, argc);
154         if (e != NULL)
155                 return e;
156         Fsconnected(c, NULL);
157
158         return NULL;
159 }
160
161 extern void icmpclose(struct conv *c)
162 {
163         qclose(c->rq);
164         qclose(c->wq);
165         ipmove(c->laddr, IPnoaddr);
166         ipmove(c->raddr, IPnoaddr);
167         c->lport = 0;
168 }
169
170 static void icmpkick(void *x, struct block *bp)
171 {
172         struct conv *c = x;
173         Icmp *p;
174         Icmppriv *ipriv;
175
176         if (bp == NULL)
177                 return;
178
179         if (blocklen(bp) < ICMP_IPSIZE + ICMP_HDRSIZE) {
180                 freeblist(bp);
181                 return;
182         }
183         p = (Icmp *) (bp->rp);
184         p->vihl = IP_VER4;
185         ipriv = c->p->priv;
186         if (p->type <= Maxtype)
187                 ipriv->out[p->type]++;
188
189         v6tov4(p->dst, c->raddr);
190         v6tov4(p->src, c->laddr);
191         p->proto = IP_ICMPPROTO;
192         hnputs(p->icmpid, c->lport);
193         memset(p->cksum, 0, sizeof(p->cksum));
194         hnputs(p->cksum, ptclcsum(bp, ICMP_IPSIZE, blocklen(bp) - ICMP_IPSIZE));
195         ipriv->stats[OutMsgs]++;
196         ipoput4(c->p->f, bp, 0, c->ttl, c->tos, NULL);
197 }
198
199 extern void icmpttlexceeded(struct Fs *f, uint8_t * ia, struct block *bp)
200 {
201         struct block *nbp;
202         Icmp *p, *np;
203
204         p = (Icmp *) bp->rp;
205
206         netlog(f, Logicmp, "sending icmpttlexceeded -> %V\n", p->src);
207         nbp = allocb(ICMP_IPSIZE + ICMP_HDRSIZE + ICMP_IPSIZE + 8);
208         nbp->wp += ICMP_IPSIZE + ICMP_HDRSIZE + ICMP_IPSIZE + 8;
209         np = (Icmp *) nbp->rp;
210         np->vihl = IP_VER4;
211         memmove(np->dst, p->src, sizeof(np->dst));
212         v6tov4(np->src, ia);
213         memmove(np->data, bp->rp, ICMP_IPSIZE + 8);
214         np->type = TimeExceed;
215         np->code = 0;
216         np->proto = IP_ICMPPROTO;
217         hnputs(np->icmpid, 0);
218         hnputs(np->seq, 0);
219         memset(np->cksum, 0, sizeof(np->cksum));
220         hnputs(np->cksum, ptclcsum(nbp, ICMP_IPSIZE, blocklen(nbp) - ICMP_IPSIZE));
221         ipoput4(f, nbp, 0, MAXTTL, DFLTTOS, NULL);
222
223 }
224
225 static void icmpunreachable(struct Fs *f, struct block *bp, int code, int seq)
226 {
227         struct block *nbp;
228         Icmp *p, *np;
229         int i;
230         uint8_t addr[IPaddrlen];
231
232         p = (Icmp *) bp->rp;
233
234         /* only do this for unicast sources and destinations */
235         v4tov6(addr, p->dst);
236         i = ipforme(f, addr);
237         if ((i & Runi) == 0)
238                 return;
239         v4tov6(addr, p->src);
240         i = ipforme(f, addr);
241         if (i != 0 && (i & Runi) == 0)
242                 return;
243
244         netlog(f, Logicmp, "sending icmpnoconv -> %V\n", p->src);
245         nbp = allocb(ICMP_IPSIZE + ICMP_HDRSIZE + ICMP_IPSIZE + 8);
246         nbp->wp += ICMP_IPSIZE + ICMP_HDRSIZE + ICMP_IPSIZE + 8;
247         np = (Icmp *) nbp->rp;
248         np->vihl = IP_VER4;
249         memmove(np->dst, p->src, sizeof(np->dst));
250         memmove(np->src, p->dst, sizeof(np->src));
251         memmove(np->data, bp->rp, ICMP_IPSIZE + 8);
252         np->type = Unreachable;
253         np->code = code;
254         np->proto = IP_ICMPPROTO;
255         hnputs(np->icmpid, 0);
256         hnputs(np->seq, seq);
257         memset(np->cksum, 0, sizeof(np->cksum));
258         hnputs(np->cksum, ptclcsum(nbp, ICMP_IPSIZE, blocklen(nbp) - ICMP_IPSIZE));
259         ipoput4(f, nbp, 0, MAXTTL, DFLTTOS, NULL);
260 }
261
262 extern void icmpnoconv(struct Fs *f, struct block *bp)
263 {
264         icmpunreachable(f, bp, 3, 0);
265 }
266
267 extern void icmpcantfrag(struct Fs *f, struct block *bp, int mtu)
268 {
269         icmpunreachable(f, bp, 4, mtu);
270 }
271
272 static void goticmpkt(struct Proto *icmp, struct block *bp)
273 {
274         struct conv **c, *s;
275         Icmp *p;
276         uint8_t dst[IPaddrlen];
277         uint16_t recid;
278
279         p = (Icmp *) bp->rp;
280         v4tov6(dst, p->src);
281         recid = nhgets(p->icmpid);
282
283         for (c = icmp->conv; *c; c++) {
284                 s = *c;
285                 if (s->lport == recid)
286                         if (ipcmp(s->raddr, dst) == 0) {
287                                 bp = concatblock(bp);
288                                 if (bp != NULL)
289                                         qpass(s->rq, bp);
290                                 return;
291                         }
292         }
293         freeblist(bp);
294 }
295
296 static struct block *mkechoreply(struct block *bp)
297 {
298         Icmp *q;
299         uint8_t ip[4];
300
301         q = (Icmp *) bp->rp;
302         q->vihl = IP_VER4;
303         memmove(ip, q->src, sizeof(q->dst));
304         memmove(q->src, q->dst, sizeof(q->src));
305         memmove(q->dst, ip, sizeof(q->dst));
306         q->type = EchoReply;
307         memset(q->cksum, 0, sizeof(q->cksum));
308         hnputs(q->cksum, ptclcsum(bp, ICMP_IPSIZE, blocklen(bp) - ICMP_IPSIZE));
309
310         return bp;
311 }
312
313 static char *unreachcode[] = {
314         [0] "net unreachable",
315         [1] "host unreachable",
316         [2] "protocol unreachable",
317         [3] "port unreachable",
318         [4] "fragmentation needed and DF set",
319         [5] "source route failed",
320 };
321
322 static void icmpiput(struct Proto *icmp, struct Ipifc *unused, struct block *bp)
323 {
324         int n, iplen;
325         Icmp *p;
326         struct block *r;
327         struct Proto *pr;
328         char *msg;
329         char m2[128];
330         Icmppriv *ipriv;
331
332         ipriv = icmp->priv;
333
334         ipriv->stats[InMsgs]++;
335
336         p = (Icmp *) bp->rp;
337         netlog(icmp->f, Logicmp, "icmpiput %d %d\n", p->type, p->code);
338         n = blocklen(bp);
339         if (n < ICMP_IPSIZE + ICMP_HDRSIZE) {
340                 ipriv->stats[InErrors]++;
341                 ipriv->stats[HlenErrs]++;
342                 netlog(icmp->f, Logicmp, "icmp hlen %d\n", n);
343                 goto raise;
344         }
345         iplen = nhgets(p->length);
346         if (iplen > n || (iplen % 1)) {
347                 ipriv->stats[LenErrs]++;
348                 ipriv->stats[InErrors]++;
349                 netlog(icmp->f, Logicmp, "icmp length %d\n", iplen);
350                 goto raise;
351         }
352         if (ptclcsum(bp, ICMP_IPSIZE, iplen - ICMP_IPSIZE)) {
353                 ipriv->stats[InErrors]++;
354                 ipriv->stats[CsumErrs]++;
355                 netlog(icmp->f, Logicmp, "icmp checksum error\n");
356                 goto raise;
357         }
358         if (p->type <= Maxtype)
359                 ipriv->in[p->type]++;
360
361         switch (p->type) {
362                 case EchoRequest:
363                         if (iplen < n)
364                                 bp = trimblock(bp, 0, iplen);
365                         r = mkechoreply(bp);
366                         ipriv->out[EchoReply]++;
367                         ipoput4(icmp->f, r, 0, MAXTTL, DFLTTOS, NULL);
368                         break;
369                 case Unreachable:
370                         if (p->code > 5)
371                                 msg = unreachcode[1];
372                         else
373                                 msg = unreachcode[p->code];
374
375                         bp->rp += ICMP_IPSIZE + ICMP_HDRSIZE;
376                         if (blocklen(bp) < MinAdvise) {
377                                 ipriv->stats[LenErrs]++;
378                                 goto raise;
379                         }
380                         p = (Icmp *) bp->rp;
381                         pr = Fsrcvpcolx(icmp->f, p->proto);
382                         if (pr != NULL && pr->advise != NULL) {
383                                 (*pr->advise) (pr, bp, msg);
384                                 return;
385                         }
386
387                         bp->rp -= ICMP_IPSIZE + ICMP_HDRSIZE;
388                         goticmpkt(icmp, bp);
389                         break;
390                 case TimeExceed:
391                         if (p->code == 0) {
392                                 snprintf(m2, sizeof(m2), "ttl exceeded at %V", p->src);
393
394                                 bp->rp += ICMP_IPSIZE + ICMP_HDRSIZE;
395                                 if (blocklen(bp) < MinAdvise) {
396                                         ipriv->stats[LenErrs]++;
397                                         goto raise;
398                                 }
399                                 p = (Icmp *) bp->rp;
400                                 pr = Fsrcvpcolx(icmp->f, p->proto);
401                                 if (pr != NULL && pr->advise != NULL) {
402                                         (*pr->advise) (pr, bp, m2);
403                                         return;
404                                 }
405                                 bp->rp -= ICMP_IPSIZE + ICMP_HDRSIZE;
406                         }
407
408                         goticmpkt(icmp, bp);
409                         break;
410                 default:
411                         goticmpkt(icmp, bp);
412                         break;
413         }
414         return;
415
416 raise:
417         freeblist(bp);
418 }
419
420 void icmpadvise(struct Proto *icmp, struct block *bp, char *msg)
421 {
422         struct conv **c, *s;
423         Icmp *p;
424         uint8_t dst[IPaddrlen];
425         uint16_t recid;
426
427         p = (Icmp *) bp->rp;
428         v4tov6(dst, p->dst);
429         recid = nhgets(p->icmpid);
430
431         for (c = icmp->conv; *c; c++) {
432                 s = *c;
433                 if (s->lport == recid)
434                         if (ipcmp(s->raddr, dst) == 0) {
435                                 qhangup(s->rq, msg);
436                                 qhangup(s->wq, msg);
437                                 break;
438                         }
439         }
440         freeblist(bp);
441 }
442
443 int icmpstats(struct Proto *icmp, char *buf, int len)
444 {
445         Icmppriv *priv;
446         char *p, *e;
447         int i;
448
449         priv = icmp->priv;
450         p = buf;
451         e = p + len;
452         for (i = 0; i < Nstats; i++)
453                 p = seprintf(p, e, "%s: %u\n", statnames[i], priv->stats[i]);
454         for (i = 0; i <= Maxtype; i++) {
455                 if (icmpnames[i])
456                         p = seprintf(p, e, "%s: %u %u\n", icmpnames[i], priv->in[i],
457                                                  priv->out[i]);
458                 else
459                         p = seprintf(p, e, "%d: %u %u\n", i, priv->in[i], priv->out[i]);
460         }
461         return p - buf;
462 }
463
464 void icmpinit(struct Fs *fs)
465 {
466         struct Proto *icmp;
467
468         icmp = kzmalloc(sizeof(struct Proto), 0);
469         icmp->priv = kzmalloc(sizeof(Icmppriv), 0);
470         icmp->name = "icmp";
471         icmp->connect = icmpconnect;
472         icmp->announce = icmpannounce;
473         icmp->state = icmpstate;
474         icmp->create = icmpcreate;
475         icmp->close = icmpclose;
476         icmp->rcv = icmpiput;
477         icmp->stats = icmpstats;
478         icmp->ctl = NULL;
479         icmp->advise = icmpadvise;
480         icmp->gc = NULL;
481         icmp->ipproto = IP_ICMPPROTO;
482         icmp->nc = 128;
483         icmp->ptclsize = 0;
484
485         Fsproto(fs, icmp);
486 }