fuzzer rx overflow mitigate
[platform/upstream/libwebsockets.git] / lib / parsers.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2013 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  */
21
22 #include "private-libwebsockets.h"
23
24 unsigned char lextable[] = {
25         #include "lextable.h"
26 };
27
28 #define FAIL_CHAR 0x08
29
30 int lextable_decode(int pos, char c)
31 {
32         c = tolower(c);
33
34         while (1) {
35                 if (lextable[pos] & (1 << 7)) { /* 1-byte, fail on mismatch */
36                         if ((lextable[pos] & 0x7f) != c)
37                                 return -1;
38                         /* fall thru */
39                         pos++;
40                         if (lextable[pos] == FAIL_CHAR)
41                                 return -1;
42                         return pos;
43                 }
44
45                 if (lextable[pos] == FAIL_CHAR)
46                         return -1;
47
48                 /* b7 = 0, end or 3-byte */
49                 if (lextable[pos] < FAIL_CHAR) /* terminal marker */
50                         return pos;
51
52                 if (lextable[pos] == c) /* goto */
53                         return pos + (lextable[pos + 1]) +
54                                                 (lextable[pos + 2] << 8);
55                 /* fall thru goto */
56                 pos += 3;
57                 /* continue */
58         }
59 }
60
61 int lws_allocate_header_table(struct lws *wsi)
62 {
63         struct lws_context *context = wsi->context;
64         int n;
65
66         lwsl_debug("%s: wsi %p: ah %p\n", __func__, (void *)wsi,
67                  (void *)wsi->u.hdr.ah);
68
69         /* if we are already bound to one, just clear it down */
70         if (wsi->u.hdr.ah)
71                 goto reset;
72         /*
73          * server should have suppressed the accept of a new wsi before this
74          * became the case.  If initiating multiple client connects, make sure
75          * the ah pool is big enough to cope, or be prepared to retry
76          */
77         if (context->ah_count_in_use == context->max_http_header_pool) {
78                 lwsl_err("No free ah\n");
79                 return -1;
80         }
81
82         for (n = 0; n < context->max_http_header_pool; n++)
83                 if (!context->ah_pool[n].in_use)
84                         break;
85
86         /* if the count of in use said something free... */
87         assert(n != context->max_http_header_pool);
88
89         wsi->u.hdr.ah = &context->ah_pool[n];
90         wsi->u.hdr.ah->in_use = 1;
91
92         context->ah_count_in_use++;
93         /* if we used up all the ah, defeat accepting new server connections */
94         if (context->ah_count_in_use == context->max_http_header_pool)
95                 if (_lws_server_listen_accept_flow_control(context, 0))
96                         return 1;
97
98         lwsl_debug("%s: wsi %p: ah %p: count %d (on exit)\n",
99                  __func__, (void *)wsi, (void *)wsi->u.hdr.ah,
100                  context->ah_count_in_use);
101
102 reset:
103         /* init the ah to reflect no headers or data have appeared yet */
104         memset(wsi->u.hdr.ah->frag_index, 0, sizeof(wsi->u.hdr.ah->frag_index));
105         wsi->u.hdr.ah->nfrag = 0;
106         wsi->u.hdr.ah->pos = 0;
107
108         return 0;
109 }
110
111 int lws_free_header_table(struct lws *wsi)
112 {
113         struct lws_context *context = wsi->context;
114
115         lwsl_debug("%s: wsi %p: ah %p (count = %d)\n", __func__, (void *)wsi,
116                  (void *)wsi->u.hdr.ah, context->ah_count_in_use);
117
118         assert(wsi->u.hdr.ah);
119         if (!wsi->u.hdr.ah)
120                 return 0;
121
122         /* if we think we're freeing one, there should be one to free */
123         assert(context->ah_count_in_use > 0);
124
125         assert(wsi->u.hdr.ah->in_use);
126         wsi->u.hdr.ah->in_use = 0;
127
128         /* if we just freed up one ah, allow new server connection */
129         if (context->ah_count_in_use == context->max_http_header_pool)
130                 if (_lws_server_listen_accept_flow_control(context, 1))
131                         return 1;
132
133         context->ah_count_in_use--;
134         wsi->u.hdr.ah = NULL;
135
136         return 0;
137 }
138
139 /**
140  * lws_hdr_fragment_length: report length of a single fragment of a header
141  *              The returned length does not include the space for a
142  *              terminating '\0'
143  *
144  * @wsi: websocket connection
145  * @h: which header index we are interested in
146  * @frag_idx: which fragment of @h we want to get the length of
147  */
148
149 LWS_VISIBLE int
150 lws_hdr_fragment_length(struct lws *wsi, enum lws_token_indexes h, int frag_idx)
151 {
152         int n;
153
154         n = wsi->u.hdr.ah->frag_index[h];
155         if (!n)
156                 return 0;
157         do {
158                 if (!frag_idx)
159                         return wsi->u.hdr.ah->frags[n].len;
160                 n = wsi->u.hdr.ah->frags[n].nfrag;
161         } while (frag_idx-- && n);
162
163         return 0;
164 }
165
166 /**
167  * lws_hdr_total_length: report length of all fragments of a header totalled up
168  *              The returned length does not include the space for a
169  *              terminating '\0'
170  *
171  * @wsi: websocket connection
172  * @h: which header index we are interested in
173  */
174
175 LWS_VISIBLE int lws_hdr_total_length(struct lws *wsi, enum lws_token_indexes h)
176 {
177         int n;
178         int len = 0;
179
180         n = wsi->u.hdr.ah->frag_index[h];
181         if (!n)
182                 return 0;
183         do {
184                 len += wsi->u.hdr.ah->frags[n].len;
185                 n = wsi->u.hdr.ah->frags[n].nfrag;
186         } while (n);
187
188         return len;
189 }
190
191 /**
192  * lws_hdr_copy_fragment: copy a single fragment of the given header to a buffer
193  *              The buffer length @len must include space for an additional
194  *              terminating '\0', or it will fail returning -1.
195  *              If the requested fragment index is not present, it fails
196  *              returning -1.
197  *
198  * @wsi: websocket connection
199  * @dst: destination buffer
200  * @len: length of destination buffer
201  * @h: which header index we are interested in
202  * @frag_index: which fragment of @h we want to copy
203  */
204
205 LWS_VISIBLE int lws_hdr_copy_fragment(struct lws *wsi, char *dst, int len,
206                                       enum lws_token_indexes h, int frag_idx)
207 {
208         int n = 0;
209         int f = wsi->u.hdr.ah->frag_index[h];
210
211         while (n < frag_idx) {
212                 f = wsi->u.hdr.ah->frags[f].nfrag;
213                 if (!f)
214                         return -1;
215                 n++;
216         }
217
218         if (wsi->u.hdr.ah->frags[f].len >= len)
219                 return -1;
220
221         memcpy(dst, wsi->u.hdr.ah->data + wsi->u.hdr.ah->frags[f].offset,
222                wsi->u.hdr.ah->frags[f].len);
223         dst[wsi->u.hdr.ah->frags[f].len] = '\0';
224
225         return wsi->u.hdr.ah->frags[f].len;
226 }
227
228 /**
229  * lws_hdr_copy: copy a single fragment of the given header to a buffer
230  *              The buffer length @len must include space for an additional
231  *              terminating '\0', or it will fail returning -1.
232  *
233  * @wsi: websocket connection
234  * @dst: destination buffer
235  * @len: length of destination buffer
236  * @h: which header index we are interested in
237  */
238
239 LWS_VISIBLE int lws_hdr_copy(struct lws *wsi, char *dst, int len,
240                              enum lws_token_indexes h)
241 {
242         int toklen = lws_hdr_total_length(wsi, h);
243         int n;
244
245         if (toklen >= len)
246                 return -1;
247
248         n = wsi->u.hdr.ah->frag_index[h];
249         if (!n)
250                 return 0;
251
252         do {
253                 strcpy(dst, &wsi->u.hdr.ah->data[wsi->u.hdr.ah->frags[n].offset]);
254                 dst += wsi->u.hdr.ah->frags[n].len;
255                 n = wsi->u.hdr.ah->frags[n].nfrag;
256         } while (n);
257
258         return toklen;
259 }
260
261 char *lws_hdr_simple_ptr(struct lws *wsi, enum lws_token_indexes h)
262 {
263         int n;
264
265         n = wsi->u.hdr.ah->frag_index[h];
266         if (!n)
267                 return NULL;
268
269         return wsi->u.hdr.ah->data + wsi->u.hdr.ah->frags[n].offset;
270 }
271
272 int lws_hdr_simple_create(struct lws *wsi, enum lws_token_indexes h,
273                           const char *s)
274 {
275         wsi->u.hdr.ah->nfrag++;
276         if (wsi->u.hdr.ah->nfrag == ARRAY_SIZE(wsi->u.hdr.ah->frags)) {
277                 lwsl_warn("More hdr frags than we can deal with, dropping\n");
278                 return -1;
279         }
280
281         wsi->u.hdr.ah->frag_index[h] = wsi->u.hdr.ah->nfrag;
282
283         wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].offset = wsi->u.hdr.ah->pos;
284         wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len = 0;
285         wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].nfrag = 0;
286
287         do {
288                 if (wsi->u.hdr.ah->pos == wsi->context->max_http_header_data) {
289                         lwsl_err("Ran out of header data space\n");
290                         return -1;
291                 }
292                 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = *s;
293                 if (*s)
294                         wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len++;
295         } while (*s++);
296
297         return 0;
298 }
299
300 static signed char char_to_hex(const char c)
301 {
302         if (c >= '0' && c <= '9')
303                 return c - '0';
304
305         if (c >= 'a' && c <= 'f')
306                 return c - 'a' + 10;
307
308         if (c >= 'A' && c <= 'F')
309                 return c - 'A' + 10;
310
311         return -1;
312 }
313
314 static int issue_char(struct lws *wsi, unsigned char c)
315 {
316         unsigned short frag_len;
317
318         if (wsi->u.hdr.ah->pos == wsi->context->max_http_header_data) {
319                 lwsl_warn("excessive header content\n");
320                 return -1;
321         }
322
323         frag_len = wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len;
324         /*
325          * If we haven't hit the token limit, just copy the character into
326          * the header
327          */
328         if (frag_len < wsi->u.hdr.current_token_limit) {
329                 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = c;
330                 if (c)
331                         wsi->u.hdr.ah->frags[wsi->u.hdr.ah->nfrag].len++;
332                 return 0;
333         }
334
335         /* Insert a null character when we *hit* the limit: */
336         if (frag_len == wsi->u.hdr.current_token_limit) {
337                 wsi->u.hdr.ah->data[wsi->u.hdr.ah->pos++] = '\0';
338                 lwsl_warn("header %i exceeds limit %d\n",
339                           wsi->u.hdr.parser_state, wsi->u.hdr.current_token_limit);
340         }
341
342         return 1;
343 }
344
345 int lws_parse(struct lws *wsi, unsigned char c)
346 {
347         static const unsigned char methods[] = {
348                 WSI_TOKEN_GET_URI,
349                 WSI_TOKEN_POST_URI,
350                 WSI_TOKEN_OPTIONS_URI,
351                 WSI_TOKEN_PUT_URI,
352                 WSI_TOKEN_PATCH_URI,
353                 WSI_TOKEN_DELETE_URI,
354         };
355         struct allocated_headers *ah = wsi->u.hdr.ah;
356         struct lws_context *context = wsi->context;
357         unsigned int n, m, enc = 0;
358
359         switch (wsi->u.hdr.parser_state) {
360         default:
361
362                 lwsl_parser("WSI_TOK_(%d) '%c'\n", wsi->u.hdr.parser_state, c);
363
364                 /* collect into malloc'd buffers */
365                 /* optional initial space swallow */
366                 if (!ah->frags[ah->frag_index[
367                                       wsi->u.hdr.parser_state]].len && c == ' ')
368                         break;
369
370                 for (m = 0; m < ARRAY_SIZE(methods); m++)
371                         if (wsi->u.hdr.parser_state == methods[m])
372                                 break;
373                 if (m == ARRAY_SIZE(methods))
374                         /* it was not any of the methods */
375                         goto check_eol;
376
377                 /* special URI processing... end at space */
378
379                 if (c == ' ') {
380                         /* enforce starting with / */
381                         if (!ah->frags[ah->nfrag].len)
382                                 if (issue_char(wsi, '/') < 0)
383                                         return -1;
384
385                         /* begin parsing HTTP version: */
386                         if (issue_char(wsi, '\0') < 0)
387                                 return -1;
388                         wsi->u.hdr.parser_state = WSI_TOKEN_HTTP;
389                         goto start_fragment;
390                 }
391
392                 /* special URI processing... convert %xx */
393
394                 switch (wsi->u.hdr.ues) {
395                 case URIES_IDLE:
396                         if (c == '%') {
397                                 wsi->u.hdr.ues = URIES_SEEN_PERCENT;
398                                 goto swallow;
399                         }
400                         break;
401                 case URIES_SEEN_PERCENT:
402                         if (char_to_hex(c) < 0) {
403                                 /* regurgitate */
404                                 if (issue_char(wsi, '%') < 0)
405                                         return -1;
406                                 wsi->u.hdr.ues = URIES_IDLE;
407                                 /* continue on to assess c */
408                                 break;
409                         }
410                         wsi->u.hdr.esc_stash = c;
411                         wsi->u.hdr.ues = URIES_SEEN_PERCENT_H1;
412                         goto swallow;
413
414                 case URIES_SEEN_PERCENT_H1:
415                         if (char_to_hex(c) < 0) {
416                                 /* regurgitate */
417                                 issue_char(wsi, '%');
418                                 wsi->u.hdr.ues = URIES_IDLE;
419                                 /* regurgitate + assess */
420                                 if (lws_parse(wsi, wsi->u.hdr.esc_stash) < 0)
421                                         return -1;
422                                 /* continue on to assess c */
423                                 break;
424                         }
425                         c = (char_to_hex(wsi->u.hdr.esc_stash) << 4) |
426                                         char_to_hex(c);
427                         enc = 1;
428                         wsi->u.hdr.ues = URIES_IDLE;
429                         break;
430                 }
431
432                 /*
433                  * special URI processing...
434                  *  convert /.. or /... or /../ etc to /
435                  *  convert /./ to /
436                  *  convert // or /// etc to /
437                  *  leave /.dir or whatever alone
438                  */
439
440                 switch (wsi->u.hdr.ups) {
441                 case URIPS_IDLE:
442                         /* genuine delimiter */
443                         if ((c == '&' || c == ';') && !enc) {
444                                 issue_char(wsi, c);
445                                 /* swallow the terminator */
446                                 ah->frags[ah->nfrag].len--;
447                                 /* link to next fragment */
448                                 ah->frags[ah->nfrag].nfrag = ah->nfrag + 1;
449                                 ah->nfrag++;
450                                 if (ah->nfrag >= ARRAY_SIZE(ah->frags))
451                                         goto excessive;
452                                 /* start next fragment after the & */
453                                 wsi->u.hdr.post_literal_equal = 0;
454                                 ah->frags[ah->nfrag].offset = ah->pos;
455                                 ah->frags[ah->nfrag].len = 0;
456                                 ah->frags[ah->nfrag].nfrag = 0;
457                                 goto swallow;
458                         }
459                         /* uriencoded = in the name part, disallow */
460                         if (c == '=' && enc && !wsi->u.hdr.post_literal_equal)
461                                 c = '_';
462
463                         /* after the real =, we don't care how many = */
464                         if (c == '=' && !enc)
465                                 wsi->u.hdr.post_literal_equal = 1;
466
467                         /* + to space */
468                         if (c == '+' && !enc)
469                                 c = ' ';
470                         /* issue the first / always */
471                         if (c == '/' && !ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS])
472                                 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
473                         break;
474                 case URIPS_SEEN_SLASH:
475                         /* swallow subsequent slashes */
476                         if (c == '/')
477                                 goto swallow;
478                         /* track and swallow the first . after / */
479                         if (c == '.') {
480                                 wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT;
481                                 goto swallow;
482                         }
483                         wsi->u.hdr.ups = URIPS_IDLE;
484                         break;
485                 case URIPS_SEEN_SLASH_DOT:
486                         /* swallow second . */
487                         if (c == '.') {
488                                 /*
489                                  * back up one dir level if possible
490                                  * safe against header fragmentation because
491                                  * the method URI can only be in 1 fragment
492                                  */
493                                 if (ah->frags[ah->nfrag].len > 2) {
494                                         ah->pos--;
495                                         ah->frags[ah->nfrag].len--;
496                                         do {
497                                                 ah->pos--;
498                                                 ah->frags[ah->nfrag].len--;
499                                         } while (ah->frags[ah->nfrag].len > 1 &&
500                                                  ah->data[ah->pos] != '/');
501                                 }
502                                 wsi->u.hdr.ups = URIPS_SEEN_SLASH_DOT_DOT;
503                                 goto swallow;
504                         }
505                         /* change /./ to / */
506                         if (c == '/') {
507                                 wsi->u.hdr.ups = URIPS_SEEN_SLASH;
508                                 goto swallow;
509                         }
510                         /* it was like /.dir ... regurgitate the . */
511                         wsi->u.hdr.ups = URIPS_IDLE;
512                         if (issue_char(wsi, '.') < 0)
513                                 return -1;
514                         break;
515
516                 case URIPS_SEEN_SLASH_DOT_DOT:
517                         /* swallow prior .. chars and any subsequent . */
518                         if (c == '.')
519                                 goto swallow;
520                         /* last issued was /, so another / == // */
521                         if (c == '/')
522                                 goto swallow;
523                         /* last we issued was / so SEEN_SLASH */
524                         wsi->u.hdr.ups = URIPS_SEEN_SLASH;
525                         break;
526                 }
527
528                 if (c == '?' && !enc &&
529                     !ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS]) { /* start of URI arguments */
530                         /* seal off uri header */
531                         ah->data[ah->pos++] = '\0';
532
533                         /* move to using WSI_TOKEN_HTTP_URI_ARGS */
534                         ah->nfrag++;
535                         if (ah->nfrag >= ARRAY_SIZE(ah->frags))
536                                 goto excessive;
537                         ah->frags[ah->nfrag].offset = ah->pos;
538                         ah->frags[ah->nfrag].len = 0;
539                         ah->frags[ah->nfrag].nfrag = 0;
540
541                         wsi->u.hdr.post_literal_equal = 0;
542                         ah->frag_index[WSI_TOKEN_HTTP_URI_ARGS] = ah->nfrag;
543                         wsi->u.hdr.ups = URIPS_IDLE;
544                         goto swallow;
545                 }
546
547 check_eol:
548
549                 /* bail at EOL */
550                 if (wsi->u.hdr.parser_state != WSI_TOKEN_CHALLENGE &&
551                                                                   c == '\x0d') {
552                         c = '\0';
553                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
554                         lwsl_parser("*\n");
555                 }
556
557                 n = issue_char(wsi, c);
558                 if ((int)n < 0)
559                         return -1;
560                 if (n > 0)
561                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
562
563 swallow:
564                 /* per-protocol end of headers management */
565
566                 if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
567                         goto set_parsing_complete;
568                 break;
569
570                 /* collecting and checking a name part */
571         case WSI_TOKEN_NAME_PART:
572                 lwsl_parser("WSI_TOKEN_NAME_PART '%c' (mode=%d)\n", c, wsi->mode);
573
574                 wsi->u.hdr.lextable_pos =
575                                 lextable_decode(wsi->u.hdr.lextable_pos, c);
576                 /*
577                  * Server needs to look out for unknown methods...
578                  */
579                 if (wsi->u.hdr.lextable_pos < 0 &&
580                     wsi->mode == LWSCM_HTTP_SERVING) {
581                         /* this is not a header we know about */
582                         for (m = 0; m < ARRAY_SIZE(methods); m++)
583                                 if (ah->frag_index[methods[m]]) {
584                                         /*
585                                          * already had the method, no idea what
586                                          * this crap from the client is, ignore
587                                          */
588                                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
589                                         break;
590                                 }
591                         /*
592                          * hm it's an unknown http method from a client in fact,
593                          * treat as dangerous
594                          */
595                         if (m == ARRAY_SIZE(methods)) {
596                                 lwsl_info("Unknown method - dropping\n");
597                                 return -1;
598                         }
599                         break;
600                 }
601                 /*
602                  * ...otherwise for a client, let him ignore unknown headers
603                  * coming from the server
604                  */
605                 if (wsi->u.hdr.lextable_pos < 0) {
606                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
607                         break;
608                 }
609
610                 if (lextable[wsi->u.hdr.lextable_pos] < FAIL_CHAR) {
611                         /* terminal state */
612
613                         n = ((unsigned int)lextable[wsi->u.hdr.lextable_pos] << 8) |
614                                         lextable[wsi->u.hdr.lextable_pos + 1];
615
616                         lwsl_parser("known hdr %d\n", n);
617                         for (m = 0; m < ARRAY_SIZE(methods); m++)
618                                 if (n == methods[m] &&
619                                                 ah->frag_index[
620                                                         methods[m]]) {
621                                         lwsl_warn("Duplicated method\n");
622                                         return -1;
623                                 }
624
625                         /*
626                          * WSORIGIN is protocol equiv to ORIGIN,
627                          * JWebSocket likes to send it, map to ORIGIN
628                          */
629                         if (n == WSI_TOKEN_SWORIGIN)
630                                 n = WSI_TOKEN_ORIGIN;
631
632                         wsi->u.hdr.parser_state = (enum lws_token_indexes)
633                                                         (WSI_TOKEN_GET_URI + n);
634
635                         if (context->token_limits)
636                                 wsi->u.hdr.current_token_limit =
637                                         context->token_limits->token_limit[
638                                                        wsi->u.hdr.parser_state];
639                         else
640                                 wsi->u.hdr.current_token_limit =
641                                         wsi->context->max_http_header_data;
642
643                         if (wsi->u.hdr.parser_state == WSI_TOKEN_CHALLENGE)
644                                 goto set_parsing_complete;
645
646                         goto start_fragment;
647                 }
648                 break;
649
650 start_fragment:
651                 ah->nfrag++;
652 excessive:
653                 if (ah->nfrag == ARRAY_SIZE(ah->frags)) {
654                         lwsl_warn("More hdr frags than we can deal with\n");
655                         return -1;
656                 }
657
658                 ah->frags[ah->nfrag].offset = ah->pos;
659                 ah->frags[ah->nfrag].len = 0;
660                 ah->frags[ ah->nfrag].nfrag = 0;
661
662                 n = ah->frag_index[wsi->u.hdr.parser_state];
663                 if (!n) { /* first fragment */
664                         ah->frag_index[wsi->u.hdr.parser_state] = ah->nfrag;
665                         break;
666                 }
667                 /* continuation */
668                 while (ah->frags[n].nfrag)
669                                 n = ah->frags[n].nfrag;
670                 ah->frags[n].nfrag = ah->nfrag;
671
672                 if (ah->pos == wsi->context->max_http_header_data) {
673                         lwsl_warn("excessive header content\n");
674                         return -1;
675                 }
676
677                 ah->data[ah->pos++] = ' ';
678                 ah->frags[ah->nfrag].len++;
679                 break;
680
681                 /* skipping arg part of a name we didn't recognize */
682         case WSI_TOKEN_SKIPPING:
683                 lwsl_parser("WSI_TOKEN_SKIPPING '%c'\n", c);
684
685                 if (c == '\x0d')
686                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING_SAW_CR;
687                 break;
688
689         case WSI_TOKEN_SKIPPING_SAW_CR:
690                 lwsl_parser("WSI_TOKEN_SKIPPING_SAW_CR '%c'\n", c);
691                 if (c == '\x0a') {
692                         wsi->u.hdr.parser_state = WSI_TOKEN_NAME_PART;
693                         wsi->u.hdr.lextable_pos = 0;
694                 } else
695                         wsi->u.hdr.parser_state = WSI_TOKEN_SKIPPING;
696                 break;
697                 /* we're done, ignore anything else */
698
699         case WSI_PARSING_COMPLETE:
700                 lwsl_parser("WSI_PARSING_COMPLETE '%c'\n", c);
701                 break;
702         }
703
704         return 0;
705
706 set_parsing_complete:
707
708         if (lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE)) {
709                 if (lws_hdr_total_length(wsi, WSI_TOKEN_VERSION))
710                         wsi->ietf_spec_revision =
711                                atoi(lws_hdr_simple_ptr(wsi, WSI_TOKEN_VERSION));
712
713                 lwsl_parser("v%02d hdrs completed\n", wsi->ietf_spec_revision);
714         }
715         wsi->u.hdr.parser_state = WSI_PARSING_COMPLETE;
716         wsi->hdr_parsing_completed = 1;
717
718         return 0;
719 }
720
721
722 /**
723  * lws_frame_is_binary: true if the current frame was sent in binary mode
724  *
725  * @wsi: the connection we are inquiring about
726  *
727  * This is intended to be called from the LWS_CALLBACK_RECEIVE callback if
728  * it's interested to see if the frame it's dealing with was sent in binary
729  * mode.
730  */
731
732 LWS_VISIBLE int lws_frame_is_binary(struct lws *wsi)
733 {
734         return wsi->u.ws.frame_is_binary;
735 }
736
737 int
738 lws_rx_sm(struct lws *wsi, unsigned char c)
739 {
740         struct lws_tokens eff_buf;
741         int ret = 0, n, rx_draining_ext = 0;
742         int callback_action = LWS_CALLBACK_RECEIVE;
743         if (wsi->socket_is_permanently_unusable)
744                 return -1;
745
746         switch (wsi->lws_rx_parse_state) {
747         case LWS_RXPS_NEW:
748                 if (wsi->u.ws.rx_draining_ext) {
749                         struct lws **w = &wsi->context->rx_draining_ext_list;
750
751                         eff_buf.token = NULL;
752                         eff_buf.token_len = 0;
753                         wsi->u.ws.rx_draining_ext = 0;
754                         /* remove us from context draining ext list */
755                         while (*w) {
756                                 if (*w == wsi) {
757                                         *w = wsi->u.ws.rx_draining_ext_list;
758                                         break;
759                                 }
760                                 w = &((*w)->u.ws.rx_draining_ext_list);
761                         }
762                         wsi->u.ws.rx_draining_ext_list = NULL;
763                         rx_draining_ext = 1;
764                         lwsl_err("%s: doing draining flow\n", __func__);
765
766                         goto drain_extension;
767                 }
768                 switch (wsi->ietf_spec_revision) {
769                 case 13:
770                         /*
771                          * no prepended frame key any more
772                          */
773                         wsi->u.ws.all_zero_nonce = 1;
774                         goto handle_first;
775
776                 default:
777                         lwsl_warn("lws_rx_sm: unknown spec version %d\n",
778                                                        wsi->ietf_spec_revision);
779                         break;
780                 }
781                 break;
782         case LWS_RXPS_04_mask_1:
783                 wsi->u.ws.mask[1] = c;
784                 if (c)
785                         wsi->u.ws.all_zero_nonce = 0;
786                 wsi->lws_rx_parse_state = LWS_RXPS_04_mask_2;
787                 break;
788         case LWS_RXPS_04_mask_2:
789                 wsi->u.ws.mask[2] = c;
790                 if (c)
791                         wsi->u.ws.all_zero_nonce = 0;
792                 wsi->lws_rx_parse_state = LWS_RXPS_04_mask_3;
793                 break;
794         case LWS_RXPS_04_mask_3:
795                 wsi->u.ws.mask[3] = c;
796                 if (c)
797                         wsi->u.ws.all_zero_nonce = 0;
798
799                 /*
800                  * start from the zero'th byte in the XOR key buffer since
801                  * this is the start of a frame with a new key
802                  */
803
804                 wsi->u.ws.mask_idx = 0;
805
806                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_1;
807                 break;
808
809         /*
810          *  04 logical framing from the spec (all this is masked when incoming
811          *  and has to be unmasked)
812          *
813          * We ignore the possibility of extension data because we don't
814          * negotiate any extensions at the moment.
815          *
816          *    0                   1                   2                   3
817          *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
818          *   +-+-+-+-+-------+-+-------------+-------------------------------+
819          *   |F|R|R|R| opcode|R| Payload len |    Extended payload length    |
820          *   |I|S|S|S|  (4)  |S|     (7)     |             (16/63)           |
821          *   |N|V|V|V|       |V|             |   (if payload len==126/127)   |
822          *   | |1|2|3|       |4|             |                               |
823          *   +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
824          *   |     Extended payload length continued, if payload len == 127  |
825          *   + - - - - - - - - - - - - - - - +-------------------------------+
826          *   |                               |         Extension data        |
827          *   +-------------------------------+ - - - - - - - - - - - - - - - +
828          *   :                                                               :
829          *   +---------------------------------------------------------------+
830          *   :                       Application data                        :
831          *   +---------------------------------------------------------------+
832          *
833          *  We pass payload through to userland as soon as we get it, ignoring
834          *  FIN.  It's up to userland to buffer it up if it wants to see a
835          *  whole unfragmented block of the original size (which may be up to
836          *  2^63 long!)
837          */
838
839         case LWS_RXPS_04_FRAME_HDR_1:
840 handle_first:
841
842                 wsi->u.ws.opcode = c & 0xf;
843                 wsi->u.ws.rsv = c & 0x70;
844                 wsi->u.ws.final = !!((c >> 7) & 1);
845
846                 switch (wsi->u.ws.opcode) {
847                 case LWSWSOPC_TEXT_FRAME:
848                 case LWSWSOPC_BINARY_FRAME:
849                         wsi->u.ws.rsv_first_msg = (c & 0x70);
850                         wsi->u.ws.frame_is_binary =
851                              wsi->u.ws.opcode == LWSWSOPC_BINARY_FRAME;
852                         break;
853                 case 3:
854                 case 4:
855                 case 5:
856                 case 6:
857                 case 7:
858                 case 0xb:
859                 case 0xc:
860                 case 0xd:
861                 case 0xe:
862                 case 0xf:
863                         lwsl_info("illegal opcode\n");
864                         return -1;
865                 }
866                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN;
867                 break;
868
869         case LWS_RXPS_04_FRAME_HDR_LEN:
870
871                 wsi->u.ws.this_frame_masked = !!(c & 0x80);
872
873                 switch (c & 0x7f) {
874                 case 126:
875                         /* control frames are not allowed to have big lengths */
876                         if (wsi->u.ws.opcode & 8)
877                                 goto illegal_ctl_length;
878
879                         wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_2;
880                         break;
881                 case 127:
882                         /* control frames are not allowed to have big lengths */
883                         if (wsi->u.ws.opcode & 8)
884                                 goto illegal_ctl_length;
885
886                         wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_8;
887                         break;
888                 default:
889                         wsi->u.ws.rx_packet_length = c & 0x7f;
890                         if (wsi->u.ws.this_frame_masked)
891                                 wsi->lws_rx_parse_state =
892                                                 LWS_RXPS_07_COLLECT_FRAME_KEY_1;
893                         else
894                                 if (wsi->u.ws.rx_packet_length)
895                                         wsi->lws_rx_parse_state =
896                                         LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
897                                 else {
898                                         wsi->lws_rx_parse_state = LWS_RXPS_NEW;
899                                         goto spill;
900                                 }
901                         break;
902                 }
903                 break;
904
905         case LWS_RXPS_04_FRAME_HDR_LEN16_2:
906                 wsi->u.ws.rx_packet_length = c << 8;
907                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN16_1;
908                 break;
909
910         case LWS_RXPS_04_FRAME_HDR_LEN16_1:
911                 wsi->u.ws.rx_packet_length |= c;
912                 if (wsi->u.ws.this_frame_masked)
913                         wsi->lws_rx_parse_state =
914                                         LWS_RXPS_07_COLLECT_FRAME_KEY_1;
915                 else
916                         wsi->lws_rx_parse_state =
917                                 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
918                 break;
919
920         case LWS_RXPS_04_FRAME_HDR_LEN64_8:
921                 if (c & 0x80) {
922                         lwsl_warn("b63 of length must be zero\n");
923                         /* kill the connection */
924                         return -1;
925                 }
926 #if defined __LP64__
927                 wsi->u.ws.rx_packet_length = ((size_t)c) << 56;
928 #else
929                 wsi->u.ws.rx_packet_length = 0;
930 #endif
931                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_7;
932                 break;
933
934         case LWS_RXPS_04_FRAME_HDR_LEN64_7:
935 #if defined __LP64__
936                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 48;
937 #endif
938                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_6;
939                 break;
940
941         case LWS_RXPS_04_FRAME_HDR_LEN64_6:
942 #if defined __LP64__
943                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 40;
944 #endif
945                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_5;
946                 break;
947
948         case LWS_RXPS_04_FRAME_HDR_LEN64_5:
949 #if defined __LP64__
950                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 32;
951 #endif
952                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_4;
953                 break;
954
955         case LWS_RXPS_04_FRAME_HDR_LEN64_4:
956                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 24;
957                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_3;
958                 break;
959
960         case LWS_RXPS_04_FRAME_HDR_LEN64_3:
961                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 16;
962                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_2;
963                 break;
964
965         case LWS_RXPS_04_FRAME_HDR_LEN64_2:
966                 wsi->u.ws.rx_packet_length |= ((size_t)c) << 8;
967                 wsi->lws_rx_parse_state = LWS_RXPS_04_FRAME_HDR_LEN64_1;
968                 break;
969
970         case LWS_RXPS_04_FRAME_HDR_LEN64_1:
971                 wsi->u.ws.rx_packet_length |= ((size_t)c);
972                 if (wsi->u.ws.this_frame_masked)
973                         wsi->lws_rx_parse_state =
974                                         LWS_RXPS_07_COLLECT_FRAME_KEY_1;
975                 else
976                         wsi->lws_rx_parse_state =
977                                 LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
978                 break;
979
980         case LWS_RXPS_07_COLLECT_FRAME_KEY_1:
981                 wsi->u.ws.mask[0] = c;
982                 if (c)
983                         wsi->u.ws.all_zero_nonce = 0;
984                 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_2;
985                 break;
986
987         case LWS_RXPS_07_COLLECT_FRAME_KEY_2:
988                 wsi->u.ws.mask[1] = c;
989                 if (c)
990                         wsi->u.ws.all_zero_nonce = 0;
991                 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_3;
992                 break;
993
994         case LWS_RXPS_07_COLLECT_FRAME_KEY_3:
995                 wsi->u.ws.mask[2] = c;
996                 if (c)
997                         wsi->u.ws.all_zero_nonce = 0;
998                 wsi->lws_rx_parse_state = LWS_RXPS_07_COLLECT_FRAME_KEY_4;
999                 break;
1000
1001         case LWS_RXPS_07_COLLECT_FRAME_KEY_4:
1002                 wsi->u.ws.mask[3] = c;
1003                 if (c)
1004                         wsi->u.ws.all_zero_nonce = 0;
1005                 wsi->lws_rx_parse_state =
1006                                         LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED;
1007                 wsi->u.ws.mask_idx = 0;
1008                 if (wsi->u.ws.rx_packet_length == 0) {
1009                         wsi->lws_rx_parse_state = LWS_RXPS_NEW;
1010                         goto spill;
1011                 }
1012                 break;
1013
1014
1015         case LWS_RXPS_PAYLOAD_UNTIL_LENGTH_EXHAUSTED:
1016
1017                 assert(wsi->u.ws.rx_ubuf);
1018
1019                if (wsi->u.ws.rx_ubuf_head + LWS_PRE + 4 >= wsi->u.ws.rx_ubuf_alloc) {
1020                        lwsl_err("Attempted overflow\n");
1021                        return -1;
1022                }
1023                 if (wsi->u.ws.all_zero_nonce)
1024                         wsi->u.ws.rx_ubuf[LWS_PRE +
1025                                (wsi->u.ws.rx_ubuf_head++)] = c;
1026                 else
1027                         wsi->u.ws.rx_ubuf[LWS_PRE +
1028                                (wsi->u.ws.rx_ubuf_head++)] =
1029                                    c ^ wsi->u.ws.mask[
1030                                             (wsi->u.ws.mask_idx++) & 3];
1031
1032                 if (--wsi->u.ws.rx_packet_length == 0) {
1033                         /* spill because we have the whole frame */
1034                         wsi->lws_rx_parse_state = LWS_RXPS_NEW;
1035                         goto spill;
1036                 }
1037
1038                 /*
1039                  * if there's no protocol max frame size given, we are
1040                  * supposed to default to LWS_MAX_SOCKET_IO_BUF
1041                  */
1042
1043                 if (!wsi->protocol->rx_buffer_size &&
1044                                         wsi->u.ws.rx_ubuf_head !=
1045                                                           LWS_MAX_SOCKET_IO_BUF)
1046                         break;
1047                 else
1048                         if (wsi->protocol->rx_buffer_size &&
1049                                         wsi->u.ws.rx_ubuf_head !=
1050                                                   wsi->protocol->rx_buffer_size)
1051                         break;
1052
1053                 /* spill because we filled our rx buffer */
1054 spill:
1055                 /*
1056                  * is this frame a control packet we should take care of at this
1057                  * layer?  If so service it and hide it from the user callback
1058                  */
1059
1060                 lwsl_parser("spill on %s\n", wsi->protocol->name);
1061
1062                 switch (wsi->u.ws.opcode) {
1063                 case LWSWSOPC_CLOSE:
1064
1065                         /* is this an acknowledgement of our close? */
1066                         if (wsi->state == LWSS_AWAITING_CLOSE_ACK) {
1067                                 /*
1068                                  * fine he has told us he is closing too, let's
1069                                  * finish our close
1070                                  */
1071                                 lwsl_parser("seen client close ack\n");
1072                                 return -1;
1073                         }
1074                         if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY)
1075                                 /* if he sends us 2 CLOSE, kill him */
1076                                 return -1;
1077
1078                         if (user_callback_handle_rxflow(
1079                                         wsi->protocol->callback, wsi,
1080                                         LWS_CALLBACK_WS_PEER_INITIATED_CLOSE,
1081                                         wsi->user_space,
1082                                         &wsi->u.ws.rx_ubuf[
1083                                                 LWS_PRE],
1084                                         wsi->u.ws.rx_ubuf_head))
1085                                 return -1;
1086
1087                         lwsl_parser("server sees client close packet\n");
1088                         wsi->state = LWSS_RETURNED_CLOSE_ALREADY;
1089                         /* deal with the close packet contents as a PONG */
1090                         wsi->u.ws.payload_is_close = 1;
1091                         goto process_as_ping;
1092
1093                 case LWSWSOPC_PING:
1094                         lwsl_info("received %d byte ping, sending pong\n",
1095                                                  wsi->u.ws.rx_ubuf_head);
1096
1097                         if (wsi->u.ws.ping_pending_flag) {
1098                                 /*
1099                                  * there is already a pending ping payload
1100                                  * we should just log and drop
1101                                  */
1102                                 lwsl_parser("DROP PING since one pending\n");
1103                                 goto ping_drop;
1104                         }
1105 process_as_ping:
1106                         /* control packets can only be < 128 bytes long */
1107                         if (wsi->u.ws.rx_ubuf_head > 128 - 3) {
1108                                 lwsl_parser("DROP PING payload too large\n");
1109                                 goto ping_drop;
1110                         }
1111
1112                         /* stash the pong payload */
1113                         memcpy(wsi->u.ws.ping_payload_buf + LWS_PRE,
1114                                &wsi->u.ws.rx_ubuf[LWS_PRE],
1115                                 wsi->u.ws.rx_ubuf_head);
1116
1117                         wsi->u.ws.ping_payload_len = wsi->u.ws.rx_ubuf_head;
1118                         wsi->u.ws.ping_pending_flag = 1;
1119
1120                         /* get it sent as soon as possible */
1121                         lws_callback_on_writable(wsi);
1122 ping_drop:
1123                         wsi->u.ws.rx_ubuf_head = 0;
1124                         return 0;
1125
1126                 case LWSWSOPC_PONG:
1127                         lwsl_info("received pong\n");
1128                         lwsl_hexdump(&wsi->u.ws.rx_ubuf[LWS_PRE],
1129                                      wsi->u.ws.rx_ubuf_head);
1130
1131                         /* issue it */
1132                         callback_action = LWS_CALLBACK_RECEIVE_PONG;
1133                         break;
1134
1135                 case LWSWSOPC_TEXT_FRAME:
1136                 case LWSWSOPC_BINARY_FRAME:
1137                 case LWSWSOPC_CONTINUATION:
1138                         break;
1139
1140                 default:
1141                         lwsl_parser("passing opc %x up to exts\n",
1142                                                         wsi->u.ws.opcode);
1143                         /*
1144                          * It's something special we can't understand here.
1145                          * Pass the payload up to the extension's parsing
1146                          * state machine.
1147                          */
1148
1149                         eff_buf.token = &wsi->u.ws.rx_ubuf[LWS_PRE];
1150                         eff_buf.token_len = wsi->u.ws.rx_ubuf_head;
1151
1152                         if (lws_ext_cb_active(wsi, LWS_EXT_CB_EXTENDED_PAYLOAD_RX,
1153                                         &eff_buf, 0) <= 0) /* not handle or fail */
1154                                 lwsl_ext("ext opc opcode 0x%x unknown\n",
1155                                                               wsi->u.ws.opcode);
1156
1157                         wsi->u.ws.rx_ubuf_head = 0;
1158                         return 0;
1159                 }
1160
1161                 /*
1162                  * No it's real payload, pass it up to the user callback.
1163                  * It's nicely buffered with the pre-padding taken care of
1164                  * so it can be sent straight out again using lws_write
1165                  */
1166
1167                 eff_buf.token = &wsi->u.ws.rx_ubuf[LWS_PRE];
1168                 eff_buf.token_len = wsi->u.ws.rx_ubuf_head;
1169
1170 drain_extension:
1171                 lwsl_ext("%s: passing %d to ext\n", __func__, eff_buf.token_len);
1172
1173                 if (wsi->state == LWSS_RETURNED_CLOSE_ALREADY ||
1174                     wsi->state == LWSS_AWAITING_CLOSE_ACK)
1175                         goto already_done;
1176
1177                 n = lws_ext_cb_active(wsi, LWS_EXT_CB_PAYLOAD_RX, &eff_buf, 0);
1178                 if (n < 0) {
1179                         /*
1180                          * we may rely on this to get RX, just drop connection
1181                          */
1182                         wsi->socket_is_permanently_unusable = 1;
1183                         return -1;
1184                 }
1185
1186                 if (rx_draining_ext && eff_buf.token_len == 0)
1187                         goto already_done;
1188
1189                 if (n && eff_buf.token_len) {
1190                         /* extension had more... main loop will come back */
1191                         wsi->u.ws.rx_draining_ext = 1;
1192                         wsi->u.ws.rx_draining_ext_list = wsi->context->rx_draining_ext_list;
1193                         wsi->context->rx_draining_ext_list = wsi;
1194                 }
1195
1196                 if (eff_buf.token_len > 0 ||
1197                     callback_action == LWS_CALLBACK_RECEIVE_PONG) {
1198                         eff_buf.token[eff_buf.token_len] = '\0';
1199
1200                         if (wsi->protocol->callback) {
1201
1202                                 if (callback_action == LWS_CALLBACK_RECEIVE_PONG)
1203                                         lwsl_info("Doing pong callback\n");
1204
1205                                 ret = user_callback_handle_rxflow(
1206                                                 wsi->protocol->callback,
1207                                                 wsi,
1208                                                 (enum lws_callback_reasons)callback_action,
1209                                                 wsi->user_space,
1210                                                 eff_buf.token,
1211                                                 eff_buf.token_len);
1212                         }
1213                         else
1214                                 lwsl_err("No callback on payload spill!\n");
1215                 }
1216
1217 already_done:
1218                 wsi->u.ws.rx_ubuf_head = 0;
1219                 break;
1220         }
1221
1222         return ret;
1223
1224 illegal_ctl_length:
1225
1226         lwsl_warn("Control frame with xtended length is illegal\n");
1227         /* kill the connection */
1228         return -1;
1229 }
1230
1231
1232 /**
1233  * lws_remaining_packet_payload() - Bytes to come before "overall"
1234  *                                            rx packet is complete
1235  * @wsi:                Websocket instance (available from user callback)
1236  *
1237  *      This function is intended to be called from the callback if the
1238  *  user code is interested in "complete packets" from the client.
1239  *  libwebsockets just passes through payload as it comes and issues a buffer
1240  *  additionally when it hits a built-in limit.  The LWS_CALLBACK_RECEIVE
1241  *  callback handler can use this API to find out if the buffer it has just
1242  *  been given is the last piece of a "complete packet" from the client --
1243  *  when that is the case lws_remaining_packet_payload() will return
1244  *  0.
1245  *
1246  *  Many protocols won't care becuse their packets are always small.
1247  */
1248
1249 LWS_VISIBLE size_t
1250 lws_remaining_packet_payload(struct lws *wsi)
1251 {
1252         return wsi->u.ws.rx_packet_length;
1253 }