2 * Layer Two Tunnelling Protocol Daemon
3 * Copyright (C) 1998 Adtran, Inc.
4 * Copyright (C) 2002 Jeff McAdams
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
18 #include <netinet/in.h>
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"}
69 "Start-Control-Connection-Request",
70 "Start-Control-Connection-Reply",
71 "Start-Control-Connection-Connected",
72 "Stop-Control-Connection-Notification",
75 "Outgoing-Call-Request",
76 "Outgoing-Call-Reply",
77 "Outgoing-Call-Connected",
78 "Incoming-Call-Request",
79 "Incoming-Call-Reply",
80 "Incoming-Call-Connected",
82 "Call-Disconnect-Notify",
87 char *stopccn_result_codes
[] = {
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
[] = {
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
,
116 snprintf (c
->errormsg
, sizeof (c
->errormsg
),
117 "%s: expected at least %d, got %d", field
, expected
, found
);
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
;
127 struct unaligned_u16
{
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
,
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
);
149 l2tp_log (LOG_DEBUG
, "%s: wrong size (%d != 8)\n", __FUNCTION__
,
151 wrong_length (c
, "Message Type", 8, datalen
, 0);
154 if ((c
->msgtype
> MAX_MSG
) || (!msgtypes
[c
->msgtype
]))
157 l2tp_log (LOG_DEBUG
, "%s: unknown message type %d\n", __FUNCTION__
,
161 if (gconfig
.debug_avp
)
163 l2tp_log (LOG_DEBUG
, "%s: message type %d (%s)\n", __FUNCTION__
,
164 c
->msgtype
, msgtypes
[c
->msgtype
]);
169 * Look ou our state for each message and make sure everything
172 if ((c
!= t
->self
) && (c
->msgtype
< Hello
))
176 "%s: attempting to negotiate tunnel inside a call!\n",
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
193 "%s: attempting to negotiate SCCRQ with state != 0\n",
199 if (t
->state
!= SCCRQ
)
203 "%s: attempting to negotiate SCCRP with state != SCCRQ!\n",
209 if (t
->state
!= SCCRP
)
213 "%s: attempting to negotiate SCCCN with state != SCCRP!\n",
219 if (t
->state
!= SCCCN
)
223 "%s: attempting to negotiate ICRQ when state != SCCCN\n",
231 "%s: attempting to negotiate ICRQ on a call!\n",
237 if (t
->state
!= SCCCN
)
241 "%s: attempting to negotiate ICRP on tunnel!=SCCCN\n",
245 if (c
->state
!= ICRQ
)
249 "%s: attempting to negotiate ICRP when state != ICRQ\n",
255 if (c
->state
!= ICRP
)
259 "%s: attempting to negotiate ICCN when state != ICRP\n",
265 if (c
->state
!= ICCN
)
269 "%s: attempting to negotiate SLI when state != ICCN\n",
274 case OCRP
: /* jz: case for ORCP */
275 if (t
->state
!= SCCCN
)
279 "%s: attempting to negotiate OCRP on tunnel!=SCCCN\n",
283 if (c
->state
!= OCRQ
)
287 "%s: attempting to negotiate OCRP when state != OCRQ\n",
292 case OCCN
: /* jz: case for OCCN */
294 if (c
->state
!= OCRQ
)
298 "%s: attempting to negotiate OCCN when state != OCRQ\n",
308 l2tp_log (LOG_WARNING
, "%s: i don't know how to handle %s messages\n",
309 __FUNCTION__
, msgtypes
[c
->msgtype
]);
314 if (c
->msgtype
== ICRQ
)
317 if (gconfig
.debug_avp
)
320 l2tp_log (LOG_DEBUG
, "%s: new incoming call\n", __FUNCTION__
);
325 l2tp_log (LOG_WARNING
, "%s: unable to create new call\n", __FUNCTION__
);
328 tmp
->next
= t
->call_head
;
332 * Is this still safe to assume that the head will always
333 * be the most recent call being negotiated?
334 * Probably... FIXME anyway...
341 int rand_vector_avp (struct tunnel
*t
, struct call
*c
, void *data
,
345 struct unaligned_u16
*raw
= data
;
346 size
= raw
[0].s
& 0x03FF;
347 size
-= sizeof (struct avp_hdr
);
354 l2tp_log (LOG_DEBUG
, "%s: Random vector too small (%d < 0)\n",
356 wrong_length (c
, "Random Vector", 6, datalen
, 1);
359 if (size
> MAX_VECTOR_SIZE
)
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);
369 if (gconfig
.debug_avp
)
370 l2tp_log (LOG_DEBUG
, "%s: Random Vector of %d octets\n", __FUNCTION__
,
372 t
->chal_us
.vector
= (unsigned char *) &raw
[3].s
;
373 t
->chal_us
.vector_len
= size
;
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
)
393 l2tp_log (LOG_DEBUG
, "%s : Ignoring AVP\n", __FUNCTION__
);
398 int seq_reqd_avp (struct tunnel
*t
, struct call
*c
, void *data
, int datalen
)
407 "%s: avp is incorrect size. %d != 6\n", __FUNCTION__
,
409 wrong_length (c
, "Sequencing Required", 6, datalen
, 1);
419 "%s: sequencing required not appropriate for %s!\n",
420 __FUNCTION__
, msgtypes
[c
->msgtype
]);
425 if (gconfig
.debug_avp
)
428 l2tp_log (LOG_DEBUG
, "%s: peer requires sequencing.\n", __FUNCTION__
);
434 int result_code_avp (struct tunnel
*t
, struct call
*c
, void *data
,
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..
444 struct unaligned_u16
*raw
= data
;
452 "%s: avp is incorrect size. %d < 10\n", __FUNCTION__
,
454 wrong_length (c
, "Result Code", 10, datalen
, 1);
465 "%s: result code not appropriate for %s. Ignoring.\n",
466 __FUNCTION__
, msgtypes
[c
->msgtype
]);
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))
482 "%s: result code endianness fix for buggy Apple client. network=%d, le=%d\n",
483 __FUNCTION__
, result
, result
>> 8);
487 if (((error
& 0xFF) == 0) && (error
>> 8 != 0))
491 "%s: error code endianness fix for buggy Apple client. network=%d, le=%d\n",
492 __FUNCTION__
, error
, error
>> 8);
496 if ((c
->msgtype
== StopCCN
) && ((result
> 7) || (result
< 1)))
500 "%s: result code out of range (%d %d %d). Ignoring.\n",
501 __FUNCTION__
, result
, error
, datalen
);
505 if ((c
->msgtype
== CDN
) && ((result
> 11) || (result
< 1)))
509 "%s: result code out of range (%d %d %d). Ignoring.\n",
510 __FUNCTION__
, result
, error
, datalen
);
516 safe_copy (c
->errormsg
, (char *) &raw
[5].s
, datalen
- 10);
517 if (gconfig
.debug_avp
)
519 if (DEBUG
&& (c
->msgtype
== StopCCN
))
522 "%s: peer closing for reason %d (%s), error = %d (%s)\n",
523 __FUNCTION__
, result
, stopccn_result_codes
[result
], error
,
529 "%s: peer closing for reason %d (%s), error = %d (%s)\n",
530 __FUNCTION__
, result
, cdn_result_codes
[result
], error
,
537 int protocol_version_avp (struct tunnel
*t
, struct call
*c
, void *data
,
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..
546 struct unaligned_u16
*raw
= data
;
554 "%s: avp is incorrect size. %d != 8\n", __FUNCTION__
,
556 wrong_length (c
, "Protocol Version", 8, datalen
, 1);
567 "%s: protocol version not appropriate for %s. Ignoring.\n",
568 __FUNCTION__
, msgtypes
[c
->msgtype
]);
573 ver
= ntohs (raw
[3].s
);
574 if (gconfig
.debug_avp
)
578 "%s: peer is using version %d, revision %d.\n", __FUNCTION__
,
579 (ver
>> 8), ver
& 0xFF);
584 int framing_caps_avp (struct tunnel
*t
, struct call
*c
, void *data
,
588 * Retrieve the framing capabilities
593 struct unaligned_u16
*raw
= data
;
606 "%s: framing capabilities not appropriate for %s. Ignoring.\n",
607 __FUNCTION__
, msgtypes
[c
->msgtype
]);
614 "%s: avp is incorrect size. %d != 10\n", __FUNCTION__
,
616 wrong_length (c
, "Framming Capabilities", 10, datalen
, 0);
621 caps
= ntohs (raw
[4].s
);
622 if (gconfig
.debug_avp
)
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
);
632 int bearer_caps_avp (struct tunnel
*t
, struct call
*c
, void *data
,
636 * What kind of bearer channels does our peer support?
639 struct unaligned_u16
*raw
= data
;
652 "%s: bearer capabilities not appropriate for message %s. Ignoring.\n",
653 __FUNCTION__
, msgtypes
[c
->msgtype
]);
660 "%s: avp is incorrect size. %d != 10\n", __FUNCTION__
,
662 wrong_length (c
, "Bearer Capabilities", 10, datalen
, 0);
667 caps
= ntohs (raw
[4].s
);
668 if (gconfig
.debug_avp
)
673 "%s: supported peer bearers:%s%s\n",
675 caps
& ANALOG_BEARER
? " analog" : "",
676 caps
& DIGITAL_BEARER
? " digital" : "");
680 t
->bc
= caps
& (ANALOG_BEARER
| DIGITAL_BEARER
);
685 /* FIXME: I need to handle tie breakers eventually */
687 int firmware_rev_avp (struct tunnel
*t
, struct call
*c
, void *data
,
691 * Report and record remote firmware version
694 struct unaligned_u16
*raw
= data
;
707 "%s: firmware revision not appropriate for message %s. Ignoring.\n",
708 __FUNCTION__
, msgtypes
[c
->msgtype
]);
715 "%s: avp is incorrect size. %d != 8\n", __FUNCTION__
,
717 wrong_length (c
, "Firmware Revision", 8, datalen
, 0);
722 ver
= ntohs (raw
[3].s
);
723 if (gconfig
.debug_avp
)
727 "%s: peer reports firmware version %d (0x%.4x)\n",
728 __FUNCTION__
, ver
, ver
);
734 int bearer_type_avp (struct tunnel
*t
, struct call
*c
, void *data
,
738 * What kind of bearer channel is the call on?
741 struct unaligned_u16
*raw
= data
;
754 "%s: bearer type not appropriate for message %s. Ignoring.\n",
755 __FUNCTION__
, msgtypes
[c
->msgtype
]);
762 "%s: avp is incorrect size. %d != 10\n", __FUNCTION__
,
764 wrong_length (c
, "Bearer Type", 10, datalen
, 0);
769 b
= ntohs (raw
[4].s
);
770 if (gconfig
.debug_avp
)
774 "%s: peer bears:%s\n", __FUNCTION__
,
775 b
& ANALOG_BEARER
? " analog" : "digital");
777 t
->call_head
->bearer
= b
;
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?
787 struct unaligned_u16
*raw
= data
;
801 "%s: frame type not appropriate for message %s. Ignoring.\n",
802 __FUNCTION__
, msgtypes
[c
->msgtype
]);
809 "%s: avp is incorrect size. %d != 10\n", __FUNCTION__
,
811 wrong_length (c
, "Frame Type", 10, datalen
, 0);
816 b
= ntohs (raw
[4].s
);
817 if (gconfig
.debug_avp
)
821 "%s: peer uses:%s frames\n", __FUNCTION__
,
822 b
& ASYNC_FRAMING
? " async" : "sync");
828 int hostname_avp (struct tunnel
*t
, struct call
*c
, void *data
, int datalen
)
831 * What is the peer's name?
834 struct unaligned_u16
*raw
= data
;
847 "%s: hostname not appropriate for message %s. Ignoring.\n",
848 __FUNCTION__
, msgtypes
[c
->msgtype
]);
855 "%s: avp is too small. %d < 6\n", __FUNCTION__
,
857 wrong_length (c
, "Hostname", 6, datalen
, 1);
862 size
= raw
[0].s
& 0x03FF;
863 size
-= sizeof (struct avp_hdr
);
864 if (size
> MAXSTRLEN
- 1)
867 l2tp_log (LOG_DEBUG
, "%s: truncating reported hostname (size is %d)\n",
869 size
= MAXSTRLEN
- 1;
871 safe_copy (t
->hostname
, (char *) &raw
[3].s
, size
);
872 if (gconfig
.debug_avp
)
876 "%s: peer reports hostname '%s'\n", __FUNCTION__
,
882 int dialing_number_avp (struct tunnel
*t
, struct call
*c
, void *data
,
886 * What is the peer's name?
889 struct unaligned_u16
*raw
= data
;
901 "%s: dialing number not appropriate for message %s. Ignoring.\n",
902 __FUNCTION__
, msgtypes
[c
->msgtype
]);
909 "%s: avp is too small. %d < 6\n", __FUNCTION__
,
911 wrong_length (c
, "Dialing Number", 6, datalen
, 1);
916 size
= raw
[0].s
& 0x03FF;
917 size
-= sizeof (struct avp_hdr
);
918 if (size
> MAXSTRLEN
- 1)
922 "%s: truncating reported dialing number (size is %d)\n",
924 size
= MAXSTRLEN
- 1;
926 safe_copy (t
->call_head
->dialing
, (char *) &raw
[3].s
, size
);
927 if (gconfig
.debug_avp
)
931 "%s: peer reports dialing number '%s'\n", __FUNCTION__
,
932 t
->call_head
->dialing
);
937 int dialed_number_avp (struct tunnel
*t
, struct call
*c
, void *data
,
941 * What is the peer's name?
944 struct unaligned_u16
*raw
= data
;
957 "%s: dialed number not appropriate for message %s. Ignoring.\n",
958 __FUNCTION__
, msgtypes
[c
->msgtype
]);
965 "%s: avp is too small. %d < 6\n", __FUNCTION__
,
967 wrong_length (c
, "Dialed Number", 6, datalen
, 1);
972 size
= raw
[0].s
& 0x03FF;
973 size
-= sizeof (struct avp_hdr
);
974 if (size
> MAXSTRLEN
- 1)
978 "%s: truncating reported dialed number (size is %d)\n",
980 size
= MAXSTRLEN
- 1;
982 safe_copy (t
->call_head
->dialed
, (char *) &raw
[3].s
, size
);
983 if (gconfig
.debug_avp
)
987 "%s: peer reports dialed number '%s'\n", __FUNCTION__
,
988 t
->call_head
->dialed
);
993 int sub_address_avp (struct tunnel
*t
, struct call
*c
, void *data
,
997 * What is the peer's name?
1000 struct unaligned_u16
*raw
= data
;
1012 l2tp_log (LOG_DEBUG
,
1013 "%s: sub_address not appropriate for message %s. Ignoring.\n",
1014 __FUNCTION__
, msgtypes
[c
->msgtype
]);
1020 l2tp_log (LOG_DEBUG
,
1021 "%s: avp is too small. %d < 6\n", __FUNCTION__
,
1023 wrong_length (c
, "Sub-address", 6, datalen
, 1);
1028 size
= raw
[0].s
& 0x03FF;
1029 size
-= sizeof (struct avp_hdr
);
1030 if (size
> MAXSTRLEN
- 1)
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
)
1042 l2tp_log (LOG_DEBUG
,
1043 "%s: peer reports subaddress '%s'\n", __FUNCTION__
,
1044 t
->call_head
->subaddy
);
1049 int vendor_avp (struct tunnel
*t
, struct call
*c
, void *data
, int datalen
)
1052 * What vendor makes the other end?
1055 struct unaligned_u16
*raw
= data
;
1067 l2tp_log (LOG_DEBUG
,
1068 "%s: vendor not appropriate for message %s. Ignoring.\n",
1069 __FUNCTION__
, msgtypes
[c
->msgtype
]);
1075 l2tp_log (LOG_DEBUG
,
1076 "%s: avp is too small. %d < 6\n", __FUNCTION__
,
1078 wrong_length (c
, "Vendor", 6, datalen
, 1);
1083 size
= raw
[0].s
& 0x03FF;
1084 size
-= sizeof (struct avp_hdr
);
1085 if (size
> MAXSTRLEN
- 1)
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
)
1096 l2tp_log (LOG_DEBUG
,
1097 "%s: peer reports vendor '%s'\n", __FUNCTION__
, t
->vendor
);
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
;
1119 l2tp_log (LOG_DEBUG
,
1120 "%s: challenge not appropriate for message %s. Ignoring.\n",
1121 __FUNCTION__
, msgtypes
[c
->msgtype
]);
1127 l2tp_log (LOG_DEBUG
,
1128 "%s: avp is too small. %d < 6\n", __FUNCTION__
,
1130 wrong_length (c
, "challenge", 6, datalen
, 1);
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);
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
)
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__
);
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
;
1179 l2tp_log (LOG_DEBUG
,
1180 "%s: challenge response not appropriate for message %s. Ignoring.\n",
1181 __FUNCTION__
, msgtypes
[c
->msgtype
]);
1187 l2tp_log (LOG_DEBUG
,
1188 "%s: avp is too small. %d < 6\n", __FUNCTION__
,
1190 wrong_length (c
, "challenge", 6, datalen
, 1);
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
);
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__
);
1212 int assigned_tunnel_avp (struct tunnel
*t
, struct call
*c
, void *data
,
1216 * What is their TID that we must use from now on?
1218 struct unaligned_u16
*raw
= data
;
1231 l2tp_log (LOG_DEBUG
,
1232 "%s: tunnel ID not appropriate for message %s. Ignoring.\n",
1233 __FUNCTION__
, msgtypes
[c
->msgtype
]);
1239 l2tp_log (LOG_DEBUG
,
1240 "%s: avp is wrong size. %d != 8\n", __FUNCTION__
,
1242 wrong_length (c
, "Assigned Tunnel ID", 8, datalen
, 0);
1247 if (c
->msgtype
== StopCCN
)
1249 t
->qtid
= ntohs (raw
[3].s
);
1253 t
->tid
= ntohs (raw
[3].s
);
1255 if (gconfig
.debug_avp
)
1258 l2tp_log (LOG_DEBUG
,
1259 "%s: using peer's tunnel %d\n", __FUNCTION__
,
1265 int assigned_call_avp (struct tunnel
*t
, struct call
*c
, void *data
,
1269 * What is their CID that we must use from now on?
1271 struct unaligned_u16
*raw
= data
;
1281 case OCRP
: /* jz: deleting the debug message */
1286 l2tp_log (LOG_DEBUG
,
1287 "%s: call ID not appropriate for message %s. Ignoring.\n",
1288 __FUNCTION__
, msgtypes
[c
->msgtype
]);
1294 l2tp_log (LOG_DEBUG
,
1295 "%s: avp is wrong size. %d != 8\n", __FUNCTION__
,
1297 wrong_length (c
, "Assigned Call ID", 8, datalen
, 0);
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
);
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
)
1326 l2tp_log (LOG_DEBUG
,
1327 "%s: using peer's call %d\n", __FUNCTION__
, ntohs (raw
[3].s
));
1332 int packet_delay_avp (struct tunnel
*t
, struct call
*c
, void *data
,
1336 * What is their CID that we must use from now on?
1338 struct unaligned_u16
*raw
= data
;
1353 l2tp_log (LOG_DEBUG
,
1354 "%s: packet delay not appropriate for message %s. Ignoring.\n",
1355 __FUNCTION__
, msgtypes
[c
->msgtype
]);
1361 l2tp_log (LOG_DEBUG
,
1362 "%s: avp is wrong size. %d != 8\n", __FUNCTION__
,
1364 wrong_length (c
, "Assigned Call ID", 8, datalen
, 0);
1369 c
->ppd
= ntohs (raw
[3].s
);
1370 if (gconfig
.debug_avp
)
1373 l2tp_log (LOG_DEBUG
,
1374 "%s: peer's delay is %d 1/10's of a second\n", __FUNCTION__
,
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
;
1397 l2tp_log (LOG_DEBUG
,
1398 "%s: call ID not appropriate for message %s. Ignoring.\n",
1399 __FUNCTION__
, msgtypes
[c
->msgtype
]);
1406 l2tp_log (LOG_DEBUG
,
1407 "%s: avp is wrong size. %d != 10\n", __FUNCTION__
,
1409 wrong_length (c
, "Serial Number", 10, datalen
, 0);
1412 l2tp_log (LOG_DEBUG
,
1413 "%s: peer is using old style serial number. Will be invalid.\n",
1420 t
->call_head
->serno
= (((unsigned int) ntohs (raw
[3].s
)) << 16) |
1421 ((unsigned int) ntohs (raw
[4].s
));
1422 if (gconfig
.debug_avp
)
1425 l2tp_log (LOG_DEBUG
,
1426 "%s: serial number is %d\n", __FUNCTION__
,
1427 t
->call_head
->serno
);
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
;
1450 l2tp_log (LOG_DEBUG
,
1451 "%s: rx connect speed not appropriate for message %s. Ignoring.\n",
1452 __FUNCTION__
, msgtypes
[c
->msgtype
]);
1458 l2tp_log (LOG_DEBUG
,
1459 "%s: avp is wrong size. %d != 10\n", __FUNCTION__
,
1461 wrong_length (c
, "Connect Speed (RX)", 10, datalen
, 0);
1466 c
->rxspeed
= (((unsigned int) ntohs (raw
[3].s
)) << 16) |
1467 ((unsigned int) ntohs (raw
[4].s
));
1468 if (gconfig
.debug_avp
)
1471 l2tp_log (LOG_DEBUG
,
1472 "%s: receive baud rate is %d\n", __FUNCTION__
, c
->rxspeed
);
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
;
1495 l2tp_log (LOG_DEBUG
,
1496 "%s: tx connect speed not appropriate for message %s. Ignoring.\n",
1497 __FUNCTION__
, msgtypes
[c
->msgtype
]);
1503 l2tp_log (LOG_DEBUG
,
1504 "%s: avp is wrong size. %d != 10\n", __FUNCTION__
,
1506 wrong_length (c
, "Connect Speed (tx)", 10, datalen
, 0);
1511 c
->txspeed
= (((unsigned int) ntohs (raw
[3].s
)) << 16) |
1512 ((unsigned int) ntohs (raw
[4].s
));
1513 if (gconfig
.debug_avp
)
1516 l2tp_log (LOG_DEBUG
,
1517 "%s: transmit baud rate is %d\n", __FUNCTION__
, c
->txspeed
);
1521 int call_physchan_avp (struct tunnel
*t
, struct call
*c
, void *data
,
1525 * What is the physical channel?
1527 struct unaligned_u16
*raw
= data
;
1541 l2tp_log (LOG_DEBUG
,
1542 "%s: physical channel not appropriate for message %s. Ignoring.\n",
1543 __FUNCTION__
, msgtypes
[c
->msgtype
]);
1549 l2tp_log (LOG_DEBUG
,
1550 "%s: avp is wrong size. %d != 10\n", __FUNCTION__
,
1552 wrong_length (c
, "Physical Channel", 10, datalen
, 0);
1557 t
->call_head
->physchan
= (((unsigned int) ntohs (raw
[3].s
)) << 16) |
1558 ((unsigned int) ntohs (raw
[4].s
));
1559 if (gconfig
.debug_avp
)
1562 l2tp_log (LOG_DEBUG
,
1563 "%s: physical channel is %d\n", __FUNCTION__
,
1564 t
->call_head
->physchan
);
1569 int receive_window_size_avp (struct tunnel
*t
, struct call
*c
, void *data
,
1573 * What is their RWS?
1575 struct unaligned_u16
*raw
= data
;
1592 l2tp_log (LOG_DEBUG
,
1593 "%s: RWS not appropriate for message %s. Ignoring.\n",
1594 __FUNCTION__
, msgtypes
[c
->msgtype
]);
1600 l2tp_log (LOG_DEBUG
,
1601 "%s: avp is wrong size. %d != 8\n", __FUNCTION__
,
1603 wrong_length (c
, "Receive Window Size", 8, datalen
, 0);
1608 t
->rws
= ntohs (raw
[3].s
);
1611 if (gconfig
.debug_avp
)
1614 l2tp_log (LOG_DEBUG
,
1615 "%s: peer wants RWS of %d. Will use flow control.\n",
1616 __FUNCTION__
, t
->rws
);
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
);
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
);
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",
1660 l2tp_log (LOG_WARNING
,
1661 "%s: don't know how to handle atribute %d.\n",
1662 __FUNCTION__
, avp
->attr
);
1666 if (ALENGTH (avp
->length
) > len
)
1668 l2tp_log (LOG_WARNING
,
1669 "%s: AVP received with length > remaining packet length!\n",
1671 set_error (c
, ERROR_LENGTH
, "Invalid AVP length");
1675 if (avp
->attr
&& firstavp
)
1677 l2tp_log (LOG_WARNING
, "%s: First AVP was not message type.\n",
1679 set_error (c
, VENDOR_ERROR
, "First AVP must be message type");
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");
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");
1703 if (AHBIT (avp
->length
))
1706 l2tp_log (LOG_DEBUG
, "%s: Hidden bit set on AVP.\n", __FUNCTION__
);
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:",
1717 (AMBIT (avp
->length
) ? "mandatory " : ""));
1718 if (AMBIT (avp
->length
))
1720 set_error (c
, VENDOR_ERROR
, "Invalid Hidden AVP");
1729 avp
= (struct avp_hdr
*) data
;
1730 /* Now we should look like a normal AVP */
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
);
1750 l2tp_log (LOG_DEBUG
,
1751 "%s: Bad exit status handling attribute %d (%s).\n",
1752 __FUNCTION__
, avp
->attr
,
1753 avps
[avp
->attr
].description
);
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
);
1772 l2tp_log (LOG_WARNING
, "%s: no handler for atribute %d (%s).\n",
1773 __FUNCTION__
, avp
->attr
,
1774 avps
[avp
->attr
].description
);
1780 /* Skip over the complete length of the hidden AVP */
1781 len
-= ALENGTH (hidlen
);
1782 data
+= ALENGTH (hidlen
);
1786 len
-= ALENGTH (avp
->length
);
1787 data
+= ALENGTH (avp
->length
); /* Next AVP, please */
1789 avp
= (struct avp_hdr
*) data
;
1794 l2tp_log (LOG_WARNING
, "%s: negative overall packet length\n", __FUNCTION__
);