Implement TSO
[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         bp = pullupblock(bp, ICMP_IPSIZE + ICMP_HDRSIZE);
180         if (bp == 0)
181                 return;
182         p = (Icmp *) (bp->rp);
183         p->vihl = IP_VER4;
184         ipriv = c->p->priv;
185         if (p->type <= Maxtype)
186                 ipriv->out[p->type]++;
187
188         v6tov4(p->dst, c->raddr);
189         v6tov4(p->src, c->laddr);
190         p->proto = IP_ICMPPROTO;
191         hnputs(p->icmpid, c->lport);
192         memset(p->cksum, 0, sizeof(p->cksum));
193         hnputs(p->cksum, ptclcsum(bp, ICMP_IPSIZE, blocklen(bp) - ICMP_IPSIZE));
194         ipriv->stats[OutMsgs]++;
195         ipoput4(c->p->f, bp, 0, c->ttl, c->tos, NULL);
196 }
197
198 extern void icmpttlexceeded(struct Fs *f, uint8_t * ia, struct block *bp)
199 {
200         struct block *nbp;
201         Icmp *p, *np;
202
203         p = (Icmp *) bp->rp;
204
205         netlog(f, Logicmp, "sending icmpttlexceeded -> %V\n", p->src);
206         nbp = allocb(ICMP_IPSIZE + ICMP_HDRSIZE + ICMP_IPSIZE + 8);
207         nbp->wp += ICMP_IPSIZE + ICMP_HDRSIZE + ICMP_IPSIZE + 8;
208         np = (Icmp *) nbp->rp;
209         np->vihl = IP_VER4;
210         memmove(np->dst, p->src, sizeof(np->dst));
211         v6tov4(np->src, ia);
212         memmove(np->data, bp->rp, ICMP_IPSIZE + 8);
213         np->type = TimeExceed;
214         np->code = 0;
215         np->proto = IP_ICMPPROTO;
216         hnputs(np->icmpid, 0);
217         hnputs(np->seq, 0);
218         memset(np->cksum, 0, sizeof(np->cksum));
219         hnputs(np->cksum, ptclcsum(nbp, ICMP_IPSIZE, blocklen(nbp) - ICMP_IPSIZE));
220         ipoput4(f, nbp, 0, MAXTTL, DFLTTOS, NULL);
221
222 }
223
224 static void icmpunreachable(struct Fs *f, struct block *bp, int code, int seq)
225 {
226         struct block *nbp;
227         Icmp *p, *np;
228         int i;
229         uint8_t addr[IPaddrlen];
230
231         p = (Icmp *) bp->rp;
232
233         /* only do this for unicast sources and destinations */
234         v4tov6(addr, p->dst);
235         i = ipforme(f, addr);
236         if ((i & Runi) == 0)
237                 return;
238         v4tov6(addr, p->src);
239         i = ipforme(f, addr);
240         if (i != 0 && (i & Runi) == 0)
241                 return;
242
243         netlog(f, Logicmp, "sending icmpnoconv -> %V\n", p->src);
244         nbp = allocb(ICMP_IPSIZE + ICMP_HDRSIZE + ICMP_IPSIZE + 8);
245         nbp->wp += ICMP_IPSIZE + ICMP_HDRSIZE + ICMP_IPSIZE + 8;
246         np = (Icmp *) nbp->rp;
247         np->vihl = IP_VER4;
248         memmove(np->dst, p->src, sizeof(np->dst));
249         memmove(np->src, p->dst, sizeof(np->src));
250         memmove(np->data, bp->rp, ICMP_IPSIZE + 8);
251         np->type = Unreachable;
252         np->code = code;
253         np->proto = IP_ICMPPROTO;
254         hnputs(np->icmpid, 0);
255         hnputs(np->seq, seq);
256         memset(np->cksum, 0, sizeof(np->cksum));
257         hnputs(np->cksum, ptclcsum(nbp, ICMP_IPSIZE, blocklen(nbp) - ICMP_IPSIZE));
258         ipoput4(f, nbp, 0, MAXTTL, DFLTTOS, NULL);
259 }
260
261 extern void icmpnoconv(struct Fs *f, struct block *bp)
262 {
263         icmpunreachable(f, bp, 3, 0);
264 }
265
266 extern void icmpcantfrag(struct Fs *f, struct block *bp, int mtu)
267 {
268         icmpunreachable(f, bp, 4, mtu);
269 }
270
271 static void goticmpkt(struct Proto *icmp, struct block *bp)
272 {
273         struct conv **c, *s;
274         Icmp *p;
275         uint8_t dst[IPaddrlen];
276         uint16_t recid;
277
278         p = (Icmp *) bp->rp;
279         v4tov6(dst, p->src);
280         recid = nhgets(p->icmpid);
281
282         for (c = icmp->conv; *c; c++) {
283                 s = *c;
284                 if (s->lport == recid)
285                         if (ipcmp(s->raddr, dst) == 0) {
286                                 bp = concatblock(bp);
287                                 if (bp != NULL)
288                                         qpass(s->rq, bp);
289                                 return;
290                         }
291         }
292         freeblist(bp);
293 }
294
295 static struct block *mkechoreply(struct block *bp)
296 {
297         Icmp *q;
298         uint8_t ip[4];
299
300         q = (Icmp *) bp->rp;
301         q->vihl = IP_VER4;
302         memmove(ip, q->src, sizeof(q->dst));
303         memmove(q->src, q->dst, sizeof(q->src));
304         memmove(q->dst, ip, sizeof(q->dst));
305         q->type = EchoReply;
306         memset(q->cksum, 0, sizeof(q->cksum));
307         hnputs(q->cksum, ptclcsum(bp, ICMP_IPSIZE, blocklen(bp) - ICMP_IPSIZE));
308
309         return bp;
310 }
311
312 static char *unreachcode[] = {
313         [0] "net unreachable",
314         [1] "host unreachable",
315         [2] "protocol unreachable",
316         [3] "port unreachable",
317         [4] "fragmentation needed and DF set",
318         [5] "source route failed",
319 };
320
321 static void icmpiput(struct Proto *icmp, struct Ipifc *unused, struct block *bp)
322 {
323         int n, iplen;
324         Icmp *p;
325         struct block *r;
326         struct Proto *pr;
327         char *msg;
328         char m2[128];
329         Icmppriv *ipriv;
330
331         ipriv = icmp->priv;
332
333         ipriv->stats[InMsgs]++;
334
335         p = (Icmp *) bp->rp;
336         netlog(icmp->f, Logicmp, "icmpiput %d %d\n", p->type, p->code);
337         n = blocklen(bp);
338         if (n < ICMP_IPSIZE + ICMP_HDRSIZE) {
339                 ipriv->stats[InErrors]++;
340                 ipriv->stats[HlenErrs]++;
341                 netlog(icmp->f, Logicmp, "icmp hlen %d\n", n);
342                 goto raise;
343         }
344         iplen = nhgets(p->length);
345         if (iplen > n || (iplen % 1)) {
346                 ipriv->stats[LenErrs]++;
347                 ipriv->stats[InErrors]++;
348                 netlog(icmp->f, Logicmp, "icmp length %d\n", iplen);
349                 goto raise;
350         }
351         if (ptclcsum(bp, ICMP_IPSIZE, iplen - ICMP_IPSIZE)) {
352                 ipriv->stats[InErrors]++;
353                 ipriv->stats[CsumErrs]++;
354                 netlog(icmp->f, Logicmp, "icmp checksum error\n");
355                 goto raise;
356         }
357         if (p->type <= Maxtype)
358                 ipriv->in[p->type]++;
359
360         switch (p->type) {
361                 case EchoRequest:
362                         if (iplen < n)
363                                 bp = trimblock(bp, 0, iplen);
364                         r = mkechoreply(bp);
365                         ipriv->out[EchoReply]++;
366                         ipoput4(icmp->f, r, 0, MAXTTL, DFLTTOS, NULL);
367                         break;
368                 case Unreachable:
369                         if (p->code > 5)
370                                 msg = unreachcode[1];
371                         else
372                                 msg = unreachcode[p->code];
373
374                         bp->rp += ICMP_IPSIZE + ICMP_HDRSIZE;
375                         if (blocklen(bp) < MinAdvise) {
376                                 ipriv->stats[LenErrs]++;
377                                 goto raise;
378                         }
379                         p = (Icmp *) bp->rp;
380                         pr = Fsrcvpcolx(icmp->f, p->proto);
381                         if (pr != NULL && pr->advise != NULL) {
382                                 (*pr->advise) (pr, bp, msg);
383                                 return;
384                         }
385
386                         bp->rp -= ICMP_IPSIZE + ICMP_HDRSIZE;
387                         goticmpkt(icmp, bp);
388                         break;
389                 case TimeExceed:
390                         if (p->code == 0) {
391                                 snprintf(m2, sizeof(m2), "ttl exceeded at %V", p->src);
392
393                                 bp->rp += ICMP_IPSIZE + ICMP_HDRSIZE;
394                                 if (blocklen(bp) < MinAdvise) {
395                                         ipriv->stats[LenErrs]++;
396                                         goto raise;
397                                 }
398                                 p = (Icmp *) bp->rp;
399                                 pr = Fsrcvpcolx(icmp->f, p->proto);
400                                 if (pr != NULL && pr->advise != NULL) {
401                                         (*pr->advise) (pr, bp, m2);
402                                         return;
403                                 }
404                                 bp->rp -= ICMP_IPSIZE + ICMP_HDRSIZE;
405                         }
406
407                         goticmpkt(icmp, bp);
408                         break;
409                 default:
410                         goticmpkt(icmp, bp);
411                         break;
412         }
413         return;
414
415 raise:
416         freeblist(bp);
417 }
418
419 void icmpadvise(struct Proto *icmp, struct block *bp, char *msg)
420 {
421         struct conv **c, *s;
422         Icmp *p;
423         uint8_t dst[IPaddrlen];
424         uint16_t recid;
425
426         p = (Icmp *) bp->rp;
427         v4tov6(dst, p->dst);
428         recid = nhgets(p->icmpid);
429
430         for (c = icmp->conv; *c; c++) {
431                 s = *c;
432                 if (s->lport == recid)
433                         if (ipcmp(s->raddr, dst) == 0) {
434                                 qhangup(s->rq, msg);
435                                 qhangup(s->wq, msg);
436                                 break;
437                         }
438         }
439         freeblist(bp);
440 }
441
442 int icmpstats(struct Proto *icmp, char *buf, int len)
443 {
444         Icmppriv *priv;
445         char *p, *e;
446         int i;
447
448         priv = icmp->priv;
449         p = buf;
450         e = p + len;
451         for (i = 0; i < Nstats; i++)
452                 p = seprintf(p, e, "%s: %u\n", statnames[i], priv->stats[i]);
453         for (i = 0; i <= Maxtype; i++) {
454                 if (icmpnames[i])
455                         p = seprintf(p, e, "%s: %u %u\n", icmpnames[i], priv->in[i],
456                                                  priv->out[i]);
457                 else
458                         p = seprintf(p, e, "%d: %u %u\n", i, priv->in[i], priv->out[i]);
459         }
460         return p - buf;
461 }
462
463 void icmpinit(struct Fs *fs)
464 {
465         struct Proto *icmp;
466
467         icmp = kzmalloc(sizeof(struct Proto), 0);
468         icmp->priv = kzmalloc(sizeof(Icmppriv), 0);
469         icmp->name = "icmp";
470         icmp->connect = icmpconnect;
471         icmp->announce = icmpannounce;
472         icmp->state = icmpstate;
473         icmp->create = icmpcreate;
474         icmp->close = icmpclose;
475         icmp->rcv = icmpiput;
476         icmp->stats = icmpstats;
477         icmp->ctl = NULL;
478         icmp->advise = icmpadvise;
479         icmp->gc = NULL;
480         icmp->ipproto = IP_ICMPPROTO;
481         icmp->nc = 128;
482         icmp->ptclsize = 0;
483
484         Fsproto(fs, icmp);
485 }