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 * Authorization, Accounting, and Access control
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
24 extern void bufferDump (char *, int);
26 /* FIXME: Accounting? */
28 struct addr_ent
*uaddr
[ADDR_HASH_SIZE
];
33 for (x
= 0; x
< ADDR_HASH_SIZE
; x
++)
37 static int ip_used (unsigned int addr
)
40 tmp
= uaddr
[addr
% ADDR_HASH_SIZE
];
43 if (tmp
->addr
== addr
)
50 void mk_challenge (unsigned char *c
, int length
)
52 get_entropy(c
, length
);
56 for (x = 0; x < length / sizeof (int); x++)
60 void reserve_addr (unsigned int addr
)
62 /* Mark this address as in use */
63 struct addr_ent
*tmp
, *tmp2
;
67 tmp
= uaddr
[addr
% ADDR_HASH_SIZE
];
68 tmp2
= (struct addr_ent
*) malloc (sizeof (struct addr_ent
));
69 uaddr
[addr
% ADDR_HASH_SIZE
] = tmp2
;
74 void unreserve_addr (unsigned int addr
)
76 struct addr_ent
*tmp
, *last
= NULL
, *z
;
78 tmp
= uaddr
[addr
% ADDR_HASH_SIZE
];
81 if (tmp
->addr
== addr
)
85 last
->next
= tmp
->next
;
89 uaddr
[addr
% ADDR_HASH_SIZE
] = tmp
->next
;
103 unsigned int get_addr (struct iprange
*ipr
)
107 struct iprange
*ipr2
;
110 if (ipr
->sense
== SENSE_ALLOW
)
111 for (x
= ntohl (ipr
->start
); x
<= ntohl (ipr
->end
); x
++)
113 /* Found an IP in an ALLOW range, check to be sure it is
114 consistant through the remaining regions */
117 status
= SENSE_ALLOW
;
121 if ((x
>= ntohl (ipr2
->start
))
122 && (x
<= ntohl (ipr2
->end
)))
123 status
= ipr2
->sense
;
127 if (status
== SENSE_ALLOW
)
136 static int get_secret (char *us
, char *them
, unsigned char *secret
, int size
)
142 f
= fopen (gconfig
.authfile
, "r");
145 l2tp_log (LOG_WARNING
, "%s : Unable to open '%s' for authentication\n",
146 __FUNCTION__
, gconfig
.authfile
);
152 if (NULL
== fgets (buf
, sizeof (buf
), f
))
158 for (t
= buf
; *t
; t
++)
159 *t
= ((*t
== '#') || (*t
== ';')) ? 0 : *t
;
160 /* Strip trailing whitespace */
161 for (t
= buf
+ strlen (buf
) - 1; (t
>= buf
) && (*t
< 33); t
--)
164 continue; /* Empty line */
166 while (*u
&& (*u
< 33))
171 l2tp_log (LOG_WARNING
,
172 "%s: Invalid authentication info (no us), line %d\n",
180 while (*t
&& (*t
< 33))
185 l2tp_log (LOG_WARNING
,
186 "%s: Invalid authentication info (nothem), line %d\n",
194 while (*s
&& (*s
< 33))
198 l2tp_log (LOG_WARNING
,
199 "%s: Invalid authentication info (no secret), line %d\n",
203 if ((!strcasecmp (u
, us
) || !strcasecmp (u
, "*")) &&
204 (!strcasecmp (t
, them
) || !strcasecmp (t
, "*")))
208 "%s: we are '%s', they are '%s', secret is '%s'\n",
209 __FUNCTION__
, u
, t
, s
);
211 strncpy ((char *)secret
, s
, size
);
220 int handle_challenge (struct tunnel
*t
, struct challenge
*chal
)
224 if (!t
->lns
&& !t
->lac
)
226 l2tp_log (LOG_DEBUG
, "%s: No LNS or LAC to handle challenge!\n",
231 l2tp_log (LOG_DEBUG
, "%s: making response for tunnel: %d\n", __FUNCTION__
,
236 if (t
->lns
->hostname
[0])
237 us
= t
->lns
->hostname
;
240 if (t
->lns
->peername
[0])
241 them
= t
->lns
->peername
;
247 if (t
->lac
->hostname
[0])
248 us
= t
->lac
->hostname
;
251 if (t
->lac
->peername
[0])
252 them
= t
->lac
->peername
;
257 if (!get_secret (us
, them
, chal
->secret
, sizeof (chal
->secret
)))
259 l2tp_log (LOG_DEBUG
, "%s: no secret found for us='%s' and them='%s'\n",
260 __FUNCTION__
, us
, them
);
265 l2tp_log (LOG_DEBUG
, "*%s: Here comes the chal->ss:\n", __FUNCTION__
);
266 bufferDump (&chal
->ss
, 1);
268 l2tp_log (LOG_DEBUG
, "%s: Here comes the secret\n", __FUNCTION__
);
269 bufferDump (chal
->secret
, strlen (chal
->secret
));
271 l2tp_log (LOG_DEBUG
, "%s: Here comes the challenge\n", __FUNCTION__
);
272 bufferDump (chal
->challenge
, chal
->chal_len
);
275 memset (chal
->response
, 0, MD_SIG_SIZE
);
276 MD5Init (&chal
->md5
);
277 MD5Update (&chal
->md5
, &chal
->ss
, 1);
278 MD5Update (&chal
->md5
, chal
->secret
, strlen ((char *)chal
->secret
));
279 MD5Update (&chal
->md5
, chal
->challenge
, chal
->chal_len
);
280 MD5Final (chal
->response
, &chal
->md5
);
282 l2tp_log (LOG_DEBUG
, "response is %X%X%X%X to '%s' and %X%X%X%X, %d\n",
283 *((int *) &chal
->response
[0]),
284 *((int *) &chal
->response
[4]),
285 *((int *) &chal
->response
[8]),
286 *((int *) &chal
->response
[12]),
288 *((int *) &chal
->challenge
[0]),
289 *((int *) &chal
->challenge
[4]),
290 *((int *) &chal
->challenge
[8]),
291 *((int *) &chal
->challenge
[12]), chal
->ss
);
293 chal
->state
= STATE_CHALLENGED
;
297 struct lns
*get_lns (struct tunnel
*t
)
300 * Look through our list of LNS's and
301 * find a reasonable LNS for this call
302 * if one is available
306 int allow
, checkdefault
= 0;
307 /* If access control is disabled, we give the default
308 otherwise, we give nothing */
321 if ((ntohl (t
->peer
.sin_addr
.s_addr
) >= ntohl (ipr
->start
)) &&
322 (ntohl (t
->peer
.sin_addr
.s_addr
) <= ntohl (ipr
->end
)))
326 "$s: Rule %s to %s, sense %s matched %s\n", __FUNCTION__
,
327 IPADDY (ipr
->start
), IPADDY (ipr
->end
),
328 (ipr
->sense
? "allow" : "deny"), IPADDY (t
->peer
.sin_addr
.s_addr
));
337 if (!lns
&& !checkdefault
)
343 if (gconfig
.accesscontrol
)
350 static void print_md5 (void * const md5
)
352 int *i
= (int *) md5
;
353 l2tp_log (LOG_DEBUG
, "%X%X%X%X\n", i
[0], i
[1], i
[2], i
[3], i
[4]);
356 static inline void print_challenge (struct challenge
*chal
)
358 l2tp_log (LOG_DEBUG
, "vector: ");
359 print_md5 (chal
->vector
);
360 l2tp_log (LOG_DEBUG
, "secret: %s\n", chal
->secret
);
363 void encrypt_avp (struct buffer
*buf
, _u16 len
, struct tunnel
*t
)
365 /* Encrypts an AVP of len, at data. We assume there
366 are two "spare bytes" before the data pointer,l but otherwise
367 this is just a normal AVP that is about to be returned from
368 an avpsend routine */
369 struct avp_hdr
*new_hdr
=
370 (struct avp_hdr
*) (buf
->start
+ buf
->len
- len
);
371 struct avp_hdr
*old_hdr
=
372 (struct avp_hdr
*) (buf
->start
+ buf
->len
- len
+ 2);
373 _u16 length
, flags
, attr
; /* New length, old flags */
374 unsigned char *ptr
, *end
;
376 unsigned char digest
[MD_SIG_SIZE
];
377 unsigned char *previous_segment
;
379 /* FIXME: Should I pad more randomly? Right now I pad to nearest 16 bytes */
381 ((len
- sizeof (struct avp_hdr
) + 1) / 16 + 1) * 16 +
382 sizeof (struct avp_hdr
);
383 flags
= htons (old_hdr
->length
) & 0xF000;
384 new_hdr
->length
= htons (length
| flags
| HBIT
);
385 new_hdr
->vendorid
= old_hdr
->vendorid
;
386 new_hdr
->attr
= attr
= old_hdr
->attr
;
387 /* This is really the length field of the hidden sub-format */
388 old_hdr
->attr
= htons (len
- sizeof (struct avp_hdr
));
389 /* Okay, now we've rewritten the header, as it should be. Let's start
390 encrypting the actual data now */
393 /* Back to the beginning of real data, including the original length AVP */
395 MD5Init (&t
->chal_them
.md5
);
396 MD5Update (&t
->chal_them
.md5
, (void *) &attr
, 2);
397 MD5Update (&t
->chal_them
.md5
, t
->chal_them
.secret
,
398 strlen ((char *)t
->chal_them
.secret
));
399 MD5Update (&t
->chal_them
.md5
, t
->chal_them
.vector
, VECTOR_SIZE
);
400 MD5Final (digest
, &t
->chal_them
.md5
);
402 /* Though not a "MUST" in the spec, our subformat length is always a multiple of 16 */
403 ptr
= ((unsigned char *) new_hdr
) + sizeof (struct avp_hdr
);
404 end
= ((unsigned char *) new_hdr
) + length
;
405 previous_segment
= ptr
;
409 l2tp_log (LOG_DEBUG
, "%s: The digest to be XOR'ed\n", __FUNCTION__
);
410 bufferDump (digest
, MD_SIG_SIZE
);
411 l2tp_log (LOG_DEBUG
, "%s: The plaintext to be XOR'ed\n", __FUNCTION__
);
412 bufferDump (ptr
, MD_SIG_SIZE
);
414 for (cnt
= 0; cnt
< MD_SIG_SIZE
; cnt
++, ptr
++)
416 *ptr
= *ptr
^ digest
[cnt
];
419 l2tp_log (LOG_DEBUG
, "%s: The result of XOR\n", __FUNCTION__
);
420 bufferDump (previous_segment
, MD_SIG_SIZE
);
424 MD5Init (&t
->chal_them
.md5
);
425 MD5Update (&t
->chal_them
.md5
, t
->chal_them
.secret
,
426 strlen ((char *)t
->chal_them
.secret
));
427 MD5Update (&t
->chal_them
.md5
, previous_segment
, MD_SIG_SIZE
);
428 MD5Final (digest
, &t
->chal_them
.md5
);
430 previous_segment
= ptr
;
434 int decrypt_avp (char *buf
, struct tunnel
*t
)
436 /* Decrypts a hidden AVP pointed to by buf. The
437 new header will be exptected to be two characters
438 offset from the old */
440 int len
, olen
, flags
;
441 unsigned char digest
[MD_SIG_SIZE
];
444 struct avp_hdr
*old_hdr
= (struct avp_hdr
*) buf
;
445 struct avp_hdr
*new_hdr
= (struct avp_hdr
*) (buf
+ 2);
446 int saved_segment_len
; /* maybe less 16; may be used if the cipher is longer than 16 octets */
447 unsigned char saved_segment
[MD_SIG_SIZE
];
448 ptr
= ((char *) old_hdr
) + sizeof (struct avp_hdr
);
449 olen
= old_hdr
->length
& 0x0FFF;
451 if (!t
->chal_us
.vector
)
454 "%s: Hidden bit set, but no random vector specified!\n", __FUNCTION__
);
457 /* First, let's decrypt all the data. We're not guaranteed
458 that it will be padded to a 16 byte boundary, so we
459 have to be more careful than when encrypting */
460 attr
= ntohs (old_hdr
->attr
);
461 MD5Init (&t
->chal_us
.md5
);
462 MD5Update (&t
->chal_us
.md5
, (void *) &attr
, 2);
463 MD5Update (&t
->chal_us
.md5
, t
->chal_us
.secret
,
464 strlen ((char *)t
->chal_us
.secret
));
465 MD5Update (&t
->chal_us
.md5
, t
->chal_us
.vector
, t
->chal_us
.vector_len
);
466 MD5Final (digest
, &t
->chal_us
.md5
);
468 l2tp_log (LOG_DEBUG
, "attribute is %d and challenge is: ", attr
);
469 print_challenge (&t
->chal_us
);
470 l2tp_log (LOG_DEBUG
, "md5 is: ");
475 if (cnt
>= MD_SIG_SIZE
)
477 MD5Init (&t
->chal_us
.md5
);
478 MD5Update (&t
->chal_us
.md5
, t
->chal_us
.secret
,
479 strlen ((char *)t
->chal_us
.secret
));
480 MD5Update (&t
->chal_us
.md5
, saved_segment
, MD_SIG_SIZE
);
481 MD5Final (digest
, &t
->chal_us
.md5
);
484 /* at the beginning of each segment, we save the current segment (16 octets or less) of cipher
485 * so that the next round of MD5 (if there is a next round) hash could use it
490 (end
- ptr
< MD_SIG_SIZE
) ? (end
- ptr
) : MD_SIG_SIZE
;
491 memcpy (saved_segment
, ptr
, saved_segment_len
);
493 *ptr
= *ptr
^ digest
[cnt
++];
496 /* Hopefully we're all nice and decrypted now. Let's rewrite the header.
497 First save the old flags, and get the new stuff */
498 flags
= old_hdr
->length
& 0xF000 & ~HBIT
;
499 len
= ntohs (new_hdr
->attr
) + sizeof (struct avp_hdr
);
503 "%s: Decrypted length is too long (%d > %d)\n", __FUNCTION__
, len
,
507 new_hdr
->attr
= old_hdr
->attr
;
508 new_hdr
->vendorid
= old_hdr
->vendorid
;
509 new_hdr
->length
= len
| flags
;