1 /***********************************************************************
5 * Routines for manipulating L2TP datagrams.
7 * Copyright (C) 2002 by Roaring Penguin Software Inc.
9 * This software may be distributed under the terms of the GNU General
10 * Public License, Version 2, or (at your option) any later version.
14 ***********************************************************************/
16 static char const RCSID
[] =
17 "$Id: dgram.c,v 1.1.48.1 2005/08/08 12:05:25 honor Exp $";
21 #include <sys/types.h>
22 #include <sys/socket.h>
27 #define PULL_UINT16(buf, cursor, val) \
29 val = ((uint16_t) buf[cursor]) * 256 + (uint16_t) buf[cursor+1]; \
33 #define PUSH_UINT16(buf, cursor, val) \
35 buf[cursor] = val / 256; \
36 buf[cursor+1] = val & 0xFF; \
40 #define GET_AVP_LEN(x) ((((x)[0] & 3) * 256) + (x)[1])
42 static int dgram_add_random_vector_avp(l2tp_dgram
*dgram
);
44 static void dgram_do_hide(uint16_t type
,
47 unsigned char const *secret
,
49 unsigned char const *random
,
51 unsigned char *output
);
53 static unsigned char *dgram_do_unhide(uint16_t type
,
56 unsigned char const *secret
,
58 unsigned char const *random
,
60 unsigned char *output
);
62 /* Description of AVP's indexed by AVP type */
64 char const *name
; /* AVP name */
65 int can_hide
; /* Can AVP be hidden? */
66 uint16_t min_len
; /* Minimum PAYLOAD length */
67 uint16_t max_len
; /* Maximum PAYLOAD length (9999 = no limit) */
70 static struct avp_descrip avp_table
[] = {
71 /* Name can_hide min_len max_len */
72 {"Message Type", 0, 2, 2}, /* 0 */
73 {"Result Code", 0, 2, 9999}, /* 1 */
74 {"Protocol Version", 0, 2, 2}, /* 2 */
75 {"Framing Capabilities", 1, 4, 4}, /* 3 */
76 {"Bearer Capabilities", 1, 4, 4}, /* 4 */
77 {"Tie Breaker", 0, 8, 8}, /* 5 */
78 {"Firmware Revision", 1, 2, 2}, /* 6 */
79 {"Host Name", 0, 0, 9999}, /* 7 */
80 {"Vendor Name", 1, 0, 9999}, /* 8 */
81 {"Assigned Tunnel ID", 1, 2, 2}, /* 9 */
82 {"Receive Window Size", 0, 2, 2}, /* 10 */
83 {"Challenge", 1, 0, 9999}, /* 11 */
84 {"Q.931 Cause Code", 0, 3, 9999}, /* 12 */
85 {"Challenge Response", 1, 16, 16}, /* 13 */
86 {"Assigned Session ID", 1, 2, 2}, /* 14 */
87 {"Call Serial Number", 1, 4, 4}, /* 15 */
88 {"Minimum BPS", 1, 4, 4}, /* 16 */
89 {"Maximum BPS", 1, 4, 4}, /* 17 */
90 {"Bearer Type", 1, 4, 4}, /* 18 */
91 {"Framing Type", 1, 4, 4}, /* 19 */
92 {"Unknown", 0, 0, 9999}, /* 20 */
93 {"Called Number", 1, 0, 9999}, /* 21 */
94 {"Calling Number", 1, 0, 9999}, /* 22 */
95 {"Sub-Address", 1, 0, 9999}, /* 23 */
96 {"TX Connect Speed", 1, 4, 4}, /* 24 */
97 {"Physical Channel ID", 1, 4, 4}, /* 25 */
98 {"Intial Received Confreq", 1, 0, 9999}, /* 26 */
99 {"Last Sent Confreq", 1, 0, 9999}, /* 27 */
100 {"Last Received Confreq", 1, 0, 9999}, /* 28 */
101 {"Proxy Authen Type", 1, 2, 2}, /* 29 */
102 {"Proxy Authen Name", 1, 0, 9999}, /* 30 */
103 {"Proxy Authen Challenge", 1, 0, 9999}, /* 31 */
104 {"Proxy Authen ID", 1, 2, 2}, /* 32 */
105 {"Proxy Authen Response", 1, 0, 9999}, /* 33 */
106 {"Call Errors", 1, 26, 26}, /* 34 */
107 {"ACCM", 1, 10, 10}, /* 35 */
108 {"Random Vector", 0, 0, 9999}, /* 36 */
109 {"Private Group ID", 1, 0, 9999}, /* 37 */
110 {"RX Connect Speed", 1, 4, 4}, /* 38 */
111 {"Sequencing Required", 0, 0, 0} /* 39 */
114 /* A free list of L2TP dgram strucures. Allocation of L2TP dgrams
116 static l2tp_dgram
*dgram_free_list
= NULL
;
119 describe_pulled_avp(uint16_t vendor
,
126 unsigned char *buf
= val
;
130 fprintf(stderr
, "Pulled avp: len=%d ", (int) len
);
131 if (vendor
== VENDOR_IETF
) {
132 fprintf(stderr
, "type='%s' ",avp_table
[type
].name
);
134 fprintf(stderr
, "type=%d/%d ", (int) vendor
, (int) type
);
136 fprintf(stderr
, "M=%d H=%d ", mandatory
? 1 : 0, hidden
? 1 : 0);
138 fprintf(stderr
, "val='");
139 for (i
=0; i
<len
; i
++) {
140 if (buf
[i
] < 32 || buf
[i
] > 126) {
145 for (i
=0; i
<len
; i
++) {
147 fprintf(stderr
, "%c", buf
[i
]);
149 fprintf(stderr
, "%02X ", (unsigned int) buf
[i
]);
152 fprintf(stderr
, "'\n");
155 /**********************************************************************
156 * %FUNCTION: dgram_validate_avp
158 * vendor -- vendor code
160 * len -- PAYLOAD length
161 * mandatory -- non-zero if mandatory bit is set
163 * 1 if len is valid for type, 0 otherwise.
164 ***********************************************************************/
166 l2tp_dgram_validate_avp(uint16_t vendor
,
171 if (vendor
!= VENDOR_IETF
) {
173 l2tp_set_errmsg("Unknown mandatory AVP (vendor %d, type %d)",
174 (int) vendor
, (int) type
);
181 if (type
> HIGHEST_AVP
) {
183 l2tp_set_errmsg("Unknown mandatory AVP of type %d",
190 return (len
>= avp_table
[type
].min_len
&&
191 len
<= avp_table
[type
].max_len
);
194 /**********************************************************************
195 * %FUNCTION: dgram_new
197 * len -- payload length to allocate (not currently used...)
199 * A newly-allocated l2tp_dgram structure or NULL
201 * Allocates and initializes an datagram structure.
202 ***********************************************************************/
204 l2tp_dgram_new(size_t len
)
208 if (len
> MAX_PACKET_LEN
) {
209 l2tp_set_errmsg("dgram_new: cannot allocate datagram with payload length %d",
214 if (dgram_free_list
) {
215 dgram
= dgram_free_list
;
216 dgram_free_list
= dgram
->next
;
218 dgram
= malloc(sizeof(l2tp_dgram
));
221 l2tp_set_errmsg("dgram_new: Out of memory");
232 dgram
->last_random
= 0;
233 dgram
->payload_len
= 0;
236 /* We maintain this in case it becomes dynamic later on */
237 dgram
->alloc_len
= MAX_PACKET_LEN
;
243 /**********************************************************************
244 * %FUNCTION: dgram_new_control
246 * msg_type -- message type
250 * A newly-allocated control datagram
252 * Allocates datagram; sets tid and sid fields; adds message_type AVP
253 ***********************************************************************/
255 l2tp_dgram_new_control(uint16_t msg_type
,
259 l2tp_dgram
*dgram
= l2tp_dgram_new(MAX_PACKET_LEN
);
262 if (!dgram
) return NULL
;
264 dgram
->bits
= TYPE_BIT
| LENGTH_BIT
| SEQUENCE_BIT
;
267 dgram
->msg_type
= msg_type
;
268 if (msg_type
!= MESSAGE_ZLB
) {
269 u16
= htons(msg_type
);
271 l2tp_dgram_add_avp(dgram
, NULL
, MANDATORY
,
272 sizeof(u16
), VENDOR_IETF
, AVP_MESSAGE_TYPE
, &u16
);
277 /**********************************************************************
278 * %FUNCTION: dgram_free
280 * dgram -- datagram to free
284 * Frees a datagram structure.
285 ***********************************************************************/
287 l2tp_dgram_free(l2tp_dgram
*dgram
)
290 dgram
->next
= dgram_free_list
;
291 dgram_free_list
= dgram
;
294 /**********************************************************************
295 * %FUNCTION: dgram_take_from_wire
297 * from -- set to address of peer.
299 * NULL on error, allocated datagram otherwise.
301 * Reads an L2TP datagram off the wire and puts it in dgram. Adjusts
302 * header fields to host byte order. Keeps reading in a loop unless
303 * we hit a control frame or EAGAIN. This is more efficient than
304 * returning to select loop each time if there's lots of traffic.
305 ***********************************************************************/
307 l2tp_dgram_take_from_wire(struct sockaddr_in
*from
)
309 /* EXTRA_HEADER_ROOM bytes for other headers like PPPoE, etc. */
311 unsigned char inbuf
[MAX_PACKET_LEN
+EXTRA_HEADER_ROOM
];
313 socklen_t len
= sizeof(struct sockaddr_in
);
316 unsigned char *payload
;
317 unsigned char *tidptr
;
319 uint16_t cache_tid
= 0, cache_sid
= 0;
321 l2tp_session
*ses
= NULL
;
322 int mandatory
, hidden
, err
;
323 uint16_t vendor
, type
;
327 /* Limit iterations before bailing back to select look. Otherwise,
328 we have a nice DoS possibility */
334 /* Active part of buffer */
335 buf
= inbuf
+ EXTRA_HEADER_ROOM
;
338 if (--iters
<= 0) return NULL
;
340 r
= recvfrom(Sock
, buf
, MAX_PACKET_LEN
, 0,
341 (struct sockaddr
*) from
, &len
);
346 /* Check version; drop frame if not L2TP (ver = 2) */
347 if ((buf
[1] & VERSION_MASK
) != 2) continue;
349 /* Not a data frame -- break out of loop and handle control frame */
350 if (buf
[0] & TYPE_BIT
) break;
352 /* If it's a data frame, we need to be FAST, so do not allocate
353 a dgram structure, etc; just call into handler */
356 if (buf
[0] & LENGTH_BIT
) {
357 framelen
= (((uint16_t) buf
[2]) << 8) + buf
[3];
361 if (buf
[0] & SEQUENCE_BIT
) {
364 if (buf
[0] & OFFSET_BIT
) {
366 off
= (((uint16_t) *(payload
-2)) << 8) + *(payload
-1);
369 off
= (payload
- buf
);
370 if (framelen
== -1) {
373 if (framelen
< off
|| framelen
> r
) {
376 /* Only count payload length */
380 /* Forget the 0xFF, 0x03 HDLC markers */
387 /* Get tunnel, session ID */
388 tid
= (((uint16_t) tidptr
[0]) << 8) + (uint16_t) tidptr
[1];
389 sid
= (((uint16_t) tidptr
[2]) << 8) + (uint16_t) tidptr
[3];
391 /* Only do hash lookup if it's not cached */
392 /* TODO: Is this optimization really worthwhile? */
393 if (!ses
|| tid
!= cache_tid
|| sid
!= cache_sid
) {
395 tunnel
= l2tp_tunnel_find_by_my_id(tid
);
397 l2tp_set_errmsg("Unknown tunnel %d", (int) tid
);
400 if (tunnel
->state
!= TUNNEL_ESTABLISHED
) {
401 /* Drop frame if tunnel in wrong state */
404 /* TODO: Verify source address */
405 ses
= l2tp_tunnel_find_session(tunnel
, sid
);
407 l2tp_set_errmsg("Unknown session %d in tunnel %d",
408 (int) sid
, (int) tid
);
413 if (ses
->state
!= SESSION_ESTABLISHED
) {
414 /* Drop frame if session in wrong state */
418 ses
->call_ops
->handle_ppp_frame(ses
, payload
, framelen
);
419 /* Snoop, if necessary */
421 l2tp_session_lcp_snoop(ses
, payload
, framelen
, 1);
426 /* A bit of slop on dgram size */
427 dgram
= l2tp_dgram_new(r
);
428 if (!dgram
) return NULL
;
430 dgram
->bits
= buf
[0];
431 dgram
->version
= buf
[1];
434 if (dgram
->bits
& LENGTH_BIT
) {
435 PULL_UINT16(buf
, cursor
, dgram
->length
);
436 if (dgram
->length
> r
) {
437 l2tp_set_errmsg("Invalid length field %d greater than received datagram size %d", (int) dgram
->length
, r
);
438 l2tp_dgram_free(dgram
);
445 PULL_UINT16(buf
, cursor
, dgram
->tid
);
446 PULL_UINT16(buf
, cursor
, dgram
->sid
);
447 if (dgram
->bits
& SEQUENCE_BIT
) {
448 PULL_UINT16(buf
, cursor
, dgram
->Ns
);
449 PULL_UINT16(buf
, cursor
, dgram
->Nr
);
455 if (dgram
->bits
& OFFSET_BIT
) {
456 PULL_UINT16(buf
, cursor
, dgram
->off_size
);
460 if (cursor
> dgram
->length
) {
461 l2tp_set_errmsg("Invalid length of datagram %d", (int) dgram
->length
);
462 l2tp_dgram_free(dgram
);
466 dgram
->payload_len
= dgram
->length
- cursor
;
467 memcpy(dgram
->data
, buf
+cursor
, dgram
->payload_len
);
469 /* Pull off the message type */
470 if (dgram
->bits
& OFFSET_BIT
) {
471 l2tp_set_errmsg("Invalid control frame: O bit is set");
472 l2tp_dgram_free(dgram
);
477 if (dgram
->payload_len
== 0) {
478 dgram
->msg_type
= MESSAGE_ZLB
;
481 msg
= l2tp_dgram_pull_avp(dgram
, NULL
, &mandatory
, &hidden
,
482 &avp_len
, &vendor
, &type
, &err
);
484 l2tp_dgram_free(dgram
);
487 if (type
!= AVP_MESSAGE_TYPE
|| vendor
!= VENDOR_IETF
) {
488 l2tp_set_errmsg("Invalid control message: First AVP must be message type");
489 l2tp_dgram_free(dgram
);
493 l2tp_set_errmsg("Invalid length %d for message-type AVP", (int) avp_len
+6);
494 l2tp_dgram_free(dgram
);
498 l2tp_set_errmsg("Invalid control message: M bit not set on message-type AVP");
499 l2tp_dgram_free(dgram
);
503 l2tp_set_errmsg("Invalid control message: H bit set on message-type AVP");
504 l2tp_dgram_free(dgram
);
508 dgram
->msg_type
= ((unsigned int) msg
[0]) * 256 +
509 (unsigned int) msg
[1];
511 DBG(l2tp_db(DBG_XMIT_RCV
,
512 "dgram_take_from_wire() -> %s\n",
513 l2tp_debug_describe_dgram(dgram
)));
517 /**********************************************************************
518 * %FUNCTION: dgram_send_to_wire
520 * dgram -- datagram to send
521 * to -- address to send datagram to
523 * Whatever sendto returns. 0 on success, -1 on failure.
525 * Adjusts header fields from host byte order, then sends datagram
526 ***********************************************************************/
528 l2tp_dgram_send_to_wire(l2tp_dgram
const *dgram
,
529 struct sockaddr_in
const *to
)
531 unsigned char buf
[MAX_PACKET_LEN
+128];
532 socklen_t len
= sizeof(struct sockaddr_in
);
535 unsigned char *len_ptr
= NULL
;
537 DBG(l2tp_db(DBG_XMIT_RCV
,
538 "dgram_send_to_wire(%s:%d) -> %s\n",
539 inet_ntoa(to
->sin_addr
), ntohs(to
->sin_port
),
540 l2tp_debug_describe_dgram(dgram
)));
541 buf
[0] = dgram
->bits
;
542 buf
[1] = dgram
->version
;
544 if (dgram
->bits
& LENGTH_BIT
) {
545 len_ptr
= buf
+ cursor
;
546 PUSH_UINT16(buf
, cursor
, dgram
->length
);
548 PUSH_UINT16(buf
, cursor
, dgram
->tid
);
549 PUSH_UINT16(buf
, cursor
, dgram
->sid
);
550 if (dgram
->bits
& SEQUENCE_BIT
) {
551 PUSH_UINT16(buf
, cursor
, dgram
->Ns
);
552 PUSH_UINT16(buf
, cursor
, dgram
->Nr
);
554 if (dgram
->bits
& OFFSET_BIT
) {
555 PUSH_UINT16(buf
, cursor
, dgram
->off_size
);
557 total_len
= cursor
+ dgram
->payload_len
;
558 if (dgram
->bits
& LENGTH_BIT
) {
559 *len_ptr
++ = total_len
/ 256;
560 *len_ptr
= total_len
& 255;
562 memcpy(buf
+cursor
, dgram
->data
, dgram
->payload_len
);
563 return sendto(Sock
, buf
, total_len
, 0,
564 (struct sockaddr
const *) to
, len
);
567 /**********************************************************************
568 * %FUNCTION: dgram_add_avp
570 * dgram -- the L2TP datagram
571 * tunnel -- the tunnel it will be sent on (for secret for hiding)
572 * mandatory -- if true, set the M bit
573 * len -- length of VALUE. NOT length of entire attribute!
574 * vendor -- vendor ID
575 * type -- attribute type
576 * val -- attribute value
578 * 0 on success, -1 on failure
580 * Adds an AVP to the datagram. If AVP may be hidden, and we have
581 * a secret for it, then hide the AVP.
582 ***********************************************************************/
584 l2tp_dgram_add_avp(l2tp_dgram
*dgram
,
592 static unsigned char hidden_buffer
[1024];
593 unsigned char const *random_data
;
597 /* max len is 1023 - 6 */
598 if (len
> 1023 - 6) {
599 l2tp_set_errmsg("AVP length of %d too long", (int) len
);
603 /* Do hiding where possible */
606 tunnel
->peer
->hide_avps
&&
607 vendor
== VENDOR_IETF
&&
608 tunnel
->peer
->secret_len
&&
610 avp_table
[type
].can_hide
) {
611 if (!dgram
->last_random
) {
612 /* Add a random vector */
613 if (dgram_add_random_vector_avp(dgram
) < 0) return -1;
615 /* Get pointer to random data */
616 random_data
= dgram
->data
+ dgram
->last_random
+ 6;
617 random_len
= GET_AVP_LEN(dgram
->data
+ dgram
->last_random
) - 6;
619 /* Hide the value into the buffer */
620 dgram_do_hide(type
, len
, val
, (unsigned char *) tunnel
->peer
->secret
,
621 tunnel
->peer
->secret_len
,
622 random_data
, random_len
, hidden_buffer
);
624 /* Length is increased by 2 */
630 /* Adjust from payload len to actual len */
633 /* Does datagram have room? */
634 if (dgram
->cursor
+ len
> dgram
->alloc_len
) {
635 l2tp_set_errmsg("No room for AVP of length %d", (int) len
);
639 dgram
->data
[dgram
->cursor
] = 0;
641 dgram
->data
[dgram
->cursor
] |= AVP_MANDATORY_BIT
;
644 dgram
->data
[dgram
->cursor
] |= AVP_HIDDEN_BIT
;
647 dgram
->data
[dgram
->cursor
] |= (len
>> 8);
648 dgram
->data
[dgram
->cursor
+1] = (len
& 0xFF);
649 dgram
->data
[dgram
->cursor
+2] = (vendor
>> 8);
650 dgram
->data
[dgram
->cursor
+3] = (vendor
& 0xFF);
651 dgram
->data
[dgram
->cursor
+4] = (type
>> 8);
652 dgram
->data
[dgram
->cursor
+5] = (type
& 0xFF);
655 memcpy(dgram
->data
+ dgram
->cursor
+ 6, val
, len
-6);
657 if (type
== AVP_RANDOM_VECTOR
) {
658 /* Remember location of random vector */
659 dgram
->last_random
= dgram
->cursor
;
661 dgram
->cursor
+= len
;
662 dgram
->payload_len
= dgram
->cursor
;
666 /**********************************************************************
667 * %FUNCTION: dgram_pull_avp
669 * dgram -- the L2TP datagram
670 * tunnel -- the tunnel it was received on (for secret for hiding)
671 * mandatory -- if set to true, the M bit was set.
672 * hidden -- if set to true, the value was hidden
673 * len -- set to length of value (after unhiding)
674 * vendor -- set to vendor ID
675 * type -- set to attribute type
676 * err -- set to true if an error occurs, false if not.
678 * A pointer to a buffer containing the value, or NULL if an
679 * error occurs or there are no more AVPs.
681 * Pulls an AVP out of a received datagram.
682 ***********************************************************************/
684 l2tp_dgram_pull_avp(l2tp_dgram
*dgram
,
693 static unsigned char val
[1024];
695 unsigned char *random_data
;
701 if (dgram
->cursor
>= dgram
->payload_len
) {
709 mandatory
= &local_mandatory
;
711 *mandatory
= dgram
->data
[dgram
->cursor
] & AVP_MANDATORY_BIT
;
714 hidden
= &local_hidden
;
716 *hidden
= dgram
->data
[dgram
->cursor
] & AVP_HIDDEN_BIT
;
717 if (dgram
->data
[dgram
->cursor
] & AVP_RESERVED_BITS
) {
718 l2tp_set_errmsg("AVP with reserved bits set to non-zero");
723 *len
= ((unsigned int) (dgram
->data
[dgram
->cursor
] & 3)) * 256 +
724 dgram
->data
[dgram
->cursor
+1];
726 l2tp_set_errmsg("Received AVP of length %d (too short)", (int) *len
);
730 if (dgram
->cursor
+ *len
> dgram
->payload_len
) {
731 l2tp_set_errmsg("Received AVP of length %d too long for rest of datagram",
737 PULL_UINT16(dgram
->data
, dgram
->cursor
, *vendor
);
738 PULL_UINT16(dgram
->data
, dgram
->cursor
, *type
);
740 /* If we see a random vector, remember it */
741 if (*vendor
== VENDOR_IETF
&& *type
== AVP_RANDOM_VECTOR
) {
743 l2tp_set_errmsg("Invalid random vector AVP has H bit set");
746 dgram
->last_random
= dgram
->cursor
- 6;
749 ans
= dgram
->data
+ dgram
->cursor
;
750 dgram
->cursor
+= *len
- 6;
753 l2tp_set_errmsg("AVP cannot be hidden");
757 l2tp_set_errmsg("Received hidden AVP of length %d (too short)",
763 l2tp_set_errmsg("No peer??");
766 if (!tunnel
->peer
->secret_len
) {
767 l2tp_set_errmsg("No shared secret to unhide AVP");
770 if (!dgram
->last_random
) {
771 l2tp_set_errmsg("Cannot unhide AVP unless Random Vector received first");
775 if (*type
<= HIGHEST_AVP
) {
776 if (!avp_table
[*type
].can_hide
) {
777 l2tp_set_errmsg("AVP of type %s cannot be hidden, but H bit set",
778 avp_table
[*type
].name
);
783 /* Get pointer to random data */
784 random_data
= dgram
->data
+ dgram
->last_random
+ 6;
785 random_len
= GET_AVP_LEN(dgram
->data
+ dgram
->last_random
) - 6;
788 ans
= dgram_do_unhide(*type
, len
, ans
,
789 (unsigned char *) tunnel
->peer
->secret
,
790 tunnel
->peer
->secret_len
,
791 random_data
, random_len
, val
);
792 if (!ans
) return NULL
;
795 /* Set len to length of value only */
799 /* DBG(describe_pulled_avp(*vendor, *type, *len, *hidden, *mandatory, ans)); */
804 /**********************************************************************
805 * %FUNCTION: dgram_do_hide
807 * type -- attribute type
808 * len -- attribute length
809 * value -- attribute value
810 * secret -- shared tunnel secret
811 * secret_len -- length of secret
812 * random -- random data
813 * random_len -- length of random data
814 * output -- where to put result
818 * Hides AVP into output as described in RFC2661. Does not do
819 * padding (should we?)
820 ***********************************************************************/
822 dgram_do_hide(uint16_t type
,
824 unsigned char *value
,
825 unsigned char const *secret
,
827 unsigned char const *random_data
,
829 unsigned char *output
)
831 struct MD5Context ctx
;
833 unsigned char digest
[16];
837 /* Put type in network byte order */
841 /* Compute initial pad */
843 MD5Update(&ctx
, t
, 2);
844 MD5Update(&ctx
, secret
, secret_len
);
845 MD5Update(&ctx
, random_data
, random_len
);
846 MD5Final(digest
, &ctx
);
849 output
[0] = digest
[0] ^ (len
/ 256);
850 output
[1] = digest
[1] ^ (len
& 255);
856 *output
= digest
[done
] ^ *value
;
861 if (done
== 16 && todo
) {
862 /* Compute new digest */
865 MD5Update(&ctx
, secret
, secret_len
);
866 MD5Update(&ctx
, output
-16, 16);
867 MD5Final(digest
, &ctx
);
872 /**********************************************************************
873 * %FUNCTION: dgram_do_unhide
875 * type -- attribute type
876 * len -- attribute length (input = orig len; output = unhidden len)
877 * value -- attribute value (the hidden characters)
878 * secret -- shared tunnel secret
879 * secret_len -- length of secret
880 * random_data -- random data
881 * random_len -- length of random data
882 * output -- where to put result
884 * pointer to "output" on success; NULL on failure.
886 * Un-hides AVP as in RFC2661
887 ***********************************************************************/
888 static unsigned char *
889 dgram_do_unhide(uint16_t type
,
891 unsigned char *value
,
892 unsigned char const *secret
,
894 unsigned char const *random_data
,
896 unsigned char *output
)
898 struct MD5Context ctx
;
900 unsigned char digest
[16];
902 unsigned char *orig_output
= output
;
905 /* Put type in network byte order */
909 /* Compute initial pad */
911 MD5Update(&ctx
, t
, 2);
912 MD5Update(&ctx
, secret
, secret_len
);
913 MD5Update(&ctx
, random_data
, random_len
);
914 MD5Final(digest
, &ctx
);
916 /* Get hidden length */
918 ((uint16_t) (digest
[0] ^ value
[0])) * 256 +
919 (uint16_t) (digest
[1] ^ value
[1]);
923 if (tmplen
> *len
-8) {
924 l2tp_set_errmsg("Hidden length %d too long in AVP of length %d",
925 (int) tmplen
, (int) *len
);
929 /* Adjust len. Add 6 to compensate for pseudo-header */
932 /* Decrypt remainder */
936 *output
= digest
[done
] ^ *value
;
941 if (done
== 16 && todo
) {
942 /* Compute new digest */
945 MD5Update(&ctx
, secret
, secret_len
);
946 MD5Update(&ctx
, value
-16, 16);
947 MD5Final(digest
, &ctx
);
953 /**********************************************************************
954 * %FUNCTION: dgram_search_avp
956 * dgram -- the datagram
957 * tunnel -- associated tunnel
958 * mandatory -- set to 1 if M bit was set
959 * hidden -- set to 1 if H bit was set
960 * len -- set to length of payload
961 * vendor -- vendor ID we want
962 * type -- AVP type we want
964 * A pointer to the AVP value if found, NULL if not
966 * Searches dgram for specific AVP type.
967 ***********************************************************************/
969 l2tp_dgram_search_avp(l2tp_dgram
*dgram
,
977 uint16_t svend
, stype
;
980 size_t cursor
= dgram
->cursor
;
981 size_t last_random
= dgram
->last_random
;
983 val
= l2tp_dgram_pull_avp(dgram
, tunnel
, mandatory
, hidden
, len
,
984 &svend
, &stype
, &err
);
987 l2tp_set_errmsg("AVP of vendor/type (%d, %d) not found",
988 (int) vendor
, (int) type
);
990 dgram
->cursor
= cursor
;
991 dgram
->last_random
= last_random
;
994 if (vendor
== svend
&& type
== stype
) break;
996 dgram
->cursor
= cursor
;
997 dgram
->last_random
= last_random
;
1001 /**********************************************************************
1002 * %FUNCTION: dgram_send_ppp_frame
1004 * ses -- the session
1005 * buf -- PPP frame. BUF MUST HAVE AT LEAST 16 BYTES OF SPACE AHEAD OF IT!
1006 * len -- length of PPP frame
1008 * Whatever sendto returns
1010 * Sends a PPP frame. TODO: Implement sequence numbers if required.
1011 ***********************************************************************/
1013 l2tp_dgram_send_ppp_frame(l2tp_session
*ses
,
1014 unsigned char const *buf
,
1018 unsigned char *real_buf
= (unsigned char *) buf
- 8;
1019 l2tp_tunnel
*tunnel
= ses
->tunnel
;
1021 /* Drop frame if tunnel and/or session in wrong state */
1023 tunnel
->state
!= TUNNEL_ESTABLISHED
||
1024 ses
->state
!= SESSION_ESTABLISHED
) {
1028 /* If there is a pending ack on the tunnel, cancel and ack now */
1029 if (tunnel
->ack_handler
) {
1030 Event_DelHandler(tunnel
->es
, tunnel
->ack_handler
);
1031 tunnel
->ack_handler
= NULL
;
1032 tunnel_send_ZLB(tunnel
);
1037 real_buf
[2] = (tunnel
->assigned_id
>> 8);
1038 real_buf
[3] = tunnel
->assigned_id
& 0xFF;
1039 real_buf
[4] = ses
->assigned_id
>> 8;
1040 real_buf
[5] = ses
->assigned_id
& 0xFF;
1041 real_buf
[6] = 0xFF; /* HDLC address */
1042 real_buf
[7] = 0x03; /* HDLC control */
1043 r
= sendto(Sock
, real_buf
, len
+8, 0,
1044 (struct sockaddr
const *) &tunnel
->peer_addr
,
1045 sizeof(struct sockaddr_in
));
1046 /* Snoop, if necessary */
1047 if (ses
->snooping
) {
1048 l2tp_session_lcp_snoop(ses
, buf
, len
, 0);
1053 /**********************************************************************
1054 * %FUNCTION: dgram_add_random_vector_avp
1058 * 0 on success; -1 on failure
1060 * Adds a random-vector AVP to the datagram
1061 ***********************************************************************/
1063 dgram_add_random_vector_avp(l2tp_dgram
*dgram
)
1065 unsigned char buf
[16];
1067 l2tp_random_fill(buf
, sizeof(buf
));
1069 return l2tp_dgram_add_avp(dgram
, NULL
, MANDATORY
, sizeof(buf
),
1070 VENDOR_IETF
, AVP_RANDOM_VECTOR
, buf
);