Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / xl2tpd / avp.c
blob5b0980f3ebe30b0da010cb892e7a03181ef9a77c
1 /*
2 * Layer Two Tunnelling Protocol Daemon
3 * Copyright (C) 1998 Adtran, Inc.
4 * Copyright (C) 2002 Jeff McAdams
6 * Mark Spencer
8 * This software is distributed under the terms
9 * of the GPL, which you should have received
10 * along with this source.
12 * Attribute Value Pair handler routines
14 #include <stdlib.h>
15 #include <string.h>
16 #include <stdio.h>
17 #include <errno.h>
18 #include <netinet/in.h>
19 #include "l2tp.h"
21 #define AVP_MAX 39
23 struct avp avps[] = {
25 {0, 1, &message_type_avp, "Message Type"},
26 {1, 1, &result_code_avp, "Result Code"},
27 {2, 1, &protocol_version_avp, "Protocol Version"},
28 {3, 1, &framing_caps_avp, "Framing Capabilities"},
29 {4, 1, &bearer_caps_avp, "Bearer Capabilities"},
30 {5, 0, NULL, "Tie Breaker"},
31 {6, 0, &firmware_rev_avp, "Firmware Revision"},
32 {7, 0, &hostname_avp, "Host Name"},
33 {8, 1, &vendor_avp, "Vendor Name"},
34 {9, 1, &assigned_tunnel_avp, "Assigned Tunnel ID"},
35 {10, 1, &receive_window_size_avp, "Receive Window Size"},
36 {11, 1, &challenge_avp, "Challenge"},
37 {12, 0, NULL, "Q.931 Cause Code"},
38 {13, 1, &chalresp_avp, "Challenge Response"},
39 {14, 1, &assigned_call_avp, "Assigned Call ID"},
40 {15, 1, &call_serno_avp, "Call Serial Number"},
41 {16, 1, NULL, "Minimum BPS"},
42 {17, 1, NULL, "Maximum BPS"},
43 {18, 1, &bearer_type_avp, "Bearer Type"},
44 {19, 1, &frame_type_avp, "Framing Type"},
45 {20, 1, &packet_delay_avp, "Packet Processing Delay"},
46 {21, 1, &dialed_number_avp, "Dialed Number"},
47 {22, 1, &dialing_number_avp, "Dialing Number"},
48 {23, 1, &sub_address_avp, "Sub-Address"},
49 {24, 1, &tx_speed_avp, "Transmit Connect Speed"},
50 {25, 1, &call_physchan_avp, "Physical channel ID"},
51 {26, 0, NULL, "Initial Received LCP Confreq"},
52 {27, 0, NULL, "Last Sent LCP Confreq"},
53 {28, 0, NULL, "Last Received LCP Confreq"},
54 {29, 1, &ignore_avp, "Proxy Authen Type"},
55 {30, 0, &ignore_avp, "Proxy Authen Name"},
56 {31, 0, &ignore_avp, "Proxy Authen Challenge"},
57 {32, 0, &ignore_avp, "Proxy Authen ID"},
58 {33, 1, &ignore_avp, "Proxy Authen Response"},
59 {34, 1, NULL, "Call Errors"},
60 {35, 1, &ignore_avp, "ACCM"},
61 {36, 1, &rand_vector_avp, "Random Vector"},
62 {37, 1, NULL, "Private Group ID"},
63 {38, 0, &rx_speed_avp, "Receive Connect Speed"},
64 {39, 1, &seq_reqd_avp, "Sequencing Required"}
67 char *msgtypes[] = {
68 NULL,
69 "Start-Control-Connection-Request",
70 "Start-Control-Connection-Reply",
71 "Start-Control-Connection-Connected",
72 "Stop-Control-Connection-Notification",
73 NULL,
74 "Hello",
75 "Outgoing-Call-Request",
76 "Outgoing-Call-Reply",
77 "Outgoing-Call-Connected",
78 "Incoming-Call-Request",
79 "Incoming-Call-Reply",
80 "Incoming-Call-Connected",
81 NULL,
82 "Call-Disconnect-Notify",
83 "WAN-Error-Notify",
84 "Set-Link-Info"
87 char *stopccn_result_codes[] = {
88 "Reserved",
89 "General request to clear control connection",
90 "General error--Error Code indicates the problem",
91 "Control channel already exists",
92 "Requester is not authorized to establish a control channel",
93 "The protocol version of the requester is not supported--Error Code indicates the highest version supported",
94 "Requester is being shut down",
95 "Finite State Machine error"
98 char *cdn_result_codes[] = {
99 "Reserved",
100 "Call disconnected due to loss of carrier",
101 "Call disconnected for the reason indicated in error code",
102 "Call disconnected for administrative reasons",
103 "Call failed due to lack of appropriate facilities being available (temporary condition)",
104 "Call failed due to lack of appropriate facilities being available (permanent condition)",
105 "Invalid destination",
106 "Call failed due to no carrier detected",
107 "Call failed due to lack of a dial tone",
108 "Call was no established within time allotted by LAC",
109 "Call was connected but no appropriate framing was detect"
112 void wrong_length (struct call *c, char *field, int expected, int found,
113 int min)
115 if (min)
116 snprintf (c->errormsg, sizeof (c->errormsg),
117 "%s: expected at least %d, got %d", field, expected, found);
118 else
119 snprintf (c->errormsg, sizeof (c->errormsg),
120 "%s: expected %d, got %d", field, expected, found);
122 c->error = ERROR_LENGTH;
123 c->result = RESULT_ERROR;
124 c->needclose = -1;
127 struct unaligned_u16 {
128 _u16 s;
129 } __attribute__((packed));
132 * t, c, data, and datalen may be assumed to be defined for all avp's
135 int message_type_avp (struct tunnel *t, struct call *c, void *data,
136 int datalen)
139 * This will be with every control message. It is critical that this
140 * procedure check for the validity of sending this kind of a message
141 * (assuming sanity check)
144 struct unaligned_u16 *raw = data;
145 c->msgtype = ntohs (raw[3].s);
146 if (datalen != 8)
148 if (DEBUG)
149 l2tp_log (LOG_DEBUG, "%s: wrong size (%d != 8)\n", __FUNCTION__,
150 datalen);
151 wrong_length (c, "Message Type", 8, datalen, 0);
152 return -EINVAL;
154 if ((c->msgtype > MAX_MSG) || (!msgtypes[c->msgtype]))
156 if (DEBUG)
157 l2tp_log (LOG_DEBUG, "%s: unknown message type %d\n", __FUNCTION__,
158 c->msgtype);
159 return -EINVAL;
161 if (gconfig.debug_avp)
162 if (DEBUG)
163 l2tp_log (LOG_DEBUG, "%s: message type %d (%s)\n", __FUNCTION__,
164 c->msgtype, msgtypes[c->msgtype]);
165 #ifdef SANITY
166 if (t->sanity)
169 * Look ou our state for each message and make sure everything
170 * make sense...
172 if ((c != t->self) && (c->msgtype < Hello))
174 if (DEBUG)
175 l2tp_log (LOG_DEBUG,
176 "%s: attempting to negotiate tunnel inside a call!\n",
177 __FUNCTION__);
178 return -EINVAL;
181 switch (c->msgtype)
183 case SCCRQ:
184 if ((t->state != 0) && (t->state != SCCRQ))
187 * When we handle tie breaker AVP's, then we'll check
188 * to see if we've both requested tunnels
191 if (DEBUG)
192 l2tp_log (LOG_DEBUG,
193 "%s: attempting to negotiate SCCRQ with state != 0\n",
194 __FUNCTION__);
195 return -EINVAL;
197 break;
198 case SCCRP:
199 if (t->state != SCCRQ)
201 if (DEBUG)
202 l2tp_log (LOG_DEBUG,
203 "%s: attempting to negotiate SCCRP with state != SCCRQ!\n",
204 __FUNCTION__);
205 return -EINVAL;
207 break;
208 case SCCCN:
209 if (t->state != SCCRP)
211 if (DEBUG)
212 l2tp_log (LOG_DEBUG,
213 "%s: attempting to negotiate SCCCN with state != SCCRP!\n",
214 __FUNCTION__);
215 return -EINVAL;
217 break;
218 case ICRQ:
219 if (t->state != SCCCN)
221 if (DEBUG)
222 l2tp_log (LOG_DEBUG,
223 "%s: attempting to negotiate ICRQ when state != SCCCN\n",
224 __FUNCTION__);
225 return -EINVAL;
227 if (c != t->self)
229 if (DEBUG)
230 l2tp_log (LOG_DEBUG,
231 "%s: attempting to negotiate ICRQ on a call!\n",
232 __FUNCTION__);
233 return -EINVAL;
235 break;
236 case ICRP:
237 if (t->state != SCCCN)
239 if (DEBUG)
240 l2tp_log (LOG_DEBUG,
241 "%s: attempting to negotiate ICRP on tunnel!=SCCCN\n",
242 __FUNCTION__);
243 return -EINVAL;
245 if (c->state != ICRQ)
247 if (DEBUG)
248 l2tp_log (LOG_DEBUG,
249 "%s: attempting to negotiate ICRP when state != ICRQ\n",
250 __FUNCTION__);
251 return -EINVAL;
253 break;
254 case ICCN:
255 if (c->state != ICRP)
257 if (DEBUG)
258 l2tp_log (LOG_DEBUG,
259 "%s: attempting to negotiate ICCN when state != ICRP\n",
260 __FUNCTION__);
261 return -EINVAL;
263 break;
264 case SLI:
265 if (c->state != ICCN)
267 if (DEBUG)
268 l2tp_log (LOG_DEBUG,
269 "%s: attempting to negotiate SLI when state != ICCN\n",
270 __FUNCTION__);
271 return -EINVAL;
273 break;
274 case OCRP: /* jz: case for ORCP */
275 if (t->state != SCCCN)
277 if (DEBUG)
278 l2tp_log (LOG_DEBUG,
279 "%s: attempting to negotiate OCRP on tunnel!=SCCCN\n",
280 __FUNCTION__);
281 return -EINVAL;
283 if (c->state != OCRQ)
285 if (DEBUG)
286 l2tp_log (LOG_DEBUG,
287 "%s: attempting to negotiate OCRP when state != OCRQ\n",
288 __FUNCTION__);
289 return -EINVAL;
291 break;
292 case OCCN: /* jz: case for OCCN */
294 if (c->state != OCRQ)
296 if (DEBUG)
297 l2tp_log (LOG_DEBUG,
298 "%s: attempting to negotiate OCCN when state != OCRQ\n",
299 __FUNCTION__);
300 return -EINVAL;
302 break;
303 case StopCCN:
304 case CDN:
305 case Hello:
306 break;
307 default:
308 l2tp_log (LOG_WARNING, "%s: i don't know how to handle %s messages\n",
309 __FUNCTION__, msgtypes[c->msgtype]);
310 return -EINVAL;
313 #endif
314 if (c->msgtype == ICRQ)
316 struct call *tmp;
317 if (gconfig.debug_avp)
319 if (DEBUG)
320 l2tp_log (LOG_DEBUG, "%s: new incoming call\n", __FUNCTION__);
322 tmp = new_call (t);
323 if (!tmp)
325 l2tp_log (LOG_WARNING, "%s: unable to create new call\n", __FUNCTION__);
326 return -EINVAL;
328 tmp->next = t->call_head;
329 t->call_head = tmp;
330 t->count++;
332 * Is this still safe to assume that the head will always
333 * be the most recent call being negotiated?
334 * Probably... FIXME anyway...
338 return 0;
341 int rand_vector_avp (struct tunnel *t, struct call *c, void *data,
342 int datalen)
344 int size;
345 struct unaligned_u16 *raw = data;
346 size = raw[0].s & 0x03FF;
347 size -= sizeof (struct avp_hdr);
348 #ifdef SANITY
349 if (t->sanity)
351 if (size < 0)
353 if (DEBUG)
354 l2tp_log (LOG_DEBUG, "%s: Random vector too small (%d < 0)\n",
355 __FUNCTION__, size);
356 wrong_length (c, "Random Vector", 6, datalen, 1);
357 return -EINVAL;
359 if (size > MAX_VECTOR_SIZE)
361 if (DEBUG)
362 l2tp_log (LOG_DEBUG, "%s: Random vector too large (%d > %d)\n",
363 __FUNCTION__, datalen, MAX_VECTOR_SIZE);
364 wrong_length (c, "Random Vector", 6, datalen, 1);
365 return -EINVAL;
368 #endif
369 if (gconfig.debug_avp)
370 l2tp_log (LOG_DEBUG, "%s: Random Vector of %d octets\n", __FUNCTION__,
371 size);
372 t->chal_us.vector = (unsigned char *) &raw[3].s;
373 t->chal_us.vector_len = size;
374 return 0;
377 int ignore_avp (struct tunnel *t, struct call *c, void *data, int datalen)
380 * The spec says we have to accept authentication information
381 * even if we just ignore it, so that's exactly what
382 * we're going to do at this point. Proxy authentication is such
383 * a rediculous security threat anyway except from local
384 * controled machines.
386 * FIXME: I need to handle proxy authentication as an option.
387 * One option is to simply change the options we pass to pppd.
390 if (gconfig.debug_avp)
392 if (DEBUG)
393 l2tp_log (LOG_DEBUG, "%s : Ignoring AVP\n", __FUNCTION__);
395 return 0;
398 int seq_reqd_avp (struct tunnel *t, struct call *c, void *data, int datalen)
400 #ifdef SANITY
401 if (t->sanity)
403 if (datalen != 6)
405 if (DEBUG)
406 l2tp_log (LOG_DEBUG,
407 "%s: avp is incorrect size. %d != 6\n", __FUNCTION__,
408 datalen);
409 wrong_length (c, "Sequencing Required", 6, datalen, 1);
410 return -EINVAL;
412 switch (c->msgtype)
414 case ICCN:
415 break;
416 default:
417 if (DEBUG)
418 l2tp_log (LOG_DEBUG,
419 "%s: sequencing required not appropriate for %s!\n",
420 __FUNCTION__, msgtypes[c->msgtype]);
421 return -EINVAL;
424 #endif
425 if (gconfig.debug_avp)
427 if (DEBUG)
428 l2tp_log (LOG_DEBUG, "%s: peer requires sequencing.\n", __FUNCTION__);
430 c->seq_reqd = -1;
431 return 0;
434 int result_code_avp (struct tunnel *t, struct call *c, void *data,
435 int datalen)
438 * Find out what version of l2tp the other side is using.
439 * I'm not sure what we're supposed to do with this but whatever..
442 int error;
443 int result;
444 struct unaligned_u16 *raw = data;
445 #ifdef SANITY
446 if (t->sanity)
448 if (datalen < 10)
450 if (DEBUG)
451 l2tp_log (LOG_DEBUG,
452 "%s: avp is incorrect size. %d < 10\n", __FUNCTION__,
453 datalen);
454 wrong_length (c, "Result Code", 10, datalen, 1);
455 return -EINVAL;
457 switch (c->msgtype)
459 case CDN:
460 case StopCCN:
461 break;
462 default:
463 if (DEBUG)
464 l2tp_log (LOG_DEBUG,
465 "%s: result code not appropriate for %s. Ignoring.\n",
466 __FUNCTION__, msgtypes[c->msgtype]);
467 return 0;
470 #endif
471 result = ntohs (raw[3].s);
472 error = ntohs (raw[4].s);
475 * from prepare_StopCCN and prepare_CDN, note missing htons() call
476 * http://www.opensource.apple.com/source/ppp/ppp-412.3/Drivers/L2TP/L2TP-plugin/l2tp.c
478 if (((result & 0xFF) == 0) && (result >> 8 != 0))
480 if (DEBUG)
481 l2tp_log (LOG_DEBUG,
482 "%s: result code endianness fix for buggy Apple client. network=%d, le=%d\n",
483 __FUNCTION__, result, result >> 8);
484 result >>= 8;
487 if (((error & 0xFF) == 0) && (error >> 8 != 0))
489 if (DEBUG)
490 l2tp_log (LOG_DEBUG,
491 "%s: error code endianness fix for buggy Apple client. network=%d, le=%d\n",
492 __FUNCTION__, error, error >> 8);
493 error >>= 8;
496 if ((c->msgtype == StopCCN) && ((result > 7) || (result < 1)))
498 if (DEBUG)
499 l2tp_log (LOG_DEBUG,
500 "%s: result code out of range (%d %d %d). Ignoring.\n",
501 __FUNCTION__, result, error, datalen);
502 return 0;
505 if ((c->msgtype == CDN) && ((result > 11) || (result < 1)))
507 if (DEBUG)
508 l2tp_log (LOG_DEBUG,
509 "%s: result code out of range (%d %d %d). Ignoring.\n",
510 __FUNCTION__, result, error, datalen);
511 return 0;
514 c->error = error;
515 c->result = result;
516 safe_copy (c->errormsg, (char *) &raw[5].s, datalen - 10);
517 if (gconfig.debug_avp)
519 if (DEBUG && (c->msgtype == StopCCN))
521 l2tp_log (LOG_DEBUG,
522 "%s: peer closing for reason %d (%s), error = %d (%s)\n",
523 __FUNCTION__, result, stopccn_result_codes[result], error,
524 c->errormsg);
526 else
528 l2tp_log (LOG_DEBUG,
529 "%s: peer closing for reason %d (%s), error = %d (%s)\n",
530 __FUNCTION__, result, cdn_result_codes[result], error,
531 c->errormsg);
534 return 0;
537 int protocol_version_avp (struct tunnel *t, struct call *c, void *data,
538 int datalen)
541 * Find out what version of l2tp the other side is using.
542 * I'm not sure what we're supposed to do with this but whatever..
545 int ver;
546 struct unaligned_u16 *raw = data;
547 #ifdef SANITY
548 if (t->sanity)
550 if (datalen != 8)
552 if (DEBUG)
553 l2tp_log (LOG_DEBUG,
554 "%s: avp is incorrect size. %d != 8\n", __FUNCTION__,
555 datalen);
556 wrong_length (c, "Protocol Version", 8, datalen, 1);
557 return -EINVAL;
559 switch (c->msgtype)
561 case SCCRP:
562 case SCCRQ:
563 break;
564 default:
565 if (DEBUG)
566 l2tp_log (LOG_DEBUG,
567 "%s: protocol version not appropriate for %s. Ignoring.\n",
568 __FUNCTION__, msgtypes[c->msgtype]);
569 return 0;
572 #endif
573 ver = ntohs (raw[3].s);
574 if (gconfig.debug_avp)
576 if (DEBUG)
577 l2tp_log (LOG_DEBUG,
578 "%s: peer is using version %d, revision %d.\n", __FUNCTION__,
579 (ver >> 8), ver & 0xFF);
581 return 0;
584 int framing_caps_avp (struct tunnel *t, struct call *c, void *data,
585 int datalen)
588 * Retrieve the framing capabilities
589 * from the peer
592 int caps;
593 struct unaligned_u16 *raw = data;
595 #ifdef SANITY
596 if (t->sanity)
598 switch (c->msgtype)
600 case SCCRP:
601 case SCCRQ:
602 break;
603 default:
604 if (DEBUG)
605 l2tp_log (LOG_DEBUG,
606 "%s: framing capabilities not appropriate for %s. Ignoring.\n",
607 __FUNCTION__, msgtypes[c->msgtype]);
608 return 0;
610 if (datalen != 10)
612 if (DEBUG)
613 l2tp_log (LOG_DEBUG,
614 "%s: avp is incorrect size. %d != 10\n", __FUNCTION__,
615 datalen);
616 wrong_length (c, "Framming Capabilities", 10, datalen, 0);
617 return -EINVAL;
620 #endif
621 caps = ntohs (raw[4].s);
622 if (gconfig.debug_avp)
623 if (DEBUG)
624 l2tp_log (LOG_DEBUG,
625 "%s: supported peer frames:%s%s\n", __FUNCTION__,
626 caps & ASYNC_FRAMING ? " async" : "",
627 caps & SYNC_FRAMING ? " sync" : "");
628 t->fc = caps & (ASYNC_FRAMING | SYNC_FRAMING);
629 return 0;
632 int bearer_caps_avp (struct tunnel *t, struct call *c, void *data,
633 int datalen)
636 * What kind of bearer channels does our peer support?
638 int caps;
639 struct unaligned_u16 *raw = data;
641 #ifdef SANITY
642 if (t->sanity)
644 switch (c->msgtype)
646 case SCCRP:
647 case SCCRQ:
648 break;
649 default:
650 if (DEBUG)
651 l2tp_log (LOG_DEBUG,
652 "%s: bearer capabilities not appropriate for message %s. Ignoring.\n",
653 __FUNCTION__, msgtypes[c->msgtype]);
654 return 0;
656 if (datalen != 10)
658 if (DEBUG)
659 l2tp_log (LOG_DEBUG,
660 "%s: avp is incorrect size. %d != 10\n", __FUNCTION__,
661 datalen);
662 wrong_length (c, "Bearer Capabilities", 10, datalen, 0);
663 return -EINVAL;
666 #endif
667 caps = ntohs (raw[4].s);
668 if (gconfig.debug_avp)
670 if (DEBUG)
672 l2tp_log (LOG_DEBUG,
673 "%s: supported peer bearers:%s%s\n",
674 __FUNCTION__,
675 caps & ANALOG_BEARER ? " analog" : "",
676 caps & DIGITAL_BEARER ? " digital" : "");
680 t->bc = caps & (ANALOG_BEARER | DIGITAL_BEARER);
681 return 0;
685 /* FIXME: I need to handle tie breakers eventually */
687 int firmware_rev_avp (struct tunnel *t, struct call *c, void *data,
688 int datalen)
691 * Report and record remote firmware version
693 int ver;
694 struct unaligned_u16 *raw = data;
696 #ifdef SANITY
697 if (t->sanity)
699 switch (c->msgtype)
701 case SCCRP:
702 case SCCRQ:
703 break;
704 default:
705 if (DEBUG)
706 l2tp_log (LOG_DEBUG,
707 "%s: firmware revision not appropriate for message %s. Ignoring.\n",
708 __FUNCTION__, msgtypes[c->msgtype]);
709 return 0;
711 if (datalen != 8)
713 if (DEBUG)
714 l2tp_log (LOG_DEBUG,
715 "%s: avp is incorrect size. %d != 8\n", __FUNCTION__,
716 datalen);
717 wrong_length (c, "Firmware Revision", 8, datalen, 0);
718 return -EINVAL;
721 #endif
722 ver = ntohs (raw[3].s);
723 if (gconfig.debug_avp)
725 if (DEBUG)
726 l2tp_log (LOG_DEBUG,
727 "%s: peer reports firmware version %d (0x%.4x)\n",
728 __FUNCTION__, ver, ver);
730 t->firmware = ver;
731 return 0;
734 int bearer_type_avp (struct tunnel *t, struct call *c, void *data,
735 int datalen)
738 * What kind of bearer channel is the call on?
740 int b;
741 struct unaligned_u16 *raw = data;
743 #ifdef SANITY
744 if (t->sanity)
746 switch (c->msgtype)
748 case ICRQ:
749 case OCRQ:
750 break;
751 default:
752 if (DEBUG)
753 l2tp_log (LOG_DEBUG,
754 "%s: bearer type not appropriate for message %s. Ignoring.\n",
755 __FUNCTION__, msgtypes[c->msgtype]);
756 return 0;
758 if (datalen != 10)
760 if (DEBUG)
761 l2tp_log (LOG_DEBUG,
762 "%s: avp is incorrect size. %d != 10\n", __FUNCTION__,
763 datalen);
764 wrong_length (c, "Bearer Type", 10, datalen, 0);
765 return -EINVAL;
768 #endif
769 b = ntohs (raw[4].s);
770 if (gconfig.debug_avp)
772 if (DEBUG)
773 l2tp_log (LOG_DEBUG,
774 "%s: peer bears:%s\n", __FUNCTION__,
775 b & ANALOG_BEARER ? " analog" : "digital");
777 t->call_head->bearer = b;
778 return 0;
781 int frame_type_avp (struct tunnel *t, struct call *c, void *data, int datalen)
784 * What kind of frame channel is the call on?
786 int b;
787 struct unaligned_u16 *raw = data;
789 #ifdef SANITY
790 if (t->sanity)
792 switch (c->msgtype)
794 case ICCN:
795 case OCRQ:
796 case OCCN:
797 break;
798 default:
799 if (DEBUG)
800 l2tp_log (LOG_DEBUG,
801 "%s: frame type not appropriate for message %s. Ignoring.\n",
802 __FUNCTION__, msgtypes[c->msgtype]);
803 return 0;
805 if (datalen != 10)
807 if (DEBUG)
808 l2tp_log (LOG_DEBUG,
809 "%s: avp is incorrect size. %d != 10\n", __FUNCTION__,
810 datalen);
811 wrong_length (c, "Frame Type", 10, datalen, 0);
812 return -EINVAL;
815 #endif
816 b = ntohs (raw[4].s);
817 if (gconfig.debug_avp)
819 if (DEBUG)
820 l2tp_log (LOG_DEBUG,
821 "%s: peer uses:%s frames\n", __FUNCTION__,
822 b & ASYNC_FRAMING ? " async" : "sync");
824 c->frame = b;
825 return 0;
828 int hostname_avp (struct tunnel *t, struct call *c, void *data, int datalen)
831 * What is the peer's name?
833 int size;
834 struct unaligned_u16 *raw = data;
836 #ifdef SANITY
837 if (t->sanity)
839 switch (c->msgtype)
841 case SCCRP:
842 case SCCRQ:
843 break;
844 default:
845 if (DEBUG)
846 l2tp_log (LOG_DEBUG,
847 "%s: hostname not appropriate for message %s. Ignoring.\n",
848 __FUNCTION__, msgtypes[c->msgtype]);
849 return 0;
851 if (datalen < 6)
853 if (DEBUG)
854 l2tp_log (LOG_DEBUG,
855 "%s: avp is too small. %d < 6\n", __FUNCTION__,
856 datalen);
857 wrong_length (c, "Hostname", 6, datalen, 1);
858 return -EINVAL;
861 #endif
862 size = raw[0].s & 0x03FF;
863 size -= sizeof (struct avp_hdr);
864 if (size > MAXSTRLEN - 1)
866 if (DEBUG)
867 l2tp_log (LOG_DEBUG, "%s: truncating reported hostname (size is %d)\n",
868 __FUNCTION__, size);
869 size = MAXSTRLEN - 1;
871 safe_copy (t->hostname, (char *) &raw[3].s, size);
872 if (gconfig.debug_avp)
874 if (DEBUG)
875 l2tp_log (LOG_DEBUG,
876 "%s: peer reports hostname '%s'\n", __FUNCTION__,
877 t->hostname);
879 return 0;
882 int dialing_number_avp (struct tunnel *t, struct call *c, void *data,
883 int datalen)
886 * What is the peer's name?
888 int size;
889 struct unaligned_u16 *raw = data;
891 #ifdef SANITY
892 if (t->sanity)
894 switch (c->msgtype)
896 case ICRQ:
897 break;
898 default:
899 if (DEBUG)
900 l2tp_log (LOG_DEBUG,
901 "%s: dialing number not appropriate for message %s. Ignoring.\n",
902 __FUNCTION__, msgtypes[c->msgtype]);
903 return 0;
905 if (datalen < 6)
907 if (DEBUG)
908 l2tp_log (LOG_DEBUG,
909 "%s: avp is too small. %d < 6\n", __FUNCTION__,
910 datalen);
911 wrong_length (c, "Dialing Number", 6, datalen, 1);
912 return -EINVAL;
915 #endif
916 size = raw[0].s & 0x03FF;
917 size -= sizeof (struct avp_hdr);
918 if (size > MAXSTRLEN - 1)
920 if (DEBUG)
921 l2tp_log (LOG_DEBUG,
922 "%s: truncating reported dialing number (size is %d)\n",
923 __FUNCTION__, size);
924 size = MAXSTRLEN - 1;
926 safe_copy (t->call_head->dialing, (char *) &raw[3].s, size);
927 if (gconfig.debug_avp)
929 if (DEBUG)
930 l2tp_log (LOG_DEBUG,
931 "%s: peer reports dialing number '%s'\n", __FUNCTION__,
932 t->call_head->dialing);
934 return 0;
937 int dialed_number_avp (struct tunnel *t, struct call *c, void *data,
938 int datalen)
941 * What is the peer's name?
943 int size;
944 struct unaligned_u16 *raw = data;
946 #ifdef SANITY
947 if (t->sanity)
949 switch (c->msgtype)
951 case OCRQ:
952 case ICRQ:
953 break;
954 default:
955 if (DEBUG)
956 l2tp_log (LOG_DEBUG,
957 "%s: dialed number not appropriate for message %s. Ignoring.\n",
958 __FUNCTION__, msgtypes[c->msgtype]);
959 return 0;
961 if (datalen < 6)
963 if (DEBUG)
964 l2tp_log (LOG_DEBUG,
965 "%s: avp is too small. %d < 6\n", __FUNCTION__,
966 datalen);
967 wrong_length (c, "Dialed Number", 6, datalen, 1);
968 return -EINVAL;
971 #endif
972 size = raw[0].s & 0x03FF;
973 size -= sizeof (struct avp_hdr);
974 if (size > MAXSTRLEN - 1)
976 if (DEBUG)
977 l2tp_log (LOG_DEBUG,
978 "%s: truncating reported dialed number (size is %d)\n",
979 __FUNCTION__, size);
980 size = MAXSTRLEN - 1;
982 safe_copy (t->call_head->dialed, (char *) &raw[3].s, size);
983 if (gconfig.debug_avp)
985 if (DEBUG)
986 l2tp_log (LOG_DEBUG,
987 "%s: peer reports dialed number '%s'\n", __FUNCTION__,
988 t->call_head->dialed);
990 return 0;
993 int sub_address_avp (struct tunnel *t, struct call *c, void *data,
994 int datalen)
997 * What is the peer's name?
999 int size;
1000 struct unaligned_u16 *raw = data;
1002 #ifdef SANITY
1003 if (t->sanity)
1005 switch (c->msgtype)
1007 case OCRP:
1008 case ICRQ:
1009 break;
1010 default:
1011 if (DEBUG)
1012 l2tp_log (LOG_DEBUG,
1013 "%s: sub_address not appropriate for message %s. Ignoring.\n",
1014 __FUNCTION__, msgtypes[c->msgtype]);
1015 return 0;
1017 if (datalen < 6)
1019 if (DEBUG)
1020 l2tp_log (LOG_DEBUG,
1021 "%s: avp is too small. %d < 6\n", __FUNCTION__,
1022 datalen);
1023 wrong_length (c, "Sub-address", 6, datalen, 1);
1024 return -EINVAL;
1027 #endif
1028 size = raw[0].s & 0x03FF;
1029 size -= sizeof (struct avp_hdr);
1030 if (size > MAXSTRLEN - 1)
1032 if (DEBUG)
1033 l2tp_log (LOG_DEBUG,
1034 "%s: truncating reported sub address (size is %d)\n",
1035 __FUNCTION__, size);
1036 size = MAXSTRLEN - 1;
1038 safe_copy (t->call_head->subaddy, (char *) &raw[3].s, size);
1039 if (gconfig.debug_avp)
1041 if (DEBUG)
1042 l2tp_log (LOG_DEBUG,
1043 "%s: peer reports subaddress '%s'\n", __FUNCTION__,
1044 t->call_head->subaddy);
1046 return 0;
1049 int vendor_avp (struct tunnel *t, struct call *c, void *data, int datalen)
1052 * What vendor makes the other end?
1054 int size;
1055 struct unaligned_u16 *raw = data;
1057 #ifdef SANITY
1058 if (t->sanity)
1060 switch (c->msgtype)
1062 case SCCRP:
1063 case SCCRQ:
1064 break;
1065 default:
1066 if (DEBUG)
1067 l2tp_log (LOG_DEBUG,
1068 "%s: vendor not appropriate for message %s. Ignoring.\n",
1069 __FUNCTION__, msgtypes[c->msgtype]);
1070 return 0;
1072 if (datalen < 6)
1074 if (DEBUG)
1075 l2tp_log (LOG_DEBUG,
1076 "%s: avp is too small. %d < 6\n", __FUNCTION__,
1077 datalen);
1078 wrong_length (c, "Vendor", 6, datalen, 1);
1079 return -EINVAL;
1082 #endif
1083 size = raw[0].s & 0x03FF;
1084 size -= sizeof (struct avp_hdr);
1085 if (size > MAXSTRLEN - 1)
1087 if (DEBUG)
1088 l2tp_log (LOG_DEBUG, "%s: truncating reported vendor (size is %d)\n",
1089 __FUNCTION__, size);
1090 size = MAXSTRLEN - 1;
1092 safe_copy (t->vendor, (char *) &raw[3].s, size);
1093 if (gconfig.debug_avp)
1095 if (DEBUG)
1096 l2tp_log (LOG_DEBUG,
1097 "%s: peer reports vendor '%s'\n", __FUNCTION__, t->vendor);
1099 return 0;
1102 int challenge_avp (struct tunnel *t, struct call *c, void *data, int datalen)
1105 * We are sent a challenge
1107 struct unaligned_u16 *raw = data;
1108 int size;
1109 #ifdef SANITY
1110 if (t->sanity)
1112 switch (c->msgtype)
1114 case SCCRP:
1115 case SCCRQ:
1116 break;
1117 default:
1118 if (DEBUG)
1119 l2tp_log (LOG_DEBUG,
1120 "%s: challenge not appropriate for message %s. Ignoring.\n",
1121 __FUNCTION__, msgtypes[c->msgtype]);
1122 return 0;
1124 if (datalen < 6)
1126 if (DEBUG)
1127 l2tp_log (LOG_DEBUG,
1128 "%s: avp is too small. %d < 6\n", __FUNCTION__,
1129 datalen);
1130 wrong_length (c, "challenge", 6, datalen, 1);
1131 return -EINVAL;
1134 #endif
1135 /* size = raw[0].s & 0x0FFF; */
1136 /* length field of AVP's is only 10 bits long, not 12 */
1137 size = raw[0].s & 0x03FF;
1138 size -= sizeof (struct avp_hdr);
1139 /* if (size != MD_SIG_SIZE)
1141 l2tp_log (LOG_DEBUG, "%s: Challenge is not the right length (%d != %d)\n",
1142 __FUNCTION__, size, MD_SIG_SIZE);
1143 return -EINVAL;
1144 } */
1145 if (t->chal_us.challenge)
1146 free(t->chal_us.challenge);
1147 t->chal_us.challenge = malloc(size);
1148 if (t->chal_us.challenge == NULL)
1150 return -ENOMEM;
1152 bcopy (&raw[3].s, (t->chal_us.challenge), size);
1153 t->chal_us.chal_len = size;
1154 t->chal_us.state = STATE_CHALLENGED;
1155 if (gconfig.debug_avp)
1157 l2tp_log (LOG_DEBUG, "%s: challenge avp found\n", __FUNCTION__);
1159 return 0;
1162 int chalresp_avp (struct tunnel *t, struct call *c, void *data, int datalen)
1165 * We are sent a challenge
1167 struct unaligned_u16 *raw = data;
1168 int size;
1169 #ifdef SANITY
1170 if (t->sanity)
1172 switch (c->msgtype)
1174 case SCCRP:
1175 case SCCCN:
1176 break;
1177 default:
1178 if (DEBUG)
1179 l2tp_log (LOG_DEBUG,
1180 "%s: challenge response not appropriate for message %s. Ignoring.\n",
1181 __FUNCTION__, msgtypes[c->msgtype]);
1182 return 0;
1184 if (datalen < 6)
1186 if (DEBUG)
1187 l2tp_log (LOG_DEBUG,
1188 "%s: avp is too small. %d < 6\n", __FUNCTION__,
1189 datalen);
1190 wrong_length (c, "challenge", 6, datalen, 1);
1191 return -EINVAL;
1194 #endif
1195 size = raw[0].s & 0x03FF;
1196 size -= sizeof (struct avp_hdr);
1197 if (size != MD_SIG_SIZE)
1199 l2tp_log (LOG_DEBUG, "%s: Challenge is not the right length (%d != %d)\n",
1200 __FUNCTION__, size, MD_SIG_SIZE);
1201 return -EINVAL;
1204 bcopy (&raw[3].s, t->chal_them.reply, MD_SIG_SIZE);
1205 if (gconfig.debug_avp)
1207 l2tp_log (LOG_DEBUG, "%s: Challenge reply found\n", __FUNCTION__);
1209 return 0;
1212 int assigned_tunnel_avp (struct tunnel *t, struct call *c, void *data,
1213 int datalen)
1216 * What is their TID that we must use from now on?
1218 struct unaligned_u16 *raw = data;
1220 #ifdef SANITY
1221 if (t->sanity)
1223 switch (c->msgtype)
1225 case SCCRP:
1226 case SCCRQ:
1227 case StopCCN:
1228 break;
1229 default:
1230 if (DEBUG)
1231 l2tp_log (LOG_DEBUG,
1232 "%s: tunnel ID not appropriate for message %s. Ignoring.\n",
1233 __FUNCTION__, msgtypes[c->msgtype]);
1234 return 0;
1236 if (datalen != 8)
1238 if (DEBUG)
1239 l2tp_log (LOG_DEBUG,
1240 "%s: avp is wrong size. %d != 8\n", __FUNCTION__,
1241 datalen);
1242 wrong_length (c, "Assigned Tunnel ID", 8, datalen, 0);
1243 return -EINVAL;
1246 #endif
1247 if (c->msgtype == StopCCN)
1249 t->qtid = ntohs (raw[3].s);
1251 else
1253 t->tid = ntohs (raw[3].s);
1255 if (gconfig.debug_avp)
1257 if (DEBUG)
1258 l2tp_log (LOG_DEBUG,
1259 "%s: using peer's tunnel %d\n", __FUNCTION__,
1260 ntohs (raw[3].s));
1262 return 0;
1265 int assigned_call_avp (struct tunnel *t, struct call *c, void *data,
1266 int datalen)
1269 * What is their CID that we must use from now on?
1271 struct unaligned_u16 *raw = data;
1273 #ifdef SANITY
1274 if (t->sanity)
1276 switch (c->msgtype)
1278 case CDN:
1279 case ICRP:
1280 case ICRQ:
1281 case OCRP: /* jz: deleting the debug message */
1282 break;
1283 case OCRQ:
1284 default:
1285 if (DEBUG)
1286 l2tp_log (LOG_DEBUG,
1287 "%s: call ID not appropriate for message %s. Ignoring.\n",
1288 __FUNCTION__, msgtypes[c->msgtype]);
1289 return 0;
1291 if (datalen != 8)
1293 if (DEBUG)
1294 l2tp_log (LOG_DEBUG,
1295 "%s: avp is wrong size. %d != 8\n", __FUNCTION__,
1296 datalen);
1297 wrong_length (c, "Assigned Call ID", 8, datalen, 0);
1298 return -EINVAL;
1301 #endif
1302 if (c->msgtype == CDN)
1304 c->qcid = ntohs (raw[3].s);
1306 else if (c->msgtype == ICRQ)
1308 t->call_head->cid = ntohs (raw[3].s);
1310 else if (c->msgtype == ICRP)
1312 c->cid = ntohs (raw[3].s);
1314 else if (c->msgtype == OCRP)
1315 { /* jz: copy callid to c->cid */
1316 c->cid = ntohs (raw[3].s);
1318 else
1320 l2tp_log (LOG_DEBUG, "%s: Dunno what to do when it's state %s!\n",
1321 __FUNCTION__, msgtypes[c->msgtype]);
1323 if (gconfig.debug_avp)
1325 if (DEBUG)
1326 l2tp_log (LOG_DEBUG,
1327 "%s: using peer's call %d\n", __FUNCTION__, ntohs (raw[3].s));
1329 return 0;
1332 int packet_delay_avp (struct tunnel *t, struct call *c, void *data,
1333 int datalen)
1336 * What is their CID that we must use from now on?
1338 struct unaligned_u16 *raw = data;
1340 #ifdef SANITY
1341 if (t->sanity)
1343 switch (c->msgtype)
1345 case ICRP:
1346 case OCRQ:
1347 case ICCN:
1348 case OCRP:
1349 case OCCN:
1350 break;
1351 default:
1352 if (DEBUG)
1353 l2tp_log (LOG_DEBUG,
1354 "%s: packet delay not appropriate for message %s. Ignoring.\n",
1355 __FUNCTION__, msgtypes[c->msgtype]);
1356 return 0;
1358 if (datalen != 8)
1360 if (DEBUG)
1361 l2tp_log (LOG_DEBUG,
1362 "%s: avp is wrong size. %d != 8\n", __FUNCTION__,
1363 datalen);
1364 wrong_length (c, "Assigned Call ID", 8, datalen, 0);
1365 return -EINVAL;
1368 #endif
1369 c->ppd = ntohs (raw[3].s);
1370 if (gconfig.debug_avp)
1372 if (DEBUG)
1373 l2tp_log (LOG_DEBUG,
1374 "%s: peer's delay is %d 1/10's of a second\n", __FUNCTION__,
1375 ntohs (raw[3].s));
1377 return 0;
1380 int call_serno_avp (struct tunnel *t, struct call *c, void *data, int datalen)
1383 * What is the serial number of the call?
1385 struct unaligned_u16 *raw = data;
1387 #ifdef SANITY
1388 if (t->sanity)
1390 switch (c->msgtype)
1392 case ICRQ:
1393 case OCRQ:
1394 break;
1395 default:
1396 if (DEBUG)
1397 l2tp_log (LOG_DEBUG,
1398 "%s: call ID not appropriate for message %s. Ignoring.\n",
1399 __FUNCTION__, msgtypes[c->msgtype]);
1400 return 0;
1402 if (datalen != 10)
1404 #ifdef STRICT
1405 if (DEBUG)
1406 l2tp_log (LOG_DEBUG,
1407 "%s: avp is wrong size. %d != 10\n", __FUNCTION__,
1408 datalen);
1409 wrong_length (c, "Serial Number", 10, datalen, 0);
1410 return -EINVAL;
1411 #else
1412 l2tp_log (LOG_DEBUG,
1413 "%s: peer is using old style serial number. Will be invalid.\n",
1414 __FUNCTION__);
1415 #endif
1419 #endif
1420 t->call_head->serno = (((unsigned int) ntohs (raw[3].s)) << 16) |
1421 ((unsigned int) ntohs (raw[4].s));
1422 if (gconfig.debug_avp)
1424 if (DEBUG)
1425 l2tp_log (LOG_DEBUG,
1426 "%s: serial number is %d\n", __FUNCTION__,
1427 t->call_head->serno);
1429 return 0;
1432 int rx_speed_avp (struct tunnel *t, struct call *c, void *data, int datalen)
1435 * What is the received baud rate of the call?
1437 struct unaligned_u16 *raw = data;
1439 #ifdef SANITY
1440 if (t->sanity)
1442 switch (c->msgtype)
1444 case ICCN:
1445 case OCCN:
1446 case OCRP:
1447 break;
1448 default:
1449 if (DEBUG)
1450 l2tp_log (LOG_DEBUG,
1451 "%s: rx connect speed not appropriate for message %s. Ignoring.\n",
1452 __FUNCTION__, msgtypes[c->msgtype]);
1453 return 0;
1455 if (datalen != 10)
1457 if (DEBUG)
1458 l2tp_log (LOG_DEBUG,
1459 "%s: avp is wrong size. %d != 10\n", __FUNCTION__,
1460 datalen);
1461 wrong_length (c, "Connect Speed (RX)", 10, datalen, 0);
1462 return -EINVAL;
1465 #endif
1466 c->rxspeed = (((unsigned int) ntohs (raw[3].s)) << 16) |
1467 ((unsigned int) ntohs (raw[4].s));
1468 if (gconfig.debug_avp)
1470 if (DEBUG)
1471 l2tp_log (LOG_DEBUG,
1472 "%s: receive baud rate is %d\n", __FUNCTION__, c->rxspeed);
1474 return 0;
1477 int tx_speed_avp (struct tunnel *t, struct call *c, void *data, int datalen)
1480 * What is the tranmsit baud rate of the call?
1482 struct unaligned_u16 *raw = data;
1484 #ifdef SANITY
1485 if (t->sanity)
1487 switch (c->msgtype)
1489 case ICCN:
1490 case OCCN:
1491 case OCRP:
1492 break;
1493 default:
1494 if (DEBUG)
1495 l2tp_log (LOG_DEBUG,
1496 "%s: tx connect speed not appropriate for message %s. Ignoring.\n",
1497 __FUNCTION__, msgtypes[c->msgtype]);
1498 return 0;
1500 if (datalen != 10)
1502 if (DEBUG)
1503 l2tp_log (LOG_DEBUG,
1504 "%s: avp is wrong size. %d != 10\n", __FUNCTION__,
1505 datalen);
1506 wrong_length (c, "Connect Speed (tx)", 10, datalen, 0);
1507 return -EINVAL;
1510 #endif
1511 c->txspeed = (((unsigned int) ntohs (raw[3].s)) << 16) |
1512 ((unsigned int) ntohs (raw[4].s));
1513 if (gconfig.debug_avp)
1515 if (DEBUG)
1516 l2tp_log (LOG_DEBUG,
1517 "%s: transmit baud rate is %d\n", __FUNCTION__, c->txspeed);
1519 return 0;
1521 int call_physchan_avp (struct tunnel *t, struct call *c, void *data,
1522 int datalen)
1525 * What is the physical channel?
1527 struct unaligned_u16 *raw = data;
1529 #ifdef SANITY
1530 if (t->sanity)
1532 switch (c->msgtype)
1534 case ICRQ:
1535 case OCRQ:
1536 case OCRP:
1537 case OCCN:
1538 break;
1539 default:
1540 if (DEBUG)
1541 l2tp_log (LOG_DEBUG,
1542 "%s: physical channel not appropriate for message %s. Ignoring.\n",
1543 __FUNCTION__, msgtypes[c->msgtype]);
1544 return 0;
1546 if (datalen != 10)
1548 if (DEBUG)
1549 l2tp_log (LOG_DEBUG,
1550 "%s: avp is wrong size. %d != 10\n", __FUNCTION__,
1551 datalen);
1552 wrong_length (c, "Physical Channel", 10, datalen, 0);
1553 return -EINVAL;
1556 #endif
1557 t->call_head->physchan = (((unsigned int) ntohs (raw[3].s)) << 16) |
1558 ((unsigned int) ntohs (raw[4].s));
1559 if (gconfig.debug_avp)
1561 if (DEBUG)
1562 l2tp_log (LOG_DEBUG,
1563 "%s: physical channel is %d\n", __FUNCTION__,
1564 t->call_head->physchan);
1566 return 0;
1569 int receive_window_size_avp (struct tunnel *t, struct call *c, void *data,
1570 int datalen)
1573 * What is their RWS?
1575 struct unaligned_u16 *raw = data;
1577 #ifdef SANITY
1578 if (t->sanity)
1580 switch (c->msgtype)
1582 case SCCRP:
1583 case SCCRQ:
1584 case OCRP: /* jz */
1585 case OCCN: /* jz */
1586 case StopCCN:
1587 /* case ICRP:
1588 case ICCN: */
1589 break;
1590 default:
1591 if (DEBUG)
1592 l2tp_log (LOG_DEBUG,
1593 "%s: RWS not appropriate for message %s. Ignoring.\n",
1594 __FUNCTION__, msgtypes[c->msgtype]);
1595 return 0;
1597 if (datalen != 8)
1599 if (DEBUG)
1600 l2tp_log (LOG_DEBUG,
1601 "%s: avp is wrong size. %d != 8\n", __FUNCTION__,
1602 datalen);
1603 wrong_length (c, "Receive Window Size", 8, datalen, 0);
1604 return -EINVAL;
1607 #endif
1608 t->rws = ntohs (raw[3].s);
1609 /* if (c->rws >= 0)
1610 c->fbit = FBIT; */
1611 if (gconfig.debug_avp)
1613 if (DEBUG)
1614 l2tp_log (LOG_DEBUG,
1615 "%s: peer wants RWS of %d. Will use flow control.\n",
1616 __FUNCTION__, t->rws);
1618 return 0;
1622 int handle_avps (struct buffer *buf, struct tunnel *t, struct call *c)
1625 * buf's start should point to the beginning of a packet. We assume it's
1626 * a valid packet and has had check_control done to it, so no error
1627 * checking is done at this point.
1630 struct avp_hdr *avp;
1631 int len = buf->len - sizeof (struct control_hdr);
1632 int firstavp = -1;
1633 int hidlen = 0;
1634 char *data = buf->start + sizeof (struct control_hdr);
1635 avp = (struct avp_hdr *) data;
1636 if (gconfig.debug_avp)
1637 l2tp_log (LOG_DEBUG, "%s: handling avp's for tunnel %d, call %d\n",
1638 __FUNCTION__, t->ourtid, c->ourcid);
1639 while (len > 0)
1641 /* Go ahead and byte-swap the header */
1642 swaps (avp, sizeof (struct avp_hdr));
1643 if (avp->attr > AVP_MAX)
1645 if (AMBIT (avp->length))
1647 l2tp_log (LOG_WARNING,
1648 "%s: don't know how to handle mandatory attribute %d. Closing %s.\n",
1649 __FUNCTION__, avp->attr,
1650 (c != t->self) ? "call" : "tunnel");
1651 set_error (c, VENDOR_ERROR,
1652 "mandatory attribute %d cannot be handled",
1653 avp->attr);
1654 c->needclose = -1;
1655 return -EINVAL;
1657 else
1659 if (DEBUG)
1660 l2tp_log (LOG_WARNING,
1661 "%s: don't know how to handle atribute %d.\n",
1662 __FUNCTION__, avp->attr);
1663 goto next;
1666 if (ALENGTH (avp->length) > len)
1668 l2tp_log (LOG_WARNING,
1669 "%s: AVP received with length > remaining packet length!\n",
1670 __FUNCTION__);
1671 set_error (c, ERROR_LENGTH, "Invalid AVP length");
1672 c->needclose = -1;
1673 return -EINVAL;
1675 if (avp->attr && firstavp)
1677 l2tp_log (LOG_WARNING, "%s: First AVP was not message type.\n",
1678 __FUNCTION__);
1679 set_error (c, VENDOR_ERROR, "First AVP must be message type");
1680 c->needclose = -1;
1681 return -EINVAL;
1683 if (ALENGTH (avp->length) < sizeof (struct avp_hdr))
1685 l2tp_log (LOG_WARNING, "%s: AVP with too small of size (%d).\n",
1686 __FUNCTION__, ALENGTH (avp->length));
1687 set_error (c, ERROR_LENGTH, "AVP too small");
1688 c->needclose = -1;
1689 return -EINVAL;
1691 if (AZBITS (avp->length))
1693 l2tp_log (LOG_WARNING, "%s: %sAVP has reserved bits set.\n", __FUNCTION__,
1694 AMBIT (avp->length) ? "Mandatory " : "");
1695 if (AMBIT (avp->length))
1697 set_error (c, ERROR_RESERVED, "reserved bits set in AVP");
1698 c->needclose = -1;
1699 return -EINVAL;
1701 goto next;
1703 if (AHBIT (avp->length))
1705 #ifdef DEBUG_HIDDEN
1706 l2tp_log (LOG_DEBUG, "%s: Hidden bit set on AVP.\n", __FUNCTION__);
1707 #endif
1708 /* We want to rewrite the AVP as an unhidden AVP
1709 and then pass it along as normal. Remeber how
1710 long the AVP was in the first place though! */
1711 hidlen = avp->length;
1712 if (decrypt_avp (data, t))
1714 if (gconfig.debug_avp)
1715 l2tp_log (LOG_WARNING, "%s: Unable to handle hidden %sAVP\n:",
1716 __FUNCTION__,
1717 (AMBIT (avp->length) ? "mandatory " : ""));
1718 if (AMBIT (avp->length))
1720 set_error (c, VENDOR_ERROR, "Invalid Hidden AVP");
1721 c->needclose = -1;
1722 return -EINVAL;
1724 goto next;
1726 len -= 2;
1727 hidlen -= 2;
1728 data += 2;
1729 avp = (struct avp_hdr *) data;
1730 /* Now we should look like a normal AVP */
1732 else
1733 hidlen = 0;
1734 if (avps[avp->attr].handler)
1736 if (avps[avp->attr].handler (t, c, avp, ALENGTH (avp->length)))
1738 if (AMBIT (avp->length))
1740 l2tp_log (LOG_WARNING,
1741 "%s: Bad exit status handling attribute %d (%s) on mandatory packet.\n",
1742 __FUNCTION__, avp->attr,
1743 avps[avp->attr].description);
1744 c->needclose = -1;
1745 return -EINVAL;
1747 else
1749 if (DEBUG)
1750 l2tp_log (LOG_DEBUG,
1751 "%s: Bad exit status handling attribute %d (%s).\n",
1752 __FUNCTION__, avp->attr,
1753 avps[avp->attr].description);
1757 else
1759 if (AMBIT (avp->length))
1761 l2tp_log (LOG_WARNING,
1762 "%s: No handler for mandatory attribute %d (%s). Closing %s.\n",
1763 __FUNCTION__, avp->attr, avps[avp->attr].description,
1764 (c != t->self) ? "call" : "tunnel");
1765 set_error (c, VENDOR_ERROR, "No handler for attr %d (%s)\n",
1766 avp->attr, avps[avp->attr].description);
1767 return -EINVAL;
1769 else
1771 if (DEBUG)
1772 l2tp_log (LOG_WARNING, "%s: no handler for atribute %d (%s).\n",
1773 __FUNCTION__, avp->attr,
1774 avps[avp->attr].description);
1777 next:
1778 if (hidlen)
1780 /* Skip over the complete length of the hidden AVP */
1781 len -= ALENGTH (hidlen);
1782 data += ALENGTH (hidlen);
1784 else
1786 len -= ALENGTH (avp->length);
1787 data += ALENGTH (avp->length); /* Next AVP, please */
1789 avp = (struct avp_hdr *) data;
1790 firstavp = 0;
1792 if (len != 0)
1794 l2tp_log (LOG_WARNING, "%s: negative overall packet length\n", __FUNCTION__);
1795 return -EINVAL;
1797 return 0;